From 03adc4fa8aaf36f5de585be00efc6198b38ded14 Mon Sep 17 00:00:00 2001 From: Chul Lee Date: Wed, 5 Oct 2016 20:50:32 +0900 Subject: [PATCH] Update the OTM module to guarantee uniqueness of OwnerPSK on the PT side. Change-Id: I22c9e7c2e81b49bb14c90d5c89744ee478ab108d Signed-off-by: Chul Lee Reviewed-on: https://gerrit.iotivity.org/gerrit/12813 Tested-by: jenkins-iotivity Reviewed-by: Randeep Singh --- .../csdk/security/include/internal/credresource.h | 18 ++++++++- .../provisioning/src/ownershiptransfermanager.c | 36 +++++++++++++++++ resource/csdk/security/src/credresource.c | 45 ++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/resource/csdk/security/include/internal/credresource.h b/resource/csdk/security/include/internal/credresource.h index beab0c4..00708b1 100644 --- a/resource/csdk/security/include/internal/credresource.h +++ b/resource/csdk/security/include/internal/credresource.h @@ -109,11 +109,20 @@ OCStackResult AddCredential(OicSecCred_t * cred); /** * Function to remove the credential from SVR DB. * + * @param subject is the Credential Subject to be deleted. + * + * @return ::OC_STACK_OK for success, or errorcode otherwise. + */ +OCStackResult RemoveCredential(const OicUuid_t *subject); + +/** + * Function to remove the credential from SVR DB. + * * @param credId is the Credential ID to be deleted. * * @return ::OC_STACK_OK for success, or errorcode otherwise. */ -OCStackResult RemoveCredential(const OicUuid_t *credId); +OCStackResult RemoveCredentialByCredId(uint16_t credId); #if defined(__WITH_DTLS__) /** @@ -164,6 +173,13 @@ int GetDtlsX509Credentials(CADtlsX509Creds_t *credInfo); #endif /*__WITH_X509__*/ /** + * Function to getting credential list + * + * @return ::credential list + */ +const OicSecCred_t* GetCredList(); + +/** * Function to deallocate allocated memory to OicSecCred_t. * * @param cred pointer to cred type. diff --git a/resource/csdk/security/provisioning/src/ownershiptransfermanager.c b/resource/csdk/security/provisioning/src/ownershiptransfermanager.c index ce91f8c..aeb9430 100644 --- a/resource/csdk/security/provisioning/src/ownershiptransfermanager.c +++ b/resource/csdk/security/provisioning/src/ownershiptransfermanager.c @@ -51,6 +51,7 @@ #include "base64.h" #include "cJSON.h" #include "global.h" +#include "utlist.h" #include "srmresourcestrings.h" #include "doxmresource.h" @@ -461,6 +462,41 @@ static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo) OICFree(b64Buf); #endif //End of Test codes + //Finding previous ownerPSK. + const OicSecCred_t* credList = GetCredList(); + OicSecCred_t* prevCred = NULL; + uint16_t credId = 0; + LL_FOREACH(credList, prevCred) + { + //OwnerPSK's type is SYMMETRIC_PAIR_WISE_KEY + if (SYMMETRIC_PAIR_WISE_KEY == prevCred->credType && + 0 == memcmp(prevCred->subject.id, cred->subject.id, sizeof(cred->subject.id))) + { + credId = prevCred->credId; + break; + } + } + + //If duplicate owner PSK is exists, remove it. + if(0 < credId) + { + OIC_LOG(WARNING, TAG, "Duplicate OwnerPSK was detected."); + OIC_LOG(WARNING, TAG, "[Subject] : "); + OIC_LOG_BUFFER(WARNING, TAG, prevCred->subject.id, sizeof(prevCred->subject.id)); + OIC_LOG_V(WARNING, TAG, "[Encoding Type] : %d", prevCred->privateData.encoding); + OIC_LOG(WARNING, TAG, "[Private Data] : "); + OIC_LOG_BUFFER(WARNING, TAG, prevCred->privateData.data, prevCred->privateData.len); + OIC_LOG(WARNING, TAG, "Previous OwnerPSK will be removed."); + + res = RemoveCredentialByCredId(credId); + if(OC_STACK_RESOURCE_DELETED != res) + { + OIC_LOG(ERROR, TAG, "Failed to remove the previous OwnerPSK"); + DeleteCredList(cred); + goto exit; + } + } + res = AddCredential(cred); if(res != OC_STACK_OK) { diff --git a/resource/csdk/security/src/credresource.c b/resource/csdk/security/src/credresource.c index 1c31203..0b9d41a 100644 --- a/resource/csdk/security/src/credresource.c +++ b/resource/csdk/security/src/credresource.c @@ -1350,6 +1350,46 @@ OCStackResult RemoveCredential(const OicUuid_t *subject) } +OCStackResult RemoveCredentialByCredId(uint16_t credId) +{ + OCStackResult ret = OC_STACK_ERROR; + OicSecCred_t *cred = NULL; + OicSecCred_t *tempCred = NULL; + bool deleteFlag = false; + + OIC_LOG(INFO, TAG, "IN RemoveCredentialByCredId"); + + if ( 0 == credId) + { + return OC_STACK_INVALID_PARAM; + } + + + LL_FOREACH_SAFE(gCred, cred, tempCred) + { + if (cred->credId == credId) + { + OIC_LOG_V(DEBUG, TAG, "Credential(ID=%d) will be removed.", credId); + + LL_DELETE(gCred, cred); + FreeCred(cred); + deleteFlag = true; + } + } + + if (deleteFlag) + { + if (UpdatePersistentStorage(gCred)) + { + ret = OC_STACK_RESOURCE_DELETED; + } + } + OIC_LOG(INFO, TAG, "OUT RemoveCredentialByCredId"); + + return ret; + +} + /** * Remove all credential data on credential resource and persistent storage * @@ -1804,6 +1844,11 @@ OicSecCred_t* GetCredResourceData(const OicUuid_t* subject) return NULL; } +const OicSecCred_t* GetCredList() +{ + return gCred; +} + OicSecCred_t* GetCredResourceDataByCredId(const uint16_t credId) { OicSecCred_t *cred = NULL; -- 2.7.4