9b0b5e98d470a7a55fab8eae5d23102b49f24216
[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
31 #include "cacommon.h"
32 #include "byte_array.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38
39 /**
40  * @enum CADtlsPskCredType_t
41  * Type of PSK credential required during DTLS handshake
42  * It does not make much sense in bringing in all definitions from dtls.h into here.
43  * Therefore, redefining them here.
44  */
45 typedef enum
46 {
47     CA_DTLS_PSK_HINT,
48     CA_DTLS_PSK_IDENTITY,
49     CA_DTLS_PSK_KEY
50 } CADtlsPskCredType_t;
51
52 /**
53  * This internal callback is used by CA layer to
54  * retrieve PSK credentials from SRM.
55  *
56  * @param[in]  type type of PSK data required by CA layer during DTLS handshake set.
57  * @param[in]  desc    Additional request information.
58  * @param[in]  desc_len The actual length of desc.
59  * @param[out] result  Must be filled with the requested information.
60  * @param[in]  result_length  Maximum size of @p result.
61  *
62  * @return The number of bytes written to @p result or a value
63  *         less than zero on error.
64  */
65 typedef int (*CAgetPskCredentialsHandler)(CADtlsPskCredType_t type,
66               const uint8_t *desc, size_t desc_len,
67               uint8_t *result, size_t result_length);
68
69 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
70 #ifdef _ENABLE_MULTIPLE_OWNER_
71 /**
72  * API to get a secure connected peer information
73  *
74  * @param[in] peer peer information includs IP address and port.
75  *
76  * @return  secure connected peer information on success, otherwise NULL
77  */
78 const CASecureEndpoint_t *CAGetSecureEndpointData(const CAEndpoint_t *peer);
79 #endif //_ENABLE_MULTIPLE_OWNER_
80 #endif
81
82 /**
83  * This internal callback is used by CA layer to
84  * retrieve all credential types from SRM
85  *
86  * @param[out]  list of enabled credential types for CA handshake
87  *
88  */
89 typedef void (*CAgetCredentialTypesHandler)(bool * list);
90 /**
91  * Binary structure containing PKIX related info
92  * own certificate chain, public key, CA's and CRL's
93  */
94 typedef struct
95 {
96     // own certificate chain
97     ByteArray_t crt;
98     // own public key
99     ByteArray_t key;
100     // trusted CA's
101     ByteArray_t ca;
102     // trusted CRL's
103     ByteArray_t crl;
104 } PkiInfo_t;
105
106 /**
107  * Register callback to receive credential types.
108  * @param[in] credTypesCallback callback to get cerdential types
109  * @return ::CA_STATUS_OK
110  */
111 CAResult_t CAregisterGetCredentialTypesCallback(CAgetCredentialTypesHandler credTypesCallback);
112 /**
113  * Register callback to receive the result of TLS handshake.
114  * @param[in] tlsHandshakeCallback callback for get tls handshake result
115  * @return ::CA_STATUS_OK
116  */
117 CAResult_t CAregisterSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback);
118
119 /**
120  * Register callback to get TLS PSK credentials.
121  * @param[in]   getTLSCredentials    GetDTLS Credetials callback.
122  * @return  ::CA_STATUS_OK
123  */
124 CAResult_t CAregisterPskCredentialsHandler(CAgetPskCredentialsHandler getTlsCredentials);
125
126 /**
127  * @brief   Callback function type for getting PKIX info
128  *
129  * @param   inf[out]   PKIX related info
130  *
131  * @return  NONE
132  */
133 typedef void (*CAgetPkixInfoHandler)(PkiInfo_t * inf);
134
135 /**
136  * Register callback to get PKIX related info.
137  * @param[in]   getPkixInfoHandler    Get PKIX related info callback.
138  * @return  ::CA_STATUS_OK or appropriate error code.
139  */
140 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler);
141 /**
142  * Register callback to get types of TLS suites.
143  * @param[in]   getCredTypesHandler    Get types of TLS suites callback.
144  * @return  ::CA_STATUS_OK or appropriate error code.
145  */
146 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler getCredTypesHandler);
147
148 /**
149  * Select the cipher suite for dtls handshake.
150  *
151  * @param[in] cipher  cipher suite (Note : Make sure endianness).
152  *                    0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA
153  *                    0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
154  *                    0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
155  *
156  * @retval  ::CA_STATUS_OK    Successful.
157  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
158  * @retval  ::CA_STATUS_FAILED Operation failed.
159  */
160 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter);
161
162 /**
163  * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls.
164  *
165  * @param[in] enable  TRUE/FALSE enables/disables anonymous cipher suite.
166  *
167  * @retval  ::CA_STATUS_OK    Successful.
168  * @retval  ::CA_STATUS_FAILED Operation failed.
169  *
170  * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
171  */
172 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
173
174
175 /**
176  * Generate ownerPSK using PRF.
177  * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
178  *                    'ID of new device(Resource Server)',
179  *                    'ID of owner smart-phone(Provisioning Server)')
180  *
181  * @param[in] endpoint  information of network address.
182  * @param[in] label  Ownership transfer method e.g)"oic.sec.doxm.jw".
183  * @param[in] labelLen  Byte length of label.
184  * @param[in] rsrcServerDeviceID  ID of new device(Resource Server).
185  * @param[in] rsrcServerDeviceIDLen  Byte length of rsrcServerDeviceID.
186  * @param[in] provServerDeviceID  label of previous owner.
187  * @param[in] provServerDeviceIDLen  byte length of provServerDeviceID.
188  * @param[in,out] ownerPSK  Output buffer for owner PSK.
189  * @param[in] ownerPskSize  Byte length of the ownerPSK to be generated.
190  *
191  * @retval  ::CA_STATUS_OK    Successful.
192  * @retval  ::CA_STATUS_FAILED Operation failed.
193  */
194 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t *endpoint,
195                               const uint8_t* label, const size_t labelLen,
196                               const uint8_t* rsrcServerDeviceID,
197                               const size_t rsrcServerDeviceIDLen,
198                               const uint8_t* provServerDeviceID,
199                               const size_t provServerDeviceIDLen,
200                               uint8_t* ownerPSK, const size_t ownerPskSize);
201
202 /**
203  * Initiate DTLS handshake with selected cipher suite.
204  *
205  * @param[in] endpoint  information of network address.
206  *
207  * @retval  ::CA_STATUS_OK    Successful.
208  * @retval  ::CA_STATUS_FAILED Operation failed.
209  */
210 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint);
211
212 /**
213  * Close the DTLS session.
214  *
215  * @param[in] endpoint  information of network address.
216  *
217  * @retval  ::CA_STATUS_OK    Successful.
218  * @retval  ::CA_STATUS_FAILED Operation failed.
219  */
220 CAResult_t CAcloseSslSession(const CAEndpoint_t *endpoint);
221
222 /**
223  * Initiate TLS handshake with selected cipher suite.
224  *
225  * @param[in] endpoint information of network address.
226  *
227  * @retval  ::CA_STATUS_OK    Successful.
228  * @retval  ::CA_STATUS_FAILED Operation failed.
229  */
230 CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
231
232 /**
233  * Close the DTLS session.
234  *
235  * @param[in] endpoint  information of network address.
236  *
237  * @retval  ::CA_STATUS_OK    Successful.
238  * @retval  ::CA_STATUS_FAILED Operation failed.
239  */
240 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
241
242
243 /**
244  * Close All of DTLS sessions.
245  */
246 void CAcloseSslConnectionAll();
247
248 #ifdef __cplusplus
249 } /* extern "C" */
250 #endif
251
252
253 #endif /* CA_SECURITY_INTERFACE_H_ */
254