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:
parent
93a2a87f07
commit
cab09d1949
@ -61,11 +61,18 @@ public class DMAPConnection implements Runnable {
|
||||
|
||||
String userInput;
|
||||
while (!Thread.currentThread().isInterrupted() && (userInput = in.readLine()) != null) {
|
||||
if (secure) {
|
||||
userInput = getAesPlaintext(userInput);
|
||||
}
|
||||
if ("quit".equals(userInput)) {
|
||||
out.println("ok bye");
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("ok bye"));
|
||||
} else out.println("ok bye");
|
||||
loginLoop();
|
||||
} else if ("logout".equals(userInput)) {
|
||||
out.println("ok");
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("ok"));
|
||||
} else out.println("ok");
|
||||
currentUser = null;
|
||||
loginLoop();
|
||||
} else if ("list".equals(userInput)) {
|
||||
@ -75,25 +82,37 @@ public class DMAPConnection implements Runnable {
|
||||
try {
|
||||
deleteMessage(userInput.split("\\s+")[1]);
|
||||
} catch (MessageNotFoundException e) {
|
||||
out.println(e.getMessage());
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext(e.getMessage()));
|
||||
} else out.println(e.getMessage());
|
||||
}
|
||||
} 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")) {
|
||||
if (userInput.split("\\s+").length == 2) {
|
||||
try {
|
||||
showMessage(userInput.split("\\s+")[1]);
|
||||
} catch (MessageNotFoundException e) {
|
||||
out.println(e.getMessage());
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext(e.getMessage()));
|
||||
} else out.println(e.getMessage());
|
||||
}
|
||||
} 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")) {
|
||||
startSecure();
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("You are already secure!"));
|
||||
} else startSecure();
|
||||
} else {
|
||||
out.println("error protocol error");
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("error protocol error"));
|
||||
} else out.println("error protocol error");
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
@ -104,6 +123,10 @@ public class DMAPConnection implements Runnable {
|
||||
logger.severe("Failed to get IO-Stream");
|
||||
e.printStackTrace();
|
||||
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 {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
userInput = in.readLine();
|
||||
if (secure) {
|
||||
userInput = getAesPlaintext(userInput);
|
||||
}
|
||||
if (userInput.startsWith("quit")) {
|
||||
out.println("ok bye");
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("ok bye"));
|
||||
} else 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 (args.length != 3) {
|
||||
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])) {
|
||||
// Check if username exists
|
||||
if (args[2].equals(this.userStorage.get(args[1]))) {
|
||||
@ -129,20 +160,28 @@ public class DMAPConnection implements Runnable {
|
||||
// Set current user if login successful
|
||||
currentUser = email;
|
||||
logger.info("User successfully logged in: " + currentUser.toString());
|
||||
out.println("ok");
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("ok"));
|
||||
} else out.println("ok");
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
out.println("error wrong password");
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("error wrong password"));
|
||||
} else out.println("error wrong password");
|
||||
}
|
||||
} 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")) {
|
||||
startSecure();
|
||||
} 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) {
|
||||
@ -155,6 +194,10 @@ public class DMAPConnection implements Runnable {
|
||||
logger.severe("Failed to get IO-Stream");
|
||||
e.printStackTrace();
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
private void showMessage(String id) throws MessageNotFoundException, BadPaddingException, IllegalBlockSizeException {
|
||||
int i;
|
||||
try {
|
||||
i = Integer.parseInt(id);
|
||||
@ -342,7 +329,9 @@ public class DMAPConnection implements Runnable {
|
||||
|
||||
for (Message m : storage.get(currentUser)) {
|
||||
if (m.getId() == i) {
|
||||
out.println(m);
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext(m.toString()));
|
||||
} else out.println(m.toString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -350,20 +339,24 @@ public class DMAPConnection implements Runnable {
|
||||
throw new MessageNotFoundException("error unknown message id");
|
||||
}
|
||||
|
||||
private void listMessages() {
|
||||
private void listMessages() throws BadPaddingException, IllegalBlockSizeException {
|
||||
logger.info("'list' command received");
|
||||
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;
|
||||
}
|
||||
|
||||
for (Message m : storage.get(currentUser)) {
|
||||
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;
|
||||
try {
|
||||
i = Integer.parseInt(id);
|
||||
@ -374,7 +367,9 @@ public class DMAPConnection implements Runnable {
|
||||
for (Message m : storage.get(currentUser)) {
|
||||
if (m.getId() == i) {
|
||||
storage.get(currentUser).remove(m);
|
||||
out.println("ok");
|
||||
if (secure) {
|
||||
out.println(getAesCiphertext("ok"));
|
||||
} else out.println("ok");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user