blob: b9f182b3c9bd7b4e88169740e40102c25a3ce0cb [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <vector>
#include "base/callback.h"
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/synchronization/lock.h"
#include "net/base/net_export.h"
namespace net {
class SSLPrivateKey;
class X509Certificate;
// TODO(rsleevi, davidben): Remove this once is fixed.
// A certificate and key store that allows several external certificate
// providers to expose certificates and keys through this store. All currently
// provided certificates will be accessible through |FetchClientCertPrivateKey|.
// Methods of this singleton can be called from any thread.
class NET_EXPORT ClientKeyStore {
class CertKeyProvider {
// This can be called from any thread.
virtual ~CertKeyProvider() {}
// Obtains a handle to the certificate private key for |cert| and stores it
// in |private_key|.
// If the CertKeyProvider does not know about the |cert|, returns false. If
// it knows about the certificate, but is unable to return the private key,
// returns true and sets |*private_key| to nullptr.
// This can be called from any thread.
virtual bool GetCertificateKey(
const X509Certificate& cert,
scoped_refptr<SSLPrivateKey>* private_key) = 0;
static ClientKeyStore* GetInstance();
// The |provider| will be accessed on any thread but no concurrent method
// invocations will happen. |provider| must be valid until it is removed using
// |RemoveProvider| or the store is destroyed.
void AddProvider(CertKeyProvider* provider);
void RemoveProvider(const CertKeyProvider* provider);
// Given a |certificate|'s public key, return the corresponding private
// key if any of the registered providers has a matching key.
// Returns its matching private key on success, nullptr otherwise.
scoped_refptr<SSLPrivateKey> FetchClientCertPrivateKey(
const X509Certificate& certificate);
friend struct base::DefaultLazyInstanceTraits<ClientKeyStore>;
base::Lock lock_;
std::vector<CertKeyProvider*> providers_;
} // namespace net