From 5bc4094ff4a9fe25d458ade55ae1019cb87eeab8 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 24 Jun 2017 09:31:13 +0200 Subject: upgrade to signal-protocol-java --- .../crypto/axolotl/XmppAxolotlSession.java | 63 ++++++++++++---------- 1 file changed, 34 insertions(+), 29 deletions(-) (limited to 'src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java') 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 5b45e28b2..4327c6116 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java @@ -3,39 +3,40 @@ package de.pixart.messenger.crypto.axolotl; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import org.whispersystems.libaxolotl.AxolotlAddress; -import org.whispersystems.libaxolotl.DuplicateMessageException; -import org.whispersystems.libaxolotl.IdentityKey; -import org.whispersystems.libaxolotl.InvalidKeyException; -import org.whispersystems.libaxolotl.InvalidKeyIdException; -import org.whispersystems.libaxolotl.InvalidMessageException; -import org.whispersystems.libaxolotl.InvalidVersionException; -import org.whispersystems.libaxolotl.LegacyMessageException; -import org.whispersystems.libaxolotl.NoSessionException; -import org.whispersystems.libaxolotl.SessionCipher; -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 org.whispersystems.libsignal.SignalProtocolAddress; +import org.whispersystems.libsignal.DuplicateMessageException; +import org.whispersystems.libsignal.IdentityKey; +import org.whispersystems.libsignal.InvalidKeyException; +import org.whispersystems.libsignal.InvalidKeyIdException; +import org.whispersystems.libsignal.InvalidMessageException; +import org.whispersystems.libsignal.InvalidVersionException; +import org.whispersystems.libsignal.LegacyMessageException; +import org.whispersystems.libsignal.NoSessionException; +import org.whispersystems.libsignal.SessionCipher; +import org.whispersystems.libsignal.UntrustedIdentityException; +import org.whispersystems.libsignal.protocol.CiphertextMessage; +import org.whispersystems.libsignal.protocol.PreKeySignalMessage; +import org.whispersystems.libsignal.protocol.SignalMessage; +import org.whispersystems.libsignal.util.guava.Optional; import de.pixart.messenger.entities.Account; +import de.pixart.messenger.utils.CryptoHelper; public class XmppAxolotlSession implements Comparable { private final SessionCipher cipher; private final SQLiteAxolotlStore sqLiteAxolotlStore; - private final AxolotlAddress remoteAddress; + private final SignalProtocolAddress remoteAddress; private final Account account; private IdentityKey identityKey; private Integer preKeyId = null; private boolean fresh = true; - public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress, IdentityKey identityKey) { + public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, SignalProtocolAddress remoteAddress, IdentityKey identityKey) { this(account, store, remoteAddress); this.identityKey = identityKey; } - public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress) { + public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, SignalProtocolAddress remoteAddress) { this.cipher = new SessionCipher(store, remoteAddress); this.remoteAddress = remoteAddress; this.sqLiteAxolotlStore = store; @@ -52,14 +53,14 @@ public class XmppAxolotlSession implements Comparable { } public String getFingerprint() { - return identityKey == null ? null : identityKey.getFingerprint().replaceAll("\\s", ""); + return identityKey == null ? null : CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()); } public IdentityKey getIdentityKey() { return identityKey; } - public AxolotlAddress getRemoteAddress() { + public SignalProtocolAddress getRemoteAddress() { return remoteAddress; } @@ -88,9 +89,9 @@ public class XmppAxolotlSession implements Comparable { try { CiphertextMessage ciphertextMessage; try { - ciphertextMessage = new PreKeyWhisperMessage(encryptedKey.key); - Optional optionalPreKeyId = ((PreKeyWhisperMessage) ciphertextMessage).getPreKeyId(); - IdentityKey identityKey = ((PreKeyWhisperMessage) ciphertextMessage).getIdentityKey(); + ciphertextMessage = new PreKeySignalMessage(encryptedKey.key); + Optional optionalPreKeyId = ((PreKeySignalMessage) ciphertextMessage).getPreKeyId(); + IdentityKey identityKey = ((PreKeySignalMessage) ciphertextMessage).getIdentityKey(); if (!optionalPreKeyId.isPresent()) { throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId"); } @@ -100,12 +101,12 @@ public class XmppAxolotlSession implements Comparable { } this.identityKey = identityKey; } catch (InvalidVersionException | InvalidMessageException e) { - ciphertextMessage = new WhisperMessage(encryptedKey.key); + ciphertextMessage = new SignalMessage(encryptedKey.key); } - if (ciphertextMessage instanceof PreKeyWhisperMessage) { - plaintext = cipher.decrypt((PreKeyWhisperMessage) ciphertextMessage); + if (ciphertextMessage instanceof PreKeySignalMessage) { + plaintext = cipher.decrypt((PreKeySignalMessage) ciphertextMessage); } else { - plaintext = cipher.decrypt((WhisperMessage) ciphertextMessage); + plaintext = cipher.decrypt((SignalMessage) ciphertextMessage); } } catch (InvalidKeyException | LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException | InvalidKeyIdException | UntrustedIdentityException e) { if (!(e instanceof DuplicateMessageException)) { @@ -126,8 +127,12 @@ public class XmppAxolotlSession implements Comparable { public AxolotlKey processSending(@NonNull byte[] outgoingMessage) { FingerprintStatus status = getTrust(); if (status.isTrustedAndActive()) { - CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage); - return new AxolotlKey(ciphertextMessage.serialize(),ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE); + try { + CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage); + return new AxolotlKey(ciphertextMessage.serialize(), ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE); + } catch (UntrustedIdentityException e) { + return null; + } } else { return null; } -- cgit v1.2.3