@Test public void testOpenKeyStore() throws Exception { File dir = OftpTestUtil.getResourceFile(KS_PATH).getParentFile(); File[] certificates = dir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return (!name.startsWith(".")); } }); for (File f : certificates) { System.out.println(f); KeyStore ks; try { ks = openKeyStore(f, KS_PWD); } catch (Exception e) { e.printStackTrace(); throw e; } assertNotNull("KeyStore not loaded: " + f.getAbsolutePath(), ks); } }
public static KeyStore openKeyStore(File path, char[] password) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException, IOException { installBouncyCastleProviderIfNecessary(); String ext = IoUtil.getFilenameExtension(path.getName()).toLowerCase(); final String type = EXT_KEYSTORE_TABLE.get(ext); List<String> typeOrder = Arrays.asList(SUPPORTED_KEYSTORE_TYPES); Collections.sort(typeOrder, new Comparator<String>() { public int compare(String o1, String o2) { if (o1.equals(type)) return -1; else if (o2.equals(type)) return 1; else return 0; } }); KeyStore ks = null; IOException keystoreNotLoaded = null; for (String t : typeOrder) { String prov = KSTYPE_PROVIDER_TABLE.get(t); if (prov == null) ks = KeyStore.getInstance(t); else ks = KeyStore.getInstance(t, prov); FileInputStream inStream = null; try { inStream = new FileInputStream(path); ks.load(inStream, password); } catch (IOException e) { if (keystoreNotLoaded == null) keystoreNotLoaded = e; continue; } finally { try { if (inStream != null) inStream.close(); } catch (Throwable twb) { } } keystoreNotLoaded = null; break; } if (keystoreNotLoaded != null) throw keystoreNotLoaded; return ks; }
SecurityUtil { public static KeyStore openKeyStore(File path, char[] password) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException, IOException { installBouncyCastleProviderIfNecessary(); String ext = IoUtil.getFilenameExtension(path.getName()).toLowerCase(); final String type = EXT_KEYSTORE_TABLE.get(ext); List<String> typeOrder = Arrays.asList(SUPPORTED_KEYSTORE_TYPES); Collections.sort(typeOrder, new Comparator<String>() { public int compare(String o1, String o2) { if (o1.equals(type)) return -1; else if (o2.equals(type)) return 1; else return 0; } }); KeyStore ks = null; IOException keystoreNotLoaded = null; for (String t : typeOrder) { String prov = KSTYPE_PROVIDER_TABLE.get(t); if (prov == null) ks = KeyStore.getInstance(t); else ks = KeyStore.getInstance(t, prov); FileInputStream inStream = null; try { inStream = new FileInputStream(path); ks.load(inStream, password); } catch (IOException e) { if (keystoreNotLoaded == null) keystoreNotLoaded = e; continue; } finally { try { if (inStream != null) inStream.close(); } catch (Throwable twb) { } } keystoreNotLoaded = null; break; } if (keystoreNotLoaded != null) throw keystoreNotLoaded; return ks; } }
SecurityUtil { public static KeyStore openKeyStore(File path, char[] password) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException, IOException { installBouncyCastleProviderIfNecessary(); String ext = IoUtil.getFilenameExtension(path.getName()).toLowerCase(); final String type = EXT_KEYSTORE_TABLE.get(ext); List<String> typeOrder = Arrays.asList(SUPPORTED_KEYSTORE_TYPES); Collections.sort(typeOrder, new Comparator<String>() { public int compare(String o1, String o2) { if (o1.equals(type)) return -1; else if (o2.equals(type)) return 1; else return 0; } }); KeyStore ks = null; IOException keystoreNotLoaded = null; for (String t : typeOrder) { String prov = KSTYPE_PROVIDER_TABLE.get(t); if (prov == null) ks = KeyStore.getInstance(t); else ks = KeyStore.getInstance(t, prov); FileInputStream inStream = null; try { inStream = new FileInputStream(path); ks.load(inStream, password); } catch (IOException e) { if (keystoreNotLoaded == null) keystoreNotLoaded = e; continue; } finally { try { if (inStream != null) inStream.close(); } catch (Throwable twb) { } } keystoreNotLoaded = null; break; } if (keystoreNotLoaded != null) throw keystoreNotLoaded; return ks; }  }
SecurityUtil { public static KeyStore openKeyStore(File path, char[] password) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException, IOException { installBouncyCastleProviderIfNecessary(); String ext = IoUtil.getFilenameExtension(path.getName()).toLowerCase(); final String type = EXT_KEYSTORE_TABLE.get(ext); List<String> typeOrder = Arrays.asList(SUPPORTED_KEYSTORE_TYPES); Collections.sort(typeOrder, new Comparator<String>() { public int compare(String o1, String o2) { if (o1.equals(type)) return -1; else if (o2.equals(type)) return 1; else return 0; } }); KeyStore ks = null; IOException keystoreNotLoaded = null; for (String t : typeOrder) { String prov = KSTYPE_PROVIDER_TABLE.get(t); if (prov == null) ks = KeyStore.getInstance(t); else ks = KeyStore.getInstance(t, prov); FileInputStream inStream = null; try { inStream = new FileInputStream(path); ks.load(inStream, password); } catch (IOException e) { if (keystoreNotLoaded == null) keystoreNotLoaded = e; continue; } finally { try { if (inStream != null) inStream.close(); } catch (Throwable twb) { } } keystoreNotLoaded = null; break; } if (keystoreNotLoaded != null) throw keystoreNotLoaded; return ks; }  static KeyStore openKeyStore(File path, char[] password); static PrivateKey getPrivateKey(KeyStore ks, char[] password); static X509Certificate openCertificate(File path); static void installBouncyCastleProviderIfNecessary(); static X509Certificate getCertificateEntry(KeyStore ks); static byte[] computeFileHash(File file, String algorithm); }
SecurityUtil { public static KeyStore openKeyStore(File path, char[] password) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException, IOException { installBouncyCastleProviderIfNecessary(); String ext = IoUtil.getFilenameExtension(path.getName()).toLowerCase(); final String type = EXT_KEYSTORE_TABLE.get(ext); List<String> typeOrder = Arrays.asList(SUPPORTED_KEYSTORE_TYPES); Collections.sort(typeOrder, new Comparator<String>() { public int compare(String o1, String o2) { if (o1.equals(type)) return -1; else if (o2.equals(type)) return 1; else return 0; } }); KeyStore ks = null; IOException keystoreNotLoaded = null; for (String t : typeOrder) { String prov = KSTYPE_PROVIDER_TABLE.get(t); if (prov == null) ks = KeyStore.getInstance(t); else ks = KeyStore.getInstance(t, prov); FileInputStream inStream = null; try { inStream = new FileInputStream(path); ks.load(inStream, password); } catch (IOException e) { if (keystoreNotLoaded == null) keystoreNotLoaded = e; continue; } finally { try { if (inStream != null) inStream.close(); } catch (Throwable twb) { } } keystoreNotLoaded = null; break; } if (keystoreNotLoaded != null) throw keystoreNotLoaded; return ks; }  static KeyStore openKeyStore(File path, char[] password); static PrivateKey getPrivateKey(KeyStore ks, char[] password); static X509Certificate openCertificate(File path); static void installBouncyCastleProviderIfNecessary(); static X509Certificate getCertificateEntry(KeyStore ks); static byte[] computeFileHash(File file, String algorithm); static final String BC_PROVIDER; static final String DEFAULT_OFTP_HASH_ALGORITHM; }