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