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. |
Ryan Sleevi
2013/02/15 23:53:26
nit: delete the "The OpenSSL library ..." comment,
digit1
2013/02/25 14:26:22
Done.
| |
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 struct EVP_PKEY_Deleter { |
33 inline void operator()(EVP_PKEY* ptr) const { | |
34 EVP_PKEY_free(ptr); | |
35 } | |
36 }; | |
30 | 37 |
31 // Called to store a private key generated via <keygen> while visiting |url|. | 38 typedef scoped_ptr<EVP_PKEY, EVP_PKEY_Deleter> ScopedEVP_PKEY; |
32 // Does not takes ownership of |pkey|, the caller reamins responsible to | 39 |
33 // EVP_PKEY_free it. (Internally, a copy maybe made or the reference count | 40 // Called to store a private/public key pair, generated via <keygen> while |
34 // incremented). | 41 // 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
| |
42 // Increments |pkey|'s reference count, so the caller is still responsible | |
43 // for calling EVP_PKEY_free on it. | |
44 // |url| is the corresponding server URL. | |
45 // |pkey| is the key pair handle. | |
35 // Returns false if an error occurred whilst attempting to store the key. | 46 // Returns false if an error occurred whilst attempting to store the key. |
36 virtual bool StorePrivateKey(const GURL& url, EVP_PKEY* pkey) = 0; | 47 virtual bool StoreKeyPair(const GURL& url, EVP_PKEY* pkey) = 0; |
37 | 48 |
38 // Given a |public_key| part returns the corresponding private key, or NULL | 49 // Record the association between a certificate and its private key. |
39 // if no key found. Does NOT return ownership. | 50 // This method should be called _before_ FetchPrivateKey to ensure that |
40 virtual EVP_PKEY* FetchPrivateKey(EVP_PKEY* public_key) = 0; | 51 // 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
| |
52 // |cert| is a handle to a certificate object. | |
53 // |private_key| is an OpenSSL EVP_PKEY that corresponds to the | |
54 // certificate's private key. | |
55 // Returns false if an error occured. | |
56 // This function does not take ownership of the private_key, but may | |
57 // increment its internal reference count. | |
58 virtual bool RecordClientCertPrivateKey(const X509Certificate* cert, | |
59 EVP_PKEY* private_key); | |
60 | |
61 // Given a certificate's |public_key|, return the corresponding private | |
62 // key that has been recorded previously by RecordClientCertPrivateKey(). | |
63 // |cert| is a client certificate. | |
64 // |*private_key| will be reset to its matching private key on success. | |
65 // Returns true on success, false otherwise. | |
66 virtual bool FetchClientCertPrivateKey(const X509Certificate* cert, | |
67 ScopedEVP_PKEY* private_key); | |
68 | |
69 // Flush all recorded keys. Used only during testing. | |
70 virtual void Flush(); | |
41 | 71 |
42 protected: | 72 protected: |
43 OpenSSLPrivateKeyStore() {} | 73 OpenSSLPrivateKeyStore(); |
74 | |
75 virtual ~OpenSSLPrivateKeyStore(); | |
76 | |
77 // Adds a given public/private key pair. | |
78 // |pub_key| and |private_key| can point to the same object. | |
79 // This increments the reference count on both objects, caller | |
80 // must still call EVP_PKEY_free on them. | |
81 void AddKeyPair(EVP_PKEY* pub_key, EVP_PKEY* private_key); | |
44 | 82 |
45 private: | 83 private: |
84 // KeyPair is an internal class used to hold a pair of private / public | |
85 // EVP_PKEY objects, with appropriate ownership. | |
86 class KeyPair { | |
87 public: | |
88 explicit KeyPair(EVP_PKEY* pub_key, EVP_PKEY* priv_key); | |
89 KeyPair(const KeyPair& other); | |
90 ~KeyPair(); | |
91 | |
92 EVP_PKEY* public_key_; | |
93 EVP_PKEY* private_key_; | |
94 | |
95 private: | |
96 KeyPair(); // intentionally not implemented. | |
97 }; | |
98 | |
99 // 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
| |
100 // if not found. Assumes that |lock_| is held. | |
101 int FindKeyPairIndexLocked(EVP_PKEY* public_key); | |
102 | |
103 base::Lock lock_; | |
104 std::vector<KeyPair> pairs_; | |
105 | |
46 DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore); | 106 DISALLOW_COPY_AND_ASSIGN(OpenSSLPrivateKeyStore); |
47 }; | 107 }; |
48 | 108 |
49 } // namespace net | 109 } // namespace net |
50 | 110 |
51 #endif // NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ | 111 #endif // NET_BASE_OPENSSL_PRIVATE_KEY_STORE_H_ |
OLD | NEW |