Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021-2022 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <app/data-model/Nullable.h>
21 : #include <app/server/AppDelegate.h>
22 : #include <app/server/CommissioningModeProvider.h>
23 : #include <crypto/CHIPCryptoPAL.h>
24 : #include <lib/core/CHIPVendorIdentifiers.hpp>
25 : #include <lib/core/ClusterEnums.h>
26 : #include <lib/core/DataModelTypes.h>
27 : #include <lib/dnssd/Advertiser.h>
28 : #include <messaging/ExchangeDelegate.h>
29 : #include <platform/CHIPDeviceConfig.h>
30 : #include <protocols/secure_channel/PASESession.h>
31 : #include <system/SystemClock.h>
32 :
33 : namespace chip {
34 :
35 : enum class CommissioningWindowAdvertisement
36 : {
37 : kAllSupported,
38 : kDnssdOnly,
39 : };
40 :
41 : class Server;
42 :
43 : class CommissioningWindowManager : public Messaging::UnsolicitedMessageHandler,
44 : public SessionEstablishmentDelegate,
45 : public app::CommissioningModeProvider,
46 : public SessionDelegate
47 : {
48 : public:
49 14 : CommissioningWindowManager() : mPASESession(*this) {}
50 :
51 1 : CHIP_ERROR Init(Server * server)
52 : {
53 1 : if (server == nullptr)
54 : {
55 0 : return CHIP_ERROR_INVALID_ARGUMENT;
56 : }
57 1 : mServer = server;
58 1 : return CHIP_NO_ERROR;
59 : }
60 :
61 : System::Clock::Seconds32 MaxCommissioningTimeout() const;
62 :
63 6 : System::Clock::Seconds32 MinCommissioningTimeout() const
64 : {
65 : // Specification section 5.4.2.3. Announcement Duration says 3 minutes.
66 6 : return mMinCommissioningTimeoutOverride.ValueOr(System::Clock::Seconds32(3 * 60));
67 : }
68 :
69 1 : void SetAppDelegate(AppDelegate * delegate) { mAppDelegate = delegate; }
70 :
71 : /**
72 : * Open the pairing window using default configured parameters.
73 : */
74 : CHIP_ERROR
75 : OpenBasicCommissioningWindow(
76 : System::Clock::Seconds32 commissioningTimeout = System::Clock::Seconds32(CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS),
77 : CommissioningWindowAdvertisement advertisementMode = chip::CommissioningWindowAdvertisement::kAllSupported);
78 :
79 : /**
80 : * Open the pairing window using default configured parameters, triggered by
81 : * the Administrator Commmissioning cluster implementation.
82 : */
83 : CHIP_ERROR
84 : OpenBasicCommissioningWindowForAdministratorCommissioningCluster(System::Clock::Seconds32 commissioningTimeout,
85 : FabricIndex fabricIndex, VendorId vendorId);
86 :
87 : CHIP_ERROR OpenEnhancedCommissioningWindow(System::Clock::Seconds32 commissioningTimeout, uint16_t discriminator,
88 : Crypto::Spake2pVerifier & verifier, uint32_t iterations, chip::ByteSpan salt,
89 : FabricIndex fabricIndex, VendorId vendorId);
90 :
91 : #if CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC
92 : CHIP_ERROR OpenJointCommissioningWindow(System::Clock::Seconds32 commissioningTimeout, uint16_t discriminator,
93 : Crypto::Spake2pVerifier & verifier, uint32_t iterations, ByteSpan salt,
94 : FabricIndex fabricIndex, VendorId vendorId);
95 :
96 : // Tracks whether joint commissioning mode is ongoing
97 : bool IsJCM() const;
98 : #endif // CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC
99 :
100 : void CloseCommissioningWindow();
101 :
102 : app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum CommissioningWindowStatusForCluster() const;
103 :
104 : bool IsCommissioningWindowOpen() const;
105 :
106 7 : const app::DataModel::Nullable<VendorId> & GetOpenerVendorId() const { return mOpenerVendorId; }
107 :
108 7 : const app::DataModel::Nullable<FabricIndex> & GetOpenerFabricIndex() const { return mOpenerFabricIndex; }
109 :
110 : void OnFabricRemoved(FabricIndex removedIndex);
111 :
112 : // CommissioningModeProvider implementation.
113 : Dnssd::CommissioningMode GetCommissioningMode() const override;
114 :
115 : //// UnsolicitedMessageHandler Implementation ////
116 : CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader,
117 : Messaging::ExchangeDelegate *& newDelegate) override;
118 : void OnExchangeCreationFailed(Messaging::ExchangeDelegate * delegate) override;
119 :
120 : //////////// SessionEstablishmentDelegate Implementation ///////////////
121 : void OnSessionEstablishmentError(CHIP_ERROR error) override;
122 : void OnSessionEstablishmentStarted() override;
123 : void OnSessionEstablished(const SessionHandle & session) override;
124 :
125 : void Shutdown();
126 :
127 : void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * event);
128 :
129 : // For tests only, allow overriding the spec-defined minimum value of the
130 : // commissioning window timeout.
131 1 : void OverrideMinCommissioningTimeout(System::Clock::Seconds32 timeout) { mMinCommissioningTimeoutOverride.SetValue(timeout); }
132 :
133 : private:
134 : //////////// SessionDelegate Implementation ///////////////
135 : void OnSessionReleased() override;
136 :
137 6 : void SetBLE(bool ble) { mIsBLE = ble; }
138 :
139 : CHIP_ERROR SetTemporaryDiscriminator(uint16_t discriminator);
140 :
141 : CHIP_ERROR RestoreDiscriminator();
142 :
143 : CHIP_ERROR StartAdvertisement();
144 :
145 : CHIP_ERROR StopAdvertisement(bool aShuttingDown);
146 :
147 : // Start a timer that will call HandleCommissioningWindowTimeout, and then
148 : // start advertising and listen for PASE.
149 : CHIP_ERROR OpenCommissioningWindow(System::Clock::Seconds32 commissioningTimeout);
150 :
151 : // Start advertising and listening for PASE connections. Should only be
152 : // called when a commissioning window timeout timer is running.
153 : CHIP_ERROR AdvertiseAndListenForPASE();
154 :
155 : // Call AdvertiseAndListenForPASE, only if max attempts have not been reached.
156 : // Cleans up and calls app server delegate on failure.
157 : // err gives the current error we're attemping to recover from
158 : void HandleFailedAttempt(CHIP_ERROR err);
159 :
160 : // Helper for Shutdown and Cleanup. Does not do anything with
161 : // advertisements, because Shutdown and Cleanup want to handle those
162 : // differently.
163 : void ResetState();
164 :
165 : void Cleanup();
166 :
167 : /**
168 : * Function that gets called when our commissioning window timeout timer
169 : * fires.
170 : *
171 : * This timer is started when a commissioning window is initially opened via
172 : * OpenEnhancedCommissioningWindow or OpenBasicCommissioningWindow.
173 : *
174 : * The timer is canceled when a PASE connection is established, because it
175 : * should not affect the actual commissioning process, and after a PASE
176 : * connection is established we will not re-enter commissioning mode without
177 : * a new call to OpenEnhancedCommissioningWindow or
178 : * OpenBasicCommissioningWindow.
179 : */
180 : static void HandleCommissioningWindowTimeout(chip::System::Layer * aSystemLayer, void * aAppState);
181 :
182 : /**
183 : * Helper to immediately expire the fail-safe if it's currently armed.
184 : */
185 : void ExpireFailSafeIfArmed();
186 :
187 : /**
188 : * Helpers to ensure the right attribute reporting happens when our state is
189 : * updated.
190 : */
191 : void UpdateWindowStatus(app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum aNewStatus);
192 : void UpdateOpenerVendorId(app::DataModel::Nullable<VendorId> aNewOpenerVendorId);
193 : void UpdateOpenerFabricIndex(app::DataModel::Nullable<FabricIndex> aNewOpenerFabricIndex);
194 :
195 : AppDelegate * mAppDelegate = nullptr;
196 : Server * mServer = nullptr;
197 :
198 : app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum mWindowStatus =
199 : app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum::kWindowNotOpen;
200 :
201 : bool mIsBLE = true;
202 :
203 : #if CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC
204 : // Boolean that tracks whether we are currently in a Joint Commissioning Mode.
205 : bool mJCM = false;
206 : #endif // CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC
207 :
208 : PASESession mPairingSession;
209 :
210 : uint8_t mFailedCommissioningAttempts = 0;
211 :
212 : bool mUseECM = false;
213 : Crypto::Spake2pVerifier mECMPASEVerifier;
214 : uint16_t mECMDiscriminator = 0;
215 : // mListeningForPASE is true only when we are listening for
216 : // PBKDFParamRequest messages or when we're in the middle of a PASE
217 : // handshake.
218 : bool mListeningForPASE = false;
219 : // Boolean that tracks whether we have a live commissioning timeout timer.
220 : bool mCommissioningTimeoutTimerArmed = false;
221 : uint32_t mECMIterations = 0;
222 : uint32_t mECMSaltLength = 0;
223 : uint8_t mECMSalt[Crypto::kSpake2p_Max_PBKDF_Salt_Length];
224 :
225 : // For tests only, so that we can test the commissioning window timeout
226 : // without having to wait 3 minutes.
227 : Optional<System::Clock::Seconds32> mMinCommissioningTimeoutOverride;
228 :
229 : // The PASE session we are using, so we can handle CloseSession properly.
230 : SessionHolderWithDelegate mPASESession;
231 :
232 : // Information about who opened the commissioning window. These will only
233 : // be non-null if the window was opened via the operational credentials
234 : // cluster and the fabric index may be null even then if the fabric has been
235 : // removed.
236 : app::DataModel::Nullable<VendorId> mOpenerVendorId;
237 : app::DataModel::Nullable<FabricIndex> mOpenerFabricIndex;
238 : };
239 :
240 : } // namespace chip
|