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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.test;
import com.yahoo.jdisc.http.ssl.SslKeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Charles Kim
*/
public class SslContextFactory {
private static final Logger log = Logger.getLogger(SslContextFactory.class.getName());
private static final String DEFAULT_ALGORITHM = "SunX509";
private static final String DEFAULT_PROTOCOL = "TLS";
private final SSLContext sslContext;
private SslContextFactory(SSLContext sslContext) {
this.sslContext = sslContext;
}
public SSLContext getServerSSLContext() {
return this.sslContext;
}
public static SslContextFactory newInstanceFromTrustStore(SslKeyStore trustStore) {
return newInstance(DEFAULT_ALGORITHM, DEFAULT_PROTOCOL, null, trustStore);
}
public static SslContextFactory newInstance(SslKeyStore trustStore, SslKeyStore keyStore) {
return newInstance(DEFAULT_ALGORITHM, DEFAULT_PROTOCOL, keyStore, trustStore);
}
public static SslContextFactory newInstance(String sslAlgorithm, String sslProtocol,
SslKeyStore keyStore, SslKeyStore trustStore) {
log.fine("Configuring SSLContext...");
log.fine("Using " + sslAlgorithm + " algorithm.");
try {
SSLContext sslContext = SSLContext.getInstance(sslProtocol);
sslContext.init(
keyStore == null ? null : getKeyManagers(keyStore, sslAlgorithm),
trustStore == null ? null : getTrustManagers(trustStore, sslAlgorithm),
null);
return new SslContextFactory(sslContext);
} catch (Exception e) {
log.log(Level.SEVERE, "Got exception creating SSLContext.", e);
throw new RuntimeException(e);
}
}
/**
* Used for the key store, which contains the SSL cert and private key.
*/
public static javax.net.ssl.KeyManager[] getKeyManagers(SslKeyStore keyStore,
String sslAlgorithm)
throws NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException,
KeyStoreException {
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(sslAlgorithm);
keyManagerFactory.init(
keyStore.loadJavaKeyStore(),
keyStore.getKeyStorePassword().map(String::toCharArray).orElse(null));
log.fine("KeyManagerFactory initialized with keystore");
return keyManagerFactory.getKeyManagers();
}
/**
* Used for the trust store, which contains certificates from other parties that you expect to communicate with,
* or from Certificate Authorities that you trust to identify other parties.
*/
public static javax.net.ssl.TrustManager[] getTrustManagers(SslKeyStore trustStore,
String sslAlgorithm)
throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(sslAlgorithm);
trustManagerFactory.init(trustStore.loadJavaKeyStore());
log.fine("TrustManagerFactory initialized with truststore.");
return trustManagerFactory.getTrustManagers();
}
}
|