Skip to content

Commit 1249007

Browse files
authored
Merge 2e6bd54 into 7f750af
2 parents 7f750af + 2e6bd54 commit 1249007

28 files changed

+487
-205
lines changed

src/app/server/Server.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,11 @@ void Server::ResumeSubscriptions()
556556
Credentials::IgnoreCertificateValidityPeriodPolicy Server::sDefaultCertValidityPolicy;
557557

558558
KvsPersistentStorageDelegate CommonCaseDeviceServerInitParams::sKvsPersistenStorageDelegate;
559+
#if CHIP_CRYPTO_PSA
560+
Crypto::PSAOperationalKeystore CommonCaseDeviceServerInitParams::sPersistentStorageOperationalKeystore;
561+
#else
559562
PersistentStorageOperationalKeystore CommonCaseDeviceServerInitParams::sPersistentStorageOperationalKeystore;
563+
#endif
560564
Credentials::PersistentStorageOpCertStore CommonCaseDeviceServerInitParams::sPersistentStorageOpCertStore;
561565
Credentials::GroupDataProviderImpl CommonCaseDeviceServerInitParams::sGroupDataProvider;
562566
app::DefaultTimerDelegate CommonCaseDeviceServerInitParams::sTimerDelegate;

src/app/server/Server.h

+12
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040
#include <credentials/PersistentStorageOpCertStore.h>
4141
#include <crypto/DefaultSessionKeystore.h>
4242
#include <crypto/OperationalKeystore.h>
43+
#if CHIP_CRYPTO_PSA
44+
#include <crypto/PSAOperationalKeystore.h>
45+
#else
4346
#include <crypto/PersistentStorageOperationalKeystore.h>
47+
#endif
4448
#include <inet/InetConfig.h>
4549
#include <lib/core/CHIPConfig.h>
4650
#include <lib/support/SafeInt.h>
@@ -206,7 +210,11 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams
206210
{
207211
// WARNING: PersistentStorageOperationalKeystore::Finish() is never called. It's fine for
208212
// for examples and for now.
213+
#if !CHIP_CRYPTO_PSA
209214
ReturnErrorOnFailure(sPersistentStorageOperationalKeystore.Init(this->persistentStorageDelegate));
215+
#else
216+
// Note: PSA Operational keystore does not require initialization
217+
#endif
210218
this->operationalKeystore = &sPersistentStorageOperationalKeystore;
211219
}
212220

@@ -263,7 +271,11 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams
263271

264272
private:
265273
static KvsPersistentStorageDelegate sKvsPersistenStorageDelegate;
274+
#if CHIP_CRYPTO_PSA
275+
static Crypto::PSAOperationalKeystore sPersistentStorageOperationalKeystore;
276+
#else
266277
static PersistentStorageOperationalKeystore sPersistentStorageOperationalKeystore;
278+
#endif
267279
static Credentials::PersistentStorageOpCertStore sPersistentStorageOpCertStore;
268280
static Credentials::GroupDataProviderImpl sGroupDataProvider;
269281
static chip::app::DefaultTimerDelegate sTimerDelegate;

src/app/tests/integration/common.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727
#include <app/tests/integration/common.h>
2828
#include <credentials/PersistentStorageOpCertStore.h>
29+
#if CHIP_CRYPTO_PSA
30+
#include <crypto/PSAOperationalKeystore.h>
31+
#else
2932
#include <crypto/PersistentStorageOperationalKeystore.h>
33+
#endif
3034
#include <lib/core/CHIPCore.h>
3135
#include <lib/core/TLVDebug.h>
3236
#include <lib/support/CodeUtils.h>
@@ -41,7 +45,11 @@ chip::SessionManager gSessionManager;
4145
chip::secure_channel::MessageCounterManager gMessageCounterManager;
4246
chip::SessionHolder gSession;
4347
chip::TestPersistentStorageDelegate gStorage;
48+
#if CHIP_CRYPTO_PSA
49+
chip::Crypto::PSAOperationalKeystore gOperationalKeystore;
50+
#else
4451
chip::PersistentStorageOperationalKeystore gOperationalKeystore;
52+
#endif
4553
chip::Credentials::PersistentStorageOpCertStore gOpCertStore;
4654
chip::Crypto::DefaultSessionKeystore gSessionKeystore;
4755

@@ -64,8 +72,10 @@ void InitializeChip()
6472
err = gOpCertStore.Init(&gStorage);
6573
SuccessOrExit(err);
6674

75+
#if !CHIP_CRYPTO_PSA
6776
err = gOperationalKeystore.Init(&gStorage);
6877
SuccessOrExit(err);
78+
#endif
6979

7080
fabricTableInitParams.storage = &gStorage;
7181
fabricTableInitParams.operationalKeystore = &gOperationalKeystore;
@@ -92,7 +102,9 @@ void ShutdownChip()
92102
gSessionManager.Shutdown();
93103

94104
gFabricTable.Shutdown();
105+
#if !CHIP_CRYPTO_PSA
95106
gOperationalKeystore.Finish();
107+
#endif
96108
gOpCertStore.Finish();
97109

98110
chip::DeviceLayer::PlatformMgr().Shutdown();

src/app/tests/suites/credentials/TestHarnessDACProvider.cpp

+2-11
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,6 @@ bool ReadValue(Json::Value jsonValue)
176176
return false;
177177
}
178178

179-
// TODO: This should be moved to a method of P256Keypair
180-
CHIP_ERROR LoadKeypairFromRaw(ByteSpan private_key, ByteSpan public_key, Crypto::P256Keypair & keypair)
181-
{
182-
Crypto::P256SerializedKeypair serialized_keypair;
183-
ReturnErrorOnFailure(serialized_keypair.SetLength(private_key.size() + public_key.size()));
184-
memcpy(serialized_keypair.Bytes(), public_key.data(), public_key.size());
185-
memcpy(serialized_keypair.Bytes() + public_key.size(), private_key.data(), private_key.size());
186-
return keypair.Deserialize(serialized_keypair);
187-
}
188-
189179
} // namespace
190180

191181
TestHarnessDACProvider::TestHarnessDACProvider()
@@ -321,7 +311,8 @@ CHIP_ERROR TestHarnessDACProvider::SignWithDeviceAttestationKey(const ByteSpan &
321311

322312
// In a non-exemplary implementation, the public key is not needed here. It is used here merely because
323313
// Crypto::P256Keypair is only (currently) constructable from raw keys if both private/public keys are present.
324-
ReturnErrorOnFailure(LoadKeypairFromRaw(mDacPrivateKey, mDacPublicKey, keypair));
314+
ReturnErrorOnFailure(keypair.ImportRawKeypair(mDacPrivateKey, mDacPublicKey));
315+
325316
ReturnErrorOnFailure(keypair.ECDSA_sign_msg(message_to_sign.data(), message_to_sign.size(), signature));
326317

327318
return CopySpanToMutableSpan(ByteSpan{ signature.ConstBytes(), signature.Length() }, out_signature_buffer);

src/credentials/FabricTable.cpp

+4-11
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,6 @@ CHIP_ERROR FabricInfo::SetOperationalKeypair(const P256Keypair * keyPair)
241241
{
242242
VerifyOrReturnError(keyPair != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
243243

244-
P256SerializedKeypair serialized;
245-
ReturnErrorOnFailure(keyPair->Serialize(serialized));
246-
247244
if (mHasExternallyOwnedOperationalKey)
248245
{
249246
// Drop it, so we will allocate an internally owned one.
@@ -256,7 +253,7 @@ CHIP_ERROR FabricInfo::SetOperationalKeypair(const P256Keypair * keyPair)
256253
mOperationalKey = chip::Platform::New<P256Keypair>();
257254
}
258255
VerifyOrReturnError(mOperationalKey != nullptr, CHIP_ERROR_NO_MEMORY);
259-
return mOperationalKey->Deserialize(serialized);
256+
return mOperationalKey->Copy(*keyPair);
260257
}
261258

262259
CHIP_ERROR FabricInfo::SetExternallyOwnedOperationalKeypair(P256Keypair * keyPair)
@@ -690,16 +687,12 @@ CHIP_ERROR FabricTable::AddNewFabricForTest(const ByteSpan & rootCert, const Byt
690687
CHIP_ERROR err = CHIP_ERROR_INTERNAL;
691688

692689
Crypto::P256Keypair injectedOpKey;
693-
Crypto::P256SerializedKeypair injectedOpKeysSerialized;
694-
695690
Crypto::P256Keypair * opKey = nullptr;
691+
696692
if (!opKeySpan.empty())
697693
{
698-
VerifyOrReturnError(opKeySpan.size() == injectedOpKeysSerialized.Capacity(), CHIP_ERROR_INVALID_ARGUMENT);
699-
700-
memcpy(injectedOpKeysSerialized.Bytes(), opKeySpan.data(), opKeySpan.size());
701-
SuccessOrExit(err = injectedOpKeysSerialized.SetLength(opKeySpan.size()));
702-
SuccessOrExit(err = injectedOpKey.Deserialize(injectedOpKeysSerialized));
694+
VerifyOrReturnError(opKeySpan.size() == kP256_PublicKey_Length + kP256_PrivateKey_Length, CHIP_ERROR_INVALID_ARGUMENT);
695+
SuccessOrExit(err = injectedOpKey.ImportRawKeypair(opKeySpan));
703696
opKey = &injectedOpKey;
704697
}
705698

src/credentials/TestOnlyLocalCertificateAuthority.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class TestOnlyLocalCertificateAuthority
6969

7070
if (rootKeyPair.Length() != 0)
7171
{
72-
mCurrentStatus = mRootKeypair->Deserialize(rootKeyPair);
72+
mCurrentStatus = mRootKeypair->ImportRawKeypair(rootKeyPair.Span());
7373
SuccessOrExit(mCurrentStatus);
7474
}
7575
else

src/credentials/examples/DeviceAttestationCredsExample.cpp

+1-11
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ namespace Examples {
3131

3232
namespace {
3333

34-
// TODO: This should be moved to a method of P256Keypair
35-
CHIP_ERROR LoadKeypairFromRaw(ByteSpan private_key, ByteSpan public_key, Crypto::P256Keypair & keypair)
36-
{
37-
Crypto::P256SerializedKeypair serialized_keypair;
38-
ReturnErrorOnFailure(serialized_keypair.SetLength(private_key.size() + public_key.size()));
39-
memcpy(serialized_keypair.Bytes(), public_key.data(), public_key.size());
40-
memcpy(serialized_keypair.Bytes() + public_key.size(), private_key.data(), private_key.size());
41-
return keypair.Deserialize(serialized_keypair);
42-
}
43-
4434
class ExampleDACProvider : public DeviceAttestationCredentialsProvider
4535
{
4636
public:
@@ -196,7 +186,7 @@ CHIP_ERROR ExampleDACProvider::SignWithDeviceAttestationKey(const ByteSpan & mes
196186

197187
// In a non-exemplary implementation, the public key is not needed here. It is used here merely because
198188
// Crypto::P256Keypair is only (currently) constructable from raw keys if both private/public keys are present.
199-
ReturnErrorOnFailure(LoadKeypairFromRaw(DevelopmentCerts::kDacPrivateKey, DevelopmentCerts::kDacPublicKey, keypair));
189+
ReturnErrorOnFailure(keypair.ImportRawKeypair(DevelopmentCerts::kDacPrivateKey, DevelopmentCerts::kDacPublicKey));
200190
ReturnErrorOnFailure(keypair.ECDSA_sign_msg(message_to_sign.data(), message_to_sign.size(), signature));
201191

202192
return CopySpanToMutableSpan(ByteSpan{ signature.ConstBytes(), signature.Length() }, out_signature_buffer);

src/credentials/tests/TestCertificationDeclaration.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,11 @@ static void TestCD_CMSSignAndVerify(nlTestSuite * inSuite, void * inContext)
363363

364364
// Test with known key
365365
P256Keypair keypair2;
366-
P256SerializedKeypair serializedKeypair;
367-
memcpy(serializedKeypair.Bytes(), sTestCMS_SignerSerializedKeypair, sizeof(sTestCMS_SignerSerializedKeypair));
368-
serializedKeypair.SetLength(sizeof(sTestCMS_SignerSerializedKeypair));
369366
cdContentIn = ByteSpan(sTestCMS_CDContent02);
370367
signedMessage = MutableByteSpan(signedMessageBuf);
371-
NL_TEST_ASSERT(inSuite, keypair2.Deserialize(serializedKeypair) == CHIP_NO_ERROR);
368+
NL_TEST_ASSERT(inSuite,
369+
keypair2.ImportRawKeypair(sTestCMS_SignerSerializedKeypair, sizeof(sTestCMS_SignerSerializedKeypair)) ==
370+
CHIP_NO_ERROR);
372371
NL_TEST_ASSERT(inSuite, CMS_Sign(cdContentIn, signerKeyId, keypair2, signedMessage) == CHIP_NO_ERROR);
373372
NL_TEST_ASSERT(inSuite, CMS_Verify(signedMessage, keypair2.Pubkey(), cdContentOut) == CHIP_NO_ERROR);
374373
NL_TEST_ASSERT(inSuite, cdContentIn.data_equal(cdContentOut));

0 commit comments

Comments
 (0)