Add first cut of protocol v3 support.

1) Use the new /v2/keys API for storing/retrieving prekey bundles.

2) For sessions built with PreKeyBundle and PreKeyWhisperMessage,
   use a v3 ratcheting session when available.
This commit is contained in:
Moxie Marlinspike
2014-07-05 12:47:01 -07:00
parent 2ed8d333d9
commit 811479d168
52 changed files with 10137 additions and 4957 deletions

View File

@@ -10,12 +10,19 @@ import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libaxolotl.IdentityKey;
import org.whispersystems.libaxolotl.IdentityKeyPair;
import org.whispersystems.libaxolotl.state.DeviceKeyRecord;
import org.whispersystems.libaxolotl.state.DeviceKeyStore;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.crypto.PreKeyUtil;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.storage.TextSecurePreKeyStore;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@@ -62,6 +69,8 @@ public class PreKeyService extends Service {
}
public void run() {
DeviceKeyRecord deviceKeyRecord = null;
try {
if (!TextSecurePreferences.isPushRegistered(context)) return;
@@ -75,13 +84,60 @@ public class PreKeyService extends Service {
List<PreKeyRecord> preKeyRecords = PreKeyUtil.generatePreKeys(context, masterSecret);
PreKeyRecord lastResortKeyRecord = PreKeyUtil.generateLastResortKey(context, masterSecret);
IdentityKey identityKey = IdentityKeyUtil.getIdentityKey(context);
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
deviceKeyRecord = PreKeyUtil.generateDeviceKey(context, masterSecret, identityKey);
Log.w(TAG, "Registering new prekeys...");
socket.registerPreKeys(identityKey, lastResortKeyRecord, preKeyRecords);
socket.registerPreKeys(identityKey.getPublicKey(), lastResortKeyRecord,
deviceKeyRecord, preKeyRecords);
removeOldDeviceKeysIfNecessary(deviceKeyRecord);
} catch (IOException e) {
Log.w(TAG, e);
if (deviceKeyRecord != null) {
Log.w(TAG, "Remote store failed, removing generated device key: " + deviceKeyRecord.getId());
new TextSecurePreKeyStore(context, masterSecret).removeDeviceKey(deviceKeyRecord.getId());
}
}
}
private void removeOldDeviceKeysIfNecessary(DeviceKeyRecord currentDeviceKey) {
DeviceKeyStore deviceKeyStore = new TextSecurePreKeyStore(context, masterSecret);
List<DeviceKeyRecord> records = deviceKeyStore.loadDeviceKeys();
Iterator<DeviceKeyRecord> iterator = records.iterator();
while (iterator.hasNext()) {
if (iterator.next().getId() == currentDeviceKey.getId()) {
iterator.remove();
}
}
DeviceKeyRecord[] recordsArray = (DeviceKeyRecord[])records.toArray();
Arrays.sort(recordsArray, new Comparator<DeviceKeyRecord>() {
@Override
public int compare(DeviceKeyRecord lhs, DeviceKeyRecord rhs) {
if (lhs.getTimestamp() < rhs.getTimestamp()) return -1;
else if (lhs.getTimestamp() > rhs.getTimestamp()) return 1;
else return 0;
}
});
Log.w(TAG, "Existing device key record count: " + recordsArray.length);
if (recordsArray.length > 3) {
long oldTimestamp = System.currentTimeMillis() - (14 * 24 * 60 * 60 * 1000);
DeviceKeyRecord[] oldRecords = Arrays.copyOf(recordsArray, recordsArray.length - 1);
for (DeviceKeyRecord oldRecord : oldRecords) {
Log.w(TAG, "Old device key record timestamp: " + oldRecord.getTimestamp());
if (oldRecord.getTimestamp() <= oldTimestamp) {
Log.w(TAG, "Remove device key record: " + oldRecord.getId());
deviceKeyStore.removeDeviceKey(oldRecord.getId());
}
}
}
}
}

View File

@@ -185,7 +185,7 @@ public class PushReceiver {
database.updateMessageBody(masterSecret, messageAndThreadId.first, messageContent.getBody());
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
sessionStore.deleteAll(recipient.getRecipientId());
sessionStore.deleteAllSessions(recipient.getRecipientId());
KeyExchangeProcessor.broadcastSecurityUpdateEvent(context, messageAndThreadId.second);
} catch (RecipientFormattingException e) {

View File

@@ -17,7 +17,8 @@ import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libaxolotl.IdentityKey;
import org.whispersystems.libaxolotl.IdentityKeyPair;
import org.whispersystems.libaxolotl.state.DeviceKeyRecord;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.util.KeyHelper;
import org.whispersystems.textsecure.crypto.MasterSecret;
@@ -227,10 +228,11 @@ public class RegistrationService extends Service {
throws IOException
{
setState(new RegistrationState(RegistrationState.STATE_GENERATING_KEYS, number));
IdentityKey identityKey = IdentityKeyUtil.getIdentityKey(this);
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(this, masterSecret);
List<PreKeyRecord> records = PreKeyUtil.generatePreKeys(this, masterSecret);
PreKeyRecord lastResort = PreKeyUtil.generateLastResortKey(this, masterSecret);
socket.registerPreKeys(identityKey, lastResort, records);
DeviceKeyRecord deviceKey = PreKeyUtil.generateDeviceKey(this, masterSecret, identityKey);
socket.registerPreKeys(identityKey.getPublicKey(), lastResort, deviceKey, records);
setState(new RegistrationState(RegistrationState.STATE_GCM_REGISTERING, number));

View File

@@ -140,7 +140,7 @@ public class SmsSender {
if (record != null && record.isEndSession()) {
Log.w("SmsSender", "Ending session...");
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
sessionStore.deleteAll(record.getIndividualRecipient().getRecipientId());
sessionStore.deleteAllSessions(record.getIndividualRecipient().getRecipientId());
KeyExchangeProcessor.broadcastSecurityUpdateEvent(context, record.getThreadId());
}