Search in sources :

Example 61 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project BBS-Android by bdpqchen.

the class RxDoHttpClient method getUnsafeOkHttpClient.

public static OkHttpClient.Builder getUnsafeOkHttpClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }
        } };
        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
        okHttpClient.sslSocketFactory(sslSocketFactory);
        okHttpClient.protocols(Collections.singletonList(Protocol.HTTP_1_1));
        okHttpClient.hostnameVerifier((hostname, session) -> true);
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SSLContext(javax.net.ssl.SSLContext) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 62 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project opennms by OpenNMS.

the class VmwareViJavaAccessTest method setUp.

@Before
public void setUp() throws Exception {
    // setup required objects
    managedObjectReferenceManagedEntity = new ManagedObjectReference();
    managedObjectReferenceManagedEntity.setType("ManagedEntity");
    managedObjectReferenceManagedEntity.setVal("moIdMe");
    managedObjectReferenceVirtualMachine = new ManagedObjectReference();
    managedObjectReferenceVirtualMachine.setType("VirtualMachine");
    managedObjectReferenceVirtualMachine.setVal("moIdVm");
    managedObjectReferenceHostSystem = new ManagedObjectReference();
    managedObjectReferenceHostSystem.setType("HostSystem");
    managedObjectReferenceHostSystem.setVal("moIdHs");
    // setup VmwareViJavaAccess
    vmwareViJavaAccess = new VmwareViJavaAccess("hostname", "username", "password") {

        @Override
        protected void relax() {
        }
    };
    // setup PerformanceManager
    mockPerformanceManager = createMock(PerformanceManager.class);
    // setup ServiceInstance mock
    mockServiceInstance = createMock(ServiceInstance.class);
    // setup ServerConnection
    mockServerConnection = new ServerConnection(new URL("https://hostname/sdk"), new VimPortType(new WSClient("https://hostname/sdk") {

        @Override
        protected SSLSocketFactory getTrustAllSocketFactory(boolean ignoreCert) throws RemoteException {
            return null;
        }
    }), mockServiceInstance);
    // setup AboutInfo
    mockAboutInfo = createMock(AboutInfo.class);
    expectNew(ServiceInstance.class, new Class<?>[] { URL.class, String.class, String.class }, new URL("https://hostname/sdk"), "username", "password").andReturn(mockServiceInstance).anyTimes();
    expect(mockServiceInstance.getServerConnection()).andReturn(mockServerConnection).anyTimes();
    expect(mockServiceInstance.getPerformanceManager()).andReturn(mockPerformanceManager).anyTimes();
    expect(mockServiceInstance.getAboutInfo()).andReturn(mockAboutInfo).anyTimes();
    managedEntity = new ManagedEntity(null, managedObjectReferenceManagedEntity);
    virtualMachine = new VirtualMachine(null, managedObjectReferenceVirtualMachine);
    hostSystem = new HostSystem(null, managedObjectReferenceHostSystem);
    // setup MorUtil
    mockStatic(MorUtil.class);
    expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceManagedEntity)).andReturn(managedEntity).anyTimes();
    expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceVirtualMachine)).andReturn(virtualMachine).anyTimes();
    expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceHostSystem)).andReturn(hostSystem).anyTimes();
    // setup about info
    expect(mockAboutInfo.getApiVersion()).andReturn("2.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("3.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("4.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("5.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("6.x");
    expect(mockAboutInfo.getApiVersion()).andReturn("x");
    // setup performance data
    int refreshRate = 100;
    int metricCount = 15;
    int instanceModifier = 5;
    perfQuerySpec = new PerfQuerySpec();
    perfQuerySpec.setEntity(managedEntity.getMOR());
    perfQuerySpec.setMaxSample(new Integer(1));
    perfQuerySpec.setIntervalId(refreshRate);
    perfEntityMetricBases = new PerfEntityMetricBase[metricCount];
    perfCounterInfos = new PerfCounterInfo[metricCount];
    for (int i = 0; i < metricCount; i++) {
        ElementDescription groupInfo = new ElementDescription();
        groupInfo.setKey("key" + i);
        ElementDescription nameInfo = new ElementDescription();
        nameInfo.setKey("name" + i);
        perfCounterInfos[i] = new PerfCounterInfo();
        perfCounterInfos[i].setKey(i);
        perfCounterInfos[i].setGroupInfo(groupInfo);
        perfCounterInfos[i].setNameInfo(nameInfo);
        perfCounterInfos[i].setRollupType(PerfSummaryType.average);
        perfEntityMetricBases[i] = new PerfEntityMetric();
        PerfMetricIntSeries[] perfMetricIntSeries;
        int instanceCount = (i % instanceModifier) + 1;
        perfMetricIntSeries = new PerfMetricIntSeries[instanceCount];
        for (int b = 0; b < instanceCount; b++) {
            PerfMetricId perfMetricId = new PerfMetricId();
            perfMetricId.setCounterId(i);
            if (instanceCount == 1) {
                perfMetricId.setInstance(null);
            } else {
                perfMetricId.setInstance("instance" + b);
            }
            perfMetricIntSeries[b] = new PerfMetricIntSeries();
            perfMetricIntSeries[b].setValue(new long[] { (long) 42 });
            perfMetricIntSeries[b].setId(perfMetricId);
        }
        ((PerfEntityMetric) perfEntityMetricBases[i]).setValue(perfMetricIntSeries);
    }
    // setup PerfProviderSummary
    mockPerfProviderSummary = createMock(PerfProviderSummary.class);
    expect(mockPerformanceManager.queryPerfProviderSummary(managedEntity)).andReturn(mockPerfProviderSummary).anyTimes();
    expect(mockPerfProviderSummary.getRefreshRate()).andReturn(refreshRate).anyTimes();
    expect(mockPerformanceManager.getPerfCounter()).andReturn(perfCounterInfos).anyTimes();
    expect(mockPerformanceManager.queryPerf(anyObject(PerfQuerySpec[].class))).andReturn(perfEntityMetricBases).anyTimes();
    // setup network info
    HostNetworkInfo hostNetworkInfo = new HostNetworkInfo();
    int numberOfVnics = 3;
    HostVirtualNic[] hostVirtualNics = new HostVirtualNic[numberOfVnics];
    for (int i = 0; i < numberOfVnics; i++) {
        HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
        HostIpConfig hostIpConfig = new HostIpConfig();
        hostIpConfig.setIpAddress("192.168.1." + (i + 1));
        hostVirtualNicSpec.setIp(hostIpConfig);
        hostVirtualNics[i] = new HostVirtualNic();
        hostVirtualNics[i].setSpec(hostVirtualNicSpec);
    }
    hostNetworkInfo.setVnic(hostVirtualNics);
    HostVirtualNic[] hostVirtualConsoleNics = new HostVirtualNic[numberOfVnics];
    for (int i = 0; i < numberOfVnics; i++) {
        HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
        HostIpConfig hostIpConfig = new HostIpConfig();
        hostIpConfig.setIpAddress("192.168.2." + (i + 1));
        hostVirtualNicSpec.setIp(hostIpConfig);
        hostVirtualConsoleNics[i] = new HostVirtualNic();
        hostVirtualConsoleNics[i].setSpec(hostVirtualNicSpec);
    }
    hostNetworkInfo.setConsoleVnic(hostVirtualConsoleNics);
    HostServiceTicket hostServiceTicket = new HostServiceTicket();
    hostServiceTicket.setSessionId("sessionId");
    // setup HostSystem
    mockHostSystem = createMock(HostSystem.class);
    // setup HostNetworkSystem
    mockHostNetworkSystem = createMock(HostNetworkSystem.class);
    // setup CIMClient
    mockCIMClient = createPartialMock(CIMClient.class, "enumerateInstances", "getSessionProperties");
    // setup the cim objects
    cimObjects = new ArrayList<>();
    int cimObjectCount = 5;
    for (int i = 0; i < cimObjectCount; i++) {
        CIMInstance cimInstance = new CIMInstance();
        cimInstance.setName("cimInstance" + i);
        cimObjects.add(cimInstance);
    }
    expect(mockHostSystem.getName()).andReturn("mockesxi01.local").anyTimes();
    expect(mockHostSystem.getHostNetworkSystem()).andReturn(mockHostNetworkSystem).anyTimes();
    expect(mockHostSystem.acquireCimServicesTicket()).andReturn(hostServiceTicket).anyTimes();
    expect(mockHostNetworkSystem.getNetworkInfo()).andReturn(hostNetworkInfo).anyTimes();
    expectNew(CIMClient.class, new Class<?>[] { CIMNameSpace.class, Principal.class, Object.class }, anyObject(), anyObject(), anyObject()).andReturn(mockCIMClient).anyTimes();
    suppress(method(CIMClient.class, "useMPost"));
    expect(mockCIMClient.enumerateInstances(new CIMObjectPath("cimClass"))).andReturn(Collections.enumeration(cimObjects)).anyTimes();
    SessionProperties sessionProperties = new SessionProperties();
    expect(mockCIMClient.getSessionProperties()).andReturn(sessionProperties).anyTimes();
}
Also used : HostVirtualNic(com.vmware.vim25.HostVirtualNic) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) CIMClient(org.sblim.wbem.client.CIMClient) HostIpConfig(com.vmware.vim25.HostIpConfig) HostVirtualNicSpec(com.vmware.vim25.HostVirtualNicSpec) ServiceInstance(com.vmware.vim25.mo.ServiceInstance) URL(java.net.URL) CIMInstance(org.sblim.wbem.cim.CIMInstance) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) PerformanceManager(com.vmware.vim25.mo.PerformanceManager) HostSystem(com.vmware.vim25.mo.HostSystem) HostServiceTicket(com.vmware.vim25.HostServiceTicket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) VmwareViJavaAccess(org.opennms.protocols.vmware.VmwareViJavaAccess) ElementDescription(com.vmware.vim25.ElementDescription) HostNetworkSystem(com.vmware.vim25.mo.HostNetworkSystem) ManagedEntity(com.vmware.vim25.mo.ManagedEntity) HostNetworkInfo(com.vmware.vim25.HostNetworkInfo) WSClient(com.vmware.vim25.ws.WSClient) PerfMetricId(com.vmware.vim25.PerfMetricId) AboutInfo(com.vmware.vim25.AboutInfo) CIMObjectPath(org.sblim.wbem.cim.CIMObjectPath) ServerConnection(com.vmware.vim25.mo.ServerConnection) VimPortType(com.vmware.vim25.VimPortType) SessionProperties(org.sblim.wbem.util.SessionProperties) PerfProviderSummary(com.vmware.vim25.PerfProviderSummary) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VirtualMachine(com.vmware.vim25.mo.VirtualMachine) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric) Before(org.junit.Before)

Example 63 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project jdk8u_jdk by JetBrains.

the class Fix5070632 method main.

public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSFacProvider = Security.getProperty("ssl.SocketFactory.provider");
    // use a non-existing provider so that the DefaultSSLSocketFactory
    // will be used, and then test against it.
    Security.setProperty("ssl.SocketFactory.provider", "foo.NonExistant");
    SSLSocketFactory fac = (SSLSocketFactory) SSLSocketFactory.getDefault();
    try {
        fac.createSocket();
    } catch (SocketException se) {
        // if exception caught, then it's ok
        System.out.println("Throw SocketException");
        se.printStackTrace();
        return;
    } finally {
        // restore the security properties
        if (reservedSFacProvider == null) {
            reservedSFacProvider = "";
        }
        Security.setProperty("ssl.SocketFactory.provider", reservedSFacProvider);
    }
    // if not caught, or other exception caught, then it's error
    throw new Exception("should throw SocketException");
}
Also used : SocketException(java.net.SocketException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketException(java.net.SocketException)

Example 64 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project spring-data-mongodb by spring-projects.

the class MongoOptionsFactoryBeanUnitTests method testSslConnection.

// DATAMONGO-764
@Test
public void testSslConnection() throws Exception {
    MongoClientOptionsFactoryBean bean = new MongoClientOptionsFactoryBean();
    bean.setSsl(true);
    bean.afterPropertiesSet();
    MongoClientOptions options = bean.getObject();
    assertNotNull(options.getSocketFactory());
    assertTrue(options.getSocketFactory() instanceof SSLSocketFactory);
}
Also used : MongoClientOptions(com.mongodb.MongoClientOptions) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 65 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project web3sdk by FISCO-BCOS.

the class CertificateManager method createSslSocket.

private static SSLSocket createSslSocket(URI endpoint, CertificateChainTrustManager certificateChainTrustManager) throws NoSuchAlgorithmException, KeyManagementException, IOException, URISyntaxException, KeyStoreException {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, new TrustManager[] { certificateChainTrustManager }, null);
    SSLSocketFactory factory = context.getSocketFactory();
    SSLSocket sslSocket = (SSLSocket) factory.createSocket(endpoint.getHost(), 443);
    sslSocket.setSoTimeout(10000);
    return sslSocket;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

SSLSocketFactory (javax.net.ssl.SSLSocketFactory)403 SSLContext (javax.net.ssl.SSLContext)150 SSLSocket (javax.net.ssl.SSLSocket)134 IOException (java.io.IOException)106 X509TrustManager (javax.net.ssl.X509TrustManager)69 Socket (java.net.Socket)63 TrustManager (javax.net.ssl.TrustManager)56 HostnameVerifier (javax.net.ssl.HostnameVerifier)49 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)48 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)48 Test (org.junit.Test)46 KeyManagementException (java.security.KeyManagementException)45 URL (java.net.URL)41 CertificateException (java.security.cert.CertificateException)39 OkHttpClient (okhttp3.OkHttpClient)39 OutputStream (java.io.OutputStream)35 InputStream (java.io.InputStream)34 X509Certificate (java.security.cert.X509Certificate)34 SSLSession (javax.net.ssl.SSLSession)30 InetSocketAddress (java.net.InetSocketAddress)29