Imported Upstream version 0.9.2
[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 CAEndpoint_t *endpoint,
42                                          const void *data, uint32_t dataLength);
43
44 typedef void (*CAPacketSendCallback)(CAEndpoint_t *endpoint,
45                                          const void *data, uint32_t dataLength);
46
47 /**
48  * @struct stCAAdapterCallbacks_t
49  * @brief  Data structure for holding the send and recv callbacks.
50  */
51 typedef struct CAAdapterCallbacks
52 {
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;
56
57 /**
58  * @struct stCADtlsContext_t
59  * @brief  Data structure for holding the tinyDTLS interface
60  *              related info.
61  */
62 typedef struct stCADtlsContext
63 {
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];
72 } stCADtlsContext_t;
73
74 /**
75  * @struct stPacketInfo_t
76  * @brief  Data structure for holding the decrypted data address
77  *              and length provided by tinyDTLS callback interface.
78  */
79 typedef struct stPacketInfo
80 {
81     uint8_t *dataAddress;
82     uint16_t dataLen;
83 } stPacketInfo_t;
84
85 /**
86  * @enum eDtlsRet_t
87  * @brief tinyDTLS library error codes.
88  *
89  */
90 typedef enum
91 {
92     DTLS_OK = 0,
93     DTLS_FAIL,
94     DTLS_SESSION_INITIATED,
95     DTLS_HS_MSG
96 } eDtlsRet_t;
97
98
99 /** Structure to have address information which will match with DTLS session_t struct.*/
100 typedef struct
101 {
102     socklen_t size;                 /**< Size of address. */
103     union
104     {
105         struct sockaddr     sa;
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;
112
113 /**
114  * @struct stCACacheMessage_t
115  * @brief structure to holds the information of cachemessage and address info.
116  *
117  */
118 typedef struct CACacheMessage
119 {
120     void *data;
121     uint32_t dataLen;
122     stCADtlsAddrInfo_t destSession;
123 } stCACacheMessage_t;
124
125
126 /**
127  * @fn  CADTLSSetAdapterCallbacks
128  * @brief  Used set send and recv callbacks for different adapters(WIFI,EtherNet)
129  *
130  * @param[in]  recvCallback  packet received callback
131  * @param[in]  sendCallback  packet sent callback
132  * @param[in]  type  type of adapter
133  *
134  * @retval  void
135  *
136  */
137 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
138                                CAPacketSendCallback sendCallback,
139                                CATransportAdapter_t type);
140
141 /**
142  * @brief   Register callback to get DTLS PSK credentials.
143  * @param   credCallback   [IN] callback to get DTLS credentials
144  * @retval  void
145  */
146 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
147
148 /**
149  * Select the cipher suite for dtls handshake
150  *
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
155  *
156  * @retval  CA_STATUS_OK for success, otherwise some error value
157  */
158 CAResult_t CADtlsSelectCipherSuite(const dtls_cipher_t cipher);
159
160 /**
161  * Enable anonymous ECDH cipher suite for dtls handshake
162  *
163  * @param[in] enable  TRUE/FALSE enables/disables anonymous cipher suite
164  *
165  * @retval  CA_STATUS_OK for success, otherwise some error value
166  */
167 CAResult_t CADtlsEnableAnonECDHCipherSuite(const bool enable);
168
169 /**
170  * Initiate DTLS handshake with selected cipher suite
171  *
172  * @param[in] endpoint  information of network address
173  *
174  * @retval  CA_STATUS_OK for success, otherwise some error value
175  */
176 CAResult_t CADtlsInitiateHandshake(const CAEndpoint_t *endpoint);
177
178 /**
179  * Close the DTLS session
180  *
181  * @param[in] endpoint  information of network address
182  *
183  * @retval  CA_STATUS_OK for success, otherwise some error value
184  */
185 CAResult_t CADtlsClose(const CAEndpoint_t *endpoint);
186
187 /**
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)')
192  *
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
202  *
203  * @retval  CA_STATUS_OK for success, otherwise some error value
204  */
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);
210 ;
211
212 /**
213  * @fn  CAAdapterNetDtlsInit
214  * @brief  initialize tinyDTLS library and other necessary intialization.
215  *
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
220  *
221  */
222 CAResult_t CAAdapterNetDtlsInit();
223
224 /**
225  * @fn  CAAdapterNetDtlsDeInit
226  * @brief  de-inits tinyDTLS library and free the allocated memory.
227  *
228  * @return  void
229  *
230  */
231 void CAAdapterNetDtlsDeInit();
232
233 /**
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.
240  *
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
245  *
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
250  *
251  */
252
253 CAResult_t CAAdapterNetDtlsEncrypt(const CAEndpoint_t *endpoint,
254                                    void *data,
255                                    uint32_t dataLen);
256
257 /**
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.
265  *
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
270  *
271  */
272 CAResult_t CAAdapterNetDtlsDecrypt(const CAEndpoint_t *endpoint,
273                                    uint8_t *data,
274                                    uint32_t dataLen);
275
276 #endif /* CA_ADAPTER_NET_DTLS_H_ */
277
278