package com.sterlingcommerce.devtest;
import javax.net.ssl.*;
import java.io.*;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Class used to add the server's certificate to the KeyStore
* with your trusted certificates.
*/
public class CipherTest {
public static void main(String[] args) throws Exception {
String host = null;
int port = 0;
char[] passphrase = "changeit".toCharArray();
String path = null;
int j=0;
while(j<args.length){
if(args[j].startsWith("-")){
if(args[j].equalsIgnoreCase("-host")){
host=args[j+1];
} else if(args[j].equalsIgnoreCase("-port")) {
port=Integer.parseInt(args[j+1]);
} else if(args[j].equalsIgnoreCase("-passphrase")){
passphrase=args[j+1].toCharArray();
} else if(args[j].equalsIgnoreCase("-cipherSuitePropertyFileLocation")) {
path=args[j+1];
} else {
System.out.println("Invalid Values:"+args[j]);
System.out.println("Usage: java -host <host> -port <port> -cipherSuitePropertyFileLocation <file location along with file name> -passphrase <(optional) passphrase>");
}
j++;
}
if((args.length!=6 && args.length!=8) || host == null || path == null || port==0 ){
System.out.println("Usage: java -host <host> -port <port> -cipherSuitePropertyFileLocation <file location along with file name> -passphrase <(optional) passphrase>");
return;
}
if(!path.endsWith(".properties")){
System.out.println("Path should contain the file name and file extension should be .properties");
return;
}
File file = new File("jssecacerts");
if (file.isFile() == false) {
char SEP = File.separatorChar;
File dir = new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security");
file = new File(dir, "jssecacerts");
if (file.isFile() == false) {
file = new File(dir, "cacerts");
}
}
InputStream in = new FileInputStream(file);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(in, passphrase);
in.close();
System.out.println("Ensuring the Cipher Support:");
SSLContext context = SSLContext.getInstance("TLS");
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
context.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory factory = context.getSocketFactory();
System.out.println("Opening connection to " + host + ":" + port + "...");
List<String> SuccessCiphers = new ArrayList<String>();
List<String> FailureCiphers = new ArrayList<String>();
String[] CipherList = null;
System.out.println("Starting SSL handshake:");
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(path);
prop.load(input);
if(prop.getProperty("defaultCipher")!=null) {
CipherList = prop.getProperty("defaultCipher").split(",");
} else {
System.out.println("There is not property called defaultCipher in the given properties file. "
+ "Please add this property into the file with the cipher name ");
return;
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
for (String x: CipherList)
try {
SSLSocket socket = (SSLSocket)factory.createSocket(host, port);
socket.setEnabledProtocols(new String[]{"TLSv1","TLSv1.1","TLSv1.2"});
socket.setSoTimeout(10000);
socket.setEnabledCipherSuites(new String[] {x});
socket.startHandshake();
System.out.println("Cipher: "+socket.getSession().getCipherSuite()+" ---->Success");
SuccessCiphers.add(socket.getSession().getCipherSuite());
socket.close();
} catch (SSLException e) {
System.out.println("Cipher: "+x+" ---->Failed");
FailureCiphers.add(x);
}
System.out.println();
System.out.println("Supported Ciphers:");
for(String SC:SuccessCiphers){
System.out.println(SC);
}
System.out.println();
System.out.println("UnSupported Ciphers:");
for(String FC:FailureCiphers){
System.out.println(FC);
}
X509Certificate[] chain = tm.chain;
if (chain == null) {
System.out.println("Could not obtain server certificate chain");
return;
}
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
MessageDigest md5 = MessageDigest.getInstance("MD5");
for (int i = 0; i < chain.length; i++) {
X509Certificate cert = chain[i];
sha1.update(cert.getEncoded());
md5.update(cert.getEncoded());
}
int k=0;
X509Certificate cert = chain[k];
String alias = host + "-" + (k + 1);
ks.setCertificateEntry(alias, cert);
OutputStream out = new FileOutputStream("jssecacerts");
ks.store(out, passphrase);
out.close();
}
private static class SavingTrustManager implements X509TrustManager {
private final X509TrustManager tm;
private X509Certificate[] chain;
SavingTrustManager(X509TrustManager tm) {
this.tm = tm;
}
public X509Certificate[] getAcceptedIssuers() {
//throw new UnsupportedOperationException();
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
throw new UnsupportedOperationException();
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
this.chain = chain;
tm.checkServerTrusted(chain, authType);
}
}
}
|