Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / base / openssl_private_key_store_memory.cc
blob4d6a287c2a7b75e63f1fd7a7e8392557a28b0af5
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 "net/base/openssl_private_key_store.h"
9 #include <openssl/evp.h>
11 #include "base/logging.h"
12 #include "base/memory/singleton.h"
13 #include "base/synchronization/lock.h"
15 namespace net {
17 namespace {
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 {
23 public:
24 MemoryKeyPairStore() {}
26 static MemoryKeyPairStore* GetInstance() {
27 return Singleton<MemoryKeyPairStore>::get();
30 ~MemoryKeyPairStore() {
31 base::AutoLock lock(lock_);
32 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin();
33 it != keys_.end(); ++it) {
34 EVP_PKEY_free(*it);
38 bool StoreKeyPair(EVP_PKEY* pkey) {
39 EVP_PKEY_dup(pkey);
40 base::AutoLock lock(lock_);
41 keys_.push_back(pkey);
42 return true;
45 bool HasPrivateKey(EVP_PKEY* pkey) {
46 base::AutoLock lock(lock_);
47 for (std::vector<EVP_PKEY*>::iterator it = keys_.begin();
48 it != keys_.end(); ++it) {
49 if (EVP_PKEY_cmp(*it, pkey) == 1)
50 return true;
52 return false;
55 private:
56 std::vector<EVP_PKEY*> keys_;
57 base::Lock lock_;
59 DISALLOW_COPY_AND_ASSIGN(MemoryKeyPairStore);
62 } // namespace
64 bool OpenSSLPrivateKeyStore::StoreKeyPair(const GURL& url,
65 EVP_PKEY* pkey) {
66 return MemoryKeyPairStore::GetInstance()->StoreKeyPair(pkey);
69 bool OpenSSLPrivateKeyStore::HasPrivateKey(EVP_PKEY* pub_key) {
70 return MemoryKeyPairStore::GetInstance()->HasPrivateKey(pub_key);
73 } // namespace net