@Test public void testBKS() throws CertificateException, KeyStoreException, IOException { System.out.println("Testing BKS..."); Certificate jkscert = generateCertificate(); String filename = "testks.bks"; String alias = "alias"; char[] password = "password".toCharArray(); boolean bcfailed = false; try { generateKeyStoreWithKey(filename, "bks", alias, password, jkscert); } catch (Exception e) { bcfailed = true; } Assume.assumeFalse("BC elytronProvider not found, skipping BC keystore recognition", bcfailed); KeyStore loadedStore = KeyStoreUtil.loadKeyStore(providerSupplier, null, new FileInputStream(new File(workingDir, filename)), filename, password); Assert.assertNotNull(loadedStore); Certificate loadedCert = loadedStore.getCertificate(alias); Assert.assertEquals(jkscert, loadedCert); }
public static KeyStore loadKeyStore(final Supplier<Provider[]> providers, final String providerName, FileInputStream is, String filename, char[] password) throws IOException, KeyStoreException{ DataInputStream dis = new ResetableDataFileInputStream(is); int firstInt = dis.readInt(); dis.reset(); KeyStore result = null; if (firstInt == JKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JKS); } else if (firstInt == JCEKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JCEKS); } else if (firstInt == VERSION_1 || firstInt == VERSION_2) { dis.reset(); dis.skip(32); byte firstElementType = dis.readByte(); dis.reset(); if (firstElementType <= 5) { result = tryLoadKeystore(providers, providerName, dis, password, BKS, UBER); } else { result = tryLoadKeystore(providers, providerName, dis, password, UBER, BKS); } } else if (firstInt == VERSION_0) { result = tryLoadKeystore(providers, providerName, dis, password, UBER); } else if ((firstInt & 0xff000000) == SEQUENCE) { String[] parts = filename.split("\\."); String extension = parts[parts.length - 1]; if (extension.startsWith("b") || extension.startsWith("B")) { result = tryLoadKeystore(providers, providerName, dis, password, BCFKS, PKCS12); } else { result = tryLoadKeystore(providers, providerName, dis, password, PKCS12, BCFKS); } } if (result == null) { throw log.keyStoreTypeNotDetected(); } return result; }
KeyStoreUtil { public static KeyStore loadKeyStore(final Supplier<Provider[]> providers, final String providerName, FileInputStream is, String filename, char[] password) throws IOException, KeyStoreException{ DataInputStream dis = new ResetableDataFileInputStream(is); int firstInt = dis.readInt(); dis.reset(); KeyStore result = null; if (firstInt == JKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JKS); } else if (firstInt == JCEKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JCEKS); } else if (firstInt == VERSION_1 || firstInt == VERSION_2) { dis.reset(); dis.skip(32); byte firstElementType = dis.readByte(); dis.reset(); if (firstElementType <= 5) { result = tryLoadKeystore(providers, providerName, dis, password, BKS, UBER); } else { result = tryLoadKeystore(providers, providerName, dis, password, UBER, BKS); } } else if (firstInt == VERSION_0) { result = tryLoadKeystore(providers, providerName, dis, password, UBER); } else if ((firstInt & 0xff000000) == SEQUENCE) { String[] parts = filename.split("\\."); String extension = parts[parts.length - 1]; if (extension.startsWith("b") || extension.startsWith("B")) { result = tryLoadKeystore(providers, providerName, dis, password, BCFKS, PKCS12); } else { result = tryLoadKeystore(providers, providerName, dis, password, PKCS12, BCFKS); } } if (result == null) { throw log.keyStoreTypeNotDetected(); } return result; } }
KeyStoreUtil { public static KeyStore loadKeyStore(final Supplier<Provider[]> providers, final String providerName, FileInputStream is, String filename, char[] password) throws IOException, KeyStoreException{ DataInputStream dis = new ResetableDataFileInputStream(is); int firstInt = dis.readInt(); dis.reset(); KeyStore result = null; if (firstInt == JKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JKS); } else if (firstInt == JCEKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JCEKS); } else if (firstInt == VERSION_1 || firstInt == VERSION_2) { dis.reset(); dis.skip(32); byte firstElementType = dis.readByte(); dis.reset(); if (firstElementType <= 5) { result = tryLoadKeystore(providers, providerName, dis, password, BKS, UBER); } else { result = tryLoadKeystore(providers, providerName, dis, password, UBER, BKS); } } else if (firstInt == VERSION_0) { result = tryLoadKeystore(providers, providerName, dis, password, UBER); } else if ((firstInt & 0xff000000) == SEQUENCE) { String[] parts = filename.split("\\."); String extension = parts[parts.length - 1]; if (extension.startsWith("b") || extension.startsWith("B")) { result = tryLoadKeystore(providers, providerName, dis, password, BCFKS, PKCS12); } else { result = tryLoadKeystore(providers, providerName, dis, password, PKCS12, BCFKS); } } if (result == null) { throw log.keyStoreTypeNotDetected(); } return result; }  }
KeyStoreUtil { public static KeyStore loadKeyStore(final Supplier<Provider[]> providers, final String providerName, FileInputStream is, String filename, char[] password) throws IOException, KeyStoreException{ DataInputStream dis = new ResetableDataFileInputStream(is); int firstInt = dis.readInt(); dis.reset(); KeyStore result = null; if (firstInt == JKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JKS); } else if (firstInt == JCEKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JCEKS); } else if (firstInt == VERSION_1 || firstInt == VERSION_2) { dis.reset(); dis.skip(32); byte firstElementType = dis.readByte(); dis.reset(); if (firstElementType <= 5) { result = tryLoadKeystore(providers, providerName, dis, password, BKS, UBER); } else { result = tryLoadKeystore(providers, providerName, dis, password, UBER, BKS); } } else if (firstInt == VERSION_0) { result = tryLoadKeystore(providers, providerName, dis, password, UBER); } else if ((firstInt & 0xff000000) == SEQUENCE) { String[] parts = filename.split("\\."); String extension = parts[parts.length - 1]; if (extension.startsWith("b") || extension.startsWith("B")) { result = tryLoadKeystore(providers, providerName, dis, password, BCFKS, PKCS12); } else { result = tryLoadKeystore(providers, providerName, dis, password, PKCS12, BCFKS); } } if (result == null) { throw log.keyStoreTypeNotDetected(); } return result; }  static KeyStore loadKeyStore(final Supplier<Provider[]> providers, final String providerName, FileInputStream is, String filename, char[] password); }
KeyStoreUtil { public static KeyStore loadKeyStore(final Supplier<Provider[]> providers, final String providerName, FileInputStream is, String filename, char[] password) throws IOException, KeyStoreException{ DataInputStream dis = new ResetableDataFileInputStream(is); int firstInt = dis.readInt(); dis.reset(); KeyStore result = null; if (firstInt == JKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JKS); } else if (firstInt == JCEKS_MAGIC) { result = tryLoadKeystore(providers, providerName, dis, password, JCEKS); } else if (firstInt == VERSION_1 || firstInt == VERSION_2) { dis.reset(); dis.skip(32); byte firstElementType = dis.readByte(); dis.reset(); if (firstElementType <= 5) { result = tryLoadKeystore(providers, providerName, dis, password, BKS, UBER); } else { result = tryLoadKeystore(providers, providerName, dis, password, UBER, BKS); } } else if (firstInt == VERSION_0) { result = tryLoadKeystore(providers, providerName, dis, password, UBER); } else if ((firstInt & 0xff000000) == SEQUENCE) { String[] parts = filename.split("\\."); String extension = parts[parts.length - 1]; if (extension.startsWith("b") || extension.startsWith("B")) { result = tryLoadKeystore(providers, providerName, dis, password, BCFKS, PKCS12); } else { result = tryLoadKeystore(providers, providerName, dis, password, PKCS12, BCFKS); } } if (result == null) { throw log.keyStoreTypeNotDetected(); } return result; }  static KeyStore loadKeyStore(final Supplier<Provider[]> providers, final String providerName, FileInputStream is, String filename, char[] password);  }