From 0c5a166b34ca0d56fd852ea12ad30e35ddb48570 Mon Sep 17 00:00:00 2001 From: Alexander-Kreutz Date: Fri, 10 May 2024 11:20:38 +0200 Subject: [PATCH] fix keystore loader --- src/main/java/ch/bfh/ti/i4mi/mag/Config.java | 38 +++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/java/ch/bfh/ti/i4mi/mag/Config.java b/src/main/java/ch/bfh/ti/i4mi/mag/Config.java index a9a38f37..11ae6c74 100644 --- a/src/main/java/ch/bfh/ti/i4mi/mag/Config.java +++ b/src/main/java/ch/bfh/ti/i4mi/mag/Config.java @@ -245,17 +245,20 @@ public class Config { havingValue = "true", matchIfMissing = false) public SSLContextParameters getPixSSLContext() throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException { - + KeyStoreParameters ksp = new KeyStoreParameters(); - // Keystore file may be found at src/main/resources - //ksp.setResource(keystore); - //ksp.setPassword(keystorePassword); - + // https://www.baeldung.com/java-keystore KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); log.info("keystore base64 valued: " + (keystoreBase64 != null && !keystoreBase64.trim().isEmpty())); - ks.load(ReadCertificateStream(), keystorePassword.toCharArray()); - ksp.setKeyStore(ks); + if (keystoreBase64 != null && !keystoreBase64.trim().isEmpty()) { + ks.load(ReadCertificateStream(), keystorePassword.toCharArray()); + ksp.setKeyStore(ks); + } else { + // Keystore file may be found at src/main/resources + ksp.setResource(keystore); + ksp.setPassword(keystorePassword); + } KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); @@ -280,14 +283,18 @@ public SSLContextParameters getPixSSLContext() throws IOException, CertificateEx public SSLContextParameters getAuditSSLContext() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException { KeyStoreParameters ksp = new KeyStoreParameters(); - // Keystore file may be found at src/main/resources - //ksp.setResource(keystore); - //ksp.setPassword(keystorePassword); - + // https://www.baeldung.com/java-keystore KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); - ks.load(ReadCertificateStream(), keystorePassword.toCharArray()); - ksp.setKeyStore(ks); + log.info("keystore base64 valued: " + (keystoreBase64 != null && !keystoreBase64.trim().isEmpty())); + if (keystoreBase64 != null && !keystoreBase64.trim().isEmpty()) { + ks.load(ReadCertificateStream(), keystorePassword.toCharArray()); + ksp.setKeyStore(ks); + } else { + // Keystore file may be found at src/main/resources + ksp.setResource(keystore); + ksp.setPassword(keystorePassword); + } KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); @@ -348,10 +355,7 @@ public FilterRegistrationBean corsFilterRegistration() { return frb; } - private InputStream ReadCertificateStream () throws FileNotFoundException { - if (keystoreBase64 == null || keystoreBase64.trim().isEmpty()){ - return new FileInputStream(keystore); - } + private InputStream ReadCertificateStream () throws FileNotFoundException { byte[] decodedBytes = Base64.getDecoder().decode(keystoreBase64); return new ByteArrayInputStream(decodedBytes); }