Skip to content

Commit

Permalink
Remove legacy fields from the Envelope.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal authored and cody-signal committed Aug 18, 2022
1 parent b4ae13f commit 9c266e7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 91 deletions.
Expand Up @@ -58,9 +58,7 @@ public long insert(@NonNull SignalServiceEnvelope envelope) {
ContentValues values = new ContentValues();
values.put(TYPE, envelope.getType());
values.put(SOURCE_UUID, envelope.getSourceUuid().orElse(null));
values.put(SOURCE_E164, envelope.getSourceE164().orElse(null));
values.put(DEVICE_ID, envelope.getSourceDevice());
values.put(LEGACY_MSG, envelope.hasLegacyMessage() ? Base64.encodeBytes(envelope.getLegacyMessage()) : "");
values.put(CONTENT, envelope.hasContent() ? Base64.encodeBytes(envelope.getContent()) : "");
values.put(TIMESTAMP, envelope.getTimestamp());
values.put(SERVER_RECEIVED_TIMESTAMP, envelope.getServerReceivedTimestamp());
Expand Down Expand Up @@ -89,7 +87,6 @@ public SignalServiceEnvelope get(long id) throws NoSuchMessageException {
SignalServiceAddress.fromRaw(uuid, e164),
cursor.getInt(cursor.getColumnIndexOrThrow(DEVICE_ID)),
cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP)),
Util.isEmpty(legacyMessage) ? null : Base64.decode(legacyMessage),
Util.isEmpty(content) ? null : Base64.decode(content),
cursor.getLong(cursor.getColumnIndexOrThrow(SERVER_RECEIVED_TIMESTAMP)),
cursor.getLong(cursor.getColumnIndexOrThrow(SERVER_DELIVERED_TIMESTAMP)),
Expand Down Expand Up @@ -127,17 +124,13 @@ private Optional<Long> find(SignalServiceEnvelope envelope) {
LEGACY_MSG + " = ? AND " +
CONTENT + " = ? AND " +
TIMESTAMP + " = ? AND " +
"(" +
"(" + SOURCE_E164 + " NOT NULL AND " + SOURCE_E164 + " = ?) OR " +
"(" + SOURCE_UUID + " NOT NULL AND " + SOURCE_UUID + " = ?)" +
")";
"(" + SOURCE_UUID + " NOT NULL AND " + SOURCE_UUID + " = ?)";

String[] args = new String[] { String.valueOf(envelope.getType()),
String.valueOf(envelope.getSourceDevice()),
envelope.hasLegacyMessage() ? Base64.encodeBytes(envelope.getLegacyMessage()) : "",
envelope.hasContent() ? Base64.encodeBytes(envelope.getContent()) : "",
String.valueOf(envelope.getTimestamp()),
String.valueOf(envelope.getSourceUuid().orElse(null)),
String.valueOf(envelope.getSourceE164().orElse(null)) };
String.valueOf(envelope.getSourceUuid().orElse(null)) };


try (Cursor cursor = database.query(TABLE_NAME, null, query, args, null, null, null)) {
Expand Down Expand Up @@ -165,7 +158,6 @@ public SignalServiceEnvelope getNext() {
String sourceUuid = cursor.getString(cursor.getColumnIndexOrThrow(SOURCE_UUID));
String sourceE164 = cursor.getString(cursor.getColumnIndexOrThrow(SOURCE_E164));
int deviceId = cursor.getInt(cursor.getColumnIndexOrThrow(DEVICE_ID));
String legacyMessage = cursor.getString(cursor.getColumnIndexOrThrow(LEGACY_MSG));
String content = cursor.getString(cursor.getColumnIndexOrThrow(CONTENT));
long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));
long serverReceivedTimestamp = cursor.getLong(cursor.getColumnIndexOrThrow(SERVER_RECEIVED_TIMESTAMP));
Expand All @@ -176,7 +168,6 @@ public SignalServiceEnvelope getNext() {
SignalServiceAddress.fromRaw(sourceUuid, sourceE164),
deviceId,
timestamp,
legacyMessage != null ? Base64.decode(legacyMessage) : null,
content != null ? Base64.decode(content) : null,
serverReceivedTimestamp,
serverDeliveredTimestamp,
Expand Down
Expand Up @@ -81,11 +81,6 @@ private Processor(@NonNull Context context) {
* one was created. Otherwise null.
*/
public @Nullable String processEnvelope(@NonNull SignalServiceEnvelope envelope) {
if (FeatureFlags.phoneNumberPrivacy() && envelope.hasSourceE164()) {
Log.w(TAG, "PNP enabled -- mimicking PNP by dropping the E164 from the envelope.");
envelope = envelope.withoutE164();
}

if (envelope.hasSourceUuid()) {
Recipient.externalPush(envelope.getSourceAddress());
}
Expand Down
Expand Up @@ -220,7 +220,6 @@ public List<SignalServiceEnvelope> retrieveMessages(MessageReceivedCallback call
Optional.of(address),
entity.getSourceDevice(),
entity.getTimestamp(),
entity.getMessage(),
entity.getContent(),
entity.getServerTimestamp(),
messageResult.getServerDeliveredTimestamp(),
Expand All @@ -230,7 +229,6 @@ public List<SignalServiceEnvelope> retrieveMessages(MessageReceivedCallback call
} else {
envelope = new SignalServiceEnvelope(entity.getType(),
entity.getTimestamp(),
entity.getMessage(),
entity.getContent(),
entity.getServerTimestamp(),
messageResult.getServerDeliveredTimestamp(),
Expand Down
Expand Up @@ -143,18 +143,7 @@ public SignalServiceContent decrypt(SignalServiceEnvelope envelope)
SelfSendException, UnsupportedDataMessageException, InvalidMessageStructureException
{
try {
if (envelope.hasLegacyMessage()) {
Plaintext plaintext = decrypt(envelope, envelope.getLegacyMessage());
SignalServiceProtos.DataMessage dataMessage = SignalServiceProtos.DataMessage.parseFrom(plaintext.getData());

SignalServiceContentProto contentProto = SignalServiceContentProto.newBuilder()
.setLocalAddress(SignalServiceAddressProtobufSerializer.toProtobuf(localAddress))
.setMetadata(SignalServiceMetadataProtobufSerializer.toProtobuf(plaintext.metadata))
.setLegacyDataMessage(dataMessage)
.build();

return SignalServiceContent.createFromProto(contentProto);
} else if (envelope.hasContent()) {
if (envelope.hasContent()) {
Plaintext plaintext = decrypt(envelope, envelope.getContent());
SignalServiceProtos.Content content = SignalServiceProtos.Content.parseFrom(plaintext.getData());

Expand Down
Expand Up @@ -10,8 +10,10 @@
import com.google.protobuf.InvalidProtocolBufferException;

import org.whispersystems.signalservice.api.push.ACI;
import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.OptionalUtil;
import org.whispersystems.signalservice.api.util.Preconditions;
import org.whispersystems.signalservice.api.util.UuidUtil;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.Envelope;
import org.whispersystems.signalservice.internal.serialize.protos.SignalServiceEnvelopeProto;
Expand Down Expand Up @@ -60,7 +62,6 @@ public SignalServiceEnvelope(int type,
Optional<SignalServiceAddress> sender,
int senderDevice,
long timestamp,
byte[] legacyMessage,
byte[] content,
long serverReceivedTimestamp,
long serverDeliveredTimestamp,
Expand All @@ -78,26 +79,22 @@ public SignalServiceEnvelope(int type,

if (sender.isPresent()) {
builder.setSourceUuid(sender.get().getServiceId().toString());

if (sender.get().getNumber().isPresent()) {
builder.setSourceE164(sender.get().getNumber().get());
}
}

if (uuid != null) {
builder.setServerGuid(uuid);
}

if (legacyMessage != null) builder.setLegacyMessage(ByteString.copyFrom(legacyMessage));
if (content != null) builder.setContent(ByteString.copyFrom(content));
if (content != null) {
builder.setContent(ByteString.copyFrom(content));
}

this.envelope = builder.build();
this.serverDeliveredTimestamp = serverDeliveredTimestamp;
}

public SignalServiceEnvelope(int type,
long timestamp,
byte[] legacyMessage,
byte[] content,
long serverReceivedTimestamp,
long serverDeliveredTimestamp,
Expand All @@ -116,19 +113,14 @@ public SignalServiceEnvelope(int type,
builder.setServerGuid(uuid);
}

if (legacyMessage != null) builder.setLegacyMessage(ByteString.copyFrom(legacyMessage));
if (content != null) builder.setContent(ByteString.copyFrom(content));
if (content != null) {
builder.setContent(ByteString.copyFrom(content));
}

this.envelope = builder.build();
this.serverDeliveredTimestamp = serverDeliveredTimestamp;
}

public SignalServiceEnvelope withoutE164() {
return deserialize(serializeToProto().clearSourceE164()
.build()
.toByteArray());
}

public String getServerGuid() {
return envelope.getServerGuid();
}
Expand All @@ -144,13 +136,6 @@ public boolean hasSourceUuid() {
return envelope.hasSourceUuid();
}

/**
* @return The envelope's sender as an E164 number.
*/
public Optional<String> getSourceE164() {
return Optional.ofNullable(envelope.getSourceE164());
}

/**
* @return The envelope's sender as a UUID.
*/
Expand All @@ -159,17 +144,13 @@ public Optional<String> getSourceUuid() {
}

public String getSourceIdentifier() {
return OptionalUtil.or(getSourceUuid(), getSourceE164()).orElse(null);
return getSourceUuid().get().toString();
}

public boolean hasSourceDevice() {
return envelope.hasSourceDevice();
}

public boolean hasSourceE164() {
return envelope.hasSourceE164();
}

/**
* @return The envelope's sender device ID.
*/
Expand All @@ -181,7 +162,7 @@ public int getSourceDevice() {
* @return The envelope's sender as a SignalServiceAddress.
*/
public SignalServiceAddress getSourceAddress() {
return new SignalServiceAddress(ACI.parseOrNull(envelope.getSourceUuid()), envelope.getSourceE164());
return new SignalServiceAddress(ACI.parseOrNull(envelope.getSourceUuid()));
}

/**
Expand Down Expand Up @@ -212,20 +193,6 @@ public long getServerDeliveredTimestamp() {
return serverDeliveredTimestamp;
}

/**
* @return Whether the envelope contains a SignalServiceDataMessage
*/
public boolean hasLegacyMessage() {
return envelope.hasLegacyMessage();
}

/**
* @return The envelope's containing SignalService message.
*/
public byte[] getLegacyMessage() {
return envelope.getLegacyMessage().toByteArray();
}

/**
* @return Whether the envelope contains an encrypted SignalServiceContent
*/
Expand Down Expand Up @@ -294,14 +261,6 @@ private SignalServiceEnvelopeProto.Builder serializeToProto() {
builder.setSourceUuid(getSourceUuid().get());
}

if (getSourceE164().isPresent()) {
builder.setSourceE164(getSourceE164().get());
}

if (hasLegacyMessage()) {
builder.setLegacyMessage(ByteString.copyFrom(getLegacyMessage()));
}

if (hasContent()) {
builder.setContent(ByteString.copyFrom(getContent()));
}
Expand All @@ -311,7 +270,7 @@ private SignalServiceEnvelopeProto.Builder serializeToProto() {
}

if (hasDestinationUuid()) {
builder.setDestinationUuid(getDestinationUuid().toString());
builder.setDestinationUuid(getDestinationUuid());
}

return builder;
Expand All @@ -329,11 +288,14 @@ public static SignalServiceEnvelope deserialize(byte[] serialized) {
e.printStackTrace();
}

Preconditions.checkNotNull(proto);

ServiceId sourceServiceId = proto.hasSourceUuid() ? ServiceId.parseOrNull(proto.getSourceUuid()) : null;

return new SignalServiceEnvelope(proto.getType(),
SignalServiceAddress.fromRaw(proto.getSourceUuid(), proto.getSourceE164()),
sourceServiceId != null ? Optional.of(new SignalServiceAddress(sourceServiceId)) : Optional.empty(),
proto.getDeviceId(),
proto.getTimestamp(),
proto.hasLegacyMessage() ? proto.getLegacyMessage().toByteArray() : null,
proto.hasContent() ? proto.getContent().toByteArray() : null,
proto.getServerReceivedTimestamp(),
proto.getServerDeliveredTimestamp(),
Expand Down
4 changes: 2 additions & 2 deletions libsignal/service/src/main/proto/InternalSerialization.proto
Expand Up @@ -24,9 +24,9 @@ message SignalServiceContentProto {
message SignalServiceEnvelopeProto {
optional int32 type = 1;
optional string sourceUuid = 2;
optional string sourceE164 = 3;
reserved /*sourceE164*/ 3;
optional int32 deviceId = 4;
optional bytes legacyMessage = 5;
reserved /*legacyMessage*/ 5;
optional bytes content = 6;
optional int64 timestamp = 7;
optional int64 serverReceivedTimestamp = 8;
Expand Down
9 changes: 5 additions & 4 deletions libsignal/service/src/main/proto/SignalService.proto
Expand Up @@ -23,18 +23,19 @@ message Envelope {
}

optional Type type = 1;
optional string sourceE164 = 2;
reserved /*sourceE164*/ 2;
optional string sourceUuid = 11;
optional uint32 sourceDevice = 7;
optional string destinationUuid = 13;
optional string relay = 3;
reserved /*relay*/ 3;
optional uint64 timestamp = 5;
optional bytes legacyMessage = 6; // Contains an encrypted DataMessage
reserved /*legacyMessage*/ 6;
optional bytes content = 8; // Contains an encrypted Content
optional string serverGuid = 9;
optional uint64 serverTimestamp = 10;
optional bool urgent = 14 [default = true];
// NEXT ID: 15
reserved /*updatedPni*/ 15; // Not used presently, may be used in the future
// NEXT ID: 16
}

message Content {
Expand Down

0 comments on commit 9c266e7

Please sign in to comment.