Search in sources :

Example 21 with CertificateException

use of java.security.cert.CertificateException in project OpenAttestation by OpenAttestation.

the class TrustFirstCertificateTlsPolicy method addServerCertificatesToRepository.

private void addServerCertificatesToRepository(X509Certificate[] xcs) {
    for (X509Certificate cert : xcs) {
        System.out.println("server certificate: " + cert.getSubjectX500Principal().getName());
    }
    for (int i = 0; i < xcs.length; i++) {
        try {
            // CertificateExpiredException, CertificateNotYetValidEception
            xcs[i].checkValidity();
            log.info("Saving certificate {}", xcs[i].getSubjectX500Principal().getName());
            // KeyManagementException
            repository.addCertificate(xcs[i]);
        } catch (Exception e) {
            log.trace("TrustFirstCertificateTlsPolicy addServerCertificateToRepository cert was not valid. trying to save next cert");
        // don't throw an exception because we may be able to save other certificates? throw new CertificateException("Unable to save server certificate", e);
        }
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) CertificateException(java.security.cert.CertificateException)

Example 22 with CertificateException

use of java.security.cert.CertificateException in project OpenAttestation by OpenAttestation.

the class X509CertificateDerProvider method readFrom.

@Override
public X509Certificate readFrom(Class<X509Certificate> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    try {
        // instead of using X509Util.decodeDerCertificate(byte[]) here we inline it because we have an inputstream instead
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(entityStream);
        return cert;
    } catch (CertificateException e) {
        throw new IOException(e);
    }
}
Also used : CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 23 with CertificateException

use of java.security.cert.CertificateException in project OpenAttestation by OpenAttestation.

the class X509CertificatePemProvider method readFrom.

@Override
public X509Certificate readFrom(Class<X509Certificate> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    try {
        String pem = IOUtils.toString(entityStream);
        X509Certificate cert = X509Util.decodePemCertificate(pem);
        return cert;
    } catch (CertificateException e) {
        throw new IOException(e);
    }
}
Also used : CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 24 with CertificateException

use of java.security.cert.CertificateException in project OpenAttestation by OpenAttestation.

the class X509Util method createX509TrustManagerWithKeystore.

/**
     * @deprecated use TlsPolicy instead
     * @param keystore
     * @return
     * @throws KeyManagementException 
     */
public static X509TrustManager createX509TrustManagerWithKeystore(SimpleKeystore keystore) throws KeyManagementException {
    try {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(KeyStoreUtil.createTrustedSslKeystore(keystore));
        TrustManager[] tms = tmf.getTrustManagers();
        for (TrustManager tm : tms) {
            if (tm instanceof X509TrustManager) {
                return (X509TrustManager) tm;
            }
        }
    } catch (NoSuchAlgorithmException | IOException | CertificateException | UnrecoverableEntryException | KeyStoreException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    }
    throw new IllegalArgumentException("TrustManagerFactory did not return an X509TrustManager instance");
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) UnrecoverableEntryException(java.security.UnrecoverableEntryException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 25 with CertificateException

use of java.security.cert.CertificateException in project OpenAttestation by OpenAttestation.

the class X509Util method createX509TrustManagerWithCertificates.

/**
     * 
     * @deprecated use TlsPolicy instead
     * @param certificates
     * @return
     * @throws KeyManagementException 
     */
public static X509TrustManager createX509TrustManagerWithCertificates(X509Certificate[] certificates) throws KeyManagementException {
    try {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(KeyStoreUtil.createTrustedSslKeystore(certificates));
        TrustManager[] tms = tmf.getTrustManagers();
        for (TrustManager tm : tms) {
            if (tm instanceof X509TrustManager) {
                return (X509TrustManager) tm;
            }
        }
    } catch (NoSuchAlgorithmException | IOException | CertificateException | UnrecoverableEntryException | KeyStoreException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    }
    throw new IllegalArgumentException("TrustManagerFactory did not return an X509TrustManager instance");
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) UnrecoverableEntryException(java.security.UnrecoverableEntryException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Aggregations

CertificateException (java.security.cert.CertificateException)456 IOException (java.io.IOException)221 X509Certificate (java.security.cert.X509Certificate)215 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)141 KeyStoreException (java.security.KeyStoreException)123 CertificateFactory (java.security.cert.CertificateFactory)103 ByteArrayInputStream (java.io.ByteArrayInputStream)97 Certificate (java.security.cert.Certificate)75 KeyStore (java.security.KeyStore)58 InputStream (java.io.InputStream)55 UnrecoverableKeyException (java.security.UnrecoverableKeyException)53 ArrayList (java.util.ArrayList)49 InvalidKeyException (java.security.InvalidKeyException)44 X509TrustManager (javax.net.ssl.X509TrustManager)41 SSLContext (javax.net.ssl.SSLContext)36 FileInputStream (java.io.FileInputStream)34 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)34 RemoteException (android.os.RemoteException)33 FileNotFoundException (java.io.FileNotFoundException)30 KeyManagementException (java.security.KeyManagementException)30