IssueNOCChainCommand.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2023 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 "../common/CHIPCommand.h"
  20. #include "ToTLVCert.h"
  21. class IssueNOCChainCommand : public CHIPCommand
  22. {
  23. public:
  24. IssueNOCChainCommand(CredentialIssuerCommands * credIssuerCommands) :
  25. CHIPCommand("issue-noc-chain", credIssuerCommands,
  26. "Returns a base64-encoded NOC, ICAC, RCAC, and IPK prefixed with: 'base64:'"),
  27. mDeviceNOCChainCallback(OnDeviceNOCChainGeneration, this)
  28. {
  29. AddArgument("elements", &mNOCSRElements, "NOCSRElements encoded in hexadecimal");
  30. AddArgument("node-id", 0, UINT64_MAX, &mNodeId, "The target node id");
  31. }
  32. /////////// CHIPCommand Interface /////////
  33. CHIP_ERROR RunCommand() override
  34. {
  35. auto & commissioner = CurrentCommissioner();
  36. ReturnErrorOnFailure(commissioner.IssueNOCChain(mNOCSRElements, mNodeId, &mDeviceNOCChainCallback));
  37. return CHIP_NO_ERROR;
  38. }
  39. chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(10); }
  40. static void OnDeviceNOCChainGeneration(void * context, CHIP_ERROR status, const chip::ByteSpan & noc,
  41. const chip::ByteSpan & icac, const chip::ByteSpan & rcac,
  42. chip::Optional<chip::IdentityProtectionKeySpan> ipk,
  43. chip::Optional<chip::NodeId> adminSubject)
  44. {
  45. auto command = static_cast<IssueNOCChainCommand *>(context);
  46. auto err = status;
  47. VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err));
  48. std::string nocStr;
  49. err = ToTLVCert(noc, nocStr);
  50. VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err));
  51. ChipLogProgress(chipTool, "NOC: %s", nocStr.c_str());
  52. std::string icacStr;
  53. err = ToTLVCert(icac, icacStr);
  54. VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err));
  55. ChipLogProgress(chipTool, "ICAC: %s", icacStr.c_str());
  56. std::string rcacStr;
  57. err = ToTLVCert(rcac, rcacStr);
  58. VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err));
  59. ChipLogProgress(chipTool, "RCAC: %s", rcacStr.c_str());
  60. std::string ipkStr;
  61. if (ipk.HasValue())
  62. {
  63. err = ToBase64(ipk.Value(), ipkStr);
  64. VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err));
  65. }
  66. ChipLogProgress(chipTool, "IPK: %s", ipkStr.c_str());
  67. err = RemoteDataModelLogger::LogIssueNOCChain(nocStr.c_str(), icacStr.c_str(), rcacStr.c_str(), ipkStr.c_str());
  68. command->SetCommandExitStatus(err);
  69. }
  70. private:
  71. chip::Callback::Callback<chip::Controller::OnNOCChainGeneration> mDeviceNOCChainCallback;
  72. chip::ByteSpan mNOCSRElements;
  73. chip::NodeId mNodeId;
  74. };