Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(440)

Side by Side Diff: crypto/rsa_private_key.h

Issue 1539353003: Switch to standard integer types in crypto/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/random_unittest.cc ('k') | crypto/rsa_private_key.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h>
9 #include <stdint.h>
10
8 #include <list> 11 #include <list>
9 #include <vector> 12 #include <vector>
10 13
11 #include "base/basictypes.h" 14 #include "base/macros.h"
12 #include "build/build_config.h" 15 #include "build/build_config.h"
13 #include "crypto/crypto_export.h" 16 #include "crypto/crypto_export.h"
14 17
15 #if defined(USE_OPENSSL) 18 #if defined(USE_OPENSSL)
16 // Forward declaration for openssl/*.h 19 // Forward declaration for openssl/*.h
17 typedef struct evp_pkey_st EVP_PKEY; 20 typedef struct evp_pkey_st EVP_PKEY;
18 #else 21 #else
19 // Forward declaration. 22 // Forward declaration.
20 typedef struct PK11SlotInfoStr PK11SlotInfo; 23 typedef struct PK11SlotInfoStr PK11SlotInfo;
21 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; 24 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey;
22 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; 25 typedef struct SECKEYPublicKeyStr SECKEYPublicKey;
23 #endif 26 #endif
24 27
25 28
26 namespace crypto { 29 namespace crypto {
27 30
28 // Used internally by RSAPrivateKey for serializing and deserializing 31 // Used internally by RSAPrivateKey for serializing and deserializing
29 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. 32 // PKCS #8 PrivateKeyInfo and PublicKeyInfo.
30 class PrivateKeyInfoCodec { 33 class PrivateKeyInfoCodec {
31 public: 34 public:
32 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. 35 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8.
33 static const uint8 kRsaAlgorithmIdentifier[]; 36 static const uint8_t kRsaAlgorithmIdentifier[];
34 37
35 // ASN.1 tags for some types we use. 38 // ASN.1 tags for some types we use.
36 static const uint8 kBitStringTag = 0x03; 39 static const uint8_t kBitStringTag = 0x03;
37 static const uint8 kIntegerTag = 0x02; 40 static const uint8_t kIntegerTag = 0x02;
38 static const uint8 kNullTag = 0x05; 41 static const uint8_t kNullTag = 0x05;
39 static const uint8 kOctetStringTag = 0x04; 42 static const uint8_t kOctetStringTag = 0x04;
40 static const uint8 kSequenceTag = 0x30; 43 static const uint8_t kSequenceTag = 0x30;
41 44
42 // |big_endian| here specifies the byte-significance of the integer components 45 // |big_endian| here specifies the byte-significance of the integer components
43 // that will be parsed & serialized (modulus(), etc...) during Import(), 46 // that will be parsed & serialized (modulus(), etc...) during Import(),
44 // Export() and ExportPublicKeyInfo() -- not the ASN.1 DER encoding of the 47 // Export() and ExportPublicKeyInfo() -- not the ASN.1 DER encoding of the
45 // PrivateKeyInfo/PublicKeyInfo (which is always big-endian). 48 // PrivateKeyInfo/PublicKeyInfo (which is always big-endian).
46 explicit PrivateKeyInfoCodec(bool big_endian); 49 explicit PrivateKeyInfoCodec(bool big_endian);
47 50
48 ~PrivateKeyInfoCodec(); 51 ~PrivateKeyInfoCodec();
49 52
50 // Exports the contents of the integer components to the ASN.1 DER encoding 53 // Exports the contents of the integer components to the ASN.1 DER encoding
51 // of the PrivateKeyInfo structure to |output|. 54 // of the PrivateKeyInfo structure to |output|.
52 bool Export(std::vector<uint8>* output); 55 bool Export(std::vector<uint8_t>* output);
53 56
54 // Exports the contents of the integer components to the ASN.1 DER encoding 57 // Exports the contents of the integer components to the ASN.1 DER encoding
55 // of the PublicKeyInfo structure to |output|. 58 // of the PublicKeyInfo structure to |output|.
56 bool ExportPublicKeyInfo(std::vector<uint8>* output); 59 bool ExportPublicKeyInfo(std::vector<uint8_t>* output);
57 60
58 // Exports the contents of the integer components to the ASN.1 DER encoding 61 // Exports the contents of the integer components to the ASN.1 DER encoding
59 // of the RSAPublicKey structure to |output|. 62 // of the RSAPublicKey structure to |output|.
60 bool ExportPublicKey(std::vector<uint8>* output); 63 bool ExportPublicKey(std::vector<uint8_t>* output);
61 64
62 // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input| 65 // Parses the ASN.1 DER encoding of the PrivateKeyInfo structure in |input|
63 // and populates the integer components with |big_endian_| byte-significance. 66 // and populates the integer components with |big_endian_| byte-significance.
64 // IMPORTANT NOTE: This is currently *not* security-approved for importing 67 // IMPORTANT NOTE: This is currently *not* security-approved for importing
65 // keys from unstrusted sources. 68 // keys from unstrusted sources.
66 bool Import(const std::vector<uint8>& input); 69 bool Import(const std::vector<uint8_t>& input);
67 70
68 // Accessors to the contents of the integer components of the PrivateKeyInfo 71 // Accessors to the contents of the integer components of the PrivateKeyInfo
69 // structure. 72 // structure.
70 std::vector<uint8>* modulus() { return &modulus_; } 73 std::vector<uint8_t>* modulus() { return &modulus_; }
71 std::vector<uint8>* public_exponent() { return &public_exponent_; } 74 std::vector<uint8_t>* public_exponent() { return &public_exponent_; }
72 std::vector<uint8>* private_exponent() { return &private_exponent_; } 75 std::vector<uint8_t>* private_exponent() { return &private_exponent_; }
73 std::vector<uint8>* prime1() { return &prime1_; } 76 std::vector<uint8_t>* prime1() { return &prime1_; }
74 std::vector<uint8>* prime2() { return &prime2_; } 77 std::vector<uint8_t>* prime2() { return &prime2_; }
75 std::vector<uint8>* exponent1() { return &exponent1_; } 78 std::vector<uint8_t>* exponent1() { return &exponent1_; }
76 std::vector<uint8>* exponent2() { return &exponent2_; } 79 std::vector<uint8_t>* exponent2() { return &exponent2_; }
77 std::vector<uint8>* coefficient() { return &coefficient_; } 80 std::vector<uint8_t>* coefficient() { return &coefficient_; }
78 81
79 private: 82 private:
80 // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_| 83 // Utility wrappers for PrependIntegerImpl that use the class's |big_endian_|
81 // value. 84 // value.
82 void PrependInteger(const std::vector<uint8>& in, std::list<uint8>* out); 85 void PrependInteger(const std::vector<uint8_t>& in, std::list<uint8_t>* out);
83 void PrependInteger(uint8* val, int num_bytes, std::list<uint8>* data); 86 void PrependInteger(uint8_t* val, int num_bytes, std::list<uint8_t>* data);
84 87
85 // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian| 88 // Prepends the integer stored in |val| - |val + num_bytes| with |big_endian|
86 // byte-significance into |data| as an ASN.1 integer. 89 // byte-significance into |data| as an ASN.1 integer.
87 void PrependIntegerImpl(uint8* val, 90 void PrependIntegerImpl(uint8_t* val,
88 int num_bytes, 91 int num_bytes,
89 std::list<uint8>* data, 92 std::list<uint8_t>* data,
90 bool big_endian); 93 bool big_endian);
91 94
92 // Utility wrappers for ReadIntegerImpl that use the class's |big_endian_| 95 // Utility wrappers for ReadIntegerImpl that use the class's |big_endian_|
93 // value. 96 // value.
94 bool ReadInteger(uint8** pos, uint8* end, std::vector<uint8>* out); 97 bool ReadInteger(uint8_t** pos, uint8_t* end, std::vector<uint8_t>* out);
95 bool ReadIntegerWithExpectedSize(uint8** pos, 98 bool ReadIntegerWithExpectedSize(uint8_t** pos,
96 uint8* end, 99 uint8_t* end,
97 size_t expected_size, 100 size_t expected_size,
98 std::vector<uint8>* out); 101 std::vector<uint8_t>* out);
99 102
100 // Reads an ASN.1 integer from |pos|, and stores the result into |out| with 103 // Reads an ASN.1 integer from |pos|, and stores the result into |out| with
101 // |big_endian| byte-significance. 104 // |big_endian| byte-significance.
102 bool ReadIntegerImpl(uint8** pos, 105 bool ReadIntegerImpl(uint8_t** pos,
103 uint8* end, 106 uint8_t* end,
104 std::vector<uint8>* out, 107 std::vector<uint8_t>* out,
105 bool big_endian); 108 bool big_endian);
106 109
107 // Prepends the integer stored in |val|, starting a index |start|, for 110 // Prepends the integer stored in |val|, starting a index |start|, for
108 // |num_bytes| bytes onto |data|. 111 // |num_bytes| bytes onto |data|.
109 void PrependBytes(uint8* val, 112 void PrependBytes(uint8_t* val,
110 int start, 113 int start,
111 int num_bytes, 114 int num_bytes,
112 std::list<uint8>* data); 115 std::list<uint8_t>* data);
113 116
114 // Helper to prepend an ASN.1 length field. 117 // Helper to prepend an ASN.1 length field.
115 void PrependLength(size_t size, std::list<uint8>* data); 118 void PrependLength(size_t size, std::list<uint8_t>* data);
116 119
117 // Helper to prepend an ASN.1 type header. 120 // Helper to prepend an ASN.1 type header.
118 void PrependTypeHeaderAndLength(uint8 type, 121 void PrependTypeHeaderAndLength(uint8_t type,
119 uint32 length, 122 uint32_t length,
120 std::list<uint8>* output); 123 std::list<uint8_t>* output);
121 124
122 // Helper to prepend an ASN.1 bit string 125 // Helper to prepend an ASN.1 bit string
123 void PrependBitString(uint8* val, int num_bytes, std::list<uint8>* output); 126 void PrependBitString(uint8_t* val,
127 int num_bytes,
128 std::list<uint8_t>* output);
124 129
125 // Read an ASN.1 length field. This also checks that the length does not 130 // Read an ASN.1 length field. This also checks that the length does not
126 // extend beyond |end|. 131 // extend beyond |end|.
127 bool ReadLength(uint8** pos, uint8* end, uint32* result); 132 bool ReadLength(uint8_t** pos, uint8_t* end, uint32_t* result);
128 133
129 // Read an ASN.1 type header and its length. 134 // Read an ASN.1 type header and its length.
130 bool ReadTypeHeaderAndLength(uint8** pos, 135 bool ReadTypeHeaderAndLength(uint8_t** pos,
131 uint8* end, 136 uint8_t* end,
132 uint8 expected_tag, 137 uint8_t expected_tag,
133 uint32* length); 138 uint32_t* length);
134 139
135 // Read an ASN.1 sequence declaration. This consumes the type header and 140 // Read an ASN.1 sequence declaration. This consumes the type header and
136 // length field, but not the contents of the sequence. 141 // length field, but not the contents of the sequence.
137 bool ReadSequence(uint8** pos, uint8* end); 142 bool ReadSequence(uint8_t** pos, uint8_t* end);
138 143
139 // Read the RSA AlgorithmIdentifier. 144 // Read the RSA AlgorithmIdentifier.
140 bool ReadAlgorithmIdentifier(uint8** pos, uint8* end); 145 bool ReadAlgorithmIdentifier(uint8_t** pos, uint8_t* end);
141 146
142 // Read one of the two version fields in PrivateKeyInfo. 147 // Read one of the two version fields in PrivateKeyInfo.
143 bool ReadVersion(uint8** pos, uint8* end); 148 bool ReadVersion(uint8_t** pos, uint8_t* end);
144 149
145 // The byte-significance of the stored components (modulus, etc..). 150 // The byte-significance of the stored components (modulus, etc..).
146 bool big_endian_; 151 bool big_endian_;
147 152
148 // Component integers of the PrivateKeyInfo 153 // Component integers of the PrivateKeyInfo
149 std::vector<uint8> modulus_; 154 std::vector<uint8_t> modulus_;
150 std::vector<uint8> public_exponent_; 155 std::vector<uint8_t> public_exponent_;
151 std::vector<uint8> private_exponent_; 156 std::vector<uint8_t> private_exponent_;
152 std::vector<uint8> prime1_; 157 std::vector<uint8_t> prime1_;
153 std::vector<uint8> prime2_; 158 std::vector<uint8_t> prime2_;
154 std::vector<uint8> exponent1_; 159 std::vector<uint8_t> exponent1_;
155 std::vector<uint8> exponent2_; 160 std::vector<uint8_t> exponent2_;
156 std::vector<uint8> coefficient_; 161 std::vector<uint8_t> coefficient_;
157 162
158 DISALLOW_COPY_AND_ASSIGN(PrivateKeyInfoCodec); 163 DISALLOW_COPY_AND_ASSIGN(PrivateKeyInfoCodec);
159 }; 164 };
160 165
161 // Encapsulates an RSA private key. Can be used to generate new keys, export 166 // Encapsulates an RSA private key. Can be used to generate new keys, export
162 // keys to other formats, or to extract a public key. 167 // keys to other formats, or to extract a public key.
163 // TODO(hclam): This class should be ref-counted so it can be reused easily. 168 // TODO(hclam): This class should be ref-counted so it can be reused easily.
164 class CRYPTO_EXPORT RSAPrivateKey { 169 class CRYPTO_EXPORT RSAPrivateKey {
165 public: 170 public:
166 ~RSAPrivateKey(); 171 ~RSAPrivateKey();
167 172
168 // Create a new random instance. Can return NULL if initialization fails. 173 // Create a new random instance. Can return NULL if initialization fails.
169 static RSAPrivateKey* Create(uint16 num_bits); 174 static RSAPrivateKey* Create(uint16_t num_bits);
170 175
171 // Create a new instance by importing an existing private key. The format is 176 // Create a new instance by importing an existing private key. The format is
172 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if 177 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if
173 // initialization fails. 178 // initialization fails.
174 static RSAPrivateKey* CreateFromPrivateKeyInfo( 179 static RSAPrivateKey* CreateFromPrivateKeyInfo(
175 const std::vector<uint8>& input); 180 const std::vector<uint8_t>& input);
176 181
177 #if defined(USE_OPENSSL) 182 #if defined(USE_OPENSSL)
178 // Create a new instance from an existing EVP_PKEY, taking a 183 // Create a new instance from an existing EVP_PKEY, taking a
179 // reference to it. |key| must be an RSA key. Returns NULL on 184 // reference to it. |key| must be an RSA key. Returns NULL on
180 // failure. 185 // failure.
181 static RSAPrivateKey* CreateFromKey(EVP_PKEY* key); 186 static RSAPrivateKey* CreateFromKey(EVP_PKEY* key);
182 #else 187 #else
183 // Create a new instance by referencing an existing private key 188 // Create a new instance by referencing an existing private key
184 // structure. Does not import the key. 189 // structure. Does not import the key.
185 static RSAPrivateKey* CreateFromKey(SECKEYPrivateKey* key); 190 static RSAPrivateKey* CreateFromKey(SECKEYPrivateKey* key);
186 #endif 191 #endif
187 192
188 #if defined(USE_OPENSSL) 193 #if defined(USE_OPENSSL)
189 EVP_PKEY* key() { return key_; } 194 EVP_PKEY* key() { return key_; }
190 #else 195 #else
191 SECKEYPrivateKey* key() { return key_; } 196 SECKEYPrivateKey* key() { return key_; }
192 SECKEYPublicKey* public_key() { return public_key_; } 197 SECKEYPublicKey* public_key() { return public_key_; }
193 #endif 198 #endif
194 199
195 // Creates a copy of the object. 200 // Creates a copy of the object.
196 RSAPrivateKey* Copy() const; 201 RSAPrivateKey* Copy() const;
197 202
198 // Exports the private key to a PKCS #1 PrivateKey block. 203 // Exports the private key to a PKCS #1 PrivateKey block.
199 bool ExportPrivateKey(std::vector<uint8>* output) const; 204 bool ExportPrivateKey(std::vector<uint8_t>* output) const;
200 205
201 // Exports the public key to an X509 SubjectPublicKeyInfo block. 206 // Exports the public key to an X509 SubjectPublicKeyInfo block.
202 bool ExportPublicKey(std::vector<uint8>* output) const; 207 bool ExportPublicKey(std::vector<uint8_t>* output) const;
203 208
204 private: 209 private:
205 // Constructor is private. Use one of the Create*() methods above instead. 210 // Constructor is private. Use one of the Create*() methods above instead.
206 RSAPrivateKey(); 211 RSAPrivateKey();
207 212
208 #if defined(USE_OPENSSL) 213 #if defined(USE_OPENSSL)
209 EVP_PKEY* key_; 214 EVP_PKEY* key_;
210 #else 215 #else
211 SECKEYPrivateKey* key_; 216 SECKEYPrivateKey* key_;
212 SECKEYPublicKey* public_key_; 217 SECKEYPublicKey* public_key_;
213 #endif 218 #endif
214 219
215 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); 220 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey);
216 }; 221 };
217 222
218 } // namespace crypto 223 } // namespace crypto
219 224
220 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ 225 #endif // CRYPTO_RSA_PRIVATE_KEY_H_
OLDNEW
« no previous file with comments | « crypto/random_unittest.cc ('k') | crypto/rsa_private_key.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698