Add logging messages

This commit is contained in:
Tobias Eidelpes 2020-12-30 12:07:00 +01:00
parent 24dd119c8e
commit a30e0591f4

View File

@ -13,6 +13,7 @@ import java.util.Base64;
import java.util.logging.Logger; import java.util.logging.Logger;
import at.ac.tuwien.dsg.orvell.Shell; import at.ac.tuwien.dsg.orvell.Shell;
import at.ac.tuwien.dsg.orvell.StopShellException;
import at.ac.tuwien.dsg.orvell.annotation.Command; import at.ac.tuwien.dsg.orvell.annotation.Command;
import dslab.ComponentFactory; import dslab.ComponentFactory;
import dslab.exception.FailedVerificationException; import dslab.exception.FailedVerificationException;
@ -24,8 +25,6 @@ import javax.crypto.spec.SecretKeySpec;
public class MessageClient implements IMessageClient, Runnable { public class MessageClient implements IMessageClient, Runnable {
private static final Logger logger = Logger.getLogger(MessageClient.class.getName()); private static final Logger logger = Logger.getLogger(MessageClient.class.getName());
private final String componentId;
private final InputStream consoleIn;
private final String transferHost; private final String transferHost;
private final int transferPort; private final int transferPort;
@ -42,7 +41,7 @@ public class MessageClient implements IMessageClient, Runnable {
private PrintWriter dmtpOut; private PrintWriter dmtpOut;
private BufferedReader dmtpIn; private BufferedReader dmtpIn;
private PrintStream dmapOut; private PrintWriter dmapOut;
private BufferedReader dmapIn; private BufferedReader dmapIn;
private final Shell shell; private final Shell shell;
@ -68,14 +67,11 @@ public class MessageClient implements IMessageClient, Runnable {
this.mailboxUser = config.getString("mailbox.user"); this.mailboxUser = config.getString("mailbox.user");
this.mailboxPassword = config.getString("mailbox.password"); this.mailboxPassword = config.getString("mailbox.password");
this.componentId = componentId;
this.consoleIn = in;
this.shell = new Shell(in, out); this.shell = new Shell(in, out);
this.shell.register(this); this.shell.register(this);
this.shell.setPrompt(this.componentId + "> "); this.shell.setPrompt(componentId + "> ");
logger.info(String.format("TransferHost: %s\nTransferPort: %d\nMailboxHost: %s\nMailboxPort: %d\nTransferEmail: %s\nMailboxUser: %s\nMailboxPassword: %s", logger.fine(String.format("TransferHost: %s\nTransferPort: %d\nMailboxHost: %s\nMailboxPort: %d\nTransferEmail: %s\nMailboxUser: %s\nMailboxPassword: %s",
transferHost, transferPort, mailboxHost, mailboxPort, transferEmail, mailboxUser, mailboxPassword)); transferHost, transferPort, mailboxHost, mailboxPort, transferEmail, mailboxUser, mailboxPassword));
} }
@ -85,13 +81,12 @@ public class MessageClient implements IMessageClient, Runnable {
logger.info("Starting connection to MailboxHost on " + this.mailboxHost + " on port " + this.mailboxPort); logger.info("Starting connection to MailboxHost on " + this.mailboxHost + " on port " + this.mailboxPort);
this.dmapSocket = new Socket(this.mailboxHost, this.mailboxPort); this.dmapSocket = new Socket(this.mailboxHost, this.mailboxPort);
this.dmapIn = new BufferedReader(new InputStreamReader(this.dmapSocket.getInputStream())); this.dmapIn = new BufferedReader(new InputStreamReader(this.dmapSocket.getInputStream()));
this.dmapOut = new PrintStream(this.dmapSocket.getOutputStream(), true); this.dmapOut = new PrintWriter(this.dmapSocket.getOutputStream(), true);
String input = null; String input = null;
String message = null;
input = dmapIn.readLine(); input = dmapIn.readLine();
logger.info("Received message from server: " + input); logger.fine("Received message from server: " + input);
if (!input.startsWith("ok DMAP2.0")) if (!input.startsWith("ok DMAP2.0"))
shutdown(); shutdown();
@ -123,16 +118,16 @@ public class MessageClient implements IMessageClient, Runnable {
String componentId; String componentId;
PublicKey serverPublicKey; PublicKey serverPublicKey;
this.dmapOut.println("startsecure"); this.dmapOut.println("startsecure");
logger.info("Sent command 'startsecure'"); logger.finer("Sent command 'startsecure'");
input = this.dmapIn.readLine(); input = this.dmapIn.readLine();
logger.info("Server's response: " + input); logger.finer("Server's response: " + input);
if (input.startsWith("ok") && (input.split("\\s+").length == 2)) { if (input.startsWith("ok") && (input.split("\\s+").length == 2)) {
// Get the component-id from the server // Get the component-id from the server
componentId = input.split("\\s+")[1]; componentId = input.split("\\s+")[1];
try { try {
// Attempt to read server public key from file called <component-id>_pub.der // Attempt to read server public key from file called <component-id>_pub.der
byte[] keyBytes = Files.readAllBytes(Paths.get("keys", "client", componentId + "_pub.der")); byte[] keyBytes = Files.readAllBytes(Paths.get("keys", "client", componentId + "_pub.der"));
logger.info("Read bytes from path " + Paths.get("keys", "client", componentId + "_pub.der") + ": " + Arrays.toString(keyBytes)); logger.finer("Read bytes from path " + Paths.get("keys", "client", componentId + "_pub.der") + ": " + Arrays.toString(keyBytes));
// Create X509 spec object from key // Create X509 spec object from key
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
// Create generator for RSA scheme // Create generator for RSA scheme
@ -141,14 +136,14 @@ public class MessageClient implements IMessageClient, Runnable {
serverPublicKey = kf.generatePublic(spec); serverPublicKey = kf.generatePublic(spec);
String clientChallenge = generateChallengeMessage(serverPublicKey); String clientChallenge = generateChallengeMessage(serverPublicKey);
// Send clientChallenge to server // Send clientChallenge to server
logger.info("Send clientchallenge to Server: " + clientChallenge); logger.finer("Send clientchallenge to Server: " + clientChallenge);
this.dmapOut.println(clientChallenge); this.dmapOut.println(clientChallenge);
// Receive AES encrypted message saying "ok <client-challenge>" // Receive AES encrypted message saying "ok <client-challenge>"
String response = this.dmapIn.readLine(); String response = this.dmapIn.readLine();
// Compare received client challenge with generated client challenge // Compare received client challenge with generated client challenge
verifyChallenge(response); verifyChallenge(response);
// Answer with AES encrypted "ok" if matching and use AES cipher for subsequent communication // Answer with AES encrypted "ok" if matching and use AES cipher for subsequent communication
logger.info("Send ciphered 'ok' to Server: " + getAesCiphertext("ok")); logger.finer("Send ciphered 'ok' to Server: " + getAesCiphertext("ok"));
this.dmapOut.println(getAesCiphertext("ok")); this.dmapOut.println(getAesCiphertext("ok"));
} catch (NoSuchAlgorithmException | InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException | FailedVerificationException e) { } catch (NoSuchAlgorithmException | InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException | FailedVerificationException e) {
logger.severe(e.getMessage()); logger.severe(e.getMessage());
@ -190,10 +185,11 @@ public class MessageClient implements IMessageClient, Runnable {
*/ */
private void verifyChallenge(String response) throws FailedVerificationException { private void verifyChallenge(String response) throws FailedVerificationException {
// Decrypt to base64 encoded byte array // Decrypt to base64 encoded byte array
logger.info("Verifying challenge...");
String plainText; String plainText;
try { try {
plainText = new String(aesDecryptCipher.doFinal(Base64.getDecoder().decode(response))); plainText = new String(aesDecryptCipher.doFinal(Base64.getDecoder().decode(response)));
logger.info("Decrypted AES challenge: " + plainText); logger.finer("Decrypted AES challenge: " + plainText);
} catch (IllegalBlockSizeException | BadPaddingException e) { } catch (IllegalBlockSizeException | BadPaddingException e) {
logger.severe("Error during decryption of client challenge. Aborting..."); logger.severe("Error during decryption of client challenge. Aborting...");
shutdown(); shutdown();
@ -324,24 +320,28 @@ public class MessageClient implements IMessageClient, Runnable {
@Command @Command
@Override @Override
public void inbox() { public void inbox() {
logger.info("Received 'inbox' command");
} }
@Command @Command
@Override @Override
public void delete(String id) { public void delete(String id) {
logger.info("Received 'delete' command for id " + id);
} }
@Command @Command
@Override @Override
public void verify(String id) { public void verify(String id) {
logger.info("Received 'verify' command for id " + id);
} }
@Command @Command
@Override @Override
public void msg(String to, String subject, String data) { public void msg(String to, String subject, String data) {
logger.info("Received 'msg' command:\tto: " + to + "\tsubject: " + subject + "\tdata: " + data);
try { try {
logger.info("Starting connection to TransferServer on host " + this.transferHost + " on port " + this.transferPort); logger.info("Starting connection to TransferServer on host " + this.transferHost + " on port " + this.transferPort);
this.dmtpSocket = new Socket(this.transferHost, this.transferPort); this.dmtpSocket = new Socket(this.transferHost, this.transferPort);
@ -356,6 +356,7 @@ public class MessageClient implements IMessageClient, Runnable {
@Command @Command
@Override @Override
public void shutdown() { public void shutdown() {
logger.info("Received 'shutdown' command");
if (dmtpSocket != null) { if (dmtpSocket != null) {
try { try {
this.dmtpSocket.close(); this.dmtpSocket.close();
@ -379,6 +380,7 @@ public class MessageClient implements IMessageClient, Runnable {
} }
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new StopShellException();
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {