1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 14:22:05 +01:00

Fail on parsing OMEMO bundle with missing prekeys

This commit is contained in:
Paul Schaub 2018-09-16 17:40:59 +02:00
parent a23109af20
commit 2cf23892f6
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 42 additions and 15 deletions

View file

@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
@ -59,10 +60,17 @@ public abstract class OmemoBundleElement implements ExtensionElement {
* @param preKeysB64 HashMap of base64 encoded preKeys * @param preKeysB64 HashMap of base64 encoded preKeys
*/ */
public OmemoBundleElement(int signedPreKeyId, String signedPreKeyB64, String signedPreKeySigB64, String identityKeyB64, HashMap<Integer, String> preKeysB64) { public OmemoBundleElement(int signedPreKeyId, String signedPreKeyB64, String signedPreKeySigB64, String identityKeyB64, HashMap<Integer, String> preKeysB64) {
if (signedPreKeyId <= 0) {
throw new IllegalArgumentException("signedPreKeyId MUST be greater than 0.");
}
this.signedPreKeyId = signedPreKeyId; this.signedPreKeyId = signedPreKeyId;
this.signedPreKeyB64 = signedPreKeyB64; this.signedPreKeyB64 = StringUtils.requireNotNullNorEmpty(signedPreKeyB64, "signedPreKeyB64 MUST NOT be null nor empty.");
this.signedPreKeySignatureB64 = signedPreKeySigB64; this.signedPreKeySignatureB64 = StringUtils.requireNotNullNorEmpty(signedPreKeySigB64, "signedPreKeySigB64 MUST NOT be null nor empty.");
this.identityKeyB64 = identityKeyB64; 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; this.preKeysB64 = preKeysB64;
} }
@ -76,22 +84,27 @@ public abstract class OmemoBundleElement implements ExtensionElement {
* @param preKeys HashMap of preKeys * @param preKeys HashMap of preKeys
*/ */
public OmemoBundleElement(int signedPreKeyId, byte[] signedPreKey, byte[] signedPreKeySig, byte[] identityKey, HashMap<Integer, byte[]> preKeys) { public OmemoBundleElement(int signedPreKeyId, byte[] signedPreKey, byte[] signedPreKeySig, byte[] identityKey, HashMap<Integer, byte[]> 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.signedPreKey = signedPreKey;
this.signedPreKeyB64 = Base64.encodeToString(signedPreKey);
this.signedPreKeySignature = signedPreKeySig; this.signedPreKeySignature = signedPreKeySig;
this.signedPreKeySignatureB64 = Base64.encodeToString(signedPreKeySignature);
this.identityKey = identityKey; this.identityKey = identityKey;
this.identityKeyB64 = Base64.encodeToString(identityKey);
this.preKeys = preKeys; this.preKeys = preKeys;
this.preKeysB64 = new HashMap<>();
for (int id : preKeys.keySet()) {
preKeysB64.put(id, Base64.encodeToString(preKeys.get(id)));
} }
private static HashMap<Integer, String> base64EncodePreKeys(HashMap<Integer, byte[]> preKeys) {
if (preKeys == null) {
return null;
}
HashMap<Integer, String> converted = new HashMap<>();
for (Integer id : preKeys.keySet()) {
converted.put(id, Base64.encodeToString(preKeys.get(id)));
}
return converted;
} }
/** /**

View file

@ -26,11 +26,11 @@ import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils; import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl; import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl;
import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider; import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider;
import org.junit.Test; import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
/** /**
* Test serialization and parsing of the OmemoBundleVAxolotlElement. * 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))); assertTrue("B64-decoded second preKey must match.", Arrays.equals(secondPreKey, parsed.getPreKey(284)));
assertEquals("toString outputs must match.", bundle.toString(), parsed.toString()); assertEquals("toString outputs must match.", bundle.toString(), parsed.toString());
} }
@Test(expected = IllegalArgumentException.class)
public void emptyPreKeysShouldFailTest() throws Exception {
String s = "<bundle xmlns='eu.siacs.conversations.axolotl'><signedPreKeyPublic signedPreKeyId='1'>BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZb</signedPreKeyPublic><signedPreKeySignature>MaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==</signedPreKeySignature><identityKey>BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki</identityKey><prekeys></prekeys></bundle>";
XmlPullParser parser = TestUtils.getParser(s);
new OmemoBundleVAxolotlProvider().parse(parser);
}
@Test(expected = IllegalArgumentException.class)
public void missingPreKeysShouldAlsoFailTest() throws Exception {
String s = "<bundle xmlns='eu.siacs.conversations.axolotl'><signedPreKeyPublic signedPreKeyId='1'>BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZb</signedPreKeyPublic><signedPreKeySignature>MaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==</signedPreKeySignature><identityKey>BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki</identityKey></bundle>";
XmlPullParser parser = TestUtils.getParser(s);
new OmemoBundleVAxolotlProvider().parse(parser);
}
} }