1 /* *****************************************************************
3 * Copyright 2016 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_SSL_H_
21 #define CA_ADAPTER_NET_SSL_H_
27 #include "caadapterutils.h"
28 #include "cainterface.h"
31 * Currently TLS supported adapters(2) WIFI and ETHENET for linux platform.
33 #define MAX_SUPPORTED_ADAPTERS 2
35 typedef void (*CAPacketReceivedCallback)(const CASecureEndpoint_t *sep,
36 const void *data, uint32_t dataLength);
38 typedef void (*CAPacketSendCallback)(CAEndpoint_t *endpoint,
39 const void *data, uint32_t dataLength);
42 * Select the cipher suite for dtls handshake
44 * @param[in] cipher cipher suite
45 * 0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256
46 * 0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
47 * 0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
49 * @retval ::CA_STATUS_OK for success, otherwise some error value
51 CAResult_t CAsetTlsCipherSuite(const uint32_t cipher);
54 * Used set send and recv callbacks for different adapters(WIFI,EtherNet).
56 * @param[in] recvCallback packet received callback.
57 * @param[in] sendCallback packet sent callback.
58 * @param[in] type type of adapter.
61 void CAsetSslAdapterCallbacks(CAPacketReceivedCallback recvCallback,
62 CAPacketSendCallback sendCallback,
63 CATransportAdapter_t type);
66 * Register callback to get credentials types.
67 * @param[in] credTypesCallback callback to get credential types.
69 void CAsetCredentialTypesCallback(CAgetCredentialTypesHandler credTypesCallback);
71 * Register callback to get credential types.
72 * @param[in] typesCallback callback to get credential types.
74 void CAsetSslCredentialsCallback(CAgetPskCredentialsHandler credCallback);
77 * Close the TLS session
79 * @param[in] endpoint information of network address
81 * @retval ::CA_STATUS_OK for success, otherwise some error value
83 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
86 * initialize mbedTLS library and other necessary initialization.
88 * @return 0 on success otherwise a positive error value.
89 * @retval ::CA_STATUS_OK Successful.
90 * @retval ::CA_MEMORY_ALLOC_FAILED Memory allocation failed.
91 * @retval ::CA_STATUS_FAILED Operation failed.
94 CAResult_t CAinitSslAdapter();
97 * de-inits mbedTLS library and free the allocated memory.
99 void CAdeinitSslAdapter();
102 * Performs TLS encryption of the CoAP PDU.
104 * If a DTLS session does not exist yet with the @dst,
105 * a TLS handshake will be started. In case where a new TLS handshake
106 * is started, pdu info is cached to be send when session setup is finished.
108 * @param[in] endpoint address to which data will be sent.
109 * @param[in] data length of data.
110 * @param[in] dataLen length of given data
112 * @return 0 on success otherwise a positive error value.
113 * @retval ::CA_STATUS_OK Successful.
114 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
115 * @retval ::CA_STATUS_FAILED Operation failed.
119 CAResult_t CAencryptSsl(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen);
122 * Performs TLS decryption of the data.
124 * @param[in] sep address and flags for which data will be decrypted.
125 * @param[in] data length of data.
126 * @param[in] dataLen length of given data
128 * @return 0 on success otherwise a positive error value.
129 * @retval ::CA_STATUS_OK Successful.
130 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
131 * @retval ::CA_STATUS_FAILED Operation failed.
134 CAResult_t CAdecryptSsl(const CASecureEndpoint_t *sep, uint8_t *data, uint32_t dataLen);
137 * Initiate TLS handshake with selected cipher suite.
139 * @param[in] endpoint information of network address
141 * @retval ::CA_STATUS_OK for success, otherwise some error value
143 CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
146 * Register callback to deliver the result of TLS handshake
147 * @param[in] tlsHandshakeCallback Callback to receive the result of TLS handshake.
149 void CAsetSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback);
152 * Generate ownerPSK using PRF
153 * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
154 * 'ID of new device(Resource Server)',
155 * 'ID of owner smart-phone(Provisioning Server)')
157 * @param[in] endpoint information of network address
158 * @param[in] label Ownership transfer method e.g)"oic.sec.doxm.jw"
159 * @param[in] labelLen Byte length of label
160 * @param[in] rsrcServerDeviceID ID of new device(Resource Server)
161 * @param[in] rsrcServerDeviceIDLen Byte length of rsrcServerDeviceID
162 * @param[in] provServerDeviceID label of previous owner
163 * @param[in] provServerDeviceIDLen byte length of provServerDeviceID
164 * @param[in,out] ownerPSK Output buffer for owner PSK
165 * @param[in] ownerPSKSize Byte length of the ownerPSK to be generated
167 * @retval ::CA_STATUS_OK for success, otherwise some error value
169 CAResult_t CAsslGenerateOwnerPsk(const CAEndpoint_t *endpoint,
170 const uint8_t* label, const size_t labelLen,
171 const uint8_t* rsrcServerDeviceId, const size_t rsrcServerDeviceIdLen,
172 const uint8_t* provServerDeviceId, const size_t provServerDeviceIdLen,
173 uint8_t* ownerPsk, const size_t ownerPskSize);
178 #endif /* CA_ADAPTER_NET_SSL_H_ */