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