From 2cf23892f6d397b8e94df18e551b550f89ec6ffa Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 16 Sep 2018 17:40:59 +0200 Subject: [PATCH] Fail on parsing OMEMO bundle with missing prekeys --- .../omemo/element/OmemoBundleElement.java | 41 ++++++++++++------- .../omemo/OmemoBundleVAxolotlElementTest.java | 16 +++++++- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoBundleElement.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoBundleElement.java index cab41b636..bbd4392e2 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoBundleElement.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoBundleElement.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Map; import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.stringencoder.Base64; @@ -59,10 +60,17 @@ public abstract class OmemoBundleElement implements ExtensionElement { * @param preKeysB64 HashMap of base64 encoded preKeys */ public OmemoBundleElement(int signedPreKeyId, String signedPreKeyB64, String signedPreKeySigB64, String identityKeyB64, HashMap preKeysB64) { + if (signedPreKeyId <= 0) { + throw new IllegalArgumentException("signedPreKeyId MUST be greater than 0."); + } this.signedPreKeyId = signedPreKeyId; - this.signedPreKeyB64 = signedPreKeyB64; - this.signedPreKeySignatureB64 = signedPreKeySigB64; - this.identityKeyB64 = identityKeyB64; + this.signedPreKeyB64 = StringUtils.requireNotNullNorEmpty(signedPreKeyB64, "signedPreKeyB64 MUST NOT be null nor empty."); + this.signedPreKeySignatureB64 = StringUtils.requireNotNullNorEmpty(signedPreKeySigB64, "signedPreKeySigB64 MUST NOT be null nor empty."); + this.identityKeyB64 = StringUtils.requireNotNullNorEmpty(identityKeyB64, "identityKeyB64 MUST NOT be null nor empty."); + + if (preKeysB64 == null || preKeysB64.isEmpty()) { + throw new IllegalArgumentException("PreKeys MUST NOT be null nor empty."); + } this.preKeysB64 = preKeysB64; } @@ -76,22 +84,27 @@ public abstract class OmemoBundleElement implements ExtensionElement { * @param preKeys HashMap of preKeys */ public OmemoBundleElement(int signedPreKeyId, byte[] signedPreKey, byte[] signedPreKeySig, byte[] identityKey, HashMap preKeys) { - this.signedPreKeyId = signedPreKeyId; - + this(signedPreKeyId, + signedPreKey != null ? Base64.encodeToString(signedPreKey) : null, + signedPreKeySig != null ? Base64.encodeToString(signedPreKeySig) : null, + identityKey != null ? Base64.encodeToString(identityKey) : null, + base64EncodePreKeys(preKeys)); this.signedPreKey = signedPreKey; - this.signedPreKeyB64 = Base64.encodeToString(signedPreKey); - this.signedPreKeySignature = signedPreKeySig; - this.signedPreKeySignatureB64 = Base64.encodeToString(signedPreKeySignature); - this.identityKey = identityKey; - this.identityKeyB64 = Base64.encodeToString(identityKey); - this.preKeys = preKeys; - this.preKeysB64 = new HashMap<>(); - for (int id : preKeys.keySet()) { - preKeysB64.put(id, Base64.encodeToString(preKeys.get(id))); + } + + private static HashMap base64EncodePreKeys(HashMap preKeys) { + if (preKeys == null) { + return null; } + + HashMap converted = new HashMap<>(); + for (Integer id : preKeys.keySet()) { + converted.put(id, Base64.encodeToString(preKeys.get(id))); + } + return converted; } /** diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleVAxolotlElementTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleVAxolotlElementTest.java index 701d62dbb..4054c891f 100644 --- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleVAxolotlElementTest.java +++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleVAxolotlElementTest.java @@ -26,11 +26,11 @@ import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.TestUtils; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.stringencoder.Base64; - import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl; import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider; import org.junit.Test; +import org.xmlpull.v1.XmlPullParser; /** * Test serialization and parsing of the OmemoBundleVAxolotlElement. @@ -95,4 +95,18 @@ public class OmemoBundleVAxolotlElementTest extends SmackTestSuite { assertTrue("B64-decoded second preKey must match.", Arrays.equals(secondPreKey, parsed.getPreKey(284))); assertEquals("toString outputs must match.", bundle.toString(), parsed.toString()); } + + @Test(expected = IllegalArgumentException.class) + public void emptyPreKeysShouldFailTest() throws Exception { + String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki"; + XmlPullParser parser = TestUtils.getParser(s); + new OmemoBundleVAxolotlProvider().parse(parser); + } + + @Test(expected = IllegalArgumentException.class) + public void missingPreKeysShouldAlsoFailTest() throws Exception { + String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki"; + XmlPullParser parser = TestUtils.getParser(s); + new OmemoBundleVAxolotlProvider().parse(parser); + } }