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
77 *@enum CACertificateVerificationStatus_t
78 * type of certificate status info to be used when invoking
79 * certificate verification status info callback
83 CA_CERTIFICATE_VERIFY_SUCCESS_MUTUAL = 0,
84 CA_CERTIFICATE_VERIFY_NO_CERT,
85 CA_CERTIFICATE_VERIFY_FAILED
86 } CACertificateVerificationStatus_t;
89 * Callback function type for certificate verification status.
90 * @param[in] status Certificate verification status info.
92 typedef void (*CertificateVerificationCallback_t)(CACertificateVerificationStatus_t status);
95 * This internal callback is used by CA layer to
96 * retrieve PSK credentials from SRM.
98 * @param[in] type type of PSK data required by CA layer during DTLS handshake set.
99 * @param[in] desc Additional request information.
100 * @param[in] desc_len The actual length of desc.
101 * @param[out] result Must be filled with the requested information.
102 * @param[in] result_length Maximum size of @p result.
104 * @return The number of bytes written to @p result or a value
105 * less than zero on error.
107 typedef int (*CAgetPskCredentialsHandler)(CADtlsPskCredType_t type,
108 const uint8_t *desc, size_t desc_len,
109 uint8_t *result, size_t result_length);
111 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
112 #ifdef MULTIPLE_OWNER
114 * API to get a secure connected peer information
116 * @param[in] peer peer information includs IP address and port.
118 * @return secure connected peer information on success, otherwise NULL
120 const CASecureEndpoint_t *CAGetSecureEndpointData(const CAEndpoint_t *peer);
121 #endif //MULTIPLE_OWNER
125 * This internal callback is used by CA layer to
126 * retrieve all credential types from SRM
128 * @param[out] list of enabled credential types for CA handshake
131 typedef void (*CAgetCredentialTypesHandler)(bool * list);
133 * Binary structure containing PKIX related info
134 * own certificate chain, public key, CA's and CRL's
138 // own certificate chain
148 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
150 * this callback will be invoked to utilize peer certificate information
152 typedef int (*PeerCertCallback)(void *ctx, const mbedtls_x509_crt *peerCert,
156 * API to set callback used to utilize peer certificate information
157 * @param[in] peerCertCallback callback to utilize certificate information
159 * return CA_STATUS_OK on success
161 CAResult_t CAsetPeerCertCallback(void *ctx, PeerCertCallback peerCertCallback);
165 * Register callback to receive credential types.
166 * @param[in] credTypesCallback callback to get cerdential types
167 * @return ::CA_STATUS_OK
169 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler credTypesCallback);
171 * Register callback to receive the result of TLS handshake.
172 * @param[in] tlsHandshakeCallback callback for get tls handshake result
173 * @return ::CA_STATUS_OK
175 CAResult_t CAregisterSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback);
178 * Register callback to get TLS PSK credentials.
179 * @param[in] getTLSCredentials GetDTLS Credetials callback.
180 * @return ::CA_STATUS_OK
182 CAResult_t CAregisterPskCredentialsHandler(CAgetPskCredentialsHandler getTlsCredentials);
185 * @brief Callback function type for getting PKIX info
187 * @param inf[out] PKIX related info
191 typedef void (*CAgetPkixInfoHandler)(PkiInfo_t * inf);
193 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
195 * @brief Callback function type for setup PK context
197 * @param pkCtx[in] mbedtls's PK context
199 * @return 0 on success
201 typedef int (*CAsetupPkContextHandler)(mbedtls_pk_context * pkCtx);
204 * Register callback to setup PK Context
205 * @param[in] setupPkContextCallback Callback function to setup PK context.
206 * @return ::CA_STATUS_OK or appropriate error code.
208 CAResult_t CAregisterSetupPkContextHandler(CAsetupPkContextHandler setupPkContextHandler);
209 #endif //__WITH_DTLS__ or __WITH_TLS__
212 * Register callback to get PKIX related info.
213 * @param[in] getPkixInfoHandler Get PKIX related info callback.
214 * @return ::CA_STATUS_OK or appropriate error code.
216 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler);
218 * Register callback to get types of TLS suites.
219 * @param[in] getCredTypesHandler Get types of TLS suites callback.
220 * @return ::CA_STATUS_OK or appropriate error code.
222 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler getCredTypesHandler);
225 * Select the cipher suite for dtls handshake.
227 * @param[in] cipher cipher suite (Note : Make sure endianness).
228 * TLS_RSA_WITH_AES_256_CBC_SHA256 0x3D
229 * TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C
230 * TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B
231 * TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE
232 * TLS_ECDHE_ECDSA_WITH_AES_128_CCM 0xC0AC
233 * TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023
234 * TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024
235 * TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C
236 * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 0xC037
237 * TLS_ECDH_anon_WITH_AES_128_CBC_SHA 0xC018
238 * @param[in] adapter transport adapter (TCP/IP/BLE)
240 * @retval ::CA_STATUS_OK Successful.
241 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
242 * @retval ::CA_STATUS_FAILED Operation failed.
244 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter);
247 * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls.
249 * @param[in] enable TRUE/FALSE enables/disables anonymous cipher suite.
251 * @retval ::CA_STATUS_OK Successful.
252 * @retval ::CA_STATUS_FAILED Operation failed.
254 * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
256 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
260 * Generate ownerPSK using PRF.
261 * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
262 * 'ID of new device(Resource Server)',
263 * 'ID of owner smart-phone(Provisioning Server)')
265 * @param[in] endpoint information of network address.
266 * @param[in] label Ownership transfer method e.g)"oic.sec.doxm.jw".
267 * @param[in] labelLen Byte length of label.
268 * @param[in] rsrcServerDeviceID ID of new device(Resource Server).
269 * @param[in] rsrcServerDeviceIDLen Byte length of rsrcServerDeviceID.
270 * @param[in] provServerDeviceID label of previous owner.
271 * @param[in] provServerDeviceIDLen byte length of provServerDeviceID.
272 * @param[in,out] ownerPSK Output buffer for owner PSK.
273 * @param[in] ownerPskSize Byte length of the ownerPSK to be generated.
275 * @retval ::CA_STATUS_OK Successful.
276 * @retval ::CA_STATUS_FAILED Operation failed.
278 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t *endpoint,
279 const uint8_t* label, const size_t labelLen,
280 const uint8_t* rsrcServerDeviceID,
281 const size_t rsrcServerDeviceIDLen,
282 const uint8_t* provServerDeviceID,
283 const size_t provServerDeviceIDLen,
284 uint8_t* ownerPSK, const size_t ownerPskSize);
287 * Initiate DTLS handshake with selected cipher suite.
289 * @param[in] endpoint information of network address.
291 * @retval ::CA_STATUS_OK Successful.
292 * @retval ::CA_STATUS_FAILED Operation failed.
294 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint);
297 * Close the DTLS session.
299 * @param[in] endpoint information of network address.
301 * @retval ::CA_STATUS_OK Successful.
302 * @retval ::CA_STATUS_FAILED Operation failed.
304 CAResult_t CAcloseSslSession(const CAEndpoint_t *endpoint);
307 * Initiate TLS handshake with selected cipher suite.
309 * @param[in] endpoint information of network address.
311 * @retval ::CA_STATUS_OK Successful.
312 * @retval ::CA_STATUS_FAILED Operation failed.
314 CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
317 * Close the DTLS session.
319 * @param[in] endpoint information of network address.
321 * @retval ::CA_STATUS_OK Successful.
322 * @retval ::CA_STATUS_FAILED Operation failed.
324 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
327 * Close the DTLS session and free endpoint.
329 * @param[in] endpoint information of network address;
330 * CAcloseSslConnectionWrapper takes ownership of endpoint
332 * @retval ::CA_STATUS_OK Successful.
333 * @retval ::CA_STATUS_FAILED Operation failed.
335 CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint);
338 * Close the TLS session using UUID
340 * @param[in] identity UUID of target device
341 * @param[in] idLength Byte length of 'identity'
343 * @retval ::CA_STATUS_OK Successful.
344 * @retval ::CA_STATUS_FAILED Operation failed.
346 CAResult_t CAcloseSslConnectionUsingUuid(const uint8_t *identity, size_t idLength);
349 * Close All of DTLS sessions.
351 void CAcloseSslConnectionAll(CATransportAdapter_t transportType);
353 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
356 * @brief Callback type: Export key block and master secret
357 * @note This is required for certain uses of TLS, e.g. EAP-TLS
358 * (RFC 5216) and Thread. The key pointers are ephemeral and
359 * therefore must not be stored. The master secret and keys
360 * should not be used directly except as an input to a key
361 * derivation function.
363 * @aram[in] masterSecret Pointer to master secret (fixed length: 48 bytes)
364 * @param[in] keyBlock Pointer to key block, see RFC 5246 section 6.3
365 * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
366 * @param[in] maclen MAC length
367 * @param[in] keylen Key length
368 * @param[in] ivlen IV length
370 typedef void (*SslExportKeysCallback_t)(const unsigned char* masterSecret,
371 const unsigned char* keyBlock,
372 size_t macLen, size_t keyLen, size_t ivLen);
375 * API to set a export SSL(TLS/DTLS) key callback.
376 * This callback will be invoked when SSL handshake occured.
378 * @param[in] exportKeysCb implementation of SslExportKeysCallback_t
379 * @param[in] protocol CA_SSL_EKCB_TLS=TLS, CA_SSL_EKCB_DTLS=DTLS (@ref CASslEkcbProtocol_t)
380 * @param[in] role CA_SSL_EKCB_CLIENT=client, CA_SSL_EKCB_SERVER=server (@ref CASslEkcbRole_t)
382 * @return CA_STATUS_OK on success, otherwise fail.
384 CAResult_t CASetSslExportKeysCallback(SslExportKeysCallback_t exportKeysCb,
385 CASslEkcbProtocol_t protocol, CASslEkcbRole_t role);
388 * API to set certificate verification callback.
390 void CAsetCertificateVerificationCallback(CertificateVerificationCallback_t noCertCallback);
393 * API to unset certificate verification callback.
395 void CAunsetCertificateVerificationCallback();
397 #endif //__WITH_TLS__ or __WITH_DTLS__
405 #endif /* CA_SECURITY_INTERFACE_H_ */