From 75024086c0470fe2df2a08fe3e4e437601b64887 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 7 Dec 2023 15:54:21 +0100 Subject: [PATCH] Add overrides for toString() , equals(..) and hashcode() methods --- .../codec/ohttp/OHttpServerKeys.java | 26 +++++++++++++++++++ .../codec/ohttp/OHttpServerPublicKeys.java | 14 +++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerKeys.java b/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerKeys.java index feabf7a..594feb2 100644 --- a/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerKeys.java +++ b/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerKeys.java @@ -62,6 +62,31 @@ public AsymmetricCipherKeyPair getKeyPair(OHttpCiphersuite ciphersuite) { return null; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + OHttpServerKeys that = (OHttpServerKeys) o; + return keyMap.equals(that.keyMap); + } + + @Override + public String toString() { + return "OHttpServerKeys{" + + "keyMap=" + keyMap + + '}'; + } + + @Override + public int hashCode() { + return keyMap.hashCode(); + } + /* * Encode {@link ServerKeys} into bytes that represent {@link ServerPublicKeys}, using the format * described at https://ietf-wg-ohai.github.io/oblivious-http/draft-ietf-ohai-ohttp.html#section-3.1 @@ -81,6 +106,7 @@ public void encodePublicKeys(HybridPublicKeyEncryption encryption, ByteBuf outpu output.writeBytes(hpke.serializePublicKey(kp.publicParameters())); + // Multiple by 4 as for each cipher we will write 2 short values. output.writeShort(key.getValue().ciphers().size() * 4); for (OHttpKey.Cipher cipher : key.getValue().ciphers()) { output.writeShort(cipher.kdf().id()); diff --git a/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerPublicKeys.java b/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerPublicKeys.java index 3a170ae..9c6c8e7 100644 --- a/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerPublicKeys.java +++ b/codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerPublicKeys.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -30,12 +31,17 @@ import static io.netty.incubator.codec.hpke.HybridPublicKeyEncryption.AEAD; import static io.netty.incubator.codec.hpke.HybridPublicKeyEncryption.KDF; import static io.netty.incubator.codec.hpke.HybridPublicKeyEncryption.KEM; +import static java.util.Objects.requireNonNull; /** * Set of server public keys and cipher suites for a OHTTP client. */ public final class OHttpServerPublicKeys implements Iterable> { - private final Map keys = new HashMap<>(); + private final Map keys; + + public OHttpServerPublicKeys(Map keys) { + this.keys = Collections.unmodifiableMap(requireNonNull(keys, "keys")); + } /** * Return all {@link OHttpKey.PublicKey}s. @@ -77,7 +83,7 @@ public String toString() { * Decode a serialized {@link ServerPublicKeys} on the client. */ public static OHttpServerPublicKeys decode(ByteBuf input) throws CryptoException { - OHttpServerPublicKeys keys = new OHttpServerPublicKeys(); + Map keys = new HashMap<>(); while (input.isReadable()) { byte keyId = input.readByte(); KEM kem = KEM.forId(input.readShort()); @@ -92,8 +98,8 @@ public static OHttpServerPublicKeys decode(ByteBuf input) throws CryptoException ciphers.add(OHttpKey.newCipher(kdf, aead)); } OHttpKey.PublicKey publicKey = OHttpKey.newPublicKey(keyId, kem, ciphers, publicKeyBytes); - keys.keys.put(keyId, publicKey); + keys.put(keyId, publicKey); } - return keys; + return new OHttpServerPublicKeys(keys); } }