Search in sources :

Example 31 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class AxolotlService method findDevicesWithoutSession.

public Set<SignalProtocolAddress> findDevicesWithoutSession(final Conversation conversation) {
    Set<SignalProtocolAddress> addresses = new HashSet<>();
    for (Jid jid : getCryptoTargets(conversation)) {
        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + jid);
        if (deviceIds.get(jid) != null) {
            for (Integer foreignId : this.deviceIds.get(jid)) {
                SignalProtocolAddress address = new SignalProtocolAddress(jid.toPreppedString(), foreignId);
                if (sessions.get(address) == null) {
                    IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
                    if (identityKey != null) {
                        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
                        XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
                        sessions.put(address, session);
                    } else {
                        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + jid + ":" + foreignId);
                        if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
                            addresses.add(address);
                        } else {
                            Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
                        }
                    }
                }
            }
        } else {
            mXmppConnectionService.keyStatusUpdated(FetchStatus.ERROR);
            Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
        }
    }
    if (deviceIds.get(account.getJid().toBareJid()) != null) {
        for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
            SignalProtocolAddress address = new SignalProtocolAddress(account.getJid().toBareJid().toPreppedString(), ownId);
            if (sessions.get(address) == null) {
                IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
                if (identityKey != null) {
                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
                    XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
                    sessions.put(address, session);
                } else {
                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
                    if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
                        addresses.add(address);
                    } else {
                        Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
                    }
                }
            }
        }
    }
    return addresses;
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) Jid(de.pixart.messenger.xmpp.jid.Jid) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) HashSet(java.util.HashSet)

Example 32 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class AxolotlService method verifySessionWithPEP.

private void verifySessionWithPEP(final XmppAxolotlSession session) {
    Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
    final SignalProtocolAddress address = session.getRemoteAddress();
    final IdentityKey identityKey = session.getIdentityKey();
    try {
        IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.fromString(address.getName()), address.getDeviceId());
        mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                Pair<X509Certificate[], byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
                if (verification != null) {
                    try {
                        Signature verifier = Signature.getInstance("sha256WithRSA");
                        verifier.initVerify(verification.first[0]);
                        verifier.update(identityKey.serialize());
                        if (verifier.verify(verification.second)) {
                            try {
                                mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
                                String fingerprint = session.getFingerprint();
                                Log.d(Config.LOGTAG, "verified session with x.509 signature. fingerprint was: " + fingerprint);
                                setFingerprintTrust(fingerprint, FingerprintStatus.createActiveVerified(true));
                                axolotlStore.setFingerprintCertificate(fingerprint, verification.first[0]);
                                fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED);
                                Bundle information = CryptoHelper.extractCertificateInformation(verification.first[0]);
                                try {
                                    final String cn = information.getString("subject_cn");
                                    final Jid jid = Jid.fromString(address.getName());
                                    Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
                                    account.getRoster().getContact(jid).setCommonName(cn);
                                } catch (final InvalidJidException ignored) {
                                // ignored
                                }
                                finishBuildingSessionsFromPEP(address);
                                return;
                            } catch (Exception e) {
                                Log.d(Config.LOGTAG, "could not verify certificate");
                            }
                        }
                    } catch (Exception e) {
                        Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
                    }
                } else {
                    Log.d(Config.LOGTAG, "no verification found");
                }
                fetchStatusMap.put(address, FetchStatus.SUCCESS);
                finishBuildingSessionsFromPEP(address);
            }
        });
    } catch (InvalidJidException e) {
        fetchStatusMap.put(address, FetchStatus.SUCCESS);
        finishBuildingSessionsFromPEP(address);
    }
}
Also used : Account(de.pixart.messenger.entities.Account) IdentityKey(org.whispersystems.libsignal.IdentityKey) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) Jid(de.pixart.messenger.xmpp.jid.Jid) Bundle(android.os.Bundle) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) X509Certificate(java.security.cert.X509Certificate) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) Signature(java.security.Signature) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) Pair(android.util.Pair)

Example 33 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class AbstractParser method parseItem.

public static MucOptions.User parseItem(Conversation conference, Element item, Jid fullJid) {
    final String local = conference.getJid().getLocalpart();
    final String domain = conference.getJid().getDomainpart();
    String affiliation = item.getAttribute("affiliation");
    String role = item.getAttribute("role");
    String nick = item.getAttribute("nick");
    if (nick != null && fullJid == null) {
        try {
            fullJid = Jid.fromParts(local, domain, nick);
        } catch (InvalidJidException e) {
            fullJid = null;
        }
    }
    Jid realJid = item.getAttributeAsJid("jid");
    MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid);
    user.setRealJid(realJid);
    user.setAffiliation(affiliation);
    user.setRole(role);
    return user;
}
Also used : MucOptions(de.pixart.messenger.entities.MucOptions) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException)

Example 34 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class IqParser method rosterItems.

private void rosterItems(final Account account, final Element query) {
    final String version = query.getAttribute("ver");
    if (version != null) {
        account.getRoster().setVersion(version);
    }
    for (final Element item : query.getChildren()) {
        if (item.getName().equals("item")) {
            final Jid jid = item.getAttributeAsJid("jid");
            if (jid == null) {
                continue;
            }
            final String name = item.getAttribute("name");
            final String subscription = item.getAttribute("subscription");
            final Contact contact = account.getRoster().getContact(jid);
            boolean bothPre = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM);
            if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
                contact.setServerName(name);
                contact.parseGroupsFromElement(item);
            }
            if ("remove".equals(subscription)) {
                contact.resetOption(Contact.Options.IN_ROSTER);
                contact.resetOption(Contact.Options.DIRTY_DELETE);
                contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
            } else {
                contact.setOption(Contact.Options.IN_ROSTER);
                contact.resetOption(Contact.Options.DIRTY_PUSH);
                contact.parseSubscriptionFromElement(item);
            }
            boolean both = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM);
            if ((both != bothPre) && both) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": gained mutual presence subscription with " + contact.getJid());
                AxolotlService axolotlService = account.getAxolotlService();
                if (axolotlService != null) {
                    axolotlService.clearErrorsInFetchStatusMap(contact.getJid());
                }
            }
            mXmppConnectionService.getAvatarService().clear(contact);
        }
    }
    mXmppConnectionService.updateConversationUi();
    mXmppConnectionService.updateRosterUi();
    mXmppConnectionService.getShortcutService().refresh();
}
Also used : AxolotlService(de.pixart.messenger.crypto.axolotl.AxolotlService) Jid(de.pixart.messenger.xmpp.jid.Jid) Element(de.pixart.messenger.xml.Element) Contact(de.pixart.messenger.entities.Contact)

Example 35 with Jid

use of de.pixart.messenger.xmpp.jid.Jid in project Pix-Art-Messenger by kriztan.

the class MessageParser method getTrueCounterpart.

private static Jid getTrueCounterpart(Element mucUserElement, Jid fallback) {
    final Element item = mucUserElement == null ? null : mucUserElement.findChild("item");
    Jid result = item == null ? null : item.getAttributeAsJid("jid");
    return result != null ? result : fallback;
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) Element(de.pixart.messenger.xml.Element)

Aggregations

Jid (de.pixart.messenger.xmpp.jid.Jid)75 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)26 Account (de.pixart.messenger.entities.Account)24 Element (de.pixart.messenger.xml.Element)19 Conversation (de.pixart.messenger.entities.Conversation)18 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)15 Contact (de.pixart.messenger.entities.Contact)14 MucOptions (de.pixart.messenger.entities.MucOptions)11 ArrayList (java.util.ArrayList)11 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)8 Message (de.pixart.messenger.entities.Message)7 Intent (android.content.Intent)6 AxolotlService (de.pixart.messenger.crypto.axolotl.AxolotlService)6 Bookmark (de.pixart.messenger.entities.Bookmark)6 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)4 Map (java.util.Map)4 SuppressLint (android.annotation.SuppressLint)3 AlertDialog (android.support.v7.app.AlertDialog)3 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)3 HashMap (java.util.HashMap)3