SecureSocketUtils { public static KeyStore loadKeystore(final String keyStoreFile, final String keyStorePassword, final KeyStoreTypeEnum keyStoreType) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException { final FileInputStream inputStream = new FileInputStream(keyStoreFile); KeyStore keyStore; try { keyStore = getKeyStoreInstance(keyStoreType); keyStore.load(inputStream, keyStorePassword.toCharArray()); } finally { if (inputStream != null) { inputStream.close(); } } return keyStore; }  static byte[] loadPemFileAsBytes(final String file); static byte[] loadBinaryFileAsBytes(final String file); static PrivateKey loadPrivateKeyFromPemFile(final String keyFile); static PrivateKey loadPrivateKeyFromBinaryFile(final String keyFile); static X509Certificate loadX509Certificate(final String certificateFile); static TrustManagerFactory getTrustManagerFactory(final String caCertificateFile); static TrustManagerFactory getTrustManagerFactory(final String keyStoreFile, final String keyStorePassword,
			final KeyStoreTypeEnum keyStoreType); static KeyManagerFactory getKeyManagerFactory(final String keyStoreFile, final String keyStorePassword, 
			final String keyPassword, final KeyStoreTypeEnum keyStoreType); static KeyManagerFactory getKeyManagerFactory(
			final String clientCertificateFile, final String clientKeyFile, final String clientKeyPassword, final boolean pemFormat); static KeyStore loadKeystore(final String keyStoreFile, final String keyStorePassword, final KeyStoreTypeEnum keyStoreType); static KeyStore getKeyStoreInstance(final KeyStoreTypeEnum type); static KeyStore getKeyStoreInstance(final KeyStoreTypeEnum type, final Provider provider); static KeyStoreTypeEnum getTypeFromFilename(final String filename);  }
@Test public void testLoadingDefaultKeyStore() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { SecureSocketUtils.loadKeystore("src/test/resources/keystores/public_brokers.jks", "mqtt-spy", KeyStoreTypeEnum.DEFAULT); }
@Test (expected = IOException.class) public void testInvalidFormatForDefaultType() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { SecureSocketUtils.loadKeystore("src/test/resources/keystores/public_brokers.jceks", "mqtt-spy", KeyStoreTypeEnum.DEFAULT); }
@Test public void testLoadingKeyStoreForType() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { SecureSocketUtils.loadKeystore("src/test/resources/keystores/public_brokers.jks", "mqtt-spy", KeyStoreTypeEnum.JKS); SecureSocketUtils.loadKeystore("src/test/resources/keystores/public_brokers.jceks", "mqtt-spy", KeyStoreTypeEnum.JCEKS); SecureSocketUtils.loadKeystore("src/test/resources/keystores/public_brokers.p12", "mqtt-spy", KeyStoreTypeEnum.PKCS_12); }
@Test public void testLoadingBksKeyStore() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { Security.addProvider(new BouncyCastleProvider()); SecureSocketUtils.loadKeystore("src/test/resources/keystores/public_brokers.bks", "mqtt-spy", KeyStoreTypeEnum.BKS); }