// Ciphers...
@Override
	public Socket createSocket (final Socket plainSocket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
		// we don't need the plainSocket
		if (autoClose) plainSocket.close();
		// create and connect SSL socket, but don't do hostname/certificate verification yet.
		final SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
		sslSocketFactory.setTrustManagers(this.trustManager);
		final SSLSocket sock = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
		// Protocols...
		final List<String> protocols = new ArrayList<String>();
		for (final String protocol : sock.getSupportedProtocols()) {
			if (!protocol.toUpperCase(Locale.ENGLISH).contains("SSL")) protocols.add(protocol);
		}
		sock.setEnabledProtocols(protocols.toArray(new String[0]));
		// Ciphers...
		final HashSet<String> ciphers = new HashSet<String>(ALLOWED_CIPHERS);
		ciphers.retainAll(Arrays.asList(sock.getSupportedCipherSuites()));
		ciphers.addAll(new HashSet<String>(Arrays.asList(sock.getEnabledCipherSuites()))); // All all already enabled ones for compatibility.
		sock.setEnabledCipherSuites(ciphers.toArray(new String[0]));
		// set up SNI before the handshake.
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
			sslSocketFactory.setHostname(sock, host);
		}
		else { // This hack seems to work on my 4.0.4 tablet.
			try {
				final java.lang.reflect.Method setHostnameMethod = sock.getClass().getMethod("setHostname", String.class);
				setHostnameMethod.invoke(sock, host);
			}
			catch (final Exception e) {
				LOG.w("SNI not useable: %s", ExcpetionHelper.causeTrace(e));
			}
		}
		// verify hostname and certificate.
		final SSLSession session = sock.getSession();
		if (!HOSTNAME_VERIFIER.verify(host, session)) throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
		LOG.i("Connected %s %s %s.", session.getPeerHost(), session.getProtocol(), session.getCipherSuite());
		return sock;
	}
}
sock.setEnabledProtocols(protocols.toArray(new String[0]));
// Ciphers...
final HashSet<String> ciphers = new HashSet<String>(ALLOWED_CIPHERS);
ciphers.retainAll(Arrays.asList(sock.getSupportedCipherSuites()));
// create and connect SSL socket, but don't do hostname/certificate verification yet.
final SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
sslSocketFactory.setTrustManagers(this.trustManager);
final SSLSocket sock = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
// Protocols...
final List<String> protocols = new ArrayList<String>();
for (final String protocol : sock.getSupportedProtocols()) {
if (!protocol.toUpperCase(Locale.ENGLISH).contains("SSL")) protocols.add(protocol);
}
sock.setEnabledProtocols(protocols.toArray(new String[0]));
// Ciphers...
final HashSet<String> ciphers = new HashSet<String>(ALLOWED_CIPHERS);
ciphers.retainAll(Arrays.asList(sock.getSupportedCipherSuites()));
ciphers.addAll(new HashSet<String>(Arrays.asList(sock.getEnabledCipherSuites()))); // All all already enabled ones for compatibility.
sock.setEnabledCipherSuites(ciphers.toArray(new String[0]));
// set up SNI before the handshake.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sslSocketFactory.setHostname(sock, host);
}
else { // This hack seems to work on my 4.0.4 tablet.
try {
@Override
public Socket createSocket (final Socket plainSocket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
// we don't need the plainSocket
if (autoClose) plainSocket.close();
// create and connect SSL socket, but don't do hostname/certificate verification yet.
final SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
sslSocketFactory.setTrustManagers(this.trustManager);
final SSLSocket sock = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
// Protocols...
final List<String> protocols = new ArrayList<String>();
for (final String protocol : sock.getSupportedProtocols()) {
if (!protocol.toUpperCase(Locale.ENGLISH).contains("SSL")) protocols.add(protocol);
}
sock.setEnabledProtocols(protocols.toArray(new String[0]));
// Ciphers...
final HashSet<String> ciphers = new HashSet<String>(ALLOWED_CIPHERS);
ciphers.retainAll(Arrays.asList(sock.getSupportedCipherSuites()));
ciphers.addAll(new HashSet<String>(Arrays.asList(sock.getEnabledCipherSuites()))); // All all already enabled ones for compatibility.
sock.setEnabledCipherSuites(ciphers.toArray(new String[0]));
// set up SNI before the handshake.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sslSocketFactory.setHostname(sock, host);
}
else { // This hack seems to work on my 4.0.4 tablet.
try {
final java.lang.reflect.Method setHostnameMethod = sock.getClass().getMethod("setHostname", String.class);
setHostnameMethod.invoke(sock, host);
}
catch (final Exception e) {
LOG.w("SNI not useable: %s", ExcpetionHelper.causeTrace(e));
}
}
// verify hostname and certificate.
final SSLSession session = sock.getSession();
if (!HOSTNAME_VERIFIER.verify(host, session)) throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);