blob: 40934c0faaaa1ab52af03d68a6a351c7f33a28de [file] [log] [blame]
[email protected]75d93a82013-05-06 19:51:561// Copyright (c) 2013 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.
4
[email protected]60833392013-07-26 17:48:495#ifndef CHROMEOS_CERT_LOADER_H_
6#define CHROMEOS_CERT_LOADER_H_
[email protected]75d93a82013-05-06 19:51:567
8#include <string>
9
[email protected]b1f3f5272013-08-12 15:22:4910#include "base/basictypes.h"
[email protected]75d93a82013-05-06 19:51:5611#include "base/memory/ref_counted.h"
[email protected]75d93a82013-05-06 19:51:5612#include "base/memory/weak_ptr.h"
[email protected]b239083792014-01-21 23:14:5313#include "base/observer_list.h"
[email protected]75d93a82013-05-06 19:51:5614#include "base/threading/thread_checker.h"
15#include "chromeos/chromeos_export.h"
[email protected]b239083792014-01-21 23:14:5316#include "chromeos/tpm_token_loader.h"
[email protected]75d93a82013-05-06 19:51:5617#include "net/cert/cert_database.h"
[email protected]75d93a82013-05-06 19:51:5618
[email protected]60833392013-07-26 17:48:4919namespace base {
[email protected]b1f3f5272013-08-12 15:22:4920class TaskRunner;
[email protected]60833392013-07-26 17:48:4921}
22
[email protected]b239083792014-01-21 23:14:5323namespace net {
24class X509Certificate;
[email protected]75d93a82013-05-06 19:51:5625}
26
27namespace chromeos {
28
[email protected]b239083792014-01-21 23:14:5329// This class is responsible for loading certificates once the TPM is
30// initialized. It is expected to be constructed on the UI thread and public
31// methods should all be called from the UI thread.
32// When certificates have been loaded (after login completes and tpm token is
33// initialized), or the cert database changes, observers are called with
34// OnCertificatesLoaded().
35// TODO(tbarzic): Remove direct dependency on TPMTokenLoader. The reason
36// TPMTokenLoader has to be observed is to make sure singleton NSS DB is
37// initialized before certificate loading starts. CertLoader should use
38// (primary) user specific NSS DB, whose loading already takes this into
39// account (crypto::GetPrivateSlotForChromeOSUser waits until TPM token is
40// ready).
[email protected]75d93a82013-05-06 19:51:5641class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer,
[email protected]b239083792014-01-21 23:14:5342 public TPMTokenLoader::Observer {
[email protected]75d93a82013-05-06 19:51:5643 public:
44 class Observer {
45 public:
[email protected]75d93a82013-05-06 19:51:5646 // Called when the certificates, passed for convenience as |cert_list|,
47 // have completed loading. |initial_load| is true the first time this
48 // is called.
49 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
50 bool initial_load) = 0;
[email protected]bba6b962014-01-24 00:48:1051
52 protected:
53 virtual ~Observer() {}
[email protected]75d93a82013-05-06 19:51:5654 };
55
[email protected]60833392013-07-26 17:48:4956 // Sets the global instance. Must be called before any calls to Get().
57 static void Initialize();
58
59 // Destroys the global instance.
60 static void Shutdown();
61
62 // Gets the global instance. Initialize() must be called first.
63 static CertLoader* Get();
64
65 // Returns true if the global instance has been initialized.
66 static bool IsInitialized();
67
[email protected]b1f3f5272013-08-12 15:22:4968 static std::string GetPkcs11IdForCert(const net::X509Certificate& cert);
69
[email protected]18e8d54f2013-08-06 17:15:4870 // Sets the task runner that any slow calls will be made from, e.g. calls
71 // to the NSS database. If not set, uses base::WorkerPool.
72 void SetSlowTaskRunnerForTest(
[email protected]b1f3f5272013-08-12 15:22:4973 const scoped_refptr<base::TaskRunner>& task_runner);
[email protected]18e8d54f2013-08-06 17:15:4874
[email protected]75d93a82013-05-06 19:51:5675 void AddObserver(CertLoader::Observer* observer);
76 void RemoveObserver(CertLoader::Observer* observer);
77
[email protected]75d93a82013-05-06 19:51:5678 // Returns true if the TPM is available for hardware-backed certificates.
79 bool IsHardwareBacked() const;
80
[email protected]b239083792014-01-21 23:14:5381 // Returns true when the certificate list has been requested but not loaded.
82 bool CertificatesLoading() const;
[email protected]75d93a82013-05-06 19:51:5683
[email protected]b239083792014-01-21 23:14:5384 bool certificates_loaded() const { return certificates_loaded_; }
[email protected]75d93a82013-05-06 19:51:5685
86 // This will be empty until certificates_loaded() is true.
87 const net::CertificateList& cert_list() const { return cert_list_; }
88
[email protected]b239083792014-01-21 23:14:5389 // Getters for cached TPM token info.
90 std::string tpm_user_pin() const { return tpm_user_pin_; }
91 std::string tpm_token_name() const { return tpm_token_name_; }
92 int tpm_token_slot_id() const { return tpm_token_slot_id_; }
93
[email protected]75d93a82013-05-06 19:51:5694 private:
95 CertLoader();
[email protected]60833392013-07-26 17:48:4996 virtual ~CertLoader();
[email protected]75d93a82013-05-06 19:51:5697
[email protected]b239083792014-01-21 23:14:5398 // Starts certificate loading.
99 void RequestCertificates();
[email protected]72b3a7e2013-08-13 15:30:04100
101 // Trigger a certificate load. If a certificate loading task is already in
[email protected]b239083792014-01-21 23:14:53102 // progress, will start a reload once the current task finished.
[email protected]72b3a7e2013-08-13 15:30:04103 void LoadCertificates();
104
105 // Called if a certificate load task is finished.
[email protected]75d93a82013-05-06 19:51:56106 void UpdateCertificates(net::CertificateList* cert_list);
[email protected]75d93a82013-05-06 19:51:56107
108 void NotifyCertificatesLoaded(bool initial_load);
109
110 // net::CertDatabase::Observer
[email protected]c157b2e22013-10-31 01:38:33111 virtual void OnCACertChanged(const net::X509Certificate* cert) OVERRIDE;
[email protected]75d93a82013-05-06 19:51:56112 virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE;
113 virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE;
114
[email protected]b239083792014-01-21 23:14:53115 // chromeos::TPMTokenLoader::Observer
116 virtual void OnTPMTokenReady(const std::string& tpm_user_pin,
117 const std::string& tpm_token_name,
118 int tpm_token_slot_id) OVERRIDE;
[email protected]72b3a7e2013-08-13 15:30:04119
[email protected]75d93a82013-05-06 19:51:56120 ObserverList<Observer> observers_;
121
[email protected]b239083792014-01-21 23:14:53122 // Flags describing current CertLoader state.
[email protected]75d93a82013-05-06 19:51:56123 bool certificates_requested_;
124 bool certificates_loaded_;
[email protected]7d3d0c02013-05-22 12:32:13125 bool certificates_update_required_;
126 bool certificates_update_running_;
127
[email protected]b239083792014-01-21 23:14:53128 // Cached TPM token info. Set when the |OnTPMTokenReady| gets called.
129 std::string tpm_user_pin_;
[email protected]75d93a82013-05-06 19:51:56130 std::string tpm_token_name_;
[email protected]3f3b9b12013-10-25 22:03:26131 int tpm_token_slot_id_;
[email protected]75d93a82013-05-06 19:51:56132
133 // Cached Certificates.
134 net::CertificateList cert_list_;
135
136 base::ThreadChecker thread_checker_;
137
[email protected]18e8d54f2013-08-06 17:15:48138 // TaskRunner for other slow tasks. May be set in tests.
139 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_;
140
[email protected]b239083792014-01-21 23:14:53141 base::WeakPtrFactory<CertLoader> weak_factory_;
[email protected]75d93a82013-05-06 19:51:56142
143 DISALLOW_COPY_AND_ASSIGN(CertLoader);
144};
145
146} // namespace chromeos
147
[email protected]60833392013-07-26 17:48:49148#endif // CHROMEOS_CERT_LOADER_H_