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"
30 #define MAX_SUPPORTED_ADAPTERS 2
33 * @brief The implementation will be provided by OIC RI layer.
35 extern void OCGetDtlsPskCredentials(OCDtlsPskCredsBlob **credInfo);
37 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint32_t port,
38 const void *data, const uint32_t dataLength, const CABool_t isSecured);
40 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint32_t port,
41 const void *data, const uint32_t dataLength);
44 * @struct stCAAdapterCallbacks_t
45 * @brief Data structure for holding the send and recv callbacks.
47 typedef struct CAAdapterCallbacks
49 CAPacketReceivedCallback recvCallback;
50 CAPacketSendCallback sendCallback;
51 } stCAAdapterCallbacks_t;
54 * @struct stCADtlsContext_t
55 * @brief Data structure for holding the tinyDTLS interface
58 typedef struct stCADtlsContext
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];
69 * @struct stPacketInfo_t
70 * @brief Data structure for holding the decrypted data address
71 * and length provided by tinyDTLS callback interface.
73 typedef struct stPacketInfo
81 * @brief tinyDTLS library error codes.
88 DTLS_SESSION_INITIATED,
93 * @struct stGattServiceInfo_t
94 * @brief structure to have address information.
99 socklen_t size; /**< size of addr */
103 struct sockaddr_storage st;
104 struct sockaddr_in sin;
105 struct sockaddr_in6 sin6;
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 adapter types
131 } eDtlsAdapterType_t;
134 * @fn CADTLSSetAdapterCallbacks
135 * @brief Used set send and recv callbacks for different adapters(WIFI,EtherNet)
137 * @param[in] recvCallback packet received callback
138 * @param[in] sendCallback packet sent callback
139 * @param[in] type type of adapter
144 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
145 CAPacketSendCallback sendCallback, eDtlsAdapterType_t type);
148 * @brief Register callback to get DTLS PSK credentials.
149 * @param credCallback [IN] callback to get DTLS credentials
152 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
155 * @fn CAAdapterNetDtlsInit
156 * @brief initialize tinyDTLS library and other necessary intialization.
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
164 CAResult_t CAAdapterNetDtlsInit();
167 * @fn CAAdapterNetDtlsDeInit
168 * @brief de-inits tinyDTLS library and free the allocated memory.
173 void CAAdapterNetDtlsDeInit();
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.
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
199 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
204 eDtlsAdapterType_t type);
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.
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
221 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
225 eDtlsAdapterType_t type);
227 #endif //_CA_ADAPTER_NET_DTLS_H