Class SecurityEnhancedTLSSocketFactory
- java.lang.Object
-
- org.opensaml.security.httpclient.impl.SecurityEnhancedTLSSocketFactory
-
- All Implemented Interfaces:
ConnectionSocketFactory,LayeredConnectionSocketFactory
public class SecurityEnhancedTLSSocketFactory extends Object implements LayeredConnectionSocketFactory
An security-enhanced implementation of HttpClient's TLS-capableLayeredConnectionSocketFactory.This implementation wraps an existing TLS socket factory instance, decorating it with additional support for:
- Verifying the server TLS certificate and chain via a
TrustEngine<Credential>andCriteriaSetsupplied by the HttpClient caller via theHttpContext. - Loading and clearing a thread-local instance of
X509Credentialused for client TLS.
The context keys used by this component are as follows, defined in
HttpClientSecurityConstants:HttpClientSecurityConstants.CONTEXT_KEY_TRUST_ENGINE: The trust engine instance used. Supplied by the HttpClient caller. Must be an instance ofTrustEngine<Credential>.HttpClientSecurityConstants.CONTEXT_KEY_CRITERIA_SET: The criteria set instance used. Supplied by the HttpClient caller. Must be an instance ofCriteriaSet.HttpClientSecurityConstants.CONTEXT_KEY_SERVER_TLS_CREDENTIAL_TRUSTED: The result of the trust evaluation, if it was performed. Populated by this component. Will be aBoolean, wheretruemeans the server TLS was evaluated as trusted,falsemeans the credential was evaluated as untrusted. A null or missing value means that trust engine evaluation was not performed.HttpClientSecurityConstants.CONTEXT_KEY_CLIENT_TLS_CREDENTIAL: The client TLS credential used. Supplied by the HttpClient caller. Must be an instance ofX509Credential.
If the trust engine context attribute is not populated by the caller and
isTrustEngineRequired()istrue(the default), then anSSLPeerUnverifiedExceptionis thrown.If the trust engine context attribute is not populated by the caller and
isTrustEngineRequired()isfalse, then no trust evaluation is performed. This allows use of this implementation with use cases where, given a particular HttpClient instance, sometimes TLS trust engine evaluation is to be performed, and sometimes not. The caller is then responsible for ensuring they supply a trust engine or not, as appropriate.Since this implementation may typically be used with and wrap a "no trust" SSL socket factory, an optional instance of
X509HostnameVerifiermay also be supplied. If supplied, hostname verification will be performed against the newSSLSocketviaX509HostnameVerifier.verify(String, SSLSocket).If the client TLS credential context attribute is not populated by the caller, then client TLS is not attempted.
Client TLS support requires use of a compatible
KeyManagerimplementation configured in theSSLContextof the wrappedLayeredConnectionSocketFactory, such asThreadLocalX509CredentialKeyManager.
-
-
Field Summary
Fields Modifier and Type Field Description private static ThreadLocalClientTLSCredentialHandlerCLIENT_TLS_HANDLERInstance ofThreadLocalClientTLSCredentialHandlerto use.private booleanengineRequiredFlag indicating whether a context trust engine attribute is required for TLS server validation.private X509HostnameVerifierhostnameVerifierThe hostname verifier evaluated by this implementation.private LoggerlogLogger.private LayeredConnectionSocketFactorywrappedFactoryThe HttpClient socket factory instance wrapped by this implementation.
-
Constructor Summary
Constructors Constructor Description SecurityEnhancedTLSSocketFactory(LayeredConnectionSocketFactory factory)Constructor.SecurityEnhancedTLSSocketFactory(LayeredConnectionSocketFactory factory, boolean trustEngineRequired)Constructor.SecurityEnhancedTLSSocketFactory(LayeredConnectionSocketFactory factory, X509HostnameVerifier verifier)Constructor.SecurityEnhancedTLSSocketFactory(LayeredConnectionSocketFactory factory, X509HostnameVerifier verifier, boolean trustEngineRequired)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SocketconnectSocket(int connectTimeout, Socket sock, org.apache.http.HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, org.apache.http.protocol.HttpContext context)SocketcreateLayeredSocket(Socket socket, String target, int port, org.apache.http.protocol.HttpContext context)SocketcreateSocket(org.apache.http.protocol.HttpContext context)protected org.opensaml.security.x509.X509CredentialextractCredential(SSLSocket sslSocket)Extract the server TLSX509Credentialfrom the suppliedSSLSocket.booleanisTrustEngineRequired()Get the flag indicating whether a context trust engine attribute is required for TLS server validation.protected voidperformHostnameVerification(Socket socket, String hostname, org.apache.http.protocol.HttpContext context)Perform hostname verification on the connection represented by the supplied socket.protected voidperformTrustEval(Socket socket, String hostname, org.apache.http.protocol.HttpContext context)Perform trust evaluation by extracting the server TLSX509Credentialfrom theSSLSessionand evaluating it via aTrustEngine<Credential>andCriteriaSetsupplied by the caller via theHttpContext.protected voidsetup(org.apache.http.protocol.HttpContext context)Load theThreadLocalX509CredentialContextwith the client TLS credential obtained from theHttpContext.protected voidteardown(org.apache.http.protocol.HttpContext context)Schedule the deferred clearing of theThreadLocalX509CredentialContextof the client TLS credential obtained from theHttpContext.
-
-
-
Field Detail
-
CLIENT_TLS_HANDLER
private static final ThreadLocalClientTLSCredentialHandler CLIENT_TLS_HANDLER
Instance ofThreadLocalClientTLSCredentialHandlerto use.
-
log
private final Logger log
Logger.
-
wrappedFactory
@Nonnull private LayeredConnectionSocketFactory wrappedFactory
The HttpClient socket factory instance wrapped by this implementation.
-
hostnameVerifier
@Nullable private X509HostnameVerifier hostnameVerifier
The hostname verifier evaluated by this implementation.
-
engineRequired
private boolean engineRequired
Flag indicating whether a context trust engine attribute is required for TLS server validation. Default: true.
-
-
Constructor Detail
-
SecurityEnhancedTLSSocketFactory
public SecurityEnhancedTLSSocketFactory(@Nonnull LayeredConnectionSocketFactory factory, boolean trustEngineRequired)Constructor.No hostname verifier is configured in this implementation. (Does not affect whether hostname is or is not evaluated by the wrapped socket factory).
- Parameters:
factory- the underlying HttpClient socket factory wrapped by this implementation.trustEngineRequired- flag indicating whether a context trust engine attribute is required for TLS server validation.
-
SecurityEnhancedTLSSocketFactory
public SecurityEnhancedTLSSocketFactory(@Nonnull LayeredConnectionSocketFactory factory)Constructor.No hostname verifier is configured in this implementation. (Does not affect whether hostname is or is not evaluated by the wrapped socket factory).
- Parameters:
factory- the underlying HttpClient socket factory wrapped by this implementation.
-
SecurityEnhancedTLSSocketFactory
public SecurityEnhancedTLSSocketFactory(@Nonnull LayeredConnectionSocketFactory factory, @Nullable X509HostnameVerifier verifier)Constructor.- Parameters:
factory- the underlying HttpClient socket factory wrapped by this implementation.verifier- the hostname verifier evaluated by this implementation
-
SecurityEnhancedTLSSocketFactory
public SecurityEnhancedTLSSocketFactory(@Nonnull LayeredConnectionSocketFactory factory, @Nullable X509HostnameVerifier verifier, boolean trustEngineRequired)Constructor.- Parameters:
factory- the underlying HttpClient socket factory wrapped by this implementation.verifier- the hostname verifier evaluated by this implementationtrustEngineRequired- flag indicating whether a context trust engine attribute is required for TLS server validation.
-
-
Method Detail
-
isTrustEngineRequired
public boolean isTrustEngineRequired()
Get the flag indicating whether a context trust engine attribute is required for TLS server validation.Default: true.
- Returns:
- true if trust engine is required, false if not
-
createSocket
public Socket createSocket(org.apache.http.protocol.HttpContext context) throws IOException
- Specified by:
createSocketin interfaceConnectionSocketFactory- Throws:
IOException
-
connectSocket
public Socket connectSocket(int connectTimeout, Socket sock, org.apache.http.HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, org.apache.http.protocol.HttpContext context) throws IOException
- Specified by:
connectSocketin interfaceConnectionSocketFactory- Throws:
IOException
-
createLayeredSocket
public Socket createLayeredSocket(Socket socket, String target, int port, org.apache.http.protocol.HttpContext context) throws IOException
- Specified by:
createLayeredSocketin interfaceLayeredConnectionSocketFactory- Throws:
IOException
-
performTrustEval
protected void performTrustEval(@Nonnull Socket socket, @Nonnull @NotEmpty String hostname, @Nonnull org.apache.http.protocol.HttpContext context) throws IOExceptionPerform trust evaluation by extracting the server TLSX509Credentialfrom theSSLSessionand evaluating it via aTrustEngine<Credential>andCriteriaSetsupplied by the caller via theHttpContext.- Parameters:
socket- the socket instance being processedhostname- the hostname being processedcontext- the HttpClient context being processed- Throws:
IOException- if the server TLS credential is untrusted, or if there is a fatal error attempting trust evaluation.
-
extractCredential
@Nonnull protected org.opensaml.security.x509.X509Credential extractCredential(@Nonnull SSLSocket sslSocket) throws IOExceptionExtract the server TLSX509Credentialfrom the suppliedSSLSocket.- Parameters:
sslSocket- the SSL socket instance to process- Returns:
- an X509Credential representing the server TLS entity certificate as well as the supplied supporting intermediate certificate chain (if any)
- Throws:
IOException- if credential data can not be extracted from the socket
-
performHostnameVerification
protected void performHostnameVerification(Socket socket, String hostname, org.apache.http.protocol.HttpContext context) throws IOException
Perform hostname verification on the connection represented by the supplied socket.- Parameters:
socket- the socket instance being processedhostname- the hostname against which to verifycontext- the current HttpClient context instance- Throws:
IOException- if an I/O error occurs or the verification process fails
-
setup
protected void setup(@Nullable org.apache.http.protocol.HttpContext context)Load theThreadLocalX509CredentialContextwith the client TLS credential obtained from theHttpContext.- Parameters:
context- the HttpContext instance
-
teardown
protected void teardown(@Nullable org.apache.http.protocol.HttpContext context)Schedule the deferred clearing of theThreadLocalX509CredentialContextof the client TLS credential obtained from theHttpContext.- Parameters:
context- the HttpContext instance
-
-