Source:
private static SSLSocketFactory getSocketFactory(final String caCrtFile, final String crtFile, final String keyFile, final String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); X509Certificate caCert = null; FileInputStream fis = new FileInputStream(getFilePath(caCrtFile)); BufferedInputStream bis = new BufferedInputStream(fis); CertificateFactory cf = CertificateFactory.getInstance("X.509"); while (bis.available() > 0) { caCert = (X509Certificate) cf.generateCertificate(bis); } bis = new BufferedInputStream(new FileInputStream(getFilePath(crtFile))); X509Certificate cert = null; while (bis.available() > 0) { cert = (X509Certificate) cf.generateCertificate(bis); } PEMParser pemParser = new PEMParser(new FileReader(getFilePath(keyFile))); Object object = pemParser.readObject(); PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder() .build(password.toCharArray()); JcaPEMKeyConverter converter = new JcaPEMKeyConverter() .setProvider("BC"); KeyPair key; if (object instanceof PEMEncryptedKeyPair) { key = converter.getKeyPair(((PEMEncryptedKeyPair) object) .decryptKeyPair(decProv)); } else { key = converter.getKeyPair((PEMKeyPair) object); } pemParser.close(); KeyStore caKs = KeyStore.getInstance(KeyStore.getDefaultType()); caKs.load(null, null); caKs.setCertificateEntry("ca-certificate", caCert); TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); tmf.init(caKs); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null, null); ks.setCertificateEntry("certificate", cert); ks.setKeyEntry("private-key", key.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[]{cert}); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory .getDefaultAlgorithm()); kmf.init(ks, password.toCharArray()); SSLContext context = SSLContext.getInstance("TLSv1.2"); context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); return context.getSocketFactory(); }

Target:
private static SSLSocketFactory getSocketFactory(final String caCrtFile, final String crtFile, final String keyFile, final String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); X509Certificate caCert = null; final FileInputStream fis = new FileInputStream(getFilePath(caCrtFile)); final CertificateFactory cf = CertificateFactory.getInstance("X.509"); BufferedInputStream bis = new BufferedInputStream(fis); while (bis.available() > 0) { caCert = (X509Certificate) cf.generateCertificate(bis); } bis = new BufferedInputStream(new FileInputStream(getFilePath(crtFile))); X509Certificate cert = null; while (bis.available() > 0) { cert = (X509Certificate) cf.generateCertificate(bis); } final PEMParser pemParser = new PEMParser(new FileReader(getFilePath(keyFile))); final Object object = pemParser.readObject(); final PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder() .build(password.toCharArray()); final JcaPEMKeyConverter converter = new JcaPEMKeyConverter() .setProvider("BC"); KeyPair key; if (object instanceof PEMEncryptedKeyPair) { key = converter.getKeyPair(((PEMEncryptedKeyPair) object) .decryptKeyPair(decProv)); } else { key = converter.getKeyPair((PEMKeyPair) object); } pemParser.close(); final KeyStore caKs = KeyStore.getInstance(KeyStore.getDefaultType()); caKs.load(null, null); caKs.setCertificateEntry("ca-certificate", caCert); final TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); tmf.init(caKs); final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null, null); ks.setCertificateEntry("certificate", cert); ks.setKeyEntry("private-key", key.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[]{cert}); final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory .getDefaultAlgorithm()); kmf.init(ks, password.toCharArray()); SSLContext context = SSLContext.getInstance("TLSv1.2"); context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); return context.getSocketFactory(); }