refactored whispermessage processing

This commit is contained in:
Daniel Gultsch 2017-01-15 18:54:47 +01:00
parent b085426d22
commit d028f4b398
2 changed files with 26 additions and 23 deletions

View File

@ -1122,7 +1122,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
session.resetPreKeyId(); session.resetPreKeyId();
} }
} catch (CryptoFailedException e) { } 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) { if (session.isFresh() && plaintextMessage != null) {

View File

@ -4,6 +4,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import org.bouncycastle.math.ec.PreCompInfo;
import org.whispersystems.libaxolotl.AxolotlAddress; import org.whispersystems.libaxolotl.AxolotlAddress;
import org.whispersystems.libaxolotl.DuplicateMessageException; import org.whispersystems.libaxolotl.DuplicateMessageException;
import org.whispersystems.libaxolotl.IdentityKey; import org.whispersystems.libaxolotl.IdentityKey;
@ -18,9 +19,11 @@ import org.whispersystems.libaxolotl.UntrustedIdentityException;
import org.whispersystems.libaxolotl.protocol.CiphertextMessage; import org.whispersystems.libaxolotl.protocol.CiphertextMessage;
import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage; import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage;
import org.whispersystems.libaxolotl.protocol.WhisperMessage; import org.whispersystems.libaxolotl.protocol.WhisperMessage;
import org.whispersystems.libaxolotl.util.guava.Optional;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.CryptoHelper;
public class XmppAxolotlSession implements Comparable<XmppAxolotlSession> { public class XmppAxolotlSession implements Comparable<XmppAxolotlSession> {
private final SessionCipher cipher; private final SessionCipher cipher;
@ -83,36 +86,36 @@ public class XmppAxolotlSession implements Comparable<XmppAxolotlSession> {
@Nullable @Nullable
public byte[] processReceiving(AxolotlKey encryptedKey) throws CryptoFailedException { public byte[] processReceiving(AxolotlKey encryptedKey) throws CryptoFailedException {
byte[] plaintext = null; byte[] plaintext;
FingerprintStatus status = getTrust(); FingerprintStatus status = getTrust();
if (!status.isCompromised()) { if (!status.isCompromised()) {
try { try {
CiphertextMessage ciphertextMessage;
try { try {
PreKeyWhisperMessage message = new PreKeyWhisperMessage(encryptedKey.key); ciphertextMessage = new PreKeyWhisperMessage(encryptedKey.key);
if (!message.getPreKeyId().isPresent()) { Optional<Integer> optionalPreKeyId = ((PreKeyWhisperMessage) ciphertextMessage).getPreKeyId();
IdentityKey identityKey = ((PreKeyWhisperMessage) ciphertextMessage).getIdentityKey();
if (!optionalPreKeyId.isPresent()) {
throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId"); 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()); preKeyId = optionalPreKeyId.get();
IdentityKey msgIdentityKey = message.getIdentityKey(); if (this.identityKey != null && !this.identityKey.equals(identityKey)) {
if (this.identityKey != null && !this.identityKey.equals(msgIdentityKey)) { throw new CryptoFailedException("Received PreKeyWhisperMessage but preexisting identity key changed.");
Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Had session with fingerprint " + this.getFingerprint() + ", received message with fingerprint " + msgIdentityKey.getFingerprint()); }
this.identityKey = identityKey;
} catch (InvalidVersionException | InvalidMessageException e) {
ciphertextMessage = new WhisperMessage(encryptedKey.key);
}
if (ciphertextMessage instanceof PreKeyWhisperMessage) {
plaintext = cipher.decrypt((PreKeyWhisperMessage) ciphertextMessage);
} else { } else {
this.identityKey = msgIdentityKey; plaintext = cipher.decrypt((WhisperMessage) ciphertextMessage);
plaintext = cipher.decrypt(message);
preKeyId = message.getPreKeyId().get();
} }
} catch (InvalidMessageException | InvalidVersionException e) { } catch (InvalidKeyException | LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException | InvalidKeyIdException | UntrustedIdentityException e) {
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "WhisperMessage received"); if (!(e instanceof DuplicateMessageException)) {
WhisperMessage message = new WhisperMessage(encryptedKey.key); e.printStackTrace();
plaintext = cipher.decrypt(message);
} catch (InvalidKeyException | InvalidKeyIdException | UntrustedIdentityException e) {
throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()");
} }
} catch (LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException e) { throw new CryptoFailedException("Error decrypting WhisperMessage " + e.getClass().getSimpleName() + ": " + e.getMessage());
throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()");
}
if (plaintext==null) {
throw new CryptoFailedException("plaintext unexpectedly null");
} }
if (!status.isActive()) { if (!status.isActive()) {
setTrust(status.toActive()); setTrust(status.toActive());