fix: improve certificate and private key normalization logic

This commit is contained in:
Radek Davidek 2026-07-02 09:43:08 +02:00
parent ef402a02bb
commit a3db5082ac

View File

@ -212,24 +212,26 @@ public class AdfsTokenService {
}
private X509Certificate getCertificate(String certificatePem) throws Exception {
String normalized = certificatePem.replace("-----BEGIN CERTIFICATE-----", "")
.replace("-----END CERTIFICATE-----", "")
.replaceAll("\\s", "");
String normalized = removePemHeaders(certificatePem);
byte[] certBytes = Base64.getDecoder().decode(normalized);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes));
}
private PrivateKey getPrivateKey(String privateKeyPem) throws Exception {
String normalized = privateKeyPem.replaceAll("-----BEGIN (.*) PRIVATE KEY-----", "")
.replaceAll("-----END (.*) PRIVATE KEY-----", "")
.replaceAll("\\s", "");
String normalized = removePemHeaders(privateKeyPem);
byte[] keyBytes = Base64.getDecoder().decode(normalized);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(spec);
}
private String removePemHeaders(String pem) {
return pem.replaceAll("-----BEGIN (.*)-----", "")
.replaceAll("-----END (.*)-----", "")
.replaceAll("\\s+", "");
}
private String readFully(InputStream inputStream) throws Exception {
if (inputStream == null) {
return "";