OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CRYPTO_EC_PRIVATE_KEY_H_ |
| 6 #define CRYPTO_EC_PRIVATE_KEY_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "build/build_config.h" |
| 14 #include "crypto/crypto_export.h" |
| 15 |
| 16 #if defined(USE_OPENSSL) |
| 17 // Forward declaration for openssl/*.h |
| 18 typedef struct evp_pkey_st EVP_PKEY; |
| 19 #else |
| 20 // Forward declaration. |
| 21 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; |
| 22 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; |
| 23 #endif |
| 24 |
| 25 namespace crypto { |
| 26 |
| 27 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new |
| 28 // keys, export keys to other formats, or to extract a public key. |
| 29 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface. |
| 30 // (The difference in types of key() and public_key() make this a little |
| 31 // tricky.) |
| 32 class CRYPTO_EXPORT ECPrivateKey { |
| 33 public: |
| 34 ~ECPrivateKey(); |
| 35 |
| 36 // Creates a new random instance. Can return NULL if initialization fails. |
| 37 // The created key will use the NIST P-256 curve. |
| 38 // TODO(mattm): Add a curve parameter. |
| 39 static ECPrivateKey* Create(); |
| 40 |
| 41 // Creates a new random instance. Can return NULL if initialization fails. |
| 42 // The created key is permanent and is not exportable in plaintext form. |
| 43 // |
| 44 // NOTE: Currently only available if USE_NSS is defined. |
| 45 static ECPrivateKey* CreateSensitive(); |
| 46 |
| 47 // Creates a new instance by importing an existing key pair. |
| 48 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
| 49 // block and an X.509 SubjectPublicKeyInfo block. |
| 50 // Returns NULL if initialization fails. |
| 51 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfo( |
| 52 const std::string& password, |
| 53 const std::vector<uint8>& encrypted_private_key_info, |
| 54 const std::vector<uint8>& subject_public_key_info); |
| 55 |
| 56 // Creates a new instance by importing an existing key pair. |
| 57 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
| 58 // block and an X.509 SubjectPublicKeyInfo block. |
| 59 // This can return NULL if initialization fails. The created key is permanent |
| 60 // and is not exportable in plaintext form. |
| 61 // |
| 62 // NOTE: Currently only available if USE_NSS is defined. |
| 63 static ECPrivateKey* CreateSensitiveFromEncryptedPrivateKeyInfo( |
| 64 const std::string& password, |
| 65 const std::vector<uint8>& encrypted_private_key_info, |
| 66 const std::vector<uint8>& subject_public_key_info); |
| 67 |
| 68 #if defined(USE_OPENSSL) |
| 69 EVP_PKEY* key() { return key_; } |
| 70 #else |
| 71 SECKEYPrivateKey* key() { return key_; } |
| 72 SECKEYPublicKey* public_key() { return public_key_; } |
| 73 #endif |
| 74 |
| 75 // Exports the private key as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
| 76 // block and the public key as an X.509 SubjectPublicKeyInfo block. |
| 77 // The |password| and |iterations| are used as inputs to the key derivation |
| 78 // function for generating the encryption key. PKCS #5 recommends a minimum |
| 79 // of 1000 iterations, on modern systems a larger value may be preferrable. |
| 80 bool ExportEncryptedPrivateKey(const std::string& password, |
| 81 int iterations, |
| 82 std::vector<uint8>* output); |
| 83 |
| 84 // Exports the public key to an X.509 SubjectPublicKeyInfo block. |
| 85 bool ExportPublicKey(std::vector<uint8>* output); |
| 86 |
| 87 // Exports private key data for testing. The format of data stored into output |
| 88 // doesn't matter other than that it is consistent for the same key. |
| 89 bool ExportValue(std::vector<uint8>* output); |
| 90 bool ExportECParams(std::vector<uint8>* output); |
| 91 |
| 92 private: |
| 93 // Constructor is private. Use one of the Create*() methods above instead. |
| 94 ECPrivateKey(); |
| 95 |
| 96 // Shared helper for Create() and CreateSensitive(). |
| 97 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a |
| 98 // flags arg created by ORing together some enumerated values. |
| 99 static ECPrivateKey* CreateWithParams(bool permanent, |
| 100 bool sensitive); |
| 101 |
| 102 // Shared helper for CreateFromEncryptedPrivateKeyInfo() and |
| 103 // CreateSensitiveFromEncryptedPrivateKeyInfo(). |
| 104 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfoWithParams( |
| 105 const std::string& password, |
| 106 const std::vector<uint8>& encrypted_private_key_info, |
| 107 const std::vector<uint8>& subject_public_key_info, |
| 108 bool permanent, |
| 109 bool sensitive); |
| 110 |
| 111 #if defined(USE_OPENSSL) |
| 112 EVP_PKEY* key_; |
| 113 #else |
| 114 SECKEYPrivateKey* key_; |
| 115 SECKEYPublicKey* public_key_; |
| 116 #endif |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); |
| 119 }; |
| 120 |
| 121 |
| 122 } // namespace crypto |
| 123 |
| 124 #endif // CRYPTO_EC_PRIVATE_KEY_H_ |
OLD | NEW |