| 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" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY; | 16 typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY; |
| 17 | 17 |
| 18 // Return the internal reference count of a given EVP_PKEY. | 18 // Return the internal reference count of a given EVP_PKEY. |
| 19 int EVP_PKEY_get_refcount(EVP_PKEY* pkey) { | 19 int EVP_PKEY_get_refcount(EVP_PKEY* pkey) { |
| 20 return pkey->references; | 20 return pkey->references; |
| 21 } | 21 } |
| 22 | 22 |
| 23 // A common test class to ensure that the store is flushed after | 23 // A common test class to ensure that the store is flushed after |
| 24 // each test. | 24 // each test. |
| 25 class OpenSSLClientKeyStoreTest : public ::testing::Test { | 25 class OpenSSLClientKeyStoreTest : public ::testing::Test { |
| 26 public: | 26 public: |
| 27 OpenSSLClientKeyStoreTest() | 27 OpenSSLClientKeyStoreTest() : store_(OpenSSLClientKeyStore::GetInstance()) {} |
| 28 : store_(OpenSSLClientKeyStore::GetInstance()) { | |
| 29 } | |
| 30 | 28 |
| 31 virtual ~OpenSSLClientKeyStoreTest() { | 29 virtual ~OpenSSLClientKeyStoreTest() { |
| 32 if (store_) | 30 if (store_) |
| 33 store_->Flush(); | 31 store_->Flush(); |
| 34 } | 32 } |
| 35 | 33 |
| 36 protected: | 34 protected: |
| 37 OpenSSLClientKeyStore* store_; | 35 OpenSSLClientKeyStore* store_; |
| 38 }; | 36 }; |
| 39 | 37 |
| 40 // Check that GetInstance() returns non-null | 38 // Check that GetInstance() returns non-null |
| 41 TEST_F(OpenSSLClientKeyStoreTest, GetInstance) { | 39 TEST_F(OpenSSLClientKeyStoreTest, GetInstance) { |
| 42 ASSERT_TRUE(store_); | 40 ASSERT_TRUE(store_); |
| 43 } | 41 } |
| 44 | 42 |
| 45 // Check that Flush() works correctly. | 43 // Check that Flush() works correctly. |
| 46 TEST_F(OpenSSLClientKeyStoreTest, Flush) { | 44 TEST_F(OpenSSLClientKeyStoreTest, Flush) { |
| 47 ASSERT_TRUE(store_); | 45 ASSERT_TRUE(store_); |
| 48 | 46 |
| 49 scoped_refptr<X509Certificate> cert_1( | 47 scoped_refptr<X509Certificate> cert_1( |
| 50 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); | 48 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); |
| 51 ASSERT_TRUE(cert_1.get()); | 49 ASSERT_TRUE(cert_1.get()); |
| 52 | 50 |
| 53 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); | 51 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); |
| 54 ASSERT_TRUE(priv_key.get()); | 52 ASSERT_TRUE(priv_key.get()); |
| 55 | 53 |
| 56 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), | 54 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), priv_key.get())); |
| 57 priv_key.get())); | |
| 58 | 55 |
| 59 store_->Flush(); | 56 store_->Flush(); |
| 60 | 57 |
| 61 // Retrieve the private key. This should fail because the store | 58 // Retrieve the private key. This should fail because the store |
| 62 // was flushed. | 59 // was flushed. |
| 63 ScopedEVP_PKEY pkey; | 60 ScopedEVP_PKEY pkey; |
| 64 ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); | 61 ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); |
| 65 ASSERT_FALSE(pkey.get()); | 62 ASSERT_FALSE(pkey.get()); |
| 66 } | 63 } |
| 67 | 64 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 92 // JNI reference, with no way to access the real private key bits. | 89 // JNI reference, with no way to access the real private key bits. |
| 93 scoped_refptr<X509Certificate> cert_1( | 90 scoped_refptr<X509Certificate> cert_1( |
| 94 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); | 91 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); |
| 95 ASSERT_TRUE(cert_1.get()); | 92 ASSERT_TRUE(cert_1.get()); |
| 96 | 93 |
| 97 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); | 94 ScopedEVP_PKEY priv_key(EVP_PKEY_new()); |
| 98 ASSERT_TRUE(priv_key.get()); | 95 ASSERT_TRUE(priv_key.get()); |
| 99 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key.get())); | 96 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key.get())); |
| 100 | 97 |
| 101 // Add the key a first time, this should increment its reference count. | 98 // Add the key a first time, this should increment its reference count. |
| 102 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), | 99 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), priv_key.get())); |
| 103 priv_key.get())); | |
| 104 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); | 100 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); |
| 105 | 101 |
| 106 // Two successive calls with the same certificate / private key shall | 102 // Two successive calls with the same certificate / private key shall |
| 107 // also succeed, but the key's reference count should not be incremented. | 103 // also succeed, but the key's reference count should not be incremented. |
| 108 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), | 104 ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), priv_key.get())); |
| 109 priv_key.get())); | |
| 110 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); | 105 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key.get())); |
| 111 | 106 |
| 112 // Retrieve the private key. This should increment the private key's | 107 // Retrieve the private key. This should increment the private key's |
| 113 // reference count. | 108 // reference count. |
| 114 ScopedEVP_PKEY pkey2; | 109 ScopedEVP_PKEY pkey2; |
| 115 ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey2)); | 110 ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey2)); |
| 116 ASSERT_EQ(pkey2.get(), priv_key.get()); | 111 ASSERT_EQ(pkey2.get(), priv_key.get()); |
| 117 ASSERT_EQ(3, EVP_PKEY_get_refcount(priv_key.get())); | 112 ASSERT_EQ(3, EVP_PKEY_get_refcount(priv_key.get())); |
| 118 | 113 |
| 119 // Flush the store explicitely, this should decrement the private | 114 // Flush the store explicitely, this should decrement the private |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1.get())); | 132 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1.get())); |
| 138 | 133 |
| 139 ScopedEVP_PKEY priv_key2(EVP_PKEY_new()); | 134 ScopedEVP_PKEY priv_key2(EVP_PKEY_new()); |
| 140 ASSERT_TRUE(priv_key2.get()); | 135 ASSERT_TRUE(priv_key2.get()); |
| 141 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2.get())); | 136 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2.get())); |
| 142 | 137 |
| 143 ASSERT_NE(priv_key1.get(), priv_key2.get()); | 138 ASSERT_NE(priv_key1.get(), priv_key2.get()); |
| 144 | 139 |
| 145 // Add the key a first time, this shall succeed, and increment the | 140 // Add the key a first time, this shall succeed, and increment the |
| 146 // reference count. | 141 // reference count. |
| 147 EXPECT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), | 142 EXPECT_TRUE( |
| 148 priv_key1.get())); | 143 store_->RecordClientCertPrivateKey(cert_1.get(), priv_key1.get())); |
| 149 EXPECT_TRUE(store_->RecordClientCertPrivateKey(cert_2.get(), | 144 EXPECT_TRUE( |
| 150 priv_key2.get())); | 145 store_->RecordClientCertPrivateKey(cert_2.get(), priv_key2.get())); |
| 151 EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key1.get())); | 146 EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key1.get())); |
| 152 EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key2.get())); | 147 EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key2.get())); |
| 153 | 148 |
| 154 // Retrieve the private key now. This shall succeed and increment | 149 // Retrieve the private key now. This shall succeed and increment |
| 155 // the private key's reference count. | 150 // the private key's reference count. |
| 156 ScopedEVP_PKEY fetch_key1; | 151 ScopedEVP_PKEY fetch_key1; |
| 157 ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), | 152 ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), &fetch_key1)); |
| 158 &fetch_key1)); | |
| 159 ScopedEVP_PKEY fetch_key2; | 153 ScopedEVP_PKEY fetch_key2; |
| 160 ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_2.get(), | 154 ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_2.get(), &fetch_key2)); |
| 161 &fetch_key2)); | |
| 162 EXPECT_TRUE(fetch_key1.get()); | 155 EXPECT_TRUE(fetch_key1.get()); |
| 163 EXPECT_TRUE(fetch_key2.get()); | 156 EXPECT_TRUE(fetch_key2.get()); |
| 164 | 157 |
| 165 EXPECT_EQ(fetch_key1.get(), priv_key1.get()); | 158 EXPECT_EQ(fetch_key1.get(), priv_key1.get()); |
| 166 EXPECT_EQ(fetch_key2.get(), priv_key2.get()); | 159 EXPECT_EQ(fetch_key2.get(), priv_key2.get()); |
| 167 | 160 |
| 168 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key1.get())); | 161 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key1.get())); |
| 169 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key2.get())); | 162 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key2.get())); |
| 170 } | 163 } |
| 171 | 164 |
| 172 } // namespace | 165 } // namespace |
| 173 } // namespace net | 166 } // namespace net |
| OLD | NEW |