remove white space before parsing omemo bundle base64

This commit is contained in:
Daniel Gultsch 2020-05-08 19:33:49 +02:00
parent 285c750e69
commit abfa4eae08

View file

@ -5,6 +5,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import com.google.common.base.CharMatcher;
import com.google.common.io.BaseEncoding; import com.google.common.io.BaseEncoding;
import org.whispersystems.libsignal.IdentityKey; import org.whispersystems.libsignal.IdentityKey;
@ -206,7 +207,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
return null; return null;
} }
try { try {
publicKey = Curve.decodePoint(BaseEncoding.base64().decode(signedPreKeyPublic), 0); publicKey = Curve.decodePoint(base64decode(signedPreKeyPublic), 0);
} catch (final IllegalArgumentException | InvalidKeyException e) { } catch (final IllegalArgumentException | InvalidKeyException e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid signedPreKeyPublic in PEP: " + e.getMessage()); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid signedPreKeyPublic in PEP: " + e.getMessage());
} }
@ -219,7 +220,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
return null; return null;
} }
try { try {
return BaseEncoding.base64().decode(signedPreKeySignature); return base64decode(signedPreKeySignature);
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : Invalid base64 in signedPreKeySignature"); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : Invalid base64 in signedPreKeySignature");
return null; return null;
@ -232,7 +233,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
return null; return null;
} }
try { try {
return new IdentityKey(BaseEncoding.base64().decode(identityKey), 0); return new IdentityKey(base64decode(identityKey), 0);
} catch (final IllegalArgumentException | InvalidKeyException e) { } catch (final IllegalArgumentException | InvalidKeyException e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid identityKey in PEP: " + e.getMessage()); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid identityKey in PEP: " + e.getMessage());
return null; return null;
@ -260,10 +261,14 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Encountered unexpected tag in prekeys list: " + preKeyPublicElement); Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Encountered unexpected tag in prekeys list: " + preKeyPublicElement);
continue; continue;
} }
final String preKey = preKeyPublicElement.getContent();
if (preKey == null) {
continue;
}
Integer preKeyId = null; Integer preKeyId = null;
try { try {
preKeyId = Integer.valueOf(preKeyPublicElement.getAttribute("preKeyId")); preKeyId = Integer.valueOf(preKeyPublicElement.getAttribute("preKeyId"));
final ECPublicKey preKeyPublic = Curve.decodePoint(BaseEncoding.base64().decode(preKeyPublicElement.getContent()), 0); final ECPublicKey preKeyPublic = Curve.decodePoint(base64decode(preKey), 0);
preKeyRecords.put(preKeyId, preKeyPublic); preKeyRecords.put(preKeyId, preKeyPublic);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "could not parse preKeyId from preKey " + preKeyPublicElement.toString()); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "could not parse preKeyId from preKey " + preKeyPublicElement.toString());
@ -274,6 +279,10 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
return preKeyRecords; return preKeyRecords;
} }
private static byte[] base64decode(String input) {
return BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(input));
}
public Pair<X509Certificate[], byte[]> verification(final IqPacket packet) { public Pair<X509Certificate[], byte[]> verification(final IqPacket packet) {
Element item = getItem(packet); Element item = getItem(packet);
Element verification = item != null ? item.findChild("verification", AxolotlService.PEP_PREFIX) : null; Element verification = item != null ? item.findChild("verification", AxolotlService.PEP_PREFIX) : null;