mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-26 16:52:13 +00:00
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been a fairly clean insertion layer that handles encrypting message bodies, but the process of decrypting message bodies has always been less than ideal. Here we introduce a "Reader" interface that will decrypt message bodies when appropriate and return objects that encapsulate record state. No more MessageDisplayHelper. The MmsSmsDatabase interface is also more sane. 2) We finally rid ourselves of the technical debt associated with TextSecure's initial usage of the default SMS DB. In that world, we weren't able to use anything other than the default "Inbox, Outbox, Sent" types to describe a message, and had to overload the message content itself with a set of local "prefixes" to describe what it was (encrypted, asymetric encrypted, remote encrypted, a key exchange, procssed key exchange), and so on. This includes a major schema update that transforms the "type" field into a bitmask that describes everything that used to be encoded in a prefix, and prefixes have been completely eliminated from the system. No more Prefix.java 3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and hopefully more clear as to what's going on. The next step is to remove what we can from SmsTransportDetails and genericize that interface for a GCM equivalent.
This commit is contained in:
@@ -17,62 +17,175 @@
|
||||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.AsymmetricMasterCipher;
|
||||
import org.thoughtcrime.securesms.crypto.AsymmetricMasterSecret;
|
||||
import org.thoughtcrime.securesms.crypto.MasterCipher;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.protocol.Prefix;
|
||||
import org.thoughtcrime.securesms.sms.TextMessage;
|
||||
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
||||
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
|
||||
import org.thoughtcrime.securesms.util.InvalidMessageException;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class EncryptingSmsDatabase extends SmsDatabase {
|
||||
|
||||
private final PlaintextCache plaintextCache = new PlaintextCache();
|
||||
|
||||
public EncryptingSmsDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
||||
super(context, databaseHelper);
|
||||
}
|
||||
|
||||
private String getAsymmetricEncryptedBody(AsymmetricMasterSecret masterSecret, String body) {
|
||||
AsymmetricMasterCipher bodyCipher = new AsymmetricMasterCipher(masterSecret);
|
||||
return Prefix.ASYMMETRIC_LOCAL_ENCRYPT + bodyCipher.encryptBody(body);
|
||||
return bodyCipher.encryptBody(body);
|
||||
}
|
||||
|
||||
private String getEncryptedBody(MasterSecret masterSecret, String body) {
|
||||
MasterCipher bodyCipher = new MasterCipher(masterSecret);
|
||||
return Prefix.SYMMETRIC_ENCRYPT + bodyCipher.encryptBody(body);
|
||||
String ciphertext = bodyCipher.encryptBody(body);
|
||||
plaintextCache.put(ciphertext, body);
|
||||
|
||||
return ciphertext;
|
||||
}
|
||||
|
||||
private long insertMessageSent(MasterSecret masterSecret, String address, long threadId, String body, long date, int type) {
|
||||
String encryptedBody = getEncryptedBody(masterSecret, body);
|
||||
return insertMessageSent(address, threadId, encryptedBody, date, type);
|
||||
public List<Long> insertMessageOutbox(MasterSecret masterSecret, long threadId,
|
||||
OutgoingTextMessage message)
|
||||
{
|
||||
long type = Types.BASE_OUTBOX_TYPE;
|
||||
message = message.withBody(getEncryptedBody(masterSecret, message.getMessageBody()));
|
||||
type |= Types.ENCRYPTION_SYMMETRIC_BIT;
|
||||
|
||||
return insertMessageOutbox(threadId, message, type);
|
||||
}
|
||||
|
||||
public void updateSecureMessageBody(MasterSecret masterSecret, long messageId, String body) {
|
||||
String encryptedBody = getEncryptedBody(masterSecret, body);
|
||||
updateMessageBodyAndType(messageId, encryptedBody, Types.SECURE_RECEIVED_TYPE);
|
||||
public Pair<Long, Long> insertMessageInbox(MasterSecret masterSecret,
|
||||
IncomingTextMessage message)
|
||||
{
|
||||
long type = Types.BASE_INBOX_TYPE;
|
||||
|
||||
if (!message.isSecureMessage()) {
|
||||
type |= Types.ENCRYPTION_SYMMETRIC_BIT;
|
||||
message = message.withMessageBody(getEncryptedBody(masterSecret, message.getMessageBody()));
|
||||
}
|
||||
|
||||
return insertMessageInbox(message, type);
|
||||
}
|
||||
|
||||
public Pair<Long, Long> insertMessageInbox(AsymmetricMasterSecret masterSecret,
|
||||
IncomingTextMessage message)
|
||||
{
|
||||
long type = Types.BASE_INBOX_TYPE;
|
||||
|
||||
if (message.isSecureMessage()) {
|
||||
type |= Types.ENCRYPTION_REMOTE_BIT;
|
||||
} else {
|
||||
message = message.withMessageBody(getAsymmetricEncryptedBody(masterSecret, message.getMessageBody()));
|
||||
type |= Types.ENCRYPTION_ASYMMETRIC_BIT;
|
||||
}
|
||||
|
||||
return insertMessageInbox(message, type);
|
||||
}
|
||||
|
||||
public void updateMessageBody(MasterSecret masterSecret, long messageId, String body) {
|
||||
String encryptedBody = getEncryptedBody(masterSecret, body);
|
||||
updateMessageBodyAndType(messageId, encryptedBody, Types.INBOX_TYPE);
|
||||
updateMessageBodyAndType(messageId, encryptedBody, Types.ENCRYPTION_MASK,
|
||||
Types.ENCRYPTION_SYMMETRIC_BIT);
|
||||
}
|
||||
|
||||
public long insertMessageSent(MasterSecret masterSecret, String address, long threadId, String body, long date) {
|
||||
return insertMessageSent(masterSecret, address, threadId, body, date, Types.ENCRYPTED_OUTBOX_TYPE);
|
||||
public Reader getOutgoingMessages(MasterSecret masterSecret) {
|
||||
Cursor cursor = super.getOutgoingMessages();
|
||||
return new DecryptingReader(masterSecret, cursor);
|
||||
}
|
||||
|
||||
public long insertSecureMessageSent(MasterSecret masterSecret, String address, long threadId, String body, long date) {
|
||||
return insertMessageSent(masterSecret, address, threadId, body, date, Types.ENCRYPTING_TYPE);
|
||||
public Reader getMessage(MasterSecret masterSecret, long messageId) {
|
||||
Cursor cursor = super.getMessage(messageId);
|
||||
return new DecryptingReader(masterSecret, cursor);
|
||||
}
|
||||
|
||||
public long insertMessageReceived(MasterSecret masterSecret, TextMessage message, String body) {
|
||||
String encryptedBody = getEncryptedBody(masterSecret, body);
|
||||
return insertMessageReceived(message, encryptedBody);
|
||||
public Reader getDecryptInProgressMessages(MasterSecret masterSecret) {
|
||||
Cursor cursor = super.getDecryptInProgressMessages();
|
||||
return new DecryptingReader(masterSecret, cursor);
|
||||
}
|
||||
|
||||
public long insertMessageReceived(AsymmetricMasterSecret masterSecret, TextMessage message, String body) {
|
||||
String encryptedBody = getAsymmetricEncryptedBody(masterSecret, body);
|
||||
return insertSecureMessageReceived(message, encryptedBody);
|
||||
public Reader readerFor(MasterSecret masterSecret, Cursor cursor) {
|
||||
return new DecryptingReader(masterSecret, cursor);
|
||||
}
|
||||
|
||||
public class DecryptingReader extends SmsDatabase.Reader {
|
||||
|
||||
private final MasterCipher masterCipher;
|
||||
|
||||
public DecryptingReader(MasterSecret masterSecret, Cursor cursor) {
|
||||
super(cursor);
|
||||
this.masterCipher = new MasterCipher(masterSecret);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBody(Cursor cursor) {
|
||||
long type = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.TYPE));
|
||||
String ciphertext = super.getBody(cursor);
|
||||
|
||||
try {
|
||||
if (SmsDatabase.Types.isSymmetricEncryption(type)) {
|
||||
String plaintext = plaintextCache.get(ciphertext);
|
||||
|
||||
if (plaintext != null)
|
||||
return plaintext;
|
||||
|
||||
plaintext = masterCipher.decryptBody(ciphertext);
|
||||
|
||||
plaintextCache.put(ciphertext, plaintext);
|
||||
return plaintext;
|
||||
} else {
|
||||
return ciphertext;
|
||||
}
|
||||
} catch (InvalidMessageException e) {
|
||||
Log.w("EncryptingSmsDatabase", e);
|
||||
return "Error decrypting message.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class PlaintextCache {
|
||||
private static final int MAX_CACHE_SIZE = 2000;
|
||||
|
||||
private static final LinkedHashMap<String,SoftReference<String>> decryptedBodyCache
|
||||
= new LinkedHashMap<String,SoftReference<String>>()
|
||||
{
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Entry<String,SoftReference<String>> eldest) {
|
||||
return this.size() > MAX_CACHE_SIZE;
|
||||
}
|
||||
};
|
||||
|
||||
public void put(String ciphertext, String plaintext) {
|
||||
decryptedBodyCache.put(ciphertext, new SoftReference<String>(plaintext));
|
||||
}
|
||||
|
||||
public String get(String ciphertext) {
|
||||
synchronized (decryptedBodyCache) {
|
||||
SoftReference<String> plaintextReference = decryptedBodyCache.get(ciphertext);
|
||||
|
||||
if (plaintextReference != null) {
|
||||
String plaintext = plaintextReference.get();
|
||||
|
||||
if (plaintext != null) {
|
||||
return plaintext;
|
||||
} else {
|
||||
decryptedBodyCache.remove(ciphertext);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user