Skip to content

Instantly share code, notes, and snippets.

@c99koder
Created January 27, 2014 16:48
Show Gist options
  • Save c99koder/8652297 to your computer and use it in GitHub Desktop.
Save c99koder/8652297 to your computer and use it in GitHub Desktop.
TrustManager[] tms = new TrustManager[1];
tms[0] = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
throw new CertificateException("Not implemented");
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
trustManagerFactory.init((KeyStore)null);
for (TrustManager trustManager: trustManagerFactory.getTrustManagers()) {
if (trustManager instanceof X509TrustManager) {
X509TrustManager x509TrustManager = (X509TrustManager)trustManager;
x509TrustManager.checkServerTrusted(chain, authType);
}
}
} catch (KeyStoreException e) {
throw new CertificateException(e);
} catch (NoSuchAlgorithmException e) {
throw new CertificateException(e);
}
if(BuildConfig.SSL_CN.length() > 0 && !chain[0].getSubjectDN().getName().startsWith("CN=*.irccloud.com")) {
throw new CertificateException("Incorrect CN in cert chain");
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
WebSocketClient.setTrustManagers(tms);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment