replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / credentialgenerator.c
index 8e96518..a930e53 100644 (file)
  *
  * *****************************************************************/
 #include <string.h>
-
-#include "provisioningmanager.h"
 #include "credentialgenerator.h"
+#include "base64.h"
 #include "oic_malloc.h"
-#include "logger.h"
+#include "oic_string.h"
+#include "ocpayload.h"
+#include "payload_logging.h"
 #include "credresource.h"
 #include "ocrandom.h"
-#include "base64.h"
-#define TAG "SPProvisionAPI"
-#define KEY_LENGTH 16
+#include "srmutility.h"
+#include "stdbool.h"
+#include "securevirtualresourcetypes.h"
 
-SPResult SPGeneratePairWiseCredentials(OicSecCredType_t type, const OicUuid_t *ptDeviceId,
-                                       const OicUuid_t *firstDeviceId,
-                                       const OicUuid_t *secondDeviceId,
-                                       OicSecCred_t **firstCred,
-                                       OicSecCred_t **secondCred)
-{
+#define TAG "OIC_SRPAPI_CG"
 
-    if (NULL == ptDeviceId || NULL == firstDeviceId || NULL == secondDeviceId)
+OCStackResult PMGeneratePairWiseCredentials(OicSecCredType_t type, size_t keySize,
+        const OicUuid_t *ptDeviceId, const OicUuid_t *firstDeviceId,
+        const OicUuid_t *secondDeviceId, OicSecCred_t **firstCred, OicSecCred_t **secondCred)
+{
+    if (NULL == ptDeviceId || NULL == firstDeviceId || NULL == firstCred || NULL != *firstCred || \
+        NULL == secondDeviceId || NULL == secondCred || NULL != *secondCred)
     {
-        return SP_RESULT_INVALID_PARAM;
+        OIC_LOG(INFO, TAG, "Invalid params");
+        return OC_STACK_INVALID_PARAM;
     }
-    uint8_t privData[KEY_LENGTH] = {0,};
-    OCFillRandomMem(privData, KEY_LENGTH);
-
-    uint32_t outLen = 0;
-    char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(privData)) + 1] = {};
-    B64Result b64Ret = b64Encode(privData, sizeof(privData), base64Buff,
-                                sizeof(base64Buff), &outLen);
-    if (B64_OK != b64Ret)
+    if(!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
     {
-        OC_LOG(ERROR, TAG, "Error while encoding key");
-        return SP_RESULT_INTERNAL_ERROR;
+        OIC_LOG(INFO, TAG, "Invalid key size");
+        return OC_STACK_INVALID_PARAM;
     }
+    OCStackResult res = OC_STACK_ERROR;
+    OicSecCred_t *tempFirstCred = NULL;
+    OicSecCred_t *tempSecondCred = NULL;
+
+    size_t privDataKeySize = keySize;
+
+    uint8_t *privData = (uint8_t *)OICCalloc(privDataKeySize, sizeof(uint8_t));
+    VERIFY_NON_NULL(TAG, privData, ERROR);
+    OicSecKey_t privKey = {.data=privData, .len=keySize};
+
+    OCFillRandomMem(privData, privDataKeySize);
+
+    // TODO: currently owner array is 1. only provisioning tool's id.
+    tempFirstCred =  GenerateCredential(secondDeviceId, type, NULL, &privKey, ptDeviceId, NULL);
+    VERIFY_NON_NULL(TAG, tempFirstCred, ERROR);
+
+    // TODO: currently owner array is 1. only provisioning tool's id.
+    tempSecondCred =  GenerateCredential(firstDeviceId, type, NULL, &privKey, ptDeviceId, NULL);
+    VERIFY_NON_NULL(TAG, tempSecondCred, ERROR);
 
-    // TODO currently owner array is 1. only provisioning tool's id.
-    OicSecCred_t *tempFirstCred =  GenerateCredential(secondDeviceId, type, NULL, base64Buff, 1,
-                                   ptDeviceId);
-    if (NULL == tempFirstCred)
-    {
-        OC_LOG(ERROR, TAG, "Error while generating credential.");
-        return SP_RESULT_INTERNAL_ERROR;
-    }
-    // TODO currently owner array is 1. only provisioning tool's id.
-    OicSecCred_t *tempSecondCred =  GenerateCredential(firstDeviceId, type, NULL, base64Buff, 1,
-                                    ptDeviceId);
-    if (NULL == tempSecondCred)
-    {
-        DeleteCredList(tempFirstCred);
-        OC_LOG(ERROR, TAG, "Error while generating credential.");
-        return SP_RESULT_INTERNAL_ERROR;
-    }
     *firstCred = tempFirstCred;
     *secondCred = tempSecondCred;
-    return SP_RESULT_SUCCESS;
+    res = OC_STACK_OK;
+
+exit:
+    OICClearMemory(privData, privDataKeySize);
+    OICFree(privData);
+
+    if(res != OC_STACK_OK)
+    {
+        OICFree(tempFirstCred);
+        OICFree(tempSecondCred);
+        *firstCred = NULL;
+        *secondCred = NULL;
+    }
+
+    return res;
 }