Search in sources :

Example 6 with KeyManagementException

use of java.security.KeyManagementException in project OpenAttestation by OpenAttestation.

the class SslUtil method createX509TrustManagerWithCertificates.

public static X509TrustManager createX509TrustManagerWithCertificates(X509Certificate[] certificates) throws KeyManagementException {
    try {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(createTrustedSslKeystore(certificates));
        TrustManager[] tms = tmf.getTrustManagers();
        for (TrustManager tm : tms) {
            if (tm instanceof X509TrustManager) {
                return (X509TrustManager) tm;
            }
        }
    } catch (NoSuchAlgorithmException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (IOException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (CertificateException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (UnrecoverableEntryException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (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 7 with KeyManagementException

use of java.security.KeyManagementException 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 8 with KeyManagementException

use of java.security.KeyManagementException 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)

Example 9 with KeyManagementException

use of java.security.KeyManagementException in project okhttp-OkGo by jeasonlzy.

the class HttpsUtils method getSslSocketFactory.

public static SSLParams getSslSocketFactory(X509TrustManager trustManager, InputStream bksFile, String password, InputStream[] certificates) {
    SSLParams sslParams = new SSLParams();
    try {
        KeyManager[] keyManagers = prepareKeyManager(bksFile, password);
        TrustManager[] trustManagers = prepareTrustManager(certificates);
        X509TrustManager manager;
        if (trustManager != null) {
            //优先使用用户自定义的TrustManager
            manager = trustManager;
        } else if (trustManagers != null) {
            //然后使用默认的TrustManager
            manager = chooseTrustManager(trustManagers);
        } else {
            //否则使用不安全的TrustManager
            manager = UnSafeTrustManager;
        }
        // 创建TLS类型的SSLContext对象, that uses our TrustManager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        // 用上面得到的trustManagers初始化SSLContext,这样sslContext就会信任keyStore中的证书
        // 第一个参数是授权的密钥管理器,用来授权验证,比如授权自签名的证书验证。第二个是被授权的证书管理器,用来验证服务器端的证书
        sslContext.init(keyManagers, new TrustManager[] { manager }, null);
        // 通过sslContext获取SSLSocketFactory对象
        sslParams.sSLSocketFactory = sslContext.getSocketFactory();
        sslParams.trustManager = manager;
        return sslParams;
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    } catch (KeyManagementException e) {
        throw new AssertionError(e);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManager(javax.net.ssl.KeyManager) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 10 with KeyManagementException

use of java.security.KeyManagementException in project Smack by igniterealtime.

the class SmackIntegrationTestFramework method prepareEnvironment.

protected SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException, IOException, XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
    XMPPTCPConnection conOne = null;
    XMPPTCPConnection conTwo = null;
    XMPPTCPConnection conThree = null;
    try {
        conOne = getConnectedConnectionFor(AccountNum.One);
        conTwo = getConnectedConnectionFor(AccountNum.Two);
        conThree = getConnectedConnectionFor(AccountNum.Three);
    } catch (Exception e) {
        // TODO Reverse the order, i.e. conThree should be disconnected first.
        if (conOne != null) {
            conOne.disconnect();
        }
        if (conTwo != null) {
            conTwo.disconnect();
        }
        if (conThree != null) {
            conThree.disconnect();
        }
        throw e;
    }
    return new SmackIntegrationTestEnvironment(conOne, conTwo, conThree, testRunResult.testRunId, config);
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SmackException(org.jivesoftware.smack.SmackException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

KeyManagementException (java.security.KeyManagementException)132 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)86 SSLContext (javax.net.ssl.SSLContext)65 KeyStoreException (java.security.KeyStoreException)43 TrustManager (javax.net.ssl.TrustManager)39 IOException (java.io.IOException)38 CertificateException (java.security.cert.CertificateException)23 X509TrustManager (javax.net.ssl.X509TrustManager)22 SecureRandom (java.security.SecureRandom)21 X509Certificate (java.security.cert.X509Certificate)19 UnrecoverableKeyException (java.security.UnrecoverableKeyException)18 KeyManager (javax.net.ssl.KeyManager)18 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)16 KeyStore (java.security.KeyStore)13 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)13 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)10 HostnameVerifier (javax.net.ssl.HostnameVerifier)9 NoSuchProviderException (java.security.NoSuchProviderException)7 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)7 SSLSocket (javax.net.ssl.SSLSocket)7