xref: /aosp_15_r20/external/cronet/net/ssl/ssl_platform_key_nss_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/ssl/ssl_platform_key_nss.h"
6 
7 #include <string>
8 
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "crypto/nss_crypto_module_delegate.h"
13 #include "crypto/scoped_nss_types.h"
14 #include "crypto/scoped_test_nss_db.h"
15 #include "net/cert/x509_util_nss.h"
16 #include "net/ssl/ssl_private_key.h"
17 #include "net/ssl/ssl_private_key_test_util.h"
18 #include "net/test/cert_test_util.h"
19 #include "net/test/test_data_directory.h"
20 #include "net/test/test_with_task_environment.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/boringssl/src/include/openssl/evp.h"
23 
24 namespace net {
25 
26 namespace {
27 
28 struct TestKey {
29   const char* name;
30   const char* cert_file;
31   const char* key_file;
32   int type;
33 };
34 
35 const TestKey kTestKeys[] = {
36     {"RSA", "client_1.pem", "client_1.pk8", EVP_PKEY_RSA},
37     {"ECDSA_P256", "client_4.pem", "client_4.pk8", EVP_PKEY_EC},
38     {"ECDSA_P384", "client_5.pem", "client_5.pk8", EVP_PKEY_EC},
39     {"ECDSA_P521", "client_6.pem", "client_6.pk8", EVP_PKEY_EC},
40 };
41 
TestKeyToString(const testing::TestParamInfo<TestKey> & params)42 std::string TestKeyToString(const testing::TestParamInfo<TestKey>& params) {
43   return params.param.name;
44 }
45 
46 }  // namespace
47 
48 class SSLPlatformKeyNSSTest : public testing::TestWithParam<TestKey>,
49                               public WithTaskEnvironment {};
50 
TEST_P(SSLPlatformKeyNSSTest,KeyMatches)51 TEST_P(SSLPlatformKeyNSSTest, KeyMatches) {
52   const TestKey& test_key = GetParam();
53 
54   std::string pkcs8;
55   base::FilePath pkcs8_path =
56       GetTestCertsDirectory().AppendASCII(test_key.key_file);
57   ASSERT_TRUE(base::ReadFileToString(pkcs8_path, &pkcs8));
58 
59   // Import the key into a test NSS database.
60   crypto::ScopedTestNSSDB test_db;
61   ScopedCERTCertificate nss_cert;
62   scoped_refptr<X509Certificate> cert = ImportClientCertAndKeyFromFile(
63       GetTestCertsDirectory(), test_key.cert_file, test_key.key_file,
64       test_db.slot(), &nss_cert);
65   ASSERT_TRUE(cert);
66   ASSERT_TRUE(nss_cert);
67 
68   // Look up the key.
69   scoped_refptr<SSLPrivateKey> key =
70       FetchClientCertPrivateKey(cert.get(), nss_cert.get(), nullptr);
71   ASSERT_TRUE(key);
72 
73   // All NSS keys are expected to have the default preferences.
74   EXPECT_EQ(SSLPrivateKey::DefaultAlgorithmPreferences(test_key.type,
75                                                        true /* supports PSS */),
76             key->GetAlgorithmPreferences());
77 
78   TestSSLPrivateKeyMatches(key.get(), pkcs8);
79 }
80 
81 INSTANTIATE_TEST_SUITE_P(All,
82                          SSLPlatformKeyNSSTest,
83                          testing::ValuesIn(kTestKeys),
84                          TestKeyToString);
85 
86 }  // namespace net
87