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 "caipinterface.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
31 * @brief Logging tag for module name
33 #define NET_DTLS_TAG "NET_DTLS"
36 * @var g_caDtlsContext
37 * @brief global context which holds dtls context and cache list information.
39 static stCADtlsContext_t *g_caDtlsContext = NULL;
42 * @var g_dtlsContextMutex
43 * @brief Mutex to synchronize access to g_caDtlsContext.
45 static ca_mutex g_dtlsContextMutex = NULL;
48 * @var g_getCredentialsCallback
49 * @brief callback to get DTLS credentials
51 static CAGetDTLSCredentialsHandler g_getCredentialsCallback = NULL;
53 static CAEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer)
55 uint32_t list_index = 0;
56 uint32_t list_length = 0;
60 OIC_LOG(ERROR, NET_DTLS_TAG, "CAPeerInfoListContains invalid parameters");
64 CAEndpoint_t *peerInfo;
65 list_length = u_arraylist_length(g_caDtlsContext->peerInfoList);
66 for (list_index = 0; list_index < list_length; list_index++)
68 peerInfo = (CAEndpoint_t *)u_arraylist_get(g_caDtlsContext->peerInfoList, list_index);
74 if((0 == strncmp(peer->addr, peerInfo->addr, MAX_ADDR_STR_SIZE_CA)) &&
75 (peer->port == peerInfo->port))
83 static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint32_t port,
84 const unsigned char *id, uint16_t id_length)
90 || CA_MAX_ENDPOINT_IDENTITY_LEN < id_length)
92 OIC_LOG(ERROR, NET_DTLS_TAG, "CAAddIdToPeerInfoList invalid parameters");
93 return CA_STATUS_INVALID_PARAM;
96 CAEndpoint_t *peer = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
99 OIC_LOG(ERROR, NET_DTLS_TAG, "peerInfo malloc failed!");
100 return CA_MEMORY_ALLOC_FAILED;
103 OICStrcpy(peer->addr, sizeof(peer->addr), peerAddr);
106 memcpy(peer->identity.id, id, id_length);
107 peer->identity.id_length = id_length;
109 if(NULL != GetPeerInfo(peer))
111 OIC_LOG(ERROR, NET_DTLS_TAG, "CAAddIdToPeerInfoList peer already exist");
113 return CA_STATUS_FAILED;
116 CAResult_t result = u_arraylist_add(g_caDtlsContext->peerInfoList, (void *)peer);
117 if (CA_STATUS_OK != result)
119 OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_add failed!");
126 static void CAFreePeerInfoList()
128 uint32_t list_length = u_arraylist_length(g_caDtlsContext->peerInfoList);
129 for (uint32_t list_index = 0; list_index < list_length; list_index++)
131 CAEndpoint_t *peerInfo = (CAEndpoint_t *)u_arraylist_get(
132 g_caDtlsContext->peerInfoList, list_index);
135 u_arraylist_free(&(g_caDtlsContext->peerInfoList));
136 g_caDtlsContext->peerInfoList = NULL;
139 static void CARemovePeerFromPeerInfoList(const char * addr, uint32_t port)
141 if (NULL == addr || 0 >= port)
143 OIC_LOG(ERROR, NET_DTLS_TAG, "CADTLSGetPeerPSKId invalid parameters");
147 uint32_t list_length = u_arraylist_length(g_caDtlsContext->peerInfoList);
148 for (uint32_t list_index = 0; list_index < list_length; list_index++)
150 CAEndpoint_t *peerInfo = (CAEndpoint_t *)u_arraylist_get(
151 g_caDtlsContext->peerInfoList,list_index);
152 if (NULL == peerInfo)
156 if((0 == strncmp(addr, peerInfo->addr, MAX_ADDR_STR_SIZE_CA)) &&
157 (port == peerInfo->port))
159 OICFree(u_arraylist_remove(g_caDtlsContext->peerInfoList, list_index));
165 static int CASizeOfAddrInfo(stCADtlsAddrInfo_t *addrInfo)
167 VERIFY_NON_NULL_RET(addrInfo, NET_DTLS_TAG, "addrInfo is NULL" , DTLS_FAIL);
169 switch (addrInfo->addr.st.ss_family)
173 return sizeof (struct sockaddr_in);
177 return sizeof (struct sockaddr_in6);
184 return sizeof (struct sockaddr_storage);
187 static eDtlsRet_t CAAdapterNetDtlsEncryptInternal(const stCADtlsAddrInfo_t *dstSession,
188 uint8_t *data, uint32_t dataLen)
190 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
192 VERIFY_NON_NULL_RET(dstSession, NET_DTLS_TAG, "Param dstSession is NULL" , DTLS_FAIL);
193 VERIFY_NON_NULL_RET(data, NET_DTLS_TAG, "Param data is NULL" , DTLS_FAIL);
197 OIC_LOG(ERROR, NET_DTLS_TAG, "Given Packet length is equal to zero.");
201 if (NULL == g_caDtlsContext)
203 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
207 int retLen = dtls_write(g_caDtlsContext->dtlsContext, (session_t *)dstSession, data,
209 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "dtls_write retun len [%d]", retLen);
212 // A new DTLS session was initiated by tinyDTLS library and wait for callback.
213 return DTLS_SESSION_INITIATED;
215 else if (dataLen == retLen)
217 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
220 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT FAILURE");
224 static eDtlsRet_t CAAdapterNetDtlsDecryptInternal(const stCADtlsAddrInfo_t *srcSession,
225 uint8_t *buf, uint32_t bufLen)
227 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
229 VERIFY_NON_NULL_RET(srcSession, NET_DTLS_TAG, "Param srcSession is NULL", DTLS_FAIL);
230 VERIFY_NON_NULL_RET(buf, NET_DTLS_TAG, "Param buf is NULL", DTLS_FAIL);
234 OIC_LOG(ERROR, NET_DTLS_TAG, "Given Packet length is equal to zero.");
238 eDtlsRet_t ret = DTLS_FAIL;
240 if (dtls_handle_message(g_caDtlsContext->dtlsContext, (session_t *)srcSession, buf, bufLen) == 0)
242 OIC_LOG(DEBUG, NET_DTLS_TAG, "dtls_handle_message success");
246 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
250 static void CAFreeCacheMsg(stCACacheMessage_t *msg)
252 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
253 VERIFY_NON_NULL_VOID(msg, NET_DTLS_TAG, "msg");
258 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
261 static void CAClearCacheList()
263 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
264 uint32_t list_index = 0;
265 uint32_t list_length = 0;
266 if (NULL == g_caDtlsContext)
268 OIC_LOG(ERROR, NET_DTLS_TAG, "Dtls Context is NULL");
271 list_length = u_arraylist_length(g_caDtlsContext->cacheList);
272 for (list_index = 0; list_index < list_length; list_index++)
274 stCACacheMessage_t *msg = (stCACacheMessage_t *)u_arraylist_get(g_caDtlsContext->cacheList,
281 u_arraylist_free(&g_caDtlsContext->cacheList);
282 g_caDtlsContext->cacheList = NULL;
283 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
286 static CAResult_t CADtlsCacheMsg(stCACacheMessage_t *msg)
288 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
290 if (NULL == g_caDtlsContext)
292 OIC_LOG(ERROR, NET_DTLS_TAG, "Dtls Context is NULL");
293 return CA_STATUS_FAILED;
296 CAResult_t result = u_arraylist_add(g_caDtlsContext->cacheList, (void *)msg);
297 if (CA_STATUS_OK != result)
299 OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_add failed!");
302 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
307 static bool CAIsAddressMatching(const stCADtlsAddrInfo_t *a, const stCADtlsAddrInfo_t *b)
309 if (a->size != b->size)
313 if (memcmp(&a->addr, &b->addr, a->size))
320 static void CASendCachedMsg(const stCADtlsAddrInfo_t *dstSession)
322 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
323 VERIFY_NON_NULL_VOID(dstSession, NET_DTLS_TAG, "Param dstSession is NULL");
325 uint32_t list_index = 0;
326 uint32_t list_length = 0;
327 list_length = u_arraylist_length(g_caDtlsContext->cacheList);
328 for (list_index = 0; list_index < list_length;)
330 stCACacheMessage_t *msg = (stCACacheMessage_t *)u_arraylist_get(g_caDtlsContext->cacheList,
332 if ((NULL != msg) && (true == CAIsAddressMatching(&(msg->destSession), dstSession)))
334 eDtlsRet_t ret = CAAdapterNetDtlsEncryptInternal(&(msg->destSession),
335 msg->data, msg->dataLen);
338 OIC_LOG(DEBUG, NET_DTLS_TAG, "CAAdapterNetDtlsEncryptInternal success");
342 OIC_LOG(ERROR, NET_DTLS_TAG, "CAAdapterNetDtlsEncryptInternal failed.");
345 if (u_arraylist_remove(g_caDtlsContext->cacheList, list_index))
348 // Reduce list length by 1 as we removed one element.
353 OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_remove failed.");
359 // Move to the next element
364 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
367 static int32_t CAReadDecryptedPayload(dtls_context_t *dtlsContext,
372 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
374 VERIFY_NON_NULL_RET(session, NET_DTLS_TAG, "Param Session is NULL", 0);
375 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "Decrypted buf len [%d]", bufLen);
377 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
379 CAEndpoint_t endpoint = { 0 };
380 CAConvertAddrToName(&(addrInfo->addr.st), endpoint.addr, &endpoint.port);
381 endpoint.flags = addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6;
382 endpoint.flags |= CA_SECURE;
383 endpoint.adapter = CA_ADAPTER_IP;
386 if (NULL == g_caDtlsContext)
388 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
392 if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type) &&
393 (NULL != g_caDtlsContext->adapterCallbacks[type].recvCallback))
395 // Get identity of the source of packet
396 CAEndpoint_t *peerInfo = GetPeerInfo(&endpoint);
399 endpoint.identity = peerInfo->identity;
402 g_caDtlsContext->adapterCallbacks[type].recvCallback(&endpoint, buf, bufLen);
406 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "recvCallback Callback or adapter type is wrong [%d]", type);
409 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
413 static int32_t CASendSecureData(dtls_context_t *dtlsContext,
418 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
420 VERIFY_NON_NULL_RET(session, NET_DTLS_TAG, "Param Session is NULL", -1);
421 VERIFY_NON_NULL_RET(buf, NET_DTLS_TAG, "Param buf is NULL", -1);
425 OIC_LOG(ERROR, NET_DTLS_TAG, "Encrypted Buffer length is equal to zero");
429 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
431 CAEndpoint_t endpoint;
432 CAConvertAddrToName(&(addrInfo->addr.st), endpoint.addr, &endpoint.port);
433 endpoint.flags = addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6;
434 endpoint.flags |= CA_SECURE;
435 endpoint.adapter = CA_ADAPTER_IP;
438 //Mutex is not required for g_caDtlsContext. It will be called in same thread.
439 if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type) &&
440 (NULL != g_caDtlsContext->adapterCallbacks[type].sendCallback))
442 g_caDtlsContext->adapterCallbacks[type].sendCallback(&endpoint, buf, bufLen);
446 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "send Callback or adapter type is wrong [%d]", type );
449 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
453 static int32_t CAHandleSecureEvent(dtls_context_t *dtlsContext,
455 dtls_alert_level_t level,
458 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
460 VERIFY_NON_NULL_RET(session, NET_DTLS_TAG, "Param Session is NULL", 0);
462 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "level [%d] code [%u]", level, code);
464 if (!level && (code == DTLS_EVENT_CONNECTED))
466 OIC_LOG(DEBUG, NET_DTLS_TAG, "Received DTLS_EVENT_CONNECTED. Sending Cached data");
467 CASendCachedMsg((stCADtlsAddrInfo_t *)session);
470 if(DTLS_ALERT_LEVEL_FATAL == level && DTLS_ALERT_CLOSE_NOTIFY == code)
472 OIC_LOG(INFO, NET_DTLS_TAG, "Peer closing connection");
474 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
475 char *peerAddr = inet_ntoa(addrInfo->addr.sin.sin_addr);
476 uint32_t port = ntohs(addrInfo->addr.sin.sin_port);
478 CARemovePeerFromPeerInfoList(peerAddr, port);
481 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
486 static int32_t CAGetPskCredentials(dtls_context_t *ctx,
487 const session_t *session,
488 dtls_credentials_type_t type,
489 const unsigned char *desc, size_t descLen,
490 unsigned char *result, size_t resultLen)
492 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
495 if(NULL == ctx || NULL == session || NULL == result)
497 OIC_LOG(ERROR, NET_DTLS_TAG, "CAGetPskCredentials invalid parameters");
501 VERIFY_NON_NULL_RET(g_getCredentialsCallback, NET_DTLS_TAG, "GetCredential callback", -1);
502 VERIFY_NON_NULL_RET(result, NET_DTLS_TAG, "result", -1);
504 CADtlsPskCredsBlob_t *credInfo = NULL;
506 // Retrieve the credentials blob from security module
507 g_getCredentialsCallback(&credInfo);
509 VERIFY_NON_NULL_RET(credInfo, NET_DTLS_TAG, "credInfo is NULL", -1);
510 if(NULL == credInfo->creds)
512 OIC_LOG(DEBUG, NET_DTLS_TAG, "credentials are NULL");
513 memset(credInfo, 0, sizeof(CADtlsPskCredsBlob_t));
518 if ((type == DTLS_PSK_HINT) || (type == DTLS_PSK_IDENTITY))
520 if (DTLS_PSK_ID_LEN <= resultLen)
522 memcpy(result, credInfo->identity, DTLS_PSK_ID_LEN);
523 ret = DTLS_PSK_ID_LEN;
527 if ((type == DTLS_PSK_KEY) && (desc) && (descLen == DTLS_PSK_PSK_LEN))
529 // Check if we have the credentials for the device with which we
530 // are trying to perform a handshake
532 for (index = 0; index < credInfo->num; index++)
534 if (memcmp(desc, credInfo->creds[index].id, DTLS_PSK_ID_LEN) == 0)
536 if(NULL != ctx->peers && DTLS_SERVER == ctx->peers->role )
538 // TODO SRM needs identity of the remote end-point with every data packet to
539 // perform access control management. tinyDTLS 'frees' the handshake parameters
540 // data structure when handshake completes. Therefore, currently this is a
541 // workaround to cache remote end-point identity when tinyDTLS asks for PSK.
542 stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
543 char *peerAddress = inet_ntoa(addrInfo->addr.sin.sin_addr);
544 uint32_t port = ntohs(addrInfo->addr.sin.sin_port);
546 CAResult_t result = CAAddIdToPeerInfoList(peerAddress, port, desc, descLen);
547 if(CA_STATUS_OK != result )
549 OIC_LOG(ERROR, NET_DTLS_TAG, "Fail to add peer id to gDtlsPeerInfoList");
552 memcpy(result, credInfo->creds[index].psk, DTLS_PSK_PSK_LEN);
553 ret = DTLS_PSK_PSK_LEN;
558 // Erase sensitive data before freeing.
559 memset(credInfo->creds, 0, sizeof(OCDtlsPskCreds) * (credInfo->num));
560 OICFree(credInfo->creds);
562 memset(credInfo, 0, sizeof(CADtlsPskCredsBlob_t));
569 void CADTLSSetAdapterCallbacks(CAPacketReceivedCallback recvCallback,
570 CAPacketSendCallback sendCallback,
571 CATransportAdapter_t type)
573 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
574 ca_mutex_lock(g_dtlsContextMutex);
575 if (NULL == g_caDtlsContext)
577 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
578 ca_mutex_unlock(g_dtlsContextMutex);
582 if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type))
584 // TODO: change the zeros to better values.
585 g_caDtlsContext->adapterCallbacks[0].recvCallback = recvCallback;
586 g_caDtlsContext->adapterCallbacks[0].sendCallback = sendCallback;
589 ca_mutex_unlock(g_dtlsContextMutex);
591 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
594 void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback)
596 // TODO Does this method needs protection of DtlsContextMutex ?
597 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
598 g_getCredentialsCallback = credCallback;
599 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
602 CAResult_t CADtlsSelectCipherSuite(const dtls_cipher_t cipher)
604 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN CADtlsSelectCipherSuite");
606 ca_mutex_lock(g_dtlsContextMutex);
607 if (NULL == g_caDtlsContext)
609 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
610 ca_mutex_unlock(g_dtlsContextMutex);
611 return CA_STATUS_FAILED;
613 dtls_select_cipher(g_caDtlsContext->dtlsContext, cipher);
614 ca_mutex_unlock(g_dtlsContextMutex);
616 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "Selected cipher suite is 0x%02X%02X\n",
617 ((uint8_t*)(&cipher))[1], ((uint8_t*)(&cipher))[0]);
618 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT CADtlsSelectCipherSuite");
620 return CA_STATUS_OK ;
623 CAResult_t CADtlsEnableAnonECDHCipherSuite(const bool enable)
625 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN CADtlsEnablesAnonEcdh");
627 ca_mutex_lock(g_dtlsContextMutex);
628 if (NULL == g_caDtlsContext)
630 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
631 ca_mutex_unlock(g_dtlsContextMutex);
632 return CA_STATUS_FAILED;
634 dtls_enables_anon_ecdh(g_caDtlsContext->dtlsContext,
635 enable == true ? DTLS_CIPHER_ENABLE : DTLS_CIPHER_DISABLE);
636 ca_mutex_unlock(g_dtlsContextMutex);
637 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA is %s",
638 enable ? "enabled" : "disabled");
640 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT CADtlsEnablesAnonEcdh");
642 return CA_STATUS_OK ;
645 CAResult_t CADtlsInitiateHandshake(const CAEndpoint_t *endpoint)
647 stCADtlsAddrInfo_t dst = {};
649 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN CADtlsInitiateHandshake");
653 return CA_STATUS_INVALID_PARAM;
656 CAConvertNameToAddr(endpoint->addr, endpoint->port, &(dst.addr.st));
658 dst.size = CASizeOfAddrInfo(&dst);
660 ca_mutex_lock(g_dtlsContextMutex);
661 if(NULL == g_caDtlsContext)
663 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
664 ca_mutex_unlock(g_dtlsContextMutex);
665 return CA_STATUS_FAILED;
668 if(0 > dtls_connect(g_caDtlsContext->dtlsContext, (session_t*)(&dst)))
670 OIC_LOG(ERROR, NET_DTLS_TAG, "Failed to connect");
671 ca_mutex_unlock(g_dtlsContextMutex);
672 return CA_STATUS_FAILED;
675 ca_mutex_unlock(g_dtlsContextMutex);
677 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT CADtlsInitiateHandshake");
682 CAResult_t CADtlsClose(const CAEndpoint_t *endpoint)
684 stCADtlsAddrInfo_t dst = {};
686 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN CADtlsDisconnect");
690 return CA_STATUS_INVALID_PARAM;
693 CAConvertNameToAddr(endpoint->addr, endpoint->port, &(dst.addr.st));
695 dst.size = CASizeOfAddrInfo(&dst);
697 ca_mutex_lock(g_dtlsContextMutex);
698 if (NULL == g_caDtlsContext)
700 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
701 ca_mutex_unlock(g_dtlsContextMutex);
702 return CA_STATUS_FAILED;
705 if (0 > dtls_close(g_caDtlsContext->dtlsContext, (session_t*)(&dst)))
707 OIC_LOG(ERROR, NET_DTLS_TAG, "Failed to close the session");
708 ca_mutex_unlock(g_dtlsContextMutex);
709 return CA_STATUS_FAILED;
712 ca_mutex_unlock(g_dtlsContextMutex);
714 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT CADtlsDisconnect");
719 CAResult_t CADtlsGenerateOwnerPSK(const CAEndpoint_t *endpoint,
720 const uint8_t* label, const size_t labelLen,
721 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
722 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
723 uint8_t* ownerPSK, const size_t ownerPSKSize)
725 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN CADtlsGenerateOwnerPSK");
727 if(!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
729 return CA_STATUS_INVALID_PARAM;
732 stCADtlsAddrInfo_t dst = {};
734 CAConvertNameToAddr(endpoint->addr, endpoint->port, &(dst.addr.st));
736 dst.size = CASizeOfAddrInfo(&dst);
738 ca_mutex_lock(g_dtlsContextMutex);
739 if (NULL == g_caDtlsContext)
741 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
742 ca_mutex_unlock(g_dtlsContextMutex);
743 return CA_STATUS_FAILED;
746 if( 0 == dtls_prf_with_current_keyblock(g_caDtlsContext->dtlsContext, (session_t*)(&dst),
747 label, labelLen, rsrcServerDeviceID, rsrcServerDeviceIDLen,
748 provServerDeviceID, provServerDeviceIDLen, ownerPSK, ownerPSKSize))
750 OIC_LOG(ERROR, NET_DTLS_TAG, "Failed to DTLS PRF");
751 ca_mutex_unlock(g_dtlsContextMutex);
752 return CA_STATUS_FAILED;
754 ca_mutex_unlock(g_dtlsContextMutex);
756 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT CADtlsGenerateOwnerPSK");
761 CAResult_t CAAdapterNetDtlsInit()
763 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
765 // Initialize mutex for DtlsContext
766 if (NULL == g_dtlsContextMutex)
768 g_dtlsContextMutex = ca_mutex_new();
769 VERIFY_NON_NULL_RET(g_dtlsContextMutex, NET_DTLS_TAG, "malloc failed",
770 CA_MEMORY_ALLOC_FAILED);
774 OIC_LOG(ERROR, NET_DTLS_TAG, "CAAdapterNetDtlsInit done already!");
778 // Lock DtlsContext mutex and create DtlsContext
779 ca_mutex_lock(g_dtlsContextMutex);
780 g_caDtlsContext = (stCADtlsContext_t *)OICCalloc(1, sizeof(stCADtlsContext_t));
782 if (NULL == g_caDtlsContext)
784 OIC_LOG(ERROR, NET_DTLS_TAG, "Context malloc failed");
785 ca_mutex_unlock(g_dtlsContextMutex);
786 ca_mutex_free(g_dtlsContextMutex);
787 return CA_MEMORY_ALLOC_FAILED;
791 // Create PeerInfoList and CacheList
792 g_caDtlsContext->peerInfoList = u_arraylist_create();
793 g_caDtlsContext->cacheList = u_arraylist_create();
795 if( (NULL == g_caDtlsContext->peerInfoList) ||
796 (NULL == g_caDtlsContext->cacheList))
798 OIC_LOG(ERROR, NET_DTLS_TAG, "peerInfoList or cacheList initialization failed!");
800 CAFreePeerInfoList();
801 OICFree(g_caDtlsContext);
802 g_caDtlsContext = NULL;
803 ca_mutex_unlock(g_dtlsContextMutex);
804 ca_mutex_free(g_dtlsContextMutex);
805 return CA_STATUS_FAILED;
808 // Initialize clock, crypto and other global vars in tinyDTLS library
811 // Create tinydtls Context
812 g_caDtlsContext->dtlsContext = dtls_new_context(g_caDtlsContext);
814 if (NULL == g_caDtlsContext->dtlsContext)
816 OIC_LOG(ERROR, NET_DTLS_TAG, "dtls_new_context failed");
817 ca_mutex_unlock(g_dtlsContextMutex);
818 CAAdapterNetDtlsDeInit();
819 return CA_STATUS_FAILED;
822 g_caDtlsContext->callbacks.write = CASendSecureData;
823 g_caDtlsContext->callbacks.read = CAReadDecryptedPayload;
824 g_caDtlsContext->callbacks.event = CAHandleSecureEvent;
825 g_caDtlsContext->callbacks.get_psk_info = CAGetPskCredentials;
827 dtls_set_handler(g_caDtlsContext->dtlsContext, &(g_caDtlsContext->callbacks));
828 ca_mutex_unlock(g_dtlsContextMutex);
829 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
833 void CAAdapterNetDtlsDeInit()
835 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
837 VERIFY_NON_NULL_VOID(g_caDtlsContext, NET_DTLS_TAG, "context is NULL");
838 VERIFY_NON_NULL_VOID(g_dtlsContextMutex, NET_DTLS_TAG, "context mutex is NULL");
840 //Lock DtlsContext mutex
841 ca_mutex_lock(g_dtlsContextMutex);
844 CAFreePeerInfoList();
847 // De-initialize tinydtls context
848 dtls_free_context(g_caDtlsContext->dtlsContext);
849 g_caDtlsContext->dtlsContext = NULL;
851 // De-initialize DtlsContext
852 OICFree(g_caDtlsContext);
853 g_caDtlsContext = NULL;
855 // Unlock DtlsContext mutex and de-initialize it
856 ca_mutex_unlock(g_dtlsContextMutex);
857 ca_mutex_free(g_dtlsContextMutex);
858 g_dtlsContextMutex = NULL;
860 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
863 CAResult_t CAAdapterNetDtlsEncrypt(const CAEndpoint_t *endpoint,
864 void *data, uint32_t dataLen)
867 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
869 VERIFY_NON_NULL_RET(endpoint, NET_DTLS_TAG,"Param remoteAddress is NULL",
870 CA_STATUS_INVALID_PARAM);
871 VERIFY_NON_NULL_RET(data, NET_DTLS_TAG, "Param data is NULL" ,
872 CA_STATUS_INVALID_PARAM);
876 OIC_LOG_V(ERROR, NET_DTLS_TAG, "dataLen is less than or equal zero [%d]", dataLen);
877 return CA_STATUS_FAILED;
880 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "Data to be encrypted dataLen [%d]", dataLen);
882 stCADtlsAddrInfo_t addrInfo = {};
884 CAConvertNameToAddr(endpoint->addr, endpoint->port, &(addrInfo.addr.st));
885 addrInfo.ifIndex = 0;
886 addrInfo.size = CASizeOfAddrInfo(&addrInfo);
888 ca_mutex_lock(g_dtlsContextMutex);
889 if(NULL == g_caDtlsContext)
891 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
892 ca_mutex_unlock(g_dtlsContextMutex);
893 return CA_STATUS_FAILED;
896 eDtlsRet_t ret = CAAdapterNetDtlsEncryptInternal(&addrInfo, data, dataLen);
897 if (ret == DTLS_SESSION_INITIATED)
899 stCACacheMessage_t *message = (stCACacheMessage_t *)OICCalloc(1, sizeof(stCACacheMessage_t));
902 OIC_LOG(ERROR, NET_DTLS_TAG, "calloc failed!");
903 ca_mutex_unlock(g_dtlsContextMutex);
904 return CA_MEMORY_ALLOC_FAILED;
907 message->data = (uint8_t *)OICCalloc(dataLen + 1, sizeof(uint8_t));
908 if (NULL == message->data)
910 OIC_LOG(ERROR, NET_DTLS_TAG, "calloc failed!");
912 ca_mutex_unlock(g_dtlsContextMutex);
913 return CA_MEMORY_ALLOC_FAILED;
915 memcpy(message->data, data, dataLen);
916 message->dataLen = dataLen;
917 message->destSession = addrInfo;
919 CAResult_t result = CADtlsCacheMsg(message);
920 if (CA_STATUS_OK != result)
922 OIC_LOG(DEBUG, NET_DTLS_TAG, "CADtlsCacheMsg failed!");
923 CAFreeCacheMsg(message);
925 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "OUT Initiating Dtls session [%d]", result);
926 ca_mutex_unlock(g_dtlsContextMutex);
930 ca_mutex_unlock(g_dtlsContextMutex);
934 OIC_LOG(ERROR, NET_DTLS_TAG, "OUT FAILURE");
935 return CA_STATUS_FAILED;
938 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
942 CAResult_t CAAdapterNetDtlsDecrypt(const CAEndpoint_t *endpoint,
943 uint8_t *data, uint32_t dataLen)
945 OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
946 VERIFY_NON_NULL_RET(endpoint, NET_DTLS_TAG, "endpoint is NULL" , CA_STATUS_INVALID_PARAM);
948 stCADtlsAddrInfo_t addrInfo = {};
950 CAConvertNameToAddr(endpoint->addr, endpoint->port, &(addrInfo.addr.st));
951 addrInfo.ifIndex = 0;
952 addrInfo.size = CASizeOfAddrInfo(&addrInfo);
954 ca_mutex_lock(g_dtlsContextMutex);
955 if (NULL == g_caDtlsContext)
957 OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
958 ca_mutex_unlock(g_dtlsContextMutex);
959 return CA_STATUS_FAILED;
962 eDtlsRet_t ret = CAAdapterNetDtlsDecryptInternal(&addrInfo, data, dataLen);
963 ca_mutex_unlock(g_dtlsContextMutex);
965 if (DTLS_OK == ret || DTLS_HS_MSG == ret)
967 OIC_LOG_V(DEBUG, NET_DTLS_TAG, "Successfully Decrypted or Handshake msg recvd [%d]", ret);
968 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
972 OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT FAILURE");
973 return CA_STATUS_FAILED;