|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <crypto/OperationalKeystore.h> |
| 19 | +#include <lib/core/CHIPError.h> |
| 20 | +#include <lib/core/CHIPPersistentStorageDelegate.h> |
| 21 | +#include <lib/core/CHIPSafeCasts.h> |
| 22 | +#include <lib/core/DataModelTypes.h> |
| 23 | +#include <lib/support/CHIPMem.h> |
| 24 | +#include <lib/support/CodeUtils.h> |
| 25 | +#include <lib/support/DefaultStorageKeyAllocator.h> |
| 26 | +#include <lib/support/SafeInt.h> |
| 27 | +#include <trusty_matter.h> |
| 28 | + |
| 29 | +#include "PersistentStorageOperationalKeystore.h" |
| 30 | + |
| 31 | +using namespace matter; |
| 32 | +using namespace chip::Crypto; |
| 33 | + |
| 34 | +namespace chip { |
| 35 | + |
| 36 | +TrustyMatter & GetTrustyMatter() |
| 37 | +{ |
| 38 | + static TrustyMatter instance; |
| 39 | + return instance; |
| 40 | +} |
| 41 | + |
| 42 | +bool PersistentStorageOperationalKeystore::HasOpKeypairForFabric(FabricIndex fabricIndex) const |
| 43 | +{ |
| 44 | + VerifyOrReturnError(IsValidFabricIndex(fabricIndex), false); |
| 45 | + |
| 46 | + // If there was a pending keypair, then there's really a usable key |
| 47 | + if (mIsPendingKeypairActive && (fabricIndex == mPendingFabricIndex) && (mPendingKeypair != nullptr)) |
| 48 | + { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + return GetTrustyMatter().HasOpKeypairForFabric(fabricIndex); |
| 53 | +} |
| 54 | + |
| 55 | +CHIP_ERROR PersistentStorageOperationalKeystore::NewOpKeypairForFabric(FabricIndex fabricIndex, |
| 56 | + MutableByteSpan & outCertificateSigningRequest) |
| 57 | +{ |
| 58 | + P256SerializedKeypair serializedKeypair; |
| 59 | + uint8_t public_key[kP256_PublicKey_Length] = { 0 }; |
| 60 | + uint64_t p256_handle = 0; |
| 61 | + |
| 62 | + VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); |
| 63 | + // If a key is pending, we cannot generate for a different fabric index until we commit or revert. |
| 64 | + if ((mPendingFabricIndex != kUndefinedFabricIndex) && (fabricIndex != mPendingFabricIndex)) |
| 65 | + { |
| 66 | + return CHIP_ERROR_INVALID_FABRIC_INDEX; |
| 67 | + } |
| 68 | + VerifyOrReturnError(outCertificateSigningRequest.size() >= Crypto::kMIN_CSR_Buffer_Size, CHIP_ERROR_BUFFER_TOO_SMALL); |
| 69 | + |
| 70 | + // Replace previous pending keypair, if any was previously allocated |
| 71 | + ResetPendingKey(); |
| 72 | + |
| 73 | + mPendingKeypair = Platform::New<Crypto::P256Keypair>(); |
| 74 | + VerifyOrReturnError(mPendingKeypair != nullptr, CHIP_ERROR_NO_MEMORY); |
| 75 | + |
| 76 | + memcpy(serializedKeypair.Bytes(), public_key, kP256_PublicKey_Length); |
| 77 | + |
| 78 | + // kTrustyMagicNumberFabricIndex = { 0x2, 0x3, 0x5, 0x7, 0x0B, 0x0D, 0x11 } |
| 79 | + // It is a flag to inform the Trusty OS to use a fixed p256_handle, and provide an opaque key |
| 80 | + memcpy(&p256_handle, kTrustyMagicNumberFabricIndex, sizeof(kTrustyMagicNumberFabricIndex)); |
| 81 | + // FabricIndex is a uint8_t indeed |
| 82 | + static_assert(1 == sizeof(FabricIndex)); |
| 83 | + |
| 84 | + // Append the fabric index after the magic number |
| 85 | + memcpy((uint8_t *) &p256_handle + sizeof(kTrustyMagicNumberFabricIndex), &fabricIndex, sizeof(FabricIndex)); |
| 86 | + |
| 87 | + memcpy(serializedKeypair.Bytes() + kP256_PublicKey_Length, &p256_handle, sizeof(uint64_t)); |
| 88 | + serializedKeypair.SetLength(kP256_PublicKey_Length + sizeof(uint64_t)); |
| 89 | + |
| 90 | + ReturnErrorOnFailure(mPendingKeypair->Deserialize(serializedKeypair)); |
| 91 | + |
| 92 | + mPendingKeypair->Initialize(Crypto::ECPKeyTarget::ECDSA); |
| 93 | + |
| 94 | + size_t csrLength = outCertificateSigningRequest.size(); |
| 95 | + CHIP_ERROR err = mPendingKeypair->NewCertificateSigningRequest(outCertificateSigningRequest.data(), csrLength); |
| 96 | + if (err != CHIP_NO_ERROR) |
| 97 | + { |
| 98 | + ResetPendingKey(); |
| 99 | + return CHIP_ERROR_HSM; |
| 100 | + } |
| 101 | + |
| 102 | + outCertificateSigningRequest.reduce_size(csrLength); |
| 103 | + mPendingFabricIndex = fabricIndex; |
| 104 | + |
| 105 | + return CHIP_NO_ERROR; |
| 106 | +} |
| 107 | + |
| 108 | +CHIP_ERROR PersistentStorageOperationalKeystore::ActivateOpKeypairForFabric(FabricIndex fabricIndex, |
| 109 | + const Crypto::P256PublicKey & nocPublicKey) |
| 110 | +{ |
| 111 | + VerifyOrReturnError(mPendingKeypair != nullptr, CHIP_ERROR_INVALID_FABRIC_INDEX); |
| 112 | + VerifyOrReturnError(IsValidFabricIndex(fabricIndex) && (fabricIndex == mPendingFabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); |
| 113 | + |
| 114 | + // Validate public key being activated matches last generated pending keypair |
| 115 | + VerifyOrReturnError(mPendingKeypair->Pubkey().Matches(nocPublicKey), CHIP_ERROR_INVALID_PUBLIC_KEY); |
| 116 | + |
| 117 | + mIsPendingKeypairActive = true; |
| 118 | + |
| 119 | + return CHIP_NO_ERROR; |
| 120 | +} |
| 121 | + |
| 122 | +CHIP_ERROR PersistentStorageOperationalKeystore::CommitOpKeypairForFabric(FabricIndex fabricIndex) |
| 123 | +{ |
| 124 | + P256SerializedKeypair serializedOpKey; |
| 125 | + uint64_t p256_handle = 0; |
| 126 | + uint8_t p256_handle_expect[8] = { 0 }; |
| 127 | + |
| 128 | + VerifyOrReturnError(mPendingKeypair != nullptr, CHIP_ERROR_INVALID_FABRIC_INDEX); |
| 129 | + VerifyOrReturnError(IsValidFabricIndex(fabricIndex) && (fabricIndex == mPendingFabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); |
| 130 | + VerifyOrReturnError(mIsPendingKeypairActive == true, CHIP_ERROR_INCORRECT_STATE); |
| 131 | + |
| 132 | + // Try to store persistent key. On failure, leave everything pending as-is |
| 133 | + CHIP_ERROR err = mPendingKeypair->Serialize(serializedOpKey); |
| 134 | + VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_ERROR_INTERNAL); |
| 135 | + memcpy(&p256_handle, serializedOpKey.Bytes() + kP256_PublicKey_Length, sizeof(uint64_t)); |
| 136 | + |
| 137 | + memcpy(&p256_handle_expect[0], kTrustyMagicNumberFabricIndex, sizeof(kTrustyMagicNumberFabricIndex)); |
| 138 | + memcpy(&p256_handle_expect[7], &fabricIndex, sizeof(FabricIndex)); |
| 139 | + VerifyOrReturnError(!memcmp(&p256_handle, p256_handle_expect, sizeof(p256_handle_expect)), CHIP_ERROR_INTERNAL); |
| 140 | + |
| 141 | + int rc = GetTrustyMatter().CommitOpKeypairForFabric(p256_handle, fabricIndex); |
| 142 | + VerifyOrReturnError(rc == MATTER_ERROR_OK, CHIP_ERROR_INTERNAL); |
| 143 | + |
| 144 | + // If we got here, we succeeded and can reset the pending key: next `SignWithOpKeypair` will use the stored key. |
| 145 | + ResetPendingKey(); |
| 146 | + return CHIP_NO_ERROR; |
| 147 | +} |
| 148 | + |
| 149 | +CHIP_ERROR PersistentStorageOperationalKeystore::ExportOpKeypairForFabric(FabricIndex fabricIndex, |
| 150 | + Crypto::P256SerializedKeypair & outKeypair) |
| 151 | +{ |
| 152 | + return CHIP_ERROR_NOT_IMPLEMENTED; |
| 153 | +} |
| 154 | + |
| 155 | +CHIP_ERROR PersistentStorageOperationalKeystore::RemoveOpKeypairForFabric(FabricIndex fabricIndex) |
| 156 | +{ |
| 157 | + int rc = 0; |
| 158 | + VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); |
| 159 | + |
| 160 | + // Remove pending state if matching |
| 161 | + if ((mPendingKeypair != nullptr) && (fabricIndex == mPendingFabricIndex)) |
| 162 | + { |
| 163 | + RevertPendingKeypair(); |
| 164 | + } |
| 165 | + |
| 166 | + rc = GetTrustyMatter().RemoveOpKeypairForFabric(fabricIndex); |
| 167 | + if (rc != MATTER_ERROR_OK) |
| 168 | + return CHIP_ERROR_INTERNAL; |
| 169 | + |
| 170 | + return CHIP_NO_ERROR; |
| 171 | +} |
| 172 | + |
| 173 | +void PersistentStorageOperationalKeystore::RevertPendingKeypair() |
| 174 | +{ |
| 175 | + // Just reset the pending key, it hasn't been stored into secure storage. |
| 176 | + ResetPendingKey(); |
| 177 | +} |
| 178 | + |
| 179 | +CHIP_ERROR PersistentStorageOperationalKeystore::SignWithOpKeypair(FabricIndex fabricIndex, const ByteSpan & message, |
| 180 | + Crypto::P256ECDSASignature & outSignature) const |
| 181 | +{ |
| 182 | + int rc = 0; |
| 183 | + size_t sig_size = 0; |
| 184 | + uint8_t sig[kP256_ECDSA_Signature_Length_Raw]; |
| 185 | + |
| 186 | + VerifyOrReturnError(IsValidFabricIndex(fabricIndex), CHIP_ERROR_INVALID_FABRIC_INDEX); |
| 187 | + |
| 188 | + if (mIsPendingKeypairActive && (fabricIndex == mPendingFabricIndex)) |
| 189 | + { |
| 190 | + VerifyOrReturnError(mPendingKeypair != nullptr, CHIP_ERROR_INTERNAL); |
| 191 | + // We have an override key: sign with it! |
| 192 | + return mPendingKeypair->ECDSA_sign_msg(message.data(), message.size(), outSignature); |
| 193 | + } |
| 194 | + |
| 195 | + rc = GetTrustyMatter().SignWithStoredOpKey(fabricIndex, message.data(), message.size(), sig, sig_size); |
| 196 | + if (rc != MATTER_ERROR_OK) |
| 197 | + return CHIP_ERROR_INTERNAL; |
| 198 | + |
| 199 | + VerifyOrReturnError(sig_size == kP256_ECDSA_Signature_Length_Raw, CHIP_ERROR_INTERNAL); |
| 200 | + VerifyOrReturnError(outSignature.SetLength(sig_size) == CHIP_NO_ERROR, CHIP_ERROR_INTERNAL); |
| 201 | + memcpy(outSignature.Bytes(), sig, sig_size); |
| 202 | + return CHIP_NO_ERROR; |
| 203 | +} |
| 204 | + |
| 205 | +Crypto::P256Keypair * PersistentStorageOperationalKeystore::AllocateEphemeralKeypairForCASE() |
| 206 | +{ |
| 207 | + return Platform::New<Crypto::P256Keypair>(); |
| 208 | +} |
| 209 | + |
| 210 | +void PersistentStorageOperationalKeystore::ReleaseEphemeralKeypair(Crypto::P256Keypair * keypair) |
| 211 | +{ |
| 212 | + Platform::Delete<Crypto::P256Keypair>(keypair); |
| 213 | +} |
| 214 | + |
| 215 | +CHIP_ERROR PersistentStorageOperationalKeystore::MigrateOpKeypairForFabric(FabricIndex fabricIndex, |
| 216 | + OperationalKeystore & operationalKeystore) const |
| 217 | +{ |
| 218 | + return CHIP_ERROR_NOT_IMPLEMENTED; |
| 219 | +} |
| 220 | + |
| 221 | +} // namespace chip |
0 commit comments