Search in sources :

Example 86 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project xian by happyyangyuan.

the class Https method getSslSocketFactory.

public static SSLSocketFactory getSslSocketFactory(InputStream cerIn, String storePass) {
    SSLSocketFactory sslSocketFactory = null;
    try {
        TrustManager[] trustManagers = prepareTrustManager(cerIn, storePass);
        X509TrustManager manager;
        // 优先使用自定义的证书管理器
        if (trustManagers != null) {
            manager = chooseTrustManager(trustManagers);
            LOG.debug("---https访问,使用自定义证书---");
        } else {
            // 否则使用无证书认证的证书管理器
            manager = UnSafeTrustManager;
            LOG.debug("---https访问,无证书---");
        }
        // 创建TLS类型的SSLContext对象
        SSLContext sslContext = SSLContext.getInstance("TLS");
        // 用上面得到的trustManagers初始化SSLContext,这样sslContext就会信任keyStore中的证书
        // 第一个参数是授权的密钥管理器,用来授权验证,比如授权自签名的证书验证。第二个是被授权的证书管理器,用来验证服务器端的证书
        sslContext.init(null, new TrustManager[] { manager }, null);
        // 通过sslContext获取SSLSocketFactory对象
        sslSocketFactory = sslContext.getSocketFactory();
        return sslSocketFactory;
    } catch (Exception e) {
        // LOG.error("--证书加载出错-", e);
        throw new RuntimeException("证书信息加载错误");
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 87 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project Pix-Art-Messenger by kriztan.

the class XmppConnection method getTlsFactoryVerifier.

private TlsFactoryVerifier getTlsFactoryVerifier() throws NoSuchAlgorithmException, KeyManagementException, IOException {
    final SSLContext sc = SSLSocketHelper.getSSLContext();
    MemorizingTrustManager trustManager = this.mXmppConnectionService.getMemorizingTrustManager();
    KeyManager[] keyManager;
    if (account.getPrivateKeyAlias() != null && account.getPassword().isEmpty()) {
        keyManager = new KeyManager[] { new MyKeyManager() };
    } else {
        keyManager = null;
    }
    String domain = account.getJid().getDomainpart();
    sc.init(keyManager, new X509TrustManager[] { mInteractive ? trustManager.getInteractive(domain) : trustManager.getNonInteractive(domain) }, mXmppConnectionService.getRNG());
    final SSLSocketFactory factory = sc.getSocketFactory();
    final DomainHostnameVerifier verifier = trustManager.wrapHostnameVerifier(new XmppDomainVerifier(), mInteractive);
    return new TlsFactoryVerifier(factory, verifier);
}
Also used : MemorizingTrustManager(de.pixart.messenger.services.MemorizingTrustManager) XmppDomainVerifier(de.pixart.messenger.crypto.XmppDomainVerifier) DomainHostnameVerifier(de.pixart.messenger.crypto.DomainHostnameVerifier) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager)

Example 88 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project pwm by pwm-project.

the class X509Utils method testIfLdapServerCertsInDefaultKeystore.

public static boolean testIfLdapServerCertsInDefaultKeystore(final URI serverURI) {
    final String ldapHost = serverURI.getHost();
    final int ldapPort = serverURI.getPort();
    try {
        // use default socket factory to test if certs work with it
        final SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        final SSLSocket sslSock = (SSLSocket) factory.createSocket(ldapHost, ldapPort);
        if (!sslSock.isConnected()) {
            throw PwmUnrecoverableException.newException(PwmError.ERROR_CERTIFICATE_ERROR, "unable to connect to " + serverURI);
        }
        try (OutputStream outputStream = sslSock.getOutputStream()) {
            outputStream.write("data!".getBytes(PwmConstants.DEFAULT_CHARSET));
        }
        sslSock.close();
        return true;
    } catch (Exception e) {
        LOGGER.trace("exception while testing ldap server cert validity against default keystore: " + e.getMessage());
    }
    return false;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmOperationalException(password.pwm.error.PwmOperationalException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 89 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method registerSchemes.

private synchronized void registerSchemes(ISocketEventSource source, ISocketListener socketListener) {
    SchemeRegistry schemeRegistry = this.httpClient.getConnectionManager().getSchemeRegistry();
    Scheme http = new Scheme(HttpClientRetrieveFileTransfer.HTTP, HTTP_PORT, new ECFHttpClientProtocolSocketFactory(SocketFactory.getDefault(), source, socketListener));
    // $NON-NLS-1$
    Trace.trace(Activator.PLUGIN_ID, "registering http scheme");
    schemeRegistry.register(http);
    ISSLSocketFactoryModifier sslSocketFactoryModifier = Activator.getDefault().getSSLSocketFactoryModifier();
    if (sslSocketFactoryModifier == null) {
        sslSocketFactoryModifier = new HttpClientDefaultSSLSocketFactoryModifier();
    }
    SSLSocketFactory sslSocketFactory = null;
    try {
        sslSocketFactory = sslSocketFactoryModifier.getSSLSocketFactory();
    } catch (IOException e) {
        // $NON-NLS-1$
        Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, ISSLSocketFactoryModifier.class, "getSSLSocketFactory()", e);
        // $NON-NLS-1$
        Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, HttpClientRetrieveFileTransfer.class, "registerSchemes()", e);
        // $NON-NLS-1$
        throw new ECFRuntimeException("Unable to instantiate schemes for HttpClient.", e);
    }
    Scheme https = new Scheme(HttpClientRetrieveFileTransfer.HTTPS, HTTPS_PORT, new ECFHttpClientSecureProtocolSocketFactory(sslSocketFactory, source, socketListener));
    // $NON-NLS-1$
    Trace.trace(Activator.PLUGIN_ID, "registering https scheme; modifier=" + sslSocketFactoryModifier);
    schemeRegistry.register(https);
    // SPNEGO is not supported, so remove it from the list
    List authpref = new ArrayList(3);
    authpref.add(AuthPolicy.NTLM);
    authpref.add(AuthPolicy.DIGEST);
    authpref.add(AuthPolicy.BASIC);
    httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
    httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref);
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) ArrayList(java.util.ArrayList) ECFHttpClientProtocolSocketFactory(org.eclipse.ecf.internal.provider.filetransfer.httpclient4.ECFHttpClientProtocolSocketFactory) ISSLSocketFactoryModifier(org.eclipse.ecf.internal.provider.filetransfer.httpclient4.ISSLSocketFactoryModifier) ECFHttpClientSecureProtocolSocketFactory(org.eclipse.ecf.internal.provider.filetransfer.httpclient4.ECFHttpClientSecureProtocolSocketFactory) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) ECFRuntimeException(org.eclipse.ecf.core.util.ECFRuntimeException)

Example 90 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project ecf by eclipse.

the class ECFURLConnectionModifier method getSSLSocketFactory.

private SSLSocketFactory getSSLSocketFactory() {
    if (context == null)
        return null;
    if (sslSocketFactoryTracker == null) {
        sslSocketFactoryTracker = new ServiceTracker(this.context, SSLSocketFactory.class.getName(), null);
        sslSocketFactoryTracker.open();
    }
    return (SSLSocketFactory) sslSocketFactoryTracker.getService();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) 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