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.
5 #include "net/ssl/openssl_client_key_store.h"
7 #include "base/memory/ref_counted.h"
8 #include "net/base/test_data_directory.h"
9 #include "net/test/cert_test_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY
;
18 // Return the internal reference count of a given EVP_PKEY.
19 int EVP_PKEY_get_refcount(EVP_PKEY
* pkey
) {
20 return pkey
->references
;
23 // A common test class to ensure that the store is flushed after
25 class OpenSSLClientKeyStoreTest
: public ::testing::Test
{
27 OpenSSLClientKeyStoreTest()
28 : store_(OpenSSLClientKeyStore::GetInstance()) {
31 virtual ~OpenSSLClientKeyStoreTest() {
37 OpenSSLClientKeyStore
* store_
;
40 // Check that GetInstance() returns non-null
41 TEST_F(OpenSSLClientKeyStoreTest
, GetInstance
) {
45 // Check that Flush() works correctly.
46 TEST_F(OpenSSLClientKeyStoreTest
, Flush
) {
49 scoped_refptr
<X509Certificate
> cert_1(
50 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
51 ASSERT_TRUE(cert_1
.get());
53 ScopedEVP_PKEY
priv_key(EVP_PKEY_new());
54 ASSERT_TRUE(priv_key
.get());
56 ASSERT_TRUE(store_
->RecordClientCertPrivateKey(cert_1
.get(),
61 // Retrieve the private key. This should fail because the store
64 ASSERT_FALSE(store_
->FetchClientCertPrivateKey(cert_1
.get(), &pkey
));
65 ASSERT_FALSE(pkey
.get());
68 // Check that trying to retrieve the private key of an unknown certificate
69 // simply fails by returning null.
70 TEST_F(OpenSSLClientKeyStoreTest
, FetchEmptyPrivateKey
) {
73 scoped_refptr
<X509Certificate
> cert_1(
74 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
75 ASSERT_TRUE(cert_1
.get());
77 // Retrieve the private key now. This should fail because it was
78 // never recorded in the store.
80 ASSERT_FALSE(store_
->FetchClientCertPrivateKey(cert_1
.get(), &pkey
));
81 ASSERT_FALSE(pkey
.get());
84 // Check that any private key recorded through RecordClientCertPrivateKey
85 // can be retrieved with FetchClientCertPrivateKey.
86 TEST_F(OpenSSLClientKeyStoreTest
, RecordAndFetchPrivateKey
) {
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
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.
93 scoped_refptr
<X509Certificate
> cert_1(
94 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
95 ASSERT_TRUE(cert_1
.get());
97 ScopedEVP_PKEY
priv_key(EVP_PKEY_new());
98 ASSERT_TRUE(priv_key
.get());
99 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key
.get()));
101 // Add the key a first time, this should increment its reference count.
102 ASSERT_TRUE(store_
->RecordClientCertPrivateKey(cert_1
.get(),
104 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key
.get()));
106 // Two successive calls with the same certificate / private key shall
107 // also succeed, but the key's reference count should not be incremented.
108 ASSERT_TRUE(store_
->RecordClientCertPrivateKey(cert_1
.get(),
110 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key
.get()));
112 // Retrieve the private key. This should increment the private key's
114 ScopedEVP_PKEY pkey2
;
115 ASSERT_TRUE(store_
->FetchClientCertPrivateKey(cert_1
.get(), &pkey2
));
116 ASSERT_EQ(pkey2
.get(), priv_key
.get());
117 ASSERT_EQ(3, EVP_PKEY_get_refcount(priv_key
.get()));
119 // Flush the store explicitely, this should decrement the private
120 // key's reference count.
122 ASSERT_EQ(2, EVP_PKEY_get_refcount(priv_key
.get()));
125 // Same test, but with two certificates / private keys.
126 TEST_F(OpenSSLClientKeyStoreTest
, RecordAndFetchTwoPrivateKeys
) {
127 scoped_refptr
<X509Certificate
> cert_1(
128 ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
129 ASSERT_TRUE(cert_1
.get());
131 scoped_refptr
<X509Certificate
> cert_2(
132 ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem"));
133 ASSERT_TRUE(cert_2
.get());
135 ScopedEVP_PKEY
priv_key1(EVP_PKEY_new());
136 ASSERT_TRUE(priv_key1
.get());
137 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1
.get()));
139 ScopedEVP_PKEY
priv_key2(EVP_PKEY_new());
140 ASSERT_TRUE(priv_key2
.get());
141 ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2
.get()));
143 ASSERT_NE(priv_key1
.get(), priv_key2
.get());
145 // Add the key a first time, this shall succeed, and increment the
147 EXPECT_TRUE(store_
->RecordClientCertPrivateKey(cert_1
.get(),
149 EXPECT_TRUE(store_
->RecordClientCertPrivateKey(cert_2
.get(),
151 EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key1
.get()));
152 EXPECT_EQ(2, EVP_PKEY_get_refcount(priv_key2
.get()));
154 // Retrieve the private key now. This shall succeed and increment
155 // the private key's reference count.
156 ScopedEVP_PKEY fetch_key1
;
157 ASSERT_TRUE(store_
->FetchClientCertPrivateKey(cert_1
.get(),
159 ScopedEVP_PKEY fetch_key2
;
160 ASSERT_TRUE(store_
->FetchClientCertPrivateKey(cert_2
.get(),
162 EXPECT_TRUE(fetch_key1
.get());
163 EXPECT_TRUE(fetch_key2
.get());
165 EXPECT_EQ(fetch_key1
.get(), priv_key1
.get());
166 EXPECT_EQ(fetch_key2
.get(), priv_key2
.get());
168 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key1
.get()));
169 EXPECT_EQ(3, EVP_PKEY_get_refcount(priv_key2
.get()));