OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Defines an in-memory private key store, primarily used for testing. | 5 // Defines an in-memory private key store, primarily used for testing. |
6 | 6 |
| 7 #include "net/base/openssl_private_key_store.h" |
| 8 |
7 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
8 | 10 |
9 #include "net/base/openssl_private_key_store.h" | |
10 | |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "net/base/x509_certificate.h" | |
15 | 14 |
16 namespace net { | 15 namespace net { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 class OpenSSLMemoryKeyStore : public OpenSSLPrivateKeyStore { | 19 // A small in-memory store for public/private key pairs held in |
| 20 // a single EVP_PKEY object. This is intentionally distinct from |
| 21 // net::SSLClientKeyStore. |
| 22 class MemoryKeyPairStore { |
21 public: | 23 public: |
22 OpenSSLMemoryKeyStore() {} | 24 MemoryKeyPairStore() {} |
23 | 25 |
24 static OpenSSLMemoryKeyStore* GetInstance() { | 26 static MemoryKeyPairStore* GetInstance() { |
25 return Singleton<OpenSSLMemoryKeyStore>::get(); | 27 return Singleton<MemoryKeyPairStore>::get(); |
26 } | 28 } |
27 | 29 |
28 virtual ~OpenSSLMemoryKeyStore() { | 30 ~MemoryKeyPairStore() { |
29 base::AutoLock lock(lock_); | 31 base::AutoLock lock(lock_); |
30 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin(); | 32 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin(); |
31 it != keys_.end(); ++it) { | 33 it != keys_.end(); ++it) { |
32 EVP_PKEY_free(*it); | 34 EVP_PKEY_free(*it); |
33 } | 35 } |
34 } | 36 } |
35 | 37 |
36 virtual bool StorePrivateKey(const GURL& url, EVP_PKEY* pkey) { | 38 bool StoreKeyPair(EVP_PKEY* pkey) { |
37 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | 39 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); |
38 base::AutoLock lock(lock_); | 40 base::AutoLock lock(lock_); |
39 keys_.push_back(pkey); | 41 keys_.push_back(pkey); |
40 return true; | 42 return true; |
41 } | 43 } |
42 | 44 |
43 virtual EVP_PKEY* FetchPrivateKey(EVP_PKEY* pkey) { | 45 bool HasPrivateKey(EVP_PKEY* pkey) { |
44 base::AutoLock lock(lock_); | 46 base::AutoLock lock(lock_); |
45 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin(); | 47 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin(); |
46 it != keys_.end(); ++it) { | 48 it != keys_.end(); ++it) { |
47 if (EVP_PKEY_cmp(*it, pkey) == 1) | 49 if (EVP_PKEY_cmp(*it, pkey) == 1) |
48 return *it; | 50 return true; |
49 } | 51 } |
50 return NULL; | 52 return false; |
51 } | 53 } |
52 | 54 |
53 private: | 55 private: |
54 std::vector<EVP_PKEY*> keys_; | 56 std::vector<EVP_PKEY*> keys_; |
55 base::Lock lock_; | 57 base::Lock lock_; |
56 | 58 |
57 DISALLOW_COPY_AND_ASSIGN(OpenSSLMemoryKeyStore); | 59 DISALLOW_COPY_AND_ASSIGN(MemoryKeyPairStore); |
58 }; | 60 }; |
59 | 61 |
60 } // namespace | 62 } // namespace |
61 | 63 |
62 // static | 64 bool OpenSSLPrivateKeyStore::StoreKeyPair(const GURL& url, |
63 OpenSSLPrivateKeyStore* OpenSSLPrivateKeyStore::GetInstance() { | 65 EVP_PKEY* pkey) { |
64 return OpenSSLMemoryKeyStore::GetInstance(); | 66 return MemoryKeyPairStore::GetInstance()->StoreKeyPair(pkey); |
| 67 } |
| 68 |
| 69 bool OpenSSLPrivateKeyStore::HasPrivateKey(EVP_PKEY* pub_key) { |
| 70 return MemoryKeyPairStore::GetInstance()->HasPrivateKey(pub_key); |
65 } | 71 } |
66 | 72 |
67 } // namespace net | 73 } // namespace net |
68 | 74 |
OLD | NEW |