fix: improve certificate and private key normalization logic
This commit is contained in:
parent
ef402a02bb
commit
a3db5082ac
@ -212,24 +212,26 @@ public class AdfsTokenService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private X509Certificate getCertificate(String certificatePem) throws Exception {
|
private X509Certificate getCertificate(String certificatePem) throws Exception {
|
||||||
String normalized = certificatePem.replace("-----BEGIN CERTIFICATE-----", "")
|
String normalized = removePemHeaders(certificatePem);
|
||||||
.replace("-----END CERTIFICATE-----", "")
|
|
||||||
.replaceAll("\\s", "");
|
|
||||||
byte[] certBytes = Base64.getDecoder().decode(normalized);
|
byte[] certBytes = Base64.getDecoder().decode(normalized);
|
||||||
CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
||||||
return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes));
|
return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
private PrivateKey getPrivateKey(String privateKeyPem) throws Exception {
|
private PrivateKey getPrivateKey(String privateKeyPem) throws Exception {
|
||||||
String normalized = privateKeyPem.replaceAll("-----BEGIN (.*) PRIVATE KEY-----", "")
|
String normalized = removePemHeaders(privateKeyPem);
|
||||||
.replaceAll("-----END (.*) PRIVATE KEY-----", "")
|
|
||||||
.replaceAll("\\s", "");
|
|
||||||
byte[] keyBytes = Base64.getDecoder().decode(normalized);
|
byte[] keyBytes = Base64.getDecoder().decode(normalized);
|
||||||
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
|
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
return keyFactory.generatePrivate(spec);
|
return keyFactory.generatePrivate(spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String removePemHeaders(String pem) {
|
||||||
|
return pem.replaceAll("-----BEGIN (.*)-----", "")
|
||||||
|
.replaceAll("-----END (.*)-----", "")
|
||||||
|
.replaceAll("\\s+", "");
|
||||||
|
}
|
||||||
|
|
||||||
private String readFully(InputStream inputStream) throws Exception {
|
private String readFully(InputStream inputStream) throws Exception {
|
||||||
if (inputStream == null) {
|
if (inputStream == null) {
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user