Added connectivity files for full code review
[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 "umutex.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(OCDtlsPskCredsBlob **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;
53     CAPacketSendCallback sendCallback;
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  * @struct stGattServiceInfo_t
97  * @brief structure to have address information.
98  *
99  */
100 typedef struct
101 {
102     socklen_t size;       /**< size of addr */
103     union
104     {
105         struct sockaddr     sa;
106         struct sockaddr_storage st;
107         struct sockaddr_in  sin;
108         struct sockaddr_in6 sin6;
109     } addr;
110     uint8_t ifIndex;
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  * @enum eDtlsAdapterType_t
127  * @brief This enum is used as array index for storing adapter level callbacks.
128  *        So Keeping 0 instead of "1 << 0". It is not going to be used as addition
129  *        and removal of adapter.
130  *
131  */
132 typedef enum
133 {
134     DTLS_ETHERNET = 0,
135     DTLS_WIFI
136 } eDtlsAdapterType_t;
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, eDtlsAdapterType_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  * @fn  CAAdapterNetDtlsInit
161  * @brief  initialize tinyDTLS library and other necessary intialization.
162  *
163  * @return  0 on success otherwise a positive error value.
164  * @retval  CA_STATUS_OK  Successful
165  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
166  * @retval  CA_STATUS_FAILED Operation failed
167  *
168  */
169 CAResult_t CAAdapterNetDtlsInit();
170
171 /**
172  * @fn  CAAdapterNetDtlsDeInit
173  * @brief  de-inits tinyDTLS library and free the allocated memory.
174  *
175  * @return  void
176  *
177  */
178 void CAAdapterNetDtlsDeInit();
179
180 /**
181  * @fn  CAAdapterNetDtlsEncrypt
182  * @brief  Performs DTLS encryption of the CoAP PDU. If a
183  *              DTLS session does not exist yet with the @dst,
184  *              a DTLS handshake will be started. In case where
185  *              a new DTLS handshake is started, pdu info is
186  *              cached to be send when session setup is finished.
187  *
188  * @param[in]  remoteAddress  address to which data will be sent.
189  * @param[in]  port  port to which data will be sent.
190  * @param[in]  data  length of data.
191  * @param[in]  dataLen  length of given data
192  * @param[out]  decdata  output variable to store the starting address
193  *                        of decrypted plaintext.
194  * @param[out]  cacheFlag  utput variable to indicate if pdu
195  *                        is cached and inform the caller to
196  *                       NOT free the memory holding pdu.
197  * @return  0 on success otherwise a positive error value.
198  * @retval  CA_STATUS_OK  Successful
199  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
200  * @retval  CA_STATUS_FAILED Operation failed
201  *
202  */
203
204 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
205                                    const uint16_t port,
206                                    void *data,
207                                    uint32_t dataLen,
208                                    uint8_t *cacheFlag,
209                                    eDtlsAdapterType_t type);
210
211 /**
212  * @fn  CAAdapterNetDtlsDecrypt
213  * @brief  Performs DTLS decryption of the data received on
214  *            secure port. This method performs in-place decryption
215  *            of the cipher-text buffer. If a DTLS handshake message
216  *            is received or decryption failure happens, this method
217  *            returns -1. If a valid application PDU is decrypted, it
218  *            returns the length of the decrypted pdu.
219  *
220  * @return  0 on success otherwise a positive error value.
221  * @retval  CA_STATUS_OK  Successful
222  * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
223  * @retval  CA_STATUS_FAILED Operation failed
224  *
225  */
226 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
227                                    const uint16_t port,
228                                    uint8_t *data,
229                                    uint32_t dataLen,
230                                    eDtlsAdapterType_t type);
231
232 #endif //_CA_ADAPTER_NET_DTLS_H
233
234