Search in sources :

Example 61 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project XobotOS by xamarin.

the class DefaultSSLContextImpl method getTrustManagers.

// TODO javax.net.ssl.trustStoreProvider system property
TrustManager[] getTrustManagers() throws GeneralSecurityException, IOException {
    if (TRUST_MANAGERS != null) {
        return TRUST_MANAGERS;
    }
    // find TrustStore, TrustManagers
    String keystore = System.getProperty("javax.net.ssl.trustStore");
    if (keystore == null) {
        return null;
    }
    String keystorepwd = System.getProperty("javax.net.ssl.trustStorePassword");
    char[] pwd = (keystorepwd == null) ? null : keystorepwd.toCharArray();
    // TODO Defaults: jssecacerts; cacerts
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(keystore));
        ks.load(is, pwd);
    } finally {
        if (is != null) {
            is.close();
        }
    }
    String tmfAlg = Security.getProperty("ssl.TrustManagerFactory.algorithm");
    if (tmfAlg == null) {
        tmfAlg = "PKIX";
    }
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlg);
    tmf.init(ks);
    TRUST_MANAGERS = tmf.getTrustManagers();
    return TRUST_MANAGERS;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream)

Example 62 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project camel by apache.

the class HttpsServerTestSupport method getSSLContext.

@Override
protected SSLContext getSSLContext() throws Exception {
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, PASSWORD.toCharArray());
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    SSLContext sslcontext = SSLContext.getInstance(SECURE_SOCKET_PROTOCOL);
    sslcontext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
    return sslcontext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 63 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project camel by apache.

the class AbstractJsseParametersTest method createPropertiesPlaceholderAwareContext.

protected CamelContext createPropertiesPlaceholderAwareContext() throws Exception {
    Properties supplementalProperties = new Properties();
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    SecureRandom sr = null;
    try {
        sr = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
    // Ignore
    }
    SSLContext sslc = SSLContext.getInstance("TLS");
    sslc.init(null, null, null);
    SSLSocket socket = (SSLSocket) sslc.getSocketFactory().createSocket();
    supplementalProperties.setProperty("keyStoreParameters.type", KeyStore.getDefaultType());
    supplementalProperties.setProperty("keyStoreParameters.provider", ks.getProvider().getName());
    supplementalProperties.setProperty("keyManagersParameters.algorithm", KeyManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("keyManagersParameters.provider", kmf.getProvider().getName());
    supplementalProperties.setProperty("trustManagersParameters.algorithm", TrustManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("trustManagersParameters.provider", tmf.getProvider().getName());
    if (sr != null) {
        supplementalProperties.setProperty("secureRandomParameters.algorithm", "SHA1PRNG");
        supplementalProperties.setProperty("secureRandomParameters.provider", sr.getProvider().getName());
    }
    supplementalProperties.setProperty("sslContextParameters.provider", sslc.getProvider().getName());
    supplementalProperties.setProperty("cipherSuite.0", socket.getSupportedCipherSuites()[0]);
    // Have to skip this guy because he doesn't work with TLS as the SSLContext protocol
    String ssp = "";
    for (String protocol : socket.getSupportedProtocols()) {
        if (!"SSLv2Hello".equals(protocol)) {
            ssp = protocol;
            break;
        }
    }
    supplementalProperties.setProperty("secureSocketProtocol.0", ssp);
    return this.createPropertiesPlaceholderAwareContext(supplementalProperties);
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLContext(javax.net.ssl.SSLContext) Properties(java.util.Properties) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 64 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project robovm by robovm.

the class SSLSocketFactory method createTrustManagers.

private static TrustManager[] createTrustManagers(final KeyStore keystore) throws KeyStoreException, NoSuchAlgorithmException {
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }
    TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmfactory.init(keystore);
    return tmfactory.getTrustManagers();
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 65 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project robovm by robovm.

the class DefaultSSLContextImpl method getTrustManagers.

// TODO javax.net.ssl.trustStoreProvider system property
TrustManager[] getTrustManagers() throws GeneralSecurityException, IOException {
    if (TRUST_MANAGERS != null) {
        return TRUST_MANAGERS;
    }
    // find TrustStore, TrustManagers
    String keystore = System.getProperty("javax.net.ssl.trustStore");
    if (keystore == null) {
        return null;
    }
    String keystorepwd = System.getProperty("javax.net.ssl.trustStorePassword");
    char[] pwd = (keystorepwd == null) ? null : keystorepwd.toCharArray();
    // TODO Defaults: jssecacerts; cacerts
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(keystore));
        ks.load(is, pwd);
    } finally {
        if (is != null) {
            is.close();
        }
    }
    String tmfAlg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlg);
    tmf.init(ks);
    TRUST_MANAGERS = tmf.getTrustManagers();
    return TRUST_MANAGERS;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream)

Aggregations

TrustManagerFactory (javax.net.ssl.TrustManagerFactory)504 KeyStore (java.security.KeyStore)318 SSLContext (javax.net.ssl.SSLContext)247 TrustManager (javax.net.ssl.TrustManager)186 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)180 IOException (java.io.IOException)129 FileInputStream (java.io.FileInputStream)123 X509TrustManager (javax.net.ssl.X509TrustManager)123 InputStream (java.io.InputStream)113 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)113 KeyStoreException (java.security.KeyStoreException)98 CertificateException (java.security.cert.CertificateException)87 KeyManagementException (java.security.KeyManagementException)64 X509Certificate (java.security.cert.X509Certificate)60 SecureRandom (java.security.SecureRandom)53 KeyManager (javax.net.ssl.KeyManager)48 CertificateFactory (java.security.cert.CertificateFactory)37 GeneralSecurityException (java.security.GeneralSecurityException)36 File (java.io.File)35 Certificate (java.security.cert.Certificate)34