Skip to content

Commit

Permalink
fix keystore loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Kreutz committed May 10, 2024
1 parent 2c9e967 commit 0c5a166
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/main/java/ch/bfh/ti/i4mi/mag/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -348,10 +355,7 @@ public FilterRegistrationBean<Filter> 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);
}
Expand Down

0 comments on commit 0c5a166

Please sign in to comment.