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_EC_PRIVATE_KEY_H_ | 5 #ifndef CRYPTO_EC_PRIVATE_KEY_H_ |
6 #define CRYPTO_EC_PRIVATE_KEY_H_ | 6 #define CRYPTO_EC_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 <string> | 13 #include <string> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
17 #include "crypto/crypto_export.h" | 18 #include "crypto/crypto_export.h" |
18 | 19 |
19 // Forward declaration for openssl/*.h | |
20 typedef struct evp_pkey_st EVP_PKEY; | |
21 | |
22 namespace crypto { | 20 namespace crypto { |
23 | 21 |
24 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new | 22 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new |
25 // keys, export keys to other formats, or to extract a public key. | 23 // keys, export keys to other formats, or to extract a public key. |
26 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface. | 24 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface. |
27 // (The difference in types of key() and public_key() make this a little | 25 // (The difference in types of key() and public_key() make this a little |
28 // tricky.) | 26 // tricky.) |
29 class CRYPTO_EXPORT ECPrivateKey { | 27 class CRYPTO_EXPORT ECPrivateKey { |
30 public: | 28 public: |
31 ~ECPrivateKey(); | 29 ~ECPrivateKey(); |
(...skipping 17 matching lines...) Expand all Loading... |
49 // This function is deprecated. Use CreateFromPrivateKeyInfo for new code. | 47 // This function is deprecated. Use CreateFromPrivateKeyInfo for new code. |
50 // See https://crbug.com/603319. | 48 // See https://crbug.com/603319. |
51 static std::unique_ptr<ECPrivateKey> CreateFromEncryptedPrivateKeyInfo( | 49 static std::unique_ptr<ECPrivateKey> CreateFromEncryptedPrivateKeyInfo( |
52 const std::string& password, | 50 const std::string& password, |
53 const std::vector<uint8_t>& encrypted_private_key_info, | 51 const std::vector<uint8_t>& encrypted_private_key_info, |
54 const std::vector<uint8_t>& subject_public_key_info); | 52 const std::vector<uint8_t>& subject_public_key_info); |
55 | 53 |
56 // Returns a copy of the object. | 54 // Returns a copy of the object. |
57 std::unique_ptr<ECPrivateKey> Copy() const; | 55 std::unique_ptr<ECPrivateKey> Copy() const; |
58 | 56 |
59 EVP_PKEY* key() { return key_; } | 57 EVP_PKEY* key() { return key_.get(); } |
60 | 58 |
61 // Exports the private key to a PKCS #8 PrivateKeyInfo block. | 59 // Exports the private key to a PKCS #8 PrivateKeyInfo block. |
62 bool ExportPrivateKey(std::vector<uint8_t>* output) const; | 60 bool ExportPrivateKey(std::vector<uint8_t>* output) const; |
63 | 61 |
64 // Exports the private key as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo | 62 // Exports the private key as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
65 // block and the public key as an X.509 SubjectPublicKeyInfo block. | 63 // block and the public key as an X.509 SubjectPublicKeyInfo block. |
66 // The |password| and |iterations| are used as inputs to the key derivation | 64 // The |password| and |iterations| are used as inputs to the key derivation |
67 // function for generating the encryption key. PKCS #5 recommends a minimum | 65 // function for generating the encryption key. PKCS #5 recommends a minimum |
68 // of 1000 iterations, on modern systems a larger value may be preferrable. | 66 // of 1000 iterations, on modern systems a larger value may be preferrable. |
69 // | 67 // |
70 // This function is deprecated. Use ExportPrivateKey for new code. See | 68 // This function is deprecated. Use ExportPrivateKey for new code. See |
71 // https://crbug.com/603319. | 69 // https://crbug.com/603319. |
72 bool ExportEncryptedPrivateKey(const std::string& password, | 70 bool ExportEncryptedPrivateKey(const std::string& password, |
73 int iterations, | 71 int iterations, |
74 std::vector<uint8_t>* output) const; | 72 std::vector<uint8_t>* output) const; |
75 | 73 |
76 // Exports the public key to an X.509 SubjectPublicKeyInfo block. | 74 // Exports the public key to an X.509 SubjectPublicKeyInfo block. |
77 bool ExportPublicKey(std::vector<uint8_t>* output) const; | 75 bool ExportPublicKey(std::vector<uint8_t>* output) const; |
78 | 76 |
79 // Exports the public key as an EC point in the uncompressed point format. | 77 // Exports the public key as an EC point in the uncompressed point format. |
80 bool ExportRawPublicKey(std::string* output) const; | 78 bool ExportRawPublicKey(std::string* output) const; |
81 | 79 |
82 private: | 80 private: |
83 // Constructor is private. Use one of the Create*() methods above instead. | 81 // Constructor is private. Use one of the Create*() methods above instead. |
84 ECPrivateKey(); | 82 ECPrivateKey(); |
85 | 83 |
86 EVP_PKEY* key_; | 84 bssl::UniquePtr<EVP_PKEY> key_; |
87 | 85 |
88 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); | 86 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); |
89 }; | 87 }; |
90 | 88 |
91 | 89 |
92 } // namespace crypto | 90 } // namespace crypto |
93 | 91 |
94 #endif // CRYPTO_EC_PRIVATE_KEY_H_ | 92 #endif // CRYPTO_EC_PRIVATE_KEY_H_ |
OLD | NEW |