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 "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 kTestPrivateKeyInfo, kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); | 157 kTestPrivateKeyInfo, kTestPrivateKeyInfo + sizeof(kTestPrivateKeyInfo)); |
158 input.push_back(0); | 158 input.push_back(0); |
159 | 159 |
160 scoped_ptr<crypto::RSAPrivateKey> key( | 160 scoped_ptr<crypto::RSAPrivateKey> key( |
161 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); | 161 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); |
162 | 162 |
163 // Import should fail. | 163 // Import should fail. |
164 EXPECT_FALSE(key); | 164 EXPECT_FALSE(key); |
165 } | 165 } |
166 | 166 |
| 167 TEST(RSAPrivateKeyUnitTest, NotRsaKey) { |
| 168 // Defines a valid P-256 private key. |
| 169 const uint8 kTestEcPrivateKeyInfo[] = { |
| 170 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, |
| 171 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, |
| 172 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02, 0x01, 0x01, 0x04, 0x20, |
| 173 0x1F, 0xE3, 0x39, 0x50, 0xC5, 0xF4, 0x61, 0x12, 0x4A, 0xE9, 0x92, 0xC2, |
| 174 0xBD, 0xFD, 0xF1, 0xC7, 0x3B, 0x16, 0x15, 0xF5, 0x71, 0xBD, 0x56, 0x7E, |
| 175 0x60, 0xD1, 0x9A, 0xA1, 0xF4, 0x8C, 0xDF, 0x42, 0xA1, 0x44, 0x03, 0x42, |
| 176 0x00, 0x04, 0x7C, 0x11, 0x0C, 0x66, 0xDC, 0xFD, 0xA8, 0x07, 0xF6, 0xE6, |
| 177 0x9E, 0x45, 0xDD, 0xB3, 0xC7, 0x4F, 0x69, 0xA1, 0x48, 0x4D, 0x20, 0x3E, |
| 178 0x8D, 0xC5, 0xAD, 0xA8, 0xE9, 0xA9, 0xDD, 0x7C, 0xB3, 0xC7, 0x0D, 0xF4, |
| 179 0x48, 0x98, 0x6E, 0x51, 0xBD, 0xE5, 0xD1, 0x57, 0x6F, 0x99, 0x90, 0x1F, |
| 180 0x9C, 0x2C, 0x6A, 0x80, 0x6A, 0x47, 0xFD, 0x90, 0x76, 0x43, 0xA7, 0x2B, |
| 181 0x83, 0x55, 0x97, 0xEF, 0xC8, 0xC6 |
| 182 }; |
| 183 |
| 184 std::vector<uint8> input( |
| 185 kTestEcPrivateKeyInfo, |
| 186 kTestEcPrivateKeyInfo + sizeof(kTestEcPrivateKeyInfo)); |
| 187 |
| 188 scoped_ptr<crypto::RSAPrivateKey> key( |
| 189 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); |
| 190 |
| 191 // Import should fail as the given PKCS8 bytes were for an EC key not RSA key. |
| 192 EXPECT_FALSE(key); |
| 193 } |
167 | 194 |
168 // Verify that generated public keys look good. This test data was generated | 195 // Verify that generated public keys look good. This test data was generated |
169 // with the openssl command line tool. | 196 // with the openssl command line tool. |
170 TEST(RSAPrivateKeyUnitTest, PublicKeyTest) { | 197 TEST(RSAPrivateKeyUnitTest, PublicKeyTest) { |
171 const uint8 expected_public_key_info[] = { | 198 const uint8 expected_public_key_info[] = { |
172 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, | 199 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, |
173 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, | 200 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, |
174 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, | 201 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, |
175 0x89, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, | 202 0x89, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, |
176 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61, | 203 0x20, 0xdc, 0x7c, 0x9b, 0x0c, 0xdc, 0x51, 0x61, |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 std::vector<uint8> privkey_copy; | 464 std::vector<uint8> privkey_copy; |
438 std::vector<uint8> pubkey_copy; | 465 std::vector<uint8> pubkey_copy; |
439 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy)); | 466 ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy)); |
440 ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy)); | 467 ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy)); |
441 | 468 |
442 ASSERT_EQ(privkey, privkey_copy); | 469 ASSERT_EQ(privkey, privkey_copy); |
443 ASSERT_EQ(pubkey, pubkey_copy); | 470 ASSERT_EQ(pubkey, pubkey_copy); |
444 } | 471 } |
445 #endif | 472 #endif |
446 | 473 |
OLD | NEW |