Respond encrypted if 'startsecure' set

Upon sending a message, the server checks whether 'startsecure' has
been issued before or not. If yes, the response is encrypted.
This commit is contained in:
Tobias Eidelpes 2020-12-29 20:34:47 +01:00
parent 93a2a87f07
commit cab09d1949

View File

@ -61,11 +61,18 @@ public class DMAPConnection implements Runnable {
String userInput; String userInput;
while (!Thread.currentThread().isInterrupted() && (userInput = in.readLine()) != null) { while (!Thread.currentThread().isInterrupted() && (userInput = in.readLine()) != null) {
if (secure) {
userInput = getAesPlaintext(userInput);
}
if ("quit".equals(userInput)) { if ("quit".equals(userInput)) {
out.println("ok bye"); if (secure) {
out.println(getAesCiphertext("ok bye"));
} else out.println("ok bye");
loginLoop(); loginLoop();
} else if ("logout".equals(userInput)) { } else if ("logout".equals(userInput)) {
out.println("ok"); if (secure) {
out.println(getAesCiphertext("ok"));
} else out.println("ok");
currentUser = null; currentUser = null;
loginLoop(); loginLoop();
} else if ("list".equals(userInput)) { } else if ("list".equals(userInput)) {
@ -75,25 +82,37 @@ public class DMAPConnection implements Runnable {
try { try {
deleteMessage(userInput.split("\\s+")[1]); deleteMessage(userInput.split("\\s+")[1]);
} catch (MessageNotFoundException e) { } catch (MessageNotFoundException e) {
out.println(e.getMessage()); if (secure) {
out.println(getAesCiphertext(e.getMessage()));
} else out.println(e.getMessage());
} }
} else { } else {
out.println("Please supply a message id to delete!"); if (secure) {
out.println(getAesCiphertext("Please supply a message id to delete!"));
} else out.println("Please supply a message id to delete!");
} }
} else if (userInput.startsWith("show")) { } else if (userInput.startsWith("show")) {
if (userInput.split("\\s+").length == 2) { if (userInput.split("\\s+").length == 2) {
try { try {
showMessage(userInput.split("\\s+")[1]); showMessage(userInput.split("\\s+")[1]);
} catch (MessageNotFoundException e) { } catch (MessageNotFoundException e) {
out.println(e.getMessage()); if (secure) {
out.println(getAesCiphertext(e.getMessage()));
} else out.println(e.getMessage());
} }
} else { } else {
out.println("Please supply a message id to show!"); if (secure) {
out.println(getAesCiphertext("Please supply a message id to show!"));
} else out.println("Please supply a message id to show!");
} }
} else if (userInput.startsWith("startsecure")) { } else if (userInput.startsWith("startsecure")) {
startSecure(); if (secure) {
out.println(getAesCiphertext("You are already secure!"));
} else startSecure();
} else { } else {
out.println("error protocol error"); if (secure) {
out.println(getAesCiphertext("error protocol error"));
} else out.println("error protocol error");
shutdown(); shutdown();
} }
} }
@ -104,6 +123,10 @@ public class DMAPConnection implements Runnable {
logger.severe("Failed to get IO-Stream"); logger.severe("Failed to get IO-Stream");
e.printStackTrace(); e.printStackTrace();
shutdown(); shutdown();
} catch (BadPaddingException | IllegalBlockSizeException e) {
logger.severe("Error during encryption/decryption. Aborting...");
e.printStackTrace();
shutdown();
} }
} }
@ -113,13 +136,21 @@ public class DMAPConnection implements Runnable {
try { try {
while (!Thread.currentThread().isInterrupted()) { while (!Thread.currentThread().isInterrupted()) {
userInput = in.readLine(); userInput = in.readLine();
if (secure) {
userInput = getAesPlaintext(userInput);
}
if (userInput.startsWith("quit")) { if (userInput.startsWith("quit")) {
out.println("ok bye"); if (secure) {
out.println(getAesCiphertext("ok bye"));
} else out.println("ok bye");
shutdown(); shutdown();
} else if (userInput.startsWith("login")) { } else if (userInput.startsWith("login")) {
String[] args = userInput.split("\\s+"); String[] args = userInput.split("\\s+");
if (args.length != 3) if (args.length != 3) {
out.println("Please specify a username and password to login!"); if (secure) {
out.println(getAesCiphertext("Please specify a username and password to login!"));
} else out.println("Please specify a username and password to login!");
}
if (this.userStorage.containsKey(args[1])) { if (this.userStorage.containsKey(args[1])) {
// Check if username exists // Check if username exists
if (args[2].equals(this.userStorage.get(args[1]))) { if (args[2].equals(this.userStorage.get(args[1]))) {
@ -129,20 +160,28 @@ public class DMAPConnection implements Runnable {
// Set current user if login successful // Set current user if login successful
currentUser = email; currentUser = email;
logger.info("User successfully logged in: " + currentUser.toString()); logger.info("User successfully logged in: " + currentUser.toString());
out.println("ok"); if (secure) {
out.println(getAesCiphertext("ok"));
} else out.println("ok");
return; return;
} }
} }
} else { } else {
out.println("error wrong password"); if (secure) {
out.println(getAesCiphertext("error wrong password"));
} else out.println("error wrong password");
} }
} else { } else {
out.println("error unknown user"); if (secure) {
out.println(getAesCiphertext("error unknown user"));
} else out.println("error unknown user");
} }
} else if (userInput.startsWith("startsecure")) { } else if (userInput.startsWith("startsecure")) {
startSecure(); startSecure();
} else { } else {
out.println("error not logged in"); if (secure) {
out.println(getAesCiphertext("error not logged in"));
} else out.println("error not logged in");
} }
} }
} catch (InterruptedIOException ioe) { } catch (InterruptedIOException ioe) {
@ -155,6 +194,10 @@ public class DMAPConnection implements Runnable {
logger.severe("Failed to get IO-Stream"); logger.severe("Failed to get IO-Stream");
e.printStackTrace(); e.printStackTrace();
shutdown(); shutdown();
} catch (BadPaddingException | IllegalBlockSizeException e) {
logger.severe("Error during encryption/decryption");
e.printStackTrace();
shutdown();
} }
} }
@ -276,63 +319,7 @@ public class DMAPConnection implements Runnable {
this.aesDecryptCipher.init(Cipher.DECRYPT_MODE, decodedSecretKey, decodedIv); this.aesDecryptCipher.init(Cipher.DECRYPT_MODE, decodedSecretKey, decodedIv);
} }
/** private void showMessage(String id) throws MessageNotFoundException, BadPaddingException, IllegalBlockSizeException {
* Handles the login command issued by the MessageClient which is already encrypted.
*/
private void login() {
String userInput;
try {
while (!Thread.currentThread().isInterrupted()) {
userInput = in.readLine();
if (userInput.startsWith("quit")) {
out.println("ok bye");
shutdown();
} else if (userInput.startsWith("login")) {
String[] args = userInput.split("\\s+");
if (args.length != 3)
out.println("Please specify a username and password to login!");
if (this.userStorage.containsKey(args[1])) {
// Check if username exists
if (args[2].equals(this.userStorage.get(args[1]))) {
// Check if password matches
for (Email email : this.storage.keySet()) {
if (args[1].equals(email.getUsername())) {
// Set current user if login successful
currentUser = email;
logger.info("User successfully logged in: " + currentUser.toString());
out.println(getAesCiphertext("ok"));
return;
}
}
} else {
out.println("error wrong password");
}
} else {
out.println("error unknown user");
}
} else {
out.println("error not logged in");
}
}
} catch (InterruptedIOException ioe) {
logger.info("Received interrupt from parent. Shutting down...");
shutdown();
} catch (SocketException e) {
logger.finer("Received interrupt. Exiting " + this.toString());
shutdown();
} catch (IOException e) {
logger.severe("Failed to get IO-Stream");
e.printStackTrace();
shutdown();
} catch (BadPaddingException | IllegalBlockSizeException e) {
logger.severe("Error while encrypting/decrypting. Aborting...");
e.printStackTrace();
shutdown();
}
}
private void showMessage(String id) throws MessageNotFoundException {
int i; int i;
try { try {
i = Integer.parseInt(id); i = Integer.parseInt(id);
@ -342,7 +329,9 @@ public class DMAPConnection implements Runnable {
for (Message m : storage.get(currentUser)) { for (Message m : storage.get(currentUser)) {
if (m.getId() == i) { if (m.getId() == i) {
out.println(m); if (secure) {
out.println(getAesCiphertext(m.toString()));
} else out.println(m.toString());
return; return;
} }
} }
@ -350,20 +339,24 @@ public class DMAPConnection implements Runnable {
throw new MessageNotFoundException("error unknown message id"); throw new MessageNotFoundException("error unknown message id");
} }
private void listMessages() { private void listMessages() throws BadPaddingException, IllegalBlockSizeException {
logger.info("'list' command received"); logger.info("'list' command received");
if (storage.get(currentUser).isEmpty()) { if (storage.get(currentUser).isEmpty()) {
out.println("You do not have any messages at the moment!"); if (secure) {
out.println(getAesCiphertext("You do not have any messages at the moment!"));
} else out.println("You do not have any messages at the moment!");
return; return;
} }
for (Message m : storage.get(currentUser)) { for (Message m : storage.get(currentUser)) {
logger.info("Printing message from user: " + m.listMessage()); logger.info("Printing message from user: " + m.listMessage());
out.println(m.listMessage()); if (secure) {
out.println(getAesCiphertext(m.listMessage()));
} else out.println(m.listMessage());
} }
} }
public void deleteMessage(String id) throws MessageNotFoundException { public void deleteMessage(String id) throws MessageNotFoundException, BadPaddingException, IllegalBlockSizeException {
int i; int i;
try { try {
i = Integer.parseInt(id); i = Integer.parseInt(id);
@ -374,7 +367,9 @@ public class DMAPConnection implements Runnable {
for (Message m : storage.get(currentUser)) { for (Message m : storage.get(currentUser)) {
if (m.getId() == i) { if (m.getId() == i) {
storage.get(currentUser).remove(m); storage.get(currentUser).remove(m);
out.println("ok"); if (secure) {
out.println(getAesCiphertext("ok"));
} else out.println("ok");
return; return;
} }
} }