Refactor external app but broke some stuff

This commit is contained in:
2025-12-06 17:19:33 +01:00
parent b19da74c53
commit 2f8bcfb7f1
6 changed files with 148 additions and 143 deletions

View File

@@ -1,5 +1,5 @@
package acq.acq.src;
import common.common.src.crypto.KeyLoader;
import common.common.src.crypto.KeyFactory;
import common.common.src.logger.Logger;
import common.common.src.requestHandlers.SSLServerFactory;
import common.common.src.socket.SocketManager;
@@ -20,10 +20,10 @@ public class Main {
public static void main(String[] args) throws Exception {
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
KeyFactory keyFactory = new KeyFactory();
KeyManagerFactory kmf = loader.loadKeyStore();
TrustManagerFactory tmf = loader.loadTrustStore();
KeyManagerFactory kmf = keyFactory.loadKeyStore(KEY_STORE_PATH, KEY_STORE_PWD);
TrustManagerFactory tmf = keyFactory.loadTrustStore(TRUST_STORE_PATH, TRUST_STORE_PWD);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

View File

@@ -1,6 +1,6 @@
package acs.acs.src;
import common.common.src.crypto.KeyLoader;
import common.common.src.crypto.KeyFactory;
import common.common.src.logger.Logger;
import common.common.src.requestHandlers.SSLServerFactory;
import common.common.src.socket.SocketManager;
@@ -22,10 +22,10 @@ public class Main {
public static void main(String[] args) throws Exception {
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
KeyFactory loader = new KeyFactory();
KeyManagerFactory kmf = loader.loadKeyStore();
TrustManagerFactory tmf = loader.loadTrustStore();
KeyManagerFactory kmf = loader.loadKeyStore(KEY_STORE_PATH, KEY_STORE_PWD);
TrustManagerFactory tmf = loader.loadTrustStore(TRUST_STORE_PATH, TRUST_STORE_PWD);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
@@ -55,7 +55,7 @@ public class Main {
try{
String response = SocketManager.readResponse(clientSocket);
Logger.displayReceived(response);
SocketManager.send(clientSocket, "token");
SocketManager.send(clientSocket, "TOKEN_HERE");
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@@ -0,0 +1,64 @@
package common.common.src.crypto;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.security.*;
public class KeyFactory {
public PrivateKey createPrivateKey(KeyStore keyStore, String alias, String keystorePwd) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException {
return (PrivateKey) keyStore.getKey(alias, keystorePwd.toCharArray());
}
public KeyStore createKeyStore(String algorithm, String keyStorePath, String pwd){
try (FileInputStream fis = new FileInputStream(keyStorePath)) {
char[] keystorePass = pwd.toCharArray();
KeyStore ks = KeyStore.getInstance(algorithm);
ks.load(fis, keystorePass);
return ks;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public KeyManagerFactory loadKeyStore(String path, String pwd) {
KeyManagerFactory kmf = null;
try (FileInputStream fis = new FileInputStream(path)) {
char[] keystorePass = 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;
}
public TrustManagerFactory loadTrustStore(String path, String pwd) {
TrustManagerFactory tmf = null;
try (FileInputStream fis = new FileInputStream(path)) {
char[] truststorePass = 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;
}
}

View File

@@ -1,61 +0,0 @@
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;
}
}

View File

@@ -1,12 +1,15 @@
package externalApp.externalApp.src;
import common.common.src.crypto.KeyFactory;
import common.common.src.ports.Ports;
import common.common.src.logger.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import common.common.src.socket.SocketManager;
import javax.net.ssl.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@@ -15,91 +18,95 @@ import java.security.*;
import java.security.cert.CertificateException;
import java.util.Base64;
import static common.common.src.ports.Ports.ACS_SERVER_PORT;
public class Main {
private static String CERT_FOLDER = "assets/certs/";
private static final String KEY_STORE_PATH = "assets/certs/externalApp.p12";
private static final String KEY_STORE_PWD = "hepl";
private static final String TRUST_STORE_PATH = "assets/certs/externalApp.jks";
private static final String TRUST_STORE_PWD = "heplhepl";
public static void main(String[] args) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, InvalidKeyException, SignatureException, URISyntaxException, CertificateException, KeyManagementException {
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);
String expirationDate = acquireStringInput( br,"Quel est la date d'expiration de la carte de crédit ? (MM/AA)");
String cardNumber = acquireStringInput(br, "Numéro de la carte de crédit : ");
// Construction du JSON
ObjectMapper mapper = new ObjectMapper();
ObjectNode json = mapper.createObjectNode();
json.put("expirationDate", expirationDate);
json.put("cardNumber", cardNumber);
String jsonString = mapper.writeValueAsString(json);
String jsonString = buildCreditCardJson(expirationDate, cardNumber);
KeyFactory keyFactory = new KeyFactory();
KeyStore keyStore = keyFactory.createKeyStore("PKCS12", KEY_STORE_PATH, TRUST_STORE_PWD);
PrivateKey privateKey = keyFactory.createPrivateKey(keyStore, "externalApp", TRUST_STORE_PWD);
// Signer le JSON
KeyStore keyStore = KeyStore.getInstance("PKCS12");
try (FileInputStream pkFile = new FileInputStream(CERT_FOLDER + "externalApp.p12")) {
keyStore.load(pkFile, "heplhepl".toCharArray());
} catch (CertificateException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
PrivateKey privateKey = (PrivateKey) keyStore.getKey("externalApp", "heplhepl".toCharArray());
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);
String finalPayload = buildFinalJson(jsonString, signatureBase64);
// Construction du JSON final avec la signature
ObjectNode finalJson = mapper.createObjectNode();
finalJson.put("data", jsonString);
finalJson.put("signature", signatureBase64);
KeyManagerFactory kmf = keyFactory.loadKeyStore(KEY_STORE_PATH, KEY_STORE_PWD);
TrustManagerFactory tmf = keyFactory.loadTrustStore(TRUST_STORE_PATH, TRUST_STORE_PWD);
String finalPayload = mapper.writeValueAsString(finalJson);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
// === Envoi à l'ACS ===
sendToACS(ctx, finalPayload);
}
private static String acquireStringInput(BufferedReader reader, String message) throws IOException {
System.out.print(message);
return reader.readLine();
}
private static String buildCreditCardJson(String expirationDate, String cardNumber) {
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode json = mapper.createObjectNode();
json.put("expirationDate", expirationDate);
json.put("cardNumber", cardNumber);
return mapper.writeValueAsString(json);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String buildFinalJson(String data, String signature) {
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode json = mapper.createObjectNode();
json.put("data", data);
json.put("signature", signature);
return mapper.writeValueAsString(json);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static void sendToACS(SSLContext ctx, String payload) throws URISyntaxException, MalformedURLException {
Logger.displayInfo("Envoi des informations au service d'authentification à l'hôte " + Ports.ACS_HOST + ":" + Ports.PORT_AUTH);
URL url = new URI("https://" + Ports.ACS_HOST + ":" + Ports.PORT_AUTH).toURL();
// Gestion des Keystore et Truststore
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keyStore, "heplhepl".toCharArray());
SSLSocketFactory factory = ctx.getSocketFactory();
try (SSLSocket socket = (SSLSocket) factory.createSocket(url.toString(), ACS_SERVER_PORT)) {
socket.startHandshake();
KeyStore ts = KeyStore.getInstance("JKS");
try (FileInputStream fis = new FileInputStream("assets/certs/acs-trust.jks")) {
ts.load(fis, "heplhepl".toCharArray());
}
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
SocketManager.send(socket, payload);
Logger.displaySent(payload);
// SSL Connection
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLSocketFactory factory = sslContext.getSocketFactory();
try (SSLSocket sslSocket = (SSLSocket) factory.createSocket(Ports.ACS_HOST, Ports.PORT_AUTH)) {
// Démarrage du handshake TLS
sslSocket.startHandshake();
Logger.displayInfo("Connexion SSL établie avec " + Ports.ACS_HOST + ":" + Ports.PORT_AUTH);
// 4. Envoi des données brutes (JSON signé)
OutputStream out = sslSocket.getOutputStream();
out.write(finalPayload.getBytes(StandardCharsets.UTF_8));
out.flush();
Logger.displaySent("Payload envoyé en brut : " + finalPayload);
// 5. Lecture réponse brute
BufferedReader reader = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
String response = SocketManager.readResponse(socket);
Logger.displayReceived(response);
} catch (Exception e) {
throw new RuntimeException(e);
}
Logger.displayReceived("Réponse brute reçue : " + response.toString());
}
}
}

View File

@@ -3,7 +3,7 @@ package httpServer.httpServer.src;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
import com.sun.net.httpserver.HttpsServer;
import common.common.src.crypto.KeyLoader;
import common.common.src.crypto.KeyFactory;
import common.common.src.logger.Logger;
import httpServer.httpServer.src.handlers.IRequestHandler;
import httpServer.httpServer.src.handlers.RequestHandler;
@@ -12,8 +12,6 @@ import httpServer.httpServer.src.interceptors.RequestInterceptor;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManagerFactory;
import java.io.*;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
@@ -24,16 +22,13 @@ public class Main {
private static final String KEY_STORE_PATH = "assets/certs/https.p12";
private static final String KEY_STORE_PWD = "hepl";
private static final String TRUST_STORE_PATH = "assets/certs/https-trust.jks";
private static final String TRUST_STORE_PWD = "heplhepl";
public static void main(String[] args) {
try {
HttpsServer server = HttpsServer.create(new InetSocketAddress(HTTP_SERVER_PORT), 5);
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
KeyManagerFactory kmf = loader.loadKeyStore();
KeyFactory loader = new KeyFactory();
KeyManagerFactory kmf = loader.loadKeyStore(KEY_STORE_PATH, KEY_STORE_PWD);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), null, null);