ssl_platform_key_nss_unittest.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  3. // found in the LICENSE file.
  4. #include "net/ssl/ssl_platform_key_nss.h"
  5. #include <string>
  6. #include "base/files/file_path.h"
  7. #include "base/files/file_util.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "crypto/nss_crypto_module_delegate.h"
  10. #include "crypto/scoped_nss_types.h"
  11. #include "crypto/scoped_test_nss_db.h"
  12. #include "net/cert/x509_util_nss.h"
  13. #include "net/ssl/ssl_private_key.h"
  14. #include "net/ssl/ssl_private_key_test_util.h"
  15. #include "net/test/cert_test_util.h"
  16. #include "net/test/test_data_directory.h"
  17. #include "net/test/test_with_task_environment.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "third_party/boringssl/src/include/openssl/evp.h"
  20. namespace net {
  21. namespace {
  22. struct TestKey {
  23. const char* name;
  24. const char* cert_file;
  25. const char* key_file;
  26. int type;
  27. };
  28. const TestKey kTestKeys[] = {
  29. {"RSA", "client_1.pem", "client_1.pk8", EVP_PKEY_RSA},
  30. {"ECDSA_P256", "client_4.pem", "client_4.pk8", EVP_PKEY_EC},
  31. {"ECDSA_P384", "client_5.pem", "client_5.pk8", EVP_PKEY_EC},
  32. {"ECDSA_P521", "client_6.pem", "client_6.pk8", EVP_PKEY_EC},
  33. };
  34. std::string TestKeyToString(const testing::TestParamInfo<TestKey>& params) {
  35. return params.param.name;
  36. }
  37. } // namespace
  38. class SSLPlatformKeyNSSTest : public testing::TestWithParam<TestKey>,
  39. public WithTaskEnvironment {};
  40. TEST_P(SSLPlatformKeyNSSTest, KeyMatches) {
  41. const TestKey& test_key = GetParam();
  42. std::string pkcs8;
  43. base::FilePath pkcs8_path =
  44. GetTestCertsDirectory().AppendASCII(test_key.key_file);
  45. ASSERT_TRUE(base::ReadFileToString(pkcs8_path, &pkcs8));
  46. // Import the key into a test NSS database.
  47. crypto::ScopedTestNSSDB test_db;
  48. ScopedCERTCertificate nss_cert;
  49. scoped_refptr<X509Certificate> cert = ImportClientCertAndKeyFromFile(
  50. GetTestCertsDirectory(), test_key.cert_file, test_key.key_file,
  51. test_db.slot(), &nss_cert);
  52. ASSERT_TRUE(cert);
  53. ASSERT_TRUE(nss_cert);
  54. // Look up the key.
  55. scoped_refptr<SSLPrivateKey> key =
  56. FetchClientCertPrivateKey(cert.get(), nss_cert.get(), nullptr);
  57. ASSERT_TRUE(key);
  58. // All NSS keys are expected to have the default preferences.
  59. EXPECT_EQ(SSLPrivateKey::DefaultAlgorithmPreferences(test_key.type,
  60. true /* supports PSS */),
  61. key->GetAlgorithmPreferences());
  62. TestSSLPrivateKeyMatches(key.get(), pkcs8);
  63. }
  64. INSTANTIATE_TEST_SUITE_P(All,
  65. SSLPlatformKeyNSSTest,
  66. testing::ValuesIn(kTestKeys),
  67. TestKeyToString);
  68. } // namespace net