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 extern void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo);
30 typedef void (*CAPacketReceivedCallback)(const char *ipAddress, const uint32_t port,
31 const void *data, const uint32_t dataLength, const CABool_t isSecured);
34 *@struct stCADtlsContext_t
35 * @brief Data structure for holding the tinyDTLS interface
38 typedef struct stCADtlsContext
40 u_arraylist_t *cacheList; /**< pdu's are cached until DTLS session is formed */
41 struct dtls_context_t *dtlsContext; /**< pointer to tinyDTLS context */
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;
49 *@struct stPacketInfo_t
50 * @brief Data structure for holding the decrypted data address
51 * and length provided by tinyDTLS callback interface.
53 typedef struct stPacketInfo
61 * @brief tinyDTLS library error codes.
68 DTLS_SESSION_INITIATED,
73 * @struct stGattServiceInfo_t
74 * @brief structure to have address information.
79 socklen_t size; /**< size of addr */
83 struct sockaddr_storage st;
84 struct sockaddr_in sin;
85 struct sockaddr_in6 sin6;
91 * @struct stCACacheMessage_t
92 * @brief structure to holds the information of cachemessage and address info.
95 typedef struct CACacheMessage_t
99 stCADtlsAddrInfo_t *destSession;
100 } stCACacheMessage_t;
102 void CADTLSSetPacketReceiveCallback(CAPacketReceivedCallback callback);
105 * @fn CAAdapterNetDtlsInit
106 * @brief initialize tinyDTLS library and other necessary intialization.
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
114 CAResult_t CAAdapterNetDtlsInit();
117 * @fn CAAdapterNetDtlsDeInit
118 * @brief de-inits tinyDTLS library and free the allocated memory.
123 void CAAdapterNetDtlsDeInit();
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.
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
149 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
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.
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
170 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
175 #endif //_CA_ADAPTER_NET_DTLS_H