Merge branch 'security-basecamp' into master
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / caadapternetdtls.c
index 49c73d3..56a0f7d 100644 (file)
@@ -50,7 +50,7 @@ static ca_mutex g_dtlsContextMutex = NULL;
  */
 static CAGetDTLSCredentialsHandler g_getCredentialsCallback = NULL;
 
-static CAEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer)
+static CASecureEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer)
 {
     uint32_t list_index = 0;
     uint32_t list_length = 0;
@@ -61,18 +61,18 @@ static CAEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer)
         return NULL;
     }
 
-    CAEndpoint_t *peerInfo;
+    CASecureEndpoint_t *peerInfo = NULL;
     list_length = u_arraylist_length(g_caDtlsContext->peerInfoList);
     for (list_index = 0; list_index < list_length; list_index++)
     {
-        peerInfo = (CAEndpoint_t *)u_arraylist_get(g_caDtlsContext->peerInfoList, list_index);
+        peerInfo = (CASecureEndpoint_t *)u_arraylist_get(g_caDtlsContext->peerInfoList, list_index);
         if (NULL == peerInfo)
         {
             continue;
         }
 
-        if((0 == strncmp(peer->addr, peerInfo->addr, MAX_ADDR_STR_SIZE_CA)) &&
-                (peer->port == peerInfo->port))
+        if((0 == strncmp(peer->addr, peerInfo->endpoint.addr, MAX_ADDR_STR_SIZE_CA)) &&
+                (peer->port == peerInfo->endpoint.port))
         {
             return peerInfo;
         }
@@ -80,8 +80,8 @@ static CAEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer)
     return NULL;
 }
 
-static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint16_t port,
-        const unsigned char *id, uint16_t id_length, CATransportFlags_t flag)
+static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint32_t port,
+        const unsigned char *id, uint16_t id_length)
 {
     if(NULL == peerAddr
        || NULL == id
@@ -93,31 +93,28 @@ static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint16_t port,
         return CA_STATUS_INVALID_PARAM;
     }
 
-    CAEndpoint_t *peer = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
+    CASecureEndpoint_t *peer = (CASecureEndpoint_t *)OICCalloc(1, sizeof (CASecureEndpoint_t));
     if (NULL == peer)
     {
         OIC_LOG(ERROR, NET_DTLS_TAG, "peerInfo malloc failed!");
         return CA_MEMORY_ALLOC_FAILED;
     }
 
-    OICStrcpy(peer->addr, sizeof(peer->addr), peerAddr);
-    peer->port = port;
-    peer->flags = flag;
-    peer->flags |= CA_SECURE;
-    peer->adapter = CA_ADAPTER_IP;
+    OICStrcpy(peer->endpoint.addr, sizeof(peer->endpoint.addr), peerAddr);
+    peer->endpoint.port = port;
 
     memcpy(peer->identity.id, id, id_length);
     peer->identity.id_length = id_length;
 
-    if(NULL != GetPeerInfo(peer))
+    if (NULL != GetPeerInfo(&peer->endpoint))
     {
         OIC_LOG(ERROR, NET_DTLS_TAG, "CAAddIdToPeerInfoList peer already exist");
         OICFree(peer);
         return CA_STATUS_FAILED;
     }
 
-    CAResult_t result = u_arraylist_add(g_caDtlsContext->peerInfoList, (void *)peer);
-    if (CA_STATUS_OK != result)
+    bool result = u_arraylist_add(g_caDtlsContext->peerInfoList, (void *)peer);
+    if (!result)
     {
         OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_add failed!");
         OICFree(peer);
@@ -301,8 +298,8 @@ static CAResult_t CADtlsCacheMsg(stCACacheMessage_t *msg)
         return CA_STATUS_FAILED;
     }
 
-    CAResult_t result = u_arraylist_add(g_caDtlsContext->cacheList, (void *)msg);
-    if (CA_STATUS_OK != result)
+    bool result = u_arraylist_add(g_caDtlsContext->cacheList, (void *)msg);
+    if (!result)
     {
         OIC_LOG(ERROR, NET_DTLS_TAG, "u_arraylist_add failed!");
     }
@@ -385,12 +382,13 @@ static int32_t CAReadDecryptedPayload(dtls_context_t *context,
 
     stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
 
-    CAEndpoint_t endpoint = { 0 };
-    CAConvertAddrToName(&(addrInfo->addr.st), endpoint.addr, &endpoint.port);
-    endpoint.flags = addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6;
-    endpoint.flags |= CA_SECURE;
-    endpoint.adapter = CA_ADAPTER_IP;
-    int type = 0;
+    CASecureEndpoint_t sep =
+    { .endpoint =
+    { .adapter = CA_ADAPTER_IP, .flags =
+            ((addrInfo->addr.st.ss_family == AF_INET) ? CA_IPV4 : CA_IPV6) | CA_SECURE, .port = 0 },
+            .identity =
+            { 0 } };
+    CAConvertAddrToName(&(addrInfo->addr.st), sep.endpoint.addr, &sep.endpoint.port);
 
     if (NULL == g_caDtlsContext)
     {
@@ -398,17 +396,18 @@ static int32_t CAReadDecryptedPayload(dtls_context_t *context,
         return 0;
     }
 
+    int type = 0;
     if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type) &&
         (NULL != g_caDtlsContext->adapterCallbacks[type].recvCallback))
     {
         // Get identity of the source of packet
-        CAEndpoint_t *peerInfo = GetPeerInfo(&endpoint);
+        CASecureEndpoint_t *peerInfo = GetPeerInfo(&sep.endpoint);
         if (peerInfo)
         {
-            endpoint.identity = peerInfo->identity;
+            sep.identity = peerInfo->identity;
         }
 
-        g_caDtlsContext->adapterCallbacks[type].recvCallback(&endpoint, buf, bufLen);
+        g_caDtlsContext->adapterCallbacks[type].recvCallback(&sep, buf, bufLen);
     }
     else
     {
@@ -483,7 +482,7 @@ static int32_t CAHandleSecureEvent(dtls_context_t *context,
         OIC_LOG(INFO, NET_DTLS_TAG, "Peer closing connection");
 
         stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
-        char peerAddr[MAX_ADDR_STR_SIZE_CA] = {0};
+        char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 };
         uint16_t port = 0;
         CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port);
         CARemovePeerFromPeerInfoList(peerAddr, port);
@@ -550,15 +549,11 @@ static int32_t CAGetPskCredentials(dtls_context_t *ctx,
                     // data structure when handshake completes. Therefore, currently this is a
                     // workaround to cache remote end-point identity when tinyDTLS asks for PSK.
                     stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
-                    char peerAddress[MAX_ADDR_STR_SIZE_CA] = {0};
+                    char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 };
                     uint16_t port = 0;
-                    CAConvertAddrToName(&(addrInfo->addr.st), peerAddress, &port);
+                    CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port);
 
-                    CATransportFlags_t flag =
-                            addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6;
-
-                    CAResult_t result = CAAddIdToPeerInfoList(peerAddress,
-                            port, desc, descLen, flag);
+                    CAResult_t result = CAAddIdToPeerInfoList(peerAddr, port, desc, descLen);
                     if(CA_STATUS_OK != result )
                     {
                         OIC_LOG(ERROR, NET_DTLS_TAG, "Fail to add peer id to gDtlsPeerInfoList");
@@ -954,15 +949,15 @@ CAResult_t CAAdapterNetDtlsEncrypt(const CAEndpoint_t *endpoint,
     return CA_STATUS_OK;
 }
 
-CAResult_t CAAdapterNetDtlsDecrypt(const CAEndpoint_t *endpoint,
+CAResult_t CAAdapterNetDtlsDecrypt(const CASecureEndpoint_t *sep,
                                    uint8_t *data, uint32_t dataLen)
 {
     OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");
-    VERIFY_NON_NULL_RET(endpoint, NET_DTLS_TAG, "endpoint is NULL" , CA_STATUS_INVALID_PARAM);
+    VERIFY_NON_NULL_RET(sep, NET_DTLS_TAG, "endpoint is NULL" , CA_STATUS_INVALID_PARAM);
 
     stCADtlsAddrInfo_t addrInfo = { 0 };
 
-    CAConvertNameToAddr(endpoint->addr, endpoint->port, &(addrInfo.addr.st));
+    CAConvertNameToAddr(sep->endpoint.addr, sep->endpoint.port, &(addrInfo.addr.st));
     addrInfo.ifIndex = 0;
     addrInfo.size = CASizeOfAddrInfo(&addrInfo);