Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
TlsContext.hpp
Go to the documentation of this file.
1#pragma once
3#include <memory>
4#include <mutex>
5#include <string>
6#include <vector>
7#include <stdexcept>
8#include <openssl/ssl.h>
9#include <openssl/err.h>
11
12namespace orbit::network {
13
23public:
24 TlsContext(const std::string& cert_file, const std::string& key_file, config::HttpVersion http_version);
27 TlsContext(const config::TlsCertificate& default_cert, std::vector<config::TlsCertificate> sni_certs,
28 config::HttpVersion http_version);
30
31 // Delete copy semantics
32 TlsContext(const TlsContext&) = delete;
33 TlsContext& operator=(const TlsContext&) = delete;
34
35 SSL_CTX* get() const { return ctx_; }
37
45 bool reload(std::string* error = nullptr);
46
48 bool files_changed() const;
49
51 std::string certificate_for(const std::string& server_name) const;
52
54 void configure(SSL_CTX* ctx);
55
56 struct CertSet;
57
58private:
59 SSL_CTX* ctx_{nullptr};
62 std::vector<config::TlsCertificate> sni_certs_;
63
64 mutable std::mutex mutex_;
65 std::shared_ptr<const CertSet> certs_;
66 std::string stamp_; // sizes and modification times of the loaded files
67
68 std::shared_ptr<const CertSet> current() const;
69 std::string file_stamp() const;
70 static int servername_cb(SSL* ssl, int* alert, void* arg);
71};
72
74public:
81 explicit ClientTlsContext(bool verify_peer = true, const std::string& ca_file = "");
83
86
87 SSL_CTX* get() const { return ctx_; }
88
89private:
90 SSL_CTX* ctx_{nullptr};
91};
92
93} // namespace network
Definition TlsContext.hpp:73
ClientTlsContext & operator=(const ClientTlsContext &)=delete
ClientTlsContext(bool verify_peer=true, const std::string &ca_file="")
SSL_CTX * get() const
Definition TlsContext.hpp:87
ClientTlsContext(const ClientTlsContext &)=delete
SSL_CTX * ctx_
Definition TlsContext.hpp:90
Server TLS settings: certificates (with SNI) and ALPN.
Definition TlsContext.hpp:22
std::shared_ptr< const CertSet > current() const
config::TlsCertificate default_cert_
Definition TlsContext.hpp:61
config::HttpVersion get_http_version() const
Definition TlsContext.hpp:36
TlsContext(const std::string &cert_file, const std::string &key_file, config::HttpVersion http_version)
config::HttpVersion http_version_
Definition TlsContext.hpp:60
TlsContext & operator=(const TlsContext &)=delete
std::string file_stamp() const
TlsContext(const config::TlsCertificate &default_cert, std::vector< config::TlsCertificate > sni_certs, config::HttpVersion http_version)
std::vector< config::TlsCertificate > sni_certs_
Definition TlsContext.hpp:62
SSL_CTX * ctx_
Definition TlsContext.hpp:59
std::mutex mutex_
Definition TlsContext.hpp:64
static int servername_cb(SSL *ssl, int *alert, void *arg)
bool reload(std::string *error=nullptr)
Re-reads every certificate and key file. Thread-safe.
bool files_changed() const
True if a certificate or key file changed since it was last loaded.
TlsContext(const TlsContext &)=delete
std::string stamp_
Definition TlsContext.hpp:66
std::string certificate_for(const std::string &server_name) const
The certificate file that serves server_name (for tests and logs).
std::shared_ptr< const CertSet > certs_
Definition TlsContext.hpp:65
void configure(SSL_CTX *ctx)
Configures a context to match this one (protocol floor, ALPN).
SSL_CTX * get() const
Definition TlsContext.hpp:35
HttpVersion
Definition Config.hpp:18
Definition Http2Session.hpp:21
A PEM certificate chain and its private key.
Definition Config.hpp:25