#include "ocstack.h"
#include "securevirtualresourcetypes.h"
#include "pmtypes.h"
+#include "octypes.h"
+
#ifdef __cplusplus
extern "C"
*/
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.
*/\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
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 OC_STACK_OK in case of success and other value otherwise.
+ */
+OCStackResult OCRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB Callback)
+{
+ return SRPRegisterTrustCertChainNotifier(ctx, Callback);
+}
+
+/**
+ * function to de-register notifier for Trustcertchain change.
+ */
+void OCRemoveTrustCertChainNotifier()
+{
+ SRPRemoveTrustCertChainNotifier();
+}
#endif // __WITH_DTLS__ || __WITH_TLS__
#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.
*/
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.
*
}
*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;
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
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
{
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
*/
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__
};
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)
#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:
}
OCStackResult OCSecure::readTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
- size_t *chainSize)
+ size_t *chainSize)
{
OCStackResult result;
auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
}
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)