From b27334b0ffe457f18e061db780fe3cb9815d2839 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Fri, 9 Jun 2023 09:53:48 -0400 Subject: [PATCH] Treat blank strings as null identity keys --- .../util/IdentityKeyAdapter.java | 4 ++ .../util/IdentityKeyAdapterTest.java | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 service/src/test/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapterTest.java diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapter.java b/service/src/main/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapter.java index 66ab3d589..04bda90f1 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapter.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapter.java @@ -49,6 +49,10 @@ public class IdentityKeyAdapter { throw new JsonParseException(parser, "Could not parse identity key as a base64-encoded value", e); } + if (identityKeyBytes.length == 0) { + return null; + } + try { return new IdentityKey(identityKeyBytes); } catch (final InvalidKeyException e) { diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapterTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapterTest.java new file mode 100644 index 000000000..16e09628d --- /dev/null +++ b/service/src/test/java/org/whispersystems/textsecuregcm/util/IdentityKeyAdapterTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2023 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.signal.libsignal.protocol.IdentityKey; +import org.signal.libsignal.protocol.ecc.Curve; + +import javax.annotation.Nullable; + +import java.util.Base64; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +class IdentityKeyAdapterTest { + + private static final IdentityKey IDENTITY_KEY = new IdentityKey(Curve.generateKeyPair().getPublicKey()); + + private record IdentityKeyCarrier(@JsonSerialize(using = IdentityKeyAdapter.Serializer.class) + @JsonDeserialize(using = IdentityKeyAdapter.Deserializer.class) + IdentityKey identityKey) { + + }; + + @ParameterizedTest + @MethodSource + void deserialize(final String json, @Nullable final IdentityKey expectedIdentityKey) throws JsonProcessingException { + final IdentityKeyCarrier identityKeyCarrier = SystemMapper.jsonMapper().readValue(json, IdentityKeyCarrier.class); + + assertEquals(expectedIdentityKey, identityKeyCarrier.identityKey()); + } + + private static Stream deserialize() { + final String template = """ + { + "identityKey": %s + } + """; + + 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) + ); + } +}