scoped_mock_unexportable_key_provider.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2021 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. #include <vector>
  5. #include "crypto/scoped_mock_unexportable_key_provider.h"
  6. #include "crypto/sha2.h"
  7. #include "crypto/signature_verifier.h"
  8. #include "crypto/unexportable_key.h"
  9. #include "third_party/boringssl/src/include/openssl/bytestring.h"
  10. #include "third_party/boringssl/src/include/openssl/ec.h"
  11. #include "third_party/boringssl/src/include/openssl/ec_key.h"
  12. #include "third_party/boringssl/src/include/openssl/ecdsa.h"
  13. #include "third_party/boringssl/src/include/openssl/evp.h"
  14. #include "third_party/boringssl/src/include/openssl/obj.h"
  15. namespace crypto {
  16. namespace {
  17. std::vector<uint8_t> CBBToVector(const CBB* cbb) {
  18. return std::vector<uint8_t>(CBB_data(cbb), CBB_data(cbb) + CBB_len(cbb));
  19. }
  20. class SoftwareECDSA : public UnexportableSigningKey {
  21. public:
  22. explicit SoftwareECDSA(bssl::UniquePtr<EC_KEY> key) : key_(std::move(key)) {}
  23. ~SoftwareECDSA() override = default;
  24. SignatureVerifier::SignatureAlgorithm Algorithm() const override {
  25. return SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256;
  26. }
  27. std::vector<uint8_t> GetSubjectPublicKeyInfo() const override {
  28. bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
  29. CHECK(EVP_PKEY_set1_EC_KEY(pkey.get(), key_.get()));
  30. bssl::ScopedCBB cbb;
  31. CHECK(CBB_init(cbb.get(), /*initial_capacity=*/128) &&
  32. EVP_marshal_public_key(cbb.get(), pkey.get()));
  33. return CBBToVector(cbb.get());
  34. }
  35. std::vector<uint8_t> GetWrappedKey() const override {
  36. bssl::ScopedCBB cbb;
  37. CHECK(
  38. CBB_init(cbb.get(), /*initial_capacity=*/128) &&
  39. EC_KEY_marshal_private_key(cbb.get(), key_.get(),
  40. EC_PKEY_NO_PARAMETERS | EC_PKEY_NO_PUBKEY));
  41. return CBBToVector(cbb.get());
  42. }
  43. absl::optional<std::vector<uint8_t>> SignSlowly(
  44. base::span<const uint8_t> data) override {
  45. std::vector<uint8_t> ret(ECDSA_size(key_.get()));
  46. std::array<uint8_t, kSHA256Length> digest = SHA256Hash(data);
  47. unsigned int ret_size;
  48. CHECK(ECDSA_sign(0, digest.data(), digest.size(), ret.data(), &ret_size,
  49. key_.get()));
  50. ret.resize(ret_size);
  51. return ret;
  52. }
  53. private:
  54. bssl::UniquePtr<EC_KEY> key_;
  55. };
  56. class SoftwareProvider : public UnexportableKeyProvider {
  57. public:
  58. ~SoftwareProvider() override = default;
  59. absl::optional<SignatureVerifier::SignatureAlgorithm> SelectAlgorithm(
  60. base::span<const SignatureVerifier::SignatureAlgorithm>
  61. acceptable_algorithms) override {
  62. for (auto algo : acceptable_algorithms) {
  63. if (algo == SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256) {
  64. return algo;
  65. }
  66. }
  67. return absl::nullopt;
  68. }
  69. std::unique_ptr<UnexportableSigningKey> GenerateSigningKeySlowly(
  70. base::span<const SignatureVerifier::SignatureAlgorithm>
  71. acceptable_algorithms) override {
  72. if (!SelectAlgorithm(acceptable_algorithms)) {
  73. return nullptr;
  74. }
  75. bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  76. CHECK(EC_KEY_generate_key(key.get()));
  77. return std::make_unique<SoftwareECDSA>(std::move(key));
  78. }
  79. std::unique_ptr<UnexportableSigningKey> FromWrappedSigningKeySlowly(
  80. base::span<const uint8_t> wrapped_key) override {
  81. bssl::UniquePtr<EC_GROUP> p256(
  82. EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  83. CBS cbs;
  84. CBS_init(&cbs, wrapped_key.data(), wrapped_key.size());
  85. bssl::UniquePtr<EC_KEY> key(EC_KEY_parse_private_key(&cbs, p256.get()));
  86. if (!key || CBS_len(&cbs) != 0) {
  87. return nullptr;
  88. }
  89. return std::make_unique<SoftwareECDSA>(std::move(key));
  90. }
  91. };
  92. std::unique_ptr<UnexportableKeyProvider> GetUnexportableKeyProviderMock() {
  93. return std::make_unique<SoftwareProvider>();
  94. }
  95. } // namespace
  96. ScopedMockUnexportableKeyProvider::ScopedMockUnexportableKeyProvider() {
  97. internal::SetUnexportableKeyProviderForTesting(
  98. GetUnexportableKeyProviderMock);
  99. }
  100. ScopedMockUnexportableKeyProvider::~ScopedMockUnexportableKeyProvider() {
  101. internal::SetUnexportableKeyProviderForTesting(nullptr);
  102. }
  103. } // namespace crypto