blob: c7aeebcf725b78ed8a2b77f54b6e9323d489b0aa [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>
[email protected]69295ba2014-01-28 06:17:009#include <vector>
[email protected]75d93a82013-05-06 19:51:5610
[email protected]69295ba2014-01-28 06:17:0011#include "base/compiler_specific.h"
avi6e1a22d2015-12-21 03:43:2012#include "base/macros.h"
[email protected]75d93a82013-05-06 19:51:5613#include "base/memory/ref_counted.h"
[email protected]75d93a82013-05-06 19:51:5614#include "base/memory/weak_ptr.h"
[email protected]b239083792014-01-21 23:14:5315#include "base/observer_list.h"
[email protected]75d93a82013-05-06 19:51:5616#include "base/threading/thread_checker.h"
17#include "chromeos/chromeos_export.h"
[email protected]75d93a82013-05-06 19:51:5618#include "net/cert/cert_database.h"
[email protected]75d93a82013-05-06 19:51:5619
[email protected]b239083792014-01-21 23:14:5320namespace net {
[email protected]69295ba2014-01-28 06:17:0021class NSSCertDatabase;
[email protected]b239083792014-01-21 23:14:5322class X509Certificate;
[email protected]69295ba2014-01-28 06:17:0023typedef std::vector<scoped_refptr<X509Certificate> > CertificateList;
[email protected]75d93a82013-05-06 19:51:5624}
25
26namespace chromeos {
27
[email protected]b239083792014-01-21 23:14:5328// This class is responsible for loading certificates once the TPM is
29// initialized. It is expected to be constructed on the UI thread and public
30// methods should all be called from the UI thread.
31// When certificates have been loaded (after login completes and tpm token is
32// initialized), or the cert database changes, observers are called with
33// OnCertificatesLoaded().
[email protected]69295ba2014-01-28 06:17:0034class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer {
[email protected]75d93a82013-05-06 19:51:5635 public:
36 class Observer {
37 public:
[email protected]75d93a82013-05-06 19:51:5638 // Called when the certificates, passed for convenience as |cert_list|,
39 // have completed loading. |initial_load| is true the first time this
40 // is called.
41 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
42 bool initial_load) = 0;
[email protected]bba6b962014-01-24 00:48:1043
44 protected:
45 virtual ~Observer() {}
[email protected]75d93a82013-05-06 19:51:5646 };
47
[email protected]60833392013-07-26 17:48:4948 // Sets the global instance. Must be called before any calls to Get().
49 static void Initialize();
50
51 // Destroys the global instance.
52 static void Shutdown();
53
54 // Gets the global instance. Initialize() must be called first.
55 static CertLoader* Get();
56
57 // Returns true if the global instance has been initialized.
58 static bool IsInitialized();
59
[email protected]6084b9b2014-04-07 23:54:0760 // Returns the PKCS#11 attribute CKA_ID for a certificate as an upper-case
[email protected]5fffe152014-07-30 19:40:0961 // hex string and sets |slot_id| to the id of the containing slot, or returns
62 // an empty string and doesn't modify |slot_id| if the PKCS#11 id could not be
63 // determined.
64 static std::string GetPkcs11IdAndSlotForCert(const net::X509Certificate& cert,
65 int* slot_id);
[email protected]b1f3f5272013-08-12 15:22:4966
[email protected]69295ba2014-01-28 06:17:0067 // Starts the CertLoader with the NSS cert database.
[email protected]9e818932014-02-06 10:24:1168 // The CertLoader will _not_ take the ownership of the database, but it
69 // expects it to stay alive at least until the shutdown starts on the main
70 // thread. This assumes that |StartWithNSSDB| and other methods directly
71 // using |database_| are not called during shutdown.
[email protected]69295ba2014-01-28 06:17:0072 void StartWithNSSDB(net::NSSCertDatabase* database);
73
[email protected]75d93a82013-05-06 19:51:5674 void AddObserver(CertLoader::Observer* observer);
75 void RemoveObserver(CertLoader::Observer* observer);
76
pneubeckad2f2162014-11-06 10:56:1977 // Returns true if |cert| is hardware backed. See also
78 // ForceHardwareBackedForTesting().
79 static bool IsCertificateHardwareBacked(const net::X509Certificate* cert);
[email protected]69295ba2014-01-28 06:17:0080
[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.
[email protected]9e818932014-02-06 10:24:1187 const net::CertificateList& cert_list() const { return *cert_list_; }
[email protected]75d93a82013-05-06 19:51:5688
pneubeckad2f2162014-11-06 10:56:1989 // Called in tests if |IsCertificateHardwareBacked()| should always return
90 // true.
91 static void ForceHardwareBackedForTesting();
[email protected]b239083792014-01-21 23:14:5392
[email protected]75d93a82013-05-06 19:51:5693 private:
94 CertLoader();
dchengae98daa2015-01-21 20:30:4995 ~CertLoader() override;
[email protected]75d93a82013-05-06 19:51:5696
[email protected]72b3a7e2013-08-13 15:30:0497 // Trigger a certificate load. If a certificate loading task is already in
[email protected]69295ba2014-01-28 06:17:0098 // progress, will start a reload once the current task is finished.
[email protected]72b3a7e2013-08-13 15:30:0499 void LoadCertificates();
100
101 // Called if a certificate load task is finished.
dcheng0a6e80c2016-04-08 18:37:38102 void UpdateCertificates(std::unique_ptr<net::CertificateList> cert_list);
[email protected]75d93a82013-05-06 19:51:56103
104 void NotifyCertificatesLoaded(bool initial_load);
105
106 // net::CertDatabase::Observer
dchengae98daa2015-01-21 20:30:49107 void OnCACertChanged(const net::X509Certificate* cert) override;
108 void OnCertAdded(const net::X509Certificate* cert) override;
109 void OnCertRemoved(const net::X509Certificate* cert) override;
[email protected]75d93a82013-05-06 19:51:56110
brettw236d3172015-06-03 16:31:43111 base::ObserverList<Observer> observers_;
[email protected]75d93a82013-05-06 19:51:56112
[email protected]b239083792014-01-21 23:14:53113 // Flags describing current CertLoader state.
[email protected]75d93a82013-05-06 19:51:56114 bool certificates_loaded_;
[email protected]7d3d0c02013-05-22 12:32:13115 bool certificates_update_required_;
116 bool certificates_update_running_;
117
[email protected]69295ba2014-01-28 06:17:00118 // The user-specific NSS certificate database from which the certificates
119 // should be loaded.
120 net::NSSCertDatabase* database_;
[email protected]75d93a82013-05-06 19:51:56121
[email protected]69295ba2014-01-28 06:17:00122 // Cached Certificates loaded from the database.
dcheng0a6e80c2016-04-08 18:37:38123 std::unique_ptr<net::CertificateList> cert_list_;
[email protected]75d93a82013-05-06 19:51:56124
125 base::ThreadChecker thread_checker_;
126
[email protected]b239083792014-01-21 23:14:53127 base::WeakPtrFactory<CertLoader> weak_factory_;
[email protected]75d93a82013-05-06 19:51:56128
129 DISALLOW_COPY_AND_ASSIGN(CertLoader);
130};
131
132} // namespace chromeos
133
[email protected]60833392013-07-26 17:48:49134#endif // CHROMEOS_CERT_LOADER_H_