First acs and acq
This commit is contained in:
@@ -1,7 +1,50 @@
|
||||
package acq.acq.src;
|
||||
// File: AcqClient.java
|
||||
import javax.net.ssl.*;
|
||||
import java.io.*;
|
||||
import java.security.KeyStore;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String host = "localhost";
|
||||
int port = 8443;
|
||||
|
||||
// If client needs to present cert (mTLS)
|
||||
String keystorePath = "assets/certs/acq.p12";
|
||||
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");
|
||||
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||
|
||||
SSLSocketFactory factory = ctx.getSocketFactory();
|
||||
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
|
||||
socket.startHandshake();
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
|
||||
out.write("Hello ACS\n");
|
||||
out.flush();
|
||||
|
||||
String resp = in.readLine();
|
||||
System.out.println("Response from ACS: " + resp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,57 @@
|
||||
package acs.acs.src;
|
||||
|
||||
// File: AcsServer.java
|
||||
import javax.net.ssl.*;
|
||||
import java.io.*;
|
||||
import java.security.KeyStore;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
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)
|
||||
String truststorePath = "assets/certs/acs-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");
|
||||
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||
|
||||
SSLServerSocketFactory ssf = ctx.getServerSocketFactory();
|
||||
SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket(port);
|
||||
// si vous voulez mTLS :
|
||||
serverSocket.setNeedClientAuth(true);
|
||||
|
||||
System.out.println("ACS listening on port " + port);
|
||||
while (true) {
|
||||
try (SSLSocket socket = (SSLSocket) serverSocket.accept()) {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
|
||||
String line = in.readLine(); // simple single-line message
|
||||
System.out.println("Received from ACQ: " + line);
|
||||
|
||||
out.write("ACK from ACS\n");
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user