Example usage for javax.net.ssl SSLContext setDefault

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

Introduction

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

Prototype

public static void setDefault(SSLContext context) 

Source Link

Document

Sets the default SSL context.

Usage

From source file:eu.europa.esig.dss.client.http.commons.CommonsDataLoader.java

private RegistryBuilder<ConnectionSocketFactory> setConnectionManagerSchemeHttps(
        RegistryBuilder<ConnectionSocketFactory> socketFactoryRegistryBuilder) throws DSSException {
    try {//from   ww  w  .  j a  v  a 2 s.  co  m

        SSLContext sslContext = null;
        if (StringUtils.isEmpty(sslKeystorePath)) {
            LOG.debug("Use default SSL configuration");
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() },
                    new SecureRandom());
            SSLContext.setDefault(sslContext);
        } else {
            LOG.debug("Use specific SSL configuration with keystore");
            FileInputStream fis = new FileInputStream(new File(sslKeystorePath));
            KeyStore keystore = KeyStore.getInstance(sslKeystoreType);
            keystore.load(fis, sslKeystorePassword.toCharArray());
            IOUtils.closeQuietly(fis);
            sslContext = SSLContexts.custom().loadTrustMaterial(keystore).useTLS().build();
        }

        final SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                sslContext);
        return socketFactoryRegistryBuilder.register("https", sslConnectionSocketFactory);
    } catch (Exception e) {
        throw new DSSException(e);
    }
}

From source file:org.apache.flink.runtime.rest.RestServerEndpointITCase.java

@Before
public void setup() throws Exception {
    config.setString(WebOptions.UPLOAD_DIR, temporaryFolder.newFolder().getCanonicalPath());

    defaultSSLContext = SSLContext.getDefault();
    defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    final SSLContext sslClientContext = SSLUtils.createRestClientSSLContext(config);
    if (sslClientContext != null) {
        SSLContext.setDefault(sslClientContext);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslClientContext.getSocketFactory());
    }// ww  w.j  av a 2  s.  c  o  m

    RestServerEndpointConfiguration serverConfig = RestServerEndpointConfiguration.fromConfiguration(config);
    RestClientConfiguration clientConfig = RestClientConfiguration.fromConfiguration(config);

    final String restAddress = "http://localhost:1234";
    RestfulGateway mockRestfulGateway = mock(RestfulGateway.class);
    when(mockRestfulGateway.requestRestAddress(any(Time.class)))
            .thenReturn(CompletableFuture.completedFuture(restAddress));

    final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () -> CompletableFuture
            .completedFuture(mockRestfulGateway);

    testHandler = new TestHandler(CompletableFuture.completedFuture(restAddress), mockGatewayRetriever,
            RpcUtils.INF_TIMEOUT);

    TestVersionHandler testVersionHandler = new TestVersionHandler(
            CompletableFuture.completedFuture(restAddress), mockGatewayRetriever, RpcUtils.INF_TIMEOUT);

    TestVersionSelectionHandler1 testVersionSelectionHandler1 = new TestVersionSelectionHandler1(
            CompletableFuture.completedFuture(restAddress), mockGatewayRetriever, RpcUtils.INF_TIMEOUT);

    TestVersionSelectionHandler2 testVersionSelectionHandler2 = new TestVersionSelectionHandler2(
            CompletableFuture.completedFuture(restAddress), mockGatewayRetriever, RpcUtils.INF_TIMEOUT);

    testUploadHandler = new TestUploadHandler(CompletableFuture.completedFuture(restAddress),
            mockGatewayRetriever, RpcUtils.INF_TIMEOUT);

    final StaticFileServerHandler<RestfulGateway> staticFileServerHandler = new StaticFileServerHandler<>(
            mockGatewayRetriever, CompletableFuture.completedFuture(restAddress), RpcUtils.INF_TIMEOUT,
            temporaryFolder.getRoot());

    final List<Tuple2<RestHandlerSpecification, ChannelInboundHandler>> handlers = Arrays.asList(
            Tuple2.of(new TestHeaders(), testHandler), Tuple2.of(TestUploadHeaders.INSTANCE, testUploadHandler),
            Tuple2.of(testVersionHandler.getMessageHeaders(), testVersionHandler),
            Tuple2.of(testVersionSelectionHandler1.getMessageHeaders(), testVersionSelectionHandler1),
            Tuple2.of(testVersionSelectionHandler2.getMessageHeaders(), testVersionSelectionHandler2),
            Tuple2.of(WebContentHandlerSpecification.getInstance(), staticFileServerHandler));

    serverEndpoint = new TestRestServerEndpoint(serverConfig, handlers);
    restClient = new TestRestClient(clientConfig);

    serverEndpoint.start();
    serverAddress = serverEndpoint.getServerAddress();
}

From source file:org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization.client.OAuthRequestInterceptor.java

private static SSLSocketFactory initSSLConnection(KeyStore keyStore, String keyStorePassword,
        KeyStore trustStore)//from   w  w  w.  ja v a2  s.co  m
        throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, KeyManagementException {
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
    keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
    trustManagerFactory.init(trustStore);

    // Create and initialize SSLContext for HTTPS communication
    SSLContext sslContext = SSLContext.getInstance("SSLv3");
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
    SSLContext.setDefault(sslContext);
    return sslContext.getSocketFactory();
}

From source file:iqq.im.service.ApacheHttpService.java

@Override
public void init(QQContext context) throws QQException {
    super.init(context);
    try {//from  w  w  w .  ja  va 2  s. c  o  m
        SSLContext sslContext = new QQSSLSocketFactory().getSSLContext();
        SSLContext.setDefault(sslContext);
        asyncHttpClient = new DefaultHttpAsyncClient();

        HttpParams httpParams = asyncHttpClient.getParams();
        HttpConnectionParams.setSoTimeout(httpParams, QQConstants.HTTP_TIME_OUT);
        HttpConnectionParams.setConnectionTimeout(httpParams, QQConstants.HTTP_TIME_OUT);
        HttpConnectionParams.setTcpNoDelay(httpParams, true);
        HttpConnectionParams.setSocketBufferSize(httpParams, 4096);
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

        asyncHttpClient.getConnectionManager().getSchemeRegistry()
                .register(new AsyncScheme("https", 443, new SSLLayeringStrategy(sslContext)));
        asyncHttpClient.setRedirectStrategy(new QQDefaultRedirectStrategy());
        asyncHttpClient.start();
        cookieJar = new QQHttpCookieJar();
    } catch (IOReactorException e) {
        throw new QQException(QQErrorCode.INIT_ERROR, e);
    }
}

From source file:edu.mayo.cts2.framework.core.client.Cts2RestClient.java

/**
 * Enable trust for a self signed ssl./*ww w .j a  v  a2s  . c  o  m*/
 */
protected void trustSelfSignedSSL() {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

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

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLContext.setDefault(ctx);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.minws.frame.sdk.webqq.service.ApacheHttpService.java

/** {@inheritDoc} */
@Override//from w  w  w.  ja  v a 2  s.  c  o  m
public void init(QQContext context) throws QQException {
    super.init(context);
    try {
        SSLContext sslContext = new QQSSLSocketFactory().getSSLContext();
        SSLContext.setDefault(sslContext);
        asyncHttpClient = new DefaultHttpAsyncClient();

        HttpParams httpParams = asyncHttpClient.getParams();
        HttpConnectionParams.setSoTimeout(httpParams, QQConstants.HTTP_TIME_OUT);
        HttpConnectionParams.setConnectionTimeout(httpParams, QQConstants.HTTP_TIME_OUT);
        HttpConnectionParams.setTcpNoDelay(httpParams, true);
        HttpConnectionParams.setSocketBufferSize(httpParams, 4096);
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

        asyncHttpClient.getConnectionManager().getSchemeRegistry()
                .register(new AsyncScheme("https", 443, new SSLLayeringStrategy(sslContext)));
        asyncHttpClient.setRedirectStrategy(new QQDefaultRedirectStrategy());
        asyncHttpClient.start();
        cookieJar = new QQHttpCookieJar();
    } catch (IOReactorException e) {
        throw new QQException(QQErrorCode.INIT_ERROR, e);
    }
}

From source file:test.unit.be.fedict.eid.applet.service.AppletServiceServletTest.java

@Before
public void setUp() throws Exception {
    this.servletTester = new ServletTester();
    this.servletTester.addServlet(AppletServiceServlet.class, "/");

    Security.addProvider(new BouncyCastleProvider());

    KeyPair keyPair = generateKeyPair();
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509Certificate certificate = generateSelfSignedCertificate(keyPair, "CN=localhost", notBefore, notAfter);
    File tmpP12File = File.createTempFile("ssl-", ".p12");
    LOG.debug("p12 file: " + tmpP12File.getAbsolutePath());
    persistKey(tmpP12File, keyPair.getPrivate(), certificate, "secret".toCharArray(), "secret".toCharArray());

    SslSocketConnector sslSocketConnector = new SslSocketConnector();
    sslSocketConnector.setKeystore(tmpP12File.getAbsolutePath());
    sslSocketConnector.setTruststore(tmpP12File.getAbsolutePath());
    sslSocketConnector.setTruststoreType("pkcs12");
    sslSocketConnector.setKeystoreType("pkcs12");
    sslSocketConnector.setPassword("secret");
    sslSocketConnector.setKeyPassword("secret");
    sslSocketConnector.setTrustPassword("secret");
    sslSocketConnector.setMaxIdleTime(30000);
    int sslPort = getFreePort();
    sslSocketConnector.setPort(sslPort);
    this.servletTester.getContext().getServer().addConnector(sslSocketConnector);
    this.sslLocation = "https://localhost:" + sslPort + "/";

    this.servletTester.start();
    this.location = this.servletTester.createSocketConnector(true);

    SSLContext sslContext = SSLContext.getInstance("TLS");
    TrustManager trustManager = new TestTrustManager(certificate);
    sslContext.init(null, new TrustManager[] { trustManager }, null);
    SSLContext.setDefault(sslContext);
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Before
public void setUp() throws Exception {
    this.servletTester = new ServletTester();
    this.servletHolder = this.servletTester.addServlet(AppletServiceServlet.class, "/");

    Security.addProvider(new BouncyCastleProvider());

    KeyPair keyPair = generateKeyPair();
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    this.certificate = generateSelfSignedCertificate(keyPair, "CN=localhost", notBefore, notAfter);
    File tmpP12File = File.createTempFile("ssl-", ".p12");
    LOG.debug("p12 file: " + tmpP12File.getAbsolutePath());
    persistKey(tmpP12File, keyPair.getPrivate(), this.certificate, "secret".toCharArray(),
            "secret".toCharArray());

    SslSocketConnector sslSocketConnector = new SslSocketConnector();
    sslSocketConnector.setKeystore(tmpP12File.getAbsolutePath());
    sslSocketConnector.setTruststore(tmpP12File.getAbsolutePath());
    sslSocketConnector.setTruststoreType("pkcs12");
    sslSocketConnector.setKeystoreType("pkcs12");
    sslSocketConnector.setPassword("secret");
    sslSocketConnector.setKeyPassword("secret");
    sslSocketConnector.setTrustPassword("secret");
    sslSocketConnector.setMaxIdleTime(30000);
    int sslPort = getFreePort();
    sslSocketConnector.setPort(sslPort);
    this.servletTester.getContext().getServer().addConnector(sslSocketConnector);
    this.sslLocation = "https://localhost:" + sslPort + "/";

    this.servletTester.start();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    TrustManager trustManager = new TestTrustManager(this.certificate);
    sslContext.init(null, new TrustManager[] { trustManager }, null);
    SSLContext.setDefault(sslContext);
}