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 | |
| 5 | #ifndef CHROME_BROWSER_NET_SERVER_CERTIFICATE_DATABASE_H_ |
| 6 | #define CHROME_BROWSER_NET_SERVER_CERTIFICATE_DATABASE_H_ |
| 7 | |
| 8 | #include "base/files/file_path.h" |
| 9 | #include "base/sequence_checker.h" |
| 10 | #include "base/thread_annotations.h" |
Hubert Chao | e28ab1c | 2024-09-18 19:32:25 | [diff] [blame] | 11 | #include "chrome/browser/net/server_certificate_database.pb.h" |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 12 | #include "sql/database.h" |
| 13 | #include "sql/init_status.h" |
| 14 | |
| 15 | namespace net { |
| 16 | |
| 17 | // Wraps the SQLite database that provides on-disk storage for user-configured |
| 18 | // TLS certificates. This class is expected to be created and accessed on a |
| 19 | // backend sequence. |
| 20 | class ServerCertificateDatabase { |
| 21 | public: |
Hubert Chao | e28ab1c | 2024-09-18 19:32:25 | [diff] [blame] | 22 | struct CertInformation { |
| 23 | CertInformation(); |
| 24 | ~CertInformation(); |
| 25 | CertInformation(CertInformation&&); |
| 26 | CertInformation& operator=(CertInformation&& other); |
| 27 | |
| 28 | std::string sha256hash_hex; |
| 29 | std::vector<uint8_t> der_cert; |
| 30 | chrome_browser_server_certificate_database::CertificateMetadata |
| 31 | cert_metadata; |
| 32 | }; |
| 33 | |
| 34 | // Opens the database in `storage_dir`, creating it if one does not exist. |
| 35 | // `storage_dir` will generally be in the Profile directory. |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 36 | explicit ServerCertificateDatabase(const base::FilePath& storage_dir); |
| 37 | |
| 38 | ServerCertificateDatabase(const ServerCertificateDatabase&) = delete; |
| 39 | ServerCertificateDatabase& operator=(const ServerCertificateDatabase&) = |
| 40 | delete; |
| 41 | ~ServerCertificateDatabase(); |
| 42 | |
Hubert Chao | e28ab1c | 2024-09-18 19:32:25 | [diff] [blame] | 43 | // Insert a new certificate into the database, or if the certificate is |
| 44 | // already present (as indicated by cert_info.sha256hash_hex), update the |
| 45 | // entry in the database. |
| 46 | bool InsertOrUpdateCert(const CertInformation& cert_info); |
| 47 | |
| 48 | // Retrieve all of the certificates from the database. |
| 49 | std::vector<CertInformation> RetrieveAllCertificates(); |
| 50 | |
Hubert Chao | 52a32360 | 2024-09-11 14:40:15 | [diff] [blame] | 51 | private: |
| 52 | sql::InitStatus InitInternal(const base::FilePath& storage_dir); |
| 53 | |
| 54 | // The underlying SQL database. |
| 55 | sql::Database db_ GUARDED_BY_CONTEXT(sequence_checker_); |
| 56 | SEQUENCE_CHECKER(sequence_checker_); |
| 57 | }; |
| 58 | |
| 59 | } // namespace net |
| 60 | |
| 61 | #endif // CHROME_BROWSER_NET_SERVER_CERTIFICATE_DATABASE_H_ |