Example usage for javax.net.ssl SSLContext getSocketFactory

List of usage examples for javax.net.ssl SSLContext getSocketFactory

Introduction

In this page you can find the example usage for javax.net.ssl SSLContext getSocketFactory.

Prototype

public final SSLSocketFactory getSocketFactory() 

Source Link

Document

Returns a SocketFactory object for this context.

Usage

From source file:org.apache.geode.rest.internal.web.controllers.RestAPIsWithSSLDUnitTest.java

@Test
public void testSSLWithMultipleCipherSuiteLegacy() throws Exception {
    Properties props = new Properties();
    props.setProperty(HTTP_SERVICE_SSL_ENABLED, "true");
    props.setProperty(HTTP_SERVICE_SSL_KEYSTORE, findTrustedJKSWithSingleEntry().getCanonicalPath());
    props.setProperty(HTTP_SERVICE_SSL_KEYSTORE_PASSWORD, "password");
    props.setProperty(HTTP_SERVICE_SSL_PROTOCOLS, "TLSv1.2");

    SSLContext ssl = SSLContext.getInstance("TLSv1.2");

    ssl.init(null, null, new java.security.SecureRandom());
    String[] cipherSuites = ssl.getSocketFactory().getSupportedCipherSuites();

    props.setProperty(HTTP_SERVICE_SSL_CIPHERS, cipherSuites[0] + "," + cipherSuites[1]);

    String restEndpoint = startInfraWithSSL(props, false);
    validateConnection(restEndpoint, "TLSv1.2", props);
}

From source file:at.alladin.rmbt.client.RMBTClient.java

private SSLSocketFactory createSSLSocketFactory() {
    log("initSSL...");
    try {/*from w  w  w  . j  a  v  a 2 s. c  o m*/
        final SSLContext sc = getSSLContext(null, null);

        final SSLSocketFactory factory = sc.getSocketFactory();

        return factory;
    } catch (final Exception e) {
        setErrorStatus();
        log(e);
    }
    return null;
}

From source file:org.apache.geode.rest.internal.web.controllers.RestAPIsWithSSLDUnitTest.java

@Test
public void testSSLWithCipherSuiteLegacy() throws Exception {
    System.setProperty("javax.net.debug", "ssl");
    Properties props = new Properties();
    props.setProperty(HTTP_SERVICE_SSL_ENABLED, "true");
    props.setProperty(HTTP_SERVICE_SSL_KEYSTORE, findTrustedJKSWithSingleEntry().getCanonicalPath());
    props.setProperty(HTTP_SERVICE_SSL_KEYSTORE_PASSWORD, "password");
    props.setProperty(HTTP_SERVICE_SSL_PROTOCOLS, "TLSv1.2");

    SSLContext ssl = SSLContext.getInstance("TLSv1.2");

    ssl.init(null, null, new java.security.SecureRandom());
    String[] cipherSuites = ssl.getSocketFactory().getSupportedCipherSuites();

    props.setProperty(HTTP_SERVICE_SSL_CIPHERS, cipherSuites[0]);

    String restEndpoint = startInfraWithSSL(props, false);
    validateConnection(restEndpoint, "TLSv1.2", props);
}

From source file:org.apache.geode.rest.internal.web.controllers.RestAPIsWithSSLDUnitTest.java

@Test
public void testSSLWithMultipleCipherSuite() throws Exception {
    Properties props = new Properties();
    props.setProperty(SSL_KEYSTORE, findTrustedJKSWithSingleEntry().getCanonicalPath());
    props.setProperty(SSL_TRUSTSTORE, findTrustedJKSWithSingleEntry().getCanonicalPath());
    props.setProperty(SSL_KEYSTORE_PASSWORD, "password");
    props.setProperty(SSL_TRUSTSTORE_PASSWORD, "password");
    props.setProperty(SSL_KEYSTORE_TYPE, "JKS");
    props.setProperty(SSL_PROTOCOLS, "TLSv1.2");
    props.setProperty(SSL_ENABLED_COMPONENTS, SecurableCommunicationChannel.WEB.getConstant());

    SSLContext ssl = SSLContext.getInstance("TLSv1.2");

    ssl.init(null, null, new java.security.SecureRandom());
    String[] cipherSuites = ssl.getSocketFactory().getSupportedCipherSuites();

    props.setProperty(SSL_CIPHERS, cipherSuites[0] + "," + cipherSuites[1]);

    String restEndpoint = startInfraWithSSL(props, false);
    validateConnection(restEndpoint, "TLSv1.2", props);
}

From source file:com.flexvdi.androidlauncher.LoginActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    mContext = this;

    try {//from  w ww .j  a  v a 2  s  .  c  o  m
        GStreamer.init(mContext);
    } catch (Exception e) {
        Log.e(TAG, "Can't initialize GStreamer" + e.getMessage());
        finish();
    }

    settings = getSharedPreferences("flexVDI", MODE_PRIVATE);
    settingsEditor = settings.edit();
    /* Uncomment this for clearing preferences (useful when debugging) */
    //settingsEditor.clear();
    //settingsEditor.apply();
    //settingsEditor.commit();

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.activity_login);

    textUser = (EditText) findViewById(R.id.textUser);
    textServer = (EditText) findViewById(R.id.textServer);
    textPassword = (EditText) findViewById(R.id.textPassword);

    goButton = (Button) findViewById(R.id.buttonGO);
    goButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ConnectivityManager cm = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

            if (!isConnected) {
                Toast.makeText(view.getContext(), getResources().getString(R.string.no_network),
                        Toast.LENGTH_LONG).show();
                return;
            }

            if (checkBoxGenericSpice.isChecked()) {
                String userField = textUser.getText().toString();
                if (userField.length() == 0 || !userField.contains(":")) {
                    Toast.makeText(view.getContext(), getResources().getString(R.string.invalid_spice_server),
                            Toast.LENGTH_LONG).show();
                    return;
                }

                String spiceAddress = userField.substring(0, userField.indexOf(":"));
                String spicePort = userField.substring(userField.indexOf(":") + 1);

                if (spiceAddress.length() == 0 || spicePort.length() == 0) {
                    Toast.makeText(view.getContext(), getResources().getString(R.string.invalid_spice_server),
                            Toast.LENGTH_LONG).show();
                    return;
                }

                String spicePassword = textPassword.getText().toString();

                settingsEditor.putBoolean("enableSound", checkBoxEnableSound.isChecked());
                settingsEditor.putBoolean("staticResolution", checkBoxStaticResolution.isChecked());
                settingsEditor.putBoolean("genericSpice", checkBoxGenericSpice.isChecked());
                settingsEditor.putString("flexServerName", textServer.getText().toString());

                settingsEditor.putString("spice_address", spiceAddress);
                settingsEditor.putString("spice_port", spicePort);
                settingsEditor.putString("spice_password", spicePassword);
                settingsEditor.putBoolean("use_ws", false);

                settingsEditor.apply();
                settingsEditor.commit();

                startMainActivity();
            } else {
                if (textServer.getText().length() == 0) {
                    Toast.makeText(view.getContext(), getResources().getString(R.string.empty_flexvdi_server),
                            Toast.LENGTH_LONG).show();
                } else {
                    if (textUser.getText().length() != 0 && textPassword.getText().length() != 0) {
                        new RequestTask().execute("authmode", textServer.getText().toString(),
                                textUser.getText().toString(), textPassword.getText().toString(), "");
                    } else
                        Toast.makeText(view.getContext(), getResources().getString(R.string.empty_credentials),
                                Toast.LENGTH_LONG).show();
                }
            }
        }
    });

    // The advanced settings button.
    checkBoxAdvancedOptions = (CheckBox) findViewById(R.id.checkBoxAdvancedSettings);
    layoutAdvancedOptions = (LinearLayout) findViewById(R.id.layoutAdvancedOptions2);
    checkBoxAdvancedOptions.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean checked) {
            if (checked)
                layoutAdvancedOptions.setVisibility(View.VISIBLE);
            else
                layoutAdvancedOptions.setVisibility(View.GONE);
        }
    });

    textServer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View field, boolean hasFocus) {
            if (!hasFocus && checkBoxGenericSpice.isChecked()) {
                if (textUser.getText().toString().length() == 0) {
                    textUser.setText(textServer.getText());
                }
            }
        }
    });

    checkBoxEnableSound = (CheckBox) findViewById(R.id.checkBoxEnableSound);
    if (settings.getBoolean("enableSound", true)) {
        checkBoxEnableSound.setChecked(true);
    } else {
        checkBoxEnableSound.setChecked(false);
    }

    if (!settings.contains("staticResolution")) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        if ((size.x + size.y) > 2340) {
            /* 2340 = 1440+900 */
            settingsEditor.putBoolean("staticResolution", true);
        } else {
            settingsEditor.putBoolean("staticResolution", false);
        }
        settingsEditor.apply();
        settingsEditor.commit();
    }

    checkBoxStaticResolution = (CheckBox) findViewById(R.id.checkBoxStaticResolution);
    if (settings.getBoolean("staticResolution", true)) {
        checkBoxStaticResolution.setChecked(true);
    } else {
        checkBoxStaticResolution.setChecked(false);
    }

    checkBoxGenericSpice = (CheckBox) findViewById(R.id.checkBoxGenericSpice);
    if (settings.getBoolean("genericSpice", false)) {
        checkBoxGenericSpice.setChecked(true);
        checkBoxAdvancedOptions.setChecked(true);
        layoutAdvancedOptions.setVisibility(View.VISIBLE);

        if (settings.contains("flexServerName")) {
            textServer.setText(settings.getString("flexServerName", ""));
            textUser.setText(settings.getString("flexServerName", ""));
            textServer.setHint(getResources().getString(R.string.spice_server));
            textUser.setHint(getResources().getString(R.string.spice_server_port));
        }
    } else {
        checkBoxGenericSpice.setChecked(false);
        if (settings.contains("flexServerName")) {
            textServer.setText(settings.getString("flexServerName", ""));
            layoutAdvancedOptions.setVisibility(View.GONE);
        } else {
            textServer.setText("manager.flexvdi.com");
            checkBoxAdvancedOptions.setChecked(true);
        }
    }

    checkBoxGenericSpice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean checked) {
            if (checked) {
                textServer.setHint(getResources().getString(R.string.spice_server));
                textUser.setHint(getResources().getString(R.string.spice_server_port));
                String server = textServer.getText().toString();
                if (server.length() != 0) {
                    if (server.contains(":")) {
                        textUser.setText(server);
                    } else {
                        textUser.setText(server + ":5900");
                        textServer.setText(server + ":5900");
                    }
                }
            } else {
                textServer.setHint(getResources().getString(R.string.flexvdi_server));
                String server = textServer.getText().toString();
                if (server.length() != 0 && server.contains(":")) {
                    textServer.setText(server.substring(0, server.indexOf(":")));
                }
                textUser.setText("");
                textUser.setHint(getResources().getString(R.string.user));
            }
        }
    });

    deviceID = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);

    textViewDeviceID = (TextView) findViewById(R.id.textViewDeviceID);
    textViewDeviceID.setText("ID: " + deviceID + " (" + BuildConfig.VERSION_NAME + ")");

    try {
        HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[] { new NullX509TrustManager() }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (NoSuchAlgorithmException nsae) {
        Log.e(TAG, nsae.getMessage());
    } catch (KeyManagementException kme) {
        Log.e(TAG, kme.getMessage());
    }
}

From source file:org.apache.geode.rest.internal.web.controllers.RestAPIsWithSSLDUnitTest.java

@Test
public void testSSLWithCipherSuite() throws Exception {
    System.setProperty("javax.net.debug", "ssl");
    Properties props = new Properties();
    props.setProperty(SSL_KEYSTORE, findTrustedJKSWithSingleEntry().getCanonicalPath());
    props.setProperty(SSL_TRUSTSTORE, findTrustedJKSWithSingleEntry().getCanonicalPath());
    props.setProperty(SSL_KEYSTORE_PASSWORD, "password");
    props.setProperty(SSL_TRUSTSTORE_PASSWORD, "password");
    props.setProperty(SSL_KEYSTORE_TYPE, "JKS");
    props.setProperty(SSL_PROTOCOLS, "TLSv1.2");
    props.setProperty(SSL_ENABLED_COMPONENTS, SecurableCommunicationChannel.WEB.getConstant());

    SSLContext ssl = SSLContext.getInstance("TLSv1.2");

    ssl.init(null, null, new java.security.SecureRandom());
    String[] cipherSuites = ssl.getSocketFactory().getSupportedCipherSuites();

    props.setProperty(SSL_CIPHERS, cipherSuites[0]);

    String restEndpoint = startInfraWithSSL(props, false);
    validateConnection(restEndpoint, "TLSv1.2", props);
}

From source file:it.anyplace.sync.core.security.KeystoreHandler.java

private SSLSocketFactory getSocketFactory()
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException {
    SSLContext sslContext = SSLContext.getInstance(TLS_VERSION);
    sslContext.init(getKeyManagers(), new TrustManager[] { new X509TrustManager() {
        @Override/*from  w  w  w .j  av a2 s .  c o  m*/
        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } }, null);
    return sslContext.getSocketFactory();
}

From source file:com.vimc.ahttp.HurlWorker.java

private void setDefaultSSLSocketFactory() {
    SSLContext sslContext = null;
    try {//  w  w w . ja  va2 s .c  o  m
        X509TrustManagerImpl mtm = new X509TrustManagerImpl();
        TrustManager[] tms = new TrustManager[] { mtm };

        // ?X509TrustManagerSSLContext
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tms, new java.security.SecureRandom());
    } catch (Exception e) {
        e.printStackTrace();
    }

    // javax.net.ssl.HttpsURLConnectionSocketFactoryHostnameVerifier
    if (sslContext != null) {
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    }
    X509HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}

From source file:edu.vt.middleware.ldap.LdapTLSSocketFactory.java

/**
 * Creates the underlying SSLContext using truststore and keystore attributes
 * and makes this factory ready for use. Must be called before factory can be
 * used./*from w  ww. j ava2s. c om*/
 *
 * @throws  IOException  if the keystore cannot be loaded
 * @throws  GeneralSecurityException  if the SSLContext cannot be created
 */
public void initialize() throws IOException, GeneralSecurityException {
    final SSLContext ctx = SSLContext.getInstance(DEFAULT_PROTOCOL);
    final TrustManager[] tm = this.initTrustManager(this.getTrustStoreStream(), this.getTrustStorePassword(),
            this.getTrustStoreType());
    final KeyManager[] km = this.initKeyManager(this.getKeyStoreStream(), this.getKeyStorePassword(),
            this.getKeyStoreType());
    ctx.init(km, tm, null);
    this.factory = ctx.getSocketFactory();
}

From source file:org.apache.jmeter.protocol.smtp.sampler.protocol.LocalTrustStoreSSLSocketFactory.java

public LocalTrustStoreSSLSocketFactory(File truststore) {
    SSLContext sslcontext = null;
    try {//  www.j a  v  a2 s.c o m
        KeyStore ks = KeyStore.getInstance("JKS"); // $NON-NLS-1$
        InputStream stream = null;
        try {
            stream = new BufferedInputStream(new FileInputStream(truststore));
            ks.load(stream, null);
        } finally {
            IOUtils.closeQuietly(stream);
        }

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        TrustManager[] trustmanagers = tmf.getTrustManagers();
        sslcontext = SSLContext.getInstance("TLS"); // $NON-NLS-1$
        sslcontext.init(null, trustmanagers, new SecureRandom());
    } catch (Exception e) {
        throw new RuntimeException("Could not create the SSL context", e);
    }
    factory = sslcontext.getSocketFactory();
}