Skip to content

Instantly share code, notes, and snippets.

@levisre
Last active May 17, 2016 09:35
Show Gist options
  • Select an option

  • Save levisre/3f9d7bab203a3d4f0fe6b88cc11f901e to your computer and use it in GitHub Desktop.

Select an option

Save levisre/3f9d7bab203a3d4f0fe6b88cc11f901e to your computer and use it in GitHub Desktop.
Testing TLSv1.2 and Default TLSv1 in JDK1.7
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testssl;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
//Need Apache HttpClient
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
/**
*
* @author levis
*/
public class TestSSL {
/**
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.security.NoSuchAlgorithmException
* @throws java.security.KeyManagementException
*/
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, IOException {
SocketFactory cus = CustomizedSSL();
System.out.println("Testing with TLSv1.2");
Test_SSL(cus);
SocketFactory std = DefaultSSL();
System.out.println("Testing with Default SSL/TLS");
Test_SSL(std);
}
//Create Socket Factory with TLS1.2 enabled
public static SocketFactory CustomizedSSL() throws NoSuchAlgorithmException, KeyManagementException{
SSLContext context;
context = SSLContext.getInstance("TLSv1.2");
context.init(null, null, null);
//SSLSocketFactory
SSLSocketFactory
sslsf;
sslsf = new SSLSocketFactory(context);
/*sslcontext,
new String[] { "TLSv1.2"},
null,
/*new String[] {"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"}
SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);*/
return (SocketFactory) sslsf;
}
//Create SocketFactory with Default TLS1 (Java 7)
public static SocketFactory DefaultSSL() throws NoSuchAlgorithmException{
SSLContext context;
context = SSLContext.getDefault();
SSLSocketFactory sslsf;
sslsf = new SSLSocketFactory(context);
return (SocketFactory) sslsf;
}
public static void Test_SSL(SocketFactory sf) throws IOException{
HttpGet get;
get = new HttpGet("https://google.com/");
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", (SocketFactory) sf,443));
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
ClientConnectionManager cm = new SingleClientConnManager(params, registry);
DefaultHttpClient mClient = new DefaultHttpClient(cm, params);
HttpResponse response = mClient.execute(get);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(response.toString());
EntityUtils.consume(entity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment