Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / ca_adapter_net_ssl.h
1 /* *****************************************************************
2  *
3  * Copyright 2016 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 #ifndef CA_ADAPTER_NET_SSL_H_
21 #define CA_ADAPTER_NET_SSL_H_
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif //__cplusplus
26
27 #include "caadapterutils.h"
28 #include "cainterface.h"
29
30 /**
31  * Currently TLS supported adapters(2) WIFI and ETHENET for linux platform.
32  */
33 #define MAX_SUPPORTED_ADAPTERS 2
34
35 typedef void (*CAPacketReceivedCallback)(const CASecureEndpoint_t *sep,
36                                          const void *data, uint32_t dataLength);
37
38 typedef void (*CAPacketSendCallback)(CAEndpoint_t *endpoint,
39                                          const void *data, uint32_t dataLength);
40
41 /**
42  * Select the cipher suite for dtls handshake
43  *
44  * @param[in] cipher    cipher suite
45  *                             0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256
46  *                             0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
47  *                             0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
48  *
49  * @retval  ::CA_STATUS_OK for success, otherwise some error value
50  */
51 CAResult_t CAsetTlsCipherSuite(const uint32_t cipher);
52
53 /**
54  * Used set send and recv callbacks for different adapters(WIFI,EtherNet).
55  *
56  * @param[in]  recvCallback    packet received callback.
57  * @param[in]  sendCallback    packet sent callback.
58  * @param[in]  type  type of adapter.
59  *
60  */
61 void CAsetSslAdapterCallbacks(CAPacketReceivedCallback recvCallback,
62                               CAPacketSendCallback sendCallback,
63                               CATransportAdapter_t type);
64
65 /**
66  * Register callback to get credentials types.
67  * @param[in]   credTypesCallback    callback to get credential types.
68  */
69 void CAsetCredentialTypesCallback(CAgetCredentialTypesHandler credTypesCallback);
70 /**
71  * Register callback to get credential types.
72  * @param[in]  typesCallback    callback to get credential types.
73  */
74 void CAsetSslCredentialsCallback(CAgetPskCredentialsHandler credCallback);
75
76 /**
77  * Close the TLS session
78  *
79  * @param[in] endpoint  information of network address
80  *
81  * @retval  ::CA_STATUS_OK for success, otherwise some error value
82  */
83 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
84
85 /**
86  * initialize mbedTLS library and other necessary initialization.
87  *
88  * @return  0 on success otherwise a positive error value.
89  * @retval  ::CA_STATUS_OK  Successful.
90  * @retval  ::CA_MEMORY_ALLOC_FAILED  Memory allocation failed.
91  * @retval  ::CA_STATUS_FAILED Operation failed.
92  *
93  */
94 CAResult_t CAinitSslAdapter();
95
96 /**
97  * de-inits mbedTLS library and free the allocated memory.
98  */
99 void CAdeinitSslAdapter();
100
101 /**
102  * Performs TLS encryption of the CoAP PDU.
103  *
104  * If a DTLS session does not exist yet with the @dst,
105  * a TLS handshake will be started. In case where a new TLS handshake
106  * is started, pdu info is cached to be send when session setup is finished.
107  *
108  * @param[in]  endpoint  address to which data will be sent.
109  * @param[in]  data  length of data.
110  * @param[in]  dataLen  length of given data
111  *
112  * @return  0 on success otherwise a positive error value.
113  * @retval  ::CA_STATUS_OK  Successful.
114  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
115  * @retval  ::CA_STATUS_FAILED Operation failed.
116  *
117  */
118
119 CAResult_t CAencryptSsl(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen);
120
121 /**
122  * Performs TLS decryption of the data.
123  *
124  * @param[in]  sep  address and flags for which data will be decrypted.
125  * @param[in]  data  length of data.
126  * @param[in]  dataLen  length of given data
127  *
128  * @return  0 on success otherwise a positive error value.
129  * @retval  ::CA_STATUS_OK  Successful.
130  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
131  * @retval  ::CA_STATUS_FAILED Operation failed.
132  *
133  */
134 CAResult_t CAdecryptSsl(const CASecureEndpoint_t *sep, uint8_t *data, uint32_t dataLen);
135
136 /**
137  * Initiate TLS handshake with selected cipher suite.
138  *
139  * @param[in] endpoint  information of network address
140  *
141  * @retval  ::CA_STATUS_OK for success, otherwise some error value
142  */
143 CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
144
145 /**
146  * Register callback to deliver the result of TLS handshake
147  * @param[in] tlsHandshakeCallback Callback to receive the result of TLS handshake.
148  */
149 void CAsetSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback);
150
151 /**
152  * Generate ownerPSK using PRF
153  * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
154  *                                    'ID of new device(Resource Server)',
155  *                                    'ID of owner smart-phone(Provisioning Server)')
156  *
157  * @param[in] endpoint  information of network address
158  * @param[in] label  Ownership transfer method e.g)"oic.sec.doxm.jw"
159  * @param[in] labelLen  Byte length of label
160  * @param[in] rsrcServerDeviceID  ID of new device(Resource Server)
161  * @param[in] rsrcServerDeviceIDLen  Byte length of rsrcServerDeviceID
162  * @param[in] provServerDeviceID  label of previous owner
163  * @param[in] provServerDeviceIDLen  byte length of provServerDeviceID
164  * @param[in,out] ownerPSK  Output buffer for owner PSK
165  * @param[in] ownerPSKSize  Byte length of the ownerPSK to be generated
166  *
167  * @retval  ::CA_STATUS_OK for success, otherwise some error value
168  */
169 CAResult_t CAsslGenerateOwnerPsk(const CAEndpoint_t *endpoint,
170                     const uint8_t* label, const size_t labelLen,
171                     const uint8_t* rsrcServerDeviceId, const size_t rsrcServerDeviceIdLen,
172                     const uint8_t* provServerDeviceId, const size_t provServerDeviceIdLen,
173                     uint8_t* ownerPsk, const size_t ownerPskSize);
174
175 #ifdef _ENABLE_MULTIPLE_OWNER_
176 /**
177  * Gets CA secure endpoint info corresponding for endpoint.
178  *
179  * @param[in]  peer    remote address
180  *
181  * @return  CASecureEndpoint or NULL
182  */
183 const CASecureEndpoint_t *GetCASecureEndpointData(const CAEndpoint_t* peer);
184 #endif
185
186 #ifdef __cplusplus
187 }
188 #endif //__cplusplus
189
190 #endif /* CA_ADAPTER_NET_SSL_H_ */
191
192