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