Search in sources :

Example 26 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnection method processStream.

private void processStream() throws XmlPullParserException, IOException, NoSuchAlgorithmException {
    Tag nextTag = tagReader.readTag();
    while (nextTag != null && !nextTag.isEnd("stream")) {
        if (nextTag.isStart("error")) {
            processStreamError(nextTag);
        } else if (nextTag.isStart("features")) {
            processStreamFeatures(nextTag);
        } else if (nextTag.isStart("proceed")) {
            switchOverToTls(nextTag);
        } else if (nextTag.isStart("success")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            try {
                saslMechanism.getResponse(challenge);
            } catch (final SaslMechanism.AuthenticationException e) {
                disconnect(true);
                Log.e(Config.LOGTAG, String.valueOf(e));
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
            account.setKey(Account.PINNED_MECHANISM_KEY, String.valueOf(saslMechanism.getPriority()));
            tagReader.reset();
            sendStartStream();
            final Tag tag = tagReader.readTag();
            if (tag != null && tag.isStart("stream")) {
                processStream();
            } else {
                throw new IOException("server didn't restart stream after successful auth");
            }
            break;
        } else if (nextTag.isStart("failure")) {
            final Element failure = tagReader.readElement(nextTag);
            final String text = failure.findChildContent("text");
            if (failure.hasChild("account-disabled") && text != null && text.contains("renew") && Config.MAGIC_CREATE_DOMAIN != null && text.contains(Config.MAGIC_CREATE_DOMAIN)) {
                throw new PaymentRequiredException();
            } else {
                throw new UnauthorizedException();
            }
        } else if (nextTag.isStart("challenge")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            final Element response = new Element("response");
            response.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
            try {
                response.setContent(saslMechanism.getResponse(challenge));
            } catch (final SaslMechanism.AuthenticationException e) {
                // TODO: Send auth abort tag.
                Log.e(Config.LOGTAG, e.toString());
            }
            tagWriter.writeElement(response);
        } else if (nextTag.isStart("enabled")) {
            final Element enabled = tagReader.readElement(nextTag);
            if ("true".equals(enabled.getAttribute("resume"))) {
                this.streamId = enabled.getAttribute("id");
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled (resumable)");
            } else {
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled");
            }
            this.stanzasReceived = 0;
            final RequestPacket r = new RequestPacket(smVersion);
            tagWriter.writeStanzaAsync(r);
        } else if (nextTag.isStart("resumed")) {
            lastPacketReceived = SystemClock.elapsedRealtime();
            final Element resumed = tagReader.readElement(nextTag);
            final String h = resumed.getAttribute("h");
            try {
                ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
                synchronized (this.mStanzaQueue) {
                    final int serverCount = Integer.parseInt(h);
                    if (serverCount != stanzasSent) {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed with lost packages");
                        stanzasSent = serverCount;
                    } else {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed");
                    }
                    acknowledgeStanzaUpTo(serverCount);
                    for (int i = 0; i < this.mStanzaQueue.size(); ++i) {
                        failedStanzas.add(mStanzaQueue.valueAt(i));
                    }
                    mStanzaQueue.clear();
                }
                Log.d(Config.LOGTAG, "resending " + failedStanzas.size() + " stanzas");
                for (AbstractAcknowledgeableStanza packet : failedStanzas) {
                    if (packet instanceof MessagePacket) {
                        MessagePacket message = (MessagePacket) packet;
                        mXmppConnectionService.markMessage(account, message.getTo().toBareJid(), message.getId(), Message.STATUS_UNSEND);
                    }
                    sendPacket(packet);
                }
            } catch (final NumberFormatException ignored) {
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource());
            changeStatus(Account.State.ONLINE);
        } else if (nextTag.isStart("r")) {
            tagReader.readElement(nextTag);
            if (Config.EXTENDED_SM_LOGGING) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": acknowledging stanza #" + this.stanzasReceived);
            }
            final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
            tagWriter.writeStanzaAsync(ack);
        } else if (nextTag.isStart("a")) {
            final Element ack = tagReader.readElement(nextTag);
            lastPacketReceived = SystemClock.elapsedRealtime();
            try {
                synchronized (this.mStanzaQueue) {
                    final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
                    acknowledgeStanzaUpTo(serverSequence);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server send ack without sequence number");
            }
        } else if (nextTag.isStart("failed")) {
            Element failed = tagReader.readElement(nextTag);
            try {
                final int serverCount = Integer.parseInt(failed.getAttribute("h"));
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed but server acknowledged stanza #" + serverCount);
                synchronized (this.mStanzaQueue) {
                    acknowledgeStanzaUpTo(serverCount);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed");
            }
            resetStreamId();
            if (account.getStatus() != Account.State.ONLINE) {
                sendBindRequest();
            }
        } else if (nextTag.isStart("iq")) {
            processIq(nextTag);
        } else if (nextTag.isStart("message")) {
            processMessage(nextTag);
        } else if (nextTag.isStart("presence")) {
            processPresence(nextTag);
        }
        nextTag = tagReader.readTag();
    }
}
Also used : RequestPacket(eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket) Element(eu.siacs.conversations.xml.Element) ArrayList(java.util.ArrayList) AckPacket(eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket) IOException(java.io.IOException) MessagePacket(eu.siacs.conversations.xmpp.stanzas.MessagePacket) AbstractAcknowledgeableStanza(eu.siacs.conversations.xmpp.stanzas.AbstractAcknowledgeableStanza) Tag(eu.siacs.conversations.xml.Tag)

Example 27 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnection method sendRegistryRequest.

private void sendRegistryRequest() {
    final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
    register.query("jabber:iq:register");
    register.setTo(account.getServer());
    sendUnmodifiedIqPacket(register, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(final Account account, final IqPacket packet) {
            boolean failed = false;
            if (packet.getType() == IqPacket.TYPE.RESULT && packet.query().hasChild("username") && (packet.query().hasChild("password"))) {
                final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
                final Element username = new Element("username").setContent(account.getUsername());
                final Element password = new Element("password").setContent(account.getPassword());
                register.query("jabber:iq:register").addChild(username);
                register.query().addChild(password);
                register.setFrom(account.getJid().toBareJid());
                sendUnmodifiedIqPacket(register, registrationResponseListener);
            } else if (packet.getType() == IqPacket.TYPE.RESULT && (packet.query().hasChild("x", "jabber:x:data"))) {
                final Data data = Data.parse(packet.query().findChild("x", "jabber:x:data"));
                final Element blob = packet.query().findChild("data", "urn:xmpp:bob");
                final String id = packet.getId();
                Bitmap captcha = null;
                if (blob != null) {
                    try {
                        final String base64Blob = blob.getContent();
                        final byte[] strBlob = Base64.decode(base64Blob, Base64.DEFAULT);
                        InputStream stream = new ByteArrayInputStream(strBlob);
                        captcha = BitmapFactory.decodeStream(stream);
                    } catch (Exception e) {
                    //ignored
                    }
                } else {
                    try {
                        Field url = data.getFieldByName("url");
                        String urlString = url.findChildContent("value");
                        URL uri = new URL(urlString);
                        captcha = BitmapFactory.decodeStream(uri.openConnection().getInputStream());
                    } catch (IOException e) {
                        Log.e(Config.LOGTAG, e.toString());
                    }
                }
                if (captcha != null) {
                    failed = !mXmppConnectionService.displayCaptchaRequest(account, id, data, captcha);
                }
            } else {
                failed = true;
            }
            if (failed) {
                final Element instructions = packet.query().findChild("instructions");
                setAccountCreationFailed((instructions != null) ? instructions.getContent() : "");
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(eu.siacs.conversations.xml.Element) Data(eu.siacs.conversations.xmpp.forms.Data) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) URL(java.net.URL) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) Field(eu.siacs.conversations.xmpp.forms.Field) Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 28 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnection method sendPostBindInitialization.

private void sendPostBindInitialization() {
    smVersion = 0;
    if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
        smVersion = 3;
    } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) {
        smVersion = 2;
    }
    if (smVersion != 0) {
        synchronized (this.mStanzaQueue) {
            final EnablePacket enable = new EnablePacket(smVersion);
            tagWriter.writeStanzaAsync(enable);
            stanzasSent = 0;
            mStanzaQueue.clear();
        }
    }
    features.carbonsEnabled = false;
    features.blockListRequested = false;
    synchronized (this.disco) {
        this.disco.clear();
    }
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": starting service discovery");
    mPendingServiceDiscoveries.set(0);
    if (smVersion == 0 || Patches.DISCO_EXCEPTIONS.contains(account.getJid().getDomainpart())) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": do not wait for service discovery");
        mWaitForDisco.set(false);
    } else {
        mWaitForDisco.set(true);
    }
    lastDiscoStarted = SystemClock.elapsedRealtime();
    mXmppConnectionService.scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
    Element caps = streamFeatures.findChild("c");
    final String hash = caps == null ? null : caps.getAttribute("hash");
    final String ver = caps == null ? null : caps.getAttribute("ver");
    ServiceDiscoveryResult discoveryResult = null;
    if (hash != null && ver != null) {
        discoveryResult = mXmppConnectionService.getCachedServiceDiscoveryResult(new Pair<>(hash, ver));
    }
    if (discoveryResult == null) {
        sendServiceDiscoveryInfo(account.getServer());
    } else {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server caps came from cache");
        disco.put(account.getServer(), discoveryResult);
    }
    sendServiceDiscoveryInfo(account.getJid().toBareJid());
    sendServiceDiscoveryItems(account.getServer());
    if (!mWaitForDisco.get()) {
        finalizeBind();
    }
    this.lastSessionStarted = SystemClock.elapsedRealtime();
}
Also used : EnablePacket(eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket) ServiceDiscoveryResult(eu.siacs.conversations.entities.ServiceDiscoveryResult) Element(eu.siacs.conversations.xml.Element) Pair(android.util.Pair)

Example 29 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class XmppConnection method processStreamError.

private void processStreamError(final Tag currentTag) throws XmlPullParserException, IOException {
    final Element streamError = tagReader.readElement(currentTag);
    if (streamError == null) {
        return;
    }
    if (streamError.hasChild("conflict")) {
        final String resource = account.getResource().split("\\.")[0];
        account.setResource(resource + "." + nextRandomId());
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": switching resource due to conflict (" + account.getResource() + ")");
        throw new IOException();
    } else if (streamError.hasChild("host-unknown")) {
        throw new StreamErrorHostUnknown();
    } else if (streamError.hasChild("policy-violation")) {
        throw new StreamErrorPolicyViolation();
    } else {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": stream error " + streamError.toString());
        throw new StreamError();
    }
}
Also used : Element(eu.siacs.conversations.xml.Element) IOException(java.io.IOException)

Example 30 with Element

use of eu.siacs.conversations.xml.Element in project Conversations by siacs.

the class AxolotlService method publishBundlesIfNeeded.

public void publishBundlesIfNeeded(final boolean announce, final boolean wipe) {
    if (pepBroken) {
        Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
        return;
    }
    IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
    mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
                //ignore timeout. do nothing
                return;
            }
            if (packet.getType() == IqPacket.TYPE.ERROR) {
                Element error = packet.findChild("error");
                if (error == null || !error.hasChild("item-not-found")) {
                    pepBroken = true;
                    Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "request for device bundles came back with something other than item-not-found" + packet);
                    return;
                }
            }
            PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
            Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
            boolean flush = false;
            if (bundle == null) {
                Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
                bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
                flush = true;
            }
            if (keys == null) {
                Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
            }
            try {
                boolean changed = false;
                // Validate IdentityKey
                IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
                if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
                    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
                    changed = true;
                }
                // Validate signedPreKeyRecord + ID
                SignedPreKeyRecord signedPreKeyRecord;
                int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
                try {
                    signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
                    if (flush || !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey()) || !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
                        Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
                        signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
                        axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
                        changed = true;
                    }
                } catch (InvalidKeyIdException e) {
                    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
                    signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
                    axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
                    changed = true;
                }
                // Validate PreKeys
                Set<PreKeyRecord> preKeyRecords = new HashSet<>();
                if (keys != null) {
                    for (Integer id : keys.keySet()) {
                        try {
                            PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
                            if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
                                preKeyRecords.add(preKeyRecord);
                            }
                        } catch (InvalidKeyIdException ignored) {
                        }
                    }
                }
                int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
                if (newKeys > 0) {
                    List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(axolotlStore.getCurrentPreKeyId() + 1, newKeys);
                    preKeyRecords.addAll(newRecords);
                    for (PreKeyRecord record : newRecords) {
                        axolotlStore.storePreKey(record.getId(), record);
                    }
                    changed = true;
                    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
                }
                if (changed) {
                    if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
                        mXmppConnectionService.publishDisplayName(account);
                        publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
                    } else {
                        publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
                    }
                } else {
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
                    if (wipe) {
                        wipeOtherPepDevices();
                    } else if (announce) {
                        Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
                        publishOwnDeviceIdIfNeeded();
                    }
                }
            } catch (InvalidKeyException e) {
                Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) Set(java.util.Set) HashSet(java.util.HashSet) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Element(eu.siacs.conversations.xml.Element) InvalidKeyException(org.whispersystems.libaxolotl.InvalidKeyException) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) PreKeyBundle(org.whispersystems.libaxolotl.state.PreKeyBundle) SignedPreKeyRecord(org.whispersystems.libaxolotl.state.SignedPreKeyRecord) PreKeyRecord(org.whispersystems.libaxolotl.state.PreKeyRecord) InvalidKeyIdException(org.whispersystems.libaxolotl.InvalidKeyIdException) List(java.util.List) ArrayList(java.util.ArrayList) IdentityKeyPair(org.whispersystems.libaxolotl.IdentityKeyPair) Map(java.util.Map) HashMap(java.util.HashMap) SignedPreKeyRecord(org.whispersystems.libaxolotl.state.SignedPreKeyRecord)

Aggregations

Element (eu.siacs.conversations.xml.Element)93 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)43 Account (eu.siacs.conversations.entities.Account)21 Jid (eu.siacs.conversations.xmpp.jid.Jid)17 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)16 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)8 Contact (eu.siacs.conversations.entities.Contact)7 Conversation (eu.siacs.conversations.entities.Conversation)6 ArrayList (java.util.ArrayList)6 Data (eu.siacs.conversations.xmpp.forms.Data)5 Avatar (eu.siacs.conversations.xmpp.pep.Avatar)5 IOException (java.io.IOException)5 MucOptions (eu.siacs.conversations.entities.MucOptions)4 PresencePacket (eu.siacs.conversations.xmpp.stanzas.PresencePacket)4 Pair (android.util.Pair)3 Bookmark (eu.siacs.conversations.entities.Bookmark)3 Message (eu.siacs.conversations.entities.Message)3 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3