fido_cable_handshake_handler.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef DEVICE_FIDO_CABLE_FIDO_CABLE_HANDSHAKE_HANDLER_H_
  5. #define DEVICE_FIDO_CABLE_FIDO_CABLE_HANDSHAKE_HANDLER_H_
  6. #include <stdint.h>
  7. #include <array>
  8. #include <string>
  9. #include <vector>
  10. #include "base/component_export.h"
  11. #include "base/containers/span.h"
  12. #include "base/gtest_prod_util.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "device/fido/cable/cable_discovery_data.h"
  16. #include "device/fido/cable/noise.h"
  17. #include "device/fido/cable/v2_handshake.h"
  18. #include "device/fido/fido_device.h"
  19. #include "third_party/boringssl/src/include/openssl/base.h"
  20. namespace device {
  21. class FidoCableDevice;
  22. // FidoCableHandshakeHandler abstracts FidoCableV1HandshakeHandler to allow
  23. // tests to inject fake handshake handlers.
  24. class FidoCableHandshakeHandler {
  25. public:
  26. virtual ~FidoCableHandshakeHandler() = 0;
  27. virtual void InitiateCableHandshake(FidoDevice::DeviceCallback callback) = 0;
  28. virtual bool ValidateAuthenticatorHandshakeMessage(
  29. base::span<const uint8_t> response) = 0;
  30. };
  31. // Handles exchanging handshake messages with external authenticator and
  32. // validating the handshake messages to derive a shared session key to be used
  33. // for message encryption.
  34. // See: fido-client-to-authenticator-protocol.html#cable-encryption-handshake of
  35. // the most up-to-date spec.
  36. class COMPONENT_EXPORT(DEVICE_FIDO) FidoCableV1HandshakeHandler
  37. : public FidoCableHandshakeHandler {
  38. public:
  39. FidoCableV1HandshakeHandler(FidoCableDevice* device,
  40. base::span<const uint8_t, 8> nonce,
  41. base::span<const uint8_t, 32> session_pre_key);
  42. FidoCableV1HandshakeHandler(const FidoCableV1HandshakeHandler&) = delete;
  43. FidoCableV1HandshakeHandler& operator=(const FidoCableV1HandshakeHandler&) =
  44. delete;
  45. ~FidoCableV1HandshakeHandler() override;
  46. // FidoCableHandshakeHandler:
  47. void InitiateCableHandshake(FidoDevice::DeviceCallback callback) override;
  48. bool ValidateAuthenticatorHandshakeMessage(
  49. base::span<const uint8_t> response) override;
  50. private:
  51. FRIEND_TEST_ALL_PREFIXES(FidoCableHandshakeHandlerTest, HandShakeSuccess);
  52. FRIEND_TEST_ALL_PREFIXES(FidoCableHandshakeHandlerTest,
  53. HandshakeFailWithIncorrectAuthenticatorResponse);
  54. std::vector<uint8_t> GetEncryptionKeyAfterSuccessfulHandshake(
  55. base::span<const uint8_t, 16> authenticator_random_nonce) const;
  56. const raw_ptr<FidoCableDevice> cable_device_;
  57. std::array<uint8_t, 8> nonce_;
  58. std::array<uint8_t, 32> session_pre_key_;
  59. std::array<uint8_t, 16> client_session_random_;
  60. std::string handshake_key_;
  61. base::WeakPtrFactory<FidoCableV1HandshakeHandler> weak_factory_{this};
  62. };
  63. } // namespace device
  64. #endif // DEVICE_FIDO_CABLE_FIDO_CABLE_HANDSHAKE_HANDLER_H_