1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
20 #include "caadapternetdtls.h"
22 #include "cawifiinterface.h"
24 #include "oic_malloc.h"
28 * @brief Logging tag for module name
30 #define NET_DTLS_TAG "NET_DTLS"
33 * @var g_caDtlsContext
34 * @brief global context which holds dtls context and cache list information.
36 static stCADtlsContext_t *g_caDtlsContext = NULL;
39 * @var g_dtlsContextMutex
40 * @brief Mutex to synchronize access to g_caDtlsContext.
42 static u_mutex g_dtlsContextMutex = NULL;
45 * @var g_dtlsListMutex
46 * @brief Mutex to synchronize access to DTLS Cache.
48 static u_mutex g_dtlsListMutex = NULL;
51 * @var g_getCredentialsCallback
52 * @brief callback to get DTLS credentials
54 static CAGetDTLSCredentialsHandler g_getCredentialsCallback = NULL;
56 static eDtlsRet_t CAAdapterNetDtlsEncryptInternal(const stCADtlsAddrInfo_t *dstSession,
57 uint8_t *data, uint32_t dataLen)
59 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
61 VERIFY_NON_NULL_RET(dstSession, NET_DTLS_TAG, "Param dstSession is NULL" , DTLS_FAIL);
62 VERIFY_NON_NULL_RET(data, NET_DTLS_TAG, "Param data is NULL" , DTLS_FAIL);
66 OIC_LOG(ERROR, NET_DTLS_TAG, "Given Packet length is equal to zero.");
70 u_mutex_lock(g_dtlsContextMutex);
71 if (NULL == g_caDtlsContext)
73 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
74 u_mutex_unlock(g_dtlsContextMutex);
78 int retLen = dtls_write(g_caDtlsContext->dtlsContext, (session_t *)dstSession, data,
80 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "dtls_write retun len [%d]", retLen);
81 u_mutex_unlock(g_dtlsContextMutex);
85 // A new DTLS session was initiated by tinyDTLS library and wait for callback.
86 return DTLS_SESSION_INITIATED;
88 else if (dataLen == retLen)
90 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
93 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT FAILURE");
97 static eDtlsRet_t CAAdapterNetDtlsDecryptInternal(const stCADtlsAddrInfo_t *srcSession,
98 uint8_t *buf, uint32_t bufLen)
100 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
102 VERIFY_NON_NULL_RET(srcSession, NET_DTLS_TAG, "Param srcSession is NULL", DTLS_FAIL);
103 VERIFY_NON_NULL_RET(buf, NET_DTLS_TAG, "Param buf is NULL", DTLS_FAIL);
107 OIC_LOG(ERROR, NET_DTLS_TAG, "Given Packet length is equal to zero.");
111 eDtlsRet_t ret = DTLS_FAIL;
113 /// TODO: how to protect g_caDtlsContext as dtls_handle_message is blocking call
114 if (dtls_handle_message(g_caDtlsContext->dtlsContext, (session_t *)srcSession, buf, bufLen) == 0)
116 OIC_LOG(DEBUG, NET_DTLS_TAG, "dtls_handle_message success");
120 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
124 static void CAFreeCacheMsg(stCACacheMessage_t *msg)
126 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
127 VERIFY_NON_NULL_VOID(msg, NET_DTLS_TAG, "msg");
129 OICFree(msg->destSession);
133 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
136 static void CAClearCacheList()
138 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
139 uint32_t list_index = 0;
140 uint32_t list_length = 0;
141 u_mutex_lock(g_dtlsListMutex);
142 if (NULL == g_caDtlsContext)
144 OIC_LOG(ERROR, NET_DTLS_TAG, "Dtls Context is NULL");
145 u_mutex_unlock(g_dtlsListMutex);
148 list_length = u_arraylist_length(g_caDtlsContext->cacheList);
149 for (list_index = 0; list_index < list_length; list_index++)
151 stCACacheMessage_t *msg = (stCACacheMessage_t *)u_arraylist_get(g_caDtlsContext->cacheList,
158 u_arraylist_free(&g_caDtlsContext->cacheList);
159 g_caDtlsContext->cacheList = NULL;
160 u_mutex_unlock(g_dtlsListMutex);
161 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
164 static CAResult_t CADtlsCacheMsg(stCACacheMessage_t *msg)
166 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
168 u_mutex_lock(g_dtlsListMutex);
169 if (NULL == g_caDtlsContext)
171 OIC_LOG(ERROR, NET_DTLS_TAG, "Dtls Context is NULL");
172 u_mutex_unlock(g_dtlsListMutex);
173 return CA_STATUS_FAILED;
176 CAResult_t result = u_arraylist_add(g_caDtlsContext->cacheList, (void *)msg);
177 if (CA_STATUS_OK != result)
179 OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_add failed!");
181 u_mutex_unlock(g_dtlsListMutex);
183 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
188 static bool CAIsAddressMatching(const stCADtlsAddrInfo_t *a, const stCADtlsAddrInfo_t *b)
190 return (a->size == b->size) &&
191 (a->addr.sa.sa_family == b->addr.sa.sa_family) &&
192 (a->addr.sin.sin_port == b->addr.sin.sin_port) &&
193 memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr, sizeof(struct in_addr)) == 0;
196 static void CASendCachedMsg(const stCADtlsAddrInfo_t *dstSession)
198 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
199 VERIFY_NON_NULL_VOID(dstSession, NET_DTLS_TAG, "Param dstSession is NULL");
201 uint32_t list_index = 0;
202 uint32_t list_length = 0;
203 u_mutex_lock(g_dtlsListMutex);
204 list_length = u_arraylist_length(g_caDtlsContext->cacheList);
205 for (list_index = 0; list_index < list_length;)
207 stCACacheMessage_t *msg = (stCACacheMessage_t *)u_arraylist_get(g_caDtlsContext->cacheList,
209 if ((NULL != msg) && (true == CAIsAddressMatching(msg->destSession, dstSession)))
211 eDtlsRet_t ret = CAAdapterNetDtlsEncryptInternal(msg->destSession,
212 msg->data, msg->dataLen);
215 OIC_LOG(DEBUG, NET_DTLS_TAG, "CAAdapterNetDtlsEncryptInternal success");
219 OIC_LOG(ERROR, NET_DTLS_TAG, "CAAdapterNetDtlsEncryptInternal failed.");
222 if (u_arraylist_remove(g_caDtlsContext->cacheList, list_index))
225 // Reduce list length by 1 as we removed one element.
230 OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_remove failed.");
236 // Move to the next element
240 u_mutex_unlock(g_dtlsListMutex);
242 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
245 static int32_t CAReadDecryptedPayload(dtls_context_t *dtlsContext,
250 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
252 VERIFY_NON_NULL_RET(session, NET_DTLS_TAG, "Param Session is NULL", 0);
253 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "Decrypted buf len [%d]", bufLen);
255 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
257 char *remoteAddress = inet_ntoa(addrInfo->addr.sin.sin_addr);
258 uint32_t port = ntohs(addrInfo->addr.sin.sin_port);
259 eDtlsAdapterType_t type = (eDtlsAdapterType_t)addrInfo->ifIndex;
261 u_mutex_lock(g_dtlsContextMutex);
262 if (NULL == g_caDtlsContext)
264 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
265 u_mutex_unlock(g_dtlsContextMutex);
269 if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type) &&
270 (NULL != g_caDtlsContext->adapterCallbacks[type].recvCallback))
272 g_caDtlsContext->adapterCallbacks[type].recvCallback(remoteAddress, port,
277 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "recvCallback Callback or adapter type is wrong [%d]", type);
279 u_mutex_unlock(g_dtlsContextMutex);
281 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
285 static int32_t CASendSecureData(dtls_context_t *dtlsContext,
290 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
292 VERIFY_NON_NULL_RET(session, NET_DTLS_TAG, "Param Session is NULL", -1);
293 VERIFY_NON_NULL_RET(buf, NET_DTLS_TAG, "Param buf is NULL", -1);
297 OIC_LOG(ERROR, NET_DTLS_TAG, "Encrypted Buffer length is equal to zero");
301 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
303 char *remoteAddress = inet_ntoa(addrInfo->addr.sin.sin_addr);
304 uint16_t port = ntohs(addrInfo->addr.sin.sin_port);
305 eDtlsAdapterType_t type = (eDtlsAdapterType_t)addrInfo->ifIndex;
307 //Mutex is not required for g_caDtlsContext. It will be called in same thread.
309 if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type) &&
310 (NULL != g_caDtlsContext->adapterCallbacks[type].sendCallback))
312 sentLen = g_caDtlsContext->adapterCallbacks[type].sendCallback(remoteAddress, port,
317 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "send Callback or adapter type is wrong [%d]", type );
320 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "sent buffer length [%d]", sentLen);
322 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
327 static int32_t CAHandleSecureEvent(dtls_context_t *dtlsContext,
329 dtls_alert_level_t level,
332 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
334 VERIFY_NON_NULL_RET(session, NET_DTLS_TAG, "Param Session is NULL", 0);
336 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "level [%d] code [%u]", level, code);
338 if (!level && (code == DTLS_EVENT_CONNECTED))
340 OIC_LOG(DEBUG, NET_DTLS_TAG, "Received DTLS_EVENT_CONNECTED. Sending Cached data");
341 CASendCachedMsg((stCADtlsAddrInfo_t *)session);
344 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
349 static int32_t CAGetPskCredentials(dtls_context_t *ctx,
350 const session_t *session,
351 dtls_credentials_type_t type,
352 const unsigned char *desc, size_t descLen,
353 unsigned char *result, size_t resultLen)
355 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
359 VERIFY_NON_NULL_RET(g_getCredentialsCallback, NET_DTLS_TAG, "GetCredential callback", -1);
360 VERIFY_NON_NULL_RET(result, NET_DTLS_TAG, "result", -1);
362 OCDtlsPskCredsBlob *credInfo = NULL;
364 // Retrieve the credentials blob from security module
365 // OCGetDtlsPskCredentials(&credInfo);
366 g_getCredentialsCallback(&credInfo);
368 VERIFY_NON_NULL_RET(credInfo, NET_DTLS_TAG, "CAGetDtlsPskCredentials credInfo is NULL", -1);
370 if ((type == DTLS_PSK_HINT) || (type == DTLS_PSK_IDENTITY))
372 if (DTLS_PSK_ID_LEN <= resultLen)
374 memcpy(result, credInfo->identity, DTLS_PSK_ID_LEN);
375 ret = DTLS_PSK_ID_LEN;
379 if ((type == DTLS_PSK_KEY) && (desc) && (descLen == DTLS_PSK_PSK_LEN))
381 //Check if we have the credentials for the device with which we
382 //are trying to perform a handshake
384 for (index = 0; index < credInfo->num; index++)
386 if (memcmp(desc, credInfo->creds[index].id, DTLS_PSK_ID_LEN) == 0)
388 memcpy(result, credInfo->creds[index].psk, DTLS_PSK_PSK_LEN);
389 ret = DTLS_PSK_PSK_LEN;
397 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
398 CAPacketSendCallback sendCallback, eDtlsAdapterType_t type)
400 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
401 u_mutex_lock(g_dtlsContextMutex);
402 if (NULL == g_caDtlsContext)
404 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
405 u_mutex_unlock(g_dtlsContextMutex);
409 if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type))
411 g_caDtlsContext->adapterCallbacks[type].recvCallback = recvCallback;
412 g_caDtlsContext->adapterCallbacks[type].sendCallback = sendCallback;
415 u_mutex_unlock(g_dtlsContextMutex);
417 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
420 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback)
422 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
423 g_getCredentialsCallback = credCallback;
424 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
427 CAResult_t CAAdapterNetDtlsInit()
429 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
431 if (NULL == g_dtlsContextMutex)
433 g_dtlsContextMutex = u_mutex_new();
434 VERIFY_NON_NULL_RET(g_dtlsContextMutex, NET_DTLS_TAG, "malloc failed",
435 CA_MEMORY_ALLOC_FAILED);
439 OIC_LOG(ERROR, NET_DTLS_TAG, "CAAdapterNetDtlsInit done already!");
443 if (NULL == g_dtlsListMutex)
445 g_dtlsListMutex = u_mutex_new();
446 if (NULL == g_dtlsListMutex)
448 OIC_LOG(ERROR, NET_DTLS_TAG, "g_dtlsListMutex malloc failed");
449 u_mutex_free(g_dtlsContextMutex);
450 return CA_MEMORY_ALLOC_FAILED;
454 u_mutex_lock(g_dtlsContextMutex);
455 g_caDtlsContext = (stCADtlsContext_t *)OICCalloc(1, sizeof(stCADtlsContext_t));
457 if (NULL == g_caDtlsContext)
459 OIC_LOG(ERROR, NET_DTLS_TAG, "Context malloc failed");
460 u_mutex_free(g_dtlsListMutex);
461 u_mutex_unlock(g_dtlsContextMutex);
462 u_mutex_free(g_dtlsContextMutex);
463 return CA_MEMORY_ALLOC_FAILED;
466 u_mutex_lock(g_dtlsListMutex);
467 g_caDtlsContext->cacheList = u_arraylist_create();
469 if (NULL == g_caDtlsContext->cacheList)
471 OIC_LOG(ERROR, NET_DTLS_TAG, "cacheList initialization failed!");
472 u_mutex_unlock(g_dtlsListMutex);
473 u_mutex_free(g_dtlsListMutex);
474 u_mutex_unlock(g_dtlsContextMutex);
475 u_mutex_free(g_dtlsContextMutex);
476 OICFree(g_caDtlsContext);
477 g_caDtlsContext = NULL;
478 return CA_STATUS_FAILED;
480 u_mutex_unlock(g_dtlsListMutex);
481 // Initialize clock, crypto and other global vars in tinyDTLS library
484 g_caDtlsContext->dtlsContext = dtls_new_context(g_caDtlsContext);
486 if (NULL == g_caDtlsContext->dtlsContext)
488 OIC_LOG(ERROR, NET_DTLS_TAG, "dtls_new_context failed");
489 u_mutex_unlock(g_dtlsContextMutex);
490 CAAdapterNetDtlsDeInit();
491 return CA_STATUS_FAILED;
494 g_caDtlsContext->callbacks.write = CASendSecureData;
495 g_caDtlsContext->callbacks.read = CAReadDecryptedPayload;
496 g_caDtlsContext->callbacks.event = CAHandleSecureEvent;
497 g_caDtlsContext->callbacks.get_psk_info = CAGetPskCredentials;
499 dtls_set_handler(g_caDtlsContext->dtlsContext, &(g_caDtlsContext->callbacks));
500 u_mutex_unlock(g_dtlsContextMutex);
501 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
505 void CAAdapterNetDtlsDeInit()
507 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
509 VERIFY_NON_NULL_VOID(g_caDtlsContext, NET_DTLS_TAG, "context is NULL");
511 u_mutex_lock(g_dtlsContextMutex);
513 dtls_free_context(g_caDtlsContext->dtlsContext);
514 g_caDtlsContext->dtlsContext = NULL;
515 OICFree(g_caDtlsContext);
516 g_caDtlsContext = NULL;
517 u_mutex_unlock(g_dtlsContextMutex);
519 u_mutex_free(g_dtlsContextMutex);
520 g_dtlsContextMutex = NULL;
521 u_mutex_free(g_dtlsListMutex);
522 g_dtlsListMutex = NULL;
523 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
526 CAResult_t CAAdapterNetDtlsEncrypt(const char *remoteAddress,
531 eDtlsAdapterType_t adapterType)
534 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
536 VERIFY_NON_NULL_RET(remoteAddress, NET_DTLS_TAG,"Param remoteAddress is NULL",CA_STATUS_FAILED);
538 VERIFY_NON_NULL_RET(data, NET_DTLS_TAG, "Param data is NULL" , CA_STATUS_FAILED);
542 OIC_LOG_V(ERROR, NET_DTLS_TAG, "dataLen is less than or equal zero [%d]", dataLen);
543 return CA_STATUS_FAILED;
546 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "Data to be encrypted dataLen [%d]", dataLen);
548 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)OICCalloc(1, sizeof(stCADtlsAddrInfo_t));
550 VERIFY_NON_NULL_RET(addrInfo, NET_DTLS_TAG, "malloc failed" , CA_MEMORY_ALLOC_FAILED);
552 addrInfo->addr.sin.sin_family = AF_INET;
553 addrInfo->addr.sin.sin_port = htons(port);
554 // Conversion from ASCII format to Network format
555 if (inet_aton(remoteAddress, &addrInfo->addr.sin.sin_addr) == 0)
557 OIC_LOG(ERROR, NET_DTLS_TAG, "Failed to convert from ASCII to Network Address");
559 return CA_STATUS_FAILED;
561 addrInfo->size = sizeof(addrInfo->addr);
562 addrInfo->ifIndex = adapterType;
564 eDtlsRet_t ret = CAAdapterNetDtlsEncryptInternal(addrInfo, data, dataLen);
565 if (ret == DTLS_SESSION_INITIATED)
567 stCACacheMessage_t *message = (stCACacheMessage_t *)OICCalloc(1, sizeof(stCACacheMessage_t));
570 OIC_LOG(ERROR, NET_DTLS_TAG, "calloc failed!");
572 return CA_MEMORY_ALLOC_FAILED;
575 message->data = (uint8_t *)OICCalloc(dataLen + 1, sizeof(uint8_t));
576 if (NULL == message->data)
578 OIC_LOG(ERROR, NET_DTLS_TAG, "calloc failed!");
581 return CA_MEMORY_ALLOC_FAILED;
583 memcpy(message->data, data, dataLen);
584 message->dataLen = dataLen;
585 message->destSession = addrInfo;
587 CAResult_t result = CADtlsCacheMsg(message);
588 if (CA_STATUS_OK == result)
597 OIC_LOG(DEBUG, NET_DTLS_TAG, "CADtlsCacheMsg failed!");
598 CAFreeCacheMsg(message);
600 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "OUT Initiating Dtls session [%d]", result);
608 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
612 OIC_LOG(ERROR, NET_DTLS_TAG, "OUT FAILURE");
613 return CA_STATUS_FAILED;
617 CAResult_t CAAdapterNetDtlsDecrypt(const char *remoteAddress,
621 eDtlsAdapterType_t adapterType)
623 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
625 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)OICCalloc(1, sizeof(stCADtlsAddrInfo_t));
627 VERIFY_NON_NULL_RET(addrInfo, NET_DTLS_TAG, "calloc failed" , CA_MEMORY_ALLOC_FAILED);
629 addrInfo->addr.sin.sin_family = AF_INET;
630 addrInfo->addr.sin.sin_port = htons(port);
632 // Conversion from ASCII format to Network format
633 if (inet_aton(remoteAddress, &addrInfo->addr.sin.sin_addr) == 0)
635 OIC_LOG(ERROR, NET_DTLS_TAG, "Failed to convert from ASCII to Network Address");
637 return CA_STATUS_FAILED;
639 addrInfo->size = sizeof(addrInfo->addr);
640 addrInfo->ifIndex = adapterType;
642 eDtlsRet_t ret = CAAdapterNetDtlsDecryptInternal(addrInfo, data, dataLen);
645 if (DTLS_OK == ret || DTLS_HS_MSG == ret)
647 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "Successfully Decrypted or Handshake msg recvd [%d]", ret);
648 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
652 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT FAILURE");
653 return CA_STATUS_FAILED;