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..b1739092356be17a558b319924fbd6ca339e7c6c 100644 |
--- a/net/base/openssl_private_key_store.h |
+++ b/net/base/openssl_private_key_store.h |
@@ -5,42 +5,123 @@ |
#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. |
+class NET_EXPORT OpenSSLPrivateKeyStore { |
public: |
// Platforms must define this factory function as appropriate. |
static OpenSSLPrivateKeyStore* GetInstance(); |
- virtual ~OpenSSLPrivateKeyStore() {} |
+ // Helper class to simplify the definition of custom scoped_ptr<> |
+ // types which is a specific function for destruction, instead of |
+ // delete, delete[] or free(). Usage is: |
+ // |
+ // typedef ScopedPtrFunc<T, T_free>::type ScopedT; |
+ // |
+ // Which defines ScopedT as a scoped_ptr<T> which will call T_free() |
+ // at destruction time, instead of. |
+ // |
+ // This is considerably simpler than the default which requires defining |
+ // a custom structure for each <T, T_free> pair, as in: |
+ // |
+ // struct T_Deleter { |
+ // inline void operator()(T* ptr) { |
+ // T_free(ptr); |
+ // } |
+ // }; |
+ // typedef scoped_ptr<T, T_Deleter> ScopedT; |
+ // |
+ template <typename T, void (deleter)(T*)> |
+ struct ScopedPtrFunc { |
+ inline void operator()(T* ptr) const { |
+ deleter(ptr); |
+ } |
+ typedef scoped_ptr<T, ScopedPtrFunc<T, deleter> > type; |
+ }; |
Ryan Sleevi
2013/02/13 23:25:55
I would have trouble stamping this change. It feel
digit1
2013/02/14 06:23:50
ok, I don't think it's reasonable to depend on cha
Ryan Sleevi
2013/02/14 07:15:00
I'm not sure I understand your point about "not de
digit1
2013/02/14 08:24:39
My feeling is that changes to core classes and def
|
- // 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 ScopedPtrFunc<EVP_PKEY, EVP_PKEY_free>::type ScopedEVP_PKEY; |
+ |
+ // Called to store a private/public key pair, generated via <keygen> while |
+ // visiting |url|, to an appropriate secure system location. |
+ // 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. |
+ // |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: |
Ryan Sleevi
2013/02/13 23:25:55
Please examine the use of protected here and ensur
digit1
2013/02/14 06:23:50
The class must be derived by OpenSSLMemoryPrivateK
Ryan Sleevi
2013/02/14 07:15:00
I'm asking that you carefully evaluate what needs
digit1
2013/02/14 08:24:39
That's exactly what I've already done, so I think
|
- OpenSSLPrivateKeyStore() {} |
+ OpenSSLPrivateKeyStore(); |
+ |
+ virtual ~OpenSSLPrivateKeyStore(); |
+ |
+ // 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. |
+ }; |
+ |
+ // 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); |
+ |
+ // Returns the index of the keypair for |public_key|. or -1 if not found. |
+ // if not found. Assumes that |lock_| is held. |
+ int FindKeyPairIndexLocked(EVP_PKEY* public_key); |
+ |
+ base::Lock lock_; |
Ryan Sleevi
2013/02/14 07:15:00
Please make sure to fully comment/explain the thre
digit1
2013/02/14 08:24:39
The worker threads are mentioned in the Android Ge
|
+ std::vector<KeyPair> pairs_; |
private: |
DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore); |