Search in sources :

Example 1 with StorageResolverException

use of org.apache.xml.security.keys.storage.StorageResolverException in project santuario-java by apache.

the class CertsInFilesystemDirectoryResolver method readCertsFromHarddrive.

/**
 * Method readCertsFromHarddrive
 *
 * @throws StorageResolverException
 */
private void readCertsFromHarddrive() throws StorageResolverException {
    File certDir = new File(this.merlinsCertificatesDir);
    List<String> al = new ArrayList<>();
    String[] names = certDir.list();
    if (names != null) {
        for (int i = 0; i < names.length; i++) {
            String currentFileName = names[i];
            if (currentFileName.endsWith(".crt")) {
                al.add(names[i]);
            }
        }
    }
    CertificateFactory cf = null;
    try {
        cf = CertificateFactory.getInstance("X.509");
    } catch (CertificateException ex) {
        throw new StorageResolverException(ex);
    }
    for (int i = 0; i < al.size(); i++) {
        String filename = certDir.getAbsolutePath() + File.separator + al.get(i);
        boolean added = false;
        String dn = null;
        try (InputStream inputStream = Files.newInputStream(Paths.get(filename))) {
            X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
            // add to ArrayList
            cert.checkValidity();
            this.certs.add(cert);
            dn = cert.getSubjectX500Principal().getName();
            added = true;
        } catch (FileNotFoundException ex) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not add certificate from file " + filename, ex);
            }
        } catch (CertificateNotYetValidException ex) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not add certificate from file " + filename, ex);
            }
        } catch (CertificateExpiredException ex) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not add certificate from file " + filename, ex);
            }
        } catch (CertificateException ex) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not add certificate from file " + filename, ex);
            }
        } catch (IOException ex) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not add certificate from file " + filename, ex);
            }
        }
        if (added) {
            LOG.debug("Added certificate: {}", dn);
        }
    }
}
Also used : StorageResolverException(org.apache.xml.security.keys.storage.StorageResolverException) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateExpiredException(java.security.cert.CertificateExpiredException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) File(java.io.File)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CertificateException (java.security.cert.CertificateException)1 CertificateExpiredException (java.security.cert.CertificateExpiredException)1 CertificateFactory (java.security.cert.CertificateFactory)1 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 StorageResolverException (org.apache.xml.security.keys.storage.StorageResolverException)1