dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
25 #include <openssl/pkcs12.h>
26 #include <openssl/ssl.h>
27 #include <openssl/x509_vfy.h>
28 #include <openssl/x509v3.h>
39 SSL_load_error_strings();
41 OpenSSL_add_all_algorithms();
52 bool allowSANWildcards ) {
53 construct( peerverficiation, tlsversion, enableSNI, allowSANWildcards );
62 construct( pv, tv, enable_sni, allow_wildcards );
64 std::string priv = common::YAML_read_key<std::string>( yaml[
"pem"],
"private" );
65 std::string pub = common::YAML_read_key<std::string>( yaml[
"pem"],
"public" );
66 std::string pass = common::YAML_read_key<std::string>( yaml[
"pem"],
"passphrase" );
71 }
else if ( yaml[
"pkcs12"] ) {
78 bool allowSANWildcards ) {
86 tlsctx_ = SSL_CTX_new( TLS_method() );
91 rc = SSL_CTX_set_min_proto_version(
tlsctx_, TLS1_1_VERSION );
94 rc = SSL_CTX_set_min_proto_version(
tlsctx_, TLS1_2_VERSION );
97 rc = SSL_CTX_set_min_proto_version(
tlsctx_, TLS1_3_VERSION );
100 rc = SSL_CTX_set_min_proto_version(
tlsctx_, TLS1_1_VERSION );
107 SSL_CTX_set_default_passwd_cb_userdata(
tlsctx_,
this );
110 SSL_CTX_set_verify(
tlsctx_, SSL_VERIFY_NONE,
nullptr );
112 SSL_CTX_set_verify(
tlsctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
nullptr );
115 rc = SSL_CTX_set_default_verify_paths(
tlsctx_);
121 TLSContext::~TLSContext() {
127 const std::string& keyfile,
128 const std::string& passphrase ) {
130 if ( SSL_CTX_use_certificate_file(
tlsctx_, certfile.c_str(), SSL_FILETYPE_PEM ) != 1 ) {
133 if ( SSL_CTX_use_PrivateKey_file(
tlsctx_, keyfile.c_str(), SSL_FILETYPE_PEM ) != 1 ) {
136 if ( !SSL_CTX_check_private_key(
tlsctx_ ) ) {
143 const std::string &p12passphrase ) {
145 PKCS12 *p12 =
nullptr;
147 if ( ( fp = fopen( p12file.c_str(),
"rb" ) ) )
149 p12 = d2i_PKCS12_fp( fp, NULL );
151 EVP_PKEY *pkey =
nullptr;
152 X509 *cert =
nullptr;
153 STACK_OF(X509) *ca =
nullptr;
154 if ( PKCS12_parse( p12, p12passphrase.c_str(), &pkey, &cert, &ca) ) {
158 if ( SSL_CTX_use_certificate(
tlsctx_, cert ) != 1 )
162 if ( SSL_CTX_use_PrivateKey(
tlsctx_, pkey ) != 1 || !pkey )
166 if ( !SSL_CTX_check_private_key(
tlsctx_ ) )
170 if ( !SSL_CTX_set0_chain(
tlsctx_, ca ) ) {
175 if ( cert ) X509_free( cert );
176 if ( pkey ) EVP_PKEY_free( pkey );
177 if ( p12 ) PKCS12_free( p12 );
181 if ( cert ) X509_free( cert );
182 if ( pkey ) EVP_PKEY_free( pkey );
183 if ( p12 ) PKCS12_free( p12 );
199 else throw_Exception(
"invalid TLSContext::PeerVerification '" << src <<
"'" );
204 if ( size >
static_cast<int>( strlen( tlsctx->
passphrase_.c_str() ) ) ) {
208 return static_cast<int>( strlen( tlsctx->
passphrase_.c_str() ) );
214 rc = SSL_CTX_set_ciphersuites(
tlsctx_, cipherlist.c_str() );
216 rc = SSL_CTX_set_cipher_list(
tlsctx_, cipherlist.c_str() );
219 cipherlist <<
"'",
this );
223 return SSL_CTX_set_options(
tlsctx_, options );
227 const std::string& capath ) {
228 const char *cafile_ptr =
nullptr;
229 const char *capath_ptr =
nullptr;
230 if ( cafile.length() > 0 ) cafile_ptr = cafile.c_str();
231 if ( capath.length() > 0 ) capath_ptr = capath.c_str();
232 if ( !SSL_CTX_load_verify_locations(
tlsctx_, cafile_ptr, capath_ptr ) )
241 else throw_Exception(
"invalid TLSContext::TLSVersion '" << src <<
"'" );
PeerVerification peerverficiation_
The peer verification method used.
static void ShutdownSSL()
Shutdown the SSL library.
@ tls1_3
TLS 1.3 disables SSLv2, SSLv3, TLS 1.0, TLS 1.1 and TLS 1.2.
TLSContext(const PeerVerification &peerverficiation=PeerVerification::pvVerifyFQDN, const TLSVersion &tlsversion=TLSVersion::tlsBest, bool enableSNI=true, bool allowSANWildcards=true)
Construct a TLS context.
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 loadCe...
#define throw_ExceptionObject(what, thing)
Throws an Exception with DebugContext, passes FILE and LINE to constructor.
void setTrustPaths(const std::string &cafile, const std::string &capath)
Trust all certificates (PEM format) in the specified file and/or directory.
void construct(const PeerVerification &peerverficiation, const TLSVersion &tlsversion, bool enableSNI, bool allowSANWildcards)
Construct the TLSContext.
static int decrypt(const std::string &key, const std::string src, Bytes &dest)
Decrypt data with a key.
An array of Octets with size elements.
@ pvVerifyNone
No peer verification - transmission is encrypted, peer is accepted even if peer certificate is invali...
template bool YAML_read_key< bool >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for bool.
static PeerVerification peerVerficiationFromString(const std::string &src)
Get a PeerVerfication enum from a string.
bool allow_san_wildcards_
Allow SAN names to match agains wildcards (eg foo.domain.org matches *.domain.org).
@ tls1_2
TLS 1.2 disables SSLv2, SSLv3, TLS 1.0 and TLS 1.1.
@ tls1_1
TLS 1.1 disables SSLv2, SSLv3 and TLS 1.0.
PeerVerification
The TLS peer verification method.
@ pvVerifyPeer
The peer must have a trusted certificate (unless a anonymous cipher is used).
std::string passphrase_
The passphrase to decrypt encrypted private keys (may be empty when the key is not encrypted).
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.
static void InitializeSSL()
Initialize the SSL library.
TLSVersion tlsversion_
The TLS version.
bool enable_sni_
Enable / disable SNI on TLSSocket objects using this TLSContext.
TLSVersion
The TLS version.
@ pvVerifyFQDN
As pvVerifyPeer, but the remote DNS name must match either the peer cert commonname or match one of t...
#define throw_Exception(what)
Throws an Exception, passes FILE and LINE to constructor.
Interface for network communication.
std::string getSSLErrors(char terminator)
Get all OpenSSL errors as a single string, and clear their error state.
void setCipherList(const std::string &cipherlist)
Set a list of ciphers, separated by a colon, the TLSContext will accept.
std::string asString() const
Convert to a std::string.
static TLSVersion tlsVersionFromString(const std::string &src)
Convert the src string to a TLSVersion or throw a common::Exception if that mapping fails.
SSL_CTX * tlsctx_
The openssl SSL_CTX.
long setOptions(long option)
Set SSL options.
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 PKCS1...