| 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_CERTS) |
| 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 #else | 23 #else |
| 24 // Forward declaration. | 24 // Forward declaration. |
| 25 typedef struct PK11SlotInfoStr PK11SlotInfo; | 25 typedef struct PK11SlotInfoStr PK11SlotInfo; |
| 26 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; | 26 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 173 |
| 174 // Create a new random instance. Can return NULL if initialization fails. | 174 // Create a new random instance. Can return NULL if initialization fails. |
| 175 static RSAPrivateKey* Create(uint16 num_bits); | 175 static RSAPrivateKey* Create(uint16 num_bits); |
| 176 | 176 |
| 177 // 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 |
| 178 // 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 |
| 179 // initialization fails. | 179 // initialization fails. |
| 180 static RSAPrivateKey* CreateFromPrivateKeyInfo( | 180 static RSAPrivateKey* CreateFromPrivateKeyInfo( |
| 181 const std::vector<uint8>& input); | 181 const std::vector<uint8>& input); |
| 182 | 182 |
| 183 #if defined(USE_NSS) | 183 #if defined(USE_NSS_CERTS) |
| 184 // Create a new random instance in |slot|. Can return NULL if initialization | 184 // Create a new random instance in |slot|. Can return NULL if initialization |
| 185 // fails. The created key is permanent and is not exportable in plaintext | 185 // fails. The created key is permanent and is not exportable in plaintext |
| 186 // form. | 186 // form. |
| 187 static RSAPrivateKey* CreateSensitive(PK11SlotInfo* slot, uint16 num_bits); | 187 static RSAPrivateKey* CreateSensitive(PK11SlotInfo* slot, uint16 num_bits); |
| 188 | 188 |
| 189 // Create a new instance in |slot| by importing an existing private key. The | 189 // Create a new instance in |slot| by importing an existing private key. The |
| 190 // format is an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can | 190 // format is an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can |
| 191 // return NULL if initialization fails. | 191 // return NULL if initialization fails. |
| 192 // 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. |
| 193 static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo( | 193 static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // Creates a copy of the object. | 234 // Creates a copy of the object. |
| 235 RSAPrivateKey* Copy() const; | 235 RSAPrivateKey* Copy() const; |
| 236 | 236 |
| 237 // Exports the private key to a PKCS #1 PrivateKey block. | 237 // Exports the private key to a PKCS #1 PrivateKey block. |
| 238 bool ExportPrivateKey(std::vector<uint8>* output) const; | 238 bool ExportPrivateKey(std::vector<uint8>* output) const; |
| 239 | 239 |
| 240 // Exports the public key to an X509 SubjectPublicKeyInfo block. | 240 // Exports the public key to an X509 SubjectPublicKeyInfo block. |
| 241 bool ExportPublicKey(std::vector<uint8>* output) const; | 241 bool ExportPublicKey(std::vector<uint8>* output) const; |
| 242 | 242 |
| 243 private: | 243 private: |
| 244 #if defined(USE_NSS) | 244 #if defined(USE_NSS_CERTS) |
| 245 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); | 245 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); |
| 246 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); | 246 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); |
| 247 #endif | 247 #endif |
| 248 | 248 |
| 249 // Constructor is private. Use one of the Create*() or Find*() | 249 // Constructor is private. Use one of the Create*() or Find*() |
| 250 // methods above instead. | 250 // methods above instead. |
| 251 RSAPrivateKey(); | 251 RSAPrivateKey(); |
| 252 | 252 |
| 253 #if !defined(USE_OPENSSL) | 253 #if !defined(USE_OPENSSL) |
| 254 // Shared helper for Create() and CreateSensitive(). | 254 // Shared helper for Create() and CreateSensitive(). |
| 255 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a | 255 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a |
| 256 // flags arg created by ORing together some enumerated values. | 256 // flags arg created by ORing together some enumerated values. |
| 257 // Note: |permanent| is only supported when USE_NSS is defined. | 257 // Note: |permanent| is only supported when USE_NSS_CERTS is defined. |
| 258 static RSAPrivateKey* CreateWithParams(PK11SlotInfo* slot, | 258 static RSAPrivateKey* CreateWithParams(PK11SlotInfo* slot, |
| 259 uint16 num_bits, | 259 uint16 num_bits, |
| 260 bool permanent, | 260 bool permanent, |
| 261 bool sensitive); | 261 bool sensitive); |
| 262 | 262 |
| 263 // Shared helper for CreateFromPrivateKeyInfo() and | 263 // Shared helper for CreateFromPrivateKeyInfo() and |
| 264 // CreateSensitiveFromPrivateKeyInfo(). | 264 // CreateSensitiveFromPrivateKeyInfo(). |
| 265 // Note: |permanent| is only supported when USE_NSS is defined. | 265 // Note: |permanent| is only supported when USE_NSS_CERTS is defined. |
| 266 static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams( | 266 static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams( |
| 267 PK11SlotInfo* slot, | 267 PK11SlotInfo* slot, |
| 268 const std::vector<uint8>& input, | 268 const std::vector<uint8>& input, |
| 269 bool permanent, | 269 bool permanent, |
| 270 bool sensitive); | 270 bool sensitive); |
| 271 #endif | 271 #endif |
| 272 | 272 |
| 273 #if defined(USE_NSS) | 273 #if defined(USE_NSS_CERTS) |
| 274 // Import an existing public key. The format of the public key blob | 274 // Import an existing public key. The format of the public key blob |
| 275 // is an X509 SubjectPublicKeyInfo block. This can return NULL if | 275 // is an X509 SubjectPublicKeyInfo block. This can return NULL if |
| 276 // initialization fails. The caller takes ownership of the returned | 276 // initialization fails. The caller takes ownership of the returned |
| 277 // object. Note that this method doesn't initialize the |key_| member. | 277 // object. Note that this method doesn't initialize the |key_| member. |
| 278 static RSAPrivateKey* InitPublicPart(const std::vector<uint8>& input); | 278 static RSAPrivateKey* InitPublicPart(const std::vector<uint8>& input); |
| 279 #endif | 279 #endif |
| 280 | 280 |
| 281 #if defined(USE_OPENSSL) | 281 #if defined(USE_OPENSSL) |
| 282 EVP_PKEY* key_; | 282 EVP_PKEY* key_; |
| 283 #else | 283 #else |
| 284 SECKEYPrivateKey* key_; | 284 SECKEYPrivateKey* key_; |
| 285 SECKEYPublicKey* public_key_; | 285 SECKEYPublicKey* public_key_; |
| 286 #endif | 286 #endif |
| 287 | 287 |
| 288 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | 288 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
| 289 }; | 289 }; |
| 290 | 290 |
| 291 } // namespace crypto | 291 } // namespace crypto |
| 292 | 292 |
| 293 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ | 293 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ |
| OLD | NEW |