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

Unified Diff: net/base/openssl_private_key_store.h

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: address recent nits 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 side-by-side diff with in-line comments
Download patch
Index: net/base/openssl_private_key_store.h
diff --git a/net/base/openssl_private_key_store.h b/net/base/openssl_private_key_store.h
index edd54f38aa150159336a088bbc74475106a0c211..42fed05c99ed064c5e0e4cf0cfca1fefe7f61958 100644
--- a/net/base/openssl_private_key_store.h
+++ b/net/base/openssl_private_key_store.h
@@ -5,44 +5,104 @@
#ifndef NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_
#define NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_
-#include "base/basictypes.h"
+#include <openssl/evp.h>
+
+#include <vector>
-// Avoid including <openssl/evp.h> here.
-typedef struct evp_pkey_st EVP_PKEY;
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/synchronization/lock.h"
+#include "crypto/openssl_util.h"
+#include "net/base/net_export.h"
class GURL;
namespace net {
-// Defines an abstract store for private keys; the OpenSSL library does not
-// provide this service so it is left to individual platforms to provide it.
-//
-// The contract is that the private key will be stored in an appropriate secure
-// system location, and be available to the SSLClientSocketOpenSSL when using a
-// client certificate created against the associated public key for client
-// authentication.
-class OpenSSLPrivateKeyStore {
+class X509Certificate;
+
+// Defines an interface to the store for private keys. The OpenSSL library
+// does not provide this service so it is left to individual platforms to
+// provide it.
Ryan Sleevi 2013/02/15 23:53:26 nit: delete the "The OpenSSL library ..." comment,
digit1 2013/02/25 14:26:22 Done.
+class NET_EXPORT OpenSSLPrivateKeyStore {
public:
// Platforms must define this factory function as appropriate.
static OpenSSLPrivateKeyStore* GetInstance();
- virtual ~OpenSSLPrivateKeyStore() {}
+ struct EVP_PKEY_Deleter {
+ inline void operator()(EVP_PKEY* ptr) const {
+ EVP_PKEY_free(ptr);
+ }
+ };
- // Called to store a private key generated via <keygen> while visiting |url|.
- // Does not takes ownership of |pkey|, the caller reamins responsible to
- // EVP_PKEY_free it. (Internally, a copy maybe made or the reference count
- // incremented).
+ typedef scoped_ptr<EVP_PKEY, EVP_PKEY_Deleter> ScopedEVP_PKEY;
+
+ // Called to store a private/public key pair, generated via <keygen> while
+ // visiting |url|, to an appropriate secure system location.
Ryan Sleevi 2013/02/15 23:53:26 Isn't the "appropriate secure system location" mis
digit1 2013/02/25 14:26:22 It must be stored on the system, this is specifica
+ // Increments |pkey|'s reference count, so the caller is still responsible
+ // for calling EVP_PKEY_free on it.
+ // |url| is the corresponding server URL.
+ // |pkey| is the key pair handle.
// Returns false if an error occurred whilst attempting to store the key.
- virtual bool StorePrivateKey(const GURL& url, EVP_PKEY* pkey) = 0;
+ virtual bool StoreKeyPair(const GURL& url, EVP_PKEY* pkey) = 0;
+
+ // Record the association between a certificate and its private key.
+ // This method should be called _before_ FetchPrivateKey to ensure that
+ // the private key is returned when it is called later.
Ryan Sleevi 2013/02/15 23:53:26 Are you sure you want this second sentance to be p
+ // |cert| is a handle to a certificate object.
+ // |private_key| is an OpenSSL EVP_PKEY that corresponds to the
+ // certificate's private key.
+ // Returns false if an error occured.
+ // This function does not take ownership of the private_key, but may
+ // increment its internal reference count.
+ virtual bool RecordClientCertPrivateKey(const X509Certificate* cert,
+ EVP_PKEY* private_key);
+
+ // Given a certificate's |public_key|, return the corresponding private
+ // key that has been recorded previously by RecordClientCertPrivateKey().
+ // |cert| is a client certificate.
+ // |*private_key| will be reset to its matching private key on success.
+ // Returns true on success, false otherwise.
+ virtual bool FetchClientCertPrivateKey(const X509Certificate* cert,
+ ScopedEVP_PKEY* private_key);
- // Given a |public_key| part returns the corresponding private key, or NULL
- // if no key found. Does NOT return ownership.
- virtual EVP_PKEY* FetchPrivateKey(EVP_PKEY* public_key) = 0;
+ // Flush all recorded keys. Used only during testing.
+ virtual void Flush();
protected:
- OpenSSLPrivateKeyStore() {}
+ OpenSSLPrivateKeyStore();
+
+ virtual ~OpenSSLPrivateKeyStore();
+
+ // Adds a given public/private key pair.
+ // |pub_key| and |private_key| can point to the same object.
+ // This increments the reference count on both objects, caller
+ // must still call EVP_PKEY_free on them.
+ void AddKeyPair(EVP_PKEY* pub_key, EVP_PKEY* private_key);
private:
+ // KeyPair is an internal class used to hold a pair of private / public
+ // EVP_PKEY objects, with appropriate ownership.
+ class KeyPair {
+ public:
+ explicit KeyPair(EVP_PKEY* pub_key, EVP_PKEY* priv_key);
+ KeyPair(const KeyPair& other);
+ ~KeyPair();
+
+ EVP_PKEY* public_key_;
+ EVP_PKEY* private_key_;
+
+ private:
+ KeyPair(); // intentionally not implemented.
+ };
+
+ // Returns the index of the keypair for |public_key|. or -1 if not found.
ppi 2013/02/15 19:54:46 This comment seems to be in the course of non atom
digit1 2013/02/25 14:26:22 Yep, I've fixed that, thanks for pointing this out
+ // if not found. Assumes that |lock_| is held.
+ int FindKeyPairIndexLocked(EVP_PKEY* public_key);
+
+ base::Lock lock_;
+ std::vector<KeyPair> pairs_;
+
DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore);
};

Powered by Google App Engine
This is Rietveld 408576698