BLE implementation for Tizen Platform.
[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
30 /**
31  *   Currently DTLS supported adapters(2) WIFI and ETHENET for linux platform.
32  */
33 #define MAX_SUPPORTED_ADAPTERS 2
34
35 /**
36  * @brief The implementation will be provided by OIC RI layer.
37  */
38 extern void OCGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo);
39
40 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint16_t port,
41          const void *data, const uint32_t dataLength, const bool isSecured);
42
43 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint16_t port,
44         const void *data, const uint32_t dataLength);
45
46 /**
47  * @struct stCAAdapterCallbacks_t
48  * @brief  Data structure for holding the send and recv callbacks.
49  */
50 typedef struct CAAdapterCallbacks
51 {
52     CAPacketReceivedCallback recvCallback;  /**< Callback used to send data to upper layer. */
53     CAPacketSendCallback sendCallback;      /**< Callback used to send data to socket layer. */
54 } stCAAdapterCallbacks_t;
55
56 /**
57  * @struct stCADtlsContext_t
58  * @brief  Data structure for holding the tinyDTLS interface
59  *              related info.
60  */
61 typedef struct stCADtlsContext
62 {
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];
69 } stCADtlsContext_t;
70
71 /**
72  * @struct stPacketInfo_t
73  * @brief  Data structure for holding the decrypted data address
74  *              and length provided by tinyDTLS callback interface.
75  */
76 typedef struct stPacketInfo
77 {
78     uint8_t *dataAddress;
79     uint16_t dataLen;
80 } stPacketInfo_t;
81
82 /**
83  * @enum eDtlsRet_t
84  * @brief tinyDTLS library error codes.
85  *
86  */
87 typedef enum
88 {
89     DTLS_OK = 0,
90     DTLS_FAIL,
91     DTLS_SESSION_INITIATED,
92     DTLS_HS_MSG
93 } eDtlsRet_t;
94
95
96 /** Structure to have address information which will match with DTLS session_t struct.*/
97 typedef struct
98 {
99     socklen_t size;                 /**< Size of address. */
100     union
101     {
102         struct sockaddr     sa;
103         struct sockaddr_storage st;
104         struct sockaddr_in  sin;
105         struct sockaddr_in6 sin6;
106     } addr;                         /**< Address information. */
107     uint8_t ifIndex;                /**< Holds adpater index to get callback info. */
108 } stCADtlsAddrInfo_t;
109
110 /**
111  * @struct stCACacheMessage_t
112  * @brief structure to holds the information of cachemessage and address info.
113  *
114  */
115 typedef struct CACacheMessage
116 {
117     void *data;
118     uint32_t dataLen;
119     stCADtlsAddrInfo_t *destSession;
120 } stCACacheMessage_t;
121
122 /**
123  * @enum eDtlsAdapterType_t
124  * @brief This enum is used as array index for storing adapter level callbacks.
125  *        So Keeping 0 instead of "1 << 0". It is not going to be used as addition
126  *        and removal of adapter.
127  *
128  */
129 typedef enum
130 {
131     DTLS_ETHERNET = 0,
132     DTLS_WIFI
133 } eDtlsAdapterType_t;
134
135 /**
136  * @fn  CADTLSSetAdapterCallbacks
137  * @brief  Used set send and recv callbacks for different adapters(WIFI,EtherNet)
138  *
139  * @param[in]  recvCallback  packet received callback
140  * @param[in]  sendCallback  packet sent callback
141  * @param[in]  type  type of adapter
142  *
143  * @retval  void
144  *
145  */
146 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
147                                CAPacketSendCallback sendCallback, eDtlsAdapterType_t type);
148
149 /**
150  * @brief   Register callback to get DTLS PSK credentials.
151  * @param   credCallback   [IN] callback to get DTLS credentials
152  * @retval  void
153  */
154 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
155
156 /**
157  * @fn  CAAdapterNetDtlsInit
158  * @brief  initialize tinyDTLS library and other necessary intialization.
159  *
160  * @return  0 on success otherwise a positive error value.
161  * @retval  CA_STATUS_OK  Successful
162  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
163  * @retval  CA_STATUS_FAILED Operation failed
164  *
165  */
166 CAResult_t CAAdapterNetDtlsInit();
167
168 /**
169  * @fn  CAAdapterNetDtlsDeInit
170  * @brief  de-inits tinyDTLS library and free the allocated memory.
171  *
172  * @return  void
173  *
174  */
175 void CAAdapterNetDtlsDeInit();
176
177 /**
178  * @fn  CAAdapterNetDtlsEncrypt
179  * @brief  Performs DTLS encryption of the CoAP PDU. If a
180  *              DTLS session does not exist yet with the @dst,
181  *              a DTLS handshake will be started. In case where
182  *              a new DTLS handshake is started, pdu info is
183  *              cached to be send when session setup is finished.
184  *
185  * @param[in]  remoteAddress  address to which data will be sent.
186  * @param[in]  port  port to which data will be sent.
187  * @param[in]  data  length of data.
188  * @param[in]  dataLen  length of given data
189  * @param[out]  decdata  output variable to store the starting address
190  *                        of decrypted plaintext.
191  * @param[out]  cacheFlag  utput variable to indicate if pdu
192  *                        is cached and inform the caller to
193  *                       NOT free the memory holding pdu.
194  * @return  0 on success otherwise a positive error value.
195  * @retval  CA_STATUS_OK  Successful
196  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
197  * @retval  CA_STATUS_FAILED Operation failed
198  *
199  */
200
201 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
202                                    const uint16_t port,
203                                    void *data,
204                                    uint32_t dataLen,
205                                    uint8_t *cacheFlag,
206                                    eDtlsAdapterType_t type);
207
208 /**
209  * @fn  CAAdapterNetDtlsDecrypt
210  * @brief  Performs DTLS decryption of the data received on
211  *            secure port. This method performs in-place decryption
212  *            of the cipher-text buffer. If a DTLS handshake message
213  *            is received or decryption failure happens, this method
214  *            returns -1. If a valid application PDU is decrypted, it
215  *            returns the length of the decrypted pdu.
216  *
217  * @return  0 on success otherwise a positive error value.
218  * @retval  CA_STATUS_OK  Successful
219  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
220  * @retval  CA_STATUS_FAILED Operation failed
221  *
222  */
223 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
224                                    const uint16_t port,
225                                    uint8_t *data,
226                                    uint32_t dataLen,
227                                    eDtlsAdapterType_t type);
228
229 #endif //_CA_ADAPTER_NET_DTLS_H
230
231