Skip to content

Commit

Permalink
Inject version bytes if missing from existing EC pre-keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-signal committed Jun 9, 2023
1 parent 7f1ee01 commit e8f01be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Base64;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
import org.signal.libsignal.protocol.InvalidKeyException;
import org.signal.libsignal.protocol.ecc.ECPublicKey;
import org.whispersystems.textsecuregcm.metrics.MetricsUtil;

public class ECPublicKeyAdapter {

private static final Counter EC_PUBLIC_KEY_WITHOUT_VERSION_BYTE_COUNTER =
Metrics.counter(MetricsUtil.name(ECPublicKeyAdapter.class, "keyWithoutVersionByte"));

public static class Serializer extends JsonSerializer<ECPublicKey> {

@Override
Expand Down Expand Up @@ -49,7 +55,12 @@ public ECPublicKey deserialize(final JsonParser parser, final DeserializationCon
try {
return new ECPublicKey(ecPublicKeyBytes);
} catch (final InvalidKeyException e) {
throw new JsonParseException(parser, "Could not interpret key bytes as an EC public key", e);
if (ecPublicKeyBytes.length == ECPublicKey.KEY_SIZE - 1) {
EC_PUBLIC_KEY_WITHOUT_VERSION_BYTE_COUNTER.increment();
return ECPublicKey.fromPublicKeyBytes(ecPublicKeyBytes);
}

throw new JsonParseException(parser, "Could not interpret identity key bytes as an EC public key", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private static Stream<Arguments> deserialize() {
return Stream.of(
Arguments.of(String.format(template, "null"), null),
Arguments.of(String.format(template, "\"\""), null),
Arguments.of(String.format(template, "\"" + Base64.getEncoder().encodeToString(EC_PUBLIC_KEY.serialize()) + "\""), EC_PUBLIC_KEY)
Arguments.of(String.format(template, "\"" + Base64.getEncoder().encodeToString(EC_PUBLIC_KEY.serialize()) + "\""), EC_PUBLIC_KEY),
Arguments.of(String.format(template, "\"" + Base64.getEncoder().encodeToString(EC_PUBLIC_KEY.getPublicKeyBytes()) + "\""), EC_PUBLIC_KEY)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private static Stream<Arguments> deserialize() {
return Stream.of(
Arguments.of(String.format(template, "null"), null),
Arguments.of(String.format(template, "\"\""), null),
Arguments.of(String.format(template, "\"" + Base64.getEncoder().encodeToString(IDENTITY_KEY.serialize()) + "\""), IDENTITY_KEY)
Arguments.of(String.format(template, "\"" + Base64.getEncoder().encodeToString(IDENTITY_KEY.serialize()) + "\""), IDENTITY_KEY),
Arguments.of(String.format(template, "\"" + Base64.getEncoder().encodeToString(IDENTITY_KEY.getPublicKey().getPublicKeyBytes()) + "\""), IDENTITY_KEY)
);
}
}

0 comments on commit e8f01be

Please sign in to comment.