dodo
0.0.1
A C++ library to create containerized Linux services
|
TLS security context. More...
#include <tlscontext.hpp>
Public Types | |
enum | PeerVerification { PeerVerification::pvVerifyNone, PeerVerification::pvVerifyPeer, PeerVerification::pvVerifyFQDN } |
The TLS peer verification method. More... | |
enum | TLSVersion { TLSVersion::tls1_1, TLSVersion::tls1_2, TLSVersion::tls1_3, TLSVersion::tlsBest = tls1_3 } |
The TLS version. More... | |
Public Member Functions | |
TLSContext (const PeerVerification &peerverficiation=PeerVerification::pvVerifyFQDN, const TLSVersion &tlsversion=TLSVersion::tlsBest, bool enableSNI=true, bool allowSANWildcards=true) | |
Construct a TLS context. More... | |
TLSContext (const YAML::Node &yaml) | |
Construct a TLSContext from a YAML node. More... | |
SSL_CTX * | getContext () const |
Return a pointer to the SSL_CTX. More... | |
PeerVerification | getPeerVerification () const |
Return the getPeerVerification mode. More... | |
bool | isAllowSANWildcards () const |
If true, TLS will allow SAN wildcard matching. More... | |
bool | isSNIEnabled () const |
Return true when SNI (server Name Information) is to be enabled by TLSSocket objects using this TLSContext. More... | |
void | loadPEMIdentity (const std::string &certfile, const std::string &keyfile, const std::string &passphrase) |
Load a certificate and the corresponding private key for an identity. More... | |
void | loadPKCS12 (const std::string &p12file, const std::string &p12passphrase) |
Loads a private key, matching certificate and optional CA certificates (eg a truststore) from a PKCS12 file. More... | |
void | setCipherList (const std::string &cipherlist) |
Set a list of ciphers, separated by a colon, the TLSContext will accept. More... | |
long | setOptions (long option) |
Set SSL options. More... | |
void | setTrustPaths (const std::string &cafile, const std::string &capath) |
Trust all certificates (PEM format) in the specified file and/or directory. More... | |
![]() | |
DebugObject () | |
Default constructor does nothing. More... | |
virtual | ~DebugObject () |
Destructor does nothing. More... | |
std::string | debugString () const |
Return the object dump to string. More... | |
Static Public Member Functions | |
static PeerVerification | peerVerficiationFromString (const std::string &src) |
Get a PeerVerfication enum from a string. More... | |
static TLSVersion | tlsVersionFromString (const std::string &src) |
Convert the src string to a TLSVersion or throw a common::Exception if that mapping fails. More... | |
Private Member Functions | |
void | construct (const PeerVerification &peerverficiation, const TLSVersion &tlsversion, bool enableSNI, bool allowSANWildcards) |
Construct the TLSContext. More... | |
Static Private Member Functions | |
static void | InitializeSSL () |
Initialize the SSL library. More... | |
static int | pem_passwd_cb (char *buf, int size, int rwflag, void *userdata) |
Password callback, returns the passphrase set in the TLS context by the passphrase argument of loadCertificate or pkeypassphrase argument of the loadPKCS12 method. More... | |
static void | ShutdownSSL () |
Shutdown the SSL library. More... | |
Private Attributes | |
bool | allow_san_wildcards_ |
Allow SAN names to match agains wildcards (eg foo.domain.org matches *.domain.org). More... | |
bool | enable_clr_ |
Enable / disable CRL (Certificate Revocation List) checking. More... | |
bool | enable_sni_ |
Enable / disable SNI on TLSSocket objects using this TLSContext. More... | |
std::string | passphrase_ |
The passphrase to decrypt encrypted private keys (may be empty when the key is not encrypted). More... | |
PeerVerification | peerverficiation_ |
The peer verification method used. More... | |
SSL_CTX * | tlsctx_ |
The openssl SSL_CTX. More... | |
TLSVersion | tlsversion_ |
The TLS version. More... | |
Additional Inherited Members | |
![]() | |
virtual std::string | debugDetail () const |
Descendant classes can override to dump details specific to the class. More... | |
std::string | debugHeader () const |
Generates a debug header (address of this object and a demangled class name. More... | |
TLS security context.
A single TLSContext can be shared among multiple TLSSocket classes.
See Secure sockets for more information on the role of this class.
Definition at line 50 of file tlscontext.hpp.
|
strong |
The TLS peer verification method.
PeerVerification | SSL_CTX_set_verify |
---|---|
pvVerifyNone | SSL_VERIFY_NONE |
pvVerifyPeer | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
pvVerifyFQDN | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
PeerVerification | Server / accept | Client / connect |
---|---|---|
pvVerifyNone | encryption of traffic | encryption of traffic |
pvVerifyPeer | pvVerifyNone + client must present trusted cert | pvVerifyNone + server must present trusted cert |
pvVerifyFQDN | pvVerifyPeer | pvVerifyPeer + X509Certificate::verifyName() |
Definition at line 80 of file tlscontext.hpp.
|
strong |
The TLS version.
Use tlsBest.
Definition at line 56 of file tlscontext.hpp.
dodo::network::TLSContext::TLSContext | ( | const PeerVerification & | peerverficiation = PeerVerification::pvVerifyFQDN , |
const TLSVersion & | tlsversion = TLSVersion::tlsBest , |
||
bool | enableSNI = true , |
||
bool | allowSANWildcards = true |
||
) |
Construct a TLS context.
Use depends on the context being either server or client side, see TLSContext::PeerVerification.
Example for a server-side setup enforcing at least TLS1.3, requiring the peer to present a trusted certificate (pvVerifyPeer).
peerverficiation | The TLSContext::PeerVerification method to use. |
tlsversion | The TLS version to use. Use of default is less future code hassle. |
enableSNI | Enable the Server Name Indication extension. Note that this exposes the target hostname |
allowSANWildcards | Allow SAN wildcard matching under pvVerifyFQDN of TLSSocket connections as the hostname is sent unencrypted, facilitated all kinds of evil such as censorship. Use only when you must connect to a server that requires it. |
Definition at line 49 of file tlscontext.cpp.
References construct().
dodo::network::TLSContext::TLSContext | ( | const YAML::Node & | yaml | ) |
Construct a TLSContext from a YAML node.
If the PEM format is used to provide keys and passphrase:
If the PKCS12 format is used to provide keys and passphrase:
yaml | The YAML node to read from, which would be 'tlscontext' in the above examples. |
Definition at line 57 of file tlscontext.cpp.
References dodo::common::Bytes::asString(), construct(), dodo::common::DataCrypt::decrypt(), loadPEMIdentity(), passphrase_, peerVerficiationFromString(), throw_Exception, tlsVersionFromString(), and dodo::common::YAML_read_key< bool >().
|
private |
Construct the TLSContext.
peerverficiation | The TLSContext::PeerVerification method to use. |
tlsversion | The TLS version to use. Use of default is less future code hassle. |
enableSNI | Enable the Server Name Indication extension. Note that this exposes the target hostname |
allowSANWildcards | Allow SAN wildcard matching under pvVerifyFQDN |
Definition at line 75 of file tlscontext.cpp.
References allow_san_wildcards_, enable_sni_, dodo::common::getSSLErrors(), passphrase_, peerverficiation_, pem_passwd_cb(), pvVerifyNone, throw_ExceptionObject, tls1_1, tls1_2, tls1_3, tlsctx_, and tlsversion_.
Referenced by TLSContext().
|
inline |
Return a pointer to the SSL_CTX.
Definition at line 215 of file tlscontext.hpp.
References tlsctx_.
|
inline |
Return the getPeerVerification mode.
Definition at line 221 of file tlscontext.hpp.
References peerverficiation_.
Referenced by dodo::network::TLSSocket::connect().
|
staticprivate |
Initialize the SSL library.
Definition at line 38 of file tlscontext.cpp.
|
inline |
If true, TLS will allow SAN wildcard matching.
Definition at line 233 of file tlscontext.hpp.
References allow_san_wildcards_.
Referenced by dodo::network::TLSSocket::connect().
|
inline |
Return true when SNI (server Name Information) is to be enabled by TLSSocket objects using this TLSContext.
Definition at line 227 of file tlscontext.hpp.
References enable_sni_.
Referenced by dodo::network::TLSSocket::TLSSocket().
void dodo::network::TLSContext::loadPEMIdentity | ( | const std::string & | certfile, |
const std::string & | keyfile, | ||
const std::string & | passphrase | ||
) |
Load a certificate and the corresponding private key for an identity.
certfile | The certificate PEM file. |
keyfile | The private key PEM file. |
passphrase | The passphrase for the private key PEM file. If the private key is not protected by a passphrase its value is stored in this object nonetheless but unused. |
Definition at line 126 of file tlscontext.cpp.
References dodo::common::getSSLErrors(), passphrase_, throw_ExceptionObject, and tlsctx_.
Referenced by TLSContext().
void dodo::network::TLSContext::loadPKCS12 | ( | const std::string & | p12file, |
const std::string & | p12passphrase | ||
) |
Loads a private key, matching certificate and optional CA certificates (eg a truststore) from a PKCS12 file.
p12file | The PKCS12 file to read from. |
p12passphrase | The passphrase for the PKCS12 file. |
Definition at line 142 of file tlscontext.cpp.
References dodo::common::getSSLErrors(), passphrase_, throw_ExceptionObject, and tlsctx_.
|
static |
Get a PeerVerfication enum from a string.
The comparison is case sensitive and must match the enum name ( "pvVerifyNone", "pvVerifyPeer", "pvVerifyFQDN" ). If the name does not translate, a common::Exception is thrown.
src | The source string. |
Definition at line 195 of file tlscontext.cpp.
References pvVerifyFQDN, pvVerifyNone, pvVerifyPeer, and throw_Exception.
Referenced by TLSContext().
|
staticprivate |
Password callback, returns the passphrase set in the TLS context by the passphrase argument of loadCertificate or pkeypassphrase argument of the loadPKCS12 method.
buf | The passphrase should be copied to here. |
size | No more than size bytes should be copied into buf. |
rwflag | 0 = decryption 1 = encryption |
userdata | Pass a pointer to the TLSContext object. |
Definition at line 202 of file tlscontext.cpp.
References passphrase_.
Referenced by construct().
void dodo::network::TLSContext::setCipherList | ( | const std::string & | cipherlist | ) |
Set a list of ciphers, separated by a colon, the TLSContext will accept.
There are differences between TLSVersion tough,
A few examples (note the hyphens and underscores)
A list of available ciphers is given by
Note that this call will not return a SystemError, but throws a dodo::common::Exception when the cipher list is invalid.
cipherlist | The cipherlist. |
dodo::common::Exception |
Definition at line 211 of file tlscontext.cpp.
References throw_ExceptionObject, tls1_3, tlsctx_, and tlsversion_.
long dodo::network::TLSContext::setOptions | ( | long | option | ) |
Set SSL options.
option | The option or OR-ed options to apply. |
Definition at line 222 of file tlscontext.cpp.
References tlsctx_.
void dodo::network::TLSContext::setTrustPaths | ( | const std::string & | cafile, |
const std::string & | capath | ||
) |
Trust all certificates (PEM format) in the specified file and/or directory.
cafile | A PEM file containing one or more certificates. If an empty string, unused. |
capath | A directory containing certificate files. If an empty string, unused. |
Definition at line 226 of file tlscontext.cpp.
References dodo::common::getSSLErrors(), throw_ExceptionObject, and tlsctx_.
|
staticprivate |
Shutdown the SSL library.
Definition at line 44 of file tlscontext.cpp.
|
static |
Convert the src string to a TLSVersion or throw a common::Exception if that mapping fails.
TLSversion strings could be "1.1", "1.2" and "1.3".
src | The source string. |
Definition at line 237 of file tlscontext.cpp.
References throw_Exception, tls1_1, tls1_2, and tls1_3.
Referenced by TLSContext().
|
private |
Allow SAN names to match agains wildcards (eg foo.domain.org matches *.domain.org).
Definition at line 315 of file tlscontext.hpp.
Referenced by construct(), and isAllowSANWildcards().
|
private |
Enable / disable CRL (Certificate Revocation List) checking.
Definition at line 320 of file tlscontext.hpp.
|
private |
Enable / disable SNI on TLSSocket objects using this TLSContext.
Definition at line 310 of file tlscontext.hpp.
Referenced by construct(), and isSNIEnabled().
|
private |
The passphrase to decrypt encrypted private keys (may be empty when the key is not encrypted).
Definition at line 305 of file tlscontext.hpp.
Referenced by construct(), loadPEMIdentity(), loadPKCS12(), pem_passwd_cb(), and TLSContext().
|
private |
The peer verification method used.
Definition at line 300 of file tlscontext.hpp.
Referenced by construct(), and getPeerVerification().
|
private |
The openssl SSL_CTX.
Definition at line 290 of file tlscontext.hpp.
Referenced by construct(), getContext(), loadPEMIdentity(), loadPKCS12(), setCipherList(), setOptions(), and setTrustPaths().
|
private |
The TLS version.
Definition at line 295 of file tlscontext.hpp.
Referenced by construct(), and setCipherList().