[IOT-1454] Change the Preconfigured-PIN OxM related defines to prevent conflict with...
authorChul Lee <chuls.lee@samsung.com>
Wed, 26 Oct 2016 05:22:41 +0000 (14:22 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Fri, 28 Oct 2016 04:34:48 +0000 (04:34 +0000)
  - Preconfigured-PIN OxM constant changed to 0xFF00
  - According to changes Preconfig-PIN OxM constant,
    OxM Callback array can't use anymore.
    So, Callback pointers are moved into the OTMContext.

Change-Id: Iefaf9de29bdb98809506df3b2efae35024cae4de
Signed-off-by: Chul Lee <chuls.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13681
Reviewed-by: Kevin Kane <kkane@microsoft.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/include/securevirtualresourcetypes.h
resource/csdk/security/provisioning/include/internal/ownershiptransfermanager.h
resource/csdk/security/provisioning/sample/provisioningclient.c
resource/csdk/security/provisioning/src/multipleownershiptransfermanager.c
resource/csdk/security/provisioning/src/ownershiptransfermanager.c
resource/csdk/security/src/srmresourcestrings.c

index 1641f48..0f2ac78 100644 (file)
@@ -270,11 +270,12 @@ typedef enum
 {
     OIC_JUST_WORKS                          = 0x0,
     OIC_RANDOM_DEVICE_PIN                   = 0x1,
-    OIC_MANUFACTURER_CERTIFICATE           = 0x2,
+    OIC_MANUFACTURER_CERTIFICATE            = 0x2,
+    OIC_DECENTRALIZED_PUBLIC_KEY            = 0x3,
+    OIC_OXM_COUNT,
 #ifdef _ENABLE_MULTIPLE_OWNER_
-    OIC_PRECONFIG_PIN                      = 0x3,
+    OIC_PRECONFIG_PIN                       = 0xFF00,
 #endif //_ENABLE_MULTIPLE_OWNER_
-    OIC_OXM_COUNT
 }OicSecOxm_t;
 
 typedef enum
index 546bb28..7d85cfb 100644 (file)
@@ -33,19 +33,8 @@ extern "C" {
 #define OXM_STRING_MAX_LENGTH 32\r
 #define WRONG_PIN_MAX_ATTEMP 5\r
 \r
-/**\r
- * Context for ownership transfer(OT)\r
- */\r
-typedef struct OTMContext{\r
-    void* userCtx;                            /**< Context for user.*/\r
-    OCProvisionDev_t* selectedDeviceInfo;     /**< Selected device info for OT. */\r
-    OicUuid_t subIdForPinOxm;                 /**< Subject Id which uses PIN based OTM. */\r
-    OCProvisionResultCB ctxResultCallback;    /**< Function pointer to store result callback. */\r
-    OCProvisionResult_t* ctxResultArray;      /**< Result array having result of all device. */\r
-    size_t ctxResultArraySize;                /**< No of elements in result array. */\r
-    bool ctxHasError;                         /**< Does OT process have any error. */\r
-    int attemptCnt;\r
-}OTMContext_t;\r
+typedef struct OTMCallbackData OTMCallbackData_t;\r
+typedef struct OTMContext OTMContext_t;\r
 \r
 /**\r
  * Do ownership transfer for the unowned devices.\r
@@ -81,14 +70,30 @@ typedef OCStackResult (*OTMCreatePayloadCallback)(OTMContext_t* otmCtx, uint8_t
 /**\r
  * Required callback for performing ownership transfer\r
  */\r
-typedef struct OTMCallbackData\r
+struct OTMCallbackData\r
 {\r
     OTMLoadSecret loadSecretCB;\r
     OTMCreateSecureSession createSecureSessionCB;\r
     OTMCreatePayloadCallback createSelectOxmPayloadCB;\r
     OTMCreatePayloadCallback createOwnerTransferPayloadCB;\r
-} OTMCallbackData_t;\r
+};\r
+\r
+/**\r
+ * Context for ownership transfer(OT)\r
+ */\r
+struct OTMContext{\r
+    void* userCtx;                            /**< Context for user.*/\r
+    OCProvisionDev_t* selectedDeviceInfo;     /**< Selected device info for OT. */\r
+    OicUuid_t subIdForPinOxm;                 /**< Subject Id which uses PIN based OTM. */\r
+    OCProvisionResultCB ctxResultCallback;    /**< Function pointer to store result callback. */\r
+    OCProvisionResult_t* ctxResultArray;      /**< Result array having result of all device. */\r
+    size_t ctxResultArraySize;                /**< No of elements in result array. */\r
+    bool ctxHasError;                         /**< Does OT process have any error. */\r
+    OTMCallbackData_t otmCallback; /**< OTM callbacks to perform the OT/MOT. **/\r
+    int attemptCnt;\r
+};\r
 \r
+// TODO: Remove this OTMSetOwnershipTransferCallbackData, Please see the jira ticket IOT-1484\r
 /**\r
  * Set the callbacks for ownership transfer\r
  *\r
@@ -98,6 +103,15 @@ typedef struct OTMCallbackData
  */\r
 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData);\r
 \r
+/**\r
+ * API to assign the OTMCallback for each OxM.\r
+ *\r
+ * @param[out] callbacks Instance of OTMCallback_t\r
+ * @param[in] oxm Ownership transfer method\r
+ * @return  OC_STACK_OK on success\r
+ */\r
+OCStackResult OTMSetOTCallback(OicSecOxm_t oxm, OTMCallbackData_t* callbacks);\r
+\r
 #ifdef __cplusplus\r
 }\r
 #endif\r
index c9e3b23..855148b 100644 (file)
@@ -1228,13 +1228,15 @@ static int selectMultipleOwnershipTrnasferMethod(void)
         printf("     Entered Wrong Number. Please Enter Again\n");
     }
 
+    const int preconfOxm = 4;
     int oxm = 0;
     for( ; ; )
     {
-        printf("   0. (Not Supported)\n");
-        printf("   1. Random PIN OxM\n");
-        printf("   2. (Not Supported)\n");
-        printf("   3. Pre-Configured PIN OxM\n");
+        printf("   %d. (Not Supported)\n", OIC_JUST_WORKS);
+        printf("   %d. Random PIN OxM\n", OIC_RANDOM_DEVICE_PIN);
+        printf("   %d. (Not Supported)\n", OIC_MANUFACTURER_CERTIFICATE);
+        printf("   %d. (Not Supported)\n", OIC_DECENTRALIZED_PUBLIC_KEY);
+        printf("   %d. Pre-Configured PIN OxM\n", OIC_PRECONFIG_PIN);
         printf("   > Enter Number of  OxM for Multiple Ownership Transfer : ");
         for(int ret=0; 1!=ret; )
         {
index 492c184..03e4eec 100644 (file)
@@ -272,7 +272,7 @@ OCStackResult MOTAddMOTMethod(void *ctx, OCProvisionDev_t *targetDeviceInfo,
 
     OIC_LOG(DEBUG, TAG, "IN MOTAddMOTMethod");
 
-    VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > newOxm), ERROR);
+    VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > newOxm || OIC_PRECONFIG_PIN == newOxm), ERROR);
     VERIFY_NON_NULL(TAG, targetDeviceInfo, ERROR);
     postOxmRes = OC_STACK_INVALID_CALLBACK;
     VERIFY_NON_NULL(TAG, resultCallback, ERROR);
@@ -484,35 +484,6 @@ exit:
 static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
                                                     OCProvisionDev_t* selectedDevice);
 
-/**
- * Array to store the callbacks for each owner transfer method.
- */
-static OTMCallbackData_t g_MOTCbDatas[OIC_OXM_COUNT] = {
-        //Just works
-        {.loadSecretCB = LoadSecretJustWorksCallback,
-          .createSecureSessionCB = CreateSecureSessionJustWorksCallback,
-          .createSelectOxmPayloadCB = NULL,
-          .createOwnerTransferPayloadCB = NULL},
-
-          //Random PIN
-        {.loadSecretCB = InputPinCodeCallback,
-          .createSecureSessionCB = CreateSecureSessionRandomPinCallback,
-          .createSelectOxmPayloadCB = NULL,
-          .createOwnerTransferPayloadCB = NULL},
-
-        //Manufacturer Cert
-        {.loadSecretCB = NULL,
-          .createSecureSessionCB = NULL,
-          .createSelectOxmPayloadCB = NULL,
-          .createOwnerTransferPayloadCB = NULL},
-
-          //Preconfig PIN
-        {.loadSecretCB = LoadPreconfPinCodeCallback,
-          .createSecureSessionCB = CreateSecureSessionPreconfPinCallback,
-          .createSelectOxmPayloadCB = NULL,
-          .createOwnerTransferPayloadCB = NULL},
-};
-
 static OTMContext_t* g_MotCtx = NULL;
 
 static bool IsComplete(OTMContext_t* otmCtx)
@@ -1015,8 +986,18 @@ static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
         OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
     }
 
-    size_t oxmSel = (size_t)(selectedDevice->doxm->oxmSel);
-    OIC_LOG_V(DEBUG, TAG, "Multiple Ownership Transfer method = %d", selectedDevice->doxm->oxmSel);
+    OicSecOxm_t oxmSel = selectedDevice->doxm->oxmSel;
+    OIC_LOG_V(DEBUG, TAG, "Multiple Ownership Transfer method = %d", (int)oxmSel);
+
+    res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &motCtx->otmCallback);
+    if(OC_STACK_OK != res)
+    {
+        OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
+        return res;
+    }
+    //Only two functions required for MOT
+    VERIFY_NON_NULL(TAG, motCtx->otmCallback.loadSecretCB, ERROR);
+    VERIFY_NON_NULL(TAG, motCtx->otmCallback.createSecureSessionCB, ERROR);
 
     if(OIC_PRECONFIG_PIN != oxmSel && OIC_RANDOM_DEVICE_PIN != oxmSel)
     {
@@ -1024,7 +1005,7 @@ static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
         return OC_STACK_ERROR;
     }
 
-    if(OIC_RANDOM_DEVICE_PIN == selectedDevice->doxm->oxmSel)
+    if(OIC_RANDOM_DEVICE_PIN == oxmSel)
     {
         if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm))
         {
@@ -1032,13 +1013,13 @@ static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
         }
     }
 
-    res = g_MOTCbDatas[oxmSel].loadSecretCB(motCtx);
+    res = motCtx->otmCallback.loadSecretCB(motCtx);
     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
 
     //Save the current context instance to use on the dtls handshake callback
     g_MotCtx = motCtx;
 
-    res = g_MOTCbDatas[oxmSel].createSecureSessionCB(motCtx);
+    res = motCtx->otmCallback.createSecureSessionCB(motCtx);
     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
 
     OIC_LOG(INFO, TAG, "OUT StartMultipleOwnershipTransfer");
index 977a732..6ca41d9 100644 (file)
 #define TAG "OTM"
 
 /**
- * Array to store the callbacks for each owner transfer method.
+ * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
  */
-static OTMCallbackData_t g_OTMCbDatas[OIC_OXM_COUNT] = {
-        //Just works
-        {.loadSecretCB = LoadSecretJustWorksCallback,
-          .createSecureSessionCB = CreateSecureSessionJustWorksCallback,
-          .createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload,
-          .createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload},
-
-          //Random PIN
-        {.loadSecretCB = InputPinCodeCallback,
-          .createSecureSessionCB = CreateSecureSessionRandomPinCallback,
-          .createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload,
-          .createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload},
-
-        //Manufacturer Cert
-        {.loadSecretCB = NULL,
-          .createSecureSessionCB = NULL,
-          .createSelectOxmPayloadCB = NULL,
-          .createOwnerTransferPayloadCB = NULL},
+static OTMContext_t* g_otmCtx = NULL;
+
+OCStackResult OTMSetOTCallback(OicSecOxm_t oxm, OTMCallbackData_t* callbacks)
+{
+    OCStackResult res = OC_STACK_INVALID_PARAM;
+
+    OIC_LOG(INFO, TAG, "IN OTMSetOTCallback");
 
+    VERIFY_NON_NULL(TAG, callbacks, ERROR);
 #ifdef _ENABLE_MULTIPLE_OWNER_
-          //Preconfig PIN
-        {.loadSecretCB = LoadPreconfPinCodeCallback,
-          .createSecureSessionCB = CreateSecureSessionPreconfPinCallback,
-          .createSelectOxmPayloadCB = CreatePreconfPinBasedSelectOxmPayload,
-          .createOwnerTransferPayloadCB = CreatePreconfPinBasedOwnerTransferPayload},
+    VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm || OIC_PRECONFIG_PIN == oxm), ERROR);
+#else
+    VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm), ERROR);
 #endif //_ENABLE_MULTIPLE_OWNER_
-};
 
-/**
- * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
- */
-static OTMContext_t* g_otmCtx = NULL;
+    switch(oxm)
+    {
+    case OIC_JUST_WORKS:
+        callbacks->loadSecretCB = LoadSecretJustWorksCallback;
+        callbacks->createSecureSessionCB = CreateSecureSessionJustWorksCallback;
+        callbacks->createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
+        callbacks->createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
+        break;
+    case OIC_RANDOM_DEVICE_PIN:
+        callbacks->loadSecretCB = InputPinCodeCallback;
+        callbacks->createSecureSessionCB = CreateSecureSessionRandomPinCallback;
+        callbacks->createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
+        callbacks->createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
+        break;
+    case OIC_MANUFACTURER_CERTIFICATE:
+        OIC_LOG(ERROR, TAG, "OIC_MANUFACTURER_CERTIFICATE not supported yet.");
+        return OC_STACK_INVALID_METHOD;
+    case OIC_DECENTRALIZED_PUBLIC_KEY:
+        OIC_LOG(ERROR, TAG, "OIC_DECENTRALIZED_PUBLIC_KEY not supported yet.");
+        return OC_STACK_INVALID_METHOD;
+#ifdef _ENABLE_MULTIPLE_OWNER_
+    case OIC_PRECONFIG_PIN:
+        callbacks->loadSecretCB = LoadPreconfPinCodeCallback;
+        callbacks->createSecureSessionCB = CreateSecureSessionPreconfPinCallback;
+        callbacks->createSelectOxmPayloadCB = CreatePreconfPinBasedSelectOxmPayload;
+        callbacks->createOwnerTransferPayloadCB = CreatePreconfPinBasedOwnerTransferPayload;
+        break;
+#endif //_ENABLE_MULTIPLE_OWNER_
+    default:
+        OIC_LOG_V(ERROR, TAG, "Unknown OxM : %d", (int)oxm);
+        return OC_STACK_INVALID_PARAM;
+        break;
+    }
+
+    res = OC_STACK_OK;
+exit:
+    OIC_LOG(INFO, TAG, "OUT OTMSetOTCallback");
+    return res;
+}
+
 
 /**
  * Function to select appropriate  provisioning method.
@@ -139,15 +162,6 @@ static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethod
         }
     }
 
-    if(NULL == g_OTMCbDatas[(*selectedMethod)].loadSecretCB ||
-       NULL == g_OTMCbDatas[(*selectedMethod)].createSecureSessionCB ||
-       NULL == g_OTMCbDatas[(*selectedMethod)].createSelectOxmPayloadCB ||
-       NULL == g_OTMCbDatas[(*selectedMethod)].createOwnerTransferPayloadCB)
-    {
-        OIC_LOG_V(ERROR, TAG, "Please make sure the OxM(%d)'s callback registration", (int)(*selectedMethod));
-        return OC_STACK_INVALID_CALLBACK;
-    }
-
     OIC_LOG(DEBUG, TAG, "OUT SelectProvisioningMethod");
 
     return OC_STACK_OK;
@@ -752,9 +766,9 @@ static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle
 
         //DTLS Handshake
         //Load secret for temporal secure session.
-        if(g_OTMCbDatas[selOxm].loadSecretCB)
+        if(otmCtx->otmCallback.loadSecretCB)
         {
-            res = g_OTMCbDatas[selOxm].loadSecretCB(otmCtx);
+            res = otmCtx->otmCallback.loadSecretCB(otmCtx);
             if(OC_STACK_OK != res)
             {
                 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
@@ -767,9 +781,9 @@ static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle
         g_otmCtx = otmCtx;
 
         //Try DTLS handshake to generate secure session
-        if(g_OTMCbDatas[selOxm].createSecureSessionCB)
+        if(otmCtx->otmCallback.createSecureSessionCB)
         {
-            res = g_OTMCbDatas[selOxm].createSecureSessionCB(otmCtx);
+            res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
             if(OC_STACK_OK != res)
             {
                 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
@@ -1347,14 +1361,16 @@ static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
         return OC_STACK_ERROR;
     }
     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
     if(!secPayload)
     {
         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
         return OC_STACK_NO_MEMORY;
     }
+
     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
-    OCStackResult res = g_OTMCbDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
+    OCStackResult res = otmCtx->otmCallback.createSelectOxmPayloadCB(otmCtx,
             &secPayload->securityData, &secPayload->payloadSize);
     if (OC_STACK_OK != res && NULL == secPayload->securityData)
     {
@@ -1448,7 +1464,7 @@ static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
         return OC_STACK_NO_MEMORY;
     }
     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
-    OCStackResult res =  g_OTMCbDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
+    OCStackResult res = otmCtx->otmCallback.createOwnerTransferPayloadCB(
             otmCtx, &secPayload->securityData, &secPayload->payloadSize);
     if (OC_STACK_OK != res && NULL == secPayload->securityData)
     {
@@ -1610,6 +1626,13 @@ static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selecte
     }
     OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
 
+    res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
+    if(OC_STACK_OK != res)
+    {
+        OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
+        return res;
+    }
+
     //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
     res = PostOwnerTransferModeToResource(otmCtx);
     if(OC_STACK_OK != res)
@@ -1647,10 +1670,7 @@ OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallba
         return OC_STACK_INVALID_PARAM;
     }
 
-    g_OTMCbDatas[oxmType].loadSecretCB= data->loadSecretCB;
-    g_OTMCbDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
-    g_OTMCbDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
-    g_OTMCbDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
+    // TODO: Remove this API, Please see the jira ticket IOT-1484
 
     OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
 
index df6cf5a..681f68e 100644 (file)
@@ -169,7 +169,7 @@ const char * OXM_JUST_WORKS = "oic.sec.doxm.jw";
 const char * OXM_RANDOM_DEVICE_PIN = "oic.sec.doxm.rdp";
 const char * OXM_MANUFACTURER_CERTIFICATE = "oic.sec.doxm.mfgcert";
 #ifdef _ENABLE_MULTIPLE_OWNER_
-const char * OXM_PRECONF_PIN = "oic.sec.doxm.pcp";
+const char * OXM_PRECONF_PIN = "x.org.iotivity.doxm.pcp";
 #endif //_ENABLE_MULTIPLE_OWNER_
 
 //Credential data encoding methods