Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / ocprovisioningmanager.c
index 59f6895..b5d4bed 100644 (file)
@@ -30,6 +30,7 @@
 #include "credresource.h"
 #include "utlist.h"
 #include "aclresource.h" //Note: SRM internal header
+#include "pconfresource.h"
 
 #define TAG "OCPMAPI"
 
@@ -69,13 +70,13 @@ OCStackResult OCInitPM(const char* dbPath)
  * OCMode.
  *
  * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
- *                    client before returning the list of devices.
+ *                    server before returning the list of devices.
  * @param[out] ppList List of candidate devices to be provisioned
  * @return OTM_SUCCESS in case of success and other value otherwise.
  */
 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
 {
-    if( ppList == NULL || *ppList != NULL)
+    if( ppList == NULL || *ppList != NULL || 0 == timeout)
     {
         return OC_STACK_INVALID_PARAM;
     }
@@ -88,13 +89,13 @@ OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t
  * all the device in subnet which are owned by calling provisioning client.
  *
  * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
- *                    client before returning the list of devices.
+ *                    server before returning the list of devices.
  * @param[out] ppList List of device owned by provisioning tool.
  * @return OTM_SUCCESS in case of success and other value otherwise.
  */
 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
 {
-    if( ppList == NULL || *ppList != NULL)
+    if( ppList == NULL || *ppList != NULL || 0 == timeout)
     {
         return OC_STACK_INVALID_PARAM;
     }
@@ -113,7 +114,7 @@ OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t*
 {
     if(NULL == callbackData)
     {
-        return OC_STACK_INVALID_PARAM;
+        return OC_STACK_INVALID_CALLBACK ;
     }
 
     return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
@@ -127,7 +128,11 @@ OCStackResult OCDoOwnershipTransfer(void* ctx,
     {
         return OC_STACK_INVALID_PARAM;
     }
-
+    if (!resultCallback)
+    {
+        OIC_LOG(INFO, TAG, "OCDoOwnershipTransfer : NULL Callback");
+        return OC_STACK_INVALID_CALLBACK;
+    }
     return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
 }
 
@@ -178,6 +183,22 @@ OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t ke
 
 }
 
+/**
+ * this function sends Direct-Pairing Configuration to a device.
+ *
+ * @param[in] ctx Application context would be returned in result callback.
+ * @param[in] selectedDeviceInfo Selected target device.
+ * @param[in] pconf PCONF pointer.
+ * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
+              request recieves a response from resource server.
+ * @return  OC_STACK_OK in case of success and other value otherwise.
+ */
+OCStackResult OCProvisionDirectPairing(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecPconf_t *pconf,
+                             OCProvisionResultCB resultCallback)
+{
+    return SRPProvisionDirectPairing(ctx, selectedDeviceInfo, pconf, resultCallback);
+}
+
 /*
 * Function to unlink devices.
 * This function will remove the credential & relationship between the two devices.
@@ -194,13 +215,23 @@ OCStackResult OCUnlinkDevices(void* ctx,
                               const OCProvisionDev_t* pTargetDev2,
                               OCProvisionResultCB resultCallback)
 {
-    OC_LOG(INFO, TAG, "IN OCUnlinkDevices");
+    OIC_LOG(INFO, TAG, "IN OCUnlinkDevices");
     OCUuidList_t* idList = NULL;
     size_t numOfDev = 0;
 
-    if (!pTargetDev1 || !pTargetDev2 || !resultCallback)
+    if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
     {
-        OC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
+        OIC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
+        return OC_STACK_INVALID_PARAM;
+    }
+    if (!resultCallback)
+    {
+        OIC_LOG(INFO, TAG, "OCUnlinkDevices : NULL Callback");
+        return OC_STACK_INVALID_CALLBACK;
+    }
+    if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
+    {
+        OIC_LOG(INFO, TAG, "OCUnlinkDevices : Same device ID");
         return OC_STACK_INVALID_PARAM;
     }
 
@@ -208,12 +239,12 @@ OCStackResult OCUnlinkDevices(void* ctx,
     OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
     if (OC_STACK_OK != res)
     {
-        OC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
+        OIC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
         goto error;
     }
     if (1 > numOfDev)
     {
-        OC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
+        OIC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
         res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
         goto error;
     }
@@ -227,17 +258,17 @@ OCStackResult OCUnlinkDevices(void* ctx,
             res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
             if (OC_STACK_OK != res)
             {
-                OC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
+                OIC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
             }
             goto error;
         }
         curDev = curDev->next;
     }
-    OC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
+    OIC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
     res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
 
 error:
-    OC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
+    OIC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
 
     PDMDestoryOicUuidLinkList(idList);
     return res;
@@ -258,13 +289,18 @@ OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDis
                             const OCProvisionDev_t* pTargetDev,
                             OCProvisionResultCB resultCallback)
 {
-    OC_LOG(INFO, TAG, "IN OCRemoveDevice");
+    OIC_LOG(INFO, TAG, "IN OCRemoveDevice");
     OCStackResult res = OC_STACK_ERROR;
-    if (!pTargetDev || !resultCallback || 0 == waitTimeForOwnedDeviceDiscovery)
+    if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
     {
-        OC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
+        OIC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
         return OC_STACK_INVALID_PARAM;
     }
+    if (!resultCallback)
+    {
+        OIC_LOG(INFO, TAG, "OCRemoveDevice : NULL Callback");
+        return OC_STACK_INVALID_CALLBACK;
+    }
 
     // Send DELETE requests to linked devices
     OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
@@ -273,11 +309,11 @@ OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDis
     {
         if (OC_STACK_CONTINUE == resReq)
         {
-            OC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
+            OIC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
         }
         else
         {
-            OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
+            OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
             res = resReq;
             goto error;
         }
@@ -288,30 +324,56 @@ OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDis
     cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
     if (cred == NULL)
     {
-        OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device.");
+        OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device.");
         goto error;
     }
 
     res = RemoveCredential(&cred->subject);
     if (res != OC_STACK_RESOURCE_DELETED)
     {
-        OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential.");
+        OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential.");
         goto error;
     }
 
-    // Remove device info from prvisioning database.
-    res = PDMDeleteDevice(&pTargetDev->doxm->deviceID);
+    /**
+     * Change the device status as stale status.
+     * If all request are successed, this device information will be deleted.
+     */
+    res = PDMSetDeviceStale(&pTargetDev->doxm->deviceID);
     if (res != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to delete device in PDM.");
+        OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to set device status as stale");
         goto error;
     }
 
-    // Check that we have to wait callback for DELETE request or not
+    // TODO: We need to add new mechanism to clean up the stale state of the device.
+
     res = resReq;
 
+    //Close the DTLS session of the removed device.
+    CAEndpoint_t* endpoint = (CAEndpoint_t *)&pTargetDev->endpoint;
+    endpoint->port = pTargetDev->securePort;
+    CAResult_t caResult = CACloseDtlsSession(endpoint);
+    if(CA_STATUS_OK != caResult)
+    {
+        OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult);
+    }
+
+    /**
+     * If there is no linked device, PM does not send any request.
+     * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
+     */
+    if(OC_STACK_CONTINUE == res)
+    {
+        if(resultCallback)
+        {
+            resultCallback(ctx, 0, NULL, false);
+        }
+        res = OC_STACK_OK;
+    }
+
 error:
-    OC_LOG(INFO, TAG, "OUT OCRemoveDevice");
+    OIC_LOG(INFO, TAG, "OUT OCRemoveDevice");
     return res;
 }
 
@@ -322,7 +384,7 @@ error:
 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
 {
 
-    OC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
+    OIC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
     if (1 == device)
     {
         memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
@@ -344,7 +406,7 @@ static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool has
 
     if (NULL == ctx)
     {
-        OC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
+        OIC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
         return;
     }
     (void)nOfRes;
@@ -355,7 +417,7 @@ static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool has
     if (hasError)
     {
         UpdateLinkResults(link, 2,arr[0].res);
-        OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
+        OIC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
                                                 link->resArr,
                                                 true);
@@ -380,7 +442,7 @@ static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool has
 
     if (NULL == ctx)
     {
-        OC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
+        OIC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
         return;
     }
     (void)nOfRes;
@@ -389,7 +451,7 @@ static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool has
 
     if (hasError)
     {
-        OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
+        OIC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
         UpdateLinkResults(link, 1, arr[0].res);
         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
                                                 link->resArr,
@@ -430,17 +492,17 @@ static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bo
 {
     if (NULL == ctx)
     {
-        OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
+        OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
         return;
     }
     Linkdata_t *link = (Linkdata_t*)ctx;
     OCProvisionResultCB resultCallback = link->resultCallback;
-    OC_LOG_V(INFO, TAG, "has error returned %d",hasError);
+    OIC_LOG_V(INFO, TAG, "has error returned %d",hasError);
     UpdateLinkResults(link, 1, arr[0].res);
     UpdateLinkResults(link, 2, arr[1].res);
     if (hasError)
     {
-        OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
+        OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
         ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
                                                 link->resArr,
                                                 true);
@@ -454,7 +516,7 @@ static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bo
         OCStackResult res =  SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
         if (OC_STACK_OK!=res)
         {
-             OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
+             OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
              UpdateLinkResults(link, 1, res);
              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
                                                      link->resArr,
@@ -465,11 +527,11 @@ static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bo
     }
     else if (NULL!=link->pDev2Acl)
     {
-        OC_LOG(ERROR, TAG, "ACL for device 1 is NULL");
+        OIC_LOG(ERROR, TAG, "ACL for device 1 is NULL");
         OCStackResult res =  SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
         if (OC_STACK_OK!=res)
         {
-             OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
+             OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
               UpdateLinkResults(link, 2, res);
              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
                                                      link->resArr,
@@ -480,7 +542,7 @@ static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bo
     }
     else
     {
-        OC_LOG(INFO, TAG, "ACLs of both devices are NULL");
+        OIC_LOG(INFO, TAG, "ACLs of both devices are NULL");
         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
                                                 link->resArr,
                                                 false);
@@ -508,28 +570,38 @@ OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_
                                          OCProvisionResultCB resultCallback)
 {
 
-    if (!pDev1 || !pDev2 || !resultCallback)
+    if (!pDev1 || !pDev2 || !pDev1->doxm || !pDev2->doxm)
     {
-        OC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
+        OIC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
         return OC_STACK_INVALID_PARAM;
     }
+    if (!resultCallback)
+    {
+        OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : NULL Callback");
+        return OC_STACK_INVALID_CALLBACK;
+    }
     if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
     {
-        OC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
+        OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
+        return OC_STACK_INVALID_PARAM;
+    }
+    if (0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
+    {
+        OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Same device ID");
         return OC_STACK_INVALID_PARAM;
     }
 
-    OC_LOG(DEBUG, TAG, "Checking link in DB");
+    OIC_LOG(DEBUG, TAG, "Checking link in DB");
     bool linkExists = true;
     OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExists);
     if(res != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "Internal Error Occured");
+        OIC_LOG(ERROR, TAG, "Internal Error Occured");
         return res;
     }
     if (linkExists)
     {
-        OC_LOG(ERROR, TAG, "Link already exists");
+        OIC_LOG(ERROR, TAG, "Link already exists");
         return OC_STACK_INVALID_PARAM;
     }
 
@@ -545,10 +617,10 @@ OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_
     Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
     if (!link)
     {
-        OC_LOG(ERROR, TAG, "Failed to memory allocation");
+        OIC_LOG(ERROR, TAG, "Failed to memory allocation");
         return OC_STACK_NO_MEMORY;
     }
-    OC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
+    OIC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
 
     link->pDev1 = pDev1;
     link->pDev1Acl = pDev1Acl;
@@ -578,7 +650,7 @@ OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
 {
     //TODO will be replaced by more efficient logic
     if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
-         || *pUnownedDevList != NULL)
+         || *pUnownedDevList != NULL || 0 == waittime)
     {
         return OC_STACK_INVALID_PARAM;
     }
@@ -588,7 +660,7 @@ OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
     OCStackResult res =  OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
     if (OC_STACK_OK != res)
     {
-        OC_LOG(ERROR,TAG, "Error in unowned discovery");
+        OIC_LOG(ERROR,TAG, "Error in unowned discovery");
         return res;
     }
 
@@ -597,7 +669,7 @@ OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
     res =  OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
     if (OC_STACK_OK != res)
     {
-        OC_LOG(ERROR,TAG, "Error in owned discovery");
+        OIC_LOG(ERROR,TAG, "Error in owned discovery");
         PMDeleteDeviceList(unownedDevice);
         return res;
     }
@@ -608,7 +680,7 @@ OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
     res =  PDMGetOwnedDevices(&uuidList, &numOfDevices);
     if (OC_STACK_OK != res)
     {
-        OC_LOG(ERROR, TAG, "Error while getting info from DB");
+        OIC_LOG(ERROR, TAG, "Error while getting info from DB");
         PMDeleteDeviceList(unownedDevice);
         PMDeleteDeviceList(ownedDevice);
         return res;
@@ -637,7 +709,7 @@ OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
         OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
         if (NULL == ptr)
         {
-            OC_LOG(ERROR,TAG,"Fail to allocate memory");
+            OIC_LOG(ERROR,TAG,"Fail to allocate memory");
             PMDeleteDeviceList(unownedDevice);
             PMDeleteDeviceList(ownedDevice);
             OCDeleteUuidList(uuidList);
@@ -647,7 +719,7 @@ OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
         ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
         if (NULL == ptr->doxm)
         {
-            OC_LOG(ERROR,TAG,"Fail to allocate memory");
+            OIC_LOG(ERROR,TAG,"Fail to allocate memory");
             PMDeleteDeviceList(unownedDevice);
             PMDeleteDeviceList(ownedDevice);
             OCDeleteUuidList(uuidList);
@@ -689,6 +761,16 @@ void OCDeleteACLList(OicSecAcl_t* pAcl)
     DeleteACLList(pAcl);
 }
 
+/**
+ * This function deletes PDACL data.
+ *
+ * @param pPdAcl Pointer to OicSecPdAcl_t structure.
+ */
+void OCDeletePdAclList(OicSecPdAcl_t* pPdAcl)
+{
+    FreePdAclList(pPdAcl);
+}
+
 
 #ifdef __WITH_X509__
 /**