OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/crypto/rsa_private_key.h" | 5 #include "base/crypto/rsa_private_key.h" |
6 | 6 |
7 #include <openssl/evp.h> | 7 #include <openssl/evp.h> |
8 #include <openssl/pkcs12.h> | 8 #include <openssl/pkcs12.h> |
9 #include <openssl/rsa.h> | 9 #include <openssl/rsa.h> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 // the supplied BIO, returning 1 on success or 0 on failure. | 22 // the supplied BIO, returning 1 on success or 0 on failure. |
23 typedef int (ExportFunction)(BIO*, EVP_PKEY*); | 23 typedef int (ExportFunction)(BIO*, EVP_PKEY*); |
24 | 24 |
25 // Helper to export |key| into |output| via the specified ExportFunction. | 25 // Helper to export |key| into |output| via the specified ExportFunction. |
26 bool ExportKey(EVP_PKEY* key, | 26 bool ExportKey(EVP_PKEY* key, |
27 ExportFunction export_fn, | 27 ExportFunction export_fn, |
28 std::vector<uint8>* output) { | 28 std::vector<uint8>* output) { |
29 if (!key) | 29 if (!key) |
30 return false; | 30 return false; |
31 | 31 |
| 32 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
32 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new(BIO_s_mem())); | 33 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new(BIO_s_mem())); |
33 | 34 |
34 int res = export_fn(bio.get(), key); | 35 int res = export_fn(bio.get(), key); |
35 ClearOpenSSLERRStack(); | |
36 if (!res) | 36 if (!res) |
37 return false; | 37 return false; |
38 | 38 |
39 char* data = NULL; | 39 char* data = NULL; |
40 long len = BIO_get_mem_data(bio.get(), &data); | 40 long len = BIO_get_mem_data(bio.get(), &data); |
41 if (!data || len < 0) | 41 if (!data || len < 0) |
42 return false; | 42 return false; |
43 | 43 |
44 STLAssignToVector(output, reinterpret_cast<const uint8*>(data), len); | 44 STLAssignToVector(output, reinterpret_cast<const uint8*>(data), len); |
45 return true; | 45 return true; |
46 } | 46 } |
47 | 47 |
48 } // namespace | 48 } // namespace |
49 | 49 |
50 // static | 50 // static |
51 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 51 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
52 EnsureOpenSSLInit(); | 52 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
53 | |
54 ScopedOpenSSL<RSA, RSA_free> rsa_key(RSA_generate_key(num_bits, 65537L, | 53 ScopedOpenSSL<RSA, RSA_free> rsa_key(RSA_generate_key(num_bits, 65537L, |
55 NULL, NULL)); | 54 NULL, NULL)); |
56 ClearOpenSSLERRStack(); | |
57 if (!rsa_key.get()) | 55 if (!rsa_key.get()) |
58 return NULL; | 56 return NULL; |
59 | 57 |
60 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 58 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
61 result->key_ = EVP_PKEY_new(); | 59 result->key_ = EVP_PKEY_new(); |
62 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) | 60 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) |
63 return NULL; | 61 return NULL; |
64 | 62 |
65 return result.release(); | 63 return result.release(); |
66 } | 64 } |
67 | 65 |
68 // static | 66 // static |
69 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { | 67 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { |
70 NOTIMPLEMENTED(); | 68 NOTIMPLEMENTED(); |
71 return NULL; | 69 return NULL; |
72 } | 70 } |
73 | 71 |
74 // static | 72 // static |
75 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 73 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
76 const std::vector<uint8>& input) { | 74 const std::vector<uint8>& input) { |
77 EnsureOpenSSLInit(); | 75 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
78 | 76 |
79 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. | 77 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. |
80 char* data = reinterpret_cast<char*>(const_cast<uint8*>(input.data())); | 78 char* data = reinterpret_cast<char*>(const_cast<uint8*>(input.data())); |
81 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size())); | 79 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size())); |
82 if (!bio.get()) | 80 if (!bio.get()) |
83 return NULL; | 81 return NULL; |
84 | 82 |
85 // Importing is a little more involved than exporting, as we must first | 83 // Importing is a little more involved than exporting, as we must first |
86 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key | 84 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key |
87 // Info structure returned. | 85 // Info structure returned. |
88 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf( | 86 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf( |
89 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL)); | 87 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL)); |
90 ClearOpenSSLERRStack(); | |
91 if (!p8inf.get()) | 88 if (!p8inf.get()) |
92 return NULL; | 89 return NULL; |
93 | 90 |
94 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 91 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
95 result->key_ = EVP_PKCS82PKEY(p8inf.get()); | 92 result->key_ = EVP_PKCS82PKEY(p8inf.get()); |
96 ClearOpenSSLERRStack(); | |
97 if (!result->key_) | 93 if (!result->key_) |
98 return NULL; | 94 return NULL; |
99 | 95 |
100 return result.release(); | 96 return result.release(); |
101 } | 97 } |
102 | 98 |
103 // static | 99 // static |
104 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( | 100 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( |
105 const std::vector<uint8>& input) { | 101 const std::vector<uint8>& input) { |
106 NOTIMPLEMENTED(); | 102 NOTIMPLEMENTED(); |
(...skipping 18 matching lines...) Expand all Loading... |
125 | 121 |
126 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 122 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { |
127 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); | 123 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); |
128 } | 124 } |
129 | 125 |
130 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { | 126 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { |
131 return ExportKey(key_, i2d_PUBKEY_bio, output); | 127 return ExportKey(key_, i2d_PUBKEY_bio, output); |
132 } | 128 } |
133 | 129 |
134 } // namespace base | 130 } // namespace base |
OLD | NEW |