CustomCSRResponseOperationalKeyStore.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. *
  3. * Copyright (c) 2022 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "CustomCSRResponseOperationalKeyStore.h"
  19. #include <credentials/FabricTable.h>
  20. #include <lib/core/TLV.h>
  21. #include <lib/support/DefaultStorageKeyAllocator.h>
  22. namespace chip {
  23. namespace {
  24. // Tags for our operational keypair storage.
  25. constexpr TLV::Tag kOpKeyVersionTag = TLV::ContextTag(0);
  26. constexpr TLV::Tag kOpKeyDataTag = TLV::ContextTag(1);
  27. constexpr size_t OpKeyTLVMaxSize()
  28. {
  29. // Version and serialized key
  30. return TLV::EstimateStructOverhead(sizeof(uint16_t), Crypto::P256SerializedKeypair::Capacity());
  31. }
  32. } // namespace
  33. CHIP_ERROR CustomCSRResponseOperationalKeyStore::NewOpKeypairForFabric(FabricIndex fabricIndex,
  34. MutableByteSpan & outCertificateSigningRequest)
  35. {
  36. if (fabricIndex == 1)
  37. {
  38. return PersistentStorageOperationalKeystore::NewOpKeypairForFabric(fabricIndex, outCertificateSigningRequest);
  39. }
  40. return ReuseOpKeypair(fabricIndex, outCertificateSigningRequest);
  41. }
  42. CHIP_ERROR CustomCSRResponseOperationalKeyStore::ReuseOpKeypair(FabricIndex fabricIndex, MutableByteSpan & outCSR)
  43. {
  44. //
  45. // DO NOT COPY THIS METHOD - IT IS FOR TESTING PURPOSES ONLY
  46. //
  47. VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
  48. // Replace previous pending keypair, if any was previously allocated
  49. ResetPendingKey();
  50. mPendingKeypair = Platform::New<Crypto::P256Keypair>();
  51. VerifyOrReturnError(mPendingKeypair != nullptr, CHIP_ERROR_NO_MEMORY);
  52. // Scope 1: Load up the keypair data from storage
  53. {
  54. // Use a SensitiveDataBuffer to get RAII secret data clearing on scope exit.
  55. Crypto::SensitiveDataBuffer<OpKeyTLVMaxSize()> buf;
  56. // Load up the operational key structure from storage
  57. uint16_t size = static_cast<uint16_t>(buf.Capacity());
  58. // In order to retrieve a keypair that has already been registered, assume the device
  59. // as already been commissioned and fabric index 1 is the registered fabric.
  60. CHIP_ERROR err =
  61. mStorage->SyncGetKeyValue(DefaultStorageKeyAllocator::FabricOpKey(1 /* fabricIndex */).KeyName(), buf.Bytes(), size);
  62. if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
  63. {
  64. err = CHIP_ERROR_INVALID_FABRIC_INDEX;
  65. }
  66. ReturnErrorOnFailure(err);
  67. buf.SetLength(static_cast<size_t>(size));
  68. // Read-out the operational key TLV entry.
  69. TLV::ContiguousBufferTLVReader reader;
  70. reader.Init(buf.Bytes(), buf.Length());
  71. ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag()));
  72. TLV::TLVType containerType;
  73. ReturnErrorOnFailure(reader.EnterContainer(containerType));
  74. ReturnErrorOnFailure(reader.Next(kOpKeyVersionTag));
  75. uint16_t opKeyVersion;
  76. ReturnErrorOnFailure(reader.Get(opKeyVersion));
  77. ReturnErrorOnFailure(reader.Next(kOpKeyDataTag));
  78. {
  79. ByteSpan keyData;
  80. Crypto::P256SerializedKeypair serializedOpKey;
  81. ReturnErrorOnFailure(reader.GetByteView(keyData));
  82. // Unfortunately, we have to copy the data into a P256SerializedKeypair.
  83. VerifyOrReturnError(keyData.size() <= serializedOpKey.Capacity(), CHIP_ERROR_BUFFER_TOO_SMALL);
  84. // Before doing anything with the key, validate format further.
  85. ReturnErrorOnFailure(reader.ExitContainer(containerType));
  86. ReturnErrorOnFailure(reader.VerifyEndOfContainer());
  87. memcpy(serializedOpKey.Bytes(), keyData.data(), keyData.size());
  88. serializedOpKey.SetLength(keyData.size());
  89. // Load-up key material
  90. // WARNING: This makes use of the raw key bits
  91. ReturnErrorOnFailure(mPendingKeypair->Deserialize(serializedOpKey));
  92. }
  93. }
  94. size_t outCSRLength = outCSR.size();
  95. CHIP_ERROR err = mPendingKeypair->NewCertificateSigningRequest(outCSR.data(), outCSRLength);
  96. if (CHIP_NO_ERROR != err)
  97. {
  98. ResetPendingKey();
  99. return err;
  100. }
  101. outCSR.reduce_size(outCSRLength);
  102. mPendingFabricIndex = fabricIndex;
  103. return CHIP_NO_ERROR;
  104. }
  105. } // namespace chip