Chromium Code Reviews| 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. | |
|
wtc
2011/11/08 23:21:18
|password| and |iterations| should be documented.
mattm
2011/11/09 04:11:35
Done.
| |
| 77 bool ExportEncryptedPrivateKey(const std::string& password, | |
| 78 int iterations, | |
| 79 std::vector<uint8>* output); | |
| 80 | |
| 81 // Exports the public key to an X.509 SubjectPublicKeyInfo block. | |
| 82 bool ExportPublicKey(std::vector<uint8>* output); | |
| 83 | |
| 84 // Exports private key data for testing. The format of data stored into output | |
| 85 // doesn't matter other than that it is consistent for the same key. | |
| 86 bool ExportValue(std::vector<uint8>* output); | |
| 87 bool ExportECParams(std::vector<uint8>* output); | |
| 88 | |
| 89 private: | |
| 90 // Constructor is private. Use one of the Create*() methods above instead. | |
| 91 ECPrivateKey(); | |
| 92 | |
| 93 // Shared helper for Create() and CreateSensitive(). | |
| 94 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a | |
| 95 // flags arg created by ORing together some enumerated values. | |
| 96 static ECPrivateKey* CreateWithParams(bool permanent, | |
| 97 bool sensitive); | |
| 98 | |
| 99 // Shared helper for CreateFromEncryptedPrivateKeyInfo() and | |
| 100 // CreateSensitiveFromEncryptedPrivateKeyInfo(). | |
| 101 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfoWithParams( | |
| 102 const std::string& password, | |
| 103 const std::vector<uint8>& encrypted_private_key_info, | |
| 104 const std::vector<uint8>& subject_public_key_info, | |
| 105 bool permanent, | |
| 106 bool sensitive); | |
| 107 | |
| 108 #if defined(USE_OPENSSL) | |
| 109 EVP_PKEY* key_; | |
| 110 #else | |
| 111 SECKEYPrivateKey* key_; | |
| 112 SECKEYPublicKey* public_key_; | |
| 113 #endif | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); | |
| 116 }; | |
| 117 | |
| 118 | |
| 119 } // namespace crypto | |
| 120 | |
| 121 #endif // CRYPTO_EC_PRIVATE_KEY_H_ | |
| OLD | NEW |