OadrPKISecurity { public static SSLContext createSSLContext(String clientPrivateKeyPemFilePath, String clientCertificatePemFilePath, List<String> trustCertificates, String password) throws OadrSecurityException { KeyManagerFactory kmf; TrustManagerFactory tmf; SSLContext sslContext; try { if (clientPrivateKeyPemFilePath != null && clientCertificatePemFilePath != null) { kmf = OadrPKISecurity.createKeyManagerFactory(clientPrivateKeyPemFilePath, clientCertificatePemFilePath, password); } else { kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(null, null); } if (trustCertificates != null) { tmf = OadrPKISecurity.createTrustManagerFactory(trustCertificates); } else { tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(KeyStore.getInstance("JKS")); } sslContext = SSLContext.getInstance("TLS"); String seed = UUID.randomUUID().toString(); sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom(seed.getBytes())); return sslContext; } catch (NoSuchAlgorithmException | KeyManagementException | UnrecoverableKeyException | KeyStoreException e) { throw new OadrSecurityException(e); } } private  OadrPKISecurity(); static PrivateKey parsePrivateKey(String privateKeyFilePath); static PrivateKey parsePrivateKey(FileReader fileReader); static X509Certificate parseCertificate(String certificateFilePath); static X509Certificate parseCertificate(FileReader fileReader); static KeyStore createKeyStore(String privateKeyFilePath, String clientCertificatefilePath, String password); static KeyManagerFactory createKeyManagerFactory(String clientPrivateKeyPemFilePath,
			String clientCertificatePemFilePath, String password); static KeyStore createTrustStore(List<String> certificates); static TrustManagerFactory createTrustManagerFactory(List<String> trustedCertificateFilePath); static SSLContext createSSLContext(String clientPrivateKeyPemFilePath, String clientCertificatePemFilePath,
			List<String> trustCertificates, String password); static String md5Hex(String data); static KeyPair generateRsaKeyPair(); static KeyPair generateEccKeyPair(); static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert,
			String commonName, OadrPKIAlgorithm algo);  }
@Test public void createSSLContextTest() throws OadrSecurityException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { String password = UUID.randomUUID().toString(); List<String> certificates = new ArrayList<>(); certificates.add(TestUtils.TEST_CRT); SSLContext createSSLContext = OadrPKISecurity.createSSLContext(TestUtils.TEST_KEY, TestUtils.TEST_CRT, certificates, password); assertNotNull(createSSLContext); createSSLContext = OadrPKISecurity.createSSLContext(TestUtils.TEST_KEY, TestUtils.TEST_CRT, Lists.newArrayList(), password); assertNotNull(createSSLContext); boolean exception = false; try { createSSLContext = OadrPKISecurity.createSSLContext(TestUtils.TEST_CRT, TestUtils.TEST_KEY, certificates, password); } catch (OadrSecurityException e) { exception = true; } assertTrue(exception); }
@Test public void createDefaultSSLContextTest() throws OadrSecurityException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { String password = UUID.randomUUID().toString(); List<String> certificates = new ArrayList<>(); certificates.add(TestUtils.TEST_CRT); SSLContext createSSLContext = OadrPKISecurity.createSSLContext(null, null, null, password); assertNotNull(createSSLContext); }