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 CAEndpoint_t *endpoint,
42 const void *data, uint32_t dataLength);
44 typedef uint32_t (*CAPacketSendCallback)(const CAEndpoint_t *endpoint,
45 const void *data, uint32_t dataLength);
48 * @struct stCAAdapterCallbacks_t
49 * @brief Data structure for holding the send and recv callbacks.
51 typedef struct CAAdapterCallbacks
53 CAPacketReceivedCallback recvCallback; /**< Callback used to send data to upper layer. */
54 CAPacketSendCallback sendCallback; /**< Callback used to send data to socket layer. */
55 } stCAAdapterCallbacks_t;
58 * @struct stCADtlsContext_t
59 * @brief Data structure for holding the tinyDTLS interface
62 typedef struct stCADtlsContext
64 u_arraylist_t *peerInfoList; /**< peerInfo list which holds the mapping between
65 peer id to it's n/w address */
66 u_arraylist_t *cacheList; /**< PDU's are cached until DTLS session is formed. */
67 struct dtls_context_t *dtlsContext; /**< Pointer to tinyDTLS context. */
68 struct stPacketInfo *packetInfo; /**< used by callback during
69 decryption to hold address/length. */
70 dtls_handler_t callbacks; /**< Pointer to callbacks needed by tinyDTLS. */
71 stCAAdapterCallbacks_t adapterCallbacks[MAX_SUPPORTED_ADAPTERS];
75 * @struct stPacketInfo_t
76 * @brief Data structure for holding the decrypted data address
77 * and length provided by tinyDTLS callback interface.
79 typedef struct stPacketInfo
87 * @brief tinyDTLS library error codes.
94 DTLS_SESSION_INITIATED,
99 /** Structure to have address information which will match with DTLS session_t struct.*/
102 socklen_t size; /**< Size of address. */
106 struct sockaddr_storage st;
107 struct sockaddr_in sin;
108 struct sockaddr_in6 sin6;
109 } addr; /**< Address information. */
110 uint8_t ifIndex; /**< Holds adpater index to get callback info. */
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;
127 * @fn CADTLSSetAdapterCallbacks
128 * @brief Used set send and recv callbacks for different adapters(WIFI,EtherNet)
130 * @param[in] recvCallback packet received callback
131 * @param[in] sendCallback packet sent callback
132 * @param[in] type type of adapter
137 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
138 CAPacketSendCallback sendCallback,
139 CATransportAdapter_t type);
142 * @brief Register callback to get DTLS PSK credentials.
143 * @param credCallback [IN] callback to get DTLS credentials
146 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
149 * Select the cipher suite for dtls handshake
151 * @param[in] cipher cipher suite
152 * 0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA
153 * 0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
154 * 0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
156 * @retval CA_STATUS_OK for success, otherwise some error value
158 CAResult_t CADtlsSelectCipherSuite(const dtls_cipher_t cipher);
161 * Enable anonymous ECDH cipher suite for dtls handshake
163 * @param[in] enable TRUE/FALSE enables/disables anonymous cipher suite
165 * @retval CA_STATUS_OK for success, otherwise some error value
167 CAResult_t CADtlsEnableAnonECDHCipherSuite(const bool enable);
170 * Initiate DTLS handshake with selected cipher suite
172 * @param[in] endpoint information of network address
174 * @retval CA_STATUS_OK for success, otherwise some error value
176 CAResult_t CADtlsInitiateHandshake(const CAEndpoint_t *endpoint);
179 * Close the DTLS session
181 * @param[in] endpoint information of network address
183 * @retval CA_STATUS_OK for success, otherwise some error value
185 CAResult_t CADtlsClose(const CAEndpoint_t *endpoint);
188 * Generate ownerPSK using PRF
189 * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
190 * 'ID of new device(Resource Server)',
191 * 'ID of owner smart-phone(Provisioning Server)')
193 * @param[in] endpoint information of network address
194 * @param[in] label Ownership transfer method e.g)"oic.sec.doxm.jw"
195 * @param[in] labelLen Byte length of label
196 * @param[in] rsrcServerDeviceID ID of new device(Resource Server)
197 * @param[in] rsrcServerDeviceIDLen Byte length of rsrcServerDeviceID
198 * @param[in] provServerDeviceID label of previous owner
199 * @param[in] provServerDeviceIDLen byte length of provServerDeviceID
200 * @param[in,out] ownerPSK Output buffer for owner PSK
201 * @param[in] ownerPSKSize Byte length of the ownerPSK to be generated
203 * @retval CA_STATUS_OK for success, otherwise some error value
205 CAResult_t CADtlsGenerateOwnerPSK(const CAEndpoint_t *endpoint,
206 const uint8_t* label, const size_t labelLen,
207 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
208 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
209 uint8_t* ownerPSK, const size_t ownerPSKSize);
213 * @fn CAAdapterNetDtlsInit
214 * @brief initialize tinyDTLS library and other necessary intialization.
216 * @return 0 on success otherwise a positive error value.
217 * @retval CA_STATUS_OK Successful
218 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
219 * @retval CA_STATUS_FAILED Operation failed
222 CAResult_t CAAdapterNetDtlsInit();
225 * @fn CAAdapterNetDtlsDeInit
226 * @brief de-inits tinyDTLS library and free the allocated memory.
231 void CAAdapterNetDtlsDeInit();
234 * @fn CAAdapterNetDtlsEncrypt
235 * @brief Performs DTLS encryption of the CoAP PDU. If a
236 * DTLS session does not exist yet with the @dst,
237 * a DTLS handshake will be started. In case where
238 * a new DTLS handshake is started, pdu info is
239 * cached to be send when session setup is finished.
241 * @param[in] endpoint address to which data will be sent.
242 * @param[in] port port to which data will be sent.
243 * @param[in] data length of data.
244 * @param[in] dataLen length of given data
246 * @return 0 on success otherwise a positive error value.
247 * @retval CA_STATUS_OK Successful
248 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
249 * @retval CA_STATUS_FAILED Operation failed
253 CAResult_t CAAdapterNetDtlsEncrypt(const CAEndpoint_t *endpoint,
258 * @fn CAAdapterNetDtlsDecrypt
259 * @brief Performs DTLS decryption of the data received on
260 * secure port. This method performs in-place decryption
261 * of the cipher-text buffer. If a DTLS handshake message
262 * is received or decryption failure happens, this method
263 * returns -1. If a valid application PDU is decrypted, it
264 * returns the length of the decrypted pdu.
266 * @return 0 on success otherwise a positive error value.
267 * @retval CA_STATUS_OK Successful
268 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
269 * @retval CA_STATUS_FAILED Operation failed
272 CAResult_t CAAdapterNetDtlsDecrypt(const CAEndpoint_t *endpoint,
276 #endif /* CA_ADAPTER_NET_DTLS_H_ */