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"
28 #include "cainterface.h"
31 * Currently DTLS supported adapters(2) WIFI and ETHENET for linux platform.
33 #define MAX_SUPPORTED_ADAPTERS 2
36 * @brief The implementation will be provided by OIC RI layer.
38 extern void OCGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo);
40 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint16_t port,
41 const void *data, const uint32_t dataLength, const bool isSecured);
43 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint16_t port,
44 const void *data, const uint32_t dataLength);
47 * @struct stCAAdapterCallbacks_t
48 * @brief Data structure for holding the send and recv callbacks.
50 typedef struct CAAdapterCallbacks
52 CAPacketReceivedCallback recvCallback; /**< Callback used to send data to upper layer. */
53 CAPacketSendCallback sendCallback; /**< Callback used to send data to socket layer. */
54 } stCAAdapterCallbacks_t;
57 * @struct stCADtlsContext_t
58 * @brief Data structure for holding the tinyDTLS interface
61 typedef struct stCADtlsContext
63 u_arraylist_t *cacheList; /**< PDU's are cached until DTLS session is formed. */
64 struct dtls_context_t *dtlsContext; /**< Pointer to tinyDTLS context. */
65 struct stPacketInfo *packetInfo; /**< used by callback during
66 decryption to hold address/length. */
67 dtls_handler_t callbacks; /**< Pointer to callbacks needed by tinyDTLS. */
68 stCAAdapterCallbacks_t adapterCallbacks[MAX_SUPPORTED_ADAPTERS];
72 * @struct stPacketInfo_t
73 * @brief Data structure for holding the decrypted data address
74 * and length provided by tinyDTLS callback interface.
76 typedef struct stPacketInfo
84 * @brief tinyDTLS library error codes.
91 DTLS_SESSION_INITIATED,
96 /** Structure to have address information which will match with DTLS session_t struct.*/
99 socklen_t size; /**< Size of address. */
103 struct sockaddr_storage st;
104 struct sockaddr_in sin;
105 struct sockaddr_in6 sin6;
106 } addr; /**< Address information. */
107 uint8_t ifIndex; /**< Holds adpater index to get callback info. */
108 } stCADtlsAddrInfo_t;
111 * @struct stCACacheMessage_t
112 * @brief structure to holds the information of cachemessage and address info.
115 typedef struct CACacheMessage
119 stCADtlsAddrInfo_t *destSession;
120 } stCACacheMessage_t;
123 * @enum eDtlsAdapterType_t
124 * @brief This enum is used as array index for storing adapter level callbacks.
125 * So Keeping 0 instead of "1 << 0". It is not going to be used as addition
126 * and removal of adapter.
133 } eDtlsAdapterType_t;
136 * @fn CADTLSSetAdapterCallbacks
137 * @brief Used set send and recv callbacks for different adapters(WIFI,EtherNet)
139 * @param[in] recvCallback packet received callback
140 * @param[in] sendCallback packet sent callback
141 * @param[in] type type of adapter
146 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
147 CAPacketSendCallback sendCallback, eDtlsAdapterType_t type);
150 * @brief Register callback to get DTLS PSK credentials.
151 * @param credCallback [IN] callback to get DTLS credentials
154 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
157 * @fn CAAdapterNetDtlsInit
158 * @brief initialize tinyDTLS library and other necessary intialization.
160 * @return 0 on success otherwise a positive error value.
161 * @retval CA_STATUS_OK Successful
162 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
163 * @retval CA_STATUS_FAILED Operation failed
166 CAResult_t CAAdapterNetDtlsInit();
169 * @fn CAAdapterNetDtlsDeInit
170 * @brief de-inits tinyDTLS library and free the allocated memory.
175 void CAAdapterNetDtlsDeInit();
178 * @fn CAAdapterNetDtlsEncrypt
179 * @brief Performs DTLS encryption of the CoAP PDU. If a
180 * DTLS session does not exist yet with the @dst,
181 * a DTLS handshake will be started. In case where
182 * a new DTLS handshake is started, pdu info is
183 * cached to be send when session setup is finished.
185 * @param[in] remoteAddress address to which data will be sent.
186 * @param[in] port port to which data will be sent.
187 * @param[in] data length of data.
188 * @param[in] dataLen length of given data
189 * @param[out] decdata output variable to store the starting address
190 * of decrypted plaintext.
191 * @param[out] cacheFlag utput variable to indicate if pdu
192 * is cached and inform the caller to
193 * NOT free the memory holding pdu.
194 * @return 0 on success otherwise a positive error value.
195 * @retval CA_STATUS_OK Successful
196 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
197 * @retval CA_STATUS_FAILED Operation failed
201 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
206 eDtlsAdapterType_t type);
209 * @fn CAAdapterNetDtlsDecrypt
210 * @brief Performs DTLS decryption of the data received on
211 * secure port. This method performs in-place decryption
212 * of the cipher-text buffer. If a DTLS handshake message
213 * is received or decryption failure happens, this method
214 * returns -1. If a valid application PDU is decrypted, it
215 * returns the length of the decrypted pdu.
217 * @return 0 on success otherwise a positive error value.
218 * @retval CA_STATUS_OK Successful
219 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
220 * @retval CA_STATUS_FAILED Operation failed
223 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
227 eDtlsAdapterType_t type);
229 #endif //_CA_ADAPTER_NET_DTLS_H