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

Side by Side Diff: crypto/rsa_private_key_openssl.cc

Issue 1909513003: Rename crypto/ *_openssl files (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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.cc ('k') | crypto/secure_hash.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 <openssl/bn.h>
8 #include <openssl/bytestring.h>
9 #include <openssl/evp.h>
10 #include <openssl/mem.h>
11 #include <openssl/rsa.h>
12 #include <stdint.h>
13
14 #include <memory>
15
16 #include "base/logging.h"
17 #include "crypto/auto_cbb.h"
18 #include "crypto/openssl_util.h"
19 #include "crypto/scoped_openssl_types.h"
20
21 namespace crypto {
22
23 // static
24 RSAPrivateKey* RSAPrivateKey::Create(uint16_t num_bits) {
25 OpenSSLErrStackTracer err_tracer(FROM_HERE);
26
27 ScopedRSA rsa_key(RSA_new());
28 ScopedBIGNUM bn(BN_new());
29 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L))
30 return NULL;
31
32 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL))
33 return NULL;
34
35 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey);
36 result->key_ = EVP_PKEY_new();
37 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get()))
38 return NULL;
39
40 return result.release();
41 }
42
43 // static
44 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
45 const std::vector<uint8_t>& input) {
46 OpenSSLErrStackTracer err_tracer(FROM_HERE);
47
48 CBS cbs;
49 CBS_init(&cbs, input.data(), input.size());
50 ScopedEVP_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_ = pkey.release();
56 return result.release();
57 }
58
59 // static
60 RSAPrivateKey* RSAPrivateKey::CreateFromKey(EVP_PKEY* key) {
61 DCHECK(key);
62 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA)
63 return NULL;
64 RSAPrivateKey* copy = new RSAPrivateKey();
65 copy->key_ = EVP_PKEY_up_ref(key);
66 return copy;
67 }
68
69 RSAPrivateKey::RSAPrivateKey()
70 : key_(NULL) {
71 }
72
73 RSAPrivateKey::~RSAPrivateKey() {
74 if (key_)
75 EVP_PKEY_free(key_);
76 }
77
78 RSAPrivateKey* RSAPrivateKey::Copy() const {
79 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey());
80 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_));
81 if (!rsa)
82 return NULL;
83 copy->key_ = EVP_PKEY_new();
84 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get()))
85 return NULL;
86 return copy.release();
87 }
88
89 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
90 uint8_t *der;
91 size_t der_len;
92 AutoCBB cbb;
93 if (!CBB_init(cbb.get(), 0) ||
94 !EVP_marshal_private_key(cbb.get(), key_) ||
95 !CBB_finish(cbb.get(), &der, &der_len)) {
96 return false;
97 }
98 output->assign(der, der + der_len);
99 OPENSSL_free(der);
100 return true;
101 }
102
103 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
104 uint8_t *der;
105 size_t der_len;
106 AutoCBB cbb;
107 if (!CBB_init(cbb.get(), 0) ||
108 !EVP_marshal_public_key(cbb.get(), key_) ||
109 !CBB_finish(cbb.get(), &der, &der_len)) {
110 return false;
111 }
112 output->assign(der, der + der_len);
113 OPENSSL_free(der);
114 return true;
115 }
116
117 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/rsa_private_key.cc ('k') | crypto/secure_hash.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698