ssl_platform_key_mac_unittest.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_mac.h"
  5. #include <CoreFoundation/CoreFoundation.h>
  6. #include <string>
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/mac/scoped_cftyperef.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/test/task_environment.h"
  12. #include "net/ssl/ssl_private_key.h"
  13. #include "net/ssl/ssl_private_key_test_util.h"
  14. #include "net/test/cert_test_util.h"
  15. #include "net/test/keychain_test_util_mac.h"
  16. #include "net/test/test_data_directory.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "third_party/boringssl/src/include/openssl/ssl.h"
  19. namespace net {
  20. namespace {
  21. struct TestKey {
  22. const char* name;
  23. const char* cert_file;
  24. const char* key_file;
  25. int type;
  26. };
  27. const TestKey kTestKeys[] = {
  28. {"RSA", "client_1.pem", "client_1.pk8", EVP_PKEY_RSA},
  29. {"ECDSA_P256", "client_4.pem", "client_4.pk8", EVP_PKEY_EC},
  30. {"ECDSA_P384", "client_5.pem", "client_5.pk8", EVP_PKEY_EC},
  31. {"ECDSA_P521", "client_6.pem", "client_6.pk8", EVP_PKEY_EC},
  32. };
  33. std::string TestKeyToString(const testing::TestParamInfo<TestKey>& params) {
  34. return params.param.name;
  35. }
  36. } // namespace
  37. class SSLPlatformKeyMacTest : public testing::TestWithParam<TestKey> {};
  38. TEST_P(SSLPlatformKeyMacTest, KeyMatches) {
  39. base::test::TaskEnvironment task_environment;
  40. const TestKey& test_key = GetParam();
  41. // Load test data.
  42. scoped_refptr<X509Certificate> cert =
  43. ImportCertFromFile(GetTestCertsDirectory(), test_key.cert_file);
  44. ASSERT_TRUE(cert);
  45. std::string pkcs8;
  46. base::FilePath pkcs8_path =
  47. GetTestCertsDirectory().AppendASCII(test_key.key_file);
  48. ASSERT_TRUE(base::ReadFileToString(pkcs8_path, &pkcs8));
  49. // Create a temporary keychain.
  50. ScopedTestKeychain scoped_keychain;
  51. ASSERT_TRUE(scoped_keychain.Initialize());
  52. SecKeychainRef keychain = scoped_keychain.keychain();
  53. // Import cert and key to the keychain.
  54. base::ScopedCFTypeRef<SecIdentityRef> sec_identity(
  55. ImportCertAndKeyToKeychain(cert.get(), pkcs8, keychain));
  56. ASSERT_TRUE(sec_identity);
  57. // Finally, test the code to look up the key.
  58. scoped_refptr<SSLPrivateKey> key =
  59. CreateSSLPrivateKeyForSecIdentity(cert.get(), sec_identity.get());
  60. ASSERT_TRUE(key);
  61. // Mac keys from the default provider are expected to support all algorithms.
  62. EXPECT_EQ(SSLPrivateKey::DefaultAlgorithmPreferences(test_key.type, true),
  63. key->GetAlgorithmPreferences());
  64. TestSSLPrivateKeyMatches(key.get(), pkcs8);
  65. }
  66. INSTANTIATE_TEST_SUITE_P(All,
  67. SSLPlatformKeyMacTest,
  68. testing::ValuesIn(kTestKeys),
  69. TestKeyToString);
  70. } // namespace net