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