Fix to get only single credential entry
authorJoonghwan Lee <jh05.lee@samsung.com>
Fri, 21 Oct 2016 06:58:39 +0000 (15:58 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Wed, 26 Oct 2016 03:18:46 +0000 (03:18 +0000)
Old function(GetCredResourceDataByCredId) return not only the indicated credential entry but also all linked entries.

Change-Id: I0466b4c25a21395f08664a11ecfa6fd51a94b258
Signed-off-by: Joonghwan Lee <jh05.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13547
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Kevin Kane <kkane@microsoft.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/include/internal/credresource.h
resource/csdk/security/provisioning/src/secureresourceprovider.c
resource/csdk/security/src/credresource.c [changed mode: 0644->0755]

index 427efc7..670afc7 100644 (file)
@@ -57,14 +57,16 @@ OCStackResult DeInitCredResource();
 OicSecCred_t* GetCredResourceData(const OicUuid_t* subjectId);
 
 /**
- * This method is used by SRM to retrieve credential for given credId.
+ * This method is used by SRM to retrieve credential entry for given credId.
+ *
+ * @note Caller needs to release this memory by calling DeleteCredList().
  *
  * @param credId for which credential is required.
  *
  * @return reference to @ref OicSecCred_t, if credential is found, else NULL, if credential
  * not found.
  */
-OicSecCred_t* GetCredResourceDataByCredId(const uint16_t credId);
+OicSecCred_t* GetCredEntryByCredId(const uint16_t credId);
 
 /**
  * This function converts credential data into CBOR format.
index 3a8f242..a71da0b 100644 (file)
@@ -506,7 +506,7 @@ OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint1
         return OC_STACK_INVALID_PARAM;
     }
 
-    OicSecCred_t *trustCertChainCred = GetCredResourceDataByCredId(credId);
+    OicSecCred_t *trustCertChainCred = GetCredEntryByCredId(credId);
     if(NULL == trustCertChainCred)
     {
         OIC_LOG(ERROR, TAG, "Can not find matched Trust Cert. Chain.");
@@ -516,6 +516,7 @@ OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint1
     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
     if(!secPayload)
     {
+        DeleteCredList(trustCertChainCred);
         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
         return OC_STACK_NO_MEMORY;
     }
@@ -523,10 +524,12 @@ OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint1
     int secureFlag = 0;
     if(OC_STACK_OK != CredToCBORPayload(trustCertChainCred, &secPayload->securityData, &secPayload->payloadSize, secureFlag))
     {
+        DeleteCredList(trustCertChainCred);
         OCPayloadDestroy((OCPayload *)secPayload);
         OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
         return OC_STACK_NO_MEMORY;
     }
+    DeleteCredList(trustCertChainCred);
     OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
     OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
 
old mode 100644 (file)
new mode 100755 (executable)
index 9819887..e53b2f4
@@ -38,6 +38,7 @@
 #include "base64.h"
 #include "ocserverrequest.h"
 #include "oic_malloc.h"
+#include "oic_string.h"
 #include "ocpayload.h"
 #include "utlist.h"
 #include "credresource.h"
@@ -2074,21 +2075,75 @@ const OicSecCred_t* GetCredList()
     return gCred;
 }
 
-OicSecCred_t* GetCredResourceDataByCredId(const uint16_t credId)
+OicSecCred_t* GetCredEntryByCredId(const uint16_t credId)
 {
     OicSecCred_t *cred = NULL;
-    if ( 1 > credId)
+    OicSecCred_t *tmpCred = NULL;
+
+   if ( 1 > credId)
     {
        return NULL;
     }
 
-    LL_FOREACH(gCred, cred)
+    LL_FOREACH(gCred, tmpCred)
     {
-        if(cred->credId == credId)
+        if(tmpCred->credId == credId)
         {
+            cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
+            VERIFY_NON_NULL(TAG, cred, ERROR);
+
+            // common
+            cred->next = NULL;
+            cred->credId = tmpCred->credId;
+            cred->credType = tmpCred->credType;
+            memcpy(cred->subject.id, tmpCred->subject.id , sizeof(cred->subject.id));
+            memcpy(cred->rownerID.id, tmpCred->rownerID.id , sizeof(cred->rownerID.id));
+            if (tmpCred->period)
+            {
+                cred->period = OICStrdup(tmpCred->period);
+            }
+
+            // key data
+            if (tmpCred->privateData.data)
+            {
+                cred->privateData.data = (uint8_t *)OICCalloc(1, tmpCred->privateData.len);
+                VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
+
+                memcpy(cred->privateData.data, tmpCred->privateData.data, tmpCred->privateData.len);
+                cred->privateData.len = tmpCred->privateData.len;
+                cred->privateData.encoding = tmpCred->privateData.encoding;
+            }
+#if defined(__WITH_X509__) || defined(__WITH_TLS__)
+            else if (tmpCred->publicData.data)
+            {
+                cred->publicData.data = (uint8_t *)OICCalloc(1, tmpCred->publicData.len);
+                VERIFY_NON_NULL(TAG, cred->publicData.data, ERROR);
+
+                memcpy(cred->publicData.data, tmpCred->publicData.data, tmpCred->publicData.len);
+                cred->publicData.len = tmpCred->publicData.len;
+            }
+            else if (tmpCred->optionalData.data)
+            {
+                cred->optionalData.data = (uint8_t *)OICCalloc(1, tmpCred->optionalData.len);
+                VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR);
+
+                memcpy(cred->optionalData.data, tmpCred->optionalData.data, tmpCred->optionalData.len);
+                cred->optionalData.len = tmpCred->optionalData.len;
+                cred->optionalData.encoding = tmpCred->optionalData.encoding;
+            }
+
+            if (tmpCred->credUsage)
+            {
+                cred->credUsage = OICStrdup(tmpCred->credUsage);
+            }
+#endif /* __WITH_X509__  or __WITH_TLS__*/
+
             return cred;
         }
     }
+
+exit:
+    FreeCred(cred);
     return NULL;
 }