1 /* *****************************************************************
3 * Copyright 2015 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 ******************************************************************/
24 * This file contains the Security APIs for Resource Model to use.
27 #ifndef CA_SECURITY_INTERFACE_H_
28 #define CA_SECURITY_INTERFACE_H_
30 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
31 #include "mbedtls/ssl.h"
32 #include "mbedtls/x509_crt.h"
33 #endif //__WITH_DTLS__ or __WITH_TLS__
35 #include "byte_array.h"
44 * @enum CADtlsPskCredType_t
45 * Type of PSK credential required during DTLS handshake
46 * It does not make much sense in bringing in all definitions from dtls.h into here.
47 * Therefore, redefining them here.
54 } CADtlsPskCredType_t;
57 *@enum CASslEkcbRole_t
58 * type of SSL role to be used when invoking export key callback
62 CA_SSL_EKCB_CLIENT = 0,
63 CA_SSL_EKCB_SERVER = 1
67 *@enum CASslEkcbProtocol_t
68 * type of SSL protocol(TLS or DTLS) to be used when invoking export key callback
76 typedef OCStackResult (*UserConfirmNoCertCallback)(void * ctx);
79 * This internal callback is used by CA layer to
80 * retrieve PSK credentials from SRM.
82 * @param[in] type type of PSK data required by CA layer during DTLS handshake set.
83 * @param[in] desc Additional request information.
84 * @param[in] desc_len The actual length of desc.
85 * @param[out] result Must be filled with the requested information.
86 * @param[in] result_length Maximum size of @p result.
88 * @return The number of bytes written to @p result or a value
89 * less than zero on error.
91 typedef int (*CAgetPskCredentialsHandler)(CADtlsPskCredType_t type,
92 const uint8_t *desc, size_t desc_len,
93 uint8_t *result, size_t result_length);
95 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
98 * API to get a secure connected peer information
100 * @param[in] peer peer information includs IP address and port.
102 * @return secure connected peer information on success, otherwise NULL
104 const CASecureEndpoint_t *CAGetSecureEndpointData(const CAEndpoint_t *peer);
105 #endif //MULTIPLE_OWNER
109 * This internal callback is used by CA layer to
110 * retrieve all credential types from SRM
112 * @param[out] list of enabled credential types for CA handshake
115 typedef void (*CAgetCredentialTypesHandler)(bool * list);
117 * Binary structure containing PKIX related info
118 * own certificate chain, public key, CA's and CRL's
122 // own certificate chain
132 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
134 * this callback will be invoked to utilize peer certificate information
136 typedef int (*PeerCertCallback)(void *ctx, const mbedtls_x509_crt *peerCert,
140 * API to set callback used to utilize peer certificate information
141 * @param[in] peerCertCallback callback to utilize certificate information
143 * return CA_STATUS_OK on success
145 CAResult_t CAsetPeerCertCallback(void *ctx, PeerCertCallback peerCertCallback);
149 * Register callback to receive credential types.
150 * @param[in] credTypesCallback callback to get cerdential types
151 * @return ::CA_STATUS_OK
153 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler credTypesCallback);
155 * Register callback to receive the result of TLS handshake.
156 * @param[in] tlsHandshakeCallback callback for get tls handshake result
157 * @return ::CA_STATUS_OK
159 CAResult_t CAregisterSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback);
162 * Register callback to get TLS PSK credentials.
163 * @param[in] getTLSCredentials GetDTLS Credetials callback.
164 * @return ::CA_STATUS_OK
166 CAResult_t CAregisterPskCredentialsHandler(CAgetPskCredentialsHandler getTlsCredentials);
169 * @brief Callback function type for getting PKIX info
171 * @param inf[out] PKIX related info
175 typedef void (*CAgetPkixInfoHandler)(PkiInfo_t * inf);
177 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
179 * @brief Callback function type for setup PK context
181 * @param pkCtx[in] mbedtls's PK context
183 * @return 0 on success
185 typedef int (*CAsetupPkContextHandler)(mbedtls_pk_context * pkCtx);
188 * Register callback to setup PK Context
189 * @param[in] setupPkContextCallback Callback function to setup PK context.
190 * @return ::CA_STATUS_OK or appropriate error code.
192 CAResult_t CAregisterSetupPkContextHandler(CAsetupPkContextHandler setupPkContextHandler);
193 #endif //__WITH_DTLS__ or __WITH_TLS__
196 * Register callback to get PKIX related info.
197 * @param[in] getPkixInfoHandler Get PKIX related info callback.
198 * @return ::CA_STATUS_OK or appropriate error code.
200 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler);
202 * Register callback to get types of TLS suites.
203 * @param[in] getCredTypesHandler Get types of TLS suites callback.
204 * @return ::CA_STATUS_OK or appropriate error code.
206 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler getCredTypesHandler);
209 * Select the cipher suite for dtls handshake.
211 * @param[in] cipher cipher suite (Note : Make sure endianness).
212 * TLS_RSA_WITH_AES_256_CBC_SHA256 0x3D
213 * TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C
214 * TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B
215 * TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE
216 * TLS_ECDHE_ECDSA_WITH_AES_128_CCM 0xC0AC
217 * TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023
218 * TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024
219 * TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C
220 * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 0xC037
221 * TLS_ECDH_anon_WITH_AES_128_CBC_SHA 0xC018
222 * @param[in] adapter transport adapter (TCP/IP/BLE)
224 * @retval ::CA_STATUS_OK Successful.
225 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
226 * @retval ::CA_STATUS_FAILED Operation failed.
228 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter);
231 * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls.
233 * @param[in] enable TRUE/FALSE enables/disables anonymous cipher suite.
235 * @retval ::CA_STATUS_OK Successful.
236 * @retval ::CA_STATUS_FAILED Operation failed.
238 * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
240 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
244 * Generate ownerPSK using PRF.
245 * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
246 * 'ID of new device(Resource Server)',
247 * 'ID of owner smart-phone(Provisioning Server)')
249 * @param[in] endpoint information of network address.
250 * @param[in] label Ownership transfer method e.g)"oic.sec.doxm.jw".
251 * @param[in] labelLen Byte length of label.
252 * @param[in] rsrcServerDeviceID ID of new device(Resource Server).
253 * @param[in] rsrcServerDeviceIDLen Byte length of rsrcServerDeviceID.
254 * @param[in] provServerDeviceID label of previous owner.
255 * @param[in] provServerDeviceIDLen byte length of provServerDeviceID.
256 * @param[in,out] ownerPSK Output buffer for owner PSK.
257 * @param[in] ownerPskSize Byte length of the ownerPSK to be generated.
259 * @retval ::CA_STATUS_OK Successful.
260 * @retval ::CA_STATUS_FAILED Operation failed.
262 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t *endpoint,
263 const uint8_t* label, const size_t labelLen,
264 const uint8_t* rsrcServerDeviceID,
265 const size_t rsrcServerDeviceIDLen,
266 const uint8_t* provServerDeviceID,
267 const size_t provServerDeviceIDLen,
268 uint8_t* ownerPSK, const size_t ownerPskSize);
271 * Initiate DTLS handshake with selected cipher suite.
273 * @param[in] endpoint information of network address.
275 * @retval ::CA_STATUS_OK Successful.
276 * @retval ::CA_STATUS_FAILED Operation failed.
278 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint);
281 * Close the DTLS session.
283 * @param[in] endpoint information of network address.
285 * @retval ::CA_STATUS_OK Successful.
286 * @retval ::CA_STATUS_FAILED Operation failed.
288 CAResult_t CAcloseSslSession(const CAEndpoint_t *endpoint);
291 * Initiate TLS handshake with selected cipher suite.
293 * @param[in] endpoint information of network address.
295 * @retval ::CA_STATUS_OK Successful.
296 * @retval ::CA_STATUS_FAILED Operation failed.
298 CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
301 * Close the DTLS session.
303 * @param[in] endpoint information of network address.
305 * @retval ::CA_STATUS_OK Successful.
306 * @retval ::CA_STATUS_FAILED Operation failed.
308 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
311 * Close the TLS session using UUID
313 * @param[in] identity UUID of target device
314 * @param[in] idLength Byte length of 'identity'
316 * @retval ::CA_STATUS_OK Successful.
317 * @retval ::CA_STATUS_FAILED Operation failed.
319 CAResult_t CAcloseSslConnectionUsingUuid(const uint8_t *identity, size_t idLength);
322 * Close All of DTLS sessions.
324 void CAcloseSslConnectionAll(CATransportAdapter_t transportType);
326 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
329 * @brief Callback type: Export key block and master secret
330 * @note This is required for certain uses of TLS, e.g. EAP-TLS
331 * (RFC 5216) and Thread. The key pointers are ephemeral and
332 * therefore must not be stored. The master secret and keys
333 * should not be used directly except as an input to a key
334 * derivation function.
336 * @aram[in] masterSecret Pointer to master secret (fixed length: 48 bytes)
337 * @param[in] keyBlock Pointer to key block, see RFC 5246 section 6.3
338 * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
339 * @param[in] maclen MAC length
340 * @param[in] keylen Key length
341 * @param[in] ivlen IV length
343 typedef void (*SslExportKeysCallback_t)(const unsigned char* masterSecret,
344 const unsigned char* keyBlock,
345 size_t macLen, size_t keyLen, size_t ivLen);
348 * API to set a export SSL(TLS/DTLS) key callback.
349 * This callback will be invoked when SSL handshake occured.
351 * @param[in] exportKeysCb implementation of SslExportKeysCallback_t
352 * @param[in] protocol CA_SSL_EKCB_TLS=TLS, CA_SSL_EKCB_DTLS=DTLS (@ref CASslEkcbProtocol_t)
353 * @param[in] role CA_SSL_EKCB_CLIENT=client, CA_SSL_EKCB_SERVER=server (@ref CASslEkcbRole_t)
355 * @return CA_STATUS_OK on success, otherwise fail.
357 CAResult_t CASetSslExportKeysCallback(SslExportKeysCallback_t exportKeysCb,
358 CASslEkcbProtocol_t protocol, CASslEkcbRole_t role);
360 void CAsetNoCertConfirmCallback(UserConfirmNoCertCallback noCertCallback);
362 #endif //__WITH_TLS__ or __WITH_DTLS__
370 #endif /* CA_SECURITY_INTERFACE_H_ */