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 75 : 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 10 : System::Clock::Seconds32 MinCommissioningTimeout() const
64 : {
65 : // Specification section 5.4.2.3. Announcement Duration says 3 minutes.
66 10 : 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 9 : Optional<SessionHandle> GetPASESession() const { return mPASESession.Get(); }
134 :
135 : /**
136 : * Expire the fail-safe if there is an active PASE session, since this indicates that the fail-safe is for the commissioning
137 : * happening over the PASE session, and not for some unrelated non-commissioning activity.
138 : */
139 : void ExpireFailSafeIfHeldByOpenPASESession();
140 :
141 : private:
142 : //////////// SessionDelegate Implementation ///////////////
143 : void OnSessionReleased() override;
144 :
145 8 : void SetBLE(bool ble) { mIsBLE = ble; }
146 :
147 : CHIP_ERROR SetTemporaryDiscriminator(uint16_t discriminator);
148 :
149 : CHIP_ERROR RestoreDiscriminator();
150 :
151 : CHIP_ERROR StartAdvertisement();
152 :
153 : CHIP_ERROR StopAdvertisement(bool aShuttingDown);
154 :
155 : // Start a timer that will call HandleCommissioningWindowTimeout, and then
156 : // start advertising and listen for PASE.
157 : CHIP_ERROR OpenCommissioningWindow(System::Clock::Seconds32 commissioningTimeout);
158 :
159 : // Start advertising and listening for PASE connections. Should only be
160 : // called when a commissioning window timeout timer is running.
161 : CHIP_ERROR AdvertiseAndListenForPASE();
162 :
163 : // Call AdvertiseAndListenForPASE, only if max attempts have not been reached.
164 : // Cleans up and calls app server delegate on failure.
165 : // err gives the current error we're attemping to recover from
166 : void HandleFailedAttempt(CHIP_ERROR err);
167 :
168 : // Helper for Shutdown and Cleanup. Does not do anything with
169 : // advertisements, because Shutdown and Cleanup want to handle those
170 : // differently.
171 : void ResetState();
172 :
173 : void Cleanup();
174 :
175 : /**
176 : * Function that gets called when our commissioning window timeout timer
177 : * fires.
178 : *
179 : * This timer is started when a commissioning window is initially opened via
180 : * OpenEnhancedCommissioningWindow or OpenBasicCommissioningWindow.
181 : *
182 : * The timer is canceled when a PASE connection is established, because it
183 : * should not affect the actual commissioning process, and after a PASE
184 : * connection is established we will not re-enter commissioning mode without
185 : * a new call to OpenEnhancedCommissioningWindow or
186 : * OpenBasicCommissioningWindow.
187 : */
188 : static void HandleCommissioningWindowTimeout(chip::System::Layer * aSystemLayer, void * aAppState);
189 :
190 : /**
191 : * Helper to immediately expire the fail-safe if it's currently armed.
192 : */
193 : void ExpireFailSafeIfArmed();
194 :
195 : /**
196 : * Helpers to ensure the right attribute reporting happens when our state is
197 : * updated.
198 : */
199 : void UpdateWindowStatus(app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum aNewStatus);
200 : void UpdateOpenerVendorId(app::DataModel::Nullable<VendorId> aNewOpenerVendorId);
201 : void UpdateOpenerFabricIndex(app::DataModel::Nullable<FabricIndex> aNewOpenerFabricIndex);
202 :
203 : AppDelegate * mAppDelegate = nullptr;
204 : Server * mServer = nullptr;
205 :
206 : app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum mWindowStatus =
207 : app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum::kWindowNotOpen;
208 :
209 : bool mIsBLE = true;
210 :
211 : #if CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC
212 : // Boolean that tracks whether we are currently in a Joint Commissioning Mode.
213 : bool mJCM = false;
214 : #endif // CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC
215 :
216 : PASESession mPairingSession;
217 :
218 : uint8_t mFailedCommissioningAttempts = 0;
219 :
220 : bool mUseECM = false;
221 : Crypto::Spake2pVerifier mECMPASEVerifier;
222 : uint16_t mECMDiscriminator = 0;
223 : // mListeningForPASE is true only when we are listening for
224 : // PBKDFParamRequest messages or when we're in the middle of a PASE
225 : // handshake.
226 : bool mListeningForPASE = false;
227 : // Boolean that tracks whether we have a live commissioning timeout timer.
228 : bool mCommissioningTimeoutTimerArmed = false;
229 : uint32_t mECMIterations = 0;
230 : uint32_t mECMSaltLength = 0;
231 : uint8_t mECMSalt[Crypto::kSpake2p_Max_PBKDF_Salt_Length];
232 :
233 : // For tests only, so that we can test the commissioning window timeout
234 : // without having to wait 3 minutes.
235 : Optional<System::Clock::Seconds32> mMinCommissioningTimeoutOverride;
236 :
237 : // The PASE session we are using, so we can handle CloseSession properly.
238 : SessionHolderWithDelegate mPASESession;
239 :
240 : // Information about who opened the commissioning window. These will only
241 : // be non-null if the window was opened via the operational credentials
242 : // cluster and the fabric index may be null even then if the fabric has been
243 : // removed.
244 : app::DataModel::Nullable<VendorId> mOpenerVendorId;
245 : app::DataModel::Nullable<FabricIndex> mOpenerFabricIndex;
246 : };
247 :
248 : } // namespace chip
|