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"
28 #define MAX_SUPPORTED_ADAPTERS 2
30 ///TODO: once proper .h provided for this function, it will be removed
31 extern void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo);
33 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint32_t port,
34 const void *data, const uint32_t dataLength, const CABool_t isSecured);
36 typedef uint32_t (*CAPacketSendCallback)(const char *ipAddress, const uint32_t port,
37 const void *data, const uint32_t dataLength);
40 * @struct stCAAdapterCallbacks_t
41 * @brief Data structure for holding the send and recv callbacks.
43 typedef struct CAAdapterCallbacks
45 CAPacketReceivedCallback recvCallback;
46 CAPacketSendCallback sendCallback;
47 }stCAAdapterCallbacks_t;
50 * @struct stCADtlsContext_t
51 * @brief Data structure for holding the tinyDTLS interface
54 typedef struct stCADtlsContext
56 u_arraylist_t *cacheList; /**< pdu's are cached until DTLS session is formed */
57 struct dtls_context_t *dtlsContext; /**< pointer to tinyDTLS context */
58 struct stPacketInfo *packetInfo; /**< used by callback during
59 decryption to hold address/length */
60 dtls_handler_t callbacks; /**< pointer to callbacks needed by tinyDTLS */
61 stCAAdapterCallbacks_t adapterCallbacks[MAX_SUPPORTED_ADAPTERS];
65 * @struct stPacketInfo_t
66 * @brief Data structure for holding the decrypted data address
67 * and length provided by tinyDTLS callback interface.
69 typedef struct stPacketInfo
77 * @brief tinyDTLS library error codes.
84 DTLS_SESSION_INITIATED,
89 * @struct stGattServiceInfo_t
90 * @brief structure to have address information.
95 socklen_t size; /**< size of addr */
99 struct sockaddr_storage st;
100 struct sockaddr_in sin;
101 struct sockaddr_in6 sin6;
104 } stCADtlsAddrInfo_t;
107 * @struct stCACacheMessage_t
108 * @brief structure to holds the information of cachemessage and address info.
111 typedef struct CACacheMessage
115 stCADtlsAddrInfo_t *destSession;
116 } stCACacheMessage_t;
119 * @enum eDtlsAdapterType_t
120 * @brief adapter types
127 } eDtlsAdapterType_t;
130 * @fn CADTLSSetAdapterCallbacks
131 * @brief Used set send and recv callbacks for different adapters(WIFI,EtherNet)
133 * @param[in] recvCallback packet received callback
134 * @param[in] sendCallback packet sent callback
135 * @param[in] type type of adapter
141 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
142 CAPacketSendCallback sendCallback, eDtlsAdapterType_t type);
145 * @fn CAAdapterNetDtlsInit
146 * @brief initialize tinyDTLS library and other necessary intialization.
148 * @return 0 on success otherwise a positive error value.
149 * @retval CA_STATUS_OK Successful
150 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
151 * @retval CA_STATUS_FAILED Operation failed
154 CAResult_t CAAdapterNetDtlsInit();
157 * @fn CAAdapterNetDtlsDeInit
158 * @brief de-inits tinyDTLS library and free the allocated memory.
163 void CAAdapterNetDtlsDeInit();
166 * @fn CAAdapterNetDtlsEncrypt
167 * @brief Performs DTLS encryption of the CoAP PDU. If a
168 * DTLS session does not exist yet with the @dst,
169 * a DTLS handshake will be started. In case where
170 * a new DTLS handshake is started, pdu info is
171 * cached to be send when session setup is finished.
173 * @param[in] remoteAddress address to which data will be sent.
174 * @param[in] port port to which data will be sent.
175 * @param[in] data length of data.
176 * @param[in] dataLen length of given data
177 * @param[out] decdata output variable to store the starting address
178 * of decrypted plaintext.
179 * @param[out] cacheFlag utput variable to indicate if pdu
180 * is cached and inform the caller to
181 * NOT free the memory holding pdu.
182 * @return 0 on success otherwise a positive error value.
183 * @retval CA_STATUS_OK Successful
184 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
185 * @retval CA_STATUS_FAILED Operation failed
189 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
194 eDtlsAdapterType_t type);
197 * @fn CAAdapterNetDtlsDecrypt
198 * @brief Performs DTLS decryption of the data received on
199 * secure port. This method performs in-place decryption
200 * of the cipher-text buffer. If a DTLS handshake message
201 * is received or decryption failure happens, this method
202 * returns -1. If a valid application PDU is decrypted, it
203 * returns the length of the decrypted pdu.
205 * @return 0 on success otherwise a positive error value.
206 * @retval CA_STATUS_OK Successful
207 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
208 * @retval CA_STATUS_FAILED Operation failed
211 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
215 eDtlsAdapterType_t type);
217 #endif //_CA_ADAPTER_NET_DTLS_H