Last active
August 29, 2015 14:02
-
-
Save nuboat/30fd584f8c02e8cb4bc1 to your computer and use it in GitHub Desktop.
InstallCert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.thjug.tools.sslcert; | |
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; | |
/** | |
* Default cert path : $JAVA_HOME\jre\lib\security | |
* @author nuboat | |
*/ | |
public class InstallCert { | |
public static void main(final String[] args) throws Exception { | |
final String host = "facebook.com"; | |
final int port = 443; | |
final char[] passphrase = "changeit".toCharArray(); | |
final String source = "./src/main/resources/cacerts"; | |
final String output = "./src/main/resources/jssecacerts"; | |
final char[] outputpassphase = "changeit".toCharArray(); | |
final File file = new File(source); | |
System.out.println("Loading KeyStore " + file + "..."); | |
final KeyStore ks; | |
try (final InputStream in = new FileInputStream(file)) { | |
ks = KeyStore.getInstance(KeyStore.getDefaultType()); | |
ks.load(in, passphrase); | |
} | |
final SSLContext context = SSLContext.getInstance("TLS"); | |
final TrustManagerFactory tmf = | |
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | |
tmf.init(ks); | |
final X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0]; | |
final SavingTrustManager tm = new SavingTrustManager(defaultTrustManager); | |
context.init(null, new TrustManager[]{tm}, null); | |
final SSLSocketFactory factory = context.getSocketFactory(); | |
System.out.println("Opening connection to " + host + ":" + port + "..."); | |
try (final SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) { | |
socket.setSoTimeout(10000); | |
System.out.println("Starting SSL handshake..."); | |
socket.startHandshake(); | |
System.out.println(); | |
System.out.println("No errors, certificate is already trusted"); | |
return; | |
} catch (final Exception e) { | |
System.out.println("Errors: " + e.getMessage()); | |
} | |
final X509Certificate[] chain = tm.chain; | |
if (chain == null) { | |
System.out.println("Could not obtain server certificate chain"); | |
return; | |
} | |
System.out.println(); | |
System.out.println("Server sent " + chain.length + " certificate(s):"); | |
System.out.println(); | |
final MessageDigest sha1 = MessageDigest.getInstance("SHA1"); | |
final MessageDigest md5 = MessageDigest.getInstance("MD5"); | |
for (final X509Certificate cert : chain) { | |
System.out.println(" Subject " + cert.getSubjectDN()); | |
System.out.println(" Issuer " + cert.getIssuerDN()); | |
sha1.update(cert.getEncoded()); | |
System.out.println(" sha1 " + toHexString(sha1.digest())); | |
md5.update(cert.getEncoded()); | |
System.out.println(" md5 " + toHexString(md5.digest())); | |
} | |
int i = 1; | |
try (final OutputStream out = new FileOutputStream(output)) { | |
for (final X509Certificate cert : chain) { | |
final String alias = host + "-" + i++; | |
ks.setCertificateEntry(alias, cert); | |
ks.store(out, outputpassphase); | |
System.out.println(); | |
System.out.println(cert); | |
System.out.println(); | |
System.out.println("Added certificate to keystore 'cacerts.jks' using alias '" + alias + "'"); | |
} | |
} | |
} | |
private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray(); | |
private static String toHexString(final byte[] bytes) { | |
final StringBuilder sb = new StringBuilder(bytes.length * 3); | |
for (int b : bytes) { | |
b &= 0xff; | |
sb.append(HEXDIGITS[b >> 4]); | |
sb.append(HEXDIGITS[b & 15]); | |
sb.append(' '); | |
} | |
return sb.toString(); | |
} | |
private static class SavingTrustManager implements X509TrustManager { | |
private final X509TrustManager tm; | |
private X509Certificate[] chain; | |
SavingTrustManager(final X509TrustManager tm) { | |
this.tm = tm; | |
} | |
@Override | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
@Override | |
public void checkClientTrusted(final X509Certificate[] chain, final String authType) | |
throws CertificateException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void checkServerTrusted(final X509Certificate[] chain, final String authType) | |
throws CertificateException { | |
this.chain = chain; | |
tm.checkServerTrusted(chain, authType); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment