-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
CertificateUtils.java
427 lines (369 loc) 路 19.5 KB
/
CertificateUtils.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
/*
* 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.GenericCertificateException;
import nl.altindag.ssl.exception.GenericIOException;
import nl.altindag.ssl.util.internal.IOUtils;
import javax.net.ssl.X509TrustManager;
import javax.security.auth.x500.X500Principal;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static nl.altindag.ssl.util.internal.CollectorsUtils.toModifiableList;
import static nl.altindag.ssl.util.internal.CollectorsUtils.toUnmodifiableList;
import static nl.altindag.ssl.util.internal.ValidationUtils.requireNotNull;
/**
* @author Hakan Altindag
*/
public final class CertificateUtils {
private static final String CERTIFICATE_TYPE = "X.509";
private static final String P7B_HEADER = "-----BEGIN PKCS7-----";
private static final String P7B_FOOTER = "-----END PKCS7-----";
private static final String PEM_HEADER = "-----BEGIN CERTIFICATE-----";
private static final String PEM_FOOTER = "-----END CERTIFICATE-----";
private static final Pattern PEM_PATTERN = Pattern.compile(PEM_HEADER + "(.*?)" + PEM_FOOTER, Pattern.DOTALL);
private static final Pattern P7B_PATTERN = Pattern.compile(P7B_HEADER + "(.*?)" + P7B_FOOTER, Pattern.DOTALL);
private static final String EMPTY_INPUT_STREAM_EXCEPTION_MESSAGE = "Failed to load the certificate from the provided InputStream because it is null";
private static final UnaryOperator<String> CERTIFICATE_NOT_FOUND_EXCEPTION_MESSAGE = certificatePath -> String.format("Failed to load the certificate from the classpath for the given path: [%s]", certificatePath);
private static final String MAX_64_CHARACTER_LINE_SPLITTER = "(?<=\\G.{64})";
private static final String EMPTY = "";
private CertificateUtils() {}
public static <T extends Certificate> String generateAlias(T certificate) {
if (certificate instanceof X509Certificate) {
return ((X509Certificate) certificate)
.getSubjectX500Principal()
.getName(X500Principal.CANONICAL)
.replace(" ", "-")
.replace(",", "_")
.replace("'", "")
.replaceAll("[.*\\\\/:()#]+", "")
.replaceAll("(-)\\1+", "$1");
} else {
return UUID.randomUUID().toString().toLowerCase(Locale.US);
}
}
public static <T extends Certificate> Map<String, T> generateAliases(List<T> certificates) {
Map<String, T> aliasToCertificate = new LinkedHashMap<>();
for (T certificate : certificates) {
String alias = generateUniqueAlias(certificate, aliasToCertificate::containsKey);
aliasToCertificate.put(alias, certificate);
}
return Collections.unmodifiableMap(aliasToCertificate);
}
public static <T extends Certificate> String generateUniqueAlias(T certificate, Predicate<String> aliasPredicate) {
String initialAlias = generateAlias(certificate);
String alias = initialAlias;
int counter = 0;
while (aliasPredicate.test(alias)) {
alias = String.format("%s-%d", initialAlias, counter);
counter++;
}
return alias;
}
public static <T extends Certificate> void write(Path destination, T certificate) {
try {
byte[] encodedCertificate = certificate.getEncoded();
IOUtils.write(destination, encodedCertificate);
} catch (CertificateEncodingException e) {
throw new GenericCertificateException(e);
}
}
/**
* Loads certificates from the classpath and maps it into a list of {@link Certificate}.
* <br>
* Supported input format: PEM, P7B and DER
*/
public static List<Certificate> loadCertificate(String... certificatePaths) {
return loadCertificate(certificatePath ->
requireNotNull(
CertificateUtils.class.getClassLoader().getResourceAsStream(certificatePath),
CERTIFICATE_NOT_FOUND_EXCEPTION_MESSAGE.apply(certificatePath)),
certificatePaths
);
}
/**
* Loads certificates from the filesystem and maps it into a list of {@link Certificate}.
* <br>
* Supported input format: PEM, P7B and DER
*/
public static List<Certificate> loadCertificate(Path... certificatePaths) {
return loadCertificate(certificatePath -> {
try {
return Files.newInputStream(certificatePath, StandardOpenOption.READ);
} catch (IOException exception) {
throw new GenericIOException(exception);
}
}, certificatePaths);
}
/**
* Loads certificates from multiple InputStreams and maps it into a list of {@link Certificate}.
* <br>
* Supported input format: PEM, P7B and DER
*/
public static List<Certificate> loadCertificate(InputStream... certificateStreams) {
return loadCertificate(certificateStream ->
requireNotNull(certificateStream, EMPTY_INPUT_STREAM_EXCEPTION_MESSAGE),
certificateStreams
);
}
private static <T> List<Certificate> loadCertificate(Function<T, InputStream> resourceMapper, T[] resources) {
List<Certificate> certificates = new ArrayList<>();
for (T resource : resources) {
try (InputStream certificateStream = resourceMapper.apply(resource)) {
certificates.addAll(parseCertificate(certificateStream));
} catch (Exception e) {
throw new GenericIOException(e);
}
}
return Collections.unmodifiableList(certificates);
}
/**
* Tries to map the InputStream to a list of {@link Certificate}.
* It assumes that the content of the InputStream is either PEM, P7B or DER.
* The InputStream will copied into an OutputStream so it can be read multiple times.
*/
private static List<Certificate> parseCertificate(InputStream certificateStream) {
List<Certificate> certificates;
byte[] certificateData = IOUtils.copyToByteArray(certificateStream);
String certificateContent = new String(certificateData, StandardCharsets.UTF_8);
if (isPemFormatted(certificateContent)) {
certificates = parsePemCertificate(certificateContent);
} else if(isP7bFormatted(certificateContent)) {
certificates = parseP7bCertificate(certificateContent);
} else {
certificates = parseDerCertificate(new ByteArrayInputStream(certificateData));
}
return certificates;
}
private static boolean isPemFormatted(String certificateContent) {
return PEM_PATTERN.matcher(certificateContent).find();
}
private static boolean isP7bFormatted(String certificateContent) {
return P7B_PATTERN.matcher(certificateContent).find();
}
/**
* Parses PEM formatted certificates containing a
* header as -----BEGIN CERTIFICATE----- and footer as -----END CERTIFICATE-----
* or header as -----BEGIN PKCS7----- and footer as -----END PKCS7-----
* with a base64 encoded data between the header and footer.
*/
public static List<Certificate> parsePemCertificate(String certificateContent) {
Matcher pemMatcher = PEM_PATTERN.matcher(certificateContent);
return parseCertificate(pemMatcher);
}
/**
* Parses P7B formatted certificates containing a
* header as -----BEGIN PKCS7----- and footer as -----END PKCS7-----
* with a base64 encoded data between the header and footer.
*/
public static List<Certificate> parseP7bCertificate(String certificateContent) {
Matcher p7bMatcher = P7B_PATTERN.matcher(certificateContent);
return parseCertificate(p7bMatcher);
}
private static List<Certificate> parseCertificate(Matcher certificateMatcher) {
List<Certificate> certificates = new ArrayList<>();
while (certificateMatcher.find()) {
String certificate = certificateMatcher.group(1);
String sanitizedCertificate = certificate.replaceAll("[\\n|\\r]+", EMPTY).trim();
byte[] decodedCertificate = Base64.getDecoder().decode(sanitizedCertificate);
ByteArrayInputStream certificateAsInputStream = new ByteArrayInputStream(decodedCertificate);
List<Certificate> parsedCertificates = CertificateUtils.parseDerCertificate(certificateAsInputStream);
certificates.addAll(parsedCertificates);
IOUtils.closeSilently(certificateAsInputStream);
}
return Collections.unmodifiableList(certificates);
}
public static List<Certificate> parseDerCertificate(InputStream certificateStream) {
try(BufferedInputStream bufferedCertificateStream = new BufferedInputStream(certificateStream)) {
return CertificateFactory.getInstance(CERTIFICATE_TYPE)
.generateCertificates(bufferedCertificateStream).stream()
.collect(toUnmodifiableList());
} catch (CertificateException | IOException e) {
return Collections.emptyList();
}
}
public static List<X509Certificate> getJdkTrustedCertificates() {
return Stream.of(TrustManagerUtils.createTrustManagerWithJdkTrustedCertificates().getAcceptedIssuers())
.collect(toUnmodifiableList());
}
public static List<X509Certificate> getSystemTrustedCertificates() {
return TrustManagerUtils.createTrustManagerWithSystemTrustedCertificates()
.map(X509TrustManager::getAcceptedIssuers)
.map(Arrays::asList)
.map(Collections::unmodifiableList)
.orElseGet(Collections::emptyList);
}
public static List<X509Certificate> getCertificatesFromExternalSource(String url) {
return CertificateExtractingClient.getInstance().get(url);
}
public static List<X509Certificate> getCertificatesFromExternalSource(Proxy proxy, String url) {
return CertificateExtractingClient.builder()
.withResolvedRootCa(true)
.withProxy(proxy)
.build()
.get(url);
}
public static List<X509Certificate> getCertificatesFromExternalSource(Proxy proxy, PasswordAuthentication passwordAuthentication, String url) {
return CertificateExtractingClient.builder()
.withResolvedRootCa(true)
.withProxy(proxy)
.withPasswordAuthentication(passwordAuthentication)
.build()
.get(url);
}
public static List<String> getCertificatesFromExternalSourceAsPem(String url) {
return getCertificatesFromExternalSource(url).stream()
.map(CertificateUtils::convertToPem)
.collect(toUnmodifiableList());
}
public static List<String> getCertificatesFromExternalSourceAsPem(Proxy proxy, String url) {
return getCertificatesFromExternalSource(proxy, url).stream()
.map(CertificateUtils::convertToPem)
.collect(toUnmodifiableList());
}
public static List<String> getCertificatesFromExternalSourceAsPem(Proxy proxy, PasswordAuthentication passwordAuthentication, String url) {
return getCertificatesFromExternalSource(proxy, passwordAuthentication, url).stream()
.map(CertificateUtils::convertToPem)
.collect(toUnmodifiableList());
}
public static Map<String, List<X509Certificate>> getCertificatesFromExternalSources(String... urls) {
return getCertificatesFromExternalSources(Arrays.asList(urls));
}
public static Map<String, List<X509Certificate>> getCertificatesFromExternalSources(Proxy proxy, String... urls) {
return getCertificatesFromExternalSources(proxy, Arrays.asList(urls));
}
public static Map<String, List<X509Certificate>> getCertificatesFromExternalSources(Proxy proxy, PasswordAuthentication passwordAuthentication, String... urls) {
return getCertificatesFromExternalSources(proxy, passwordAuthentication, Arrays.asList(urls));
}
public static Map<String, List<X509Certificate>> getCertificatesFromExternalSources(List<String> urls) {
return urls.stream()
.distinct()
.map(url -> new AbstractMap.SimpleEntry<>(url, getCertificatesFromExternalSource(url)))
.collect(Collectors.collectingAndThen(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue, (key1, key2) -> key1, LinkedHashMap::new), Collections::unmodifiableMap));
}
public static Map<String, List<X509Certificate>> getCertificatesFromExternalSources(Proxy proxy, List<String> urls) {
CertificateExtractingClient client = CertificateExtractingClient.builder()
.withResolvedRootCa(true)
.withProxy(proxy)
.build();
return urls.stream()
.distinct()
.map(url -> new AbstractMap.SimpleEntry<>(url, client.get(url)))
.collect(Collectors.collectingAndThen(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue, (key1, key2) -> key1, LinkedHashMap::new), Collections::unmodifiableMap));
}
public static Map<String, List<X509Certificate>> getCertificatesFromExternalSources(Proxy proxy, PasswordAuthentication passwordAuthentication, List<String> urls) {
CertificateExtractingClient client = CertificateExtractingClient.builder()
.withResolvedRootCa(true)
.withPasswordAuthentication(passwordAuthentication)
.withProxy(proxy)
.build();
return urls.stream()
.distinct()
.map(url -> new AbstractMap.SimpleEntry<>(url, client.get(url)))
.collect(Collectors.collectingAndThen(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue, (key1, key2) -> key1, LinkedHashMap::new), Collections::unmodifiableMap));
}
public static Map<String, List<String>> getCertificatesFromExternalSourcesAsPem(String... urls) {
return getCertificatesFromExternalSourcesAsPem(Arrays.asList(urls));
}
public static Map<String, List<String>> getCertificatesFromExternalSourcesAsPem(Proxy proxy, String... urls) {
return getCertificatesFromExternalSourcesAsPem(proxy, Arrays.asList(urls));
}
public static Map<String, List<String>> getCertificatesFromExternalSourcesAsPem(Proxy proxy, PasswordAuthentication passwordAuthentication, String... urls) {
return getCertificatesFromExternalSourcesAsPem(proxy, passwordAuthentication, Arrays.asList(urls));
}
public static Map<String, List<String>> getCertificatesFromExternalSourcesAsPem(List<String> urls) {
return CertificateUtils.getCertificatesFromExternalSources(urls).entrySet().stream()
.collect(Collectors.collectingAndThen(Collectors.toMap(Map.Entry::getKey, entry -> CertificateUtils.convertToPem(entry.getValue()), (key1, key2) -> key1, LinkedHashMap::new), Collections::unmodifiableMap));
}
public static Map<String, List<String>> getCertificatesFromExternalSourcesAsPem(Proxy proxy, List<String> urls) {
Map<String, List<String>> certificates = CertificateUtils.getCertificatesFromExternalSources(proxy, urls).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> CertificateUtils.convertToPem(entry.getValue())));
return Collections.unmodifiableMap(certificates);
}
public static Map<String, List<String>> getCertificatesFromExternalSourcesAsPem(Proxy proxy, PasswordAuthentication passwordAuthentication, List<String> urls) {
return CertificateUtils.getCertificatesFromExternalSources(proxy, passwordAuthentication, urls).entrySet().stream()
.collect(Collectors.collectingAndThen(Collectors.toMap(Map.Entry::getKey, entry -> CertificateUtils.convertToPem(entry.getValue()), (key1, key2) -> key1, LinkedHashMap::new), Collections::unmodifiableMap));
}
public static List<String> convertToPem(List<X509Certificate> certificates) {
return certificates.stream()
.map(CertificateUtils::convertToPem)
.collect(toUnmodifiableList());
}
public static String convertToPem(Certificate certificate) {
try {
byte[] encodedCertificate = certificate.getEncoded();
byte[] base64EncodedCertificate = Base64.getEncoder().encode(encodedCertificate);
String parsedCertificate = new String(base64EncodedCertificate);
List<String> certificateContainer = Stream.of(parsedCertificate.split(MAX_64_CHARACTER_LINE_SPLITTER))
.collect(toModifiableList());
certificateContainer.add(0, PEM_HEADER);
certificateContainer.add(PEM_FOOTER);
if (certificate instanceof X509Certificate) {
X509Certificate x509Certificate = (X509Certificate) certificate;
X500Principal issuer = x509Certificate.getIssuerX500Principal();
certificateContainer.add(0, String.format("issuer=%s", issuer.getName()));
X500Principal subject = x509Certificate.getSubjectX500Principal();
certificateContainer.add(0, String.format("subject=%s", subject.getName()));
}
return String.join(System.lineSeparator(), certificateContainer);
} catch (CertificateEncodingException e) {
throw new GenericCertificateException(e);
}
}
public static <T extends Certificate> boolean isSelfSigned(T certificate) {
try {
certificate.verify(certificate.getPublicKey());
return true;
} catch (SignatureException e) {
return false;
} catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException e) {
throw new GenericCertificateException(e);
}
}
}