Chromium Code Reviews| 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 <openssl/evp.h> | 7 #include <openssl/evp.h> |
| 8 | 8 |
| 9 #include "net/base/openssl_private_key_store.h" | 9 #include "net/base/openssl_private_key_store.h" |
| 10 | 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/openssl_util.h" | |
| 14 #include "net/base/x509_certificate.h" | 15 #include "net/base/x509_certificate.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 class OpenSSLMemoryKeyStore : public OpenSSLPrivateKeyStore { | 21 class OpenSSLMemoryKeyStore : public OpenSSLPrivateKeyStore { |
| 21 public: | 22 public: |
| 22 OpenSSLMemoryKeyStore() {} | 23 OpenSSLMemoryKeyStore() {} |
| 23 | 24 |
| 24 static OpenSSLMemoryKeyStore* GetInstance() { | 25 static OpenSSLMemoryKeyStore* GetInstance() { |
| 25 return Singleton<OpenSSLMemoryKeyStore>::get(); | 26 return Singleton<OpenSSLMemoryKeyStore, |
| 27 OpenSSLMemoryKeyStoreLeakyTraits>::get(); | |
|
Ryan Sleevi
2013/02/12 00:25:17
style: indent to the <
digit1
2013/02/12 15:05:25
Done.
| |
| 26 } | 28 } |
| 27 | 29 |
| 28 virtual ~OpenSSLMemoryKeyStore() { | 30 virtual ~OpenSSLMemoryKeyStore() { } |
| 31 | |
| 32 virtual bool StoreKeyPair(const GURL& url, EVP_PKEY* pkey) OVERRIDE { | |
| 29 base::AutoLock lock(lock_); | 33 base::AutoLock lock(lock_); |
| 30 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin(); | 34 // The 'pkey' holds both a private and a public key. |
| 31 it != keys_.end(); ++it) { | 35 return pairs_.AddKeyPair(pkey, pkey); |
| 32 EVP_PKEY_free(*it); | |
| 33 } | |
| 34 } | 36 } |
| 35 | 37 |
| 36 virtual bool StorePrivateKey(const GURL& url, EVP_PKEY* pkey) { | 38 virtual bool RecordClientCertPrivateKey( |
| 37 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | 39 const net::X509Certificate& client_cert, |
| 40 EVP_PKEY* private_key) OVERRIDE { | |
| 41 // Sanity check. | |
| 42 if (private_key == NULL) { | |
| 43 LOG(ERROR) << "NULL private key for client certificate!"; | |
| 44 return false; | |
| 45 } | |
| 46 // Get public key from certificate. | |
| 47 ScopedEVP_PKEY pub_key(GetCertificatePublicKeyOpenSSL(client_cert)); | |
| 48 if (!pub_key.get()) { | |
| 49 LOG(ERROR) << "Can't extract public key from certificate!"; | |
| 50 return false; | |
| 51 } | |
| 38 base::AutoLock lock(lock_); | 52 base::AutoLock lock(lock_); |
| 39 keys_.push_back(pkey); | 53 return pairs_.AddKeyPair(pub_key.get(), private_key); |
| 40 return true; | |
| 41 } | 54 } |
| 42 | 55 |
| 43 virtual EVP_PKEY* FetchPrivateKey(EVP_PKEY* pkey) { | 56 virtual EVP_PKEY* FetchClientCertPrivateKey( |
| 57 const X509Certificate& client_cert) { | |
| 58 ScopedEVP_PKEY pub_key(GetCertificatePublicKeyOpenSSL(client_cert)); | |
| 59 if (!pub_key.get()) { | |
| 60 LOG(ERROR) << "Could not extract public key from client certificate"; | |
| 61 return NULL; | |
| 62 } | |
| 44 base::AutoLock lock(lock_); | 63 base::AutoLock lock(lock_); |
| 45 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin(); | 64 return pairs_.FindPrivateKey(pub_key.get()); |
| 46 it != keys_.end(); ++it) { | |
| 47 if (EVP_PKEY_cmp(*it, pkey) == 1) | |
| 48 return *it; | |
| 49 } | |
| 50 return NULL; | |
| 51 } | 65 } |
| 52 | 66 |
| 53 private: | 67 private: |
| 54 std::vector<EVP_PKEY*> keys_; | 68 friend struct DefaultSingletonTraits<OpenSSLMemoryKeyStore>; |
| 69 typedef LeakySingletonTraits<OpenSSLMemoryKeyStore> | |
| 70 OpenSSLMemoryKeyStoreLeakyTraits; | |
|
Ryan Sleevi
2013/02/12 00:25:17
Why is this typedef hanging off the class, and not
digit1
2013/02/12 15:05:25
Most of this code new actually comes from the exis
| |
| 71 | |
| 72 OpenSSLKeyPairList pairs_; | |
| 55 base::Lock lock_; | 73 base::Lock lock_; |
| 56 | 74 |
| 57 DISALLOW_COPY_AND_ASSIGN(OpenSSLMemoryKeyStore); | 75 DISALLOW_COPY_AND_ASSIGN(OpenSSLMemoryKeyStore); |
| 58 }; | 76 }; |
| 59 | 77 |
| 60 } // namespace | 78 } // namespace |
| 61 | 79 |
| 62 // static | 80 // static |
| 63 OpenSSLPrivateKeyStore* OpenSSLPrivateKeyStore::GetInstance() { | 81 OpenSSLPrivateKeyStore* OpenSSLPrivateKeyStore::GetInstance() { |
| 64 return OpenSSLMemoryKeyStore::GetInstance(); | 82 return OpenSSLMemoryKeyStore::GetInstance(); |
| 65 } | 83 } |
| 66 | 84 |
| 67 } // namespace net | 85 } // namespace net |
| 68 | 86 |
| OLD | NEW |