Skip to content

Instantly share code, notes, and snippets.

@tleyden
Created April 22, 2014 20:28
Show Gist options
  • Save tleyden/11193162 to your computer and use it in GitHub Desktop.
Save tleyden/11193162 to your computer and use it in GitHub Desktop.
Customize CouchbaseLiteHttpClientFactory
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
private void initializeSecurity() throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, InterruptedException, UnrecoverableKeyException {
AssetManager am = getAssets();
InputStream is = am.open("ca.pem");
// Load CAs from an InputStream
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = new BufferedInputStream(is);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
} finally {
caInput.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
SSLSocketFactory sf = new MySSLSocketFactory(keyStore);
CouchbaseLiteHttpClientFactory.INSTANCE.setSSLSocketFactory(sf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment