OLD | NEW |
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/bio.h> | 7 #include <openssl/bio.h> |
8 #include <openssl/bn.h> | 8 #include <openssl/bn.h> |
9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
10 #include <openssl/pkcs12.h> | 10 #include <openssl/pkcs12.h> |
11 #include <openssl/rsa.h> | 11 #include <openssl/rsa.h> |
| 12 #include <stdint.h> |
12 | 13 |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "crypto/openssl_util.h" | 16 #include "crypto/openssl_util.h" |
16 #include "crypto/scoped_openssl_types.h" | 17 #include "crypto/scoped_openssl_types.h" |
17 | 18 |
18 namespace crypto { | 19 namespace crypto { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 using ScopedPKCS8_PRIV_KEY_INFO = | 23 using ScopedPKCS8_PRIV_KEY_INFO = |
23 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>; | 24 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>; |
24 | 25 |
25 // Function pointer definition, for injecting the required key export function | 26 // Function pointer definition, for injecting the required key export function |
26 // into ExportKey, below. The supplied function should export EVP_PKEY into | 27 // into ExportKey, below. The supplied function should export EVP_PKEY into |
27 // the supplied BIO, returning 1 on success or 0 on failure. | 28 // the supplied BIO, returning 1 on success or 0 on failure. |
28 using ExportFunction = int (*)(BIO*, EVP_PKEY*); | 29 using ExportFunction = int (*)(BIO*, EVP_PKEY*); |
29 | 30 |
30 // Helper to export |key| into |output| via the specified ExportFunction. | 31 // Helper to export |key| into |output| via the specified ExportFunction. |
31 bool ExportKey(EVP_PKEY* key, | 32 bool ExportKey(EVP_PKEY* key, |
32 ExportFunction export_fn, | 33 ExportFunction export_fn, |
33 std::vector<uint8>* output) { | 34 std::vector<uint8_t>* output) { |
34 if (!key) | 35 if (!key) |
35 return false; | 36 return false; |
36 | 37 |
37 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 38 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
38 ScopedBIO bio(BIO_new(BIO_s_mem())); | 39 ScopedBIO bio(BIO_new(BIO_s_mem())); |
39 | 40 |
40 int res = export_fn(bio.get(), key); | 41 int res = export_fn(bio.get(), key); |
41 if (!res) | 42 if (!res) |
42 return false; | 43 return false; |
43 | 44 |
44 char* data = NULL; | 45 char* data = NULL; |
45 long len = BIO_get_mem_data(bio.get(), &data); | 46 long len = BIO_get_mem_data(bio.get(), &data); |
46 if (!data || len < 0) | 47 if (!data || len < 0) |
47 return false; | 48 return false; |
48 | 49 |
49 output->assign(data, data + len); | 50 output->assign(data, data + len); |
50 return true; | 51 return true; |
51 } | 52 } |
52 | 53 |
53 } // namespace | 54 } // namespace |
54 | 55 |
55 // static | 56 // static |
56 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 57 RSAPrivateKey* RSAPrivateKey::Create(uint16_t num_bits) { |
57 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 58 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
58 | 59 |
59 ScopedRSA rsa_key(RSA_new()); | 60 ScopedRSA rsa_key(RSA_new()); |
60 ScopedBIGNUM bn(BN_new()); | 61 ScopedBIGNUM bn(BN_new()); |
61 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L)) | 62 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L)) |
62 return NULL; | 63 return NULL; |
63 | 64 |
64 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL)) | 65 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL)) |
65 return NULL; | 66 return NULL; |
66 | 67 |
67 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 68 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
68 result->key_ = EVP_PKEY_new(); | 69 result->key_ = EVP_PKEY_new(); |
69 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) | 70 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) |
70 return NULL; | 71 return NULL; |
71 | 72 |
72 return result.release(); | 73 return result.release(); |
73 } | 74 } |
74 | 75 |
75 // static | 76 // static |
76 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 77 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
77 const std::vector<uint8>& input) { | 78 const std::vector<uint8_t>& input) { |
78 if (input.empty()) | 79 if (input.empty()) |
79 return NULL; | 80 return NULL; |
80 | 81 |
81 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 82 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
82 | 83 |
83 // Importing is a little more involved than exporting, as we must first | 84 // Importing is a little more involved than exporting, as we must first |
84 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key | 85 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key |
85 // Info structure returned. | 86 // Info structure returned. |
86 const uint8_t* ptr = &input[0]; | 87 const uint8_t* ptr = &input[0]; |
87 ScopedPKCS8_PRIV_KEY_INFO p8inf( | 88 ScopedPKCS8_PRIV_KEY_INFO p8inf( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); | 121 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); |
121 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_)); | 122 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_)); |
122 if (!rsa) | 123 if (!rsa) |
123 return NULL; | 124 return NULL; |
124 copy->key_ = EVP_PKEY_new(); | 125 copy->key_ = EVP_PKEY_new(); |
125 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get())) | 126 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get())) |
126 return NULL; | 127 return NULL; |
127 return copy.release(); | 128 return copy.release(); |
128 } | 129 } |
129 | 130 |
130 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 131 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const { |
131 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); | 132 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); |
132 } | 133 } |
133 | 134 |
134 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { | 135 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const { |
135 return ExportKey(key_, i2d_PUBKEY_bio, output); | 136 return ExportKey(key_, i2d_PUBKEY_bio, output); |
136 } | 137 } |
137 | 138 |
138 } // namespace crypto | 139 } // namespace crypto |
OLD | NEW |