Skip to content

Commit 27963fb

Browse files
committed
feat: add support for other security providers#3590
Dynamically load security providers using the Java Service Provider Interface. This allows the user to pick the provider based upon the dependencies it has on its classpath.
1 parent b14c36d commit 27963fb

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

util/src/main/java/io/kubernetes/client/util/SSLUtils.java

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.security.KeyStoreException;
2525
import java.security.NoSuchAlgorithmException;
2626
import java.security.PrivateKey;
27+
import java.security.Provider;
2728
import java.security.Security;
2829
import java.security.UnrecoverableKeyException;
2930
import java.security.cert.Certificate;
@@ -32,6 +33,7 @@
3233
import java.security.cert.X509Certificate;
3334
import java.security.spec.InvalidKeySpecException;
3435
import java.util.Collection;
36+
import java.util.ServiceLoader;
3537
import javax.net.ssl.KeyManager;
3638
import javax.net.ssl.KeyManagerFactory;
3739
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
@@ -40,55 +42,63 @@
4042
import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator;
4143
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
4244
import org.bouncycastle.util.io.pem.PemWriter;
45+
import org.slf4j.Logger;
46+
import org.slf4j.LoggerFactory;
4347

4448
public class SSLUtils {
49+
private static final Logger log = LoggerFactory.getLogger(SSLUtils.class);
50+
4551
static {
46-
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
52+
ServiceLoader<Provider> services = ServiceLoader.load(java.security.Provider.class);
53+
for (Provider service : services) {
54+
log.debug("Found security provider: " + service.getName());
55+
Security.addProvider(service);
56+
}
4757
}
4858

4959
public static boolean isNotNullOrEmpty(String val) {
5060
return val != null && val.length() > 0;
5161
}
5262

5363
public static KeyManager[] keyManagers(
54-
byte[] certData,
55-
byte[] keyData,
56-
String algo,
57-
String passphrase,
58-
String keyStoreFile,
59-
String keyStorePassphrase)
60-
throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException,
64+
byte[] certData,
65+
byte[] keyData,
66+
String algo,
67+
String passphrase,
68+
String keyStoreFile,
69+
String keyStorePassphrase)
70+
throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException,
6171
CertificateException, InvalidKeySpecException, IOException {
6272
KeyManager[] keyManagers = null;
6373
if (certData != null && keyData != null) {
6474
KeyStore keyStore =
65-
createKeyStore(certData, keyData, algo, passphrase, keyStoreFile, keyStorePassphrase);
75+
createKeyStore(certData, keyData, algo, passphrase, keyStoreFile, keyStorePassphrase);
6676
KeyManagerFactory kmf =
67-
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
77+
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
6878
kmf.init(keyStore, passphrase.toCharArray());
6979
keyManagers = kmf.getKeyManagers();
7080
}
7181
return keyManagers;
7282
}
7383

7484
public static KeyStore createKeyStore(
75-
byte[] clientCertData,
76-
byte[] clientKeyData,
77-
String clientKeyAlgo,
78-
String clientKeyPassphrase,
79-
String keyStoreFile,
80-
String keyStorePassphrase)
81-
throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException,
85+
byte[] clientCertData,
86+
byte[] clientKeyData,
87+
String clientKeyAlgo,
88+
String clientKeyPassphrase,
89+
String keyStoreFile,
90+
String keyStorePassphrase)
91+
throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException,
8292
KeyStoreException {
8393
try (InputStream certInputStream = new ByteArrayInputStream(clientCertData);
84-
InputStream keyInputStream = new ByteArrayInputStream(clientKeyData)) {
94+
InputStream keyInputStream = new ByteArrayInputStream(clientKeyData)) {
8595
return createKeyStore(
86-
certInputStream,
87-
keyInputStream,
88-
clientKeyAlgo,
89-
clientKeyPassphrase != null ? clientKeyPassphrase.toCharArray() : null,
90-
keyStoreFile,
91-
getKeyStorePassphrase(keyStorePassphrase));
96+
certInputStream,
97+
keyInputStream,
98+
clientKeyAlgo,
99+
clientKeyPassphrase != null ? clientKeyPassphrase.toCharArray() : null,
100+
keyStoreFile,
101+
getKeyStorePassphrase(keyStorePassphrase));
92102
}
93103
}
94104

@@ -113,24 +123,24 @@ public static String recognizePrivateKeyAlgo(byte[] privateKeyBytes) {
113123
}
114124

115125
public static PrivateKey loadKey(byte[] privateKeyBytes)
116-
throws IOException, InvalidKeySpecException {
126+
throws IOException, InvalidKeySpecException {
117127
return loadKey(
118-
new ByteArrayInputStream(privateKeyBytes), recognizePrivateKeyAlgo(privateKeyBytes));
128+
new ByteArrayInputStream(privateKeyBytes), recognizePrivateKeyAlgo(privateKeyBytes));
119129
}
120130

121131
public static PrivateKey loadKey(byte[] pemPrivateKeyBytes, String algo)
122-
throws IOException, InvalidKeySpecException {
132+
throws IOException, InvalidKeySpecException {
123133
return loadKey(new ByteArrayInputStream(pemPrivateKeyBytes), algo);
124134
}
125135

126136
public static PrivateKey loadKey(InputStream keyInputStream, String clientKeyAlgo)
127-
throws IOException, InvalidKeySpecException {
137+
throws IOException, InvalidKeySpecException {
128138
final PrivateKey privateKey;
129139
try (final PEMParser pemParser = new PEMParser(new InputStreamReader(keyInputStream))) {
130140
final Object pemObject = pemParser.readObject();
131141
if (pemObject == null) {
132142
final String message =
133-
String.format("PEM Private Key Algorithm [%s] not parsed", clientKeyAlgo);
143+
String.format("PEM Private Key Algorithm [%s] not parsed", clientKeyAlgo);
134144
throw new InvalidKeySpecException(message);
135145
}
136146
final JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
@@ -144,23 +154,23 @@ public static PrivateKey loadKey(InputStream keyInputStream, String clientKeyAlg
144154
} else {
145155
final String pemObjectType = pemObject.getClass().getSimpleName();
146156
final String message =
147-
String.format(
148-
"PEM Private Key Algorithm [%s] Type [%s] not supported",
149-
clientKeyAlgo, pemObjectType);
157+
String.format(
158+
"PEM Private Key Algorithm [%s] Type [%s] not supported",
159+
clientKeyAlgo, pemObjectType);
150160
throw new InvalidKeySpecException(message);
151161
}
152162
}
153163
return privateKey;
154164
}
155165

156166
public static KeyStore createKeyStore(
157-
InputStream certInputStream,
158-
InputStream keyInputStream,
159-
String clientKeyAlgo,
160-
char[] clientKeyPassphrase,
161-
String keyStoreFile,
162-
char[] keyStorePassphrase)
163-
throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException,
167+
InputStream certInputStream,
168+
InputStream keyInputStream,
169+
String clientKeyAlgo,
170+
char[] clientKeyPassphrase,
171+
String keyStoreFile,
172+
char[] keyStorePassphrase)
173+
throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException,
164174
KeyStoreException {
165175
CertificateFactory certFactory = CertificateFactory.getInstance("X509");
166176
Collection<? extends Certificate> certs = certFactory.generateCertificates(certInputStream);
@@ -184,15 +194,15 @@ public static KeyStore createKeyStore(
184194
}
185195

186196
String alias =
187-
((X509Certificate) certs.stream().findFirst().get()).getSubjectX500Principal().getName();
197+
((X509Certificate) certs.stream().findFirst().get()).getSubjectX500Principal().getName();
188198
keyStore.setKeyEntry(
189-
alias, privateKey, clientKeyPassphrase, certs.toArray(new X509Certificate[certs.size()]));
199+
alias, privateKey, clientKeyPassphrase, certs.toArray(new X509Certificate[certs.size()]));
190200

191201
return keyStore;
192202
}
193203

194204
private static void loadDefaultKeyStoreFile(KeyStore keyStore, char[] keyStorePassphrase)
195-
throws CertificateException, NoSuchAlgorithmException, IOException {
205+
throws CertificateException, NoSuchAlgorithmException, IOException {
196206

197207
String keyStorePath = System.getProperty("javax.net.ssl.keyStore");
198208
if (keyStorePath != null && keyStorePath.length() > 0) {
@@ -206,7 +216,7 @@ private static void loadDefaultKeyStoreFile(KeyStore keyStore, char[] keyStorePa
206216
}
207217

208218
private static boolean loadDefaultStoreFile(KeyStore keyStore, File fileToLoad, char[] passphrase)
209-
throws CertificateException, NoSuchAlgorithmException, IOException {
219+
throws CertificateException, NoSuchAlgorithmException, IOException {
210220
if (fileToLoad.exists() && fileToLoad.isFile() && fileToLoad.length() > 0) {
211221
try (FileInputStream inputStream = new FileInputStream(fileToLoad)) {
212222
keyStore.load(inputStream, passphrase);

0 commit comments

Comments
 (0)