Search in sources :

Example 46 with CertificateException

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

the class OpenSSLSocketImpl method verifyCertificateChain.

// used by NativeCrypto.SSLHandshakeCallbacks
@SuppressWarnings("unused")
@Override
public void verifyCertificateChain(byte[][] bytes, String authMethod) throws CertificateException {
    try {
        if (bytes == null || bytes.length == 0) {
            throw new SSLException("Peer sent no certificate");
        }
        X509Certificate[] peerCertificateChain = new X509Certificate[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            peerCertificateChain[i] = OpenSSLX509Certificate.fromX509Der(bytes[i]);
        }
        boolean client = sslParameters.getUseClientMode();
        if (client) {
            X509TrustManager x509tm = sslParameters.getTrustManager();
            if (x509tm instanceof TrustManagerImpl) {
                TrustManagerImpl tm = (TrustManagerImpl) x509tm;
                tm.checkServerTrusted(peerCertificateChain, authMethod, wrappedHost);
            } else {
                x509tm.checkServerTrusted(peerCertificateChain, authMethod);
            }
        } else {
            String authType = peerCertificateChain[0].getPublicKey().getAlgorithm();
            sslParameters.getTrustManager().checkClientTrusted(peerCertificateChain, authType);
        }
    } catch (CertificateException e) {
        throw e;
    } catch (Exception e) {
        throw new CertificateException(e);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) X509Certificate(java.security.cert.X509Certificate) SocketException(java.net.SocketException) ErrnoException(libcore.io.ErrnoException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) InvalidKeyException(java.security.InvalidKeyException) CertificateEncodingException(java.security.cert.CertificateEncodingException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 47 with CertificateException

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

the class X509V2AttributeCertificate method verify.

public final void verify(PublicKey key, String provider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
    Signature signature = null;
    if (!cert.getSignatureAlgorithm().equals(cert.getAcinfo().getSignature())) {
        throw new CertificateException("Signature algorithm in certificate info not same as outer certificate");
    }
    signature = Signature.getInstance(cert.getSignatureAlgorithm().getObjectId().getId(), provider);
    signature.initVerify(key);
    try {
        signature.update(cert.getAcinfo().getEncoded());
    } catch (IOException e) {
        throw new SignatureException("Exception encoding certificate info object");
    }
    if (!signature.verify(this.getSignature())) {
        throw new InvalidKeyException("Public key presented not for certificate signature");
    }
}
Also used : Signature(java.security.Signature) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 48 with CertificateException

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

the class X509CertificateObject method verify.

public final void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
    Signature signature;
    String sigName = X509SignatureUtil.getSignatureName(c.getSignatureAlgorithm());
    try {
        signature = Signature.getInstance(sigName, BouncyCastleProvider.PROVIDER_NAME);
    } catch (Exception e) {
        signature = Signature.getInstance(sigName);
    }
    checkSignature(key, signature);
}
Also used : Signature(java.security.Signature) DERBitString(org.bouncycastle.asn1.DERBitString) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERIA5String(org.bouncycastle.asn1.DERIA5String) ASN1String(org.bouncycastle.asn1.ASN1String) CertificateExpiredException(java.security.cert.CertificateExpiredException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CertificateEncodingException(java.security.cert.CertificateEncodingException) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateParsingException(java.security.cert.CertificateParsingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 49 with CertificateException

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

the class X509TrustManagerTest method test_checkClientTrusted_02.

/**
     * javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType)
     */
public void test_checkClientTrusted_02() throws Exception {
    X509TrustManagerImpl xtm = new X509TrustManagerImpl();
    X509Certificate[] xcert = setInvalid();
    try {
        xtm.checkClientTrusted(xcert, "SSL");
        fail("CertificateException wasn't thrown");
    } catch (CertificateException expected) {
    }
}
Also used : X509TrustManagerImpl(org.apache.harmony.xnet.tests.support.X509TrustManagerImpl) CertificateException(java.security.cert.CertificateException) X509Certificate(java.security.cert.X509Certificate)

Example 50 with CertificateException

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

the class myTrustManagerFactory method test_getTrustManagers.

/**
     * Test for <code>geTrustManagers()</code>
     * @throws KeyStoreException
     * @throws IOException
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     */
public void test_getTrustManagers() {
    try {
        TrustManagerFactory trustMF = TrustManagerFactory.getInstance(getDefaultAlgorithm());
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        trustMF.init(ks);
        TrustManager[] tm = trustMF.getTrustManagers();
        assertNotNull("Result has not be null", tm);
        assertTrue("Length of result TrustManager array should not be 0", (tm.length > 0));
    } catch (Exception ex) {
        fail("Unexpected exception " + ex.toString());
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) TrustManager(javax.net.ssl.TrustManager)

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