From 0156ec58eeb39c796953e2942930aae0b8049940 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 16 Jan 2017 20:53:18 +0100 Subject: refactored whispermessage processing --- .../messenger/crypto/axolotl/AxolotlService.java | 2 +- .../crypto/axolotl/XmppAxolotlSession.java | 52 +++++++++++----------- 2 files changed, 27 insertions(+), 27 deletions(-) (limited to 'src/main/java/de/pixart/messenger/crypto/axolotl') diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java index 76302141e..a5aa06284 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java @@ -1119,7 +1119,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { session.resetPreKeyId(); } } catch (CryptoFailedException e) { - Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage()); + Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message from "+message.getFrom()+": " + e.getMessage()); } if (session.isFresh() && plaintextMessage != null) { diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java b/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java index 84237d645..5b45e28b2 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java @@ -2,7 +2,6 @@ package de.pixart.messenger.crypto.axolotl; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.util.Log; import org.whispersystems.libaxolotl.AxolotlAddress; import org.whispersystems.libaxolotl.DuplicateMessageException; @@ -18,8 +17,8 @@ import org.whispersystems.libaxolotl.UntrustedIdentityException; import org.whispersystems.libaxolotl.protocol.CiphertextMessage; import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage; import org.whispersystems.libaxolotl.protocol.WhisperMessage; +import org.whispersystems.libaxolotl.util.guava.Optional; -import de.pixart.messenger.Config; import de.pixart.messenger.entities.Account; public class XmppAxolotlSession implements Comparable { @@ -83,37 +82,36 @@ public class XmppAxolotlSession implements Comparable { @Nullable public byte[] processReceiving(AxolotlKey encryptedKey) throws CryptoFailedException { - byte[] plaintext = null; + byte[] plaintext; FingerprintStatus status = getTrust(); if (!status.isCompromised()) { try { + CiphertextMessage ciphertextMessage; try { - PreKeyWhisperMessage message = new PreKeyWhisperMessage(encryptedKey.key); - if (!message.getPreKeyId().isPresent()) { + ciphertextMessage = new PreKeyWhisperMessage(encryptedKey.key); + Optional optionalPreKeyId = ((PreKeyWhisperMessage) ciphertextMessage).getPreKeyId(); + IdentityKey identityKey = ((PreKeyWhisperMessage) ciphertextMessage).getIdentityKey(); + if (!optionalPreKeyId.isPresent()) { throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId"); } - Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage received, new session ID:" + message.getSignedPreKeyId() + "/" + message.getPreKeyId()); - IdentityKey msgIdentityKey = message.getIdentityKey(); - if (this.identityKey != null && !this.identityKey.equals(msgIdentityKey)) { - Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Had session with fingerprint " + this.getFingerprint() + ", received message with fingerprint " + msgIdentityKey.getFingerprint()); - } else { - this.identityKey = msgIdentityKey; - plaintext = cipher.decrypt(message); - preKeyId = message.getPreKeyId().get(); + preKeyId = optionalPreKeyId.get(); + if (this.identityKey != null && !this.identityKey.equals(identityKey)) { + throw new CryptoFailedException("Received PreKeyWhisperMessage but preexisting identity key changed."); } - } catch (InvalidMessageException | InvalidVersionException e) { - Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "WhisperMessage received"); - WhisperMessage message = new WhisperMessage(encryptedKey.key); - plaintext = cipher.decrypt(message); - } catch (InvalidKeyException | InvalidKeyIdException | UntrustedIdentityException e) { - throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()"); + this.identityKey = identityKey; + } catch (InvalidVersionException | InvalidMessageException e) { + ciphertextMessage = new WhisperMessage(encryptedKey.key); } - } catch (LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException e) { - throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()"); - } - - if (plaintext == null) { - throw new CryptoFailedException("plaintext unexpectedly null"); + if (ciphertextMessage instanceof PreKeyWhisperMessage) { + plaintext = cipher.decrypt((PreKeyWhisperMessage) ciphertextMessage); + } else { + plaintext = cipher.decrypt((WhisperMessage) ciphertextMessage); + } + } catch (InvalidKeyException | LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException | InvalidKeyIdException | UntrustedIdentityException e) { + if (!(e instanceof DuplicateMessageException)) { + e.printStackTrace(); + } + throw new CryptoFailedException("Error decrypting WhisperMessage " + e.getClass().getSimpleName() + ": " + e.getMessage()); } if (!status.isActive()) { setTrust(status.toActive()); @@ -145,6 +143,8 @@ public class XmppAxolotlSession implements Comparable { } public static class AxolotlKey { + + public final byte[] key; public final boolean prekey; @@ -153,4 +153,4 @@ public class XmppAxolotlSession implements Comparable { this.prekey = prekey; } } -} +} \ No newline at end of file -- cgit v1.2.3