blob: cfa0fc2c81943d81f9a90f32922e180a2769567a [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
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"
Hubert Chaoe28ab1c2024-09-18 19:32:2513#include "chrome/browser/net/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
20// Wraps the SQLite database that provides on-disk storage for user-configured
21// TLS certificates. This class is expected to be created and accessed on a
22// backend sequence.
23class ServerCertificateDatabase {
24 public:
Hubert Chaoe28ab1c2024-09-18 19:32:2525 struct CertInformation {
Matt Mueller778dfcb2024-12-10 19:02:1426 // Initializes a CertInformation object with the `der_cert` and calculates
27 // the `sha256hash_hex` from the supplied cert.
28 explicit CertInformation(base::span<const uint8_t> cert);
Hubert Chaoe28ab1c2024-09-18 19:32:2529 CertInformation();
30 ~CertInformation();
31 CertInformation(CertInformation&&);
32 CertInformation& operator=(CertInformation&& other);
33
34 std::string sha256hash_hex;
35 std::vector<uint8_t> der_cert;
36 chrome_browser_server_certificate_database::CertificateMetadata
37 cert_metadata;
38 };
39
40 // Opens the database in `storage_dir`, creating it if one does not exist.
41 // `storage_dir` will generally be in the Profile directory.
Hubert Chao52a323602024-09-11 14:40:1542 explicit ServerCertificateDatabase(const base::FilePath& storage_dir);
43
44 ServerCertificateDatabase(const ServerCertificateDatabase&) = delete;
45 ServerCertificateDatabase& operator=(const ServerCertificateDatabase&) =
46 delete;
47 ~ServerCertificateDatabase();
48
Hubert Chao7299aa6b2024-10-03 18:20:2249 static std::optional<bssl::CertificateTrustType> GetUserCertificateTrust(
50 const net::ServerCertificateDatabase::CertInformation& cert_info);
51
Hubert Chaod4b21142024-11-22 17:00:3752 // Insert new certificates into the database, or if some of the certificates
53 // are already present (as indicated by cert_info.sha256hash_hex), update the
Hubert Chaoe28ab1c2024-09-18 19:32:2554 // entry in the database.
Hubert Chaod4b21142024-11-22 17:00:3755 bool InsertOrUpdateCerts(const std::vector<CertInformation>& cert_infos);
Hubert Chaoe28ab1c2024-09-18 19:32:2556
57 // Retrieve all of the certificates from the database.
58 std::vector<CertInformation> RetrieveAllCertificates();
59
Carlos IL0722522c72024-10-22 18:50:0360 uint32_t RetrieveCertificatesCount();
61
Carlos IL1de43532024-10-24 18:27:0062 // Delete the certificate with a matching hash from the database.
63 bool DeleteCertificate(const std::string& sha256hash_hex);
64
Hubert Chao52a323602024-09-11 14:40:1565 private:
66 sql::InitStatus InitInternal(const base::FilePath& storage_dir);
67
68 // The underlying SQL database.
69 sql::Database db_ GUARDED_BY_CONTEXT(sequence_checker_);
70 SEQUENCE_CHECKER(sequence_checker_);
71};
72
73} // namespace net
74
75#endif // CHROME_BROWSER_NET_SERVER_CERTIFICATE_DATABASE_H_