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