Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2021 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 : #pragma once 19 : #include <controller/CommissioneeDeviceProxy.h> 20 : #include <controller/CommissioningDelegate.h> 21 : #include <credentials/DeviceAttestationConstructor.h> 22 : #include <protocols/secure_channel/RendezvousParameters.h> 23 : 24 : namespace chip { 25 : namespace Controller { 26 : 27 : class DeviceCommissioner; 28 : 29 : class AutoCommissioner : public CommissioningDelegate 30 : { 31 : public: 32 : AutoCommissioner(); 33 : ~AutoCommissioner() override; 34 : CHIP_ERROR SetCommissioningParameters(const CommissioningParameters & params) override; 35 : const CommissioningParameters & GetCommissioningParameters() const override; 36 : void SetOperationalCredentialsDelegate(OperationalCredentialsDelegate * operationalCredentialsDelegate) override; 37 : 38 : CHIP_ERROR StartCommissioning(DeviceCommissioner * commissioner, CommissioneeDeviceProxy * proxy) override; 39 : void StopCommissioning() { mStopCommissioning = true; }; 40 : 41 : CHIP_ERROR CommissioningStepFinished(CHIP_ERROR err, CommissioningDelegate::CommissioningReport report) override; 42 : 43 : ByteSpan GetAttestationElements() const { return ByteSpan(mAttestationElements, mAttestationElementsLen); } 44 : ByteSpan GetAttestationSignature() const { return ByteSpan(mAttestationSignature, mAttestationSignatureLen); } 45 : ByteSpan GetAttestationNonce() const { return ByteSpan(mAttestationNonce); } 46 : 47 : protected: 48 : CommissioningStage GetNextCommissioningStage(CommissioningStage currentStage, CHIP_ERROR & lastErr); 49 : DeviceCommissioner * GetCommissioner() { return mCommissioner; } 50 : CHIP_ERROR PerformStep(CommissioningStage nextStage); 51 : CommissioneeDeviceProxy * GetCommissioneeDeviceProxy() { return mCommissioneeDeviceProxy; } 52 : /** 53 : * The device argument to GetCommandTimeout is the device whose session will 54 : * be used for sending the relevant command. 55 : */ 56 : Optional<System::Clock::Timeout> GetCommandTimeout(DeviceProxy * device, CommissioningStage stage) const; 57 : CommissioningParameters mParams = CommissioningParameters(); 58 : 59 : private: 60 : DeviceProxy * GetDeviceProxyForStep(CommissioningStage nextStage); 61 : 62 : // Adjust the failsafe timer if CommissioningDelegate GetCASEFailsafeTimerSeconds is set 63 : void SetCASEFailsafeTimerIfNeeded(); 64 : void ReleaseDAC(); 65 : void ReleasePAI(); 66 : 67 : CHIP_ERROR SetDAC(const ByteSpan & dac); 68 : CHIP_ERROR SetPAI(const ByteSpan & pai); 69 : 70 : ByteSpan GetDAC() const { return ByteSpan(mDAC, mDACLen); } 71 : ByteSpan GetPAI() const { return ByteSpan(mPAI, mPAILen); } 72 : 73 : CHIP_ERROR NOCChainGenerated(ByteSpan noc, ByteSpan icac, ByteSpan rcac, IdentityProtectionKeySpan ipk, NodeId adminSubject); 74 : EndpointId GetEndpoint(const CommissioningStage & stage) const; 75 : CommissioningStage GetNextCommissioningStageInternal(CommissioningStage currentStage, CHIP_ERROR & lastErr); 76 : 77 : CHIP_ERROR VerifyICDRegistrationInfo(const CommissioningParameters & params); 78 : 79 : // Helper function to determine whether next stage should be kWiFiNetworkSetup, 80 : // kThreadNetworkSetup or kCleanup, depending whether network information has 81 : // been provided that matches the thread/wifi endpoint of the target. 82 : CommissioningStage GetNextCommissioningStageNetworkSetup(CommissioningStage currentStage, CHIP_ERROR & lastErr); 83 : 84 : // Helper function to determine if a scan attempt should be made given the 85 : // scan attempt commissioning params and the corresponding network endpoint of 86 : // the target. 87 0 : bool IsScanNeeded() 88 : { 89 0 : return ((mParams.GetAttemptWiFiNetworkScan().ValueOr(false) && 90 0 : mDeviceCommissioningInfo.network.wifi.endpoint != kInvalidEndpointId) || 91 0 : (mParams.GetAttemptThreadNetworkScan().ValueOr(false) && 92 0 : mDeviceCommissioningInfo.network.thread.endpoint != kInvalidEndpointId)); 93 : }; 94 : 95 : bool mStopCommissioning = false; 96 : 97 : DeviceCommissioner * mCommissioner = nullptr; 98 : CommissioneeDeviceProxy * mCommissioneeDeviceProxy = nullptr; 99 : OperationalCredentialsDelegate * mOperationalCredentialsDelegate = nullptr; 100 : OperationalDeviceProxy mOperationalDeviceProxy; 101 : // Memory space for the commisisoning parameters that come in as ByteSpans - the caller is not guaranteed to retain this memory 102 : uint8_t mSsid[CommissioningParameters::kMaxSsidLen]; 103 : uint8_t mCredentials[CommissioningParameters::kMaxCredentialsLen]; 104 : uint8_t mThreadOperationalDataset[CommissioningParameters::kMaxThreadDatasetLen]; 105 : char mCountryCode[CommissioningParameters::kMaxCountryCodeLen]; 106 : 107 : // Time zone is statically allocated because it is max 2 and not trivially destructible 108 : static constexpr size_t kMaxSupportedTimeZones = 2; 109 : app::Clusters::TimeSynchronization::Structs::TimeZoneStruct::Type mTimeZoneBuf[kMaxSupportedTimeZones]; 110 : static constexpr size_t kMaxTimeZoneNameLen = 64; 111 : char mTimeZoneNames[kMaxTimeZoneNameLen][kMaxSupportedTimeZones]; 112 : 113 : // DSTOffsetStructs are similarly not trivially destructible. They don't have a defined size, but we're 114 : // going to do static allocation of the buffers anyway until we replace chip::Optional with std::optional. 115 : static constexpr size_t kMaxSupportedDstStructs = 10; 116 : app::Clusters::TimeSynchronization::Structs::DSTOffsetStruct::Type mDstOffsetsBuf[kMaxSupportedDstStructs]; 117 : 118 : static constexpr size_t kMaxDefaultNtpSize = 128; 119 : char mDefaultNtp[kMaxDefaultNtpSize]; 120 : 121 : bool mNeedsNetworkSetup = false; 122 : ReadCommissioningInfo mDeviceCommissioningInfo; 123 : bool mNeedsDST = false; 124 : 125 : bool mNeedIcdRegistration = false; 126 : 127 : // TODO: Why were the nonces statically allocated, but the certs dynamically allocated? 128 : uint8_t * mDAC = nullptr; 129 : uint16_t mDACLen = 0; 130 : uint8_t * mPAI = nullptr; 131 : uint16_t mPAILen = 0; 132 : uint8_t mAttestationNonce[kAttestationNonceLength]; 133 : uint8_t mCSRNonce[kCSRNonceLength]; 134 : uint8_t mNOCertBuffer[Credentials::kMaxCHIPCertLength]; 135 : uint8_t mICACertBuffer[Credentials::kMaxCHIPCertLength]; 136 : 137 : uint16_t mAttestationElementsLen = 0; 138 : uint8_t mAttestationElements[Credentials::kMaxRspLen]; 139 : uint16_t mAttestationSignatureLen = 0; 140 : uint8_t mAttestationSignature[Crypto::kMax_ECDSA_Signature_Length]; 141 : 142 : uint8_t mICDSymmetricKey[Crypto::kAES_CCM128_Key_Length]; 143 : }; 144 : } // namespace Controller 145 : } // namespace chip