Search in sources :

Example 21 with X509Certificate

use of java.security.cert.X509Certificate in project android_frameworks_base by ParanoidAndroid.

the class SslCertificate method restoreState.

/**
     * Restores the certificate stored in the bundle
     * @param bundle The bundle with the certificate state stored in it
     * @return The SSL certificate stored in the bundle or null if fails
     */
public static SslCertificate restoreState(Bundle bundle) {
    if (bundle == null) {
        return null;
    }
    X509Certificate x509Certificate;
    byte[] bytes = bundle.getByteArray(X509_CERTIFICATE);
    if (bytes == null) {
        x509Certificate = null;
    } else {
        try {
            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
            x509Certificate = (X509Certificate) cert;
        } catch (CertificateException e) {
            x509Certificate = null;
        }
    }
    return new SslCertificate(bundle.getString(ISSUED_TO), bundle.getString(ISSUED_BY), parseDate(bundle.getString(VALID_NOT_BEFORE)), parseDate(bundle.getString(VALID_NOT_AFTER)), x509Certificate);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 22 with X509Certificate

use of java.security.cert.X509Certificate in project android_frameworks_base by ParanoidAndroid.

the class BrowserFrame method setCertificate.

/**
     * Called by JNI when we recieve a certificate for the page's main resource.
     * Used by the Chromium HTTP stack only.
     */
private void setCertificate(byte[] cert_der) {
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(cert_der));
        mCallbackProxy.onReceivedCertificate(new SslCertificate(cert));
    } catch (Exception e) {
        // Can't get the certificate, not much to do.
        Log.e(LOGTAG, "Can't get the certificate from WebKit, canceling");
        return;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SslCertificate(android.net.http.SslCertificate) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) ParseException(android.net.ParseException) IOException(java.io.IOException) NotFoundException(android.content.res.Resources.NotFoundException)

Example 23 with X509Certificate

use of java.security.cert.X509Certificate in project android_frameworks_base by ParanoidAndroid.

the class BrowserFrame method reportSslCertError.

/**
     * Called by JNI when the Chromium HTTP stack gets an invalid certificate chain.
     *
     * We delegate the request to CallbackProxy, and route its response to
     * {@link #nativeSslCertErrorProceed(int)} or
     * {@link #nativeSslCertErrorCancel(int, int)}.
     */
private void reportSslCertError(final int handle, final int certError, byte[] certDER, String url) {
    final SslError sslError;
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certDER));
        SslCertificate sslCert = new SslCertificate(cert);
        sslError = SslError.SslErrorFromChromiumErrorCode(certError, sslCert, url);
    } catch (Exception e) {
        // Can't get the certificate, not much to do.
        Log.e(LOGTAG, "Can't get the certificate from WebKit, canceling");
        nativeSslCertErrorCancel(handle, certError);
        return;
    }
    if (SslCertLookupTable.getInstance().isAllowed(sslError)) {
        nativeSslCertErrorProceed(handle);
        mCallbackProxy.onProceededAfterSslError(sslError);
        return;
    }
    SslErrorHandler handler = new SslErrorHandler() {

        @Override
        public void proceed() {
            SslCertLookupTable.getInstance().setIsAllowed(sslError);
            post(new Runnable() {

                public void run() {
                    nativeSslCertErrorProceed(handle);
                }
            });
        }

        @Override
        public void cancel() {
            post(new Runnable() {

                public void run() {
                    nativeSslCertErrorCancel(handle, certError);
                }
            });
        }
    };
    mCallbackProxy.onReceivedSslError(handler, sslError);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SslCertificate(android.net.http.SslCertificate) SslError(android.net.http.SslError) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) ParseException(android.net.ParseException) IOException(java.io.IOException) NotFoundException(android.content.res.Resources.NotFoundException)

Example 24 with X509Certificate

use of java.security.cert.X509Certificate in project android_frameworks_base by ParanoidAndroid.

the class KeyChain method getCertificateChain.

/**
     * Returns the {@code X509Certificate} chain for the requested
     * alias, or null if no there is no result.
     *
     * @param alias The alias of the desired certificate chain, typically
     * returned via {@link KeyChainAliasCallback#alias}.
     * @throws KeyChainException if the alias was valid but there was some problem accessing it.
     */
public static X509Certificate[] getCertificateChain(Context context, String alias) throws KeyChainException, InterruptedException {
    if (alias == null) {
        throw new NullPointerException("alias == null");
    }
    KeyChainConnection keyChainConnection = bind(context);
    try {
        IKeyChainService keyChainService = keyChainConnection.getService();
        final byte[] certificateBytes = keyChainService.getCertificate(alias);
        if (certificateBytes == null) {
            return null;
        }
        TrustedCertificateStore store = new TrustedCertificateStore();
        List<X509Certificate> chain = store.getCertificateChain(toCertificate(certificateBytes));
        return chain.toArray(new X509Certificate[chain.size()]);
    } catch (RemoteException e) {
        throw new KeyChainException(e);
    } catch (RuntimeException e) {
        // only certain RuntimeExceptions can be propagated across the IKeyChainService call
        throw new KeyChainException(e);
    } finally {
        keyChainConnection.close();
    }
}
Also used : TrustedCertificateStore(org.apache.harmony.xnet.provider.jsse.TrustedCertificateStore) RemoteException(android.os.RemoteException) X509Certificate(java.security.cert.X509Certificate)

Example 25 with X509Certificate

use of java.security.cert.X509Certificate in project android_frameworks_base by ParanoidAndroid.

the class KeyChain method toCertificate.

private static X509Certificate toCertificate(byte[] bytes) {
    if (bytes == null) {
        throw new IllegalArgumentException("bytes == null");
    }
    try {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
        return (X509Certificate) cert;
    } catch (CertificateException e) {
        throw new AssertionError(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

X509Certificate (java.security.cert.X509Certificate)1706 IOException (java.io.IOException)336 CertificateException (java.security.cert.CertificateException)272 ByteArrayInputStream (java.io.ByteArrayInputStream)260 CertificateFactory (java.security.cert.CertificateFactory)251 ArrayList (java.util.ArrayList)232 Certificate (java.security.cert.Certificate)227 KeyStore (java.security.KeyStore)177 PrivateKey (java.security.PrivateKey)150 InputStream (java.io.InputStream)134 File (java.io.File)112 KeyStoreException (java.security.KeyStoreException)112 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)111 GeneralSecurityException (java.security.GeneralSecurityException)100 Test (org.junit.Test)90 List (java.util.List)89 PublicKey (java.security.PublicKey)88 X509TrustManager (javax.net.ssl.X509TrustManager)80 X500Principal (javax.security.auth.x500.X500Principal)76 HashSet (java.util.HashSet)64