blob: 7a1dccd768a9287e295fe5d6345d155a6c7b06df [file] [log] [blame]
[email protected]cf211882012-07-11 07:19:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]28ae8fe2009-06-05 18:25:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_RSA_PRIVATE_KEY_H_
6#define CRYPTO_RSA_PRIVATE_KEY_H_
[email protected]28ae8fe2009-06-05 18:25:067
8#include "build/build_config.h"
9
[email protected]1f923942010-11-17 14:39:2210#if defined(USE_OPENSSL)
11// Forward declaration for openssl/*.h
12typedef struct evp_pkey_st EVP_PKEY;
13#elif defined(USE_NSS)
[email protected]13555c122009-10-08 01:18:0214// Forward declaration.
15struct SECKEYPrivateKeyStr;
16struct SECKEYPublicKeyStr;
[email protected]71a9f842009-09-24 01:21:1217#elif defined(OS_MACOSX)
[email protected]0691bdf42009-10-02 21:05:0118#include <Security/cssm.h>
[email protected]28ae8fe2009-06-05 18:25:0619#endif
20
[email protected]308379a52009-10-07 02:46:3121#include <list>
[email protected]28ae8fe2009-06-05 18:25:0622#include <vector>
23
24#include "base/basictypes.h"
[email protected]d613a9902011-08-05 20:59:1125#include "crypto/crypto_export.h"
[email protected]28ae8fe2009-06-05 18:25:0626
[email protected]692033a2010-04-09 18:40:5027#if defined(OS_WIN)
[email protected]4b559b4d2011-04-14 17:37:1428#include "crypto/scoped_capi_types.h"
[email protected]692033a2010-04-09 18:40:5029#endif
[email protected]74648052010-08-10 19:37:5130#if defined(USE_NSS)
31#include "base/gtest_prod_util.h"
32#endif
[email protected]692033a2010-04-09 18:40:5033
[email protected]4b559b4d2011-04-14 17:37:1434namespace crypto {
[email protected]28ae8fe2009-06-05 18:25:0635
[email protected]308379a52009-10-07 02:46:3136// Used internally by RSAPrivateKey for serializing and deserializing
37// PKCS #8 PrivateKeyInfo and PublicKeyInfo.
[email protected]4b559b4d2011-04-14 17:37:1438class PrivateKeyInfoCodec {
[email protected]308379a52009-10-07 02:46:3139 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]90a28472009-10-20 20:39:5250
[email protected]308379a52009-10-07 02:46:3151 // |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]1889dc1b2010-10-14 22:03:1355 explicit PrivateKeyInfoCodec(bool big_endian);
56
57 ~PrivateKeyInfoCodec();
[email protected]308379a52009-10-07 02:46:3158
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
[email protected]eed00112011-02-08 14:28:2967 // Exports the contents of the integer components to the ASN.1 DER encoding
68 // of the RSAPublicKey structure to |output|.
69 bool ExportPublicKey(std::vector<uint8>* output);
70
[email protected]308379a52009-10-07 02:46:3171 // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input|
72 // and populates the integer components with |big_endian_| byte-significance.
[email protected]90a28472009-10-20 20:39:5273 // IMPORTANT NOTE: This is currently *not* security-approved for importing
74 // keys from unstrusted sources.
[email protected]308379a52009-10-07 02:46:3175 bool Import(const std::vector<uint8>& input);
76
77 // Accessors to the contents of the integer components of the PrivateKeyInfo
78 // structure.
79 std::vector<uint8>* modulus() { return &modulus_; };
80 std::vector<uint8>* public_exponent() { return &public_exponent_; };
81 std::vector<uint8>* private_exponent() { return &private_exponent_; };
82 std::vector<uint8>* prime1() { return &prime1_; };
83 std::vector<uint8>* prime2() { return &prime2_; };
84 std::vector<uint8>* exponent1() { return &exponent1_; };
85 std::vector<uint8>* exponent2() { return &exponent2_; };
86 std::vector<uint8>* coefficient() { return &coefficient_; };
87
88 private:
89 // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_|
90 // value.
91 void PrependInteger(const std::vector<uint8>& in, std::list<uint8>* out);
92 void PrependInteger(uint8* val, int num_bytes, std::list<uint8>* data);
[email protected]90a28472009-10-20 20:39:5293
[email protected]308379a52009-10-07 02:46:3194 // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian|
95 // byte-significance into |data| as an ASN.1 integer.
96 void PrependIntegerImpl(uint8* val,
97 int num_bytes,
98 std::list<uint8>* data,
99 bool big_endian);
100
101 // Utility wrappers for ReadIntegerImpl that use the class's |big_endian_|
102 // value.
103 bool ReadInteger(uint8** pos, uint8* end, std::vector<uint8>* out);
104 bool ReadIntegerWithExpectedSize(uint8** pos,
105 uint8* end,
106 size_t expected_size,
107 std::vector<uint8>* out);
108
109 // Reads an ASN.1 integer from |pos|, and stores the result into |out| with
110 // |big_endian| byte-significance.
111 bool ReadIntegerImpl(uint8** pos,
112 uint8* end,
[email protected]90a28472009-10-20 20:39:52113 std::vector<uint8>* out,
[email protected]308379a52009-10-07 02:46:31114 bool big_endian);
[email protected]90a28472009-10-20 20:39:52115
[email protected]308379a52009-10-07 02:46:31116 // Prepends the integer stored in |val|, starting a index |start|, for
117 // |num_bytes| bytes onto |data|.
118 void PrependBytes(uint8* val,
119 int start,
120 int num_bytes,
121 std::list<uint8>* data);
122
123 // Helper to prepend an ASN.1 length field.
124 void PrependLength(size_t size, std::list<uint8>* data);
125
126 // Helper to prepend an ASN.1 type header.
127 void PrependTypeHeaderAndLength(uint8 type,
128 uint32 length,
129 std::list<uint8>* output);
130
131 // Helper to prepend an ASN.1 bit string
132 void PrependBitString(uint8* val, int num_bytes, std::list<uint8>* output);
133
134 // Read an ASN.1 length field. This also checks that the length does not
135 // extend beyond |end|.
136 bool ReadLength(uint8** pos, uint8* end, uint32* result);
137
138 // Read an ASN.1 type header and its length.
139 bool ReadTypeHeaderAndLength(uint8** pos,
140 uint8* end,
141 uint8 expected_tag,
142 uint32* length);
143
144 // Read an ASN.1 sequence declaration. This consumes the type header and
145 // length field, but not the contents of the sequence.
146 bool ReadSequence(uint8** pos, uint8* end);
147
148 // Read the RSA AlgorithmIdentifier.
149 bool ReadAlgorithmIdentifier(uint8** pos, uint8* end);
150
151 // Read one of the two version fields in PrivateKeyInfo.
152 bool ReadVersion(uint8** pos, uint8* end);
153
154 // The byte-significance of the stored components (modulus, etc..).
155 bool big_endian_;
156
157 // Component integers of the PrivateKeyInfo
158 std::vector<uint8> modulus_;
159 std::vector<uint8> public_exponent_;
160 std::vector<uint8> private_exponent_;
161 std::vector<uint8> prime1_;
162 std::vector<uint8> prime2_;
163 std::vector<uint8> exponent1_;
164 std::vector<uint8> exponent2_;
165 std::vector<uint8> coefficient_;
166
167 DISALLOW_COPY_AND_ASSIGN(PrivateKeyInfoCodec);
168};
169
[email protected]28ae8fe2009-06-05 18:25:06170// Encapsulates an RSA private key. Can be used to generate new keys, export
171// keys to other formats, or to extract a public key.
[email protected]f61c3972010-12-23 09:54:15172// TODO(hclam): This class should be ref-counted so it can be reused easily.
[email protected]d613a9902011-08-05 20:59:11173class CRYPTO_EXPORT RSAPrivateKey {
[email protected]28ae8fe2009-06-05 18:25:06174 public:
[email protected]a502bbe72011-01-07 18:06:45175 ~RSAPrivateKey();
176
[email protected]28ae8fe2009-06-05 18:25:06177 // Create a new random instance. Can return NULL if initialization fails.
178 static RSAPrivateKey* Create(uint16 num_bits);
179
[email protected]74648052010-08-10 19:37:51180 // Create a new random instance. Can return NULL if initialization fails.
181 // The created key is permanent and is not exportable in plaintext form.
182 //
183 // NOTE: Currently only available if USE_NSS is defined.
184 static RSAPrivateKey* CreateSensitive(uint16 num_bits);
185
[email protected]28ae8fe2009-06-05 18:25:06186 // Create a new instance by importing an existing private key. The format is
187 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if
188 // initialization fails.
189 static RSAPrivateKey* CreateFromPrivateKeyInfo(
190 const std::vector<uint8>& input);
191
[email protected]74648052010-08-10 19:37:51192 // Create a new instance by importing an existing private key. The format is
193 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if
194 // initialization fails.
195 // The created key is permanent and is not exportable in plaintext form.
196 //
197 // NOTE: Currently only available if USE_NSS is defined.
198 static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo(
199 const std::vector<uint8>& input);
200
201 // Import an existing public key, and then search for the private
202 // half in the key database. The format of the public key blob is is
203 // an X509 SubjectPublicKeyInfo block. This can return NULL if
204 // initialization fails or the private key cannot be found. The
205 // caller takes ownership of the returned object, but nothing new is
206 // created in the key database.
207 //
208 // NOTE: Currently only available if USE_NSS is defined.
209 static RSAPrivateKey* FindFromPublicKeyInfo(
210 const std::vector<uint8>& input);
211
[email protected]be796bb2010-11-18 15:43:43212#if defined(USE_OPENSSL)
213 EVP_PKEY* key() { return key_; }
214#elif defined(USE_NSS)
[email protected]13555c122009-10-08 01:18:02215 SECKEYPrivateKeyStr* key() { return key_; }
[email protected]56f2ec392010-12-17 21:13:16216 SECKEYPublicKeyStr* public_key() { return public_key_; }
[email protected]71a9f842009-09-24 01:21:12217#elif defined(OS_WIN)
[email protected]28ae8fe2009-06-05 18:25:06218 HCRYPTPROV provider() { return provider_; }
219 HCRYPTKEY key() { return key_; }
[email protected]0691bdf42009-10-02 21:05:01220#elif defined(OS_MACOSX)
[email protected]0691bdf42009-10-02 21:05:01221 CSSM_KEY_PTR key() { return &key_; }
[email protected]eed00112011-02-08 14:28:29222 CSSM_KEY_PTR public_key() { return &public_key_; }
[email protected]28ae8fe2009-06-05 18:25:06223#endif
224
[email protected]58782882011-12-03 01:12:08225 // Creates a copy of the object.
226 RSAPrivateKey* Copy() const;
227
[email protected]28ae8fe2009-06-05 18:25:06228 // Exports the private key to a PKCS #1 PrivateKey block.
[email protected]58782882011-12-03 01:12:08229 bool ExportPrivateKey(std::vector<uint8>* output) const;
[email protected]28ae8fe2009-06-05 18:25:06230
231 // Exports the public key to an X509 SubjectPublicKeyInfo block.
[email protected]58782882011-12-03 01:12:08232 bool ExportPublicKey(std::vector<uint8>* output) const;
[email protected]28ae8fe2009-06-05 18:25:06233
[email protected]1f923942010-11-17 14:39:22234 private:
[email protected]74648052010-08-10 19:37:51235#if defined(USE_NSS)
236 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey);
237 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey);
238#endif
239
240 // Constructor is private. Use one of the Create*() or Find*()
241 // methods above instead.
[email protected]28ae8fe2009-06-05 18:25:06242 RSAPrivateKey();
243
[email protected]74648052010-08-10 19:37:51244 // Shared helper for Create() and CreateSensitive().
245 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a
246 // flags arg created by ORing together some enumerated values.
247 static RSAPrivateKey* CreateWithParams(uint16 num_bits,
248 bool permanent,
249 bool sensitive);
250
251 // Shared helper for CreateFromPrivateKeyInfo() and
252 // CreateSensitiveFromPrivateKeyInfo().
253 static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams(
254 const std::vector<uint8>& input, bool permanent, bool sensitive);
255
[email protected]1f923942010-11-17 14:39:22256#if defined(USE_OPENSSL)
257 EVP_PKEY* key_;
258#elif defined(USE_NSS)
[email protected]13555c122009-10-08 01:18:02259 SECKEYPrivateKeyStr* key_;
260 SECKEYPublicKeyStr* public_key_;
[email protected]71a9f842009-09-24 01:21:12261#elif defined(OS_WIN)
[email protected]28ae8fe2009-06-05 18:25:06262 bool InitProvider();
263
[email protected]692033a2010-04-09 18:40:50264 ScopedHCRYPTPROV provider_;
265 ScopedHCRYPTKEY key_;
[email protected]0691bdf42009-10-02 21:05:01266#elif defined(OS_MACOSX)
267 CSSM_KEY key_;
[email protected]eed00112011-02-08 14:28:29268 CSSM_KEY public_key_;
[email protected]28ae8fe2009-06-05 18:25:06269#endif
270
271 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey);
272};
273
[email protected]4b559b4d2011-04-14 17:37:14274} // namespace crypto
[email protected]28ae8fe2009-06-05 18:25:06275
[email protected]4b559b4d2011-04-14 17:37:14276#endif // CRYPTO_RSA_PRIVATE_KEY_H_