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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 61 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
62 result->key_ = EVP_PKEY_new(); | 62 result->key_ = EVP_PKEY_new(); |
63 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) | 63 if (!result->key_ || !EVP_PKEY_set1_RSA(result->key_, rsa_key.get())) |
64 return NULL; | 64 return NULL; |
65 | 65 |
66 return result.release(); | 66 return result.release(); |
67 } | 67 } |
68 | 68 |
69 // static | 69 // static |
70 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { | |
71 NOTIMPLEMENTED(); | |
72 return NULL; | |
73 } | |
74 | |
75 // static | |
76 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 70 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
77 const std::vector<uint8>& input) { | 71 const std::vector<uint8>& input) { |
78 if (input.empty()) | 72 if (input.empty()) |
79 return NULL; | 73 return NULL; |
80 | 74 |
81 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 75 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
82 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. | 76 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. |
83 char* data = reinterpret_cast<char*>(const_cast<uint8*>(&input[0])); | 77 char* data = reinterpret_cast<char*>(const_cast<uint8*>(&input[0])); |
84 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size())); | 78 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, input.size())); |
85 if (!bio.get()) | 79 if (!bio.get()) |
86 return NULL; | 80 return NULL; |
87 | 81 |
88 // Importing is a little more involved than exporting, as we must first | 82 // 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 | 83 // PKCS#8 decode the input, and then import the EVP_PKEY from Private Key |
90 // Info structure returned. | 84 // Info structure returned. |
91 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf( | 85 ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf( |
92 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL)); | 86 d2i_PKCS8_PRIV_KEY_INFO_bio(bio.get(), NULL)); |
93 if (!p8inf.get()) | 87 if (!p8inf.get()) |
94 return NULL; | 88 return NULL; |
95 | 89 |
96 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 90 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
97 result->key_ = EVP_PKCS82PKEY(p8inf.get()); | 91 result->key_ = EVP_PKCS82PKEY(p8inf.get()); |
98 if (!result->key_) | 92 if (!result->key_) |
99 return NULL; | 93 return NULL; |
100 | 94 |
101 return result.release(); | 95 return result.release(); |
102 } | 96 } |
103 | 97 |
104 // static | |
105 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( | |
106 const std::vector<uint8>& input) { | |
107 NOTIMPLEMENTED(); | |
108 return NULL; | |
109 } | |
110 | |
111 // static | |
112 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( | |
113 const std::vector<uint8>& input) { | |
114 NOTIMPLEMENTED(); | |
115 return NULL; | |
116 } | |
117 | |
118 RSAPrivateKey::RSAPrivateKey() | 98 RSAPrivateKey::RSAPrivateKey() |
119 : key_(NULL) { | 99 : key_(NULL) { |
120 } | 100 } |
121 | 101 |
122 RSAPrivateKey::~RSAPrivateKey() { | 102 RSAPrivateKey::~RSAPrivateKey() { |
123 if (key_) | 103 if (key_) |
124 EVP_PKEY_free(key_); | 104 EVP_PKEY_free(key_); |
125 } | 105 } |
126 | 106 |
127 RSAPrivateKey* RSAPrivateKey::Copy() const { | 107 RSAPrivateKey* RSAPrivateKey::Copy() const { |
128 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); | 108 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); |
129 RSA* rsa = EVP_PKEY_get1_RSA(key_); | 109 RSA* rsa = EVP_PKEY_get1_RSA(key_); |
130 if (!rsa) | 110 if (!rsa) |
131 return NULL; | 111 return NULL; |
132 copy->key_ = EVP_PKEY_new(); | 112 copy->key_ = EVP_PKEY_new(); |
133 if (!EVP_PKEY_set1_RSA(copy->key_, rsa)) | 113 if (!EVP_PKEY_set1_RSA(copy->key_, rsa)) |
134 return NULL; | 114 return NULL; |
135 return copy.release(); | 115 return copy.release(); |
136 } | 116 } |
137 | 117 |
138 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 118 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
139 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); | 119 return ExportKey(key_, i2d_PKCS8PrivateKeyInfo_bio, output); |
140 } | 120 } |
141 | 121 |
142 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { | 122 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { |
143 return ExportKey(key_, i2d_PUBKEY_bio, output); | 123 return ExportKey(key_, i2d_PUBKEY_bio, output); |
144 } | 124 } |
145 | 125 |
146 } // namespace crypto | 126 } // namespace crypto |
OLD | NEW |