[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ |
| 6 | #define CRYPTO_RSA_PRIVATE_KEY_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 8 | |
| 9 | #include "build/build_config.h" |
| 10 | |
[email protected] | 1f92394 | 2010-11-17 14:39:22 | [diff] [blame] | 11 | #if defined(USE_OPENSSL) |
| 12 | // Forward declaration for openssl/*.h |
| 13 | typedef struct evp_pkey_st EVP_PKEY; |
| 14 | #elif defined(USE_NSS) |
[email protected] | 13555c12 | 2009-10-08 01:18:02 | [diff] [blame] | 15 | // Forward declaration. |
| 16 | struct SECKEYPrivateKeyStr; |
| 17 | struct SECKEYPublicKeyStr; |
[email protected] | 71a9f84 | 2009-09-24 01:21:12 | [diff] [blame] | 18 | #elif defined(OS_MACOSX) |
[email protected] | 0691bdf4 | 2009-10-02 21:05:01 | [diff] [blame] | 19 | #include <Security/cssm.h> |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 20 | #endif |
| 21 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 22 | #include <list> |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #include "base/basictypes.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame^] | 26 | #include "crypto/crypto_export.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 27 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 28 | #if defined(OS_WIN) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 29 | #include "crypto/scoped_capi_types.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 30 | #endif |
[email protected] | 7464805 | 2010-08-10 19:37:51 | [diff] [blame] | 31 | #if defined(USE_NSS) |
| 32 | #include "base/gtest_prod_util.h" |
| 33 | #endif |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 34 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 35 | namespace crypto { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 36 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 37 | // Used internally by RSAPrivateKey for serializing and deserializing |
| 38 | // PKCS #8 PrivateKeyInfo and PublicKeyInfo. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 39 | class PrivateKeyInfoCodec { |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 40 | public: |
| 41 | |
| 42 | // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. |
| 43 | static const uint8 kRsaAlgorithmIdentifier[]; |
| 44 | |
| 45 | // ASN.1 tags for some types we use. |
| 46 | static const uint8 kBitStringTag = 0x03; |
| 47 | static const uint8 kIntegerTag = 0x02; |
| 48 | static const uint8 kNullTag = 0x05; |
| 49 | static const uint8 kOctetStringTag = 0x04; |
| 50 | static const uint8 kSequenceTag = 0x30; |
[email protected] | 90a2847 | 2009-10-20 20:39:52 | [diff] [blame] | 51 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 52 | // |big_endian| here specifies the byte-significance of the integer components |
| 53 | // that will be parsed & serialized (modulus(), etc...) during Import(), |
| 54 | // Export() and ExportPublicKeyInfo() -- not the ASN.1 DER encoding of the |
| 55 | // PrivateKeyInfo/PublicKeyInfo (which is always big-endian). |
[email protected] | 1889dc1b | 2010-10-14 22:03:13 | [diff] [blame] | 56 | explicit PrivateKeyInfoCodec(bool big_endian); |
| 57 | |
| 58 | ~PrivateKeyInfoCodec(); |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 59 | |
| 60 | // Exports the contents of the integer components to the ASN.1 DER encoding |
| 61 | // of the PrivateKeyInfo structure to |output|. |
| 62 | bool Export(std::vector<uint8>* output); |
| 63 | |
| 64 | // Exports the contents of the integer components to the ASN.1 DER encoding |
| 65 | // of the PublicKeyInfo structure to |output|. |
| 66 | bool ExportPublicKeyInfo(std::vector<uint8>* output); |
| 67 | |
[email protected] | eed0011 | 2011-02-08 14:28:29 | [diff] [blame] | 68 | // Exports the contents of the integer components to the ASN.1 DER encoding |
| 69 | // of the RSAPublicKey structure to |output|. |
| 70 | bool ExportPublicKey(std::vector<uint8>* output); |
| 71 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 72 | // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input| |
| 73 | // and populates the integer components with |big_endian_| byte-significance. |
[email protected] | 90a2847 | 2009-10-20 20:39:52 | [diff] [blame] | 74 | // IMPORTANT NOTE: This is currently *not* security-approved for importing |
| 75 | // keys from unstrusted sources. |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 76 | bool Import(const std::vector<uint8>& input); |
| 77 | |
| 78 | // Accessors to the contents of the integer components of the PrivateKeyInfo |
| 79 | // structure. |
| 80 | std::vector<uint8>* modulus() { return &modulus_; }; |
| 81 | std::vector<uint8>* public_exponent() { return &public_exponent_; }; |
| 82 | std::vector<uint8>* private_exponent() { return &private_exponent_; }; |
| 83 | std::vector<uint8>* prime1() { return &prime1_; }; |
| 84 | std::vector<uint8>* prime2() { return &prime2_; }; |
| 85 | std::vector<uint8>* exponent1() { return &exponent1_; }; |
| 86 | std::vector<uint8>* exponent2() { return &exponent2_; }; |
| 87 | std::vector<uint8>* coefficient() { return &coefficient_; }; |
| 88 | |
| 89 | private: |
| 90 | // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_| |
| 91 | // value. |
| 92 | void PrependInteger(const std::vector<uint8>& in, std::list<uint8>* out); |
| 93 | void PrependInteger(uint8* val, int num_bytes, std::list<uint8>* data); |
[email protected] | 90a2847 | 2009-10-20 20:39:52 | [diff] [blame] | 94 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 95 | // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian| |
| 96 | // byte-significance into |data| as an ASN.1 integer. |
| 97 | void PrependIntegerImpl(uint8* val, |
| 98 | int num_bytes, |
| 99 | std::list<uint8>* data, |
| 100 | bool big_endian); |
| 101 | |
| 102 | // Utility wrappers for ReadIntegerImpl that use the class's |big_endian_| |
| 103 | // value. |
| 104 | bool ReadInteger(uint8** pos, uint8* end, std::vector<uint8>* out); |
| 105 | bool ReadIntegerWithExpectedSize(uint8** pos, |
| 106 | uint8* end, |
| 107 | size_t expected_size, |
| 108 | std::vector<uint8>* out); |
| 109 | |
| 110 | // Reads an ASN.1 integer from |pos|, and stores the result into |out| with |
| 111 | // |big_endian| byte-significance. |
| 112 | bool ReadIntegerImpl(uint8** pos, |
| 113 | uint8* end, |
[email protected] | 90a2847 | 2009-10-20 20:39:52 | [diff] [blame] | 114 | std::vector<uint8>* out, |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 115 | bool big_endian); |
[email protected] | 90a2847 | 2009-10-20 20:39:52 | [diff] [blame] | 116 | |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 117 | // Prepends the integer stored in |val|, starting a index |start|, for |
| 118 | // |num_bytes| bytes onto |data|. |
| 119 | void PrependBytes(uint8* val, |
| 120 | int start, |
| 121 | int num_bytes, |
| 122 | std::list<uint8>* data); |
| 123 | |
| 124 | // Helper to prepend an ASN.1 length field. |
| 125 | void PrependLength(size_t size, std::list<uint8>* data); |
| 126 | |
| 127 | // Helper to prepend an ASN.1 type header. |
| 128 | void PrependTypeHeaderAndLength(uint8 type, |
| 129 | uint32 length, |
| 130 | std::list<uint8>* output); |
| 131 | |
| 132 | // Helper to prepend an ASN.1 bit string |
| 133 | void PrependBitString(uint8* val, int num_bytes, std::list<uint8>* output); |
| 134 | |
| 135 | // Read an ASN.1 length field. This also checks that the length does not |
| 136 | // extend beyond |end|. |
| 137 | bool ReadLength(uint8** pos, uint8* end, uint32* result); |
| 138 | |
| 139 | // Read an ASN.1 type header and its length. |
| 140 | bool ReadTypeHeaderAndLength(uint8** pos, |
| 141 | uint8* end, |
| 142 | uint8 expected_tag, |
| 143 | uint32* length); |
| 144 | |
| 145 | // Read an ASN.1 sequence declaration. This consumes the type header and |
| 146 | // length field, but not the contents of the sequence. |
| 147 | bool ReadSequence(uint8** pos, uint8* end); |
| 148 | |
| 149 | // Read the RSA AlgorithmIdentifier. |
| 150 | bool ReadAlgorithmIdentifier(uint8** pos, uint8* end); |
| 151 | |
| 152 | // Read one of the two version fields in PrivateKeyInfo. |
| 153 | bool ReadVersion(uint8** pos, uint8* end); |
| 154 | |
| 155 | // The byte-significance of the stored components (modulus, etc..). |
| 156 | bool big_endian_; |
| 157 | |
| 158 | // Component integers of the PrivateKeyInfo |
| 159 | std::vector<uint8> modulus_; |
| 160 | std::vector<uint8> public_exponent_; |
| 161 | std::vector<uint8> private_exponent_; |
| 162 | std::vector<uint8> prime1_; |
| 163 | std::vector<uint8> prime2_; |
| 164 | std::vector<uint8> exponent1_; |
| 165 | std::vector<uint8> exponent2_; |
| 166 | std::vector<uint8> coefficient_; |
| 167 | |
| 168 | DISALLOW_COPY_AND_ASSIGN(PrivateKeyInfoCodec); |
| 169 | }; |
| 170 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 171 | // Encapsulates an RSA private key. Can be used to generate new keys, export |
| 172 | // keys to other formats, or to extract a public key. |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 173 | // TODO(hclam): This class should be ref-counted so it can be reused easily. |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame^] | 174 | class CRYPTO_EXPORT RSAPrivateKey { |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 175 | public: |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 176 | ~RSAPrivateKey(); |
| 177 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 178 | // Create a new random instance. Can return NULL if initialization fails. |
| 179 | static RSAPrivateKey* Create(uint16 num_bits); |
| 180 | |
[email protected] | 7464805 | 2010-08-10 19:37:51 | [diff] [blame] | 181 | // Create a new random instance. Can return NULL if initialization fails. |
| 182 | // The created key is permanent and is not exportable in plaintext form. |
| 183 | // |
| 184 | // NOTE: Currently only available if USE_NSS is defined. |
| 185 | static RSAPrivateKey* CreateSensitive(uint16 num_bits); |
| 186 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 187 | // Create a new instance by importing an existing private key. The format is |
| 188 | // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if |
| 189 | // initialization fails. |
| 190 | static RSAPrivateKey* CreateFromPrivateKeyInfo( |
| 191 | const std::vector<uint8>& input); |
| 192 | |
[email protected] | 7464805 | 2010-08-10 19:37:51 | [diff] [blame] | 193 | // Create a new instance by importing an existing private key. The format is |
| 194 | // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if |
| 195 | // initialization fails. |
| 196 | // The created key is permanent and is not exportable in plaintext form. |
| 197 | // |
| 198 | // NOTE: Currently only available if USE_NSS is defined. |
| 199 | static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo( |
| 200 | const std::vector<uint8>& input); |
| 201 | |
| 202 | // Import an existing public key, and then search for the private |
| 203 | // half in the key database. The format of the public key blob is is |
| 204 | // an X509 SubjectPublicKeyInfo block. This can return NULL if |
| 205 | // initialization fails or the private key cannot be found. The |
| 206 | // caller takes ownership of the returned object, but nothing new is |
| 207 | // created in the key database. |
| 208 | // |
| 209 | // NOTE: Currently only available if USE_NSS is defined. |
| 210 | static RSAPrivateKey* FindFromPublicKeyInfo( |
| 211 | const std::vector<uint8>& input); |
| 212 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 213 | #if defined(USE_OPENSSL) |
| 214 | EVP_PKEY* key() { return key_; } |
| 215 | #elif defined(USE_NSS) |
[email protected] | 13555c12 | 2009-10-08 01:18:02 | [diff] [blame] | 216 | SECKEYPrivateKeyStr* key() { return key_; } |
[email protected] | 56f2ec39 | 2010-12-17 21:13:16 | [diff] [blame] | 217 | SECKEYPublicKeyStr* public_key() { return public_key_; } |
[email protected] | 71a9f84 | 2009-09-24 01:21:12 | [diff] [blame] | 218 | #elif defined(OS_WIN) |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 219 | HCRYPTPROV provider() { return provider_; } |
| 220 | HCRYPTKEY key() { return key_; } |
[email protected] | 0691bdf4 | 2009-10-02 21:05:01 | [diff] [blame] | 221 | #elif defined(OS_MACOSX) |
[email protected] | 0691bdf4 | 2009-10-02 21:05:01 | [diff] [blame] | 222 | CSSM_KEY_PTR key() { return &key_; } |
[email protected] | eed0011 | 2011-02-08 14:28:29 | [diff] [blame] | 223 | CSSM_KEY_PTR public_key() { return &public_key_; } |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 224 | #endif |
| 225 | |
| 226 | // Exports the private key to a PKCS #1 PrivateKey block. |
| 227 | bool ExportPrivateKey(std::vector<uint8>* output); |
| 228 | |
| 229 | // Exports the public key to an X509 SubjectPublicKeyInfo block. |
| 230 | bool ExportPublicKey(std::vector<uint8>* output); |
| 231 | |
[email protected] | 1f92394 | 2010-11-17 14:39:22 | [diff] [blame] | 232 | private: |
[email protected] | 7464805 | 2010-08-10 19:37:51 | [diff] [blame] | 233 | #if defined(USE_NSS) |
| 234 | FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); |
| 235 | FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); |
| 236 | #endif |
| 237 | |
| 238 | // Constructor is private. Use one of the Create*() or Find*() |
| 239 | // methods above instead. |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 240 | RSAPrivateKey(); |
| 241 | |
[email protected] | 7464805 | 2010-08-10 19:37:51 | [diff] [blame] | 242 | // Shared helper for Create() and CreateSensitive(). |
| 243 | // TODO(cmasone): consider replacing |permanent| and |sensitive| with a |
| 244 | // flags arg created by ORing together some enumerated values. |
| 245 | static RSAPrivateKey* CreateWithParams(uint16 num_bits, |
| 246 | bool permanent, |
| 247 | bool sensitive); |
| 248 | |
| 249 | // Shared helper for CreateFromPrivateKeyInfo() and |
| 250 | // CreateSensitiveFromPrivateKeyInfo(). |
| 251 | static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams( |
| 252 | const std::vector<uint8>& input, bool permanent, bool sensitive); |
| 253 | |
[email protected] | 1f92394 | 2010-11-17 14:39:22 | [diff] [blame] | 254 | #if defined(USE_OPENSSL) |
| 255 | EVP_PKEY* key_; |
| 256 | #elif defined(USE_NSS) |
[email protected] | 13555c12 | 2009-10-08 01:18:02 | [diff] [blame] | 257 | SECKEYPrivateKeyStr* key_; |
| 258 | SECKEYPublicKeyStr* public_key_; |
[email protected] | 71a9f84 | 2009-09-24 01:21:12 | [diff] [blame] | 259 | #elif defined(OS_WIN) |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 260 | bool InitProvider(); |
| 261 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 262 | ScopedHCRYPTPROV provider_; |
| 263 | ScopedHCRYPTKEY key_; |
[email protected] | 0691bdf4 | 2009-10-02 21:05:01 | [diff] [blame] | 264 | #elif defined(OS_MACOSX) |
| 265 | CSSM_KEY key_; |
[email protected] | eed0011 | 2011-02-08 14:28:29 | [diff] [blame] | 266 | CSSM_KEY public_key_; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 267 | #endif |
| 268 | |
| 269 | DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
| 270 | }; |
| 271 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 272 | } // namespace crypto |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 273 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 274 | #endif // CRYPTO_RSA_PRIVATE_KEY_H_ |