1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #ifndef _CA_ADAPTER_NET_DTLS_H
21 #define _CA_ADAPTER_NET_DTLS_H
24 #include "uarraylist.h"
26 #include "caadapterutils.h"
27 #include "ocsecurityconfig.h"
28 #include "cainterface.h"
31 * Currently DTLS supported adapters(2) WIFI and ETHENET for linux platform.
33 #define MAX_SUPPORTED_ADAPTERS 2
36 * @brief The implementation will be provided by OIC RI layer.
38 extern void OCGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo);
40 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint16_t port,
41 const void *data, const uint32_t dataLength, const bool isSecured);
43 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint16_t port,
44 const void *data, const uint32_t dataLength);
47 * @struct stCAAdapterCallbacks_t
48 * @brief Data structure for holding the send and recv callbacks.
50 typedef struct CAAdapterCallbacks
52 CAPacketReceivedCallback recvCallback;
53 CAPacketSendCallback sendCallback;
54 } stCAAdapterCallbacks_t;
57 * @struct stCADtlsContext_t
58 * @brief Data structure for holding the tinyDTLS interface
61 typedef struct stCADtlsContext
63 u_arraylist_t *cacheList; /**< pdu's are cached until DTLS session is formed */
64 struct dtls_context_t *dtlsContext; /**< pointer to tinyDTLS context */
65 struct stPacketInfo *packetInfo; /**< used by callback during
66 decryption to hold address/length */
67 dtls_handler_t callbacks; /**< pointer to callbacks needed by tinyDTLS */
68 stCAAdapterCallbacks_t adapterCallbacks[MAX_SUPPORTED_ADAPTERS];
72 * @struct stPacketInfo_t
73 * @brief Data structure for holding the decrypted data address
74 * and length provided by tinyDTLS callback interface.
76 typedef struct stPacketInfo
84 * @brief tinyDTLS library error codes.
91 DTLS_SESSION_INITIATED,
96 * @struct stGattServiceInfo_t
97 * @brief structure to have address information.
102 socklen_t size; /**< size of addr */
106 struct sockaddr_storage st;
107 struct sockaddr_in sin;
108 struct sockaddr_in6 sin6;
111 } stCADtlsAddrInfo_t;
114 * @struct stCACacheMessage_t
115 * @brief structure to holds the information of cachemessage and address info.
118 typedef struct CACacheMessage
122 stCADtlsAddrInfo_t *destSession;
123 } stCACacheMessage_t;
126 * @enum eDtlsAdapterType_t
127 * @brief This enum is used as array index for storing adapter level callbacks.
128 * So Keeping 0 instead of "1 << 0". It is not going to be used as addition
129 * and removal of adapter.
136 } eDtlsAdapterType_t;
139 * @fn CADTLSSetAdapterCallbacks
140 * @brief Used set send and recv callbacks for different adapters(WIFI,EtherNet)
142 * @param[in] recvCallback packet received callback
143 * @param[in] sendCallback packet sent callback
144 * @param[in] type type of adapter
149 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
150 CAPacketSendCallback sendCallback, eDtlsAdapterType_t type);
153 * @brief Register callback to get DTLS PSK credentials.
154 * @param credCallback [IN] callback to get DTLS credentials
157 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
160 * @fn CAAdapterNetDtlsInit
161 * @brief initialize tinyDTLS library and other necessary intialization.
163 * @return 0 on success otherwise a positive error value.
164 * @retval CA_STATUS_OK Successful
165 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
166 * @retval CA_STATUS_FAILED Operation failed
169 CAResult_t CAAdapterNetDtlsInit();
172 * @fn CAAdapterNetDtlsDeInit
173 * @brief de-inits tinyDTLS library and free the allocated memory.
178 void CAAdapterNetDtlsDeInit();
181 * @fn CAAdapterNetDtlsEncrypt
182 * @brief Performs DTLS encryption of the CoAP PDU. If a
183 * DTLS session does not exist yet with the @dst,
184 * a DTLS handshake will be started. In case where
185 * a new DTLS handshake is started, pdu info is
186 * cached to be send when session setup is finished.
188 * @param[in] remoteAddress address to which data will be sent.
189 * @param[in] port port to which data will be sent.
190 * @param[in] data length of data.
191 * @param[in] dataLen length of given data
192 * @param[out] decdata output variable to store the starting address
193 * of decrypted plaintext.
194 * @param[out] cacheFlag utput variable to indicate if pdu
195 * is cached and inform the caller to
196 * NOT free the memory holding pdu.
197 * @return 0 on success otherwise a positive error value.
198 * @retval CA_STATUS_OK Successful
199 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
200 * @retval CA_STATUS_FAILED Operation failed
204 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
209 eDtlsAdapterType_t type);
212 * @fn CAAdapterNetDtlsDecrypt
213 * @brief Performs DTLS decryption of the data received on
214 * secure port. This method performs in-place decryption
215 * of the cipher-text buffer. If a DTLS handshake message
216 * is received or decryption failure happens, this method
217 * returns -1. If a valid application PDU is decrypted, it
218 * returns the length of the decrypted pdu.
220 * @return 0 on success otherwise a positive error value.
221 * @retval CA_STATUS_OK Successful
222 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
223 * @retval CA_STATUS_FAILED Operation failed
226 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
230 eDtlsAdapterType_t type);
232 #endif //_CA_ADAPTER_NET_DTLS_H