-
Notifications
You must be signed in to change notification settings - Fork 180
/
SslConfiguration.java
783 lines (682 loc) · 27.6 KB
/
SslConfiguration.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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
* Copyright 2016-2021 the original author or authors.
*
* 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 org.springframework.vault.support;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* SSL configuration.
* <p>
* Provides configuration for a key store and trust store for TLS certificate
* verification. Key store and trust store may be left unconfigured if the JDK trust store
* contains all necessary certificates to verify TLS certificates. The key store is used
* for Client Certificate authentication.
*
* @author Mark Paluch
* @author Ryan Gow
* @see Resource
* @see java.security.KeyStore
* @see org.springframework.vault.authentication.ClientCertificateAuthentication
*/
public class SslConfiguration {
/**
* Constant for PEM-based keystore type.
*
* @since 2.3
*/
public static final String PEM_KEYSTORE_TYPE = "PEM";
/**
* Constant for system-default keystore type.
*
* @since 2.3
*/
public static final String DEFAULT_KEYSTORE_TYPE = KeyStore.getDefaultType();
private final KeyStoreConfiguration keyStoreConfiguration;
private final KeyStoreConfiguration trustStoreConfiguration;
private final KeyConfiguration keyConfiguration;
private final List<String> enabledProtocols;
private final List<String> enabledCipherSuites;
/**
* Create a new {@link SslConfiguration} with the default {@link KeyStore} type.
* @param keyStore the key store resource, must not be {@literal null}.
* @param keyStorePassword the key store password.
* @param trustStore the trust store resource, must not be {@literal null}.
* @param trustStorePassword the trust store password.
* @deprecated Since 1.1, use
* {@link #SslConfiguration(KeyStoreConfiguration, KeyStoreConfiguration)} to prevent
* {@link String} interning and retaining passwords represented as String longer from
* GC than necessary.
*/
@Deprecated
public SslConfiguration(Resource keyStore, @Nullable String keyStorePassword, Resource trustStore,
@Nullable String trustStorePassword) {
this(new KeyStoreConfiguration(keyStore, charsOrNull(keyStorePassword), DEFAULT_KEYSTORE_TYPE),
new KeyStoreConfiguration(trustStore, charsOrNull(trustStorePassword), DEFAULT_KEYSTORE_TYPE));
}
/**
* Create a new {@link SslConfiguration}.
* @param keyStoreConfiguration the key store configuration, must not be
* {@literal null}.
* @param trustStoreConfiguration the trust store configuration, must not be
* {@literal null}.
* @since 1.1
*/
public SslConfiguration(KeyStoreConfiguration keyStoreConfiguration,
KeyStoreConfiguration trustStoreConfiguration) {
this(keyStoreConfiguration, KeyConfiguration.unconfigured(), trustStoreConfiguration);
}
/**
* Create a new {@link SslConfiguration}.
* @param keyStoreConfiguration the key store configuration, must not be
* {@literal null}.
* @param trustStoreConfiguration the trust store configuration, must not be
* {@literal null}.
* @param enabledProtocols the enabled SSL protocols, elements must match protocol
* version strings used by the enabled Java SSL provider. May be {@literal null} to
* indicate the SSL socket factory should use a default list of enabled protocol
* versions.
* @param enabledCipherSuites the enabled SSL cipher suites, elements must match
* cipher suite strings used by the enabled Java SSL provider. May be {@literal null}
* to indicate the SSL socket factory should use a default list of enabled cipher
* suites.
* @since 2.3.2
* @see sun.security.ssl.ProtocolVersion
* @see sun.security.ssl.CipherSuite
*/
public SslConfiguration(KeyStoreConfiguration keyStoreConfiguration, KeyConfiguration keyConfiguration,
KeyStoreConfiguration trustStoreConfiguration, List<String> enabledProtocols,
List<String> enabledCipherSuites) {
Assert.notNull(keyStoreConfiguration, "KeyStore configuration must not be null");
Assert.notNull(keyConfiguration, "KeyConfiguration must not be null");
Assert.notNull(trustStoreConfiguration, "TrustStore configuration must not be null");
this.keyStoreConfiguration = keyStoreConfiguration;
this.keyConfiguration = keyConfiguration;
this.trustStoreConfiguration = trustStoreConfiguration;
this.enabledProtocols = Collections.unmodifiableList(new ArrayList<>(enabledProtocols));
this.enabledCipherSuites = Collections.unmodifiableList(new ArrayList<>(enabledCipherSuites));
}
/**
* Create a new {@link SslConfiguration}.
* @param keyStoreConfiguration the key store configuration, must not be
* {@literal null}.
* @param keyConfiguration the configuration for a specific key in
* {@code keyStoreConfiguration} to use.
* @param trustStoreConfiguration the trust store configuration, must not be
* {@literal null}.
* @since 2.2
*/
public SslConfiguration(KeyStoreConfiguration keyStoreConfiguration, KeyConfiguration keyConfiguration,
KeyStoreConfiguration trustStoreConfiguration) {
this(keyStoreConfiguration, keyConfiguration, trustStoreConfiguration, Collections.emptyList(),
Collections.emptyList());
}
/**
* Create a new {@link SslConfiguration}.
* @param keyStoreConfiguration the key store configuration, must not be
* {@literal null}.
* @param trustStoreConfiguration the trust store configuration, must not be
* {@literal null}.
* @param enabledProtocols the enabled SSL protocols, elements must match protocol
* version strings used by the enabled Java SSL provider. May be {@literal null} to
* indicate the SSL socket factory should use a default list of enabled protocol
* versions.
* @param enabledCipherSuites the enabled SSL cipher suites, elements must match
* cipher suite strings used by the enabled Java SSL provider. May be {@literal null}
* to indicate the SSL socket factory should use a default list of enabled cipher
* suites.
* @since 2.3.2
* @see sun.security.ssl.ProtocolVersion
* @see sun.security.ssl.CipherSuite
*/
public SslConfiguration(KeyStoreConfiguration keyStoreConfiguration, KeyStoreConfiguration trustStoreConfiguration,
List<String> enabledProtocols, List<String> enabledCipherSuites) {
this(keyStoreConfiguration, KeyConfiguration.unconfigured(), trustStoreConfiguration, enabledProtocols,
enabledCipherSuites);
}
/**
* Create a new {@link SslConfiguration} for the given trust store with the default
* {@link KeyStore} type.
* @param trustStore resource pointing to an existing trust store, must not be
* {@literal null}.
* @param trustStorePassword may be {@literal null}.
* @return the created {@link SslConfiguration}.
* @see java.security.KeyStore
* @deprecated Since 1.1, use {@link #forTrustStore(Resource, char[])} to prevent
* {@link String} interning and retaining passwords represented as String longer from
* GC than necessary.
*/
@Deprecated
public static SslConfiguration forTrustStore(Resource trustStore, @Nullable String trustStorePassword) {
return forTrustStore(trustStore, charsOrNull(trustStorePassword));
}
/**
* Create a new {@link SslConfiguration} for the given trust store with the default
* {@link KeyStore} type.
* @param trustStore resource pointing to an existing trust store, must not be
* {@literal null}.
* @param trustStorePassword may be {@literal null}.
* @return the created {@link SslConfiguration}.
* @see java.security.KeyStore
*/
public static SslConfiguration forTrustStore(Resource trustStore, @Nullable char[] trustStorePassword) {
Assert.notNull(trustStore, "TrustStore must not be null");
Assert.isTrue(trustStore.exists(), () -> String.format("TrustStore %s does not exist", trustStore));
return new SslConfiguration(KeyStoreConfiguration.unconfigured(), KeyConfiguration.unconfigured(),
new KeyStoreConfiguration(trustStore, trustStorePassword, DEFAULT_KEYSTORE_TYPE));
}
/**
* Create a new {@link SslConfiguration} for the given {@link KeyStoreConfiguration
* trust store}.
* @param trustStore must not be {@literal null}.
* @return a new {@link SslConfiguration} with {@link KeyStoreConfiguration trust
* store configuration} applied.
* @since 2.2
* @see java.security.KeyStore
*/
public static SslConfiguration forTrustStore(KeyStoreConfiguration trustStore) {
return unconfigured().withTrustStore(trustStore);
}
/**
* Create a new {@link SslConfiguration} for the given key store with the default
* {@link KeyStore} type.
* @param keyStore resource pointing to an existing key store, must not be
* {@literal null}.
* @param keyStorePassword may be {@literal null}.
* @return the created {@link SslConfiguration}.
* @see java.security.KeyStore
* @deprecated Since 1.1, use {@link #forKeyStore(Resource, char[])} to prevent
* {@link String} interning and retaining passwords represented as String longer from
* GC than necessary.
*/
@Deprecated
public static SslConfiguration forKeyStore(Resource keyStore, @Nullable String keyStorePassword) {
return forKeyStore(keyStore, charsOrNull(keyStorePassword));
}
/**
* Create a new {@link SslConfiguration} for the given key store with the default
* {@link KeyStore} type.
* @param keyStore resource pointing to an existing key store, must not be
* {@literal null}.
* @param keyStorePassword may be {@literal null}.
* @return the created {@link SslConfiguration}.
* @see java.security.KeyStore
*/
public static SslConfiguration forKeyStore(Resource keyStore, @Nullable char[] keyStorePassword) {
return forKeyStore(new KeyStoreConfiguration(keyStore, keyStorePassword, DEFAULT_KEYSTORE_TYPE),
KeyConfiguration.unconfigured());
}
/**
* Create a new {@link SslConfiguration} for the given {@link KeyStoreConfiguration
* key store}.
* @param keyStore resource pointing to an existing key store, must not be
* {@literal null}.
* @return the created {@link SslConfiguration}.
* @since 2.2
* @see java.security.KeyStore
*/
public static SslConfiguration forKeyStore(KeyStoreConfiguration keyStore) {
return forKeyStore(keyStore, KeyConfiguration.unconfigured());
}
/**
* Create a new {@link SslConfiguration} for the given {@link KeyStoreConfiguration
* key store} and {@link KeyConfiguration}.
* @param keyStore resource pointing to an existing key store, must not be
* {@literal null}.
* @param keyConfiguration the configuration for a specific key in
* {@code keyStoreConfiguration} to use.
* @return the created {@link SslConfiguration}.
* @since 2.2
* @see java.security.KeyStore
*/
public static SslConfiguration forKeyStore(KeyStoreConfiguration keyStore, KeyConfiguration keyConfiguration) {
return unconfigured().withKeyStore(keyStore, keyConfiguration);
}
/**
* Create a new {@link SslConfiguration} for the given key store with the default
* {@link KeyStore} type.
* @param keyStore resource pointing to an existing key store, must not be
* {@literal null}.
* @param keyStorePassword may be {@literal null}.
* @param keyConfiguration the configuration for a specific key in
* {@code keyStoreConfiguration} to use.
* @return the created {@link SslConfiguration}.
* @since 2.2
* @see java.security.KeyStore
*/
public static SslConfiguration forKeyStore(Resource keyStore, @Nullable char[] keyStorePassword,
KeyConfiguration keyConfiguration) {
Assert.notNull(keyStore, "KeyStore must not be null");
Assert.isTrue(keyStore.exists(), () -> String.format("KeyStore %s does not exist", keyStore));
Assert.notNull(keyConfiguration, "KeyConfiguration must not be null");
return new SslConfiguration(new KeyStoreConfiguration(keyStore, keyStorePassword, DEFAULT_KEYSTORE_TYPE),
keyConfiguration, KeyStoreConfiguration.unconfigured());
}
/**
* Create a new {@link SslConfiguration} for the given truststore with the default
* {@link KeyStore} type.
* @param keyStore resource pointing to an existing keystore, must not be
* {@literal null}.
* @param keyStorePassword may be {@literal null}.
* @param trustStore resource pointing to an existing trust store, must not be
* {@literal null}.
* @param trustStorePassword may be {@literal null}.
* @return the created {@link SslConfiguration}.
* @see java.security.KeyStore
* @deprecated Since 1.1, use {@link #create(Resource, char[], Resource, char[])} to
* prevent {@link String} interning and retaining passwords represented as String
* longer from GC than necessary.
*/
@Deprecated
public SslConfiguration create(Resource keyStore, @Nullable String keyStorePassword, Resource trustStore,
@Nullable String trustStorePassword) {
return create(keyStore, charsOrNull(keyStorePassword), trustStore, charsOrNull(trustStorePassword));
}
/**
* Create a new {@link SslConfiguration} for the given truststore with the default
* {@link KeyStore} type.
* @param keyStore resource pointing to an existing keystore, must not be
* {@literal null}.
* @param keyStorePassword may be {@literal null}.
* @param trustStore resource pointing to an existing trust store, must not be
* {@literal null}.
* @param trustStorePassword may be {@literal null}.
* @return the created {@link SslConfiguration}.
* @see java.security.KeyStore
*/
public static SslConfiguration create(Resource keyStore, @Nullable char[] keyStorePassword, Resource trustStore,
@Nullable char[] trustStorePassword) {
Assert.notNull(keyStore, "KeyStore must not be null");
Assert.isTrue(keyStore.exists(), () -> String.format("KeyStore %s does not exist", keyStore));
Assert.notNull(trustStore, "TrustStore must not be null");
Assert.isTrue(trustStore.exists(), String.format("TrustStore %s does not exist", trustStore));
return new SslConfiguration(new KeyStoreConfiguration(keyStore, keyStorePassword, DEFAULT_KEYSTORE_TYPE),
new KeyStoreConfiguration(trustStore, trustStorePassword, DEFAULT_KEYSTORE_TYPE));
}
/**
* Factory method returning an unconfigured {@link SslConfiguration} instance.
* @return an unconfigured {@link SslConfiguration} instance.
* @since 2.0
*/
public static SslConfiguration unconfigured() {
return new SslConfiguration(KeyStoreConfiguration.unconfigured(), KeyStoreConfiguration.unconfigured());
}
/**
* The list of SSL protocol versions that must be enabled. A value of {@literal null}
* indicates that the SSL socket factory should use a default list of enabled protocol
* versions.
* @return the list of enabled SSL protocol versions.
* @since 2.3.2
*/
public List<String> getEnabledProtocols() {
return this.enabledProtocols;
}
/**
* Create a new {@link SslConfiguration} with the enabled protocol versions applied
* retaining the other configuration from this instance.
* @param enabledProtocols must not be {@literal null}.
* @return a new {@link SslConfiguration} with the enabled protocol versions applied.
* @since 2.3.2
* @see sun.security.ssl.ProtocolVersion
*/
public SslConfiguration withEnabledProtocols(String... enabledProtocols) {
Assert.notNull(enabledProtocols, "Enabled protocols must not be null");
return withEnabledProtocols(Arrays.asList(enabledProtocols));
}
/**
* Create a new {@link SslConfiguration} with the enabled protocol versions applied
* retaining the other configuration from this instance.
* @param enabledProtocols must not be {@literal null}.
* @return a new {@link SslConfiguration} with the enabled protocol versions applied.
* @since 2.3.2
* @see sun.security.ssl.ProtocolVersion
*/
public SslConfiguration withEnabledProtocols(List<String> enabledProtocols) {
Assert.notNull(enabledProtocols, "Enabled protocols must not be null");
return new SslConfiguration(this.keyStoreConfiguration, this.keyConfiguration, this.trustStoreConfiguration,
enabledProtocols, this.enabledCipherSuites);
}
/**
* The list of SSL cipher suites that must be enabled. A value of {@literal null}
* indicates that the SSL socket factory should use a default list of enabled cipher
* suites.
* @return the list of enabled SSL cipher suites.
* @since 2.3.2
*/
public List<String> getEnabledCipherSuites() {
return this.enabledCipherSuites;
}
/**
* Create a new {@link SslConfiguration} with the enabled cipher suites applied
* retaining the other configuration from this instance.
* @param enabledCipherSuites must not be {@literal null}.
* @return a new {@link SslConfiguration} with the enabled cipher suites applied.
* @since 2.3.2
* @see sun.security.ssl.CipherSuite
*/
public SslConfiguration withEnabledCipherSuites(String... enabledCipherSuites) {
Assert.notNull(enabledProtocols, "Enabled cipher suites must not be null");
return withEnabledCipherSuites(Arrays.asList(enabledCipherSuites));
}
/**
* Create a new {@link SslConfiguration} with the enabled cipher suites applied
* retaining the other configuration from this instance.
* @param enabledCipherSuites must not be {@literal null}.
* @return a new {@link SslConfiguration} with the enabled cipher suites applied.
* @since 2.3.2
* @see sun.security.ssl.CipherSuite
*/
public SslConfiguration withEnabledCipherSuites(List<String> enabledCipherSuites) {
Assert.notNull(enabledProtocols, "Enabled cipher suites must not be null");
return new SslConfiguration(this.keyStoreConfiguration, this.keyConfiguration, this.trustStoreConfiguration,
this.enabledProtocols, enabledCipherSuites);
}
/**
* @return the {@link java.security.KeyStore key store} resource or {@literal null} if
* not configured.
*/
public Resource getKeyStore() {
return this.keyStoreConfiguration.getResource();
}
/**
* @return the key store password or {@literal null} if not configured.
* @deprecated Since 1.1, use {@link KeyStoreConfiguration#getStorePassword()} to
* prevent {@link String} interning and retaining passwords represented as String
* longer from GC than necessary.
*/
@Deprecated
@Nullable
public String getKeyStorePassword() {
return stringOrNull(this.keyStoreConfiguration.getStorePassword());
}
/**
* @return the key store configuration.
* @since 1.1
*/
public KeyStoreConfiguration getKeyStoreConfiguration() {
return this.keyStoreConfiguration;
}
/**
* @return the key configuration.
* @since 2.2
*/
public KeyConfiguration getKeyConfiguration() {
return this.keyConfiguration;
}
/**
* Create a new {@link SslConfiguration} with {@link KeyStoreConfiguration} applied
* retaining the {@link #getTrustStoreConfiguration() trust store} configuration.
* @param configuration must not be {@literal null}.
* @return a new {@link SslConfiguration} with {@link KeyStoreConfiguration} applied.
* @since 2.0
*/
public SslConfiguration withKeyStore(KeyStoreConfiguration configuration) {
return withKeyStore(configuration, KeyConfiguration.unconfigured());
}
/**
* Create a new {@link SslConfiguration} with {@link KeyStoreConfiguration} and
* {@link KeyConfiguration} applied retaining the {@link #getTrustStoreConfiguration()
* trust store} configuration.
* @param configuration must not be {@literal null}.
* @param keyConfiguration the configuration for a specific key in
* {@code keyStoreConfiguration} to use.
* @return a new {@link SslConfiguration} with {@link KeyStoreConfiguration} and
* {@link KeyConfiguration} applied.
* @since 2.2
*/
public SslConfiguration withKeyStore(KeyStoreConfiguration configuration, KeyConfiguration keyConfiguration) {
return new SslConfiguration(configuration, keyConfiguration, this.trustStoreConfiguration);
}
/**
* @return the {@link java.security.KeyStore key store} resource or {@literal null} if
* not configured.
*/
public Resource getTrustStore() {
return this.trustStoreConfiguration.getResource();
}
/**
* @return the trust store password or {@literal null} if not configured.
* @deprecated Since 1.1, use {@link KeyStoreConfiguration#getStorePassword()} to
* prevent {@link String} interning and retaining passwords represented as String
* longer from GC than necessary.
*/
@Deprecated
@Nullable
public String getTrustStorePassword() {
return stringOrNull(this.trustStoreConfiguration.getStorePassword());
}
/**
* @return the trust store configuration.
* @since 1.1
*/
public KeyStoreConfiguration getTrustStoreConfiguration() {
return this.trustStoreConfiguration;
}
/**
* Create a new {@link SslConfiguration} with {@link KeyStoreConfiguration trust store
* configuration} applied retaining the {@link #getKeyStoreConfiguration()} key store}
* configuration.
* @param configuration must not be {@literal null}.
* @return a new {@link SslConfiguration} with {@link KeyStoreConfiguration trust
* store configuration} applied.
* @since 2.0
*/
public SslConfiguration withTrustStore(KeyStoreConfiguration configuration) {
return new SslConfiguration(this.keyStoreConfiguration, this.keyConfiguration, configuration);
}
@Nullable
private static String stringOrNull(@Nullable char[] storePassword) {
return storePassword != null ? new String(storePassword) : null;
}
@Nullable
private static char[] charsOrNull(@Nullable String trustStorePassword) {
return trustStorePassword != null ? trustStorePassword.toCharArray() : null;
}
/**
* Configuration for a key store/trust store.
*
* @since 1.1
*/
public static class KeyStoreConfiguration {
private static final KeyStoreConfiguration UNCONFIGURED = new KeyStoreConfiguration(AbsentResource.INSTANCE,
null, DEFAULT_KEYSTORE_TYPE);
/**
* Store that holds certificates, private keys.
*/
private final Resource resource;
/**
* Password used to access the key store/trust store.
*/
@Nullable
private final char[] storePassword;
/**
* Key store/trust store type.
*/
private final String storeType;
/**
* Create a new {@link KeyStoreConfiguration}.
*/
public KeyStoreConfiguration(Resource resource, @Nullable char[] storePassword, String storeType) {
Assert.notNull(resource, "Resource must not be null");
Assert.isTrue(resource instanceof AbsentResource || resource.exists(),
() -> String.format("Resource %s does not exist", resource));
Assert.notNull(storeType, "Keystore type must not be null");
this.resource = resource;
this.storeType = storeType;
if (storePassword == null) {
this.storePassword = null;
}
else {
this.storePassword = Arrays.copyOf(storePassword, storePassword.length);
}
}
/**
* Create a new {@link KeyStoreConfiguration} given {@link Resource}.
* @param resource resource referencing the key store, must not be {@literal null}
* .
* @return the {@link KeyStoreConfiguration} for {@code resource}.
* @since 2.0
*/
public static KeyStoreConfiguration of(Resource resource) {
return new KeyStoreConfiguration(resource, null, DEFAULT_KEYSTORE_TYPE);
}
/**
* Create a new {@link KeyStoreConfiguration} given {@link Resource} and
* {@code storePassword} using the default keystore type.
* @param resource resource referencing the key store, must not be {@literal null}
* .
* @param storePassword key store password, may be {@literal null}.
* @return the {@link KeyStoreConfiguration} for {@code resource}.
* @since 2.0
*/
public static KeyStoreConfiguration of(Resource resource, @Nullable char[] storePassword) {
return of(resource, storePassword, DEFAULT_KEYSTORE_TYPE);
}
/**
* Create a new {@link KeyStoreConfiguration} given {@link Resource},
* {@code storePassword}, and {@code keyStoreType}.
* @param resource resource referencing the key store, must not be {@literal null}
* .
* @param storePassword key store password, may be {@literal null}.
* @param keyStoreType key store type, must not be {@literal null}.
* @return the {@link KeyStoreConfiguration} for {@code resource}.
* @since 2.3
*/
public static KeyStoreConfiguration of(Resource resource, @Nullable char[] storePassword, String keyStoreType) {
return new KeyStoreConfiguration(resource, storePassword, keyStoreType);
}
/**
* Create an unconfigured, empty {@link KeyStoreConfiguration}.
* @return unconfigured, empty {@link KeyStoreConfiguration}.
* @since 2.0
*/
public static KeyStoreConfiguration unconfigured() {
return UNCONFIGURED;
}
/**
* @return {@literal true} if the resource is present.
* @since 2.0
*/
public boolean isPresent() {
return !(this.resource instanceof AbsentResource);
}
/**
* @return the {@link java.security.KeyStore key store} resource or
* {@literal null} if not configured.
*/
public Resource getResource() {
return this.resource;
}
/**
* @return the key store/trust store password. Empty {@code char} array if not
* set.
*/
@Nullable
public char[] getStorePassword() {
return this.storePassword;
}
/**
* @return the trust store type.
*/
public String getStoreType() {
return this.storeType;
}
/**
* Create a new {@link KeyStoreConfiguration} by applying all values from this
* object and the given {@code storeType}.
* @param storeType must not be {@literal null}.
* @return a new {@link KeyStoreConfiguration}.
* @since 2.3
*/
public KeyStoreConfiguration withStoreType(String storeType) {
Assert.notNull(storeType, "Key store type must not be null");
return new KeyStoreConfiguration(this.resource, this.storePassword, storeType);
}
}
/**
* Configuration for a key in a keystore.
*
* @author Mark Paluch
* @since 2.2
*/
public static class KeyConfiguration {
private static final KeyConfiguration UNCONFIGURED = new KeyConfiguration(null, null);
private final @Nullable char[] keyPassword;
private final @Nullable String keyAlias;
private KeyConfiguration(@Nullable char[] keyPassword, @Nullable String keyAlias) {
if (keyPassword == null) {
this.keyPassword = null;
}
else {
this.keyPassword = Arrays.copyOf(keyPassword, keyPassword.length);
}
this.keyAlias = keyAlias;
}
/**
* Create an unconfigured, empty {@link KeyConfiguration}.
* @return unconfigured, empty {@link KeyConfiguration}.
*/
public static KeyConfiguration unconfigured() {
return UNCONFIGURED;
}
/**
* Create a {@link KeyConfiguration} to configure a specific key within a
* {@link KeyStore}.
* @param keyPassword the key password to use. Uses
* {@link KeyStoreConfiguration#getStorePassword()} if left {@code null}.
* @param keyAlias the key alias to use. Uses the first alias if left {@code null}
* .
* @return the {@link KeyConfiguration}.
*/
public static KeyConfiguration of(@Nullable char[] keyPassword, @Nullable String keyAlias) {
return new KeyConfiguration(keyPassword, keyAlias);
}
/**
* @return the key password to use.
*/
@Nullable
public char[] getKeyPassword() {
return this.keyPassword;
}
/**
* @return key alias to use.
*/
@Nullable
public String getKeyAlias() {
return this.keyAlias;
}
}
static class AbsentResource extends AbstractResource {
static final AbsentResource INSTANCE = new AbsentResource();
private AbsentResource() {
}
@Override
public String getDescription() {
return getClass().getSimpleName();
}
@Override
public InputStream getInputStream() throws IOException {
throw new UnsupportedOperationException("Empty resource");
}
}
}