Absent client's certificate CB
[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 typedef OCStackResult (*UserConfirmNoCertCallback)(void * ctx);
77
78 /**
79  * This internal callback is used by CA layer to
80  * retrieve PSK credentials from SRM.
81  *
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.
87  *
88  * @return The number of bytes written to @p result or a value
89  *         less than zero on error.
90  */
91 typedef int (*CAgetPskCredentialsHandler)(CADtlsPskCredType_t type,
92               const uint8_t *desc, size_t desc_len,
93               uint8_t *result, size_t result_length);
94
95 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
96 #ifdef MULTIPLE_OWNER
97 /**
98  * API to get a secure connected peer information
99  *
100  * @param[in] peer peer information includs IP address and port.
101  *
102  * @return  secure connected peer information on success, otherwise NULL
103  */
104 const CASecureEndpoint_t *CAGetSecureEndpointData(const CAEndpoint_t *peer);
105 #endif //MULTIPLE_OWNER
106 #endif
107
108 /**
109  * This internal callback is used by CA layer to
110  * retrieve all credential types from SRM
111  *
112  * @param[out]  list of enabled credential types for CA handshake
113  *
114  */
115 typedef void (*CAgetCredentialTypesHandler)(bool * list);
116 /**
117  * Binary structure containing PKIX related info
118  * own certificate chain, public key, CA's and CRL's
119  */
120 typedef struct
121 {
122     // own certificate chain
123     ByteArray_t crt;
124     // own public key
125     ByteArray_t key;
126     // trusted CA's
127     ByteArray_t ca;
128     // trusted CRL's
129     ByteArray_t crl;
130 } PkiInfo_t;
131
132 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
133 /**
134  * this callback will be invoked to utilize peer certificate information
135  */
136 typedef int (*PeerCertCallback)(void *ctx, const mbedtls_x509_crt *peerCert,
137         int depth);
138
139 /**
140  * API to set callback used to utilize peer certificate information
141  * @param[in] peerCertCallback callback to utilize certificate information
142  *
143  * return CA_STATUS_OK on success
144  */
145 CAResult_t CAsetPeerCertCallback(void *ctx, PeerCertCallback peerCertCallback);
146 #endif
147
148 /**
149  * Register callback to receive credential types.
150  * @param[in] credTypesCallback callback to get cerdential types
151  * @return ::CA_STATUS_OK
152  */
153 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler credTypesCallback);
154 /**
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
158  */
159 CAResult_t CAregisterSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback);
160
161 /**
162  * Register callback to get TLS PSK credentials.
163  * @param[in]   getTLSCredentials    GetDTLS Credetials callback.
164  * @return  ::CA_STATUS_OK
165  */
166 CAResult_t CAregisterPskCredentialsHandler(CAgetPskCredentialsHandler getTlsCredentials);
167
168 /**
169  * @brief   Callback function type for getting PKIX info
170  *
171  * @param   inf[out]   PKIX related info
172  *
173  * @return  NONE
174  */
175 typedef void (*CAgetPkixInfoHandler)(PkiInfo_t * inf);
176
177 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
178 /**
179  * @brief   Callback function type for setup PK context
180  *
181  * @param   pkCtx[in]   mbedtls's PK context
182  *
183  * @return  0 on success
184  */
185 typedef int (*CAsetupPkContextHandler)(mbedtls_pk_context * pkCtx);
186
187 /**
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.
191  */
192 CAResult_t CAregisterSetupPkContextHandler(CAsetupPkContextHandler setupPkContextHandler);
193 #endif //__WITH_DTLS__ or __WITH_TLS__
194
195 /**
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.
199  */
200 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler);
201 /**
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.
205  */
206 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler getCredTypesHandler);
207
208 /**
209  * Select the cipher suite for dtls handshake.
210  *
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)
223  *
224  * @retval  ::CA_STATUS_OK    Successful.
225  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
226  * @retval  ::CA_STATUS_FAILED Operation failed.
227  */
228 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter);
229
230 /**
231  * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls.
232  *
233  * @param[in] enable  TRUE/FALSE enables/disables anonymous cipher suite.
234  *
235  * @retval  ::CA_STATUS_OK    Successful.
236  * @retval  ::CA_STATUS_FAILED Operation failed.
237  *
238  * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
239  */
240 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
241
242
243 /**
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)')
248  *
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.
258  *
259  * @retval  ::CA_STATUS_OK    Successful.
260  * @retval  ::CA_STATUS_FAILED Operation failed.
261  */
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);
269
270 /**
271  * Initiate DTLS handshake with selected cipher suite.
272  *
273  * @param[in] endpoint  information of network address.
274  *
275  * @retval  ::CA_STATUS_OK    Successful.
276  * @retval  ::CA_STATUS_FAILED Operation failed.
277  */
278 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint);
279
280 /**
281  * Close the DTLS session.
282  *
283  * @param[in] endpoint  information of network address.
284  *
285  * @retval  ::CA_STATUS_OK    Successful.
286  * @retval  ::CA_STATUS_FAILED Operation failed.
287  */
288 CAResult_t CAcloseSslSession(const CAEndpoint_t *endpoint);
289
290 /**
291  * Initiate TLS handshake with selected cipher suite.
292  *
293  * @param[in] endpoint information of network address.
294  *
295  * @retval  ::CA_STATUS_OK    Successful.
296  * @retval  ::CA_STATUS_FAILED Operation failed.
297  */
298 CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
299
300 /**
301  * Close the DTLS session.
302  *
303  * @param[in] endpoint  information of network address.
304  *
305  * @retval  ::CA_STATUS_OK    Successful.
306  * @retval  ::CA_STATUS_FAILED Operation failed.
307  */
308 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
309
310 /**
311  * Close the TLS session using UUID
312  *
313  * @param[in] identity  UUID of target device
314  * @param[in] idLength Byte length of 'identity'
315  *
316  * @retval  ::CA_STATUS_OK    Successful.
317  * @retval  ::CA_STATUS_FAILED Operation failed.
318  */
319 CAResult_t CAcloseSslConnectionUsingUuid(const uint8_t *identity, size_t idLength);
320
321 /**
322  * Close All of DTLS sessions.
323  */
324 void CAcloseSslConnectionAll(CATransportAdapter_t transportType);
325
326 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
327
328 /**
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.
335  *
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
342  */
343 typedef void (*SslExportKeysCallback_t)(const unsigned char* masterSecret,
344                                         const unsigned char* keyBlock,
345                                         size_t macLen, size_t keyLen, size_t ivLen);
346
347 /**
348  * API to set a export SSL(TLS/DTLS) key callback.
349  * This callback will be invoked when SSL handshake occured.
350  *
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)
354  *
355  * @return CA_STATUS_OK on success, otherwise fail.
356  */
357 CAResult_t CASetSslExportKeysCallback(SslExportKeysCallback_t exportKeysCb,
358                                       CASslEkcbProtocol_t protocol, CASslEkcbRole_t role);
359
360 void CAsetNoCertConfirmCallback(UserConfirmNoCertCallback noCertCallback);
361
362 #endif //__WITH_TLS__ or __WITH_DTLS__
363
364
365 #ifdef __cplusplus
366 } /* extern "C" */
367 #endif
368
369
370 #endif /* CA_SECURITY_INTERFACE_H_ */
371