[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 1 | // 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] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 5 | #ifndef CHROMEOS_CERT_LOADER_H_ |
6 | #define CHROMEOS_CERT_LOADER_H_ | ||||
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 7 | |
8 | #include <string> | ||||
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 9 | #include <vector> |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 10 | |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 15 | #include "base/observer_list.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 16 | #include "base/threading/thread_checker.h" |
17 | #include "chromeos/chromeos_export.h" | ||||
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 18 | #include "net/cert/cert_database.h" |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 19 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 20 | namespace net { |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 21 | class NSSCertDatabase; |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 22 | class X509Certificate; |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 23 | typedef std::vector<scoped_refptr<X509Certificate> > CertificateList; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 24 | } |
25 | |||||
26 | namespace chromeos { | ||||
27 | |||||
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 28 | // 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] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 34 | class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer { |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 35 | public: |
36 | class Observer { | ||||
37 | public: | ||||
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 38 | // 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] | bba6b96 | 2014-01-24 00:48:10 | [diff] [blame] | 43 | |
44 | protected: | ||||
45 | virtual ~Observer() {} | ||||
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 46 | }; |
47 | |||||
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 48 | // 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] | 6084b9b | 2014-04-07 23:54:07 | [diff] [blame] | 60 | // Returns the PKCS#11 attribute CKA_ID for a certificate as an upper-case |
[email protected] | 5fffe15 | 2014-07-30 19:40:09 | [diff] [blame] | 61 | // 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] | b1f3f527 | 2013-08-12 15:22:49 | [diff] [blame] | 66 | |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 67 | // Starts the CertLoader with the NSS cert database. |
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 68 | // 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] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 72 | void StartWithNSSDB(net::NSSCertDatabase* database); |
73 | |||||
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 74 | void AddObserver(CertLoader::Observer* observer); |
75 | void RemoveObserver(CertLoader::Observer* observer); | ||||
76 | |||||
pneubeck | ad2f216 | 2014-11-06 10:56:19 | [diff] [blame] | 77 | // Returns true if |cert| is hardware backed. See also |
78 | // ForceHardwareBackedForTesting(). | ||||
79 | static bool IsCertificateHardwareBacked(const net::X509Certificate* cert); | ||||
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 80 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 81 | // Returns true when the certificate list has been requested but not loaded. |
82 | bool CertificatesLoading() const; | ||||
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 83 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 84 | bool certificates_loaded() const { return certificates_loaded_; } |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 85 | |
86 | // This will be empty until certificates_loaded() is true. | ||||
[email protected] | 9e81893 | 2014-02-06 10:24:11 | [diff] [blame] | 87 | const net::CertificateList& cert_list() const { return *cert_list_; } |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 88 | |
pneubeck | ad2f216 | 2014-11-06 10:56:19 | [diff] [blame] | 89 | // Called in tests if |IsCertificateHardwareBacked()| should always return |
90 | // true. | ||||
91 | static void ForceHardwareBackedForTesting(); | ||||
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 92 | |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 93 | private: |
94 | CertLoader(); | ||||
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 95 | ~CertLoader() override; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 96 | |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 97 | // Trigger a certificate load. If a certificate loading task is already in |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 98 | // progress, will start a reload once the current task is finished. |
[email protected] | 72b3a7e | 2013-08-13 15:30:04 | [diff] [blame] | 99 | void LoadCertificates(); |
100 | |||||
101 | // Called if a certificate load task is finished. | ||||
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 102 | void UpdateCertificates(std::unique_ptr<net::CertificateList> cert_list); |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 103 | |
104 | void NotifyCertificatesLoaded(bool initial_load); | ||||
105 | |||||
106 | // net::CertDatabase::Observer | ||||
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 107 | 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] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 110 | |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 111 | base::ObserverList<Observer> observers_; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 112 | |
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 113 | // Flags describing current CertLoader state. |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 114 | bool certificates_loaded_; |
[email protected] | 7d3d0c0 | 2013-05-22 12:32:13 | [diff] [blame] | 115 | bool certificates_update_required_; |
116 | bool certificates_update_running_; | ||||
117 | |||||
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 118 | // The user-specific NSS certificate database from which the certificates |
119 | // should be loaded. | ||||
120 | net::NSSCertDatabase* database_; | ||||
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 121 | |
[email protected] | 69295ba | 2014-01-28 06:17:00 | [diff] [blame] | 122 | // Cached Certificates loaded from the database. |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 123 | std::unique_ptr<net::CertificateList> cert_list_; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 124 | |
125 | base::ThreadChecker thread_checker_; | ||||
126 | |||||
[email protected] | b23908379 | 2014-01-21 23:14:53 | [diff] [blame] | 127 | base::WeakPtrFactory<CertLoader> weak_factory_; |
[email protected] | 75d93a8 | 2013-05-06 19:51:56 | [diff] [blame] | 128 | |
129 | DISALLOW_COPY_AND_ASSIGN(CertLoader); | ||||
130 | }; | ||||
131 | |||||
132 | } // namespace chromeos | ||||
133 | |||||
[email protected] | 6083339 | 2013-07-26 17:48:49 | [diff] [blame] | 134 | #endif // CHROMEOS_CERT_LOADER_H_ |