+ OCStackResult result = OC_STACK_OK;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ SetGeneratePinCB(displayPin);
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+
+ return result;
+ }
+
+ OCStackResult OCSecure::removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery,
+ std::string uuid,
+ ResultCallBack resultCallback)
+ {
+ if (!resultCallback)
+ {
+ oclog() << "Result calback can't be null";
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ OCStackResult result;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+ if (cLock)
+ {
+ ProvisionContext* context = new ProvisionContext(resultCallback);
+
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+
+ OicUuid_t targetDev;
+ result = ConvertStrToUuid(uuid.c_str(), &targetDev);
+ if(OC_STACK_OK == result)
+ {
+ result = OCRemoveDeviceWithUuid(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
+ &targetDev, &OCSecureResource::callbackWrapper);
+ }
+ else
+ {
+ oclog() <<"Can not convert struuid to uuid";
+ }
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+ return result;
+ }
+
+ OCStackResult OCSecure::saveACL(const OicSecAcl_t* acl)
+ {
+ if (!acl)
+ {
+ oclog() <<"ACL can't be null";
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult result;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCSaveACL(const_cast<OicSecAcl_t*>(acl));
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+ return result;
+ }
+
+ OCStackResult OCSecure::displayNumCallbackWrapper(void* ctx,
+ uint8_t verifNum[MUTUAL_VERIF_NUM_LEN])
+ {
+ uint8_t *number = NULL;
+
+ DisplayNumContext* context = static_cast<DisplayNumContext*>(ctx);
+ if (!context)
+ {
+ oclog() << "Invalid context";
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ if (NULL != verifNum) {
+ number = new uint8_t[MUTUAL_VERIF_NUM_LEN];
+ memcpy(number, verifNum, MUTUAL_VERIF_NUM_LEN);
+ }
+
+ return context->callback(number);
+ }
+
+ OCStackResult OCSecure::registerDisplayNumCallback(DisplayNumCB displayNumCB)
+ {
+ if(!displayNumCB)
+ {
+ oclog() << "Failed to register callback for display.";
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ OCStackResult result = OCSecure::deregisterDisplayNumCallback();
+ if (OC_STACK_OK != result)
+ {
+ oclog() << "Failed to de-register callback for display."<<std::endl;
+ return 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);
+ DisplayNumContext* context = static_cast<DisplayNumContext*>(UnsetDisplayNumCB());
+ if (context)
+ {
+ oclog() << "Delete registered display num context"<<std::endl;
+ delete context;
+ }
+ result = OC_STACK_OK;
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+ return result;
+ }
+
+ OCStackResult OCSecure::confirmUserCallbackWrapper(void* ctx)
+ {
+ UserConfirmNumContext* context = static_cast<UserConfirmNumContext*>(ctx);
+ if (!context)
+ {
+ oclog() << "Invalid context";
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ return context->callback();
+ }
+
+ OCStackResult OCSecure::registerUserConfirmCallback(UserConfirmNumCB userConfirmCB)
+ {
+ if(!userConfirmCB)
+ {
+ oclog() << "Failed to set callback for confirming verifying callback.";
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ OCStackResult result = OCSecure::deregisterUserConfirmCallback();
+ if (OC_STACK_OK != result)
+ {
+ oclog() << "Failed to de-register callback for comfirm."<<std::endl;
+ return 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);
+ UserConfirmNumContext* context = static_cast<UserConfirmNumContext*>(UnsetUserConfirmCB());
+ if (context)
+ {
+ oclog() << "Delete registered user confirm context"<<std::endl;
+ delete context;
+ }
+ 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;
+ }
+
+ OCStackResult OCSecure::pdmCleanupForTimeout()
+ {
+ OCStackResult result;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+ if (cLock)
+ {
+ result = OCPDMCleanupForTimeout();
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+ return result;
+ }
+
+ OCStackResult OCSecure::configSelfOwnership()
+ {
+ OCStackResult result;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCConfigSelfOwnership();
+ }
+ 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)
+ {
+ if (!trustCertChain)
+ {
+ oclog() <<"trustCertChain can't be null";
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!credId)
+ {
+ oclog() <<"cred ID can not be null";
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult result;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId );
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+ return result;
+ }
+
+ OCStackResult OCSecure::readTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
+ size_t *chainSize)
+ {
+ OCStackResult result;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCReadTrustCertChain(credId, trustCertChain, chainSize);
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+ 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;
+ }
+
+ OCStackResult OCSecure::setDeviceIdSeed(const uint8_t* seed, size_t seedSize)
+ {
+ if (!seed)
+ {
+ oclog() <<"seed can not be null";
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult result;
+ auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
+
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = SetDeviceIdSeed(seed, seedSize);
+ }
+ else
+ {
+ oclog() <<"Mutex not found";
+ result = OC_STACK_ERROR;
+ }
+ return result;
+ }
+
+ int OCSecure::peerCertCallbackWrapper(void *ctx, const mbedtls_x509_crt *cert,
+ int depth)
+ {
+ OCStackResult ret = OC_STACK_ERROR;
+
+ PeerCertContext *context = static_cast<PeerCertContext*>(ctx);
+ if (NULL == context)
+ {
+ oclog() << "Invalid Context";
+ return 1;
+ }
+
+ if (context->callback)
+ {
+ ret = context->callback(cert, depth);
+ }
+
+ if (0 == depth)
+ {
+ delete context;
+ }
+
+ return (OC_STACK_OK == ret)? 0 : 1;
+ }
+
+ OCStackResult OCSecure::setPeerCertCallback(PeerCertCB peerCertCallback)
+ {
+ OCStackResult result;
+ // UNSET cb
+ if (NULL == peerCertCallback)
+ {
+ result = OCSetPeerCertCallback(NULL, NULL);
+ if (OC_STACK_OK != result)
+ {
+ oclog() << "OCSetPeerCertCallback() Failed";
+ return result;
+ }
+ }
+