Class TLSSocketFactory
java.lang.Object
net.shibboleth.utilities.java.support.httpclient.TLSSocketFactory
- All Implemented Interfaces:
org.apache.http.conn.socket.ConnectionSocketFactory
,org.apache.http.conn.socket.LayeredConnectionSocketFactory
@ThreadSafe
public class TLSSocketFactory
extends Object
implements org.apache.http.conn.socket.LayeredConnectionSocketFactory
An implementation of HttpClient
LayeredConnectionSocketFactory
that is a factory
for TLS sockets.
This class is functionally modeled on SSLConnectionSocketFactory
,
but provides better support for subclassing, as well as specific additional features:
- Factory hostname verifier defaults to
DefaultHostnameVerifier
without a configuredPublicSuffixMatcher
. - Per-request specification of enabled TLS protocols and cipher suites via
HttpContext
attributes. - Per-request specification of hostname verifier via
HttpContext
attribute.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final HostnameVerifier
Hostname verifier which passes all hostnames.static final String
HttpContext key for an instance ofHostnameVerifier
.static final String
HttpContext key for a a list of TLS cipher suites to enable on the socket.static final String
HttpContext key for a a list of TLS protocols to enable on the socket.private final HostnameVerifier
Hostname verifier.private final org.slf4j.Logger
Logger.private final SSLSocketFactory
Socket factory.static final String
Protocol: SSL.static final String
Protocol: SSLv2.static final HostnameVerifier
Hostname verifier which implements a strict policy.private final String[]
Factory-wide supported cipher suites.private final String[]
Factory-wide supported protocols.static final String
Protocol: TLS. -
Constructor Summary
ConstructorsConstructorDescriptionTLSSocketFactory
(SSLContext sslContext) Constructor.TLSSocketFactory
(SSLContext sslContext, String[] protocols, String[] cipherSuites, HostnameVerifier verifier) Constructor.TLSSocketFactory
(SSLContext sslContext, HostnameVerifier verifier) Constructor.TLSSocketFactory
(SSLSocketFactory factory, String[] protocols, String[] cipherSuites, HostnameVerifier verifier) Constructor.TLSSocketFactory
(SSLSocketFactory factory, HostnameVerifier verifier) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionconnectSocket
(int connectTimeout, Socket socket, org.apache.http.HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, org.apache.http.protocol.HttpContext context) createLayeredSocket
(Socket socket, String target, int port, org.apache.http.protocol.HttpContext context) createSocket
(org.apache.http.protocol.HttpContext context) protected HostnameVerifier
Get the configured hostname verifier.protected String[]
getListAttribute
(org.apache.http.protocol.HttpContext context, String contextKey) protected SSLSocketFactory
Get the JSSE socket factory instance.protected String[]
Get the configured factory-wide supported cipher suites.protected String[]
Get the configured factory-wide supported protocols.private void
logSocketInfo
(SSLSocket socket) Log various diagnostic information from theSSLSocket
andSSLSession
.protected void
prepareSocket
(SSLSocket socket, org.apache.http.protocol.HttpContext context) Performs any custom initialization for a newly created SSLSocket (before the SSL handshake happens).protected void
verifyHostname
(SSLSocket sslsock, String hostname, org.apache.http.protocol.HttpContext context) Verify the peer's socket hostname against the supplied expected name.
-
Field Details
-
CONTEXT_KEY_TLS_PROTOCOLS
HttpContext key for a a list of TLS protocols to enable on the socket. Must be an instance ofList
<String
>.- See Also:
-
CONTEXT_KEY_TLS_CIPHER_SUITES
HttpContext key for a a list of TLS cipher suites to enable on the socket. Must be an instance ofList
<String
>.- See Also:
-
CONTEXT_KEY_HOSTNAME_VERIFIER
HttpContext key for an instance ofHostnameVerifier
.- See Also:
-
TLS
Protocol: TLS.- See Also:
-
SSL
Protocol: SSL.- See Also:
-
SSLV2
Protocol: SSLv2.- See Also:
-
ALLOW_ALL_HOSTNAME_VERIFIER
Hostname verifier which passes all hostnames. -
STRICT_HOSTNAME_VERIFIER
Hostname verifier which implements a strict policy. -
log
@Nonnull private final org.slf4j.Logger logLogger. -
socketfactory
Socket factory. -
hostnameVerifier
Hostname verifier. -
supportedProtocols
Factory-wide supported protocols. -
supportedCipherSuites
Factory-wide supported cipher suites.
-
-
Constructor Details
-
TLSSocketFactory
Constructor.- Parameters:
sslContext
- the effective SSLContext instance
-
TLSSocketFactory
Constructor.- Parameters:
sslContext
- the effective SSLContext instanceverifier
- the effective hostname verifier
-
TLSSocketFactory
public TLSSocketFactory(@Nonnull SSLContext sslContext, @Nullable String[] protocols, @Nullable String[] cipherSuites, @Nullable HostnameVerifier verifier) Constructor.- Parameters:
sslContext
- the effective SSLContext instanceprotocols
- the factory-wide enabled TLS protocolscipherSuites
- the factory-wide enabled TLS cipher suitesverifier
- the effective hostname verifier
-
TLSSocketFactory
Constructor.- Parameters:
factory
- the effective SSL socket factoryverifier
- the effective hostname verifier
-
TLSSocketFactory
public TLSSocketFactory(@Nonnull SSLSocketFactory factory, @Nullable String[] protocols, @Nullable String[] cipherSuites, @Nullable HostnameVerifier verifier) Constructor.- Parameters:
factory
- the effective SSL socket factoryprotocols
- the factory-wide enabled TLS protocolscipherSuites
- the factory-wide enabled TLS cipher suitesverifier
- the effective hostname verifier
-
-
Method Details
-
getSocketfactory
Get the JSSE socket factory instance.- Returns:
- the socket factory
-
getHostnameVerifier
Get the configured hostname verifier.- Returns:
- the hostname verifier
-
getSupportedProtocols
Get the configured factory-wide supported protocols.- Returns:
- the configured protocols
-
getSupportedCipherSuites
Get the configured factory-wide supported cipher suites.- Returns:
- the configured cipher suites
-
prepareSocket
protected void prepareSocket(@Nonnull SSLSocket socket, @Nullable org.apache.http.protocol.HttpContext context) throws IOException Performs any custom initialization for a newly created SSLSocket (before the SSL handshake happens). The default implementation is a no-op, but could be overridden to, e.g., callSSLSocket.setEnabledCipherSuites(String[])
.- Parameters:
socket
- the SSL socket instance being preparedcontext
- the current HttpContext instance- Throws:
IOException
- if there is an error customizing the socket
-
createSocket
@Nonnull public Socket createSocket(@Nullable org.apache.http.protocol.HttpContext context) throws IOException - Specified by:
createSocket
in interfaceorg.apache.http.conn.socket.ConnectionSocketFactory
- Throws:
IOException
-
connectSocket
public Socket connectSocket(int connectTimeout, Socket socket, org.apache.http.HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, org.apache.http.protocol.HttpContext context) throws IOException - Specified by:
connectSocket
in interfaceorg.apache.http.conn.socket.ConnectionSocketFactory
- Throws:
IOException
-
createLayeredSocket
public Socket createLayeredSocket(Socket socket, String target, int port, org.apache.http.protocol.HttpContext context) throws IOException - Specified by:
createLayeredSocket
in interfaceorg.apache.http.conn.socket.LayeredConnectionSocketFactory
- Throws:
IOException
-
logSocketInfo
Log various diagnostic information from theSSLSocket
andSSLSession
.- Parameters:
socket
- the SSLSocket instance
-
getListAttribute
@Nullable protected String[] getListAttribute(@Nullable org.apache.http.protocol.HttpContext context, @Nonnull String contextKey) - Parameters:
context
- the current HttpContextcontextKey
- the attribute context key- Returns:
- a String array, or null
-
verifyHostname
protected void verifyHostname(@Nonnull SSLSocket sslsock, @Nonnull String hostname, @Nullable org.apache.http.protocol.HttpContext context) throws IOException Verify the peer's socket hostname against the supplied expected name.- Parameters:
sslsock
- the SSL socket being preparedhostname
- the expected hostnamecontext
- the current HttpContext instance- Throws:
IOException
- if peer failed hostname verification, or if there was an error during verification
-