Class Poco::Net::SSLManager (original) (raw)

Library: NetSSL_OpenSSL
Package: SSLCore
Header: Poco/Net/SSLManager.h

Description

SSLManager is a singleton for holding the default server/client Context and handling callbacks for certificate verification errors and private key passphrases.

Proper initialization of SSLManager is critical.

SSLManager can be initialized manually, by calling initializeServer() and/or initializeClient(), or initialization can be automatic. In the latter case, a Poco::Util::Application instance must be available and the required configuration properties must be set (see below).

Note that manual initialization must happen very early in the application, before defaultClientContext() or defaultServerContext() are called.

If defaultClientContext() and defaultServerContext() are never called in an application, initialization of SSLManager can be omitted. However, in this case, delegates for the ServerVerificationError, ClientVerificationError and PrivateKeyPassphraseRequired events must be registered.

An exemplary documentation which sets either the server or client default context and creates a PrivateKeyPassphraseHandler that reads the password from the XML file looks like this:

mycert.key mycert.crt rootcert.pem none|relaxed|strict|once 1..9 true|false ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH true|false KeyFileHandler test ConsoleCertificateHandler true|false someString 0..n 0..n true|false true|false true|false true|false true|false sslv2,sslv3,tlsv1,tlsv1_1,tlsv1_2,tlsv1_3 dh.pem prime256v1 false

Following is a list of supported configuration properties. Property names must always be prefixed with openSSL.server or openSSL.client. Some properties are only supported for servers.

Please see the Context class documentation regarding TLSv1.3 support.

Member Summary

Member Functions: appConfig, certificateHandlerFactoryMgr, clientCertificateHandler, clientPassphraseHandler, contextIndex, defaultClientContext, defaultServerContext, initializeClient, initializeServer, instance, isFIPSEnabled, privateKeyFactoryMgr, privateKeyPassphraseCallback, serverCertificateHandler, serverPassphraseHandler, shutdown, socketIndex, verifyClientCallback, verifyOCSPResponseCallback, verifyServerCallback

Types Aliases

InvalidCertificateHandlerPtr

using InvalidCertificateHandlerPtr = Poco::SharedPtr < InvalidCertificateHandler >;

PrivateKeyPassphraseHandlerPtr

using PrivateKeyPassphraseHandlerPtr = Poco::SharedPtr < PrivateKeyPassphraseHandler >;

Constructors

Destructor

~SSLManager protected

~SSLManager();

Member Functions

certificateHandlerFactoryMgr inline

CertificateHandlerFactoryMgr & certificateHandlerFactoryMgr();

Returns the CertificateHandlerFactoryMgr which stores the factories for the different registered certificate handlers.

clientCertificateHandler

InvalidCertificateHandlerPtr clientCertificateHandler();

Returns an initialized certificate handler (used by the client to verify server cert) which determines how invalid certificates are treated. If none is set, it will try to auto-initialize one from an application configuration.

clientPassphraseHandler

PrivateKeyPassphraseHandlerPtr clientPassphraseHandler();

Returns the configured passphrase handler of the client. If none is set, the method will create a default one from an application configuration.

defaultClientContext

Context::Ptr defaultClientContext();

Returns the default Context used by the client.

Unless initializeClient() has been called, the first call to this method initializes the default Context from the application configuration.

defaultServerContext

Context::Ptr defaultServerContext();

Returns the default Context used by the server.

Unless initializeServer() has been called, the first call to this method initializes the default Context from the application configuration.

initializeClient

void initializeClient(
PrivateKeyPassphraseHandlerPtr ptrPassphraseHandler,
InvalidCertificateHandlerPtr ptrHandler,
Context::Ptr ptrContext
);

Initializes the client side of the SSLManager with a default passphrase handler, a default invalid certificate handler and a default context. If this method is never called the SSLmanager will try to initialize its members from an application configuration.

PtrPassphraseHandler and ptrCertificateHandler can be 0. However, in this case, event delegates must be registered with the ClientVerificationError and PrivateKeyPassphraseRequired events.

Note: Always create the handlers (or register the corresponding event delegates) before creating the Context, as during creation of the Context the passphrase for the private key might be needed.

Valid initialization code would be:

SharedPtr pConsoleHandler = new KeyConsoleHandler; SharedPtr pInvalidCertHandler = new ConsoleCertificateHandler; Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "rootcert.pem", Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); SSLManager::instance().initializeClient(pConsoleHandler, pInvalidCertHandler, pContext);

initializeServer

void initializeServer(
PrivateKeyPassphraseHandlerPtr ptrPassphraseHandler,
InvalidCertificateHandlerPtr ptrCertificateHandler,
Context::Ptr ptrContext
);

Initializes the server side of the SSLManager with a default passphrase handler, a default invalid certificate handler and a default context. If this method is never called the SSLmanager will try to initialize its members from an application configuration.

PtrPassphraseHandler and ptrCertificateHandler can be 0. However, in this case, event delegates must be registered with the ServerVerificationError and PrivateKeyPassphraseRequired events.

Note: Always create the handlers (or register the corresponding event delegates) before creating the Context, as during creation of the Context the passphrase for the private key might be needed.

Valid initialization code would be:

SharedPtr pConsoleHandler = new KeyConsoleHandler; SharedPtr pInvalidCertHandler = new ConsoleCertificateHandler; Context::Ptr pContext = new Context(Context::SERVER_USE, "any.pem", "any.pem", "rootcert.pem", Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); SSLManager::instance().initializeServer(pConsoleHandler, pInvalidCertHandler, pContext);

instance static

static SSLManager & instance();

Returns the instance of the SSLManager singleton.

isFIPSEnabled static inline

static bool isFIPSEnabled();

privateKeyFactoryMgr inline

PrivateKeyFactoryMgr & privateKeyFactoryMgr();

Returns the private key factory manager which stores the factories for the different registered passphrase handlers for private keys.

serverCertificateHandler

InvalidCertificateHandlerPtr serverCertificateHandler();

Returns an initialized certificate handler (used by the server to verify client cert) which determines how invalid certificates are treated. If none is set, it will try to auto-initialize one from an application configuration.

serverPassphraseHandler

PrivateKeyPassphraseHandlerPtr serverPassphraseHandler();

Returns the configured passphrase handler of the server. If none is set, the method will create a default one from an application configuration.

shutdown

void shutdown();

appConfig protected static

static Poco::Util::AbstractConfiguration & appConfig();

Returns the application configuration.

Throws a InvalidStateException if not application instance is available.

contextIndex protected inline

int contextIndex() const;

Returns the index for SSL_CTX_set_ex_data() and SSL_CTX_get_ex_data() to store the Context* in the underlying SSL_CTX.

privateKeyPassphraseCallback protected static

static int privateKeyPassphraseCallback(
char * pBuf,
int size,
int flag,
void * userData
);

Method is invoked by OpenSSL to retrieve a passwd for an encrypted certificate. The request is delegated to the PrivatekeyPassword event. This method returns the length of the password.

socketIndex protected inline

int socketIndex() const;

Returns the index for SSL_set_ex_data() and SSL_get_ex_data() to store the SecureSocketImpl* in the underlying SSL.

verifyClientCallback protected static inline

static int verifyClientCallback(
int ok,
X509_STORE_CTX * pStore
);

The return value of this method defines how errors in verification are handled. Return 0 to terminate the handshake, or 1 to continue despite the error.

verifyOCSPResponseCallback protected static

static int verifyOCSPResponseCallback(
SSL * pSSL,
void * arg
);

The return value of this method defines how errors in verification are handled. Return 0 to terminate the handshake, or 1 to continue despite the error.

verifyServerCallback protected static inline

static int verifyServerCallback(
int ok,
X509_STORE_CTX * pStore
);

The return value of this method defines how errors in verification are handled. Return 0 to terminate the handshake, or 1 to continue despite the error.

Variables

CFG_CLIENT_PREFIX static

static const std::string CFG_CLIENT_PREFIX;

CFG_SERVER_PREFIX static

static const std::string CFG_SERVER_PREFIX;

ClientVerificationError

Poco::BasicEvent < VerificationErrorArgs > ClientVerificationError;

Fired whenever a certificate verification error is detected by the client during a handshake.

PrivateKeyPassphraseRequired

Poco::BasicEvent < std::string > PrivateKeyPassphraseRequired;

Fired when a encrypted certificate is loaded. Not setting the password in the event parameter will result in a failure to load the certificate.

ServerVerificationError

Poco::BasicEvent < VerificationErrorArgs > ServerVerificationError;

Fired whenever a certificate verification error is detected by the server during a handshake.