Extract loader methods into a dedicated class
This commit is contained in:
@@ -1,15 +1,11 @@
|
|||||||
package acq.acq.src;
|
package acq.acq.src;
|
||||||
// File: AcqClient.java
|
import common.common.src.crypto.KeyLoader;
|
||||||
import common.common.src.logger.Logger;
|
import common.common.src.logger.Logger;
|
||||||
import common.common.src.socket.SocketManager;
|
import common.common.src.socket.SocketManager;
|
||||||
|
|
||||||
import javax.net.ssl.*;
|
import javax.net.ssl.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
import java.security.KeyStoreException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.security.UnrecoverableKeyException;
|
|
||||||
import java.security.cert.CertificateException;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
@@ -23,8 +19,10 @@ public class Main {
|
|||||||
String host = "localhost";
|
String host = "localhost";
|
||||||
int port = 8443;
|
int port = 8443;
|
||||||
|
|
||||||
KeyManagerFactory kmf = loadKeyStore();
|
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
|
||||||
TrustManagerFactory tmf = loadTrustStore();
|
|
||||||
|
KeyManagerFactory kmf = loader.loadKeyStore();
|
||||||
|
TrustManagerFactory tmf = loader.loadTrustStore();
|
||||||
|
|
||||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||||
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||||
@@ -37,7 +35,6 @@ public class Main {
|
|||||||
SocketManager.send(socket, message);
|
SocketManager.send(socket, message);
|
||||||
Logger.displaySent(message);
|
Logger.displaySent(message);
|
||||||
|
|
||||||
|
|
||||||
String response = SocketManager.readResponse(socket);
|
String response = SocketManager.readResponse(socket);
|
||||||
Logger.displayReceived(response);
|
Logger.displayReceived(response);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -45,41 +42,5 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static KeyManagerFactory loadKeyStore() {
|
|
||||||
|
|
||||||
KeyManagerFactory kmf = null;
|
|
||||||
|
|
||||||
try (FileInputStream fis = new FileInputStream(KEY_STORE_PATH)) {
|
|
||||||
char[] keystorePass = KEY_STORE_PWD.toCharArray();
|
|
||||||
|
|
||||||
KeyStore ks = KeyStore.getInstance("PKCS12");
|
|
||||||
ks.load(fis, keystorePass);
|
|
||||||
|
|
||||||
kmf = KeyManagerFactory.getInstance("SunX509");
|
|
||||||
kmf.init(ks, keystorePass);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return kmf;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static TrustManagerFactory loadTrustStore() {
|
|
||||||
|
|
||||||
TrustManagerFactory tmf = null;
|
|
||||||
|
|
||||||
try (FileInputStream fis = new FileInputStream(TRUST_STORE_PATH)) {
|
|
||||||
char[] truststorePass = TRUST_STORE_PWD.toCharArray();
|
|
||||||
|
|
||||||
KeyStore ts = KeyStore.getInstance("JKS");
|
|
||||||
ts.load(fis, truststorePass);
|
|
||||||
|
|
||||||
tmf = TrustManagerFactory.getInstance("SunX509");
|
|
||||||
tmf.init(ts);
|
|
||||||
} catch (Exception e){
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tmf;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,27 @@
|
|||||||
package acs.acs.src;
|
package acs.acs.src;
|
||||||
|
|
||||||
// File: AcsServer.java
|
// File: AcsServer.java
|
||||||
|
import common.common.src.crypto.KeyLoader;
|
||||||
|
|
||||||
import javax.net.ssl.*;
|
import javax.net.ssl.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
private static final String KEY_STORE_PATH = "assets/certs/acs.p12";
|
||||||
|
private static final String KEY_STORE_PWD = "hepl";
|
||||||
|
|
||||||
|
private static final String TRUST_STORE_PATH = "assets/certs/acs-trust.jks";
|
||||||
|
private static final String TRUST_STORE_PWD = "heplhepl";
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
int port = 8443;
|
int port = 8443;
|
||||||
// Keystore containing server private key + cert (PKCS12 or JKS)
|
|
||||||
String keystorePath = "assets/certs/acs.p12";
|
|
||||||
char[] keystorePass = "hepl".toCharArray();
|
|
||||||
|
|
||||||
// Truststore (to verify client if mutual TLS)
|
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
|
||||||
String truststorePath = "assets/certs/acs-trust.jks";
|
|
||||||
char[] truststorePass = "heplhepl".toCharArray();
|
|
||||||
|
|
||||||
KeyStore ks = KeyStore.getInstance("PKCS12");
|
KeyManagerFactory kmf = loader.loadKeyStore();
|
||||||
try (FileInputStream fis = new FileInputStream(keystorePath)) {
|
TrustManagerFactory tmf = loader.loadTrustStore();
|
||||||
ks.load(fis, keystorePass);
|
|
||||||
}
|
|
||||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
|
||||||
kmf.init(ks, keystorePass);
|
|
||||||
|
|
||||||
KeyStore ts = KeyStore.getInstance("JKS");
|
|
||||||
try (FileInputStream fis = new FileInputStream(truststorePath)) {
|
|
||||||
ts.load(fis, truststorePass);
|
|
||||||
}
|
|
||||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
|
|
||||||
tmf.init(ts);
|
|
||||||
|
|
||||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||||
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||||
|
|||||||
61
src/main/java/common/common/src/crypto/KeyLoader.java
Normal file
61
src/main/java/common/common/src/crypto/KeyLoader.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package common.common.src.crypto;
|
||||||
|
|
||||||
|
import javax.net.ssl.KeyManagerFactory;
|
||||||
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
|
||||||
|
public class KeyLoader {
|
||||||
|
|
||||||
|
private final String keyStorePath;
|
||||||
|
private final String keystorePwd;
|
||||||
|
|
||||||
|
private final String trustStorePath;
|
||||||
|
private final String trustStorePwd;
|
||||||
|
|
||||||
|
public KeyLoader(String keyStorePath, String keystorePwd, String trustStorePath, String trusttorePwd) {
|
||||||
|
this.keyStorePath = keyStorePath;
|
||||||
|
this.keystorePwd = keystorePwd;
|
||||||
|
this.trustStorePath = trustStorePath;
|
||||||
|
this.trustStorePwd = trusttorePwd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public KeyManagerFactory loadKeyStore() {
|
||||||
|
|
||||||
|
KeyManagerFactory kmf = null;
|
||||||
|
|
||||||
|
try (FileInputStream fis = new FileInputStream(keyStorePath)) {
|
||||||
|
char[] keystorePass = keystorePwd.toCharArray();
|
||||||
|
|
||||||
|
KeyStore ks = KeyStore.getInstance("PKCS12");
|
||||||
|
ks.load(fis, keystorePass);
|
||||||
|
|
||||||
|
kmf = KeyManagerFactory.getInstance("SunX509");
|
||||||
|
kmf.init(ks, keystorePass);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return kmf;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TrustManagerFactory loadTrustStore() {
|
||||||
|
|
||||||
|
TrustManagerFactory tmf = null;
|
||||||
|
|
||||||
|
try (FileInputStream fis = new FileInputStream(trustStorePath)) {
|
||||||
|
char[] truststorePass = trustStorePwd.toCharArray();
|
||||||
|
|
||||||
|
KeyStore ts = KeyStore.getInstance("JKS");
|
||||||
|
ts.load(fis, truststorePass);
|
||||||
|
|
||||||
|
tmf = TrustManagerFactory.getInstance("SunX509");
|
||||||
|
tmf.init(ts);
|
||||||
|
} catch (Exception e){
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmf;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user