Mercurial > stress-tester
changeset 1052:9b341057540b
SSLUtils, JKS keystores for test
author | Devel 2 |
---|---|
date | Wed, 15 Apr 2020 10:14:57 +0200 |
parents | ef65dfd56fdb |
children | 6b8d78ecdeba |
files | stress-tester/src/main/java/com/passus/st/utils/SSLUtils.java stress-tester/src/test/java/com/passus/st/utils/TestSslUtils.java stress-tester/src/test/resources/certs/amb_keystore.jks stress-tester/src/test/resources/certs/amb_truststore.jks |
diffstat | 4 files changed, 108 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/utils/SSLUtils.java Wed Apr 15 10:14:57 2020 +0200 @@ -0,0 +1,70 @@ +package com.passus.st.utils; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManagerFactory; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyStore; + +public class SSLUtils { + + private SSLUtils() { + } + + public static KeyStore loadKeyStore(String fileName, char[] storePassword) throws IOException { + return loadKeyStore(new File(fileName), storePassword); + } + + public static KeyStore loadKeyStore(File storeFile, char[] storePassword) throws IOException { + try { + KeyStore ks; + if (storeFile.exists()) { + ks = KeyStore.getInstance(KeyStore.getDefaultType()); + try (FileInputStream ksfis = new FileInputStream(storeFile)) { + ks.load(ksfis, storePassword); + } + } else { + throw new IOException("Store file '" + storeFile + "' does not exist."); + } + + return ks; + } catch (Exception e) { + throw new IOException("Unable to open file '" + storeFile + "'. " + e.getMessage(), e); + } + } + + public static KeyManagerFactory loadKeyManagerFactory(String fileName, String password) throws IOException, SecurityException, GeneralSecurityException { + return loadKeyManagerFactory(new File(fileName), password); + } + + public static KeyManagerFactory loadKeyManagerFactory(File file, String password) throws IOException, SecurityException, GeneralSecurityException { + char[] storePassword = null; + if (password.length() != 0) { + storePassword = password.toCharArray(); + } + + KeyStore ks = loadKeyStore(file, storePassword); + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + kmf.init(ks, storePassword); + return kmf; + } + + public static TrustManagerFactory loadTrustManagerFactory(String fileName, String password) throws IOException, SecurityException, GeneralSecurityException { + return loadTrustManagerFactory(new File(fileName), password); + } + + public static TrustManagerFactory loadTrustManagerFactory(File file, String password) throws IOException, SecurityException, GeneralSecurityException { + char[] storePassword = null; + if (password.length() != 0) { + storePassword = password.toCharArray(); + } + + KeyStore ks = loadKeyStore(file, storePassword); + TrustManagerFactory tmf = TrustManagerFactory.getInstance( + TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(ks); + return tmf; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/utils/TestSslUtils.java Wed Apr 15 10:14:57 2020 +0200 @@ -0,0 +1,38 @@ +package com.passus.st.utils; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManagerFactory; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyStore; + +import static com.passus.st.utils.TestResourceUtils.getFile; + +public class TestSslUtils { + + public static final String DEFAULT_KEY_STORE_FILE; + + public static final String DEFAULT_TRUST_STORE_FILE; + + public static final String DEFAULT_KEY_STORE_PASS = "secret"; + + static { + DEFAULT_KEY_STORE_FILE = getFile("certs/amb_keystore.jks").getAbsolutePath(); + DEFAULT_TRUST_STORE_FILE = getFile("certs/amb_truststore.jks").getAbsolutePath(); + } + + private TestSslUtils() { + } + + public static KeyStore loadKeyStore() throws IOException { + return SSLUtils.loadKeyStore(DEFAULT_KEY_STORE_FILE, DEFAULT_KEY_STORE_PASS.toCharArray()); + } + + public static TrustManagerFactory loadTrustManagerFactory() throws IOException, GeneralSecurityException { + return SSLUtils.loadTrustManagerFactory(DEFAULT_TRUST_STORE_FILE, DEFAULT_KEY_STORE_PASS); + } + + public static KeyManagerFactory loadKeyManagerFactory() { + return null; + } +}