OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_SSL_OPENSSL_CLIENT_KEY_STORE_H_ | 5 #ifndef NET_SSL_OPENSSL_CLIENT_KEY_STORE_H_ |
6 #define NET_SSL_OPENSSL_CLIENT_KEY_STORE_H_ | 6 #define NET_SSL_OPENSSL_CLIENT_KEY_STORE_H_ |
7 | 7 |
8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "crypto/openssl_util.h" | 15 #include "crypto/openssl_util.h" |
| 16 #include "crypto/scoped_openssl_types.h" |
16 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
20 class X509Certificate; | 21 class X509Certificate; |
21 | 22 |
22 // OpenSSLClientKeyStore implements an in-memory store for client | 23 // OpenSSLClientKeyStore implements an in-memory store for client |
23 // certificate private keys, because the platforms where OpenSSL is | 24 // certificate private keys, because the platforms where OpenSSL is |
24 // used do not provide a way to retrieve the private key of a known | 25 // used do not provide a way to retrieve the private key of a known |
25 // certificate. | 26 // certificate. |
26 // | 27 // |
27 // This class is not thread-safe and should only be used from the network | 28 // This class is not thread-safe and should only be used from the network |
28 // thread. | 29 // thread. |
29 class NET_EXPORT OpenSSLClientKeyStore { | 30 class NET_EXPORT OpenSSLClientKeyStore { |
30 public: | 31 public: |
31 // Platforms must define this factory function as appropriate. | 32 // Platforms must define this factory function as appropriate. |
32 static OpenSSLClientKeyStore* GetInstance(); | 33 static OpenSSLClientKeyStore* GetInstance(); |
33 | 34 |
34 struct EVP_PKEY_Deleter { | |
35 inline void operator()(EVP_PKEY* ptr) const { | |
36 EVP_PKEY_free(ptr); | |
37 } | |
38 }; | |
39 | |
40 typedef scoped_ptr<EVP_PKEY, EVP_PKEY_Deleter> ScopedEVP_PKEY; | |
41 | |
42 // Record the association between a certificate and its | 35 // Record the association between a certificate and its |
43 // private key. This method should be called _before_ | 36 // private key. This method should be called _before_ |
44 // FetchClientCertPrivateKey to ensure that the private key is returned | 37 // FetchClientCertPrivateKey to ensure that the private key is returned |
45 // when it is called later. The association is recorded in memory | 38 // when it is called later. The association is recorded in memory |
46 // exclusively. | 39 // exclusively. |
47 // |cert| is a handle to a certificate object. | 40 // |cert| is a handle to a certificate object. |
48 // |private_key| is an OpenSSL EVP_PKEY that corresponds to the | 41 // |private_key| is an OpenSSL EVP_PKEY that corresponds to the |
49 // certificate's private key. | 42 // certificate's private key. |
50 // Returns false if an error occured. | 43 // Returns false if an error occured. |
51 // This function does not take ownership of the private_key, but may | 44 // This function does not take ownership of the private_key, but may |
52 // increment its internal reference count. | 45 // increment its internal reference count. |
53 NET_EXPORT bool RecordClientCertPrivateKey(const X509Certificate* cert, | 46 NET_EXPORT bool RecordClientCertPrivateKey(const X509Certificate* cert, |
54 EVP_PKEY* private_key); | 47 EVP_PKEY* private_key); |
55 | 48 |
56 // Given a certificate's |public_key|, return the corresponding private | 49 // Given a certificate's |public_key|, return the corresponding private |
57 // key that has been recorded previously by RecordClientCertPrivateKey(). | 50 // key that has been recorded previously by RecordClientCertPrivateKey(). |
58 // |cert| is a client certificate. | 51 // |cert| is a client certificate. |
59 // |*private_key| will be reset to its matching private key on success. | 52 // |*private_key| will be reset to its matching private key on success. |
60 // Returns true on success, false otherwise. This increments the reference | 53 // Returns true on success, false otherwise. This increments the reference |
61 // count of the private key on success. | 54 // count of the private key on success. |
62 bool FetchClientCertPrivateKey(const X509Certificate* cert, | 55 bool FetchClientCertPrivateKey(const X509Certificate* cert, |
63 ScopedEVP_PKEY* private_key); | 56 crypto::ScopedEVP_PKEY* private_key); |
64 | 57 |
65 // Flush all recorded keys. | 58 // Flush all recorded keys. |
66 void Flush(); | 59 void Flush(); |
67 | 60 |
68 protected: | 61 protected: |
69 OpenSSLClientKeyStore(); | 62 OpenSSLClientKeyStore(); |
70 | 63 |
71 ~OpenSSLClientKeyStore(); | 64 ~OpenSSLClientKeyStore(); |
72 | 65 |
73 // Adds a given public/private key pair. | 66 // Adds a given public/private key pair. |
(...skipping 25 matching lines...) Expand all Loading... |
99 std::vector<KeyPair> pairs_; | 92 std::vector<KeyPair> pairs_; |
100 | 93 |
101 friend struct DefaultSingletonTraits<OpenSSLClientKeyStore>; | 94 friend struct DefaultSingletonTraits<OpenSSLClientKeyStore>; |
102 | 95 |
103 DISALLOW_COPY_AND_ASSIGN(OpenSSLClientKeyStore); | 96 DISALLOW_COPY_AND_ASSIGN(OpenSSLClientKeyStore); |
104 }; | 97 }; |
105 | 98 |
106 } // namespace net | 99 } // namespace net |
107 | 100 |
108 #endif // NET_SSL_OPENSSL_CLIENT_KEY_STORE_H_ | 101 #endif // NET_SSL_OPENSSL_CLIENT_KEY_STORE_H_ |
OLD | NEW |