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