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