| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "net/ssl/ssl_platform_key.h" | 5 #include "net/ssl/ssl_platform_key.h" |
| 6 | 6 |
| 7 #include <keyhi.h> | 7 #include <keyhi.h> |
| 8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "third_party/boringssl/src/include/openssl/ec.h" | 28 #include "third_party/boringssl/src/include/openssl/ec.h" |
| 29 #include "third_party/boringssl/src/include/openssl/ec_key.h" | 29 #include "third_party/boringssl/src/include/openssl/ec_key.h" |
| 30 #include "third_party/boringssl/src/include/openssl/evp.h" | 30 #include "third_party/boringssl/src/include/openssl/evp.h" |
| 31 #include "third_party/boringssl/src/include/openssl/mem.h" | 31 #include "third_party/boringssl/src/include/openssl/mem.h" |
| 32 | 32 |
| 33 namespace net { | 33 namespace net { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 struct TestKey { | 37 struct TestKey { |
| 38 const char* name; |
| 38 const char* cert_file; | 39 const char* cert_file; |
| 39 const char* key_file; | 40 const char* key_file; |
| 40 SSLPrivateKey::Type key_type; | 41 bool is_ecdsa; |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 const TestKey kTestKeys[] = { | 44 const TestKey kTestKeys[] = { |
| 44 {"client_1.pem", "client_1.pk8", SSLPrivateKey::Type::RSA}, | 45 {"RSA", "client_1.pem", "client_1.pk8", false}, |
| 45 {"client_4.pem", "client_4.pk8", SSLPrivateKey::Type::ECDSA_P256}, | 46 {"ECDSA_P256", "client_4.pem", "client_4.pk8", true}, |
| 46 {"client_5.pem", "client_5.pk8", SSLPrivateKey::Type::ECDSA_P384}, | 47 {"ECDSA_P384", "client_5.pem", "client_5.pk8", true}, |
| 47 {"client_6.pem", "client_6.pk8", SSLPrivateKey::Type::ECDSA_P521}, | 48 {"ECDSA_P521", "client_6.pem", "client_6.pk8", true}, |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 std::string TestKeyToString(const testing::TestParamInfo<TestKey>& params) { | 51 std::string TestKeyToString(const testing::TestParamInfo<TestKey>& params) { |
| 51 return SSLPrivateKeyTypeToString(params.param.key_type); | 52 return params.param.name; |
| 52 } | 53 } |
| 53 | 54 |
| 54 } // namespace | 55 } // namespace |
| 55 | 56 |
| 56 class SSLPlatformKeyNSSTest : public testing::TestWithParam<TestKey> {}; | 57 class SSLPlatformKeyNSSTest : public testing::TestWithParam<TestKey> {}; |
| 57 | 58 |
| 58 TEST_P(SSLPlatformKeyNSSTest, KeyMatches) { | 59 TEST_P(SSLPlatformKeyNSSTest, KeyMatches) { |
| 59 const TestKey& test_key = GetParam(); | 60 const TestKey& test_key = GetParam(); |
| 60 | 61 |
| 61 std::string pkcs8; | 62 std::string pkcs8; |
| 62 base::FilePath pkcs8_path = | 63 base::FilePath pkcs8_path = |
| 63 GetTestCertsDirectory().AppendASCII(test_key.key_file); | 64 GetTestCertsDirectory().AppendASCII(test_key.key_file); |
| 64 ASSERT_TRUE(base::ReadFileToString(pkcs8_path, &pkcs8)); | 65 ASSERT_TRUE(base::ReadFileToString(pkcs8_path, &pkcs8)); |
| 65 | 66 |
| 66 // Import the key into a test NSS database. | 67 // Import the key into a test NSS database. |
| 67 crypto::ScopedTestNSSDB test_db; | 68 crypto::ScopedTestNSSDB test_db; |
| 68 scoped_refptr<X509Certificate> cert; | 69 scoped_refptr<X509Certificate> cert; |
| 69 if (SSLPrivateKey::IsECDSAType(test_key.key_type)) { | 70 if (test_key.is_ecdsa) { |
| 70 // NSS cannot import unencrypted ECDSA keys, so we encrypt it with an empty | 71 // NSS cannot import unencrypted ECDSA keys, so we encrypt it with an empty |
| 71 // password and import manually. | 72 // password and import manually. |
| 72 std::vector<uint8_t> pkcs8_vector(pkcs8.begin(), pkcs8.end()); | 73 std::vector<uint8_t> pkcs8_vector(pkcs8.begin(), pkcs8.end()); |
| 73 std::unique_ptr<crypto::ECPrivateKey> ec_private_key = | 74 std::unique_ptr<crypto::ECPrivateKey> ec_private_key = |
| 74 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(pkcs8_vector); | 75 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(pkcs8_vector); |
| 75 ASSERT_TRUE(ec_private_key); | 76 ASSERT_TRUE(ec_private_key); |
| 76 std::vector<uint8_t> encrypted; | 77 std::vector<uint8_t> encrypted; |
| 77 ASSERT_TRUE(ec_private_key->ExportEncryptedPrivateKey(&encrypted)); | 78 ASSERT_TRUE(ec_private_key->ExportEncryptedPrivateKey(&encrypted)); |
| 78 | 79 |
| 79 SECItem encrypted_item = {siBuffer, encrypted.data(), | 80 SECItem encrypted_item = {siBuffer, encrypted.data(), |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 133 |
| 133 TestSSLPrivateKeyMatches(key.get(), pkcs8); | 134 TestSSLPrivateKeyMatches(key.get(), pkcs8); |
| 134 } | 135 } |
| 135 | 136 |
| 136 INSTANTIATE_TEST_CASE_P(, | 137 INSTANTIATE_TEST_CASE_P(, |
| 137 SSLPlatformKeyNSSTest, | 138 SSLPlatformKeyNSSTest, |
| 138 testing::ValuesIn(kTestKeys), | 139 testing::ValuesIn(kTestKeys), |
| 139 TestKeyToString); | 140 TestKeyToString); |
| 140 | 141 |
| 141 } // namespace net | 142 } // namespace net |
| OLD | NEW |