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"
32 * Currently DTLS supported adapters(2) WIFI and ETHENET for linux platform.
34 #define MAX_SUPPORTED_ADAPTERS 2
37 * @brief The implementation will be provided by OIC RI layer.
39 extern void OCGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo);
41 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint16_t port,
42 const void *data, const uint32_t dataLength,
43 const bool isSecured, const CARemoteId_t *identity);
45 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint16_t port,
46 const void *data, const uint32_t dataLength);
49 * @struct stCAAdapterCallbacks_t
50 * @brief Data structure for holding the send and recv callbacks.
52 typedef struct CAAdapterCallbacks
54 CAPacketReceivedCallback recvCallback; /**< Callback used to send data to upper layer. */
55 CAPacketSendCallback sendCallback; /**< Callback used to send data to socket layer. */
56 } stCAAdapterCallbacks_t;
59 * @struct stCADtlsContext_t
60 * @brief Data structure for holding the tinyDTLS interface
63 typedef struct stCADtlsContext
65 u_arraylist_t *peerInfoList; /**< peerInfo list which holds the mapping between
66 peer id to it's n/w address */
67 u_arraylist_t *cacheList; /**< PDU's are cached until DTLS session is formed. */
68 struct dtls_context_t *dtlsContext; /**< Pointer to tinyDTLS context. */
69 struct stPacketInfo *packetInfo; /**< used by callback during
70 decryption to hold address/length. */
71 dtls_handler_t callbacks; /**< Pointer to callbacks needed by tinyDTLS. */
72 stCAAdapterCallbacks_t adapterCallbacks[MAX_SUPPORTED_ADAPTERS];
76 * @struct stPacketInfo_t
77 * @brief Data structure for holding the decrypted data address
78 * and length provided by tinyDTLS callback interface.
80 typedef struct stPacketInfo
88 * @brief tinyDTLS library error codes.
95 DTLS_SESSION_INITIATED,
100 /** Structure to have address information which will match with DTLS session_t struct.*/
103 socklen_t size; /**< Size of address. */
107 struct sockaddr_storage st;
108 struct sockaddr_in sin;
109 struct sockaddr_in6 sin6;
110 } addr; /**< Address information. */
111 uint8_t ifIndex; /**< Holds adpater index to get callback info. */
112 } stCADtlsAddrInfo_t;
115 * @struct stCACacheMessage_t
116 * @brief structure to holds the information of cachemessage and address info.
119 typedef struct CACacheMessage
123 stCADtlsAddrInfo_t destSession;
124 } stCACacheMessage_t;
128 * @struct stCADtlsPeerInfo_t
129 * @brief structure associates the peer psk id with peer n/w address
131 typedef struct stCADtlsPeerInfo
134 CARemoteId_t identity;
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, CATransportType_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 * Select the cipher suite for dtls handshake
162 * @param[in] cipher cipher suite
163 * 0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA
164 * 0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
165 * 0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
167 * @retval CA_STATUS_OK for success, otherwise some error value
169 CAResult_t CADtlsSelectCipherSuite(const dtls_cipher_t cipher);
172 * Enable anonymous ECDH cipher suite for dtls handshake
174 * @param[in] enable TRUE/FALSE enables/disables anonymous cipher suite
176 * @retval CA_STATUS_OK for success, otherwise some error value
178 CAResult_t CADtlsEnableAnonECDHCipherSuite(const bool enable);
181 * Initiate DTLS handshake with selected cipher suite
183 * @param[in] addrInfo information of network address
184 * @param[in] transportType transport type
186 * @retval CA_STATUS_OK for success, otherwise some error value
188 CAResult_t CADtlsInitiateHandshake(const CAAddress_t* addrInfo,
189 const CATransportType_t transportType);
192 * Close the DTLS session
194 * @param[in] addrInfo information of network address
195 * @param[in] transportType transport type
197 * @retval CA_STATUS_OK for success, otherwise some error value
199 CAResult_t CADtlsClose(const CAAddress_t* addrInfo,
200 const CATransportType_t transportType);
203 * Generate ownerPSK using PRF
204 * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
205 * 'ID of new device(Resource Server)',
206 * 'ID of owner smart-phone(Provisioning Server)')
208 * @param[in] addrInfo information of network address
209 * @param[in] transportType transport type
210 * @param[in] label Ownership transfer method e.g)"oic.sec.doxm.jw"
211 * @param[in] labelLen Byte length of label
212 * @param[in] rsrcServerDeviceID ID of new device(Resource Server)
213 * @param[in] rsrcServerDeviceIDLen Byte length of rsrcServerDeviceID
214 * @param[in] provServerDeviceID label of previous owner
215 * @param[in] provServerDeviceIDLen byte length of provServerDeviceID
216 * @param[in,out] ownerPSK Output buffer for owner PSK
217 * @param[in] ownerPSKSize Byte length of the ownerPSK to be generated
219 * @retval CA_STATUS_OK for success, otherwise some error value
221 CAResult_t CADtlsGenerateOwnerPSK(const CAAddress_t* addrInfo,
222 const CATransportType_t transportType,
223 const uint8_t* label, const size_t labelLen,
224 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
225 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
226 uint8_t* ownerPSK, const size_t ownerPSKSize);
230 * @fn CAAdapterNetDtlsInit
231 * @brief initialize tinyDTLS library and other necessary intialization.
233 * @return 0 on success otherwise a positive error value.
234 * @retval CA_STATUS_OK Successful
235 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
236 * @retval CA_STATUS_FAILED Operation failed
239 CAResult_t CAAdapterNetDtlsInit();
242 * @fn CAAdapterNetDtlsDeInit
243 * @brief de-inits tinyDTLS library and free the allocated memory.
248 void CAAdapterNetDtlsDeInit();
251 * @fn CAAdapterNetDtlsEncrypt
252 * @brief Performs DTLS encryption of the CoAP PDU. If a
253 * DTLS session does not exist yet with the @dst,
254 * a DTLS handshake will be started. In case where
255 * a new DTLS handshake is started, pdu info is
256 * cached to be send when session setup is finished.
258 * @param[in] remoteAddress address to which data will be sent.
259 * @param[in] port port to which data will be sent.
260 * @param[in] data length of data.
261 * @param[in] dataLen length of given data
262 * @param[in] type transport at which packet needs to be send
264 * @return 0 on success otherwise a positive error value.
265 * @retval CA_STATUS_OK Successful
266 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
267 * @retval CA_STATUS_FAILED Operation failed
271 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
275 CATransportType_t type);
278 * @fn CAAdapterNetDtlsDecrypt
279 * @brief Performs DTLS decryption of the data received on
280 * secure port. This method performs in-place decryption
281 * of the cipher-text buffer. If a DTLS handshake message
282 * is received or decryption failure happens, this method
283 * returns -1. If a valid application PDU is decrypted, it
284 * returns the length of the decrypted pdu.
286 * @return 0 on success otherwise a positive error value.
287 * @retval CA_STATUS_OK Successful
288 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
289 * @retval CA_STATUS_FAILED Operation failed
292 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
296 CATransportType_t type);
298 #endif /* CA_ADAPTER_NET_DTLS_H_ */