1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.net.ssl;
15
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19
20 import javax.net.ssl.SSLEngine;
21
22 import org.codehaus.janino.Java;
23
24 import ch.qos.logback.core.spi.ContextAwareBase;
25 import ch.qos.logback.core.util.OptionHelper;
26 import ch.qos.logback.core.util.StringCollectionUtil;
27
28
29
30
31
32
33
34 public class SSLParametersConfiguration extends ContextAwareBase {
35
36 private String includedProtocols;
37 private String excludedProtocols;
38 private String includedCipherSuites;
39 private String excludedCipherSuites;
40 private Boolean needClientAuth;
41 private Boolean wantClientAuth;
42 private String[] enabledProtocols;
43 private String[] enabledCipherSuites;
44 private Boolean hostnameVerification;
45
46
47
48
49
50
51 public void configure(SSLConfigurable socket) {
52 socket.setEnabledProtocols(enabledProtocols(socket.getSupportedProtocols(), socket.getDefaultProtocols()));
53 socket.setEnabledCipherSuites(
54 enabledCipherSuites(socket.getSupportedCipherSuites(), socket.getDefaultCipherSuites()));
55 if (isNeedClientAuth() != null) {
56 socket.setNeedClientAuth(isNeedClientAuth());
57 }
58 if (isWantClientAuth() != null) {
59 socket.setWantClientAuth(isWantClientAuth());
60 }
61 if (hostnameVerification != null) {
62 addInfo("hostnameVerification=" + hostnameVerification);
63 socket.setHostnameVerification(hostnameVerification);
64 }
65 }
66
67 public boolean getHostnameVerification() {
68 if (hostnameVerification == null)
69 return false;
70 return hostnameVerification;
71 }
72
73 public void setHostnameVerification(boolean hostnameVerification) {
74 this.hostnameVerification = hostnameVerification;
75 }
76
77
78
79
80
81
82
83
84 private String[] enabledProtocols(String[] supportedProtocols, String[] defaultProtocols) {
85 if (enabledProtocols == null) {
86
87
88 if (OptionHelper.isNullOrEmptyOrAllSpaces(getIncludedProtocols())
89 && OptionHelper.isNullOrEmptyOrAllSpaces(getExcludedProtocols())) {
90 enabledProtocols = Arrays.copyOf(defaultProtocols, defaultProtocols.length);
91 } else {
92 enabledProtocols = includedStrings(supportedProtocols, getIncludedProtocols(), getExcludedProtocols());
93 }
94 for (String protocol : enabledProtocols) {
95 addInfo("enabled protocol: " + protocol);
96 }
97 }
98 return enabledProtocols;
99 }
100
101
102
103
104
105
106
107
108 private String[] enabledCipherSuites(String[] supportedCipherSuites, String[] defaultCipherSuites) {
109 if (enabledCipherSuites == null) {
110
111
112 if (OptionHelper.isNullOrEmptyOrAllSpaces(getIncludedCipherSuites())
113 && OptionHelper.isNullOrEmptyOrAllSpaces(getExcludedCipherSuites())) {
114 enabledCipherSuites = Arrays.copyOf(defaultCipherSuites, defaultCipherSuites.length);
115 } else {
116 enabledCipherSuites = includedStrings(supportedCipherSuites, getIncludedCipherSuites(),
117 getExcludedCipherSuites());
118 }
119 for (String cipherSuite : enabledCipherSuites) {
120 addInfo("enabled cipher suite: " + cipherSuite);
121 }
122 }
123 return enabledCipherSuites;
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137 private String[] includedStrings(String[] defaults, String included, String excluded) {
138 List<String> values = new ArrayList<String>(defaults.length);
139 values.addAll(Arrays.asList(defaults));
140 if (included != null) {
141 StringCollectionUtil.retainMatching(values, stringToArray(included));
142 }
143 if (excluded != null) {
144 StringCollectionUtil.removeMatching(values, stringToArray(excluded));
145 }
146 return values.toArray(new String[values.size()]);
147 }
148
149
150
151
152
153
154
155 private String[] stringToArray(String s) {
156 return s.split("\\s*,\\s*");
157 }
158
159
160
161
162
163
164
165 public String getIncludedProtocols() {
166 return includedProtocols;
167 }
168
169
170
171
172
173
174
175
176 public void setIncludedProtocols(String protocols) {
177 this.includedProtocols = protocols;
178 }
179
180
181
182
183
184
185
186 public String getExcludedProtocols() {
187 return excludedProtocols;
188 }
189
190
191
192
193
194
195
196
197 public void setExcludedProtocols(String protocols) {
198 this.excludedProtocols = protocols;
199 }
200
201
202
203
204
205
206
207 public String getIncludedCipherSuites() {
208 return includedCipherSuites;
209 }
210
211
212
213
214
215
216
217
218 public void setIncludedCipherSuites(String cipherSuites) {
219 this.includedCipherSuites = cipherSuites;
220 }
221
222
223
224
225
226
227
228 public String getExcludedCipherSuites() {
229 return excludedCipherSuites;
230 }
231
232
233
234
235
236
237
238
239 public void setExcludedCipherSuites(String cipherSuites) {
240 this.excludedCipherSuites = cipherSuites;
241 }
242
243
244
245
246
247
248 public Boolean isNeedClientAuth() {
249 return needClientAuth;
250 }
251
252
253
254
255
256
257 public void setNeedClientAuth(Boolean needClientAuth) {
258 this.needClientAuth = needClientAuth;
259 }
260
261
262
263
264
265
266 public Boolean isWantClientAuth() {
267 return wantClientAuth;
268 }
269
270
271
272
273
274
275 public void setWantClientAuth(Boolean wantClientAuth) {
276 this.wantClientAuth = wantClientAuth;
277 }
278
279 }