Adding C++ Api for Mutual Verified Just-Works
authorSandeep Sharma <sandeep.s9@samsung.com>
Fri, 2 Dec 2016 10:03:36 +0000 (15:33 +0530)
committerRandeep Singh <randeep.s@samsung.com>
Thu, 8 Dec 2016 05:49:55 +0000 (05:49 +0000)
patch# 5: Change C++ API's name and other fixes.

Change-Id: Id18546ea50bd7308f1871173ca54d199b8b7d3df
Signed-off-by: amar <amar.sri@samsung.com>
Signed-off-by: Sandeep Sharma <sandeep.s9@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14779
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/15233

resource/include/OCProvisioningManager.h
resource/provisioning/examples/provisioningclient.cpp
resource/provisioning/src/OCProvisioningManager.cpp

index a6fbc7d..7435e24 100644 (file)
@@ -27,6 +27,7 @@
 #include "ocprovisioningmanager.h"
 #include "OCApi.h"
 #include "OCPlatform_impl.h"
+#include "oxmverifycommon.h"
 
 namespace OC
 {
@@ -38,6 +39,8 @@ namespace OC
     typedef std::function<void(PMResultList_t *result, int hasError)> ResultCallBack;
     typedef std::function<void(uint16_t credId, uint8_t *trustCertChain,
             size_t chainSize)>CertChainCallBack;
+    typedef std::function<OCStackResult(uint8_t verifNum[])> DisplayNumCB;
+    typedef std::function<OCStackResult()> UserConfirmNumCB;
 
     struct ProvisionContext
     {
@@ -50,6 +53,19 @@ namespace OC
         CertChainCallBack callback;
         TrustCertChainContext(CertChainCallBack cb) : callback(cb){}
     };
+
+    struct DisplayNumContext
+    {
+        DisplayNumCB callback;
+        DisplayNumContext(DisplayNumCB cb) : callback(cb){}
+    };
+
+    struct UserConfirmNumContext
+    {
+        UserConfirmNumCB callback;
+        UserConfirmNumContext(UserConfirmNumCB cb) : callback(cb){}
+    };
+
     /**
      * This class is for credential's to be set to devices.
      * The types supported are
@@ -247,6 +263,59 @@ namespace OC
              */
             static OCStackResult saveACL(const OicSecAcl_t* acl);
 
+            /**
+             *  api to register Callback for displaying verifNum in verification Just-Works
+             *
+             * @param displayNumCB Callback which is to be registered.
+             * @return  OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult registerDisplayNumCallback(DisplayNumCB displayNumCB);
+
+             /**
+             * API to De-register Callback for displaying verifNum in verification Just-Works
+             *
+             * @return  OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult deregisterDisplayNumCallback();
+
+            /**
+             * API to reister Callback for getting user confirmation in verification Just-Works
+             *@param userConfirmCB Callback which is to be registered.
+             * @return  OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult registerUserConfirmCallback(UserConfirmNumCB userConfirmCB);
+
+             /**
+             * API to De-register Callback for getting user confirmation in verification Just-Works
+             *
+             * @return  OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult deregisterUserConfirmCallback();
+
+             /*
+             * Set option for Mutual Verified Just-Works
+             * The default is both display PIN and get user confirmation.
+             */
+            static OCStackResult setVerifyOptionMask(VerifyOptionBitmask_t optionMask);
+
+            /**
+             * Callback function to display Verification Number.
+             *
+             * @param[in] ctx  User context returned in callback
+             * @param[in] verifNum  Array of MUTUAL_VERIF_NUM_LEN size bytes
+             *
+             * @return OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult displayNumCallbackWrapper(void* ctx,
+                    uint8_t verifNum[MUTUAL_VERIF_NUM_LEN]);
+
+             /**
+             * Callback function to get 'Num' verification result.
+             *
+             * @return OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult confirmUserCallbackWrapper(void* ctx);
+
 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
             /**
              * API to save Trust certificate chain into Cred of SVR.
index 6c4ffd1..03cab4b 100644 (file)
@@ -808,6 +808,47 @@ PVDP_ERROR:
     ask = 1;
 }
 
+OCStackResult displayMutualVerifNumCB(uint8_t mutualVerifNum[MUTUAL_VERIF_NUM_LEN])
+{
+    OIC_LOG(INFO, TAG, "IN displayMutualVerifNumCB");
+    OIC_LOG(INFO, TAG, "############ mutualVerifNum ############");
+    OIC_LOG_BUFFER(INFO, TAG, mutualVerifNum, MUTUAL_VERIF_NUM_LEN);
+    OIC_LOG(INFO, TAG, "############ mutualVerifNum ############");
+    OIC_LOG(INFO, TAG, "OUT displayMutualVerifNumCB");
+    return OC_STACK_OK;
+}
+
+OCStackResult confirmMutualVerifNumCB(void)
+{
+    for (;;)
+    {
+        int userConfirm;
+
+        printf("   > Press 1 if the mutual verification numbers are the same\n");
+        printf("   > Press 0 if the mutual verification numbers are not the same\n");
+
+        for (int ret=0; 1!=ret; )
+        {
+            ret = scanf("%d", &userConfirm);
+            for (; 0x20<=getchar(); );  // for removing overflow garbage
+                                        // '0x20<=code' is character region
+        }
+        if (1 == userConfirm)
+        {
+            break;
+        }
+        else if (0 == userConfirm)
+        {
+            return OC_STACK_USER_DENIED_REQ;
+        }
+        printf("   Entered Wrong Number. Please Enter Again\n");
+    }
+    return OC_STACK_OK;
+}
+
+
+
+
 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
 static int saveTrustCert(void)
 {
@@ -878,6 +919,7 @@ void MOTMethodCB(PMResultList_t *result, int hasError)
 
 int main(void)
 {
+    OCStackResult result;
     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
 
     // Create PlatformConfig object
@@ -902,6 +944,18 @@ int main(void)
             return 1;
         }
 
+        result = OCSecure::registerDisplayNumCallback(displayMutualVerifNumCB);
+        if (result != OC_STACK_OK)
+        {
+            std::cout<< "!!Error - setDisplayVerifNumCB failed."<<std::endl;
+        }
+
+        result = OCSecure::registerUserConfirmCallback(confirmMutualVerifNumCB);
+        if (result != OC_STACK_OK)
+        {
+            std::cout<< "!!Error - setConfirmVerifNumCB failed."<<std::endl;
+        }
+
         for (int out = 0; !out;)
         {
             while (!ask)
@@ -1428,7 +1482,7 @@ int main(void)
                             }
                             else
                             {
-                                if(OC_STACK_OK != pMOTEnabledDeviceList[(dev_num - 
+                                if(OC_STACK_OK != pMOTEnabledDeviceList[(dev_num -
                                 pOwnedDevList.size() - 1)]->changeMOTMode(momType,
                                 MOTMethodCB))
                                 {
index 67cced3..80421c3 100644 (file)
@@ -393,6 +393,136 @@ namespace OC
         return result;
     }
 
+    OCStackResult OCSecure::displayNumCallbackWrapper(void* ctx,
+            uint8_t verifNum[MUTUAL_VERIF_NUM_LEN])
+    {
+        OCStackResult result;
+
+        DisplayNumContext* context = static_cast<DisplayNumContext*>(ctx);
+        uint8_t *number = new uint8_t[MUTUAL_VERIF_NUM_LEN];
+        memcpy(number, verifNum, MUTUAL_VERIF_NUM_LEN);
+        result = context->callback(number);
+        delete context;
+        return result;
+    }
+
+    OCStackResult OCSecure::registerDisplayNumCallback(DisplayNumCB displayNumCB)
+    {
+        if(!displayNumCB)
+        {
+            oclog() << "Failed to register callback for display.";
+            return OC_STACK_INVALID_CALLBACK;
+        }
+
+        OCStackResult result;
+        auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+        if (cLock)
+        {
+            DisplayNumContext* context = new DisplayNumContext(displayNumCB);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            SetDisplayNumCB(static_cast<void*>(context), &OCSecure::displayNumCallbackWrapper);
+            result = OC_STACK_OK;
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
+
+    OCStackResult OCSecure::deregisterDisplayNumCallback()
+    {
+        OCStackResult result;
+        auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            UnsetDisplayNumCB();
+            result = OC_STACK_OK;
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
+
+    OCStackResult OCSecure::confirmUserCallbackWrapper(void* ctx)
+    {
+        OCStackResult result;
+
+        UserConfirmNumContext* context = static_cast<UserConfirmNumContext*>(ctx);
+        result = context->callback();
+        delete context;
+        return result;
+    }
+
+    OCStackResult OCSecure::registerUserConfirmCallback(UserConfirmNumCB userConfirmCB)
+    {
+        if(!userConfirmCB)
+        {
+            oclog() << "Failed to set callback for confirming verifying callback.";
+            return OC_STACK_INVALID_CALLBACK;
+        }
+
+        OCStackResult result;
+        auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+        if (cLock)
+        {
+            UserConfirmNumContext* context = new UserConfirmNumContext(userConfirmCB);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            SetUserConfirmCB(static_cast<void*>(context), &OCSecure::confirmUserCallbackWrapper);
+            result = OC_STACK_OK;
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
+
+    OCStackResult OCSecure::deregisterUserConfirmCallback()
+    {
+        OCStackResult result;
+        auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            UnsetUserConfirmCB();
+            result = OC_STACK_OK;
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
+
+    OCStackResult OCSecure::setVerifyOptionMask(VerifyOptionBitmask_t optionMask)
+    {
+        OCStackResult result;
+        auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            SetVerifyOption(optionMask);
+            result = OC_STACK_OK;
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
+
 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
     OCStackResult OCSecure::saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
                                         OicEncodingType_t encodingType, uint16_t *credId)