mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-20 06:18:26 +00:00
clean up decryption call back
This commit is contained in:
parent
bbd067873a
commit
f0020ea811
@ -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);
|
|
||||||
}
|
|
@ -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)
|
public byte[] decrypt(PreKeySignalMessage ciphertext)
|
||||||
throws DuplicateMessageException, LegacyMessageException, InvalidMessageException,
|
throws DuplicateMessageException, LegacyMessageException, InvalidMessageException,
|
||||||
InvalidKeyIdException, InvalidKeyException, UntrustedIdentityException
|
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) {
|
synchronized (SESSION_LOCK) {
|
||||||
SessionRecord sessionRecord = sessionStore.loadSession(remoteAddress);
|
SessionRecord sessionRecord = sessionStore.loadSession(remoteAddress);
|
||||||
Optional<Integer> unsignedPreKeyId = sessionBuilder.process(sessionRecord, ciphertext);
|
Optional<Integer> unsignedPreKeyId = sessionBuilder.process(sessionRecord, ciphertext);
|
||||||
byte[] plaintext = decrypt(sessionRecord, ciphertext.getWhisperMessage());
|
byte[] plaintext = decrypt(sessionRecord, ciphertext.getWhisperMessage());
|
||||||
|
|
||||||
callback.handlePlaintext(plaintext);
|
|
||||||
|
|
||||||
sessionStore.storeSession(remoteAddress, sessionRecord);
|
sessionStore.storeSession(remoteAddress, sessionRecord);
|
||||||
|
|
||||||
if (unsignedPreKeyId.isPresent()) {
|
if (unsignedPreKeyId.isPresent()) {
|
||||||
@ -191,46 +146,9 @@ 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)
|
public byte[] decrypt(SignalMessage ciphertext)
|
||||||
throws InvalidMessageException, DuplicateMessageException, LegacyMessageException,
|
throws InvalidMessageException, DuplicateMessageException, LegacyMessageException,
|
||||||
NoSessionException, UntrustedIdentityException
|
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
|
|
||||||
{
|
{
|
||||||
synchronized (SESSION_LOCK) {
|
synchronized (SESSION_LOCK) {
|
||||||
|
|
||||||
@ -247,8 +165,6 @@ public class SessionCipher {
|
|||||||
|
|
||||||
identityKeyStore.saveIdentity(remoteAddress, sessionRecord.getSessionState().getRemoteIdentityKey());
|
identityKeyStore.saveIdentity(remoteAddress, sessionRecord.getSessionState().getRemoteIdentityKey());
|
||||||
|
|
||||||
callback.handlePlaintext(plaintext);
|
|
||||||
|
|
||||||
sessionStore.storeSession(remoteAddress, sessionRecord);
|
sessionStore.storeSession(remoteAddress, sessionRecord);
|
||||||
|
|
||||||
return plaintext;
|
return plaintext;
|
||||||
@ -431,9 +347,4 @@ public class SessionCipher {
|
|||||||
throw new AssertionError(e);
|
throw new AssertionError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class NullDecryptionCallback implements DecryptionCallback {
|
|
||||||
@Override
|
|
||||||
public void handlePlaintext(byte[] plaintext) {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,13 +5,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.session.libsignal.libsignal.groups;
|
package org.session.libsignal.libsignal.groups;
|
||||||
|
|
||||||
import org.session.libsignal.libsignal.DecryptionCallback;
|
|
||||||
import org.session.libsignal.libsignal.DuplicateMessageException;
|
import org.session.libsignal.libsignal.DuplicateMessageException;
|
||||||
import org.session.libsignal.libsignal.InvalidKeyIdException;
|
import org.session.libsignal.libsignal.InvalidKeyIdException;
|
||||||
import org.session.libsignal.libsignal.InvalidMessageException;
|
import org.session.libsignal.libsignal.InvalidMessageException;
|
||||||
import org.session.libsignal.libsignal.LegacyMessageException;
|
import org.session.libsignal.libsignal.LegacyMessageException;
|
||||||
import org.session.libsignal.libsignal.NoSessionException;
|
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.SenderChainKey;
|
||||||
import org.session.libsignal.libsignal.groups.ratchet.SenderMessageKey;
|
import org.session.libsignal.libsignal.groups.ratchet.SenderMessageKey;
|
||||||
import org.session.libsignal.libsignal.groups.state.SenderKeyRecord;
|
import org.session.libsignal.libsignal.groups.state.SenderKeyRecord;
|
||||||
@ -82,39 +80,7 @@ public class GroupCipher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public byte[] decrypt(byte[] senderKeyMessageBytes) throws LegacyMessageException, InvalidMessageException, DuplicateMessageException, NoSessionException
|
||||||
* 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
|
|
||||||
{
|
{
|
||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
try {
|
try {
|
||||||
@ -133,8 +99,6 @@ public class GroupCipher {
|
|||||||
|
|
||||||
byte[] plaintext = getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
byte[] plaintext = getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
||||||
|
|
||||||
callback.handlePlaintext(plaintext);
|
|
||||||
|
|
||||||
senderKeyStore.storeSenderKey(senderKeyId, record);
|
senderKeyStore.storeSenderKey(senderKeyId, record);
|
||||||
|
|
||||||
return plaintext;
|
return plaintext;
|
||||||
@ -220,10 +184,4 @@ public class GroupCipher {
|
|||||||
throw new AssertionError(e);
|
throw new AssertionError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class NullDecryptionCallback implements DecryptionCallback {
|
|
||||||
@Override
|
|
||||||
public void handlePlaintext(byte[] plaintext) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user