-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
TrustManagerUtils.java
645 lines (540 loc) 路 31.8 KB
/
TrustManagerUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
* Copyright 2019 Thunderberry.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.altindag.ssl.util;
import nl.altindag.ssl.exception.GenericTrustManagerException;
import nl.altindag.ssl.model.TrustManagerParameters;
import nl.altindag.ssl.trustmanager.CertificateCapturingX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.CompositeX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.DelegatingX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.DummyX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.EnhanceableX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.HotSwappableX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.InflatableX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.JdkX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.LoggingX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.SystemX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.TrustManagerFactoryWrapper;
import nl.altindag.ssl.trustmanager.UnsafeX509ExtendedTrustManager;
import nl.altindag.ssl.trustmanager.X509TrustManagerWrapper;
import nl.altindag.ssl.trustmanager.validator.ChainAndAuthTypeValidator;
import nl.altindag.ssl.trustmanager.validator.ChainAndAuthTypeWithSSLEngineValidator;
import nl.altindag.ssl.trustmanager.validator.ChainAndAuthTypeWithSocketValidator;
import nl.altindag.ssl.util.internal.CollectorsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509ExtendedTrustManager;
import javax.net.ssl.X509TrustManager;
import java.nio.file.Path;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import static nl.altindag.ssl.util.internal.CollectorsUtils.toListAndThen;
import static nl.altindag.ssl.util.internal.ValidationUtils.requireNotEmpty;
/**
* @author Hakan Altindag
*/
public final class TrustManagerUtils {
private TrustManagerUtils() {}
public static X509ExtendedTrustManager combine(X509TrustManager... trustManagers) {
return combine(Arrays.asList(trustManagers));
}
public static X509ExtendedTrustManager combine(List<? extends X509TrustManager> trustManagers) {
return TrustManagerUtils.trustManagerBuilder()
.withTrustManagers(trustManagers)
.build();
}
public static <T extends X509TrustManager> X509ExtendedTrustManager[] toArray(T trustManager) {
return new X509ExtendedTrustManager[]{TrustManagerUtils.wrapIfNeeded(trustManager)};
}
public static X509ExtendedTrustManager createTrustManagerWithJdkTrustedCertificates() {
return new JdkX509ExtendedTrustManager(createTrustManager((KeyStore) null));
}
public static Optional<X509ExtendedTrustManager> createTrustManagerWithSystemTrustedCertificates() {
List<KeyStore> trustStores = KeyStoreUtils.loadSystemKeyStores();
if (trustStores.isEmpty()) {
return Optional.empty();
}
X509ExtendedTrustManager trustManager = createTrustManager(trustStores.toArray(new KeyStore[]{}));
return Optional.of(new SystemX509ExtendedTrustManager(trustManager, OperatingSystem.get().name().toLowerCase()));
}
public static X509ExtendedTrustManager createTrustManager(List<X509Certificate> certificates) {
KeyStore trustStore = KeyStoreUtils.createTrustStore(certificates);
return TrustManagerUtils.createTrustManager(trustStore);
}
public static X509ExtendedTrustManager createTrustManager(KeyStore... trustStores) {
return Arrays.stream(trustStores)
.map(TrustManagerUtils::createTrustManager)
.collect(toListAndThen(TrustManagerUtils::combine));
}
public static X509ExtendedTrustManager createTrustManager(KeyStore trustStore) {
return createTrustManager(trustStore, TrustManagerFactory.getDefaultAlgorithm());
}
public static X509ExtendedTrustManager createTrustManager(KeyStore trustStore, String trustManagerFactoryAlgorithm) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm);
return createTrustManager(trustStore, trustManagerFactory);
} catch (NoSuchAlgorithmException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createTrustManager(KeyStore trustStore, String trustManagerFactoryAlgorithm, String securityProviderName) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm, securityProviderName);
return createTrustManager(trustStore, trustManagerFactory);
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createTrustManager(KeyStore trustStore, String trustManagerFactoryAlgorithm, Provider securityProvider) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm, securityProvider);
return createTrustManager(trustStore, trustManagerFactory);
} catch (NoSuchAlgorithmException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createTrustManager(KeyStore trustStore, TrustManagerFactory trustManagerFactory) {
try {
trustManagerFactory.init(trustStore);
return TrustManagerUtils.getTrustManager(trustManagerFactory);
} catch (KeyStoreException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createTrustManager(ManagerFactoryParameters... managerFactoryParameters) {
return Arrays.stream(managerFactoryParameters)
.map(TrustManagerUtils::createTrustManager)
.collect(toListAndThen(TrustManagerUtils::combine));
}
public static X509ExtendedTrustManager createTrustManager(ManagerFactoryParameters managerFactoryParameters) {
return createTrustManager(managerFactoryParameters, TrustManagerFactory.getDefaultAlgorithm());
}
public static X509ExtendedTrustManager createTrustManager(ManagerFactoryParameters managerFactoryParameters, String trustManagerFactoryAlgorithm) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm);
return createTrustManager(managerFactoryParameters, trustManagerFactory);
} catch (NoSuchAlgorithmException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createTrustManager(ManagerFactoryParameters managerFactoryParameters, String trustManagerFactoryAlgorithm, String securityProviderName) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm, securityProviderName);
return createTrustManager(managerFactoryParameters, trustManagerFactory);
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createTrustManager(ManagerFactoryParameters managerFactoryParameters, String trustManagerFactoryAlgorithm, Provider securityProvider) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm, securityProvider);
return createTrustManager(managerFactoryParameters, trustManagerFactory);
} catch (NoSuchAlgorithmException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createTrustManager(ManagerFactoryParameters managerFactoryParameters, TrustManagerFactory trustManagerFactory) {
try {
trustManagerFactory.init(managerFactoryParameters);
return TrustManagerUtils.getTrustManager(trustManagerFactory);
} catch (InvalidAlgorithmParameterException e) {
throw new GenericTrustManagerException(e);
}
}
public static X509ExtendedTrustManager createUnsafeTrustManager() {
return UnsafeX509ExtendedTrustManager.getInstance();
}
public static X509ExtendedTrustManager createDummyTrustManager() {
return DummyX509ExtendedTrustManager.getInstance();
}
public static X509ExtendedTrustManager createLoggingTrustManager(X509TrustManager baseTrustManager) {
return new LoggingX509ExtendedTrustManager(wrapIfNeeded(baseTrustManager));
}
public static X509ExtendedTrustManager createCertificateCapturingTrustManager(List<X509Certificate> certificatesCollector) {
return createCertificateCapturingTrustManager(TrustManagerUtils.createUnsafeTrustManager(), certificatesCollector);
}
public static X509ExtendedTrustManager createCertificateCapturingTrustManager(X509TrustManager baseTrustManager, List<X509Certificate> certificatesCollector) {
return new CertificateCapturingX509ExtendedTrustManager(wrapIfNeeded(baseTrustManager), certificatesCollector);
}
public static X509ExtendedTrustManager wrapIfNeeded(X509TrustManager trustManager) {
if (trustManager instanceof X509ExtendedTrustManager) {
return (X509ExtendedTrustManager) trustManager;
} else {
return new X509TrustManagerWrapper(trustManager);
}
}
public static TrustManagerFactory createTrustManagerFactory(TrustManager trustManager) {
return new TrustManagerFactoryWrapper(trustManager);
}
public static <T extends TrustManagerFactory> X509ExtendedTrustManager getTrustManager(T trustManagerFactory) {
return Arrays.stream(trustManagerFactory.getTrustManagers())
.filter(X509TrustManager.class::isInstance)
.map(X509TrustManager.class::cast)
.map(TrustManagerUtils::wrapIfNeeded)
.collect(Collectors.collectingAndThen(Collectors.toList(), TrustManagerUtils::combine));
}
public static X509ExtendedTrustManager createInflatableTrustManager() {
return new InflatableX509ExtendedTrustManager();
}
@Deprecated
public static X509ExtendedTrustManager createInflatableTrustManager(Path trustStorePath,
char[] trustStorePassword,
String trustStoreType,
BiPredicate<X509Certificate[], String> certificateAndAuthTypeTrustPredicate) {
return new InflatableX509ExtendedTrustManager(
trustStorePath,
trustStorePassword,
trustStoreType,
trustManagerParameters -> certificateAndAuthTypeTrustPredicate.test(trustManagerParameters.getChain(), trustManagerParameters.getAuthType()));
}
public static X509ExtendedTrustManager createInflatableTrustManager(Path trustStorePath,
char[] trustStorePassword,
String trustStoreType,
Predicate<TrustManagerParameters> trustManagerParametersPredicate) {
return new InflatableX509ExtendedTrustManager(trustStorePath, trustStorePassword, trustStoreType, trustManagerParametersPredicate);
}
/**
* Adds a new to be trusted certificate to the existing TrustManager.
* The provided TrustManager should be an instance of {@link InflatableX509ExtendedTrustManager}
* and it is allowed that it is wrapped in a {@link CompositeX509ExtendedTrustManager}
*/
public static void addCertificate(X509ExtendedTrustManager trustManager, X509Certificate certificate) {
addCertificate(trustManager, Collections.singletonList(certificate));
}
/**
* Adds a new to be trusted certificate to the existing TrustManager.
* The provided TrustManager should be an instance of {@link InflatableX509ExtendedTrustManager}
* and it is allowed that it is wrapped in a {@link CompositeX509ExtendedTrustManager}
*/
public static void addCertificate(X509ExtendedTrustManager trustManager, List<X509Certificate> certificates) {
boolean certificateAdded = addCertificateIfPossible(trustManager, certificates);
if (certificateAdded) {
return;
}
throw new GenericTrustManagerException(
String.format("The provided trustManager should be an instance of [%s]", InflatableX509ExtendedTrustManager.class.getName())
);
}
private static boolean addCertificateIfPossible(X509ExtendedTrustManager trustManager, List<X509Certificate> certificates) {
if (trustManager instanceof InflatableX509ExtendedTrustManager) {
((InflatableX509ExtendedTrustManager) trustManager).addCertificates(certificates);
return true;
}
if (trustManager instanceof DelegatingX509ExtendedTrustManager) {
X509ExtendedTrustManager innerTrustManager = ((DelegatingX509ExtendedTrustManager) trustManager).getInnerTrustManager();
return addCertificateIfPossible(innerTrustManager, certificates);
}
if (trustManager instanceof CompositeX509ExtendedTrustManager) {
List<X509ExtendedTrustManager> innerTrustManagers = ((CompositeX509ExtendedTrustManager) trustManager).getInnerTrustManagers();
Optional<InflatableX509ExtendedTrustManager> inflatableX509ExtendedTrustManager = innerTrustManagers.stream()
.filter(InflatableX509ExtendedTrustManager.class::isInstance)
.map(InflatableX509ExtendedTrustManager.class::cast)
.findFirst();
if (inflatableX509ExtendedTrustManager.isPresent()) {
return addCertificateIfPossible(inflatableX509ExtendedTrustManager.get(), certificates);
}
}
return false;
}
/**
* Wraps the given TrustManager into an instance of a Hot Swappable TrustManager.
* This type of TrustManager has the capability of swapping in and out different TrustManagers at runtime.
*
* @param trustManager To be wrapped TrustManager
* @return Swappable TrustManager
*/
public static X509ExtendedTrustManager createSwappableTrustManager(X509TrustManager trustManager) {
return new HotSwappableX509ExtendedTrustManager(TrustManagerUtils.wrapIfNeeded(trustManager));
}
/**
* Swaps the internal TrustManager instance with the given trustManager object.
* The baseTrustManager should be an instance of {@link HotSwappableX509ExtendedTrustManager}
* and can be created with {@link TrustManagerUtils#createSwappableTrustManager(X509TrustManager)}
*
* @param baseTrustManager an instance of {@link HotSwappableX509ExtendedTrustManager}
* @param newTrustManager to be injected instance of a TrustManager
* @throws GenericTrustManagerException if {@code baseTrustManager} is not instance of {@link HotSwappableX509ExtendedTrustManager}
*/
public static void swapTrustManager(X509TrustManager baseTrustManager, X509TrustManager newTrustManager) {
if (baseTrustManager instanceof InflatableX509ExtendedTrustManager) {
throw new GenericTrustManagerException(
String.format("The baseTrustManager is from the instance of [%s] and should be an instance of [%s].",
baseTrustManager.getClass().getName(),
HotSwappableX509ExtendedTrustManager.class.getName())
);
}
if (!(baseTrustManager instanceof HotSwappableX509ExtendedTrustManager)) {
throw new GenericTrustManagerException(
String.format("The baseTrustManager is from the instance of [%s] and should be an instance of [%s].",
baseTrustManager.getClass().getName(),
HotSwappableX509ExtendedTrustManager.class.getName())
);
}
if (newTrustManager instanceof HotSwappableX509ExtendedTrustManager
&& !(newTrustManager instanceof InflatableX509ExtendedTrustManager)) {
throw new GenericTrustManagerException(
String.format("The newTrustManager should not be an instance of [%s]", HotSwappableX509ExtendedTrustManager.class.getName())
);
}
HotSwappableX509ExtendedTrustManager swappableTrustManager = (HotSwappableX509ExtendedTrustManager) baseTrustManager;
X509ExtendedTrustManager innerTrustManager = swappableTrustManager.getInnerTrustManager();
List<UnaryOperator<X509ExtendedTrustManager>> mappers = new ArrayList<>();
computeMappersForNewTrustManager(innerTrustManager, mappers);
Collections.reverse(mappers);
X509ExtendedTrustManager resolvedNewTrustManager = TrustManagerUtils.wrapIfNeeded(newTrustManager);
for (UnaryOperator<X509ExtendedTrustManager> mapper : mappers) {
resolvedNewTrustManager = mapper.apply(resolvedNewTrustManager);
}
swappableTrustManager.setTrustManager(resolvedNewTrustManager);
}
private static void computeMappersForNewTrustManager(X509ExtendedTrustManager trustManager, List<UnaryOperator<X509ExtendedTrustManager>> mappers) {
if (trustManager instanceof LoggingX509ExtendedTrustManager) {
LoggingX509ExtendedTrustManager loggingTrustManager = (LoggingX509ExtendedTrustManager) trustManager;
mappers.add(LoggingX509ExtendedTrustManager::new);
computeMappersForNewTrustManager(loggingTrustManager.getInnerTrustManager(), mappers);
}
if (trustManager instanceof EnhanceableX509ExtendedTrustManager) {
EnhanceableX509ExtendedTrustManager existingEnhanceableTrustManager = (EnhanceableX509ExtendedTrustManager) trustManager;
mappers.add(newTrustManager ->
new EnhanceableX509ExtendedTrustManager(
TrustManagerUtils.wrapIfNeeded(newTrustManager),
existingEnhanceableTrustManager.getTrustManagerParametersValidator(),
existingEnhanceableTrustManager.isTrustedCertificatesConcealed()
)
);
computeMappersForNewTrustManager(existingEnhanceableTrustManager.getInnerTrustManager(), mappers);
}
}
@Deprecated
public static X509ExtendedTrustManager createEnhanceableTrustManager(
X509ExtendedTrustManager trustManager,
ChainAndAuthTypeValidator chainAndAuthTypeValidator,
ChainAndAuthTypeWithSocketValidator chainAndAuthTypeWithSocketValidator,
ChainAndAuthTypeWithSSLEngineValidator chainAndAuthTypeWithSSLEngineValidator) {
Predicate<TrustManagerParameters> trustManagerParametersValidator;
if (chainAndAuthTypeValidator == null && chainAndAuthTypeWithSocketValidator == null && chainAndAuthTypeWithSSLEngineValidator == null) {
trustManagerParametersValidator = null;
} else {
trustManagerParametersValidator = trustManagerParameters -> {
boolean result = false;
if (chainAndAuthTypeValidator != null && !trustManagerParameters.getSocket().isPresent() && !trustManagerParameters.getSslEngine().isPresent()) {
result = chainAndAuthTypeValidator.test(trustManagerParameters.getChain(), trustManagerParameters.getAuthType());
}
if (chainAndAuthTypeWithSocketValidator != null && trustManagerParameters.getSocket().isPresent()) {
result = chainAndAuthTypeWithSocketValidator.test(trustManagerParameters.getChain(), trustManagerParameters.getAuthType(), trustManagerParameters.getSocket().get());
}
if (chainAndAuthTypeWithSSLEngineValidator != null && trustManagerParameters.getSslEngine().isPresent()) {
result = chainAndAuthTypeWithSSLEngineValidator.test(trustManagerParameters.getChain(), trustManagerParameters.getAuthType(), trustManagerParameters.getSslEngine().get());
}
return result;
};
}
return createEnhanceableTrustManager(trustManager, trustManagerParametersValidator);
}
public static X509ExtendedTrustManager createEnhanceableTrustManager(
X509ExtendedTrustManager trustManager,
Predicate<TrustManagerParameters> trustManagerParametersValidator) {
return createEnhanceableTrustManager(trustManager, trustManagerParametersValidator, false);
}
public static X509ExtendedTrustManager createEnhanceableTrustManager(
X509ExtendedTrustManager trustManager,
Predicate<TrustManagerParameters> trustManagerParametersValidator,
boolean shouldTrustedCertificatesBeConcealed) {
return new EnhanceableX509ExtendedTrustManager(trustManager, trustManagerParametersValidator, shouldTrustedCertificatesBeConcealed);
}
private static List<X509ExtendedTrustManager> unwrapIfPossible(X509ExtendedTrustManager trustManager) {
if (trustManager instanceof CompositeX509ExtendedTrustManager) {
List<X509ExtendedTrustManager> trustManagers = new ArrayList<>();
for (X509ExtendedTrustManager innerTrustManager : ((CompositeX509ExtendedTrustManager) trustManager).getInnerTrustManagers()) {
List<X509ExtendedTrustManager> unwrappedTrustManagers = TrustManagerUtils.unwrapIfPossible(innerTrustManager);
trustManagers.addAll(unwrappedTrustManagers);
}
return trustManagers;
} else {
return Collections.singletonList(trustManager);
}
}
public static TrustManagerBuilder trustManagerBuilder() {
return new TrustManagerBuilder();
}
public static final class TrustManagerBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(TrustManagerBuilder.class);
private static final String EMPTY_TRUST_MANAGER_EXCEPTION = "Input does not contain TrustManager";
private TrustManagerBuilder() {
}
private final List<X509ExtendedTrustManager> trustManagers = new ArrayList<>();
private boolean swappableTrustManagerEnabled = false;
private boolean loggingTrustManagerEnabled = false;
private ChainAndAuthTypeValidator chainAndAuthTypeValidator;
private ChainAndAuthTypeWithSocketValidator chainAndAuthTypeWithSocketValidator;
private ChainAndAuthTypeWithSSLEngineValidator chainAndAuthTypeWithSSLEngineValidator;
private Predicate<TrustManagerParameters> trustManagerParametersValidator;
private boolean shouldTrustedCertificatesBeConcealed;
public <T extends X509TrustManager> TrustManagerBuilder withTrustManagers(T... trustManagers) {
for (T trustManager : trustManagers) {
withTrustManager(trustManager);
}
return this;
}
public <T extends X509TrustManager> TrustManagerBuilder withTrustManagers(List<T> trustManagers) {
for (X509TrustManager trustManager : trustManagers) {
withTrustManager(trustManager);
}
return this;
}
public <T extends X509TrustManager> TrustManagerBuilder withTrustManager(T trustManager) {
this.trustManagers.add(TrustManagerUtils.wrapIfNeeded(trustManager));
return this;
}
public <T extends KeyStore> TrustManagerBuilder withTrustStores(T... trustStores) {
return withTrustStores(Arrays.asList(trustStores));
}
public TrustManagerBuilder withTrustStores(List<? extends KeyStore> trustStores) {
for (KeyStore trustStore : trustStores) {
this.trustManagers.add(TrustManagerUtils.createTrustManager(trustStore));
}
return this;
}
public <T extends KeyStore> TrustManagerBuilder withTrustStore(T trustStore) {
this.trustManagers.add(TrustManagerUtils.createTrustManager(trustStore));
return this;
}
public <T extends KeyStore> TrustManagerBuilder withTrustStore(T trustStore, String trustManagerAlgorithm) {
this.trustManagers.add(TrustManagerUtils.createTrustManager(trustStore, trustManagerAlgorithm));
return this;
}
public TrustManagerBuilder withSwappableTrustManager(boolean swappableTrustManagerEnabled) {
this.swappableTrustManagerEnabled = swappableTrustManagerEnabled;
return this;
}
public TrustManagerBuilder withLoggingTrustManager(boolean loggingTrustManagerEnabled) {
this.loggingTrustManagerEnabled = loggingTrustManagerEnabled;
return this;
}
@Deprecated
public TrustManagerBuilder withTrustEnhancer(ChainAndAuthTypeValidator validator) {
this.chainAndAuthTypeValidator = validator;
return this;
}
@Deprecated
public TrustManagerBuilder withTrustEnhancer(ChainAndAuthTypeWithSocketValidator validator) {
this.chainAndAuthTypeWithSocketValidator = validator;
return this;
}
@Deprecated
public TrustManagerBuilder withTrustEnhancer(ChainAndAuthTypeWithSSLEngineValidator validator) {
this.chainAndAuthTypeWithSSLEngineValidator = validator;
return this;
}
public TrustManagerBuilder withTrustEnhancer(Predicate<TrustManagerParameters> trustManagerParametersValidator) {
this.trustManagerParametersValidator = trustManagerParametersValidator;
return this;
}
public TrustManagerBuilder withTrustEnhancer(boolean shouldTrustedCertificatesBeConcealed) {
this.shouldTrustedCertificatesBeConcealed = shouldTrustedCertificatesBeConcealed;
return this;
}
public X509ExtendedTrustManager build() {
requireNotEmpty(trustManagers, () -> new GenericTrustManagerException(EMPTY_TRUST_MANAGER_EXCEPTION));
X509ExtendedTrustManager baseTrustManager;
Optional<X509ExtendedTrustManager> unsafeOrDummyTrustManager = getUnsafeOrDummyTrustManagerIfConfigured(trustManagers);
if (unsafeOrDummyTrustManager.isPresent()) {
baseTrustManager = unsafeOrDummyTrustManager.get();
} else {
baseTrustManager = combine(trustManagers);
baseTrustManager = createEnhanceableTrustManagerIfEnabled(baseTrustManager)
.orElse(baseTrustManager);
}
if (loggingTrustManagerEnabled) {
baseTrustManager = TrustManagerUtils.createLoggingTrustManager(baseTrustManager);
}
if (swappableTrustManagerEnabled) {
baseTrustManager = TrustManagerUtils.createSwappableTrustManager(baseTrustManager);
}
return baseTrustManager;
}
private Optional<X509ExtendedTrustManager> getUnsafeOrDummyTrustManagerIfConfigured(List<X509ExtendedTrustManager> trustManagers) {
Optional<X509ExtendedTrustManager> maybeUnsafeTrustManager = trustManagers.stream()
.filter(UnsafeX509ExtendedTrustManager.class::isInstance)
.findAny();
if (maybeUnsafeTrustManager.isPresent()) {
if (trustManagers.size() > 1) {
LOGGER.debug("Unsafe TrustManager is being used therefore other trust managers will not be included for constructing the base trust manager");
}
return maybeUnsafeTrustManager;
}
Optional<X509ExtendedTrustManager> maybeDummyTrustManager = trustManagers.stream()
.filter(DummyX509ExtendedTrustManager.class::isInstance)
.findAny();
if (maybeDummyTrustManager.isPresent()) {
if (trustManagers.size() > 1) {
LOGGER.debug("Dummy TrustManager is being used therefore other trust managers will not be included for constructing the base trust manager");
}
return maybeDummyTrustManager;
}
return Optional.empty();
}
private X509ExtendedTrustManager combine(List<X509ExtendedTrustManager> trustManagers) {
if (trustManagers.size() == 1) {
return trustManagers.get(0);
}
return trustManagers.stream()
.map(TrustManagerUtils::unwrapIfPossible)
.flatMap(Collection::stream)
.collect(CollectorsUtils.toListAndThen(CompositeX509ExtendedTrustManager::new));
}
private Optional<X509ExtendedTrustManager> createEnhanceableTrustManagerIfEnabled(X509ExtendedTrustManager baseTrustManager) {
if (chainAndAuthTypeValidator == null
&& chainAndAuthTypeWithSocketValidator == null
&& chainAndAuthTypeWithSSLEngineValidator == null
&& trustManagerParametersValidator == null
&& !shouldTrustedCertificatesBeConcealed) {
return Optional.empty();
}
Predicate<TrustManagerParameters> aTrustManagerParametersValidator;
if (trustManagerParametersValidator != null) {
aTrustManagerParametersValidator = trustManagerParametersValidator;
} else if (chainAndAuthTypeValidator != null) {
aTrustManagerParametersValidator = trustManagerParameters -> chainAndAuthTypeValidator.test(trustManagerParameters.getChain(), trustManagerParameters.getAuthType());
} else if (chainAndAuthTypeWithSocketValidator != null) {
aTrustManagerParametersValidator = trustManagerParameters -> chainAndAuthTypeWithSocketValidator.test(trustManagerParameters.getChain(), trustManagerParameters.getAuthType(), trustManagerParameters.getSocket().orElse(null));
} else if (chainAndAuthTypeWithSSLEngineValidator != null) {
aTrustManagerParametersValidator = trustManagerParameters -> chainAndAuthTypeWithSSLEngineValidator.test(trustManagerParameters.getChain(), trustManagerParameters.getAuthType(), trustManagerParameters.getSslEngine().orElse(null));
} else {
aTrustManagerParametersValidator = trustManagerParameters -> false;
}
X509ExtendedTrustManager enhanceableTrustManager = TrustManagerUtils.createEnhanceableTrustManager(baseTrustManager, aTrustManagerParametersValidator, shouldTrustedCertificatesBeConcealed);
return Optional.of(enhanceableTrustManager);
}
}
}