-
Notifications
You must be signed in to change notification settings - Fork 3
/
TLS-OpenSSL.hpp
95 lines (73 loc) · 2.76 KB
/
TLS-OpenSSL.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef TLS_OPENSSL_DOT_HPP
#define TLS_OPENSSL_DOT_HPP
#include "DNS-rrs.hpp"
#include "Domain.hpp"
#include "fs.hpp"
#include <chrono>
#include <functional>
#include <openssl/ssl.h>
namespace Config {
auto constexpr cert_verify_depth{10};
auto constexpr cert_fn_re = ".+\\.pem$";
auto constexpr key_ext = ".key";
} // namespace Config
class TLS {
public:
TLS(TLS const&) = delete;
TLS& operator=(const TLS&) = delete;
explicit TLS(std::function<void(void)> read_hook);
~TLS();
bool starttls_client(fs::path config_path,
int fd_in,
int fd_out,
char const* client_name,
char const* server_name,
DNS::RR_collection const& tlsa_rrs,
bool enforce_dane,
std::chrono::milliseconds timeout);
bool starttls_server(fs::path config_path,
int fd_in,
int fd_out,
std::chrono::milliseconds timeout);
bool pending() const { return SSL_pending(ssl_) > 0; }
std::streamsize
read(char* s, std::streamsize n, std::chrono::milliseconds wait, bool& t_o)
{
return io_tls_("SSL_read", SSL_read, s, n, wait, t_o);
}
std::streamsize write(const char* c_s,
std::streamsize n,
std::chrono::milliseconds wait,
bool& t_o)
{
auto s = const_cast<char*>(c_s);
return io_tls_("SSL_write", SSL_write, s, n, wait, t_o);
}
std::string info() const;
std::string const& verified_peername() const { return verified_peername_; }
bool verified() const { return verified_; }
struct per_cert_ctx {
explicit per_cert_ctx(SSL_CTX* ctx_, std::vector<Domain> cn_)
: ctx(ctx_)
, cn(cn_)
{
}
SSL_CTX* ctx;
std::vector<Domain> cn;
};
private:
std::streamsize io_tls_(char const* fn,
std::function<int(SSL*, void*, int)> io_fnc,
char* s,
std::streamsize n,
std::chrono::milliseconds wait,
bool& t_o);
static void ssl_error(int n_err) __attribute__((noreturn));
private:
SSL* ssl_{nullptr};
std::vector<per_cert_ctx> cert_ctx_;
std::function<void(void)> read_hook_;
std::string verified_peername_;
bool verified_{false};
};
#endif // TLS_OPENSSL_DOT_HPP