diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java index 99533afb..0061a77a 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java @@ -1136,7 +1136,12 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage; XmppAxolotlSession session = getReceivingSession(message); - keyTransportMessage = message.getParameters(session, getOwnDeviceId()); + try { + keyTransportMessage = message.getParameters(session, getOwnDeviceId()); + } catch (CryptoFailedException e) { + Log.d(Config.LOGTAG,"could not decrypt keyTransport message "+e.getMessage()); + keyTransportMessage = null; + } if (session.isFresh() && keyTransportMessage != null) { putFreshSession(session); diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/CryptoFailedException.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/CryptoFailedException.java index 5796ef30..e549598c 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/CryptoFailedException.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/CryptoFailedException.java @@ -1,6 +1,11 @@ package eu.siacs.conversations.crypto.axolotl; public class CryptoFailedException extends Exception { + + public CryptoFailedException(String msg) { + super(msg); + } + public CryptoFailedException(Exception e){ super(e); } diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java index e5e7b203..cac298e0 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java @@ -250,16 +250,16 @@ public class XmppAxolotlMessage { return encryptionElement; } - private byte[] unpackKey(XmppAxolotlSession session, Integer sourceDeviceId) { + private byte[] unpackKey(XmppAxolotlSession session, Integer sourceDeviceId) throws CryptoFailedException { XmppAxolotlSession.AxolotlKey encryptedKey = keys.get(sourceDeviceId); - return (encryptedKey != null) ? session.processReceiving(encryptedKey) : null; + if (encryptedKey == null) { + throw new CryptoFailedException("Message was not encrypted for this device"); + } + return session.processReceiving(encryptedKey); } - public XmppAxolotlKeyTransportMessage getParameters(XmppAxolotlSession session, Integer sourceDeviceId) { - byte[] key = unpackKey(session, sourceDeviceId); - return (key != null) - ? new XmppAxolotlKeyTransportMessage(session.getFingerprint(), key, getIV()) - : null; + public XmppAxolotlKeyTransportMessage getParameters(XmppAxolotlSession session, Integer sourceDeviceId) throws CryptoFailedException { + return new XmppAxolotlKeyTransportMessage(session.getFingerprint(), unpackKey(session, sourceDeviceId), getIV()); } public XmppAxolotlPlaintextMessage decrypt(XmppAxolotlSession session, Integer sourceDeviceId) throws CryptoFailedException { diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java index 938c19a4..4e03627f 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java @@ -82,7 +82,7 @@ public class XmppAxolotlSession implements Comparable { } @Nullable - public byte[] processReceiving(AxolotlKey encryptedKey) { + public byte[] processReceiving(AxolotlKey encryptedKey) throws CryptoFailedException { byte[] plaintext = null; FingerprintStatus status = getTrust(); if (!status.isCompromised()) { @@ -90,8 +90,7 @@ public class XmppAxolotlSession implements Comparable { try { PreKeyWhisperMessage message = new PreKeyWhisperMessage(encryptedKey.key); if (!message.getPreKeyId().isPresent()) { - Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage did not contain a PreKeyId"); - return null; + 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(); @@ -107,19 +106,19 @@ public class XmppAxolotlSession implements Comparable { WhisperMessage message = new WhisperMessage(encryptedKey.key); plaintext = cipher.decrypt(message); } catch (InvalidKeyException | InvalidKeyIdException | UntrustedIdentityException e) { - Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error decrypting axolotl header, " + e.getClass().getName() + ": " + e.getMessage()); + throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()"); } } catch (LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException e) { - Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error decrypting axolotl header, " + e.getClass().getName() + ": " + e.getMessage()); + throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()"); } - - if (plaintext != null) { - if (!status.isActive()) { - setTrust(status.toActive()); - } + if (plaintext==null) { + throw new CryptoFailedException("plaintext unexpectedly null"); + } + if (!status.isActive()) { + setTrust(status.toActive()); } } else { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+" not encrypting omemo message from fingerprint "+getFingerprint()+" because it was marked as compromised"); + throw new CryptoFailedException("not encrypting omemo message from fingerprint "+getFingerprint()+" because it was marked as compromised"); } return plaintext; }