XmppConnection.java

   1package eu.siacs.conversations.xmpp;
   2
   3import static eu.siacs.conversations.utils.Random.SECURE_RANDOM;
   4
   5import android.content.Context;
   6import android.graphics.Bitmap;
   7import android.graphics.BitmapFactory;
   8import android.os.Build;
   9import android.os.SystemClock;
  10import android.security.KeyChain;
  11import android.util.Base64;
  12import android.util.Log;
  13import android.util.Pair;
  14import android.util.SparseArray;
  15
  16import androidx.annotation.NonNull;
  17import androidx.annotation.Nullable;
  18
  19import com.google.common.base.MoreObjects;
  20import com.google.common.base.Optional;
  21import com.google.common.base.Preconditions;
  22import com.google.common.base.Strings;
  23import com.google.common.collect.ImmutableList;
  24
  25import eu.siacs.conversations.Config;
  26import eu.siacs.conversations.R;
  27import eu.siacs.conversations.crypto.XmppDomainVerifier;
  28import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  29import eu.siacs.conversations.crypto.sasl.ChannelBinding;
  30import eu.siacs.conversations.crypto.sasl.ChannelBindingMechanism;
  31import eu.siacs.conversations.crypto.sasl.HashedToken;
  32import eu.siacs.conversations.crypto.sasl.SaslMechanism;
  33import eu.siacs.conversations.entities.Account;
  34import eu.siacs.conversations.entities.Message;
  35import eu.siacs.conversations.entities.ServiceDiscoveryResult;
  36import eu.siacs.conversations.generator.IqGenerator;
  37import eu.siacs.conversations.http.HttpConnectionManager;
  38import eu.siacs.conversations.persistance.FileBackend;
  39import eu.siacs.conversations.services.MemorizingTrustManager;
  40import eu.siacs.conversations.services.MessageArchiveService;
  41import eu.siacs.conversations.services.NotificationService;
  42import eu.siacs.conversations.services.XmppConnectionService;
  43import eu.siacs.conversations.utils.AccountUtils;
  44import eu.siacs.conversations.utils.CryptoHelper;
  45import eu.siacs.conversations.utils.Patterns;
  46import eu.siacs.conversations.utils.PhoneHelper;
  47import eu.siacs.conversations.utils.Resolver;
  48import eu.siacs.conversations.utils.SSLSockets;
  49import eu.siacs.conversations.utils.SocksSocketFactory;
  50import eu.siacs.conversations.utils.XmlHelper;
  51import eu.siacs.conversations.xml.Element;
  52import eu.siacs.conversations.xml.LocalizedContent;
  53import eu.siacs.conversations.xml.Namespace;
  54import eu.siacs.conversations.xml.Tag;
  55import eu.siacs.conversations.xml.TagWriter;
  56import eu.siacs.conversations.xml.XmlReader;
  57import eu.siacs.conversations.xmpp.bind.Bind2;
  58import eu.siacs.conversations.xmpp.forms.Data;
  59import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
  60import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  61import eu.siacs.conversations.xmpp.stanzas.AbstractAcknowledgeableStanza;
  62import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
  63import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  64import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
  65import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
  66import eu.siacs.conversations.xmpp.stanzas.csi.ActivePacket;
  67import eu.siacs.conversations.xmpp.stanzas.csi.InactivePacket;
  68import eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket;
  69import eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket;
  70import eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket;
  71import eu.siacs.conversations.xmpp.stanzas.streammgmt.ResumePacket;
  72
  73import okhttp3.HttpUrl;
  74
  75import org.xmlpull.v1.XmlPullParserException;
  76
  77import java.io.ByteArrayInputStream;
  78import java.io.IOException;
  79import java.io.InputStream;
  80import java.net.ConnectException;
  81import java.net.IDN;
  82import java.net.InetAddress;
  83import java.net.InetSocketAddress;
  84import java.net.Socket;
  85import java.net.UnknownHostException;
  86import java.security.KeyManagementException;
  87import java.security.NoSuchAlgorithmException;
  88import java.security.Principal;
  89import java.security.PrivateKey;
  90import java.security.cert.X509Certificate;
  91import java.util.ArrayList;
  92import java.util.Arrays;
  93import java.util.Collection;
  94import java.util.Collections;
  95import java.util.HashMap;
  96import java.util.HashSet;
  97import java.util.Hashtable;
  98import java.util.Iterator;
  99import java.util.List;
 100import java.util.Map.Entry;
 101import java.util.Set;
 102import java.util.concurrent.CountDownLatch;
 103import java.util.concurrent.TimeUnit;
 104import java.util.concurrent.atomic.AtomicBoolean;
 105import java.util.concurrent.atomic.AtomicInteger;
 106import java.util.regex.Matcher;
 107
 108import javax.net.ssl.KeyManager;
 109import javax.net.ssl.SSLContext;
 110import javax.net.ssl.SSLPeerUnverifiedException;
 111import javax.net.ssl.SSLSocket;
 112import javax.net.ssl.SSLSocketFactory;
 113import javax.net.ssl.X509KeyManager;
 114import javax.net.ssl.X509TrustManager;
 115
 116public class XmppConnection implements Runnable {
 117
 118    private static final int PACKET_IQ = 0;
 119    private static final int PACKET_MESSAGE = 1;
 120    private static final int PACKET_PRESENCE = 2;
 121    public final OnIqPacketReceived registrationResponseListener =
 122            (account, packet) -> {
 123                if (packet.getType() == IqPacket.TYPE.RESULT) {
 124                    account.setOption(Account.OPTION_REGISTER, false);
 125                    Log.d(
 126                            Config.LOGTAG,
 127                            account.getJid().asBareJid()
 128                                    + ": successfully registered new account on server");
 129                    throw new StateChangingError(Account.State.REGISTRATION_SUCCESSFUL);
 130                } else {
 131                    final List<String> PASSWORD_TOO_WEAK_MSGS =
 132                            Arrays.asList(
 133                                    "The password is too weak", "Please use a longer password.");
 134                    Element error = packet.findChild("error");
 135                    Account.State state = Account.State.REGISTRATION_FAILED;
 136                    if (error != null) {
 137                        if (error.hasChild("conflict")) {
 138                            state = Account.State.REGISTRATION_CONFLICT;
 139                        } else if (error.hasChild("resource-constraint")
 140                                && "wait".equals(error.getAttribute("type"))) {
 141                            state = Account.State.REGISTRATION_PLEASE_WAIT;
 142                        } else if (error.hasChild("not-acceptable")
 143                                && PASSWORD_TOO_WEAK_MSGS.contains(
 144                                        error.findChildContent("text"))) {
 145                            state = Account.State.REGISTRATION_PASSWORD_TOO_WEAK;
 146                        }
 147                    }
 148                    throw new StateChangingError(state);
 149                }
 150            };
 151    protected final Account account;
 152    private final Features features = new Features(this);
 153    private final HashMap<Jid, ServiceDiscoveryResult> disco = new HashMap<>();
 154    private final HashMap<String, Jid> commands = new HashMap<>();
 155    private final SparseArray<AbstractAcknowledgeableStanza> mStanzaQueue = new SparseArray<>();
 156    private final Hashtable<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks =
 157            new Hashtable<>();
 158    private final Set<OnAdvancedStreamFeaturesLoaded> advancedStreamFeaturesLoadedListeners =
 159            new HashSet<>();
 160    private final XmppConnectionService mXmppConnectionService;
 161    private Socket socket;
 162    private XmlReader tagReader;
 163    private TagWriter tagWriter = new TagWriter();
 164    private boolean shouldAuthenticate = true;
 165    private boolean inSmacksSession = false;
 166    private boolean quickStartInProgress = false;
 167    private boolean isBound = false;
 168    private Element streamFeatures;
 169    private Element boundStreamFeatures;
 170    private StreamId streamId = null;
 171    private int stanzasReceived = 0;
 172    private int stanzasSent = 0;
 173    private int stanzasSentBeforeAuthentication;
 174    private long lastPacketReceived = 0;
 175    private long lastPingSent = 0;
 176    private long lastConnect = 0;
 177    private long lastSessionStarted = 0;
 178    private long lastDiscoStarted = 0;
 179    private boolean isMamPreferenceAlways = false;
 180    private final AtomicInteger mPendingServiceDiscoveries = new AtomicInteger(0);
 181    private final AtomicBoolean mWaitForDisco = new AtomicBoolean(true);
 182    private final AtomicBoolean mWaitingForSmCatchup = new AtomicBoolean(false);
 183    private final AtomicInteger mSmCatchupMessageCounter = new AtomicInteger(0);
 184    private boolean mInteractive = false;
 185    private int attempt = 0;
 186    private OnPresencePacketReceived presenceListener = null;
 187    private OnJinglePacketReceived jingleListener = null;
 188    private OnIqPacketReceived unregisteredIqListener = null;
 189    private OnMessagePacketReceived messageListener = null;
 190    private OnStatusChanged statusListener = null;
 191    private OnBindListener bindListener = null;
 192    private OnMessageAcknowledged acknowledgedListener = null;
 193    private LoginInfo loginInfo;
 194    private HashedToken.Mechanism hashTokenRequest;
 195    private HttpUrl redirectionUrl = null;
 196    private String verifiedHostname = null;
 197    private Resolver.Result currentResolverResult;
 198    private Resolver.Result seeOtherHostResolverResult;
 199    private volatile Thread mThread;
 200    private CountDownLatch mStreamCountDownLatch;
 201
 202    public XmppConnection(final Account account, final XmppConnectionService service) {
 203        this.account = account;
 204        this.mXmppConnectionService = service;
 205    }
 206
 207    private static void fixResource(Context context, Account account) {
 208        String resource = account.getResource();
 209        int fixedPartLength =
 210                context.getString(R.string.app_name).length() + 1; // include the trailing dot
 211        int randomPartLength = 4; // 3 bytes
 212        if (resource != null && resource.length() > fixedPartLength + randomPartLength) {
 213            if (validBase64(
 214                    resource.substring(fixedPartLength, fixedPartLength + randomPartLength))) {
 215                account.setResource(resource.substring(0, fixedPartLength + randomPartLength));
 216            }
 217        }
 218    }
 219
 220    private static boolean validBase64(String input) {
 221        try {
 222            return Base64.decode(input, Base64.URL_SAFE).length == 3;
 223        } catch (Throwable throwable) {
 224            return false;
 225        }
 226    }
 227
 228    private void changeStatus(final Account.State nextStatus) {
 229        synchronized (this) {
 230            if (Thread.currentThread().isInterrupted()) {
 231                Log.d(
 232                        Config.LOGTAG,
 233                        account.getJid().asBareJid()
 234                                + ": not changing status to "
 235                                + nextStatus
 236                                + " because thread was interrupted");
 237                return;
 238            }
 239            if (account.getStatus() != nextStatus) {
 240                if (nextStatus == Account.State.OFFLINE
 241                        && account.getStatus() != Account.State.CONNECTING
 242                        && account.getStatus() != Account.State.ONLINE
 243                        && account.getStatus() != Account.State.DISABLED
 244                        && account.getStatus() != Account.State.LOGGED_OUT) {
 245                    return;
 246                }
 247                if (nextStatus == Account.State.ONLINE) {
 248                    this.attempt = 0;
 249                }
 250                account.setStatus(nextStatus);
 251            } else {
 252                return;
 253            }
 254        }
 255        if (statusListener != null) {
 256            statusListener.onStatusChanged(account);
 257        }
 258    }
 259
 260    public Jid getJidForCommand(final String node) {
 261        synchronized (this.commands) {
 262            return this.commands.get(node);
 263        }
 264    }
 265
 266    public void prepareNewConnection() {
 267        this.lastConnect = SystemClock.elapsedRealtime();
 268        this.lastPingSent = SystemClock.elapsedRealtime();
 269        this.lastDiscoStarted = Long.MAX_VALUE;
 270        this.mWaitingForSmCatchup.set(false);
 271        this.changeStatus(Account.State.CONNECTING);
 272    }
 273
 274    public boolean isWaitingForSmCatchup() {
 275        return mWaitingForSmCatchup.get();
 276    }
 277
 278    public void incrementSmCatchupMessageCounter() {
 279        this.mSmCatchupMessageCounter.incrementAndGet();
 280    }
 281
 282    protected void connect() {
 283        if (mXmppConnectionService.areMessagesInitialized()) {
 284            mXmppConnectionService.resetSendingToWaiting(account);
 285        }
 286        Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": connecting");
 287        this.loginInfo = null;
 288        this.features.encryptionEnabled = false;
 289        this.inSmacksSession = false;
 290        this.quickStartInProgress = false;
 291        this.isBound = false;
 292        this.attempt++;
 293        this.verifiedHostname =
 294                null; // will be set if user entered hostname is being used or hostname was verified
 295        // with dnssec
 296        try {
 297            Socket localSocket;
 298            shouldAuthenticate = !account.isOptionSet(Account.OPTION_REGISTER);
 299            this.changeStatus(Account.State.CONNECTING);
 300            final boolean useTor = mXmppConnectionService.useTorToConnect() || account.isOnion();
 301            final boolean extended = mXmppConnectionService.showExtendedConnectionOptions();
 302            if (useTor) {
 303                String destination;
 304                if (account.getHostname().isEmpty() || account.isOnion()) {
 305                    destination = account.getServer();
 306                } else {
 307                    destination = account.getHostname();
 308                    this.verifiedHostname = destination;
 309                }
 310
 311                final int port = account.getPort();
 312                final boolean directTls = Resolver.useDirectTls(port);
 313
 314                Log.d(
 315                        Config.LOGTAG,
 316                        account.getJid().asBareJid()
 317                                + ": connect to "
 318                                + destination
 319                                + " via Tor. directTls="
 320                                + directTls);
 321                localSocket = SocksSocketFactory.createSocketOverTor(destination, port);
 322
 323                if (directTls) {
 324                    localSocket = upgradeSocketToTls(localSocket);
 325                    features.encryptionEnabled = true;
 326                }
 327
 328                try {
 329                    startXmpp(localSocket);
 330                } catch (final InterruptedException e) {
 331                    Log.d(
 332                            Config.LOGTAG,
 333                            account.getJid().asBareJid()
 334                                    + ": thread was interrupted before beginning stream");
 335                    return;
 336                } catch (final Exception e) {
 337                    throw new IOException("Could not start stream", e);
 338                }
 339            } else {
 340                final String domain = account.getServer();
 341                final List<Resolver.Result> results;
 342                final boolean hardcoded = extended && !account.getHostname().isEmpty();
 343                if (hardcoded) {
 344                    results = Resolver.fromHardCoded(account.getHostname(), account.getPort());
 345                } else {
 346                    results = Resolver.resolve(domain);
 347                }
 348                if (Thread.currentThread().isInterrupted()) {
 349                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Thread was interrupted");
 350                    return;
 351                }
 352                if (results.size() == 0) {
 353                    Log.e(
 354                            Config.LOGTAG,
 355                            account.getJid().asBareJid() + ": Resolver results were empty");
 356                    return;
 357                }
 358                final Resolver.Result storedBackupResult;
 359                if (hardcoded) {
 360                    storedBackupResult = null;
 361                } else {
 362                    storedBackupResult =
 363                            mXmppConnectionService.databaseBackend.findResolverResult(domain);
 364                    if (storedBackupResult != null && !results.contains(storedBackupResult)) {
 365                        results.add(storedBackupResult);
 366                        Log.d(
 367                                Config.LOGTAG,
 368                                account.getJid().asBareJid()
 369                                        + ": loaded backup resolver result from db: "
 370                                        + storedBackupResult);
 371                    }
 372                }
 373                final StreamId streamId = this.streamId;
 374                final Resolver.Result resumeLocation = streamId == null ? null : streamId.location;
 375                if (resumeLocation != null) {
 376                    Log.d(
 377                            Config.LOGTAG,
 378                            account.getJid().asBareJid()
 379                                    + ": injected resume location on position 0");
 380                    results.add(0, resumeLocation);
 381                }
 382                final Resolver.Result seeOtherHost = this.seeOtherHostResolverResult;
 383                if (seeOtherHost != null) {
 384                    Log.d(
 385                            Config.LOGTAG,
 386                            account.getJid().asBareJid()
 387                                    + ": injected see-other-host on position 0");
 388                    results.add(0, seeOtherHost);
 389                }
 390                for (final Iterator<Resolver.Result> iterator = results.iterator();
 391                        iterator.hasNext(); ) {
 392                    final Resolver.Result result = iterator.next();
 393                    if (Thread.currentThread().isInterrupted()) {
 394                        Log.d(
 395                                Config.LOGTAG,
 396                                account.getJid().asBareJid() + ": Thread was interrupted");
 397                        return;
 398                    }
 399                    try {
 400                        // if tls is true, encryption is implied and must not be started
 401                        features.encryptionEnabled = result.isDirectTls();
 402                        verifiedHostname =
 403                                result.isAuthenticated() ? result.getHostname().toString() : null;
 404                        final InetSocketAddress addr;
 405                        if (result.getIp() != null) {
 406                            addr = new InetSocketAddress(result.getIp(), result.getPort());
 407                            Log.d(
 408                                    Config.LOGTAG,
 409                                    account.getJid().asBareJid().toString()
 410                                            + ": using values from resolver "
 411                                            + (result.getHostname() == null
 412                                                    ? ""
 413                                                    : result.getHostname().toString() + "/")
 414                                            + result.getIp().getHostAddress()
 415                                            + ":"
 416                                            + result.getPort()
 417                                            + " tls: "
 418                                            + features.encryptionEnabled);
 419                        } else {
 420                            addr =
 421                                    new InetSocketAddress(
 422                                            IDN.toASCII(result.getHostname().toString()),
 423                                            result.getPort());
 424                            Log.d(
 425                                    Config.LOGTAG,
 426                                    account.getJid().asBareJid().toString()
 427                                            + ": using values from resolver "
 428                                            + result.getHostname().toString()
 429                                            + ":"
 430                                            + result.getPort()
 431                                            + " tls: "
 432                                            + features.encryptionEnabled);
 433                        }
 434
 435                        localSocket = new Socket();
 436                        localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
 437
 438                        if (features.encryptionEnabled) {
 439                            localSocket = upgradeSocketToTls(localSocket);
 440                        }
 441
 442                        localSocket.setSoTimeout(Config.SOCKET_TIMEOUT * 1000);
 443                        if (startXmpp(localSocket)) {
 444                            localSocket.setSoTimeout(
 445                                    0); // reset to 0; once the connection is established we don’t
 446                            // want this
 447                            if (!hardcoded && !result.equals(storedBackupResult)) {
 448                                mXmppConnectionService.databaseBackend.saveResolverResult(
 449                                        domain, result);
 450                            }
 451                            this.currentResolverResult = result;
 452                            this.seeOtherHostResolverResult = null;
 453                            break; // successfully connected to server that speaks xmpp
 454                        } else {
 455                            FileBackend.close(localSocket);
 456                            throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
 457                        }
 458                    } catch (final StateChangingException e) {
 459                        if (!iterator.hasNext()) {
 460                            throw e;
 461                        }
 462                    } catch (InterruptedException e) {
 463                        Log.d(
 464                                Config.LOGTAG,
 465                                account.getJid().asBareJid()
 466                                        + ": thread was interrupted before beginning stream");
 467                        return;
 468                    } catch (final Throwable e) {
 469                        Log.d(
 470                                Config.LOGTAG,
 471                                account.getJid().asBareJid().toString()
 472                                        + ": "
 473                                        + e.getMessage()
 474                                        + "("
 475                                        + e.getClass().getName()
 476                                        + ")");
 477                        if (!iterator.hasNext()) {
 478                            throw new UnknownHostException();
 479                        }
 480                    }
 481                }
 482            }
 483            processStream();
 484        } catch (final SecurityException e) {
 485            this.changeStatus(Account.State.MISSING_INTERNET_PERMISSION);
 486        } catch (final StateChangingException e) {
 487            this.changeStatus(e.state);
 488        } catch (final UnknownHostException
 489                | ConnectException
 490                | SocksSocketFactory.HostNotFoundException e) {
 491            this.changeStatus(Account.State.SERVER_NOT_FOUND);
 492        } catch (final SocksSocketFactory.SocksProxyNotFoundException e) {
 493            this.changeStatus(Account.State.TOR_NOT_AVAILABLE);
 494        } catch (final IOException | XmlPullParserException e) {
 495            Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": " + e.getMessage());
 496            this.changeStatus(Account.State.OFFLINE);
 497            this.attempt = Math.max(0, this.attempt - 1);
 498        } finally {
 499            if (!Thread.currentThread().isInterrupted()) {
 500                forceCloseSocket();
 501            } else {
 502                Log.d(
 503                        Config.LOGTAG,
 504                        account.getJid().asBareJid()
 505                                + ": not force closing socket because thread was interrupted");
 506            }
 507        }
 508    }
 509
 510    /**
 511     * Starts xmpp protocol, call after connecting to socket
 512     *
 513     * @return true if server returns with valid xmpp, false otherwise
 514     */
 515    private boolean startXmpp(final Socket socket) throws Exception {
 516        if (Thread.currentThread().isInterrupted()) {
 517            throw new InterruptedException();
 518        }
 519        this.socket = socket;
 520        tagReader = new XmlReader();
 521        if (tagWriter != null) {
 522            tagWriter.forceClose();
 523        }
 524        tagWriter = new TagWriter();
 525        tagWriter.setOutputStream(socket.getOutputStream());
 526        tagReader.setInputStream(socket.getInputStream());
 527        tagWriter.beginDocument();
 528        final boolean quickStart;
 529        if (socket instanceof SSLSocket) {
 530            final SSLSocket sslSocket = (SSLSocket) socket;
 531            SSLSockets.log(account, sslSocket);
 532            quickStart = establishStream(SSLSockets.version(sslSocket));
 533        } else {
 534            quickStart = establishStream(SSLSockets.Version.NONE);
 535        }
 536        final Tag tag = tagReader.readTag();
 537        if (Thread.currentThread().isInterrupted()) {
 538            throw new InterruptedException();
 539        }
 540        final boolean success = tag != null && tag.isStart("stream", Namespace.STREAMS);
 541        if (success && quickStart) {
 542            this.quickStartInProgress = true;
 543        }
 544        return success;
 545    }
 546
 547    private SSLSocketFactory getSSLSocketFactory()
 548            throws NoSuchAlgorithmException, KeyManagementException {
 549        final SSLContext sc = SSLSockets.getSSLContext();
 550        final MemorizingTrustManager trustManager =
 551                this.mXmppConnectionService.getMemorizingTrustManager();
 552        final KeyManager[] keyManager;
 553        if (account.getPrivateKeyAlias() != null) {
 554            keyManager = new KeyManager[] {new MyKeyManager()};
 555        } else {
 556            keyManager = null;
 557        }
 558        final String domain = account.getServer();
 559        sc.init(
 560                keyManager,
 561                new X509TrustManager[] {
 562                    mInteractive
 563                            ? trustManager.getInteractive(domain)
 564                            : trustManager.getNonInteractive(domain)
 565                },
 566                SECURE_RANDOM);
 567        return sc.getSocketFactory();
 568    }
 569
 570    @Override
 571    public void run() {
 572        synchronized (this) {
 573            this.mThread = Thread.currentThread();
 574            if (this.mThread.isInterrupted()) {
 575                Log.d(
 576                        Config.LOGTAG,
 577                        account.getJid().asBareJid()
 578                                + ": aborting connect because thread was interrupted");
 579                return;
 580            }
 581            forceCloseSocket();
 582        }
 583        connect();
 584    }
 585
 586    private void processStream() throws XmlPullParserException, IOException {
 587        final CountDownLatch streamCountDownLatch = new CountDownLatch(1);
 588        this.mStreamCountDownLatch = streamCountDownLatch;
 589        Tag nextTag = tagReader.readTag();
 590        while (nextTag != null && !nextTag.isEnd("stream")) {
 591            if (nextTag.isStart("error")) {
 592                processStreamError(nextTag);
 593            } else if (nextTag.isStart("features", Namespace.STREAMS)) {
 594                processStreamFeatures(nextTag);
 595            } else if (nextTag.isStart("proceed", Namespace.TLS)) {
 596                switchOverToTls();
 597            } else if (nextTag.isStart("failure", Namespace.TLS)) {
 598                throw new StateChangingException(Account.State.TLS_ERROR);
 599            } else if (account.isOptionSet(Account.OPTION_REGISTER)
 600                    && nextTag.isStart("iq", Namespace.JABBER_CLIENT)) {
 601                processIq(nextTag);
 602            } else if (!isSecure() || this.loginInfo == null) {
 603                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 604            } else if (nextTag.isStart("success")) {
 605                final Element success = tagReader.readElement(nextTag);
 606                if (processSuccess(success)) {
 607                    break;
 608                }
 609            } else if (nextTag.isStart("failure")) {
 610                final Element failure = tagReader.readElement(nextTag);
 611                processFailure(failure);
 612            } else if (nextTag.isStart("continue", Namespace.SASL_2)) {
 613                // two step sasl2 - we don’t support this yet
 614                throw new StateChangingException(Account.State.INCOMPATIBLE_CLIENT);
 615            } else if (nextTag.isStart("challenge")) {
 616                final Element challenge = tagReader.readElement(nextTag);
 617                processChallenge(challenge);
 618            } else if (this.streamId != null
 619                    && nextTag.isStart("resumed", Namespace.STREAM_MANAGEMENT)) {
 620                final Element resumed = tagReader.readElement(nextTag);
 621                processResumed(resumed);
 622            } else if (nextTag.isStart("failed", Namespace.STREAM_MANAGEMENT)) {
 623                final Element failed = tagReader.readElement(nextTag);
 624                processFailed(failed, true);
 625            } else if (nextTag.isStart("iq", Namespace.JABBER_CLIENT)) {
 626                processIq(nextTag);
 627            } else if (!isBound) {
 628                Log.d(
 629                        Config.LOGTAG,
 630                        account.getJid().asBareJid()
 631                                + ": server sent unexpected"
 632                                + nextTag.identifier());
 633                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 634            } else if (nextTag.isStart("message", Namespace.JABBER_CLIENT)) {
 635                processMessage(nextTag);
 636            } else if (nextTag.isStart("presence", Namespace.JABBER_CLIENT)) {
 637                processPresence(nextTag);
 638            } else if (nextTag.isStart("enabled", Namespace.STREAM_MANAGEMENT)) {
 639                final Element enabled = tagReader.readElement(nextTag);
 640                processEnabled(enabled);
 641            } else if (nextTag.isStart("r", Namespace.STREAM_MANAGEMENT)) {
 642                tagReader.readElement(nextTag);
 643                if (Config.EXTENDED_SM_LOGGING) {
 644                    Log.d(
 645                            Config.LOGTAG,
 646                            account.getJid().asBareJid()
 647                                    + ": acknowledging stanza #"
 648                                    + this.stanzasReceived);
 649                }
 650                final AckPacket ack = new AckPacket(this.stanzasReceived);
 651                tagWriter.writeStanzaAsync(ack);
 652            } else if (nextTag.isStart("a", Namespace.STREAM_MANAGEMENT)) {
 653                boolean accountUiNeedsRefresh = false;
 654                synchronized (NotificationService.CATCHUP_LOCK) {
 655                    if (mWaitingForSmCatchup.compareAndSet(true, false)) {
 656                        final int messageCount = mSmCatchupMessageCounter.get();
 657                        final int pendingIQs = packetCallbacks.size();
 658                        Log.d(
 659                                Config.LOGTAG,
 660                                account.getJid().asBareJid()
 661                                        + ": SM catchup complete (messages="
 662                                        + messageCount
 663                                        + ", pending IQs="
 664                                        + pendingIQs
 665                                        + ")");
 666                        accountUiNeedsRefresh = true;
 667                        if (messageCount > 0) {
 668                            mXmppConnectionService
 669                                    .getNotificationService()
 670                                    .finishBacklog(true, account);
 671                        }
 672                    }
 673                }
 674                if (accountUiNeedsRefresh) {
 675                    mXmppConnectionService.updateAccountUi();
 676                }
 677                final Element ack = tagReader.readElement(nextTag);
 678                lastPacketReceived = SystemClock.elapsedRealtime();
 679                final boolean acknowledgedMessages;
 680                synchronized (this.mStanzaQueue) {
 681                    final Optional<Integer> serverSequence = ack.getOptionalIntAttribute("h");
 682                    if (serverSequence.isPresent()) {
 683                        acknowledgedMessages = acknowledgeStanzaUpTo(serverSequence.get());
 684                    } else {
 685                        acknowledgedMessages = false;
 686                        Log.d(
 687                                Config.LOGTAG,
 688                                account.getJid().asBareJid()
 689                                        + ": server send ack without sequence number");
 690                    }
 691                }
 692                if (acknowledgedMessages) {
 693                    mXmppConnectionService.updateConversationUi();
 694                }
 695            } else {
 696                Log.e(
 697                        Config.LOGTAG,
 698                        account.getJid().asBareJid()
 699                                + ": Encountered unknown stream element"
 700                                + nextTag.identifier());
 701                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 702            }
 703            nextTag = tagReader.readTag();
 704        }
 705        if (nextTag != null && nextTag.isEnd("stream")) {
 706            streamCountDownLatch.countDown();
 707        }
 708    }
 709
 710    private void processChallenge(final Element challenge) throws IOException {
 711        final SaslMechanism.Version version;
 712        try {
 713            version = SaslMechanism.Version.of(challenge);
 714        } catch (final IllegalArgumentException e) {
 715            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 716        }
 717        final Element response;
 718        if (version == SaslMechanism.Version.SASL) {
 719            response = new Element("response", Namespace.SASL);
 720        } else if (version == SaslMechanism.Version.SASL_2) {
 721            response = new Element("response", Namespace.SASL_2);
 722        } else {
 723            throw new AssertionError("Missing implementation for " + version);
 724        }
 725        try {
 726            response.setContent(
 727                    this.loginInfo.saslMechanism.getResponse(
 728                            challenge.getContent(), sslSocketOrNull(socket)));
 729        } catch (final SaslMechanism.AuthenticationException e) {
 730            // TODO: Send auth abort tag.
 731            Log.e(Config.LOGTAG, e.toString());
 732            throw new StateChangingException(Account.State.UNAUTHORIZED);
 733        }
 734        tagWriter.writeElement(response);
 735    }
 736
 737    private boolean processSuccess(final Element success)
 738            throws IOException, XmlPullParserException {
 739        final SaslMechanism.Version version;
 740        try {
 741            version = SaslMechanism.Version.of(success);
 742        } catch (final IllegalArgumentException e) {
 743            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 744        }
 745        final LoginInfo currentLoginInfo = this.loginInfo;
 746        final SaslMechanism currentSaslMechanism = LoginInfo.mechanism(currentLoginInfo);
 747        if (currentLoginInfo == null || currentSaslMechanism == null) {
 748            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 749        }
 750        final String challenge;
 751        if (version == SaslMechanism.Version.SASL) {
 752            challenge = success.getContent();
 753        } else if (version == SaslMechanism.Version.SASL_2) {
 754            challenge = success.findChildContent("additional-data");
 755        } else {
 756            throw new AssertionError("Missing implementation for " + version);
 757        }
 758        try {
 759            currentSaslMechanism.getResponse(challenge, sslSocketOrNull(socket));
 760        } catch (final SaslMechanism.AuthenticationException e) {
 761            Log.e(Config.LOGTAG, String.valueOf(e));
 762            throw new StateChangingException(Account.State.UNAUTHORIZED);
 763        }
 764        Log.d(
 765                Config.LOGTAG,
 766                account.getJid().asBareJid().toString() + ": logged in (using " + version + ")");
 767        if (SaslMechanism.pin(currentSaslMechanism)) {
 768            account.setPinnedMechanism(currentSaslMechanism);
 769        }
 770        if (version == SaslMechanism.Version.SASL_2) {
 771            final String authorizationIdentifier =
 772                    success.findChildContent("authorization-identifier");
 773            final Jid authorizationJid;
 774            try {
 775                authorizationJid =
 776                        Strings.isNullOrEmpty(authorizationIdentifier)
 777                                ? null
 778                                : Jid.ofEscaped(authorizationIdentifier);
 779            } catch (final IllegalArgumentException e) {
 780                Log.d(
 781                        Config.LOGTAG,
 782                        account.getJid().asBareJid()
 783                                + ": SASL 2.0 authorization identifier was not a valid jid");
 784                throw new StateChangingException(Account.State.BIND_FAILURE);
 785            }
 786            if (authorizationJid == null) {
 787                throw new StateChangingException(Account.State.BIND_FAILURE);
 788            }
 789            Log.d(
 790                    Config.LOGTAG,
 791                    account.getJid().asBareJid()
 792                            + ": SASL 2.0 authorization identifier was "
 793                            + authorizationJid);
 794            if (!account.getJid().getDomain().equals(authorizationJid.getDomain())) {
 795                Log.d(
 796                        Config.LOGTAG,
 797                        account.getJid().asBareJid()
 798                                + ": server tried to re-assign domain to "
 799                                + authorizationJid.getDomain());
 800                throw new StateChangingError(Account.State.BIND_FAILURE);
 801            }
 802            if (authorizationJid.isFullJid() && account.setJid(authorizationJid)) {
 803                Log.d(
 804                        Config.LOGTAG,
 805                        account.getJid().asBareJid()
 806                                + ": jid changed during SASL 2.0. updating database");
 807            }
 808            final Element bound = success.findChild("bound", Namespace.BIND2);
 809            final Element resumed = success.findChild("resumed", Namespace.STREAM_MANAGEMENT);
 810            final Element failed = success.findChild("failed", Namespace.STREAM_MANAGEMENT);
 811            final Element tokenWrapper = success.findChild("token", Namespace.FAST);
 812            final String token = tokenWrapper == null ? null : tokenWrapper.getAttribute("token");
 813            if (bound != null && resumed != null) {
 814                Log.d(
 815                        Config.LOGTAG,
 816                        account.getJid().asBareJid()
 817                                + ": server sent bound and resumed in SASL2 success");
 818                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 819            }
 820            if (resumed != null && streamId != null) {
 821                if (this.boundStreamFeatures != null) {
 822                    this.streamFeatures = this.boundStreamFeatures;
 823                    Log.d(
 824                            Config.LOGTAG,
 825                            "putting previous stream features back in place: "
 826                                    + XmlHelper.printElementNames(this.boundStreamFeatures));
 827                }
 828                processResumed(resumed);
 829            } else if (failed != null) {
 830                processFailed(failed, false); // wait for new stream features
 831            }
 832            if (bound != null) {
 833                clearIqCallbacks();
 834                this.isBound = true;
 835                processNopStreamFeatures();
 836                this.boundStreamFeatures = this.streamFeatures;
 837                final Element streamManagementEnabled =
 838                        bound.findChild("enabled", Namespace.STREAM_MANAGEMENT);
 839                final Element carbonsEnabled = bound.findChild("enabled", Namespace.CARBONS);
 840                final boolean waitForDisco;
 841                if (streamManagementEnabled != null) {
 842                    resetOutboundStanzaQueue();
 843                    processEnabled(streamManagementEnabled);
 844                    waitForDisco = true;
 845                } else {
 846                    // if we did not enable stream management in bind do it now
 847                    waitForDisco = enableStreamManagement();
 848                }
 849                final boolean negotiatedCarbons;
 850                if (carbonsEnabled != null) {
 851                    negotiatedCarbons = true;
 852                    Log.d(
 853                            Config.LOGTAG,
 854                            account.getJid().asBareJid()
 855                                    + ": successfully enabled carbons (via Bind 2.0)");
 856                    features.carbonsEnabled = true;
 857                } else if (loginInfo.inlineBindFeatures.contains(Namespace.CARBONS)) {
 858                    negotiatedCarbons = true;
 859                    Log.d(
 860                            Config.LOGTAG,
 861                            account.getJid().asBareJid()
 862                                    + ": successfully enabled carbons (via Bind 2.0/implicit)");
 863                    features.carbonsEnabled = true;
 864                } else {
 865                    negotiatedCarbons = false;
 866                }
 867                sendPostBindInitialization(waitForDisco, negotiatedCarbons);
 868            }
 869            final HashedToken.Mechanism tokenMechanism;
 870            if (SaslMechanism.hashedToken(currentSaslMechanism)) {
 871                tokenMechanism = ((HashedToken) currentSaslMechanism).getTokenMechanism();
 872            } else if (this.hashTokenRequest != null) {
 873                tokenMechanism = this.hashTokenRequest;
 874            } else {
 875                tokenMechanism = null;
 876            }
 877            if (tokenMechanism != null && !Strings.isNullOrEmpty(token)) {
 878                if (ChannelBinding.priority(tokenMechanism.channelBinding)
 879                        >= ChannelBindingMechanism.getPriority(currentSaslMechanism)) {
 880                    this.account.setFastToken(tokenMechanism, token);
 881                    Log.d(
 882                            Config.LOGTAG,
 883                            account.getJid().asBareJid()
 884                                    + ": storing hashed token "
 885                                    + tokenMechanism);
 886                } else {
 887                    Log.d(
 888                            Config.LOGTAG,
 889                            account.getJid().asBareJid()
 890                                    + ": not accepting hashed token "
 891                                    + tokenMechanism.name()
 892                                    + " for log in mechanism "
 893                                    + currentSaslMechanism.getMechanism());
 894                    this.account.resetFastToken();
 895                }
 896            } else if (this.hashTokenRequest != null) {
 897                Log.w(
 898                        Config.LOGTAG,
 899                        account.getJid().asBareJid()
 900                                + ": no response to our hashed token request "
 901                                + this.hashTokenRequest);
 902            }
 903        }
 904        mXmppConnectionService.databaseBackend.updateAccount(account);
 905        this.quickStartInProgress = false;
 906        if (version == SaslMechanism.Version.SASL) {
 907            tagReader.reset();
 908            sendStartStream(false, true);
 909            final Tag tag = tagReader.readTag();
 910            if (tag != null && tag.isStart("stream", Namespace.STREAMS)) {
 911                processStream();
 912                return true;
 913            } else {
 914                throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
 915            }
 916        } else {
 917            return false;
 918        }
 919    }
 920
 921    private void resetOutboundStanzaQueue() {
 922        synchronized (this.mStanzaQueue) {
 923            final List<AbstractAcknowledgeableStanza> intermediateStanzas = new ArrayList<>();
 924            if (Config.EXTENDED_SM_LOGGING) {
 925                Log.d(
 926                        Config.LOGTAG,
 927                        account.getJid().asBareJid()
 928                                + ": stanzas sent before auth: "
 929                                + this.stanzasSentBeforeAuthentication);
 930            }
 931            for (int i = this.stanzasSentBeforeAuthentication + 1; i <= this.stanzasSent; ++i) {
 932                final AbstractAcknowledgeableStanza stanza = this.mStanzaQueue.get(i);
 933                if (stanza != null) {
 934                    intermediateStanzas.add(stanza);
 935                }
 936            }
 937            this.mStanzaQueue.clear();
 938            for (int i = 0; i < intermediateStanzas.size(); ++i) {
 939                this.mStanzaQueue.put(i, intermediateStanzas.get(i));
 940            }
 941            this.stanzasSent = intermediateStanzas.size();
 942            if (Config.EXTENDED_SM_LOGGING) {
 943                Log.d(
 944                        Config.LOGTAG,
 945                        account.getJid().asBareJid()
 946                                + ": resetting outbound stanza queue to "
 947                                + this.stanzasSent);
 948            }
 949        }
 950    }
 951
 952    private void processNopStreamFeatures() throws IOException {
 953        final Tag tag = tagReader.readTag();
 954        if (tag != null && tag.isStart("features", Namespace.STREAMS)) {
 955            this.streamFeatures = tagReader.readElement(tag);
 956            Log.d(
 957                    Config.LOGTAG,
 958                    account.getJid().asBareJid()
 959                            + ": processed NOP stream features after success: "
 960                            + XmlHelper.printElementNames(this.streamFeatures));
 961        } else {
 962            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received " + tag);
 963            Log.d(
 964                    Config.LOGTAG,
 965                    account.getJid().asBareJid()
 966                            + ": server did not send stream features after SASL2 success");
 967            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 968        }
 969    }
 970
 971    private void processFailure(final Element failure) throws IOException {
 972        final SaslMechanism.Version version;
 973        try {
 974            version = SaslMechanism.Version.of(failure);
 975        } catch (final IllegalArgumentException e) {
 976            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 977        }
 978        Log.d(Config.LOGTAG, failure.toString());
 979        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": login failure " + version);
 980        if (SaslMechanism.hashedToken(LoginInfo.mechanism(this.loginInfo))) {
 981            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resetting token");
 982            account.resetFastToken();
 983            mXmppConnectionService.databaseBackend.updateAccount(account);
 984        }
 985        if (failure.hasChild("temporary-auth-failure")) {
 986            throw new StateChangingException(Account.State.TEMPORARY_AUTH_FAILURE);
 987        } else if (failure.hasChild("account-disabled")) {
 988            final String text = failure.findChildContent("text");
 989            if (Strings.isNullOrEmpty(text)) {
 990                throw new StateChangingException(Account.State.UNAUTHORIZED);
 991            }
 992            final Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(text);
 993            if (matcher.find()) {
 994                final HttpUrl url;
 995                try {
 996                    url = HttpUrl.get(text.substring(matcher.start(), matcher.end()));
 997                } catch (final IllegalArgumentException e) {
 998                    throw new StateChangingException(Account.State.UNAUTHORIZED);
 999                }
1000                if (url.isHttps()) {
1001                    this.redirectionUrl = url;
1002                    throw new StateChangingException(Account.State.PAYMENT_REQUIRED);
1003                }
1004            }
1005        }
1006        if (SaslMechanism.hashedToken(LoginInfo.mechanism(this.loginInfo))) {
1007            Log.d(
1008                    Config.LOGTAG,
1009                    account.getJid().asBareJid()
1010                            + ": fast authentication failed. falling back to regular authentication");
1011            authenticate();
1012        } else {
1013            throw new StateChangingException(Account.State.UNAUTHORIZED);
1014        }
1015    }
1016
1017    private static SSLSocket sslSocketOrNull(final Socket socket) {
1018        if (socket instanceof SSLSocket) {
1019            return (SSLSocket) socket;
1020        } else {
1021            return null;
1022        }
1023    }
1024
1025    private void processEnabled(final Element enabled) {
1026        final String id;
1027        if (enabled.getAttributeAsBoolean("resume")) {
1028            id = enabled.getAttribute("id");
1029        } else {
1030            id = null;
1031        }
1032        final String locationAttribute = enabled.getAttribute("location");
1033        final Resolver.Result currentResolverResult = this.currentResolverResult;
1034        final Resolver.Result location;
1035        if (Strings.isNullOrEmpty(locationAttribute) || currentResolverResult == null) {
1036            location = null;
1037        } else {
1038            location = currentResolverResult.seeOtherHost(locationAttribute);
1039        }
1040        final StreamId streamId = id == null ? null : new StreamId(id, location);
1041        if (streamId == null) {
1042            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": stream management enabled");
1043        } else {
1044            Log.d(
1045                    Config.LOGTAG,
1046                    account.getJid().asBareJid()
1047                            + ": stream management enabled. resume at: "
1048                            + streamId.location);
1049        }
1050        this.streamId = streamId;
1051        this.stanzasReceived = 0;
1052        this.inSmacksSession = true;
1053        final RequestPacket r = new RequestPacket();
1054        tagWriter.writeStanzaAsync(r);
1055    }
1056
1057    private void processResumed(final Element resumed) throws StateChangingException {
1058        this.inSmacksSession = true;
1059        this.isBound = true;
1060        this.tagWriter.writeStanzaAsync(new RequestPacket());
1061        lastPacketReceived = SystemClock.elapsedRealtime();
1062        final Optional<Integer> h = resumed.getOptionalIntAttribute("h");
1063        final int serverCount;
1064        if (h.isPresent()) {
1065            serverCount = h.get();
1066        } else {
1067            resetStreamId();
1068            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1069        }
1070        final ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
1071        final boolean acknowledgedMessages;
1072        synchronized (this.mStanzaQueue) {
1073            if (serverCount < stanzasSent) {
1074                Log.d(
1075                        Config.LOGTAG,
1076                        account.getJid().asBareJid() + ": session resumed with lost packages");
1077                stanzasSent = serverCount;
1078            } else {
1079                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": session resumed");
1080            }
1081            acknowledgedMessages = acknowledgeStanzaUpTo(serverCount);
1082            for (int i = 0; i < this.mStanzaQueue.size(); ++i) {
1083                failedStanzas.add(mStanzaQueue.valueAt(i));
1084            }
1085            mStanzaQueue.clear();
1086        }
1087        if (acknowledgedMessages) {
1088            mXmppConnectionService.updateConversationUi();
1089        }
1090        Log.d(
1091                Config.LOGTAG,
1092                account.getJid().asBareJid() + ": resending " + failedStanzas.size() + " stanzas");
1093        for (final AbstractAcknowledgeableStanza packet : failedStanzas) {
1094            if (packet instanceof MessagePacket message) {
1095                mXmppConnectionService.markMessage(
1096                        account,
1097                        message.getTo().asBareJid(),
1098                        message.getId(),
1099                        Message.STATUS_UNSEND);
1100            }
1101            sendPacket(packet);
1102        }
1103        changeStatusToOnline();
1104    }
1105
1106    private void changeStatusToOnline() {
1107        Log.d(
1108                Config.LOGTAG,
1109                account.getJid().asBareJid() + ": online with resource " + account.getResource());
1110        changeStatus(Account.State.ONLINE);
1111    }
1112
1113    private void processFailed(final Element failed, final boolean sendBindRequest) {
1114        final Optional<Integer> serverCount = failed.getOptionalIntAttribute("h");
1115        if (serverCount.isPresent()) {
1116            Log.d(
1117                    Config.LOGTAG,
1118                    account.getJid().asBareJid()
1119                            + ": resumption failed but server acknowledged stanza #"
1120                            + serverCount.get());
1121            final boolean acknowledgedMessages;
1122            synchronized (this.mStanzaQueue) {
1123                acknowledgedMessages = acknowledgeStanzaUpTo(serverCount.get());
1124            }
1125            if (acknowledgedMessages) {
1126                mXmppConnectionService.updateConversationUi();
1127            }
1128        } else {
1129            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed");
1130        }
1131        resetStreamId();
1132        if (sendBindRequest) {
1133            sendBindRequest();
1134        }
1135    }
1136
1137    private boolean acknowledgeStanzaUpTo(final int serverCount) {
1138        if (serverCount > stanzasSent) {
1139            Log.e(
1140                    Config.LOGTAG,
1141                    "server acknowledged more stanzas than we sent. serverCount="
1142                            + serverCount
1143                            + ", ourCount="
1144                            + stanzasSent);
1145        }
1146        boolean acknowledgedMessages = false;
1147        for (int i = 0; i < mStanzaQueue.size(); ++i) {
1148            if (serverCount >= mStanzaQueue.keyAt(i)) {
1149                if (Config.EXTENDED_SM_LOGGING) {
1150                    Log.d(
1151                            Config.LOGTAG,
1152                            account.getJid().asBareJid()
1153                                    + ": server acknowledged stanza #"
1154                                    + mStanzaQueue.keyAt(i));
1155                }
1156                final AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
1157                if (stanza instanceof MessagePacket && acknowledgedListener != null) {
1158                    final MessagePacket packet = (MessagePacket) stanza;
1159                    final String id = packet.getId();
1160                    final Jid to = packet.getTo();
1161                    if (id != null && to != null) {
1162                        acknowledgedMessages |=
1163                                acknowledgedListener.onMessageAcknowledged(account, to, id);
1164                    }
1165                }
1166                mStanzaQueue.removeAt(i);
1167                i--;
1168            }
1169        }
1170        return acknowledgedMessages;
1171    }
1172
1173    private @NonNull Element processPacket(final Tag currentTag, final int packetType)
1174            throws IOException {
1175        final Element element =
1176                switch (packetType) {
1177                    case PACKET_IQ -> new IqPacket();
1178                    case PACKET_MESSAGE -> new MessagePacket();
1179                    case PACKET_PRESENCE -> new PresencePacket();
1180                    default -> throw new AssertionError("Should never encounter invalid type");
1181                };
1182        element.setAttributes(currentTag.getAttributes());
1183        Tag nextTag = tagReader.readTag();
1184        if (nextTag == null) {
1185            throw new IOException("interrupted mid tag");
1186        }
1187        while (!nextTag.isEnd(element.getName())) {
1188            if (!nextTag.isNo()) {
1189                element.addChild(tagReader.readElement(nextTag));
1190            }
1191            nextTag = tagReader.readTag();
1192            if (nextTag == null) {
1193                throw new IOException("interrupted mid tag");
1194            }
1195        }
1196        if (stanzasReceived == Integer.MAX_VALUE) {
1197            resetStreamId();
1198            throw new IOException("time to restart the session. cant handle >2 billion pcks");
1199        }
1200        if (inSmacksSession) {
1201            ++stanzasReceived;
1202        } else if (features.sm()) {
1203            Log.d(
1204                    Config.LOGTAG,
1205                    account.getJid().asBareJid()
1206                            + ": not counting stanza("
1207                            + element.getClass().getSimpleName()
1208                            + "). Not in smacks session.");
1209        }
1210        lastPacketReceived = SystemClock.elapsedRealtime();
1211        if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
1212            Log.d(Config.LOGTAG, "[background stanza] " + element);
1213        }
1214        if (element instanceof IqPacket
1215                && (((IqPacket) element).getType() == IqPacket.TYPE.SET)
1216                && element.hasChild("jingle", Namespace.JINGLE)) {
1217            return JinglePacket.upgrade((IqPacket) element);
1218        } else {
1219            return element;
1220        }
1221    }
1222
1223    private void processIq(final Tag currentTag) throws IOException {
1224        final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
1225        if (!packet.valid()) {
1226            Log.e(
1227                    Config.LOGTAG,
1228                    "encountered invalid iq from='"
1229                            + packet.getFrom()
1230                            + "' to='"
1231                            + packet.getTo()
1232                            + "'");
1233            return;
1234        }
1235        if (packet instanceof JinglePacket) {
1236            if (this.jingleListener != null) {
1237                this.jingleListener.onJinglePacketReceived(account, (JinglePacket) packet);
1238            }
1239        } else {
1240            OnIqPacketReceived callback = null;
1241            synchronized (this.packetCallbacks) {
1242                final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple =
1243                        packetCallbacks.get(packet.getId());
1244                if (packetCallbackDuple != null) {
1245                    // Packets to the server should have responses from the server
1246                    if (packetCallbackDuple.first.toServer(account)) {
1247                        if (packet.fromServer(account)) {
1248                            callback = packetCallbackDuple.second;
1249                            packetCallbacks.remove(packet.getId());
1250                        } else {
1251                            Log.e(
1252                                    Config.LOGTAG,
1253                                    account.getJid().asBareJid().toString()
1254                                            + ": ignoring spoofed iq packet");
1255                        }
1256                    } else {
1257                        if (packet.getFrom() != null
1258                                && packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
1259                            callback = packetCallbackDuple.second;
1260                            packetCallbacks.remove(packet.getId());
1261                        } else {
1262                            Log.e(
1263                                    Config.LOGTAG,
1264                                    account.getJid().asBareJid().toString()
1265                                            + ": ignoring spoofed iq packet");
1266                        }
1267                    }
1268                } else if (packet.getType() == IqPacket.TYPE.GET
1269                        || packet.getType() == IqPacket.TYPE.SET) {
1270                    callback = this.unregisteredIqListener;
1271                }
1272            }
1273            if (callback != null) {
1274                try {
1275                    callback.onIqPacketReceived(account, packet);
1276                } catch (StateChangingError error) {
1277                    throw new StateChangingException(error.state);
1278                }
1279            }
1280        }
1281    }
1282
1283    private void processMessage(final Tag currentTag) throws IOException {
1284        final MessagePacket packet = (MessagePacket) processPacket(currentTag, PACKET_MESSAGE);
1285        if (!packet.valid()) {
1286            Log.e(
1287                    Config.LOGTAG,
1288                    "encountered invalid message from='"
1289                            + packet.getFrom()
1290                            + "' to='"
1291                            + packet.getTo()
1292                            + "'");
1293            return;
1294        }
1295        this.messageListener.onMessagePacketReceived(account, packet);
1296    }
1297
1298    private void processPresence(final Tag currentTag) throws IOException {
1299        PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE);
1300        if (!packet.valid()) {
1301            Log.e(
1302                    Config.LOGTAG,
1303                    "encountered invalid presence from='"
1304                            + packet.getFrom()
1305                            + "' to='"
1306                            + packet.getTo()
1307                            + "'");
1308            return;
1309        }
1310        this.presenceListener.onPresencePacketReceived(account, packet);
1311    }
1312
1313    private void sendStartTLS() throws IOException {
1314        final Tag startTLS = Tag.empty("starttls");
1315        startTLS.setAttribute("xmlns", Namespace.TLS);
1316        tagWriter.writeTag(startTLS);
1317    }
1318
1319    private void switchOverToTls() throws XmlPullParserException, IOException {
1320        tagReader.readTag();
1321        final Socket socket = this.socket;
1322        final SSLSocket sslSocket = upgradeSocketToTls(socket);
1323        this.socket = sslSocket;
1324        this.tagReader.setInputStream(sslSocket.getInputStream());
1325        this.tagWriter.setOutputStream(sslSocket.getOutputStream());
1326        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS connection established");
1327        final boolean quickStart;
1328        try {
1329            quickStart = establishStream(SSLSockets.version(sslSocket));
1330        } catch (final InterruptedException e) {
1331            return;
1332        }
1333        if (quickStart) {
1334            this.quickStartInProgress = true;
1335        }
1336        features.encryptionEnabled = true;
1337        final Tag tag = tagReader.readTag();
1338        if (tag != null && tag.isStart("stream", Namespace.STREAMS)) {
1339            SSLSockets.log(account, sslSocket);
1340            processStream();
1341        } else {
1342            throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
1343        }
1344        sslSocket.close();
1345    }
1346
1347    private SSLSocket upgradeSocketToTls(final Socket socket) throws IOException {
1348        final SSLSocketFactory sslSocketFactory;
1349        try {
1350            sslSocketFactory = getSSLSocketFactory();
1351        } catch (final NoSuchAlgorithmException | KeyManagementException e) {
1352            throw new StateChangingException(Account.State.TLS_ERROR);
1353        }
1354        final InetAddress address = socket.getInetAddress();
1355        final SSLSocket sslSocket =
1356                (SSLSocket)
1357                        sslSocketFactory.createSocket(
1358                                socket, address.getHostAddress(), socket.getPort(), true);
1359        SSLSockets.setSecurity(sslSocket);
1360        SSLSockets.setHostname(sslSocket, IDN.toASCII(account.getServer()));
1361        SSLSockets.setApplicationProtocol(sslSocket, "xmpp-client");
1362        final XmppDomainVerifier xmppDomainVerifier = new XmppDomainVerifier();
1363        try {
1364            if (!xmppDomainVerifier.verify(
1365                    account.getServer(), this.verifiedHostname, sslSocket.getSession())) {
1366                Log.d(
1367                        Config.LOGTAG,
1368                        account.getJid().asBareJid()
1369                                + ": TLS certificate domain verification failed");
1370                FileBackend.close(sslSocket);
1371                throw new StateChangingException(Account.State.TLS_ERROR_DOMAIN);
1372            }
1373        } catch (final SSLPeerUnverifiedException e) {
1374            FileBackend.close(sslSocket);
1375            throw new StateChangingException(Account.State.TLS_ERROR);
1376        }
1377        return sslSocket;
1378    }
1379
1380    private void processStreamFeatures(final Tag currentTag) throws IOException {
1381        this.streamFeatures = tagReader.readElement(currentTag);
1382        final boolean isSecure = isSecure();
1383        final boolean needsBinding = !isBound && !account.isOptionSet(Account.OPTION_REGISTER);
1384        if (this.quickStartInProgress) {
1385            if (this.streamFeatures.hasChild("authentication", Namespace.SASL_2)) {
1386                Log.d(
1387                        Config.LOGTAG,
1388                        account.getJid().asBareJid()
1389                                + ": quick start in progress. ignoring features: "
1390                                + XmlHelper.printElementNames(this.streamFeatures));
1391                if (SaslMechanism.hashedToken(LoginInfo.mechanism(this.loginInfo))) {
1392                    return;
1393                }
1394                if (isFastTokenAvailable(
1395                        this.streamFeatures.findChild("authentication", Namespace.SASL_2))) {
1396                    Log.d(
1397                            Config.LOGTAG,
1398                            account.getJid().asBareJid()
1399                                    + ": fast token available; resetting quick start");
1400                    account.setOption(Account.OPTION_QUICKSTART_AVAILABLE, false);
1401                    mXmppConnectionService.databaseBackend.updateAccount(account);
1402                }
1403                return;
1404            }
1405            Log.d(
1406                    Config.LOGTAG,
1407                    account.getJid().asBareJid()
1408                            + ": server lost support for SASL 2. quick start not possible");
1409            this.account.setOption(Account.OPTION_QUICKSTART_AVAILABLE, false);
1410            mXmppConnectionService.databaseBackend.updateAccount(account);
1411            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1412        }
1413        if (this.streamFeatures.hasChild("starttls", Namespace.TLS)
1414                && !features.encryptionEnabled) {
1415            sendStartTLS();
1416        } else if (this.streamFeatures.hasChild("register", Namespace.REGISTER_STREAM_FEATURE)
1417                && account.isOptionSet(Account.OPTION_REGISTER)) {
1418            if (isSecure) {
1419                register();
1420            } else {
1421                Log.d(
1422                        Config.LOGTAG,
1423                        account.getJid().asBareJid()
1424                                + ": unable to find STARTTLS for registration process "
1425                                + XmlHelper.printElementNames(this.streamFeatures));
1426                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1427            }
1428        } else if (!this.streamFeatures.hasChild("register", Namespace.REGISTER_STREAM_FEATURE)
1429                && account.isOptionSet(Account.OPTION_REGISTER)) {
1430            throw new StateChangingException(Account.State.REGISTRATION_NOT_SUPPORTED);
1431        } else if (this.streamFeatures.hasChild("authentication", Namespace.SASL_2)
1432                && shouldAuthenticate
1433                && isSecure) {
1434            authenticate(SaslMechanism.Version.SASL_2);
1435        } else if (this.streamFeatures.hasChild("mechanisms", Namespace.SASL)
1436                && shouldAuthenticate
1437                && isSecure) {
1438            authenticate(SaslMechanism.Version.SASL);
1439        } else if (this.streamFeatures.hasChild("sm", Namespace.STREAM_MANAGEMENT)
1440                && streamId != null
1441                && !inSmacksSession) {
1442            if (Config.EXTENDED_SM_LOGGING) {
1443                Log.d(
1444                        Config.LOGTAG,
1445                        account.getJid().asBareJid()
1446                                + ": resuming after stanza #"
1447                                + stanzasReceived);
1448            }
1449            final ResumePacket resume = new ResumePacket(this.streamId.id, stanzasReceived);
1450            this.mSmCatchupMessageCounter.set(0);
1451            this.mWaitingForSmCatchup.set(true);
1452            this.tagWriter.writeStanzaAsync(resume);
1453        } else if (needsBinding) {
1454            if (this.streamFeatures.hasChild("bind", Namespace.BIND) && isSecure) {
1455                sendBindRequest();
1456            } else {
1457                Log.d(
1458                        Config.LOGTAG,
1459                        account.getJid().asBareJid()
1460                                + ": unable to find bind feature "
1461                                + XmlHelper.printElementNames(this.streamFeatures));
1462                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1463            }
1464        } else {
1465
1466            Log.d(
1467                    Config.LOGTAG,
1468                    account.getJid().asBareJid()
1469                            + ": received NOP stream features: "
1470                            + XmlHelper.printElementNames(this.streamFeatures));
1471        }
1472    }
1473
1474    private void authenticate() throws IOException {
1475        final boolean isSecure = isSecure();
1476        if (isSecure && this.streamFeatures.hasChild("authentication", Namespace.SASL_2)) {
1477            authenticate(SaslMechanism.Version.SASL_2);
1478        } else if (isSecure && this.streamFeatures.hasChild("mechanisms", Namespace.SASL)) {
1479            authenticate(SaslMechanism.Version.SASL);
1480        } else {
1481            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1482        }
1483    }
1484
1485    private boolean isSecure() {
1486        return features.encryptionEnabled || Config.ALLOW_NON_TLS_CONNECTIONS || account.isOnion();
1487    }
1488
1489    private void authenticate(final SaslMechanism.Version version) throws IOException {
1490        final Element authElement;
1491        if (version == SaslMechanism.Version.SASL) {
1492            authElement = this.streamFeatures.findChild("mechanisms", Namespace.SASL);
1493        } else {
1494            authElement = this.streamFeatures.findChild("authentication", Namespace.SASL_2);
1495        }
1496        final Collection<String> mechanisms = SaslMechanism.mechanisms(authElement);
1497        final Element cbElement =
1498                this.streamFeatures.findChild("sasl-channel-binding", Namespace.CHANNEL_BINDING);
1499        final Collection<ChannelBinding> channelBindings = ChannelBinding.of(cbElement);
1500        final SaslMechanism.Factory factory = new SaslMechanism.Factory(account);
1501        final SaslMechanism saslMechanism =
1502                factory.of(mechanisms, channelBindings, version, SSLSockets.version(this.socket));
1503        this.validate(saslMechanism, mechanisms);
1504        final boolean quickStartAvailable;
1505        final String firstMessage =
1506                saslMechanism.getClientFirstMessage(sslSocketOrNull(this.socket));
1507        final boolean usingFast = SaslMechanism.hashedToken(saslMechanism);
1508        final Element authenticate;
1509        if (version == SaslMechanism.Version.SASL) {
1510            authenticate = new Element("auth", Namespace.SASL);
1511            if (!Strings.isNullOrEmpty(firstMessage)) {
1512                authenticate.setContent(firstMessage);
1513            }
1514            quickStartAvailable = false;
1515            this.loginInfo = new LoginInfo(saslMechanism, version, Collections.emptyList());
1516        } else if (version == SaslMechanism.Version.SASL_2) {
1517            final Element inline = authElement.findChild("inline", Namespace.SASL_2);
1518            final boolean sm = inline != null && inline.hasChild("sm", Namespace.STREAM_MANAGEMENT);
1519            final HashedToken.Mechanism hashTokenRequest;
1520            if (usingFast) {
1521                hashTokenRequest = null;
1522            } else {
1523                final Element fast =
1524                        inline == null ? null : inline.findChild("fast", Namespace.FAST);
1525                final Collection<String> fastMechanisms = SaslMechanism.mechanisms(fast);
1526                hashTokenRequest =
1527                        HashedToken.Mechanism.best(fastMechanisms, SSLSockets.version(this.socket));
1528            }
1529            final Collection<String> bindFeatures = Bind2.features(inline);
1530            quickStartAvailable =
1531                    sm
1532                            && bindFeatures != null
1533                            && bindFeatures.containsAll(Bind2.QUICKSTART_FEATURES);
1534            if (bindFeatures != null) {
1535                try {
1536                    mXmppConnectionService.restoredFromDatabaseLatch.await();
1537                } catch (final InterruptedException e) {
1538                    Log.d(
1539                            Config.LOGTAG,
1540                            account.getJid().asBareJid()
1541                                    + ": interrupted while waiting for DB restore during SASL2 bind");
1542                    return;
1543                }
1544            }
1545            this.loginInfo = new LoginInfo(saslMechanism, version, bindFeatures);
1546            this.hashTokenRequest = hashTokenRequest;
1547            authenticate =
1548                    generateAuthenticationRequest(
1549                            firstMessage, usingFast, hashTokenRequest, bindFeatures, sm);
1550        } else {
1551            throw new AssertionError("Missing implementation for " + version);
1552        }
1553
1554        if (account.setOption(Account.OPTION_QUICKSTART_AVAILABLE, quickStartAvailable)) {
1555            mXmppConnectionService.databaseBackend.updateAccount(account);
1556        }
1557
1558        Log.d(
1559                Config.LOGTAG,
1560                account.getJid().toString()
1561                        + ": Authenticating with "
1562                        + version
1563                        + "/"
1564                        + LoginInfo.mechanism(this.loginInfo).getMechanism());
1565        authenticate.setAttribute("mechanism", LoginInfo.mechanism(this.loginInfo).getMechanism());
1566        synchronized (this.mStanzaQueue) {
1567            this.stanzasSentBeforeAuthentication = this.stanzasSent;
1568            tagWriter.writeElement(authenticate);
1569        }
1570    }
1571
1572    private static boolean isFastTokenAvailable(final Element authentication) {
1573        final Element inline = authentication == null ? null : authentication.findChild("inline");
1574        return inline != null && inline.hasChild("fast", Namespace.FAST);
1575    }
1576
1577    private void validate(
1578            final @Nullable SaslMechanism saslMechanism, Collection<String> mechanisms)
1579            throws StateChangingException {
1580        if (saslMechanism == null) {
1581            Log.d(
1582                    Config.LOGTAG,
1583                    account.getJid().asBareJid()
1584                            + ": unable to find supported SASL mechanism in "
1585                            + mechanisms);
1586            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1587        }
1588        if (SaslMechanism.hashedToken(saslMechanism)) {
1589            return;
1590        }
1591        final int pinnedMechanism = account.getPinnedMechanismPriority();
1592        if (pinnedMechanism > saslMechanism.getPriority()) {
1593            Log.e(
1594                    Config.LOGTAG,
1595                    "Auth failed. Authentication mechanism "
1596                            + saslMechanism.getMechanism()
1597                            + " has lower priority ("
1598                            + saslMechanism.getPriority()
1599                            + ") than pinned priority ("
1600                            + pinnedMechanism
1601                            + "). Possible downgrade attack?");
1602            throw new StateChangingException(Account.State.DOWNGRADE_ATTACK);
1603        }
1604    }
1605
1606    private Element generateAuthenticationRequest(
1607            final String firstMessage, final boolean usingFast) {
1608        return generateAuthenticationRequest(
1609                firstMessage, usingFast, null, Bind2.QUICKSTART_FEATURES, true);
1610    }
1611
1612    private Element generateAuthenticationRequest(
1613            final String firstMessage,
1614            final boolean usingFast,
1615            final HashedToken.Mechanism hashedTokenRequest,
1616            final Collection<String> bind,
1617            final boolean inlineStreamManagement) {
1618        final Element authenticate = new Element("authenticate", Namespace.SASL_2);
1619        if (!Strings.isNullOrEmpty(firstMessage)) {
1620            authenticate.addChild("initial-response").setContent(firstMessage);
1621        }
1622        final Element userAgent = authenticate.addChild("user-agent");
1623        userAgent.setAttribute("id", AccountUtils.publicDeviceId(account));
1624        userAgent
1625                .addChild("software")
1626                .setContent(mXmppConnectionService.getString(R.string.app_name));
1627        if (!PhoneHelper.isEmulator()) {
1628            userAgent
1629                    .addChild("device")
1630                    .setContent(String.format("%s %s", Build.MANUFACTURER, Build.MODEL));
1631        }
1632        // do not include bind if 'inlineStreamManagement' is missing and we have a streamId
1633        // (because we would rather just do a normal SM/resume)
1634        final boolean mayAttemptBind = streamId == null || inlineStreamManagement;
1635        if (bind != null && mayAttemptBind) {
1636            authenticate.addChild(generateBindRequest(bind));
1637        }
1638        if (inlineStreamManagement && streamId != null) {
1639            final ResumePacket resume = new ResumePacket(this.streamId.id, stanzasReceived);
1640            this.mSmCatchupMessageCounter.set(0);
1641            this.mWaitingForSmCatchup.set(true);
1642            authenticate.addChild(resume);
1643        }
1644        if (hashedTokenRequest != null) {
1645            authenticate
1646                    .addChild("request-token", Namespace.FAST)
1647                    .setAttribute("mechanism", hashedTokenRequest.name());
1648        }
1649        if (usingFast) {
1650            authenticate.addChild("fast", Namespace.FAST);
1651        }
1652        return authenticate;
1653    }
1654
1655    private Element generateBindRequest(final Collection<String> bindFeatures) {
1656        Log.d(Config.LOGTAG, "inline bind features: " + bindFeatures);
1657        final Element bind = new Element("bind", Namespace.BIND2);
1658        bind.addChild("tag").setContent(mXmppConnectionService.getString(R.string.app_name));
1659        if (bindFeatures.contains(Namespace.CARBONS)) {
1660            bind.addChild("enable", Namespace.CARBONS);
1661        }
1662        if (bindFeatures.contains(Namespace.STREAM_MANAGEMENT)) {
1663            bind.addChild(new EnablePacket());
1664        }
1665        return bind;
1666    }
1667
1668    private void register() {
1669        final String preAuth = account.getKey(Account.KEY_PRE_AUTH_REGISTRATION_TOKEN);
1670        if (preAuth != null && features.invite()) {
1671            final IqPacket preAuthRequest = new IqPacket(IqPacket.TYPE.SET);
1672            preAuthRequest.addChild("preauth", Namespace.PARS).setAttribute("token", preAuth);
1673            sendUnmodifiedIqPacket(
1674                    preAuthRequest,
1675                    (account, response) -> {
1676                        if (response.getType() == IqPacket.TYPE.RESULT) {
1677                            sendRegistryRequest();
1678                        } else {
1679                            final String error = response.getErrorCondition();
1680                            Log.d(
1681                                    Config.LOGTAG,
1682                                    account.getJid().asBareJid()
1683                                            + ": failed to pre auth. "
1684                                            + error);
1685                            throw new StateChangingError(Account.State.REGISTRATION_INVALID_TOKEN);
1686                        }
1687                    },
1688                    true);
1689        } else {
1690            sendRegistryRequest();
1691        }
1692    }
1693
1694    private void sendRegistryRequest() {
1695        final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
1696        register.query(Namespace.REGISTER);
1697        register.setTo(account.getDomain());
1698        sendUnmodifiedIqPacket(
1699                register,
1700                (account, packet) -> {
1701                    if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1702                        return;
1703                    }
1704                    if (packet.getType() == IqPacket.TYPE.ERROR) {
1705                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1706                    }
1707                    final Element query = packet.query(Namespace.REGISTER);
1708                    if (query.hasChild("username") && (query.hasChild("password"))) {
1709                        final IqPacket register1 = new IqPacket(IqPacket.TYPE.SET);
1710                        final Element username =
1711                                new Element("username").setContent(account.getUsername());
1712                        final Element password =
1713                                new Element("password").setContent(account.getPassword());
1714                        register1.query(Namespace.REGISTER).addChild(username);
1715                        register1.query().addChild(password);
1716                        register1.setFrom(account.getJid().asBareJid());
1717                        sendUnmodifiedIqPacket(register1, registrationResponseListener, true);
1718                    } else if (query.hasChild("x", Namespace.DATA)) {
1719                        final Data data = Data.parse(query.findChild("x", Namespace.DATA));
1720                        final Element blob = query.findChild("data", "urn:xmpp:bob");
1721                        final String id = packet.getId();
1722                        InputStream is;
1723                        if (blob != null) {
1724                            try {
1725                                final String base64Blob = blob.getContent();
1726                                final byte[] strBlob = Base64.decode(base64Blob, Base64.DEFAULT);
1727                                is = new ByteArrayInputStream(strBlob);
1728                            } catch (Exception e) {
1729                                is = null;
1730                            }
1731                        } else {
1732                            final boolean useTor =
1733                                    mXmppConnectionService.useTorToConnect() || account.isOnion();
1734                            try {
1735                                final String url = data.getValue("url");
1736                                final String fallbackUrl = data.getValue("captcha-fallback-url");
1737                                if (url != null) {
1738                                    is = HttpConnectionManager.open(url, useTor);
1739                                } else if (fallbackUrl != null) {
1740                                    is = HttpConnectionManager.open(fallbackUrl, useTor);
1741                                } else {
1742                                    is = null;
1743                                }
1744                            } catch (final IOException e) {
1745                                Log.d(
1746                                        Config.LOGTAG,
1747                                        account.getJid().asBareJid() + ": unable to fetch captcha",
1748                                        e);
1749                                is = null;
1750                            }
1751                        }
1752
1753                        if (is != null) {
1754                            Bitmap captcha = BitmapFactory.decodeStream(is);
1755                            try {
1756                                if (mXmppConnectionService.displayCaptchaRequest(
1757                                        account, id, data, captcha)) {
1758                                    return;
1759                                }
1760                            } catch (Exception e) {
1761                                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1762                            }
1763                        }
1764                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1765                    } else if (query.hasChild("instructions")
1766                            || query.hasChild("x", Namespace.OOB)) {
1767                        final String instructions = query.findChildContent("instructions");
1768                        final Element oob = query.findChild("x", Namespace.OOB);
1769                        final String url = oob == null ? null : oob.findChildContent("url");
1770                        if (url != null) {
1771                            setAccountCreationFailed(url);
1772                        } else if (instructions != null) {
1773                            final Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(instructions);
1774                            if (matcher.find()) {
1775                                setAccountCreationFailed(
1776                                        instructions.substring(matcher.start(), matcher.end()));
1777                            }
1778                        }
1779                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1780                    }
1781                },
1782                true);
1783    }
1784
1785    private void setAccountCreationFailed(final String url) {
1786        final HttpUrl httpUrl = url == null ? null : HttpUrl.parse(url);
1787        if (httpUrl != null && httpUrl.isHttps()) {
1788            this.redirectionUrl = httpUrl;
1789            throw new StateChangingError(Account.State.REGISTRATION_WEB);
1790        }
1791        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1792    }
1793
1794    public HttpUrl getRedirectionUrl() {
1795        return this.redirectionUrl;
1796    }
1797
1798    public void resetEverything() {
1799        resetAttemptCount(true);
1800        resetStreamId();
1801        clearIqCallbacks();
1802        this.stanzasSent = 0;
1803        mStanzaQueue.clear();
1804        this.redirectionUrl = null;
1805        synchronized (this.disco) {
1806            disco.clear();
1807        }
1808        synchronized (this.commands) {
1809            this.commands.clear();
1810        }
1811        this.loginInfo = null;
1812    }
1813
1814    private void sendBindRequest() {
1815        try {
1816            mXmppConnectionService.restoredFromDatabaseLatch.await();
1817        } catch (InterruptedException e) {
1818            Log.d(
1819                    Config.LOGTAG,
1820                    account.getJid().asBareJid()
1821                            + ": interrupted while waiting for DB restore during bind");
1822            return;
1823        }
1824        clearIqCallbacks();
1825        if (account.getJid().isBareJid()) {
1826            account.setResource(this.createNewResource());
1827        } else {
1828            fixResource(mXmppConnectionService, account);
1829        }
1830        final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
1831        final String resource =
1832                Config.USE_RANDOM_RESOURCE_ON_EVERY_BIND ? nextRandomId() : account.getResource();
1833        iq.addChild("bind", Namespace.BIND).addChild("resource").setContent(resource);
1834        this.sendUnmodifiedIqPacket(
1835                iq,
1836                (account, packet) -> {
1837                    if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1838                        return;
1839                    }
1840                    final Element bind = packet.findChild("bind");
1841                    if (bind != null && packet.getType() == IqPacket.TYPE.RESULT) {
1842                        isBound = true;
1843                        final Element jid = bind.findChild("jid");
1844                        if (jid != null && jid.getContent() != null) {
1845                            try {
1846                                Jid assignedJid = Jid.ofEscaped(jid.getContent());
1847                                if (!account.getJid().getDomain().equals(assignedJid.getDomain())) {
1848                                    Log.d(
1849                                            Config.LOGTAG,
1850                                            account.getJid().asBareJid()
1851                                                    + ": server tried to re-assign domain to "
1852                                                    + assignedJid.getDomain());
1853                                    throw new StateChangingError(Account.State.BIND_FAILURE);
1854                                }
1855                                if (account.setJid(assignedJid)) {
1856                                    Log.d(
1857                                            Config.LOGTAG,
1858                                            account.getJid().asBareJid()
1859                                                    + ": jid changed during bind. updating database");
1860                                    mXmppConnectionService.databaseBackend.updateAccount(account);
1861                                }
1862                                if (streamFeatures.hasChild("session")
1863                                        && !streamFeatures
1864                                                .findChild("session")
1865                                                .hasChild("optional")) {
1866                                    sendStartSession();
1867                                } else {
1868                                    final boolean waitForDisco = enableStreamManagement();
1869                                    sendPostBindInitialization(waitForDisco, false);
1870                                }
1871                                return;
1872                            } catch (final IllegalArgumentException e) {
1873                                Log.d(
1874                                        Config.LOGTAG,
1875                                        account.getJid().asBareJid()
1876                                                + ": server reported invalid jid ("
1877                                                + jid.getContent()
1878                                                + ") on bind");
1879                            }
1880                        } else {
1881                            Log.d(
1882                                    Config.LOGTAG,
1883                                    account.getJid()
1884                                            + ": disconnecting because of bind failure. (no jid)");
1885                        }
1886                    } else {
1887                        Log.d(
1888                                Config.LOGTAG,
1889                                account.getJid()
1890                                        + ": disconnecting because of bind failure ("
1891                                        + packet);
1892                    }
1893                    final Element error = packet.findChild("error");
1894                    if (packet.getType() == IqPacket.TYPE.ERROR
1895                            && error != null
1896                            && error.hasChild("conflict")) {
1897                        account.setResource(createNewResource());
1898                    }
1899                    throw new StateChangingError(Account.State.BIND_FAILURE);
1900                },
1901                true);
1902    }
1903
1904    private void clearIqCallbacks() {
1905        final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.TIMEOUT);
1906        final ArrayList<OnIqPacketReceived> callbacks = new ArrayList<>();
1907        synchronized (this.packetCallbacks) {
1908            if (this.packetCallbacks.size() == 0) {
1909                return;
1910            }
1911            Log.d(
1912                    Config.LOGTAG,
1913                    account.getJid().asBareJid()
1914                            + ": clearing "
1915                            + this.packetCallbacks.size()
1916                            + " iq callbacks");
1917            final Iterator<Pair<IqPacket, OnIqPacketReceived>> iterator =
1918                    this.packetCallbacks.values().iterator();
1919            while (iterator.hasNext()) {
1920                Pair<IqPacket, OnIqPacketReceived> entry = iterator.next();
1921                callbacks.add(entry.second);
1922                iterator.remove();
1923            }
1924        }
1925        for (OnIqPacketReceived callback : callbacks) {
1926            try {
1927                callback.onIqPacketReceived(account, failurePacket);
1928            } catch (StateChangingError error) {
1929                Log.d(
1930                        Config.LOGTAG,
1931                        account.getJid().asBareJid()
1932                                + ": caught StateChangingError("
1933                                + error.state.toString()
1934                                + ") while clearing callbacks");
1935                // ignore
1936            }
1937        }
1938        Log.d(
1939                Config.LOGTAG,
1940                account.getJid().asBareJid()
1941                        + ": done clearing iq callbacks. "
1942                        + this.packetCallbacks.size()
1943                        + " left");
1944    }
1945
1946    public void sendDiscoTimeout() {
1947        if (mWaitForDisco.compareAndSet(true, false)) {
1948            Log.d(
1949                    Config.LOGTAG,
1950                    account.getJid().asBareJid() + ": finalizing bind after disco timeout");
1951            finalizeBind();
1952        }
1953    }
1954
1955    private void sendStartSession() {
1956        Log.d(
1957                Config.LOGTAG,
1958                account.getJid().asBareJid() + ": sending legacy session to outdated server");
1959        final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET);
1960        startSession.addChild("session", "urn:ietf:params:xml:ns:xmpp-session");
1961        this.sendUnmodifiedIqPacket(
1962                startSession,
1963                (account, packet) -> {
1964                    if (packet.getType() == IqPacket.TYPE.RESULT) {
1965                        final boolean waitForDisco = enableStreamManagement();
1966                        sendPostBindInitialization(waitForDisco, false);
1967                    } else if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1968                        throw new StateChangingError(Account.State.SESSION_FAILURE);
1969                    }
1970                },
1971                true);
1972    }
1973
1974    private boolean enableStreamManagement() {
1975        final boolean streamManagement =
1976                this.streamFeatures.hasChild("sm", Namespace.STREAM_MANAGEMENT);
1977        if (streamManagement) {
1978            synchronized (this.mStanzaQueue) {
1979                final EnablePacket enable = new EnablePacket();
1980                tagWriter.writeStanzaAsync(enable);
1981                stanzasSent = 0;
1982                mStanzaQueue.clear();
1983            }
1984            return true;
1985        } else {
1986            return false;
1987        }
1988    }
1989
1990    private void sendPostBindInitialization(
1991            final boolean waitForDisco, final boolean carbonsEnabled) {
1992        features.carbonsEnabled = carbonsEnabled;
1993        features.blockListRequested = false;
1994        synchronized (this.disco) {
1995            this.disco.clear();
1996        }
1997        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": starting service discovery");
1998        mPendingServiceDiscoveries.set(0);
1999        mWaitForDisco.set(waitForDisco);
2000        lastDiscoStarted = SystemClock.elapsedRealtime();
2001        mXmppConnectionService.scheduleWakeUpCall(
2002                Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
2003        final Element caps = streamFeatures.findChild("c");
2004        final String hash = caps == null ? null : caps.getAttribute("hash");
2005        final String ver = caps == null ? null : caps.getAttribute("ver");
2006        ServiceDiscoveryResult discoveryResult = null;
2007        if (hash != null && ver != null) {
2008            discoveryResult =
2009                    mXmppConnectionService.getCachedServiceDiscoveryResult(new Pair<>(hash, ver));
2010        }
2011        final boolean requestDiscoItemsFirst =
2012                !account.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY);
2013        if (requestDiscoItemsFirst) {
2014            sendServiceDiscoveryItems(account.getDomain());
2015        }
2016        if (discoveryResult == null) {
2017            sendServiceDiscoveryInfo(account.getDomain());
2018        } else {
2019            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server caps came from cache");
2020            disco.put(account.getDomain(), discoveryResult);
2021        }
2022        discoverMamPreferences();
2023        sendServiceDiscoveryInfo(account.getJid().asBareJid());
2024        if (!requestDiscoItemsFirst) {
2025            sendServiceDiscoveryItems(account.getDomain());
2026        }
2027
2028        if (!mWaitForDisco.get()) {
2029            finalizeBind();
2030        }
2031        this.lastSessionStarted = SystemClock.elapsedRealtime();
2032    }
2033
2034    private void sendServiceDiscoveryInfo(final Jid jid) {
2035        mPendingServiceDiscoveries.incrementAndGet();
2036        final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
2037        iq.setTo(jid);
2038        iq.query("http://jabber.org/protocol/disco#info");
2039        this.sendIqPacket(
2040                iq,
2041                (account, packet) -> {
2042                    if (packet.getType() == IqPacket.TYPE.RESULT) {
2043                        boolean advancedStreamFeaturesLoaded;
2044                        synchronized (XmppConnection.this.disco) {
2045                            ServiceDiscoveryResult result = new ServiceDiscoveryResult(packet);
2046                            if (jid.equals(account.getDomain())) {
2047                                mXmppConnectionService.databaseBackend.insertDiscoveryResult(
2048                                        result);
2049                            }
2050                            disco.put(jid, result);
2051                            advancedStreamFeaturesLoaded =
2052                                    disco.containsKey(account.getDomain())
2053                                            && disco.containsKey(account.getJid().asBareJid());
2054                        }
2055                        if (advancedStreamFeaturesLoaded
2056                                && (jid.equals(account.getDomain())
2057                                        || jid.equals(account.getJid().asBareJid()))) {
2058                            enableAdvancedStreamFeatures();
2059                        }
2060                    } else if (packet.getType() == IqPacket.TYPE.ERROR) {
2061                        Log.d(
2062                                Config.LOGTAG,
2063                                account.getJid().asBareJid()
2064                                        + ": could not query disco info for "
2065                                        + jid.toString());
2066                        final boolean serverOrAccount =
2067                                jid.equals(account.getDomain())
2068                                        || jid.equals(account.getJid().asBareJid());
2069                        final boolean advancedStreamFeaturesLoaded;
2070                        if (serverOrAccount) {
2071                            synchronized (XmppConnection.this.disco) {
2072                                disco.put(jid, ServiceDiscoveryResult.empty());
2073                                advancedStreamFeaturesLoaded =
2074                                        disco.containsKey(account.getDomain())
2075                                                && disco.containsKey(account.getJid().asBareJid());
2076                            }
2077                        } else {
2078                            advancedStreamFeaturesLoaded = false;
2079                        }
2080                        if (advancedStreamFeaturesLoaded) {
2081                            enableAdvancedStreamFeatures();
2082                        }
2083                    }
2084                    if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
2085                        if (mPendingServiceDiscoveries.decrementAndGet() == 0
2086                                && mWaitForDisco.compareAndSet(true, false)) {
2087                            finalizeBind();
2088                        }
2089                    }
2090                });
2091    }
2092
2093    private void discoverMamPreferences() {
2094        IqPacket request = new IqPacket(IqPacket.TYPE.GET);
2095        request.addChild("prefs", MessageArchiveService.Version.MAM_2.namespace);
2096        sendIqPacket(
2097                request,
2098                (account, response) -> {
2099                    if (response.getType() == IqPacket.TYPE.RESULT) {
2100                        Element prefs =
2101                                response.findChild(
2102                                        "prefs", MessageArchiveService.Version.MAM_2.namespace);
2103                        isMamPreferenceAlways =
2104                                "always"
2105                                        .equals(
2106                                                prefs == null
2107                                                        ? null
2108                                                        : prefs.getAttribute("default"));
2109                    }
2110                });
2111    }
2112
2113    private void discoverCommands() {
2114        final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
2115        request.setTo(account.getDomain());
2116        request.addChild("query", Namespace.DISCO_ITEMS).setAttribute("node", Namespace.COMMANDS);
2117        sendIqPacket(
2118                request,
2119                (account, response) -> {
2120                    if (response.getType() == IqPacket.TYPE.RESULT) {
2121                        final Element query = response.findChild("query", Namespace.DISCO_ITEMS);
2122                        if (query == null) {
2123                            return;
2124                        }
2125                        final HashMap<String, Jid> commands = new HashMap<>();
2126                        for (final Element child : query.getChildren()) {
2127                            if ("item".equals(child.getName())) {
2128                                final String node = child.getAttribute("node");
2129                                final Jid jid = child.getAttributeAsJid("jid");
2130                                if (node != null && jid != null) {
2131                                    commands.put(node, jid);
2132                                }
2133                            }
2134                        }
2135                        synchronized (this.commands) {
2136                            this.commands.clear();
2137                            this.commands.putAll(commands);
2138                        }
2139                    }
2140                });
2141    }
2142
2143    public boolean isMamPreferenceAlways() {
2144        return isMamPreferenceAlways;
2145    }
2146
2147    private void finalizeBind() {
2148        if (bindListener != null) {
2149            bindListener.onBind(account);
2150        }
2151        changeStatusToOnline();
2152    }
2153
2154    private void enableAdvancedStreamFeatures() {
2155        if (getFeatures().blocking() && !features.blockListRequested) {
2156            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Requesting block list");
2157            this.sendIqPacket(
2158                    getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
2159        }
2160        for (final OnAdvancedStreamFeaturesLoaded listener :
2161                advancedStreamFeaturesLoadedListeners) {
2162            listener.onAdvancedStreamFeaturesAvailable(account);
2163        }
2164        if (getFeatures().carbons() && !features.carbonsEnabled) {
2165            sendEnableCarbons();
2166        }
2167        if (getFeatures().commands()) {
2168            discoverCommands();
2169        }
2170    }
2171
2172    private void sendServiceDiscoveryItems(final Jid server) {
2173        mPendingServiceDiscoveries.incrementAndGet();
2174        final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
2175        iq.setTo(server.getDomain());
2176        iq.query("http://jabber.org/protocol/disco#items");
2177        this.sendIqPacket(
2178                iq,
2179                (account, packet) -> {
2180                    if (packet.getType() == IqPacket.TYPE.RESULT) {
2181                        final HashSet<Jid> items = new HashSet<>();
2182                        final List<Element> elements = packet.query().getChildren();
2183                        for (final Element element : elements) {
2184                            if (element.getName().equals("item")) {
2185                                final Jid jid =
2186                                        InvalidJid.getNullForInvalid(
2187                                                element.getAttributeAsJid("jid"));
2188                                if (jid != null && !jid.equals(account.getDomain())) {
2189                                    items.add(jid);
2190                                }
2191                            }
2192                        }
2193                        for (Jid jid : items) {
2194                            sendServiceDiscoveryInfo(jid);
2195                        }
2196                    } else {
2197                        Log.d(
2198                                Config.LOGTAG,
2199                                account.getJid().asBareJid()
2200                                        + ": could not query disco items of "
2201                                        + server);
2202                    }
2203                    if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
2204                        if (mPendingServiceDiscoveries.decrementAndGet() == 0
2205                                && mWaitForDisco.compareAndSet(true, false)) {
2206                            finalizeBind();
2207                        }
2208                    }
2209                });
2210    }
2211
2212    private void sendEnableCarbons() {
2213        final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
2214        iq.addChild("enable", Namespace.CARBONS);
2215        this.sendIqPacket(
2216                iq,
2217                (account, packet) -> {
2218                    if (packet.getType() == IqPacket.TYPE.RESULT) {
2219                        Log.d(
2220                                Config.LOGTAG,
2221                                account.getJid().asBareJid() + ": successfully enabled carbons");
2222                        features.carbonsEnabled = true;
2223                    } else {
2224                        Log.d(
2225                                Config.LOGTAG,
2226                                account.getJid().asBareJid()
2227                                        + ": could not enable carbons "
2228                                        + packet);
2229                    }
2230                });
2231    }
2232
2233    private void processStreamError(final Tag currentTag) throws IOException {
2234        final Element streamError = tagReader.readElement(currentTag);
2235        if (streamError == null) {
2236            return;
2237        }
2238        if (streamError.hasChild("conflict")) {
2239            account.setResource(createNewResource());
2240            Log.d(
2241                    Config.LOGTAG,
2242                    account.getJid().asBareJid()
2243                            + ": switching resource due to conflict ("
2244                            + account.getResource()
2245                            + ")");
2246            throw new IOException();
2247        } else if (streamError.hasChild("host-unknown")) {
2248            throw new StateChangingException(Account.State.HOST_UNKNOWN);
2249        } else if (streamError.hasChild("policy-violation")) {
2250            this.lastConnect = SystemClock.elapsedRealtime();
2251            final String text = streamError.findChildContent("text");
2252            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": policy violation. " + text);
2253            failPendingMessages(text);
2254            throw new StateChangingException(Account.State.POLICY_VIOLATION);
2255        } else if (streamError.hasChild("see-other-host")) {
2256            final String seeOtherHost = streamError.findChildContent("see-other-host");
2257            final Resolver.Result currentResolverResult = this.currentResolverResult;
2258            if (Strings.isNullOrEmpty(seeOtherHost) || currentResolverResult == null) {
2259                Log.d(
2260                        Config.LOGTAG,
2261                        account.getJid().asBareJid() + ": stream error " + streamError);
2262                throw new StateChangingException(Account.State.STREAM_ERROR);
2263            }
2264            Log.d(
2265                    Config.LOGTAG,
2266                    account.getJid().asBareJid()
2267                            + ": see other host: "
2268                            + seeOtherHost
2269                            + " "
2270                            + currentResolverResult);
2271            final Resolver.Result seeOtherResult = currentResolverResult.seeOtherHost(seeOtherHost);
2272            if (seeOtherResult != null) {
2273                this.seeOtherHostResolverResult = seeOtherResult;
2274                throw new StateChangingException(Account.State.SEE_OTHER_HOST);
2275            } else {
2276                throw new StateChangingException(Account.State.STREAM_ERROR);
2277            }
2278        } else {
2279            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": stream error " + streamError);
2280            throw new StateChangingException(Account.State.STREAM_ERROR);
2281        }
2282    }
2283
2284    private void failPendingMessages(final String error) {
2285        synchronized (this.mStanzaQueue) {
2286            for (int i = 0; i < mStanzaQueue.size(); ++i) {
2287                final AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
2288                if (stanza instanceof MessagePacket) {
2289                    final MessagePacket packet = (MessagePacket) stanza;
2290                    final String id = packet.getId();
2291                    final Jid to = packet.getTo();
2292                    mXmppConnectionService.markMessage(
2293                            account, to.asBareJid(), id, Message.STATUS_SEND_FAILED, error);
2294                }
2295            }
2296        }
2297    }
2298
2299    private boolean establishStream(final SSLSockets.Version sslVersion)
2300            throws IOException, InterruptedException {
2301        final boolean secureConnection = sslVersion != SSLSockets.Version.NONE;
2302        final SaslMechanism quickStartMechanism;
2303        if (secureConnection) {
2304            quickStartMechanism =
2305                    SaslMechanism.ensureAvailable(account.getQuickStartMechanism(), sslVersion);
2306        } else {
2307            quickStartMechanism = null;
2308        }
2309        if (secureConnection
2310                && Config.QUICKSTART_ENABLED
2311                && quickStartMechanism != null
2312                && account.isOptionSet(Account.OPTION_QUICKSTART_AVAILABLE)) {
2313            mXmppConnectionService.restoredFromDatabaseLatch.await();
2314            this.loginInfo =
2315                    new LoginInfo(
2316                            quickStartMechanism,
2317                            SaslMechanism.Version.SASL_2,
2318                            Bind2.QUICKSTART_FEATURES);
2319            final boolean usingFast = quickStartMechanism instanceof HashedToken;
2320            final Element authenticate =
2321                    generateAuthenticationRequest(
2322                            quickStartMechanism.getClientFirstMessage(sslSocketOrNull(this.socket)),
2323                            usingFast);
2324            authenticate.setAttribute("mechanism", quickStartMechanism.getMechanism());
2325            sendStartStream(true, false);
2326            synchronized (this.mStanzaQueue) {
2327                this.stanzasSentBeforeAuthentication = this.stanzasSent;
2328                tagWriter.writeElement(authenticate);
2329            }
2330            Log.d(
2331                    Config.LOGTAG,
2332                    account.getJid().toString()
2333                            + ": quick start with "
2334                            + quickStartMechanism.getMechanism());
2335            return true;
2336        } else {
2337            sendStartStream(secureConnection, true);
2338            return false;
2339        }
2340    }
2341
2342    private void sendStartStream(final boolean from, final boolean flush) throws IOException {
2343        final Tag stream = Tag.start("stream:stream");
2344        stream.setAttribute("to", account.getServer());
2345        if (from) {
2346            stream.setAttribute("from", account.getJid().asBareJid().toEscapedString());
2347        }
2348        stream.setAttribute("version", "1.0");
2349        stream.setAttribute("xml:lang", LocalizedContent.STREAM_LANGUAGE);
2350        stream.setAttribute("xmlns", Namespace.JABBER_CLIENT);
2351        stream.setAttribute("xmlns:stream", Namespace.STREAMS);
2352        tagWriter.writeTag(stream, flush);
2353    }
2354
2355    private String createNewResource() {
2356        return mXmppConnectionService.getString(R.string.app_name) + '.' + nextRandomId(true);
2357    }
2358
2359    private String nextRandomId() {
2360        return nextRandomId(false);
2361    }
2362
2363    private String nextRandomId(final boolean s) {
2364        return CryptoHelper.random(s ? 3 : 9);
2365    }
2366
2367    public String sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
2368        packet.setFrom(account.getJid());
2369        return this.sendUnmodifiedIqPacket(packet, callback, false);
2370    }
2371
2372    public synchronized String sendUnmodifiedIqPacket(
2373            final IqPacket packet, final OnIqPacketReceived callback, boolean force) {
2374        if (packet.getId() == null) {
2375            packet.setAttribute("id", nextRandomId());
2376        }
2377        if (callback != null) {
2378            synchronized (this.packetCallbacks) {
2379                packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
2380            }
2381        }
2382        this.sendPacket(packet, force);
2383        return packet.getId();
2384    }
2385
2386    public void sendMessagePacket(final MessagePacket packet) {
2387        this.sendPacket(packet);
2388    }
2389
2390    public void sendPresencePacket(final PresencePacket packet) {
2391        this.sendPacket(packet);
2392    }
2393
2394    private synchronized void sendPacket(final AbstractStanza packet) {
2395        sendPacket(packet, false);
2396    }
2397
2398    private synchronized void sendPacket(final AbstractStanza packet, final boolean force) {
2399        if (stanzasSent == Integer.MAX_VALUE) {
2400            resetStreamId();
2401            disconnect(true);
2402            return;
2403        }
2404        synchronized (this.mStanzaQueue) {
2405            if (force || isBound) {
2406                tagWriter.writeStanzaAsync(packet);
2407            } else {
2408                Log.d(
2409                        Config.LOGTAG,
2410                        account.getJid().asBareJid()
2411                                + " do not write stanza to unbound stream "
2412                                + packet.toString());
2413            }
2414            if (packet instanceof AbstractAcknowledgeableStanza) {
2415                AbstractAcknowledgeableStanza stanza = (AbstractAcknowledgeableStanza) packet;
2416
2417                if (this.mStanzaQueue.size() != 0) {
2418                    int currentHighestKey = this.mStanzaQueue.keyAt(this.mStanzaQueue.size() - 1);
2419                    if (currentHighestKey != stanzasSent) {
2420                        throw new AssertionError("Stanza count messed up");
2421                    }
2422                }
2423
2424                ++stanzasSent;
2425                if (Config.EXTENDED_SM_LOGGING) {
2426                    Log.d(
2427                            Config.LOGTAG,
2428                            account.getJid().asBareJid()
2429                                    + ": counting outbound "
2430                                    + packet.getName()
2431                                    + " as #"
2432                                    + stanzasSent);
2433                }
2434                this.mStanzaQueue.append(stanzasSent, stanza);
2435                if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) {
2436                    if (Config.EXTENDED_SM_LOGGING) {
2437                        Log.d(
2438                                Config.LOGTAG,
2439                                account.getJid().asBareJid()
2440                                        + ": requesting ack for message stanza #"
2441                                        + stanzasSent);
2442                    }
2443                    tagWriter.writeStanzaAsync(new RequestPacket());
2444                }
2445            }
2446        }
2447    }
2448
2449    public void sendPing() {
2450        if (!r()) {
2451            final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
2452            iq.setFrom(account.getJid());
2453            iq.addChild("ping", Namespace.PING);
2454            this.sendIqPacket(iq, null);
2455        }
2456        this.lastPingSent = SystemClock.elapsedRealtime();
2457    }
2458
2459    public void setOnMessagePacketReceivedListener(final OnMessagePacketReceived listener) {
2460        this.messageListener = listener;
2461    }
2462
2463    public void setOnUnregisteredIqPacketReceivedListener(final OnIqPacketReceived listener) {
2464        this.unregisteredIqListener = listener;
2465    }
2466
2467    public void setOnPresencePacketReceivedListener(final OnPresencePacketReceived listener) {
2468        this.presenceListener = listener;
2469    }
2470
2471    public void setOnJinglePacketReceivedListener(final OnJinglePacketReceived listener) {
2472        this.jingleListener = listener;
2473    }
2474
2475    public void setOnStatusChangedListener(final OnStatusChanged listener) {
2476        this.statusListener = listener;
2477    }
2478
2479    public void setOnBindListener(final OnBindListener listener) {
2480        this.bindListener = listener;
2481    }
2482
2483    public void setOnMessageAcknowledgeListener(final OnMessageAcknowledged listener) {
2484        this.acknowledgedListener = listener;
2485    }
2486
2487    public void addOnAdvancedStreamFeaturesAvailableListener(
2488            final OnAdvancedStreamFeaturesLoaded listener) {
2489        this.advancedStreamFeaturesLoadedListeners.add(listener);
2490    }
2491
2492    private void forceCloseSocket() {
2493        FileBackend.close(this.socket);
2494        FileBackend.close(this.tagReader);
2495    }
2496
2497    public void interrupt() {
2498        if (this.mThread != null) {
2499            this.mThread.interrupt();
2500        }
2501    }
2502
2503    public void disconnect(final boolean force) {
2504        interrupt();
2505        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": disconnecting force=" + force);
2506        if (force) {
2507            forceCloseSocket();
2508        } else {
2509            final TagWriter currentTagWriter = this.tagWriter;
2510            if (currentTagWriter.isActive()) {
2511                currentTagWriter.finish();
2512                final Socket currentSocket = this.socket;
2513                final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch;
2514                try {
2515                    currentTagWriter.await(1, TimeUnit.SECONDS);
2516                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": closing stream");
2517                    currentTagWriter.writeTag(Tag.end("stream:stream"));
2518                    if (streamCountDownLatch != null) {
2519                        if (streamCountDownLatch.await(1, TimeUnit.SECONDS)) {
2520                            Log.d(
2521                                    Config.LOGTAG,
2522                                    account.getJid().asBareJid() + ": remote ended stream");
2523                        } else {
2524                            Log.d(
2525                                    Config.LOGTAG,
2526                                    account.getJid().asBareJid()
2527                                            + ": remote has not closed socket. force closing");
2528                        }
2529                    }
2530                } catch (InterruptedException e) {
2531                    Log.d(
2532                            Config.LOGTAG,
2533                            account.getJid().asBareJid()
2534                                    + ": interrupted while gracefully closing stream");
2535                } catch (final IOException e) {
2536                    Log.d(
2537                            Config.LOGTAG,
2538                            account.getJid().asBareJid()
2539                                    + ": io exception during disconnect ("
2540                                    + e.getMessage()
2541                                    + ")");
2542                } finally {
2543                    FileBackend.close(currentSocket);
2544                }
2545            } else {
2546                forceCloseSocket();
2547            }
2548        }
2549    }
2550
2551    private void resetStreamId() {
2552        this.streamId = null;
2553        this.boundStreamFeatures = null;
2554    }
2555
2556    private List<Entry<Jid, ServiceDiscoveryResult>> findDiscoItemsByFeature(final String feature) {
2557        synchronized (this.disco) {
2558            final List<Entry<Jid, ServiceDiscoveryResult>> items = new ArrayList<>();
2559            for (final Entry<Jid, ServiceDiscoveryResult> cursor : this.disco.entrySet()) {
2560                if (cursor.getValue().getFeatures().contains(feature)) {
2561                    items.add(cursor);
2562                }
2563            }
2564            return items;
2565        }
2566    }
2567
2568    public Jid findDiscoItemByFeature(final String feature) {
2569        final List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(feature);
2570        if (items.size() >= 1) {
2571            return items.get(0).getKey();
2572        }
2573        return null;
2574    }
2575
2576    public boolean r() {
2577        if (getFeatures().sm()) {
2578            this.tagWriter.writeStanzaAsync(new RequestPacket());
2579            return true;
2580        } else {
2581            return false;
2582        }
2583    }
2584
2585    public List<String> getMucServersWithholdAccount() {
2586        final List<String> servers = getMucServers();
2587        servers.remove(account.getDomain().toEscapedString());
2588        return servers;
2589    }
2590
2591    public List<String> getMucServers() {
2592        List<String> servers = new ArrayList<>();
2593        synchronized (this.disco) {
2594            for (final Entry<Jid, ServiceDiscoveryResult> cursor : disco.entrySet()) {
2595                final ServiceDiscoveryResult value = cursor.getValue();
2596                if (value.getFeatures().contains("http://jabber.org/protocol/muc")
2597                        && value.hasIdentity("conference", "text")
2598                        && !value.getFeatures().contains("jabber:iq:gateway")
2599                        && !value.hasIdentity("conference", "irc")) {
2600                    servers.add(cursor.getKey().toString());
2601                }
2602            }
2603        }
2604        return servers;
2605    }
2606
2607    public String getMucServer() {
2608        List<String> servers = getMucServers();
2609        return servers.size() > 0 ? servers.get(0) : null;
2610    }
2611
2612    public int getTimeToNextAttempt(final boolean aggressive) {
2613        final int interval;
2614        if (aggressive) {
2615            interval = Math.min((int) (3 * Math.pow(1.3, attempt)), 60);
2616        } else {
2617            final int additionalTime =
2618                    account.getLastErrorStatus() == Account.State.POLICY_VIOLATION ? 3 : 0;
2619            interval = Math.min((int) (25 * Math.pow(1.3, (additionalTime + attempt))), 300);
2620        }
2621        final int secondsSinceLast =
2622                (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
2623        return interval - secondsSinceLast;
2624    }
2625
2626    public int getAttempt() {
2627        return this.attempt;
2628    }
2629
2630    public Features getFeatures() {
2631        return this.features;
2632    }
2633
2634    public long getLastSessionEstablished() {
2635        final long diff = SystemClock.elapsedRealtime() - this.lastSessionStarted;
2636        return System.currentTimeMillis() - diff;
2637    }
2638
2639    public long getLastConnect() {
2640        return this.lastConnect;
2641    }
2642
2643    public long getLastPingSent() {
2644        return this.lastPingSent;
2645    }
2646
2647    public long getLastDiscoStarted() {
2648        return this.lastDiscoStarted;
2649    }
2650
2651    public long getLastPacketReceived() {
2652        return this.lastPacketReceived;
2653    }
2654
2655    public void sendActive() {
2656        this.sendPacket(new ActivePacket());
2657    }
2658
2659    public void sendInactive() {
2660        this.sendPacket(new InactivePacket());
2661    }
2662
2663    public void resetAttemptCount(boolean resetConnectTime) {
2664        this.attempt = 0;
2665        if (resetConnectTime) {
2666            this.lastConnect = 0;
2667        }
2668    }
2669
2670    public void setInteractive(boolean interactive) {
2671        this.mInteractive = interactive;
2672    }
2673
2674    private IqGenerator getIqGenerator() {
2675        return mXmppConnectionService.getIqGenerator();
2676    }
2677
2678    private class MyKeyManager implements X509KeyManager {
2679        @Override
2680        public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) {
2681            return account.getPrivateKeyAlias();
2682        }
2683
2684        @Override
2685        public String chooseServerAlias(String s, Principal[] principals, Socket socket) {
2686            return null;
2687        }
2688
2689        @Override
2690        public X509Certificate[] getCertificateChain(String alias) {
2691            Log.d(Config.LOGTAG, "getting certificate chain");
2692            try {
2693                return KeyChain.getCertificateChain(mXmppConnectionService, alias);
2694            } catch (final Exception e) {
2695                Log.d(Config.LOGTAG, "could not get certificate chain", e);
2696                return new X509Certificate[0];
2697            }
2698        }
2699
2700        @Override
2701        public String[] getClientAliases(String s, Principal[] principals) {
2702            final String alias = account.getPrivateKeyAlias();
2703            return alias != null ? new String[] {alias} : new String[0];
2704        }
2705
2706        @Override
2707        public String[] getServerAliases(String s, Principal[] principals) {
2708            return new String[0];
2709        }
2710
2711        @Override
2712        public PrivateKey getPrivateKey(String alias) {
2713            try {
2714                return KeyChain.getPrivateKey(mXmppConnectionService, alias);
2715            } catch (Exception e) {
2716                return null;
2717            }
2718        }
2719    }
2720
2721    private static class LoginInfo {
2722        public final SaslMechanism saslMechanism;
2723        public final SaslMechanism.Version saslVersion;
2724        public final List<String> inlineBindFeatures;
2725
2726        private LoginInfo(
2727                final SaslMechanism saslMechanism,
2728                final SaslMechanism.Version saslVersion,
2729                final Collection<String> inlineBindFeatures) {
2730            Preconditions.checkNotNull(saslMechanism, "SASL Mechanism must not be null");
2731            Preconditions.checkNotNull(saslVersion, "SASL version must not be null");
2732            this.saslMechanism = saslMechanism;
2733            this.saslVersion = saslVersion;
2734            this.inlineBindFeatures =
2735                    inlineBindFeatures == null
2736                            ? Collections.emptyList()
2737                            : ImmutableList.copyOf(inlineBindFeatures);
2738        }
2739
2740        public static SaslMechanism mechanism(final LoginInfo loginInfo) {
2741            return loginInfo == null ? null : loginInfo.saslMechanism;
2742        }
2743    }
2744
2745    private static class StreamId {
2746        public final String id;
2747        public final Resolver.Result location;
2748
2749        private StreamId(String id, Resolver.Result location) {
2750            this.id = id;
2751            this.location = location;
2752        }
2753
2754        @NonNull
2755        @Override
2756        public String toString() {
2757            return MoreObjects.toStringHelper(this)
2758                    .add("id", id)
2759                    .add("location", location)
2760                    .toString();
2761        }
2762    }
2763
2764    private static class StateChangingError extends Error {
2765        private final Account.State state;
2766
2767        public StateChangingError(Account.State state) {
2768            this.state = state;
2769        }
2770    }
2771
2772    private static class StateChangingException extends IOException {
2773        private final Account.State state;
2774
2775        public StateChangingException(Account.State state) {
2776            this.state = state;
2777        }
2778    }
2779
2780    public class Features {
2781        XmppConnection connection;
2782        private boolean carbonsEnabled = false;
2783        private boolean encryptionEnabled = false;
2784        private boolean blockListRequested = false;
2785
2786        public Features(final XmppConnection connection) {
2787            this.connection = connection;
2788        }
2789
2790        private boolean hasDiscoFeature(final Jid server, final String feature) {
2791            synchronized (XmppConnection.this.disco) {
2792                final ServiceDiscoveryResult sdr = connection.disco.get(server);
2793                return sdr != null && sdr.getFeatures().contains(feature);
2794            }
2795        }
2796
2797        public boolean carbons() {
2798            return hasDiscoFeature(account.getDomain(), Namespace.CARBONS);
2799        }
2800
2801        public boolean commands() {
2802            return hasDiscoFeature(account.getDomain(), Namespace.COMMANDS);
2803        }
2804
2805        public boolean easyOnboardingInvites() {
2806            synchronized (commands) {
2807                return commands.containsKey(Namespace.EASY_ONBOARDING_INVITE);
2808            }
2809        }
2810
2811        public boolean bookmarksConversion() {
2812            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.BOOKMARKS_CONVERSION)
2813                    && pepPublishOptions();
2814        }
2815
2816        public boolean avatarConversion() {
2817            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.AVATAR_CONVERSION)
2818                    && pepPublishOptions();
2819        }
2820
2821        public boolean blocking() {
2822            return hasDiscoFeature(account.getDomain(), Namespace.BLOCKING);
2823        }
2824
2825        public boolean spamReporting() {
2826            return hasDiscoFeature(account.getDomain(), Namespace.REPORTING);
2827        }
2828
2829        public boolean flexibleOfflineMessageRetrieval() {
2830            return hasDiscoFeature(
2831                    account.getDomain(), Namespace.FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL);
2832        }
2833
2834        public boolean register() {
2835            return hasDiscoFeature(account.getDomain(), Namespace.REGISTER);
2836        }
2837
2838        public boolean invite() {
2839            return connection.streamFeatures != null
2840                    && connection.streamFeatures.hasChild("register", Namespace.INVITE);
2841        }
2842
2843        public boolean sm() {
2844            return streamId != null
2845                    || (connection.streamFeatures != null
2846                            && connection.streamFeatures.hasChild(
2847                                    "sm", Namespace.STREAM_MANAGEMENT));
2848        }
2849
2850        public boolean csi() {
2851            return connection.streamFeatures != null
2852                    && connection.streamFeatures.hasChild("csi", Namespace.CSI);
2853        }
2854
2855        public boolean pep() {
2856            synchronized (XmppConnection.this.disco) {
2857                ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid());
2858                return info != null && info.hasIdentity("pubsub", "pep");
2859            }
2860        }
2861
2862        public boolean pepPersistent() {
2863            synchronized (XmppConnection.this.disco) {
2864                ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid());
2865                return info != null
2866                        && info.getFeatures()
2867                                .contains("http://jabber.org/protocol/pubsub#persistent-items");
2868            }
2869        }
2870
2871        public boolean pepPublishOptions() {
2872            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.PUBSUB_PUBLISH_OPTIONS);
2873        }
2874
2875        public boolean pepOmemoWhitelisted() {
2876            return hasDiscoFeature(
2877                    account.getJid().asBareJid(), AxolotlService.PEP_OMEMO_WHITELISTED);
2878        }
2879
2880        public boolean mam() {
2881            return MessageArchiveService.Version.has(getAccountFeatures());
2882        }
2883
2884        public List<String> getAccountFeatures() {
2885            ServiceDiscoveryResult result = connection.disco.get(account.getJid().asBareJid());
2886            return result == null ? Collections.emptyList() : result.getFeatures();
2887        }
2888
2889        public boolean push() {
2890            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.PUSH)
2891                    || hasDiscoFeature(account.getDomain(), Namespace.PUSH);
2892        }
2893
2894        public boolean rosterVersioning() {
2895            return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
2896        }
2897
2898        public void setBlockListRequested(boolean value) {
2899            this.blockListRequested = value;
2900        }
2901
2902        public boolean httpUpload(long filesize) {
2903            if (Config.DISABLE_HTTP_UPLOAD) {
2904                return false;
2905            } else {
2906                for (String namespace :
2907                        new String[] {Namespace.HTTP_UPLOAD, Namespace.HTTP_UPLOAD_LEGACY}) {
2908                    List<Entry<Jid, ServiceDiscoveryResult>> items =
2909                            findDiscoItemsByFeature(namespace);
2910                    if (items.size() > 0) {
2911                        try {
2912                            long maxsize =
2913                                    Long.parseLong(
2914                                            items.get(0)
2915                                                    .getValue()
2916                                                    .getExtendedDiscoInformation(
2917                                                            namespace, "max-file-size"));
2918                            if (filesize <= maxsize) {
2919                                return true;
2920                            } else {
2921                                Log.d(
2922                                        Config.LOGTAG,
2923                                        account.getJid().asBareJid()
2924                                                + ": http upload is not available for files with size "
2925                                                + filesize
2926                                                + " (max is "
2927                                                + maxsize
2928                                                + ")");
2929                                return false;
2930                            }
2931                        } catch (Exception e) {
2932                            return true;
2933                        }
2934                    }
2935                }
2936                return false;
2937            }
2938        }
2939
2940        public boolean useLegacyHttpUpload() {
2941            return findDiscoItemByFeature(Namespace.HTTP_UPLOAD) == null
2942                    && findDiscoItemByFeature(Namespace.HTTP_UPLOAD_LEGACY) != null;
2943        }
2944
2945        public long getMaxHttpUploadSize() {
2946            for (String namespace :
2947                    new String[] {Namespace.HTTP_UPLOAD, Namespace.HTTP_UPLOAD_LEGACY}) {
2948                List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(namespace);
2949                if (items.size() > 0) {
2950                    try {
2951                        return Long.parseLong(
2952                                items.get(0)
2953                                        .getValue()
2954                                        .getExtendedDiscoInformation(namespace, "max-file-size"));
2955                    } catch (Exception e) {
2956                        // ignored
2957                    }
2958                }
2959            }
2960            return -1;
2961        }
2962
2963        public boolean stanzaIds() {
2964            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.STANZA_IDS);
2965        }
2966
2967        public boolean bookmarks2() {
2968            return pepPublishOptions()
2969                    && hasDiscoFeature(account.getJid().asBareJid(), Namespace.BOOKMARKS2_COMPAT);
2970        }
2971
2972        public boolean externalServiceDiscovery() {
2973            return hasDiscoFeature(account.getDomain(), Namespace.EXTERNAL_SERVICE_DISCOVERY);
2974        }
2975    }
2976}