Search in sources :

Example 1 with ECPublicKey

use of org.whispersystems.libaxolotl.ecc.ECPublicKey in project Conversations by siacs.

the class IqParser method preKeys.

public List<PreKeyBundle> preKeys(final IqPacket preKeys) {
    List<PreKeyBundle> bundles = new ArrayList<>();
    Map<Integer, ECPublicKey> preKeyPublics = preKeyPublics(preKeys);
    if (preKeyPublics != null) {
        for (Integer preKeyId : preKeyPublics.keySet()) {
            ECPublicKey preKeyPublic = preKeyPublics.get(preKeyId);
            bundles.add(new PreKeyBundle(0, 0, preKeyId, preKeyPublic, 0, null, null, null));
        }
    }
    return bundles;
}
Also used : PreKeyBundle(org.whispersystems.libaxolotl.state.PreKeyBundle) ECPublicKey(org.whispersystems.libaxolotl.ecc.ECPublicKey) ArrayList(java.util.ArrayList)

Example 2 with ECPublicKey

use of org.whispersystems.libaxolotl.ecc.ECPublicKey in project Conversations by siacs.

the class IqParser method bundle.

public PreKeyBundle bundle(final IqPacket bundle) {
    Element bundleItem = getItem(bundle);
    if (bundleItem == null) {
        return null;
    }
    final Element bundleElement = bundleItem.findChild("bundle");
    if (bundleElement == null) {
        return null;
    }
    ECPublicKey signedPreKeyPublic = signedPreKeyPublic(bundleElement);
    Integer signedPreKeyId = signedPreKeyId(bundleElement);
    byte[] signedPreKeySignature = signedPreKeySignature(bundleElement);
    IdentityKey identityKey = identityKey(bundleElement);
    if (signedPreKeyId == null || signedPreKeyPublic == null || identityKey == null) {
        return null;
    }
    return new PreKeyBundle(0, 0, 0, null, signedPreKeyId, signedPreKeyPublic, signedPreKeySignature, identityKey);
}
Also used : PreKeyBundle(org.whispersystems.libaxolotl.state.PreKeyBundle) IdentityKey(org.whispersystems.libaxolotl.IdentityKey) ECPublicKey(org.whispersystems.libaxolotl.ecc.ECPublicKey) Element(eu.siacs.conversations.xml.Element)

Example 3 with ECPublicKey

use of org.whispersystems.libaxolotl.ecc.ECPublicKey in project Conversations by siacs.

the class IqParser method signedPreKeyPublic.

public ECPublicKey signedPreKeyPublic(final Element bundle) {
    ECPublicKey publicKey = null;
    final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic");
    if (signedPreKeyPublic == null) {
        return null;
    }
    try {
        publicKey = Curve.decodePoint(Base64.decode(signedPreKeyPublic.getContent(), Base64.DEFAULT), 0);
    } catch (Throwable e) {
        Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid signedPreKeyPublic in PEP: " + e.getMessage());
    }
    return publicKey;
}
Also used : ECPublicKey(org.whispersystems.libaxolotl.ecc.ECPublicKey) Element(eu.siacs.conversations.xml.Element)

Example 4 with ECPublicKey

use of org.whispersystems.libaxolotl.ecc.ECPublicKey in project Conversations by siacs.

the class IqParser method preKeyPublics.

public Map<Integer, ECPublicKey> preKeyPublics(final IqPacket packet) {
    Map<Integer, ECPublicKey> preKeyRecords = new HashMap<>();
    Element item = getItem(packet);
    if (item == null) {
        Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Couldn't find <item> in bundle IQ packet: " + packet);
        return null;
    }
    final Element bundleElement = item.findChild("bundle");
    if (bundleElement == null) {
        return null;
    }
    final Element prekeysElement = bundleElement.findChild("prekeys");
    if (prekeysElement == null) {
        Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Couldn't find <prekeys> in bundle IQ packet: " + packet);
        return null;
    }
    for (Element preKeyPublicElement : prekeysElement.getChildren()) {
        if (!preKeyPublicElement.getName().equals("preKeyPublic")) {
            Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Encountered unexpected tag in prekeys list: " + preKeyPublicElement);
            continue;
        }
        Integer preKeyId = null;
        try {
            preKeyId = Integer.valueOf(preKeyPublicElement.getAttribute("preKeyId"));
            final ECPublicKey preKeyPublic = Curve.decodePoint(Base64.decode(preKeyPublicElement.getContent(), Base64.DEFAULT), 0);
            preKeyRecords.put(preKeyId, preKeyPublic);
        } catch (NumberFormatException e) {
            Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "could not parse preKeyId from preKey " + preKeyPublicElement.toString());
        } catch (Throwable e) {
            Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid preKeyPublic (ID=" + preKeyId + ") in PEP: " + e.getMessage() + ", skipping...");
        }
    }
    return preKeyRecords;
}
Also used : ECPublicKey(org.whispersystems.libaxolotl.ecc.ECPublicKey) HashMap(java.util.HashMap) Element(eu.siacs.conversations.xml.Element)

Example 5 with ECPublicKey

use of org.whispersystems.libaxolotl.ecc.ECPublicKey in project Conversations by siacs.

the class IqGenerator method publishBundles.

public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey, final Set<PreKeyRecord> preKeyRecords, final int deviceId) {
    final Element item = new Element("item");
    final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
    final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
    signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
    ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
    signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.DEFAULT));
    final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
    signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.DEFAULT));
    final Element identityKeyElement = bundle.addChild("identityKey");
    identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT));
    final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
    for (PreKeyRecord preKeyRecord : preKeyRecords) {
        final Element prekey = prekeys.addChild("preKeyPublic");
        prekey.setAttribute("preKeyId", preKeyRecord.getId());
        prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.DEFAULT));
    }
    return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item);
}
Also used : ECPublicKey(org.whispersystems.libaxolotl.ecc.ECPublicKey) Element(eu.siacs.conversations.xml.Element) PreKeyRecord(org.whispersystems.libaxolotl.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libaxolotl.state.SignedPreKeyRecord)

Aggregations

ECPublicKey (org.whispersystems.libaxolotl.ecc.ECPublicKey)5 Element (eu.siacs.conversations.xml.Element)4 PreKeyBundle (org.whispersystems.libaxolotl.state.PreKeyBundle)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityKey (org.whispersystems.libaxolotl.IdentityKey)1 PreKeyRecord (org.whispersystems.libaxolotl.state.PreKeyRecord)1 SignedPreKeyRecord (org.whispersystems.libaxolotl.state.SignedPreKeyRecord)1