1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/check.h"
8 #include "crypto/scoped_mock_unexportable_key_provider.h"
9 #include "crypto/sha2.h"
10 #include "crypto/signature_verifier.h"
11 #include "crypto/unexportable_key.h"
12 #include "third_party/boringssl/src/include/openssl/bytestring.h"
13 #include "third_party/boringssl/src/include/openssl/ec.h"
14 #include "third_party/boringssl/src/include/openssl/ec_key.h"
15 #include "third_party/boringssl/src/include/openssl/ecdsa.h"
16 #include "third_party/boringssl/src/include/openssl/evp.h"
17 #include "third_party/boringssl/src/include/openssl/obj.h"
23 std::vector<uint8_t> CBBToVector(const CBB* cbb) {
24 return std::vector<uint8_t>(CBB_data(cbb), CBB_data(cbb) + CBB_len(cbb));
27 class SoftwareECDSA : public UnexportableSigningKey {
29 explicit SoftwareECDSA(bssl::UniquePtr<EC_KEY> key) : key_(std::move(key)) {}
30 ~SoftwareECDSA() override = default;
32 SignatureVerifier::SignatureAlgorithm Algorithm() const override {
33 return SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256;
36 std::vector<uint8_t> GetSubjectPublicKeyInfo() const override {
37 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
38 CHECK(EVP_PKEY_set1_EC_KEY(pkey.get(), key_.get()));
41 CHECK(CBB_init(cbb.get(), /*initial_capacity=*/128) &&
42 EVP_marshal_public_key(cbb.get(), pkey.get()));
43 return CBBToVector(cbb.get());
46 std::vector<uint8_t> GetWrappedKey() const override {
49 CBB_init(cbb.get(), /*initial_capacity=*/128) &&
50 EC_KEY_marshal_private_key(cbb.get(), key_.get(),
51 EC_PKEY_NO_PARAMETERS | EC_PKEY_NO_PUBKEY));
52 return CBBToVector(cbb.get());
55 absl::optional<std::vector<uint8_t>> SignSlowly(
56 base::span<const uint8_t> data) override {
57 std::vector<uint8_t> ret(ECDSA_size(key_.get()));
58 std::array<uint8_t, kSHA256Length> digest = SHA256Hash(data);
59 unsigned int ret_size;
60 CHECK(ECDSA_sign(0, digest.data(), digest.size(), ret.data(), &ret_size,
67 bssl::UniquePtr<EC_KEY> key_;
70 class SoftwareProvider : public UnexportableKeyProvider {
72 ~SoftwareProvider() override = default;
74 absl::optional<SignatureVerifier::SignatureAlgorithm> SelectAlgorithm(
75 base::span<const SignatureVerifier::SignatureAlgorithm>
76 acceptable_algorithms) override {
77 for (auto algo : acceptable_algorithms) {
78 if (algo == SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256) {
86 std::unique_ptr<UnexportableSigningKey> GenerateSigningKeySlowly(
87 base::span<const SignatureVerifier::SignatureAlgorithm>
88 acceptable_algorithms) override {
89 if (!SelectAlgorithm(acceptable_algorithms)) {
93 bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
94 CHECK(EC_KEY_generate_key(key.get()));
96 return std::make_unique<SoftwareECDSA>(std::move(key));
99 std::unique_ptr<UnexportableSigningKey> FromWrappedSigningKeySlowly(
100 base::span<const uint8_t> wrapped_key) override {
101 bssl::UniquePtr<EC_GROUP> p256(
102 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
104 CBS_init(&cbs, wrapped_key.data(), wrapped_key.size());
105 bssl::UniquePtr<EC_KEY> key(EC_KEY_parse_private_key(&cbs, p256.get()));
106 if (!key || CBS_len(&cbs) != 0) {
109 return std::make_unique<SoftwareECDSA>(std::move(key));
113 std::unique_ptr<UnexportableKeyProvider> GetUnexportableKeyProviderMock() {
114 return std::make_unique<SoftwareProvider>();
119 ScopedMockUnexportableKeyProvider::ScopedMockUnexportableKeyProvider() {
120 internal::SetUnexportableKeyProviderForTesting(
121 GetUnexportableKeyProviderMock);
124 ScopedMockUnexportableKeyProvider::~ScopedMockUnexportableKeyProvider() {
125 internal::SetUnexportableKeyProviderForTesting(nullptr);
128 } // namespace crypto