Added C and C++ callback API for TrustCertChain Change Notification.
authorAshwini Kumar <k.ashwini@samsung.com>
Fri, 4 Nov 2016 12:26:38 +0000 (17:56 +0530)
committerRandeep Singh <randeep.s@samsung.com>
Fri, 4 Nov 2016 13:06:12 +0000 (13:06 +0000)
patch #2,3,4 : Fix callback context at RI layer
patch #5: Address review comments

Change-Id: I1d776a01cfb41074ce518ee20ddda1ce7ccde40f
Signed-off-by: Sandeep Sharma <sandeep.s9@samsung.com>
Signed-off-by: Ashwini Kumar <k.ashwini@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14033
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/provisioning/include/internal/secureresourceprovider.h
resource/csdk/security/provisioning/include/ocprovisioningmanager.h
resource/csdk/security/provisioning/src/ocprovisioningmanager.c
resource/csdk/security/provisioning/src/secureresourceprovider.c
resource/csdk/stack/include/octypes.h
resource/include/OCProvisioningManager.h
resource/provisioning/examples/provisioningclient.cpp
resource/provisioning/src/OCProvisioningManager.cpp

index 876dfd9..f828606 100644 (file)
@@ -24,6 +24,8 @@
 #include "ocstack.h"
 #include "securevirtualresourcetypes.h"
 #include "pmtypes.h"
+#include "octypes.h"
+
 
 #ifdef __cplusplus
 extern "C"
@@ -111,6 +113,20 @@ OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
  */
 OCStackResult SRPSaveOwnCertChain(OicSecCert_t * cert, OicSecKey_t * key, uint16_t *credId);
 
+/**
+ * function to register callback, for getting notification for TrustCertChain change.
+ *
+ * @param[in] ctx user context to be passed.
+ * @param[in] TrustCertChainChangeCB notifier callback function
+ * @return OC_STACK_OK in case of success and other value otherwise.
+ */
+OCStackResult SRPRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB callback);
+
+/**
+ * function to de-register TrustCertChain notification callback.
+ */
+void SRPRemoveTrustCertChainNotifier(void);
+
 #endif // __WITH_DTLS__ || __WITH_TLS__
 /**
  * API to send Direct-Pairing Configuration to a device.
index 5153963..0c43003 100644 (file)
@@ -451,6 +451,18 @@ OCStackResult OCProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16
  */\r
 OCStackResult OCSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,\r
                                         OicEncodingType_t encodingType, uint16_t *credId);\r
+/**\r
+ * function to register callback, for getting notification for TrustCertChain change.\r
+ *\r
+ * @param[in] TrustCertChainChangeCB notifier callback function\r
+ * @return OC_STACK_OK in case of success and other value otherwise.\r
+ */\r
+OCStackResult OCRegisterTrustCertChainNotifier(void *cb, TrustCertChainChangeCB CB);\r
+\r
+/**\r
+ * function to de-register TrustCertChain notification callback.\r
+ */\r
+void OCRemoveTrustCertChainNotifier(void);\r
 \r
 /*\r
  * Function to read Trust certificate chain from SVR.\r
index 1ac5d42..549edfd 100644 (file)
@@ -1294,5 +1294,25 @@ OCStackResult OCSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
     return SRPSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId);
 }
 
+/**
+ * function to register notifier for Trustcertchain change.
+ *
+ * @param[in] ctx user context.
+ * @param[in] TrustCertChainChangeCB notification callback fucntion.
+ * @return @return  OC_STACK_OK in case of success and other value otherwise.
+ */
+OCStackResult OCRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB Callback)
+{
+    SRPRegisterTrustCertChainNotifier(ctx, Callback);
+    return;
+}
+
+/**
+ * function to de-register notifier for Trustcertchain change.
+ */
+void OCRemoveTrustCertChainNotifier()
+{
+    return SRPRemoveTrustCertChainNotifier();
+}
 #endif // __WITH_DTLS__ || __WITH_TLS__
 
index 9208420..be479ea 100644 (file)
@@ -65,6 +65,9 @@
 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
             {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
 
+
+trustCertChainContext_t g_trustCertChainNotifier;
+
 /**
  * Structure to carry credential data to callback.
  */
@@ -452,6 +455,26 @@ static OCStackResult provisionCertCred(const OicSecCred_t *cred,
     return ret;
 }
 
+OCStackResult SRPRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB callback)
+{
+    if (g_trustCertChainNotifier.callback)
+    {
+        OIC_LOG(ERROR, TAG, "Can't register Notifier, Unregister previous one");
+        return OC_STACK_ERROR;
+    }
+
+    g_trustCertChainNotifier.callback = callback;
+    g_trustCertChainNotifier.context = ctx;
+    return OC_STACK_OK;
+}
+
+void SRPRemoveTrustCertChainNotifier()
+{
+    g_trustCertChainNotifier.callback = NULL;
+    g_trustCertChainNotifier.context = NULL;
+    return;
+}
+
 /**
  * Callback handler for handling callback of certificate provisioning device.
  *
@@ -629,6 +652,16 @@ OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
     }
     *credId = cred->credId;
 
+    if (g_trustCertChainNotifier.callback)
+    {
+        uint8_t *certChain = (uint8_t*)OICCalloc(1, sizeof(uint8_t) * chainSize);
+        VERIFY_NON_NULL(TAG, certChain, ERROR, OC_STACK_NO_MEMORY);
+        memcpy(certChain, trustCertChain, chainSize);
+        g_trustCertChainNotifier.callback(g_trustCertChainNotifier.context, credId,
+                certChain, chainSize);
+        OICFree(certChain);
+    }
+
     OIC_LOG(DEBUG, TAG, "OUT SRPSaveTrustCertChain");
 
     return res;
index 073902c..3429ecc 100644 (file)
@@ -1694,6 +1694,28 @@ typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
 typedef void (*OCDirectPairingCB)(void *ctx, OCDPDev_t *peer, OCStackResult result);
 //#endif // DIRECT_PAIRING
 
+#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
+/**
+ * Callback function definition for Change in TrustCertChain
+ *
+ * @param[IN] ctx - user context returned in the callback.
+ * @param[IN] credId - trustCertChain changed for this ID
+ * @param[IN] trustCertChain - trustcertchain binary blob.
+ * @param[IN] chainSize - size of trustchain
+ */
+typedef void (*TrustCertChainChangeCB)(void *ctx, uint16_t credId, uint8_t *trustCertChain,
+        size_t chainSize);
+
+/**
+ * certChain context structure.
+ */
+typedef struct trustCertChainContext
+{
+    TrustCertChainChangeCB callback;
+    void *context;
+} trustCertChainContext_t;
+#endif
+
 #ifdef __cplusplus
 }
 #endif // __cplusplus
index 93f6b2c..dd60ced 100644 (file)
@@ -36,6 +36,8 @@ namespace OC
     typedef std::vector<OicUuid_t> UuidList_t;
     typedef std::vector<OCProvisionResult_t> PMResultList_t;
     typedef std::function<void(PMResultList_t *result, int hasError)> ResultCallBack;
+    typedef std::function<void(uint16_t credId, uint8_t *trustCertChain,
+            size_t chainSize)>CertChainCallBack;
 
     struct ProvisionContext
     {
@@ -43,6 +45,11 @@ namespace OC
         ProvisionContext(ResultCallBack cb) : callback(cb){}
     };
 
+    struct TrustCertChainContext
+    {
+        CertChainCallBack callback;
+        TrustCertChainContext(CertChainCallBack cb) : callback(cb){}
+    };
     /**
      * This class is for credential's to be set to devices.
      * The types supported are
@@ -229,7 +236,6 @@ namespace OC
             static OCStackResult saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
                                         OicEncodingType_t encodingType, uint16_t *credId);
 
-
             /*
             * API to read Trust certificate chain from SVR.
             * Caller must free when done using the returned trust certificate
@@ -240,6 +246,34 @@ namespace OC
             */
             static OCStackResult readTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
                                      size_t *chainSize);
+
+            /**
+             * API to register Notifier for trustCertChain change.
+             *
+             * @param[in] TrustCertChainChangeCB trustCertChain Change will be
+             * notified asynchronously. User need to "delete[]" trustCertChain
+             * in the callback function.
+             * @return  OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult registerTrustCertChangeNotifier(CertChainCallBack);
+
+            /**
+             * API to remove Already registered Notifier.
+             *
+             *@return  OC_STACK_OK always, kept it for symmetry.
+             */
+            static OCStackResult removeTrustCertChangeNotifier();
+
+            /**
+             * Notifier wrapper for trustCertChain change.
+             *
+             * @param[in] ctx  User context returned in callback
+             * @param[in] credId  trustCertChain changed for this ID
+             * @param[in] trustCertChain trustcertchain binary blob
+             * @param[in] chainSize size of trustCertChain
+             */
+            static void certCallbackWrapper(void* ctx, uint16_t credId, uint8_t *trustCertChain,
+                                size_t chainSize);
 #endif // __WITH_DTLS__ || __WITH_TLS__
 
     };
index 9f78e89..53e9c10 100644 (file)
@@ -846,6 +846,12 @@ static int saveTrustCert(void)
 
     return 0;
 }
+
+void certChainCallBack(uint16_t credId, uint8_t *trustCertChain,size_t chainSize)
+{
+    OIC_LOG_V(INFO, TAG, "trustCertChain Changed for credId %u", credId);
+    return;
+}
 #endif // __WITH_DTLS__ or __WITH_TLS__
 
 int main(void)
@@ -1287,10 +1293,14 @@ int main(void)
 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
                 case 13:
                     {
+                        std::cout<< "registering cert chain change notifier"<<std::endl;
+                        OCSecure::registerTrustCertChangeNotifier(certChainCallBack);
                         if(saveTrustCert())
                         {
                             std::cout<<"Error in saving cert"<<std::endl;
                         }
+                        std::cout<< "Unregister notifier"<<std::endl;
+                        OCSecure::removeTrustCertChangeNotifier();
                         break;
                     }
                 case 14:
index 931f0ec..42e3af4 100644 (file)
@@ -364,7 +364,7 @@ namespace OC
     }
 
     OCStackResult OCSecure::readTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
-                                                 size_t *chainSize)
+            size_t *chainSize)
     {
         OCStackResult result;
         auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
@@ -381,6 +381,63 @@ namespace OC
         }
         return result;
     }
+
+    void OCSecure::certCallbackWrapper(void* ctx, uint16_t credId, uint8_t *trustCertChain,
+            size_t chainSize)
+    {
+        TrustCertChainContext* context = static_cast<TrustCertChainContext*>(ctx);
+        uint8_t *certChain = new uint8_t[chainSize];
+        memcpy(certChain, trustCertChain, chainSize);
+        std::thread exec(context->callback, credId, certChain, chainSize);
+        exec.detach();
+        delete context;
+    }
+
+    OCStackResult OCSecure::registerTrustCertChangeNotifier(CertChainCallBack callback)
+    {
+        if (!callback)
+        {
+            oclog() <<"callback can not be null";
+            return OC_STACK_INVALID_CALLBACK;
+        }
+
+        OCStackResult result;
+        auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+        if (cLock)
+        {
+            TrustCertChainContext* context = new TrustCertChainContext(callback);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCRegisterTrustCertChainNotifier(static_cast<void*>(context),
+                    &OCSecure::certCallbackWrapper);
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
+
+
+    OCStackResult OCSecure::removeTrustCertChangeNotifier()
+    {
+        OCStackResult result;
+        auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            OCRemoveTrustCertChainNotifier();
+            result = OC_STACK_OK;
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
 #endif // __WITH_DTLS__ || __WITH_TLS__
 
     void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)