ssl_platform_key_android_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2013 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_android.h"
  5. #include <string>
  6. #include "base/android/jni_android.h"
  7. #include "base/android/jni_array.h"
  8. #include "base/android/scoped_java_ref.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "net/android/keystore.h"
  12. #include "net/cert/x509_certificate.h"
  13. #include "net/net_test_jni_headers/AndroidKeyStoreTestUtil_jni.h"
  14. #include "net/ssl/ssl_private_key.h"
  15. #include "net/ssl/ssl_private_key_test_util.h"
  16. #include "net/test/cert_test_util.h"
  17. #include "net/test/test_data_directory.h"
  18. #include "net/test/test_with_task_environment.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. #include "third_party/boringssl/src/include/openssl/ssl.h"
  21. namespace net {
  22. namespace {
  23. typedef base::android::ScopedJavaLocalRef<jobject> ScopedJava;
  24. bool ReadTestFile(const char* filename, std::string* pkcs8) {
  25. base::FilePath certs_dir = GetTestCertsDirectory();
  26. base::FilePath file_path = certs_dir.AppendASCII(filename);
  27. return base::ReadFileToString(file_path, pkcs8);
  28. }
  29. // Retrieve a JNI local ref from encoded PKCS#8 data.
  30. ScopedJava GetPKCS8PrivateKeyJava(android::PrivateKeyType key_type,
  31. const std::string& pkcs8_key) {
  32. JNIEnv* env = base::android::AttachCurrentThread();
  33. base::android::ScopedJavaLocalRef<jbyteArray> bytes(
  34. base::android::ToJavaByteArray(
  35. env, reinterpret_cast<const uint8_t*>(pkcs8_key.data()),
  36. pkcs8_key.size()));
  37. ScopedJava key(Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8(
  38. env, key_type, bytes));
  39. return key;
  40. }
  41. struct TestKey {
  42. const char* name;
  43. const char* cert_file;
  44. const char* key_file;
  45. int type;
  46. android::PrivateKeyType android_key_type;
  47. };
  48. const TestKey kTestKeys[] = {
  49. {"RSA", "client_1.pem", "client_1.pk8", EVP_PKEY_RSA,
  50. android::PRIVATE_KEY_TYPE_RSA},
  51. {"ECDSA_P256", "client_4.pem", "client_4.pk8", EVP_PKEY_EC,
  52. android::PRIVATE_KEY_TYPE_ECDSA},
  53. {"ECDSA_P384", "client_5.pem", "client_5.pk8", EVP_PKEY_EC,
  54. android::PRIVATE_KEY_TYPE_ECDSA},
  55. {"ECDSA_P521", "client_6.pem", "client_6.pk8", EVP_PKEY_EC,
  56. android::PRIVATE_KEY_TYPE_ECDSA},
  57. };
  58. std::string TestKeyToString(const testing::TestParamInfo<TestKey>& params) {
  59. return params.param.name;
  60. }
  61. } // namespace
  62. class SSLPlatformKeyAndroidTest : public testing::TestWithParam<TestKey>,
  63. public WithTaskEnvironment {};
  64. TEST_P(SSLPlatformKeyAndroidTest, Matches) {
  65. const TestKey& test_key = GetParam();
  66. scoped_refptr<X509Certificate> cert =
  67. ImportCertFromFile(GetTestCertsDirectory(), test_key.cert_file);
  68. ASSERT_TRUE(cert);
  69. std::string key_bytes;
  70. ASSERT_TRUE(ReadTestFile(test_key.key_file, &key_bytes));
  71. ScopedJava java_key =
  72. GetPKCS8PrivateKeyJava(test_key.android_key_type, key_bytes);
  73. ASSERT_FALSE(java_key.is_null());
  74. scoped_refptr<SSLPrivateKey> key = WrapJavaPrivateKey(cert.get(), java_key);
  75. ASSERT_TRUE(key);
  76. EXPECT_EQ(SSLPrivateKey::DefaultAlgorithmPreferences(test_key.type,
  77. true /* supports_pss */),
  78. key->GetAlgorithmPreferences());
  79. TestSSLPrivateKeyMatches(key.get(), key_bytes);
  80. }
  81. INSTANTIATE_TEST_SUITE_P(All,
  82. SSLPlatformKeyAndroidTest,
  83. testing::ValuesIn(kTestKeys),
  84. TestKeyToString);
  85. } // namespace net