Ring Daemon
Loading...
Searching...
No Matches
tlsvalidator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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 : std::uint8_t {
142 BOOLEAN,
143 ISO_DATE,
144 CUSTOM,
145 NUMBER,
146 COUNT__,
147 };
148
159 enum class CheckValues : std::uint8_t {
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,
192 const std::vector<std::vector<uint8_t>>& certificate_chain_raw);
193
194 TlsValidator(const dhtnet::tls::CertificateStore& certStore, const std::vector<uint8_t>& certificate_raw);
195
196 TlsValidator(const dhtnet::tls::CertificateStore& certStore, const std::shared_ptr<dht::crypto::Certificate>&);
197
199
200 bool hasCa() const;
201
202 bool isValid(bool verbose = false);
203
204 // Security checks
226
227 // Certificate details
252
254
255 std::map<std::string, std::string> getSerializedChecks();
256
257 std::map<std::string, std::string> getSerializedDetails();
258
259 std::shared_ptr<dht::crypto::Certificate> getCertificate() const { return x509crt_; }
260
261private:
262 // Enum class names
263 static const EnumClassNames<CertificateCheck> CertificateCheckNames;
264
265 static const EnumClassNames<CertificateDetails> CertificateDetailsNames;
266
267 static const EnumClassNames<const CheckValuesType> CheckValuesTypeNames;
268
269 static const EnumClassNames<CheckValues> CheckValuesNames;
270
275
280
284 static const Matrix2D<CheckValuesType, CheckValues, bool> acceptedCheckValuesResult;
285
286 static const Matrix1D<CertificateCheck, CheckValuesType> enforcedCheckType;
287
288 const dhtnet::tls::CertificateStore& certStore_;
289 std::string certificatePath_;
290 std::string privateKeyPath_;
291 std::string caListPath_ {};
292
293 std::vector<uint8_t> certificateContent_;
294
295 std::shared_ptr<dht::crypto::Certificate> x509crt_;
296
297 bool certificateFileFound_ {false};
298 bool certificateFound_ {false};
299 bool privateKeyFound_ {false};
300 bool privateKeyPassword_ {false};
301 bool privateKeyMatch_ {false};
302
303 bool caChecked_ {false};
304 unsigned int caValidationOutput_ {
305 0}; // 0 means "no flags set", where flags are ones from gnutls_certificate_status_t
306
307 mutable char copy_buffer[4096];
308
312 std::string getStringValue(const CertificateCheck check, const CheckResult& result);
313
314 // Helper
315 unsigned int compareToCa();
316
317public:
318#if 0 // TODO reimplement this method. do not use it as it
326 static int verifyHostnameCertificate(const std::string& host,
327 const uint16_t port);
328#endif
329
330}; // TlsValidator
331
332} // namespace tls
333} // 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.
@ UNSUPPORTED
Equivalent of a boolean "false"
@ FAILED
Equivalent of a boolean "true"
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.
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.
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 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.
CheckValuesType
Categories of possible values for each CertificateCheck.
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.
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 exist()
The file has been found.
CheckResult valid()
The certificate is invalid compared to the authority.
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()
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:50
void emitSignal(Args... args)
Definition jami_signal.h:64
This generic class represents a multidimensional enum class array.