blob: eb2fdf5e75026bef847222c2e5c9f982b18f5b9d [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
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 Chaoe28ab1c2024-09-18 19:32:2511#include "chrome/browser/net/server_certificate_database.pb.h"
Hubert Chao52a323602024-09-11 14:40:1512#include "sql/database.h"
13#include "sql/init_status.h"
14
15namespace 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.
20class ServerCertificateDatabase {
21 public:
Hubert Chaoe28ab1c2024-09-18 19:32:2522 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 Chao52a323602024-09-11 14:40:1536 explicit ServerCertificateDatabase(const base::FilePath& storage_dir);
37
38 ServerCertificateDatabase(const ServerCertificateDatabase&) = delete;
39 ServerCertificateDatabase& operator=(const ServerCertificateDatabase&) =
40 delete;
41 ~ServerCertificateDatabase();
42
Hubert Chaoe28ab1c2024-09-18 19:32:2543 // 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 Chao52a323602024-09-11 14:40:1551 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_