Fix some bugs with PKWM padding and attachment detection.

This commit is contained in:
Moxie Marlinspike 2014-11-11 20:29:55 -08:00
parent 9a6f65988f
commit d9d4ec9d9d
5 changed files with 32 additions and 15 deletions

View File

@ -274,6 +274,17 @@ public class SessionCipher {
}
}
public int getSessionVersion() {
synchronized (SESSION_LOCK) {
if (!sessionStore.containsSession(recipientId, deviceId)) {
throw new IllegalStateException(String.format("No session for (%d, %d)!", recipientId, deviceId));
}
SessionRecord record = sessionStore.loadSession(recipientId, deviceId);
return record.getSessionState().getSessionVersion();
}
}
private ChainKey getOrCreateChainKey(SessionState sessionState, ECPublicKey theirEphemeral)
throws InvalidMessageException
{

View File

@ -1,5 +1,7 @@
package org.whispersystems.textsecure.api.crypto;
import android.util.Log;
import com.google.protobuf.InvalidProtocolBufferException;
import org.whispersystems.libaxolotl.DuplicateMessageException;
@ -31,17 +33,13 @@ import static org.whispersystems.textsecure.push.PushMessageProtos.PushMessageCo
public class TextSecureCipher {
private final SessionCipher sessionCipher;
private final PushTransportDetails transportDetails;
public TextSecureCipher(AxolotlStore axolotlStore, long recipientId, int deviceId) {
int sessionVersion = axolotlStore.loadSession(recipientId, deviceId)
.getSessionState().getSessionVersion();
this.transportDetails = new PushTransportDetails(sessionVersion);
this.sessionCipher = new SessionCipher(axolotlStore, recipientId, deviceId);
}
public CiphertextMessage encrypt(byte[] unpaddedMessage) {
PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion());
return sessionCipher.encrypt(transportDetails.getPaddedMessageBody(unpaddedMessage));
}
@ -63,7 +61,9 @@ public class TextSecureCipher {
throw new InvalidMessageException("Unknown type: " + envelope.getType());
}
PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion());
PushMessageContent content = PushMessageContent.parseFrom(transportDetails.getStrippedPaddingMessageBody(paddedMessage));
return createTextSecureMessage(envelope, content);
} catch (InvalidProtocolBufferException e) {
throw new InvalidMessageException(e);

View File

@ -27,11 +27,16 @@ public class TextSecureMessage {
public TextSecureMessage(long timestamp, TextSecureGroup group, List<TextSecureAttachment> attachments, String body, boolean secure, boolean endSession) {
this.timestamp = timestamp;
this.attachments = Optional.fromNullable(attachments);
this.body = Optional.fromNullable(body);
this.group = Optional.fromNullable(group);
this.secure = secure;
this.endSession = endSession;
if (attachments != null && !attachments.isEmpty()) {
this.attachments = Optional.of(attachments);
} else {
this.attachments = Optional.absent();
}
}
public long getTimestamp() {

View File

@ -101,6 +101,10 @@ public class PushDecryptJob extends MasterSecretJob {
else if (message.isGroupUpdate()) handleGroupMessage(masterSecret, envelope, message);
else if (message.getAttachments().isPresent()) handleMediaMessage(masterSecret, envelope, message);
else handleTextMessage(masterSecret, envelope, message);
if (envelope.isPreKeyWhisperMessage()) {
ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob(context));
}
} catch (InvalidVersionException e) {
Log.w(TAG, e);
handleInvalidVersionMessage(masterSecret, envelope);

View File

@ -2,15 +2,16 @@ package org.thoughtcrime.securesms.push;
import android.content.Context;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.crypto.SecurityEvent;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.whispersystems.textsecure.api.TextSecureMessageSender;
public class SecurityEventListener implements TextSecureMessageSender.EventListener {
private static final String TAG = SecurityEventListener.class.getSimpleName();
private final Context context;
public SecurityEventListener(Context context) {
@ -23,10 +24,6 @@ public class SecurityEventListener implements TextSecureMessageSender.EventListe
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
SecurityEvent.broadcastSecurityUpdateEvent(context, threadId);
ApplicationContext.getInstance(context)
.getJobManager()
.add(new RefreshPreKeysJob(context));
}
}