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/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 22 matching lines...) Expand all Loading... | |
33 | 33 |
34 int res = export_fn(bio.get(), key); | 34 int res = export_fn(bio.get(), key); |
35 if (!res) | 35 if (!res) |
36 return false; | 36 return false; |
37 | 37 |
38 char* data = NULL; | 38 char* data = NULL; |
39 long len = BIO_get_mem_data(bio.get(), &data); | 39 long len = BIO_get_mem_data(bio.get(), &data); |
40 if (!data || len < 0) | 40 if (!data || len < 0) |
41 return false; | 41 return false; |
42 | 42 |
43 std::vector<uint8> for_output(data, data + len); | 43 output->assign(data, data + len); |
44 output->swap(for_output); | |
45 return true; | 44 return true; |
46 } | 45 } |
47 | 46 |
48 } // namespace | 47 } // namespace |
49 | 48 |
50 // static | 49 // static |
51 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 50 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
52 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 51 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
53 | 52 |
54 ScopedOpenSSL<RSA, RSA_free> rsa_key(RSA_new()); | 53 ScopedOpenSSL<RSA, RSA_free> rsa_key(RSA_new()); |
(...skipping 14 matching lines...) Expand all Loading... | |
69 | 68 |
70 // static | 69 // static |
71 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { | 70 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { |
72 NOTIMPLEMENTED(); | 71 NOTIMPLEMENTED(); |
73 return NULL; | 72 return NULL; |
74 } | 73 } |
75 | 74 |
76 // static | 75 // static |
77 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 76 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
78 const std::vector<uint8>& input) { | 77 const std::vector<uint8>& input) { |
78 if (input.empty()) | |
79 return NULL; | |
80 | |
79 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 81 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
80 | |
81 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. | 82 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. |
82 char* data = reinterpret_cast<char*>(const_cast<uint8*>( | 83 char* data = reinterpret_cast<char*>(const_cast<uint8*>(&input[0])); |
83 vector_as_array(&input))); | |
Denis Lagno
2011/07/20 11:13:59
some methods from stl_util are still widely used a
| |
84 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size())); | 84 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size())); |
85 if (!bio.get()) | 85 if (!bio.get()) |
86 return NULL; | 86 return NULL; |
87 | 87 |
88 // Importing is a little more involved than exporting, as we must first | 88 // Importing is a little more involved than exporting, as we must first |
89 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key | 89 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key |
90 // Info structure returned. | 90 // Info structure returned. |
91 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf( | 91 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf( |
92 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL)); | 92 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL)); |
93 if (!p8inf.get()) | 93 if (!p8inf.get()) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 | 126 |
127 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 127 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { |
128 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); | 128 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); |
129 } | 129 } |
130 | 130 |
131 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { | 131 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { |
132 return ExportKey(key_, i2d_PUBKEY_bio, output); | 132 return ExportKey(key_, i2d_PUBKEY_bio, output); |
133 } | 133 } |
134 | 134 |
135 } // namespace crypto | 135 } // namespace crypto |
OLD | NEW |