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