Search in sources :

Example 1 with DuplicateMessageException

use of org.whispersystems.libaxolotl.DuplicateMessageException in project Conversations by siacs.

the class XmppAxolotlSession method processReceiving.

@Nullable
public byte[] processReceiving(AxolotlKey encryptedKey) throws CryptoFailedException {
    byte[] plaintext;
    FingerprintStatus status = getTrust();
    if (!status.isCompromised()) {
        try {
            CiphertextMessage ciphertextMessage;
            try {
                ciphertextMessage = new PreKeyWhisperMessage(encryptedKey.key);
                Optional<Integer> optionalPreKeyId = ((PreKeyWhisperMessage) ciphertextMessage).getPreKeyId();
                IdentityKey identityKey = ((PreKeyWhisperMessage) ciphertextMessage).getIdentityKey();
                if (!optionalPreKeyId.isPresent()) {
                    throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId");
                }
                preKeyId = optionalPreKeyId.get();
                if (this.identityKey != null && !this.identityKey.equals(identityKey)) {
                    throw new CryptoFailedException("Received PreKeyWhisperMessage but preexisting identity key changed.");
                }
                this.identityKey = identityKey;
            } catch (InvalidVersionException | InvalidMessageException e) {
                ciphertextMessage = new WhisperMessage(encryptedKey.key);
            }
            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());
        }
    } else {
        throw new CryptoFailedException("not encrypting omemo message from fingerprint " + getFingerprint() + " because it was marked as compromised");
    }
    return plaintext;
}
Also used : InvalidMessageException(org.whispersystems.libaxolotl.InvalidMessageException) IdentityKey(org.whispersystems.libaxolotl.IdentityKey) UntrustedIdentityException(org.whispersystems.libaxolotl.UntrustedIdentityException) PreKeyWhisperMessage(org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage) CiphertextMessage(org.whispersystems.libaxolotl.protocol.CiphertextMessage) InvalidVersionException(org.whispersystems.libaxolotl.InvalidVersionException) InvalidKeyException(org.whispersystems.libaxolotl.InvalidKeyException) NoSessionException(org.whispersystems.libaxolotl.NoSessionException) DuplicateMessageException(org.whispersystems.libaxolotl.DuplicateMessageException) InvalidKeyIdException(org.whispersystems.libaxolotl.InvalidKeyIdException) WhisperMessage(org.whispersystems.libaxolotl.protocol.WhisperMessage) PreKeyWhisperMessage(org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage) LegacyMessageException(org.whispersystems.libaxolotl.LegacyMessageException) Nullable(android.support.annotation.Nullable)

Aggregations

Nullable (android.support.annotation.Nullable)1 DuplicateMessageException (org.whispersystems.libaxolotl.DuplicateMessageException)1 IdentityKey (org.whispersystems.libaxolotl.IdentityKey)1 InvalidKeyException (org.whispersystems.libaxolotl.InvalidKeyException)1 InvalidKeyIdException (org.whispersystems.libaxolotl.InvalidKeyIdException)1 InvalidMessageException (org.whispersystems.libaxolotl.InvalidMessageException)1 InvalidVersionException (org.whispersystems.libaxolotl.InvalidVersionException)1 LegacyMessageException (org.whispersystems.libaxolotl.LegacyMessageException)1 NoSessionException (org.whispersystems.libaxolotl.NoSessionException)1 UntrustedIdentityException (org.whispersystems.libaxolotl.UntrustedIdentityException)1 CiphertextMessage (org.whispersystems.libaxolotl.protocol.CiphertextMessage)1 PreKeyWhisperMessage (org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage)1 WhisperMessage (org.whispersystems.libaxolotl.protocol.WhisperMessage)1