LinuxCommissionableDataProvider.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #pragma once
  19. #include <stdint.h>
  20. #include <vector>
  21. #include <lib/core/CHIPError.h>
  22. #include <lib/core/Optional.h>
  23. #include <platform/CommissionableDataProvider.h>
  24. class LinuxCommissionableDataProvider : public chip::DeviceLayer::CommissionableDataProvider
  25. {
  26. public:
  27. LinuxCommissionableDataProvider() {}
  28. /**
  29. * @brief Initialize the commissionable Data provider for Linux samples
  30. *
  31. * This version attempts to properly make use of the different arguments
  32. * to allow direct override of PASE verifier, and correct computation of
  33. * the PAKE verifier based on configurable iteration counts.
  34. *
  35. * The setupPasscode is not required BUT if it is absent, it will read
  36. * back as 0 and log a warning. This means testing assumptions of being able
  37. * to generate a setup payload will be wrong, but it will allow testing
  38. * configurations where the passcode is maintained separately than the
  39. * verifier for security purposes.
  40. *
  41. * @param serializedSpake2pVerifier - Optional serialized verifier that will
  42. * override computation from setupPasscode if provided
  43. * @param spake2pSalt - Optional salt to use. A random one will be generated
  44. * otherwise.
  45. * @param spake2pIterationCount - Iteration count to use. If not in range of the
  46. * spec bounds, CHIP_ERROR_INVALID_ARGUMENT will be returned.
  47. * @param setupPasscode - Optional setup passcode to store, and to use to generate
  48. * PASE verifier if `serializedSpake2pVerifier` argument empty.
  49. * @param discriminator - Discriminator to use for advertising.
  50. * @return CHIP_ERROR_OK on success, CHIP_ERROR_INVALID_ARGUMENT on any invalid argument combinations,
  51. * CHIP_ERROR_INCORRECT_STATE if already initialized, or other CHIP_ERROR values if inner
  52. * implementation dependencies fail.
  53. */
  54. CHIP_ERROR Init(chip::Optional<std::vector<uint8_t>> serializedSpake2pVerifier,
  55. chip::Optional<std::vector<uint8_t>> spake2pSalt, uint32_t spake2pIterationCount,
  56. chip::Optional<uint32_t> setupPasscode, uint16_t discriminator);
  57. CHIP_ERROR GetSetupDiscriminator(uint16_t & setupDiscriminator) override;
  58. CHIP_ERROR SetSetupDiscriminator(uint16_t setupDiscriminator) override
  59. {
  60. // We don't support overriding the discriminator post-init (it is deprecated!)
  61. return CHIP_ERROR_NOT_IMPLEMENTED;
  62. }
  63. CHIP_ERROR GetSpake2pIterationCount(uint32_t & iterationCount) override;
  64. CHIP_ERROR GetSpake2pSalt(chip::MutableByteSpan & saltBuf) override;
  65. CHIP_ERROR GetSpake2pVerifier(chip::MutableByteSpan & verifierBuf, size_t & outVerifierLen) override;
  66. CHIP_ERROR GetSetupPasscode(uint32_t & setupPasscode) override;
  67. CHIP_ERROR SetSetupPasscode(uint32_t setupPasscode) override
  68. {
  69. // We don't support overriding the passcode post-init (it is deprecated!)
  70. return CHIP_ERROR_NOT_IMPLEMENTED;
  71. }
  72. private:
  73. bool mIsInitialized = false;
  74. std::vector<uint8_t> mSerializedPaseVerifier;
  75. std::vector<uint8_t> mPaseSalt;
  76. uint32_t mPaseIterationCount = 0;
  77. chip::Optional<uint32_t> mSetupPasscode;
  78. uint16_t mDiscriminator = 0;
  79. };