Search in sources :

Example 21 with CertStoreException

use of java.security.cert.CertStoreException in project robovm by robovm.

the class PKIXCRLUtil method findCRLs.

/**
     * Return a Collection of all CRLs found in the X509Store's that are
     * matching the crlSelect criteriums.
     *
     * @param crlSelect a {@link X509CRLStoreSelector} object that will be used
     *            to select the CRLs
     * @param crlStores a List containing only
     *            {@link org.bouncycastle.x509.X509Store  X509Store} objects.
     *            These are used to search for CRLs
     *
     * @return a Collection of all found {@link java.security.cert.X509CRL X509CRL} objects. May be
     *         empty but never <code>null</code>.
     */
private final Collection findCRLs(X509CRLStoreSelector crlSelect, List crlStores) throws AnnotatedException {
    Set crls = new HashSet();
    Iterator iter = crlStores.iterator();
    AnnotatedException lastException = null;
    boolean foundValidStore = false;
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof X509Store) {
            X509Store store = (X509Store) obj;
            try {
                crls.addAll(store.getMatches(crlSelect));
                foundValidStore = true;
            } catch (StoreException e) {
                lastException = new AnnotatedException("Exception searching in X.509 CRL store.", e);
            }
        } else {
            CertStore store = (CertStore) obj;
            try {
                crls.addAll(store.getCRLs(crlSelect));
                foundValidStore = true;
            } catch (CertStoreException e) {
                lastException = new AnnotatedException("Exception searching in X.509 CRL store.", e);
            }
        }
    }
    if (!foundValidStore && lastException != null) {
        throw lastException;
    }
    return crls;
}
Also used : X509Store(org.bouncycastle.x509.X509Store) Set(java.util.Set) HashSet(java.util.HashSet) CertStoreException(java.security.cert.CertStoreException) Iterator(java.util.Iterator) CertStore(java.security.cert.CertStore) HashSet(java.util.HashSet) StoreException(org.bouncycastle.util.StoreException) CertStoreException(java.security.cert.CertStoreException)

Example 22 with CertStoreException

use of java.security.cert.CertStoreException in project jdk8u_jdk by JetBrains.

the class SSLServerCertStore method engineGetCertificates.

public Collection<X509Certificate> engineGetCertificates(CertSelector selector) throws CertStoreException {
    try {
        URLConnection urlConn = uri.toURL().openConnection();
        if (urlConn instanceof HttpsURLConnection) {
            if (socketFactory == null) {
                throw new CertStoreException("No initialized SSLSocketFactory");
            }
            HttpsURLConnection https = (HttpsURLConnection) urlConn;
            https.setSSLSocketFactory(socketFactory);
            https.setHostnameVerifier(hostnameVerifier);
            synchronized (trustManager) {
                try {
                    https.connect();
                    return getMatchingCerts(trustManager.serverChain, selector);
                } catch (IOException ioe) {
                    // retrieved, don't mind the connection state.
                    if (trustManager.exchangedServerCerts) {
                        return getMatchingCerts(trustManager.serverChain, selector);
                    }
                    // otherwise, rethrow the exception
                    throw ioe;
                } finally {
                    trustManager.cleanup();
                }
            }
        }
    } catch (IOException ioe) {
        throw new CertStoreException(ioe);
    }
    return Collections.<X509Certificate>emptySet();
}
Also used : CertStoreException(java.security.cert.CertStoreException) IOException(java.io.IOException) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) X509Certificate(java.security.cert.X509Certificate)

Example 23 with CertStoreException

use of java.security.cert.CertStoreException in project sic by belluccifranco.

the class AfipWebServiceSOAPClient method crearCMS.

public byte[] crearCMS(byte[] p12file, String p12pass, String signer, String service, long ticketTime) {
    PrivateKey pKey = null;
    X509Certificate pCertificate = null;
    byte[] asn1_cms = null;
    CertStore cstore = null;
    try {
        KeyStore ks = KeyStore.getInstance("pkcs12");
        InputStream is;
        is = Utilidades.convertirByteArrayToInputStream(p12file);
        ks.load(is, p12pass.toCharArray());
        is.close();
        pKey = (PrivateKey) ks.getKey(signer, p12pass.toCharArray());
        pCertificate = (X509Certificate) ks.getCertificate(signer);
        ArrayList<X509Certificate> certList = new ArrayList<>();
        certList.add(pCertificate);
        if (Security.getProvider("BC") == null) {
            Security.addProvider(new BouncyCastleProvider());
        }
        cstore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), "BC");
    } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | InvalidAlgorithmParameterException | NoSuchProviderException ex) {
        LOGGER.error(ex.getMessage());
        throw new BusinessServiceException(ResourceBundle.getBundle("Mensajes").getString("mensaje_certificado_error"));
    }
    String loginTicketRequest_xml = this.crearTicketRequerimientoAcceso(service, ticketTime);
    try {
        CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
        generator.addSigner(pKey, pCertificate, CMSSignedDataGenerator.DIGEST_SHA1);
        generator.addCertificatesAndCRLs(cstore);
        CMSProcessable data = new CMSProcessableByteArray(loginTicketRequest_xml.getBytes());
        CMSSignedData signed = generator.generate(data, true, "BC");
        asn1_cms = signed.getEncoded();
    } catch (IllegalArgumentException | CertStoreException | CMSException | NoSuchAlgorithmException | NoSuchProviderException | IOException ex) {
        LOGGER.error(ex.getMessage());
        throw new BusinessServiceException(ResourceBundle.getBundle("Mensajes").getString("mensaje_firmando_certificado_error"));
    }
    return asn1_cms;
}
Also used : CMSSignedDataGenerator(org.bouncycastle.cms.CMSSignedDataGenerator) PrivateKey(java.security.PrivateKey) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CollectionCertStoreParameters(java.security.cert.CollectionCertStoreParameters) BusinessServiceException(sic.service.BusinessServiceException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) CMSProcessableByteArray(org.bouncycastle.cms.CMSProcessableByteArray) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InputStream(java.io.InputStream) CertStoreException(java.security.cert.CertStoreException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) CMSSignedData(org.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) CMSProcessable(org.bouncycastle.cms.CMSProcessable) NoSuchProviderException(java.security.NoSuchProviderException) CertStore(java.security.cert.CertStore) CMSException(org.bouncycastle.cms.CMSException)

Aggregations

CertStoreException (java.security.cert.CertStoreException)23 CertStore (java.security.cert.CertStore)10 X509Certificate (java.security.cert.X509Certificate)5 IOException (java.io.IOException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 StoreException (org.bouncycastle.util.StoreException)4 X509Store (org.bouncycastle.x509.X509Store)4 InputStream (java.io.InputStream)3 URLConnection (java.net.URLConnection)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 CertificateException (java.security.cert.CertificateException)3 HttpURLConnection (java.net.HttpURLConnection)2 KeyStoreException (java.security.KeyStoreException)2 UnrecoverableKeyException (java.security.UnrecoverableKeyException)2 Collection (java.util.Collection)2 URI (java.net.URI)1 CodeSigner (java.security.CodeSigner)1