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, kRequestWiFiCredentials, kRequestThreadCredentials, or
92 : // kCleanup, depending on whether network information has been provided that matches
93 : // the thread/wifi endpoint of the target.
94 : CommissioningStage GetNextCommissioningStageNetworkSetup(CommissioningStage currentStage, CHIP_ERROR & lastErr);
95 :
96 : // Helper function to determine if a scan attempt should be made given the
97 : // scan attempt commissioning params and the corresponding network endpoint of
98 : // the target.
99 4 : bool IsScanNeeded()
100 : {
101 4 : return ((mParams.GetAttemptWiFiNetworkScan().ValueOr(false) &&
102 6 : mDeviceCommissioningInfo.network.wifi.endpoint != kInvalidEndpointId) ||
103 2 : (mParams.GetAttemptThreadNetworkScan().ValueOr(false) &&
104 5 : mDeviceCommissioningInfo.network.thread.endpoint != kInvalidEndpointId));
105 : };
106 :
107 : // Helper function to determine whether secondary network interface is supported.
108 : // Only true if the target has endpoints for both Wi-Fi and Thread, we can
109 : // still talk to it after the first attempt to put it on the network, and
110 : // either we have credentials for both network types or we have credentials
111 : // for either network type (in which case we will prompt for the credentials).
112 6 : bool IsSecondaryNetworkSupported() const
113 : {
114 6 : return ((mParams.GetSupportsConcurrentConnection().ValueOr(false) &&
115 5 : mDeviceCommissioningInfo.network.wifi.endpoint != kInvalidEndpointId &&
116 14 : mDeviceCommissioningInfo.network.thread.endpoint != kInvalidEndpointId) &&
117 9 : mParams.GetWiFiCredentials().HasValue() == mParams.GetThreadOperationalDataset().HasValue());
118 : }
119 :
120 0 : bool IsSomeNetworkSupported() const
121 : {
122 0 : return mDeviceCommissioningInfo.network.wifi.endpoint != kInvalidEndpointId ||
123 0 : mDeviceCommissioningInfo.network.thread.endpoint != kInvalidEndpointId;
124 : }
125 :
126 : // TryingPrimaryNetwork() and TryingSecondaryNetwork() will only be true if
127 : // we decided that a secondary network is supported by our combination of
128 : // commissioner and commissionee.
129 : enum class NetworkAttemptType : uint8_t
130 : {
131 : // We will only try one network type.
132 : kSingle,
133 : // We will try two network types and we are trying the primary right now.
134 : kPrimary,
135 : // We tried the primary and if failed and we are trying the secondary
136 : // now.
137 : kSecondary,
138 : };
139 :
140 0 : void TryPrimaryNetwork() { mTryingNetworkType = NetworkAttemptType::kPrimary; }
141 0 : bool TryingPrimaryNetwork() const { return mTryingNetworkType == NetworkAttemptType::kPrimary; }
142 1 : void TrySecondaryNetwork() { mTryingNetworkType = NetworkAttemptType::kSecondary; }
143 2 : bool TryingSecondaryNetwork() const { return mTryingNetworkType == NetworkAttemptType::kSecondary; }
144 1 : void ResetNetworkAttemptType() { mTryingNetworkType = NetworkAttemptType::kSingle; }
145 :
146 : NetworkAttemptType mTryingNetworkType = NetworkAttemptType::kSingle;
147 :
148 : bool mStopCommissioning = false;
149 :
150 : DeviceCommissioner * mCommissioner = nullptr;
151 : CommissioneeDeviceProxy * mCommissioneeDeviceProxy = nullptr;
152 : OperationalCredentialsDelegate * mOperationalCredentialsDelegate = nullptr;
153 : OperationalDeviceProxy mOperationalDeviceProxy;
154 :
155 : // BEGIN memory space for commissioning parameters that are Spans or similar pointers:
156 : // The caller is not guaranteed to retain the memory these parameters point to.
157 : uint8_t mSsid[CommissioningParameters::kMaxSsidLen];
158 : uint8_t mCredentials[CommissioningParameters::kMaxCredentialsLen];
159 : uint8_t mThreadOperationalDataset[CommissioningParameters::kMaxThreadDatasetLen];
160 : char mCountryCode[CommissioningParameters::kMaxCountryCodeLen];
161 :
162 : // Time zone is statically allocated because it is max 2 and not trivially destructible
163 : static constexpr size_t kMaxSupportedTimeZones = 2;
164 : app::Clusters::TimeSynchronization::Structs::TimeZoneStruct::Type mTimeZoneBuf[kMaxSupportedTimeZones];
165 : static constexpr size_t kMaxTimeZoneNameLen = 64;
166 : char mTimeZoneNames[kMaxTimeZoneNameLen][kMaxSupportedTimeZones];
167 :
168 : // DSTOffsetStructs are similarly not trivially destructible. They don't have a defined size, but we're
169 : // going to do static allocation of the buffers anyway until we replace chip::Optional with std::optional.
170 : static constexpr size_t kMaxSupportedDstStructs = 10;
171 : app::Clusters::TimeSynchronization::Structs::DSTOffsetStruct::Type mDstOffsetsBuf[kMaxSupportedDstStructs];
172 :
173 : static constexpr size_t kMaxDefaultNtpSize = 128;
174 :
175 : char mDefaultNtp[kMaxDefaultNtpSize];
176 :
177 : uint8_t mICDSymmetricKey[Crypto::kAES_CCM128_Key_Length];
178 : Platform::ScopedMemoryBufferWithSize<app::AttributePathParams> mExtraReadPaths;
179 :
180 : // END memory space for commisisoning parameters
181 :
182 : bool mNeedsNetworkSetup = false;
183 : ReadCommissioningInfo mDeviceCommissioningInfo;
184 : bool mNeedsDST = false;
185 :
186 : bool mNeedIcdRegistration = false;
187 : // TODO: Why were the nonces statically allocated, but the certs dynamically allocated?
188 : Platform::ScopedMemoryBufferWithSize<uint8_t> mDAC;
189 : Platform::ScopedMemoryBufferWithSize<uint8_t> mPAI;
190 :
191 : uint8_t mAttestationNonce[kAttestationNonceLength];
192 : uint8_t mCSRNonce[kCSRNonceLength];
193 : uint8_t mNOCertBuffer[Credentials::kMaxCHIPCertLength];
194 : uint8_t mICACertBuffer[Credentials::kMaxCHIPCertLength];
195 :
196 : uint16_t mAttestationElementsLen = 0;
197 : uint8_t mAttestationElements[Credentials::kMaxRspLen];
198 : uint16_t mAttestationSignatureLen = 0;
199 : uint8_t mAttestationSignature[Crypto::kMax_ECDSA_Signature_Length];
200 : };
201 : } // namespace Controller
202 : } // namespace chip
|