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