Correct client challenge encoding

First, encode the individual parameters, then add them to the command,
do the encryption and then encode the binary string again for sending.
This commit is contained in:
Tobias Eidelpes 2020-12-29 10:33:26 +01:00
parent 6666494009
commit d1bf65698d

View File

@ -174,27 +174,29 @@ public class MessageClient implements IMessageClient, Runnable {
IvParameterSpec iv = generateIv(); IvParameterSpec iv = generateIv();
// Save AES cipher for subsequent communication // Save AES cipher for subsequent communication
setAesCipher(secretKeySpec, iv); setAesCipher(secretKeySpec, iv);
// Concatenate challenge, secretKey and IV // Encode parameters to base64
byte[] concatenated = new byte[80]; String clearTextChallengeEncoded = Base64.getEncoder().encodeToString(clearTextChallenge);
System.arraycopy(clearTextChallenge, 0, concatenated, 0, 32); String secretKeyEncoded = Base64.getEncoder().encodeToString(secretKeySpec.getEncoded());
System.arraycopy(secretKeySpec.getEncoded(), 0, concatenated, 32, 32); String ivEncoded = Base64.getEncoder().encodeToString(iv.getIV());
System.arraycopy(iv.getIV(), 0, concatenated, 64, 16); // Concatenate command and parameters (challenge, secretKey and IV)
String base64Encoded = Base64.getEncoder().encodeToString(concatenated); String concatenated = "ok" + clearTextChallengeEncoded + secretKeyEncoded + ivEncoded;
// Encrypt "ok <base64Encoded>" // Encrypt "<base64Encoded>"
Cipher cipher = null; Cipher cipher;
byte[] cipherTextChallenge;
try { try {
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, serverPublicKey); cipher.init(Cipher.ENCRYPT_MODE, serverPublicKey);
cipher.update(("ok " + base64Encoded).getBytes(StandardCharsets.UTF_8)); cipher.update((concatenated).getBytes(StandardCharsets.UTF_8));
byte[] cipherTextChallenge = cipher.doFinal(); cipherTextChallenge = cipher.doFinal();
return (new String(cipherTextChallenge, StandardCharsets.UTF_8));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
logger.severe("This should not be happening!"); logger.severe("This should not be happening!");
e.printStackTrace(); e.printStackTrace();
shutdown(); shutdown();
}
return null; return null;
} }
// Return base64 encoded cipherMessage
return (Base64.getEncoder().encodeToString(cipherTextChallenge));
}
@Override @Override
public void inbox() { public void inbox() {