| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <list> | 11 #include <list> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "crypto/crypto_export.h" | 16 #include "crypto/crypto_export.h" |
| 17 | 17 |
| 18 #if defined(USE_OPENSSL) | |
| 19 // Forward declaration for openssl/*.h | 18 // Forward declaration for openssl/*.h |
| 20 typedef struct evp_pkey_st EVP_PKEY; | 19 typedef struct evp_pkey_st EVP_PKEY; |
| 21 #else | |
| 22 // Forward declaration. | |
| 23 typedef struct PK11SlotInfoStr PK11SlotInfo; | |
| 24 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; | |
| 25 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; | |
| 26 #endif | |
| 27 | 20 |
| 28 namespace crypto { | 21 namespace crypto { |
| 29 | 22 |
| 30 // Encapsulates an RSA private key. Can be used to generate new keys, export | 23 // Encapsulates an RSA private key. Can be used to generate new keys, export |
| 31 // keys to other formats, or to extract a public key. | 24 // keys to other formats, or to extract a public key. |
| 32 // TODO(hclam): This class should be ref-counted so it can be reused easily. | 25 // TODO(hclam): This class should be ref-counted so it can be reused easily. |
| 33 class CRYPTO_EXPORT RSAPrivateKey { | 26 class CRYPTO_EXPORT RSAPrivateKey { |
| 34 public: | 27 public: |
| 35 ~RSAPrivateKey(); | 28 ~RSAPrivateKey(); |
| 36 | 29 |
| 37 // Create a new random instance. Can return NULL if initialization fails. | 30 // Create a new random instance. Can return NULL if initialization fails. |
| 38 static RSAPrivateKey* Create(uint16_t num_bits); | 31 static RSAPrivateKey* Create(uint16_t num_bits); |
| 39 | 32 |
| 40 // Create a new instance by importing an existing private key. The format is | 33 // Create a new instance by importing an existing private key. The format is |
| 41 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if | 34 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if |
| 42 // initialization fails. | 35 // initialization fails. |
| 43 static RSAPrivateKey* CreateFromPrivateKeyInfo( | 36 static RSAPrivateKey* CreateFromPrivateKeyInfo( |
| 44 const std::vector<uint8_t>& input); | 37 const std::vector<uint8_t>& input); |
| 45 | 38 |
| 46 #if defined(USE_OPENSSL) | |
| 47 // Create a new instance from an existing EVP_PKEY, taking a | 39 // Create a new instance from an existing EVP_PKEY, taking a |
| 48 // reference to it. |key| must be an RSA key. Returns NULL on | 40 // reference to it. |key| must be an RSA key. Returns NULL on |
| 49 // failure. | 41 // failure. |
| 50 static RSAPrivateKey* CreateFromKey(EVP_PKEY* key); | 42 static RSAPrivateKey* CreateFromKey(EVP_PKEY* key); |
| 51 #else | |
| 52 // Create a new instance by referencing an existing private key | |
| 53 // structure. Does not import the key. | |
| 54 static RSAPrivateKey* CreateFromKey(SECKEYPrivateKey* key); | |
| 55 #endif | |
| 56 | 43 |
| 57 #if defined(USE_OPENSSL) | |
| 58 EVP_PKEY* key() { return key_; } | 44 EVP_PKEY* key() { return key_; } |
| 59 #else | |
| 60 SECKEYPrivateKey* key() { return key_; } | |
| 61 SECKEYPublicKey* public_key() { return public_key_; } | |
| 62 #endif | |
| 63 | 45 |
| 64 // Creates a copy of the object. | 46 // Creates a copy of the object. |
| 65 RSAPrivateKey* Copy() const; | 47 RSAPrivateKey* Copy() const; |
| 66 | 48 |
| 67 // Exports the private key to a PKCS #8 PrivateKeyInfo block. | 49 // Exports the private key to a PKCS #8 PrivateKeyInfo block. |
| 68 bool ExportPrivateKey(std::vector<uint8_t>* output) const; | 50 bool ExportPrivateKey(std::vector<uint8_t>* output) const; |
| 69 | 51 |
| 70 // Exports the public key to an X509 SubjectPublicKeyInfo block. | 52 // Exports the public key to an X509 SubjectPublicKeyInfo block. |
| 71 bool ExportPublicKey(std::vector<uint8_t>* output) const; | 53 bool ExportPublicKey(std::vector<uint8_t>* output) const; |
| 72 | 54 |
| 73 private: | 55 private: |
| 74 // Constructor is private. Use one of the Create*() methods above instead. | 56 // Constructor is private. Use one of the Create*() methods above instead. |
| 75 RSAPrivateKey(); | 57 RSAPrivateKey(); |
| 76 | 58 |
| 77 #if defined(USE_OPENSSL) | |
| 78 EVP_PKEY* key_; | 59 EVP_PKEY* key_; |
| 79 #else | |
| 80 SECKEYPrivateKey* key_; | |
| 81 SECKEYPublicKey* public_key_; | |
| 82 #endif | |
| 83 | 60 |
| 84 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | 61 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
| 85 }; | 62 }; |
| 86 | 63 |
| 87 } // namespace crypto | 64 } // namespace crypto |
| 88 | 65 |
| 89 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ | 66 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ |
| OLD | NEW |