[srp-server] simplify tracking of host key (#9296)

This commit simplifies tracking of host key in `Srp::Server` by saving
the `Key` directly in a `Host` instead of the saving the `KeyRecord`.
This commit is contained in:
Abtin Keshavarzian
2023-07-19 10:35:42 -07:00
committed by GitHub
parent a0f7253017
commit 5340a6e944
2 changed files with 44 additions and 46 deletions
+24 -24
View File
@@ -353,7 +353,7 @@ bool Server::HasNameConflictsWith(Host &aHost) const
bool hasConflicts = false;
const Host *existingHost = mHosts.FindMatching(aHost.GetFullName());
if (existingHost != nullptr && aHost.GetKeyRecord()->GetKey() != existingHost->GetKeyRecord()->GetKey())
if ((existingHost != nullptr) && (aHost.mKey != existingHost->mKey))
{
LogWarn("Name conflict: host name %s has already been allocated", aHost.GetFullName());
ExitNow(hasConflicts = true);
@@ -367,8 +367,7 @@ bool Server::HasNameConflictsWith(Host &aHost) const
for (const Host &host : mHosts)
{
if (host.HasService(service.GetInstanceName()) &&
aHost.GetKeyRecord()->GetKey() != host.GetKeyRecord()->GetKey())
if (host.HasService(service.GetInstanceName()) && (aHost.mKey != host.mKey))
{
LogWarn("Name conflict: service name %s has already been allocated", service.GetInstanceName());
ExitNow(hasConflicts = true);
@@ -894,8 +893,15 @@ Error Server::ProcessHostDescriptionInstruction(Host &aHost,
SuccessOrExit(error = aMessage.Read(offset, keyRecord));
VerifyOrExit(keyRecord.IsValid(), error = kErrorParse);
VerifyOrExit(aHost.GetKeyRecord() == nullptr || *aHost.GetKeyRecord() == keyRecord, error = kErrorSecurity);
aHost.SetKeyRecord(keyRecord);
if (aHost.mParsedKey)
{
VerifyOrExit(aHost.mKey == keyRecord.GetKey(), error = kErrorSecurity);
}
else
{
aHost.mParsedKey = true;
aHost.mKey = keyRecord.GetKey();
}
}
offset += record.GetSize();
@@ -904,7 +910,7 @@ Error Server::ProcessHostDescriptionInstruction(Host &aHost,
// Verify that we have a complete Host Description Instruction.
VerifyOrExit(aHost.GetFullName() != nullptr, error = kErrorFailed);
VerifyOrExit(aHost.GetKeyRecord() != nullptr, error = kErrorFailed);
VerifyOrExit(aHost.mParsedKey, error = kErrorFailed);
// We check the number of host addresses after processing of the
// Lease Option in the Addition Section and determining whether
@@ -1219,8 +1225,8 @@ Error Server::ProcessAdditionalSection(Host *aHost, const Message &aMessage, Mes
VerifyOrExit(sigRecord.GetTypeCovered() == 0, error = kErrorFailed);
VerifyOrExit(signatureLength == Crypto::Ecdsa::P256::Signature::kSize, error = kErrorParse);
SuccessOrExit(error = VerifySignature(*aHost->GetKeyRecord(), aMessage, aMetadata.mDnsHeader, sigOffset,
sigRdataOffset, sigRecord.GetLength(), signerName));
SuccessOrExit(error = VerifySignature(aHost->mKey, aMessage, aMetadata.mDnsHeader, sigOffset, sigRdataOffset,
sigRecord.GetLength(), signerName));
aMetadata.mOffset = offset;
@@ -1233,13 +1239,13 @@ exit:
return error;
}
Error Server::VerifySignature(const Dns::Ecdsa256KeyRecord &aKeyRecord,
const Message &aMessage,
Dns::UpdateHeader aDnsHeader,
uint16_t aSigOffset,
uint16_t aSigRdataOffset,
uint16_t aSigRdataLength,
const char *aSignerName) const
Error Server::VerifySignature(const Host::Key &aKey,
const Message &aMessage,
Dns::UpdateHeader aDnsHeader,
uint16_t aSigOffset,
uint16_t aSigRdataOffset,
uint16_t aSigRdataLength,
const char *aSignerName) const
{
Error error;
uint16_t offset = aMessage.GetOffset();
@@ -1273,7 +1279,7 @@ Error Server::VerifySignature(const Dns::Ecdsa256KeyRecord &aKeyRecord,
signatureOffset = aSigRdataOffset + aSigRdataLength - Crypto::Ecdsa::P256::Signature::kSize;
SuccessOrExit(error = aMessage.Read(signatureOffset, signature));
error = aKeyRecord.GetKey().Verify(hash, signature);
error = aKey.Verify(hash, signature);
exit:
if (error != kErrorNone)
@@ -1894,8 +1900,9 @@ Server::Host::Host(Instance &aInstance, TimeMilli aUpdateTime)
, mLease(0)
, mKeyLease(0)
, mUpdateTime(aUpdateTime)
, mParsedKey(false)
, mUseShortLeaseOption(false)
{
mKeyRecord.Clear();
}
Server::Host::~Host(void) { FreeAllServices(); }
@@ -1925,13 +1932,6 @@ bool Server::Host::Matches(const char *aFullName) const
return StringMatch(mFullName.AsCString(), aFullName, kStringCaseInsensitiveMatch);
}
void Server::Host::SetKeyRecord(Dns::Ecdsa256KeyRecord &aKeyRecord)
{
OT_ASSERT(aKeyRecord.IsValid());
mKeyRecord = aKeyRecord;
}
TimeMilli Server::Host::GetExpireTime(void) const
{
OT_ASSERT(!IsDeleted());
+20 -22
View File
@@ -443,6 +443,8 @@ public:
friend class Heap::Allocatable<Host>;
public:
typedef Crypto::Ecdsa::P256::PublicKey Key; ///< Host key (public ECDSA P256 key).
/**
* Tells whether the Host object has been deleted.
*
@@ -511,13 +513,12 @@ public:
void GetLeaseInfo(LeaseInfo &aLeaseInfo) const;
/**
* Returns the KEY resource record of the host.
* Returns the key associated with this host.
*
* @returns A pointer to the ECDSA P 256 public key resource record
* if there is valid one. `nullptr` if no valid key exists.
* @returns The host key.
*
*/
const Dns::Ecdsa256KeyRecord *GetKeyRecord(void) const { return mKeyRecord.IsValid() ? &mKeyRecord : nullptr; }
const Key &GetKey(void) const { return mKey; }
/**
* Returns the expire time (in milliseconds) of the host.
@@ -568,7 +569,6 @@ public:
~Host(void);
Error SetFullName(const char *aFullName);
void SetKeyRecord(Dns::Ecdsa256KeyRecord &aKeyRecord);
void SetTtl(uint32_t aTtl) { mTtl = aTtl; }
void SetLease(uint32_t aLease) { mLease = aLease; }
void SetKeyLease(uint32_t aKeyLease) { mKeyLease = aKeyLease; }
@@ -589,16 +589,14 @@ public:
Host *mNext;
Heap::String mFullName;
Heap::Array<Ip6::Address> mAddresses;
// TODO(wgtdkp): there is no necessary to save the entire resource
// record, saving only the ECDSA-256 public key should be enough.
Dns::Ecdsa256KeyRecord mKeyRecord;
uint32_t mTtl; // The TTL in seconds.
uint32_t mLease; // The LEASE time in seconds.
uint32_t mKeyLease; // The KEY-LEASE time in seconds.
TimeMilli mUpdateTime;
LinkedList<Service> mServices;
bool mUseShortLeaseOption; // Use short lease option (lease only - 4 byte) when responding.
Key mKey;
uint32_t mTtl; // The TTL in seconds.
uint32_t mLease; // The LEASE time in seconds.
uint32_t mKeyLease; // The KEY-LEASE time in seconds.
TimeMilli mUpdateTime;
LinkedList<Service> mServices;
bool mParsedKey : 1;
bool mUseShortLeaseOption : 1; // Use short lease option (lease only 4 bytes).
};
/**
@@ -966,13 +964,13 @@ private:
void ProcessDnsUpdate(Message &aMessage, MessageMetadata &aMetadata);
Error ProcessUpdateSection(Host &aHost, const Message &aMessage, MessageMetadata &aMetadata) const;
Error ProcessAdditionalSection(Host *aHost, const Message &aMessage, MessageMetadata &aMetadata) const;
Error VerifySignature(const Dns::Ecdsa256KeyRecord &aKeyRecord,
const Message &aMessage,
Dns::UpdateHeader aDnsHeader,
uint16_t aSigOffset,
uint16_t aSigRdataOffset,
uint16_t aSigRdataLength,
const char *aSignerName) const;
Error VerifySignature(const Host::Key &aKey,
const Message &aMessage,
Dns::UpdateHeader aDnsHeader,
uint16_t aSigOffset,
uint16_t aSigRdataOffset,
uint16_t aSigRdataLength,
const char *aSignerName) const;
Error ProcessZoneSection(const Message &aMessage, MessageMetadata &aMetadata) const;
Error ProcessHostDescriptionInstruction(Host &aHost,
const Message &aMessage,