From 28a21a11a2bb34e7decf169378678118a8ada3cb Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 12 Aug 2022 15:47:28 +0200 Subject: [PATCH] Return CHIP_ERROR_WELL_UNINITIALIZED when not initialized (#21827) --- .../linux/LinuxCommissionableDataProvider.cpp | 12 ++++++------ .../ExampleOperationalCredentialsIssuer.cpp | 2 +- src/credentials/DeviceAttestationVendorReserved.h | 2 +- src/crypto/CHIPCryptoPALOpenSSL.cpp | 6 +++--- src/crypto/CHIPCryptoPALTinyCrypt.cpp | 6 +++--- src/crypto/CHIPCryptoPALmbedTLS.cpp | 6 +++--- src/platform/EFR32/CHIPCryptoPALPsaEfr32.cpp | 6 +++--- src/platform/EFR32/Efr32PsaOperationalKeystore.cpp | 10 +++++----- src/platform/Tizen/ThreadStackManagerImpl.cpp | 10 +++++----- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/examples/platform/linux/LinuxCommissionableDataProvider.cpp b/examples/platform/linux/LinuxCommissionableDataProvider.cpp index 4d978abaf79ecc..a4d1d8ca3db52e 100644 --- a/examples/platform/linux/LinuxCommissionableDataProvider.cpp +++ b/examples/platform/linux/LinuxCommissionableDataProvider.cpp @@ -42,7 +42,7 @@ CHIP_ERROR LinuxCommissionableDataProvider::Init(chip::Optional> spake2pSalt, uint32_t spake2pIterationCount, chip::Optional setupPasscode, uint16_t discriminator) { - VerifyOrReturnError(mIsInitialized == false, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized == false, CHIP_ERROR_WELL_UNINITIALIZED); if (discriminator > chip::kMaxDiscriminatorValue) { @@ -174,21 +174,21 @@ CHIP_ERROR LinuxCommissionableDataProvider::Init(chip::Optional= kSpake2p_Max_PBKDF_Salt_Length, CHIP_ERROR_BUFFER_TOO_SMALL); memcpy(saltBuf.data(), mPaseSalt.data(), mPaseSalt.size()); @@ -199,7 +199,7 @@ CHIP_ERROR LinuxCommissionableDataProvider::GetSpake2pSalt(chip::MutableByteSpan CHIP_ERROR LinuxCommissionableDataProvider::GetSpake2pVerifier(chip::MutableByteSpan & verifierBuf, size_t & outVerifierLen) { - VerifyOrReturnError(mIsInitialized == true, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized == true, CHIP_ERROR_WELL_UNINITIALIZED); // By now, serialized verifier from Init should be correct size VerifyOrReturnError(mSerializedPaseVerifier.size() == kSpake2p_VerifierSerialized_Length, CHIP_ERROR_INTERNAL); @@ -214,7 +214,7 @@ CHIP_ERROR LinuxCommissionableDataProvider::GetSpake2pVerifier(chip::MutableByte CHIP_ERROR LinuxCommissionableDataProvider::GetSetupPasscode(uint32_t & setupPasscode) { - VerifyOrReturnError(mIsInitialized == true, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized == true, CHIP_ERROR_WELL_UNINITIALIZED); // Pretend not implemented if we don't have a passcode value externally set if (!mSetupPasscode.HasValue()) diff --git a/src/controller/ExampleOperationalCredentialsIssuer.cpp b/src/controller/ExampleOperationalCredentialsIssuer.cpp index 14ad78463ce225..cc8cd8fbcdb404 100644 --- a/src/controller/ExampleOperationalCredentialsIssuer.cpp +++ b/src/controller/ExampleOperationalCredentialsIssuer.cpp @@ -188,7 +188,7 @@ CHIP_ERROR ExampleOperationalCredentialsIssuer::GenerateNOCChain(const ByteSpan const ByteSpan & PAI, Callback::Callback * onCompletion) { - VerifyOrReturnError(mInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mInitialized, CHIP_ERROR_WELL_UNINITIALIZED); // At this point, Credential issuer may wish to validate the CSR information (void) attestationChallenge; (void) csrNonce; diff --git a/src/credentials/DeviceAttestationVendorReserved.h b/src/credentials/DeviceAttestationVendorReserved.h index d81cb11871da67..d05bd4d9d25a59 100644 --- a/src/credentials/DeviceAttestationVendorReserved.h +++ b/src/credentials/DeviceAttestationVendorReserved.h @@ -88,7 +88,7 @@ class DeviceAttestationVendorReservedDeconstructor */ CHIP_ERROR GetNextVendorReservedElement(struct VendorReservedElement & element) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); if (mIsDone) { return CHIP_END_OF_TLV; diff --git a/src/crypto/CHIPCryptoPALOpenSSL.cpp b/src/crypto/CHIPCryptoPALOpenSSL.cpp index df04e0a4f2d9e3..e7a84c76ba90bd 100644 --- a/src/crypto/CHIPCryptoPALOpenSSL.cpp +++ b/src/crypto/CHIPCryptoPALOpenSSL.cpp @@ -700,7 +700,7 @@ CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_len ERR_clear_error(); static_assert(P256ECDSASignature::Capacity() >= kP256_ECDSA_Signature_Length_Raw, "P256ECDSASignature must be large enough"); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); nid = _nidForCurve(MapECName(mPublicKey.Type())); VerifyOrExit(nid != NID_undef, error = CHIP_ERROR_INVALID_ARGUMENT); @@ -919,7 +919,7 @@ CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_k EC_KEY * ec_key = EC_KEY_dup(to_const_EC_KEY(&mKeypair)); VerifyOrExit(ec_key != nullptr, error = CHIP_ERROR_INTERNAL); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); local_key = EVP_PKEY_new(); VerifyOrExit(local_key != nullptr, error = CHIP_ERROR_INTERNAL); @@ -1199,7 +1199,7 @@ CHIP_ERROR P256Keypair::NewCertificateSigningRequest(uint8_t * out_csr, size_t & X509_NAME * subject = X509_NAME_new(); VerifyOrExit(subject != nullptr, error = CHIP_ERROR_INTERNAL); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); result = X509_REQ_set_version(x509_req, 0); VerifyOrExit(result == 1, error = CHIP_ERROR_INTERNAL); diff --git a/src/crypto/CHIPCryptoPALTinyCrypt.cpp b/src/crypto/CHIPCryptoPALTinyCrypt.cpp index 5d7cd9d0f75210..ae166a294744fc 100644 --- a/src/crypto/CHIPCryptoPALTinyCrypt.cpp +++ b/src/crypto/CHIPCryptoPALTinyCrypt.cpp @@ -513,7 +513,7 @@ static inline const mbedtls_uecc_keypair * to_const_keypair(const P256KeypairCon CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_length, P256ECDSASignature & out_signature) const { - VerifyOrReturnError(mInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError((msg != nullptr) && (msg_length > 0), CHIP_ERROR_INVALID_ARGUMENT); uint8_t digest[kSHA256_Hash_Length]; @@ -581,7 +581,7 @@ CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_k const mbedtls_uecc_keypair * keypair = to_const_keypair(&mKeypair); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); // Fully padded raw uncompressed points expected, first byte is always 0x04 i.e uncompressed result = uECC_shared_secret(remote_public_key.ConstBytes() + 1, keypair->private_key, Uint8::to_uchar(out_secret)); @@ -737,7 +737,7 @@ CHIP_ERROR P256Keypair::NewCertificateSigningRequest(uint8_t * out_csr, size_t & pk.CHIP_CRYPTO_PAL_PRIVATE(pk_ctx) = to_keypair(&mKeypair); VerifyOrExit(pk.CHIP_CRYPTO_PAL_PRIVATE(pk_info) != nullptr, error = CHIP_ERROR_INTERNAL); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); mbedtls_x509write_csr_set_key(&csr, &pk); diff --git a/src/crypto/CHIPCryptoPALmbedTLS.cpp b/src/crypto/CHIPCryptoPALmbedTLS.cpp index c436aa23282c01..9bce7e94430796 100644 --- a/src/crypto/CHIPCryptoPALmbedTLS.cpp +++ b/src/crypto/CHIPCryptoPALmbedTLS.cpp @@ -508,7 +508,7 @@ static inline const mbedtls_ecp_keypair * to_const_keypair(const P256KeypairCont CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_length, P256ECDSASignature & out_signature) const { - VerifyOrReturnError(mInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError((msg != nullptr) && (msg_length > 0), CHIP_ERROR_INVALID_ARGUMENT); uint8_t digest[kSHA256_Hash_Length]; @@ -651,7 +651,7 @@ CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_k const mbedtls_ecp_keypair * keypair = to_const_keypair(&mKeypair); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); result = mbedtls_ecp_group_load(&ecp_grp, MapECPGroupId(remote_public_key.Type())); VerifyOrExit(result == 0, error = CHIP_ERROR_INTERNAL); @@ -845,7 +845,7 @@ CHIP_ERROR P256Keypair::NewCertificateSigningRequest(uint8_t * out_csr, size_t & pk.CHIP_CRYPTO_PAL_PRIVATE(pk_ctx) = to_keypair(&mKeypair); VerifyOrExit(pk.CHIP_CRYPTO_PAL_PRIVATE(pk_info) != nullptr, error = CHIP_ERROR_INTERNAL); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); mbedtls_x509write_csr_set_key(&csr, &pk); diff --git a/src/platform/EFR32/CHIPCryptoPALPsaEfr32.cpp b/src/platform/EFR32/CHIPCryptoPALPsaEfr32.cpp index 95c1ee4116e169..6077681f19eb57 100644 --- a/src/platform/EFR32/CHIPCryptoPALPsaEfr32.cpp +++ b/src/platform/EFR32/CHIPCryptoPALPsaEfr32.cpp @@ -628,7 +628,7 @@ CHIP_ERROR P256Keypair::ECDSA_sign_msg(const uint8_t * msg, const size_t msg_len size_t output_length = 0; const psa_plaintext_ecp_keypair * keypair = to_const_keypair(&mKeypair); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrExit((msg != nullptr) && (msg_length > 0), error = CHIP_ERROR_INVALID_ARGUMENT); psa_crypto_init(); @@ -722,7 +722,7 @@ CHIP_ERROR P256Keypair::ECDH_derive_secret(const P256PublicKey & remote_public_k size_t output_length = 0; const psa_plaintext_ecp_keypair * keypair = to_const_keypair(&mKeypair); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); // Step 1: import plaintext key as volatile for ECDH psa_crypto_init(); @@ -839,7 +839,7 @@ CHIP_ERROR P256Keypair::Serialize(P256SerializedKeypair & output) const size_t len = output.Length() == 0 ? output.Capacity() : output.Length(); Encoding::BufferWriter bbuf(output, len); - VerifyOrExit(mInitialized, error = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mInitialized, error = CHIP_ERROR_WELL_UNINITIALIZED); bbuf.Put(mPublicKey, mPublicKey.Length()); diff --git a/src/platform/EFR32/Efr32PsaOperationalKeystore.cpp b/src/platform/EFR32/Efr32PsaOperationalKeystore.cpp index 532427c5e458f9..309df0296a1011 100644 --- a/src/platform/EFR32/Efr32PsaOperationalKeystore.cpp +++ b/src/platform/EFR32/Efr32PsaOperationalKeystore.cpp @@ -185,7 +185,7 @@ CHIP_ERROR Efr32PsaOperationalKeystore::NewOpKeypairForFabric(FabricIndex fabric MutableByteSpan & outCertificateSigningRequest) { CHIP_ERROR error = CHIP_NO_ERROR; - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); // If a key is pending, we cannot generate for a different fabric index until we commit or revert. @@ -267,7 +267,7 @@ CHIP_ERROR Efr32PsaOperationalKeystore::NewOpKeypairForFabric(FabricIndex fabric CHIP_ERROR Efr32PsaOperationalKeystore::ActivateOpKeypairForFabric(FabricIndex fabricIndex, const Crypto::P256PublicKey & nocPublicKey) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError(mPendingKeypair != nullptr, CHIP_ERROR_INVALID_FABRIC_INDEX); VerifyOrReturnError(IsValidFabricIndex(fabricIndex) && (fabricIndex == mPendingFabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); @@ -281,7 +281,7 @@ CHIP_ERROR Efr32PsaOperationalKeystore::ActivateOpKeypairForFabric(FabricIndex f CHIP_ERROR Efr32PsaOperationalKeystore::CommitOpKeypairForFabric(FabricIndex fabricIndex) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError(mPendingKeypair != nullptr, CHIP_ERROR_INVALID_FABRIC_INDEX); VerifyOrReturnError(IsValidFabricIndex(fabricIndex) && (fabricIndex == mPendingFabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); VerifyOrReturnError(mIsPendingKeypairActive == true, CHIP_ERROR_INCORRECT_STATE); @@ -329,7 +329,7 @@ CHIP_ERROR Efr32PsaOperationalKeystore::CommitOpKeypairForFabric(FabricIndex fab CHIP_ERROR Efr32PsaOperationalKeystore::RemoveOpKeypairForFabric(FabricIndex fabricIndex) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); // Remove pending keypair if we have it and the fabric ID matches @@ -396,7 +396,7 @@ void Efr32PsaOperationalKeystore::RevertPendingKeypair() CHIP_ERROR Efr32PsaOperationalKeystore::SignWithOpKeypair(FabricIndex fabricIndex, const ByteSpan & message, Crypto::P256ECDSASignature & outSignature) const { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); // Check to see whether the key is an activated pending key diff --git a/src/platform/Tizen/ThreadStackManagerImpl.cpp b/src/platform/Tizen/ThreadStackManagerImpl.cpp index 3b01c19e9b054e..91bd0a3562d08b 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.cpp +++ b/src/platform/Tizen/ThreadStackManagerImpl.cpp @@ -209,7 +209,7 @@ CHIP_ERROR ThreadStackManagerImpl::_SetThreadProvision(ByteSpan netInfo) { int threadErr = THREAD_ERROR_NONE; - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrReturnError(Thread::OperationalDataset::IsValid(netInfo), CHIP_ERROR_INVALID_ARGUMENT); threadErr = thread_network_set_active_dataset_tlvs(mThreadInstance, netInfo.data(), netInfo.size()); @@ -236,7 +236,7 @@ CHIP_ERROR ThreadStackManagerImpl::_GetThreadProvision(Thread::OperationalDatase uint8_t * tlvsData = nullptr; int tlvsLen; - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); threadErr = thread_network_get_active_dataset_tlvs(mThreadInstance, &tlvsData, &tlvsLen); VerifyOrExit(threadErr == THREAD_ERROR_NONE, ChipLogError(DeviceLayer, "FAIL: get active dataset tlvs")); @@ -285,7 +285,7 @@ CHIP_ERROR ThreadStackManagerImpl::_SetThreadEnabled(bool val) { int threadErr = THREAD_ERROR_NONE; - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); bool isEnabled = sInstance._IsThreadEnabled(); if (val && !isEnabled) @@ -362,7 +362,7 @@ CHIP_ERROR ThreadStackManagerImpl::_SetThreadDeviceType(ConnectivityManager::Thr int threadErr = THREAD_ERROR_NONE; thread_device_type_e devType; - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); switch (deviceType) { @@ -507,7 +507,7 @@ CHIP_ERROR ThreadStackManagerImpl::_AddSrpService(const char * aInstanceName, co CHIP_ERROR error = CHIP_NO_ERROR; int threadErr = THREAD_ERROR_NONE; - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); VerifyOrExit(aInstanceName, error = CHIP_ERROR_INVALID_ARGUMENT); VerifyOrExit(aName, error = CHIP_ERROR_INVALID_ARGUMENT);