forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeystoreSSLConfig.java
More file actions
138 lines (114 loc) · 4.14 KB
/
Copy pathKeystoreSSLConfig.java
File metadata and controls
138 lines (114 loc) · 4.14 KB
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
package com.github.dockerjava.core;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* An SSL Config that is based on an pre-existing or pre-loaded KeyStore.
*/
public class KeystoreSSLConfig implements SSLConfig, Serializable {
private final KeyStore keystore;
private final String keystorePassword;
/**
* @param keystore
* a KeyStore
* @param keystorePassword
* key password
*/
public KeystoreSSLConfig(KeyStore keystore, String keystorePassword) {
this.keystorePassword = keystorePassword;
checkNotNull(keystore);
this.keystore = keystore;
}
/**
*
* @param pfxFile
* a PKCS12 file
* @param password
* Password for the keystore
* @throws KeyStoreException
* @throws IOException
* @throws CertificateException
* @throws NoSuchAlgorithmException
*/
public KeystoreSSLConfig(File pfxFile, String password) throws KeyStoreException, IOException,
CertificateException, NoSuchAlgorithmException {
checkNotNull(pfxFile);
checkNotNull(password);
keystore = KeyStore.getInstance("pkcs12");
try (FileInputStream fs = new FileInputStream(pfxFile)) {
keystore.load(fs, password.toCharArray());
}
keystorePassword = password;
}
/**
* Get the SSL Context out of the keystore.
*
* @return java SSLContext
* @throws KeyManagementException
* @throws UnrecoverableKeyException
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
*/
@Override
public SSLContext getSSLContext() throws KeyManagementException, UnrecoverableKeyException,
NoSuchAlgorithmException, KeyStoreException {
final SSLContext context = SSLContext.getInstance("TLS");
String httpProtocols = System.getProperty("https.protocols");
System.setProperty("https.protocols", "TLSv1");
if (httpProtocols != null) {
System.setProperty("https.protocols", httpProtocols);
}
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory
.getDefaultAlgorithm());
keyManagerFactory.init(keystore, keystorePassword.toCharArray());
context.init(keyManagerFactory.getKeyManagers(), new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
@Override
public void checkClientTrusted(final X509Certificate[] arg0, final String arg1) {
}
@Override
public void checkServerTrusted(final X509Certificate[] arg0, final String arg1) {
}
}
}, new SecureRandom());
return context;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
KeystoreSSLConfig that = (KeystoreSSLConfig) o;
return keystore.equals(that.keystore);
}
@Override
public int hashCode() {
return keystore.hashCode();
}
@Override
public String toString() {
return new StringBuilder().append(this.getClass().getSimpleName()).append("{").append("keystore=")
.append(keystore).append("}").toString();
}
}