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