@Test public void testNoKeyInFile() { try { SSLKeyStoreLoader.fromPemKeyFile(invalidPemFile, cert); failBecauseExceptionWasNotThrown(Exception.class); } catch (Exception ex) { assertThat(ex).isInstanceOf(KeyStoreException.class) .hasMessageContaining("NO PRIVATE KEY FOUND IN FILE"); } }
static KeyManager[] fromPemKeyFile(Path key, Path certificate) throws IOException, GeneralSecurityException { final PKCS8EncodedKeySpec encodedKeySpec = getEncodedKeySpec(key); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec); final List<X509Certificate> certificates = getCertificates(certificate); KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); keyStore.load(null, null); keyStore.setKeyEntry(ALIAS, privateKey, EMPTY_PASSWORD, certificates.stream().toArray(Certificate[]::new)); final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, EMPTY_PASSWORD); return keyManagerFactory.getKeyManagers(); }
SSLKeyStoreLoader { static KeyManager[] fromPemKeyFile(Path key, Path certificate) throws IOException, GeneralSecurityException { final PKCS8EncodedKeySpec encodedKeySpec = getEncodedKeySpec(key); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec); final List<X509Certificate> certificates = getCertificates(certificate); KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); keyStore.load(null, null); keyStore.setKeyEntry(ALIAS, privateKey, EMPTY_PASSWORD, certificates.stream().toArray(Certificate[]::new)); final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, EMPTY_PASSWORD); return keyManagerFactory.getKeyManagers(); } }
SSLKeyStoreLoader { static KeyManager[] fromPemKeyFile(Path key, Path certificate) throws IOException, GeneralSecurityException { final PKCS8EncodedKeySpec encodedKeySpec = getEncodedKeySpec(key); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec); final List<X509Certificate> certificates = getCertificates(certificate); KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); keyStore.load(null, null); keyStore.setKeyEntry(ALIAS, privateKey, EMPTY_PASSWORD, certificates.stream().toArray(Certificate[]::new)); final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, EMPTY_PASSWORD); return keyManagerFactory.getKeyManagers(); } private  SSLKeyStoreLoader(); }
SSLKeyStoreLoader { static KeyManager[] fromPemKeyFile(Path key, Path certificate) throws IOException, GeneralSecurityException { final PKCS8EncodedKeySpec encodedKeySpec = getEncodedKeySpec(key); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec); final List<X509Certificate> certificates = getCertificates(certificate); KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); keyStore.load(null, null); keyStore.setKeyEntry(ALIAS, privateKey, EMPTY_PASSWORD, certificates.stream().toArray(Certificate[]::new)); final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, EMPTY_PASSWORD); return keyManagerFactory.getKeyManagers(); } private  SSLKeyStoreLoader();  }
SSLKeyStoreLoader { static KeyManager[] fromPemKeyFile(Path key, Path certificate) throws IOException, GeneralSecurityException { final PKCS8EncodedKeySpec encodedKeySpec = getEncodedKeySpec(key); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec); final List<X509Certificate> certificates = getCertificates(certificate); KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); keyStore.load(null, null); keyStore.setKeyEntry(ALIAS, privateKey, EMPTY_PASSWORD, certificates.stream().toArray(Certificate[]::new)); final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, EMPTY_PASSWORD); return keyManagerFactory.getKeyManagers(); } private  SSLKeyStoreLoader();   }