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("证书信息加载错误");
}
}
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);
}
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;
}
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);
}
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();
}
Aggregations