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