OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ | 5 #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ |
6 #define CRYPTO_RSA_PRIVATE_KEY_H_ | 6 #define CRYPTO_RSA_PRIVATE_KEY_H_ |
7 | 7 |
8 #include "build/build_config.h" | |
9 | |
10 #include <list> | 8 #include <list> |
11 #include <vector> | 9 #include <vector> |
12 | 10 |
13 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "build/build_config.h" |
14 #include "crypto/crypto_export.h" | 13 #include "crypto/crypto_export.h" |
15 | 14 |
16 #if defined(USE_NSS_CERTS) | |
17 #include "base/gtest_prod_util.h" | |
18 #endif | |
19 | |
20 #if defined(USE_OPENSSL) | 15 #if defined(USE_OPENSSL) |
21 // Forward declaration for openssl/*.h | 16 // Forward declaration for openssl/*.h |
22 typedef struct evp_pkey_st EVP_PKEY; | 17 typedef struct evp_pkey_st EVP_PKEY; |
23 #else | 18 #else |
24 // Forward declaration. | 19 // Forward declaration. |
25 typedef struct PK11SlotInfoStr PK11SlotInfo; | 20 typedef struct PK11SlotInfoStr PK11SlotInfo; |
26 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; | 21 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; |
27 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; | 22 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; |
28 #endif | 23 #endif |
29 | 24 |
30 | 25 |
31 namespace crypto { | 26 namespace crypto { |
32 | 27 |
33 // Used internally by RSAPrivateKey for serializing and deserializing | 28 // Used internally by RSAPrivateKey for serializing and deserializing |
34 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. | 29 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. |
35 class PrivateKeyInfoCodec { | 30 class PrivateKeyInfoCodec { |
36 public: | 31 public: |
37 | |
38 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. | 32 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. |
39 static const uint8 kRsaAlgorithmIdentifier[]; | 33 static const uint8 kRsaAlgorithmIdentifier[]; |
40 | 34 |
41 // ASN.1 tags for some types we use. | 35 // ASN.1 tags for some types we use. |
42 static const uint8 kBitStringTag = 0x03; | 36 static const uint8 kBitStringTag = 0x03; |
43 static const uint8 kIntegerTag = 0x02; | 37 static const uint8 kIntegerTag = 0x02; |
44 static const uint8 kNullTag = 0x05; | 38 static const uint8 kNullTag = 0x05; |
45 static const uint8 kOctetStringTag = 0x04; | 39 static const uint8 kOctetStringTag = 0x04; |
46 static const uint8 kSequenceTag = 0x30; | 40 static const uint8 kSequenceTag = 0x30; |
47 | 41 |
(...skipping 18 matching lines...) Expand all Loading... |
66 bool ExportPublicKey(std::vector<uint8>* output); | 60 bool ExportPublicKey(std::vector<uint8>* output); |
67 | 61 |
68 // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input| | 62 // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input| |
69 // and populates the integer components with |big_endian_| byte-significance. | 63 // and populates the integer components with |big_endian_| byte-significance. |
70 // IMPORTANT NOTE: This is currently *not* security-approved for importing | 64 // IMPORTANT NOTE: This is currently *not* security-approved for importing |
71 // keys from unstrusted sources. | 65 // keys from unstrusted sources. |
72 bool Import(const std::vector<uint8>& input); | 66 bool Import(const std::vector<uint8>& input); |
73 | 67 |
74 // Accessors to the contents of the integer components of the PrivateKeyInfo | 68 // Accessors to the contents of the integer components of the PrivateKeyInfo |
75 // structure. | 69 // structure. |
76 std::vector<uint8>* modulus() { return &modulus_; }; | 70 std::vector<uint8>* modulus() { return &modulus_; } |
77 std::vector<uint8>* public_exponent() { return &public_exponent_; }; | 71 std::vector<uint8>* public_exponent() { return &public_exponent_; } |
78 std::vector<uint8>* private_exponent() { return &private_exponent_; }; | 72 std::vector<uint8>* private_exponent() { return &private_exponent_; } |
79 std::vector<uint8>* prime1() { return &prime1_; }; | 73 std::vector<uint8>* prime1() { return &prime1_; } |
80 std::vector<uint8>* prime2() { return &prime2_; }; | 74 std::vector<uint8>* prime2() { return &prime2_; } |
81 std::vector<uint8>* exponent1() { return &exponent1_; }; | 75 std::vector<uint8>* exponent1() { return &exponent1_; } |
82 std::vector<uint8>* exponent2() { return &exponent2_; }; | 76 std::vector<uint8>* exponent2() { return &exponent2_; } |
83 std::vector<uint8>* coefficient() { return &coefficient_; }; | 77 std::vector<uint8>* coefficient() { return &coefficient_; } |
84 | 78 |
85 private: | 79 private: |
86 // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_| | 80 // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_| |
87 // value. | 81 // value. |
88 void PrependInteger(const std::vector<uint8>& in, std::list<uint8>* out); | 82 void PrependInteger(const std::vector<uint8>& in, std::list<uint8>* out); |
89 void PrependInteger(uint8* val, int num_bytes, std::list<uint8>* data); | 83 void PrependInteger(uint8* val, int num_bytes, std::list<uint8>* data); |
90 | 84 |
91 // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian| | 85 // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian| |
92 // byte-significance into |data| as an ASN.1 integer. | 86 // byte-significance into |data| as an ASN.1 integer. |
93 void PrependIntegerImpl(uint8* val, | 87 void PrependIntegerImpl(uint8* val, |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 // Creates a copy of the object. | 195 // Creates a copy of the object. |
202 RSAPrivateKey* Copy() const; | 196 RSAPrivateKey* Copy() const; |
203 | 197 |
204 // Exports the private key to a PKCS #1 PrivateKey block. | 198 // Exports the private key to a PKCS #1 PrivateKey block. |
205 bool ExportPrivateKey(std::vector<uint8>* output) const; | 199 bool ExportPrivateKey(std::vector<uint8>* output) const; |
206 | 200 |
207 // Exports the public key to an X509 SubjectPublicKeyInfo block. | 201 // Exports the public key to an X509 SubjectPublicKeyInfo block. |
208 bool ExportPublicKey(std::vector<uint8>* output) const; | 202 bool ExportPublicKey(std::vector<uint8>* output) const; |
209 | 203 |
210 private: | 204 private: |
211 #if defined(USE_NSS_CERTS) | |
212 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); | |
213 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); | |
214 #endif | |
215 | |
216 // Constructor is private. Use one of the Create*() methods above instead. | 205 // Constructor is private. Use one of the Create*() methods above instead. |
217 RSAPrivateKey(); | 206 RSAPrivateKey(); |
218 | 207 |
219 #if defined(USE_OPENSSL) | 208 #if defined(USE_OPENSSL) |
220 EVP_PKEY* key_; | 209 EVP_PKEY* key_; |
221 #else | 210 #else |
222 SECKEYPrivateKey* key_; | 211 SECKEYPrivateKey* key_; |
223 SECKEYPublicKey* public_key_; | 212 SECKEYPublicKey* public_key_; |
224 #endif | 213 #endif |
225 | 214 |
226 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | 215 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
227 }; | 216 }; |
228 | 217 |
229 } // namespace crypto | 218 } // namespace crypto |
230 | 219 |
231 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ | 220 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ |
OLD | NEW |