Skip to content

Commit 2417350

Browse files
committed
Use CryptoIntegration for Keystore SPI
1 parent b8fd46a commit 2417350

File tree

20 files changed

+255
-272
lines changed

20 files changed

+255
-272
lines changed

common/src/main/java/org/keycloak/common/util/DerUtils.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ public static PrivateKey decodePrivateKey(InputStream is)
5252
dis.readFully(keyBytes);
5353
dis.close();
5454

55-
PKCS8EncodedKeySpec spec =
56-
new PKCS8EncodedKeySpec(keyBytes);
57-
KeyFactory kf =CryptoIntegration.getProvider().getKeyFactory("RSA");
58-
return kf.generatePrivate(spec);
55+
return decodePrivateKey(keyBytes);
5956
}
6057

6158
public static PublicKey decodePublicKey(byte[] der) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
62-
return decodePublicKey(der, "RSA");
59+
try {
60+
return decodePublicKey(der, "RSA");
61+
} catch (InvalidKeySpecException e) {
62+
return decodePublicKey(der, "EC");
63+
}
6364
}
6465

6566
public static PublicKey decodePublicKey(byte[] der, String type) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
@@ -79,7 +80,10 @@ public static X509Certificate decodeCertificate(InputStream is) throws Exception
7980
public static PrivateKey decodePrivateKey(byte[] der) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
8081
PKCS8EncodedKeySpec spec =
8182
new PKCS8EncodedKeySpec(der);
82-
KeyFactory kf = CryptoIntegration.getProvider().getKeyFactory("RSA");
83-
return kf.generatePrivate(spec);
83+
try {
84+
return CryptoIntegration.getProvider().getKeyFactory("RSA").generatePrivate(spec);
85+
} catch (InvalidKeySpecException e) {
86+
return CryptoIntegration.getProvider().getKeyFactory("EC").generatePrivate(spec);
87+
}
8488
}
8589
}

common/src/main/java/org/keycloak/common/util/PemUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import java.security.PublicKey;
2424
import java.security.cert.Certificate;
2525
import java.security.cert.X509Certificate;
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.List;
29+
import java.util.stream.Collectors;
2630

2731
import org.keycloak.common.crypto.CryptoIntegration;
2832

@@ -53,6 +57,23 @@ public static X509Certificate decodeCertificate(String cert) {
5357
return CryptoIntegration.getProvider().getPemUtils().decodeCertificate(cert);
5458
}
5559

60+
/**
61+
* Decode one or more X509 Certificates from a PEM string (certificate bundle)
62+
*
63+
* @param certs
64+
* @return
65+
* @throws Exception
66+
*/
67+
public static X509Certificate[] decodeCertificates(String certs) {
68+
String[] pemBlocks = certs.split(END_CERT);
69+
70+
List<X509Certificate> x509Certificates = Arrays.stream(pemBlocks)
71+
.filter(pemBlock -> pemBlock != null && !pemBlock.trim().isEmpty())
72+
.map(pemBlock -> PemUtils.decodeCertificate(pemBlock + END_CERT))
73+
.collect(Collectors.toList());
74+
75+
return x509Certificates.toArray(new X509Certificate[x509Certificates.size()]);
76+
}
5677

5778
/**
5879
* Decode a Public Key from a PEM string

services/src/main/java/org/keycloak/keystore/DefaultKeyStoreProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.KeyStore.Builder;
2424
import java.security.KeyStoreException;
2525
import java.security.NoSuchAlgorithmException;
26+
import java.security.NoSuchProviderException;
2627
import java.security.cert.CertificateException;
2728
import java.security.spec.InvalidKeySpecException;
2829
import java.time.Duration;
@@ -135,7 +136,7 @@ private KeyStore.Builder getKeyStore(Scope config, String prefix) {
135136
log.infov("Loading credentials for {0}: {1}", prefix, keyStoreFile);
136137
keyStoreBuilder = ReloadingKeyStore.Builder
137138
.fromKeyStoreFile(keyStoreType, Paths.get(keyStoreFile), keyStorePassword);
138-
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
139+
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | NoSuchProviderException e) {
139140
throw new RuntimeException("Failed to initialize " + prefix + " keystore: " + e.toString());
140141
}
141142
}

services/src/main/java/org/keycloak/keystore/DelegatingKeyStoreSpi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.security.KeyStoreException;
2626
import java.security.KeyStoreSpi;
2727
import java.security.NoSuchAlgorithmException;
28+
import java.security.NoSuchProviderException;
2829
import java.security.UnrecoverableKeyException;
2930
import java.security.cert.Certificate;
3031
import java.security.cert.CertificateException;
@@ -65,7 +66,7 @@ public abstract class DelegatingKeyStoreSpi extends KeyStoreSpi {
6566
* Reloads the delegate KeyStore if the underlying files have changed on disk.
6667
*/
6768
abstract void refresh() throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException,
68-
InvalidKeySpecException;
69+
InvalidKeySpecException, NoSuchProviderException;
6970

7071
/**
7172
* Calls {@link #refresh()} to refresh the cached KeyStore and if more than

services/src/main/java/org/keycloak/keystore/PemCredentialFactory.java

Lines changed: 0 additions & 128 deletions
This file was deleted.

services/src/main/java/org/keycloak/keystore/PemReader.java

Lines changed: 0 additions & 127 deletions
This file was deleted.

services/src/main/java/org/keycloak/keystore/ReloadingKeyStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.KeyStoreException;
2424
import java.security.KeyStoreSpi;
2525
import java.security.NoSuchAlgorithmException;
26+
import java.security.NoSuchProviderException;
2627
import java.security.cert.CertificateException;
2728
import java.security.spec.InvalidKeySpecException;
2829
import java.time.Duration;
@@ -124,7 +125,7 @@ public ProtectionParameter getProtectionParameter(String alias) {
124125
*/
125126
public static KeyStore.Builder fromKeyStoreFile(String type, Path path, String password)
126127
throws NoSuchAlgorithmException, CertificateException, KeyStoreException,
127-
IOException {
128+
IOException, NoSuchProviderException {
128129
return new Builder(new ReloadingKeyStore(new ReloadingKeyStoreFileSpi(type, path, password)),
129130
password.toCharArray());
130131
}
@@ -140,7 +141,7 @@ public static KeyStore.Builder fromKeyStoreFile(String type, Path path, String p
140141
*/
141142
public static KeyStore.Builder fromKeyStoreFile(String type, Path path, String password,
142143
Map<String, char[]> aliasPasswords)
143-
throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException {
144+
throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, NoSuchProviderException {
144145
return new Builder(new ReloadingKeyStore(new ReloadingKeyStoreFileSpi(type, path, password)),
145146
password.toCharArray(), aliasPasswords);
146147
}

0 commit comments

Comments
 (0)