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 <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "crypto/crypto_export.h" | 13 #include "crypto/crypto_export.h" |
14 | 14 |
15 #if defined(USE_OPENSSL) | 15 #if defined(USE_OPENSSL) |
16 // Forward declaration for openssl/*.h | 16 // Forward declaration for openssl/*.h |
17 typedef struct evp_pkey_st EVP_PKEY; | 17 typedef struct evp_pkey_st EVP_PKEY; |
18 #else | 18 #else |
19 // Forward declaration. | 19 // Forward declaration. |
20 typedef struct CERTSubjectPublicKeyInfoStr CERTSubjectPublicKeyInfo; | 20 typedef struct CERTSubjectPublicKeyInfoStr CERTSubjectPublicKeyInfo; |
| 21 typedef struct PK11SlotInfoStr PK11SlotInfo; |
21 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; | 22 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; |
22 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; | 23 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; |
23 #endif | 24 #endif |
24 | 25 |
25 namespace crypto { | 26 namespace crypto { |
26 | 27 |
27 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new | 28 // 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 // keys, export keys to other formats, or to extract a public key. |
29 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface. | 30 // 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 // (The difference in types of key() and public_key() make this a little |
31 // tricky.) | 32 // tricky.) |
32 class CRYPTO_EXPORT ECPrivateKey { | 33 class CRYPTO_EXPORT ECPrivateKey { |
33 public: | 34 public: |
34 ~ECPrivateKey(); | 35 ~ECPrivateKey(); |
35 | 36 |
36 // Returns whether the system supports elliptic curve cryptography. | 37 // Returns whether the system supports elliptic curve cryptography. |
37 static bool IsSupported(); | 38 static bool IsSupported(); |
38 | 39 |
39 // Creates a new random instance. Can return NULL if initialization fails. | 40 // Creates a new random instance. Can return NULL if initialization fails. |
40 // The created key will use the NIST P-256 curve. | 41 // The created key will use the NIST P-256 curve. |
41 // TODO(mattm): Add a curve parameter. | 42 // TODO(mattm): Add a curve parameter. |
42 static ECPrivateKey* Create(); | 43 static ECPrivateKey* Create(); |
43 | 44 |
44 // Creates a new random instance. Can return NULL if initialization fails. | 45 #if defined(USE_NSS) |
45 // The created key is permanent and is not exportable in plaintext form. | 46 // Creates a new random instance in |slot|. Can return NULL if initialization |
46 // | 47 // fails. The created key is permanent and is not exportable in plaintext |
47 // NOTE: Currently only available if USE_NSS is defined. | 48 // form. |
48 static ECPrivateKey* CreateSensitive(); | 49 static ECPrivateKey* CreateSensitive(PK11SlotInfo* slot); |
| 50 #endif |
49 | 51 |
50 // Creates a new instance by importing an existing key pair. | 52 // Creates a new instance by importing an existing key pair. |
51 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo | 53 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
52 // block and an X.509 SubjectPublicKeyInfo block. | 54 // block and an X.509 SubjectPublicKeyInfo block. |
53 // Returns NULL if initialization fails. | 55 // Returns NULL if initialization fails. |
54 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfo( | 56 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfo( |
55 const std::string& password, | 57 const std::string& password, |
56 const std::vector<uint8>& encrypted_private_key_info, | 58 const std::vector<uint8>& encrypted_private_key_info, |
57 const std::vector<uint8>& subject_public_key_info); | 59 const std::vector<uint8>& subject_public_key_info); |
58 | 60 |
59 // Creates a new instance by importing an existing key pair. | 61 #if defined(USE_NSS) |
| 62 // Creates a new instance in |slot| by importing an existing key pair. |
60 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo | 63 // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo |
61 // block and an X.509 SubjectPublicKeyInfo block. | 64 // block and an X.509 SubjectPublicKeyInfo block. |
62 // This can return NULL if initialization fails. The created key is permanent | 65 // This can return NULL if initialization fails. The created key is permanent |
63 // and is not exportable in plaintext form. | 66 // and is not exportable in plaintext form. |
64 // | |
65 // NOTE: Currently only available if USE_NSS is defined. | |
66 static ECPrivateKey* CreateSensitiveFromEncryptedPrivateKeyInfo( | 67 static ECPrivateKey* CreateSensitiveFromEncryptedPrivateKeyInfo( |
| 68 PK11SlotInfo* slot, |
67 const std::string& password, | 69 const std::string& password, |
68 const std::vector<uint8>& encrypted_private_key_info, | 70 const std::vector<uint8>& encrypted_private_key_info, |
69 const std::vector<uint8>& subject_public_key_info); | 71 const std::vector<uint8>& subject_public_key_info); |
| 72 #endif |
70 | 73 |
71 #if !defined(USE_OPENSSL) | 74 #if !defined(USE_OPENSSL) |
72 // Imports the key pair and returns in |public_key| and |key|. | 75 // Imports the key pair into |slot| and returns in |public_key| and |key|. |
73 // Shortcut for code that needs to keep a reference directly to NSS types | 76 // Shortcut for code that needs to keep a reference directly to NSS types |
74 // without having to create a ECPrivateKey object and make a copy of them. | 77 // without having to create a ECPrivateKey object and make a copy of them. |
75 // TODO(mattm): move this function to some NSS util file. | 78 // TODO(mattm): move this function to some NSS util file. |
76 static bool ImportFromEncryptedPrivateKeyInfo( | 79 static bool ImportFromEncryptedPrivateKeyInfo( |
| 80 PK11SlotInfo* slot, |
77 const std::string& password, | 81 const std::string& password, |
78 const uint8* encrypted_private_key_info, | 82 const uint8* encrypted_private_key_info, |
79 size_t encrypted_private_key_info_len, | 83 size_t encrypted_private_key_info_len, |
80 CERTSubjectPublicKeyInfo* decoded_spki, | 84 CERTSubjectPublicKeyInfo* decoded_spki, |
81 bool permanent, | 85 bool permanent, |
82 bool sensitive, | 86 bool sensitive, |
83 SECKEYPrivateKey** key, | 87 SECKEYPrivateKey** key, |
84 SECKEYPublicKey** public_key); | 88 SECKEYPublicKey** public_key); |
85 #endif | 89 #endif |
86 | 90 |
(...skipping 18 matching lines...) Expand all Loading... |
105 | 109 |
106 // Exports private key data for testing. The format of data stored into output | 110 // Exports private key data for testing. The format of data stored into output |
107 // doesn't matter other than that it is consistent for the same key. | 111 // doesn't matter other than that it is consistent for the same key. |
108 bool ExportValue(std::vector<uint8>* output); | 112 bool ExportValue(std::vector<uint8>* output); |
109 bool ExportECParams(std::vector<uint8>* output); | 113 bool ExportECParams(std::vector<uint8>* output); |
110 | 114 |
111 private: | 115 private: |
112 // Constructor is private. Use one of the Create*() methods above instead. | 116 // Constructor is private. Use one of the Create*() methods above instead. |
113 ECPrivateKey(); | 117 ECPrivateKey(); |
114 | 118 |
| 119 #if !defined(USE_OPENSSL) |
115 // Shared helper for Create() and CreateSensitive(). | 120 // Shared helper for Create() and CreateSensitive(). |
116 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a | 121 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a |
117 // flags arg created by ORing together some enumerated values. | 122 // flags arg created by ORing together some enumerated values. |
118 static ECPrivateKey* CreateWithParams(bool permanent, | 123 static ECPrivateKey* CreateWithParams(PK11SlotInfo* slot, |
| 124 bool permanent, |
119 bool sensitive); | 125 bool sensitive); |
120 | 126 |
121 // Shared helper for CreateFromEncryptedPrivateKeyInfo() and | 127 // Shared helper for CreateFromEncryptedPrivateKeyInfo() and |
122 // CreateSensitiveFromEncryptedPrivateKeyInfo(). | 128 // CreateSensitiveFromEncryptedPrivateKeyInfo(). |
123 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfoWithParams( | 129 static ECPrivateKey* CreateFromEncryptedPrivateKeyInfoWithParams( |
| 130 PK11SlotInfo* slot, |
124 const std::string& password, | 131 const std::string& password, |
125 const std::vector<uint8>& encrypted_private_key_info, | 132 const std::vector<uint8>& encrypted_private_key_info, |
126 const std::vector<uint8>& subject_public_key_info, | 133 const std::vector<uint8>& subject_public_key_info, |
127 bool permanent, | 134 bool permanent, |
128 bool sensitive); | 135 bool sensitive); |
| 136 #endif |
129 | 137 |
130 #if defined(USE_OPENSSL) | 138 #if defined(USE_OPENSSL) |
131 EVP_PKEY* key_; | 139 EVP_PKEY* key_; |
132 #else | 140 #else |
133 SECKEYPrivateKey* key_; | 141 SECKEYPrivateKey* key_; |
134 SECKEYPublicKey* public_key_; | 142 SECKEYPublicKey* public_key_; |
135 #endif | 143 #endif |
136 | 144 |
137 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); | 145 DISALLOW_COPY_AND_ASSIGN(ECPrivateKey); |
138 }; | 146 }; |
139 | 147 |
140 | 148 |
141 } // namespace crypto | 149 } // namespace crypto |
142 | 150 |
143 #endif // CRYPTO_EC_PRIVATE_KEY_H_ | 151 #endif // CRYPTO_EC_PRIVATE_KEY_H_ |
OLD | NEW |