From d3e111ce9b3530889c18f0ab40e225b42776d138 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Sat, 19 Mar 2022 20:32:30 +0800 Subject: [PATCH 1/9] add CommissionableDataProvider on android platform --- .../chiptvserver/service/MatterServant.java | 7 + .../platform/CommissionableDataProvider.h | 3 + src/platform/BUILD.gn | 2 +- .../android/AndroidChipPlatform-JNI.cpp | 14 ++ src/platform/android/BUILD.gn | 2 + .../CommissionableDataProviderImpl.cpp | 206 ++++++++++++++++++ .../android/CommissionableDataProviderImpl.h | 64 ++++++ .../chip/platform/AndroidChipPlatform.java | 2 + .../PreferencesConfigurationManager.java | 6 - 9 files changed, 299 insertions(+), 7 deletions(-) create mode 100644 src/platform/android/CommissionableDataProviderImpl.cpp create mode 100644 src/platform/android/CommissionableDataProviderImpl.h diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java index 1a1c6f914b0d57..e794d4dc5b8f27 100644 --- a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java @@ -96,6 +96,13 @@ public void init(@NonNull Context context) { new NsdManagerServiceResolver(applicationContext), new ChipMdnsCallbackImpl(), new DiagnosticDataProviderImpl(applicationContext)); + chipPlatform.updateCommissionableDataProviderData(null,null,0,20202021,0xF00); + +// String testSalt = "U1BBS0UyUCBLZXkgU2FsdA=="; +// String testVerifier = "uWFwqugDNGiEck/po7KHwwMwwqZgN10XuyBajPGuyzUEV/iree4lOrao5GuwnlQ65CJzbeUB49s31EH+NEkg0JVI5MGCQGMMT/SRPFNRODm3wH/MBiehuFc6FJ/" + "NH6Rmzw=="; +// chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,0,0xF00); +// +// chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,20202021,0xF00); tvApp.postInit(); diff --git a/src/include/platform/CommissionableDataProvider.h b/src/include/platform/CommissionableDataProvider.h index aea04bc833b2c8..6a51023f1aef03 100644 --- a/src/include/platform/CommissionableDataProvider.h +++ b/src/include/platform/CommissionableDataProvider.h @@ -24,6 +24,9 @@ namespace chip { // The largest value of the 12-bit Payload discriminator constexpr uint16_t kMaxDiscriminatorValue = 0xFFF; +constexpr uint32_t kMinSetupPasscode = 1; +constexpr uint32_t kMaxSetupPasscode = 0x5F5E0FE; + namespace DeviceLayer { class CommissionableDataProvider diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index 1ac8f1bba7f764..16b5fb07051a05 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -57,7 +57,7 @@ if (chip_device_platform != "none") { # === FOR TRANSITION UNTIL ALL EXAMPLES PROVIDE THEIR OWN === # Linux platform has already transitioned. chip_use_transitional_commissionable_data_provider = - chip_device_platform != "linux" + (chip_device_platform != "linux" && chip_device_platform != "android") # lock tracking: none/log/fatal or auto for a platform-dependent choice chip_stack_lock_tracking = "auto" diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index e655b30140c4b2..5385ee54e67c9d 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -21,6 +21,7 @@ * Implementation of JNI bridge for CHIP Device Controller for Android apps * */ +#include #include #include #include @@ -35,6 +36,7 @@ #include "AndroidChipPlatform-JNI.h" #include "BLEManagerImpl.h" +#include "CommissionableDataProviderImpl.h" #include "DiagnosticDataProviderImpl.h" #include "DnssdImpl.h" @@ -255,3 +257,15 @@ static bool JavaBytesToUUID(JNIEnv * env, jbyteArray value, chip::Ble::ChipBleUU return result; } #endif + +// for CommissionableDataProvider +JNI_METHOD(jboolean, updateCommissionableDataProviderData)(JNIEnv * env, jclass self,jstring spake2pVerifierBase64, + jstring Spake2pSaltBase64, jint spake2pIterationCount, + jlong setupPasscode, jint discriminator) +{ + chip::DeviceLayer::StackLock lock; + CHIP_ERROR err = CommissionableDataProviderMgrImpl().Update(env, spake2pVerifierBase64, Spake2pSaltBase64, + spake2pIterationCount, setupPasscode, discriminator); + + return err == CHIP_NO_ERROR; +} diff --git a/src/platform/android/BUILD.gn b/src/platform/android/BUILD.gn index c6ab8699b96cf0..fbb642003f0e1d 100644 --- a/src/platform/android/BUILD.gn +++ b/src/platform/android/BUILD.gn @@ -50,6 +50,8 @@ static_library("android") { "KeyValueStoreManagerImpl.h", "PlatformManagerImpl.cpp", "PlatformManagerImpl.h", + "CommissionableDataProviderImpl.cpp", + "CommissionableDataProviderImpl.h", ] deps = [ diff --git a/src/platform/android/CommissionableDataProviderImpl.cpp b/src/platform/android/CommissionableDataProviderImpl.cpp new file mode 100644 index 00000000000000..28b81f2fa67c6c --- /dev/null +++ b/src/platform/android/CommissionableDataProviderImpl.cpp @@ -0,0 +1,206 @@ +/* + * + * Copyright (c) 2022 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 "CommissionableDataProviderImpl.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace chip; +using namespace chip::Crypto; + +namespace { + +CHIP_ERROR GeneratePaseSalt(std::vector & spake2pSaltVector) +{ + constexpr size_t kSaltLen = kSpake2p_Max_PBKDF_Salt_Length; + spake2pSaltVector.resize(kSaltLen); + return DRBG_get_bytes(spake2pSaltVector.data(), spake2pSaltVector.size()); +} + +} // namespace + +CommissionableDataProviderImpl CommissionableDataProviderImpl::sInstance; + +CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pVerifierBase64, + jstring Spake2pSaltBase64, jint spake2pIterationCount, + jlong setupPasscode, jint discriminator) +{ + if (!mIsUpdate) { + DeviceLayer::SetCommissionableDataProvider(this); + } + + VerifyOrReturnLogError(discriminator <= chip::kMaxDiscriminatorValue, CHIP_ERROR_INVALID_ARGUMENT); + if(spake2pIterationCount == 0) { + spake2pIterationCount = CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT; + } + VerifyOrReturnLogError(static_cast(spake2pIterationCount) >= kSpake2p_Min_PBKDF_Iterations, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnLogError(static_cast(spake2pIterationCount) <= kSpake2p_Max_PBKDF_Iterations, CHIP_ERROR_INVALID_ARGUMENT); + + CHIP_ERROR err; + Spake2pVerifier providedVerifier; + std::vector serializedSpake2pVerifier(kSpake2p_VerifierSerialized_Length); + bool havePaseVerifier = (spake2pVerifierBase64 != nullptr); + if (havePaseVerifier) + { + chip::JniUtfString utfSpake2pVerifierBase64(env, spake2pVerifierBase64); + + size_t maxBase64Size = BASE64_ENCODED_LEN(chip::Crypto::kSpake2p_VerifierSerialized_Length); + VerifyOrReturnLogError(static_cast(utfSpake2pVerifierBase64.size()) <= maxBase64Size, CHIP_ERROR_INVALID_ARGUMENT); + + size_t decodedLen = chip::Base64Decode32(utfSpake2pVerifierBase64.c_str(), utfSpake2pVerifierBase64.size(), reinterpret_cast(serializedSpake2pVerifier.data())); + VerifyOrReturnLogError(decodedLen == chip::Crypto::kSpake2p_VerifierSerialized_Length, CHIP_ERROR_INVALID_ARGUMENT); + + chip::MutableByteSpan verifierSpan{ serializedSpake2pVerifier.data(), decodedLen }; + err = providedVerifier.Deserialize(verifierSpan); + VerifyOrReturnLogError(err == CHIP_NO_ERROR, err); + + ChipLogProgress(Support, "Got externally provided verifier, using it."); + } + + bool havePaseSalt = (Spake2pSaltBase64 != nullptr); + VerifyOrReturnLogError(!havePaseVerifier || (havePaseVerifier && havePaseSalt), CHIP_ERROR_INVALID_ARGUMENT); + std::vector spake2pSalt(chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length); + + if (!havePaseSalt) { + ChipLogProgress(Support, "LinuxCommissionableDataProvider didn't get a PASE salt, generating one.");\ + err = GeneratePaseSalt(spake2pSalt); + VerifyOrReturnLogError(err == CHIP_NO_ERROR, err); + } else { + chip::JniUtfString utfSpake2pSaltBase64(env, Spake2pSaltBase64); + + size_t maxBase64Size = BASE64_ENCODED_LEN(chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length); + VerifyOrReturnLogError(static_cast(utfSpake2pSaltBase64.size()) <= maxBase64Size, CHIP_ERROR_INVALID_ARGUMENT); + + size_t decodedLen = chip::Base64Decode32(utfSpake2pSaltBase64.c_str(), utfSpake2pSaltBase64.size(), reinterpret_cast(spake2pSalt.data())); + VerifyOrReturnLogError(decodedLen >= chip::Crypto::kSpake2p_Min_PBKDF_Salt_Length && decodedLen <= chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length, CHIP_ERROR_INVALID_ARGUMENT); + spake2pSalt.resize(decodedLen); + } + + bool havePasscode = (setupPasscode > kMinSetupPasscode && setupPasscode < kMaxSetupPasscode); + Spake2pVerifier passcodeVerifier; + std::vector serializedPasscodeVerifier(kSpake2p_VerifierSerialized_Length); + chip::MutableByteSpan saltSpan{ spake2pSalt.data(), spake2pSalt.size() }; + if (havePasscode) + { + uint32_t u32SetupPasscode = static_cast(setupPasscode); + err = passcodeVerifier.Generate(spake2pIterationCount, saltSpan, u32SetupPasscode); + VerifyOrReturnLogError(err == CHIP_NO_ERROR, err); + + chip::MutableByteSpan verifierSpan{ serializedPasscodeVerifier.data(), serializedPasscodeVerifier.size() }; + err = passcodeVerifier.Serialize(verifierSpan); + VerifyOrReturnLogError(err == CHIP_NO_ERROR, err); + } + + // Make sure we actually have a verifier + VerifyOrReturnLogError(havePasscode || havePaseVerifier, CHIP_ERROR_INVALID_ARGUMENT); + + // If both passcode and external verifier were provided, validate they match, otherwise + // it's ambiguous. + if (havePasscode && havePaseVerifier) + { + VerifyOrReturnLogError(serializedPasscodeVerifier == serializedSpake2pVerifier, CHIP_ERROR_INVALID_ARGUMENT); + ChipLogProgress(Support, "Validated externally provided passcode matches the one generated from provided passcode."); + } + + // External PASE verifier takes precedence when present (even though it is identical to passcode-based + // one when the latter is present). + std::vector finalSerializedVerifier(kSpake2p_VerifierSerialized_Length); + if (havePaseVerifier) + { + finalSerializedVerifier = serializedSpake2pVerifier; + } + else + { + finalSerializedVerifier = serializedPasscodeVerifier; + } + + mDiscriminator = discriminator; + mSerializedPaseVerifier = std::move(finalSerializedVerifier); + mPaseSalt = std::move(spake2pSalt); + mPaseIterationCount = spake2pIterationCount; + if (havePasscode) + { + mSetupPasscode.SetValue(setupPasscode); + } + mIsUpdate = true; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR CommissionableDataProviderImpl::GetSetupDiscriminator(uint16_t & setupDiscriminator) +{ + VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + setupDiscriminator = mDiscriminator; + return CHIP_NO_ERROR; +} + +CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pIterationCount(uint32_t & iterationCount) +{ + VerifyOrReturnLogError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + iterationCount = mPaseIterationCount; + return CHIP_NO_ERROR; +} + +CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pSalt(chip::MutableByteSpan & saltBuf) +{ + VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + + VerifyOrReturnError(saltBuf.size() >= kSpake2p_Max_PBKDF_Salt_Length, CHIP_ERROR_BUFFER_TOO_SMALL); + memcpy(saltBuf.data(), mPaseSalt.data(), mPaseSalt.size()); + saltBuf.reduce_size(mPaseSalt.size()); + + return CHIP_NO_ERROR; +} + +CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pVerifier(chip::MutableByteSpan & verifierBuf, size_t & outVerifierLen) +{ + VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + + // By now, serialized verifier from Init should be correct size + VerifyOrReturnError(mSerializedPaseVerifier.size() == kSpake2p_VerifierSerialized_Length, CHIP_ERROR_INTERNAL); + + outVerifierLen = mSerializedPaseVerifier.size(); + VerifyOrReturnError(verifierBuf.size() >= outVerifierLen, CHIP_ERROR_BUFFER_TOO_SMALL); + memcpy(verifierBuf.data(), mSerializedPaseVerifier.data(), mSerializedPaseVerifier.size()); + verifierBuf.reduce_size(mSerializedPaseVerifier.size()); + + return CHIP_NO_ERROR; +} + +CHIP_ERROR CommissionableDataProviderImpl::GetSetupPasscode(uint32_t & setupPasscode) +{ + VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + + // Pretend not implemented if we don't have a passcode value externally set + if (!mSetupPasscode.HasValue()) + { + return CHIP_ERROR_NOT_IMPLEMENTED; + } + + setupPasscode = mSetupPasscode.Value(); + return CHIP_NO_ERROR; +} diff --git a/src/platform/android/CommissionableDataProviderImpl.h b/src/platform/android/CommissionableDataProviderImpl.h new file mode 100644 index 00000000000000..96390fa418fda7 --- /dev/null +++ b/src/platform/android/CommissionableDataProviderImpl.h @@ -0,0 +1,64 @@ +/* + * + * Copyright (c) 2022 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. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +class CommissionableDataProviderImpl : public chip::DeviceLayer::CommissionableDataProvider +{ +public: + CHIP_ERROR Update(JNIEnv * env, jstring spake2pVerifierBase64, + jstring Spake2pSaltBase64, jint spake2pIterationCount, + jlong setupPasscode, jint discriminator); + CHIP_ERROR GetSetupDiscriminator(uint16_t & setupDiscriminator) override; + CHIP_ERROR SetSetupDiscriminator(uint16_t setupDiscriminator) override + { + // We don't support overriding the discriminator post-init (it is deprecated!) + return CHIP_ERROR_NOT_IMPLEMENTED; + } + CHIP_ERROR GetSpake2pIterationCount(uint32_t & iterationCount) override; + CHIP_ERROR GetSpake2pSalt(chip::MutableByteSpan & saltBuf) override; + CHIP_ERROR GetSpake2pVerifier(chip::MutableByteSpan & verifierBuf, size_t & outVerifierLen) override; + CHIP_ERROR GetSetupPasscode(uint32_t & setupPasscode) override; + CHIP_ERROR SetSetupPasscode(uint32_t setupPasscode) override + { + // We don't support overriding the passcode post-init (it is deprecated!) + return CHIP_ERROR_NOT_IMPLEMENTED; + } + +private: + friend CommissionableDataProviderImpl & CommissionableDataProviderMgrImpl(); + static CommissionableDataProviderImpl sInstance; + bool mIsUpdate = false; + std::vector mSerializedPaseVerifier; + std::vector mPaseSalt; + uint32_t mPaseIterationCount = 0; + chip::Optional mSetupPasscode; + uint16_t mDiscriminator = 0; +}; + +inline CommissionableDataProviderImpl & CommissionableDataProviderMgrImpl() +{ + return CommissionableDataProviderImpl::sInstance; +} diff --git a/src/platform/android/java/chip/platform/AndroidChipPlatform.java b/src/platform/android/java/chip/platform/AndroidChipPlatform.java index da1ee88423872f..bc4f2df84510e6 100644 --- a/src/platform/android/java/chip/platform/AndroidChipPlatform.java +++ b/src/platform/android/java/chip/platform/AndroidChipPlatform.java @@ -91,4 +91,6 @@ private native void nativeSetServiceResolver( ServiceResolver resolver, ChipMdnsCallback chipMdnsCallback); private native void setDiagnosticDataProviderManager(DiagnosticDataProvider dataProviderCallback); + + public native boolean updateCommissionableDataProviderData(String spake2pVerifierBase64, String Spake2pSaltBase64, int spake2pIterationCount, long setupPasscode, int discriminator); } diff --git a/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java b/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java index 6ead06082eadcd..0069f033c6d77b 100644 --- a/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java +++ b/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java @@ -85,12 +85,6 @@ public long readConfigValueLong(String namespace, String name) */ case kConfigNamespace_ChipFactory + ":" + kConfigKey_SoftwareVersion: return 1; - - case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupPinCode: - return 20202021; - - case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupDiscriminator: - return 0xF00; } if (preferences.contains(key)) { From eace8fd997271281dc0d860eb592f5fa39eaa9af Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Sat, 19 Mar 2022 20:38:11 +0800 Subject: [PATCH 2/9] fix restyled-io and ci errors --- .../chiptvserver/service/MatterServant.java | 15 ++++--- src/platform/BUILD.gn | 2 +- .../android/AndroidChipPlatform-JNI.cpp | 8 ++-- src/platform/android/BUILD.gn | 4 +- .../CommissionableDataProviderImpl.cpp | 45 ++++++++++++------- .../android/CommissionableDataProviderImpl.h | 11 +++-- .../chip/platform/AndroidChipPlatform.java | 7 ++- 7 files changed, 55 insertions(+), 37 deletions(-) diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java index e794d4dc5b8f27..8162952af74e48 100644 --- a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java @@ -96,13 +96,16 @@ public void init(@NonNull Context context) { new NsdManagerServiceResolver(applicationContext), new ChipMdnsCallbackImpl(), new DiagnosticDataProviderImpl(applicationContext)); - chipPlatform.updateCommissionableDataProviderData(null,null,0,20202021,0xF00); -// String testSalt = "U1BBS0UyUCBLZXkgU2FsdA=="; -// String testVerifier = "uWFwqugDNGiEck/po7KHwwMwwqZgN10XuyBajPGuyzUEV/iree4lOrao5GuwnlQ65CJzbeUB49s31EH+NEkg0JVI5MGCQGMMT/SRPFNRODm3wH/MBiehuFc6FJ/" + "NH6Rmzw=="; -// chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,0,0xF00); -// -// chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,20202021,0xF00); + chipPlatform.updateCommissionableDataProviderData(null, null, 0, 20202021, 0xF00); + + // String testSalt = "U1BBS0UyUCBLZXkgU2FsdA=="; + // String testVerifier = + // "uWFwqugDNGiEck/po7KHwwMwwqZgN10XuyBajPGuyzUEV/iree4lOrao5GuwnlQ65CJzbeUB49s31EH+NEkg0JVI5MGCQGMMT/SRPFNRODm3wH/MBiehuFc6FJ/" + "NH6Rmzw=="; + // chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,0,0xF00); + // + // + // chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,20202021,0xF00); tvApp.postInit(); diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index 16b5fb07051a05..caed34b8755eeb 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -57,7 +57,7 @@ if (chip_device_platform != "none") { # === FOR TRANSITION UNTIL ALL EXAMPLES PROVIDE THEIR OWN === # Linux platform has already transitioned. chip_use_transitional_commissionable_data_provider = - (chip_device_platform != "linux" && chip_device_platform != "android") + chip_device_platform != "linux" && chip_device_platform != "android" # lock tracking: none/log/fatal or auto for a platform-dependent choice chip_stack_lock_tracking = "auto" diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index 5385ee54e67c9d..d8e1178afb70a5 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -259,13 +259,13 @@ static bool JavaBytesToUUID(JNIEnv * env, jbyteArray value, chip::Ble::ChipBleUU #endif // for CommissionableDataProvider -JNI_METHOD(jboolean, updateCommissionableDataProviderData)(JNIEnv * env, jclass self,jstring spake2pVerifierBase64, - jstring Spake2pSaltBase64, jint spake2pIterationCount, - jlong setupPasscode, jint discriminator) +JNI_METHOD(jboolean, updateCommissionableDataProviderData) +(JNIEnv * env, jclass self, jstring spake2pVerifierBase64, jstring Spake2pSaltBase64, jint spake2pIterationCount, + jlong setupPasscode, jint discriminator) { chip::DeviceLayer::StackLock lock; CHIP_ERROR err = CommissionableDataProviderMgrImpl().Update(env, spake2pVerifierBase64, Spake2pSaltBase64, - spake2pIterationCount, setupPasscode, discriminator); + spake2pIterationCount, setupPasscode, discriminator); return err == CHIP_NO_ERROR; } diff --git a/src/platform/android/BUILD.gn b/src/platform/android/BUILD.gn index fbb642003f0e1d..5f7acc2d6d81a6 100644 --- a/src/platform/android/BUILD.gn +++ b/src/platform/android/BUILD.gn @@ -36,6 +36,8 @@ static_library("android") { "BLEManagerImpl.h", "BlePlatformConfig.h", "CHIPDevicePlatformEvent.h", + "CommissionableDataProviderImpl.cpp", + "CommissionableDataProviderImpl.h", "ConfigurationManagerImpl.cpp", "ConfigurationManagerImpl.h", "ConnectivityManagerImpl.cpp", @@ -50,8 +52,6 @@ static_library("android") { "KeyValueStoreManagerImpl.h", "PlatformManagerImpl.cpp", "PlatformManagerImpl.h", - "CommissionableDataProviderImpl.cpp", - "CommissionableDataProviderImpl.h", ] deps = [ diff --git a/src/platform/android/CommissionableDataProviderImpl.cpp b/src/platform/android/CommissionableDataProviderImpl.cpp index 28b81f2fa67c6c..1f012d17143599 100644 --- a/src/platform/android/CommissionableDataProviderImpl.cpp +++ b/src/platform/android/CommissionableDataProviderImpl.cpp @@ -22,11 +22,11 @@ #include #include +#include #include +#include #include #include -#include -#include #include using namespace chip; @@ -45,20 +45,23 @@ CHIP_ERROR GeneratePaseSalt(std::vector & spake2pSaltVector) CommissionableDataProviderImpl CommissionableDataProviderImpl::sInstance; -CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pVerifierBase64, - jstring Spake2pSaltBase64, jint spake2pIterationCount, - jlong setupPasscode, jint discriminator) +CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pVerifierBase64, jstring Spake2pSaltBase64, + jint spake2pIterationCount, jlong setupPasscode, jint discriminator) { - if (!mIsUpdate) { + if (!mIsUpdate) + { DeviceLayer::SetCommissionableDataProvider(this); } VerifyOrReturnLogError(discriminator <= chip::kMaxDiscriminatorValue, CHIP_ERROR_INVALID_ARGUMENT); - if(spake2pIterationCount == 0) { + if (spake2pIterationCount == 0) + { spake2pIterationCount = CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT; } - VerifyOrReturnLogError(static_cast(spake2pIterationCount) >= kSpake2p_Min_PBKDF_Iterations, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnLogError(static_cast(spake2pIterationCount) <= kSpake2p_Max_PBKDF_Iterations, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnLogError(static_cast(spake2pIterationCount) >= kSpake2p_Min_PBKDF_Iterations, + CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnLogError(static_cast(spake2pIterationCount) <= kSpake2p_Max_PBKDF_Iterations, + CHIP_ERROR_INVALID_ARGUMENT); CHIP_ERROR err; Spake2pVerifier providedVerifier; @@ -69,9 +72,11 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV chip::JniUtfString utfSpake2pVerifierBase64(env, spake2pVerifierBase64); size_t maxBase64Size = BASE64_ENCODED_LEN(chip::Crypto::kSpake2p_VerifierSerialized_Length); - VerifyOrReturnLogError(static_cast(utfSpake2pVerifierBase64.size()) <= maxBase64Size, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnLogError(static_cast(utfSpake2pVerifierBase64.size()) <= maxBase64Size, + CHIP_ERROR_INVALID_ARGUMENT); - size_t decodedLen = chip::Base64Decode32(utfSpake2pVerifierBase64.c_str(), utfSpake2pVerifierBase64.size(), reinterpret_cast(serializedSpake2pVerifier.data())); + size_t decodedLen = chip::Base64Decode32(utfSpake2pVerifierBase64.c_str(), utfSpake2pVerifierBase64.size(), + reinterpret_cast(serializedSpake2pVerifier.data())); VerifyOrReturnLogError(decodedLen == chip::Crypto::kSpake2p_VerifierSerialized_Length, CHIP_ERROR_INVALID_ARGUMENT); chip::MutableByteSpan verifierSpan{ serializedSpake2pVerifier.data(), decodedLen }; @@ -85,18 +90,24 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV VerifyOrReturnLogError(!havePaseVerifier || (havePaseVerifier && havePaseSalt), CHIP_ERROR_INVALID_ARGUMENT); std::vector spake2pSalt(chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length); - if (!havePaseSalt) { - ChipLogProgress(Support, "LinuxCommissionableDataProvider didn't get a PASE salt, generating one.");\ + if (!havePaseSalt) + { + ChipLogProgress(Support, "LinuxCommissionableDataProvider didn't get a PASE salt, generating one."); err = GeneratePaseSalt(spake2pSalt); VerifyOrReturnLogError(err == CHIP_NO_ERROR, err); - } else { + } + else + { chip::JniUtfString utfSpake2pSaltBase64(env, Spake2pSaltBase64); size_t maxBase64Size = BASE64_ENCODED_LEN(chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length); VerifyOrReturnLogError(static_cast(utfSpake2pSaltBase64.size()) <= maxBase64Size, CHIP_ERROR_INVALID_ARGUMENT); - size_t decodedLen = chip::Base64Decode32(utfSpake2pSaltBase64.c_str(), utfSpake2pSaltBase64.size(), reinterpret_cast(spake2pSalt.data())); - VerifyOrReturnLogError(decodedLen >= chip::Crypto::kSpake2p_Min_PBKDF_Salt_Length && decodedLen <= chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length, CHIP_ERROR_INVALID_ARGUMENT); + size_t decodedLen = chip::Base64Decode32(utfSpake2pSaltBase64.c_str(), utfSpake2pSaltBase64.size(), + reinterpret_cast(spake2pSalt.data())); + VerifyOrReturnLogError(decodedLen >= chip::Crypto::kSpake2p_Min_PBKDF_Salt_Length && + decodedLen <= chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length, + CHIP_ERROR_INVALID_ARGUMENT); spake2pSalt.resize(decodedLen); } @@ -107,7 +118,7 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV if (havePasscode) { uint32_t u32SetupPasscode = static_cast(setupPasscode); - err = passcodeVerifier.Generate(spake2pIterationCount, saltSpan, u32SetupPasscode); + err = passcodeVerifier.Generate(spake2pIterationCount, saltSpan, u32SetupPasscode); VerifyOrReturnLogError(err == CHIP_NO_ERROR, err); chip::MutableByteSpan verifierSpan{ serializedPasscodeVerifier.data(), serializedPasscodeVerifier.size() }; diff --git a/src/platform/android/CommissionableDataProviderImpl.h b/src/platform/android/CommissionableDataProviderImpl.h index 96390fa418fda7..df979034f4548e 100644 --- a/src/platform/android/CommissionableDataProviderImpl.h +++ b/src/platform/android/CommissionableDataProviderImpl.h @@ -18,19 +18,18 @@ #pragma once -#include -#include +#include #include #include #include -#include +#include +#include class CommissionableDataProviderImpl : public chip::DeviceLayer::CommissionableDataProvider { public: - CHIP_ERROR Update(JNIEnv * env, jstring spake2pVerifierBase64, - jstring Spake2pSaltBase64, jint spake2pIterationCount, - jlong setupPasscode, jint discriminator); + CHIP_ERROR Update(JNIEnv * env, jstring spake2pVerifierBase64, jstring Spake2pSaltBase64, jint spake2pIterationCount, + jlong setupPasscode, jint discriminator); CHIP_ERROR GetSetupDiscriminator(uint16_t & setupDiscriminator) override; CHIP_ERROR SetSetupDiscriminator(uint16_t setupDiscriminator) override { diff --git a/src/platform/android/java/chip/platform/AndroidChipPlatform.java b/src/platform/android/java/chip/platform/AndroidChipPlatform.java index bc4f2df84510e6..d2e7f3ed076a81 100644 --- a/src/platform/android/java/chip/platform/AndroidChipPlatform.java +++ b/src/platform/android/java/chip/platform/AndroidChipPlatform.java @@ -92,5 +92,10 @@ private native void nativeSetServiceResolver( private native void setDiagnosticDataProviderManager(DiagnosticDataProvider dataProviderCallback); - public native boolean updateCommissionableDataProviderData(String spake2pVerifierBase64, String Spake2pSaltBase64, int spake2pIterationCount, long setupPasscode, int discriminator); + public native boolean updateCommissionableDataProviderData( + String spake2pVerifierBase64, + String Spake2pSaltBase64, + int spake2pIterationCount, + long setupPasscode, + int discriminator); } From b7ce4846ff474b8f7389458ce84f61d504290596 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Mon, 21 Mar 2022 09:20:25 +0800 Subject: [PATCH 3/9] remove commented --- .../tcl/chip/chiptvserver/service/MatterServant.java | 10 +--------- .../java/chip/platform/AndroidChipPlatform.java | 6 ++++++ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java index 8162952af74e48..8528ffca575638 100644 --- a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java @@ -96,17 +96,9 @@ public void init(@NonNull Context context) { new NsdManagerServiceResolver(applicationContext), new ChipMdnsCallbackImpl(), new DiagnosticDataProviderImpl(applicationContext)); - + chipPlatform.updateCommissionableDataProviderData(null, null, 0, 20202021, 0xF00); - // String testSalt = "U1BBS0UyUCBLZXkgU2FsdA=="; - // String testVerifier = - // "uWFwqugDNGiEck/po7KHwwMwwqZgN10XuyBajPGuyzUEV/iree4lOrao5GuwnlQ65CJzbeUB49s31EH+NEkg0JVI5MGCQGMMT/SRPFNRODm3wH/MBiehuFc6FJ/" + "NH6Rmzw=="; - // chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,0,0xF00); - // - // - // chipPlatform.updateCommissionableDataProviderData(testVerifier,testSalt,1000,20202021,0xF00); - tvApp.postInit(); chipAppServer = new ChipAppServer(); diff --git a/src/platform/android/java/chip/platform/AndroidChipPlatform.java b/src/platform/android/java/chip/platform/AndroidChipPlatform.java index d2e7f3ed076a81..e5e1841e2ae0b3 100644 --- a/src/platform/android/java/chip/platform/AndroidChipPlatform.java +++ b/src/platform/android/java/chip/platform/AndroidChipPlatform.java @@ -92,6 +92,12 @@ private native void nativeSetServiceResolver( private native void setDiagnosticDataProviderManager(DiagnosticDataProvider dataProviderCallback); + /** + * update commission info + * @param spake2pVerifierBase64 base64 encoded spake2p verifier, ref CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER + * @param Spake2pSaltBase64 base64 encoded spake2p salt, ref CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT + * @return true on success of false on failed + */ public native boolean updateCommissionableDataProviderData( String spake2pVerifierBase64, String Spake2pSaltBase64, From d7ebcbaacafa64ebb9499ac6e9b27dd8634c8dc3 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Mon, 21 Mar 2022 09:26:34 +0800 Subject: [PATCH 4/9] fix restyled-io and ci errors --- .../android/java/chip/platform/AndroidChipPlatform.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/platform/android/java/chip/platform/AndroidChipPlatform.java b/src/platform/android/java/chip/platform/AndroidChipPlatform.java index e5e1841e2ae0b3..6617b2785abe89 100644 --- a/src/platform/android/java/chip/platform/AndroidChipPlatform.java +++ b/src/platform/android/java/chip/platform/AndroidChipPlatform.java @@ -94,8 +94,11 @@ private native void nativeSetServiceResolver( /** * update commission info - * @param spake2pVerifierBase64 base64 encoded spake2p verifier, ref CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER - * @param Spake2pSaltBase64 base64 encoded spake2p salt, ref CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT + * + * @param spake2pVerifierBase64 base64 encoded spake2p verifier, ref + * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER + * @param Spake2pSaltBase64 base64 encoded spake2p salt, ref + * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT * @return true on success of false on failed */ public native boolean updateCommissionableDataProviderData( From bbdf555c7e9bcdc777d0e360b3bb32c00ba7236a Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Tue, 22 Mar 2022 16:09:34 +0800 Subject: [PATCH 5/9] fix PR comment --- .../chiptvserver/service/MatterServant.java | 7 ++- .../CommissionableDataProviderImpl.cpp | 44 ++++++++++--------- .../android/CommissionableDataProviderImpl.h | 2 +- .../chip/platform/AndroidChipPlatform.java | 5 ++- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java index 8528ffca575638..e7f341a24b462a 100644 --- a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java @@ -40,6 +40,9 @@ public class MatterServant { + public int testSetupPasscode = 20202021; + public int testDiscriminator = 0xF00; + private ChipAppServer chipAppServer; private MatterServant() {} @@ -96,8 +99,8 @@ public void init(@NonNull Context context) { new NsdManagerServiceResolver(applicationContext), new ChipMdnsCallbackImpl(), new DiagnosticDataProviderImpl(applicationContext)); - - chipPlatform.updateCommissionableDataProviderData(null, null, 0, 20202021, 0xF00); + + chipPlatform.updateCommissionableDataProviderData(null, null, 0, testSetupPasscode, testDiscriminator); tvApp.postInit(); diff --git a/src/platform/android/CommissionableDataProviderImpl.cpp b/src/platform/android/CommissionableDataProviderImpl.cpp index 1f012d17143599..e69c61c3bba21b 100644 --- a/src/platform/android/CommissionableDataProviderImpl.cpp +++ b/src/platform/android/CommissionableDataProviderImpl.cpp @@ -48,12 +48,8 @@ CommissionableDataProviderImpl CommissionableDataProviderImpl::sInstance; CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pVerifierBase64, jstring Spake2pSaltBase64, jint spake2pIterationCount, jlong setupPasscode, jint discriminator) { - if (!mIsUpdate) - { - DeviceLayer::SetCommissionableDataProvider(this); - } - VerifyOrReturnLogError(discriminator <= chip::kMaxDiscriminatorValue, CHIP_ERROR_INVALID_ARGUMENT); + if (spake2pIterationCount == 0) { spake2pIterationCount = CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT; @@ -63,10 +59,14 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV VerifyOrReturnLogError(static_cast(spake2pIterationCount) <= kSpake2p_Max_PBKDF_Iterations, CHIP_ERROR_INVALID_ARGUMENT); + const bool havePaseVerifier = (spake2pVerifierBase64 != nullptr); + const bool havePaseSalt = (Spake2pSaltBase64 != nullptr); + VerifyOrReturnLogError(!havePaseVerifier || (havePaseVerifier && havePaseSalt), CHIP_ERROR_INVALID_ARGUMENT); + CHIP_ERROR err; + // read verifier from paramter is have Spake2pVerifier providedVerifier; std::vector serializedSpake2pVerifier(kSpake2p_VerifierSerialized_Length); - bool havePaseVerifier = (spake2pVerifierBase64 != nullptr); if (havePaseVerifier) { chip::JniUtfString utfSpake2pVerifierBase64(env, spake2pVerifierBase64); @@ -86,10 +86,8 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV ChipLogProgress(Support, "Got externally provided verifier, using it."); } - bool havePaseSalt = (Spake2pSaltBase64 != nullptr); - VerifyOrReturnLogError(!havePaseVerifier || (havePaseVerifier && havePaseSalt), CHIP_ERROR_INVALID_ARGUMENT); + // read slat from paramter is have or generate one std::vector spake2pSalt(chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length); - if (!havePaseSalt) { ChipLogProgress(Support, "LinuxCommissionableDataProvider didn't get a PASE salt, generating one."); @@ -111,7 +109,8 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV spake2pSalt.resize(decodedLen); } - bool havePasscode = (setupPasscode > kMinSetupPasscode && setupPasscode < kMaxSetupPasscode); + // generate verifier from passcode is have + const bool havePasscode = (setupPasscode > kMinSetupPasscode && setupPasscode < kMaxSetupPasscode); Spake2pVerifier passcodeVerifier; std::vector serializedPasscodeVerifier(kSpake2p_VerifierSerialized_Length); chip::MutableByteSpan saltSpan{ spake2pSalt.data(), spake2pSalt.size() }; @@ -139,46 +138,49 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV // External PASE verifier takes precedence when present (even though it is identical to passcode-based // one when the latter is present). - std::vector finalSerializedVerifier(kSpake2p_VerifierSerialized_Length); if (havePaseVerifier) { - finalSerializedVerifier = serializedSpake2pVerifier; + mSerializedPaseVerifier = std::move(serializedSpake2pVerifier); } else { - finalSerializedVerifier = serializedPasscodeVerifier; + mSerializedPaseVerifier = std::move(serializedPasscodeVerifier); } - mDiscriminator = discriminator; - mSerializedPaseVerifier = std::move(finalSerializedVerifier); mPaseSalt = std::move(spake2pSalt); mPaseIterationCount = spake2pIterationCount; if (havePasscode) { mSetupPasscode.SetValue(setupPasscode); } - mIsUpdate = true; + + // Set to global CommissionableDataProvider once success first time + if (!mIsInitialized) + { + DeviceLayer::SetCommissionableDataProvider(this); + } + mIsInitialized = true; return CHIP_NO_ERROR; } CHIP_ERROR CommissionableDataProviderImpl::GetSetupDiscriminator(uint16_t & setupDiscriminator) { - VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); setupDiscriminator = mDiscriminator; return CHIP_NO_ERROR; } CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pIterationCount(uint32_t & iterationCount) { - VerifyOrReturnLogError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnLogError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); iterationCount = mPaseIterationCount; return CHIP_NO_ERROR; } CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pSalt(chip::MutableByteSpan & saltBuf) { - VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(saltBuf.size() >= kSpake2p_Max_PBKDF_Salt_Length, CHIP_ERROR_BUFFER_TOO_SMALL); memcpy(saltBuf.data(), mPaseSalt.data(), mPaseSalt.size()); @@ -189,7 +191,7 @@ CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pSalt(chip::MutableByteSpan CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pVerifier(chip::MutableByteSpan & verifierBuf, size_t & outVerifierLen) { - VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); // By now, serialized verifier from Init should be correct size VerifyOrReturnError(mSerializedPaseVerifier.size() == kSpake2p_VerifierSerialized_Length, CHIP_ERROR_INTERNAL); @@ -204,7 +206,7 @@ CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pVerifier(chip::MutableByteS CHIP_ERROR CommissionableDataProviderImpl::GetSetupPasscode(uint32_t & setupPasscode) { - VerifyOrReturnError(mIsUpdate == true, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); // Pretend not implemented if we don't have a passcode value externally set if (!mSetupPasscode.HasValue()) diff --git a/src/platform/android/CommissionableDataProviderImpl.h b/src/platform/android/CommissionableDataProviderImpl.h index df979034f4548e..15e8404730496d 100644 --- a/src/platform/android/CommissionableDataProviderImpl.h +++ b/src/platform/android/CommissionableDataProviderImpl.h @@ -49,7 +49,7 @@ class CommissionableDataProviderImpl : public chip::DeviceLayer::CommissionableD private: friend CommissionableDataProviderImpl & CommissionableDataProviderMgrImpl(); static CommissionableDataProviderImpl sInstance; - bool mIsUpdate = false; + bool mIsInitialized = false; std::vector mSerializedPaseVerifier; std::vector mPaseSalt; uint32_t mPaseIterationCount = 0; diff --git a/src/platform/android/java/chip/platform/AndroidChipPlatform.java b/src/platform/android/java/chip/platform/AndroidChipPlatform.java index 6617b2785abe89..1d51164ac7ab94 100644 --- a/src/platform/android/java/chip/platform/AndroidChipPlatform.java +++ b/src/platform/android/java/chip/platform/AndroidChipPlatform.java @@ -96,9 +96,10 @@ private native void nativeSetServiceResolver( * update commission info * * @param spake2pVerifierBase64 base64 encoded spake2p verifier, ref - * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER + * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER. using null to generate it from passcode. * @param Spake2pSaltBase64 base64 encoded spake2p salt, ref - * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT + * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT. using null to generate a random one. + * @param spake2pIterationCount Spake2p iteration count, or 0 to use CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT * @return true on success of false on failed */ public native boolean updateCommissionableDataProviderData( From 5f60bbfd220e9f7051fef143dbcae275e4b322e2 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Tue, 22 Mar 2022 16:16:58 +0800 Subject: [PATCH 6/9] fix restyled-io and ci errors --- .../com/tcl/chip/chiptvserver/service/MatterServant.java | 3 ++- src/platform/android/CommissionableDataProviderImpl.cpp | 8 ++++---- .../android/java/chip/platform/AndroidChipPlatform.java | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java index e7f341a24b462a..18842aabde64b9 100644 --- a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java @@ -100,7 +100,8 @@ public void init(@NonNull Context context) { new ChipMdnsCallbackImpl(), new DiagnosticDataProviderImpl(applicationContext)); - chipPlatform.updateCommissionableDataProviderData(null, null, 0, testSetupPasscode, testDiscriminator); + chipPlatform.updateCommissionableDataProviderData( + null, null, 0, testSetupPasscode, testDiscriminator); tvApp.postInit(); diff --git a/src/platform/android/CommissionableDataProviderImpl.cpp b/src/platform/android/CommissionableDataProviderImpl.cpp index e69c61c3bba21b..5ed71008153a56 100644 --- a/src/platform/android/CommissionableDataProviderImpl.cpp +++ b/src/platform/android/CommissionableDataProviderImpl.cpp @@ -60,7 +60,7 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV CHIP_ERROR_INVALID_ARGUMENT); const bool havePaseVerifier = (spake2pVerifierBase64 != nullptr); - const bool havePaseSalt = (Spake2pSaltBase64 != nullptr); + const bool havePaseSalt = (Spake2pSaltBase64 != nullptr); VerifyOrReturnLogError(!havePaseVerifier || (havePaseVerifier && havePaseSalt), CHIP_ERROR_INVALID_ARGUMENT); CHIP_ERROR err; @@ -146,9 +146,9 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV { mSerializedPaseVerifier = std::move(serializedPasscodeVerifier); } - mDiscriminator = discriminator; - mPaseSalt = std::move(spake2pSalt); - mPaseIterationCount = spake2pIterationCount; + mDiscriminator = discriminator; + mPaseSalt = std::move(spake2pSalt); + mPaseIterationCount = spake2pIterationCount; if (havePasscode) { mSetupPasscode.SetValue(setupPasscode); diff --git a/src/platform/android/java/chip/platform/AndroidChipPlatform.java b/src/platform/android/java/chip/platform/AndroidChipPlatform.java index 1d51164ac7ab94..eaad0e6ed0d281 100644 --- a/src/platform/android/java/chip/platform/AndroidChipPlatform.java +++ b/src/platform/android/java/chip/platform/AndroidChipPlatform.java @@ -99,7 +99,8 @@ private native void nativeSetServiceResolver( * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER. using null to generate it from passcode. * @param Spake2pSaltBase64 base64 encoded spake2p salt, ref * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT. using null to generate a random one. - * @param spake2pIterationCount Spake2p iteration count, or 0 to use CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT + * @param spake2pIterationCount Spake2p iteration count, or 0 to use + * CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT * @return true on success of false on failed */ public native boolean updateCommissionableDataProviderData( From 060fafc38216765a60c6e85e6d6449bc17960bea Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Wed, 23 Mar 2022 15:16:14 +0800 Subject: [PATCH 7/9] fix PR comment --- src/platform/android/AndroidChipPlatform-JNI.cpp | 9 ++++++++- .../android/CommissionableDataProviderImpl.cpp | 14 +++++++------- .../android/CommissionableDataProviderImpl.h | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index d8e1178afb70a5..831eb516d72e45 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -266,6 +266,13 @@ JNI_METHOD(jboolean, updateCommissionableDataProviderData) chip::DeviceLayer::StackLock lock; CHIP_ERROR err = CommissionableDataProviderMgrImpl().Update(env, spake2pVerifierBase64, Spake2pSaltBase64, spake2pIterationCount, setupPasscode, discriminator); + + + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to update commissionable data provider data: %s", ErrorStr(err)); + return false; + } - return err == CHIP_NO_ERROR; + return true; } diff --git a/src/platform/android/CommissionableDataProviderImpl.cpp b/src/platform/android/CommissionableDataProviderImpl.cpp index 5ed71008153a56..ebcd6c48a302a4 100644 --- a/src/platform/android/CommissionableDataProviderImpl.cpp +++ b/src/platform/android/CommissionableDataProviderImpl.cpp @@ -155,32 +155,32 @@ CHIP_ERROR CommissionableDataProviderImpl::Update(JNIEnv * env, jstring spake2pV } // Set to global CommissionableDataProvider once success first time - if (!mIsInitialized) + if (!mFirstUpdated) { DeviceLayer::SetCommissionableDataProvider(this); } - mIsInitialized = true; + mFirstUpdated = true; return CHIP_NO_ERROR; } CHIP_ERROR CommissionableDataProviderImpl::GetSetupDiscriminator(uint16_t & setupDiscriminator) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mFirstUpdated, CHIP_ERROR_INCORRECT_STATE); setupDiscriminator = mDiscriminator; return CHIP_NO_ERROR; } CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pIterationCount(uint32_t & iterationCount) { - VerifyOrReturnLogError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnLogError(mFirstUpdated, CHIP_ERROR_INCORRECT_STATE); iterationCount = mPaseIterationCount; return CHIP_NO_ERROR; } CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pSalt(chip::MutableByteSpan & saltBuf) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mFirstUpdated, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(saltBuf.size() >= kSpake2p_Max_PBKDF_Salt_Length, CHIP_ERROR_BUFFER_TOO_SMALL); memcpy(saltBuf.data(), mPaseSalt.data(), mPaseSalt.size()); @@ -191,7 +191,7 @@ CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pSalt(chip::MutableByteSpan CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pVerifier(chip::MutableByteSpan & verifierBuf, size_t & outVerifierLen) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mFirstUpdated, CHIP_ERROR_INCORRECT_STATE); // By now, serialized verifier from Init should be correct size VerifyOrReturnError(mSerializedPaseVerifier.size() == kSpake2p_VerifierSerialized_Length, CHIP_ERROR_INTERNAL); @@ -206,7 +206,7 @@ CHIP_ERROR CommissionableDataProviderImpl::GetSpake2pVerifier(chip::MutableByteS CHIP_ERROR CommissionableDataProviderImpl::GetSetupPasscode(uint32_t & setupPasscode) { - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mFirstUpdated, CHIP_ERROR_INCORRECT_STATE); // Pretend not implemented if we don't have a passcode value externally set if (!mSetupPasscode.HasValue()) diff --git a/src/platform/android/CommissionableDataProviderImpl.h b/src/platform/android/CommissionableDataProviderImpl.h index 15e8404730496d..ef3bec7516d77a 100644 --- a/src/platform/android/CommissionableDataProviderImpl.h +++ b/src/platform/android/CommissionableDataProviderImpl.h @@ -49,7 +49,7 @@ class CommissionableDataProviderImpl : public chip::DeviceLayer::CommissionableD private: friend CommissionableDataProviderImpl & CommissionableDataProviderMgrImpl(); static CommissionableDataProviderImpl sInstance; - bool mIsInitialized = false; + bool mFirstUpdated = false; std::vector mSerializedPaseVerifier; std::vector mPaseSalt; uint32_t mPaseIterationCount = 0; From 96a7a9236093ae390de941bb73b59559a99c5f24 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Wed, 23 Mar 2022 15:23:50 +0800 Subject: [PATCH 8/9] fix restyled-io and ci errors --- src/platform/android/AndroidChipPlatform-JNI.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index 831eb516d72e45..47c328f42b2c87 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -265,9 +265,7 @@ JNI_METHOD(jboolean, updateCommissionableDataProviderData) { chip::DeviceLayer::StackLock lock; CHIP_ERROR err = CommissionableDataProviderMgrImpl().Update(env, spake2pVerifierBase64, Spake2pSaltBase64, - spake2pIterationCount, setupPasscode, discriminator); - - + spake2pIterationCount, setupPasscode, discriminator); if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "Failed to update commissionable data provider data: %s", ErrorStr(err)); From 0f76aa6a55c48d21313603c6ce8aafdc57f9a819 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Wed, 23 Mar 2022 17:24:20 +0800 Subject: [PATCH 9/9] fix restyled-io and ci errors --- src/platform/android/AndroidChipPlatform-JNI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index 47c328f42b2c87..0f1168401fa7e8 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -265,7 +265,7 @@ JNI_METHOD(jboolean, updateCommissionableDataProviderData) { chip::DeviceLayer::StackLock lock; CHIP_ERROR err = CommissionableDataProviderMgrImpl().Update(env, spake2pVerifierBase64, Spake2pSaltBase64, - spake2pIterationCount, setupPasscode, discriminator); + spake2pIterationCount, setupPasscode, discriminator); if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "Failed to update commissionable data provider data: %s", ErrorStr(err));