Ring Daemon 16.0.0
Loading...
Searching...
No Matches
tlsvalidator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "enumclass_utils.h"
20#include <dhtnet/certstore.h>
21
22#include <string>
23#include <vector>
24#include <memory>
25
26// OpenDHT
27namespace dht {
28namespace crypto {
29struct Certificate;
30}
31} // namespace dht
32
33namespace jami {
34namespace tls {
35
36#if !defined(S_IRWXG)
37#define S_IRWXG 00070
38#endif /* S_IRWXG */
39#if !defined(S_IRGRP)
40#define S_IRGRP 00040
41#endif /* S_IRGRP */
42#if !defined(S_IWGRP)
43#define S_IWGRP 00020
44#endif /* S_IWGRP */
45#if !defined(S_IXGRP)
46#define S_IXGRP 00010
47#endif /* S_IXGRP */
48#if !defined(S_IRWXO)
49#define S_IRWXO 00007
50#endif /* S_IRWXO */
51#if !defined(S_IROTH)
52#define S_IROTH 00004
53#endif /* S_IROTH */
54#if !defined(S_IWOTH)
55#define S_IWOTH 00002
56#endif /* S_IWOTH */
57#if !defined(S_IXOTH)
58#define S_IXOTH 00001
59#endif /* S_IXOTH */
60
61class TlsValidatorException : public std::runtime_error
62{
63public:
64 TlsValidatorException(const std::string& str)
65 : std::runtime_error(str) {};
66};
67
69{
70public:
105
137
141 enum class CheckValuesType {
142 BOOLEAN,
143 ISO_DATE,
144 CUSTOM,
145 NUMBER,
146 COUNT__,
147 };
148
159 enum class CheckValues {
160 PASSED,
161 FAILED,
163 ISO_DATE,
164 CUSTOM,
165 NUMBER,
166 COUNT__,
167 };
168
176 using CheckResult = std::pair<CheckValues, std::string>;
177
185 TlsValidator(const dhtnet::tls::CertificateStore& certStore,
186 const std::string& certificate,
187 const std::string& privatekey = "",
188 const std::string& privatekeyPasswd = "",
189 const std::string& caList = "");
190
191 TlsValidator(const dhtnet::tls::CertificateStore& certStore, const std::vector<std::vector<uint8_t>>& certificate_chain_raw);
192
193 TlsValidator(const dhtnet::tls::CertificateStore& certStore, const std::vector<uint8_t>& certificate_raw);
194
195 TlsValidator(const dhtnet::tls::CertificateStore& certStore, const std::shared_ptr<dht::crypto::Certificate>&);
196
198
199 bool hasCa() const;
200
201 bool isValid(bool verbose = false);
202
203 // Security checks
225
226 // Certificate details
251
253
254 std::map<std::string, std::string> getSerializedChecks();
255
256 std::map<std::string, std::string> getSerializedDetails();
257
258 std::shared_ptr<dht::crypto::Certificate> getCertificate() const { return x509crt_; }
259
260private:
261 // Enum class names
262 static const EnumClassNames<CertificateCheck> CertificateCheckNames;
263
264 static const EnumClassNames<CertificateDetails> CertificateDetailsNames;
265
266 static const EnumClassNames<const CheckValuesType> CheckValuesTypeNames;
267
268 static const EnumClassNames<CheckValues> CheckValuesNames;
269
274
279
283 static const Matrix2D<CheckValuesType, CheckValues, bool> acceptedCheckValuesResult;
284
285 static const Matrix1D<CertificateCheck, CheckValuesType> enforcedCheckType;
286
287 const dhtnet::tls::CertificateStore& certStore_;
288 std::string certificatePath_;
289 std::string privateKeyPath_;
290 std::string caListPath_ {};
291
292 std::vector<uint8_t> certificateContent_;
293
294 std::shared_ptr<dht::crypto::Certificate> x509crt_;
295
296 bool certificateFileFound_ {false};
297 bool certificateFound_ {false};
298 bool privateKeyFound_ {false};
299 bool privateKeyPassword_ {false};
300 bool privateKeyMatch_ {false};
301
302 bool caChecked_ {false};
303 unsigned int caValidationOutput_ {
304 0}; // 0 means "no flags set", where flags are ones from gnutls_certificate_status_t
305
306 mutable char copy_buffer[4096];
307
311 std::string getStringValue(const CertificateCheck check, const CheckResult result);
312
313 // Helper
314 unsigned int compareToCa();
315
316public:
317#if 0 // TODO reimplement this method. do not use it as it
325 static int verifyHostnameCertificate(const std::string& host,
326 const uint16_t port);
327#endif
328
329}; // TlsValidator
330
331} // namespace tls
332} // namespace jami
TlsValidatorException(const std::string &str)
CheckResult getSignatureAlgorithm()
Return the algorithm used to sign the Key.
CheckResult privateKeyStoragePermissions()
CheckResult requirePrivateKeyPassword()
If the key need decryption.
CheckResult getIssuerUID()
If the certificate is not self signed, return the issuer UID.
CheckResult privateKeyDirectoryPermissions()
CheckResult getSerialNumber()
Return the certificate serial number.
CheckResult validAuthority()
The provided authority is invalid.
CheckResult activated()
If the activation value is in the past.
CheckResult keyMatch()
The provided key can be used along with the certificate.
CertificateCheck
All validation fields.
@ NOT_SELF_SIGNED
This certificate has been signed with a brute-force-able method
@ PUBLIC_KEY_STORAGE_PERMISSION
The file hosting the private key isn't correctly secured.
@ EXIST
Some operating systems require keys to have extra attributes
@ AUTHORITY_MISMATCH
The certificate has been revoked by the authority
@ PUBLIC_KEY_DIRECTORY_PERMISSIONS
The folder storing the private key isn't correctly secured
@ NOT_REVOKED
Some operating systems provide a list of trusted authorities, use it.
@ EXPIRED
This certificate has a build in private key
@ PRIVATE_KEY_DIRECTORY_PERMISSIONS
The file hosting the public key isn't correctly secured.
@ PUBLIC_KEY_STORAGE_LOCATION
Some operating systems have extra policies for certificate storage
@ COUNT__
The certificate has not been activated yet
@ STRONG_SIGNING
This certificate is past its expiration date
@ PUBLIC_KEY_SELINUX_ATTRIBUTES
Some operating systems require keys to have extra attributes
@ UNEXPECTED_OWNER
The certificate and authority mismatch.
@ PRIVATE_KEY_STORAGE_PERMISSION
The public and private keys provided don't match
@ VALID_AUTHORITY
The file is not a certificate
@ KNOWN_AUTHORITY
The claimed authority did not sign the certificate
@ VALID
The certificate file doesn't exist or is not accessible
@ NOT_ACTIVATED
The certificate has an expected owner
@ PRIVATE_KEY_STORAGE_LOCATION
The folder storing the public key isn't correctly secured
@ PRIVATE_KEY_SELINUX_ATTRIBUTES
Some operating systems have extra policies for certificate storage
@ KEY_MATCH
This certificate has been self signed
CheckResult getN()
The 'N' section of a DN (RFC4514)
CheckResult getO()
The 'O' section of a DN (RFC4514)
CheckResult notRevoked()
Check if the certificate has been revoked.
CheckResult getActivationDate()
Get the activation date.
CheckResult notSelfSigned()
The certificate is not self signed.
CheckResult getVersionNumber()
Return the certificate version.
CheckResult strongSigning()
If the algorithm used to sign the certificate is considered weak by modern standard.
CheckResult getPublicKeyId()
Return an hexadecimal identifier.
CheckResult authorityMatch()
Check if the authority match the certificate.
std::shared_ptr< dht::crypto::Certificate > getCertificate() const
CheckResult getIssuer()
If the certificate is not self signed, return the issuer.
bool hasCa() const
A certificate authority has been provided.
CheckResult isCA()
If the certificate is not self signed, return the issuer.
CheckResult notExpired()
Check if the certificate is not expired.
CheckResult getUID()
The 'UID' section of a DN (RFC4514)
CheckResult getIssuerO()
If the certificate is not self signed, return the issuer O.
CheckResult getSubjectKeyAlgorithm()
The algorithm used to sign the certificate details (rather than the certificate itself)
bool isValid(bool verbose=false)
Check if all boolean check passed return true if there was no FAILED checks.
CheckResult getIssuerDN()
If the certificate is not self signed, return the issuer DN (RFC4514)
CheckResult outgoingServer()
The expected outgoing server domain.
CheckResult knownAuthority()
When an account require an authority known by the system (like /usr/share/ssl/certs) then the whole c...
CheckResult privateKeySelinuxAttributes()
SELinux provide additional key protection mechanism.
std::pair< CheckValues, std::string > CheckResult
CheckResult getPublicSignature()
An hexadecimal representation of the signature.
CheckResult getExpirationDate()
Get the expiration date.
CheckResult exist()
The file has been found.
CheckResult valid()
The certificate is invalid compared to the authority.
@ UNSUPPORTED
Equivalent of a boolean "false"
@ FAILED
Equivalent of a boolean "true"
CheckResult getMd5Fingerprint()
Compute the key fingerprint.
CheckResult hasPrivateKey()
Check if the Validator have access to a private key.
void setCaTlsValidator(const TlsValidator &validator)
std::map< std::string, std::string > getSerializedChecks()
Convert all checks results into a string map.
CheckResult getSubjectKey()
The subject public key.
CheckResult publicKeyStoragePermissions()
CheckValuesType
Categories of possible values for each CertificateCheck.
CertificateDetails
Informative fields about a certificate.
@ REQUIRE_PRIVATE_KEY_PASSWORD
The certificate activation date
@ IS_CA
The hostname/outgoing server used for this certificate
@ PUBLIC_SIGNATURE
Does the private key require a password.
@ ACTIVATION_DATE
The certificate expiration date
CheckResult getIssuerN()
If the certificate is not self signed, return the issuer N.
CheckResult privateKeyStorageLocation()
Certificate should be located in specific path on some operating systems.
CheckResult publicKeyStorageLocation()
Certificate should be located in specific path on some operating systems.
CheckResult getIssuerCN()
If the certificate is not self signed, return the issuer CN.
CheckResult getSha1Fingerprint()
Compute the key fingerprint.
std::map< std::string, std::string > getSerializedDetails()
Get a map with all common certificate details.
CheckResult publicKeySelinuxAttributes()
SELinux provide additional key protection mechanism.
CheckResult expectedOwner()
The CA and certificate provide conflicting ownership information.
CheckResult publicKeyDirectoryPermissions()
CheckResult getCN()
The 'CN' section of a DN (RFC4514)
Definition account.h:55
void emitSignal(Args... args)
Definition ring_signal.h:64
This generic class represents a multidimensional enum class array.