Java Code Examples for javax.net.ssl.TrustManagerFactory#getDefaultAlgorithm()

The following examples show how to use javax.net.ssl.TrustManagerFactory#getDefaultAlgorithm() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthSSLProtocolSocketFactory.java    From iaf with Apache License 2.0 7 votes vote down vote up
private static TrustManager[] createTrustManagers(final KeyStore keystore, String algorithm)
    throws KeyStoreException, NoSuchAlgorithmException
{ 
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }
    log.debug("Initializing trust manager");
    if (StringUtils.isEmpty(algorithm)) {
    	algorithm=TrustManagerFactory.getDefaultAlgorithm();
    	log.debug("using default TrustManager algorithm ["+algorithm+"]");
    } else {
    	log.debug("using configured TrustManager algorithm ["+algorithm+"]");
    }
    TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(algorithm);
    tmfactory.init(keystore);
    TrustManager[] trustmanagers = tmfactory.getTrustManagers();
    return trustmanagers; 
}
 
Example 2
Source File: X509Util.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a X509TrustManagerImplementation backed up by the given key
 * store. When null is passed as a key store, system default trust store is
 * used. Returns null if no created TrustManager was suitable.
 * @throws KeyStoreException, NoSuchAlgorithmException on error initializing the TrustManager.
 */
private static X509TrustManagerImplementation createTrustManager(KeyStore keyStore) throws
        KeyStoreException, NoSuchAlgorithmException {
    String algorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
    tmf.init(keyStore);

    for (TrustManager tm : tmf.getTrustManagers()) {
        if (tm instanceof X509TrustManager) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    return new X509TrustManagerJellyBean((X509TrustManager) tm);
                } else {
                    return new X509TrustManagerIceCreamSandwich((X509TrustManager) tm);
                }
            } catch (IllegalArgumentException e) {
                String className = tm.getClass().getName();
                Log.e(TAG, "Error creating trust manager (" + className + "): " + e);
            }
        }
    }
    Log.e(TAG, "Could not find suitable trust manager");
    return null;
}
 
Example 3
Source File: X509Util.java    From cronet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a X509TrustManagerImplementation backed up by the given key
 * store. When null is passed as a key store, system default trust store is
 * used. Returns null if no created TrustManager was suitable.
 * @throws KeyStoreException, NoSuchAlgorithmException on error initializing the TrustManager.
 */
private static X509TrustManagerImplementation createTrustManager(KeyStore keyStore) throws
        KeyStoreException, NoSuchAlgorithmException {
    String algorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
    tmf.init(keyStore);

    for (TrustManager tm : tmf.getTrustManagers()) {
        if (tm instanceof X509TrustManager) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    return new X509TrustManagerJellyBean((X509TrustManager) tm);
                } else {
                    return new X509TrustManagerIceCreamSandwich((X509TrustManager) tm);
                }
            } catch (IllegalArgumentException e) {
                String className = tm.getClass().getName();
                Log.e(TAG, "Error creating trust manager (" + className + "): " + e);
            }
        }
    }
    Log.e(TAG, "Could not find suitable trust manager");
    return null;
}
 
Example 4
Source File: ConfigurableX509TrustManager.java    From webarchive-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param level Level of trust to effect.
 *
 * @throws NoSuchAlgorithmException
 * @throws KeyStoreException
 */
public ConfigurableX509TrustManager(TrustLevel level)
throws NoSuchAlgorithmException, KeyStoreException {
    super();
    TrustManagerFactory factory = TrustManagerFactory.
        getInstance(TrustManagerFactory.getDefaultAlgorithm());

    // Pass in a null (Trust) KeyStore.  Null says use the 'default'
    // 'trust' keystore (KeyStore class is used to hold keys and to hold
    // 'trusts' (certs)). See 'X509TrustManager Interface' in this doc:
    // http://java.sun.com
    // /j2se/1.4.2/docs/guide/security/jsse/JSSERefGuide.html#Introduction
    factory.init((KeyStore)null);
    TrustManager[] trustmanagers = factory.getTrustManagers();
    if (trustmanagers.length == 0) {
        throw new NoSuchAlgorithmException(TrustManagerFactory.
            getDefaultAlgorithm() + " trust manager not supported");
    }
    this.standardTrustManager = (X509TrustManager)trustmanagers[0];

    this.trustLevel = level;
}
 
Example 5
Source File: KurentoRoomAPI.java    From kurento-room-client-android with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a web socket connection to the predefined URI as provided in the constructor.
 * The method responds immediately, whether or not the connection is opened.
 * The method isWebSocketConnected() should be called to ensure that the connection is open.
 * Secure socket is created if protocol contained in Uri is either https or wss.
 */
public void connectWebSocket() {
    if(isWebSocketConnected()){
        return;
    }
    // Switch to SSL web socket client factory if secure protocol detected
    String scheme;
    try {
        scheme = new URI(wsUri).getScheme();
        if (scheme.equals("https") || scheme.equals("wss")){

            // Create an SSLContext that uses our or default TrustManager
            SSLContext sslContext = SSLContext.getInstance("TLS");

            if (usingSelfSigned) {
                // Create a TrustManager that trusts the CAs in our KeyStore
                String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
                tmf.init(keyStore);
                sslContext.init(null, tmf.getTrustManagers(), null);
            } else {
                sslContext.init(null, null, null);
            }
            webSocketClientFactory = new DefaultSSLWebSocketClientFactory(sslContext);
        }
    } catch (URISyntaxException|NoSuchAlgorithmException|KeyStoreException|KeyManagementException e) {
        e.printStackTrace();
    }
    super.connectWebSocket();
}
 
Example 6
Source File: TLSUtilities.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
/**
 * Takes a given certificate and stores it inside the device's keystore.
 *
 * @param certificateStream the {@link InputStream} pointing to the certificate
 * @return a {@link ETSTLSTrust} containing the {@link X509TrustManager} as well as the {@link SSLContext} required for further usage.
 */
public static ETSTLSTrust createETSCertificateTrust(InputStream certificateStream) {

    try (InputStream caInput = new BufferedInputStream(certificateStream)) {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate ca = cf.generateCertificate(caInput);

        // Create a KeyStore containing ÉTS's CA
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        Certificate storedCertificate = keyStore.getCertificate("ca");

        // Add the certificate to the keystore if it doesn't exists or replace it if it has been changed.
        if (!keyStore.containsAlias("ca") || storedCertificate != null && !ca.equals(storedCertificate)) {
            keyStore.setCertificateEntry("ca", ca);
        }

        // Create a TrustManager that trusts the CA in the KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);

        // Create an SSLContext that uses the TrustManager
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, tmf.getTrustManagers(), null);

        X509TrustManager trustManager =  (X509TrustManager) tmf.getTrustManagers()[0];
        ETSTLSTrust sslTrust = new ETSTLSTrust(trustManager, context);

        return sslTrust;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 7
Source File: TLSParameterJaxBUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static TrustManager[] getTrustManagers(TrustManagersType tmc, boolean enableRevocation)
    throws GeneralSecurityException,
           IOException {

    final KeyStore keyStore =
        tmc.isSetKeyStore()
            ? getKeyStore(tmc.getKeyStore(), true)
            : (tmc.isSetCertStore()
                ? getKeyStore(tmc.getCertStore())
                : null);

    String alg = tmc.isSetFactoryAlgorithm()
                 ? tmc.getFactoryAlgorithm()
                 : TrustManagerFactory.getDefaultAlgorithm();

    TrustManagerFactory fac =
                 tmc.isSetProvider()
                 ? TrustManagerFactory.getInstance(alg, tmc.getProvider())
                 : TrustManagerFactory.getInstance(alg);

    if (enableRevocation) {
        PKIXBuilderParameters param = new PKIXBuilderParameters(keyStore, new X509CertSelector());
        param.setRevocationEnabled(true);

        fac.init(new CertPathTrustManagerParameters(param));
    } else {
        fac.init(keyStore);
    }

    return fac.getTrustManagers();
}
 
Example 8
Source File: SSLNettyServerTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static TrustManager[] getTrustManagers(KeyStore trustStore)
    throws NoSuchAlgorithmException, KeyStoreException {
    String alg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory fac = TrustManagerFactory.getInstance(alg);
    fac.init(trustStore);
    return fac.getTrustManagers();
}
 
Example 9
Source File: Utils.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
public static TrustManager[] getTrustManagers(KeyStore keyStore) throws GeneralSecurityException, IOException {
    // For tests, we just use the default algorithm
    String alg = TrustManagerFactory.getDefaultAlgorithm();
    // For tests, we just use the default provider.
    TrustManagerFactory fac = TrustManagerFactory.getInstance(alg);
    fac.init(keyStore);
    return fac.getTrustManagers();
}
 
Example 10
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * load the CA and use it in the https connection
 * @param filename the CA filename
 * @return the SSL context
 */
public static SSLContext getSSLContext(String filename) throws Exception {
  try {
    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    // X.509 is a standard that defines the format of public key certificates, used in TLS/SSL.
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream caInput = new BufferedInputStream(new FileInputStream(filename));
    Certificate ca = cf.generateCertificate(caInput);

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);
    return context;
  } catch (Exception e) {
    throw new Exception("Failed to load the CA file: " + e.getMessage(), e);
  }
}
 
Example 11
Source File: CertificateHelper.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
public static TrustManager[] getTrustManagers(KeyStore keyStore)
        throws KeyStoreException, NoSuchAlgorithmException,
        NoSuchProviderException {
    String trustManAlg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManAlg
    /* , PROVIDER_NAME */);
    tmf.init(keyStore);
    return tmf.getTrustManagers();
}
 
Example 12
Source File: X509Util.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates a X509TrustManager backed up by the given key store. When null is passed as a key
 * store, system default trust store is used.
 * @throws KeyStoreException, NoSuchAlgorithmException on error initializing the TrustManager.
 */
private static X509TrustManager createTrustManager(KeyStore keyStore) throws KeyStoreException,
        NoSuchAlgorithmException {
    String algorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
    tmf.init(keyStore);

    for (TrustManager tm : tmf.getTrustManagers()) {
        if (tm instanceof X509TrustManager) {
            return (X509TrustManager) tm;
        }
    }
    return null;
}
 
Example 13
Source File: SSLNettyClientTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static TrustManager[] getTrustManagers(KeyStore trustStore)
    throws NoSuchAlgorithmException, KeyStoreException {
    String alg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory fac = TrustManagerFactory.getInstance(alg);
    fac.init(trustStore);
    return fac.getTrustManagers();
}
 
Example 14
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * load the CA and use it in the https connection
 * @param filename the CA filename
 * @return the SSL context
 */
public static SSLContext getSSLContext(String filename) throws Exception {
  try {
    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    // X.509 is a standard that defines the format of public key certificates, used in TLS/SSL.
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream caInput = new BufferedInputStream(new FileInputStream(filename));
    Certificate ca = cf.generateCertificate(caInput);

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);
    return context;
  } catch (Exception e) {
    throw new Exception("Failed to load the CA file: " + e.getMessage(), e);
  }
}
 
Example 15
Source File: SSLUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static String getTrustStoreAlgorithm(
                                    String trustStoreMgrFactoryAlgorithm,
                                    Logger log) {
    final String logMsg;
    if (trustStoreMgrFactoryAlgorithm != null) {
        logMsg = "TRUST_STORE_ALGORITHM_SET";
    } else {
        trustStoreMgrFactoryAlgorithm =
            TrustManagerFactory.getDefaultAlgorithm();
        logMsg = "TRUST_STORE_ALGORITHM_NOT_SET";
    }
    LogUtils.log(log, Level.FINE, logMsg, trustStoreMgrFactoryAlgorithm);
    return trustStoreMgrFactoryAlgorithm;
}
 
Example 16
Source File: SnowTrustManagerFactorySpi.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
/**
 * if provided, the expected server spec hash is used to only validate certs
 * that match that.  If null, then allow any cert.
 */
public static TrustManagerFactory getFactory(AddressSpecHash expected_server_spec_hash, NetworkParams params)
  throws Exception
{
  String algo = TrustManagerFactory. getDefaultAlgorithm();
  Provider prov = TrustManagerFactory.getInstance(algo).getProvider();

  return new SnowTrustManagerFactory(new SnowTrustManagerFactorySpi(expected_server_spec_hash, prov, params), prov, algo);
}
 
Example 17
Source File: HttpWebConnectionInsecureSSLWithClientCertificateTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static TrustManagerFactory createTrustManagerFactory() throws NoSuchAlgorithmException {
    final String algorithm = TrustManagerFactory.getDefaultAlgorithm();
    try {
        return TrustManagerFactory.getInstance(algorithm);
    }
    catch (final NoSuchAlgorithmException e) {
        return TrustManagerFactory.getInstance("SunX");
    }
}
 
Example 18
Source File: MergeTrustManager.java    From CapturePacket with MIT License 5 votes vote down vote up
private X509TrustManager defaultTrustManager(KeyStore trustStore)
        throws NoSuchAlgorithmException, KeyStoreException {
    String tma = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tma);
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    for (TrustManager each : trustManagers) {
        if (each instanceof X509TrustManager) {
            return (X509TrustManager) each;
        }
    }
    throw new IllegalStateException("Missed X509TrustManager in "
            + Arrays.toString(trustManagers));
}
 
Example 19
Source File: MergeTrustManager.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
private X509TrustManager defaultTrustManager(KeyStore trustStore)
        throws NoSuchAlgorithmException, KeyStoreException {
    String tma = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tma);
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    for (TrustManager each : trustManagers) {
        if (each instanceof X509TrustManager) {
            return (X509TrustManager) each;
        }
    }
    throw new IllegalStateException("Missed X509TrustManager in "
            + Arrays.toString(trustManagers));
}
 
Example 20
Source File: MergeTrustManager.java    From PowerTunnel with MIT License 5 votes vote down vote up
private X509TrustManager defaultTrustManager(KeyStore trustStore)
        throws NoSuchAlgorithmException, KeyStoreException {
    String tma = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tma);
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    for (TrustManager each : trustManagers) {
        if (each instanceof X509TrustManager) {
            return (X509TrustManager) each;
        }
    }
    throw new IllegalStateException("Missed X509TrustManager in "
            + Arrays.toString(trustManagers));
}