Commit c5077667 authored by davidben@chromium.org's avatar davidben@chromium.org

Switch OpenSSLClientKeyStore::ScopedEVP_PKEY to crypto::ScopedEVP_PKEY.

BUG=none

Review URL: https://codereview.chromium.org/388683002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282684 0039d316-1c4b-4281-b951-d872f2087c98
parent 4e72e787
...@@ -9,6 +9,7 @@ include_rules = [ ...@@ -9,6 +9,7 @@ include_rules = [
"+components/data_reduction_proxy", "+components/data_reduction_proxy",
"+content/public/common", "+content/public/common",
"+crypto",
"+gpu", "+gpu",
"+jni", "+jni",
"+net", "+net",
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "crypto/scoped_openssl_types.h"
#include "jni/AwContentsClientBridge_jni.h" #include "jni/AwContentsClientBridge_jni.h"
#include "net/android/keystore_openssl.h" #include "net/android/keystore_openssl.h"
#include "net/cert/x509_certificate.h" #include "net/cert/x509_certificate.h"
...@@ -32,15 +33,13 @@ using content::BrowserThread; ...@@ -32,15 +33,13 @@ using content::BrowserThread;
namespace android_webview { namespace android_webview {
typedef net::OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
namespace { namespace {
// Must be called on the I/O thread to record a client certificate // Must be called on the I/O thread to record a client certificate
// and its private key in the OpenSSLClientKeyStore. // and its private key in the OpenSSLClientKeyStore.
void RecordClientCertificateKey( void RecordClientCertificateKey(
const scoped_refptr<net::X509Certificate>& client_cert, const scoped_refptr<net::X509Certificate>& client_cert,
ScopedEVP_PKEY private_key) { crypto::ScopedEVP_PKEY private_key) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO); DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey( net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
client_cert.get(), private_key.get()); client_cert.get(), private_key.get());
...@@ -230,7 +229,7 @@ void AwContentsClientBridge::ProvideClientCertificateResponse( ...@@ -230,7 +229,7 @@ void AwContentsClientBridge::ProvideClientCertificateResponse(
} }
// Create an EVP_PKEY wrapper for the private key JNI reference. // Create an EVP_PKEY wrapper for the private key JNI reference.
ScopedEVP_PKEY private_key( crypto::ScopedEVP_PKEY private_key(
net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref)); net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref));
if (!private_key.get()) { if (!private_key.get()) {
LOG(ERROR) << "Could not create OpenSSL wrapper for private key"; LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "chrome/browser/ssl/ssl_client_certificate_selector.h" #include "chrome/browser/ssl/ssl_client_certificate_selector.h"
#include "chrome/browser/ui/android/window_android_helper.h" #include "chrome/browser/ui/android/window_android_helper.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "crypto/scoped_openssl_types.h"
#include "jni/SSLClientCertificateRequest_jni.h" #include "jni/SSLClientCertificateRequest_jni.h"
#include "net/android/keystore_openssl.h" #include "net/android/keystore_openssl.h"
#include "net/base/host_port_pair.h" #include "net/base/host_port_pair.h"
...@@ -30,13 +31,11 @@ namespace chrome { ...@@ -30,13 +31,11 @@ namespace chrome {
namespace { namespace {
typedef net::OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
// Must be called on the I/O thread to record a client certificate // Must be called on the I/O thread to record a client certificate
// and its private key in the OpenSSLClientKeyStore. // and its private key in the OpenSSLClientKeyStore.
void RecordClientCertificateKey( void RecordClientCertificateKey(
const scoped_refptr<net::X509Certificate>& client_cert, const scoped_refptr<net::X509Certificate>& client_cert,
ScopedEVP_PKEY private_key) { crypto::ScopedEVP_PKEY private_key) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO); DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey( net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
client_cert.get(), private_key.get()); client_cert.get(), private_key.get());
...@@ -184,7 +183,7 @@ static void OnSystemRequestCompletion( ...@@ -184,7 +183,7 @@ static void OnSystemRequestCompletion(
} }
// Create an EVP_PKEY wrapper for the private key JNI reference. // Create an EVP_PKEY wrapper for the private key JNI reference.
ScopedEVP_PKEY private_key( crypto::ScopedEVP_PKEY private_key(
net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref)); net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref));
if (!private_key.get()) { if (!private_key.get()) {
LOG(ERROR) << "Could not create OpenSSL wrapper for private key"; LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
......
...@@ -1354,7 +1354,7 @@ int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl, ...@@ -1354,7 +1354,7 @@ int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl,
// the reference count of the EVP_PKEY. Ownership of this reference // the reference count of the EVP_PKEY. Ownership of this reference
// is passed directly to OpenSSL, which will release the reference // is passed directly to OpenSSL, which will release the reference
// using EVP_PKEY_free() when the SSL object is destroyed. // using EVP_PKEY_free() when the SSL object is destroyed.
OpenSSLClientKeyStore::ScopedEVP_PKEY privkey; crypto::ScopedEVP_PKEY privkey;
if (OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( if (OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey(
ssl_config_.client_cert.get(), &privkey)) { ssl_config_.client_cert.get(), &privkey)) {
// TODO(joth): (copied from NSS) We should wait for server certificate // TODO(joth): (copied from NSS) We should wait for server certificate
......
...@@ -58,7 +58,7 @@ const SSLConfig kDefaultSSLConfig; ...@@ -58,7 +58,7 @@ const SSLConfig kDefaultSSLConfig;
// Returns true on success, false on failure. // Returns true on success, false on failure.
bool LoadPrivateKeyOpenSSL( bool LoadPrivateKeyOpenSSL(
const base::FilePath& filepath, const base::FilePath& filepath,
OpenSSLClientKeyStore::ScopedEVP_PKEY* pkey) { crypto::ScopedEVP_PKEY* pkey) {
std::string data; std::string data;
if (!base::ReadFileToString(filepath, &data)) { if (!base::ReadFileToString(filepath, &data)) {
LOG(ERROR) << "Could not read private key file: " LOG(ERROR) << "Could not read private key file: "
...@@ -250,7 +250,7 @@ TEST_F(SSLClientSocketOpenSSLClientAuthTest, SendGoodCert) { ...@@ -250,7 +250,7 @@ TEST_F(SSLClientSocketOpenSSLClientAuthTest, SendGoodCert) {
// This is required to ensure that signing works with the client // This is required to ensure that signing works with the client
// certificate's private key. // certificate's private key.
OpenSSLClientKeyStore::ScopedEVP_PKEY client_private_key; crypto::ScopedEVP_PKEY client_private_key;
ASSERT_TRUE(LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key"), ASSERT_TRUE(LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key"),
&client_private_key)); &client_private_key));
EXPECT_TRUE(RecordPrivateKey(ssl_config, client_private_key.get())); EXPECT_TRUE(RecordPrivateKey(ssl_config, client_private_key.get()));
......
...@@ -15,8 +15,6 @@ namespace net { ...@@ -15,8 +15,6 @@ namespace net {
namespace { namespace {
typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
// Increment the reference count of a given EVP_PKEY. This function // Increment the reference count of a given EVP_PKEY. This function
// is similar to EVP_PKEY_dup which is not available from the OpenSSL // is similar to EVP_PKEY_dup which is not available from the OpenSSL
// version used by Chromium at the moment. Its name is distinct to // version used by Chromium at the moment. Its name is distinct to
...@@ -31,14 +29,14 @@ EVP_PKEY* CopyEVP_PKEY(EVP_PKEY* key) { ...@@ -31,14 +29,14 @@ EVP_PKEY* CopyEVP_PKEY(EVP_PKEY* key) {
// Return the EVP_PKEY holding the public key of a given certificate. // Return the EVP_PKEY holding the public key of a given certificate.
// |cert| is a certificate. // |cert| is a certificate.
// Returns a scoped EVP_PKEY for it. // Returns a scoped EVP_PKEY for it.
ScopedEVP_PKEY GetOpenSSLPublicKey(const X509Certificate* cert) { crypto::ScopedEVP_PKEY GetOpenSSLPublicKey(const X509Certificate* cert) {
// X509_PUBKEY_get() increments the reference count of its result. // X509_PUBKEY_get() increments the reference count of its result.
// Unlike X509_get_X509_PUBKEY() which simply returns a direct pointer. // Unlike X509_get_X509_PUBKEY() which simply returns a direct pointer.
EVP_PKEY* pkey = EVP_PKEY* pkey =
X509_PUBKEY_get(X509_get_X509_PUBKEY(cert->os_cert_handle())); X509_PUBKEY_get(X509_get_X509_PUBKEY(cert->os_cert_handle()));
if (!pkey) if (!pkey)
LOG(ERROR) << "Can't extract private key from certificate!"; LOG(ERROR) << "Can't extract private key from certificate!";
return ScopedEVP_PKEY(pkey); return crypto::ScopedEVP_PKEY(pkey);
} }
} // namespace } // namespace
...@@ -101,7 +99,7 @@ bool OpenSSLClientKeyStore::RecordClientCertPrivateKey( ...@@ -101,7 +99,7 @@ bool OpenSSLClientKeyStore::RecordClientCertPrivateKey(
return false; return false;
// Get public key from certificate. // Get public key from certificate.
ScopedEVP_PKEY pub_key(GetOpenSSLPublicKey(client_cert)); crypto::ScopedEVP_PKEY pub_key(GetOpenSSLPublicKey(client_cert));
if (!pub_key.get()) if (!pub_key.get())
return false; return false;
...@@ -111,11 +109,11 @@ bool OpenSSLClientKeyStore::RecordClientCertPrivateKey( ...@@ -111,11 +109,11 @@ bool OpenSSLClientKeyStore::RecordClientCertPrivateKey(
bool OpenSSLClientKeyStore::FetchClientCertPrivateKey( bool OpenSSLClientKeyStore::FetchClientCertPrivateKey(
const X509Certificate* client_cert, const X509Certificate* client_cert,
ScopedEVP_PKEY* private_key) { crypto::ScopedEVP_PKEY* private_key) {
if (!client_cert) if (!client_cert)
return false; return false;
ScopedEVP_PKEY pub_key(GetOpenSSLPublicKey(client_cert)); crypto::ScopedEVP_PKEY pub_key(GetOpenSSLPublicKey(client_cert));
if (!pub_key.get()) if (!pub_key.get())
return false; return false;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "crypto/openssl_util.h" #include "crypto/openssl_util.h"
#include "crypto/scoped_openssl_types.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
namespace net { namespace net {
...@@ -31,14 +32,6 @@ class NET_EXPORT OpenSSLClientKeyStore { ...@@ -31,14 +32,6 @@ class NET_EXPORT OpenSSLClientKeyStore {
// Platforms must define this factory function as appropriate. // Platforms must define this factory function as appropriate.
static OpenSSLClientKeyStore* GetInstance(); static OpenSSLClientKeyStore* GetInstance();
struct EVP_PKEY_Deleter {
inline void operator()(EVP_PKEY* ptr) const {
EVP_PKEY_free(ptr);
}
};
typedef scoped_ptr<EVP_PKEY, EVP_PKEY_Deleter> ScopedEVP_PKEY;
// Record the association between a certificate and its // Record the association between a certificate and its
// private key. This method should be called _before_ // private key. This method should be called _before_
// FetchClientCertPrivateKey to ensure that the private key is returned // FetchClientCertPrivateKey to ensure that the private key is returned
...@@ -60,7 +53,7 @@ class NET_EXPORT OpenSSLClientKeyStore { ...@@ -60,7 +53,7 @@ class NET_EXPORT OpenSSLClientKeyStore {
// Returns true on success, false otherwise. This increments the reference // Returns true on success, false otherwise. This increments the reference
// count of the private key on success. // count of the private key on success.
bool FetchClientCertPrivateKey(const X509Certificate* cert, bool FetchClientCertPrivateKey(const X509Certificate* cert,
ScopedEVP_PKEY* private_key); crypto::ScopedEVP_PKEY* private_key);
// Flush all recorded keys. // Flush all recorded keys.
void Flush(); void Flush();
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "net/ssl/openssl_client_key_store.h" #include "net/ssl/openssl_client_key_store.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "crypto/scoped_openssl_types.h"
#include "net/base/test_data_directory.h" #include "net/base/test_data_directory.h"
#include "net/test/cert_test_util.h" #include "net/test/cert_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -13,8 +14,6 @@ namespace net { ...@@ -13,8 +14,6 @@ namespace net {
namespace { namespace {
typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
// Return the internal reference count of a given EVP_PKEY. // Return the internal reference count of a given EVP_PKEY.
int EVP_PKEY_get_refcount(EVP_PKEY* pkey) { int EVP_PKEY_get_refcount(EVP_PKEY* pkey) {
return pkey->references; return pkey->references;
...@@ -50,7 +49,7 @@ TEST_F(OpenSSLClientKeyStoreTest, Flush) { ...@@ -50,7 +49,7 @@ TEST_F(OpenSSLClientKeyStoreTest, Flush) {
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get()); ASSERT_TRUE(cert_1.get());
ScopedEVP_PKEY priv_key(EVP_PKEY_new()); crypto::ScopedEVP_PKEY priv_key(EVP_PKEY_new());
ASSERT_TRUE(priv_key.get()); ASSERT_TRUE(priv_key.get());
ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(), ASSERT_TRUE(store_->RecordClientCertPrivateKey(cert_1.get(),
...@@ -60,7 +59,7 @@ TEST_F(OpenSSLClientKeyStoreTest, Flush) { ...@@ -60,7 +59,7 @@ TEST_F(OpenSSLClientKeyStoreTest, Flush) {
// Retrieve the private key. This should fail because the store // Retrieve the private key. This should fail because the store
// was flushed. // was flushed.
ScopedEVP_PKEY pkey; crypto::ScopedEVP_PKEY pkey;
ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey));
ASSERT_FALSE(pkey.get()); ASSERT_FALSE(pkey.get());
} }
...@@ -76,7 +75,7 @@ TEST_F(OpenSSLClientKeyStoreTest, FetchEmptyPrivateKey) { ...@@ -76,7 +75,7 @@ TEST_F(OpenSSLClientKeyStoreTest, FetchEmptyPrivateKey) {
// Retrieve the private key now. This should fail because it was // Retrieve the private key now. This should fail because it was
// never recorded in the store. // never recorded in the store.
ScopedEVP_PKEY pkey; crypto::ScopedEVP_PKEY pkey;
ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey)); ASSERT_FALSE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey));
ASSERT_FALSE(pkey.get()); ASSERT_FALSE(pkey.get());
} }
...@@ -94,7 +93,7 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchPrivateKey) { ...@@ -94,7 +93,7 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchPrivateKey) {
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem")); ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get()); ASSERT_TRUE(cert_1.get());
ScopedEVP_PKEY priv_key(EVP_PKEY_new()); crypto::ScopedEVP_PKEY priv_key(EVP_PKEY_new());
ASSERT_TRUE(priv_key.get()); ASSERT_TRUE(priv_key.get());
ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key.get())); ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key.get()));
...@@ -111,7 +110,7 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchPrivateKey) { ...@@ -111,7 +110,7 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchPrivateKey) {
// Retrieve the private key. This should increment the private key's // Retrieve the private key. This should increment the private key's
// reference count. // reference count.
ScopedEVP_PKEY pkey2; crypto::ScopedEVP_PKEY pkey2;
ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey2)); ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), &pkey2));
ASSERT_EQ(pkey2.get(), priv_key.get()); ASSERT_EQ(pkey2.get(), priv_key.get());
ASSERT_EQ(3, EVP_PKEY_get_refcount(priv_key.get())); ASSERT_EQ(3, EVP_PKEY_get_refcount(priv_key.get()));
...@@ -132,11 +131,11 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchTwoPrivateKeys) { ...@@ -132,11 +131,11 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchTwoPrivateKeys) {
ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem")); ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem"));
ASSERT_TRUE(cert_2.get()); ASSERT_TRUE(cert_2.get());
ScopedEVP_PKEY priv_key1(EVP_PKEY_new()); crypto::ScopedEVP_PKEY priv_key1(EVP_PKEY_new());
ASSERT_TRUE(priv_key1.get()); ASSERT_TRUE(priv_key1.get());
ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1.get())); ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key1.get()));
ScopedEVP_PKEY priv_key2(EVP_PKEY_new()); crypto::ScopedEVP_PKEY priv_key2(EVP_PKEY_new());
ASSERT_TRUE(priv_key2.get()); ASSERT_TRUE(priv_key2.get());
ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2.get())); ASSERT_EQ(1, EVP_PKEY_get_refcount(priv_key2.get()));
...@@ -153,10 +152,10 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchTwoPrivateKeys) { ...@@ -153,10 +152,10 @@ TEST_F(OpenSSLClientKeyStoreTest, RecordAndFetchTwoPrivateKeys) {
// Retrieve the private key now. This shall succeed and increment // Retrieve the private key now. This shall succeed and increment
// the private key's reference count. // the private key's reference count.
ScopedEVP_PKEY fetch_key1; crypto::ScopedEVP_PKEY fetch_key1;
ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(), ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_1.get(),
&fetch_key1)); &fetch_key1));
ScopedEVP_PKEY fetch_key2; crypto::ScopedEVP_PKEY fetch_key2;
ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_2.get(), ASSERT_TRUE(store_->FetchClientCertPrivateKey(cert_2.get(),
&fetch_key2)); &fetch_key2));
EXPECT_TRUE(fetch_key1.get()); EXPECT_TRUE(fetch_key1.get());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment