OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 BASE_CRYPTO_RSA_PRIVATE_KEY_H_ | 5 #ifndef BASE_CRYPTO_RSA_PRIVATE_KEY_H_ |
6 #define BASE_CRYPTO_RSA_PRIVATE_KEY_H_ | 6 #define BASE_CRYPTO_RSA_PRIVATE_KEY_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #if defined(USE_NSS) | 10 #if defined(USE_NSS) |
11 #include <cryptoht.h> | 11 #include <cryptoht.h> |
12 #include <keythi.h> | 12 #include <keythi.h> |
13 #elif defined(OS_MACOSX) | 13 #elif defined(OS_MACOSX) |
14 #include <Security/cssm.h> | 14 #include <Security/cssm.h> |
15 #elif defined(OS_WIN) | 15 #elif defined(OS_WIN) |
16 #include <windows.h> | 16 #include <windows.h> |
17 #include <wincrypt.h> | 17 #include <wincrypt.h> |
18 #endif | 18 #endif |
19 | 19 |
| 20 #include <list> |
20 #include <vector> | 21 #include <vector> |
21 | 22 |
22 #include "base/basictypes.h" | 23 #include "base/basictypes.h" |
23 | 24 |
24 namespace base { | 25 namespace base { |
25 | 26 |
| 27 // Used internally by RSAPrivateKey for serializing and deserializing |
| 28 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. |
| 29 class PrivateKeyInfoCodec { |
| 30 public: |
| 31 |
| 32 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. |
| 33 static const uint8 kRsaAlgorithmIdentifier[]; |
| 34 |
| 35 // ASN.1 tags for some types we use. |
| 36 static const uint8 kBitStringTag = 0x03; |
| 37 static const uint8 kIntegerTag = 0x02; |
| 38 static const uint8 kNullTag = 0x05; |
| 39 static const uint8 kOctetStringTag = 0x04; |
| 40 static const uint8 kSequenceTag = 0x30; |
| 41 |
| 42 // |big_endian| here specifies the byte-significance of the integer components |
| 43 // that will be parsed & serialized (modulus(), etc...) during Import(), |
| 44 // Export() and ExportPublicKeyInfo() -- not the ASN.1 DER encoding of the |
| 45 // PrivateKeyInfo/PublicKeyInfo (which is always big-endian). |
| 46 explicit PrivateKeyInfoCodec(bool big_endian) : big_endian_(big_endian) {} |
| 47 |
| 48 // Exports the contents of the integer components to the ASN.1 DER encoding |
| 49 // of the PrivateKeyInfo structure to |output|. |
| 50 bool Export(std::vector<uint8>* output); |
| 51 |
| 52 // Exports the contents of the integer components to the ASN.1 DER encoding |
| 53 // of the PublicKeyInfo structure to |output|. |
| 54 bool ExportPublicKeyInfo(std::vector<uint8>* output); |
| 55 |
| 56 // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input| |
| 57 // and populates the integer components with |big_endian_| byte-significance. |
| 58 bool Import(const std::vector<uint8>& input); |
| 59 |
| 60 // Accessors to the contents of the integer components of the PrivateKeyInfo |
| 61 // structure. |
| 62 std::vector<uint8>* modulus() { return &modulus_; }; |
| 63 std::vector<uint8>* public_exponent() { return &public_exponent_; }; |
| 64 std::vector<uint8>* private_exponent() { return &private_exponent_; }; |
| 65 std::vector<uint8>* prime1() { return &prime1_; }; |
| 66 std::vector<uint8>* prime2() { return &prime2_; }; |
| 67 std::vector<uint8>* exponent1() { return &exponent1_; }; |
| 68 std::vector<uint8>* exponent2() { return &exponent2_; }; |
| 69 std::vector<uint8>* coefficient() { return &coefficient_; }; |
| 70 |
| 71 private: |
| 72 // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_| |
| 73 // value. |
| 74 void PrependInteger(const std::vector<uint8>& in, std::list<uint8>* out); |
| 75 void PrependInteger(uint8* val, int num_bytes, std::list<uint8>* data); |
| 76 |
| 77 // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian| |
| 78 // byte-significance into |data| as an ASN.1 integer. |
| 79 void PrependIntegerImpl(uint8* val, |
| 80 int num_bytes, |
| 81 std::list<uint8>* data, |
| 82 bool big_endian); |
| 83 |
| 84 // Utility wrappers for ReadIntegerImpl that use the class's |big_endian_| |
| 85 // value. |
| 86 bool ReadInteger(uint8** pos, uint8* end, std::vector<uint8>* out); |
| 87 bool ReadIntegerWithExpectedSize(uint8** pos, |
| 88 uint8* end, |
| 89 size_t expected_size, |
| 90 std::vector<uint8>* out); |
| 91 |
| 92 // Reads an ASN.1 integer from |pos|, and stores the result into |out| with |
| 93 // |big_endian| byte-significance. |
| 94 bool ReadIntegerImpl(uint8** pos, |
| 95 uint8* end, |
| 96 std::vector<uint8>* out, |
| 97 bool big_endian); |
| 98 |
| 99 // Prepends the integer stored in |val|, starting a index |start|, for |
| 100 // |num_bytes| bytes onto |data|. |
| 101 void PrependBytes(uint8* val, |
| 102 int start, |
| 103 int num_bytes, |
| 104 std::list<uint8>* data); |
| 105 |
| 106 // Helper to prepend an ASN.1 length field. |
| 107 void PrependLength(size_t size, std::list<uint8>* data); |
| 108 |
| 109 // Helper to prepend an ASN.1 type header. |
| 110 void PrependTypeHeaderAndLength(uint8 type, |
| 111 uint32 length, |
| 112 std::list<uint8>* output); |
| 113 |
| 114 // Helper to prepend an ASN.1 bit string |
| 115 void PrependBitString(uint8* val, int num_bytes, std::list<uint8>* output); |
| 116 |
| 117 // Read an ASN.1 length field. This also checks that the length does not |
| 118 // extend beyond |end|. |
| 119 bool ReadLength(uint8** pos, uint8* end, uint32* result); |
| 120 |
| 121 // Read an ASN.1 type header and its length. |
| 122 bool ReadTypeHeaderAndLength(uint8** pos, |
| 123 uint8* end, |
| 124 uint8 expected_tag, |
| 125 uint32* length); |
| 126 |
| 127 // Read an ASN.1 sequence declaration. This consumes the type header and |
| 128 // length field, but not the contents of the sequence. |
| 129 bool ReadSequence(uint8** pos, uint8* end); |
| 130 |
| 131 // Read the RSA AlgorithmIdentifier. |
| 132 bool ReadAlgorithmIdentifier(uint8** pos, uint8* end); |
| 133 |
| 134 // Read one of the two version fields in PrivateKeyInfo. |
| 135 bool ReadVersion(uint8** pos, uint8* end); |
| 136 |
| 137 // The byte-significance of the stored components (modulus, etc..). |
| 138 bool big_endian_; |
| 139 |
| 140 // Component integers of the PrivateKeyInfo |
| 141 std::vector<uint8> modulus_; |
| 142 std::vector<uint8> public_exponent_; |
| 143 std::vector<uint8> private_exponent_; |
| 144 std::vector<uint8> prime1_; |
| 145 std::vector<uint8> prime2_; |
| 146 std::vector<uint8> exponent1_; |
| 147 std::vector<uint8> exponent2_; |
| 148 std::vector<uint8> coefficient_; |
| 149 |
| 150 DISALLOW_COPY_AND_ASSIGN(PrivateKeyInfoCodec); |
| 151 }; |
| 152 |
26 // Encapsulates an RSA private key. Can be used to generate new keys, export | 153 // Encapsulates an RSA private key. Can be used to generate new keys, export |
27 // keys to other formats, or to extract a public key. | 154 // keys to other formats, or to extract a public key. |
28 class RSAPrivateKey { | 155 class RSAPrivateKey { |
29 public: | 156 public: |
30 // Create a new random instance. Can return NULL if initialization fails. | 157 // Create a new random instance. Can return NULL if initialization fails. |
31 static RSAPrivateKey* Create(uint16 num_bits); | 158 static RSAPrivateKey* Create(uint16 num_bits); |
32 | 159 |
33 // Create a new instance by importing an existing private key. The format is | 160 // Create a new instance by importing an existing private key. The format is |
34 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if | 161 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if |
35 // initialization fails. | 162 // initialization fails. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 CSSM_KEY key_; | 198 CSSM_KEY key_; |
72 CSSM_CSP_HANDLE csp_handle_; | 199 CSSM_CSP_HANDLE csp_handle_; |
73 #endif | 200 #endif |
74 | 201 |
75 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | 202 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
76 }; | 203 }; |
77 | 204 |
78 } // namespace base | 205 } // namespace base |
79 | 206 |
80 #endif // BASE_CRYPTO_RSA_PRIVATE_KEY_H_ | 207 #endif // BASE_CRYPTO_RSA_PRIVATE_KEY_H_ |
OLD | NEW |