Skip to content

Commit 0347bec

Browse files
wsargentStephane Landelle
authored and
Stephane Landelle
committed
Ensure certificate verification by using a singleton.
1 parent 710a19f commit 0347bec

File tree

3 files changed

+12
-126
lines changed

3 files changed

+12
-126
lines changed

api/src/main/java/org/asynchttpclient/util/SslUtils.java

Lines changed: 10 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,25 @@
1515
*/
1616
package org.asynchttpclient.util;
1717

18-
import javax.net.ssl.KeyManager;
19-
import javax.net.ssl.KeyManagerFactory;
2018
import javax.net.ssl.SSLContext;
2119
import javax.net.ssl.SSLEngine;
22-
import javax.net.ssl.TrustManager;
23-
import javax.net.ssl.TrustManagerFactory;
24-
import javax.net.ssl.X509TrustManager;
25-
26-
import java.io.FileInputStream;
2720
import java.io.IOException;
28-
import java.io.InputStream;
2921
import java.security.GeneralSecurityException;
30-
import java.security.KeyStore;
31-
import java.security.SecureRandom;
32-
import java.security.Security;
3322

3423
/**
3524
* This class is a copy of http://github.com/sonatype/wagon-ning/raw/master/src/main/java/org/apache/maven/wagon/providers/http/SslUtils.java
3625
*/
3726
public class SslUtils {
3827

39-
private static SSLContext context = null;
28+
private static class SingletonHolder {
29+
public static final SslUtils instance = new SslUtils();
30+
}
31+
32+
public static SslUtils getInstance() {
33+
return SingletonHolder.instance;
34+
}
4035

41-
public static SSLEngine getSSLEngine() throws GeneralSecurityException, IOException {
36+
public SSLEngine getSSLEngine() throws GeneralSecurityException, IOException {
4237
SSLEngine engine = null;
4338

4439
SSLContext context = getSSLContext();
@@ -50,117 +45,8 @@ public static SSLEngine getSSLEngine() throws GeneralSecurityException, IOExcept
5045
return engine;
5146
}
5247

53-
public static SSLContext getSSLContext() throws GeneralSecurityException, IOException {
54-
if (context == null) {
55-
SSLConfig config = new SSLConfig();
56-
if (config.keyStoreLocation == null || config.trustStoreLocation == null) {
57-
context = getLooseSSLContext();
58-
} else {
59-
context = getStrictSSLContext(config);
60-
}
61-
}
62-
return context;
63-
}
64-
65-
static SSLContext getStrictSSLContext(SSLConfig config) throws GeneralSecurityException, IOException {
66-
KeyStore keyStore = KeyStore.getInstance(config.keyStoreType);
67-
InputStream keystoreInputStream = new FileInputStream(config.keyStoreLocation);
68-
try {
69-
keyStore.load(keystoreInputStream, (config.keyStorePassword == null) ? null : config.keyStorePassword.toCharArray());
70-
} finally {
71-
keystoreInputStream.close();
72-
}
73-
74-
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(config.keyManagerAlgorithm);
75-
keyManagerFactory.init(keyStore, (config.keyManagerPassword == null) ? null : config.keyManagerPassword.toCharArray());
76-
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
77-
78-
KeyStore trustStore = KeyStore.getInstance(config.trustStoreType);
79-
InputStream truststoreInputStream = new FileInputStream(config.trustStoreLocation);
80-
try {
81-
trustStore.load(truststoreInputStream, (config.trustStorePassword == null) ? null : config.trustStorePassword.toCharArray());
82-
} finally {
83-
truststoreInputStream.close();
84-
}
85-
86-
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(config.trustManagerAlgorithm);
87-
trustManagerFactory.init(trustStore);
88-
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
89-
90-
SSLContext context = SSLContext.getInstance("TLS");
91-
context.init(keyManagers, trustManagers, null);
92-
93-
return context;
94-
}
95-
96-
static SSLContext getLooseSSLContext() throws GeneralSecurityException {
97-
SSLContext sslContext = SSLContext.getInstance("TLS");
98-
sslContext.init(null, new TrustManager[] { LooseTrustManager.INSTANCE }, new SecureRandom());
99-
return sslContext;
100-
}
101-
102-
static class LooseTrustManager implements X509TrustManager {
103-
104-
public static final LooseTrustManager INSTANCE = new LooseTrustManager();
105-
106-
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
107-
return new java.security.cert.X509Certificate[0];
108-
}
109-
110-
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
111-
}
112-
113-
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
114-
}
115-
}
116-
117-
private final static class SSLConfig {
118-
119-
public String keyStoreLocation;
120-
121-
public String keyStoreType = "JKS";
122-
123-
public String keyStorePassword = "changeit";
124-
125-
public String keyManagerAlgorithm = "SunX509";
126-
127-
public String keyManagerPassword = "changeit";
128-
129-
public String trustStoreLocation;
130-
131-
public String trustStoreType = "JKS";
132-
133-
public String trustStorePassword = "changeit";
134-
135-
public String trustManagerAlgorithm = "SunX509";
136-
137-
public SSLConfig() {
138-
keyStoreLocation = System.getProperty("javax.net.ssl.keyStore");
139-
keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword", "changeit");
140-
keyStoreType = System.getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType());
141-
keyManagerAlgorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
142-
143-
if (keyManagerAlgorithm == null) {
144-
keyManagerAlgorithm = "SunX509";
145-
}
146-
147-
keyManagerPassword = System.getProperty("javax.net.ssl.keyStorePassword", "changeit");
148-
149-
trustStoreLocation = System.getProperty("javax.net.ssl.trustStore");
150-
if (trustStoreLocation == null) {
151-
trustStoreLocation = keyStoreLocation;
152-
trustStorePassword = keyStorePassword;
153-
trustStoreType = keyStoreType;
154-
} else {
155-
trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword", "changeit");
156-
trustStoreType = System.getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType());
157-
}
158-
trustManagerAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
159-
160-
if (trustManagerAlgorithm == null) {
161-
trustManagerAlgorithm = "SunX509";
162-
}
163-
}
48+
public SSLContext getSSLContext() throws GeneralSecurityException, IOException {
49+
return SSLContext.getDefault();
16450
}
16551

16652
}

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public void onTimeout(Connection connection) {
252252
SSLContext context = clientConfig.getSSLContext();
253253
if (context == null) {
254254
try {
255-
context = SslUtils.getSSLContext();
255+
context = SslUtils.getInstance().getSSLContext();
256256
} catch (Exception e) {
257257
throw new IllegalStateException(e);
258258
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/channel/Channels.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private Timer newNettyTimer() {
204204
private SSLEngine createSSLEngine() throws IOException, GeneralSecurityException {
205205
SSLEngine sslEngine = config.getSSLEngineFactory().newSSLEngine();
206206
if (sslEngine == null) {
207-
sslEngine = SslUtils.getSSLEngine();
207+
sslEngine = SslUtils.getInstance().getSSLEngine();
208208
}
209209
return sslEngine;
210210
}

0 commit comments

Comments
 (0)