@Test public void testNoKeystore() throws IOException, KeyStoreException{ String filename = "notakeystore.txt"; File f = new File(workingDir, filename); files.add(f); FileWriter fw = new FileWriter(f); fw.write("joejoejoe"); fw.flush(); fw.close(); try { KeyStore loadedStore = KeyStoreUtil.loadKeyStore(providerSupplier, null, new FileInputStream(f), filename, "whatever".toCharArray()); } catch (Exception e) { return; } Assert.fail("Key store detection should fail."); }
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);  }