Merge remote-tracking branch 'origin/main'
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
src/main/java/common/common/src/ports/Ports.java
Normal file
9
src/main/java/common/common/src/ports/Ports.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package common.common.src.ports;
|
||||
|
||||
public class Ports {
|
||||
/**
|
||||
* Port d'écoute du service ACS pour la communication avec l'application externe.
|
||||
*/
|
||||
public static int PORT_AUTH = 8786;
|
||||
public static String ACS_HOST = "127.0.0.1";
|
||||
}
|
||||
@@ -1,9 +1,75 @@
|
||||
package externalApp.externalApp.src;
|
||||
|
||||
import common.common.src.ports.Ports;
|
||||
import common.common.src.logger.Logger;
|
||||
import org.gradle.internal.impldep.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gradle.internal.impldep.com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.*;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.Base64;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Logger.displayInfo("Hello World");
|
||||
public static void main(String[] args) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, InvalidKeyException, SignatureException, URISyntaxException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Quel est la date d'expiration de la carte de crédit ? (MM/AA)");
|
||||
String expirationDate = br.readLine();
|
||||
Logger.displayInfo("Date d'expiration saisie : " + expirationDate);
|
||||
System.out.println("Numéro de la carte de crédit : ");
|
||||
String cardNumber = br.readLine();
|
||||
Logger.displayInfo("Numéro de carte saisi : " + cardNumber);
|
||||
|
||||
// Construction du JSON
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode json = mapper.createObjectNode();
|
||||
json.put("expirationDate", expirationDate);
|
||||
json.put("cardNumber", cardNumber);
|
||||
String jsonString = mapper.writeValueAsString(json);
|
||||
|
||||
// Signer le JSON
|
||||
KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
||||
char[] password = /* TODO */.toCharArray();
|
||||
try (FileInputStream pkFile = new FileInputStream("ma_cle.p12")) {
|
||||
keyStore.load(pkFile, password);
|
||||
} catch (CertificateException | NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
PrivateKey privateKey = (PrivateKey) keyStore.getKey(/* TODO */, password);
|
||||
Signature signature = Signature.getInstance("SHA256withRSA");
|
||||
signature.initSign(privateKey);
|
||||
signature.update(jsonString.getBytes(StandardCharsets.UTF_8));
|
||||
byte[] signedBytes = signature.sign();
|
||||
|
||||
String signatureBase64 = Base64.getEncoder().encodeToString(signedBytes);
|
||||
|
||||
// Construction du JSON final avec la signature
|
||||
ObjectNode finalJson = mapper.createObjectNode();
|
||||
finalJson.put("data", jsonString);
|
||||
finalJson.put("signature", signatureBase64);
|
||||
|
||||
String finalPayload = mapper.writeValueAsString(finalJson);
|
||||
|
||||
// Envoi à l'ACS
|
||||
Logger.displayInfo("Envoi des informations au service d'authentification sur le port " + Ports.PORT_AUTH + " à l'hôte " + Ports.ACS_HOST);
|
||||
URL url = new URI("https://" + Ports.ACS_HOST + ":" + Ports.PORT_AUTH).toURL();
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setDoOutput(true);
|
||||
|
||||
try (OutputStream os = con.getOutputStream()) {
|
||||
os.write(finalPayload.getBytes(StandardCharsets.UTF_8));
|
||||
Logger.displaySent("Payload final envoyé : " + finalPayload);
|
||||
}
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
Logger.displayReceived("Code de réponse reçu : " + responseCode);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user