[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / certificate_manager_model.h
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_
6 #define CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11
12 #include "base/functional/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/raw_ptr.h"
15 #include "build/chromeos_buildflags.h"
16 #include "chrome/browser/net/nss_service.h"
17 #include "net/cert/nss_cert_database.h"
18 #include "net/cert/scoped_nss_types.h"
19 #include "net/ssl/client_cert_identity.h"
20
21 namespace content {
22 class BrowserContext;
23 }  // namespace content
24
25 #if BUILDFLAG(IS_CHROMEOS)
26 namespace ash {
27 class PolicyCertificateProvider;
28 }
29
30 namespace chromeos {
31 class CertificateProvider;
32 }
33 #endif
34
35 // CertificateManagerModel provides the data to be displayed in the certificate
36 // manager dialog, and processes changes from the view.
37 class CertificateManagerModel {
38  public:
39   // Holds information about a certificate, along with the certificate itself.
40   class CertInfo {
41    public:
42     enum class Source {
43       // This certificate is installed in the platform certificate database.
44       kPlatform,
45       // This certificate is provided by enterprise policy.
46       kPolicy,
47       // This certificate is provided by an extension.
48       kExtension
49     };
50
51     CertInfo(net::ScopedCERTCertificate cert,
52              net::CertType type,
53              std::u16string name,
54              bool can_be_deleted,
55              bool untrusted,
56              Source source,
57              bool web_trust_anchor,
58              bool hardware_backed,
59              bool device_wide);
60
61     CertInfo(const CertInfo&) = delete;
62     CertInfo& operator=(const CertInfo&) = delete;
63
64     ~CertInfo();
65
66     CERTCertificate* cert() const { return cert_.get(); }
67     net::CertType type() const { return type_; }
68     const std::u16string& name() const { return name_; }
69     bool can_be_deleted() const { return can_be_deleted_; }
70     bool untrusted() const { return untrusted_; }
71     Source source() const { return source_; }
72     bool web_trust_anchor() const { return web_trust_anchor_; }
73     bool hardware_backed() const { return hardware_backed_; }
74     bool device_wide() const { return device_wide_; }
75
76     // Clones a CertInfo, duplicating the contained NSS certificate.
77     static std::unique_ptr<CertInfo> Clone(const CertInfo* cert_info);
78
79    private:
80     // The certificate itself.
81     net::ScopedCERTCertificate cert_;
82
83     // The type of the certificate. Used to filter certificates to be displayed
84     // on the tabs of the certificate manager UI.
85     net::CertType type_;
86
87     // A user readable certificate name.
88     std::u16string name_;
89
90     // false if the certificate is stored on a read-only slot or provided by
91     // enterprise policy or an extension, otherwise true.
92     bool can_be_deleted_;
93
94     // true if the certificate is untrusted.
95     bool untrusted_;
96
97     // Describes where this certificate originates from.
98     Source source_;
99
100     // true if the certificate is given web trust (either by its platform trust
101     // settings, or by enterprise policy).
102     bool web_trust_anchor_;
103
104     // true if the certificate is hardware-backed. Note that extension-provided
105     // certificates are not regarded as hardware-backed.
106     bool hardware_backed_;
107
108     // true if the certificate is device-wide.
109     // Note: can be true only on Chrome OS.
110     bool device_wide_;
111
112     FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
113                              CanDeleteCertificateCommonTest);
114     FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
115                              CanDeleteUserCertificateTest);
116     FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
117                              CanDeleteCACertificateTest);
118     FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
119                              CanEditCertificateCommonTest);
120     FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
121                              CanEditUserCertificateTest);
122     FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest, CanEditCACertificateTest);
123   };
124
125   class CertsSource;
126
127   // Holds parameters during construction.
128   struct Params {
129 #if BUILDFLAG(IS_CHROMEOS)
130     // May be nullptr.
131     raw_ptr<ash::PolicyCertificateProvider> policy_certs_provider = nullptr;
132     // May be nullptr.
133     std::unique_ptr<chromeos::CertificateProvider>
134         extension_certificate_provider;
135 #endif
136
137     Params();
138
139     Params(const Params&) = delete;
140     Params& operator=(const Params&) = delete;
141
142     Params(Params&& other);
143
144     ~Params();
145   };
146
147   // Map from the subject organization name to the list of certs from that
148   // organization.  If a cert does not have an organization name, the
149   // subject's CertPrincipal::GetDisplayName() value is used instead.
150   using OrgGroupingMap =
151       std::map<std::string, std::vector<std::unique_ptr<CertInfo>>>;
152
153   using CreationCallback =
154       base::OnceCallback<void(std::unique_ptr<CertificateManagerModel>)>;
155
156   class Observer {
157    public:
158     // Called to notify the view that the certificate list has been refreshed.
159     // TODO(mattm): do a more granular updating strategy?  Maybe retrieve new
160     // list of certs, diff against past list, and then notify of the changes?
161     virtual void CertificatesRefreshed() = 0;
162
163    protected:
164     virtual ~Observer() = default;
165   };
166
167   // Creates a CertificateManagerModel. The model will be passed to the callback
168   // when it is ready. The caller must ensure the model does not outlive the
169   // |browser_context|.
170   static void Create(content::BrowserContext* browser_context,
171                      Observer* observer,
172                      CreationCallback callback);
173
174   // Use |Create| instead to create a |CertificateManagerModel| for a
175   // |BrowserContext|.
176   CertificateManagerModel(std::unique_ptr<Params> params,
177                           Observer* observer,
178                           net::NSSCertDatabase* nss_cert_database);
179
180   CertificateManagerModel(const CertificateManagerModel&) = delete;
181   CertificateManagerModel& operator=(const CertificateManagerModel&) = delete;
182
183   ~CertificateManagerModel();
184
185   // Accessor for read-only access to the underlying NSSCertDatabase.
186   const net::NSSCertDatabase* cert_db() const { return cert_db_; }
187
188   // Trigger a refresh of the list of certs, unlock any slots if necessary.
189   // Following this call, the observer CertificatesRefreshed method will be
190   // called so the view can call FilterAndBuildOrgGroupingMap as necessary to
191   // refresh its tree views.
192   void Refresh();
193
194   // Fill |*out_org_grouping_map| with the certificates matching |filter_type|.
195   void FilterAndBuildOrgGroupingMap(net::CertType filter_type,
196                                     OrgGroupingMap* out_org_grouping_map) const;
197
198   // Import private keys and certificates from PKCS #12 encoded
199   // |data|, using the given |password|. If |is_extractable| is false,
200   // mark the private key as unextractable from the slot.
201   // Returns a net error code on failure.
202   int ImportFromPKCS12(PK11SlotInfo* slot_info,
203                        const std::string& data,
204                        const std::u16string& password,
205                        bool is_extractable);
206
207   // Import user certificate from DER encoded |data|.
208   // Returns a net error code on failure.
209   int ImportUserCert(const std::string& data);
210
211   // Import CA certificates.
212   // Tries to import all the certificates given.  The root will be trusted
213   // according to |trust_bits|.  Any certificates that could not be imported
214   // will be listed in |not_imported|.
215   // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
216   // Returns false if there is an internal error, otherwise true is returned and
217   // |not_imported| should be checked for any certificates that were not
218   // imported.
219   bool ImportCACerts(const net::ScopedCERTCertificateList& certificates,
220                      net::NSSCertDatabase::TrustBits trust_bits,
221                      net::NSSCertDatabase::ImportCertFailureList* not_imported);
222
223   // Import server certificate.  The first cert should be the server cert.  Any
224   // additional certs should be intermediate/CA certs and will be imported but
225   // not given any trust.
226   // Any certificates that could not be imported will be listed in
227   // |not_imported|.
228   // |trust_bits| can be set to explicitly trust or distrust the certificate, or
229   // use TRUST_DEFAULT to inherit trust as normal.
230   // Returns false if there is an internal error, otherwise true is returned and
231   // |not_imported| should be checked for any certificates that were not
232   // imported.
233   bool ImportServerCert(
234       const net::ScopedCERTCertificateList& certificates,
235       net::NSSCertDatabase::TrustBits trust_bits,
236       net::NSSCertDatabase::ImportCertFailureList* not_imported);
237
238   // Set trust values for certificate.
239   // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
240   // Returns true on success or false on failure.
241   bool SetCertTrust(CERTCertificate* cert,
242                     net::CertType type,
243                     net::NSSCertDatabase::TrustBits trust_bits);
244
245   // Remove the cert from the cert database.
246   void RemoveFromDatabase(net::ScopedCERTCertificate cert,
247                           base::OnceCallback<void(bool /*success*/)> callback);
248
249  private:
250   // Called when one of the |certs_sources_| has been updated. Will notify the
251   // |observer_| that the certificate list has been refreshed.
252   void OnCertsSourceUpdated();
253
254   // Finds the |CertsSource| which provided |cert|. Can return nullptr (e.g. if
255   // the cert has been deleted in the meantime).
256   CertsSource* FindCertsSourceForCert(CERTCertificate* cert);
257
258   // Methods used during initialization, see the comment at the top of the .cc
259   // file for details.
260   static void DidGetCertDBOnUIThread(
261       std::unique_ptr<Params> params,
262       CertificateManagerModel::Observer* observer,
263       CreationCallback callback,
264       net::NSSCertDatabase* cert_db);
265   static void DidGetCertDBOnIOThread(
266       std::unique_ptr<Params> params,
267       CertificateManagerModel::Observer* observer,
268       CreationCallback callback,
269       net::NSSCertDatabase* cert_db);
270   static void GetCertDBOnIOThread(std::unique_ptr<Params> params,
271                                   NssCertDatabaseGetter database_getter,
272                                   CertificateManagerModel::Observer* observer,
273                                   CreationCallback callback);
274
275   raw_ptr<net::NSSCertDatabase> cert_db_;
276
277   // CertsSource instances providing certificates. The order matters - if a
278   // certificate is provided by more than one CertsSource, only the first one is
279   // accepted.
280   std::vector<std::unique_ptr<CertsSource>> certs_sources_;
281
282   bool hold_back_updates_ = false;
283
284   // The observer to notify when certificate list is refreshed.
285   raw_ptr<Observer> observer_;
286 };
287
288 #endif  // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_