Top Description Fields Constructors Methods
org.apache.http.conn.ssl

public Class SSLConnectionSocketFactory

extends Object
implements LayeredConnectionSocketFactory
Class Inheritance
All Implemented Interfaces
org.apache.http.conn.socket.LayeredConnectionSocketFactory, org.apache.http.conn.socket.ConnectionSocketFactory
Annotations
@Contract
threading:SAFE
@SuppressWarnings:deprecation
Imports
java.io.IOException, .InputStream, java.net.InetSocketAddress, .Socket, java.security.cert.Certificate, .X509Certificate, java.util.ArrayList, .Arrays, .Collection, .Collections, .List, java.util.regex.Pattern, javax.net.SocketFactory, javax.net.ssl.HostnameVerifier, .SSLContext, .SSLHandshakeException, .SSLPeerUnverifiedException, .SSLSession, .SSLSocket, javax.security.auth.x500.X500Principal, org.apache.commons.logging.Log, .LogFactory, org.apache.http.HttpHost, org.apache.http.annotation.Contract, .ThreadingBehavior, org.apache.http.conn.socket.LayeredConnectionSocketFactory, org.apache.http.conn.util.PublicSuffixMatcherLoader, org.apache.http.protocol.HttpContext, org.apache.http.ssl.SSLContexts, org.apache.http.util.Args, .TextUtils

Layered socket factory for TLS/SSL connections.

SSLSocketFactory can be used to validate the identity of the HTTPS server against a list of trusted certificates and to authenticate to the HTTPS server using a private key.

SSLSocketFactory will enable server authentication when supplied with a trust-store file containing one or several trusted certificates. The client secure socket will reject the connection during the SSL session handshake if the target HTTPS server attempts to authenticate itself with a non-trusted certificate.

Use JDK keytool utility to import a trusted certificate and generate a trust-store file:

    keytool -import -alias "my server cert" -file server.crt -keystore my.truststore
   

In special cases the standard trust verification process can be bypassed by using a custom org.apache.http.conn.ssl.TrustStrategy. This interface is primarily intended for allowing self-signed certificates to be accepted as trusted without having to add them to the trust-store file.

SSLSocketFactory will enable client authentication when supplied with a key-store file containing a private key/public certificate pair. The client secure socket will use the private key to authenticate itself to the target HTTPS server during the SSL session handshake if requested to do so by the server. The target HTTPS server will in its turn verify the certificate presented by the client in order to establish client's authenticity.

Use the following sequence of actions to generate a key-store file

Since
4.3

Field Summary

Modifier and TypeField and Description
public static final X509HostnameVerifier
public static final X509HostnameVerifier
private final HostnameVerifier
private final Log
private final SSLSocketFactory
public static final String
public static final String
public static final X509HostnameVerifier
private final String[]
private final String[]
public static final String
private static final List<Pattern>
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
public
public
public
SSLConnectionSocketFactory(final SSLContext sslContext, final String[] supportedProtocols, final String[] supportedCipherSuites, final X509HostnameVerifier hostnameVerifier)

public
public
SSLConnectionSocketFactory(final SSLSocketFactory socketfactory, final String[] supportedProtocols, final String[] supportedCipherSuites, final X509HostnameVerifier hostnameVerifier)

public
SSLConnectionSocketFactory(final SSLContext sslContext, final HostnameVerifier hostnameVerifier)

public
SSLConnectionSocketFactory(final SSLContext sslContext, final String[] supportedProtocols, final String[] supportedCipherSuites, final HostnameVerifier hostnameVerifier)

public
SSLConnectionSocketFactory(final SSLSocketFactory socketfactory, final HostnameVerifier hostnameVerifier)

public
SSLConnectionSocketFactory(final SSLSocketFactory socketfactory, final String[] supportedProtocols, final String[] supportedCipherSuites, final HostnameVerifier hostnameVerifier)

Method Summary

Modifier and TypeMethod and Description
public Socket
connectSocket(final int
connect timeout.
connectTimeout
,
final Socket
the socket to connect, as obtained from createSocket(HttpContext). null indicates that a new socket should be created and connected.
socket
,
final HttpHost
target host as specified by the caller (end user).
host
,
final InetSocketAddress
the resolved remote address to connect to.
remoteAddress
,
final InetSocketAddress
the local address to bind the socket to, or null for any.
localAddress
,
final HttpContext
the actual HTTP context.
context
)

Implements org.apache.http.conn.socket.ConnectionSocketFactory.connectSocket.

Connects the socket to the target host with the given resolved remote address.
public Socket
createLayeredSocket(final Socket
the existing socket
socket
,
final String
the name of the target host.
target
,
final int
the port to connect to on the target host.
port
,
final HttpContext
the actual HTTP context.
context
)

Implements org.apache.http.conn.socket.LayeredConnectionSocketFactory.createLayeredSocket.

Returns a socket connected to the given host that is layered over an existing socket.
public Socket
createSocket(final HttpContext context)

Implements org.apache.http.conn.socket.ConnectionSocketFactory.createSocket.

Creates new, unconnected socket.
public static HostnameVerifier
public static SSLConnectionSocketFactory

Returns:

default SSL socket factory
getSocketFactory
()

Obtains default SSL socket factory with an SSL context based on the standard JSSE trust material (cacerts file in the security properties directory).

public static SSLConnectionSocketFactory

Returns:

default system SSL socket factory
getSystemSocketFactory
()

Obtains default SSL socket factory with an SSL context based on system properties as described in Java™ Secure Socket Extension (JSSE) Reference Guide.

pack-priv static boolean
isWeakCipherSuite(final String cipherSuite)

protected void
prepareSocket(final SSLSocket socket)

Performs any custom initialization for a newly created SSLSocket (before the SSL handshake happens).

private static String[]
split(final String s)

private void
verifyHostname(final SSLSocket sslsock, final String hostname)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALLOW_ALL_HOSTNAME_VERIFIERback to summary
public static final X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER
Annotations
@Deprecated
BROWSER_COMPATIBLE_HOSTNAME_VERIFIERback to summary
public static final X509HostnameVerifier BROWSER_COMPATIBLE_HOSTNAME_VERIFIER
Annotations
@Deprecated
hostnameVerifierback to summary
private final HostnameVerifier hostnameVerifier
logback to summary
private final Log log
socketfactoryback to summary
private final SSLSocketFactory socketfactory
SSLback to summary
public static final String SSL
SSLV2back to summary
public static final String SSLV2
STRICT_HOSTNAME_VERIFIERback to summary
public static final X509HostnameVerifier STRICT_HOSTNAME_VERIFIER
Annotations
@Deprecated
supportedCipherSuitesback to summary
private final String[] supportedCipherSuites
supportedProtocolsback to summary
private final String[] supportedProtocols
TLSback to summary
public static final String TLS
WEAK_CIPHER_SUITE_PATTERNSback to summary
private static final List<Pattern> WEAK_CIPHER_SUITE_PATTERNS
WEAK_CIPHERSback to summary
private static final String WEAK_CIPHERS
WEAK_KEY_EXCHANGESback to summary
private static final String WEAK_KEY_EXCHANGES

Constructor Detail

SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLContext sslContext)
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLContext sslContext, final X509HostnameVerifier hostnameVerifier)
Annotations
@Deprecated
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLContext sslContext, final String[] supportedProtocols, final String[] supportedCipherSuites, final X509HostnameVerifier hostnameVerifier)
Annotations
@Deprecated
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLSocketFactory socketfactory, final X509HostnameVerifier hostnameVerifier)
Annotations
@Deprecated
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLSocketFactory socketfactory, final String[] supportedProtocols, final String[] supportedCipherSuites, final X509HostnameVerifier hostnameVerifier)
Annotations
@Deprecated
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLContext sslContext, final HostnameVerifier hostnameVerifier)
Since
4.4
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLContext sslContext, final String[] supportedProtocols, final String[] supportedCipherSuites, final HostnameVerifier hostnameVerifier)
Since
4.4
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLSocketFactory socketfactory, final HostnameVerifier hostnameVerifier)
Since
4.4
SSLConnectionSocketFactoryback to summary
public SSLConnectionSocketFactory(final SSLSocketFactory socketfactory, final String[] supportedProtocols, final String[] supportedCipherSuites, final HostnameVerifier hostnameVerifier)
Since
4.4

Method Detail

connectSocketback to summary
public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException

Implements org.apache.http.conn.socket.ConnectionSocketFactory.connectSocket.

Doc from org.apache.http.conn.socket.ConnectionSocketFactory.connectSocket.

Connects the socket to the target host with the given resolved remote address.

Parameters
connectTimeout:int

connect timeout.

socket:Socket

the socket to connect, as obtained from createSocket(HttpContext). null indicates that a new socket should be created and connected.

host:HttpHost

target host as specified by the caller (end user).

remoteAddress:InetSocketAddress

the resolved remote address to connect to.

localAddress:InetSocketAddress

the local address to bind the socket to, or null for any.

context:HttpContext

the actual HTTP context.

Returns:Socket

the connected socket. The returned object may be different from the sock argument if this factory supports a layered protocol.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
createLayeredSocketback to summary
public Socket createLayeredSocket(final Socket socket, final String target, final int port, final HttpContext context) throws IOException

Implements org.apache.http.conn.socket.LayeredConnectionSocketFactory.createLayeredSocket.

Doc from org.apache.http.conn.socket.LayeredConnectionSocketFactory.createLayeredSocket.

Returns a socket connected to the given host that is layered over an existing socket. Used primarily for creating secure sockets through proxies.

Parameters
socket:Socket

the existing socket

target:String

the name of the target host.

port:int

the port to connect to on the target host.

context:HttpContext

the actual HTTP context.

Returns:Socket

Socket a new socket

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs while creating the socket
createSocketback to summary
public Socket createSocket(final HttpContext context) throws IOException

Implements org.apache.http.conn.socket.ConnectionSocketFactory.createSocket.

Doc from org.apache.http.conn.socket.ConnectionSocketFactory.createSocket.

Creates new, unconnected socket. The socket should subsequently be passed to connectSocket method.

Returns:Socket

a new socket

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs while creating the socket
getDefaultHostnameVerifierback to summary
public static HostnameVerifier getDefaultHostnameVerifier()
Since
4.4
getSocketFactoryback to summary
public static SSLConnectionSocketFactory getSocketFactory() throws SSLInitializationException

Obtains default SSL socket factory with an SSL context based on the standard JSSE trust material (cacerts file in the security properties directory). System properties are not taken into consideration.

Returns:SSLConnectionSocketFactory

default SSL socket factory

getSystemSocketFactoryback to summary
public static SSLConnectionSocketFactory getSystemSocketFactory() throws SSLInitializationException

Obtains default SSL socket factory with an SSL context based on system properties as described in Java™ Secure Socket Extension (JSSE) Reference Guide.

Returns:SSLConnectionSocketFactory

default system SSL socket factory

isWeakCipherSuiteback to summary
pack-priv static boolean isWeakCipherSuite(final String cipherSuite)
prepareSocketback to summary
protected void prepareSocket(final SSLSocket socket) 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., call javax.net.ssl.SSLSocket#setEnabledCipherSuites(String[]).

Exceptions
IOException:
may be thrown if overridden
splitback to summary
private static String[] split(final String s)
verifyHostnameback to summary
private void verifyHostname(final SSLSocket sslsock, final String hostname) throws IOException