Skip to content

Commit 119a199

Browse files
committed
Use a InsecureTrustManagerFactory that returns X509ExtendedTrustManagers, close AsyncHttpClient#1272
1 parent 299559d commit 119a199

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

client/src/main/java/org/asynchttpclient/netty/ssl/DefaultSslEngineFactory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import io.netty.handler.ssl.SslContext;
1818
import io.netty.handler.ssl.SslContextBuilder;
1919
import io.netty.handler.ssl.SslProvider;
20-
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
2120

2221
import javax.net.ssl.SSLEngine;
2322
import javax.net.ssl.SSLException;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2014 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package org.asynchttpclient.netty.ssl;
17+
18+
import io.netty.handler.ssl.util.SimpleTrustManagerFactory;
19+
import io.netty.util.internal.EmptyArrays;
20+
21+
import java.net.Socket;
22+
import java.security.KeyStore;
23+
import java.security.cert.X509Certificate;
24+
25+
import javax.net.ssl.ManagerFactoryParameters;
26+
import javax.net.ssl.SSLEngine;
27+
import javax.net.ssl.TrustManager;
28+
import javax.net.ssl.TrustManagerFactory;
29+
import javax.net.ssl.X509ExtendedTrustManager;
30+
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
//TODO: Replace this with Netty's InsecureTrustManager once it creates X509ExtendedTrustManager.
35+
//
36+
// When a server mandates the authentication of a client certificate, JDK internally wraps a TrustManager
37+
// with AbstractTrustManagerWrapper unless it extends X509ExtendedTrustManager. AbstractTrustManagerWrapper
38+
// performs an additional check (DN comparison), making InsecureTrustManager not insecure enough.
39+
//
40+
// To work around this problem, we forked Netty's InsecureTrustManagerFactory and made its TrustManager
41+
// implementation extend X509ExtendedTrustManager instead of X509TrustManager.
42+
// see https://github.com/netty/netty/issues/5910
43+
public final class InsecureTrustManagerFactory extends SimpleTrustManagerFactory {
44+
45+
private static final Logger logger = LoggerFactory.getLogger(InsecureTrustManagerFactory.class);
46+
47+
public static final TrustManagerFactory INSTANCE = new InsecureTrustManagerFactory();
48+
49+
private static final TrustManager tm = new X509ExtendedTrustManager() {
50+
@Override
51+
public void checkClientTrusted(X509Certificate[] chain, String s) {
52+
log("client", chain);
53+
}
54+
55+
@Override
56+
public void checkClientTrusted(X509Certificate[] chain, String s, Socket socket) {
57+
log("client", chain);
58+
}
59+
60+
@Override
61+
public void checkClientTrusted(X509Certificate[] chain, String s, SSLEngine sslEngine) {
62+
log("client", chain);
63+
}
64+
65+
@Override
66+
public void checkServerTrusted(X509Certificate[] chain, String s) {
67+
log("server", chain);
68+
}
69+
70+
@Override
71+
public void checkServerTrusted(X509Certificate[] chain, String s, Socket socket) {
72+
log("server", chain);
73+
}
74+
75+
@Override
76+
public void checkServerTrusted(X509Certificate[] chain, String s, SSLEngine sslEngine) {
77+
log("server", chain);
78+
}
79+
80+
@Override
81+
public X509Certificate[] getAcceptedIssuers() {
82+
return EmptyArrays.EMPTY_X509_CERTIFICATES;
83+
}
84+
85+
private void log(String type, X509Certificate[] chain) {
86+
logger.debug("Accepting a {} certificate: {}", type, chain[0].getSubjectDN());
87+
}
88+
};
89+
90+
private InsecureTrustManagerFactory() {
91+
}
92+
93+
@Override
94+
protected void engineInit(KeyStore keyStore) throws Exception {
95+
}
96+
97+
@Override
98+
protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception {
99+
}
100+
101+
@Override
102+
protected TrustManager[] engineGetTrustManagers() {
103+
return new TrustManager[] { tm };
104+
}
105+
}

client/src/main/java/org/asynchttpclient/netty/ssl/SslEngineFactoryBase.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ public abstract class SslEngineFactoryBase implements SslEngineFactory {
2525

2626
protected void configureSslEngine(SSLEngine sslEngine, AsyncHttpClientConfig config) {
2727
sslEngine.setUseClientMode(true);
28-
if (!config.isAcceptAnyCertificate()) {
29-
SSLParameters params = sslEngine.getSSLParameters();
30-
params.setEndpointIdentificationAlgorithm("HTTPS");
31-
sslEngine.setSSLParameters(params);
32-
}
28+
SSLParameters params = sslEngine.getSSLParameters();
29+
params.setEndpointIdentificationAlgorithm("HTTPS");
30+
sslEngine.setSSLParameters(params);
3331

3432
if (isNonEmpty(config.getEnabledProtocols()))
3533
sslEngine.setEnabledProtocols(config.getEnabledProtocols());

0 commit comments

Comments
 (0)