replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / ocprovisioningmanager.c
index 1bb5efe..ffd1256 100644 (file)
@@ -24,9 +24,9 @@
 #include "pmutility.h"
 #include "srmutility.h"
 #include "ownershiptransfermanager.h"
-#ifdef _ENABLE_MULTIPLE_OWNER_
+#ifdef MULTIPLE_OWNER
 #include "multipleownershiptransfermanager.h"
-#endif //_ENABLE_MULTIPLE_OWNER_
+#endif //MULTIPLE_OWNER
 #include "oic_malloc.h"
 #include "logger.h"
 #include "secureresourceprovider.h"
@@ -35,6 +35,8 @@
 #include "utlist.h"
 #include "aclresource.h" //Note: SRM internal header
 #include "pconfresource.h"
+#include "psinterface.h"
+#include "srmresourcestrings.h"
 
 #define TAG "OIC_OCPMAPI"
 
@@ -53,7 +55,7 @@ struct Linkdata
 
 };
 
-#ifdef _ENABLE_MULTIPLE_OWNER_
+#ifdef MULTIPLE_OWNER
 typedef struct ProvPreconfPINCtx ProvPreconfPINCtx_t;
 struct ProvPreconfPINCtx
 {
@@ -63,7 +65,7 @@ struct ProvPreconfPINCtx
     size_t pinLen;
     OCProvisionResultCB resultCallback;
 };
-#endif //_ENABLE_MULTIPLE_OWNER_
+#endif //MULTIPLE_OWNER
 
 /**
  * The function is responsible for initializaton of the provisioning manager. It will load
@@ -80,6 +82,16 @@ OCStackResult OCInitPM(const char* dbPath)
     return PDMInit(dbPath);
 }
 
+void OCTerminatePM()
+{
+    OTMTerminate();
+}
+
+OCStackResult OCPDMCleanupForTimeout()
+{
+    return PDMDeleteDeviceWithState(PDM_DEVICE_INIT);
+}
+
 /**
  * The function is responsible for discovery of owned/unowned device is specified endpoint/deviceID.
  * And this function will only return the specified device's response.
@@ -102,6 +114,33 @@ OCStackResult OCDiscoverSingleDevice(unsigned short timeout, const OicUuid_t* de
 }
 
 /**
+ * The function is responsible for discovery of owned/unowned device is specified endpoint/deviceID.
+ * And this function will only return the specified device's response.
+ *
+ * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
+ *                    server before returning the device.
+ * @param[in] deviceID         deviceID of target device.
+ * @param[in] hostAddress       MAC address of target device.
+ * @param[in] connType       ConnectivityType for discovery.
+ * @param[out] ppFoundDevice     OCProvisionDev_t of found device.
+ * @return OTM_SUCCESS in case of success and other value otherwise.
+ */
+OCStackResult OCDiscoverSingleDeviceInUnicast(unsigned short timeout, const OicUuid_t* deviceID,
+                             const char* hostAddress, OCConnectivityType connType,
+                             OCProvisionDev_t **ppFoundDevice)
+{
+    if( NULL == ppFoundDevice || NULL != *ppFoundDevice || 0 == timeout || NULL == deviceID ||
+            NULL == hostAddress)
+    {
+        OIC_LOG(ERROR, TAG, "OCDiscoverSingleDeviceInUnicast : Invalid Parameter");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    return PMSingleDeviceDiscoveryInUnicast(timeout, deviceID, hostAddress, connType,
+            ppFoundDevice);
+}
+
+/**
  * The function is responsible for discovery of device is current subnet. It will list
  * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as
  * OCMode.
@@ -140,7 +179,7 @@ OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **
     return PMDeviceDiscovery(timeout, true, ppList);
 }
 
-#ifdef _ENABLE_MULTIPLE_OWNER_
+#ifdef MULTIPLE_OWNER
 /**
  * The function is responsible for discovery of MOT enabled device is current subnet.
  *
@@ -211,7 +250,45 @@ OCStackResult OCDoMultipleOwnershipTransfer(void* ctx,
     return MOTDoOwnershipTransfer(ctx, targetDevices, resultCallback);
 }
 
-#endif //_ENABLE_MULTIPLE_OWNER_
+OCStackResult OCRemoveSubOwner(void* ctx,
+                                const OCProvisionDev_t *targetDeviceInfo,
+                                const OicUuid_t* subOwner,
+                                OCProvisionResultCB resultCallback)
+{
+    if (NULL == targetDeviceInfo || NULL == subOwner)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s : NULL Param", __func__);
+        return OC_STACK_INVALID_PARAM;
+    }
+    if (NULL == resultCallback)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s : NULL Callback", __func__);
+        return OC_STACK_INVALID_CALLBACK;
+    }
+
+    return MOTRemoveSubOwner(ctx, targetDeviceInfo, subOwner, resultCallback);
+}
+
+OCStackResult OCRemoveAllSubOwner(void* ctx,
+                                const OCProvisionDev_t *targetDeviceInfo,
+                                OCProvisionResultCB resultCallback)
+{
+    if (NULL == targetDeviceInfo)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s : NULL Param", __func__);
+        return OC_STACK_INVALID_PARAM;
+    }
+    if (NULL == resultCallback)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s : NULL Callback", __func__);
+        return OC_STACK_INVALID_CALLBACK;
+    }
+
+    return MOTRemoveSubOwner(ctx, targetDeviceInfo, &WILDCARD_SUBJECT_ID, resultCallback);
+}
+
+
+#endif //MULTIPLE_OWNER
 
 /**
  * API to register for particular OxM.
@@ -369,7 +446,7 @@ OCStackResult OCProvisionDirectPairing(void* ctx, const OCProvisionDev_t *select
     return SRPProvisionDirectPairing(ctx, selectedDeviceInfo, pconf, resultCallback);
 }
 
-#ifdef _ENABLE_MULTIPLE_OWNER_
+#ifdef MULTIPLE_OWNER
 static void AddPreconfPinOxMCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
 {
     ProvPreconfPINCtx_t* provCtx = (ProvPreconfPINCtx_t*)ctx;
@@ -390,7 +467,7 @@ OCStackResult OCProvisionPreconfigPin(void *ctx,
                                       size_t preconfigPinLen,
                                       OCProvisionResultCB resultCallback)
 {
-    if( NULL == targetDeviceInfo )
+    if( NULL == targetDeviceInfo || NULL == preconfigPin || 0 == preconfigPinLen )
     {
         return OC_STACK_INVALID_PARAM;
     }
@@ -419,7 +496,7 @@ OCStackResult OCProvisionPreconfigPin(void *ctx,
      */
     return MOTAddMOTMethod((void*)provCtx, targetDeviceInfo, OIC_PRECONFIG_PIN, AddPreconfPinOxMCB);
 }
-#endif //_ENABLE_MULTIPLE_OWNER_
+#endif //MULTIPLE_OWNER
 
 /*
 * Function to unlink devices.
@@ -526,14 +603,12 @@ static OCStackResult RemoveDeviceInfoFromLocal(const OCProvisionDev_t* pTargetDe
     // TODO: We need to add new mechanism to clean up the stale state of the device.
 
     // Close the DTLS session of the removed device.
-    CAEndpoint_t *endpoint = (CAEndpoint_t *)&pTargetDev->endpoint;
-    endpoint->port = pTargetDev->securePort;
-    CAResult_t caResult = CAcloseSslSession(endpoint);
+    CAResult_t caResult = CAcloseSslConnectionUsingUuid(pTargetDev->doxm->deviceID.id
+                                                        , sizeof(pTargetDev->doxm->deviceID.id));
     if(CA_STATUS_OK != caResult)
     {
-        OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult);
+        OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close (D)TLS session : %d", caResult);
     }
-
     OIC_LOG(DEBUG, TAG, "OUT RemoveDeviceInfoFromLocal");
 error:
     return res;
@@ -818,6 +893,26 @@ OCStackResult OCResetDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDisc
 }
 
 /**
+ * This function resets SVR DB to its factory setting.
+ *
+ * @return OC_STACK_OK in case of successful reset and other value otherwise.
+ */
+OCStackResult OCResetSVRDB(void)
+{
+    return ResetSecureResourceInPS();
+}
+
+/**
+ * This function configures SVR DB as self-ownership.
+ *
+ *@return OC_STACK_OK in case of successful configue and other value otherwise.
+ */
+OCStackResult OCConfigSelfOwnership(void)
+{
+    return ConfigSelfOwnership();
+}
+
+/**
  * Internal Function to update result in link result array.
  */
 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
@@ -1240,7 +1335,7 @@ void OCDeletePdAclList(OicSecPdAcl_t* pPdAcl)
     FreePdAclList(pPdAcl);
 }
 
-#ifdef _ENABLE_MULTIPLE_OWNER_
+#ifdef MULTIPLE_OWNER
 /**
  * API to update 'doxm.mom' to resource server.
  *
@@ -1270,7 +1365,23 @@ OCStackResult OCSelectMOTMethod(void *ctx, const OCProvisionDev_t *targetDeviceI
 {
     return MOTSelectMOTMethod(ctx, targetDeviceInfo, oxmSelValue, resultCallback);
 }
-#endif //_ENABLE_MULTIPLE_OWNER_
+#endif //MULTIPLE_OWNER
+
+/**
+ * Function to select appropriate security provisioning method.
+ *
+ * @param[in] supportedMethods   Array of supported methods
+ * @param[in] numberOfMethods   number of supported methods
+ * @param[out]  selectedMethod         Selected methods
+ * @param[in] ownerType type of owner device (SUPER_OWNER or SUB_OWNER)
+ * @return  OC_STACK_OK on success
+ */
+OCStackResult OCSelectOwnershipTransferMethod(const OicSecOxm_t *supportedMethods,
+        size_t numberOfMethods, OicSecOxm_t *selectedMethod, OwnerType_t ownerType)
+{
+    return OTMSelectOwnershipTransferMethod(supportedMethods, numberOfMethods,
+                                            selectedMethod, ownerType);
+}
 
 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
 /**
@@ -1326,5 +1437,25 @@ void OCRemoveTrustCertChainNotifier()
 {
     SRPRemoveTrustCertChainNotifier();
 }
+
+/**
+ * This function sets the callback to utilize peer certificate information
+ */
+OCStackResult OCSetPeerCertCallback(void *ctx, PeerCertCallback peerCertCallback)
+{
+    CAResult_t ret;
+
+    OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+    ret = CAsetPeerCertCallback(ctx, peerCertCallback);
+    if (CA_STATUS_OK != ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "CAsetPeerCertCallback() Failed(%d)", ret);
+        return OC_STACK_ERROR;
+    }
+    OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
+
+    return OC_STACK_OK;
+}
+
 #endif // __WITH_DTLS__ || __WITH_TLS__