| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/openssl_client_key_store.h" | 5 #include "net/ssl/openssl_client_key_store.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "net/base/test_data_directory.h" | 8 #include "net/base/test_data_directory.h" |
| 9 #include "net/test/cert_test_util.h" | 9 #include "net/test/cert_test_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 TEST_F(OpenSSLClientKeyStoreTest, GetInstance) { | 41 TEST_F(OpenSSLClientKeyStoreTest, GetInstance) { |
| 42 ASSERT_TRUE(store_); | 42 ASSERT_TRUE(store_); |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Check that Flush() works correctly. | 45 // Check that Flush() works correctly. |
| 46 TEST_F(OpenSSLClientKeyStoreTest, Flush) { | 46 TEST_F(OpenSSLClientKeyStoreTest, Flush) { |
| 47 ASSERT_TRUE(store_); | 47 ASSERT_TRUE(store_); |
| 48 | 48 |
| 49 scoped_refptr<X509Certificate> cert_1( | 49 scoped_refptr<X509Certificate> cert_1( |
| 50 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); | 50 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); |
| 51 ASSERT_TRUE(cert_1); | 51 ASSERT_TRUE(cert_1.get()); |
| 52 | 52 |
| 53 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); | 53 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); |
| 54 ASSERT_TRUE(priv_key.get()); | 54 ASSERT_TRUE(priv_key.get()); |
| 55 | 55 |
| 56 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), | 56 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), |
| 57 priv_key.get())); | 57 priv_key.get())); |
| 58 | 58 |
| 59 store_->Flush(); | 59 store_->Flush(); |
| 60 | 60 |
| 61 // Retrieve the private key. This should fail because the store | 61 // Retrieve the private key. This should fail because the store |
| 62 // was flushed. | 62 // was flushed. |
| 63 ScopedEVP_PKEY pkey; | 63 ScopedEVP_PKEY pkey; |
| 64 ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); | 64 ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); |
| 65 ASSERT_FALSE(pkey.get()); | 65 ASSERT_FALSE(pkey.get()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Check that trying to retrieve the private key of an unknown certificate | 68 // Check that trying to retrieve the private key of an unknown certificate |
| 69 // simply fails by returning null. | 69 // simply fails by returning null. |
| 70 TEST_F(OpenSSLClientKeyStoreTest, FetchEmptyPrivateKey) { | 70 TEST_F(OpenSSLClientKeyStoreTest, FetchEmptyPrivateKey) { |
| 71 ASSERT_TRUE(store_); | 71 ASSERT_TRUE(store_); |
| 72 | 72 |
| 73 scoped_refptr<X509Certificate> cert_1( | 73 scoped_refptr<X509Certificate> cert_1( |
| 74 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); | 74 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); |
| 75 ASSERT_TRUE(cert_1); | 75 ASSERT_TRUE(cert_1.get()); |
| 76 | 76 |
| 77 // Retrieve the private key now. This should fail because it was | 77 // Retrieve the private key now. This should fail because it was |
| 78 // never recorded in the store. | 78 // never recorded in the store. |
| 79 ScopedEVP_PKEY pkey; | 79 ScopedEVP_PKEY pkey; |
| 80 ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); | 80 ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); |
| 81 ASSERT_FALSE(pkey.get()); | 81 ASSERT_FALSE(pkey.get()); |
| 82 } | 82 } |
| 83 | 83 |
| 84 // Check that any private key recorded through RecordClientCertPrivateKey | 84 // Check that any private key recorded through RecordClientCertPrivateKey |
| 85 // can be retrieved with FetchClientCertPrivateKey. | 85 // can be retrieved with FetchClientCertPrivateKey. |
| 86 TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchPrivateKey) { | 86 TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchPrivateKey) { |
| 87 ASSERT_TRUE(store_); | 87 ASSERT_TRUE(store_); |
| 88 | 88 |
| 89 // Any certificate / key pair will do, the store is not supposed to | 89 // Any certificate / key pair will do, the store is not supposed to |
| 90 // check that the private and certificate public keys match. This is | 90 // check that the private and certificate public keys match. This is |
| 91 // by design since the private EVP_PKEY could be a wrapper around a | 91 // by design since the private EVP_PKEY could be a wrapper around a |
| 92 // JNI reference, with no way to access the real private key bits. | 92 // JNI reference, with no way to access the real private key bits. |
| 93 scoped_refptr<X509Certificate> cert_1( | 93 scoped_refptr<X509Certificate> cert_1( |
| 94 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); | 94 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); |
| 95 ASSERT_TRUE(cert_1); | 95 ASSERT_TRUE(cert_1.get()); |
| 96 | 96 |
| 97 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); | 97 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); |
| 98 ASSERT_TRUE(priv_key.get()); | 98 ASSERT_TRUE(priv_key.get()); |
| 99 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key.get())); | 99 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key.get())); |
| 100 | 100 |
| 101 // Add the key a first time, this should increment its reference count. | 101 // Add the key a first time, this should increment its reference count. |
| 102 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), | 102 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), |
| 103 priv_key.get())); | 103 priv_key.get())); |
| 104 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); | 104 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); |
| 105 | 105 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 119 // Flush the store explicitely, this should decrement the private | 119 // Flush the store explicitely, this should decrement the private |
| 120 // key's reference count. | 120 // key's reference count. |
| 121 store_->Flush(); | 121 store_->Flush(); |
| 122 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); | 122 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Same test, but with two certificates / private keys. | 125 // Same test, but with two certificates / private keys. |
| 126 TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchTwoPrivateKeys) { | 126 TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchTwoPrivateKeys) { |
| 127 scoped_refptr<X509Certificate> cert_1( | 127 scoped_refptr<X509Certificate> cert_1( |
| 128 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); | 128 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); |
| 129 ASSERT_TRUE(cert_1); | 129 ASSERT_TRUE(cert_1.get()); |
| 130 | 130 |
| 131 scoped_refptr<X509Certificate> cert_2( | 131 scoped_refptr<X509Certificate> cert_2( |
| 132 ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem")); | 132 ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem")); |
| 133 ASSERT_TRUE(cert_2); | 133 ASSERT_TRUE(cert_2.get()); |
| 134 | 134 |
| 135 ScopedEVP_PKEY priv_key1(EVP_PKEY_new()); | 135 ScopedEVP_PKEY priv_key1(EVP_PKEY_new()); |
| 136 ASSERT_TRUE(priv_key1.get()); | 136 ASSERT_TRUE(priv_key1.get()); |
| 137 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1.get())); | 137 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1.get())); |
| 138 | 138 |
| 139 ScopedEVP_PKEY priv_key2(EVP_PKEY_new()); | 139 ScopedEVP_PKEY priv_key2(EVP_PKEY_new()); |
| 140 ASSERT_TRUE(priv_key2.get()); | 140 ASSERT_TRUE(priv_key2.get()); |
| 141 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2.get())); | 141 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2.get())); |
| 142 | 142 |
| 143 ASSERT_NE(priv_key1.get(), priv_key2.get()); | 143 ASSERT_NE(priv_key1.get(), priv_key2.get()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 164 | 164 |
| 165 EXPECT_EQ(fetch_key1.get(), priv_key1.get()); | 165 EXPECT_EQ(fetch_key1.get(), priv_key1.get()); |
| 166 EXPECT_EQ(fetch_key2.get(), priv_key2.get()); | 166 EXPECT_EQ(fetch_key2.get(), priv_key2.get()); |
| 167 | 167 |
| 168 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key1.get())); | 168 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key1.get())); |
| 169 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key2.get())); | 169 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key2.get())); |
| 170 } | 170 } |
| 171 | 171 |
| 172 } // namespace | 172 } // namespace |
| 173 } // namespace net | 173 } // namespace net |
| OLD | NEW |