Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ | 5 #ifndef NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ |
| 6 #define NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ | 6 #define NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ |
| 7 | 7 |
| 8 #include <openssl/evp.h> | |
| 9 | |
| 10 #include <vector> | |
| 11 | |
| 8 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 9 | 13 #include "base/memory/scoped_ptr.h" |
| 10 // Avoid including <openssl/evp.h> here. | 14 #include "base/synchronization/lock.h" |
| 11 typedef struct evp_pkey_st EVP_PKEY; | 15 #include "crypto/openssl_util.h" |
| 16 #include "net/base/net_export.h" | |
| 12 | 17 |
| 13 class GURL; | 18 class GURL; |
| 14 | 19 |
| 15 namespace net { | 20 namespace net { |
| 16 | 21 |
| 17 // Defines an abstract store for private keys; the OpenSSL library does not | 22 class X509Certificate; |
| 18 // provide this service so it is left to individual platforms to provide it. | 23 |
| 19 // | 24 // Defines an interface to the store for private keys. The OpenSSL library |
| 20 // The contract is that the private key will be stored in an appropriate secure | 25 // does not provide this service so it is left to individual platforms to |
| 21 // system location, and be available to the SSLClientSocketOpenSSL when using a | 26 // provide it. |
| 22 // client certificate created against the associated public key for client | 27 class NET_EXPORT OpenSSLPrivateKeyStore { |
| 23 // authentication. | |
| 24 class OpenSSLPrivateKeyStore { | |
| 25 public: | 28 public: |
| 26 // Platforms must define this factory function as appropriate. | 29 // Platforms must define this factory function as appropriate. |
| 27 static OpenSSLPrivateKeyStore* GetInstance(); | 30 static OpenSSLPrivateKeyStore* GetInstance(); |
| 28 | 31 |
| 29 virtual ~OpenSSLPrivateKeyStore() {} | 32 // Helper class to simplify the definition of custom scoped_ptr<> |
| 33 // types which is a specific function for destruction, instead of | |
| 34 // delete, delete[] or free(). Usage is: | |
| 35 // | |
| 36 // typedef ScopedPtrFunc<T, T_free>::type ScopedT; | |
| 37 // | |
| 38 // Which defines ScopedT as a scoped_ptr<T> which will call T_free() | |
| 39 // at destruction time, instead of. | |
| 40 // | |
| 41 // This is considerably simpler than the default which requires defining | |
| 42 // a custom structure for each <T, T_free> pair, as in: | |
| 43 // | |
| 44 // struct T_Deleter { | |
| 45 // inline void operator()(T* ptr) { | |
| 46 // T_free(ptr); | |
| 47 // } | |
| 48 // }; | |
| 49 // typedef scoped_ptr<T, T_Deleter> ScopedT; | |
| 50 // | |
| 51 template <typename T, void (deleter)(T*)> | |
| 52 struct ScopedPtrFunc { | |
| 53 inline void operator()(T* ptr) const { | |
| 54 deleter(ptr); | |
| 55 } | |
| 56 typedef scoped_ptr<T, ScopedPtrFunc<T, deleter> > type; | |
| 57 }; | |
|
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
| |
| 30 | 58 |
| 31 // Called to store a private key generated via <keygen> while visiting |url|. | 59 typedef ScopedPtrFunc<EVP_PKEY, EVP_PKEY_free>::type ScopedEVP_PKEY; |
| 32 // Does not takes ownership of |pkey|, the caller reamins responsible to | 60 |
| 33 // EVP_PKEY_free it. (Internally, a copy maybe made or the reference count | 61 // Called to store a private/public key pair, generated via <keygen> while |
| 34 // incremented). | 62 // visiting |url|, to an appropriate secure system location. |
| 63 // Increments |pkey|'s reference count, so the caller is still responsible | |
| 64 // for calling EVP_PKEY_free on it. | |
| 65 // |url| is the corresponding server URL. | |
| 66 // |pkey| is the key pair handle. | |
| 35 // Returns false if an error occurred whilst attempting to store the key. | 67 // Returns false if an error occurred whilst attempting to store the key. |
| 36 virtual bool StorePrivateKey(const GURL& url, EVP_PKEY* pkey) = 0; | 68 virtual bool StoreKeyPair(const GURL& url, EVP_PKEY* pkey) = 0; |
| 37 | 69 |
| 38 // Given a |public_key| part returns the corresponding private key, or NULL | 70 // Record the association between a certificate and its private key. |
| 39 // if no key found. Does NOT return ownership. | 71 // This method should be called _before_ FetchPrivateKey to ensure that |
| 40 virtual EVP_PKEY* FetchPrivateKey(EVP_PKEY* public_key) = 0; | 72 // the private key is returned when it is called later. |
| 73 // |cert| is a handle to a certificate object. | |
| 74 // |private_key| is an OpenSSL EVP_PKEY that corresponds to the | |
| 75 // certificate's private key. | |
| 76 // Returns false if an error occured. | |
| 77 // This function does not take ownership of the private_key, but may | |
| 78 // increment its internal reference count. | |
| 79 virtual bool RecordClientCertPrivateKey(const X509Certificate* cert, | |
| 80 EVP_PKEY* private_key); | |
| 81 | |
| 82 // Given a certificate's |public_key|, return the corresponding private | |
| 83 // key that has been recorded previously by RecordClientCertPrivateKey(). | |
| 84 // |cert| is a client certificate. | |
| 85 // |*private_key| will be reset to its matching private key on success. | |
| 86 // Returns true on success, false otherwise. | |
| 87 virtual bool FetchClientCertPrivateKey(const X509Certificate* cert, | |
| 88 ScopedEVP_PKEY* private_key); | |
| 89 | |
| 90 // Flush all recorded keys. Used only during testing. | |
| 91 virtual void Flush(); | |
| 41 | 92 |
| 42 protected: | 93 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
| |
| 43 OpenSSLPrivateKeyStore() {} | 94 OpenSSLPrivateKeyStore(); |
| 95 | |
| 96 virtual ~OpenSSLPrivateKeyStore(); | |
| 97 | |
| 98 // KeyPair is an internal class used to hold a pair of private / public | |
| 99 // EVP_PKEY objects, with appropriate ownership. | |
| 100 class KeyPair { | |
| 101 public: | |
| 102 explicit KeyPair(EVP_PKEY* pub_key, EVP_PKEY* priv_key); | |
| 103 KeyPair(const KeyPair& other); | |
| 104 ~KeyPair(); | |
| 105 | |
| 106 EVP_PKEY* public_key_; | |
| 107 EVP_PKEY* private_key_; | |
| 108 | |
| 109 private: | |
| 110 KeyPair(); // intentionally not implemented. | |
| 111 }; | |
| 112 | |
| 113 // Adds a given public/private key pair. | |
| 114 // |pub_key| and |private_key| can point to the same object. | |
| 115 // This increments the reference count on both objects, caller | |
| 116 // must still call EVP_PKEY_free on them. | |
| 117 void AddKeyPair(EVP_PKEY* pub_key, EVP_PKEY* private_key); | |
| 118 | |
| 119 // Returns the index of the keypair for |public_key|. or -1 if not found. | |
| 120 // if not found. Assumes that |lock_| is held. | |
| 121 int FindKeyPairIndexLocked(EVP_PKEY* public_key); | |
| 122 | |
| 123 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
| |
| 124 std::vector<KeyPair> pairs_; | |
| 44 | 125 |
| 45 private: | 126 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore); | 127 DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore); |
| 47 }; | 128 }; |
| 48 | 129 |
| 49 } // namespace net | 130 } // namespace net |
| 50 | 131 |
| 51 #endif // NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ | 132 #endif // NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ |
| OLD | NEW |