join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
tls_context.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CRYPTO_TLS_CONTEXT_HPP
26#define JOIN_CRYPTO_TLS_CONTEXT_HPP
27
28// libjoin.
29#include <join/tls_error.hpp>
30#include <join/openssl.hpp>
31
32// C++.
33#include <string>
34#include <vector>
35
36namespace join
37{
42 {
43 public:
54
58 TlsContext () noexcept = default;
59
65 explicit TlsContext (Role role);
66
71 TlsContext (const TlsContext&) = default;
72
78 TlsContext& operator= (const TlsContext&) = default;
79
84 TlsContext (TlsContext&&) = default;
85
91 TlsContext& operator= (TlsContext&&) = default;
92
96 ~TlsContext () = default;
97
104 int setCertificate (const std::string& cert, const std::string& key = "") noexcept;
105
111 int setCaPath (const std::string& caPath);
112
118 int setCaFile (const std::string& caFile);
119
125 void setVerify (bool verify, int depth = -1) noexcept;
126
132 int setCipher (const std::string& cipher) noexcept;
133
139 int setCipher_1_3 (const std::string& cipher) noexcept;
140
141#if OPENSSL_VERSION_NUMBER >= 0x30000000L
147 int setCurve (const std::string& curves);
148#endif
149
155 int setAlpnProtocols (const std::vector<std::string>& protocols);
156
161 SSL_CTX* handle () const noexcept;
162
167 bool verify () const noexcept;
168
173 int depth () const noexcept;
174
179 bool isServer () const noexcept;
180
181 private:
187 static const SSL_METHOD* method (Role role) noexcept;
188
189#if OPENSSL_VERSION_NUMBER < 0x30000000L
195 static DH* getDh2236 ();
196#endif
197
199 Role _role = Role::TlsClient;
200
202 bool _verify = false;
203
205 int _depth = -1;
206
208 SslCtxPtr _ctx;
209 };
210}
211
212#endif
TLS/DTLS context.
Definition tls_context.hpp:42
int setCaFile(const std::string &caFile)
set the location of the trusted CA certificate file.
Definition tls_context.cpp:179
void setVerify(bool verify, int depth=-1) noexcept
enable or disable peer certificate verification.
Definition tls_context.cpp:202
bool isServer() const noexcept
check if the role is a server role.
Definition tls_context.cpp:340
int setCertificate(const std::string &cert, const std::string &key="") noexcept
set the certificate and the private key.
Definition tls_context.cpp:120
int setAlpnProtocols(const std::vector< std::string > &protocols)
set the ALPN protocols list.
Definition tls_context.cpp:277
bool verify() const noexcept
check if peer verification is enabled.
Definition tls_context.cpp:322
TlsContext() noexcept=default
create an empty context, unable to negotiate.
int setCipher(const std::string &cipher) noexcept
set the cipher list (TLSv1.2 and below).
Definition tls_context.cpp:212
SSL_CTX * handle() const noexcept
get the native SSL_CTX handle.
Definition tls_context.cpp:313
int setCipher_1_3(const std::string &cipher) noexcept
set the cipher list (TLSv1.3).
Definition tls_context.cpp:233
Role
TLS/DTLS role.
Definition tls_context.hpp:48
@ DtlsClient
Definition tls_context.hpp:51
@ TlsClient
Definition tls_context.hpp:49
@ DtlsServer
Definition tls_context.hpp:52
@ TlsServer
Definition tls_context.hpp:50
int depth() const noexcept
get the maximum certificate chain depth.
Definition tls_context.cpp:331
int setCaPath(const std::string &caPath)
set the location of the trusted CA certificates.
Definition tls_context.cpp:156
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::shared_ptr< SSL_CTX > SslCtxPtr
Definition openssl.hpp:240
Definition error.hpp:144