CommissionableInit.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. *
  3. * Copyright (c) 2022 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <crypto/CHIPCryptoPAL.h>
  19. #include <lib/support/CodeUtils.h>
  20. #include <lib/support/logging/CHIPLogging.h>
  21. #include <platform/TestOnlyCommissionableDataProvider.h>
  22. #include "CommissionableInit.h"
  23. namespace chip {
  24. namespace examples {
  25. using namespace chip::DeviceLayer;
  26. CHIP_ERROR InitCommissionableDataProvider(LinuxCommissionableDataProvider & provider, LinuxDeviceOptions & options)
  27. {
  28. chip::Optional<uint32_t> setupPasscode;
  29. if (options.payload.setUpPINCode != 0)
  30. {
  31. setupPasscode.SetValue(options.payload.setUpPINCode);
  32. }
  33. else if (!options.spake2pVerifier.HasValue())
  34. {
  35. uint32_t defaultTestPasscode = 0;
  36. chip::DeviceLayer::TestOnlyCommissionableDataProvider TestOnlyCommissionableDataProvider;
  37. VerifyOrDie(TestOnlyCommissionableDataProvider.GetSetupPasscode(defaultTestPasscode) == CHIP_NO_ERROR);
  38. ChipLogError(Support,
  39. "*** WARNING: Using temporary passcode %u due to no neither --passcode or --spake2p-verifier-base64 "
  40. "given on command line. This is temporary and will disappear. Please update your scripts "
  41. "to explicitly configure onboarding credentials. ***",
  42. static_cast<unsigned>(defaultTestPasscode));
  43. setupPasscode.SetValue(defaultTestPasscode);
  44. options.payload.setUpPINCode = defaultTestPasscode;
  45. }
  46. else
  47. {
  48. // Passcode is 0, so will be ignored, and verifier will take over. Onboarding payload
  49. // printed for debug will be invalid, but if the onboarding payload had been given
  50. // properly to the commissioner later, PASE will succeed.
  51. }
  52. if (options.discriminator.HasValue())
  53. {
  54. options.payload.discriminator.SetLongValue(options.discriminator.Value());
  55. }
  56. else
  57. {
  58. uint16_t defaultTestDiscriminator = 0;
  59. chip::DeviceLayer::TestOnlyCommissionableDataProvider TestOnlyCommissionableDataProvider;
  60. VerifyOrDie(TestOnlyCommissionableDataProvider.GetSetupDiscriminator(defaultTestDiscriminator) == CHIP_NO_ERROR);
  61. ChipLogError(Support,
  62. "*** WARNING: Using temporary test discriminator %u due to --discriminator not "
  63. "given on command line. This is temporary and will disappear. Please update your scripts "
  64. "to explicitly configure discriminator. ***",
  65. static_cast<unsigned>(defaultTestDiscriminator));
  66. options.payload.discriminator.SetLongValue(defaultTestDiscriminator);
  67. }
  68. // Default to minimum PBKDF iterations
  69. uint32_t spake2pIterationCount = chip::Crypto::kSpake2p_Min_PBKDF_Iterations;
  70. if (options.spake2pIterations != 0)
  71. {
  72. spake2pIterationCount = options.spake2pIterations;
  73. }
  74. ChipLogError(Support, "PASE PBKDF iterations set to %u", static_cast<unsigned>(spake2pIterationCount));
  75. return provider.Init(options.spake2pVerifier, options.spake2pSalt, spake2pIterationCount, setupPasscode,
  76. options.payload.discriminator.GetLongValue());
  77. }
  78. CHIP_ERROR InitConfigurationManager(ConfigurationManagerImpl & configManager, LinuxDeviceOptions & options)
  79. {
  80. if (options.payload.vendorID != 0)
  81. {
  82. configManager.StoreVendorId(options.payload.vendorID);
  83. }
  84. if (options.payload.productID != 0)
  85. {
  86. configManager.StoreProductId(options.payload.productID);
  87. }
  88. return CHIP_NO_ERROR;
  89. }
  90. } // namespace examples
  91. } // namespace chip