scoped_mock_unexportable_key_provider.cc 4.2 KB

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