| 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> |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 return NULL; | 91 return NULL; |
| 92 | 92 |
| 93 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 93 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 94 result->key_ = EVP_PKCS82PKEY(p8inf.get()); | 94 result->key_ = EVP_PKCS82PKEY(p8inf.get()); |
| 95 if (!result->key_) | 95 if (!result->key_) |
| 96 return NULL; | 96 return NULL; |
| 97 | 97 |
| 98 return result.release(); | 98 return result.release(); |
| 99 } | 99 } |
| 100 | 100 |
| 101 // static |
| 102 RSAPrivateKey* RSAPrivateKey::CreateFromKey(EVP_PKEY* key) { |
| 103 DCHECK(key); |
| 104 if (EVP_PKEY_type(key->type) != EVP_PKEY_RSA) |
| 105 return NULL; |
| 106 RSAPrivateKey* copy = new RSAPrivateKey(); |
| 107 copy->key_ = EVP_PKEY_dup(key); |
| 108 return copy; |
| 109 } |
| 110 |
| 101 RSAPrivateKey::RSAPrivateKey() | 111 RSAPrivateKey::RSAPrivateKey() |
| 102 : key_(NULL) { | 112 : key_(NULL) { |
| 103 } | 113 } |
| 104 | 114 |
| 105 RSAPrivateKey::~RSAPrivateKey() { | 115 RSAPrivateKey::~RSAPrivateKey() { |
| 106 if (key_) | 116 if (key_) |
| 107 EVP_PKEY_free(key_); | 117 EVP_PKEY_free(key_); |
| 108 } | 118 } |
| 109 | 119 |
| 110 RSAPrivateKey* RSAPrivateKey::Copy() const { | 120 RSAPrivateKey* RSAPrivateKey::Copy() const { |
| 111 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); | 121 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); |
| 112 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_)); | 122 ScopedRSA rsa(EVP_PKEY_get1_RSA(key_)); |
| 113 if (!rsa) | 123 if (!rsa) |
| 114 return NULL; | 124 return NULL; |
| 115 copy->key_ = EVP_PKEY_new(); | 125 copy->key_ = EVP_PKEY_new(); |
| 116 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get())) | 126 if (!EVP_PKEY_set1_RSA(copy->key_, rsa.get())) |
| 117 return NULL; | 127 return NULL; |
| 118 return copy.release(); | 128 return copy.release(); |
| 119 } | 129 } |
| 120 | 130 |
| 121 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 131 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
| 122 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); | 132 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); |
| 123 } | 133 } |
| 124 | 134 |
| 125 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { | 135 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { |
| 126 return ExportKey(key_, i2d_PUBKEY_bio, output); | 136 return ExportKey(key_, i2d_PUBKEY_bio, output); |
| 127 } | 137 } |
| 128 | 138 |
| 129 } // namespace crypto | 139 } // namespace crypto |
| OLD | NEW |