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
+ }
0 commit comments