blob: 9b8b4fd4cc7012a55c8f44e1de68650947424b9f [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 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
5#ifndef BASE_CRYPTO_RSA_PRIVATE_KEY_H_
6#define BASE_CRYPTO_RSA_PRIVATE_KEY_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]28ae8fe2009-06-05 18:25:068
9#include "build/build_config.h"
10
[email protected]1f923942010-11-17 14:39:2211#if defined(USE_OPENSSL)
12// Forward declaration for openssl/*.h
13typedef struct evp_pkey_st EVP_PKEY;
14#elif defined(USE_NSS)
[email protected]13555c122009-10-08 01:18:0215// Forward declaration.
16struct SECKEYPrivateKeyStr;
17struct SECKEYPublicKeyStr;
[email protected]71a9f842009-09-24 01:21:1218#elif defined(OS_MACOSX)
[email protected]0691bdf42009-10-02 21:05:0119#include <Security/cssm.h>
[email protected]28ae8fe2009-06-05 18:25:0620#endif
21
[email protected]308379a52009-10-07 02:46:3122#include <list>
[email protected]28ae8fe2009-06-05 18:25:0623#include <vector>
24
25#include "base/basictypes.h"
26
[email protected]692033a2010-04-09 18:40:5027#if defined(OS_WIN)
28#include "base/crypto/scoped_capi_types.h"
29#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]28ae8fe2009-06-05 18:25:0634namespace base {
35
[email protected]308379a52009-10-07 02:46:3136// Used internally by RSAPrivateKey for serializing and deserializing
37// PKCS #8 PrivateKeyInfo and PublicKeyInfo.
38class 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]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
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]90a28472009-10-20 20:39:5269 // IMPORTANT NOTE: This is currently *not* security-approved for importing
70 // keys from unstrusted sources.
[email protected]308379a52009-10-07 02:46:3171 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]90a28472009-10-20 20:39:5289
[email protected]308379a52009-10-07 02:46:3190 // 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]90a28472009-10-20 20:39:52109 std::vector<uint8>* out,
[email protected]308379a52009-10-07 02:46:31110 bool big_endian);
[email protected]90a28472009-10-20 20:39:52111
[email protected]308379a52009-10-07 02:46:31112 // 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]28ae8fe2009-06-05 18:25:06166// 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]f61c3972010-12-23 09:54:15168// TODO(hclam): This class should be ref-counted so it can be reused easily.
[email protected]28ae8fe2009-06-05 18:25:06169class RSAPrivateKey {
170 public:
[email protected]a502bbe72011-01-07 18:06:45171 ~RSAPrivateKey();
172
[email protected]28ae8fe2009-06-05 18:25:06173 // Create a new random instance. Can return NULL if initialization fails.
174 static RSAPrivateKey* Create(uint16 num_bits);
175
[email protected]74648052010-08-10 19:37:51176 // 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]28ae8fe2009-06-05 18:25:06182 // 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]74648052010-08-10 19:37:51188 // 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]be796bb2010-11-18 15:43:43208#if defined(USE_OPENSSL)
209 EVP_PKEY* key() { return key_; }
210#elif defined(USE_NSS)
[email protected]13555c122009-10-08 01:18:02211 SECKEYPrivateKeyStr* key() { return key_; }
[email protected]56f2ec392010-12-17 21:13:16212 SECKEYPublicKeyStr* public_key() { return public_key_; }
[email protected]71a9f842009-09-24 01:21:12213#elif defined(OS_WIN)
[email protected]28ae8fe2009-06-05 18:25:06214 HCRYPTPROV provider() { return provider_; }
215 HCRYPTKEY key() { return key_; }
[email protected]0691bdf42009-10-02 21:05:01216#elif defined(OS_MACOSX)
[email protected]0691bdf42009-10-02 21:05:01217 CSSM_KEY_PTR key() { return &key_; }
[email protected]28ae8fe2009-06-05 18:25:06218#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]1f923942010-11-17 14:39:22226 private:
[email protected]74648052010-08-10 19:37:51227#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]28ae8fe2009-06-05 18:25:06234 RSAPrivateKey();
235
[email protected]74648052010-08-10 19:37:51236 // 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]1f923942010-11-17 14:39:22248#if defined(USE_OPENSSL)
249 EVP_PKEY* key_;
250#elif defined(USE_NSS)
[email protected]13555c122009-10-08 01:18:02251 SECKEYPrivateKeyStr* key_;
252 SECKEYPublicKeyStr* public_key_;
[email protected]71a9f842009-09-24 01:21:12253#elif defined(OS_WIN)
[email protected]28ae8fe2009-06-05 18:25:06254 bool InitProvider();
255
[email protected]692033a2010-04-09 18:40:50256 ScopedHCRYPTPROV provider_;
257 ScopedHCRYPTKEY key_;
[email protected]0691bdf42009-10-02 21:05:01258#elif defined(OS_MACOSX)
259 CSSM_KEY key_;
[email protected]28ae8fe2009-06-05 18:25:06260#endif
261
262 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey);
263};
264
265} // namespace base
266
267#endif // BASE_CRYPTO_RSA_PRIVATE_KEY_H_