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 // OpenSSLPrivateKeyStore provides an interface for storing |
| 20 // The contract is that the private key will be stored in an appropriate secure | 25 // or locating the associated private key for a given public |
| 21 // system location, and be available to the SSLClientSocketOpenSSL when using a | 26 // key. Because OpenSSL does not provide any notion of key |
| 22 // client certificate created against the associated public key for client | 27 // or certificate storage, unlike other platforms, this class |
| 23 // authentication. | 28 // provides a basic interface to implement these services. |
| 24 class OpenSSLPrivateKeyStore { | 29 // IMPORTANT: This is NET_EXPORT to allow client code to call |
| 30 // GetInstance() and RecordClientCertPrivateKey(). All other methods | |
| 31 // are internal to net/. | |
|
Ryan Sleevi
2013/02/25 19:51:07
style: You can just NET_EXPORT those methods then
digit1
2013/02/26 11:03:13
Good to know, thanks, I've done just that.
| |
| 32 // This class shall only be used from the network thread. | |
| 33 class NET_EXPORT OpenSSLPrivateKeyStore { | |
| 25 public: | 34 public: |
| 26 // Platforms must define this factory function as appropriate. | 35 // Platforms must define this factory function as appropriate. |
| 27 static OpenSSLPrivateKeyStore* GetInstance(); | 36 static OpenSSLPrivateKeyStore* GetInstance(); |
| 28 | 37 |
| 29 virtual ~OpenSSLPrivateKeyStore() {} | 38 struct EVP_PKEY_Deleter { |
| 39 inline void operator()(EVP_PKEY* ptr) const { | |
| 40 EVP_PKEY_free(ptr); | |
| 41 } | |
| 42 }; | |
| 30 | 43 |
| 31 // Called to store a private key generated via <keygen> while visiting |url|. | 44 typedef scoped_ptr<EVP_PKEY, EVP_PKEY_Deleter> ScopedEVP_PKEY; |
| 32 // Does not takes ownership of |pkey|, the caller reamins responsible to | 45 |
| 33 // EVP_PKEY_free it. (Internally, a copy maybe made or the reference count | 46 // Called to permanently store a private/public key pair, generated |
| 34 // incremented). | 47 // via <keygen> while visiting |url|, to an appropriate system |
| 48 // location. Increments |pkey|'s reference count, so the caller is still | |
| 49 // responsible for calling EVP_PKEY_free on it. Note that this has | |
| 50 // nothing to do with the methods RecordClientCertPrivateKey() and | |
| 51 // FetchClientCertPrivateKey() below. | |
| 52 // |url| is the corresponding server URL. | |
| 53 // |pkey| is the key pair handle. | |
| 35 // Returns false if an error occurred whilst attempting to store the key. | 54 // Returns false if an error occurred whilst attempting to store the key. |
| 36 virtual bool StorePrivateKey(const GURL& url, EVP_PKEY* pkey) = 0; | 55 virtual bool StoreKeyPair(const GURL& url, EVP_PKEY* pkey) = 0; |
|
Ryan Sleevi
2013/02/25 19:51:07
DESIGN: I'm concerned about the class here, if onl
digit1
2013/02/26 11:03:13
To address this, I've separated both usage in my l
| |
| 37 | 56 |
| 38 // Given a |public_key| part returns the corresponding private key, or NULL | 57 // Record the association between a certificate and its |
| 39 // if no key found. Does NOT return ownership. | 58 // private key. This method should be called _before_ |
| 40 virtual EVP_PKEY* FetchPrivateKey(EVP_PKEY* public_key) = 0; | 59 // FetchClientCertPrivateKey to ensure that the private key is returned |
| 60 // when it is called later. The association is recorded in memory | |
| 61 // exclusively. | |
| 62 // |cert| is a handle to a certificate object. | |
| 63 // |private_key| is an OpenSSL EVP_PKEY that corresponds to the | |
| 64 // certificate's private key. | |
| 65 // Returns false if an error occured. | |
| 66 // This function does not take ownership of the private_key, but may | |
| 67 // increment its internal reference count. | |
| 68 virtual bool RecordClientCertPrivateKey(const X509Certificate* cert, | |
| 69 EVP_PKEY* private_key); | |
|
Ryan Sleevi
2013/02/25 19:51:07
These (RecordClientCertPrivateKey/FetchClientCertP
digit1
2013/02/26 11:03:13
Indeed, I've removed the virtual.
| |
| 70 | |
| 71 // Given a certificate's |public_key|, return the corresponding private | |
| 72 // key that has been recorded previously by RecordClientCertPrivateKey(). | |
| 73 // |cert| is a client certificate. | |
| 74 // |*private_key| will be reset to its matching private key on success. | |
| 75 // Returns true on success, false otherwise. This increments the reference | |
| 76 // count of the private key on success. | |
| 77 virtual bool FetchClientCertPrivateKey(const X509Certificate* cert, | |
| 78 ScopedEVP_PKEY* private_key); | |
| 79 | |
| 80 // Flush all recorded keys. Used only during testing. | |
| 81 virtual void Flush(); | |
| 41 | 82 |
| 42 protected: | 83 protected: |
| 43 OpenSSLPrivateKeyStore() {} | 84 OpenSSLPrivateKeyStore(); |
| 85 | |
| 86 virtual ~OpenSSLPrivateKeyStore(); | |
| 87 | |
| 88 // Adds a given public/private key pair. | |
| 89 // |pub_key| and |private_key| can point to the same object. | |
| 90 // This increments the reference count on both objects, caller | |
| 91 // must still call EVP_PKEY_free on them. | |
| 92 void AddKeyPair(EVP_PKEY* pub_key, EVP_PKEY* private_key); | |
| 44 | 93 |
| 45 private: | 94 private: |
| 95 // KeyPair is an internal class used to hold a pair of private / public | |
| 96 // EVP_PKEY objects, with appropriate ownership. | |
| 97 class KeyPair { | |
| 98 public: | |
| 99 explicit KeyPair(EVP_PKEY* pub_key, EVP_PKEY* priv_key); | |
| 100 KeyPair(const KeyPair& other); | |
| 101 ~KeyPair(); | |
| 102 | |
| 103 EVP_PKEY* public_key_; | |
| 104 EVP_PKEY* private_key_; | |
| 105 | |
| 106 private: | |
| 107 KeyPair(); // intentionally not implemented. | |
| 108 }; | |
| 109 | |
| 110 // Returns the index of the keypair for |public_key|. or -1 if not found. | |
| 111 int FindKeyPairIndex(EVP_PKEY* public_key); | |
| 112 | |
| 113 std::vector<KeyPair> pairs_; | |
| 114 | |
| 46 DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore); | 115 DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore); |
| 47 }; | 116 }; |
| 48 | 117 |
| 49 } // namespace net | 118 } // namespace net |
| 50 | 119 |
| 51 #endif // NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ | 120 #endif // NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ |
| OLD | NEW |