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
3 // found in the LICENSE file.
5 // Defines an in-memory private key store, primarily used for testing.
7 #include <openssl/evp.h>
9 #include "net/base/openssl_private_key_store.h"
11 #include "base/logging.h"
12 #include "base/memory/singleton.h"
13 #include "base/synchronization/lock.h"
14 #include "net/base/x509_certificate.h"
20 class OpenSSLMemoryKeyStore
: public OpenSSLPrivateKeyStore
{
22 OpenSSLMemoryKeyStore() {}
24 static OpenSSLMemoryKeyStore
* GetInstance() {
25 return Singleton
<OpenSSLMemoryKeyStore
>::get();
28 virtual ~OpenSSLMemoryKeyStore() {
29 base::AutoLock
lock(lock_
);
30 for (std::vector
<EVP_PKEY
*>::iterator it
= keys_
.begin();
31 it
!= keys_
.end(); ++it
) {
36 virtual bool StorePrivateKey(const GURL
& url
, EVP_PKEY
* pkey
) {
37 CRYPTO_add(&pkey
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
38 base::AutoLock
lock(lock_
);
39 keys_
.push_back(pkey
);
43 virtual EVP_PKEY
* FetchPrivateKey(EVP_PKEY
* pkey
) {
44 base::AutoLock
lock(lock_
);
45 for (std::vector
<EVP_PKEY
*>::iterator it
= keys_
.begin();
46 it
!= keys_
.end(); ++it
) {
47 if (EVP_PKEY_cmp(*it
, pkey
) == 1)
54 std::vector
<EVP_PKEY
*> keys_
;
57 DISALLOW_COPY_AND_ASSIGN(OpenSSLMemoryKeyStore
);
63 OpenSSLPrivateKeyStore
* OpenSSLPrivateKeyStore::GetInstance() {
64 return OpenSSLMemoryKeyStore::GetInstance();