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