libfilezilla
Loading...
Searching...
No Matches
tls_layer.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_TLS_LAYER_HEADER
2#define LIBFILEZILLA_TLS_LAYER_HEADER
3
8#include "socket.hpp"
9
10namespace fz {
11class logger_interface;
12class tls_system_trust_store;
13class tls_session_info;
14
15class tls_layer;
16class tls_layer_impl;
17
18struct certificate_verification_event_type;
19
25
26enum class tls_ver
27{
28 v1_0,
29 v1_1,
30 v1_2,
31 v1_3
32};
33
34enum class tls_server_flags : unsigned int
35{
36 none = 0,
37
39 no_auto_ticket = 0x1,
40
43};
44
45inline bool operator&(tls_server_flags lhs, tls_server_flags rhs) {
46 return (static_cast<std::underlying_type_t<tls_server_flags>>(lhs) & static_cast<std::underlying_type_t<tls_server_flags>>(rhs)) != 0;
47}
48inline tls_server_flags operator|(tls_server_flags lhs, tls_server_flags rhs) {
49 return static_cast<tls_server_flags>(static_cast<std::underlying_type_t<tls_server_flags>>(lhs) | static_cast<std::underlying_type_t<tls_server_flags>>(rhs));
50}
51inline tls_server_flags& operator|=(tls_server_flags & lhs, tls_server_flags rhs) {
52 lhs = lhs | rhs;
53 return lhs;
54}
55
56
57enum class tls_client_flags : unsigned int
58{
59 none = 0,
60
63};
64
65inline bool operator&(tls_client_flags lhs, tls_client_flags rhs) {
66 return (static_cast<std::underlying_type_t<tls_client_flags>>(lhs) & static_cast<std::underlying_type_t<tls_client_flags>>(rhs)) != 0;
67}
68inline tls_client_flags operator|(tls_client_flags lhs, tls_client_flags rhs) {
69 return static_cast<tls_client_flags>(static_cast<std::underlying_type_t<tls_client_flags>>(lhs) | static_cast<std::underlying_type_t<tls_client_flags>>(rhs));
70}
71inline tls_client_flags& operator|=(tls_client_flags & lhs, tls_client_flags rhs) {
72 lhs = lhs | rhs;
73 return lhs;
74}
75
76
77
90class FZ_PUBLIC_SYMBOL tls_layer final : protected event_handler, public socket_layer
91{
92public:
93 tls_layer(event_loop& event_loop, event_handler* evt_handler, socket_interface& layer, tls_system_trust_store * system_trust_store, logger_interface& logger);
94 virtual ~tls_layer();
95
107 bool client_handshake(std::vector<uint8_t> const& required_certificate, std::vector<uint8_t> const& session_to_resume = std::vector<uint8_t>(), native_string const& session_hostname = native_string(), tls_client_flags flags = {});
108
125 bool client_handshake(event_handler *const verification_handler, std::vector<uint8_t> const& session_to_resume = std::vector<uint8_t>(), native_string const& session_hostname = native_string(), tls_client_flags flags = {});
126
142 bool server_handshake(std::vector<uint8_t> const& session_to_resume = {}, std::string_view const& preamble = {}, tls_server_flags flags = {});
143
145 std::vector<uint8_t> get_session_parameters() const;
146
148 std::vector<uint8_t> get_raw_certificate() const;
149
155 void set_verification_result(bool trusted);
156
157 std::string get_protocol() const;
158
159 std::string get_key_exchange() const;
160 std::string get_cipher() const;
161 std::string get_mac() const;
162 int get_algorithm_warnings() const;
163
165 bool resumed_session() const;
166
168 static std::string list_tls_ciphers(std::string const& priority);
169
178 bool set_certificate_file(native_string const& keyfile, native_string const& certsfile, native_string const& password, bool pem = true);
179
188 bool set_certificate(std::string_view const& key, std::string_view const& certs, native_string const& password, bool pem = true);
189
191 static std::string get_gnutls_version();
192
194 enum class cert_type {
195 any,
196 client,
197 server,
198 ca
199 };
200
209 static std::pair<std::string, std::string> generate_selfsigned_certificate(native_string const& password, std::string const& distinguished_name, std::vector<std::string> const& hostnames, cert_type type = cert_type::any, bool ecsda = true);
210
212 static std::pair<std::string, std::string> generate_ca_certificate(native_string const& password, std::string const& distinguished_name, duration const& lifetime = {}, bool ecdsa = true);
213
215 static std::pair<std::string, std::string> generate_csr(native_string const& password, std::string const& distinguished_name, std::vector<std::string> const& hostnames, bool csr_as_pem = true, cert_type type = cert_type::any);
216
222 static std::string generate_cert_from_csr(std::pair<std::string, std::string> const& issuer, native_string const& password, std::string const& csr, std::string const& distinguished_name = {}, std::vector<std::string> const& hostnames = {}, duration const& lifetime = {}, cert_type type = cert_type::any);
223
236 bool set_alpn(std::string_view const& alpn);
237 bool set_alpn(std::vector<std::string> const& alpns, bool server_priority = false);
238
241 void set_min_tls_ver(tls_ver ver);
242
247 void set_max_tls_ver(tls_ver ver);
248
250 std::string get_alpn() const;
251
254
255 bool is_server() const;
256
266
280 void set_unexpected_eof_cb(std::function<bool()> const& cb);
281 void set_unexpected_eof_cb(std::function<bool()> && cb);
282
283 virtual socket_state get_state() const override;
284
285 virtual int connect(native_string const& host, unsigned int port, address_type family = address_type::unknown) override;
286
287 virtual int read(void *buffer, unsigned int size, int& error) override;
288 virtual int write(void const* buffer, unsigned int size, int& error) override;
289
290 virtual int shutdown() override;
291
292 virtual int shutdown_read() override;
293
294 virtual void set_event_handler(event_handler* pEvtHandler, fz::socket_event_flag retrigger_block = socket_event_flag{}) override;
295
296private:
297 virtual void FZ_PRIVATE_SYMBOL operator()(event_base const& ev) override;
298
299 friend class tls_layer_impl;
300 std::unique_ptr<tls_layer_impl> impl_;
301};
302}
303
304#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:27
The duration class represents a time interval in milliseconds.
Definition: time.hpp:291
Common base class for all events.
Definition: event.hpp:23
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:34
Abstract interface for logging strings.
Definition: logger.hpp:51
This is the recommended event class.
Definition: event.hpp:68
Interface for sockets.
Definition: socket.hpp:375
A base class for socket layers.
Definition: socket.hpp:654
A Transport Layer Security (TLS) layer.
Definition: tls_layer.hpp:91
bool resumed_session() const
After a successful handshake, returns whether the session has been resumed.
void set_min_tls_ver(tls_ver ver)
Sets minimum allowed TLS version.
virtual int shutdown() override
Signals peers that we want to close the connections.
static std::pair< std::string, std::string > generate_ca_certificate(native_string const &password, std::string const &distinguished_name, duration const &lifetime={}, bool ecdsa=true)
Creates CA certificate.
static std::pair< std::string, std::string > generate_csr(native_string const &password, std::string const &distinguished_name, std::vector< std::string > const &hostnames, bool csr_as_pem=true, cert_type type=cert_type::any)
Creates a CSR.
bool set_certificate(std::string_view const &key, std::string_view const &certs, native_string const &password, bool pem=true)
Sets the certificate (and its chain) and the private key.
std::vector< uint8_t > get_raw_certificate() const
Gets the session's peer certificate in DER.
virtual void set_event_handler(event_handler *pEvtHandler, fz::socket_event_flag retrigger_block=socket_event_flag{}) override
The handler for any events generated (or forwarded) by this layer.
static std::string get_gnutls_version()
Returns the version of the loaded GnuTLS library, may be different than the version used at compile-t...
void set_unexpected_eof_cb(std::function< bool()> const &cb)
Sets a callback to control whether unexpected eof is seen as error.
std::vector< uint8_t > get_session_parameters() const
Gets session parameters for resumption.
bool server_handshake(std::vector< uint8_t > const &session_to_resume={}, std::string_view const &preamble={}, tls_server_flags flags={})
Starts shaking hand for a new TLS session as server.
void set_max_tls_ver(tls_ver ver)
Sets maximum allowed TLS versions.
static std::string generate_cert_from_csr(std::pair< std::string, std::string > const &issuer, native_string const &password, std::string const &csr, std::string const &distinguished_name={}, std::vector< std::string > const &hostnames={}, duration const &lifetime={}, cert_type type=cert_type::any)
Creates a certificate from a CSR.
int new_session_ticket()
If running as server with TLS1.3, send out a new session ticket before the next data payload.
void set_verification_result(bool trusted)
Must be called after having received certificate_verification_event.
bool client_handshake(std::vector< uint8_t > const &required_certificate, std::vector< uint8_t > const &session_to_resume=std::vector< uint8_t >(), native_string const &session_hostname=native_string(), tls_client_flags flags={})
Starts shaking hands for a new TLS session as client.
std::string get_alpn() const
After a successful handshake, returns which protocol, if any, has been negotiated.
static std::pair< std::string, std::string > generate_selfsigned_certificate(native_string const &password, std::string const &distinguished_name, std::vector< std::string > const &hostnames, cert_type type=cert_type::any, bool ecsda=true)
Creates a new private key and a self-signed certificate.
static std::string list_tls_ciphers(std::string const &priority)
Returns a human-readable list of all TLS ciphers available with the passed priority string.
cert_type
Type of certificate to create.
Definition: tls_layer.hpp:194
native_string get_hostname() const
If running as server, get the SNI sent by the client.
bool set_certificate_file(native_string const &keyfile, native_string const &certsfile, native_string const &password, bool pem=true)
Sets the file containing the certificate (and its chain) and the file with the corresponding private ...
bool client_handshake(event_handler *const verification_handler, std::vector< uint8_t > const &session_to_resume=std::vector< uint8_t >(), native_string const &session_hostname=native_string(), tls_client_flags flags={})
Starts shaking hands for a new TLS session as client.
virtual int shutdown_read() override
Check that all layers further down also have reached EOF.
bool set_alpn(std::string_view const &alpn)
Negotiate application protocol.
Opaque class to load the system trust store asynchronously.
Definition: tls_system_trust_store.hpp:30
type
Definition: logger.hpp:16
The namespace used by libfilezilla.
Definition: apply.hpp:17
tls_server_flags
Definition: tls_layer.hpp:35
@ no_auto_ticket
In TLS 1.3, do not automatically send PSKs after finishing handshake. Ignored if not TLS 1....
@ debug_no_tickets
Used in unit tests. Don't use in production.
simple_event< certificate_verification_event_type, tls_layer *, tls_session_info > certificate_verification_event
This event gets sent during the handshake with details about the session and the used certificate.
Definition: tls_layer.hpp:24
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:34
tls_client_flags
Definition: tls_layer.hpp:58
socket_state
State transitions are monotonically increasing.
Definition: socket.hpp:343
@ ecdsa
EC key type with P-256 as algorithm.
@ read
Data has become available.
@ write
data can be written.
socket_event_flag
The type of a socket event.
Definition: socket.hpp:35
Socket classes for networking.