Merge branch 'master' into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caadapternetdtls.h
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20 #ifndef CA_ADAPTER_NET_DTLS_H_
21 #define CA_ADAPTER_NET_DTLS_H_
22
23 #include "dtls.h"
24 #include "uarraylist.h"
25 #include "camutex.h"
26 #include "caadapterutils.h"
27 #include "ocsecurityconfig.h"
28 #include "cainterface.h"
29 #include "cacommon.h"
30
31 /**
32  *   Currently DTLS supported adapters(2) WIFI and ETHENET for linux platform.
33  */
34 #define MAX_SUPPORTED_ADAPTERS 2
35
36 /**
37  * @brief The implementation will be provided by OIC RI layer.
38  */
39 extern void OCGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo);
40
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);
44
45 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint16_t port,
46                                          const void *data, const uint32_t dataLength);
47
48 /**
49  * @struct stCAAdapterCallbacks_t
50  * @brief  Data structure for holding the send and recv callbacks.
51  */
52 typedef struct CAAdapterCallbacks
53 {
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;
57
58 /**
59  * @struct stCADtlsContext_t
60  * @brief  Data structure for holding the tinyDTLS interface
61  *              related info.
62  */
63 typedef struct stCADtlsContext
64 {
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];
73 } stCADtlsContext_t;
74
75 /**
76  * @struct stPacketInfo_t
77  * @brief  Data structure for holding the decrypted data address
78  *              and length provided by tinyDTLS callback interface.
79  */
80 typedef struct stPacketInfo
81 {
82     uint8_t *dataAddress;
83     uint16_t dataLen;
84 } stPacketInfo_t;
85
86 /**
87  * @enum eDtlsRet_t
88  * @brief tinyDTLS library error codes.
89  *
90  */
91 typedef enum
92 {
93     DTLS_OK = 0,
94     DTLS_FAIL,
95     DTLS_SESSION_INITIATED,
96     DTLS_HS_MSG
97 } eDtlsRet_t;
98
99
100 /** Structure to have address information which will match with DTLS session_t struct.*/
101 typedef struct
102 {
103     socklen_t size;                 /**< Size of address. */
104     union
105     {
106         struct sockaddr     sa;
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;
113
114 /**
115  * @struct stCACacheMessage_t
116  * @brief structure to holds the information of cachemessage and address info.
117  *
118  */
119 typedef struct CACacheMessage
120 {
121     void *data;
122     uint32_t dataLen;
123     stCADtlsAddrInfo_t destSession;
124 } stCACacheMessage_t;
125
126
127 /**
128  * @struct stCADtlsPeerInfo_t
129  * @brief structure associates the peer psk id with peer n/w address
130  */
131 typedef struct stCADtlsPeerInfo
132 {
133     CAAddress_t address;
134     CARemoteId_t identity;
135 }stCADtlsPeerInfo_t;
136
137
138 /**
139  * @fn  CADTLSSetAdapterCallbacks
140  * @brief  Used set send and recv callbacks for different adapters(WIFI,EtherNet)
141  *
142  * @param[in]  recvCallback  packet received callback
143  * @param[in]  sendCallback  packet sent callback
144  * @param[in]  type  type of adapter
145  *
146  * @retval  void
147  *
148  */
149 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
150                                CAPacketSendCallback sendCallback, CATransportType_t type);
151
152 /**
153  * @brief   Register callback to get DTLS PSK credentials.
154  * @param   credCallback   [IN] callback to get DTLS credentials
155  * @retval  void
156  */
157 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
158
159 /**
160  * Select the cipher suite for dtls handshake
161  *
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
166  *
167  * @retval  CA_STATUS_OK for success, otherwise some error value
168  */
169 CAResult_t CADtlsSelectCipherSuite(const dtls_cipher_t cipher);
170
171 /**
172  * Enable anonymous ECDH cipher suite for dtls handshake
173  *
174  * @param[in] enable  TRUE/FALSE enables/disables anonymous cipher suite
175  *
176  * @retval  CA_STATUS_OK for success, otherwise some error value
177  */
178 CAResult_t CADtlsEnableAnonECDHCipherSuite(const bool enable);
179
180 /**
181  * Initiate DTLS handshake with selected cipher suite
182  *
183  * @param[in] addrInfo  information of network address
184  * @param[in] transportType  transport type
185  *
186  * @retval  CA_STATUS_OK for success, otherwise some error value
187  */
188 CAResult_t CADtlsInitiateHandshake(const CAAddress_t* addrInfo,
189                                    const CATransportType_t transportType);
190
191 /**
192  * Close the DTLS session
193  *
194  * @param[in] addrInfo  information of network address
195  * @param[in] transportType  transport type
196  *
197  * @retval  CA_STATUS_OK for success, otherwise some error value
198  */
199 CAResult_t CADtlsClose(const CAAddress_t* addrInfo,
200                        const CATransportType_t transportType);
201
202 /**
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)')
207  *
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
218  *
219  * @retval  CA_STATUS_OK for success, otherwise some error value
220  */
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);
227 ;
228
229 /**
230  * @fn  CAAdapterNetDtlsInit
231  * @brief  initialize tinyDTLS library and other necessary intialization.
232  *
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
237  *
238  */
239 CAResult_t CAAdapterNetDtlsInit();
240
241 /**
242  * @fn  CAAdapterNetDtlsDeInit
243  * @brief  de-inits tinyDTLS library and free the allocated memory.
244  *
245  * @return  void
246  *
247  */
248 void CAAdapterNetDtlsDeInit();
249
250 /**
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.
257  *
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
263  *
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
268  *
269  */
270
271 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
272                                    const uint16_t port,
273                                    void *data,
274                                    uint32_t dataLen,
275                                    CATransportType_t type);
276
277 /**
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.
285  *
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
290  *
291  */
292 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
293                                    const uint16_t port,
294                                    uint8_t *data,
295                                    uint32_t dataLen,
296                                    CATransportType_t type);
297
298 #endif /* CA_ADAPTER_NET_DTLS_H_ */
299
300