blob: 1893ed6e8a400b2bc53457b685e1beffcfe577b1 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Adrien Béraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#pragma once
18
Adrien Béraud612b55b2023-05-29 10:42:04 -040019#include <opendht/crypto.h>
20
21#include <string>
22#include <vector>
23#include <map>
24#include <set>
25#include <future>
26#include <mutex>
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040027#include <filesystem>
Adrien Béraud612b55b2023-05-29 10:42:04 -040028
29namespace crypto = ::dht::crypto;
30
31namespace dht {
32namespace log {
Adrien Béraud9132a812023-07-21 11:20:40 -040033struct Logger;
Adrien Béraud612b55b2023-05-29 10:42:04 -040034}
35}
36
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040037namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040038
39using Logger = dht::log::Logger;
40namespace tls {
41
42enum class TrustStatus { UNTRUSTED = 0, TRUSTED };
43TrustStatus trustStatusFromStr(const char* str);
44const char* statusToStr(TrustStatus s);
45
46/**
47 * Global certificate store.
48 * Stores system root CAs and any other encountred certificate
49 */
50class CertificateStore
51{
52public:
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040053 explicit CertificateStore(const std::filesystem::path& path, std::shared_ptr<Logger> logger);
Adrien Béraud612b55b2023-05-29 10:42:04 -040054
55 std::vector<std::string> getPinnedCertificates() const;
56 /**
57 * Return certificate (with full chain)
58 */
59 std::shared_ptr<crypto::Certificate> getCertificate(const std::string& cert_id);
60 std::shared_ptr<crypto::Certificate> getCertificateLegacy(const std::string& dataDir, const std::string& cert_id);
61
62 std::shared_ptr<crypto::Certificate> findCertificateByName(
63 const std::string& name, crypto::NameType type = crypto::NameType::UNKNOWN) const;
64 std::shared_ptr<crypto::Certificate> findCertificateByUID(const std::string& uid) const;
65 std::shared_ptr<crypto::Certificate> findIssuer(
66 const std::shared_ptr<crypto::Certificate>& crt) const;
67
68 std::vector<std::string> pinCertificate(const std::vector<uint8_t>& crt,
69 bool local = true) noexcept;
70 std::vector<std::string> pinCertificate(crypto::Certificate&& crt, bool local = true);
71 std::vector<std::string> pinCertificate(const std::shared_ptr<crypto::Certificate>& crt,
72 bool local = true);
73 bool unpinCertificate(const std::string&);
74
75 void pinCertificatePath(const std::string& path,
76 std::function<void(const std::vector<std::string>&)> cb = {});
77 unsigned unpinCertificatePath(const std::string&);
78
79 bool setTrustedCertificate(const std::string& id, TrustStatus status);
80 std::vector<gnutls_x509_crt_t> getTrustedCertificates() const;
81
82 void pinRevocationList(const std::string& id,
83 const std::shared_ptr<dht::crypto::RevocationList>& crl);
84 void pinRevocationList(const std::string& id, dht::crypto::RevocationList&& crl)
85 {
86 pinRevocationList(id,
87 std::make_shared<dht::crypto::RevocationList>(
88 std::forward<dht::crypto::RevocationList>(crl)));
89 }
90 void pinOcspResponse(const dht::crypto::Certificate& cert);
91
92 void loadRevocations(crypto::Certificate& crt) const;
93
94 const std::shared_ptr<Logger>& logger() const {
95 return logger_;
96 }
97
98private:
99 //NON_COPYABLE(CertificateStore);
100
101
102 unsigned loadLocalCertificates();
103 void pinRevocationList(const std::string& id, const dht::crypto::RevocationList& crl);
104 std::shared_ptr<Logger> logger_;
105
Adrien Béraud2a4e73d2023-08-27 12:53:55 -0400106 const std::filesystem::path certPath_;
107 const std::filesystem::path crlPath_;
108 const std::filesystem::path ocspPath_;
Adrien Béraud612b55b2023-05-29 10:42:04 -0400109
110 mutable std::mutex lock_;
111 std::map<std::string, std::shared_ptr<crypto::Certificate>> certs_;
112 std::map<std::string, std::vector<std::weak_ptr<crypto::Certificate>>> paths_;
113
114 // globally trusted certificates (root CAs)
115 std::vector<std::shared_ptr<crypto::Certificate>> trustedCerts_;
116};
117
118/**
119 * Keeps track of the allowed and trust status of certificates
120 * Trusted is the status of top certificates we trust to build our
121 * certificate chain: root CAs and other configured CAs.
122 *
123 * Allowed is the status of certificates we accept for incoming
124 * connections.
125 */
126class TrustStore
127{
128public:
129 explicit TrustStore(CertificateStore& certStore)
130 : certStore_(certStore)
131 {}
132
133 enum class PermissionStatus { UNDEFINED = 0, ALLOWED, BANNED };
134
135 static PermissionStatus statusFromStr(const char* str);
136 static const char* statusToStr(PermissionStatus s);
137
138 bool addRevocationList(dht::crypto::RevocationList&& crl);
139
140 bool setCertificateStatus(const std::string& cert_id, const PermissionStatus status);
141 bool setCertificateStatus(const std::shared_ptr<crypto::Certificate>& cert,
142 PermissionStatus status,
143 bool local = true);
144
145 PermissionStatus getCertificateStatus(const std::string& cert_id) const;
146
147 std::vector<std::string> getCertificatesByStatus(PermissionStatus status) const;
148
149 /**
150 * Check that the certificate is allowed (valid and permited) for contact.
151 * Valid means the certificate chain matches with our CA list,
152 * has valid signatures, expiration dates etc.
153 * Permited means at least one of the certificate in the chain is
154 * ALLOWED (if allowPublic is false), and none is BANNED.
155 *
156 * @param crt the end certificate of the chain to check
157 * @param allowPublic if false, requires at least one ALLOWED certificate.
158 * (not required otherwise). In any case a BANNED
159 * certificate means permission refusal.
160 * @return true if the certificate is valid and permitted.
161 */
162 bool isAllowed(const crypto::Certificate& crt, bool allowPublic = false);
163
164private:
165 TrustStore(const TrustStore& o) = delete;
166 TrustStore& operator=(const TrustStore& o) = delete;
167 TrustStore(TrustStore&& o) = delete;
168 TrustStore& operator=(TrustStore&& o) = delete;
169
170 void updateKnownCerts();
171 bool setCertificateStatus(std::shared_ptr<crypto::Certificate> cert,
172 const std::string& cert_id,
173 const TrustStore::PermissionStatus status,
174 bool local);
175 void setStoreCertStatus(const crypto::Certificate& crt, bool status);
176 void rebuildTrust();
177
178 struct Status
179 {
180 bool allowed;
181 };
182
183 // unknown certificates with known status
184 mutable std::recursive_mutex mutex_;
185 std::map<std::string, Status> unknownCertStatus_;
186 std::map<std::string, std::pair<std::shared_ptr<crypto::Certificate>, Status>> certStatus_;
187 dht::crypto::TrustList allowed_;
188 CertificateStore& certStore_;
189};
190
191} // namespace tls
Sébastien Blin464bdff2023-07-19 08:02:53 -0400192} // namespace dhtnet