[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.
This commit is contained in:
Abtin Keshavarzian
2024-08-06 08:56:26 -06:00
committed by GitHub
parent 634745dd72
commit 19dadd9c45
2 changed files with 43 additions and 54 deletions
+15 -24
View File
@@ -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<Settings>().Read<Settings::SrpEcdsaKey>(keyPair);
if (error == kErrorNone)
{
if (aKeyRef.ImportKeyPair(keyPair) != kErrorNone)
if (aKeyInfo.ImportKeyPair(keyPair) != kErrorNone)
{
SuccessOrExit(error = aKeyRef.Generate());
SuccessOrExit(error = aKeyInfo.Generate());
}
IgnoreError(Get<Settings>().Delete<Settings::SrpEcdsaKey>());
}
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<Settings>().Read<Settings::SrpEcdsaKey>(aKeyPair);
error = Get<Settings>().Read<Settings::SrpEcdsaKey>(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<Settings>().Save<Settings::SrpEcdsaKey>(aKeyPair));
SuccessOrExit(error = aKeyInfo.Generate());
IgnoreError(Get<Settings>().Save<Settings::SrpEcdsaKey>(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.
+28 -30
View File
@@ -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<TxJitter>
{
// 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[];