blob: 4fea8d1a1d79fd00c8388280bc59f96a59addfcb [file] [log] [blame]
Bob Beckbc97b7a2023-04-18 08:35:15 -06001// Copyright 2016 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 BSSL_PKI_TRUST_STORE_IN_MEMORY_H_
6#define BSSL_PKI_TRUST_STORE_IN_MEMORY_H_
7
Bob Beckbc97b7a2023-04-18 08:35:15 -06008#include <unordered_map>
9
Bob Beck3cd30cc2023-11-22 16:59:00 -070010#include <openssl/base.h>
Bob Beckbc97b7a2023-04-18 08:35:15 -060011
12#include "trust_store.h"
13
14namespace bssl {
15
16// A very simple implementation of a TrustStore, which contains a set of
17// certificates and their trustedness.
18class OPENSSL_EXPORT TrustStoreInMemory : public TrustStore {
19 public:
20 TrustStoreInMemory();
21
Bob Beck5c7a2a02023-11-20 17:28:21 -070022 TrustStoreInMemory(const TrustStoreInMemory &) = delete;
23 TrustStoreInMemory &operator=(const TrustStoreInMemory &) = delete;
Bob Beckbc97b7a2023-04-18 08:35:15 -060024
25 ~TrustStoreInMemory() override;
26
27 // Returns whether the TrustStore is in the initial empty state.
28 bool IsEmpty() const;
29
30 // Empties the trust store, resetting it to original state.
31 void Clear();
32
33 // Adds a certificate with the specified trust settings. Both trusted and
34 // distrusted certificates require a full DER match.
35 void AddCertificate(std::shared_ptr<const ParsedCertificate> cert,
Bob Beck5c7a2a02023-11-20 17:28:21 -070036 const CertificateTrust &trust);
Bob Beckbc97b7a2023-04-18 08:35:15 -060037
38 // Adds a certificate as a trust anchor (only the SPKI and subject will be
39 // used during verification).
40 void AddTrustAnchor(std::shared_ptr<const ParsedCertificate> cert);
41
42 // Adds a certificate as a trust anchor which will have expiration enforced.
43 // See VerifyCertificateChain for details.
44 void AddTrustAnchorWithExpiration(
45 std::shared_ptr<const ParsedCertificate> cert);
46
47 // Adds a certificate as a trust anchor and extracts anchor constraints from
48 // the certificate. See VerifyCertificateChain for details.
49 void AddTrustAnchorWithConstraints(
50 std::shared_ptr<const ParsedCertificate> cert);
51
52 // TODO(eroman): This is marked "ForTest" as the current implementation
53 // requires an exact match on the certificate DER (a wider match by say
54 // issuer/serial is probably what we would want for a real implementation).
55 void AddDistrustedCertificateForTest(
56 std::shared_ptr<const ParsedCertificate> cert);
57
58 // Adds a certificate to the store, that is neither trusted nor untrusted.
59 void AddCertificateWithUnspecifiedTrust(
60 std::shared_ptr<const ParsedCertificate> cert);
61
62 // TrustStore implementation:
Bob Beck5c7a2a02023-11-20 17:28:21 -070063 void SyncGetIssuersOf(const ParsedCertificate *cert,
64 ParsedCertificateList *issuers) override;
65 CertificateTrust GetTrust(const ParsedCertificate *cert) override;
Bob Beckbc97b7a2023-04-18 08:35:15 -060066
67 // Returns true if the trust store contains the given ParsedCertificate
68 // (matches by DER).
Bob Beck5c7a2a02023-11-20 17:28:21 -070069 bool Contains(const ParsedCertificate *cert) const;
Bob Beckbc97b7a2023-04-18 08:35:15 -060070
71 private:
72 struct Entry {
73 Entry();
Bob Beck5c7a2a02023-11-20 17:28:21 -070074 Entry(const Entry &other);
Bob Beckbc97b7a2023-04-18 08:35:15 -060075 ~Entry();
76
77 std::shared_ptr<const ParsedCertificate> cert;
78 CertificateTrust trust;
79 };
80
81 // Multimap from normalized subject -> Entry.
82 std::unordered_multimap<std::string_view, Entry> entries_;
83
84 // Returns the `Entry` matching `cert`, or `nullptr` if not in the trust
85 // store.
Bob Beck5c7a2a02023-11-20 17:28:21 -070086 const Entry *GetEntry(const ParsedCertificate *cert) const;
Bob Beckbc97b7a2023-04-18 08:35:15 -060087};
88
Bob Beck5c7a2a02023-11-20 17:28:21 -070089} // namespace bssl
Bob Beckbc97b7a2023-04-18 08:35:15 -060090
91#endif // BSSL_PKI_TRUST_STORE_IN_MEMORY_H_