From f849ddefea68e5668900f274976d440931818ef3 Mon Sep 17 00:00:00 2001 From: Joonghwan Lee Date: Fri, 21 Oct 2016 15:58:39 +0900 Subject: [PATCH] Fix to get only single credential entry Old function(GetCredResourceDataByCredId) return not only the indicated credential entry but also all linked entries. Change-Id: I0466b4c25a21395f08664a11ecfa6fd51a94b258 Signed-off-by: Joonghwan Lee Reviewed-on: https://gerrit.iotivity.org/gerrit/13547 Tested-by: jenkins-iotivity Reviewed-by: Kevin Kane Reviewed-by: Randeep Singh --- .../csdk/security/include/internal/credresource.h | 6 ++- .../provisioning/src/secureresourceprovider.c | 5 +- resource/csdk/security/src/credresource.c | 63 ++++++++++++++++++++-- 3 files changed, 67 insertions(+), 7 deletions(-) mode change 100644 => 100755 resource/csdk/security/src/credresource.c diff --git a/resource/csdk/security/include/internal/credresource.h b/resource/csdk/security/include/internal/credresource.h index 427efc7..670afc7 100644 --- a/resource/csdk/security/include/internal/credresource.h +++ b/resource/csdk/security/include/internal/credresource.h @@ -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. diff --git a/resource/csdk/security/provisioning/src/secureresourceprovider.c b/resource/csdk/security/provisioning/src/secureresourceprovider.c index 3a8f242..a71da0b 100644 --- a/resource/csdk/security/provisioning/src/secureresourceprovider.c +++ b/resource/csdk/security/provisioning/src/secureresourceprovider.c @@ -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); diff --git a/resource/csdk/security/src/credresource.c b/resource/csdk/security/src/credresource.c old mode 100644 new mode 100755 index 9819887..e53b2f4 --- a/resource/csdk/security/src/credresource.c +++ b/resource/csdk/security/src/credresource.c @@ -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; } -- 2.7.4