ExampleCredentialIssuerCommands.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2021-2022 Project CHIP Authors
  3. * All rights reserved.
  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. #pragma once
  19. #include <commands/common/CredentialIssuerCommands.h>
  20. #include <controller/CHIPDeviceControllerFactory.h>
  21. #include <controller/ExampleOperationalCredentialsIssuer.h>
  22. #include <credentials/DeviceAttestationCredsProvider.h>
  23. #include <credentials/attestation_verifier/DefaultDeviceAttestationVerifier.h>
  24. #include <credentials/attestation_verifier/DeviceAttestationVerifier.h>
  25. #include <credentials/examples/DeviceAttestationCredsExample.h>
  26. class ExampleCredentialIssuerCommands : public CredentialIssuerCommands
  27. {
  28. public:
  29. CHIP_ERROR InitializeCredentialsIssuer(chip::PersistentStorageDelegate & storage) override
  30. {
  31. return mOpCredsIssuer.Initialize(storage);
  32. }
  33. CHIP_ERROR SetupDeviceAttestation(chip::Controller::SetupParams & setupParams,
  34. const chip::Credentials::AttestationTrustStore * trustStore) override
  35. {
  36. chip::Credentials::SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());
  37. mDacVerifier = chip::Credentials::GetDefaultDACVerifier(trustStore);
  38. setupParams.deviceAttestationVerifier = mDacVerifier;
  39. mDacVerifier->EnableCdTestKeySupport(mAllowTestCdSigningKey);
  40. return CHIP_NO_ERROR;
  41. }
  42. chip::Controller::OperationalCredentialsDelegate * GetCredentialIssuer() override { return &mOpCredsIssuer; }
  43. void SetCredentialIssuerCATValues(chip::CATValues cats) override { mOpCredsIssuer.SetCATValuesForNextNOCRequest(cats); }
  44. CHIP_ERROR GenerateControllerNOCChain(chip::NodeId nodeId, chip::FabricId fabricId, const chip::CATValues & cats,
  45. chip::Crypto::P256Keypair & keypair, chip::MutableByteSpan & rcac,
  46. chip::MutableByteSpan & icac, chip::MutableByteSpan & noc) override
  47. {
  48. return mOpCredsIssuer.GenerateNOCChainAfterValidation(nodeId, fabricId, cats, keypair.Pubkey(), rcac, icac, noc);
  49. }
  50. CHIP_ERROR AddAdditionalCDVerifyingCerts(const std::vector<std::vector<uint8_t>> & additionalCdCerts) override
  51. {
  52. VerifyOrReturnError(mDacVerifier != nullptr, CHIP_ERROR_INCORRECT_STATE);
  53. for (const auto & cert : additionalCdCerts)
  54. {
  55. auto cdTrustStore = mDacVerifier->GetCertificationDeclarationTrustStore();
  56. VerifyOrReturnError(cdTrustStore != nullptr, CHIP_ERROR_INCORRECT_STATE);
  57. ReturnErrorOnFailure(cdTrustStore->AddTrustedKey(chip::ByteSpan(cert.data(), cert.size())));
  58. }
  59. return CHIP_NO_ERROR;
  60. }
  61. void SetCredentialIssuerOption(CredentialIssuerOptions option, bool isEnabled) override
  62. {
  63. switch (option)
  64. {
  65. case CredentialIssuerOptions::kMaximizeCertificateSizes:
  66. mUsesMaxSizedCerts = isEnabled;
  67. mOpCredsIssuer.SetMaximallyLargeCertsUsed(mUsesMaxSizedCerts);
  68. break;
  69. case CredentialIssuerOptions::kAllowTestCdSigningKey:
  70. mAllowTestCdSigningKey = isEnabled;
  71. if (mDacVerifier != nullptr)
  72. {
  73. mDacVerifier->EnableCdTestKeySupport(isEnabled);
  74. }
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. bool GetCredentialIssuerOption(CredentialIssuerOptions option) override
  81. {
  82. switch (option)
  83. {
  84. case CredentialIssuerOptions::kMaximizeCertificateSizes:
  85. return mUsesMaxSizedCerts;
  86. case CredentialIssuerOptions::kAllowTestCdSigningKey:
  87. return mAllowTestCdSigningKey;
  88. default:
  89. return false;
  90. }
  91. }
  92. protected:
  93. bool mUsesMaxSizedCerts = false;
  94. // Starts true for legacy purposes
  95. bool mAllowTestCdSigningKey = true;
  96. private:
  97. chip::Controller::ExampleOperationalCredentialsIssuer mOpCredsIssuer;
  98. chip::Credentials::DeviceAttestationVerifier * mDacVerifier;
  99. };