| 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/bn.h> |
| 7 #include <openssl/bytestring.h> | 8 #include <openssl/bytestring.h> |
| 8 #include <openssl/bn.h> | |
| 9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
| 10 #include <openssl/mem.h> | 10 #include <openssl/mem.h> |
| 11 #include <openssl/rsa.h> | 11 #include <openssl/rsa.h> |
| 12 #include <stdint.h> | 12 #include <stdint.h> |
| 13 | 13 |
| 14 #include <memory> |
| 15 |
| 14 #include "base/logging.h" | 16 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "crypto/auto_cbb.h" | 17 #include "crypto/auto_cbb.h" |
| 17 #include "crypto/openssl_util.h" | 18 #include "crypto/openssl_util.h" |
| 18 #include "crypto/scoped_openssl_types.h" | 19 #include "crypto/scoped_openssl_types.h" |
| 19 | 20 |
| 20 namespace crypto { | 21 namespace crypto { |
| 21 | 22 |
| 22 // static | 23 // static |
| 23 RSAPrivateKey* RSAPrivateKey::Create(uint16_t num_bits) { | 24 RSAPrivateKey* RSAPrivateKey::Create(uint16_t num_bits) { |
| 24 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 25 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 25 | 26 |
| 26 ScopedRSA rsa_key(RSA_new()); | 27 ScopedRSA rsa_key(RSA_new()); |
| 27 ScopedBIGNUM bn(BN_new()); | 28 ScopedBIGNUM bn(BN_new()); |
| 28 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L)) | 29 if (!rsa_key.get() || !bn.get() || !BN_set_word(bn.get(), 65537L)) |
| 29 return NULL; | 30 return NULL; |
| 30 | 31 |
| 31 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL)) | 32 if (!RSA_generate_key_ex(rsa_key.get(), num_bits, bn.get(), NULL)) |
| 32 return NULL; | 33 return NULL; |
| 33 | 34 |
| 34 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 35 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 35 result->key_ = EVP_PKEY_new(); | 36 result->key_ = EVP_PKEY_new(); |
| 36 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) | 37 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) |
| 37 return NULL; | 38 return NULL; |
| 38 | 39 |
| 39 return result.release(); | 40 return result.release(); |
| 40 } | 41 } |
| 41 | 42 |
| 42 // static | 43 // static |
| 43 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 44 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
| 44 const std::vector<uint8_t>& input) { | 45 const std::vector<uint8_t>& input) { |
| 45 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 46 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 46 | 47 |
| 47 CBS cbs; | 48 CBS cbs; |
| 48 CBS_init(&cbs, input.data(), input.size()); | 49 CBS_init(&cbs, input.data(), input.size()); |
| 49 ScopedEVP_PKEY pkey(EVP_parse_private_key(&cbs)); | 50 ScopedEVP_PKEY pkey(EVP_parse_private_key(&cbs)); |
| 50 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_RSA) | 51 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_RSA) |
| 51 return nullptr; | 52 return nullptr; |
| 52 | 53 |
| 53 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 54 std::unique_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 54 result->key_ = pkey.release(); | 55 result->key_ = pkey.release(); |
| 55 return result.release(); | 56 return result.release(); |
| 56 } | 57 } |
| 57 | 58 |
| 58 // static | 59 // static |
| 59 RSAPrivateKey* RSAPrivateKey::CreateFromKey(EVP_PKEY* key) { | 60 RSAPrivateKey* RSAPrivateKey::CreateFromKey(EVP_PKEY* key) { |
| 60 DCHECK(key); | 61 DCHECK(key); |
| 61 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA) | 62 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA) |
| 62 return NULL; | 63 return NULL; |
| 63 RSAPrivateKey* copy = new RSAPrivateKey(); | 64 RSAPrivateKey* copy = new RSAPrivateKey(); |
| 64 copy->key_ = EVP_PKEY_up_ref(key); | 65 copy->key_ = EVP_PKEY_up_ref(key); |
| 65 return copy; | 66 return copy; |
| 66 } | 67 } |
| 67 | 68 |
| 68 RSAPrivateKey::RSAPrivateKey() | 69 RSAPrivateKey::RSAPrivateKey() |
| 69 : key_(NULL) { | 70 : key_(NULL) { |
| 70 } | 71 } |
| 71 | 72 |
| 72 RSAPrivateKey::~RSAPrivateKey() { | 73 RSAPrivateKey::~RSAPrivateKey() { |
| 73 if (key_) | 74 if (key_) |
| 74 EVP_PKEY_free(key_); | 75 EVP_PKEY_free(key_); |
| 75 } | 76 } |
| 76 | 77 |
| 77 RSAPrivateKey* RSAPrivateKey::Copy() const { | 78 RSAPrivateKey* RSAPrivateKey::Copy() const { |
| 78 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); | 79 std::unique_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); |
| 79 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_)); | 80 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_)); |
| 80 if (!rsa) | 81 if (!rsa) |
| 81 return NULL; | 82 return NULL; |
| 82 copy->key_ = EVP_PKEY_new(); | 83 copy->key_ = EVP_PKEY_new(); |
| 83 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get())) | 84 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get())) |
| 84 return NULL; | 85 return NULL; |
| 85 return copy.release(); | 86 return copy.release(); |
| 86 } | 87 } |
| 87 | 88 |
| 88 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const { | 89 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 107 !EVP_marshal_public_key(cbb.get(), key_) || | 108 !EVP_marshal_public_key(cbb.get(), key_) || |
| 108 !CBB_finish(cbb.get(), &der, &der_len)) { | 109 !CBB_finish(cbb.get(), &der, &der_len)) { |
| 109 return false; | 110 return false; |
| 110 } | 111 } |
| 111 output->assign(der, der + der_len); | 112 output->assign(der, der + der_len); |
| 112 OPENSSL_free(der); | 113 OPENSSL_free(der); |
| 113 return true; | 114 return true; |
| 114 } | 115 } |
| 115 | 116 |
| 116 } // namespace crypto | 117 } // namespace crypto |
| OLD | NEW |