 Chromium Code Reviews
 Chromium Code Reviews Issue 1935053003:
  Add PKCS#8 ECPrivateKey export/import functions.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1935053003:
  Add PKCS#8 ECPrivateKey export/import functions.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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> | 
| 11 | 11 | 
| 12 #include "base/macros.h" | 12 #include "base/macros.h" | 
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" | 
| 14 | 14 | 
| 15 // Generate random private keys. Export, then re-import. We should get | 15 namespace { | 
| 16 // back the same exact public key, and the private key should have the same | |
| 17 // value and elliptic curve params. | |
| 18 TEST(ECPrivateKeyUnitTest, InitRandomTest) { | |
| 19 const std::string password1; | |
| 20 const std::string password2 = "test"; | |
| 21 | 16 | 
| 22 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 17 void ExpectKeysEqual(const crypto::ECPrivateKey* keypair1, | 
| 23 crypto::ECPrivateKey::Create()); | 18 const crypto::ECPrivateKey* keypair2) { | 
| 24 std::unique_ptr<crypto::ECPrivateKey> keypair2( | |
| 25 crypto::ECPrivateKey::Create()); | |
| 26 ASSERT_TRUE(keypair1.get()); | |
| 27 ASSERT_TRUE(keypair2.get()); | |
| 28 | |
| 29 std::vector<uint8_t> key1value; | |
| 30 std::vector<uint8_t> key2value; | |
| 31 EXPECT_TRUE(keypair1->ExportValueForTesting(&key1value)); | |
| 32 EXPECT_TRUE(keypair2->ExportValueForTesting(&key2value)); | |
| 33 | |
| 34 std::vector<uint8_t> privkey1; | 19 std::vector<uint8_t> privkey1; | 
| 35 std::vector<uint8_t> privkey2; | 20 std::vector<uint8_t> privkey2; | 
| 21 EXPECT_TRUE(keypair1->ExportPrivateKey(&privkey1)); | |
| 22 EXPECT_TRUE(keypair2->ExportPrivateKey(&privkey2)); | |
| 23 EXPECT_EQ(privkey1, privkey2); | |
| 24 | |
| 36 std::vector<uint8_t> pubkey1; | 25 std::vector<uint8_t> pubkey1; | 
| 37 std::vector<uint8_t> pubkey2; | 26 std::vector<uint8_t> pubkey2; | 
| 27 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | |
| 28 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); | |
| 29 EXPECT_EQ(pubkey1, pubkey2); | |
| 30 | |
| 38 std::string raw_pubkey1; | 31 std::string raw_pubkey1; | 
| 39 std::string raw_pubkey2; | 32 std::string raw_pubkey2; | 
| 40 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(password1, 1, &privkey1)); | |
| 41 ASSERT_TRUE(keypair2->ExportEncryptedPrivateKey(password2, 1, &privkey2)); | |
| 42 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | |
| 43 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); | |
| 44 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1)); | 33 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1)); | 
| 45 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); | 34 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); | 
| 35 EXPECT_EQ(raw_pubkey1, raw_pubkey2); | |
| 36 } | |
| 46 | 37 | 
| 47 std::unique_ptr<crypto::ECPrivateKey> keypair3( | 38 } // namespace | 
| 48 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
| 49 password1, privkey1, pubkey1)); | |
| 50 std::unique_ptr<crypto::ECPrivateKey> keypair4( | |
| 51 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
| 52 password2, privkey2, pubkey2)); | |
| 53 ASSERT_TRUE(keypair3.get()); | |
| 54 ASSERT_TRUE(keypair4.get()); | |
| 55 | 39 | 
| 56 std::vector<uint8_t> key3value; | 40 // Generate random private keys. Export, then re-import in several ways. We | 
| 57 std::vector<uint8_t> key4value; | 41 // should get back the same exact public key, and the private key should have | 
| 58 EXPECT_TRUE(keypair3->ExportValueForTesting(&key3value)); | 42 // the same value and elliptic curve params. | 
| 59 EXPECT_TRUE(keypair4->ExportValueForTesting(&key4value)); | 43 TEST(ECPrivateKeyUnitTest, InitRandomTest) { | 
| 
davidben
2016/05/02 23:18:53
(I more-or-less rewrote this test. The old version
 | |
| 44 static const char kPassword1[] = ""; | |
| 45 static const char kPassword2[] = "test"; | |
| 60 | 46 | 
| 61 EXPECT_EQ(key1value, key3value); | 47 std::unique_ptr<crypto::ECPrivateKey> keypair(crypto::ECPrivateKey::Create()); | 
| 62 EXPECT_EQ(key2value, key4value); | 48 ASSERT_TRUE(keypair.get()); | 
| 63 | 49 | 
| 64 std::vector<uint8_t> pubkey3; | 50 // Re-import as a PrivateKeyInfo. | 
| 65 std::vector<uint8_t> pubkey4; | 51 std::vector<uint8_t> privkey; | 
| 66 std::string raw_pubkey3; | 52 EXPECT_TRUE(keypair->ExportPrivateKey(&privkey)); | 
| 67 std::string raw_pubkey4; | 53 std::unique_ptr<crypto::ECPrivateKey> keypair_copy = | 
| 68 EXPECT_TRUE(keypair3->ExportPublicKey(&pubkey3)); | 54 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(privkey); | 
| 69 EXPECT_TRUE(keypair4->ExportPublicKey(&pubkey4)); | 55 ASSERT_TRUE(keypair_copy); | 
| 70 EXPECT_TRUE(keypair3->ExportRawPublicKey(&raw_pubkey3)); | 56 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | 
| 71 EXPECT_TRUE(keypair4->ExportRawPublicKey(&raw_pubkey4)); | |
| 72 | 57 | 
| 73 EXPECT_EQ(pubkey1, pubkey3); | 58 // Re-import as an EncryptedPrivateKeyInfo with kPassword1. | 
| 74 EXPECT_EQ(pubkey2, pubkey4); | 59 std::vector<uint8_t> encrypted_privkey; | 
| 75 EXPECT_EQ(raw_pubkey1, raw_pubkey3); | 60 std::vector<uint8_t> pubkey; | 
| 76 EXPECT_EQ(raw_pubkey2, raw_pubkey4); | 61 EXPECT_TRUE( | 
| 62 keypair->ExportEncryptedPrivateKey(kPassword1, 1, &encrypted_privkey)); | |
| 63 EXPECT_TRUE(keypair->ExportPublicKey(&pubkey)); | |
| 64 keypair_copy.reset(crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
| 65 kPassword1, encrypted_privkey, pubkey)); | |
| 66 ASSERT_TRUE(keypair_copy); | |
| 67 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | |
| 68 | |
| 69 // Re-import as an EncryptedPrivateKeyInfo with kPassword2. | |
| 70 EXPECT_TRUE( | |
| 71 keypair->ExportEncryptedPrivateKey(kPassword2, 1, &encrypted_privkey)); | |
| 72 keypair_copy.reset(crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
| 73 kPassword2, encrypted_privkey, pubkey)); | |
| 74 ASSERT_TRUE(keypair_copy); | |
| 75 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | |
| 77 } | 76 } | 
| 78 | 77 | 
| 79 TEST(ECPrivateKeyUnitTest, Copy) { | 78 TEST(ECPrivateKeyUnitTest, Copy) { | 
| 80 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 79 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 
| 81 crypto::ECPrivateKey::Create()); | 80 crypto::ECPrivateKey::Create()); | 
| 82 std::unique_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); | 81 std::unique_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); | 
| 83 ASSERT_TRUE(keypair1.get()); | 82 ASSERT_TRUE(keypair1.get()); | 
| 84 ASSERT_TRUE(keypair2.get()); | 83 ASSERT_TRUE(keypair2.get()); | 
| 85 | 84 | 
| 86 std::vector<uint8_t> key1value; | 85 ExpectKeysEqual(keypair1.get(), keypair2.get()); | 
| 87 std::vector<uint8_t> key2value; | 86 } | 
| 88 EXPECT_TRUE(keypair1->ExportValueForTesting(&key1value)); | |
| 89 EXPECT_TRUE(keypair2->ExportValueForTesting(&key2value)); | |
| 90 EXPECT_EQ(key1value, key2value); | |
| 91 | 87 | 
| 92 std::vector<uint8_t> pubkey1; | 88 TEST(ECPrivateKeyUnitTest, CreateFromPrivateKeyInfo) { | 
| 93 std::vector<uint8_t> pubkey2; | 89 static const uint8_t kPrivateKeyInfo[] = { | 
| 94 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | 90 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | 
| 95 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); | 91 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, | 
| 96 EXPECT_EQ(pubkey1, pubkey2); | 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, | |
| 94 0x4d, 0x89, 0x68, 0x77, 0x08, 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, | |
| 95 0xe8, 0xd1, 0xc9, 0xce, 0x0a, 0x8b, 0xb4, 0x6a, 0xa1, 0x44, 0x03, 0x42, | |
| 96 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, | |
| 97 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, | |
| 98 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, | |
| 99 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, | |
| 100 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, | |
| 101 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1, | |
| 102 }; | |
| 103 static const uint8_t kSubjectPublicKeyInfo[] = { | |
| 104 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, | |
| 105 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | |
| 106 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, | |
| 107 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, | |
| 108 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, | |
| 109 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, | |
| 110 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, | |
| 111 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1, | |
| 112 }; | |
| 113 static const uint8_t kRawPublicKey[] = { | |
| 114 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, | |
| 115 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, | |
| 116 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, | |
| 117 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, | |
| 118 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, | |
| 119 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1, | |
| 120 }; | |
| 97 | 121 | 
| 98 std::string raw_pubkey1; | 122 std::unique_ptr<crypto::ECPrivateKey> key = | 
| 99 std::string raw_pubkey2; | 123 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(std::vector<uint8_t>( | 
| 100 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1)); | 124 std::begin(kPrivateKeyInfo), std::end(kPrivateKeyInfo))); | 
| 101 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); | 125 ASSERT_TRUE(key); | 
| 102 EXPECT_EQ(raw_pubkey1, raw_pubkey2); | 126 | 
| 127 std::vector<uint8_t> public_key; | |
| 128 ASSERT_TRUE(key->ExportPublicKey(&public_key)); | |
| 129 EXPECT_EQ(std::vector<uint8_t>(std::begin(kSubjectPublicKeyInfo), | |
| 130 std::end(kSubjectPublicKeyInfo)), | |
| 131 public_key); | |
| 132 | |
| 133 std::string raw_public_key; | |
| 134 ASSERT_TRUE(key->ExportRawPublicKey(&raw_public_key)); | |
| 135 EXPECT_EQ(std::string(reinterpret_cast<const char*>(kRawPublicKey), | |
| 136 sizeof(kRawPublicKey)), | |
| 137 raw_public_key); | |
| 103 } | 138 } | 
| 104 | 139 | 
| 105 TEST(ECPrivateKeyUnitTest, BadPasswordTest) { | 140 TEST(ECPrivateKeyUnitTest, BadPasswordTest) { | 
| 106 const std::string password1; | 141 const std::string password1; | 
| 107 const std::string password2 = "test"; | 142 const std::string password2 = "test"; | 
| 108 | 143 | 
| 109 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 144 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 
| 110 crypto::ECPrivateKey::Create()); | 145 crypto::ECPrivateKey::Create()); | 
| 111 ASSERT_TRUE(keypair1.get()); | 146 ASSERT_TRUE(keypair1.get()); | 
| 112 | 147 | 
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 | 330 | 
| 296 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( | 331 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( | 
| 297 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 332 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 
| 298 "", | 333 "", | 
| 299 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), | 334 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), | 
| 300 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 335 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 
| 301 std::end(kOpenSSLPublicKey)))); | 336 std::end(kOpenSSLPublicKey)))); | 
| 302 | 337 | 
| 303 EXPECT_TRUE(keypair_openssl.get()); | 338 EXPECT_TRUE(keypair_openssl.get()); | 
| 304 } | 339 } | 
| OLD | NEW |