| 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 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "crypto/openssl_util.h" | 15 #include "crypto/openssl_util.h" |
| 16 #include "crypto/scoped_openssl_types.h" | 16 #include "crypto/scoped_openssl_types.h" |
| 17 | 17 |
| 18 namespace crypto { | 18 namespace crypto { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 typedef ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>::Type |
| 23 ScopedPKCS8_PRIV_KEY_INFO; |
| 24 |
| 22 // Function pointer definition, for injecting the required key export function | 25 // Function pointer definition, for injecting the required key export function |
| 23 // into ExportKey, below. The supplied function should export EVP_PKEY into | 26 // into ExportKey, below. The supplied function should export EVP_PKEY into |
| 24 // the supplied BIO, returning 1 on success or 0 on failure. | 27 // the supplied BIO, returning 1 on success or 0 on failure. |
| 25 typedef int (ExportFunction)(BIO*, EVP_PKEY*); | 28 typedef int (ExportFunction)(BIO*, EVP_PKEY*); |
| 26 | 29 |
| 27 // Helper to export |key| into |output| via the specified ExportFunction. | 30 // Helper to export |key| into |output| via the specified ExportFunction. |
| 28 bool ExportKey(EVP_PKEY* key, | 31 bool ExportKey(EVP_PKEY* key, |
| 29 ExportFunction export_fn, | 32 ExportFunction export_fn, |
| 30 std::vector<uint8>* output) { | 33 std::vector<uint8>* output) { |
| 31 if (!key) | 34 if (!key) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 return result.release(); | 72 return result.release(); |
| 70 } | 73 } |
| 71 | 74 |
| 72 // static | 75 // static |
| 73 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 76 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
| 74 const std::vector<uint8>& input) { | 77 const std::vector<uint8>& input) { |
| 75 if (input.empty()) | 78 if (input.empty()) |
| 76 return NULL; | 79 return NULL; |
| 77 | 80 |
| 78 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 81 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 79 // 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[0])); | |
| 81 ScopedBIO bio(BIO_new_mem_buf(data, input.size())); | |
| 82 if (!bio.get()) | |
| 83 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>::Type p8inf( | 86 // |
| 89 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL)); | 87 // TODO(davidben): This should check that |ptr| advanced to the end of |input| |
| 88 // to ensure there is no trailing data. |
| 89 const uint8_t* ptr = &input[0]; |
| 90 ScopedPKCS8_PRIV_KEY_INFO p8inf( |
| 91 d2i_PKCS8_PRIV_KEY_INFO(nullptr, &ptr, input.size())); |
| 90 if (!p8inf.get()) | 92 if (!p8inf.get()) |
| 91 return NULL; | 93 return NULL; |
| 92 | 94 |
| 93 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 95 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 94 result->key_ = EVP_PKCS82PKEY(p8inf.get()); | 96 result->key_ = EVP_PKCS82PKEY(p8inf.get()); |
| 95 if (!result->key_) | 97 if (!result->key_) |
| 96 return NULL; | 98 return NULL; |
| 97 | 99 |
| 98 return result.release(); | 100 return result.release(); |
| 99 } | 101 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 130 | 132 |
| 131 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 133 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
| 132 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); | 134 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); |
| 133 } | 135 } |
| 134 | 136 |
| 135 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { | 137 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { |
| 136 return ExportKey(key_, i2d_PUBKEY_bio, output); | 138 return ExportKey(key_, i2d_PUBKEY_bio, output); |
| 137 } | 139 } |
| 138 | 140 |
| 139 } // namespace crypto | 141 } // namespace crypto |
| OLD | NEW |