OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ | 5 #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ |
6 #define CRYPTO_RSA_PRIVATE_KEY_H_ | 6 #define CRYPTO_RSA_PRIVATE_KEY_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #include <list> | 10 #include <list> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "crypto/crypto_export.h" | 14 #include "crypto/crypto_export.h" |
15 | 15 |
16 #if defined(USE_NSS) | 16 #if defined(USE_NSS) |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
18 #endif | 18 #endif |
19 | 19 |
20 #if defined(USE_OPENSSL) | 20 #if defined(USE_OPENSSL) |
21 // Forward declaration for openssl/*.h | 21 // Forward declaration for openssl/*.h |
22 typedef struct evp_pkey_st EVP_PKEY; | 22 typedef struct evp_pkey_st EVP_PKEY; |
23 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | 23 #else |
24 // Forward declaration. | 24 // Forward declaration. |
| 25 typedef struct PK11SlotInfoStr PK11SlotInfo; |
25 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; | 26 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; |
26 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; | 27 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; |
27 #endif | 28 #endif |
28 | 29 |
29 | 30 |
30 namespace crypto { | 31 namespace crypto { |
31 | 32 |
32 // Used internally by RSAPrivateKey for serializing and deserializing | 33 // Used internally by RSAPrivateKey for serializing and deserializing |
33 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. | 34 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. |
34 class PrivateKeyInfoCodec { | 35 class PrivateKeyInfoCodec { |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 // Create a new random instance. Can return NULL if initialization fails. | 174 // Create a new random instance. Can return NULL if initialization fails. |
174 static RSAPrivateKey* Create(uint16 num_bits); | 175 static RSAPrivateKey* Create(uint16 num_bits); |
175 | 176 |
176 // Create a new instance by importing an existing private key. The format is | 177 // Create a new instance by importing an existing private key. The format is |
177 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if | 178 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if |
178 // initialization fails. | 179 // initialization fails. |
179 static RSAPrivateKey* CreateFromPrivateKeyInfo( | 180 static RSAPrivateKey* CreateFromPrivateKeyInfo( |
180 const std::vector<uint8>& input); | 181 const std::vector<uint8>& input); |
181 | 182 |
182 #if defined(USE_NSS) | 183 #if defined(USE_NSS) |
183 // Create a new random instance. Can return NULL if initialization fails. | 184 // Create a new random instance in |slot|. Can return NULL if initialization |
184 // The created key is permanent and is not exportable in plaintext form. | 185 // fails. The created key is permanent and is not exportable in plaintext |
185 static RSAPrivateKey* CreateSensitive(uint16 num_bits); | 186 // form. |
| 187 static RSAPrivateKey* CreateSensitive(PK11SlotInfo* slot, uint16 num_bits); |
186 | 188 |
187 // Create a new instance by importing an existing private key. The format is | 189 // Create a new instance in |slot| by importing an existing private key. The |
188 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if | 190 // format is an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can |
189 // initialization fails. | 191 // return NULL if initialization fails. |
190 // The created key is permanent and is not exportable in plaintext form. | 192 // The created key is permanent and is not exportable in plaintext form. |
191 static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo( | 193 static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo( |
| 194 PK11SlotInfo* slot, |
192 const std::vector<uint8>& input); | 195 const std::vector<uint8>& input); |
193 | 196 |
194 // Create a new instance by referencing an existing private key | 197 // Create a new instance by referencing an existing private key |
195 // structure. Does not import the key. | 198 // structure. Does not import the key. |
196 static RSAPrivateKey* CreateFromKey(SECKEYPrivateKey* key); | 199 static RSAPrivateKey* CreateFromKey(SECKEYPrivateKey* key); |
197 | 200 |
198 // Import an existing public key, and then search for the private | 201 // Import an existing public key, and then search for the private |
199 // half in the key database. The format of the public key blob is is | 202 // half in the key database. The format of the public key blob is is |
200 // an X509 SubjectPublicKeyInfo block. This can return NULL if | 203 // an X509 SubjectPublicKeyInfo block. This can return NULL if |
201 // initialization fails or the private key cannot be found. The | 204 // initialization fails or the private key cannot be found. The |
202 // caller takes ownership of the returned object, but nothing new is | 205 // caller takes ownership of the returned object, but nothing new is |
203 // created in the key database. | 206 // created in the key database. |
204 static RSAPrivateKey* FindFromPublicKeyInfo( | 207 static RSAPrivateKey* FindFromPublicKeyInfo( |
205 const std::vector<uint8>& input); | 208 const std::vector<uint8>& input); |
206 #endif | 209 #endif |
207 | 210 |
208 #if defined(USE_OPENSSL) | 211 #if defined(USE_OPENSSL) |
209 EVP_PKEY* key() { return key_; } | 212 EVP_PKEY* key() { return key_; } |
210 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | 213 #else |
211 SECKEYPrivateKey* key() { return key_; } | 214 SECKEYPrivateKey* key() { return key_; } |
212 SECKEYPublicKey* public_key() { return public_key_; } | 215 SECKEYPublicKey* public_key() { return public_key_; } |
213 #endif | 216 #endif |
214 | 217 |
215 // Creates a copy of the object. | 218 // Creates a copy of the object. |
216 RSAPrivateKey* Copy() const; | 219 RSAPrivateKey* Copy() const; |
217 | 220 |
218 // Exports the private key to a PKCS #1 PrivateKey block. | 221 // Exports the private key to a PKCS #1 PrivateKey block. |
219 bool ExportPrivateKey(std::vector<uint8>* output) const; | 222 bool ExportPrivateKey(std::vector<uint8>* output) const; |
220 | 223 |
221 // Exports the public key to an X509 SubjectPublicKeyInfo block. | 224 // Exports the public key to an X509 SubjectPublicKeyInfo block. |
222 bool ExportPublicKey(std::vector<uint8>* output) const; | 225 bool ExportPublicKey(std::vector<uint8>* output) const; |
223 | 226 |
224 private: | 227 private: |
225 #if defined(USE_NSS) | 228 #if defined(USE_NSS) |
226 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); | 229 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); |
227 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); | 230 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); |
228 #endif | 231 #endif |
229 | 232 |
230 // Constructor is private. Use one of the Create*() or Find*() | 233 // Constructor is private. Use one of the Create*() or Find*() |
231 // methods above instead. | 234 // methods above instead. |
232 RSAPrivateKey(); | 235 RSAPrivateKey(); |
233 | 236 |
| 237 #if !defined(USE_OPENSSL) |
234 // Shared helper for Create() and CreateSensitive(). | 238 // Shared helper for Create() and CreateSensitive(). |
235 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a | 239 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a |
236 // flags arg created by ORing together some enumerated values. | 240 // flags arg created by ORing together some enumerated values. |
237 // Note: |permanent| is only supported when USE_NSS is defined. | 241 // Note: |permanent| is only supported when USE_NSS is defined. |
238 static RSAPrivateKey* CreateWithParams(uint16 num_bits, | 242 static RSAPrivateKey* CreateWithParams(PK11SlotInfo* slot, |
| 243 uint16 num_bits, |
239 bool permanent, | 244 bool permanent, |
240 bool sensitive); | 245 bool sensitive); |
241 | 246 |
242 // Shared helper for CreateFromPrivateKeyInfo() and | 247 // Shared helper for CreateFromPrivateKeyInfo() and |
243 // CreateSensitiveFromPrivateKeyInfo(). | 248 // CreateSensitiveFromPrivateKeyInfo(). |
244 // Note: |permanent| is only supported when USE_NSS is defined. | 249 // Note: |permanent| is only supported when USE_NSS is defined. |
245 static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams( | 250 static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams( |
| 251 PK11SlotInfo* slot, |
246 const std::vector<uint8>& input, | 252 const std::vector<uint8>& input, |
247 bool permanent, | 253 bool permanent, |
248 bool sensitive); | 254 bool sensitive); |
| 255 #endif |
249 | 256 |
250 #if defined(USE_OPENSSL) | 257 #if defined(USE_OPENSSL) |
251 EVP_PKEY* key_; | 258 EVP_PKEY* key_; |
252 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | 259 #else |
253 SECKEYPrivateKey* key_; | 260 SECKEYPrivateKey* key_; |
254 SECKEYPublicKey* public_key_; | 261 SECKEYPublicKey* public_key_; |
255 #endif | 262 #endif |
256 | 263 |
257 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | 264 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
258 }; | 265 }; |
259 | 266 |
260 } // namespace crypto | 267 } // namespace crypto |
261 | 268 |
262 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ | 269 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ |
OLD | NEW |