From 105638227566a0c7437db1d08943fb43f237bfdc Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Fri, 24 Mar 2017 16:57:09 +0100 Subject: [PATCH] Create Joiner Finalize Response sub type to add KEK on retransmission. (#1511) --- src/core/coap/secure_coap_client.cpp | 7 ++++--- src/core/coap/secure_coap_client.hpp | 4 ++-- src/core/coap/secure_coap_server.cpp | 7 ++++--- src/core/coap/secure_coap_server.hpp | 4 ++-- src/core/common/message.hpp | 15 ++++++++------- src/core/meshcop/commissioner.cpp | 10 +++------- src/core/meshcop/commissioner.hpp | 1 - src/core/meshcop/dtls.cpp | 9 ++++++++- src/core/meshcop/dtls.hpp | 11 +++++++---- 9 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/core/coap/secure_coap_client.cpp b/src/core/coap/secure_coap_client.cpp index 53e9d1d89..f3e2c62c8 100644 --- a/src/core/coap/secure_coap_client.cpp +++ b/src/core/coap/secure_coap_client.cpp @@ -180,12 +180,12 @@ exit: otLogFuncExit(); } -ThreadError SecureClient::HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength) +ThreadError SecureClient::HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType) { - return static_cast(aContext)->HandleDtlsSend(aBuf, aLength); + return static_cast(aContext)->HandleDtlsSend(aBuf, aLength, aMessageSubType); } -ThreadError SecureClient::HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength) +ThreadError SecureClient::HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType) { ThreadError error = kThreadError_None; @@ -194,6 +194,7 @@ ThreadError SecureClient::HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength) if (mTransmitMessage == NULL) { VerifyOrExit((mTransmitMessage = mSocket.NewMessage(0)) != NULL, error = kThreadError_NoBufs); + mTransmitMessage->SetSubType(aMessageSubType); mTransmitMessage->SetLinkSecurityEnabled(false); } diff --git a/src/core/coap/secure_coap_client.hpp b/src/core/coap/secure_coap_client.hpp index a513339c2..4c75729c5 100644 --- a/src/core/coap/secure_coap_client.hpp +++ b/src/core/coap/secure_coap_client.hpp @@ -147,8 +147,8 @@ private: static void HandleDtlsReceive(void *aContext, uint8_t *aBuf, uint16_t aLength); void HandleDtlsReceive(uint8_t *aBuf, uint16_t aLength); - static ThreadError HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength); - ThreadError HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength); + static ThreadError HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType); + ThreadError HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType); static void HandleUdpTransmit(void *aContext); void HandleUdpTransmit(void); diff --git a/src/core/coap/secure_coap_server.cpp b/src/core/coap/secure_coap_server.cpp index 8eb92ffc1..97a22851c 100644 --- a/src/core/coap/secure_coap_server.cpp +++ b/src/core/coap/secure_coap_server.cpp @@ -176,12 +176,12 @@ exit: otLogFuncExit(); } -ThreadError SecureServer::HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength) +ThreadError SecureServer::HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType) { - return static_cast(aContext)->HandleDtlsSend(aBuf, aLength); + return static_cast(aContext)->HandleDtlsSend(aBuf, aLength, aMessageSubType); } -ThreadError SecureServer::HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength) +ThreadError SecureServer::HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType) { ThreadError error = kThreadError_None; @@ -190,6 +190,7 @@ ThreadError SecureServer::HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength) if (mTransmitMessage == NULL) { VerifyOrExit((mTransmitMessage = mSocket.NewMessage(0)) != NULL, error = kThreadError_NoBufs); + mTransmitMessage->SetSubType(aMessageSubType); mTransmitMessage->SetLinkSecurityEnabled(false); } diff --git a/src/core/coap/secure_coap_server.hpp b/src/core/coap/secure_coap_server.hpp index c6edf3ea3..ab20db50f 100644 --- a/src/core/coap/secure_coap_server.hpp +++ b/src/core/coap/secure_coap_server.hpp @@ -126,8 +126,8 @@ private: static void HandleDtlsReceive(void *aContext, uint8_t *aBuf, uint16_t aLength); void HandleDtlsReceive(uint8_t *aBuf, uint16_t aLength); - static ThreadError HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength); - ThreadError HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength); + static ThreadError HandleDtlsSend(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType); + ThreadError HandleDtlsSend(const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType); static void HandleUdpTransmit(void *aContext); void HandleUdpTransmit(void); diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 9ad2ef58d..7bf6f6d92 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -210,13 +210,14 @@ public: enum { - kSubTypeNone = 0, ///< None - kSubTypeMleAnnounce = 1, ///< MLE Announce - kSubTypeMleDiscoverRequest = 2, ///< MLE Discover Request - kSubTypeMleDiscoverResponse = 3, ///< MLE Discover Response - kSubTypeJoinerEntrust = 4, ///< Joiner Entrust - kSubTypeMplRetransmission = 5, ///< MPL next retranmission message - kSubTypeMleGeneral = 6, ///< General MLE + kSubTypeNone = 0, ///< None + kSubTypeMleAnnounce = 1, ///< MLE Announce + kSubTypeMleDiscoverRequest = 2, ///< MLE Discover Request + kSubTypeMleDiscoverResponse = 3, ///< MLE Discover Response + kSubTypeJoinerEntrust = 4, ///< Joiner Entrust + kSubTypeMplRetransmission = 5, ///< MPL next retranmission message + kSubTypeMleGeneral = 6, ///< General MLE + kSubTypeJoinerFinalizeResponse = 7, ///< Joiner Finalize Response }; enum diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index b01f48c5c..811279d1b 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -72,7 +72,6 @@ Commissioner::Commissioner(ThreadNetif &aThreadNetif): mTimer(aThreadNetif.GetIp6().mTimerScheduler, HandleTimer, this), mSessionId(0), mTransmitAttempts(0), - mSendKek(false), mRelayReceive(OPENTHREAD_URI_RELAY_RX, &Commissioner::HandleRelayReceive, this), mDatasetChanged(OPENTHREAD_URI_DATASET_CHANGED, &Commissioner::HandleDatasetChanged, this), mJoinerFinalize(OPENTHREAD_URI_JOINER_FINALIZE, &Commissioner::HandleJoinerFinalize, this), @@ -100,7 +99,6 @@ ThreadError Commissioner::Start(void) mState = kStatePetition; mTransmitAttempts = 0; - mSendKek = false; SendPetition(); @@ -120,7 +118,6 @@ ThreadError Commissioner::Stop(void) mState = kStateDisabled; mTransmitAttempts = 0; - mSendKek = false; mTimer.Stop(); @@ -904,6 +901,8 @@ void Commissioner::SendJoinFinalizeResponse(const Coap::Header &aRequestHeader, VerifyOrExit((message = mNetif.GetSecureCoapServer().NewMeshCoPMessage(responseHeader)) != NULL, error = kThreadError_NoBufs); + message->SetSubType(Message::kSubTypeJoinerFinalizeResponse); + stateTlv.Init(); stateTlv.SetState(aState); SuccessOrExit(error = message->Append(&stateTlv, sizeof(stateTlv))); @@ -912,7 +911,6 @@ void Commissioner::SendJoinFinalizeResponse(const Coap::Header &aRequestHeader, joinerMessageInfo.GetPeerAddr().SetIid(mJoinerIid); joinerMessageInfo.SetPeerPort(mJoinerPort); - mSendKek = true; #if OPENTHREAD_ENABLE_CERT_LOG uint8_t buf[OPENTHREAD_CONFIG_MESSAGE_BUFFER_SIZE]; VerifyOrExit(message->GetLength() <= sizeof(buf), ;); @@ -933,7 +931,6 @@ exit: if (error != kThreadError_None && message != NULL) { - mSendKek = false; message->Free(); } @@ -978,13 +975,12 @@ ThreadError Commissioner::SendRelayTransmit(Message &aMessage, const Ip6::Messag rloc.SetJoinerRouterLocator(mJoinerRloc); SuccessOrExit(error = message->Append(&rloc, sizeof(rloc))); - if (mSendKek) + if (aMessage.GetSubType() == Message::kSubTypeJoinerFinalizeResponse) { JoinerRouterKekTlv kek; kek.Init(); kek.SetKek(mNetif.GetKeyManager().GetKek()); SuccessOrExit(error = message->Append(&kek, sizeof(kek))); - mSendKek = false; } tlv.SetType(Tlv::kJoinerDtlsEncapsulation); diff --git a/src/core/meshcop/commissioner.hpp b/src/core/meshcop/commissioner.hpp index 9c7f51913..af8cdf2da 100644 --- a/src/core/meshcop/commissioner.hpp +++ b/src/core/meshcop/commissioner.hpp @@ -284,7 +284,6 @@ private: Timer mTimer; uint16_t mSessionId; uint8_t mTransmitAttempts; - bool mSendKek; Coap::Resource mRelayReceive; Coap::Resource mDatasetChanged; diff --git a/src/core/meshcop/dtls.cpp b/src/core/meshcop/dtls.cpp index 7b4fce28d..37d7c7111 100644 --- a/src/core/meshcop/dtls.cpp +++ b/src/core/meshcop/dtls.cpp @@ -61,6 +61,7 @@ Dtls::Dtls(ThreadNetif &aNetif): mSendHandler(NULL), mContext(NULL), mClient(false), + mMessageSubType(0), mNetif(aNetif) { memset(mPsk, 0, sizeof(mPsk)); @@ -89,6 +90,7 @@ ThreadError Dtls::Start(bool aClient, ConnectedHandler aConnectedHandler, Receiv mContext = aContext; mClient = aClient; mReceiveMessage = NULL; + mMessageSubType = 0; mbedtls_ssl_init(&mSsl); mbedtls_ssl_config_init(&mConf); @@ -204,6 +206,8 @@ ThreadError Dtls::Send(Message &aMessage, uint16_t aLength) VerifyOrExit(aLength <= kApplicationDataMaxLength, error = kThreadError_NoBufs); + // Store message specific sub type. + mMessageSubType = aMessage.GetSubType(); aMessage.Read(0, aLength, buffer); SuccessOrExit(error = MapError(mbedtls_ssl_write(&mSsl, buffer, aLength))); @@ -237,7 +241,10 @@ int Dtls::HandleMbedtlsTransmit(const unsigned char *aBuf, size_t aLength) otLogInfoMeshCoP(GetInstance(), "Dtls::HandleMbedtlsTransmit"); - error = mSendHandler(mContext, aBuf, (uint16_t)aLength); + error = mSendHandler(mContext, aBuf, static_cast(aLength), mMessageSubType); + + // Restore default sub type. + mMessageSubType = 0; switch (error) { diff --git a/src/core/meshcop/dtls.hpp b/src/core/meshcop/dtls.hpp index cedcd77ea..dd54a9d33 100644 --- a/src/core/meshcop/dtls.hpp +++ b/src/core/meshcop/dtls.hpp @@ -101,12 +101,13 @@ public: /** * This function pointer is called when data is ready to transmit for the DTLS session. * - * @param[in] aContext A pointer to application-specific context. - * @param[in] aBuf A pointer to the transmit data buffer. - * @param[in] aLength Number of bytes in the transmit data buffer. + * @param[in] aContext A pointer to application-specific context. + * @param[in] aBuf A pointer to the transmit data buffer. + * @param[in] aLength Number of bytes in the transmit data buffer. + * @param[in] aMessageSubtype A message sub type information for the sender. * */ - typedef ThreadError(*SendHandler)(void *aContext, const uint8_t *aBuf, uint16_t aLength); + typedef ThreadError(*SendHandler)(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType); /** * This method starts the DTLS service. @@ -253,6 +254,8 @@ private: void *mContext; bool mClient; + uint8_t mMessageSubType; + ThreadNetif &mNetif; };