1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
20 #ifndef _CA_ADAPTER_NET_DTLS_H
21 #define _CA_ADAPTER_NET_DTLS_H
24 #include "uarraylist.h"
26 #include "caadapterutils.h"
27 #include "ocsecurityconfig.h"
29 #define MAX_SUPPORTED_ADAPTERS 2
32 * @brief The implementation will be provided by OIC RI layer.
34 extern void OCGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo);
36 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint32_t port,
37 const void *data, const uint32_t dataLength, const CABool_t isSecured);
39 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint32_t port,
40 const void *data, const uint32_t dataLength);
43 * @struct stCAAdapterCallbacks_t
44 * @brief Data structure for holding the send and recv callbacks.
46 typedef struct CAAdapterCallbacks
48 CAPacketReceivedCallback recvCallback;
49 CAPacketSendCallback sendCallback;
50 } stCAAdapterCallbacks_t;
53 * @struct stCADtlsContext_t
54 * @brief Data structure for holding the tinyDTLS interface
57 typedef struct stCADtlsContext
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];
68 * @struct stPacketInfo_t
69 * @brief Data structure for holding the decrypted data address
70 * and length provided by tinyDTLS callback interface.
72 typedef struct stPacketInfo
80 * @brief tinyDTLS library error codes.
87 DTLS_SESSION_INITIATED,
92 * @struct stGattServiceInfo_t
93 * @brief structure to have address information.
98 socklen_t size; /**< size of addr */
102 struct sockaddr_storage st;
103 struct sockaddr_in sin;
104 struct sockaddr_in6 sin6;
107 } stCADtlsAddrInfo_t;
110 * @struct stCACacheMessage_t
111 * @brief structure to holds the information of cachemessage and address info.
114 typedef struct CACacheMessage
118 stCADtlsAddrInfo_t *destSession;
119 } stCACacheMessage_t;
122 * @enum eDtlsAdapterType_t
123 * @brief adapter types
130 } eDtlsAdapterType_t;
133 * @fn CADTLSSetAdapterCallbacks
134 * @brief Used set send and recv callbacks for different adapters(WIFI,EtherNet)
136 * @param[in] recvCallback packet received callback
137 * @param[in] sendCallback packet sent callback
138 * @param[in] type type of adapter
144 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
145 CAPacketSendCallback sendCallback, eDtlsAdapterType_t type);
148 * @fn CAAdapterNetDtlsInit
149 * @brief initialize tinyDTLS library and other necessary intialization.
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
157 CAResult_t CAAdapterNetDtlsInit();
160 * @fn CAAdapterNetDtlsDeInit
161 * @brief de-inits tinyDTLS library and free the allocated memory.
166 void CAAdapterNetDtlsDeInit();
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.
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
192 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
197 eDtlsAdapterType_t type);
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.
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
214 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
218 eDtlsAdapterType_t type);
220 #endif //_CA_ADAPTER_NET_DTLS_H