morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
qt_tls.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4#include <QSslCertificate>
5#include <QSslConfiguration>
6#include <QSslSocket>
7
8namespace morph::qt {
9
10/// @brief Builds a TLS client configuration that verifies the server against
11/// the system/CA trust store.
12///
13/// This is the recommended production default: pass the result as the `tls`
14/// argument to `QtWebSocketBackend`'s constructor to connect over `wss://`
15/// with full peer authentication. Prefer `tlsPinnedConfig()` instead for a
16/// self-signed deployment.
17///
18/// @return A `QSslConfiguration` derived from `QSslConfiguration::defaultConfiguration()`
19/// with `peerVerifyMode` set to `QSslSocket::VerifyPeer`.
20[[nodiscard]] inline QSslConfiguration tlsVerifyingConfig() {
21 QSslConfiguration cfg = QSslConfiguration::defaultConfiguration();
22 cfg.setPeerVerifyMode(QSslSocket::VerifyPeer);
23 return cfg;
24}
25
26/// @brief Builds a TLS client configuration that verifies the server against
27/// one pinned certificate instead of the system CA store.
28///
29/// The correct choice for a self-signed deployment: rather than disabling
30/// verification (`tlsInsecureNoVerify()`), this keeps `QSslSocket::VerifyPeer`
31/// enabled and trusts exactly @p pinned, so a self-signed server is
32/// authenticated rather than merely encrypted-to. A peer presenting any other
33/// certificate — including a MITM's own self-signed certificate — fails the
34/// TLS handshake.
35///
36/// @param pinned The exact certificate the server is expected to present.
37/// @return A `QSslConfiguration` with `peerVerifyMode` `QSslSocket::VerifyPeer`
38/// and @p pinned as the sole trusted CA certificate.
39[[nodiscard]] inline QSslConfiguration tlsPinnedConfig(const QSslCertificate& pinned) {
40 QSslConfiguration cfg = QSslConfiguration::defaultConfiguration();
41 cfg.setPeerVerifyMode(QSslSocket::VerifyPeer);
42 cfg.setCaCertificates({pinned});
43 return cfg;
44}
45
46/// @brief Builds a TLS client configuration that encrypts but does not
47/// authenticate the server.
48///
49/// Kept for local development and tests only — the name states the hazard so
50/// it can be grepped for in a security review. A client built with this
51/// configuration completes a TLS handshake with *any* server presenting *any*
52/// certificate, which is MITM-vulnerable. Prefer `tlsPinnedConfig()` for
53/// self-signed deployments and `tlsVerifyingConfig()` for CA-issued
54/// certificates.
55///
56/// @return A `QSslConfiguration` with `peerVerifyMode` set to `QSslSocket::VerifyNone`.
57[[nodiscard]] inline QSslConfiguration tlsInsecureNoVerify() {
58 QSslConfiguration cfg = QSslConfiguration::defaultConfiguration();
59 cfg.setPeerVerifyMode(QSslSocket::VerifyNone);
60 return cfg;
61}
62
63} // namespace morph::qt