Skip to content

Commit

Permalink
Fix crash when receiving call with no corresponding identity key.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Feb 17, 2021
1 parent a1457d2 commit 214cb25
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
Expand Up @@ -518,6 +518,7 @@ private void handleUntrustedIdentity(@NonNull WebRtcViewModel event) {
final Recipient recipient = event.getRemoteParticipants().get(0).getRecipient();

if (theirKey == null) {
Log.w(TAG, "Untrusted identity without an identity key, terminating call.");
handleTerminate(recipient, HangupMessage.Type.NORMAL);
}

Expand Down
Expand Up @@ -19,6 +19,9 @@
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.Nullable;

import org.thoughtcrime.securesms.util.ParcelUtil;
import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.InvalidKeyException;

Expand All @@ -40,19 +43,17 @@ public IdentityKeyParcelable[] newArray(int size) {

private final IdentityKey identityKey;

public IdentityKeyParcelable(IdentityKey identityKey) {
public IdentityKeyParcelable(@Nullable IdentityKey identityKey) {
this.identityKey = identityKey;
}

public IdentityKeyParcelable(Parcel in) throws InvalidKeyException {
int serializedLength = in.readInt();
byte[] serialized = new byte[serializedLength];
byte[] serialized = ParcelUtil.readByteArray(in);

in.readByteArray(serialized);
this.identityKey = new IdentityKey(serialized, 0);
this.identityKey = serialized != null ? new IdentityKey(serialized, 0) : null;
}

public IdentityKey get() {
public @Nullable IdentityKey get() {
return identityKey;
}

Expand All @@ -63,7 +64,6 @@ public int describeContents() {

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(identityKey.serialize().length);
dest.writeByteArray(identityKey.serialize());
ParcelUtil.writeByteArray(dest, identityKey != null ? identityKey.serialize() : null);
}
}
Expand Up @@ -85,7 +85,7 @@ private CallParticipant(@NonNull CallParticipantId callParticipantId,
this.deviceOrdinal = deviceOrdinal;
}

public @NonNull CallParticipant withIdentityKey(@NonNull IdentityKey identityKey) {
public @NonNull CallParticipant withIdentityKey(@Nullable IdentityKey identityKey) {
return new CallParticipant(callParticipantId, recipient, identityKey, videoSink, cameraState, videoEnabled, microphoneEnabled, lastSpoke, mediaKeysReceived, addedToCallTime, deviceOrdinal);
}

Expand Down
Expand Up @@ -533,7 +533,7 @@ public WebRtcActionProcessor(@NonNull WebRtcInteractor webRtcInteractor, @NonNul

if (errorCallState == WebRtcViewModel.State.UNTRUSTED_IDENTITY) {
CallParticipant participant = Objects.requireNonNull(currentState.getCallInfoState().getRemoteCallParticipant(activePeer.getRecipient()));
CallParticipant untrusted = participant.withIdentityKey(identityKey.get());
CallParticipant untrusted = participant.withIdentityKey(identityKey.orNull());

builder.changeCallInfoState()
.callState(WebRtcViewModel.State.UNTRUSTED_IDENTITY)
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/org/thoughtcrime/securesms/util/ParcelUtil.java
Expand Up @@ -4,6 +4,7 @@
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -59,4 +60,23 @@ public static void writeBoolean(@NonNull Parcel dest, boolean value) {
public static boolean readBoolean(@NonNull Parcel in) {
return in.readByte() != 0;
}

public static void writeByteArray(@NonNull Parcel dest, @Nullable byte[] data) {
if (data == null) {
dest.writeInt(-1);
} else {
dest.writeInt(data.length);
dest.writeByteArray(data);
}
}

public static @Nullable byte[] readByteArray(@NonNull Parcel in) {
int length = in.readInt();
if (length == -1) {
return null;
}
byte[] data = new byte[length];
in.readByteArray(data);
return data;
}
}

0 comments on commit 214cb25

Please sign in to comment.