mirror of
				https://github.com/oxen-io/session-android.git
				synced 2025-11-03 16:26:26 +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:
		@@ -25,7 +25,6 @@ import android.util.Log;
 | 
			
		||||
 | 
			
		||||
import org.thoughtcrime.securesms.crypto.MasterCipher;
 | 
			
		||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
 | 
			
		||||
import org.thoughtcrime.securesms.protocol.Prefix;
 | 
			
		||||
import org.thoughtcrime.securesms.recipients.RecipientFactory;
 | 
			
		||||
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
 | 
			
		||||
import org.thoughtcrime.securesms.recipients.Recipients;
 | 
			
		||||
@@ -43,7 +42,7 @@ public class SmsMigrator {
 | 
			
		||||
    if (cursor.isNull(columnIndex)) {
 | 
			
		||||
      statement.bindNull(index);
 | 
			
		||||
    } else {
 | 
			
		||||
      statement.bindString(index, encryptIfNecessary(context, masterSecret, cursor.getString(columnIndex)));
 | 
			
		||||
      statement.bindString(index, encrypt(masterSecret, cursor.getString(columnIndex)));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -71,6 +70,19 @@ public class SmsMigrator {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static void addTranslatedTypeToStatement(SQLiteStatement statement, Cursor cursor,
 | 
			
		||||
                                                   int index, String key)
 | 
			
		||||
  {
 | 
			
		||||
    int columnIndex = cursor.getColumnIndexOrThrow(key);
 | 
			
		||||
 | 
			
		||||
    if (cursor.isNull(columnIndex)) {
 | 
			
		||||
      statement.bindLong(index, SmsDatabase.Types.BASE_INBOX_TYPE | SmsDatabase.Types.ENCRYPTION_SYMMETRIC_BIT);
 | 
			
		||||
    } else {
 | 
			
		||||
      long theirType = cursor.getLong(columnIndex);
 | 
			
		||||
      statement.bindLong(index, SmsDatabase.Types.translateFromSystemBaseType(theirType) | SmsDatabase.Types.ENCRYPTION_SYMMETRIC_BIT);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static void getContentValuesForRow(Context context, MasterSecret masterSecret,
 | 
			
		||||
                                             Cursor cursor, long threadId,
 | 
			
		||||
                                             SQLiteStatement statement)
 | 
			
		||||
@@ -82,7 +94,7 @@ public class SmsMigrator {
 | 
			
		||||
    addIntToStatement(statement, cursor, 5, SmsDatabase.PROTOCOL);
 | 
			
		||||
    addIntToStatement(statement, cursor, 6, SmsDatabase.READ);
 | 
			
		||||
    addIntToStatement(statement, cursor, 7, SmsDatabase.STATUS);
 | 
			
		||||
    addIntToStatement(statement, cursor, 8, SmsDatabase.TYPE);
 | 
			
		||||
    addTranslatedTypeToStatement(statement, cursor, 8, SmsDatabase.TYPE);
 | 
			
		||||
    addIntToStatement(statement, cursor, 9, SmsDatabase.REPLY_PATH_PRESENT);
 | 
			
		||||
    addStringToStatement(statement, cursor, 10, SmsDatabase.SUBJECT);
 | 
			
		||||
    addEncryptedStringToStatement(context, statement, cursor, masterSecret, 11, SmsDatabase.BODY);
 | 
			
		||||
@@ -138,16 +150,10 @@ public class SmsMigrator {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static String encryptIfNecessary(Context context,
 | 
			
		||||
                                           MasterSecret masterSecret,
 | 
			
		||||
                                           String body)
 | 
			
		||||
  private static String encrypt(MasterSecret masterSecret, String body)
 | 
			
		||||
  {
 | 
			
		||||
    if (!body.startsWith(Prefix.SYMMETRIC_ENCRYPT) && !body.startsWith(Prefix.ASYMMETRIC_ENCRYPT)) {
 | 
			
		||||
      MasterCipher masterCipher = new MasterCipher(masterSecret);
 | 
			
		||||
      return Prefix.SYMMETRIC_ENCRYPT + masterCipher.encryptBody(body);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return body;
 | 
			
		||||
    MasterCipher masterCipher = new MasterCipher(masterSecret);
 | 
			
		||||
    return masterCipher.encryptBody(body);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static void migrateConversation(Context context, MasterSecret masterSecret,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user