Move security identity out of CAEndpoint_t and OCDevAddr.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caadapternetdtls.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 #ifndef CA_ADAPTER_NET_DTLS_H_
21 #define CA_ADAPTER_NET_DTLS_H_
22
23 #include "dtls.h"
24 #include "uarraylist.h"
25 #include "camutex.h"
26 #include "caadapterutils.h"
27 #include "ocsecurityconfig.h"
28 #include "cainterface.h"
29 #include "cacommon.h"
30
31 /**
32  * Currently DTLS supported adapters(2) WIFI and ETHENET for linux platform.
33  */
34 #define MAX_SUPPORTED_ADAPTERS 2
35
36 /**
37  * The implementation will be provided by OIC RI layer.
38  */
39 extern void OCGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo);
40
41 typedef void (*CAPacketReceivedCallback)(const CASecureEndpoint_t *sep,
42                                          const void *data, uint32_t dataLength);
43
44 typedef void (*CAPacketSendCallback)(CAEndpoint_t *endpoint,
45                                          const void *data, uint32_t dataLength);
46
47 /**
48  * Data structure for holding the send and recv callbacks.
49  */
50 typedef struct CAAdapterCallbacks
51 {
52     CAPacketReceivedCallback recvCallback;  /**< Callback used to send data to upper layer. */
53     CAPacketSendCallback sendCallback;      /**< Callback used to send data to socket layer. */
54 } stCAAdapterCallbacks_t;
55
56 /**
57  * Data structure for holding the tinyDTLS interface related info.
58  */
59 typedef struct stCADtlsContext
60 {
61     u_arraylist_t *peerInfoList;         /**< peerInfo list which holds the mapping between
62                                               peer id to it's n/w address. */
63     u_arraylist_t *cacheList;            /**< PDU's are cached until DTLS session is formed. */
64     struct dtls_context_t *dtlsContext;  /**< Pointer to tinyDTLS context. */
65     struct stPacketInfo *packetInfo;     /**< used by callback during
66                                               decryption to hold address/length. */
67     dtls_handler_t callbacks;            /**< Pointer to callbacks needed by tinyDTLS. */
68     stCAAdapterCallbacks_t adapterCallbacks[MAX_SUPPORTED_ADAPTERS];
69 } stCADtlsContext_t;
70
71 /**
72  * Data structure for holding the decrypted data address
73  * and length provided by tinyDTLS callback interface.
74  */
75 typedef struct stPacketInfo
76 {
77     uint8_t *dataAddress;
78     uint16_t dataLen;
79 } stPacketInfo_t;
80
81 /**
82  * tinyDTLS library error codes.
83  *
84  */
85 typedef enum
86 {
87     DTLS_OK = 0,
88     DTLS_FAIL,
89     DTLS_SESSION_INITIATED,
90     DTLS_HS_MSG
91 } eDtlsRet_t;
92
93
94 /** Structure to have address information which will match with DTLS session_t structure.*/
95 typedef struct
96 {
97     socklen_t size;                 /**< Size of address. */
98     union
99     {
100         struct sockaddr     sa;
101         struct sockaddr_storage st;
102         struct sockaddr_in  sin;
103         struct sockaddr_in6 sin6;
104     } addr;                         /**< Address information. */
105     uint8_t ifIndex;                /**< Holds adapter index to get callback info. */
106 } stCADtlsAddrInfo_t;
107
108 /**
109  * structure to holds the information of cache message and address info.
110  */
111 typedef struct CACacheMessage
112 {
113     void *data;
114     uint32_t dataLen;
115     stCADtlsAddrInfo_t destSession;
116 } stCACacheMessage_t;
117
118
119 /**
120  * Used set send and recv callbacks for different adapters(WIFI,EtherNet).
121  *
122  * @param[in]  recvCallback    packet received callback.
123  * @param[in]  sendCallback    packet sent callback.
124  * @param[in]  type  type of adapter.
125  *
126  */
127 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
128                                CAPacketSendCallback sendCallback,
129                                CATransportAdapter_t type);
130
131 /**
132  * Register callback to get DTLS PSK credentials.
133  * @param[in]  credCallback    callback to get DTLS credentials.
134  */
135 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
136
137 /**
138  * Select the cipher suite for dtls handshake
139  *
140  * @param[in] cipher    cipher suite
141  *                             0xC018 : TLS_ECDH_anon_WITH_AES_128_CBC_SHA
142  *                             0xC0A8 : TLS_PSK_WITH_AES_128_CCM_8
143  *                             0xC0AE : TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
144  *
145  * @retval  ::CA_STATUS_OK for success, otherwise some error value
146  */
147 CAResult_t CADtlsSelectCipherSuite(const dtls_cipher_t cipher);
148
149 /**
150  * Enable anonymous ECDH cipher suite for dtls handshake
151  *
152  * @param[in] enable  TRUE/FALSE enables/disables anonymous cipher suite
153  *
154  * @retval  ::CA_STATUS_OK for success, otherwise some error value
155  */
156 CAResult_t CADtlsEnableAnonECDHCipherSuite(const bool enable);
157
158 /**
159  * Initiate DTLS handshake with selected cipher suite
160  *
161  * @param[in] endpoint  information of network address
162  *
163  * @retval  ::CA_STATUS_OK for success, otherwise some error value
164  */
165 CAResult_t CADtlsInitiateHandshake(const CAEndpoint_t *endpoint);
166
167 /**
168  * Close the DTLS session
169  *
170  * @param[in] endpoint  information of network address
171  *
172  * @retval  ::CA_STATUS_OK for success, otherwise some error value
173  */
174 CAResult_t CADtlsClose(const CAEndpoint_t *endpoint);
175
176 /**
177  * Generate ownerPSK using PRF
178  * OwnerPSK = TLS-PRF('master key' , 'oic.sec.doxm.jw',
179  *                                    'ID of new device(Resource Server)',
180  *                                    'ID of owner smart-phone(Provisioning Server)')
181  *
182  * @param[in] endpoint  information of network address
183  * @param[in] label  Ownership transfer method e.g)"oic.sec.doxm.jw"
184  * @param[in] labelLen  Byte length of label
185  * @param[in] rsrcServerDeviceID  ID of new device(Resource Server)
186  * @param[in] rsrcServerDeviceIDLen  Byte length of rsrcServerDeviceID
187  * @param[in] provServerDeviceID  label of previous owner
188  * @param[in] provServerDeviceIDLen  byte length of provServerDeviceID
189  * @param[in,out] ownerPSK  Output buffer for owner PSK
190  * @param[in] ownerPSKSize  Byte length of the ownerPSK to be generated
191  *
192  * @retval  ::CA_STATUS_OK for success, otherwise some error value
193  */
194 CAResult_t CADtlsGenerateOwnerPSK(const CAEndpoint_t *endpoint,
195                     const uint8_t* label, const size_t labelLen,
196                     const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
197                     const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
198                     uint8_t* ownerPSK, const size_t ownerPSKSize);
199 ;
200
201 /**
202  * initialize tinyDTLS library and other necessary initialization.
203  *
204  * @return  0 on success otherwise a positive error value.
205  * @retval  ::CA_STATUS_OK  Successful.
206  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
207  * @retval  ::CA_STATUS_FAILED Operation failed.
208  *
209  */
210 CAResult_t CAAdapterNetDtlsInit();
211
212 /**
213  * de-inits tinyDTLS library and free the allocated memory.
214  */
215 void CAAdapterNetDtlsDeInit();
216
217 /**
218  * Performs DTLS encryption of the CoAP PDU. If a DTLS session does not exist yet
219  * with the @dst, a DTLS handshake will be started. In case where a new DTLS handshake
220  * is started, pdu info is cached to be send when session setup is finished.
221  *
222  * @param[in]  endpoint  address to which data will be sent.
223  * @param[in]  port  port to which data will be sent.
224  * @param[in]  data  length of data.
225  * @param[in]  dataLen  length of given data
226  *
227  * @return  0 on success otherwise a positive error value.
228  * @retval  ::CA_STATUS_OK  Successful.
229  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
230  * @retval  ::CA_STATUS_FAILED Operation failed.
231  *
232  */
233
234 CAResult_t CAAdapterNetDtlsEncrypt(const CAEndpoint_t *endpoint,
235                                    void *data,
236                                    uint32_t dataLen);
237
238 /**
239  * Performs DTLS decryption of the data received on
240  * secure port. This method performs in-place decryption
241  * of the cipher-text buffer. If a DTLS handshake message
242  * is received or decryption failure happens, this method
243  * returns -1. If a valid application PDU is decrypted, it
244  * returns the length of the decrypted pdu.
245  *
246  * @return  0 on success otherwise a positive error value.
247  * @retval  ::CA_STATUS_OK  Successful.
248  * @retval  ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
249  * @retval  ::CA_STATUS_FAILED Operation failed.
250  *
251  */
252 CAResult_t CAAdapterNetDtlsDecrypt(const CASecureEndpoint_t *sep,
253                                    uint8_t *data,
254                                    uint32_t dataLen);
255
256 #endif /* CA_ADAPTER_NET_DTLS_H_ */
257
258