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