blob: ab8027ca3ce9c5923d79dc445f6f6887723f9118 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]70372d42010-10-22 13:12:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/rsa_private_key.h"
[email protected]70372d42010-10-22 13:12:346
avidd373b8b2015-12-21 21:34:437#include <stdint.h>
[email protected]1f923942010-11-17 14:39:228
thakisd1a18472016-04-08 22:30:419#include <memory>
davidben74f67442016-10-01 01:45:2210#include <utility>
thakisd1a18472016-04-08 22:30:4111
[email protected]70372d42010-10-22 13:12:3412#include "base/logging.h"
[email protected]4b559b4d2011-04-14 17:37:1413#include "crypto/openssl_util.h"
tfarina29a3a1742016-10-28 18:47:3314#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"
[email protected]70372d42010-10-22 13:12:3419
[email protected]4b559b4d2011-04-14 17:37:1420namespace crypto {
[email protected]70372d42010-10-22 13:12:3421
[email protected]70372d42010-10-22 13:12:3422// static
rsleevid1afa1e2016-06-22 04:00:4823std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Create(uint16_t num_bits) {
[email protected]be796bb2010-11-18 15:43:4324 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]fed406f82010-12-08 15:40:4525
davidben74f67442016-10-01 01:45:2226 bssl::UniquePtr<RSA> rsa_key(RSA_new());
27 bssl::UniquePtr<BIGNUM> bn(BN_new());
[email protected]fed406f82010-12-08 15:40:4528 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
rsleevid1afa1e2016-06-22 04:00:4829 return nullptr;
[email protected]fed406f82010-12-08 15:40:4530
rsleevid1afa1e2016-06-22 04:00:4831 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), nullptr))
32 return nullptr;
[email protected]1f923942010-11-17 14:39:2233
thakisd1a18472016-04-08 22:30:4134 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
davidben74f67442016-10-01 01:45:2235 result->key_.reset(EVP_PKEY_new());
36 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_.get(), rsa_key.get()))
rsleevid1afa1e2016-06-22 04:00:4837 return nullptr;
[email protected]1f923942010-11-17 14:39:2238
rsleevid1afa1e2016-06-22 04:00:4839 return result;
[email protected]70372d42010-10-22 13:12:3440}
41
42// static
rsleevid1afa1e2016-06-22 04:00:4843std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromPrivateKeyInfo(
avidd373b8b2015-12-21 21:34:4344 const std::vector<uint8_t>& input) {
[email protected]d641b6e2011-07-20 11:07:1645 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]1f923942010-11-17 14:39:2246
davidben7dad2a32016-03-01 23:47:4747 CBS cbs;
48 CBS_init(&cbs, input.data(), input.size());
davidben74f67442016-10-01 01:45:2249 bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
davidben7dad2a32016-03-01 23:47:4750 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_RSA)
51 return nullptr;
[email protected]1f923942010-11-17 14:39:2252
thakisd1a18472016-04-08 22:30:4153 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
davidben74f67442016-10-01 01:45:2254 result->key_ = std::move(pkey);
rsleevid1afa1e2016-06-22 04:00:4855 return result;
[email protected]70372d42010-10-22 13:12:3456}
57
dougsteeddb7726ae2014-09-10 23:21:4858// static
rsleevid1afa1e2016-06-22 04:00:4859std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromKey(EVP_PKEY* key) {
dougsteeddb7726ae2014-09-10 23:21:4860 DCHECK(key);
61 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA)
rsleevid1afa1e2016-06-22 04:00:4862 return nullptr;
63 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
David Benjamin139c5572018-07-06 23:53:4264 copy->key_ = bssl::UpRef(key);
dougsteeddb7726ae2014-09-10 23:21:4865 return copy;
66}
67
Chris Watkinsa850a302017-11-30 03:53:4968RSAPrivateKey::RSAPrivateKey() = default;
[email protected]70372d42010-10-22 13:12:3469
Chris Watkinsa850a302017-11-30 03:53:4970RSAPrivateKey::~RSAPrivateKey() = default;
[email protected]70372d42010-10-22 13:12:3471
rsleevid1afa1e2016-06-22 04:00:4872std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Copy() const {
73 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
davidben74f67442016-10-01 01:45:2274 bssl::UniquePtr<RSA> rsa(EVP_PKEY_get1_RSA(key_.get()));
[email protected]58782882011-12-03 01:12:0875 if (!rsa)
rsleevid1afa1e2016-06-22 04:00:4876 return nullptr;
davidben74f67442016-10-01 01:45:2277 copy->key_.reset(EVP_PKEY_new());
78 if (!EVP_PKEY_set1_RSA(copy->key_.get(), rsa.get()))
rsleevid1afa1e2016-06-22 04:00:4879 return nullptr;
80 return copy;
[email protected]58782882011-12-03 01:12:0881}
82
avidd373b8b2015-12-21 21:34:4383bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
davidben212cdf62016-06-07 17:11:0984 OpenSSLErrStackTracer err_tracer(FROM_HERE);
davidben7dad2a32016-03-01 23:47:4785 uint8_t *der;
86 size_t der_len;
davidben74f67442016-10-01 01:45:2287 bssl::ScopedCBB cbb;
davidben7dad2a32016-03-01 23:47:4788 if (!CBB_init(cbb.get(), 0) ||
davidben74f67442016-10-01 01:45:2289 !EVP_marshal_private_key(cbb.get(), key_.get()) ||
davidben7dad2a32016-03-01 23:47:4790 !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;
[email protected]70372d42010-10-22 13:12:3496}
97
avidd373b8b2015-12-21 21:34:4398bool RSAPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
davidben212cdf62016-06-07 17:11:0999 OpenSSLErrStackTracer err_tracer(FROM_HERE);
davidben7dad2a32016-03-01 23:47:47100 uint8_t *der;
101 size_t der_len;
davidben74f67442016-10-01 01:45:22102 bssl::ScopedCBB cbb;
davidben7dad2a32016-03-01 23:47:47103 if (!CBB_init(cbb.get(), 0) ||
davidben74f67442016-10-01 01:45:22104 !EVP_marshal_public_key(cbb.get(), key_.get()) ||
davidben7dad2a32016-03-01 23:47:47105 !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;
[email protected]70372d42010-10-22 13:12:34111}
112
[email protected]4b559b4d2011-04-14 17:37:14113} // namespace crypto