blob: 4bc6b7cdc82d3b3943e996ef694ed8b459bedb74 [file] [log] [blame]
Hubert Chao52a323602024-09-11 14:40:151// Copyright 2024 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
Matt Mueller7f1d7902025-01-21 22:34:375#ifndef COMPONENTS_SERVER_CERTIFICATE_DATABASE_SERVER_CERTIFICATE_DATABASE_H_
6#define COMPONENTS_SERVER_CERTIFICATE_DATABASE_SERVER_CERTIFICATE_DATABASE_H_
Hubert Chao52a323602024-09-11 14:40:157
Hubert Chao7299aa6b2024-10-03 18:20:228#include <optional>
9
Hubert Chao52a323602024-09-11 14:40:1510#include "base/files/file_path.h"
11#include "base/sequence_checker.h"
12#include "base/thread_annotations.h"
Matt Mueller7f1d7902025-01-21 22:34:3713#include "components/server_certificate_database/server_certificate_database.pb.h"
Hubert Chao52a323602024-09-11 14:40:1514#include "sql/database.h"
15#include "sql/init_status.h"
Hubert Chao7299aa6b2024-10-03 18:20:2216#include "third_party/boringssl/src/pki/trust_store.h"
Hubert Chao52a323602024-09-11 14:40:1517
18namespace net {
19
Etienne Bergeron872a6a642025-03-21 19:57:1320extern const base::FilePath::CharType kServerCertificateDatabaseName[];
21
Hubert Chao52a323602024-09-11 14:40:1522// Wraps the SQLite database that provides on-disk storage for user-configured
23// TLS certificates. This class is expected to be created and accessed on a
24// backend sequence.
25class ServerCertificateDatabase {
26 public:
Hubert Chaoe28ab1c2024-09-18 19:32:2527 struct CertInformation {
Matt Mueller778dfcb2024-12-10 19:02:1428 // Initializes a CertInformation object with the `der_cert` and calculates
29 // the `sha256hash_hex` from the supplied cert.
30 explicit CertInformation(base::span<const uint8_t> cert);
Hubert Chaoe28ab1c2024-09-18 19:32:2531 CertInformation();
32 ~CertInformation();
33 CertInformation(CertInformation&&);
34 CertInformation& operator=(CertInformation&& other);
35
36 std::string sha256hash_hex;
37 std::vector<uint8_t> der_cert;
38 chrome_browser_server_certificate_database::CertificateMetadata
39 cert_metadata;
40 };
41
42 // Opens the database in `storage_dir`, creating it if one does not exist.
43 // `storage_dir` will generally be in the Profile directory.
Hubert Chao52a323602024-09-11 14:40:1544 explicit ServerCertificateDatabase(const base::FilePath& storage_dir);
45
46 ServerCertificateDatabase(const ServerCertificateDatabase&) = delete;
47 ServerCertificateDatabase& operator=(const ServerCertificateDatabase&) =
48 delete;
49 ~ServerCertificateDatabase();
50
Hubert Chao7299aa6b2024-10-03 18:20:2251 static std::optional<bssl::CertificateTrustType> GetUserCertificateTrust(
52 const net::ServerCertificateDatabase::CertInformation& cert_info);
53
Hubert Chaod4b21142024-11-22 17:00:3754 // Insert new certificates into the database, or if some of the certificates
55 // are already present (as indicated by cert_info.sha256hash_hex), update the
Hubert Chaoe28ab1c2024-09-18 19:32:2556 // entry in the database.
Hubert Chaod4b21142024-11-22 17:00:3757 bool InsertOrUpdateCerts(const std::vector<CertInformation>& cert_infos);
Hubert Chaoe28ab1c2024-09-18 19:32:2558
59 // Retrieve all of the certificates from the database.
60 std::vector<CertInformation> RetrieveAllCertificates();
61
Carlos IL0722522c72024-10-22 18:50:0362 uint32_t RetrieveCertificatesCount();
63
Carlos IL1de43532024-10-24 18:27:0064 // Delete the certificate with a matching hash from the database.
65 bool DeleteCertificate(const std::string& sha256hash_hex);
66
Hubert Chao52a323602024-09-11 14:40:1567 private:
68 sql::InitStatus InitInternal(const base::FilePath& storage_dir);
69
70 // The underlying SQL database.
71 sql::Database db_ GUARDED_BY_CONTEXT(sequence_checker_);
72 SEQUENCE_CHECKER(sequence_checker_);
Hubert Chaof2a73f72025-01-08 20:37:5573
74 // If the DB was successfully initialized. This is used to ensure that we
75 // don't crash if the DB is unable to be initialized (e.g if Chrome is being
76 // run from a read-only volume).
77 bool db_initialized_ = false;
Hubert Chao52a323602024-09-11 14:40:1578};
79
80} // namespace net
81
Matt Mueller7f1d7902025-01-21 22:34:3782#endif // COMPONENTS_SERVER_CERTIFICATE_DATABASE_SERVER_CERTIFICATE_DATABASE_H_