Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: crypto/rsa_private_key.cc

Issue 2332473002: Use new BoringSSL scopers in //crypto (Closed)
Patch Set: rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/rsa_private_key.h ('k') | crypto/signature_creator.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "crypto/rsa_private_key.h" 5 #include "crypto/rsa_private_key.h"
6 6
7 #include <openssl/bn.h> 7 #include <openssl/bn.h>
8 #include <openssl/bytestring.h> 8 #include <openssl/bytestring.h>
9 #include <openssl/evp.h> 9 #include <openssl/evp.h>
10 #include <openssl/mem.h> 10 #include <openssl/mem.h>
11 #include <openssl/rsa.h> 11 #include <openssl/rsa.h>
12 #include <stdint.h> 12 #include <stdint.h>
13 13
14 #include <memory> 14 #include <memory>
15 #include <utility>
15 16
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "crypto/auto_cbb.h"
18 #include "crypto/openssl_util.h" 18 #include "crypto/openssl_util.h"
19 #include "crypto/scoped_openssl_types.h"
20 19
21 namespace crypto { 20 namespace crypto {
22 21
23 // static 22 // static
24 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Create(uint16_t num_bits) { 23 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Create(uint16_t num_bits) {
25 OpenSSLErrStackTracer err_tracer(FROM_HERE); 24 OpenSSLErrStackTracer err_tracer(FROM_HERE);
26 25
27 ScopedRSA rsa_key(RSA_new()); 26 bssl::UniquePtr<RSA> rsa_key(RSA_new());
28 ScopedBIGNUM bn(BN_new()); 27 bssl::UniquePtr<BIGNUM> bn(BN_new());
29 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L)) 28 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
30 return nullptr; 29 return nullptr;
31 30
32 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), nullptr)) 31 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), nullptr))
33 return nullptr; 32 return nullptr;
34 33
35 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey); 34 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
36 result->key_ = EVP_PKEY_new(); 35 result->key_.reset(EVP_PKEY_new());
37 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) 36 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_.get(), rsa_key.get()))
38 return nullptr; 37 return nullptr;
39 38
40 return result; 39 return result;
41 } 40 }
42 41
43 // static 42 // static
44 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromPrivateKeyInfo( 43 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromPrivateKeyInfo(
45 const std::vector<uint8_t>& input) { 44 const std::vector<uint8_t>& input) {
46 OpenSSLErrStackTracer err_tracer(FROM_HERE); 45 OpenSSLErrStackTracer err_tracer(FROM_HERE);
47 46
48 CBS cbs; 47 CBS cbs;
49 CBS_init(&cbs, input.data(), input.size()); 48 CBS_init(&cbs, input.data(), input.size());
50 ScopedEVP_PKEY pkey(EVP_parse_private_key(&cbs)); 49 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) 50 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_RSA)
52 return nullptr; 51 return nullptr;
53 52
54 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey); 53 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
55 result->key_ = pkey.release(); 54 result->key_ = std::move(pkey);
56 return result; 55 return result;
57 } 56 }
58 57
59 // static 58 // static
60 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromKey(EVP_PKEY* key) { 59 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::CreateFromKey(EVP_PKEY* key) {
61 DCHECK(key); 60 DCHECK(key);
62 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA) 61 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA)
63 return nullptr; 62 return nullptr;
64 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey); 63 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
65 EVP_PKEY_up_ref(key); 64 EVP_PKEY_up_ref(key);
66 copy->key_ = key; 65 copy->key_.reset(key);
67 return copy; 66 return copy;
68 } 67 }
69 68
70 RSAPrivateKey::RSAPrivateKey() : key_(nullptr) {} 69 RSAPrivateKey::RSAPrivateKey() {}
71 70
72 RSAPrivateKey::~RSAPrivateKey() { 71 RSAPrivateKey::~RSAPrivateKey() {}
73 if (key_)
74 EVP_PKEY_free(key_);
75 }
76 72
77 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Copy() const { 73 std::unique_ptr<RSAPrivateKey> RSAPrivateKey::Copy() const {
78 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey); 74 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey);
79 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_)); 75 bssl::UniquePtr<RSA> rsa(EVP_PKEY_get1_RSA(key_.get()));
80 if (!rsa) 76 if (!rsa)
81 return nullptr; 77 return nullptr;
82 copy->key_ = EVP_PKEY_new(); 78 copy->key_.reset(EVP_PKEY_new());
83 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get())) 79 if (!EVP_PKEY_set1_RSA(copy->key_.get(), rsa.get()))
84 return nullptr; 80 return nullptr;
85 return copy; 81 return copy;
86 } 82 }
87 83
88 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const { 84 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
89 OpenSSLErrStackTracer err_tracer(FROM_HERE); 85 OpenSSLErrStackTracer err_tracer(FROM_HERE);
90 uint8_t *der; 86 uint8_t *der;
91 size_t der_len; 87 size_t der_len;
92 AutoCBB cbb; 88 bssl::ScopedCBB cbb;
93 if (!CBB_init(cbb.get(), 0) || 89 if (!CBB_init(cbb.get(), 0) ||
94 !EVP_marshal_private_key(cbb.get(), key_) || 90 !EVP_marshal_private_key(cbb.get(), key_.get()) ||
95 !CBB_finish(cbb.get(), &der, &der_len)) { 91 !CBB_finish(cbb.get(), &der, &der_len)) {
96 return false; 92 return false;
97 } 93 }
98 output->assign(der, der + der_len); 94 output->assign(der, der + der_len);
99 OPENSSL_free(der); 95 OPENSSL_free(der);
100 return true; 96 return true;
101 } 97 }
102 98
103 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const { 99 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
104 OpenSSLErrStackTracer err_tracer(FROM_HERE); 100 OpenSSLErrStackTracer err_tracer(FROM_HERE);
105 uint8_t *der; 101 uint8_t *der;
106 size_t der_len; 102 size_t der_len;
107 AutoCBB cbb; 103 bssl::ScopedCBB cbb;
108 if (!CBB_init(cbb.get(), 0) || 104 if (!CBB_init(cbb.get(), 0) ||
109 !EVP_marshal_public_key(cbb.get(), key_) || 105 !EVP_marshal_public_key(cbb.get(), key_.get()) ||
110 !CBB_finish(cbb.get(), &der, &der_len)) { 106 !CBB_finish(cbb.get(), &der, &der_len)) {
111 return false; 107 return false;
112 } 108 }
113 output->assign(der, der + der_len); 109 output->assign(der, der + der_len);
114 OPENSSL_free(der); 110 OPENSSL_free(der);
115 return true; 111 return true;
116 } 112 }
117 113
118 } // namespace crypto 114 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/rsa_private_key.h ('k') | crypto/signature_creator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698