Search in sources :

Example 1 with CertificateFactory

use of java.security.cert.CertificateFactory in project tomcat by apache.

the class AjpProcessor method populateSslRequestAttributes.

@Override
protected final void populateSslRequestAttributes() {
    if (!certificates.isNull()) {
        ByteChunk certData = certificates.getByteChunk();
        X509Certificate[] jsseCerts = null;
        ByteArrayInputStream bais = new ByteArrayInputStream(certData.getBytes(), certData.getStart(), certData.getLength());
        // Fill the  elements.
        try {
            CertificateFactory cf;
            String clientCertProvider = protocol.getClientCertProvider();
            if (clientCertProvider == null) {
                cf = CertificateFactory.getInstance("X.509");
            } else {
                cf = CertificateFactory.getInstance("X.509", clientCertProvider);
            }
            while (bais.available() > 0) {
                X509Certificate cert = (X509Certificate) cf.generateCertificate(bais);
                if (jsseCerts == null) {
                    jsseCerts = new X509Certificate[1];
                    jsseCerts[0] = cert;
                } else {
                    X509Certificate[] temp = new X509Certificate[jsseCerts.length + 1];
                    System.arraycopy(jsseCerts, 0, temp, 0, jsseCerts.length);
                    temp[jsseCerts.length] = cert;
                    jsseCerts = temp;
                }
            }
        } catch (java.security.cert.CertificateException e) {
            getLog().error(sm.getString("ajpprocessor.certs.fail"), e);
            return;
        } catch (NoSuchProviderException e) {
            getLog().error(sm.getString("ajpprocessor.certs.fail"), e);
            return;
        }
        request.setAttribute(SSLSupport.CERTIFICATE_KEY, jsseCerts);
    }
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSuchProviderException(java.security.NoSuchProviderException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 2 with CertificateFactory

use of java.security.cert.CertificateFactory in project tomcat by apache.

the class JSSESupport method getPeerCertificateChain.

@Override
public java.security.cert.X509Certificate[] getPeerCertificateChain() throws IOException {
    // Look up the current SSLSession
    if (session == null)
        return null;
    Certificate[] certs = null;
    try {
        certs = session.getPeerCertificates();
    } catch (Throwable t) {
        log.debug(sm.getString("jsseSupport.clientCertError"), t);
        return null;
    }
    if (certs == null)
        return null;
    java.security.cert.X509Certificate[] x509Certs = new java.security.cert.X509Certificate[certs.length];
    for (int i = 0; i < certs.length; i++) {
        if (certs[i] instanceof java.security.cert.X509Certificate) {
            // always currently true with the JSSE 1.1.x
            x509Certs[i] = (java.security.cert.X509Certificate) certs[i];
        } else {
            try {
                byte[] buffer = certs[i].getEncoded();
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
                x509Certs[i] = (java.security.cert.X509Certificate) cf.generateCertificate(stream);
            } catch (Exception ex) {
                log.info(sm.getString("jseeSupport.certTranslationError", certs[i]), ex);
                return null;
            }
        }
        if (log.isTraceEnabled())
            log.trace("Cert #" + i + " = " + x509Certs[i]);
    }
    if (x509Certs.length < 1)
        return null;
    return x509Certs;
}
Also used : CertificateFactory(java.security.cert.CertificateFactory) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) Certificate(java.security.cert.Certificate)

Example 3 with CertificateFactory

use of java.security.cert.CertificateFactory in project cw-omnibus by commonsguy.

the class SignatureFragment method show.

void show(byte[] raw) {
    CertificateFactory cf = null;
    try {
        cf = CertificateFactory.getInstance("X509");
    } catch (CertificateException e) {
        Log.e(getClass().getSimpleName(), "Exception getting CertificateFactory", e);
        return;
    }
    X509Certificate c = null;
    ByteArrayInputStream bin = new ByteArrayInputStream(raw);
    try {
        c = (X509Certificate) cf.generateCertificate(bin);
    } catch (CertificateException e) {
        Log.e(getClass().getSimpleName(), "Exception getting X509Certificate", e);
        return;
    }
    TextView tv = (TextView) getView().findViewById(R.id.subject);
    tv.setText(c.getSubjectDN().toString());
    tv = (TextView) getView().findViewById(R.id.issuer);
    tv.setText(c.getIssuerDN().toString());
    tv = (TextView) getView().findViewById(R.id.valid);
    tv.setText(fmt.format(c.getNotBefore()) + " to " + fmt.format(c.getNotAfter()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) TextView(android.widget.TextView) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 4 with CertificateFactory

use of java.security.cert.CertificateFactory in project UltimateAndroid by cymcsg.

the class HttpsUtils method getX509CertifaceteFromCrtFile.

/**
     * Get X509 Certificate
     *
     * @param certFilePath
     * @return Certificate
     */
public static Certificate getX509CertifaceteFromCrtFile(String certFilePath) {
    InputStream inStream = null;
    X509Certificate cert = null;
    try {
        inStream = new FileInputStream(certFilePath);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        cert = (X509Certificate) cf.generateCertificate(inStream);
        inStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
    } finally {
        return cert;
    }
}
Also used : CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 5 with CertificateFactory

use of java.security.cert.CertificateFactory in project vert.x by eclipse.

the class SSLHelper method getTrustMgrFactory.

private TrustManagerFactory getTrustMgrFactory(VertxInternal vertx) throws Exception {
    TrustManagerFactory fact;
    if (trustAll) {
        TrustManager[] mgrs = new TrustManager[] { createTrustAllTrustManager() };
        fact = new VertxTrustManagerFactory(mgrs);
    } else if (trustOptions != null) {
        fact = trustOptions.getTrustManagerFactory(vertx);
    } else {
        return null;
    }
    if (crlPaths != null && crlValues != null && (crlPaths.size() > 0 || crlValues.size() > 0)) {
        Stream<Buffer> tmp = crlPaths.stream().map(path -> vertx.resolveFile(path).getAbsolutePath()).map(vertx.fileSystem()::readFileBlocking);
        tmp = Stream.concat(tmp, crlValues.stream());
        CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
        ArrayList<CRL> crls = new ArrayList<>();
        for (Buffer crlValue : tmp.collect(Collectors.toList())) {
            crls.addAll(certificatefactory.generateCRLs(new ByteArrayInputStream(crlValue.getBytes())));
        }
        TrustManager[] mgrs = createUntrustRevokedCertTrustManager(fact.getTrustManagers(), crls);
        fact = new VertxTrustManagerFactory(mgrs);
    }
    return fact;
}
Also used : VertxException(io.vertx.core.VertxException) X509Certificate(java.security.cert.X509Certificate) java.util(java.util) CertificateFactory(java.security.cert.CertificateFactory) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) SimpleTrustManagerFactory(io.netty.handler.ssl.util.SimpleTrustManagerFactory) LoggerFactory(io.vertx.core.logging.LoggerFactory) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpVersion(io.vertx.core.http.HttpVersion) KeyCertOptions(io.vertx.core.net.KeyCertOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Logger(io.vertx.core.logging.Logger) CRL(java.security.cert.CRL) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) TCPSSLOptions(io.vertx.core.net.TCPSSLOptions) SSLEngineOptions(io.vertx.core.net.SSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) KeyStore(java.security.KeyStore) CertificateException(java.security.cert.CertificateException) io.netty.handler.ssl(io.netty.handler.ssl) Collectors(java.util.stream.Collectors) NetClientOptions(io.vertx.core.net.NetClientOptions) TrustOptions(io.vertx.core.net.TrustOptions) NetServerOptions(io.vertx.core.net.NetServerOptions) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) ClientAuth(io.vertx.core.http.ClientAuth) HttpServerOptions(io.vertx.core.http.HttpServerOptions) javax.net.ssl(javax.net.ssl) Buffer(io.vertx.core.buffer.Buffer) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleTrustManagerFactory(io.netty.handler.ssl.util.SimpleTrustManagerFactory) CRL(java.security.cert.CRL) CertificateFactory(java.security.cert.CertificateFactory)

Aggregations

CertificateFactory (java.security.cert.CertificateFactory)550 X509Certificate (java.security.cert.X509Certificate)409 ByteArrayInputStream (java.io.ByteArrayInputStream)372 Certificate (java.security.cert.Certificate)272 CertificateException (java.security.cert.CertificateException)120 KeyFactory (java.security.KeyFactory)103 PrivateKey (java.security.PrivateKey)93 InputStream (java.io.InputStream)92 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)86 IOException (java.io.IOException)80 KeyStore (java.security.KeyStore)77 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)67 Entry (java.security.KeyStore.Entry)59 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)59 KeyStoreException (java.security.KeyStoreException)49 ArrayList (java.util.ArrayList)49 FileInputStream (java.io.FileInputStream)47 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 File (java.io.File)23 HashSet (java.util.HashSet)21