OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "crypto/rsa_private_key.h" | 5 #include "crypto/rsa_private_key.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 ASSERT_TRUE(keypair3.get()); | 96 ASSERT_TRUE(keypair3.get()); |
97 ASSERT_TRUE(keypair4.get()); | 97 ASSERT_TRUE(keypair4.get()); |
98 | 98 |
99 std::vector<uint8_t> privkey3; | 99 std::vector<uint8_t> privkey3; |
100 std::vector<uint8_t> privkey4; | 100 std::vector<uint8_t> privkey4; |
101 ASSERT_TRUE(keypair3->ExportPrivateKey(&privkey3)); | 101 ASSERT_TRUE(keypair3->ExportPrivateKey(&privkey3)); |
102 ASSERT_TRUE(keypair4->ExportPrivateKey(&privkey4)); | 102 ASSERT_TRUE(keypair4->ExportPrivateKey(&privkey4)); |
103 | 103 |
104 ASSERT_EQ(privkey1.size(), privkey3.size()); | 104 ASSERT_EQ(privkey1.size(), privkey3.size()); |
105 ASSERT_EQ(privkey2.size(), privkey4.size()); | 105 ASSERT_EQ(privkey2.size(), privkey4.size()); |
106 ASSERT_TRUE(0 == memcmp(&privkey1.front(), &privkey3.front(), | 106 ASSERT_EQ(0, memcmp(&privkey1.front(), &privkey3.front(), privkey1.size())); |
107 privkey1.size())); | 107 ASSERT_EQ(0, memcmp(&privkey2.front(), &privkey4.front(), privkey2.size())); |
108 ASSERT_TRUE(0 == memcmp(&privkey2.front(), &privkey4.front(), | |
109 privkey2.size())); | |
110 } | 108 } |
111 | 109 |
112 // Test Copy() method. | 110 // Test Copy() method. |
113 TEST(RSAPrivateKeyUnitTest, CopyTest) { | 111 TEST(RSAPrivateKeyUnitTest, CopyTest) { |
114 std::vector<uint8_t> input(kTestPrivateKeyInfo, | 112 std::vector<uint8_t> input(kTestPrivateKeyInfo, |
115 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); | 113 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); |
116 | 114 |
117 std::unique_ptr<crypto::RSAPrivateKey> key( | 115 std::unique_ptr<crypto::RSAPrivateKey> key( |
118 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); | 116 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); |
119 | 117 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 std::vector<uint8_t> input(kTestPrivateKeyInfo, | 186 std::vector<uint8_t> input(kTestPrivateKeyInfo, |
189 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); | 187 kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); |
190 | 188 |
191 std::unique_ptr<crypto::RSAPrivateKey> key( | 189 std::unique_ptr<crypto::RSAPrivateKey> key( |
192 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); | 190 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); |
193 ASSERT_TRUE(key.get()); | 191 ASSERT_TRUE(key.get()); |
194 | 192 |
195 std::vector<uint8_t> output; | 193 std::vector<uint8_t> output; |
196 ASSERT_TRUE(key->ExportPublicKey(&output)); | 194 ASSERT_TRUE(key->ExportPublicKey(&output)); |
197 | 195 |
198 ASSERT_TRUE( | 196 ASSERT_EQ(0, |
199 memcmp(expected_public_key_info, &output.front(), output.size()) == 0); | 197 memcmp(expected_public_key_info, &output.front(), output.size())); |
200 } | 198 } |
201 | 199 |
202 // These two test keys each contain an integer that has 0x00 for its most | 200 // These two test keys each contain an integer that has 0x00 for its most |
203 // significant byte. When encoded as ASN.1, this byte is dropped and there are | 201 // significant byte. When encoded as ASN.1, this byte is dropped and there are |
204 // two interesting sub-cases. When the sign bit of the integer is set, an extra | 202 // two interesting sub-cases. When the sign bit of the integer is set, an extra |
205 // null byte is added back to force the encoded value to be positive. When the | 203 // null byte is added back to force the encoded value to be positive. When the |
206 // sign bit is not set, the encoded integer is just left shorter than usual. | 204 // sign bit is not set, the encoded integer is just left shorter than usual. |
207 // See also: http://code.google.com/p/chromium/issues/detail?id=14877. | 205 // See also: http://code.google.com/p/chromium/issues/detail?id=14877. |
208 // | 206 // |
209 // Before we were handling this correctly, we would see one of two failures: | 207 // Before we were handling this correctly, we would see one of two failures: |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 ASSERT_TRUE(keypair1.get()); | 340 ASSERT_TRUE(keypair1.get()); |
343 ASSERT_TRUE(keypair2.get()); | 341 ASSERT_TRUE(keypair2.get()); |
344 | 342 |
345 std::vector<uint8_t> output1; | 343 std::vector<uint8_t> output1; |
346 std::vector<uint8_t> output2; | 344 std::vector<uint8_t> output2; |
347 ASSERT_TRUE(keypair1->ExportPrivateKey(&output1)); | 345 ASSERT_TRUE(keypair1->ExportPrivateKey(&output1)); |
348 ASSERT_TRUE(keypair2->ExportPrivateKey(&output2)); | 346 ASSERT_TRUE(keypair2->ExportPrivateKey(&output2)); |
349 | 347 |
350 ASSERT_EQ(input1.size(), output1.size()); | 348 ASSERT_EQ(input1.size(), output1.size()); |
351 ASSERT_EQ(input2.size(), output2.size()); | 349 ASSERT_EQ(input2.size(), output2.size()); |
352 ASSERT_TRUE(0 == memcmp(&output1.front(), &input1.front(), | 350 ASSERT_EQ(0, memcmp(&output1.front(), &input1.front(), input1.size())); |
353 input1.size())); | 351 ASSERT_EQ(0, memcmp(&output2.front(), &input2.front(), input2.size())); |
354 ASSERT_TRUE(0 == memcmp(&output2.front(), &input2.front(), | |
355 input2.size())); | |
356 } | 352 } |
357 | 353 |
358 TEST(RSAPrivateKeyUnitTest, CreateFromKeyTest) { | 354 TEST(RSAPrivateKeyUnitTest, CreateFromKeyTest) { |
359 std::unique_ptr<crypto::RSAPrivateKey> key_pair( | 355 std::unique_ptr<crypto::RSAPrivateKey> key_pair( |
360 crypto::RSAPrivateKey::Create(512)); | 356 crypto::RSAPrivateKey::Create(512)); |
361 ASSERT_TRUE(key_pair.get()); | 357 ASSERT_TRUE(key_pair.get()); |
362 | 358 |
363 std::unique_ptr<crypto::RSAPrivateKey> key_copy( | 359 std::unique_ptr<crypto::RSAPrivateKey> key_copy( |
364 crypto::RSAPrivateKey::CreateFromKey(key_pair->key())); | 360 crypto::RSAPrivateKey::CreateFromKey(key_pair->key())); |
365 ASSERT_TRUE(key_copy.get()); | 361 ASSERT_TRUE(key_copy.get()); |
366 | 362 |
367 std::vector<uint8_t> privkey; | 363 std::vector<uint8_t> privkey; |
368 std::vector<uint8_t> pubkey; | 364 std::vector<uint8_t> pubkey; |
369 ASSERT_TRUE(key_pair->ExportPrivateKey(&privkey)); | 365 ASSERT_TRUE(key_pair->ExportPrivateKey(&privkey)); |
370 ASSERT_TRUE(key_pair->ExportPublicKey(&pubkey)); | 366 ASSERT_TRUE(key_pair->ExportPublicKey(&pubkey)); |
371 | 367 |
372 std::vector<uint8_t> privkey_copy; | 368 std::vector<uint8_t> privkey_copy; |
373 std::vector<uint8_t> pubkey_copy; | 369 std::vector<uint8_t> pubkey_copy; |
374 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy)); | 370 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy)); |
375 ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy)); | 371 ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy)); |
376 | 372 |
377 ASSERT_EQ(privkey, privkey_copy); | 373 ASSERT_EQ(privkey, privkey_copy); |
378 ASSERT_EQ(pubkey, pubkey_copy); | 374 ASSERT_EQ(pubkey, pubkey_copy); |
379 } | 375 } |
380 | 376 |
OLD | NEW |