Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Evgeny Margolis | c69b6f1 | 2022-02-07 09:34:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2020-2022 Project CHIP Authors |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 4 | * Copyright (c) 2013-2017 Nest Labs, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * @file |
| 22 | * Declaration of CHIP Device Controller, a common class |
| 23 | * that implements connecting and messaging and will later |
| 24 | * be expanded to support discovery, pairing and |
| 25 | * provisioning of CHIP devices. |
| 26 | * |
| 27 | */ |
| 28 | |
Andrei Litvin | 1873e8c | 2020-10-12 10:52:26 -0400 | [diff] [blame] | 29 | #pragma once |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 30 | |
Jiacheng Guo | aba3c81 | 2021-12-09 02:18:03 +0800 | [diff] [blame] | 31 | #include <app/CASEClientPool.h> |
Pankaj Garg | eb84a9d | 2021-11-16 13:59:57 -0800 | [diff] [blame] | 32 | #include <app/CASESessionManager.h> |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 33 | #include <app/ClusterStateCache.h> |
Terence Hampson | b6859d7 | 2022-08-05 13:48:11 -0400 | [diff] [blame] | 34 | #include <app/OperationalSessionSetup.h> |
| 35 | #include <app/OperationalSessionSetupPool.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 36 | #include <controller/AbstractDnssdDiscoveryController.h> |
C Freeman | 798d7d2 | 2021-12-10 12:06:36 -0500 | [diff] [blame] | 37 | #include <controller/AutoCommissioner.h> |
C Freeman | 8ee96e4 | 2022-01-21 11:43:45 -0500 | [diff] [blame] | 38 | #include <controller/CHIPCluster.h> |
Sagar Dhawan | ae69dd7 | 2021-09-29 15:13:09 -0700 | [diff] [blame] | 39 | #include <controller/CHIPDeviceControllerSystemState.h> |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 40 | #include <controller/CommissioneeDeviceProxy.h> |
C Freeman | 798d7d2 | 2021-12-10 12:06:36 -0500 | [diff] [blame] | 41 | #include <controller/CommissioningDelegate.h> |
Boris Zbarsky | d170a25 | 2022-03-31 20:51:58 -0400 | [diff] [blame] | 42 | #include <controller/DevicePairingDelegate.h> |
Pankaj Garg | a490773 | 2021-05-07 09:08:45 -0700 | [diff] [blame] | 43 | #include <controller/OperationalCredentialsDelegate.h> |
Vivien Nicolas | 02d8900 | 2021-10-04 12:30:52 +0200 | [diff] [blame] | 44 | #include <controller/SetUpCodePairer.h> |
Pankaj Garg | 4052bdc | 2021-11-17 11:38:59 -0800 | [diff] [blame] | 45 | #include <credentials/FabricTable.h> |
Anush Nadathur | b06bd8c | 2022-04-06 17:29:16 -0700 | [diff] [blame] | 46 | #include <credentials/attestation_verifier/DeviceAttestationDelegate.h> |
Tennessee Carmel-Veilleux | 22fb6c3 | 2022-02-22 16:49:16 -0500 | [diff] [blame] | 47 | #include <credentials/attestation_verifier/DeviceAttestationVerifier.h> |
Kevin Coppock | 2f85372 | 2022-06-16 09:53:08 -0500 | [diff] [blame] | 48 | #include <inet/InetInterface.h> |
Damian Królik | 8d41ec1 | 2021-09-28 16:22:13 +0200 | [diff] [blame] | 49 | #include <lib/core/CHIPConfig.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 50 | #include <lib/core/CHIPCore.h> |
| 51 | #include <lib/core/CHIPPersistentStorageDelegate.h> |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 52 | #include <lib/core/DataModelTypes.h> |
Martin Turon | 82bfcd5 | 2023-01-09 13:30:38 -0800 | [diff] [blame] | 53 | #include <lib/core/TLV.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 54 | #include <lib/support/DLLUtil.h> |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 55 | #include <lib/support/Pool.h> |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 56 | #include <lib/support/SafeInt.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 57 | #include <lib/support/SerializableIntegerSet.h> |
Boris Zbarsky | f72e0b7 | 2021-05-14 14:47:40 -0400 | [diff] [blame] | 58 | #include <lib/support/Span.h> |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 59 | #include <lib/support/ThreadOperationalDataset.h> |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 60 | #include <messaging/ExchangeMgr.h> |
Zang MingJie | c64d9d1 | 2021-05-13 00:09:08 +0800 | [diff] [blame] | 61 | #include <protocols/secure_channel/MessageCounterManager.h> |
Pankaj Garg | 0f443b8 | 2021-05-04 14:21:10 -0700 | [diff] [blame] | 62 | #include <protocols/secure_channel/RendezvousParameters.h> |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 63 | #include <protocols/user_directed_commissioning/UserDirectedCommissioning.h> |
Boris Zbarsky | 8afc1f5 | 2023-03-09 23:58:40 -0500 | [diff] [blame] | 64 | #include <system/SystemClock.h> |
Zang MingJie | eca9bff | 2021-09-23 03:19:51 +0800 | [diff] [blame] | 65 | #include <transport/SessionManager.h> |
Song Guo | fba2fdc | 2020-11-21 04:08:24 +0800 | [diff] [blame] | 66 | #include <transport/TransportMgr.h> |
Andrei Litvin | 992775c | 2020-10-01 20:20:52 -0400 | [diff] [blame] | 67 | #include <transport/raw/UDP.h> |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 68 | |
Vivien Nicolas | 65c3982 | 2021-04-30 00:39:25 +0200 | [diff] [blame] | 69 | #if CONFIG_DEVICE_LAYER |
| 70 | #include <platform/CHIPDeviceLayer.h> |
| 71 | #endif |
| 72 | |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 73 | #if CONFIG_NETWORK_LAYER_BLE |
| 74 | #include <ble/BleLayer.h> |
| 75 | #endif |
Vivien Nicolas | 02d8900 | 2021-10-04 12:30:52 +0200 | [diff] [blame] | 76 | #include <controller/DeviceDiscoveryDelegate.h> |
Vivien Nicolas | e047d27 | 2021-04-13 15:39:41 +0200 | [diff] [blame] | 77 | |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 78 | namespace chip { |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 79 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 80 | namespace Controller { |
Pankaj Garg | 09e2582 | 2020-10-22 09:14:12 -0700 | [diff] [blame] | 81 | |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 82 | using namespace chip::Protocols::UserDirectedCommissioning; |
| 83 | |
Damian Królik | 8d41ec1 | 2021-09-28 16:22:13 +0200 | [diff] [blame] | 84 | constexpr uint16_t kNumMaxActiveDevices = CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES; |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 85 | |
C Freeman | e86bb35 | 2021-05-26 19:19:40 -0400 | [diff] [blame] | 86 | // Raw functions for cluster callbacks |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 87 | void OnBasicFailure(void * context, CHIP_ERROR err); |
C Freeman | e86bb35 | 2021-05-26 19:19:40 -0400 | [diff] [blame] | 88 | |
Song Guo | 3cb69ff | 2021-04-01 20:58:25 +0800 | [diff] [blame] | 89 | struct ControllerInitParams |
| 90 | { |
Andrei Litvin | b792463 | 2022-03-03 15:29:54 -0500 | [diff] [blame] | 91 | DeviceControllerSystemState * systemState = nullptr; |
Andrei Litvin | b792463 | 2022-03-03 15:29:54 -0500 | [diff] [blame] | 92 | DeviceDiscoveryDelegate * deviceDiscoveryDelegate = nullptr; |
Pankaj Garg | aeebd19 | 2021-05-14 13:17:50 -0700 | [diff] [blame] | 93 | OperationalCredentialsDelegate * operationalCredentialsDelegate = nullptr; |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 94 | |
| 95 | /* The following keypair must correspond to the public key used for generating |
| 96 | controllerNOC. It's used by controller to establish CASE sessions with devices */ |
Tennessee Carmel-Veilleux | 8d75845 | 2022-01-26 23:27:11 -0500 | [diff] [blame] | 97 | Crypto::P256Keypair * operationalKeypair = nullptr; |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 98 | |
Tennessee Carmel-Veilleux | 51cbb53 | 2022-05-16 20:37:21 -0400 | [diff] [blame] | 99 | /** |
| 100 | * Controls whether or not the operationalKeypair should be owned by the caller. |
| 101 | * By default, this is false, but if the keypair cannot be serialized, then |
Boris Zbarsky | 78728ab | 2022-05-17 06:31:15 -0400 | [diff] [blame] | 102 | * setting this to true will allow the caller to manage this keypair's lifecycle. |
Tennessee Carmel-Veilleux | 51cbb53 | 2022-05-16 20:37:21 -0400 | [diff] [blame] | 103 | */ |
| 104 | bool hasExternallyOwnedOperationalKeypair = false; |
| 105 | |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 106 | /* The following certificates must be in x509 DER format */ |
| 107 | ByteSpan controllerNOC; |
| 108 | ByteSpan controllerICAC; |
| 109 | ByteSpan controllerRCAC; |
| 110 | |
Jerry Johns | 0eed15d | 2022-08-01 17:56:38 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Controls whether we permit multiple DeviceController instances to exist |
| 113 | * on the same logical fabric (identified by the tuple of the fabric's |
| 114 | * root public key + fabric id). |
| 115 | * |
| 116 | * Each controller instance will be associated with its own FabricIndex. |
| 117 | * This pivots the FabricTable to tracking identities instead of fabrics, |
| 118 | * represented by FabricInfo instances that can have colliding logical fabrics. |
| 119 | * |
| 120 | */ |
| 121 | bool permitMultiControllerFabrics = false; |
| 122 | |
Jerry Johns | 75372d8 | 2022-03-01 06:32:40 -0800 | [diff] [blame] | 123 | // |
| 124 | // Controls enabling server cluster interactions on a controller. This in turn |
| 125 | // causes the following to get enabled: |
| 126 | // |
| 127 | // - Advertisement of active controller operational identities. |
| 128 | // |
| 129 | bool enableServerInteractions = false; |
| 130 | |
Boris Zbarsky | 01bfdd0 | 2023-04-06 20:40:13 -0400 | [diff] [blame] | 131 | /** |
| 132 | * Controls whether shutdown of the controller removes the corresponding |
| 133 | * entry from the fabric table. For now the removal is just from the |
| 134 | * in-memory table, not from storage, which means that after controller |
| 135 | * shutdown the storage and the in-memory fabric table will be out of sync. |
| 136 | * This is acceptable for implementations that don't actually store any of |
| 137 | * the fabric table information, but if someone wants a true removal at some |
| 138 | * point another option will need to be added here. |
| 139 | */ |
| 140 | bool removeFromFabricTableOnShutdown = true; |
| 141 | |
Vivien Nicolas | 401c393 | 2022-05-11 13:04:29 +0200 | [diff] [blame] | 142 | chip::VendorId controllerVendorId; |
Song Guo | 3cb69ff | 2021-04-01 20:58:25 +0800 | [diff] [blame] | 143 | }; |
| 144 | |
Andrei Litvin | d4da2fa | 2021-04-28 11:25:25 -0400 | [diff] [blame] | 145 | struct CommissionerInitParams : public ControllerInitParams |
| 146 | { |
C Freeman | 8ffe422 | 2022-02-02 14:21:16 -0500 | [diff] [blame] | 147 | DevicePairingDelegate * pairingDelegate = nullptr; |
| 148 | CommissioningDelegate * defaultCommissioner = nullptr; |
Tennessee Carmel-Veilleux | a777a80 | 2022-05-17 22:56:37 -0400 | [diff] [blame] | 149 | // Device attestation verifier instance for the commissioning. |
| 150 | // If null, the globally set attestation verifier (e.g. from GetDeviceAttestationVerifier() |
| 151 | // singleton) will be used. |
| 152 | Credentials::DeviceAttestationVerifier * deviceAttestationVerifier = nullptr; |
Andrei Litvin | d4da2fa | 2021-04-28 11:25:25 -0400 | [diff] [blame] | 153 | }; |
| 154 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 155 | /** |
| 156 | * @brief |
| 157 | * Controller applications can use this class to communicate with already paired CHIP devices. The |
| 158 | * application is required to provide access to the persistent storage, where the paired device information |
| 159 | * is stored. This object of this class can be initialized with the data from the storage (List of devices, |
| 160 | * and device pairing information for individual devices). Alternatively, this class can retrieve the |
| 161 | * relevant information when the application tries to communicate with the device |
| 162 | */ |
Zang MingJie | 7699761 | 2022-05-30 23:59:55 +0800 | [diff] [blame] | 163 | class DLL_EXPORT DeviceController : public AbstractDnssdDiscoveryController |
Pankaj Garg | a43ec92 | 2020-06-30 06:03:18 -0700 | [diff] [blame] | 164 | { |
| 165 | public: |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 166 | DeviceController(); |
Andrei Litvin | d80d593 | 2022-03-16 21:15:16 -0400 | [diff] [blame] | 167 | ~DeviceController() override {} |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 168 | |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 169 | CHIP_ERROR Init(ControllerInitParams params); |
Song Guo | 3cb69ff | 2021-04-01 20:58:25 +0800 | [diff] [blame] | 170 | |
Jerry Johns | 6cf91db | 2021-06-14 11:53:53 -0700 | [diff] [blame] | 171 | /** |
| 172 | * @brief |
| 173 | * Tears down the entirety of the stack, including destructing key objects in the system. |
| 174 | * This expects to be called with external thread synchronization, and will not internally |
| 175 | * grab the CHIP stack lock. |
| 176 | * |
| 177 | * This will also not stop the CHIP event queue / thread (if one exists). Consumers are expected to |
Martin Turon | d24eff1 | 2021-12-17 06:21:23 -0800 | [diff] [blame] | 178 | * ensure this happened before calling this method. |
Jerry Johns | 6cf91db | 2021-06-14 11:53:53 -0700 | [diff] [blame] | 179 | */ |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 180 | virtual void Shutdown(); |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 181 | |
Jerry Johns | c503a97 | 2022-04-15 15:22:58 -0700 | [diff] [blame] | 182 | SessionManager * SessionMgr() |
| 183 | { |
| 184 | if (mSystemState) |
| 185 | { |
| 186 | return mSystemState->SessionMgr(); |
| 187 | } |
| 188 | |
| 189 | return nullptr; |
| 190 | } |
| 191 | |
Jerry Johns | d94cbdc | 2022-07-26 08:07:06 -0700 | [diff] [blame] | 192 | CHIP_ERROR GetPeerAddressAndPort(NodeId peerId, Inet::IPAddress & addr, uint16_t & port); |
Pankaj Garg | fee3d26 | 2021-10-17 19:31:32 -0700 | [diff] [blame] | 193 | |
Vivien Nicolas | e047d27 | 2021-04-13 15:39:41 +0200 | [diff] [blame] | 194 | /** |
Kevin Coppock | 2f85372 | 2022-06-16 09:53:08 -0500 | [diff] [blame] | 195 | * @brief |
| 196 | * Looks up the PeerAddress for an established CASE session. |
| 197 | * |
Jerry Johns | d94cbdc | 2022-07-26 08:07:06 -0700 | [diff] [blame] | 198 | * @param[in] nodeId the NodeId of the target. |
Kevin Coppock | 2f85372 | 2022-06-16 09:53:08 -0500 | [diff] [blame] | 199 | * @param[out] addr the PeerAddress to be filled on success |
| 200 | * |
| 201 | * @return CHIP_ERROR CHIP_ERROR_NOT_CONNECTED if no CASE session exists for the device |
| 202 | */ |
| 203 | CHIP_ERROR GetPeerAddress(NodeId nodeId, Transport::PeerAddress & addr); |
| 204 | |
Jerry Johns | d94cbdc | 2022-07-26 08:07:06 -0700 | [diff] [blame] | 205 | ScopedNodeId GetPeerScopedId(NodeId nodeId) { return ScopedNodeId(nodeId, GetFabricIndex()); } |
| 206 | |
Kevin Coppock | 2f85372 | 2022-06-16 09:53:08 -0500 | [diff] [blame] | 207 | /** |
Boris Zbarsky | 1165152 | 2022-05-19 13:04:39 -0400 | [diff] [blame] | 208 | * This function finds the device corresponding to deviceId, and establishes |
| 209 | * a CASE session with it. |
| 210 | * |
| 211 | * Once the CASE session is successfully established the `onConnectedDevice` |
| 212 | * callback is called. This can happen before GetConnectedDevice returns if |
| 213 | * there is an existing CASE session. |
| 214 | * |
| 215 | * If a CASE sessions fails to be established, the `onError` callback will |
| 216 | * be called. This can also happen before GetConnectedDevice returns. |
| 217 | * |
| 218 | * An error return from this function means that neither callback has been |
| 219 | * called yet, and neither callback will be called in the future. |
Pankaj Garg | da45346 | 2021-06-22 19:53:34 -0700 | [diff] [blame] | 220 | */ |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 221 | CHIP_ERROR GetConnectedDevice(NodeId peerNodeId, Callback::Callback<OnDeviceConnected> * onConnection, |
Andrei Litvin | 5a28865 | 2022-03-11 16:23:36 -0500 | [diff] [blame] | 222 | chip::Callback::Callback<OnDeviceConnectionFailure> * onFailure) |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 223 | { |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 224 | VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE); |
Jerry Johns | d94cbdc | 2022-07-26 08:07:06 -0700 | [diff] [blame] | 225 | mSystemState->CASESessionMgr()->FindOrEstablishSession(ScopedNodeId(peerNodeId, GetFabricIndex()), onConnection, onFailure); |
Boris Zbarsky | 1165152 | 2022-05-19 13:04:39 -0400 | [diff] [blame] | 226 | return CHIP_NO_ERROR; |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 227 | } |
Pankaj Garg | da45346 | 2021-06-22 19:53:34 -0700 | [diff] [blame] | 228 | |
| 229 | /** |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 230 | * @brief |
| 231 | * Compute a PASE verifier and passcode ID for the desired setup pincode. |
| 232 | * |
| 233 | * This can be used to open a commissioning window on the device for |
| 234 | * additional administrator commissioning. |
| 235 | * |
| 236 | * @param[in] iterations The number of iterations to use when generating the verifier |
| 237 | * @param[in] setupPincode The desired PIN code to use |
| 238 | * @param[in] salt The 16-byte salt for verifier computation |
alexhqwang | 9279ab2 | 2022-02-18 03:20:52 -0800 | [diff] [blame] | 239 | * @param[out] outVerifier The Spake2pVerifier to be populated on success |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 240 | * |
| 241 | * @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error |
| 242 | */ |
Evgeny Margolis | 2727739 | 2022-03-09 19:29:14 -0800 | [diff] [blame] | 243 | CHIP_ERROR ComputePASEVerifier(uint32_t iterations, uint32_t setupPincode, const ByteSpan & salt, |
| 244 | Spake2pVerifier & outVerifier); |
Vivien Nicolas | 74bfa69 | 2021-04-08 18:19:33 +0200 | [diff] [blame] | 245 | |
Vivien Nicolas | 02d8900 | 2021-10-04 12:30:52 +0200 | [diff] [blame] | 246 | void RegisterDeviceDiscoveryDelegate(DeviceDiscoveryDelegate * delegate) { mDeviceDiscoveryDelegate = delegate; } |
Jerry Johns | 6cf91db | 2021-06-14 11:53:53 -0700 | [diff] [blame] | 247 | |
Sagar Dhawan | 57b104c | 2020-05-21 13:31:39 -0700 | [diff] [blame] | 248 | /** |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 249 | * @brief Get the Compressed Fabric ID assigned to the device. |
Yufeng Wang | b728b74 | 2021-06-16 08:04:17 -0700 | [diff] [blame] | 250 | */ |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 251 | uint64_t GetCompressedFabricId() const |
| 252 | { |
| 253 | const auto * fabricInfo = GetFabricInfo(); |
| 254 | return (fabricInfo != nullptr) ? static_cast<uint64_t>(fabricInfo->GetCompressedFabricId()) : kUndefinedCompressedFabricId; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @brief Get the Compressed Fabric Id as a big-endian 64 bit octet string. |
| 259 | * |
| 260 | * Output span is resized to 8 bytes on success if it was larger. |
| 261 | * |
| 262 | * @param outBytes span to contain the compressed fabric ID, must be at least 8 bytes long |
| 263 | * @return CHIP_ERROR_BUFFER_TOO_SMALL if `outBytes` is too small, CHIP_ERROR_INVALID_FABRIC_INDEX |
| 264 | * if the controller is somehow not associated with a fabric (internal error!) or |
| 265 | * CHIP_NO_ERROR on success. |
| 266 | */ |
| 267 | CHIP_ERROR GetCompressedFabricIdBytes(MutableByteSpan & outBytes) const; |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * @brief Get the raw Fabric ID assigned to the device. |
| 271 | */ |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 272 | uint64_t GetFabricId() const |
| 273 | { |
| 274 | const auto * fabricInfo = GetFabricInfo(); |
| 275 | return (fabricInfo != nullptr) ? static_cast<uint64_t>(fabricInfo->GetFabricId()) : kUndefinedFabricId; |
| 276 | } |
Yufeng Wang | b728b74 | 2021-06-16 08:04:17 -0700 | [diff] [blame] | 277 | |
Michael Sandstedt | d625d35 | 2022-01-25 22:31:41 -0600 | [diff] [blame] | 278 | /** |
| 279 | * @brief Get the Node ID of this instance. |
| 280 | */ |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 281 | NodeId GetNodeId() const |
Jean-Francois Penven | ae2a7e8 | 2022-03-02 17:33:06 -0500 | [diff] [blame] | 282 | { |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 283 | const auto * fabricInfo = GetFabricInfo(); |
| 284 | return (fabricInfo != nullptr) ? static_cast<uint64_t>(fabricInfo->GetNodeId()) : kUndefinedNodeId; |
Jean-Francois Penven | ae2a7e8 | 2022-03-02 17:33:06 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 287 | /** |
| 288 | * @brief Get the root public key for the fabric |
| 289 | * |
| 290 | * @param outRootPublicKey reference to public key object that gets updated on success. |
| 291 | * |
| 292 | * @return CHIP_NO_ERROR on success, CHIP_ERROR_INCORRECT_STATE if fabric table is unset, or another internal error |
| 293 | * on storage access failure. |
| 294 | */ |
| 295 | CHIP_ERROR GetRootPublicKey(Crypto::P256PublicKey & outRootPublicKey) const; |
| 296 | |
| 297 | FabricIndex GetFabricIndex() const { return mFabricIndex; } |
Ricardo Casallas | 0b10bc8 | 2022-03-07 11:43:47 -0500 | [diff] [blame] | 298 | |
Tennessee Carmel-Veilleux | 8cbfd2f | 2022-06-13 18:59:23 -0400 | [diff] [blame] | 299 | const FabricTable * GetFabricTable() const |
| 300 | { |
| 301 | if (mSystemState == nullptr) |
| 302 | { |
| 303 | return nullptr; |
| 304 | } |
| 305 | return mSystemState->Fabrics(); |
| 306 | } |
| 307 | |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 308 | OperationalCredentialsDelegate * GetOperationalCredentialsDelegate() { return mOperationalCredentialsDelegate; } |
| 309 | |
Andrei Litvin | 5a28865 | 2022-03-11 16:23:36 -0500 | [diff] [blame] | 310 | /** |
Tennessee Carmel-Veilleux | 51cbb53 | 2022-05-16 20:37:21 -0400 | [diff] [blame] | 311 | * @brief |
| 312 | * Reconfigures a new set of operational credentials to be used with this |
| 313 | * controller given ControllerInitParams state. |
| 314 | * |
| 315 | * WARNING: This is a low-level method that should only be called directly |
| 316 | * if you know exactly how this will interact with controller state, |
| 317 | * since there are several integrations that do this call for you. |
| 318 | * It can be used for fine-grained dependency injection of a controller's |
| 319 | * NOC and operational keypair. |
| 320 | */ |
| 321 | CHIP_ERROR InitControllerNOCChain(const ControllerInitParams & params); |
| 322 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 323 | protected: |
| 324 | enum class State |
| 325 | { |
| 326 | NotInitialized, |
| 327 | Initialized |
| 328 | }; |
| 329 | |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 330 | // This is not public to avoid users of DeviceController relying on "innards" access to |
| 331 | // the raw fabric table. Everything needed should be available with getters on DeviceController. |
| 332 | const FabricInfo * GetFabricInfo() const |
| 333 | { |
| 334 | VerifyOrReturnError((mState == State::Initialized) && (mFabricIndex != kUndefinedFabricIndex), nullptr); |
| 335 | VerifyOrReturnError(GetFabricTable() != nullptr, nullptr); |
| 336 | |
| 337 | return GetFabricTable()->FindFabricWithIndex(mFabricIndex); |
| 338 | } |
| 339 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 340 | State mState; |
| 341 | |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 342 | FabricIndex mFabricIndex = kUndefinedFabricIndex; |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 343 | |
Boris Zbarsky | 01bfdd0 | 2023-04-06 20:40:13 -0400 | [diff] [blame] | 344 | bool mRemoveFromFabricTableOnShutdown = true; |
| 345 | |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 346 | // TODO(cecille): Make this configuarable. |
| 347 | static constexpr int kMaxCommissionableNodes = 10; |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 348 | Dnssd::DiscoveredNodeData mCommissionableNodes[kMaxCommissionableNodes]; |
Sagar Dhawan | ae69dd7 | 2021-09-29 15:13:09 -0700 | [diff] [blame] | 349 | DeviceControllerSystemState * mSystemState = nullptr; |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 350 | |
Song Guo | 3cb69ff | 2021-04-01 20:58:25 +0800 | [diff] [blame] | 351 | ControllerDeviceInitParams GetControllerDeviceInitParams(); |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 352 | |
Pankaj Garg | aeebd19 | 2021-05-14 13:17:50 -0700 | [diff] [blame] | 353 | OperationalCredentialsDelegate * mOperationalCredentialsDelegate; |
| 354 | |
Vivien Nicolas | 401c393 | 2022-05-11 13:04:29 +0200 | [diff] [blame] | 355 | chip::VendorId mVendorId; |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 356 | |
Boris Zbarsky | 5081bbe | 2021-07-12 11:53:06 -0400 | [diff] [blame] | 357 | DiscoveredNodeList GetDiscoveredNodes() override { return DiscoveredNodeList(mCommissionableNodes); } |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 358 | }; |
| 359 | |
Zang MingJie | 6109d14 | 2022-04-22 06:19:42 +0800 | [diff] [blame] | 360 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
| 361 | using UdcTransportMgr = TransportMgr<Transport::UDP /* IPv6 */ |
| 362 | #if INET_CONFIG_ENABLE_IPV4 |
| 363 | , |
| 364 | Transport::UDP /* IPv4 */ |
| 365 | #endif |
| 366 | >; |
| 367 | #endif |
| 368 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 369 | /** |
chrisdecenzo | 620ef8f | 2022-11-22 09:10:02 -0800 | [diff] [blame] | 370 | * @brief Callback prototype for ExtendArmFailSafe command. |
| 371 | */ |
| 372 | typedef void (*OnExtendFailsafeSuccess)( |
| 373 | void * context, const app::Clusters::GeneralCommissioning::Commands::ArmFailSafeResponse::DecodableType & data); |
| 374 | typedef void (*OnExtendFailsafeFailure)(void * context, CHIP_ERROR error); |
| 375 | |
| 376 | /** |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 377 | * @brief |
| 378 | * The commissioner applications can use this class to pair new/unpaired CHIP devices. The application is |
| 379 | * required to provide write access to the persistent storage, where the paired device information |
| 380 | * will be stored. |
| 381 | */ |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 382 | class DLL_EXPORT DeviceCommissioner : public DeviceController, |
| 383 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY // make this commissioner discoverable |
| 384 | public Protocols::UserDirectedCommissioning::InstanceNameResolver, |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 385 | #endif |
C Freeman | 1ee9366 | 2022-02-03 09:51:04 -0500 | [diff] [blame] | 386 | public SessionEstablishmentDelegate, |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 387 | public app::ClusterStateCache::Callback |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 388 | { |
| 389 | public: |
| 390 | DeviceCommissioner(); |
Andrei Litvin | d80d593 | 2022-03-16 21:15:16 -0400 | [diff] [blame] | 391 | ~DeviceCommissioner() override {} |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 392 | |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 393 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY // make this commissioner discoverable |
| 394 | /** |
| 395 | * Set port for User Directed Commissioning |
| 396 | */ |
| 397 | CHIP_ERROR SetUdcListenPort(uint16_t listenPort); |
| 398 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
| 399 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 400 | /** |
Pankaj Garg | 0238a6c | 2021-05-10 07:33:19 -0700 | [diff] [blame] | 401 | * Commissioner-specific initialization, includes parameters such as the pairing delegate. |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 402 | */ |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 403 | CHIP_ERROR Init(CommissionerInitParams params); |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 404 | |
Jerry Johns | 6cf91db | 2021-06-14 11:53:53 -0700 | [diff] [blame] | 405 | /** |
| 406 | * @brief |
| 407 | * Tears down the entirety of the stack, including destructing key objects in the system. |
| 408 | * This is not a thread-safe API, and should be called with external synchronization. |
| 409 | * |
| 410 | * Please see implementation for more details. |
| 411 | */ |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 412 | void Shutdown() override; |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 413 | |
| 414 | // ----- Connection Management ----- |
| 415 | /** |
| 416 | * @brief |
Vivien Nicolas | 02d8900 | 2021-10-04 12:30:52 +0200 | [diff] [blame] | 417 | * Pair a CHIP device with the provided code. The code can be either a QRCode |
| 418 | * or a Manual Setup Code. |
| 419 | * Use registered DevicePairingDelegate object to receive notifications on |
| 420 | * pairing status updates. |
| 421 | * |
| 422 | * Note: Pairing process requires that the caller has registered PersistentStorageDelegate |
| 423 | * in the Init() call. |
| 424 | * |
| 425 | * @param[in] remoteDeviceId The remote device Id. |
| 426 | * @param[in] setUpCode The setup code for connecting to the device |
Vivien Nicolas | a60fb5a | 2022-09-12 18:23:18 +0200 | [diff] [blame] | 427 | * @param[in] discoveryType The network discovery type, defaults to DiscoveryType::kAll. |
Vivien Nicolas | 9f93a41 | 2023-06-08 18:16:15 +0200 | [diff] [blame] | 428 | * @param[in] resolutionData Optional resolution data previously discovered on the network for the target device. |
Vivien Nicolas | 02d8900 | 2021-10-04 12:30:52 +0200 | [diff] [blame] | 429 | */ |
Vivien Nicolas | 9f93a41 | 2023-06-08 18:16:15 +0200 | [diff] [blame] | 430 | CHIP_ERROR PairDevice(NodeId remoteDeviceId, const char * setUpCode, DiscoveryType discoveryType = DiscoveryType::kAll, |
| 431 | Optional<Dnssd::CommonResolutionData> resolutionData = NullOptional); |
Vivien Nicolas | a60fb5a | 2022-09-12 18:23:18 +0200 | [diff] [blame] | 432 | CHIP_ERROR PairDevice(NodeId remoteDeviceId, const char * setUpCode, const CommissioningParameters & CommissioningParameters, |
Vivien Nicolas | 9f93a41 | 2023-06-08 18:16:15 +0200 | [diff] [blame] | 433 | DiscoveryType discoveryType = DiscoveryType::kAll, |
| 434 | Optional<Dnssd::CommonResolutionData> resolutionData = NullOptional); |
Vivien Nicolas | 02d8900 | 2021-10-04 12:30:52 +0200 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * @brief |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 438 | * Pair a CHIP device with the provided Rendezvous connection parameters. |
| 439 | * Use registered DevicePairingDelegate object to receive notifications on |
| 440 | * pairing status updates. |
| 441 | * |
| 442 | * Note: Pairing process requires that the caller has registered PersistentStorageDelegate |
| 443 | * in the Init() call. |
| 444 | * |
| 445 | * @param[in] remoteDeviceId The remote device Id. |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 446 | * @param[in] rendezvousParams The Rendezvous connection parameters |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 447 | */ |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 448 | CHIP_ERROR PairDevice(NodeId remoteDeviceId, RendezvousParameters & rendezvousParams); |
Yufeng Wang | c6f6584 | 2023-05-15 19:53:07 -0700 | [diff] [blame] | 449 | |
Kevin Schoedel | fcd9785 | 2021-12-07 17:16:40 -0500 | [diff] [blame] | 450 | /** |
| 451 | * @overload |
| 452 | * @param[in] remoteDeviceId The remote device Id. |
| 453 | * @param[in] rendezvousParams The Rendezvous connection parameters |
| 454 | * @param[in] commissioningParams The commissioning parameters (uses default if not supplied) |
| 455 | */ |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 456 | CHIP_ERROR PairDevice(NodeId remoteDeviceId, RendezvousParameters & rendezvousParams, |
| 457 | CommissioningParameters & commissioningParams); |
| 458 | |
| 459 | /** |
| 460 | * @brief |
| 461 | * Start establishing a PASE connection with a node for the purposes of commissioning. |
| 462 | * Commissioners that wish to use the auto-commissioning functions should use the |
| 463 | * supplied "PairDevice" functions above to automatically establish a connection then |
| 464 | * perform commissioning. This function is intended to be use by commissioners that |
| 465 | * are not using the supplied auto-commissioner. |
| 466 | * |
| 467 | * This function is non-blocking. PASE is established once the DevicePairingDelegate |
| 468 | * receives the OnPairingComplete call. |
| 469 | * |
| 470 | * PASE connections can only be established with nodes that have their commissioning |
| 471 | * window open. The PASE connection will fail if this window is not open and the |
| 472 | * OnPairingComplete will be called with an error. |
| 473 | * |
| 474 | * @param[in] remoteDeviceId The remote device Id. |
Kevin Schoedel | fcd9785 | 2021-12-07 17:16:40 -0500 | [diff] [blame] | 475 | * @param[in] params The Rendezvous connection parameters |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 476 | */ |
| 477 | CHIP_ERROR EstablishPASEConnection(NodeId remoteDeviceId, RendezvousParameters & params); |
| 478 | |
| 479 | /** |
| 480 | * @brief |
C Freeman | 3382a5f | 2022-03-09 16:06:59 -0500 | [diff] [blame] | 481 | * Start establishing a PASE connection with a node for the purposes of commissioning. |
| 482 | * Commissioners that wish to use the auto-commissioning functions should use the |
| 483 | * supplied "PairDevice" functions above to automatically establish a connection then |
| 484 | * perform commissioning. This function is intended to be used by commissioners that |
| 485 | * are not using the supplied auto-commissioner. |
| 486 | * |
| 487 | * This function is non-blocking. PASE is established once the DevicePairingDelegate |
| 488 | * receives the OnPairingComplete call. |
| 489 | * |
| 490 | * PASE connections can only be established with nodes that have their commissioning |
| 491 | * window open. The PASE connection will fail if this window is not open and in that case |
| 492 | * OnPairingComplete will be called with an error. |
| 493 | * |
| 494 | * @param[in] remoteDeviceId The remote device Id. |
| 495 | * @param[in] setUpCode The setup code for connecting to the device |
Vivien Nicolas | a60fb5a | 2022-09-12 18:23:18 +0200 | [diff] [blame] | 496 | * @param[in] discoveryType The network discovery type, defaults to DiscoveryType::kAll. |
Vivien Nicolas | 9f93a41 | 2023-06-08 18:16:15 +0200 | [diff] [blame] | 497 | * @param[in] resolutionData Optional resolution data previously discovered on the network for the target device. |
C Freeman | 3382a5f | 2022-03-09 16:06:59 -0500 | [diff] [blame] | 498 | */ |
Vivien Nicolas | a60fb5a | 2022-09-12 18:23:18 +0200 | [diff] [blame] | 499 | CHIP_ERROR EstablishPASEConnection(NodeId remoteDeviceId, const char * setUpCode, |
Vivien Nicolas | 9f93a41 | 2023-06-08 18:16:15 +0200 | [diff] [blame] | 500 | DiscoveryType discoveryType = DiscoveryType::kAll, |
| 501 | Optional<Dnssd::CommonResolutionData> resolutionData = NullOptional); |
C Freeman | 3382a5f | 2022-03-09 16:06:59 -0500 | [diff] [blame] | 502 | |
| 503 | /** |
| 504 | * @brief |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 505 | * Start the auto-commissioning process on a node after establishing a PASE connection. |
| 506 | * This function is intended to be used in conjunction with the EstablishPASEConnection |
| 507 | * function. It can be called either before or after the DevicePairingDelegate receives |
| 508 | * the OnPairingComplete call. Commissioners that want to perform simple auto-commissioning |
| 509 | * should use the supplied "PairDevice" functions above, which will establish the PASE |
| 510 | * connection and commission automatically. |
| 511 | * |
| 512 | * @param[in] remoteDeviceId The remote device Id. |
| 513 | * @param[in] params The commissioning parameters |
| 514 | */ |
| 515 | CHIP_ERROR Commission(NodeId remoteDeviceId, CommissioningParameters & params); |
C Freeman | 3382a5f | 2022-03-09 16:06:59 -0500 | [diff] [blame] | 516 | CHIP_ERROR Commission(NodeId remoteDeviceId); |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 517 | |
Anush Nadathur | b06bd8c | 2022-04-06 17:29:16 -0700 | [diff] [blame] | 518 | /** |
| 519 | * @brief |
| 520 | * This function instructs the commissioner to proceed to the next stage of commissioning after |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 521 | * attestation is reported to an installed attestation delegate. |
Anush Nadathur | b06bd8c | 2022-04-06 17:29:16 -0700 | [diff] [blame] | 522 | * |
| 523 | * @param[in] device The device being commissioned. |
| 524 | * @param[in] attestationResult The attestation result to use instead of whatever the device |
| 525 | * attestation verifier came up with. May be a success or an error result. |
| 526 | */ |
| 527 | CHIP_ERROR |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 528 | ContinueCommissioningAfterDeviceAttestation(DeviceProxy * device, Credentials::AttestationVerificationResult attestationResult); |
Anush Nadathur | b06bd8c | 2022-04-06 17:29:16 -0700 | [diff] [blame] | 529 | |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 530 | CHIP_ERROR GetDeviceBeingCommissioned(NodeId deviceId, CommissioneeDeviceProxy ** device); |
| 531 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 532 | /** |
| 533 | * @brief |
| 534 | * This function stops a pairing process that's in progress. It does not delete the pairing of a previously |
| 535 | * paired device. |
| 536 | * |
| 537 | * @param[in] remoteDeviceId The remote device Id. |
| 538 | * |
| 539 | * @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error |
| 540 | */ |
| 541 | CHIP_ERROR StopPairing(NodeId remoteDeviceId); |
| 542 | |
| 543 | /** |
| 544 | * @brief |
| 545 | * Remove pairing for a paired device. If the device is currently being paired, it'll stop the pairing process. |
| 546 | * |
| 547 | * @param[in] remoteDeviceId The remote device Id. |
| 548 | * |
| 549 | * @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error |
| 550 | */ |
| 551 | CHIP_ERROR UnpairDevice(NodeId remoteDeviceId); |
Jiacheng Guo | e4ac46a | 2020-11-11 00:04:34 +0800 | [diff] [blame] | 552 | |
Pankaj Garg | 0f443b8 | 2021-05-04 14:21:10 -0700 | [diff] [blame] | 553 | //////////// SessionEstablishmentDelegate Implementation /////////////// |
| 554 | void OnSessionEstablishmentError(CHIP_ERROR error) override; |
Zang MingJie | 477862d | 2022-04-20 04:52:22 +0800 | [diff] [blame] | 555 | void OnSessionEstablished(const SessionHandle & session) override; |
Pankaj Garg | e1c1bb1 | 2020-07-02 08:55:46 -0700 | [diff] [blame] | 556 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 557 | void RendezvousCleanup(CHIP_ERROR status); |
| 558 | |
C Freeman | 798d7d2 | 2021-12-10 12:06:36 -0500 | [diff] [blame] | 559 | void PerformCommissioningStep(DeviceProxy * device, CommissioningStage step, CommissioningParameters & params, |
C Freeman | 8ee96e4 | 2022-01-21 11:43:45 -0500 | [diff] [blame] | 560 | CommissioningDelegate * delegate, EndpointId endpoint, Optional<System::Clock::Timeout> timeout); |
C Freeman | 798d7d2 | 2021-12-10 12:06:36 -0500 | [diff] [blame] | 561 | |
C Freeman | 09a4921 | 2022-01-24 21:19:43 -0500 | [diff] [blame] | 562 | /** |
| 563 | * @brief |
| 564 | * This function validates the Attestation Information sent by the device. |
| 565 | * |
C Freeman | b541dee | 2022-02-17 01:14:48 -0500 | [diff] [blame] | 566 | * @param[in] info Structure contatining all the required information for validating the device attestation. |
C Freeman | 09a4921 | 2022-01-24 21:19:43 -0500 | [diff] [blame] | 567 | */ |
C Freeman | b541dee | 2022-02-17 01:14:48 -0500 | [diff] [blame] | 568 | CHIP_ERROR ValidateAttestationInfo(const Credentials::DeviceAttestationVerifier::AttestationInfo & info); |
C Freeman | 09a4921 | 2022-01-24 21:19:43 -0500 | [diff] [blame] | 569 | |
C Freeman | 9fc0d2e | 2022-01-28 10:52:04 -0500 | [diff] [blame] | 570 | /** |
| 571 | * @brief |
| 572 | * Sends CommissioningStepComplete report to the commissioning delegate. Function will fill in current step. |
| 573 | * @params[in] err error from the current step |
| 574 | * @params[in] report report to send. Current step will be filled in automatically |
| 575 | */ |
C Freeman | 8d6805f | 2022-01-26 22:31:49 -0500 | [diff] [blame] | 576 | void |
| 577 | CommissioningStageComplete(CHIP_ERROR err, |
| 578 | CommissioningDelegate::CommissioningReport report = CommissioningDelegate::CommissioningReport()); |
C Freeman | e86bb35 | 2021-05-26 19:19:40 -0400 | [diff] [blame] | 579 | |
chrisdecenzo | a604ce2 | 2022-08-15 20:44:23 -0700 | [diff] [blame] | 580 | /** |
| 581 | * @brief |
| 582 | * This function is called by the DevicePairingDelegate to indicate that network credentials have been set |
| 583 | * on the CommissioningParameters of the CommissioningDelegate using CommissioningDelegate.SetCommissioningParameters(). |
| 584 | * As a result, commissioning can advance to the next stage. |
| 585 | * |
| 586 | * The DevicePairingDelegate may call this method from the OnScanNetworksSuccess and OnScanNetworksFailure callbacks, |
| 587 | * or it may call this method after obtaining network credentials using asyncronous methods (prompting user, cloud API call, |
| 588 | * etc). |
| 589 | * |
chrisdecenzo | 620ef8f | 2022-11-22 09:10:02 -0800 | [diff] [blame] | 590 | * If an error happens in the subsequent network commissioning step (either NetworkConfig or ConnectNetwork commands) |
| 591 | * then the DevicePairingDelegate will receive the error in completionStatus.networkCommissioningStatus and the |
| 592 | * commissioning stage will return to kNeedsNetworkCreds so that the DevicePairingDelegate can re-attempt with new |
| 593 | * network information. The DevicePairingDelegate can exit the commissioning process by calling StopPairing. |
| 594 | * |
chrisdecenzo | a604ce2 | 2022-08-15 20:44:23 -0700 | [diff] [blame] | 595 | * @return CHIP_ERROR The return status. Returns CHIP_ERROR_INCORRECT_STATE if not in the correct state (kNeedsNetworkCreds). |
| 596 | */ |
| 597 | CHIP_ERROR NetworkCredentialsReady(); |
| 598 | |
chrisdecenzo | 06457ae | 2022-08-16 17:06:29 -0700 | [diff] [blame] | 599 | /** |
| 600 | * @brief |
| 601 | * This function returns the current CommissioningStage for this commissioner. |
| 602 | */ |
| 603 | CommissioningStage GetCommissioningStage() { return mCommissioningStage; } |
| 604 | |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 605 | #if CONFIG_NETWORK_LAYER_BLE |
chrisdecenzo | 654104c | 2022-03-02 15:11:33 -0800 | [diff] [blame] | 606 | #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE |
| 607 | /** |
| 608 | * @brief |
| 609 | * Prior to commissioning, the Controller should make sure the BleLayer transport |
| 610 | * is set to the Commissioner transport and not the Server transport. |
| 611 | */ |
| 612 | void ConnectBleTransportToSelf(); |
| 613 | #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE |
| 614 | |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 615 | /** |
| 616 | * @brief |
| 617 | * Once we have finished all commissioning work, the Controller should close the BLE |
| 618 | * connection to the device and establish CASE session / another PASE session to the device |
| 619 | * if needed. |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 620 | */ |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 621 | void CloseBleConnection(); |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 622 | #endif |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 623 | /** |
| 624 | * @brief |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 625 | * Discover all devices advertising as commissionable. |
| 626 | * Should be called on main loop thread. |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 627 | * * @param[in] filter Browse filter - controller will look for only the specified subtype. |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 628 | * @return CHIP_ERROR The return status |
| 629 | */ |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 630 | CHIP_ERROR DiscoverCommissionableNodes(Dnssd::DiscoveryFilter filter); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 631 | |
| 632 | /** |
Boris Zbarsky | bd83c1a | 2022-10-04 16:22:19 -0400 | [diff] [blame] | 633 | * Stop commissionable discovery triggered by a previous |
| 634 | * DiscoverCommissionableNodes call. |
| 635 | */ |
| 636 | CHIP_ERROR StopCommissionableDiscovery(); |
| 637 | |
| 638 | /** |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 639 | * @brief |
| 640 | * Returns information about discovered devices. |
| 641 | * Should be called on main loop thread. |
Sharad Binjola | f78d60e | 2021-06-17 05:50:45 -0700 | [diff] [blame] | 642 | * @return const DiscoveredNodeData* info about the selected device. May be nullptr if no information has been returned yet. |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 643 | */ |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 644 | const Dnssd::DiscoveredNodeData * GetDiscoveredDevice(int idx); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 645 | |
| 646 | /** |
| 647 | * @brief |
| 648 | * Returns the max number of commissionable nodes this commissioner can track mdns information for. |
| 649 | * @return int The max number of commissionable nodes supported |
| 650 | */ |
| 651 | int GetMaxCommissionableNodesSupported() { return kMaxCommissionableNodes; } |
| 652 | |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 653 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY // make this commissioner discoverable |
| 654 | /** |
| 655 | * @brief |
| 656 | * Called when a UDC message is received specifying the given instanceName |
| 657 | * This method indicates that UDC Server needs the Commissionable Node corresponding to |
| 658 | * the given instance name to be found. UDC Server will wait for OnCommissionableNodeFound. |
| 659 | * |
| 660 | * @param instanceName DNS-SD instance name for the client requesting commissioning |
| 661 | * |
| 662 | */ |
| 663 | void FindCommissionableNode(char * instanceName) override; |
| 664 | |
| 665 | /** |
| 666 | * @brief |
C Freeman | 3a339bc | 2021-12-03 14:48:02 -0500 | [diff] [blame] | 667 | * Return the UDC Server instance |
| 668 | * |
| 669 | */ |
| 670 | UserDirectedCommissioningServer * GetUserDirectedCommissioningServer() { return mUdcServer; } |
| 671 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
| 672 | |
C Freeman | 3a339bc | 2021-12-03 14:48:02 -0500 | [diff] [blame] | 673 | /** |
| 674 | * @brief |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 675 | * Overrides method from AbstractDnssdDiscoveryController |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 676 | * |
| 677 | * @param nodeData DNS-SD node information |
| 678 | * |
| 679 | */ |
Andrei Litvin | f2c450c | 2022-02-23 23:57:30 -0500 | [diff] [blame] | 680 | void OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData) override; |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 681 | |
Jerry Johns | 6cf91db | 2021-06-14 11:53:53 -0700 | [diff] [blame] | 682 | void RegisterPairingDelegate(DevicePairingDelegate * pairingDelegate) { mPairingDelegate = pairingDelegate; } |
Boris Zbarsky | d170a25 | 2022-03-31 20:51:58 -0400 | [diff] [blame] | 683 | DevicePairingDelegate * GetPairingDelegate() const { return mPairingDelegate; } |
Jerry Johns | 6cf91db | 2021-06-14 11:53:53 -0700 | [diff] [blame] | 684 | |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 685 | // ClusterStateCache::Callback impl |
Boris Zbarsky | 6c86c94 | 2022-05-23 18:15:17 -0400 | [diff] [blame] | 686 | void OnDone(app::ReadClient *) override; |
C Freeman | 1ee9366 | 2022-02-03 09:51:04 -0500 | [diff] [blame] | 687 | |
tehampson | 7600179 | 2022-06-27 19:58:47 -0400 | [diff] [blame] | 688 | // Issue an NOC chain using the associated OperationalCredentialsDelegate. The NOC chain will |
| 689 | // be provided in X509 DER format. |
tehampson | f3be2ee | 2022-06-23 13:10:52 -0400 | [diff] [blame] | 690 | // NOTE: This is only valid assuming that `mOperationalCredentialsDelegate` is what is desired |
| 691 | // to issue the NOC chain. |
| 692 | CHIP_ERROR IssueNOCChain(const ByteSpan & NOCSRElements, NodeId nodeId, |
| 693 | chip::Callback::Callback<OnNOCChainGeneration> * callback); |
| 694 | |
chrisdecenzo | 488262f | 2022-08-15 19:53:18 -0700 | [diff] [blame] | 695 | void SetDeviceAttestationVerifier(Credentials::DeviceAttestationVerifier * deviceAttestationVerifier) |
| 696 | { |
| 697 | mDeviceAttestationVerifier = deviceAttestationVerifier; |
| 698 | } |
| 699 | |
Sharad Binjola | 74ea252 | 2022-08-19 19:21:47 -0700 | [diff] [blame] | 700 | Optional<CommissioningParameters> GetCommissioningParameters() |
| 701 | { |
| 702 | return mDefaultCommissioner == nullptr ? NullOptional : MakeOptional(mDefaultCommissioner->GetCommissioningParameters()); |
| 703 | } |
| 704 | |
Boris Zbarsky | e42b533 | 2023-03-14 00:13:25 -0400 | [diff] [blame] | 705 | // Reset the arm failsafe timer during commissioning. If this returns |
| 706 | // false, that means that the timer was already set for a longer time period |
| 707 | // than the new time we are trying to set. In this case, neither |
| 708 | // onSuccess nor onFailure will be called. |
| 709 | bool ExtendArmFailSafe(DeviceProxy * proxy, CommissioningStage step, uint16_t armFailSafeTimeout, |
chrisdecenzo | 620ef8f | 2022-11-22 09:10:02 -0800 | [diff] [blame] | 710 | Optional<System::Clock::Timeout> commandTimeout, OnExtendFailsafeSuccess onSuccess, |
| 711 | OnExtendFailsafeFailure onFailure); |
| 712 | |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 713 | private: |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 714 | DevicePairingDelegate * mPairingDelegate; |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 715 | |
C Freeman | 6e981ec | 2022-03-18 16:04:49 -0400 | [diff] [blame] | 716 | DeviceProxy * mDeviceBeingCommissioned = nullptr; |
| 717 | CommissioneeDeviceProxy * mDeviceInPASEEstablishment = nullptr; |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 718 | |
C Freeman | e86bb35 | 2021-05-26 19:19:40 -0400 | [diff] [blame] | 719 | CommissioningStage mCommissioningStage = CommissioningStage::kSecurePairing; |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 720 | bool mRunCommissioningAfterConnection = false; |
C Freeman | e86bb35 | 2021-05-26 19:19:40 -0400 | [diff] [blame] | 721 | |
Kevin Schoedel | 22f5d22 | 2022-02-02 14:35:20 -0500 | [diff] [blame] | 722 | ObjectPool<CommissioneeDeviceProxy, kNumMaxActiveDevices> mCommissioneeDevicePool; |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 723 | |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 724 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY // make this commissioner discoverable |
| 725 | UserDirectedCommissioningServer * mUdcServer = nullptr; |
| 726 | // mUdcTransportMgr is for insecure communication (ex. user directed commissioning) |
Zang MingJie | 6109d14 | 2022-04-22 06:19:42 +0800 | [diff] [blame] | 727 | UdcTransportMgr * mUdcTransportMgr = nullptr; |
| 728 | uint16_t mUdcListenPort = CHIP_UDC_PORT; |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 729 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
| 730 | |
Vivien Nicolas | df18a8c | 2022-11-21 16:10:18 +0100 | [diff] [blame] | 731 | #if CONFIG_NETWORK_LAYER_BLE |
| 732 | static void OnDiscoveredDeviceOverBleSuccess(void * appState, BLE_CONNECTION_OBJECT connObj); |
| 733 | static void OnDiscoveredDeviceOverBleError(void * appState, CHIP_ERROR err); |
| 734 | RendezvousParameters mRendezvousParametersForDeviceDiscoveredOverBle; |
| 735 | #endif |
| 736 | |
Pankaj Garg | 2f064a5 | 2021-03-17 06:47:58 -0700 | [diff] [blame] | 737 | CHIP_ERROR LoadKeyId(PersistentStorageDelegate * delegate, uint16_t & out); |
| 738 | |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 739 | /* This function sends a Device Attestation Certificate chain request to the device. |
| 740 | The function does not hold a reference to the device object. |
| 741 | */ |
Boris Zbarsky | b27420a | 2022-06-03 19:50:39 -0400 | [diff] [blame] | 742 | CHIP_ERROR SendCertificateChainRequestCommand(DeviceProxy * device, Credentials::CertificateType certificateType, |
| 743 | Optional<System::Clock::Timeout> timeout); |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 744 | /* This function sends an Attestation request to the device. |
| 745 | The function does not hold a reference to the device object. |
| 746 | */ |
Boris Zbarsky | b27420a | 2022-06-03 19:50:39 -0400 | [diff] [blame] | 747 | CHIP_ERROR SendAttestationRequestCommand(DeviceProxy * device, const ByteSpan & attestationNonce, |
| 748 | Optional<System::Clock::Timeout> timeout); |
Boris Zbarsky | 570a1ce | 2022-02-04 11:38:38 -0500 | [diff] [blame] | 749 | /* This function sends an CSR request to the device. |
Martin Turon | d24eff1 | 2021-12-17 06:21:23 -0800 | [diff] [blame] | 750 | The function does not hold a reference to the device object. |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 751 | */ |
Boris Zbarsky | b27420a | 2022-06-03 19:50:39 -0400 | [diff] [blame] | 752 | CHIP_ERROR SendOperationalCertificateSigningRequestCommand(DeviceProxy * device, const ByteSpan & csrNonce, |
| 753 | Optional<System::Clock::Timeout> timeout); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 754 | /* This function sends the operational credentials to the device. |
Martin Turon | d24eff1 | 2021-12-17 06:21:23 -0800 | [diff] [blame] | 755 | The function does not hold a reference to the device object. |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 756 | */ |
C Freeman | 566cf88 | 2022-03-31 19:58:36 -0400 | [diff] [blame] | 757 | CHIP_ERROR SendOperationalCertificate(DeviceProxy * device, const ByteSpan & nocCertBuf, const Optional<ByteSpan> & icaCertBuf, |
Damian Królik | 9b63633 | 2023-01-15 05:42:02 +0100 | [diff] [blame] | 758 | IdentityProtectionKeySpan ipk, NodeId adminSubject, |
| 759 | Optional<System::Clock::Timeout> timeout); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 760 | /* This function sends the trusted root certificate to the device. |
Martin Turon | d24eff1 | 2021-12-17 06:21:23 -0800 | [diff] [blame] | 761 | The function does not hold a reference to the device object. |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 762 | */ |
Boris Zbarsky | b27420a | 2022-06-03 19:50:39 -0400 | [diff] [blame] | 763 | CHIP_ERROR SendTrustedRootCertificate(DeviceProxy * device, const ByteSpan & rcac, Optional<System::Clock::Timeout> timeout); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 764 | |
| 765 | /* This function is called by the commissioner code when the device completes |
| 766 | the operational credential provisioning process. |
Martin Turon | d24eff1 | 2021-12-17 06:21:23 -0800 | [diff] [blame] | 767 | The function does not hold a reference to the device object. |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 768 | */ |
C Freeman | 6e981ec | 2022-03-18 16:04:49 -0400 | [diff] [blame] | 769 | CHIP_ERROR OnOperationalCredentialsProvisioningCompletion(DeviceProxy * device); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 770 | |
| 771 | /* Callback when the previously sent CSR request results in failure */ |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 772 | static void OnCSRFailureResponse(void * context, CHIP_ERROR error); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 773 | |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 774 | void ExtendArmFailSafeForDeviceAttestation(const Credentials::DeviceAttestationVerifier::AttestationInfo & info, |
| 775 | Credentials::AttestationVerificationResult result); |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 776 | static void OnCertificateChainFailureResponse(void * context, CHIP_ERROR error); |
| 777 | static void OnCertificateChainResponse( |
| 778 | void * context, const app::Clusters::OperationalCredentials::Commands::CertificateChainResponse::DecodableType & response); |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 779 | |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 780 | static void OnAttestationFailureResponse(void * context, CHIP_ERROR error); |
| 781 | static void |
| 782 | OnAttestationResponse(void * context, |
| 783 | const app::Clusters::OperationalCredentials::Commands::AttestationResponse::DecodableType & data); |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 784 | |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 785 | /** |
| 786 | * @brief |
| 787 | * This function is called by the IM layer when the commissioner receives the CSR from the device. |
Boris Zbarsky | 570a1ce | 2022-02-04 11:38:38 -0500 | [diff] [blame] | 788 | * (Reference: Specifications section 11.18.5.6. NOCSR Elements) |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 789 | * |
Pankaj Garg | 84c369c | 2021-07-30 11:54:25 -0700 | [diff] [blame] | 790 | * @param[in] context The context provided while registering the callback. |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 791 | * @param[in] data The response struct containing the following fields: |
| 792 | * NOCSRElements: CSR elements as per specifications section 11.22.5.6. NOCSR Elements. |
| 793 | * AttestationSignature: Cryptographic signature generated for the fields in the response |
| 794 | * message. |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 795 | */ |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 796 | static void OnOperationalCertificateSigningRequest( |
Boris Zbarsky | 570a1ce | 2022-02-04 11:38:38 -0500 | [diff] [blame] | 797 | void * context, const app::Clusters::OperationalCredentials::Commands::CSRResponse::DecodableType & data); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 798 | |
| 799 | /* Callback when adding operational certs to device results in failure */ |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 800 | static void OnAddNOCFailureResponse(void * context, CHIP_ERROR errro); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 801 | /* Callback when the device confirms that it has added the operational certificates */ |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 802 | static void |
| 803 | OnOperationalCertificateAddResponse(void * context, |
| 804 | const app::Clusters::OperationalCredentials::Commands::NOCResponse::DecodableType & data); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 805 | |
| 806 | /* Callback when the device confirms that it has added the root certificate */ |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 807 | static void OnRootCertSuccessResponse(void * context, const chip::app::DataModel::NullObjectType &); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 808 | /* Callback called when adding root cert to device results in failure */ |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 809 | static void OnRootCertFailureResponse(void * context, CHIP_ERROR error); |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 810 | |
Marcos B | cae6319 | 2023-01-13 14:53:14 -0700 | [diff] [blame] | 811 | static void OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle); |
Jerry Johns | d94cbdc | 2022-07-26 08:07:06 -0700 | [diff] [blame] | 812 | static void OnDeviceConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error); |
Boris Zbarsky | 8afc1f5 | 2023-03-09 23:58:40 -0500 | [diff] [blame] | 813 | #if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES |
| 814 | static void OnDeviceConnectionRetryFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error, |
| 815 | System::Clock::Seconds16 retryTimeout); |
| 816 | #endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES |
Pankaj Garg | da45346 | 2021-06-22 19:53:34 -0700 | [diff] [blame] | 817 | |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 818 | static void OnDeviceAttestationInformationVerification(void * context, |
| 819 | const Credentials::DeviceAttestationVerifier::AttestationInfo & info, |
| 820 | Credentials::AttestationVerificationResult result); |
Vijay Selvaraj | b8c540a | 2022-01-05 11:58:12 -0500 | [diff] [blame] | 821 | |
Pankaj Garg | 84c369c | 2021-07-30 11:54:25 -0700 | [diff] [blame] | 822 | static void OnDeviceNOCChainGeneration(void * context, CHIP_ERROR status, const ByteSpan & noc, const ByteSpan & icac, |
Damian Królik | 9b63633 | 2023-01-15 05:42:02 +0100 | [diff] [blame] | 823 | const ByteSpan & rcac, Optional<IdentityProtectionKeySpan> ipk, |
| 824 | Optional<NodeId> adminSubject); |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 825 | static void OnArmFailSafe(void * context, |
| 826 | const chip::app::Clusters::GeneralCommissioning::Commands::ArmFailSafeResponse::DecodableType & data); |
| 827 | static void OnSetRegulatoryConfigResponse( |
| 828 | void * context, |
| 829 | const chip::app::Clusters::GeneralCommissioning::Commands::SetRegulatoryConfigResponse::DecodableType & data); |
| 830 | static void |
chrisdecenzo | 1679342 | 2022-07-28 08:00:08 -0700 | [diff] [blame] | 831 | OnScanNetworksResponse(void * context, |
| 832 | const app::Clusters::NetworkCommissioning::Commands::ScanNetworksResponse::DecodableType & data); |
| 833 | static void OnScanNetworksFailure(void * context, CHIP_ERROR err); |
| 834 | static void |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 835 | OnNetworkConfigResponse(void * context, |
chrisdecenzo | 1679342 | 2022-07-28 08:00:08 -0700 | [diff] [blame] | 836 | const app::Clusters::NetworkCommissioning::Commands::NetworkConfigResponse::DecodableType & data); |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 837 | static void OnConnectNetworkResponse( |
| 838 | void * context, const chip::app::Clusters::NetworkCommissioning::Commands::ConnectNetworkResponse::DecodableType & data); |
| 839 | static void OnCommissioningCompleteResponse( |
| 840 | void * context, |
| 841 | const chip::app::Clusters::GeneralCommissioning::Commands::CommissioningCompleteResponse::DecodableType & data); |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 842 | static void OnDisarmFailsafe(void * context, |
| 843 | const app::Clusters::GeneralCommissioning::Commands::ArmFailSafeResponse::DecodableType & data); |
| 844 | static void OnDisarmFailsafeFailure(void * context, CHIP_ERROR error); |
| 845 | void DisarmDone(); |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 846 | static void OnArmFailSafeExtendedForDeviceAttestation( |
Anush Nadathur | b06bd8c | 2022-04-06 17:29:16 -0700 | [diff] [blame] | 847 | void * context, const chip::app::Clusters::GeneralCommissioning::Commands::ArmFailSafeResponse::DecodableType & data); |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 848 | static void OnFailedToExtendedArmFailSafeDeviceAttestation(void * context, CHIP_ERROR error); |
Pankaj Garg | ab5c617 | 2021-06-28 11:03:11 -0700 | [diff] [blame] | 849 | |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 850 | /** |
| 851 | * @brief |
| 852 | * This function processes the CSR sent by the device. |
Boris Zbarsky | 570a1ce | 2022-02-04 11:38:38 -0500 | [diff] [blame] | 853 | * (Reference: Specifications section 11.18.5.6. NOCSR Elements) |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 854 | * |
C Freeman | 09a4921 | 2022-01-24 21:19:43 -0500 | [diff] [blame] | 855 | * @param[in] proxy device proxy |
Pankaj Garg | 84c369c | 2021-07-30 11:54:25 -0700 | [diff] [blame] | 856 | * @param[in] NOCSRElements CSR elements as per specifications section 11.22.5.6. NOCSR Elements. |
| 857 | * @param[in] AttestationSignature Cryptographic signature generated for all the above fields. |
C Freeman | 09a4921 | 2022-01-24 21:19:43 -0500 | [diff] [blame] | 858 | * @param[in] dac device attestation certificate |
C Freeman | a208c3d | 2022-04-04 08:23:45 -0400 | [diff] [blame] | 859 | * @param[in] pai Product Attestation Intermediate certificate |
C Freeman | 09a4921 | 2022-01-24 21:19:43 -0500 | [diff] [blame] | 860 | * @param[in] csrNonce certificate signing request nonce |
Pankaj Garg | 22365d8 | 2021-05-13 09:02:36 -0700 | [diff] [blame] | 861 | */ |
C Freeman | a208c3d | 2022-04-04 08:23:45 -0400 | [diff] [blame] | 862 | CHIP_ERROR ProcessCSR(DeviceProxy * proxy, const ByteSpan & NOCSRElements, const ByteSpan & AttestationSignature, |
| 863 | const ByteSpan & dac, const ByteSpan & pai, const ByteSpan & csrNonce); |
| 864 | |
| 865 | /** |
| 866 | * @brief |
| 867 | * This function validates the CSR information from the device. |
| 868 | * (Reference: Specifications section 11.18.5.6. NOCSR Elements) |
| 869 | * |
| 870 | * @param[in] proxy device proxy |
| 871 | * @param[in] NOCSRElements CSR elements as per specifications section 11.22.5.6. NOCSR Elements. |
| 872 | * @param[in] AttestationSignature Cryptographic signature generated for all the above fields. |
| 873 | * @param[in] dac device attestation certificate |
| 874 | * @param[in] csrNonce certificate signing request nonce |
| 875 | */ |
| 876 | CHIP_ERROR ValidateCSR(DeviceProxy * proxy, const ByteSpan & NOCSRElements, const ByteSpan & AttestationSignature, |
| 877 | const ByteSpan & dac, const ByteSpan & csrNonce); |
Pankaj Garg | a490773 | 2021-05-07 09:08:45 -0700 | [diff] [blame] | 878 | |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 879 | /** |
| 880 | * @brief |
| 881 | * This function processes the DAC or PAI certificate sent by the device. |
| 882 | */ |
| 883 | CHIP_ERROR ProcessCertificateChain(const ByteSpan & certificate); |
| 884 | |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 885 | void HandleAttestationResult(CHIP_ERROR err); |
| 886 | |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 887 | CommissioneeDeviceProxy * FindCommissioneeDevice(NodeId id); |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 888 | CommissioneeDeviceProxy * FindCommissioneeDevice(const Transport::PeerAddress & peerAddress); |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 889 | void ReleaseCommissioneeDevice(CommissioneeDeviceProxy * device); |
| 890 | |
Boris Zbarsky | 7fe4abc | 2023-01-20 15:43:30 -0500 | [diff] [blame] | 891 | template <typename RequestObjectT> |
Boris Zbarsky | 43a1e1a | 2022-01-29 04:13:17 -0500 | [diff] [blame] | 892 | CHIP_ERROR SendCommand(DeviceProxy * device, const RequestObjectT & request, |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 893 | CommandResponseSuccessCallback<typename RequestObjectT::ResponseType> successCb, |
Boris Zbarsky | b27420a | 2022-06-03 19:50:39 -0400 | [diff] [blame] | 894 | CommandResponseFailureCallback failureCb, Optional<System::Clock::Timeout> timeout) |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 895 | { |
Boris Zbarsky | 7fe4abc | 2023-01-20 15:43:30 -0500 | [diff] [blame] | 896 | return SendCommand(device, request, successCb, failureCb, 0, timeout); |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 897 | } |
| 898 | |
Boris Zbarsky | 7fe4abc | 2023-01-20 15:43:30 -0500 | [diff] [blame] | 899 | template <typename RequestObjectT> |
Boris Zbarsky | 43a1e1a | 2022-01-29 04:13:17 -0500 | [diff] [blame] | 900 | CHIP_ERROR SendCommand(DeviceProxy * device, const RequestObjectT & request, |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 901 | CommandResponseSuccessCallback<typename RequestObjectT::ResponseType> successCb, |
Boris Zbarsky | 43a1e1a | 2022-01-29 04:13:17 -0500 | [diff] [blame] | 902 | CommandResponseFailureCallback failureCb, EndpointId endpoint, Optional<System::Clock::Timeout> timeout) |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 903 | { |
Boris Zbarsky | 7fe4abc | 2023-01-20 15:43:30 -0500 | [diff] [blame] | 904 | ClusterBase cluster(*device->GetExchangeManager(), device->GetSecureSession().Value(), endpoint); |
Boris Zbarsky | 43a1e1a | 2022-01-29 04:13:17 -0500 | [diff] [blame] | 905 | cluster.SetCommandTimeout(timeout); |
Vivien Nicolas | f24e6f1 | 2022-01-29 08:31:44 +0100 | [diff] [blame] | 906 | |
| 907 | return cluster.InvokeCommand(request, this, successCb, failureCb); |
| 908 | } |
C Freeman | e86bb35 | 2021-05-26 19:19:40 -0400 | [diff] [blame] | 909 | |
Boris Zbarsky | cccce01 | 2023-01-18 11:27:01 -0500 | [diff] [blame] | 910 | static CHIP_ERROR |
| 911 | ConvertFromOperationalCertStatus(chip::app::Clusters::OperationalCredentials::NodeOperationalCertStatusEnum err); |
C Freeman | e86bb35 | 2021-05-26 19:19:40 -0400 | [diff] [blame] | 912 | |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 913 | // Sends commissioning complete callbacks to the delegate depending on the status. Sends |
| 914 | // OnCommissioningComplete and either OnCommissioningSuccess or OnCommissioningFailure depending on the given completion status. |
| 915 | void SendCommissioningCompleteCallbacks(NodeId nodeId, const CompletionStatus & completionStatus); |
| 916 | |
| 917 | // Cleans up and resets failsafe as appropriate depending on the error and the failed stage. |
| 918 | // For success, sends completion report with the CommissioningDelegate and sends callbacks to the PairingDelegate |
| 919 | // For failures after AddNOC succeeds, sends completion report with the CommissioningDelegate and sends callbacks to the |
| 920 | // PairingDelegate. In this case, it does not disarm the failsafe or close the pase connection. For failures up through AddNOC, |
| 921 | // sends a command to immediately expire the failsafe, then sends completion report with the CommissioningDelegate and callbacks |
| 922 | // to the PairingDelegate upon arm failsafe command completion. |
| 923 | void CleanupCommissioning(DeviceProxy * proxy, NodeId nodeId, const CompletionStatus & completionStatus); |
| 924 | |
Boris Zbarsky | e42b533 | 2023-03-14 00:13:25 -0400 | [diff] [blame] | 925 | // Extend the fail-safe before trying to do network-enable (since after that |
| 926 | // point, for non-concurrent-commissioning devices, we may not have a way to |
| 927 | // extend it). |
| 928 | void ExtendFailsafeBeforeNetworkEnable(DeviceProxy * device, CommissioningParameters & params, CommissioningStage step); |
| 929 | |
C Freeman | 1ee9366 | 2022-02-03 09:51:04 -0500 | [diff] [blame] | 930 | chip::Callback::Callback<OnDeviceConnected> mOnDeviceConnectedCallback; |
| 931 | chip::Callback::Callback<OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback; |
Boris Zbarsky | 8afc1f5 | 2023-03-09 23:58:40 -0500 | [diff] [blame] | 932 | #if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES |
| 933 | chip::Callback::Callback<OnDeviceConnectionRetry> mOnDeviceConnectionRetryCallback; |
| 934 | #endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES |
Pankaj Garg | da45346 | 2021-06-22 19:53:34 -0700 | [diff] [blame] | 935 | |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 936 | chip::Callback::Callback<Credentials::DeviceAttestationVerifier::OnAttestationInformationVerification> |
| 937 | mDeviceAttestationInformationVerificationCallback; |
Vijay Selvaraj | b8c540a | 2022-01-05 11:58:12 -0500 | [diff] [blame] | 938 | |
C Freeman | 1ee9366 | 2022-02-03 09:51:04 -0500 | [diff] [blame] | 939 | chip::Callback::Callback<OnNOCChainGeneration> mDeviceNOCChainCallback; |
Vivien Nicolas | 02d8900 | 2021-10-04 12:30:52 +0200 | [diff] [blame] | 940 | SetUpCodePairer mSetUpCodePairer; |
C Freeman | 798d7d2 | 2021-12-10 12:06:36 -0500 | [diff] [blame] | 941 | AutoCommissioner mAutoCommissioner; |
C Freeman | 8ffe422 | 2022-02-02 14:21:16 -0500 | [diff] [blame] | 942 | CommissioningDelegate * mDefaultCommissioner = |
| 943 | nullptr; // Commissioning delegate to call when PairDevice / Commission functions are used |
| 944 | CommissioningDelegate * mCommissioningDelegate = |
| 945 | nullptr; // Commissioning delegate that issued the PerformCommissioningStep command |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 946 | CompletionStatus commissioningCompletionStatus; |
C Freeman | 1ee9366 | 2022-02-03 09:51:04 -0500 | [diff] [blame] | 947 | |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 948 | Platform::UniquePtr<app::ClusterStateCache> mAttributeCache; |
C Freeman | 1ee9366 | 2022-02-03 09:51:04 -0500 | [diff] [blame] | 949 | Platform::UniquePtr<app::ReadClient> mReadClient; |
Anush Nadathur | b06bd8c | 2022-04-06 17:29:16 -0700 | [diff] [blame] | 950 | Credentials::AttestationVerificationResult mAttestationResult; |
Jeff Tung | 89f5c8a | 2022-09-06 18:30:08 -0700 | [diff] [blame] | 951 | Platform::UniquePtr<Credentials::DeviceAttestationVerifier::AttestationDeviceInfo> mAttestationDeviceInfo; |
Tennessee Carmel-Veilleux | a777a80 | 2022-05-17 22:56:37 -0400 | [diff] [blame] | 952 | Credentials::DeviceAttestationVerifier * mDeviceAttestationVerifier = nullptr; |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 953 | }; |
| 954 | |
Pankaj Garg | 047424f | 2020-11-10 21:56:59 -0800 | [diff] [blame] | 955 | } // namespace Controller |
Sagar Dhawan | 55ef62f | 2020-05-13 20:35:27 -0700 | [diff] [blame] | 956 | } // namespace chip |