Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 1 | // 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 Mueller | 7f1d790 | 2025-01-21 22:34:37 | [diff] [blame] | 5 | #ifndef COMPONENTS_SERVER_CERTIFICATE_DATABASE_SERVER_CERTIFICATE_DATABASE_H_ |
| 6 | #define COMPONENTS_SERVER_CERTIFICATE_DATABASE_SERVER_CERTIFICATE_DATABASE_H_ |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 7 | |
Hubert Chao | 7299aa6b | 2024-10-03 18:20:22 | [diff] [blame] | 8 | #include <optional> |
| 9 | |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
| 11 | #include "base/sequence_checker.h" |
| 12 | #include "base/thread_annotations.h" |
Matt Mueller | 7f1d790 | 2025-01-21 22:34:37 | [diff] [blame] | 13 | #include "components/server_certificate_database/server_certificate_database.pb.h" |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 14 | #include "sql/database.h" |
| 15 | #include "sql/init_status.h" |
Hubert Chao | 7299aa6b | 2024-10-03 18:20:22 | [diff] [blame] | 16 | #include "third_party/boringssl/src/pki/trust_store.h" |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
Etienne Bergeron | 872a6a64 | 2025-03-21 19:57:13 | [diff] [blame] | 20 | extern const base::FilePath::CharType kServerCertificateDatabaseName[]; |
| 21 | |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 22 | // 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. |
| 25 | class ServerCertificateDatabase { |
| 26 | public: |
Hubert Chao | e28ab1c | 2024-09-18 19:32:25 | [diff] [blame] | 27 | struct CertInformation { |
Matt Mueller | 778dfcb | 2024-12-10 19:02:14 | [diff] [blame] | 28 | // 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 Chao | e28ab1c | 2024-09-18 19:32:25 | [diff] [blame] | 31 | 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 Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 44 | explicit ServerCertificateDatabase(const base::FilePath& storage_dir); |
| 45 | |
| 46 | ServerCertificateDatabase(const ServerCertificateDatabase&) = delete; |
| 47 | ServerCertificateDatabase& operator=(const ServerCertificateDatabase&) = |
| 48 | delete; |
| 49 | ~ServerCertificateDatabase(); |
| 50 | |
Hubert Chao | 7299aa6b | 2024-10-03 18:20:22 | [diff] [blame] | 51 | static std::optional<bssl::CertificateTrustType> GetUserCertificateTrust( |
| 52 | const net::ServerCertificateDatabase::CertInformation& cert_info); |
| 53 | |
Hubert Chao | d4b2114 | 2024-11-22 17:00:37 | [diff] [blame] | 54 | // 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 Chao | e28ab1c | 2024-09-18 19:32:25 | [diff] [blame] | 56 | // entry in the database. |
Hubert Chao | d4b2114 | 2024-11-22 17:00:37 | [diff] [blame] | 57 | bool InsertOrUpdateCerts(const std::vector<CertInformation>& cert_infos); |
Hubert Chao | e28ab1c | 2024-09-18 19:32:25 | [diff] [blame] | 58 | |
| 59 | // Retrieve all of the certificates from the database. |
| 60 | std::vector<CertInformation> RetrieveAllCertificates(); |
| 61 | |
Carlos IL | 0722522c7 | 2024-10-22 18:50:03 | [diff] [blame] | 62 | uint32_t RetrieveCertificatesCount(); |
| 63 | |
Carlos IL | 1de4353 | 2024-10-24 18:27:00 | [diff] [blame] | 64 | // Delete the certificate with a matching hash from the database. |
| 65 | bool DeleteCertificate(const std::string& sha256hash_hex); |
| 66 | |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 67 | 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 Chao | f2a73f7 | 2025-01-08 20:37:55 | [diff] [blame] | 73 | |
| 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 Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | } // namespace net |
| 81 | |
Matt Mueller | 7f1d790 | 2025-01-21 22:34:37 | [diff] [blame] | 82 | #endif // COMPONENTS_SERVER_CERTIFICATE_DATABASE_SERVER_CERTIFICATE_DATABASE_H_ |