Skip to content

Commit

Permalink
Fix some bugs with PKWM padding and attachment detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
moxie0 committed Nov 12, 2014
1 parent 9a6f659 commit d9d4ec9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ public int getRemoteRegistrationId() {
}
}

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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.whispersystems.textsecure.api.crypto;

import android.util.Log;

import com.google.protobuf.InvalidProtocolBufferException;

import org.whispersystems.libaxolotl.DuplicateMessageException;
Expand Down Expand Up @@ -30,18 +32,14 @@

public class TextSecureCipher {

private final SessionCipher sessionCipher;
private final PushTransportDetails transportDetails;
private final SessionCipher sessionCipher;

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);
this.sessionCipher = new SessionCipher(axolotlStore, recipientId, deviceId);
}

public CiphertextMessage encrypt(byte[] unpaddedMessage) {
PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion());
return sessionCipher.encrypt(transportDetails.getPaddedMessageBody(unpaddedMessage));
}

Expand All @@ -63,7 +61,9 @@ public TextSecureMessage decrypt(TextSecureEnvelope envelope)
throw new InvalidMessageException("Unknown type: " + envelope.getType());
}

PushMessageContent content = PushMessageContent.parseFrom(transportDetails.getStrippedPaddingMessageBody(paddedMessage));
PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion());
PushMessageContent content = PushMessageContent.parseFrom(transportDetails.getStrippedPaddingMessageBody(paddedMessage));

return createTextSecureMessage(envelope, content);
} catch (InvalidProtocolBufferException e) {
throw new InvalidMessageException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ public TextSecureMessage(long timestamp, TextSecureGroup group, List<TextSecureA

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() {
Expand Down
4 changes: 4 additions & 0 deletions src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ private void handleMessage(MasterSecret masterSecret, TextSecureEnvelope envelop
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

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) {
Expand All @@ -23,10 +24,6 @@ public void onSecurityEvent(long recipientId) {
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);

SecurityEvent.broadcastSecurityUpdateEvent(context, threadId);

ApplicationContext.getInstance(context)
.getJobManager()
.add(new RefreshPreKeysJob(context));
}

}

0 comments on commit d9d4ec9

Please sign in to comment.