5e628d41f617544e784224aa4b07444b310fbdc8
[platform/upstream/iotivity.git] / resource / csdk / connectivity / api / casecurityinterface.h
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  *
24  * This file contains the Security APIs for Resource Model to use.
25  */
26
27 #ifndef CA_SECURITY_INTERFACE_H_
28 #define CA_SECURITY_INTERFACE_H_
29
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__
34 #include "cacommon.h"
35 #include "byte_array.h"
36 #include "octypes.h"
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42
43 /**
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.
48  */
49 typedef enum
50 {
51     CA_DTLS_PSK_HINT,
52     CA_DTLS_PSK_IDENTITY,
53     CA_DTLS_PSK_KEY
54 } CADtlsPskCredType_t;
55
56 /**
57  *@enum CASslEkcbRole_t
58  * type of SSL role to be used when invoking export key callback
59  */
60 typedef enum
61 {
62     CA_SSL_EKCB_CLIENT = 0,
63     CA_SSL_EKCB_SERVER = 1
64 }CASslEkcbRole_t;
65
66 /**
67  *@enum CASslEkcbProtocol_t
68  * type of SSL protocol(TLS or DTLS) to be used when invoking export key callback
69  */
70 typedef enum
71 {
72     CA_SSL_EKCB_TLS = 0,
73     CA_SSL_EKCB_DTLS = 1
74 }CASslEkcbProtocol_t;
75
76 /**
77  *@enum CACertificateVerificationStatus_t
78  * type of certificate status info to be used when invoking
79  * certificate verification status info callback
80  */
81 typedef enum
82 {
83     CA_CERTIFICATE_VERIFY_SUCCESS_MUTUAL = 0,
84     CA_CERTIFICATE_VERIFY_NO_CERT,
85     CA_CERTIFICATE_VERIFY_FAILED
86 } CACertificateVerificationStatus_t;
87
88 /**
89  * Callback function type for certificate verification status.
90  * @param[in]   status      Certificate verification status info.
91  */
92 typedef void (*CertificateVerificationCallback_t)(CACertificateVerificationStatus_t status);
93
94 /**
95  * This internal callback is used by CA layer to
96  * retrieve PSK credentials from SRM.
97  *
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.
103  *
104  * @return The number of bytes written to @p result or a value
105  *         less than zero on error.
106  */
107 typedef int (*CAgetPskCredentialsHandler)(CADtlsPskCredType_t type,
108               const uint8_t *desc, size_t desc_len,
109               uint8_t *result, size_t result_length);
110
111 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
112 #ifdef MULTIPLE_OWNER
113 /**
114  * API to get a secure connected peer information
115  *
116  * @param[in] peer peer information includs IP address and port.
117  *
118  * @return  secure connected peer information on success, otherwise NULL
119  */
120 const CASecureEndpoint_t *CAGetSecureEndpointData(const CAEndpoint_t *peer);
121 #endif //MULTIPLE_OWNER
122 #endif
123
124 /**
125  * This internal callback is used by CA layer to
126  * retrieve all credential types from SRM
127  *
128  * @param[out]  list of enabled credential types for CA handshake
129  *
130  */
131 typedef void (*CAgetCredentialTypesHandler)(bool * list);
132 /**
133  * Binary structure containing PKIX related info
134  * own certificate chain, public key, CA's and CRL's
135  */
136 typedef struct
137 {
138     // own certificate chain
139     ByteArray_t crt;
140     // own public key
141     ByteArray_t key;
142     // trusted CA's
143     ByteArray_t ca;
144     // trusted CRL's
145     ByteArray_t crl;
146 } PkiInfo_t;
147
148 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
149 /**
150  * this callback will be invoked to utilize peer certificate information
151  */
152 typedef int (*PeerCertCallback)(void *ctx, const mbedtls_x509_crt *peerCert,
153         int depth);
154
155 /**
156  * API to set callback used to utilize peer certificate information
157  * @param[in] peerCertCallback callback to utilize certificate information
158  *
159  * return CA_STATUS_OK on success
160  */
161 CAResult_t CAsetPeerCertCallback(void *ctx, PeerCertCallback peerCertCallback);
162 #endif
163
164 /**
165  * Register callback to receive credential types.
166  * @param[in] credTypesCallback callback to get cerdential types
167  * @return ::CA_STATUS_OK
168  */
169 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler credTypesCallback);
170 /**
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
174  */
175 CAResult_t CAregisterSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback);
176
177 /**
178  * Register callback to get TLS PSK credentials.
179  * @param[in]   getTLSCredentials    GetDTLS Credetials callback.
180  * @return  ::CA_STATUS_OK
181  */
182 CAResult_t CAregisterPskCredentialsHandler(CAgetPskCredentialsHandler getTlsCredentials);
183
184 /**
185  * @brief   Callback function type for getting PKIX info
186  *
187  * @param   inf[out]   PKIX related info
188  *
189  * @return  NONE
190  */
191 typedef void (*CAgetPkixInfoHandler)(PkiInfo_t * inf);
192
193 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
194 /**
195  * @brief   Callback function type for setup PK context
196  *
197  * @param   pkCtx[in]   mbedtls's PK context
198  *
199  * @return  0 on success
200  */
201 typedef int (*CAsetupPkContextHandler)(mbedtls_pk_context * pkCtx);
202
203 /**
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.
207  */
208 CAResult_t CAregisterSetupPkContextHandler(CAsetupPkContextHandler setupPkContextHandler);
209 #endif //__WITH_DTLS__ or __WITH_TLS__
210
211 /**
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.
215  */
216 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler);
217 /**
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.
221  */
222 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler getCredTypesHandler);
223
224 /**
225  * Select the cipher suite for dtls handshake.
226  *
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)
239  *
240  * @retval  ::CA_STATUS_OK    Successful.
241  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
242  * @retval  ::CA_STATUS_FAILED Operation failed.
243  */
244 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter);
245
246 /**
247  * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls.
248  *
249  * @param[in] enable  TRUE/FALSE enables/disables anonymous cipher suite.
250  *
251  * @retval  ::CA_STATUS_OK    Successful.
252  * @retval  ::CA_STATUS_FAILED Operation failed.
253  *
254  * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
255  */
256 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
257
258
259 /**
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)')
264  *
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.
274  *
275  * @retval  ::CA_STATUS_OK    Successful.
276  * @retval  ::CA_STATUS_FAILED Operation failed.
277  */
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);
285
286 /**
287  * Initiate DTLS handshake with selected cipher suite.
288  *
289  * @param[in] endpoint  information of network address.
290  *
291  * @retval  ::CA_STATUS_OK    Successful.
292  * @retval  ::CA_STATUS_FAILED Operation failed.
293  */
294 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint);
295
296 /**
297  * Close the DTLS session.
298  *
299  * @param[in] endpoint  information of network address.
300  *
301  * @retval  ::CA_STATUS_OK    Successful.
302  * @retval  ::CA_STATUS_FAILED Operation failed.
303  */
304 CAResult_t CAcloseSslSession(const CAEndpoint_t *endpoint);
305
306 /**
307  * Initiate TLS handshake with selected cipher suite.
308  *
309  * @param[in] endpoint information of network address.
310  *
311  * @retval  ::CA_STATUS_OK    Successful.
312  * @retval  ::CA_STATUS_FAILED Operation failed.
313  */
314 CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
315
316 /**
317  * Close the DTLS session.
318  *
319  * @param[in] endpoint  information of network address.
320  *
321  * @retval  ::CA_STATUS_OK    Successful.
322  * @retval  ::CA_STATUS_FAILED Operation failed.
323  */
324 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
325
326 #ifdef __TIZEN__
327 /**
328  * Close the DTLS session and free endpoint.
329  *
330  * @param[in] endpoint  information of network address;
331  *            CAcloseSslConnectionWrapper takes ownership of endpoint
332  *
333  * @retval  ::CA_STATUS_OK    Successful.
334  * @retval  ::CA_STATUS_FAILED Operation failed.
335  */
336 CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint);
337 #endif //__TIZEN__
338
339 /**
340  * Close the TLS session using UUID
341  *
342  * @param[in] identity  UUID of target device
343  * @param[in] idLength Byte length of 'identity'
344  *
345  * @retval  ::CA_STATUS_OK    Successful.
346  * @retval  ::CA_STATUS_FAILED Operation failed.
347  */
348 CAResult_t CAcloseSslConnectionUsingUuid(const uint8_t *identity, size_t idLength);
349
350 /**
351  * Close All of DTLS sessions.
352  */
353 void CAcloseSslConnectionAll(CATransportAdapter_t transportType);
354
355 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
356
357 /**
358  * @brief           Callback type: Export key block and master secret
359  * @note            This is required for certain uses of TLS, e.g. EAP-TLS
360  *                  (RFC 5216) and Thread. The key pointers are ephemeral and
361  *                  therefore must not be stored. The master secret and keys
362  *                  should not be used directly except as an input to a key
363  *                  derivation function.
364  *
365  * @aram[in] masterSecret        Pointer to master secret (fixed length: 48 bytes)
366  * @param[in] keyBlock        Pointer to key block, see RFC 5246 section 6.3
367  *                  (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
368  * @param[in] maclen    MAC length
369  * @param[in] keylen    Key length
370  * @param[in] ivlen     IV length
371  */
372 typedef void (*SslExportKeysCallback_t)(const unsigned char* masterSecret,
373                                         const unsigned char* keyBlock,
374                                         size_t macLen, size_t keyLen, size_t ivLen);
375
376 /**
377  * API to set a export SSL(TLS/DTLS) key callback.
378  * This callback will be invoked when SSL handshake occured.
379  *
380  * @param[in] exportKeysCb implementation of SslExportKeysCallback_t
381  * @param[in] protocol CA_SSL_EKCB_TLS=TLS, CA_SSL_EKCB_DTLS=DTLS (@ref CASslEkcbProtocol_t)
382  * @param[in] role CA_SSL_EKCB_CLIENT=client, CA_SSL_EKCB_SERVER=server (@ref CASslEkcbRole_t)
383  *
384  * @return CA_STATUS_OK on success, otherwise fail.
385  */
386 CAResult_t CASetSslExportKeysCallback(SslExportKeysCallback_t exportKeysCb,
387                                       CASslEkcbProtocol_t protocol, CASslEkcbRole_t role);
388
389 /**
390  * API to set certificate verification callback.
391  */
392 void CAsetCertificateVerificationCallback(CertificateVerificationCallback_t noCertCallback);
393
394 /**
395  * API to unset certificate verification callback.
396  */
397 void CAunsetCertificateVerificationCallback();
398
399 #endif //__WITH_TLS__ or __WITH_DTLS__
400
401
402 #ifdef __cplusplus
403 } /* extern "C" */
404 #endif
405
406
407 #endif /* CA_SECURITY_INTERFACE_H_ */
408