Search in sources :

Example 76 with KeyManagementException

use of java.security.KeyManagementException in project vcell by virtualcell.

the class RemoteProxyVCellConnectionFactory method getVCellSoftwareVersion.

public static String getVCellSoftwareVersion(String apihost, Integer apiport) {
    boolean bIgnoreCertProblems = true;
    boolean bIgnoreHostMismatch = true;
    try {
        VCellApiClient tempApiClient = new VCellApiClient(apihost, apiport, bIgnoreCertProblems, bIgnoreHostMismatch);
        String serverSoftwareVersion = tempApiClient.getServerSoftwareVersion();
        return serverSoftwareVersion;
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        e.printStackTrace();
        throw new RuntimeException("VCellApiClient configuration exception: " + e.getMessage(), e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("VCellApiClient communication exception while retrieving server software version: " + e.getMessage(), e);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) VCellApiClient(org.vcell.api.client.VCellApiClient) KeyManagementException(java.security.KeyManagementException)

Example 77 with KeyManagementException

use of java.security.KeyManagementException in project web3sdk by FISCO-BCOS.

the class CertificateManager method buildKeyStore.

static File buildKeyStore(String url, char[] keyStorePassword) {
    KeyStore keyStore;
    try {
        keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, keyStorePassword);
        CertificateChainTrustManager certificateChainTrustManager = createCertificateChainTrustManager(keyStore);
        URI endpoint = new URI(url);
        SSLSocket sslSocket = createSslSocket(endpoint, certificateChainTrustManager);
        if (!isTrustedEndPoint(sslSocket)) {
            X509Certificate[] x509Certificates = certificateChainTrustManager.x509Certificates;
            if (x509Certificates == null) {
                throw new RuntimeException("Unable to obtain x509 certificate from server");
            }
            for (int i = 0; i < x509Certificates.length; i++) {
                keyStore.setCertificateEntry(endpoint.getHost() + i, x509Certificates[i]);
            }
        }
        SecureRandom random = new SecureRandom();
        File keyFile = File.createTempFile("web3j-", "" + random.nextLong());
        FileOutputStream fileOutputStream = new FileOutputStream(keyFile);
        keyStore.store(fileOutputStream, keyStorePassword);
        fileOutputStream.close();
        deleteFileOnShutdown(keyFile);
        return keyFile;
    } catch (KeyStoreException e) {
        throw new RuntimeException(e);
    } catch (CertificateException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) KeyStore(java.security.KeyStore) URI(java.net.URI) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 78 with KeyManagementException

use of java.security.KeyManagementException in project yamcs-studio by yamcs.

the class ResourceUtil method openRawURLStream.

/**
 * Open URL Stream from remote.
 *
 * @param url
 * @return
 * @throws IOException
 */
private static InputStream openRawURLStream(final URL url) throws IOException {
    if (url.getProtocol().equals("https")) {
        // $NON-NLS-1$
        // The code to support https protocol is provided by Eric Berryman (eric.berryman@gmail.com) from Frib
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        // Install the all-trusting trust manager
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("SSL");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        try {
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    }
    URLConnection connection = url.openConnection();
    connection.setReadTimeout(PreferencesHelper.getURLFileLoadingTimeout());
    return connection.getInputStream();
}
Also used : SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 79 with KeyManagementException

use of java.security.KeyManagementException in project ORCID-Source by ORCID.

the class DevJerseyClientConfig method createSslContext.

private SSLContext createSslContext() {
    try {
        // DANGER!!! Accepts all certs!
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext ssl = SSLContext.getInstance("TLS");
        ssl.init(null, trustAllCerts, new SecureRandom());
        return ssl;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 80 with KeyManagementException

use of java.security.KeyManagementException in project ORCID-Source by ORCID.

the class OrcidJerseyT2ClientConfig method createSslContext.

private SSLContext createSslContext() {
    try {
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(keyStore, keyStorePassword.toCharArray());
        KeyManager[] keyManagers = kmf.getKeyManagers();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        // Use the trustStore if present, otherwise default to keyStore.
        if (trustStore != null) {
            tmf.init(trustStore);
        } else {
            tmf.init(keyStore);
        }
        TrustManager[] trustManagers = tmf.getTrustManagers();
        SSLContext ssl = SSLContext.getInstance("TLS");
        ssl.init(keyManagers, trustManagers, new SecureRandom());
        return ssl;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (KeyStoreException e) {
        throw new RuntimeException(e);
    } catch (UnrecoverableKeyException e) {
        throw new RuntimeException(e);
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) UnrecoverableKeyException(java.security.UnrecoverableKeyException) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager)

Aggregations

KeyManagementException (java.security.KeyManagementException)157 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)111 SSLContext (javax.net.ssl.SSLContext)83 KeyStoreException (java.security.KeyStoreException)60 IOException (java.io.IOException)55 TrustManager (javax.net.ssl.TrustManager)45 CertificateException (java.security.cert.CertificateException)35 X509TrustManager (javax.net.ssl.X509TrustManager)28 SecureRandom (java.security.SecureRandom)27 X509Certificate (java.security.cert.X509Certificate)26 UnrecoverableKeyException (java.security.UnrecoverableKeyException)24 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)24 KeyStore (java.security.KeyStore)22 KeyManager (javax.net.ssl.KeyManager)19 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)16 HostnameVerifier (javax.net.ssl.HostnameVerifier)15 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)15 InputStream (java.io.InputStream)12 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)11 SSLSession (javax.net.ssl.SSLSession)10