| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> |
| 11 #include <string> | 12 #include <string> |
| 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 // Forward declaration for openssl/*.h |
| 19 typedef struct evp_pkey_st EVP_PKEY; | 20 typedef struct evp_pkey_st EVP_PKEY; |
| 20 | 21 |
| 21 namespace crypto { | 22 namespace crypto { |
| 22 | 23 |
| 23 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new | 24 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new |
| 24 // keys, export keys to other formats, or to extract a public key. | 25 // keys, export keys to other formats, or to extract a public key. |
| 25 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface. | 26 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface. |
| 26 // (The difference in types of key() and public_key() make this a little | 27 // (The difference in types of key() and public_key() make this a little |
| 27 // tricky.) | 28 // tricky.) |
| 28 class CRYPTO_EXPORT ECPrivateKey { | 29 class CRYPTO_EXPORT ECPrivateKey { |
| 29 public: | 30 public: |
| 30 ~ECPrivateKey(); | 31 ~ECPrivateKey(); |
| 31 | 32 |
| 32 // Creates a new random instance. Can return NULL if initialization fails. | 33 // Creates a new random instance. Can return NULL if initialization fails. |
| 33 // The created key will use the NIST P-256 curve. | 34 // The created key will use the NIST P-256 curve. |
| 34 // TODO(mattm): Add a curve parameter. | 35 // TODO(mattm): Add a curve parameter. |
| 35 static ECPrivateKey* Create(); | 36 static ECPrivateKey* Create(); |
| 36 | 37 |
| 38 // Create a new instance by importing an existing private key. The format is |
| 39 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return |
| 40 // nullptr if initialization fails. |
| 41 static std::unique_ptr<ECPrivateKey> CreateFromPrivateKeyInfo( |
| 42 const std::vector<uint8_t>& input); |
| 43 |
| 37 // Creates a new instance by importing an existing key pair. | 44 // Creates a new instance by importing an existing key pair. |
| 38 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo | 45 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
| 39 // block and an X.509 SubjectPublicKeyInfo block. | 46 // block and an X.509 SubjectPublicKeyInfo block. |
| 40 // Returns NULL if initialization fails. | 47 // Returns NULL if initialization fails. |
| 48 // |
| 49 // This function is deprecated. Use CreateFromPrivateKeyInfo for new code. |
| 50 // See https://crbug.com/603319. |
| 41 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfo( | 51 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfo( |
| 42 const std::string& password, | 52 const std::string& password, |
| 43 const std::vector<uint8_t>& encrypted_private_key_info, | 53 const std::vector<uint8_t>& encrypted_private_key_info, |
| 44 const std::vector<uint8_t>& subject_public_key_info); | 54 const std::vector<uint8_t>& subject_public_key_info); |
| 45 | 55 |
| 46 // Returns a copy of the object. | 56 // Returns a copy of the object. |
| 47 ECPrivateKey* Copy() const; | 57 ECPrivateKey* Copy() const; |
| 48 | 58 |
| 49 EVP_PKEY* key() { return key_; } | 59 EVP_PKEY* key() { return key_; } |
| 50 | 60 |
| 61 // Exports the private key to a PKCS #8 PrivateKeyInfo block. |
| 62 bool ExportPrivateKey(std::vector<uint8_t>* output) const; |
| 63 |
| 51 // Exports the private key as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo | 64 // Exports the private key as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
| 52 // block and the public key as an X.509 SubjectPublicKeyInfo block. | 65 // block and the public key as an X.509 SubjectPublicKeyInfo block. |
| 53 // The |password| and |iterations| are used as inputs to the key derivation | 66 // The |password| and |iterations| are used as inputs to the key derivation |
| 54 // function for generating the encryption key. PKCS #5 recommends a minimum | 67 // function for generating the encryption key. PKCS #5 recommends a minimum |
| 55 // of 1000 iterations, on modern systems a larger value may be preferrable. | 68 // of 1000 iterations, on modern systems a larger value may be preferrable. |
| 69 // |
| 70 // This function is deprecated. Use ExportPrivateKey for new code. See |
| 71 // https://crbug.com/603319. |
| 56 bool ExportEncryptedPrivateKey(const std::string& password, | 72 bool ExportEncryptedPrivateKey(const std::string& password, |
| 57 int iterations, | 73 int iterations, |
| 58 std::vector<uint8_t>* output); | 74 std::vector<uint8_t>* output) const; |
| 59 | 75 |
| 60 // Exports the public key to an X.509 SubjectPublicKeyInfo block. | 76 // Exports the public key to an X.509 SubjectPublicKeyInfo block. |
| 61 bool ExportPublicKey(std::vector<uint8_t>* output); | 77 bool ExportPublicKey(std::vector<uint8_t>* output) const; |
| 62 | 78 |
| 63 // Exports the public key as an EC point in the uncompressed point format. | 79 // Exports the public key as an EC point in the uncompressed point format. |
| 64 bool ExportRawPublicKey(std::string* output); | 80 bool ExportRawPublicKey(std::string* output) const; |
| 65 | |
| 66 // Exports private key data for testing. The format of data stored into output | |
| 67 // doesn't matter other than that it is consistent for the same key. | |
| 68 bool ExportValueForTesting(std::vector<uint8_t>* output); | |
| 69 | 81 |
| 70 private: | 82 private: |
| 71 // Constructor is private. Use one of the Create*() methods above instead. | 83 // Constructor is private. Use one of the Create*() methods above instead. |
| 72 ECPrivateKey(); | 84 ECPrivateKey(); |
| 73 | 85 |
| 74 EVP_PKEY* key_; | 86 EVP_PKEY* key_; |
| 75 | 87 |
| 76 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); | 88 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); |
| 77 }; | 89 }; |
| 78 | 90 |
| 79 | 91 |
| 80 } // namespace crypto | 92 } // namespace crypto |
| 81 | 93 |
| 82 #endif // CRYPTO_EC_PRIVATE_KEY_H_ | 94 #endif // CRYPTO_EC_PRIVATE_KEY_H_ |
| OLD | NEW |