Extract trust store and key store methods
This commit is contained in:
@@ -1,35 +1,30 @@
|
|||||||
package acq.acq.src;
|
package acq.acq.src;
|
||||||
// File: AcqClient.java
|
// File: AcqClient.java
|
||||||
|
import common.common.src.logger.Logger;
|
||||||
|
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 {
|
||||||
|
|
||||||
|
private static final String KEY_STORE_PATH = "assets/certs/acq.p12";
|
||||||
|
private static final String KEY_STORE_PWD = "hepl";
|
||||||
|
|
||||||
|
private static final String TRUST_STORE_PATH = "assets/certs/acq-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 {
|
||||||
String host = "localhost";
|
String host = "localhost";
|
||||||
int port = 8443;
|
int port = 8443;
|
||||||
|
|
||||||
// If client needs to present cert (mTLS)
|
KeyManagerFactory kmf = loadKeyStore();
|
||||||
String keystorePath = "assets/certs/acq.p12";
|
TrustManagerFactory tmf = loadTrustStore();
|
||||||
char[] keystorePass = "hepl".toCharArray();
|
|
||||||
|
|
||||||
// Truststore to trust the server's CA
|
|
||||||
String truststorePath = "assets/certs/acq-trust.jks";
|
|
||||||
char[] truststorePass = "heplhepl".toCharArray();
|
|
||||||
|
|
||||||
KeyStore ks = KeyStore.getInstance("PKCS12");
|
|
||||||
try (FileInputStream fis = new FileInputStream(keystorePath)) {
|
|
||||||
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);
|
||||||
@@ -37,14 +32,54 @@ public class Main {
|
|||||||
SSLSocketFactory factory = ctx.getSocketFactory();
|
SSLSocketFactory factory = ctx.getSocketFactory();
|
||||||
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
|
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
|
||||||
socket.startHandshake();
|
socket.startHandshake();
|
||||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
|
||||||
|
|
||||||
out.write("Hello ACS\n");
|
String message = "Hello ACS\n";
|
||||||
out.flush();
|
SocketManager.send(socket, message);
|
||||||
|
Logger.displaySent(message);
|
||||||
|
|
||||||
String resp = in.readLine();
|
|
||||||
System.out.println("Response from ACS: " + resp);
|
String response = SocketManager.readResponse(socket);
|
||||||
|
Logger.displayReceived(response);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package common.common.src.socket;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -17,4 +18,9 @@ public class SocketManager {
|
|||||||
public static String readResponse(BufferedReader reader) throws IOException {
|
public static String readResponse(BufferedReader reader) throws IOException {
|
||||||
return reader.readLine();
|
return reader.readLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String readResponse(Socket socket) throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||||
|
return reader.readLine();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user