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

@@ -118,6 +118,6 @@ public class AutoInitiateActivity extends Activity {
Recipient recipient)
{
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
return sessionStore.contains(recipient.getRecipientId(), RecipientDevice.DEFAULT_DEVICE_ID);
return sessionStore.containsSession(recipient.getRecipientId(), RecipientDevice.DEFAULT_DEVICE_ID);
}
}

View File

@@ -319,8 +319,8 @@ public class ConversationActivity extends PassphraseRequiredSherlockFragmentActi
SessionStore sessionStore = new TextSecureSessionStore(this, masterSecret);
Recipient primaryRecipient = getRecipients() == null ? null : getRecipients().getPrimaryRecipient();
boolean isPushDestination = DirectoryHelper.isPushDestination(this, getRecipients());
boolean isSecureDestination = isSingleConversation() && sessionStore.contains(primaryRecipient.getRecipientId(),
RecipientDevice.DEFAULT_DEVICE_ID);
boolean isSecureDestination = isSingleConversation() && sessionStore.containsSession(primaryRecipient.getRecipientId(),
RecipientDevice.DEFAULT_DEVICE_ID);
getMenuInflater().inflate(R.menu.conversation_button_context, menu);
@@ -693,8 +693,8 @@ public class ConversationActivity extends PassphraseRequiredSherlockFragmentActi
SessionStore sessionStore = new TextSecureSessionStore(this, masterSecret);
Recipient primaryRecipient = getRecipients() == null ? null : getRecipients().getPrimaryRecipient();
boolean isPushDestination = DirectoryHelper.isPushDestination(this, getRecipients());
boolean isSecureDestination = isSingleConversation() && sessionStore.contains(primaryRecipient.getRecipientId(),
RecipientDevice.DEFAULT_DEVICE_ID);
boolean isSecureDestination = isSingleConversation() && sessionStore.containsSession(primaryRecipient.getRecipientId(),
RecipientDevice.DEFAULT_DEVICE_ID);
if (isPushDestination || isSecureDestination) {
this.isEncryptedConversation = true;

View File

@@ -183,8 +183,8 @@ public class VerifyIdentityActivity extends KeyScanningActivity {
private IdentityKey getRemoteIdentityKey(MasterSecret masterSecret, Recipient recipient) {
SessionStore sessionStore = new TextSecureSessionStore(this, masterSecret);
SessionRecord record = sessionStore.load(recipient.getRecipientId(),
RecipientDevice.DEFAULT_DEVICE_ID);
SessionRecord record = sessionStore.loadSession(recipient.getRecipientId(),
RecipientDevice.DEFAULT_DEVICE_ID);
if (record == null) {
return null;

View File

@@ -205,7 +205,7 @@ public class DecryptingQueue {
Recipient recipient = recipients.getPrimaryRecipient();
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), message.getSourceDevice());
if (!sessionStore.contains(recipientDevice.getRecipientId(), recipientDevice.getDeviceId())) {
if (!sessionStore.containsSession(recipientDevice.getRecipientId(), recipientDevice.getDeviceId())) {
sendResult(PushReceiver.RESULT_NO_SESSION);
return;
}
@@ -280,7 +280,7 @@ public class DecryptingQueue {
return;
}
if (!sessionStore.contains(recipientDevice.getRecipientId(), recipientDevice.getDeviceId())) {
if (!sessionStore.containsSession(recipientDevice.getRecipientId(), recipientDevice.getDeviceId())) {
Log.w("DecryptingQueue", "No such recipient session for MMS...");
database.markAsNoSession(messageId, threadId);
return;
@@ -371,7 +371,7 @@ public class DecryptingQueue {
SmsTransportDetails transportDetails = new SmsTransportDetails();
byte[] decodedCiphertext = transportDetails.getDecodedMessage(body.getBytes());
if (!sessionStore.contains(recipientDevice.getRecipientId(), recipientDevice.getDeviceId())) {
if (!sessionStore.containsSession(recipientDevice.getRecipientId(), recipientDevice.getDeviceId())) {
if (WhisperMessage.isLegacy(decodedCiphertext)) database.markAsLegacyVersion(messageId);
else database.markAsNoSession(messageId);
return;
@@ -384,9 +384,9 @@ public class DecryptingQueue {
if (isEndSession &&
"TERMINATE".equals(plaintextBody) &&
sessionStore.contains(recipientDevice.getRecipientId(), recipientDevice.getDeviceId()))
sessionStore.containsSession(recipientDevice.getRecipientId(), recipientDevice.getDeviceId()))
{
sessionStore.delete(recipientDevice.getRecipientId(), recipientDevice.getDeviceId());
sessionStore.deleteSession(recipientDevice.getRecipientId(), recipientDevice.getDeviceId());
}
} catch (InvalidMessageException | IOException | RecipientFormattingException e) {
Log.w("DecryptionQueue", e);

View File

@@ -28,6 +28,7 @@ import org.thoughtcrime.securesms.sms.OutgoingKeyExchangeMessage;
import org.thoughtcrime.securesms.util.Dialogs;
import org.whispersystems.libaxolotl.SessionBuilder;
import org.whispersystems.libaxolotl.protocol.KeyExchangeMessage;
import org.whispersystems.libaxolotl.state.DeviceKeyStore;
import org.whispersystems.libaxolotl.state.IdentityKeyStore;
import org.whispersystems.libaxolotl.state.PreKeyStore;
import org.whispersystems.libaxolotl.state.SessionRecord;
@@ -62,10 +63,11 @@ public class KeyExchangeInitiator {
private static void initiateKeyExchange(Context context, MasterSecret masterSecret, Recipient recipient) {
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context, masterSecret);
DeviceKeyStore deviceKeyStore = new TextSecurePreKeyStore(context, masterSecret);
IdentityKeyStore identityKeyStore = new TextSecureIdentityKeyStore(context, masterSecret);
SessionBuilder sessionBuilder = new SessionBuilder(sessionStore, preKeyStore, identityKeyStore,
recipient.getRecipientId(),
SessionBuilder sessionBuilder = new SessionBuilder(sessionStore, preKeyStore, deviceKeyStore,
identityKeyStore, recipient.getRecipientId(),
RecipientDevice.DEFAULT_DEVICE_ID);
KeyExchangeMessage keyExchangeMessage = sessionBuilder.process();
@@ -79,7 +81,7 @@ public class KeyExchangeInitiator {
Recipient recipient)
{
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
SessionRecord sessionRecord = sessionStore.load(recipient.getRecipientId(), RecipientDevice.DEFAULT_DEVICE_ID);
SessionRecord sessionRecord = sessionStore.loadSession(recipient.getRecipientId(), RecipientDevice.DEFAULT_DEVICE_ID);
return sessionRecord.getSessionState().hasPendingPreKey();
}

View File

@@ -15,7 +15,9 @@ import org.whispersystems.libaxolotl.StaleKeyExchangeException;
import org.whispersystems.libaxolotl.UntrustedIdentityException;
import org.whispersystems.libaxolotl.protocol.KeyExchangeMessage;
import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage;
import org.whispersystems.libaxolotl.state.DeviceKeyStore;
import org.whispersystems.libaxolotl.state.IdentityKeyStore;
import org.whispersystems.libaxolotl.state.PreKeyBundle;
import org.whispersystems.libaxolotl.state.PreKeyStore;
import org.whispersystems.libaxolotl.state.SessionStore;
import org.whispersystems.textsecure.crypto.MasterSecret;
@@ -48,10 +50,11 @@ public class KeyExchangeProcessor {
IdentityKeyStore identityKeyStore = new TextSecureIdentityKeyStore(context, masterSecret);
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context, masterSecret);
DeviceKeyStore deviceKeyStore = new TextSecurePreKeyStore(context, masterSecret);
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
this.sessionBuilder = new SessionBuilder(sessionStore, preKeyStore, identityKeyStore,
recipientDevice.getRecipientId(),
this.sessionBuilder = new SessionBuilder(sessionStore, preKeyStore, deviceKeyStore,
identityKeyStore, recipientDevice.getRecipientId(),
recipientDevice.getDeviceId());
}
@@ -62,10 +65,10 @@ public class KeyExchangeProcessor {
PreKeyService.initiateRefresh(context, masterSecret);
}
public void processKeyExchangeMessage(PreKeyEntity message, long threadId)
public void processKeyExchangeMessage(PreKeyBundle bundle, long threadId)
throws InvalidKeyException, UntrustedIdentityException
{
sessionBuilder.process(message);
sessionBuilder.process(bundle);
if (threadId != -1) {
broadcastSecurityUpdateEvent(context, threadId);

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());
}

View File

@@ -37,6 +37,7 @@ import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.libaxolotl.InvalidKeyException;
import org.whispersystems.libaxolotl.SessionCipher;
import org.whispersystems.libaxolotl.protocol.CiphertextMessage;
import org.whispersystems.libaxolotl.state.PreKeyBundle;
import org.whispersystems.libaxolotl.state.SessionStore;
import org.whispersystems.textsecure.crypto.AttachmentCipher;
import org.whispersystems.textsecure.crypto.MasterSecret;
@@ -45,7 +46,6 @@ import org.whispersystems.textsecure.push.MismatchedDevices;
import org.whispersystems.textsecure.push.MismatchedDevicesException;
import org.whispersystems.textsecure.push.OutgoingPushMessage;
import org.whispersystems.textsecure.push.OutgoingPushMessageList;
import org.whispersystems.textsecure.push.PreKeyEntity;
import org.whispersystems.textsecure.push.PushAddress;
import org.whispersystems.textsecure.push.PushAttachmentData;
import org.whispersystems.textsecure.push.PushAttachmentPointer;
@@ -95,7 +95,7 @@ public class PushTransport extends BaseTransport {
if (message.isEndSession()) {
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
sessionStore.deleteAll(recipient.getRecipientId());
sessionStore.deleteAllSessions(recipient.getRecipientId());
KeyExchangeProcessor.broadcastSecurityUpdateEvent(context, threadId);
}
@@ -205,12 +205,12 @@ public class PushTransport extends BaseTransport {
long recipientId = recipient.getRecipientId();
for (int extraDeviceId : mismatchedDevices.getExtraDevices()) {
sessionStore.delete(recipientId, extraDeviceId);
sessionStore.deleteSession(recipientId, extraDeviceId);
}
for (int missingDeviceId : mismatchedDevices.getMissingDevices()) {
PushAddress address = PushAddress.create(context, recipientId, e164number, missingDeviceId);
PreKeyEntity preKey = socket.getPreKey(address);
PushAddress address = PushAddress.create(context, recipientId, e164number, missingDeviceId);
PreKeyBundle preKey = socket.getPreKey(address);
KeyExchangeProcessor processor = new KeyExchangeProcessor(context, masterSecret, address);
try {
@@ -230,7 +230,7 @@ public class PushTransport extends BaseTransport {
long recipientId = recipient.getRecipientId();
for (int staleDeviceId : staleDevices.getStaleDevices()) {
sessionStore.delete(recipientId, staleDeviceId);
sessionStore.deleteSession(recipientId, staleDeviceId);
}
}
@@ -327,10 +327,10 @@ public class PushTransport extends BaseTransport {
{
if (!SessionUtil.hasEncryptCapableSession(context, masterSecret, pushAddress)) {
try {
List<PreKeyEntity> preKeys = socket.getPreKeys(pushAddress);
List<PreKeyBundle> preKeys = socket.getPreKeys(pushAddress);
for (PreKeyEntity preKey : preKeys) {
PushAddress device = PushAddress.create(context, pushAddress.getRecipientId(), pushAddress.getNumber(), preKey.getDeviceId());
for (PreKeyBundle preKey : preKeys) {
PushAddress device = PushAddress.create(context, pushAddress.getRecipientId(), pushAddress.getNumber(), preKey.getDeviceId());
KeyExchangeProcessor processor = new KeyExchangeProcessor(context, masterSecret, device);
try {