| /* | |
| * | |
| * Copyright (c) 2021-2024 Project CHIP Authors | |
| * All rights reserved. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include <app/InteractionModelTimeout.h> | |
| #include <controller/AutoCommissioner.h> | |
| #include <controller/CHIPDeviceController.h> | |
| #include <credentials/CHIPCert.h> | |
| #include <lib/support/SafeInt.h> | |
| #include <cinttypes> | |
| #include <cstring> | |
| #include <type_traits> | |
| namespace chip { | |
| namespace Controller { | |
| using namespace chip::app::Clusters; | |
| using namespace chip::Crypto; | |
| using chip::app::DataModel::MakeNullable; | |
| using chip::app::DataModel::NullNullable; | |
| namespace Internal { | |
| AttestationProfileBitmap GetControllerSupportedAttestationRequestProfiles() | |
| { | |
| AttestationProfileBitmap profiles(OperationalCredentials::AttestationCryptoProfileBitmap::kSupportsEcdsaMatterLegacy); | |
| if (IsMlDsa44Supported()) | |
| { | |
| profiles.Set(OperationalCredentials::AttestationCryptoProfileBitmap::kSupportsMlDsa44); | |
| } | |
| if (IsMlDsa65Supported()) | |
| { | |
| profiles.Set(OperationalCredentials::AttestationCryptoProfileBitmap::kSupportsMlDsa65); | |
| } | |
| return profiles; | |
| } | |
| std::optional<Credentials::DeviceAttestationChainProfile> | |
| SelectAttestationCertificateChain(AttestationProfileBitmap paa, AttestationProfileBitmap pai, AttestationProfileBitmap dac, | |
| AttestationProfileBitmap localProfiles, | |
| std::optional<Credentials::DeviceAttestationChainProfile> previous) | |
| { | |
| using Profile = OperationalCredentials::AttestationCryptoProfileEnum; | |
| using Bitmap = OperationalCredentials::AttestationCryptoProfileBitmap; | |
| if (!dac.Has(Bitmap::kSupportsEcdsaMatterLegacy) || !localProfiles.Has(Bitmap::kSupportsEcdsaMatterLegacy)) | |
| { | |
| return std::nullopt; | |
| } | |
| for (auto paaProfile : { Profile::kMlDsa65, Profile::kMlDsa44, Profile::kEcdsaMatterLegacy }) | |
| { | |
| const auto paaBit = static_cast<Bitmap>(1u << to_underlying(paaProfile)); | |
| if (!paa.Has(paaBit) || !localProfiles.Has(paaBit)) | |
| { | |
| continue; | |
| } | |
| for (auto paiProfile : { Profile::kMlDsa65, Profile::kMlDsa44, Profile::kEcdsaMatterLegacy }) | |
| { | |
| if (previous.has_value() && | |
| (paaProfile > previous->PAACrypto || (paaProfile == previous->PAACrypto && paiProfile >= previous->PAICrypto))) | |
| { | |
| continue; | |
| } | |
| const auto paiBit = static_cast<Bitmap>(1u << to_underlying(paiProfile)); | |
| if (paiProfile <= paaProfile && pai.Has(paiBit) && localProfiles.Has(paiBit)) | |
| { | |
| return Credentials::DeviceAttestationChainProfile{ paaProfile, paiProfile, Profile::kEcdsaMatterLegacy }; | |
| } | |
| } | |
| } | |
| return std::nullopt; | |
| } | |
| } // namespace Internal | |
| AutoCommissioner::AutoCommissioner() | |
| { | |
| TEMPORARY_RETURN_IGNORED AutoCommissioner::SetCommissioningParameters(CommissioningParameters()); | |
| } | |
| AutoCommissioner::~AutoCommissioner() {} | |
| void AutoCommissioner::SetOperationalCredentialsDelegate(OperationalCredentialsDelegate * operationalCredentialsDelegate) | |
| { | |
| mOperationalCredentialsDelegate = operationalCredentialsDelegate; | |
| } | |
| CHIP_ERROR AutoCommissioner::VerifyICDRegistrationInfo(const CommissioningParameters & params) | |
| { | |
| ChipLogProgress(Controller, "Checking ICD registration parameters"); | |
| if (!params.GetICDSymmetricKey().HasValue()) | |
| { | |
| ChipLogError(Controller, "Missing ICD symmetric key!"); | |
| return CHIP_ERROR_INVALID_ARGUMENT; | |
| } | |
| if (params.GetICDSymmetricKey().Value().size() != sizeof(mICDSymmetricKey)) | |
| { | |
| ChipLogError(Controller, "Invalid ICD symmetric key length!"); | |
| return CHIP_ERROR_INVALID_ARGUMENT; | |
| } | |
| if (!params.GetICDCheckInNodeId().HasValue()) | |
| { | |
| ChipLogError(Controller, "Missing ICD check-in node id!"); | |
| return CHIP_ERROR_INVALID_ARGUMENT; | |
| } | |
| if (!params.GetICDMonitoredSubject().HasValue()) | |
| { | |
| ChipLogError(Controller, "Missing ICD monitored subject!"); | |
| return CHIP_ERROR_INVALID_ARGUMENT; | |
| } | |
| if (!params.GetICDClientType().HasValue()) | |
| { | |
| ChipLogError(Controller, "Missing ICD Client Type!"); | |
| return CHIP_ERROR_INVALID_ARGUMENT; | |
| } | |
| return CHIP_NO_ERROR; | |
| } | |
| namespace { | |
| // Copies the contents of a Span into our own buffer and updates the Span | |
| template <typename T, size_t N> | |
| CHIP_ERROR RelocateSpan(Span<const T> & inOutSpan, T (&buffer)[N], bool exactSize = false) | |
| { | |
| static_assert(std::is_trivially_copyable_v<T>); | |
| size_t actualSize = inOutSpan.size(); | |
| VerifyOrReturnError(exactSize ? actualSize == N : actualSize <= N, CHIP_ERROR_INVALID_ARGUMENT); | |
| if (actualSize > 0) // data() can be nullptr if size() == 0, and memmove(buffer, nullptr, 0) would be UB | |
| { | |
| memmove(buffer, inOutSpan.data(), actualSize * sizeof(T)); | |
| } | |
| inOutSpan = Span(buffer, actualSize); | |
| return CHIP_NO_ERROR; | |
| } | |
| } // namespace | |
| CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParameters & params) | |
| { | |
| // Our logic below assumes that we can modify mParams without affecting params. | |
| VerifyOrReturnError(¶ms != &mParams, CHIP_NO_ERROR); | |
| // Copy the whole struct (scalars and pointers), but clear any members that might point to | |
| // external buffers. For those members we have to copy the data over into our own buffers below. | |
| // Note that all of the copy operations use memmove() instead of memcpy(), because the caller | |
| // may be passing a modified shallow copy of our CommissioningParmeters, i.e. where various spans | |
| // already point into the buffers we're copying into, and memcpy() with overlapping buffers is UB. | |
| // | |
| // Note: Only the parameters that are inputs are copied from params below. Output-only | |
| // parameters that we populate ourselves as commissioning progresses (the generated NOC chain, | |
| // the PDC network and client identities, the attestation elements) are cleared here and left | |
| // for the stage that produces them to set again. | |
| // | |
| // Dropping them is safe because the delegate only gets to call us back mid-commissioning at | |
| // kICDGetRegistrationInfo and kNeedsNetworkCreds, both of which are past kSendNOC: the attestation | |
| // values were consumed at kAttestationVerification, and the CSR and NOC chain by kSendNOC, while the | |
| // PDC values are not produced until the network setup stages that follow. A retry walks the flow back | |
| // to kScanNetworks, but CommissioningStepFinished() clears the PDC parameters before the delegate is | |
| // asked for credentials again, so there is nothing stale to carry forward either. State that does have | |
| // to outlive a rewrite is deliberately kept out of CommissioningParameters: mDeviceCommissioningInfo | |
| // and the network attempt type here, and the pending Network Client Identity rollback in the | |
| // DeviceCommissioner. | |
| // | |
| // Note this assignment also replaces the scalar parameters, including ones we derived from the | |
| // commissionee ourselves. Of those only the failsafe timer is read again after a delegate callback | |
| // (at kFailsafeBeforeWiFiEnable / kFailsafeBeforeThreadEnable), and losing it there is harmless: the | |
| // fail-safe is already armed with the recommended value and never gets shortened. | |
| mParams = params; | |
| mParams.ClearExternalBufferDependentValues(); | |
| if (params.GetThreadOperationalDataset().HasValue()) | |
| { | |
| ByteSpan dataset = params.GetThreadOperationalDataset().Value(); | |
| ReturnErrorOnFailure(RelocateSpan(dataset, mThreadOperationalDataset), | |
| ChipLogError(Controller, "Thread operational data set is too large")); | |
| ChipLogProgress(Controller, "Setting thread operational dataset from parameters"); | |
| mParams.SetThreadOperationalDataset(dataset); | |
| } | |
| auto wiFiCredentialsParam = params.GetWiFiCredentials(); // optional copied by value | |
| if (wiFiCredentialsParam.HasValue()) | |
| { | |
| WiFiCredentials & creds = wiFiCredentialsParam.Value(); | |
| ReturnErrorOnFailure(RelocateSpan(creds.ssid, mSsid), // | |
| ChipLogError(Controller, "WiFiCredentials.ssid is too large")); | |
| ReturnErrorOnFailure(RelocateSpan(creds.credentials, mCredentials), | |
| ChipLogError(Controller, "WiFiCredentials.credentials is too large")); | |
| ChipLogProgress(Controller, "Setting wifi credentials from parameters"); | |
| mParams.SetWiFiCredentials(creds); | |
| } | |
| if (params.GetCountryCode().HasValue()) | |
| { | |
| CharSpan countryCode = params.GetCountryCode().Value(); | |
| ReturnErrorOnFailure(RelocateSpan(countryCode, mCountryCode), ChipLogError(Controller, "Country code is too large")); | |
| mParams.SetCountryCode(countryCode); | |
| } | |
| // If the AttestationNonce is passed in, using that else using a random one.. | |
| if (params.GetAttestationNonce().HasValue()) | |
| { | |
| ByteSpan attestationNonce = params.GetAttestationNonce().Value(); | |
| ReturnErrorOnFailure(RelocateSpan(attestationNonce, mAttestationNonce, /* exactSize = */ true), | |
| ChipLogError(Controller, "Attestation nonce length is invalid")); | |
| ChipLogProgress(Controller, "Setting attestation nonce from parameters"); | |
| mParams.SetAttestationNonce(attestationNonce); | |
| } | |
| else | |
| { | |
| ChipLogProgress(Controller, "Setting attestation nonce to random value"); | |
| ReturnErrorOnFailure(Crypto::DRBG_get_bytes(mAttestationNonce, sizeof(mAttestationNonce))); | |
| mParams.SetAttestationNonce(ByteSpan(mAttestationNonce)); | |
| } | |
| if (params.GetCSRNonce().HasValue()) | |
| { | |
| ByteSpan csrNonce = params.GetCSRNonce().Value(); | |
| ReturnErrorOnFailure(RelocateSpan(csrNonce, mCSRNonce, /* exactSize = */ true), | |
| ChipLogError(Controller, "CSR nonce length is invalid")); | |
| ChipLogProgress(Controller, "Setting CSR nonce from parameters"); | |
| mParams.SetCSRNonce(csrNonce); | |
| } | |
| else | |
| { | |
| ChipLogProgress(Controller, "Setting CSR nonce to random value"); | |
| ReturnErrorOnFailure(Crypto::DRBG_get_bytes(mCSRNonce, sizeof(mCSRNonce))); | |
| mParams.SetCSRNonce(ByteSpan(mCSRNonce)); | |
| } | |
| // Unlike the CSR nonce above, we only copy a PDC possession nonce that was actually supplied; the | |
| // fallback to a random value happens lazily in kPDCGetNetworkIdentity, i.e. only once we know we | |
| // are going to use PDC at all. Leaving mParams without a nonce here is what signals that. | |
| if (params.GetPDCPossessionNonce().HasValue()) | |
| { | |
| ByteSpan possessionNonce = params.GetPDCPossessionNonce().Value(); | |
| ReturnErrorOnFailure(RelocateSpan(possessionNonce, mPossessionNonce, /* exactSize = */ true), | |
| ChipLogError(Controller, "PDC possession nonce length is invalid")); | |
| ChipLogProgress(Controller, "Setting PDC possession nonce from parameters"); | |
| mParams.SetPDCPossessionNonce(possessionNonce); | |
| } | |
| if (params.GetDSTOffsets().HasValue()) | |
| { | |
| ChipLogProgress(Controller, "Setting DST offsets from parameters"); | |
| size_t size = std::min(params.GetDSTOffsets().Value().size(), kMaxSupportedDstStructs); | |
| for (size_t i = 0; i < size; ++i) | |
| { | |
| mDstOffsetsBuf[i] = params.GetDSTOffsets().Value()[i]; | |
| } | |
| auto list = app::DataModel::List<app::Clusters::TimeSynchronization::Structs::DSTOffsetStruct::Type>(mDstOffsetsBuf, size); | |
| mParams.SetDSTOffsets(list); | |
| } | |
| if (params.GetTimeZone().HasValue()) | |
| { | |
| ChipLogProgress(Controller, "Setting Time Zone from parameters"); | |
| size_t size = std::min(params.GetTimeZone().Value().size(), kMaxSupportedTimeZones); | |
| for (size_t i = 0; i < size; ++i) | |
| { | |
| mTimeZoneBuf[i] = params.GetTimeZone().Value()[i]; | |
| if (params.GetTimeZone().Value()[i].name.HasValue() && | |
| params.GetTimeZone().Value()[i].name.Value().size() <= kMaxTimeZoneNameLen) | |
| { | |
| auto span = MutableCharSpan(mTimeZoneNames[i], kMaxTimeZoneNameLen); | |
| // The buffer backing "span" is statically allocated and is of size kMaxSupportedTimeZones, so this should never | |
| // fail. | |
| TEMPORARY_RETURN_IGNORED CopyCharSpanToMutableCharSpan(params.GetTimeZone().Value()[i].name.Value(), span); | |
| mTimeZoneBuf[i].name.SetValue(span); | |
| } | |
| else | |
| { | |
| mTimeZoneBuf[i].name.ClearValue(); | |
| } | |
| } | |
| auto list = app::DataModel::List<app::Clusters::TimeSynchronization::Structs::TimeZoneStruct::Type>(mTimeZoneBuf, size); | |
| mParams.SetTimeZone(list); | |
| } | |
| if (params.GetDefaultNTP().HasValue()) | |
| { | |
| ChipLogProgress(Controller, "Setting Default NTP from parameters"); | |
| // This parameter is an optional nullable, so we need to go two levels deep here. | |
| if (!params.GetDefaultNTP().Value().IsNull() && params.GetDefaultNTP().Value().Value().size() <= kMaxDefaultNtpSize) | |
| { | |
| // The buffer backing "span" is statically allocated and is of size kMaxDefaultNtpSize. | |
| auto span = MutableCharSpan(mDefaultNtp, kMaxDefaultNtpSize); | |
| TEMPORARY_RETURN_IGNORED CopyCharSpanToMutableCharSpan(params.GetDefaultNTP().Value().Value(), span); | |
| auto default_ntp = MakeNullable(CharSpan(mDefaultNtp, params.GetDefaultNTP().Value().Value().size())); | |
| mParams.SetDefaultNTP(default_ntp); | |
| } | |
| } | |
| if (params.GetICDRegistrationStrategy() != ICDRegistrationStrategy::kIgnore && params.GetICDSymmetricKey().HasValue()) | |
| { | |
| ReturnErrorOnFailure(VerifyICDRegistrationInfo(params)); | |
| // The values must be valid now. | |
| memmove(mICDSymmetricKey, params.GetICDSymmetricKey().Value().data(), params.GetICDSymmetricKey().Value().size()); | |
| mParams.SetICDSymmetricKey(ByteSpan(mICDSymmetricKey)); | |
| mParams.SetICDCheckInNodeId(params.GetICDCheckInNodeId().Value()); | |
| mParams.SetICDMonitoredSubject(params.GetICDMonitoredSubject().Value()); | |
| mParams.SetICDClientType(params.GetICDClientType().Value()); | |
| } | |
| auto extraReadPaths = params.GetExtraReadPaths(); | |
| if (extraReadPaths.size() > 0) | |
| { | |
| using ReadPath = std::remove_pointer_t<decltype(extraReadPaths.data())>; | |
| static_assert(std::is_trivially_copyable_v<ReadPath>, "can't use memmove / memcpy, not trivially copyable"); | |
| if (mExtraReadPaths.AllocatedSize() == extraReadPaths.size()) | |
| { | |
| memmove(mExtraReadPaths.Get(), extraReadPaths.data(), extraReadPaths.size() * sizeof(ReadPath)); | |
| } | |
| else | |
| { | |
| // We can't reallocate mExtraReadPaths yet as this would free the old buffer, | |
| // and the caller might be passing a sub-span of the old paths. | |
| decltype(mExtraReadPaths) oldReadPaths(std::move(mExtraReadPaths)); | |
| VerifyOrReturnError(mExtraReadPaths.Alloc(extraReadPaths.size()), CHIP_ERROR_NO_MEMORY); | |
| memcpy(mExtraReadPaths.Get(), extraReadPaths.data(), extraReadPaths.size() * sizeof(ReadPath)); | |
| } | |
| mParams.SetExtraReadPaths(mExtraReadPaths.Span()); | |
| } | |
| else | |
| { | |
| mExtraReadPaths.Free(); | |
| } | |
| return CHIP_NO_ERROR; | |
| } | |
| const CommissioningParameters & AutoCommissioner::GetCommissioningParameters() const | |
| { | |
| return mParams; | |
| } | |
| void AutoCommissioner::ClearPDCParameters() | |
| { | |
| mParams.ClearPDCNetworkIdentity(); | |
| mParams.ClearPDCPossessionNonce(); | |
| mParams.ClearPDCClientIdentity(); | |
| mParams.ClearPDCPossessionSignature(); | |
| } | |
| CommissioningStage AutoCommissioner::GetNextCommissioningStage(CommissioningStage currentStage, CHIP_ERROR & lastErr) | |
| { | |
| auto nextStage = GetNextCommissioningStageInternal(currentStage, lastErr); | |
| if (lastErr == CHIP_NO_ERROR) | |
| { | |
| ChipLogProgress(Controller, "Commissioning stage next step: '%s' -> '%s'", StageToString(currentStage), | |
| StageToString(nextStage)); | |
| } | |
| else | |
| { | |
| ChipLogProgress(Controller, "Going from commissioning step '%s' with lastErr = '%s' -> '%s'", StageToString(currentStage), | |
| lastErr.AsString(), StageToString(nextStage)); | |
| } | |
| return nextStage; | |
| } | |
| CommissioningStage AutoCommissioner::GetNextCommissioningStageNetworkSetup(CommissioningStage currentStage, CHIP_ERROR & lastErr) | |
| { | |
| if (!IsSomeNetworkSupported()) | |
| { | |
| ChipLogError(Controller, "Network setup is needed, but commissionee does not support any network types we know about"); | |
| lastErr = CHIP_ERROR_INCORRECT_STATE; | |
| return CommissioningStage::kCleanup; | |
| } | |
| enum class NetworkType | |
| { | |
| kWiFi, | |
| kThread, | |
| }; | |
| NetworkType networkToUse; | |
| if (TryingSecondaryNetwork()) | |
| { | |
| // Try secondary network interface. | |
| networkToUse = | |
| mDeviceCommissioningInfo.network.wifi.endpoint == kRootEndpointId ? NetworkType::kThread : NetworkType::kWiFi; | |
| } | |
| else if (IsSecondaryNetworkSupported()) | |
| { | |
| // Try primary network interface. | |
| TryPrimaryNetwork(); | |
| networkToUse = | |
| mDeviceCommissioningInfo.network.wifi.endpoint == kRootEndpointId ? NetworkType::kWiFi : NetworkType::kThread; | |
| } | |
| else if (mParams.GetWiFiCredentials().HasValue() && mDeviceCommissioningInfo.network.wifi.endpoint != kInvalidEndpointId) | |
| { | |
| networkToUse = NetworkType::kWiFi; | |
| } | |
| else if (mParams.GetThreadOperationalDataset().HasValue() && | |
| mDeviceCommissioningInfo.network.thread.endpoint != kInvalidEndpointId) | |
| { | |
| networkToUse = NetworkType::kThread; | |
| } | |
| else | |
| { | |
| // If we have ended up here, then the commissionee does not support trying two different | |
| // network technologies. If it did, then either we would have credentials for one of them | |
| // and take one of those branches, or we would not have credentials for either one and | |
| // IsSecondaryNetworkSupported() would have tested true. | |
| // | |
| // Also, since we got here, we don't (yet) have credentials for the one network technology | |
| // the commissionee supports. Go ahead and just try to use that one network technology, | |
| // which should be the primary network (on the root endpoint). | |
| // | |
| // Note that we don't call TryPrimaryNetwork() here, because that's only used when there | |
| // will be a secondary network to try. Which in this case there isn't. | |
| networkToUse = | |
| mDeviceCommissioningInfo.network.wifi.endpoint == kRootEndpointId ? NetworkType::kWiFi : NetworkType::kThread; | |
| } | |
| if (networkToUse == NetworkType::kWiFi) | |
| { | |
| // We need credentials, request them if necessary. | |
| auto wiFiCredentialsParam = mParams.GetWiFiCredentials(); // optional copied by value | |
| VerifyOrReturnValue(wiFiCredentialsParam.HasValue(), CommissioningStage::kRequestWiFiCredentials); | |
| auto & credentials = wiFiCredentialsParam.Value(); | |
| if (credentials.registrar != nullptr) | |
| { | |
| if (mDeviceCommissioningInfo.network.wifi.supportsPerDeviceCredentials) | |
| { | |
| // We will use PDC, so we need to obtain the Network Identity if we don't have it yet. | |
| VerifyOrReturnValue(mParams.GetPDCNetworkIdentity().HasValue(), CommissioningStage::kPDCGetNetworkIdentity); | |
| } | |
| else if (credentials.hasCredentials) | |
| { | |
| ChipLogProgress(Controller, "Commissionee does not support PDC, using plain Wi-Fi credentials instead"); | |
| } | |
| else | |
| { | |
| // Nothing to configure the commissionee with. Fall through to kWiFiNetworkSetup and let it | |
| // fail there, so that this counts as a network setup failure and we reach the secondary | |
| // network (if any) via the normal failover path. | |
| ChipLogError(Controller, "Commissionee does not support PDC and no plain Wi-Fi credentials are available"); | |
| } | |
| } | |
| return CommissioningStage::kWiFiNetworkSetup; | |
| } | |
| // networkToUse must be kThread here. | |
| if (mParams.GetThreadOperationalDataset().HasValue()) | |
| { | |
| // Just go ahead and set that up. | |
| return CommissioningStage::kThreadNetworkSetup; | |
| } | |
| // We need credentials but don't have them. We need to ask for those. | |
| return CommissioningStage::kRequestThreadCredentials; | |
| } | |
| CommissioningStage AutoCommissioner::GetNextCommissioningStageInternal(CommissioningStage currentStage, CHIP_ERROR & lastErr) | |
| { | |
| if (mStopCommissioning) | |
| { | |
| return CommissioningStage::kCleanup; | |
| } | |
| if (lastErr != CHIP_NO_ERROR) | |
| { | |
| return CommissioningStage::kCleanup; | |
| } | |
| switch (currentStage) | |
| { | |
| case CommissioningStage::kSecurePairing: | |
| return CommissioningStage::kReadCommissioningInfo; | |
| case CommissioningStage::kReadCommissioningInfo: | |
| if (mDeviceCommissioningInfo.general.breadcrumb > 0) | |
| { | |
| // If the breadcrumb is 0, the failsafe was disarmed. | |
| // We failed on network setup or later, the node failsafe has not been re-armed and the breadcrumb has not been reset. | |
| // Per the spec, we restart from after adding the NOC. | |
| return GetNextCommissioningStage(CommissioningStage::kSendNOC, lastErr); | |
| } | |
| return CommissioningStage::kArmFailSafe; | |
| case CommissioningStage::kArmFailSafe: | |
| return CommissioningStage::kConfigRegulatory; | |
| case CommissioningStage::kConfigRegulatory: | |
| return CommissioningStage::kConfigureTCAcknowledgments; | |
| case CommissioningStage::kConfigureTCAcknowledgments: | |
| if (mDeviceCommissioningInfo.requiresUTC) | |
| { | |
| return CommissioningStage::kConfigureUTCTime; | |
| } | |
| // Time cluster is not supported, move right to DA | |
| return CommissioningStage::kSendPAICertificateRequest; | |
| case CommissioningStage::kConfigureUTCTime: | |
| if (mDeviceCommissioningInfo.requiresTimeZone && mParams.GetTimeZone().HasValue()) | |
| { | |
| return kConfigureTimeZone; | |
| } | |
| return GetNextCommissioningStageInternal(CommissioningStage::kConfigureTimeZone, lastErr); | |
| case CommissioningStage::kConfigureTimeZone: | |
| if (mNeedsDST && mParams.GetDSTOffsets().HasValue()) | |
| { | |
| return CommissioningStage::kConfigureDSTOffset; | |
| } | |
| return GetNextCommissioningStageInternal(CommissioningStage::kConfigureDSTOffset, lastErr); | |
| case CommissioningStage::kConfigureDSTOffset: | |
| if (mDeviceCommissioningInfo.requiresDefaultNTP && mParams.GetDefaultNTP().HasValue()) | |
| { | |
| return CommissioningStage::kConfigureDefaultNTP; | |
| } | |
| return GetNextCommissioningStageInternal(CommissioningStage::kConfigureDefaultNTP, lastErr); | |
| case CommissioningStage::kConfigureDefaultNTP: | |
| return CommissioningStage::kSendPAICertificateRequest; | |
| case CommissioningStage::kSendPAICertificateRequest: | |
| return CommissioningStage::kSendDACCertificateRequest; | |
| case CommissioningStage::kSendDACCertificateRequest: | |
| return CommissioningStage::kSendAttestationRequest; | |
| case CommissioningStage::kSendAttestationRequest: | |
| return CommissioningStage::kAttestationVerification; | |
| case CommissioningStage::kAttestationVerification: | |
| return CommissioningStage::kAttestationRevocationCheck; | |
| case CommissioningStage::kAttestationRevocationCheck: | |
| #if CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC | |
| if (mParams.GetUseJCM().ValueOr(false)) | |
| { | |
| return CommissioningStage::kJCMTrustVerification; | |
| } | |
| #endif // CHIP_DEVICE_CONFIG_ENABLE_JOINT_FABRIC | |
| return CommissioningStage::kSendOpCertSigningRequest; | |
| case CommissioningStage::kJCMTrustVerification: | |
| return CommissioningStage::kSendOpCertSigningRequest; | |
| case CommissioningStage::kSendOpCertSigningRequest: | |
| return CommissioningStage::kValidateCSR; | |
| case CommissioningStage::kValidateCSR: | |
| return CommissioningStage::kGenerateNOCChain; | |
| case CommissioningStage::kGenerateNOCChain: | |
| return CommissioningStage::kSendTrustedRootCert; | |
| case CommissioningStage::kSendTrustedRootCert: | |
| return CommissioningStage::kSendNOC; | |
| case CommissioningStage::kSendNOC: | |
| if (mDeviceCommissioningInfo.requiresTrustedTimeSource && mParams.GetTrustedTimeSource().HasValue()) | |
| { | |
| return CommissioningStage::kConfigureTrustedTimeSource; | |
| } | |
| return GetNextCommissioningStageInternal(CommissioningStage::kConfigureTrustedTimeSource, lastErr); | |
| case CommissioningStage::kConfigureTrustedTimeSource: | |
| if (mNeedIcdRegistration) | |
| { | |
| if (mParams.GetICDCheckInNodeId().HasValue() && mParams.GetICDMonitoredSubject().HasValue() && | |
| mParams.GetICDSymmetricKey().HasValue()) | |
| { | |
| return CommissioningStage::kICDRegistration; | |
| } | |
| return CommissioningStage::kICDGetRegistrationInfo; | |
| } | |
| return GetNextCommissioningStageInternal(CommissioningStage::kICDRegistration, lastErr); | |
| case CommissioningStage::kICDGetRegistrationInfo: | |
| return CommissioningStage::kICDRegistration; | |
| case CommissioningStage::kICDRegistration: | |
| // TODO(cecille): device attestation casues operational cert provisioning to happen, This should be a separate stage. | |
| // For thread and wifi, this should go to network setup then enable. For on-network we can skip right to finding the | |
| // operational network because the provisioning of certificates will trigger the device to start operational advertising. | |
| if (mNeedsNetworkSetup) | |
| { | |
| // if there is a WiFi or a Thread endpoint, then perform scan | |
| if (IsScanNeeded()) | |
| { | |
| // Perform Scan (kScanNetworks) and collect credentials (kNeedsNetworkCreds) right before configuring network. | |
| // This order of steps allows the workflow to return to collect credentials again if network enablement fails. | |
| // TODO: This is broken when we have multiple network commissioning endpoints | |
| // We always end up doing the scan on endpoint 0, even if we have disabled that | |
| // endpoint and are trying to use the secondary network commissioning endpoint. | |
| // We should probably have separate stages for "scan Thread" and "scan Wi-Fi", which | |
| // would allow GetEndpoint() to do the right thing. The IsScanNeeded() check should | |
| // also be changed to check the actual endpoint we are going to try to commission | |
| // here, not just "all the endpoints". | |
| // | |
| // See https://github.com/project-chip/connectedhomeip/issues/40755 | |
| return CommissioningStage::kScanNetworks; | |
| } | |
| ChipLogProgress(Controller, "No NetworkScan enabled or WiFi/Thread endpoint not specified, skipping ScanNetworks"); | |
| return GetNextCommissioningStageNetworkSetup(currentStage, lastErr); | |
| } | |
| SetCASEFailsafeTimerIfNeeded(); | |
| if (mParams.GetSkipCommissioningComplete().ValueOr(false)) | |
| { | |
| return CommissioningStage::kCleanup; | |
| } | |
| return CommissioningStage::kEvictPreviousCaseSessions; | |
| case CommissioningStage::kScanNetworks: | |
| case CommissioningStage::kRequestWiFiCredentials: | |
| case CommissioningStage::kRequestThreadCredentials: | |
| return CommissioningStage::kNeedsNetworkCreds; | |
| case CommissioningStage::kNeedsNetworkCreds: | |
| return GetNextCommissioningStageNetworkSetup(currentStage, lastErr); | |
| case CommissioningStage::kPDCGetNetworkIdentity: | |
| // We now have the Network Identity, so this will select kWiFiNetworkSetup. | |
| return GetNextCommissioningStageNetworkSetup(currentStage, lastErr); | |
| case CommissioningStage::kWiFiNetworkSetup: | |
| if (mParams.GetPDCNetworkIdentity().HasValue()) | |
| { | |
| // We're configuring the commissionee for PDC, so the Network Client Identity it | |
| // provided from kWiFiNetworkSetup needs to be registered before ConnectNetwork. | |
| return CommissioningStage::kPDCRegisterClientIdentity; | |
| } | |
| return CommissioningStage::kFailsafeBeforeWiFiEnable; | |
| case CommissioningStage::kPDCRegisterClientIdentity: | |
| return CommissioningStage::kFailsafeBeforeWiFiEnable; | |
| case CommissioningStage::kThreadNetworkSetup: | |
| return CommissioningStage::kFailsafeBeforeThreadEnable; | |
| case CommissioningStage::kFailsafeBeforeWiFiEnable: | |
| return CommissioningStage::kWiFiNetworkEnable; | |
| case CommissioningStage::kFailsafeBeforeThreadEnable: | |
| return CommissioningStage::kThreadNetworkEnable; | |
| case CommissioningStage::kWiFiNetworkEnable: | |
| if (mParams.GetSkipCommissioningComplete().ValueOr(false)) | |
| { | |
| SetCASEFailsafeTimerIfNeeded(); | |
| return CommissioningStage::kCleanup; | |
| } | |
| SetCASEFailsafeTimerIfNeeded(); | |
| return CommissioningStage::kEvictPreviousCaseSessions; | |
| case CommissioningStage::kThreadNetworkEnable: | |
| SetCASEFailsafeTimerIfNeeded(); | |
| if (mParams.GetSkipCommissioningComplete().ValueOr(false)) | |
| { | |
| return CommissioningStage::kCleanup; | |
| } | |
| return CommissioningStage::kEvictPreviousCaseSessions; | |
| case CommissioningStage::kEvictPreviousCaseSessions: | |
| #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING | |
| { | |
| // If there is no secure session, means commissioning has been continued after the unpowered phase was completed | |
| // In such case, move on setup the CASE session over operational network | |
| if (!mCommissioneeDeviceProxy->GetSecureSession().HasValue()) | |
| { | |
| return CommissioningStage::kFindOperationalForStayActive; | |
| } | |
| // If the transport is NFC, move to unpowered phase complete to end the first phase of commissioning | |
| auto transportType = | |
| mCommissioneeDeviceProxy->GetSecureSession().Value()->AsSecureSession()->GetPeerAddress().GetTransportType(); | |
| return (transportType == Transport::Type::kNfc && mDeviceCommissioningInfo.general.isCommissioningWithoutPower) | |
| ? CommissioningStage::kUnpoweredPhaseComplete | |
| : CommissioningStage::kFindOperationalForStayActive; | |
| } | |
| #else | |
| return CommissioningStage::kFindOperationalForStayActive; | |
| #endif | |
| case CommissioningStage::kPrimaryOperationalNetworkFailed: | |
| if (!mWroteNetworkConfig) | |
| { | |
| // We never got as far as writing a network configuration for the primary network, | |
| // so there is nothing on the commissionee to remove. Note that TrySecondaryNetwork() | |
| // has already been called by the time we get here, so this selects the secondary network. | |
| return GetNextCommissioningStageNetworkSetup(currentStage, lastErr); | |
| } | |
| if (mDeviceCommissioningInfo.network.wifi.endpoint == kRootEndpointId) | |
| { | |
| return CommissioningStage::kRemoveWiFiNetworkConfig; | |
| } | |
| return CommissioningStage::kRemoveThreadNetworkConfig; | |
| case CommissioningStage::kRemoveWiFiNetworkConfig: | |
| case CommissioningStage::kRemoveThreadNetworkConfig: | |
| return GetNextCommissioningStageNetworkSetup(currentStage, lastErr); | |
| case CommissioningStage::kFindOperationalForStayActive: | |
| return CommissioningStage::kICDSendStayActive; | |
| case CommissioningStage::kICDSendStayActive: | |
| return CommissioningStage::kFindOperationalForCommissioningComplete; | |
| case CommissioningStage::kFindOperationalForCommissioningComplete: | |
| return CommissioningStage::kSendComplete; | |
| case CommissioningStage::kSendComplete: | |
| return CommissioningStage::kCleanup; | |
| case CommissioningStage::kUnpoweredPhaseComplete: | |
| return CommissioningStage::kCleanup; // End commissioning (phase 1) | |
| // Neither of these have a next stage so return kError; | |
| case CommissioningStage::kCleanup: | |
| case CommissioningStage::kError: | |
| return CommissioningStage::kError; | |
| } | |
| return CommissioningStage::kError; | |
| } | |
| // No specific actions to take when an error happens since this command can fail and commissioning can still succeed. | |
| static void OnFailsafeFailureForCASE(void * context, CHIP_ERROR error) | |
| { | |
| ChipLogProgress(Controller, "ExtendFailsafe received failure response: %" CHIP_ERROR_FORMAT, error.Format()); | |
| } | |
| // No specific actions to take upon success. | |
| static void | |
| OnExtendFailsafeSuccessForCASE(void * context, | |
| const app::Clusters::GeneralCommissioning::Commands::ArmFailSafeResponse::DecodableType & data) | |
| { | |
| ChipLogProgress(Controller, "ExtendFailsafe received ArmFailSafe response errorCode=%u", to_underlying(data.errorCode)); | |
| } | |
| void AutoCommissioner::SetCASEFailsafeTimerIfNeeded() | |
| { | |
| // if there is a final fail-safe timer configured then, send it | |
| if (mParams.GetCASEFailsafeTimerSeconds().HasValue() && mCommissioneeDeviceProxy != nullptr) | |
| { | |
| // send the command via the PASE session (mCommissioneeDeviceProxy) since the CASE portion of commissioning | |
| // might be done by a different service (ex. PASE is done by a phone app and CASE is done by a Hub). | |
| // Also, we want the CASE failsafe timer to apply for the time it takes the Hub to perform operational discovery, | |
| // CASE establishment, and receipt of the commissioning complete command. | |
| // We know that the mCommissioneeDeviceProxy is still valid at this point since it gets cleared during cleanup | |
| // and SetCASEFailsafeTimerIfNeeded is always called before that stage. | |
| // | |
| // A false return from ExtendArmFailSafe is fine; we don't want to make | |
| // the fail-safe shorter here. | |
| mCommissioner->ExtendArmFailSafe(mCommissioneeDeviceProxy, mCommissioner->GetCommissioningStage(), | |
| mParams.GetCASEFailsafeTimerSeconds().Value(), | |
| GetCommandTimeout(mCommissioneeDeviceProxy, CommissioningStage::kArmFailSafe), | |
| OnExtendFailsafeSuccessForCASE, OnFailsafeFailureForCASE); | |
| } | |
| } | |
| EndpointId AutoCommissioner::GetEndpoint(const CommissioningStage & stage) const | |
| { | |
| switch (stage) | |
| { | |
| case CommissioningStage::kRequestWiFiCredentials: | |
| case CommissioningStage::kWiFiNetworkSetup: | |
| case CommissioningStage::kWiFiNetworkEnable: | |
| return mDeviceCommissioningInfo.network.wifi.endpoint; | |
| case CommissioningStage::kRequestThreadCredentials: | |
| case CommissioningStage::kThreadNetworkSetup: | |
| case CommissioningStage::kThreadNetworkEnable: | |
| return mDeviceCommissioningInfo.network.thread.endpoint; | |
| case CommissioningStage::kRemoveWiFiNetworkConfig: | |
| case CommissioningStage::kRemoveThreadNetworkConfig: | |
| return kRootEndpointId; | |
| case CommissioningStage::kPDCGetNetworkIdentity: | |
| case CommissioningStage::kPDCRegisterClientIdentity: | |
| return kInvalidEndpointId; // interact with the NetworkIdentityRegistrar, not with the commissionee | |
| default: | |
| return kRootEndpointId; | |
| } | |
| } | |
| CHIP_ERROR AutoCommissioner::StartCommissioning(DeviceCommissioner * commissioner, CommissioneeDeviceProxy * proxy) | |
| { | |
| if (commissioner == nullptr) | |
| { | |
| ChipLogError(Controller, "Invalid DeviceCommissioner"); | |
| return CHIP_ERROR_INVALID_ARGUMENT; | |
| } | |
| // Proxy is expected to have a valid secure session before starting to commission. However, in case of continuing | |
| // commissioning post unpowered phase, allow proceeding if the stage is evict secure CASE sessions | |
| if (proxy == nullptr || | |
| (!proxy->GetSecureSession().HasValue() | |
| #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING | |
| && commissioner->GetCommissioningStage() != CommissioningStage::kEvictPreviousCaseSessions | |
| #endif | |
| )) | |
| { | |
| ChipLogError(Controller, "Device proxy secure session error"); | |
| return CHIP_ERROR_INVALID_ARGUMENT; | |
| } | |
| mStopCommissioning = false; | |
| mCommissioner = commissioner; | |
| mCommissioneeDeviceProxy = proxy; | |
| // When commissioning is started after unpowered phase, there will be no secure session since the CASE session first needs to be | |
| // setup over operational network. Hence assume transport is UDP in such case. | |
| auto transportType = Transport::Type::kUdp; | |
| if (mCommissioneeDeviceProxy->GetSecureSession().HasValue()) | |
| { | |
| transportType = | |
| mCommissioneeDeviceProxy->GetSecureSession().Value()->AsSecureSession()->GetPeerAddress().GetTransportType(); | |
| } | |
| // mNeedsNetworkSetup may be set by SetNetworkSetupNeeded(). | |
| mNeedsNetworkSetup = mNeedsNetworkSetup || (transportType == Transport::Type::kBle); | |
| #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING | |
| mNeedsNetworkSetup = mNeedsNetworkSetup || (transportType == Transport::Type::kNfc); | |
| #endif | |
| #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF | |
| mNeedsNetworkSetup = mNeedsNetworkSetup || (transportType == Transport::Type::kWiFiPAF); | |
| #endif | |
| // For proxy transport, the commissioner tunnels commissioning packets to the | |
| // end device via the Commissioning Proxy cluster. The end device still needs | |
| // WiFi/Thread credentials, so run network-setup stages when any credentials | |
| // were provided. Without credentials the stages are skipped rather than | |
| // requested: kRequestWiFiCredentials leads to kNeedsNetworkCreds, which waits | |
| // for DevicePairingDelegate::NetworkCredentialsReady(), and the proxy | |
| // commissioning path has no delegate wired up to supply them. | |
| mNeedsNetworkSetup = mNeedsNetworkSetup || | |
| (transportType == Transport::Type::kProxy && | |
| (mParams.GetWiFiCredentials().HasValue() || mParams.GetThreadOperationalDataset().HasValue())); | |
| CHIP_ERROR err = CHIP_NO_ERROR; | |
| CommissioningStage nextStage = GetNextCommissioningStage(commissioner->GetCommissioningStage(), err); | |
| mCommissioner->PerformCommissioningStep(mCommissioneeDeviceProxy, nextStage, mParams, this, GetEndpoint(nextStage), | |
| GetCommandTimeout(mCommissioneeDeviceProxy, nextStage)); | |
| return CHIP_NO_ERROR; | |
| } | |
| Optional<System::Clock::Timeout> AutoCommissioner::GetCommandTimeout(DeviceProxy * device, CommissioningStage stage) const | |
| { | |
| // Network clusters can indicate the time required to connect, so if we are | |
| // connecting, use that time as our "how long it takes to process server | |
| // side" time. Otherwise pick a time that should be enough for the command | |
| // processing: 7s for slow steps that can involve crypto, the default IM | |
| // timeout otherwise. | |
| // TODO: is this a reasonable estimate for the slow-crypto cases? | |
| constexpr System::Clock::Timeout kSlowCryptoProcessingTime = System::Clock::Seconds16(7); | |
| System::Clock::Timeout timeout; | |
| switch (stage) | |
| { | |
| case CommissioningStage::kWiFiNetworkEnable: | |
| ChipLogProgress(Controller, "Setting wifi connection time min = %u", | |
| mDeviceCommissioningInfo.network.wifi.minConnectionTime); | |
| timeout = System::Clock::Seconds16(mDeviceCommissioningInfo.network.wifi.minConnectionTime); | |
| break; | |
| case CommissioningStage::kThreadNetworkEnable: | |
| timeout = System::Clock::Seconds16(mDeviceCommissioningInfo.network.thread.minConnectionTime); | |
| break; | |
| case CommissioningStage::kScanNetworks: | |
| // We're not sure which sort of scan we will do, so just use the larger | |
| // of the timeouts we might have. Note that anything we select here is | |
| // still clamped to be at least kMinimumCommissioningStepTimeout below. | |
| timeout = System::Clock::Seconds16( | |
| std::max(mDeviceCommissioningInfo.network.wifi.maxScanTime, mDeviceCommissioningInfo.network.thread.maxScanTime)); | |
| break; | |
| case CommissioningStage::kSendNOC: | |
| case CommissioningStage::kSendOpCertSigningRequest: | |
| timeout = kSlowCryptoProcessingTime; | |
| break; | |
| default: | |
| timeout = app::kExpectedIMProcessingTime; | |
| break; | |
| } | |
| // Adjust the timeout for our session transport latency, if we have access | |
| // to a session. | |
| auto sessionHandle = device->GetSecureSession(); | |
| if (sessionHandle.HasValue()) | |
| { | |
| timeout = sessionHandle.Value()->ComputeRoundTripTimeout(timeout, true /*isFirstMessageOnExchange*/); | |
| } | |
| // Enforce the spec minimal timeout. Maybe this enforcement should live in | |
| // the DeviceCommissioner? | |
| if (timeout < kMinimumCommissioningStepTimeout) | |
| { | |
| timeout = kMinimumCommissioningStepTimeout; | |
| } | |
| return MakeOptional(timeout); | |
| } | |
| CHIP_ERROR AutoCommissioner::NOCChainGenerated(ByteSpan noc, ByteSpan icac, ByteSpan rcac, IdentityProtectionKeySpan ipk, | |
| NodeId adminSubject) | |
| { | |
| // Reuse ICA Cert buffer for temporary store Root Cert. | |
| MutableByteSpan rootCert = MutableByteSpan(mICACertBuffer); | |
| ReturnErrorOnFailure(Credentials::ConvertX509CertToChipCert(rcac, rootCert)); | |
| mParams.SetRootCert(rootCert); | |
| MutableByteSpan noCert = MutableByteSpan(mNOCertBuffer); | |
| ReturnErrorOnFailure(Credentials::ConvertX509CertToChipCert(noc, noCert)); | |
| mParams.SetNoc(noCert); | |
| CommissioningStage nextStage = CommissioningStage::kSendTrustedRootCert; | |
| mCommissioner->PerformCommissioningStep(mCommissioneeDeviceProxy, nextStage, mParams, this, 0, | |
| GetCommandTimeout(mCommissioneeDeviceProxy, nextStage)); | |
| // Trusted root cert has been sent, so we can re-use the icac buffer for the icac. | |
| if (!icac.empty()) | |
| { | |
| MutableByteSpan icaCert = MutableByteSpan(mICACertBuffer); | |
| ReturnErrorOnFailure(Credentials::ConvertX509CertToChipCert(icac, icaCert)); | |
| mParams.SetIcac(icaCert); | |
| } | |
| else | |
| { | |
| mParams.SetIcac(ByteSpan()); | |
| } | |
| mParams.SetIpk(ipk); | |
| mParams.SetAdminSubject(adminSubject); | |
| return CHIP_NO_ERROR; | |
| } | |
| void AutoCommissioner::CleanupCommissioning() | |
| { | |
| ClearPDCParameters(); | |
| ResetNetworkAttemptType(); | |
| mPAI.Free(); | |
| mDAC.Free(); | |
| mCommissioneeDeviceProxy = nullptr; | |
| mOperationalDeviceProxy = OperationalDeviceProxy(); | |
| mDeviceCommissioningInfo = ReadCommissioningInfo(); | |
| mWroteNetworkConfig = false; | |
| mNeedsDST = false; | |
| mNeedsNetworkSetup = false; | |
| mNeedIcdRegistration = false; | |
| mStopCommissioning = false; | |
| mAttestationElementsLen = 0; | |
| mAttestationSignatureLen = 0; | |
| } | |
| CHIP_ERROR AutoCommissioner::CommissioningStepFinished(CHIP_ERROR err, CommissioningDelegate::CommissioningReport report) | |
| { | |
| if (!mStopCommissioning && err == CHIP_IM_GLOBAL_STATUS(InvalidCommand) && | |
| report.stageCompleted == CommissioningStage::kSendPAICertificateRequest && report.Is<AttestationChainNotAvailable>() && | |
| mParams.GetAttestationCertificateChain().has_value()) | |
| { | |
| const auto chain = Internal::SelectAttestationCertificateChain( | |
| mDeviceCommissioningInfo.paaSupportedAttestationProfiles, mDeviceCommissioningInfo.paiSupportedAttestationProfiles, | |
| mDeviceCommissioningInfo.dacSupportedAttestationProfiles, Internal::GetControllerSupportedAttestationRequestProfiles(), | |
| mParams.GetAttestationCertificateChain()); | |
| if (chain.has_value()) | |
| { | |
| if (chain->PAACrypto == OperationalCredentials::AttestationCryptoProfileEnum::kEcdsaMatterLegacy) | |
| { | |
| mParams.ClearAttestationCertificateChain(); | |
| } | |
| else | |
| { | |
| mParams.SetAttestationCertificateChain(*chain); | |
| } | |
| ChipLogProgress(Controller, "Trying attestation chain: PAA %u, PAI %u, DAC %u", to_underlying(chain->PAACrypto), | |
| to_underlying(chain->PAICrypto), to_underlying(chain->DACCrypto)); | |
| return PerformStep(CommissioningStage::kSendPAICertificateRequest); | |
| } | |
| } | |
| CompletionStatus completionStatus; | |
| completionStatus.err = err; | |
| if (err != CHIP_NO_ERROR) | |
| { | |
| ChipLogError(Controller, "Error on commissioning step '%s': '%s'", StageToString(report.stageCompleted), err.AsString()); | |
| completionStatus.failedStage = MakeOptional(report.stageCompleted); | |
| if (report.Is<AttestationErrorInfo>()) | |
| { | |
| completionStatus.attestationResult = MakeOptional(report.Get<AttestationErrorInfo>().attestationResult); | |
| if ((report.Get<AttestationErrorInfo>().attestationResult == | |
| Credentials::AttestationVerificationResult::kDacProductIdMismatch) || | |
| (report.Get<AttestationErrorInfo>().attestationResult == | |
| Credentials::AttestationVerificationResult::kDacVendorIdMismatch)) | |
| { | |
| ChipLogError(Controller, | |
| "Failed device attestation. Device vendor and/or product ID do not match the IDs expected. " | |
| "Verify DAC certificate chain and certification declaration to ensure spec rules followed."); | |
| } | |
| if (report.stageCompleted == CommissioningStage::kAttestationVerification) | |
| { | |
| ChipLogError(Controller, "Failed verifying attestation information. Now checking DAC chain revoked status."); | |
| // don't error out until we check for DAC chain revocation status | |
| err = CHIP_NO_ERROR; | |
| } | |
| } | |
| else if (report.Is<CommissioningErrorInfo>()) | |
| { | |
| const auto & commissioningInfo = report.Get<CommissioningErrorInfo>(); | |
| completionStatus.commissioningError = MakeOptional(commissioningInfo.commissioningError); | |
| completionStatus.commissioningDebugText = commissioningInfo.debugText; | |
| } | |
| else if (report.Is<OperationalCertErrorInfo>()) | |
| { | |
| // Preserve the NodeOperationalCertStatusEnum from the device's NOCResponse | |
| // (kInvalidPublicKey, kInvalidNodeOpId, kInvalidNOC, kFabricConflict, kLabelConflict, | |
| // kInvalidFabricIndex, etc.) so callers can distinguish without losing fidelity to a | |
| // generic CHIP_ERROR. | |
| completionStatus.operationalCertStatus = MakeOptional(report.Get<OperationalCertErrorInfo>().operationalCertStatus); | |
| } | |
| else if (report.Is<NetworkCommissioningStatusInfo>()) | |
| { | |
| // This report type is used when an error happens in either NetworkConfig or ConnectNetwork commands | |
| const auto & networkInfo = report.Get<NetworkCommissioningStatusInfo>(); | |
| completionStatus.networkCommissioningStatus = MakeOptional(networkInfo.networkCommissioningStatus); | |
| // Preserve the optional ConnectNetworkResponse.errorValue (driver-level detail | |
| // distinct from the spec-level networkingStatus). Null for NetworkConfigResponse. | |
| completionStatus.connectNetworkErrorValue = networkInfo.connectNetworkErrorValue; | |
| completionStatus.networkCommissioningDebugText = networkInfo.debugText; | |
| // If we are configured to scan networks, then don't error out. | |
| // Instead, allow the app to try another network. | |
| // | |
| // TODO: This doesn't actually work, because in order to provide credentials someone | |
| // had to SetWiFiCredentials() or SetThreadOperationalDataset() on our params, so | |
| // IsScanNeeded() will no longer test true for that network technology. | |
| // Scanning is the wrong condition in any case: the question is whether the application | |
| // is able to supply another set of credentials, which a CommissioningParameters flag | |
| // along the lines of RetryNetworkCredentials would say directly. | |
| // | |
| // TODO: A retry also has to remove the configuration we just wrote before writing | |
| // another one, (unless it is for the same NetworkID). Under PDC this is not just | |
| // untidy: the Network Client Identity registered for the old configuration is only | |
| // rolled back on RemoveNetwork or a failed attempt, so kPDCRegisterClientIdentity will | |
| // refuse to register a second one while it is still outstanding. | |
| if (IsScanNeeded()) | |
| { | |
| if (completionStatus.err == CHIP_NO_ERROR) | |
| { | |
| completionStatus.err = err; | |
| } | |
| err = CHIP_NO_ERROR; | |
| // Walk back the completed stage to kScanNetworks. | |
| // This will allow the app to try another network. | |
| report.stageCompleted = CommissioningStage::kScanNetworks; | |
| ClearPDCParameters(); // parameters from a failed attempt are no longer valid / useful | |
| } | |
| } | |
| auto isNetworkFailureStage = [](CommissioningStage stage) { | |
| // If we were somewhere between network setup and and trying to finish up commissioning, | |
| // treat that as a network failure stage; trying the secondary network might work | |
| // better. | |
| if (stage >= kWiFiNetworkSetup && stage <= kICDSendStayActive) | |
| { | |
| return true; | |
| } | |
| // If we were trying to request credentials and failed (because our delegate does not | |
| // implement that functionality, for example), treat that as a network failure stage and | |
| // try the other network type. | |
| if (stage == kRequestThreadCredentials || stage == kRequestWiFiCredentials || stage == kNeedsNetworkCreds) | |
| { | |
| return true; | |
| } | |
| // Likewise if we could not reach the network's Network Identity Management provider to | |
| // obtain a Network Identity: the other network type might still work. | |
| if (stage == kPDCGetNetworkIdentity) | |
| { | |
| return true; | |
| } | |
| return false; | |
| }; | |
| if (err != CHIP_NO_ERROR && TryingPrimaryNetwork() && completionStatus.failedStage.HasValue() && | |
| isNetworkFailureStage(completionStatus.failedStage.Value())) | |
| { | |
| // Primary network failed, disable primary network interface and try secondary network interface. | |
| TrySecondaryNetwork(); | |
| err = CHIP_NO_ERROR; | |
| report.stageCompleted = CommissioningStage::kPrimaryOperationalNetworkFailed; | |
| } | |
| } | |
| else | |
| { | |
| ChipLogProgress(Controller, "Successfully finished commissioning step '%s'", StageToString(report.stageCompleted)); | |
| switch (report.stageCompleted) | |
| { | |
| case CommissioningStage::kReadCommissioningInfo: { | |
| mDeviceCommissioningInfo = report.Get<ReadCommissioningInfo>(); | |
| if (!mParams.GetFailsafeTimerSeconds().HasValue() && mDeviceCommissioningInfo.general.recommendedFailsafe > 0) | |
| { | |
| mParams.SetFailsafeTimerSeconds(mDeviceCommissioningInfo.general.recommendedFailsafe); | |
| } | |
| mParams.SetRemoteVendorId(mDeviceCommissioningInfo.basic.vendorId) | |
| .SetRemoteProductId(mDeviceCommissioningInfo.basic.productId) | |
| .SetDefaultRegulatoryLocation(mDeviceCommissioningInfo.general.currentRegulatoryLocation) | |
| .SetLocationCapability(mDeviceCommissioningInfo.general.locationCapability); | |
| mParams.ClearAttestationCertificateChain(); | |
| if (mDeviceCommissioningInfo.supportsPqcDeviceAttestation) | |
| { | |
| const auto chain = | |
| Internal::SelectAttestationCertificateChain(mDeviceCommissioningInfo.paaSupportedAttestationProfiles, | |
| mDeviceCommissioningInfo.paiSupportedAttestationProfiles, | |
| mDeviceCommissioningInfo.dacSupportedAttestationProfiles, | |
| Internal::GetControllerSupportedAttestationRequestProfiles()); | |
| if (chain.has_value()) | |
| { | |
| mParams.SetAttestationCertificateChain(*chain); | |
| ChipLogProgress(Controller, "Selected attestation chain: PAA %u, PAI %u, DAC %u", | |
| to_underlying(chain->PAACrypto), to_underlying(chain->PAICrypto), | |
| to_underlying(chain->DACCrypto)); | |
| } | |
| else | |
| { | |
| ChipLogError(Controller, "No mutually supported attestation chain; using legacy device attestation"); | |
| } | |
| } | |
| // Don't send DST unless the device says it needs it | |
| mNeedsDST = false; | |
| mParams.SetSupportsConcurrentConnection(mDeviceCommissioningInfo.supportsConcurrentConnection); | |
| if (mParams.GetCheckForMatchingFabric()) | |
| { | |
| NodeId nodeId = mDeviceCommissioningInfo.remoteNodeId; | |
| if (nodeId != kUndefinedNodeId) | |
| { | |
| mParams.SetRemoteNodeId(nodeId); | |
| } | |
| } | |
| mNeedIcdRegistration = false; | |
| if (mParams.GetICDRegistrationStrategy() != ICDRegistrationStrategy::kIgnore) | |
| { | |
| if (mDeviceCommissioningInfo.icd.isLIT && mDeviceCommissioningInfo.icd.checkInProtocolSupport) | |
| { | |
| mNeedIcdRegistration = true; | |
| ChipLogDetail(Controller, "AutoCommissioner: ICD supports the check-in protocol."); | |
| } | |
| else if (mParams.GetICDStayActiveDurationMsec().HasValue()) | |
| { | |
| ChipLogDetail(Controller, "AutoCommissioner: Clear ICD StayActiveDurationMsec"); | |
| mParams.ClearICDStayActiveDurationMsec(); | |
| } | |
| } | |
| break; | |
| } | |
| case CommissioningStage::kConfigureTimeZone: | |
| mNeedsDST = report.Get<TimeZoneResponseInfo>().requiresDSTOffsets; | |
| break; | |
| case CommissioningStage::kSendPAICertificateRequest: { | |
| auto reportPAISpan = report.Get<RequestedCertificate>().certificate; | |
| mPAI.CopyFromSpan(reportPAISpan); | |
| mParams.SetPAI(mPAI.Span()); | |
| break; | |
| } | |
| case CommissioningStage::kSendDACCertificateRequest: { | |
| auto reportDACSpan = report.Get<RequestedCertificate>().certificate; | |
| mDAC.CopyFromSpan(reportDACSpan); | |
| mParams.SetDAC(ByteSpan(mDAC.Span())); | |
| break; | |
| } | |
| case CommissioningStage::kSendAttestationRequest: { | |
| auto & elements = report.Get<AttestationResponse>().attestationElements; | |
| auto & signature = report.Get<AttestationResponse>().signature; | |
| if (elements.size() > sizeof(mAttestationElements)) | |
| { | |
| ChipLogError(Controller, "AutoCommissioner attestationElements buffer size %u larger than cache size %u", | |
| static_cast<unsigned>(elements.size()), static_cast<unsigned>(sizeof(mAttestationElements))); | |
| return CHIP_ERROR_MESSAGE_TOO_LONG; | |
| } | |
| memcpy(mAttestationElements, elements.data(), elements.size()); | |
| mAttestationElementsLen = static_cast<uint16_t>(elements.size()); | |
| mParams.SetAttestationElements(ByteSpan(mAttestationElements, elements.size())); | |
| ChipLogDetail(Controller, "AutoCommissioner setting attestationElements buffer size %u/%u", | |
| static_cast<unsigned>(elements.size()), | |
| static_cast<unsigned>(mParams.GetAttestationElements().Value().size())); | |
| if (signature.size() > sizeof(mAttestationSignature)) | |
| { | |
| ChipLogError(Controller, | |
| "AutoCommissioner attestationSignature buffer size %u larger than " | |
| "cache size %u", | |
| static_cast<unsigned>(signature.size()), static_cast<unsigned>(sizeof(mAttestationSignature))); | |
| return CHIP_ERROR_MESSAGE_TOO_LONG; | |
| } | |
| memcpy(mAttestationSignature, signature.data(), signature.size()); | |
| mAttestationSignatureLen = static_cast<uint16_t>(signature.size()); | |
| mParams.SetAttestationSignature(ByteSpan(mAttestationSignature, signature.size())); | |
| // TODO: Does this need to be done at runtime? Seems like this could be done earlier and we wouldn't need to hold a | |
| // reference to the operational credential delegate here | |
| if (mOperationalCredentialsDelegate != nullptr) | |
| { | |
| uint8_t csrNonceBytes[sizeof(mCSRNonce)]; | |
| MutableByteSpan nonce(csrNonceBytes); | |
| CHIP_ERROR csrError = mOperationalCredentialsDelegate->ObtainCsrNonce(nonce); | |
| if (csrError != CHIP_ERROR_NOT_IMPLEMENTED) | |
| { | |
| ReturnErrorOnFailure(csrError); | |
| MutableByteSpan savedCSRNonce(mCSRNonce); | |
| ReturnErrorOnFailure(CopySpanToMutableSpan(nonce, savedCSRNonce)); | |
| mParams.SetCSRNonce(savedCSRNonce); | |
| } | |
| } | |
| break; | |
| } | |
| case CommissioningStage::kSendOpCertSigningRequest: { | |
| NOCChainGenerationParameters nocParams; | |
| nocParams.nocsrElements = report.Get<CSRResponse>().nocsrElements; | |
| nocParams.signature = report.Get<CSRResponse>().signature; | |
| mParams.SetNOCChainGenerationParameters(nocParams); | |
| } | |
| break; | |
| case CommissioningStage::kGenerateNOCChain: | |
| // For NOC chain generation, we re-use the buffers. NOCChainGenerated triggers the next stage before | |
| // storing the returned certs, so just return here without triggering the next stage. | |
| return NOCChainGenerated(report.Get<NocChain>().noc, report.Get<NocChain>().icac, report.Get<NocChain>().rcac, | |
| report.Get<NocChain>().ipk, report.Get<NocChain>().adminSubject); | |
| case CommissioningStage::kICDGetRegistrationInfo: | |
| // Noting to do. The ICD registation info is handled elsewhere. | |
| break; | |
| case CommissioningStage::kICDRegistration: | |
| // Noting to do. DevicePairingDelegate will handle this. | |
| break; | |
| case CommissioningStage::kPDCGetNetworkIdentity: { | |
| ByteSpan networkIdentity = report.Get<PDCNetworkIdentityInfo>().networkIdentity; | |
| ReturnErrorOnFailure(RelocateSpan(networkIdentity, mNetworkIdentity)); | |
| mParams.SetPDCNetworkIdentity(networkIdentity); | |
| // Use a random possession nonce for kWiFiNetworkSetup unless the application supplied one. | |
| if (!mParams.GetPDCPossessionNonce().HasValue()) | |
| { | |
| ReturnErrorOnFailure(Crypto::DRBG_get_bytes(mPossessionNonce, sizeof(mPossessionNonce))); | |
| mParams.SetPDCPossessionNonce(ByteSpan(mPossessionNonce)); | |
| } | |
| break; | |
| } | |
| case CommissioningStage::kWiFiNetworkSetup: | |
| mWroteNetworkConfig = true; | |
| if (mParams.GetPDCNetworkIdentity().HasValue()) | |
| { | |
| // We configured the commissionee for PDC, so it returned a client identity along with | |
| // proof that it holds the corresponding private key, both already checked for shape by | |
| // the DeviceCommissioner (which verifies the proof itself during | |
| // kPDCRegisterClientIdentity). Note that we need to copy the underlying bytes: the | |
| // report spans point into the response message buffer. | |
| const auto & info = report.Get<PDCClientIdentityInfo>(); | |
| ByteSpan clientIdentity = info.clientIdentity; | |
| ReturnErrorOnFailure(RelocateSpan(clientIdentity, mClientIdentity)); | |
| mParams.SetPDCClientIdentity(clientIdentity); | |
| ByteSpan possessionSignature = info.possessionSignature; | |
| ReturnErrorOnFailure(RelocateSpan(possessionSignature, mPossessionSignature, /* exactSize = */ true)); | |
| mParams.SetPDCPossessionSignature(possessionSignature); | |
| } | |
| break; | |
| case CommissioningStage::kThreadNetworkSetup: | |
| mWroteNetworkConfig = true; | |
| break; | |
| case CommissioningStage::kRemoveWiFiNetworkConfig: | |
| case CommissioningStage::kRemoveThreadNetworkConfig: | |
| mWroteNetworkConfig = false; | |
| break; | |
| case CommissioningStage::kFindOperationalForStayActive: | |
| case CommissioningStage::kFindOperationalForCommissioningComplete: | |
| mOperationalDeviceProxy = report.Get<OperationalNodeFoundData>().operationalProxy; | |
| break; | |
| case CommissioningStage::kCleanup: | |
| CleanupCommissioning(); | |
| return CHIP_NO_ERROR; | |
| default: | |
| break; | |
| } | |
| } | |
| CommissioningStage nextStage = GetNextCommissioningStage(report.stageCompleted, err); | |
| if (nextStage == CommissioningStage::kError) | |
| { | |
| return CHIP_ERROR_INCORRECT_STATE; | |
| } | |
| // If GetNextCommissioningStage indicated a failure, don't lose track of | |
| // that. But don't overwrite any existing failures we had hanging | |
| // around. | |
| if (completionStatus.err == CHIP_NO_ERROR) | |
| { | |
| completionStatus.err = err; | |
| } | |
| mParams.SetCompletionStatus(completionStatus); | |
| return PerformStep(nextStage); | |
| } | |
| DeviceProxy * AutoCommissioner::GetDeviceProxyForStep(CommissioningStage nextStage) | |
| { | |
| if (nextStage == CommissioningStage::kSendComplete || nextStage == CommissioningStage::kICDSendStayActive || | |
| (nextStage == CommissioningStage::kCleanup && mOperationalDeviceProxy.GetDeviceId() != kUndefinedNodeId)) | |
| { | |
| return &mOperationalDeviceProxy; | |
| } | |
| return mCommissioneeDeviceProxy; | |
| } | |
| CHIP_ERROR AutoCommissioner::PerformStep(CommissioningStage nextStage) | |
| { | |
| DeviceProxy * proxy = GetDeviceProxyForStep(nextStage); | |
| if (proxy == nullptr) | |
| { | |
| ChipLogError(Controller, "Invalid device for commissioning"); | |
| return CHIP_ERROR_INCORRECT_STATE; | |
| } | |
| // Perform any last minute parameter adjustments before calling the commissioner object | |
| switch (nextStage) | |
| { | |
| case CommissioningStage::kConfigureTimeZone: | |
| if (mParams.GetTimeZone().Value().size() > mDeviceCommissioningInfo.maxTimeZoneSize) | |
| { | |
| mParams.SetTimeZone(app::DataModel::List<app::Clusters::TimeSynchronization::Structs::TimeZoneStruct::Type>( | |
| mParams.GetTimeZone().Value().SubSpan(0, mDeviceCommissioningInfo.maxTimeZoneSize))); | |
| } | |
| break; | |
| case CommissioningStage::kConfigureDSTOffset: | |
| if (mParams.GetDSTOffsets().Value().size() > mDeviceCommissioningInfo.maxDSTSize) | |
| { | |
| mParams.SetDSTOffsets(app::DataModel::List<app::Clusters::TimeSynchronization::Structs::DSTOffsetStruct::Type>( | |
| mParams.GetDSTOffsets().Value().SubSpan(0, mDeviceCommissioningInfo.maxDSTSize))); | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| mCommissioner->PerformCommissioningStep(proxy, nextStage, mParams, this, GetEndpoint(nextStage), | |
| GetCommandTimeout(proxy, nextStage)); | |
| return CHIP_NO_ERROR; | |
| } | |
| } // namespace Controller | |
| } // namespace chip |