15
15
*/
16
16
package org .asynchttpclient .util ;
17
17
18
- import javax .net .ssl .KeyManager ;
19
- import javax .net .ssl .KeyManagerFactory ;
20
18
import javax .net .ssl .SSLContext ;
21
19
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 ;
27
20
import java .io .IOException ;
28
- import java .io .InputStream ;
29
21
import java .security .GeneralSecurityException ;
30
- import java .security .KeyStore ;
31
- import java .security .SecureRandom ;
32
- import java .security .Security ;
33
22
34
23
/**
35
24
* 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
36
25
*/
37
26
public class SslUtils {
38
27
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
+ }
40
35
41
- public static SSLEngine getSSLEngine () throws GeneralSecurityException , IOException {
36
+ public SSLEngine getSSLEngine () throws GeneralSecurityException , IOException {
42
37
SSLEngine engine = null ;
43
38
44
39
SSLContext context = getSSLContext ();
@@ -50,117 +45,8 @@ public static SSLEngine getSSLEngine() throws GeneralSecurityException, IOExcept
50
45
return engine ;
51
46
}
52
47
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 ();
164
50
}
165
51
166
52
}
0 commit comments