ExternalApp : Send barebone instead of with HTTP

This commit is contained in:
Matthias Guillitte
2025-12-03 17:12:36 +01:00
parent 7f22d2d55f
commit ce7c61075b

View File

@@ -5,7 +5,7 @@ import common.common.src.logger.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
@@ -16,7 +16,9 @@ import java.security.cert.CertificateException;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, InvalidKeyException, SignatureException, URISyntaxException {
private static String CERT_FOLDER = "assets/certs/";
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();
@@ -34,12 +36,12 @@ public class Main {
// Signer le JSON
KeyStore keyStore = KeyStore.getInstance("PKCS12");
try (FileInputStream pkFile = new FileInputStream("ma_cle.p12")) {
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("TODO : nom de la clé dans le trustore", "hepl".toCharArray());
PrivateKey privateKey = (PrivateKey) keyStore.getKey("externalApp", "heplhepl".toCharArray());
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(jsonString.getBytes(StandardCharsets.UTF_8));
@@ -54,21 +56,50 @@ public class Main {
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);
// === Envoi à l'ACS ===
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();
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
// Gestion des Keystore et Truststore
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keyStore, "heplhepl".toCharArray());
try (OutputStream os = con.getOutputStream()) {
os.write(finalPayload.getBytes(StandardCharsets.UTF_8));
Logger.displaySent("Payload final envoyé : " + finalPayload);
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);
int responseCode = con.getResponseCode();
Logger.displayReceived("Code de réponse reçu : " + responseCode);
// 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);
}
Logger.displayReceived("Réponse brute reçue : " + response.toString());
}
}
}