1 // Copyright 2011 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.
4
5 #include "crypto/rsa_private_key.h"
6
7 #include <stdint.h>
8
9 #include <memory>
10 #include <utility>
11
12 #include "base/check.h"
13 #include "base/containers/span.h"
14 #include "crypto/openssl_util.h"
15 #include "third_party/boringssl/src/include/openssl/bn.h"
16 #include "third_party/boringssl/src/include/openssl/bytestring.h"
17 #include "third_party/boringssl/src/include/openssl/evp.h"
18 #include "third_party/boringssl/src/include/openssl/mem.h"
19 #include "third_party/boringssl/src/include/openssl/rsa.h"
20
21 namespace crypto {
22
23 // static
Create(uint16_t num_bits)24 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Create(uint16_t num_bits) {
25 OpenSSLErrStackTracer err_tracer(FROM_HERE);
26
27 bssl::UniquePtr<RSA> rsa_key(RSA_new());
28 bssl::UniquePtr<BIGNUM> bn(BN_new());
29 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
30 return nullptr;
31
32 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), nullptr))
33 return nullptr;
34
35 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
36 result->key_.reset(EVP_PKEY_new());
37 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_.get(), rsa_key.get()))
38 return nullptr;
39
40 return result;
41 }
42
43 // static
CreateFromPrivateKeyInfo(base::span<const uint8_t> input)44 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromPrivateKeyInfo(
45 base::span<const uint8_t> input) {
46 OpenSSLErrStackTracer err_tracer(FROM_HERE);
47
48 CBS cbs;
49 CBS_init(&cbs, input.data(), input.size());
50 bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
51 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_RSA)
52 return nullptr;
53
54 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
55 result->key_ = std::move(pkey);
56 return result;
57 }
58
59 // static
CreateFromKey(EVP_PKEY * key)60 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromKey(EVP_PKEY* key) {
61 DCHECK(key);
62 if (EVP_PKEY_id(key) != EVP_PKEY_RSA)
63 return nullptr;
64 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
65 copy->key_ = bssl::UpRef(key);
66 return copy;
67 }
68
69 RSAPrivateKey::RSAPrivateKey() = default;
70
71 RSAPrivateKey::~RSAPrivateKey() = default;
72
Copy() const73 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Copy() const {
74 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
75 bssl::UniquePtr<RSA> rsa(EVP_PKEY_get1_RSA(key_.get()));
76 if (!rsa)
77 return nullptr;
78 copy->key_.reset(EVP_PKEY_new());
79 if (!EVP_PKEY_set1_RSA(copy->key_.get(), rsa.get()))
80 return nullptr;
81 return copy;
82 }
83
ExportPrivateKey(std::vector<uint8_t> * output) const84 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
85 OpenSSLErrStackTracer err_tracer(FROM_HERE);
86 uint8_t *der;
87 size_t der_len;
88 bssl::ScopedCBB cbb;
89 if (!CBB_init(cbb.get(), 0) ||
90 !EVP_marshal_private_key(cbb.get(), key_.get()) ||
91 !CBB_finish(cbb.get(), &der, &der_len)) {
92 return false;
93 }
94 output->assign(der, der + der_len);
95 OPENSSL_free(der);
96 return true;
97 }
98
ExportPublicKey(std::vector<uint8_t> * output) const99 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
100 OpenSSLErrStackTracer err_tracer(FROM_HERE);
101 uint8_t *der;
102 size_t der_len;
103 bssl::ScopedCBB cbb;
104 if (!CBB_init(cbb.get(), 0) ||
105 !EVP_marshal_public_key(cbb.get(), key_.get()) ||
106 !CBB_finish(cbb.get(), &der, &der_len)) {
107 return false;
108 }
109 output->assign(der, der + der_len);
110 OPENSSL_free(der);
111 return true;
112 }
113
114 } // namespace crypto
115