void Joiner::HandleDiscoverResult(otActiveScanResult *aResult) { if (aResult != NULL) { JoinerRouter joinerRouter; otLogDebgMeshCoP("Received Discovery Response (%s)", static_cast<Mac::ExtAddress &>(aResult->mExtAddress).ToString().AsCString()); // Joining is disabled if the Steering Data is not included if (aResult->mSteeringData.mLength == 0) { otLogDebgMeshCoP("No steering data, joining disabled"); ExitNow(); } SteeringDataTlv steeringData; steeringData.SetLength(aResult->mSteeringData.mLength); memcpy(steeringData.GetValue(), aResult->mSteeringData.m8, steeringData.GetLength()); if (!steeringData.GetBit(mCcitt % steeringData.GetNumBits()) || !steeringData.GetBit(mAnsi % steeringData.GetNumBits())) { otLogDebgMeshCoP("Steering data does not include this device"); ExitNow(); } joinerRouter.mPriority = static_cast<uint16_t>(aResult->mRssi) + 0x80; if (!steeringData.DoesAllowAny()) { joinerRouter.mPriority += kSpecificPriorityBonus; } joinerRouter.mJoinerUdpPort = aResult->mJoinerUdpPort; joinerRouter.mPanId = aResult->mPanId; joinerRouter.mChannel = aResult->mChannel; joinerRouter.mExtAddr = static_cast<Mac::ExtAddress &>(aResult->mExtAddress); AddJoinerRouter(joinerRouter); } else { otError error = TryNextJoin(); if (error != OT_ERROR_NONE) { Complete(error); } } exit: return; }
int Dtls::HandleMbedtlsExportKeys(const unsigned char *aMasterSecret, const unsigned char *aKeyBlock, size_t aMacLength, size_t aKeyLength, size_t aIvLength) { OT_UNUSED_VARIABLE(aMasterSecret); uint8_t kek[Crypto::Sha256::kHashSize]; Crypto::Sha256 sha256; sha256.Start(); sha256.Update(aKeyBlock, 2 * static_cast<uint16_t>(aMacLength + aKeyLength + aIvLength)); sha256.Finish(kek); Get<KeyManager>().SetKek(kek); if (mCipherSuites[0] == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8) { otLogDebgMeshCoP("Generated KEK"); } #if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE else { otLogDebgCoap("ApplicationCoapSecure Generated KEK"); } #endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE return 0; }
void Dtls::HandleMbedtlsSetTimer(uint32_t aIntermediate, uint32_t aFinish) { if (mCipherSuites[0] == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8) { otLogDebgMeshCoP("Dtls::SetTimer"); } #if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE else { otLogDebgCoap("Dtls::ApplicationCoapSecure SetTimer"); } #endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE if (aFinish == 0) { mTimerSet = false; mTimer.Stop(); } else { mTimerSet = true; mTimer.Start(aFinish); mTimerIntermediate = TimerMilli::GetNow() + aIntermediate; } }
int Dtls::HandleMbedtlsGetTimer(void) { int rval; if (mCipherSuites[0] == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8) { otLogDebgMeshCoP("Dtls::HandleMbedtlsGetTimer"); } #if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE else { otLogDebgCoap("Dtls:: ApplicationCoapSecure HandleMbedtlsGetTimer"); } #endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE if (!mTimerSet) { rval = -1; } else if (!mTimer.IsRunning()) { rval = 2; } else if (static_cast<int32_t>(mTimerIntermediate - TimerMilli::GetNow()) <= 0) { rval = 1; } else { rval = 0; } return rval; }
int Dtls::HandleMbedtlsReceive(unsigned char *aBuf, size_t aLength) { int rval; if (mCipherSuites[0] == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8) { otLogDebgMeshCoP("Dtls::HandleMbedtlsReceive"); } #if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE else { otLogDebgCoap("Dtls:: ApplicationCoapSecure HandleMbedtlsReceive"); } #endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE VerifyOrExit(mReceiveMessage != NULL && mReceiveLength != 0, rval = MBEDTLS_ERR_SSL_WANT_READ); if (aLength > mReceiveLength) { aLength = mReceiveLength; } rval = mReceiveMessage->Read(mReceiveOffset, static_cast<uint16_t>(aLength), aBuf); mReceiveOffset += static_cast<uint16_t>(rval); mReceiveLength -= static_cast<uint16_t>(rval); exit: return rval; }
void CoapSecure::HandleTransmit(void) { otError error = OT_ERROR_NONE; ot::Message *message = mTransmitQueue.GetHead(); VerifyOrExit(message != NULL); mTransmitQueue.Dequeue(*message); if (mTransmitQueue.GetHead() != NULL) { mTransmitTask.Post(); } SuccessOrExit(error = mDtls.Send(*message, message->GetLength())); exit: if (error != OT_ERROR_NONE) { otLogNoteMeshCoP("CoapSecure Transmit: %s", otThreadErrorToString(error)); message->Free(); } else { otLogDebgMeshCoP("CoapSecure Transmit: %s", otThreadErrorToString(error)); } }
otError Joiner::TryNextJoin() { ThreadNetif & netif = GetNetif(); otError error = OT_ERROR_NOT_FOUND; JoinerRouter *joinerRouter = &mJoinerRouters[0]; for (size_t i = 1; i < OPENTHREAD_CONFIG_MAX_JOINER_ROUTER_ENTRIES; i++) { if (mJoinerRouters[i].mPriority > joinerRouter->mPriority) { joinerRouter = &mJoinerRouters[i]; } } if (joinerRouter->mPriority > 0) { Ip6::SockAddr sockaddr; joinerRouter->mPriority = 0; netif.GetMac().SetPanId(joinerRouter->mPanId); netif.GetMac().SetPanChannel(joinerRouter->mChannel); netif.GetIp6Filter().AddUnsecurePort(OPENTHREAD_CONFIG_JOINER_UDP_PORT); sockaddr.GetAddress().mFields.m16[0] = HostSwap16(0xfe80); sockaddr.GetAddress().SetIid(joinerRouter->mExtAddr); sockaddr.mPort = joinerRouter->mJoinerUdpPort; sockaddr.mScopeId = OT_NETIF_INTERFACE_ID_THREAD; netif.GetCoapSecure().Connect(sockaddr, Joiner::HandleSecureCoapClientConnect, this); mState = OT_JOINER_STATE_CONNECT; error = OT_ERROR_NONE; } else { otLogDebgMeshCoP("No joinable networks remaining to try"); } return error; }
int Dtls::HandleMbedtlsTransmit(const unsigned char *aBuf, size_t aLength) { otError error; int rval = 0; if (mCipherSuites[0] == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8) { otLogDebgMeshCoP("Dtls::HandleMbedtlsTransmit"); } #if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE else { otLogDebgCoap("Dtls::ApplicationCoapSecure HandleMbedtlsTransmit"); } #endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE error = HandleDtlsSend(aBuf, static_cast<uint16_t>(aLength), mMessageSubType); // Restore default sub type. mMessageSubType = mMessageDefaultSubType; switch (error) { case OT_ERROR_NONE: rval = static_cast<int>(aLength); break; case OT_ERROR_NO_BUFS: rval = MBEDTLS_ERR_SSL_WANT_WRITE; break; default: otLogWarnMeshCoP("Dtls::HandleMbedtlsTransmit: %s error", otThreadErrorToString(error)); rval = MBEDTLS_ERR_NET_SEND_FAILED; break; } return rval; }