From f0020ea81143503b09363166617bcf7c6b269f55 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 19 Feb 2021 11:47:40 +1100 Subject: [PATCH] clean up decryption call back --- .../libsignal/DecryptionCallback.java | 10 --- .../libsignal/libsignal/SessionCipher.java | 89 ------------------- .../libsignal/groups/GroupCipher.java | 44 +-------- 3 files changed, 1 insertion(+), 142 deletions(-) delete mode 100644 libsignal/src/main/java/org/session/libsignal/libsignal/DecryptionCallback.java diff --git a/libsignal/src/main/java/org/session/libsignal/libsignal/DecryptionCallback.java b/libsignal/src/main/java/org/session/libsignal/libsignal/DecryptionCallback.java deleted file mode 100644 index 88b107b086..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/libsignal/DecryptionCallback.java +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Copyright (C) 2014-2016 Open Whisper Systems - * - * Licensed according to the LICENSE file in this repository. - */ -package org.session.libsignal.libsignal; - -public interface DecryptionCallback { - public void handlePlaintext(byte[] plaintext); -} \ No newline at end of file diff --git a/libsignal/src/main/java/org/session/libsignal/libsignal/SessionCipher.java b/libsignal/src/main/java/org/session/libsignal/libsignal/SessionCipher.java index dffa2b9355..7d75806bc8 100644 --- a/libsignal/src/main/java/org/session/libsignal/libsignal/SessionCipher.java +++ b/libsignal/src/main/java/org/session/libsignal/libsignal/SessionCipher.java @@ -127,60 +127,15 @@ public class SessionCipher { } } - /** - * Decrypt a message. - * - * @param ciphertext The {@link PreKeySignalMessage} to decrypt. - * - * @return The plaintext. - * @throws InvalidMessageException if the input is not valid ciphertext. - * @throws DuplicateMessageException if the input is a message that has already been received. - * @throws LegacyMessageException if the input is a message formatted by a protocol version that - * is no longer supported. - * @throws InvalidKeyIdException when there is no local {@link org.session.libsignal.libsignal.state.PreKeyRecord} - * that corresponds to the PreKey ID in the message. - * @throws InvalidKeyException when the message is formatted incorrectly. - * @throws UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted. - */ public byte[] decrypt(PreKeySignalMessage ciphertext) throws DuplicateMessageException, LegacyMessageException, InvalidMessageException, InvalidKeyIdException, InvalidKeyException, UntrustedIdentityException - { - return decrypt(ciphertext, new NullDecryptionCallback()); - } - - /** - * Decrypt a message. - * - * @param ciphertext The {@link PreKeySignalMessage} to decrypt. - * @param callback A callback that is triggered after decryption is complete, - * but before the updated session state has been committed to the session - * DB. This allows some implementations to store the committed plaintext - * to a DB first, in case they are concerned with a crash happening between - * the time the session state is updated but before they're able to store - * the plaintext to disk. - * - * @return The plaintext. - * @throws InvalidMessageException if the input is not valid ciphertext. - * @throws DuplicateMessageException if the input is a message that has already been received. - * @throws LegacyMessageException if the input is a message formatted by a protocol version that - * is no longer supported. - * @throws InvalidKeyIdException when there is no local {@link org.session.libsignal.libsignal.state.PreKeyRecord} - * that corresponds to the PreKey ID in the message. - * @throws InvalidKeyException when the message is formatted incorrectly. - * @throws UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted. - */ - public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callback) - throws DuplicateMessageException, LegacyMessageException, InvalidMessageException, - InvalidKeyIdException, InvalidKeyException, UntrustedIdentityException { synchronized (SESSION_LOCK) { SessionRecord sessionRecord = sessionStore.loadSession(remoteAddress); Optional unsignedPreKeyId = sessionBuilder.process(sessionRecord, ciphertext); byte[] plaintext = decrypt(sessionRecord, ciphertext.getWhisperMessage()); - callback.handlePlaintext(plaintext); - sessionStore.storeSession(remoteAddress, sessionRecord); if (unsignedPreKeyId.isPresent()) { @@ -191,44 +146,7 @@ public class SessionCipher { } } - /** - * Decrypt a message. - * - * @param ciphertext The {@link SignalMessage} to decrypt. - * - * @return The plaintext. - * @throws InvalidMessageException if the input is not valid ciphertext. - * @throws DuplicateMessageException if the input is a message that has already been received. - * @throws LegacyMessageException if the input is a message formatted by a protocol version that - * is no longer supported. - * @throws NoSessionException if there is no established session for this contact. - */ public byte[] decrypt(SignalMessage ciphertext) - throws InvalidMessageException, DuplicateMessageException, LegacyMessageException, - NoSessionException, UntrustedIdentityException - { - return decrypt(ciphertext, new NullDecryptionCallback()); - } - - /** - * Decrypt a message. - * - * @param ciphertext The {@link SignalMessage} to decrypt. - * @param callback A callback that is triggered after decryption is complete, - * but before the updated session state has been committed to the session - * DB. This allows some implementations to store the committed plaintext - * to a DB first, in case they are concerned with a crash happening between - * the time the session state is updated but before they're able to store - * the plaintext to disk. - * - * @return The plaintext. - * @throws InvalidMessageException if the input is not valid ciphertext. - * @throws DuplicateMessageException if the input is a message that has already been received. - * @throws LegacyMessageException if the input is a message formatted by a protocol version that - * is no longer supported. - * @throws NoSessionException if there is no established session for this contact. - */ - public byte[] decrypt(SignalMessage ciphertext, DecryptionCallback callback) throws InvalidMessageException, DuplicateMessageException, LegacyMessageException, NoSessionException, UntrustedIdentityException { @@ -247,8 +165,6 @@ public class SessionCipher { identityKeyStore.saveIdentity(remoteAddress, sessionRecord.getSessionState().getRemoteIdentityKey()); - callback.handlePlaintext(plaintext); - sessionStore.storeSession(remoteAddress, sessionRecord); return plaintext; @@ -431,9 +347,4 @@ public class SessionCipher { throw new AssertionError(e); } } - - private static class NullDecryptionCallback implements DecryptionCallback { - @Override - public void handlePlaintext(byte[] plaintext) {} - } } diff --git a/libsignal/src/main/java/org/session/libsignal/libsignal/groups/GroupCipher.java b/libsignal/src/main/java/org/session/libsignal/libsignal/groups/GroupCipher.java index 84f754081c..c1a4944cce 100644 --- a/libsignal/src/main/java/org/session/libsignal/libsignal/groups/GroupCipher.java +++ b/libsignal/src/main/java/org/session/libsignal/libsignal/groups/GroupCipher.java @@ -5,13 +5,11 @@ */ package org.session.libsignal.libsignal.groups; -import org.session.libsignal.libsignal.DecryptionCallback; import org.session.libsignal.libsignal.DuplicateMessageException; import org.session.libsignal.libsignal.InvalidKeyIdException; import org.session.libsignal.libsignal.InvalidMessageException; import org.session.libsignal.libsignal.LegacyMessageException; import org.session.libsignal.libsignal.NoSessionException; -import org.session.libsignal.libsignal.groups.SenderKeyName; import org.session.libsignal.libsignal.groups.ratchet.SenderChainKey; import org.session.libsignal.libsignal.groups.ratchet.SenderMessageKey; import org.session.libsignal.libsignal.groups.state.SenderKeyRecord; @@ -82,39 +80,7 @@ public class GroupCipher { } } - /** - * Decrypt a SenderKey group message. - * - * @param senderKeyMessageBytes The received ciphertext. - * @return Plaintext - * @throws LegacyMessageException - * @throws InvalidMessageException - * @throws DuplicateMessageException - */ - public byte[] decrypt(byte[] senderKeyMessageBytes) - throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException - { - return decrypt(senderKeyMessageBytes, new NullDecryptionCallback()); - } - - /** - * Decrypt a SenderKey group message. - * - * @param senderKeyMessageBytes The received ciphertext. - * @param callback A callback that is triggered after decryption is complete, - * but before the updated session state has been committed to the session - * DB. This allows some implementations to store the committed plaintext - * to a DB first, in case they are concerned with a crash happening between - * the time the session state is updated but before they're able to store - * the plaintext to disk. - * @return Plaintext - * @throws LegacyMessageException - * @throws InvalidMessageException - * @throws DuplicateMessageException - */ - public byte[] decrypt(byte[] senderKeyMessageBytes, DecryptionCallback callback) - throws LegacyMessageException, InvalidMessageException, DuplicateMessageException, - NoSessionException + public byte[] decrypt(byte[] senderKeyMessageBytes) throws LegacyMessageException, InvalidMessageException, DuplicateMessageException, NoSessionException { synchronized (LOCK) { try { @@ -133,8 +99,6 @@ public class GroupCipher { byte[] plaintext = getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText()); - callback.handlePlaintext(plaintext); - senderKeyStore.storeSenderKey(senderKeyId, record); return plaintext; @@ -220,10 +184,4 @@ public class GroupCipher { throw new AssertionError(e); } } - - private static class NullDecryptionCallback implements DecryptionCallback { - @Override - public void handlePlaintext(byte[] plaintext) {} - } - }