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
3 // found in the LICENSE file.
5 #ifndef CRYPTO_RSA_PRIVATE_KEY_H_
6 #define CRYPTO_RSA_PRIVATE_KEY_H_
8 #include "build/build_config.h"
13 #include "base/basictypes.h"
14 #include "crypto/crypto_export.h"
16 #if defined(USE_NSS_CERTS)
17 #include "base/gtest_prod_util.h"
20 #if defined(USE_OPENSSL)
21 // Forward declaration for openssl/*.h
22 typedef struct evp_pkey_st EVP_PKEY
;
24 // Forward declaration.
25 typedef struct PK11SlotInfoStr PK11SlotInfo
;
26 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey
;
27 typedef struct SECKEYPublicKeyStr SECKEYPublicKey
;
33 // Used internally by RSAPrivateKey for serializing and deserializing
34 // PKCS #8 PrivateKeyInfo and PublicKeyInfo.
35 class PrivateKeyInfoCodec
{
38 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8.
39 static const uint8 kRsaAlgorithmIdentifier
[];
41 // ASN.1 tags for some types we use.
42 static const uint8 kBitStringTag
= 0x03;
43 static const uint8 kIntegerTag
= 0x02;
44 static const uint8 kNullTag
= 0x05;
45 static const uint8 kOctetStringTag
= 0x04;
46 static const uint8 kSequenceTag
= 0x30;
48 // |big_endian| here specifies the byte-significance of the integer components
49 // that will be parsed & serialized (modulus(), etc...) during Import(),
50 // Export() and ExportPublicKeyInfo() -- not the ASN.1 DER encoding of the
51 // PrivateKeyInfo/PublicKeyInfo (which is always big-endian).
52 explicit PrivateKeyInfoCodec(bool big_endian
);
54 ~PrivateKeyInfoCodec();
56 // Exports the contents of the integer components to the ASN.1 DER encoding
57 // of the PrivateKeyInfo structure to |output|.
58 bool Export(std::vector
<uint8
>* output
);
60 // Exports the contents of the integer components to the ASN.1 DER encoding
61 // of the PublicKeyInfo structure to |output|.
62 bool ExportPublicKeyInfo(std::vector
<uint8
>* output
);
64 // Exports the contents of the integer components to the ASN.1 DER encoding
65 // of the RSAPublicKey structure to |output|.
66 bool ExportPublicKey(std::vector
<uint8
>* output
);
68 // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input|
69 // and populates the integer components with |big_endian_| byte-significance.
70 // IMPORTANT NOTE: This is currently *not* security-approved for importing
71 // keys from unstrusted sources.
72 bool Import(const std::vector
<uint8
>& input
);
74 // Accessors to the contents of the integer components of the PrivateKeyInfo
76 std::vector
<uint8
>* modulus() { return &modulus_
; };
77 std::vector
<uint8
>* public_exponent() { return &public_exponent_
; };
78 std::vector
<uint8
>* private_exponent() { return &private_exponent_
; };
79 std::vector
<uint8
>* prime1() { return &prime1_
; };
80 std::vector
<uint8
>* prime2() { return &prime2_
; };
81 std::vector
<uint8
>* exponent1() { return &exponent1_
; };
82 std::vector
<uint8
>* exponent2() { return &exponent2_
; };
83 std::vector
<uint8
>* coefficient() { return &coefficient_
; };
86 // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_|
88 void PrependInteger(const std::vector
<uint8
>& in
, std::list
<uint8
>* out
);
89 void PrependInteger(uint8
* val
, int num_bytes
, std::list
<uint8
>* data
);
91 // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian|
92 // byte-significance into |data| as an ASN.1 integer.
93 void PrependIntegerImpl(uint8
* val
,
95 std::list
<uint8
>* data
,
98 // Utility wrappers for ReadIntegerImpl that use the class's |big_endian_|
100 bool ReadInteger(uint8
** pos
, uint8
* end
, std::vector
<uint8
>* out
);
101 bool ReadIntegerWithExpectedSize(uint8
** pos
,
103 size_t expected_size
,
104 std::vector
<uint8
>* out
);
106 // Reads an ASN.1 integer from |pos|, and stores the result into |out| with
107 // |big_endian| byte-significance.
108 bool ReadIntegerImpl(uint8
** pos
,
110 std::vector
<uint8
>* out
,
113 // Prepends the integer stored in |val|, starting a index |start|, for
114 // |num_bytes| bytes onto |data|.
115 void PrependBytes(uint8
* val
,
118 std::list
<uint8
>* data
);
120 // Helper to prepend an ASN.1 length field.
121 void PrependLength(size_t size
, std::list
<uint8
>* data
);
123 // Helper to prepend an ASN.1 type header.
124 void PrependTypeHeaderAndLength(uint8 type
,
126 std::list
<uint8
>* output
);
128 // Helper to prepend an ASN.1 bit string
129 void PrependBitString(uint8
* val
, int num_bytes
, std::list
<uint8
>* output
);
131 // Read an ASN.1 length field. This also checks that the length does not
132 // extend beyond |end|.
133 bool ReadLength(uint8
** pos
, uint8
* end
, uint32
* result
);
135 // Read an ASN.1 type header and its length.
136 bool ReadTypeHeaderAndLength(uint8
** pos
,
141 // Read an ASN.1 sequence declaration. This consumes the type header and
142 // length field, but not the contents of the sequence.
143 bool ReadSequence(uint8
** pos
, uint8
* end
);
145 // Read the RSA AlgorithmIdentifier.
146 bool ReadAlgorithmIdentifier(uint8
** pos
, uint8
* end
);
148 // Read one of the two version fields in PrivateKeyInfo.
149 bool ReadVersion(uint8
** pos
, uint8
* end
);
151 // The byte-significance of the stored components (modulus, etc..).
154 // Component integers of the PrivateKeyInfo
155 std::vector
<uint8
> modulus_
;
156 std::vector
<uint8
> public_exponent_
;
157 std::vector
<uint8
> private_exponent_
;
158 std::vector
<uint8
> prime1_
;
159 std::vector
<uint8
> prime2_
;
160 std::vector
<uint8
> exponent1_
;
161 std::vector
<uint8
> exponent2_
;
162 std::vector
<uint8
> coefficient_
;
164 DISALLOW_COPY_AND_ASSIGN(PrivateKeyInfoCodec
);
167 // Encapsulates an RSA private key. Can be used to generate new keys, export
168 // keys to other formats, or to extract a public key.
169 // TODO(hclam): This class should be ref-counted so it can be reused easily.
170 class CRYPTO_EXPORT RSAPrivateKey
{
174 // Create a new random instance. Can return NULL if initialization fails.
175 static RSAPrivateKey
* Create(uint16 num_bits
);
177 // Create a new instance by importing an existing private key. The format is
178 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if
179 // initialization fails.
180 static RSAPrivateKey
* CreateFromPrivateKeyInfo(
181 const std::vector
<uint8
>& input
);
183 #if defined(USE_OPENSSL)
184 // Create a new instance from an existing EVP_PKEY, taking a
185 // reference to it. |key| must be an RSA key. Returns NULL on
187 static RSAPrivateKey
* CreateFromKey(EVP_PKEY
* key
);
189 // Create a new instance by referencing an existing private key
190 // structure. Does not import the key.
191 static RSAPrivateKey
* CreateFromKey(SECKEYPrivateKey
* key
);
194 // TODO(davidben): These functions are used when NSS is the platform key
195 // store, but they also assume that the internal crypto library is NSS. Split
196 // out the convenience NSS platform key methods from the logic which expects
197 // an RSAPrivateKey. See https://crbug.com/478777.
198 #if defined(USE_NSS_CERTS) && !defined(USE_OPENSSL)
199 // Create a new random instance in |slot|. Can return NULL if initialization
200 // fails. The created key is permanent and is not exportable in plaintext
202 static RSAPrivateKey
* CreateSensitive(PK11SlotInfo
* slot
, uint16 num_bits
);
204 // Create a new instance in |slot| by importing an existing private key. The
205 // format is an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can
206 // return NULL if initialization fails.
207 // The created key is permanent and is not exportable in plaintext form.
208 static RSAPrivateKey
* CreateSensitiveFromPrivateKeyInfo(
210 const std::vector
<uint8
>& input
);
212 // Import an existing public key, and then search for the private
213 // half in the key database. The format of the public key blob is is
214 // an X509 SubjectPublicKeyInfo block. This can return NULL if
215 // initialization fails or the private key cannot be found. The
216 // caller takes ownership of the returned object, but nothing new is
217 // created in the key database.
218 static RSAPrivateKey
* FindFromPublicKeyInfo(
219 const std::vector
<uint8
>& input
);
221 // Import an existing public key, and then search for the private
222 // half in the slot specified by |slot|. The format of the public
223 // key blob is is an X509 SubjectPublicKeyInfo block. This can return
224 // NULL if initialization fails or the private key cannot be found.
225 // The caller takes ownership of the returned object, but nothing new
226 // is created in the slot.
227 static RSAPrivateKey
* FindFromPublicKeyInfoInSlot(
228 const std::vector
<uint8
>& input
,
230 #endif // USE_NSS_CERTS && !USE_OPENSSL
232 #if defined(USE_OPENSSL)
233 EVP_PKEY
* key() { return key_
; }
235 SECKEYPrivateKey
* key() { return key_
; }
236 SECKEYPublicKey
* public_key() { return public_key_
; }
239 // Creates a copy of the object.
240 RSAPrivateKey
* Copy() const;
242 // Exports the private key to a PKCS #1 PrivateKey block.
243 bool ExportPrivateKey(std::vector
<uint8
>* output
) const;
245 // Exports the public key to an X509 SubjectPublicKeyInfo block.
246 bool ExportPublicKey(std::vector
<uint8
>* output
) const;
249 #if defined(USE_NSS_CERTS)
250 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest
, FindFromPublicKey
);
251 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest
, FailedFindFromPublicKey
);
254 // Constructor is private. Use one of the Create*() or Find*()
255 // methods above instead.
258 #if !defined(USE_OPENSSL)
259 // Shared helper for Create() and CreateSensitive().
260 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a
261 // flags arg created by ORing together some enumerated values.
262 // Note: |permanent| is only supported when USE_NSS_CERTS is defined.
263 static RSAPrivateKey
* CreateWithParams(PK11SlotInfo
* slot
,
268 // Shared helper for CreateFromPrivateKeyInfo() and
269 // CreateSensitiveFromPrivateKeyInfo().
270 // Note: |permanent| is only supported when USE_NSS_CERTS is defined.
271 static RSAPrivateKey
* CreateFromPrivateKeyInfoWithParams(
273 const std::vector
<uint8
>& input
,
278 #if defined(USE_NSS_CERTS)
279 // Import an existing public key. The format of the public key blob
280 // is an X509 SubjectPublicKeyInfo block. This can return NULL if
281 // initialization fails. The caller takes ownership of the returned
282 // object. Note that this method doesn't initialize the |key_| member.
283 static RSAPrivateKey
* InitPublicPart(const std::vector
<uint8
>& input
);
286 #if defined(USE_OPENSSL)
289 SECKEYPrivateKey
* key_
;
290 SECKEYPublicKey
* public_key_
;
293 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey
);
296 } // namespace crypto
298 #endif // CRYPTO_RSA_PRIVATE_KEY_H_