Create a Curve25519 asymmetric master secret for users without.

Fixes #1701
This commit is contained in:
Moxie Marlinspike 2014-07-18 20:29:00 -07:00
parent 19ae5043cc
commit 40698212bb
2 changed files with 52 additions and 13 deletions

View File

@ -43,11 +43,13 @@ public class DatabaseUpgradeActivity extends Activity {
public static final int MMS_BODY_VERSION = 46;
public static final int TOFU_IDENTITIES_VERSION = 50;
public static final int CURVE25519_VERSION = 63;
public static final int ASYMMETRIC_MASTER_SECRET_FIX_VERSION = 73;
private static final SortedSet<Integer> UPGRADE_VERSIONS = new TreeSet<Integer>() {{
add(NO_MORE_KEY_EXCHANGE_PREFIX_VERSION);
add(TOFU_IDENTITIES_VERSION);
add(CURVE25519_VERSION);
add(ASYMMETRIC_MASTER_SECRET_FIX_VERSION);
}};
private MasterSecret masterSecret;

View File

@ -16,6 +16,7 @@
*/
package org.thoughtcrime.securesms.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
@ -26,6 +27,7 @@ import android.util.Log;
import org.thoughtcrime.securesms.DatabaseUpgradeActivity;
import org.thoughtcrime.securesms.crypto.DecryptingPartInputStream;
import org.thoughtcrime.securesms.crypto.DecryptingQueue;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.whispersystems.textsecure.crypto.IdentityKey;
import org.whispersystems.textsecure.crypto.InvalidMessageException;
import org.whispersystems.textsecure.crypto.MasterCipher;
@ -419,6 +421,41 @@ public class DatabaseFactory {
}
}
if (fromVersion < DatabaseUpgradeActivity.ASYMMETRIC_MASTER_SECRET_FIX_VERSION) {
if (!MasterSecretUtil.hasAsymmericMasterSecret(context)) {
MasterSecretUtil.generateAsymmetricMasterSecret(context, masterSecret);
MasterCipher masterCipher = new MasterCipher(masterSecret);
Cursor cursor = null;
try {
cursor = db.query(SmsDatabase.TABLE_NAME,
new String[] {SmsDatabase.ID, SmsDatabase.BODY, SmsDatabase.TYPE},
SmsDatabase.TYPE + " & ? == 0",
new String[] {String.valueOf(SmsDatabase.Types.ENCRYPTION_MASK)},
null, null, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String body = cursor.getString(1);
long type = cursor.getLong(2);
String encryptedBody = masterCipher.encryptBody(body);
ContentValues update = new ContentValues();
update.put(SmsDatabase.BODY, encryptedBody);
update.put(SmsDatabase.TYPE, type | SmsDatabase.Types.ENCRYPTION_SYMMETRIC_BIT);
db.update(SmsDatabase.TABLE_NAME, update, SmsDatabase.ID + " = ?",
new String[] {String.valueOf(id)});
}
} finally {
if (cursor != null)
cursor.close();
}
}
}
db.setTransactionSuccessful();
db.endTransaction();