Imported Upstream version 1.0.0
[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 #ifdef __WITH_X509__
31 #include "pki.h"
32 #endif //__WITH_X509__
33
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39
40
41 #ifdef __WITH_DTLS__
42 /**
43  * @enum CADtlsPskCredType_t
44  * Type of PSK credential required during DTLS handshake
45  * It does not make much sense in bringing in all definitions from dtls.h into here.
46  * Therefore, redefining them here.
47  */
48 typedef enum
49 {
50     CA_DTLS_PSK_HINT,
51     CA_DTLS_PSK_IDENTITY,
52     CA_DTLS_PSK_KEY
53 } CADtlsPskCredType_t;
54
55 /**
56  * This internal callback is used by CA layer to
57  * retrieve PSK credentials from SRM.
58  *
59  * @param[in]  type type of PSK data required by CA layer during DTLS handshake set.
60  * @param[in]  desc    Additional request information.
61  * @param[in]  desc_len The actual length of desc.
62  * @param[out] result  Must be filled with the requested information.
63  * @param[in]  result_length  Maximum size of @p result.
64  *
65  * @return The number of bytes written to @p result or a value
66  *         less than zero on error.
67  */
68 typedef int (*CAGetDTLSPskCredentialsHandler)( CADtlsPskCredType_t type,
69                       const unsigned char *desc, size_t desc_len,
70                       unsigned char *result, size_t result_length);
71
72 /**
73  * Register callback to get DTLS PSK credentials.
74  * @param[in]   GetDTLSCredentials    GetDTLS Credetials callback.
75  * @return  ::CA_STATUS_OK
76  */
77 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSPskCredentialsHandler GetDTLSCredentials);
78
79 #endif //__WITH_DTLS__
80
81 #ifdef __WITH_X509__
82 /**
83  * Binary structure containing certificate chain and certificate credentials
84  * for this device.
85  */
86 typedef struct
87 {
88     // certificate message  for DTLS
89     unsigned char certificateChain[MAX_CERT_MESSAGE_LEN];
90     // length of the certificate message
91     uint32_t  certificateChainLen;
92     // number of certificates in  certificate message
93     uint8_t   chainLen;
94     // x component of EC public key
95     uint8_t   rootPublicKeyX[PUBLIC_KEY_SIZE / 2];
96     // y component of EC public key
97     uint8_t   rootPublicKeyY[PUBLIC_KEY_SIZE / 2];
98     // EC private key
99     uint8_t   devicePrivateKey[PRIVATE_KEY_SIZE];
100
101 } CADtlsX509Creds_t;
102
103 /**
104  * @brief   Callback function type for getting certificate credentials.
105  * @param   credInfo          [OUT] Certificate credentials info. Handler has to allocate new memory for
106  *                                  credInfo which is then freed by CA
107  * @return  NONE
108  */
109 typedef int (*CAGetDTLSX509CredentialsHandler)(CADtlsX509Creds_t *credInfo);
110 /**
111  * @brief   Callback function type for getting CRL.
112  * @param   crlInfo          [OUT] Certificate credentials info. Handler has to allocate new memory for
113  *                                  credInfo which is then freed by CA
114  * @return  NONE
115  */
116 typedef void (*CAGetDTLSCrlHandler)(ByteArray crlInfo);
117
118 /**
119  * @brief   Register callback to get DTLS Cert credentials.
120  * @param   GetCertCredentials   [IN] GetCert Credetials callback
121  * @return  #CA_STATUS_OK
122  */
123 CAResult_t CARegisterDTLSX509CredentialsHandler(CAGetDTLSX509CredentialsHandler GetX509Credentials);
124 /**
125  * @brief   Register callback to get CRL.
126  * @param   GetCrl   [IN] GetCrl callback
127  * @return  #CA_STATUS_OK
128  */
129 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetCrl);
130 #endif //__WITH_X509__
131
132
133 #ifdef __WITH_DTLS__
134
135 /**
136  * Select the cipher suite for dtls handshake.
137  *
138  * @param[in] cipher  cipher suite (Note : Make sure endianness).
139  *                    0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA
140  *                    0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
141  *                    0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
142  *
143  * @retval  ::CA_STATUS_OK    Successful.
144  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
145  * @retval  ::CA_STATUS_FAILED Operation failed.
146  */
147 CAResult_t CASelectCipherSuite(const uint16_t cipher);
148
149 /**
150  * Enable TLS_ECDH_anon_WITH_AES_128_CBC_SHA cipher suite in dtls.
151  *
152  * @param[in] enable  TRUE/FALSE enables/disables anonymous cipher suite.
153  *
154  * @retval  ::CA_STATUS_OK    Successful.
155  * @retval  ::CA_STATUS_FAILED Operation failed.
156  *
157  * @note anonymous cipher suite should only be enabled for 'JustWorks' provisioning.
158  */
159 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable);
160
161
162 /**
163  * Generate ownerPSK using PRF.
164  * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
165  *                    'ID of new device(Resource Server)',
166  *                    'ID of owner smart-phone(Provisioning Server)')
167  *
168  * @param[in] endpoint  information of network address.
169  * @param[in] label  Ownership transfer method e.g)"oic.sec.doxm.jw".
170  * @param[in] labelLen  Byte length of label.
171  * @param[in] rsrcServerDeviceID  ID of new device(Resource Server).
172  * @param[in] rsrcServerDeviceIDLen  Byte length of rsrcServerDeviceID.
173  * @param[in] provServerDeviceID  label of previous owner.
174  * @param[in] provServerDeviceIDLen  byte length of provServerDeviceID.
175  * @param[in,out] ownerPSK  Output buffer for owner PSK.
176  * @param[in] ownerPSKSize  Byte length of the ownerPSK to be generated.
177  *
178  * @retval  ::CA_STATUS_OK    Successful.
179  * @retval  ::CA_STATUS_FAILED Operation failed.
180  */
181 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t *endpoint,
182                               const uint8_t* label, const size_t labelLen,
183                               const uint8_t* rsrcServerDeviceID,
184                               const size_t rsrcServerDeviceIDLen,
185                               const uint8_t* provServerDeviceID,
186                               const size_t provServerDeviceIDLen,
187                               uint8_t* ownerPSK, const size_t ownerPSKSize);
188
189 /**
190  * Initiate DTLS handshake with selected cipher suite.
191  *
192  * @param[in] endpoint  information of network address.
193  *
194  * @retval  ::CA_STATUS_OK    Successful.
195  * @retval  ::CA_STATUS_FAILED Operation failed.
196  */
197 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint);
198
199 /**
200  * Close the DTLS session.
201  *
202  * @param[in] endpoint  information of network address.
203  *
204  * @retval  ::CA_STATUS_OK    Successful.
205  * @retval  ::CA_STATUS_FAILED Operation failed.
206  */
207 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint);
208
209 #endif /* __WITH_DTLS__ */
210
211
212 #ifdef __cplusplus
213 } /* extern "C" */
214 #endif
215
216
217 #endif /* CA_SECURITY_INTERFACE_H_ */
218