Created
May 27, 2014 10:20
-
-
Save vegaasen/f83a388b369a1e8f2c70 to your computer and use it in GitHub Desktop.
This file contains 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.vegaasen.playhouse.cert.run; | |
import com.telenor.sec.certificate.cert.CertificateUtils; | |
import com.telenor.sec.certificate.common.CertificateProperties; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.SSLSocketFactory; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.security.cert.Certificate; | |
import java.security.cert.CertificateEncodingException; | |
import java.security.cert.X509Certificate; | |
/** | |
* @author <a href="vegard.aasen@telenor.com">Vegard Aasen</a> | |
*/ | |
public final class ClientRequest { | |
private static final String | |
KEYSTORE = "whatnot.jks", | |
KEYSTORE_PASSWORD = "whatnot", | |
SERVER_HOST = "https://otl.telenormobil.no/", | |
TRUSTSTORE = "", | |
TRUSTSTORE_PASSWORD = ""; | |
public static void main(final String... args) { | |
try { | |
configureKeyStore(); | |
final URL url = new URL(SERVER_HOST); | |
final HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); | |
conn.setSSLSocketFactory(configureSslSocketFactory()); | |
conn.connect(); | |
final Certificate[] certs = conn.getServerCertificates(); | |
if (certs != null) { | |
System.out.println("Connected! The server certificates seems to be in order.\n*********Some cool server-certificate-information*********"); | |
for (final Certificate certificate : certs) { | |
X509Certificate cert = CertificateUtils.getCertificateFromByteArray(certificate.getEncoded(), CertificateProperties.ALG_X_509); | |
System.out.println(cert.getSubjectDN()); | |
} | |
} else { | |
System.out.print("Connected, however unable to fetch server certificates."); | |
} | |
System.out.println(String.format("Protocol used: {%s}", System.getProperty("https.protocols"))); | |
} catch (final IOException e) { | |
System.out.print("Something went wrong."); | |
e.printStackTrace(); | |
} catch (CertificateEncodingException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static SSLSocketFactory configureSslSocketFactory() { | |
return (SSLSocketFactory) SSLSocketFactory.getDefault(); | |
} | |
private static void configureKeyStore() { | |
System.setProperty("javax.net.ssl.keyStore", ClassLoader.getSystemResource(KEYSTORE).getPath()); | |
System.setProperty("javax.net.ssl.keyStorePassword", KEYSTORE_PASSWORD); | |
System.setProperty("https.protocols", "SSLv3,SSLv2Hello"); | |
} | |
private static void configureTrustStore() { | |
System.setProperty("javax.net.ssl.trustStore", ClassLoader.getSystemResource(TRUSTSTORE).getPath()); | |
System.setProperty("javax.net.ssl.trustStorePassword", TRUSTSTORE_PASSWORD); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment