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