From 19dadd9c4524700a0747e1f40e6e1f36b80c9ce9 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 6 Aug 2024 07:56:26 -0700 Subject: [PATCH] [srp-client] add `KeyInfo` type alias for `KeyPair` or `KeyPairAsRef` (#10579) This commit introduces the `KeyInfo` type, which is a `typedef` to either `KeyPair` or `KeyPairAsRef` depending on whether the feature `KEY_REFERENCES_ENABLE` is enabled. This simplifies the code, removes repeated code, and eliminates extra `#if` checks when `KeyInfo` is used. --- src/core/net/srp_client.cpp | 39 ++++++++++--------------- src/core/net/srp_client.hpp | 58 ++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/core/net/srp_client.cpp b/src/core/net/srp_client.cpp index 504599f2f..15c6c6efc 100644 --- a/src/core/net/srp_client.cpp +++ b/src/core/net/srp_client.cpp @@ -1093,12 +1093,11 @@ Error Client::PrepareUpdateMessage(MsgInfo &aInfo) aInfo.mRecordCount = 0; #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE - aInfo.mKeyRef.SetKeyRef(kSrpEcdsaKeyRef); - SuccessOrExit(error = ReadOrGenerateKey(aInfo.mKeyRef)); -#else - SuccessOrExit(error = ReadOrGenerateKey(aInfo.mKeyPair)); + aInfo.mKeyInfo.SetKeyRef(kSrpEcdsaKeyRef); #endif + SuccessOrExit(error = ReadOrGenerateKey(aInfo.mKeyInfo)); + header.SetMessageId(mNextMessageId); // SRP Update (DNS Update) message must have exactly one record in @@ -1142,48 +1141,48 @@ exit: } #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE -Error Client::ReadOrGenerateKey(Crypto::Ecdsa::P256::KeyPairAsRef &aKeyRef) +Error Client::ReadOrGenerateKey(KeyInfo &aKeyInfo) { Error error = kErrorNone; Crypto::Ecdsa::P256::KeyPair keyPair; - VerifyOrExit(!Crypto::Storage::HasKey(aKeyRef.GetKeyRef())); + VerifyOrExit(!Crypto::Storage::HasKey(aKeyInfo.GetKeyRef())); error = Get().Read(keyPair); if (error == kErrorNone) { - if (aKeyRef.ImportKeyPair(keyPair) != kErrorNone) + if (aKeyInfo.ImportKeyPair(keyPair) != kErrorNone) { - SuccessOrExit(error = aKeyRef.Generate()); + SuccessOrExit(error = aKeyInfo.Generate()); } IgnoreError(Get().Delete()); } else { - SuccessOrExit(error = aKeyRef.Generate()); + SuccessOrExit(error = aKeyInfo.Generate()); } exit: return error; } #else -Error Client::ReadOrGenerateKey(Crypto::Ecdsa::P256::KeyPair &aKeyPair) +Error Client::ReadOrGenerateKey(KeyInfo &aKeyInfo) { Error error; - error = Get().Read(aKeyPair); + error = Get().Read(aKeyInfo); if (error == kErrorNone) { Crypto::Ecdsa::P256::PublicKey publicKey; - if (aKeyPair.GetPublicKey(publicKey) == kErrorNone) + if (aKeyInfo.GetPublicKey(publicKey) == kErrorNone) { ExitNow(); } } - SuccessOrExit(error = aKeyPair.Generate()); - IgnoreError(Get().Save(aKeyPair)); + SuccessOrExit(error = aKeyInfo.Generate()); + IgnoreError(Get().Save(aKeyInfo)); exit: return error; @@ -1566,11 +1565,7 @@ Error Client::AppendKeyRecord(MsgInfo &aInfo) const key.SetAlgorithm(Dns::KeyRecord::kAlgorithmEcdsaP256Sha256); key.SetLength(sizeof(Dns::KeyRecord) - sizeof(Dns::ResourceRecord) + sizeof(Crypto::Ecdsa::P256::PublicKey)); SuccessOrExit(error = aInfo.mMessage->Append(key)); -#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE - SuccessOrExit(error = aInfo.mKeyRef.GetPublicKey(publicKey)); -#else - SuccessOrExit(error = aInfo.mKeyPair.GetPublicKey(publicKey)); -#endif + SuccessOrExit(error = aInfo.mKeyInfo.GetPublicKey(publicKey)); SuccessOrExit(error = aInfo.mMessage->Append(publicKey)); aInfo.mRecordCount++; @@ -1707,11 +1702,7 @@ Error Client::AppendSignature(MsgInfo &aInfo) sha256.Update(*aInfo.mMessage, 0, offset); sha256.Finish(hash); -#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE - SuccessOrExit(error = aInfo.mKeyRef.Sign(hash, signature)); -#else - SuccessOrExit(error = aInfo.mKeyPair.Sign(hash, signature)); -#endif + SuccessOrExit(error = aInfo.mKeyInfo.Sign(hash, signature)); // Move back in message and append SIG RR now with compressed host // name (as signer's name) along with the calculated signature. diff --git a/src/core/net/srp_client.hpp b/src/core/net/srp_client.hpp index ddb013be0..d3d28ebd3 100644 --- a/src/core/net/srp_client.hpp +++ b/src/core/net/srp_client.hpp @@ -966,6 +966,12 @@ private: kForServicesAppendedInMessage, }; +#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE + typedef Crypto::Ecdsa::P256::KeyPairAsRef KeyInfo; +#else + typedef Crypto::Ecdsa::P256::KeyPair KeyInfo; +#endif + class TxJitter : public Clearable { // Manages the random TX jitter to use when sending SRP update @@ -1052,37 +1058,29 @@ private: uint16_t mDomainNameOffset; uint16_t mHostNameOffset; uint16_t mRecordCount; -#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE - Crypto::Ecdsa::P256::KeyPairAsRef mKeyRef; -#else - Crypto::Ecdsa::P256::KeyPair mKeyPair; -#endif + KeyInfo mKeyInfo; }; - Error Start(const Ip6::SockAddr &aServerSockAddr, Requester aRequester); - void Stop(Requester aRequester, StopMode aMode); - void Resume(void); - void Pause(void); - void HandleNotifierEvents(Events aEvents); - void HandleRoleChanged(void); - void HandleUnicastAddressEvent(Ip6::Netif::AddressEvent aEvent, const Ip6::Netif::UnicastAddress &aAddress); - bool ShouldUpdateHostAutoAddresses(void) const; - bool ShouldHostAutoAddressRegister(const Ip6::Netif::UnicastAddress &aUnicastAddress) const; - Error UpdateHostInfoStateOnAddressChange(void); - void UpdateServiceStateToRemove(Service &aService); - State GetState(void) const { return mState; } - void SetState(State aState); - bool ChangeHostAndServiceStates(const ItemState *aNewStates, ServiceStateChangeMode aMode); - void InvokeCallback(Error aError) const; - void InvokeCallback(Error aError, const HostInfo &aHostInfo, const Service *aRemovedServices) const; - void HandleHostInfoOrServiceChange(void); - void SendUpdate(void); - Error PrepareUpdateMessage(MsgInfo &aInfo); -#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE - Error ReadOrGenerateKey(Crypto::Ecdsa::P256::KeyPairAsRef &aKeyRef); -#else - Error ReadOrGenerateKey(Crypto::Ecdsa::P256::KeyPair &aKeyPair); -#endif + Error Start(const Ip6::SockAddr &aServerSockAddr, Requester aRequester); + void Stop(Requester aRequester, StopMode aMode); + void Resume(void); + void Pause(void); + void HandleNotifierEvents(Events aEvents); + void HandleRoleChanged(void); + void HandleUnicastAddressEvent(Ip6::Netif::AddressEvent aEvent, const Ip6::Netif::UnicastAddress &aAddress); + bool ShouldUpdateHostAutoAddresses(void) const; + bool ShouldHostAutoAddressRegister(const Ip6::Netif::UnicastAddress &aUnicastAddress) const; + Error UpdateHostInfoStateOnAddressChange(void); + void UpdateServiceStateToRemove(Service &aService); + State GetState(void) const { return mState; } + void SetState(State aState); + bool ChangeHostAndServiceStates(const ItemState *aNewStates, ServiceStateChangeMode aMode); + void InvokeCallback(Error aError) const; + void InvokeCallback(Error aError, const HostInfo &aHostInfo, const Service *aRemovedServices) const; + void HandleHostInfoOrServiceChange(void); + void SendUpdate(void); + Error PrepareUpdateMessage(MsgInfo &aInfo); + Error ReadOrGenerateKey(KeyInfo &aKeyInfo); Error AppendServiceInstructions(MsgInfo &aInfo); bool CanAppendService(const Service &aService); Error AppendServiceInstruction(Service &aService, MsgInfo &aInfo); @@ -1123,7 +1121,7 @@ private: static const char *StateToString(State aState); void LogRetryWaitInterval(void) const; #else - void LogRetryWaitInterval(void) const {} + void LogRetryWaitInterval(void) const {} #endif static const char kDefaultDomainName[];