8000 Support EC in PEM/DER utils. by tsaarni · Pull Request #38483 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions common/src/main/java/org/keycloak/common/util/DerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ public static PrivateKey decodePrivateKey(InputStream is)
dis.readFully(keyBytes);
dis.close();

PKCS8EncodedKeySpec spec =
new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf =CryptoIntegration.getProvider().getKeyFactory("RSA");
return kf.generatePrivate(spec);
return decodePrivateKey(keyBytes);
}

public static PublicKey decodePublicKey(byte[] der) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
Expand All @@ -79,7 +76,14 @@ public static X509Certificate decodeCertificate(InputStream is) throws Exception
public static PrivateKey decodePrivateKey(byte[] der) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
PKCS8EncodedKeySpec spec =
new PKCS8EncodedKeySpec(der);
KeyFactory kf = CryptoIntegration.getProvider().getKeyFactory("RSA");
return kf.generatePrivate(spec);
}
String[] algorithms = { "RSA", "EC" };
for (String algorithm : algorithms) {
try {
return CryptoIntegration.getProvider().getKeyFactory(algorithm).generatePrivate(spec);
} catch (InvalidKeySpecException e) {
// Ignore and try the next algorithm.
}
}
throw new InvalidKeySpecException("Unable to decode the private key with supported algorithms: " + String.join(", ", algorithms));
}
}
17 changes: 17 additions & 0 deletions common/src/main/java/org/keycloak/common/util/PemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.keycloak.common.crypto.CryptoIntegration;

Expand Down Expand Up @@ -53,6 +56,20 @@ public static X509Certificate decodeCertificate(String cert) {
return CryptoIntegration.getProvider().getPemUtils().decodeCertificate(cert);
}

/**
* Decode one or more X509 Certificates from a PEM string (certificate bundle)
*
* @param certs
* @return
* @throws Exception
*/
public static X509Certificate[] decodeCertificates(String certs) {
return Arrays.stream(certs.split(END_CERT))
.map(String::trim)
.filter(pemBlock -> !pemBlock.isEmpty())
.map(pemBlock -> PemUtils.decodeCertificate(pemBlock + END_CERT))
.toArray(X509Certificate[]::new);
}

/**
* Decode a Public Key from a PEM string
Expand Down
45 changes: 45 additions & 0 deletions core/src/test/java/org/keycloak/util/PemUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Test;
import org.keycloak.common.util.CertificateUtils;
import org.keycloak.common.util.KeyUtils;
import org.keycloak.common.util.PemException;
import org.keycloak.common.util.PemUtils;
import org.keycloak.rule.CryptoInitRule;

Expand Down Expand Up @@ -118,6 +119,50 @@ public void testPrivateKeyInPKCS8Format() {
String pk = PemUtils.removeBeginEnd(privateKeyPkcs8).replace("\n", "");
PrivateKey decodedPrivateKey2 = PemUtils.decodePrivateKey(pk);
Assert.assertEquals(decodedPrivateKey1, decodedPrivateKey2);

String ecPrivateKeyPkcs8 = "-----BEGIN PRIVATE KEY-----\n" +
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgO1oavi4kqVFc/rxj\n" +
"24SJivHXq7buWX58U0tswYikPwyhRANCAASCIp6nVvOk9flbUrMW7JPDmyaXCnDc\n" +
"Q2uMfvxVWIJzBuhG6VDoeFPk3yf2EN5t7Q8FU5jPSp6gJz9xbaFYYLL6\n" +
"-----END PRIVATE KEY-----";

PrivateKey decodedEcPrivateKey = PemUtils.decodePrivateKey(ecPrivateKeyPkcs8);
Assert.assertEquals("EC", decodedEcPrivateKey.getAlgorithm());
}

@Test
public void testDecodeCertificateBundle() {
String certBundleEC = "-----BEGIN CERTIFICATE-----\n" +
"MIIBUTCB96ADAgECAgg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SxM4eJNExdfHeJBYjDD345EW0vg==\n" +
"-----END CERTIFICATE-----";

X509Certificate[] certs = PemUtils.decodeCertificates(certBundleEC);
Assert.assertEquals(2, certs.length);
Assert.assertEquals("CN=end-entity", certs[0].getSubjectX500Principal().getName());
Assert.assertEquals("CN=sub-ca", certs[1].getSubjectX500Principal().getName());

String invalidCertBundle = "foo\n";
Assert.assertThrows(PemException.class, () -> {
PemUtils.decodeCertificates(invalidCertBundle);
});

}

private void testPrivateKeyEncodeDecode(String origPrivateKeyPem) {
Expand Down
0