Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(593)

Side by Side Diff: net/base/openssl_private_key_store.cc

Issue 12220104: Wire up SSL client authentication for OpenSSL/Android through the net/ stack (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "net/base/openssl_private_key_store.h"
6
7 #include <openssl/evp.h>
8 #include <openssl/x509.h>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/x509_certificate.h"
12
13 namespace net {
14
15 namespace {
16
17 typedef OpenSSLPrivateKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
18
19 // Increment the reference count of a given EVP_PKEY. This function
20 // is not available from the OpenSSL version used by Chromium at the
21 // moment.
22 EVP_PKEY* EVP_PKEY_dup(EVP_PKEY* key) {
23 if (key != NULL)
24 CRYPTO_add(&key->references, 1, CRYPTO_LOCK_EVP_PKEY);
25 return key;
26 }
27
28 // Return the EVP_PKEY holding the public key of a given certificate.
29 // |cert| is a certificate.
30 // Returns a scoped EVP_PKEY for it.
31 ScopedEVP_PKEY GetOpenSSLPublicKey(const X509Certificate* cert) {
32 // X509_PUBKEY_get() increments the reference count of its result.
33 // Unlike X509_get_X509_PUBKEY() which simply returns a direct pointer.
34 EVP_PKEY* pkey =
35 X509_PUBKEY_get(X509_get_X509_PUBKEY(cert->os_cert_handle()));
36 if (!pkey)
37 LOG(ERROR) << "Can't extract private key from certificate!";
38 return ScopedEVP_PKEY(pkey);
39 }
40
41 } // namespace
42
43 OpenSSLPrivateKeyStore::OpenSSLPrivateKeyStore() {
44 }
45
46 OpenSSLPrivateKeyStore::~OpenSSLPrivateKeyStore() {
47 }
48
49 OpenSSLPrivateKeyStore::KeyPair::KeyPair(EVP_PKEY* pub_key,
50 EVP_PKEY* priv_key) {
51 public_key_ = EVP_PKEY_dup(pub_key);
52 private_key_ = EVP_PKEY_dup(priv_key);
53 }
54
55 OpenSSLPrivateKeyStore::KeyPair::~KeyPair() {
56 EVP_PKEY_free(public_key_);
57 EVP_PKEY_free(private_key_);
58 }
59
60 OpenSSLPrivateKeyStore::KeyPair::KeyPair(const KeyPair& other) {
61 public_key_ = EVP_PKEY_dup(other.public_key_);
62 private_key_ = EVP_PKEY_dup(other.private_key_);
63 }
64
65 int OpenSSLPrivateKeyStore::FindKeyPairIndex(EVP_PKEY* public_key) {
66 if (!public_key)
67 return -1;
68 for (size_t n = 0; n < pairs_.size(); ++n) {
69 if (EVP_PKEY_cmp(pairs_[n].public_key_, public_key) == 1)
70 return static_cast<int>(n);
71 }
72 return -1;
73 }
74
75 void OpenSSLPrivateKeyStore::AddKeyPair(EVP_PKEY* pub_key,
76 EVP_PKEY* private_key) {
77 int index = FindKeyPairIndex(pub_key);
78 if (index < 0)
79 pairs_.push_back(KeyPair(pub_key, private_key));
80 }
81
82 // Common code for OpenSSLPrivateKeyStore. Shared by all OpenSSL-based
83 // builds.
84 bool OpenSSLPrivateKeyStore::RecordClientCertPrivateKey(
85 const X509Certificate* client_cert, EVP_PKEY* private_key) {
86 // Sanity check.
87 if (client_cert == NULL || private_key == NULL)
88 return false;
89
90 // Get public key from certificate.
91 ScopedEVP_PKEY pub_key(GetOpenSSLPublicKey(client_cert));
92 if (!pub_key.get())
93 return false;
94
95 AddKeyPair(pub_key.get(), private_key);
96 return true;
97 }
98
99 bool OpenSSLPrivateKeyStore::FetchClientCertPrivateKey(
100 const X509Certificate* client_cert,
101 ScopedEVP_PKEY* private_key) {
102 if (!client_cert)
103 return false;
104
105 ScopedEVP_PKEY pub_key(GetOpenSSLPublicKey(client_cert));
106 if (!pub_key.get())
107 return false;
108
109 int index = FindKeyPairIndex(pub_key.get());
110 if (index < 0)
111 return false;
112
113 ScopedEVP_PKEY result(EVP_PKEY_dup(pairs_[index].private_key_));
114 private_key->swap(result);
115 return true;
116 }
117
118 void OpenSSLPrivateKeyStore::Flush() {
119 pairs_.clear();
120 }
121
122 } // namespace net
123
124
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698