| 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/ec_private_key.h" | 5 #include "crypto/ec_private_key.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 // Generate random private keys. Export, then re-import in several ways. We | 40 // Generate random private keys. Export, then re-import in several ways. We |
| 41 // should get back the same exact public key, and the private key should have | 41 // should get back the same exact public key, and the private key should have |
| 42 // the same value and elliptic curve params. | 42 // the same value and elliptic curve params. |
| 43 TEST(ECPrivateKeyUnitTest, InitRandomTest) { | 43 TEST(ECPrivateKeyUnitTest, InitRandomTest) { |
| 44 static const char kPassword1[] = ""; | 44 static const char kPassword1[] = ""; |
| 45 static const char kPassword2[] = "test"; | 45 static const char kPassword2[] = "test"; |
| 46 | 46 |
| 47 std::unique_ptr<crypto::ECPrivateKey> keypair(crypto::ECPrivateKey::Create()); | 47 std::unique_ptr<crypto::ECPrivateKey> keypair(crypto::ECPrivateKey::Create()); |
| 48 ASSERT_TRUE(keypair.get()); | 48 ASSERT_TRUE(keypair); |
| 49 | 49 |
| 50 // Re-import as a PrivateKeyInfo. | 50 // Re-import as a PrivateKeyInfo. |
| 51 std::vector<uint8_t> privkey; | 51 std::vector<uint8_t> privkey; |
| 52 EXPECT_TRUE(keypair->ExportPrivateKey(&privkey)); | 52 EXPECT_TRUE(keypair->ExportPrivateKey(&privkey)); |
| 53 std::unique_ptr<crypto::ECPrivateKey> keypair_copy = | 53 std::unique_ptr<crypto::ECPrivateKey> keypair_copy = |
| 54 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(privkey); | 54 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(privkey); |
| 55 ASSERT_TRUE(keypair_copy); | 55 ASSERT_TRUE(keypair_copy); |
| 56 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | 56 ExpectKeysEqual(keypair.get(), keypair_copy.get()); |
| 57 | 57 |
| 58 // Re-import as an EncryptedPrivateKeyInfo with kPassword1. | 58 // Re-import as an EncryptedPrivateKeyInfo with kPassword1. |
| 59 std::vector<uint8_t> encrypted_privkey; | 59 std::vector<uint8_t> encrypted_privkey; |
| 60 std::vector<uint8_t> pubkey; | 60 std::vector<uint8_t> pubkey; |
| 61 EXPECT_TRUE( | 61 EXPECT_TRUE( |
| 62 keypair->ExportEncryptedPrivateKey(kPassword1, 1, &encrypted_privkey)); | 62 keypair->ExportEncryptedPrivateKey(kPassword1, 1, &encrypted_privkey)); |
| 63 EXPECT_TRUE(keypair->ExportPublicKey(&pubkey)); | 63 EXPECT_TRUE(keypair->ExportPublicKey(&pubkey)); |
| 64 keypair_copy.reset(crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 64 keypair_copy = crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 65 kPassword1, encrypted_privkey, pubkey)); | 65 kPassword1, encrypted_privkey, pubkey); |
| 66 ASSERT_TRUE(keypair_copy); | 66 ASSERT_TRUE(keypair_copy); |
| 67 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | 67 ExpectKeysEqual(keypair.get(), keypair_copy.get()); |
| 68 | 68 |
| 69 // Re-import as an EncryptedPrivateKeyInfo with kPassword2. | 69 // Re-import as an EncryptedPrivateKeyInfo with kPassword2. |
| 70 EXPECT_TRUE( | 70 EXPECT_TRUE( |
| 71 keypair->ExportEncryptedPrivateKey(kPassword2, 1, &encrypted_privkey)); | 71 keypair->ExportEncryptedPrivateKey(kPassword2, 1, &encrypted_privkey)); |
| 72 keypair_copy.reset(crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 72 keypair_copy = crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 73 kPassword2, encrypted_privkey, pubkey)); | 73 kPassword2, encrypted_privkey, pubkey); |
| 74 ASSERT_TRUE(keypair_copy); | 74 ASSERT_TRUE(keypair_copy); |
| 75 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | 75 ExpectKeysEqual(keypair.get(), keypair_copy.get()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 TEST(ECPrivateKeyUnitTest, Copy) { | 78 TEST(ECPrivateKeyUnitTest, Copy) { |
| 79 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 79 std::unique_ptr<crypto::ECPrivateKey> keypair1( |
| 80 crypto::ECPrivateKey::Create()); | 80 crypto::ECPrivateKey::Create()); |
| 81 std::unique_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); | 81 std::unique_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); |
| 82 ASSERT_TRUE(keypair1.get()); | 82 ASSERT_TRUE(keypair1); |
| 83 ASSERT_TRUE(keypair2.get()); | 83 ASSERT_TRUE(keypair2); |
| 84 | 84 |
| 85 ExpectKeysEqual(keypair1.get(), keypair2.get()); | 85 ExpectKeysEqual(keypair1.get(), keypair2.get()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 TEST(ECPrivateKeyUnitTest, CreateFromPrivateKeyInfo) { | 88 TEST(ECPrivateKeyUnitTest, CreateFromPrivateKeyInfo) { |
| 89 static const uint8_t kPrivateKeyInfo[] = { | 89 static const uint8_t kPrivateKeyInfo[] = { |
| 90 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | 90 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
| 91 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, | 91 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, |
| 92 0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, | 92 0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, |
| 93 0x07, 0x0f, 0x08, 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, | 93 0x07, 0x0f, 0x08, 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 std::begin(kPrivateKeyInfo), std::end(kPrivateKeyInfo))); | 199 std::begin(kPrivateKeyInfo), std::end(kPrivateKeyInfo))); |
| 200 EXPECT_FALSE(key); | 200 EXPECT_FALSE(key); |
| 201 } | 201 } |
| 202 | 202 |
| 203 TEST(ECPrivateKeyUnitTest, BadPasswordTest) { | 203 TEST(ECPrivateKeyUnitTest, BadPasswordTest) { |
| 204 const std::string password1; | 204 const std::string password1; |
| 205 const std::string password2 = "test"; | 205 const std::string password2 = "test"; |
| 206 | 206 |
| 207 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 207 std::unique_ptr<crypto::ECPrivateKey> keypair1( |
| 208 crypto::ECPrivateKey::Create()); | 208 crypto::ECPrivateKey::Create()); |
| 209 ASSERT_TRUE(keypair1.get()); | 209 ASSERT_TRUE(keypair1); |
| 210 | 210 |
| 211 std::vector<uint8_t> privkey1; | 211 std::vector<uint8_t> privkey1; |
| 212 std::vector<uint8_t> pubkey1; | 212 std::vector<uint8_t> pubkey1; |
| 213 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey( | 213 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey( |
| 214 password1, 1, &privkey1)); | 214 password1, 1, &privkey1)); |
| 215 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | 215 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1)); |
| 216 | 216 |
| 217 std::unique_ptr<crypto::ECPrivateKey> keypair2( | 217 std::unique_ptr<crypto::ECPrivateKey> keypair2( |
| 218 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 218 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 219 password2, privkey1, pubkey1)); | 219 password2, privkey1, pubkey1)); |
| 220 ASSERT_FALSE(keypair2.get()); | 220 ASSERT_FALSE(keypair2); |
| 221 } | 221 } |
| 222 | 222 |
| 223 TEST(ECPrivateKeyUnitTest, LoadNSSKeyTest) { | 223 TEST(ECPrivateKeyUnitTest, LoadNSSKeyTest) { |
| 224 static const uint8_t kNSSKey[] = { | 224 static const uint8_t kNSSKey[] = { |
| 225 0x30, 0x81, 0xb8, 0x30, 0x23, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, | 225 0x30, 0x81, 0xb8, 0x30, 0x23, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 226 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x15, 0x04, 0x10, 0x3f, 0xac, 0xe9, | 226 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x15, 0x04, 0x10, 0x3f, 0xac, 0xe9, |
| 227 0x38, 0xdb, 0x40, 0x6b, 0x26, 0x89, 0x09, 0x73, 0x18, 0x8d, 0x7f, 0x1c, | 227 0x38, 0xdb, 0x40, 0x6b, 0x26, 0x89, 0x09, 0x73, 0x18, 0x8d, 0x7f, 0x1c, |
| 228 0x82, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0x5e, 0x5e, 0x11, 0xef, 0xbb, | 228 0x82, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0x5e, 0x5e, 0x11, 0xef, 0xbb, |
| 229 0x7c, 0x4d, 0xec, 0xc0, 0xdc, 0xc7, 0x23, 0xd2, 0xc4, 0x77, 0xbc, 0xf4, | 229 0x7c, 0x4d, 0xec, 0xc0, 0xdc, 0xc7, 0x23, 0xd2, 0xc4, 0x77, 0xbc, 0xf4, |
| 230 0x5d, 0x59, 0x4c, 0x07, 0xc2, 0x8a, 0x26, 0xfa, 0x25, 0x1c, 0xaa, 0x42, | 230 0x5d, 0x59, 0x4c, 0x07, 0xc2, 0x8a, 0x26, 0xfa, 0x25, 0x1c, 0xaa, 0x42, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 249 0x53, 0x78, 0xdd, 0xd6, 0x12, 0x3a, 0x5e, 0xeb, 0xbf, 0xdf, 0x16, 0xd3, | 249 0x53, 0x78, 0xdd, 0xd6, 0x12, 0x3a, 0x5e, 0xeb, 0xbf, 0xdf, 0x16, 0xd3, |
| 250 0x2c, 0x3b, 0xe8, 0xdb, 0x19, 0xfc, 0x5e, | 250 0x2c, 0x3b, 0xe8, 0xdb, 0x19, 0xfc, 0x5e, |
| 251 }; | 251 }; |
| 252 | 252 |
| 253 std::unique_ptr<crypto::ECPrivateKey> keypair_nss( | 253 std::unique_ptr<crypto::ECPrivateKey> keypair_nss( |
| 254 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 254 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 255 "", std::vector<uint8_t>(std::begin(kNSSKey), std::end(kNSSKey)), | 255 "", std::vector<uint8_t>(std::begin(kNSSKey), std::end(kNSSKey)), |
| 256 std::vector<uint8_t>(std::begin(kNSSPublicKey), | 256 std::vector<uint8_t>(std::begin(kNSSPublicKey), |
| 257 std::end(kNSSPublicKey)))); | 257 std::end(kNSSPublicKey)))); |
| 258 | 258 |
| 259 EXPECT_TRUE(keypair_nss.get()); | 259 EXPECT_TRUE(keypair_nss); |
| 260 } | 260 } |
| 261 | 261 |
| 262 TEST(ECPrivateKeyUnitTest, LoadOpenSSLKeyTest) { | 262 TEST(ECPrivateKeyUnitTest, LoadOpenSSLKeyTest) { |
| 263 static const uint8_t kOpenSSLKey[] = { | 263 static const uint8_t kOpenSSLKey[] = { |
| 264 0x30, 0x81, 0xb0, 0x30, 0x1b, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, | 264 0x30, 0x81, 0xb0, 0x30, 0x1b, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 265 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0d, 0x04, 0x08, 0xb2, 0xfe, 0x68, | 265 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0d, 0x04, 0x08, 0xb2, 0xfe, 0x68, |
| 266 0xc2, 0xea, 0x0f, 0x10, 0x9c, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0xe2, | 266 0xc2, 0xea, 0x0f, 0x10, 0x9c, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0xe2, |
| 267 0xf6, 0x1c, 0xca, 0xad, 0x64, 0x30, 0xbf, 0x88, 0x04, 0x35, 0xe5, 0x0f, | 267 0xf6, 0x1c, 0xca, 0xad, 0x64, 0x30, 0xbf, 0x88, 0x04, 0x35, 0xe5, 0x0f, |
| 268 0x11, 0x49, 0x06, 0x01, 0x14, 0x33, 0x80, 0xa2, 0x78, 0x44, 0x5b, 0xaa, | 268 0x11, 0x49, 0x06, 0x01, 0x14, 0x33, 0x80, 0xa2, 0x78, 0x44, 0x5b, 0xaa, |
| 269 0x0d, 0xd7, 0x00, 0x36, 0x9d, 0x91, 0x97, 0x37, 0x20, 0x7b, 0x27, 0xc1, | 269 0x0d, 0xd7, 0x00, 0x36, 0x9d, 0x91, 0x97, 0x37, 0x20, 0x7b, 0x27, 0xc1, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 296 0xaa, 0x44, 0xff, 0xab, 0x4d, 0xb5, 0x7e, 0x25, 0x3d, | 296 0xaa, 0x44, 0xff, 0xab, 0x4d, 0xb5, 0x7e, 0x25, 0x3d, |
| 297 }; | 297 }; |
| 298 | 298 |
| 299 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( | 299 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( |
| 300 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 300 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 301 "", | 301 "", |
| 302 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), | 302 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), |
| 303 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 303 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), |
| 304 std::end(kOpenSSLPublicKey)))); | 304 std::end(kOpenSSLPublicKey)))); |
| 305 | 305 |
| 306 EXPECT_TRUE(keypair_openssl.get()); | 306 EXPECT_TRUE(keypair_openssl); |
| 307 | 307 |
| 308 std::vector<uint8_t> public_key; | 308 std::vector<uint8_t> public_key; |
| 309 EXPECT_TRUE(keypair_openssl->ExportPublicKey(&public_key)); | 309 EXPECT_TRUE(keypair_openssl->ExportPublicKey(&public_key)); |
| 310 EXPECT_EQ(std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 310 EXPECT_EQ(std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), |
| 311 std::end(kOpenSSLPublicKey)), | 311 std::end(kOpenSSLPublicKey)), |
| 312 public_key); | 312 public_key); |
| 313 | 313 |
| 314 std::string raw_public_key; | 314 std::string raw_public_key; |
| 315 EXPECT_TRUE(keypair_openssl->ExportRawPublicKey(&raw_public_key)); | 315 EXPECT_TRUE(keypair_openssl->ExportRawPublicKey(&raw_public_key)); |
| 316 EXPECT_EQ(std::string(reinterpret_cast<const char*>(kOpenSSLRawPublicKey), | 316 EXPECT_EQ(std::string(reinterpret_cast<const char*>(kOpenSSLRawPublicKey), |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 0x41, 0x3b, 0x0d, 0x10, 0xa7, 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec, | 391 0x41, 0x3b, 0x0d, 0x10, 0xa7, 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec, |
| 392 }; | 392 }; |
| 393 | 393 |
| 394 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( | 394 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( |
| 395 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 395 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 396 "", | 396 "", |
| 397 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), | 397 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), |
| 398 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 398 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), |
| 399 std::end(kOpenSSLPublicKey)))); | 399 std::end(kOpenSSLPublicKey)))); |
| 400 | 400 |
| 401 EXPECT_TRUE(keypair_openssl.get()); | 401 EXPECT_TRUE(keypair_openssl); |
| 402 } | 402 } |
| OLD | NEW |