Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / ck_manager / sample / provisioningclient.c
index 972a373..91a7619 100644 (file)
@@ -50,7 +50,7 @@
 
 static OicSecAcl_t        *gAcl = NULL;
 static OicSecCrl_t        *gCrl = NULL;
-static char PROV_TOOL_DB_FILE[] = "oic_svr_db_pt.json";
+static char PROV_TOOL_DB_FILE[] = "oic_svr_db_pt.dat";
 static const char* PRVN_DB_FILE_NAME = "oic_prvn_mng.db";
 static int gOwnershipState = 0;
 
@@ -80,9 +80,6 @@ static void deleteACL(OicSecAcl_t *acl)
         }
         OICFree((acl)->resources);
 
-        /* Clean Owners */
-        OICFree((acl)->owners);
-
         /* Clean ACL node itself */
         OICFree((acl));
 
@@ -205,7 +202,7 @@ static int InputACL(OicSecAcl_t *acl)
     {
         if (DASH != temp_id[i])
         {
-            if(j>UUID_LENGTH)
+            if(j >= UUID_LENGTH)
             {
                 printf("Invalid input\n");
                 return -1;
@@ -217,12 +214,17 @@ static int InputACL(OicSecAcl_t *acl)
     //Set Resource.
     printf("Num. of Resource : \n");
     ret = scanf("%zu", &acl->resourcesLen);
+    if(-1 == ret)
+    {
+        printf("Error while input\n");
+        return -1;
+    }
     printf("-URI of resource\n");
     printf("ex) /a/light (Max_URI_Length: 64 Byte )\n");
     acl->resources = (char **)OICCalloc(acl->resourcesLen, sizeof(char *));
     if (NULL == acl->resources)
     {
-        OC_LOG(ERROR, TAG, "Error while memory allocation");
+        OIC_LOG(ERROR, TAG, "Error while memory allocation");
         return -1;
     }
     for (size_t i = 0; i < acl->resourcesLen; i++)
@@ -244,7 +246,7 @@ static int InputACL(OicSecAcl_t *acl)
 
         if (NULL == acl->resources[i])
         {
-            OC_LOG(ERROR, TAG, "Error while memory allocation");
+            OIC_LOG(ERROR, TAG, "Error while memory allocation");
             return -1;
         }
     }
@@ -270,38 +272,28 @@ static int InputACL(OicSecAcl_t *acl)
     }
     while (0 != CalculateAclPermission(temp_pms, &(acl->permission)) );
     // Set Rowner
-    printf("Num. of Rowner : ");
-    ret = scanf("%zu", &acl->ownersLen);
     printf("-URN identifying the rowner\n");
     printf("ex) lightDeviceUUID0 (16 Numbers except to '-')\n");
-    acl->owners = (OicUuid_t *)OICCalloc(acl->ownersLen, sizeof(OicUuid_t));
-    if (NULL == acl->owners)
+
+    printf("Rowner : ");
+    char *ptr_temp_id = NULL;
+    ret = scanf("%19ms", &ptr_temp_id);
+    if (1 == ret)
+    {
+        OICStrcpy(temp_id, sizeof(temp_id), ptr_temp_id);
+        OICFree(ptr_temp_id);
+    }
+    else
     {
-        OC_LOG(ERROR, TAG, "Error while memory allocation");
+        printf("Error while input\n");
         return -1;
     }
-    for (size_t i = 0; i < acl->ownersLen; i++)
+    j = 0;
+    for (int k = 0; temp_id[k] != '\0'; k++)
     {
-        printf("[%zu]Rowner : ", i + 1);
-        char *ptr_temp_id = NULL;
-        ret = scanf("%19ms", &ptr_temp_id);
-        if (1 == ret)
-        {
-            OICStrcpy(temp_id, sizeof(temp_id), ptr_temp_id);
-            OICFree(ptr_temp_id);
-        }
-        else
-        {
-            printf("Error while input\n");
-            return -1;
-        }
-        j = 0;
-        for (int k = 0; temp_id[k] != '\0'; k++)
+        if (DASH != temp_id[k])
         {
-            if (DASH != temp_id[k])
-            {
-                acl->owners[i].id[j++] = temp_id[k];
-            }
+            acl->rownerID.id[j++] = temp_id[k];
         }
     }
     return 0;
@@ -446,19 +438,43 @@ static int InputCRL(OicSecCrl_t *crlRes)
    // const uint8_t revocationDatesContent[MAX_Revoked_NUMBER][DATE_LENGTH];
     uint32_t nuberOfRevoked = 0;
     printf("Enter number of Revoked certificates(1..%d)\n", MAX_Revoked_NUMBER);
-    scanf("%u", &nuberOfRevoked);
+    int ret = 0;
+    ret = scanf("%u", &nuberOfRevoked);
+    if(-1 == ret)
+    {
+        printf("Error while input\n");
+        return PKI_UNKNOWN_ERROR;
+    }
+
+    if((uint32_t)MAX_Revoked_NUMBER < nuberOfRevoked)
+    {
+        OIC_LOG(ERROR, TAG, "Wrong revoked certificate number");
+        return PKI_UNKNOWN_ERROR;
+    }
 
     for (size_t i = 0; i < nuberOfRevoked; ++i)
     {
         printf("Revoked certificate %d:", i);
         printf("Serial number (E. g.: 100):");
-        scanf("%u", &revokedNumbers[i]);
+        ret = scanf("%u", &revokedNumbers[i]);
+        if(-1 == ret)
+        {
+            printf("Error while input\n");
+            return PKI_UNKNOWN_ERROR;
+        }
+
         revocationDates[i] = (const uint8_t*)"130101000005Z";
     }
 
     crl.len = CRL_MIN_SIZE + nuberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4)/* + 1000*/;
     crl.data = (uint8_t *)OICCalloc(1, crl.len);
 
+    if (NULL == crl.data)
+    {
+        OIC_LOG(ERROR, TAG, "Error while memory allocation");
+        return PKI_MEMORY_ALLOC_FAILED;
+    }
+
     CHECK_CALL(CKMIssueCRL, uint8ThisUpdateTime, nuberOfRevoked, revokedNumbers,
             revocationDates, &crl);
     PRINT_BYTE_ARRAY("CRL:\n",crl);
@@ -481,6 +497,8 @@ static int InputCRL(OicSecCrl_t *crlRes)
 int main()
 {
     OCStackResult res = OC_STACK_OK;
+    OCProvisionDev_t* pDeviceList = NULL;
+    OCProvisionDev_t *pOwnedList = NULL;
 
     // Initialize Persistent Storage for SVR database
     OCPersistentStorage ps = { .open = client_fopen,
@@ -493,20 +511,19 @@ int main()
 
     if (OC_STACK_OK != OCInit(NULL, 0, OC_CLIENT_SERVER))
     {
-        OC_LOG(ERROR, TAG, "OCStack init error");
+        OIC_LOG(ERROR, TAG, "OCStack init error");
         goto error;
     }
     if(OC_STACK_OK != OCInitPM(PRVN_DB_FILE_NAME))
     {
-        OC_LOG(ERROR, TAG, "OC_PM init error");
+        OIC_LOG(ERROR, TAG, "OC_PM init error");
         goto error;
     }
 
-    OCProvisionDev_t* pDeviceList = NULL;
     res = OCDiscoverUnownedDevices(PREDEFINED_TIMEOUT, &pDeviceList);
     if(OC_STACK_OK != res)
     {
-        OC_LOG_V(ERROR, TAG, "Failed to PMDeviceDiscovery : %d", res);
+        OIC_LOG_V(ERROR, TAG, "Failed to PMDeviceDiscovery : %d", res);
         goto error;
     }
 
@@ -538,11 +555,11 @@ int main()
     res = OCDoOwnershipTransfer((void*)myContext, pDeviceList, OwnershipTransferCB);
     if(OC_STACK_OK == res)
     {
-        OC_LOG(INFO, TAG, "Request for ownership transfer is sent successfully.");
+        OIC_LOG(INFO, TAG, "Request for ownership transfer is sent successfully.");
     }
     else
     {
-        OC_LOG_V(ERROR, TAG, "Failed to OCDoOwnershipTransfer : %d", res);
+        OIC_LOG_V(ERROR, TAG, "Failed to OCDoOwnershipTransfer : %d", res);
     }
 
     gOwnershipState = 0;
@@ -550,7 +567,7 @@ int main()
     {
         if (OCProcess() != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "OCStack process error");
+            OIC_LOG(ERROR, TAG, "OCStack process error");
             goto error;
         }
         sleep(1);
@@ -558,7 +575,6 @@ int main()
 
 // Credential & ACL provisioning between two devices.
 
-    OCProvisionDev_t *pOwnedList = NULL;
     OCProvisionDev_t *pOwnedDevices [MAX_OWNED_DEVICE] = {0,};
     int nOwnedDevice = 0;
 
@@ -581,29 +597,46 @@ int main()
     }
     else
     {
-        OC_LOG(ERROR, TAG, "Error while Owned Device Discovery");
+        OIC_LOG(ERROR, TAG, "Error while Owned Device Discovery");
     }
 
     int Device1 = 0;
     int Device2 = 0;
+    int ret = 0;
 
     printf("Select 2 devices for Credential & ACL provisioning\n");
     printf("Device 1: ");
-    scanf("%d", &Device1);
+    ret = scanf("%d", &Device1);
+    if(-1 == ret)
+    {
+        printf("Error while input\n");
+        goto error;
+    }
+
     printf("Device 2: ");
-    scanf("%d", &Device2);
+    ret = scanf("%d", &Device2);
+    if(-1 == ret)
+    {
+        printf("Error while input\n");
+        goto error;
+    }
 
+    if( 0 > Device1 || 0 > Device2 || Device1 > nOwnedDevice || Device2 > nOwnedDevice)
+    {
+        OIC_LOG(ERROR, TAG, "Wrong devices number");
+        goto error;
+    }
 
     gAcl = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
     if (NULL == gAcl)
     {
-        OC_LOG(ERROR, TAG, "Error while memory allocation");
+        OIC_LOG(ERROR, TAG, "Error while memory allocation");
         goto error;
     }
 
     if (PKI_SUCCESS != InitCA())
     {
-        OC_LOG(ERROR, TAG, "CA init error");
+        OIC_LOG(ERROR, TAG, "CA init error");
         goto error;
     }
 
@@ -612,13 +645,13 @@ int main()
 
     res = OCProvisionCredentials(ctx, SIGNED_ASYMMETRIC_KEY, 0, pOwnedDevices[Device1],
                                                                 NULL, ProvisionCertCB);
-    if (OC_STACK_OK != res) OC_LOG_V(ERROR, TAG, "Failed to provision Device 1 : %d", res);
+    if (OC_STACK_OK != res) OIC_LOG_V(ERROR, TAG, "Failed to provision Device 1 : %d", res);
     gOwnershipState = 0;
     while ( gOwnershipState == 0 )
     {
         if (OCProcess() != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "OCStack process error");
+            OIC_LOG(ERROR, TAG, "OCStack process error");
             goto error;
         }
         sleep(1);
@@ -628,7 +661,7 @@ int main()
                                                                 NULL, ProvisionCertCB);
     if (OC_STACK_OK != res)
     {
-        OC_LOG_V(ERROR, TAG, "Failed to provision Device 2 : %d", res);
+        OIC_LOG_V(ERROR, TAG, "Failed to provision Device 2 : %d", res);
     }
 
     gOwnershipState = 0;
@@ -636,7 +669,7 @@ int main()
     {
         if (OCProcess() != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "OCStack process error");
+            OIC_LOG(ERROR, TAG, "OCStack process error");
             goto error;
         }
         sleep(1);
@@ -649,13 +682,13 @@ int main()
     }
     else
     {
-        OC_LOG(ERROR, TAG, "InputACL error");
+        OIC_LOG(ERROR, TAG, "InputACL error");
         goto error;
     }
     res = OCProvisionACL(ctx, pOwnedDevices[Device2], gAcl, &ProvisionAclCB);
     if (OC_STACK_OK != res)
     {
-        OC_LOG_V(ERROR, TAG, "Failed to ACL provision Device 2 : %d", res);
+        OIC_LOG_V(ERROR, TAG, "Failed to ACL provision Device 2 : %d", res);
     }
 
     gOwnershipState = 0;
@@ -663,29 +696,36 @@ int main()
     {
         if (OCProcess() != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "OCStack process error");
+            OIC_LOG(ERROR, TAG, "OCStack process error");
             goto error;
         }
         sleep(1);
     }
     gCrl = (OicSecCrl_t *)OICMalloc(sizeof(OicSecCrl_t));
+
+    if (NULL == gCrl)
+    {
+        OIC_LOG(ERROR, TAG, "Error while memory allocation");
+        goto error;
+    }
+
     if (PKI_SUCCESS != InputCRL(gCrl))
     {
-        OC_LOG(ERROR, TAG, "CA init error");
+        OIC_LOG(ERROR, TAG, "CA init error");
         goto error;
     }
 
     PRINT_BYTE_ARRAY("gCrl = \n", gCrl->CrlData);
 
     res = OCProvisionCRL(ctx, pOwnedDevices[Device2], gCrl, &ProvisionCrlCB);
-    if (OC_STACK_OK != res) OC_LOG_V(ERROR, TAG, "Failed to CRL provision Device 2 : %d", res);
+    if (OC_STACK_OK != res) OIC_LOG_V(ERROR, TAG, "Failed to CRL provision Device 2 : %d", res);
 
     gOwnershipState = 0;
     while (gOwnershipState == 0)
     {
         if (OCProcess() != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "OCStack process error");
+            OIC_LOG(ERROR, TAG, "OCStack process error");
             goto error;
         }
         sleep(1);
@@ -693,7 +733,7 @@ int main()
 
     if (OCStop() != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "OCStack process error");
+        OIC_LOG(ERROR, TAG, "OCStack process error");
         goto error;
     }