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