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