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 Element bound = success.findChild("bound", Namespace.BIND2);
 762            final Element resumed = success.findChild("resumed", Namespace.STREAM_MANAGEMENT);
 763            final Element failed = success.findChild("failed", Namespace.STREAM_MANAGEMENT);
 764            final Element tokenWrapper = success.findChild("token", Namespace.FAST);
 765            final String token = tokenWrapper == null ? null : tokenWrapper.getAttribute("token");
 766            if (bound != null && resumed != null) {
 767                Log.d(
 768                        Config.LOGTAG,
 769                        account.getJid().asBareJid()
 770                                + ": server sent bound and resumed in SASL2 success");
 771                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 772            }
 773            final boolean processNopStreamFeatures;
 774            if (resumed != null && streamId != null) {
 775                processResumed(resumed);
 776            } else if (failed != null) {
 777                processFailed(failed, false); // wait for new stream features
 778            }
 779            if (bound != null) {
 780                clearIqCallbacks();
 781                this.isBound = true;
 782                final Element streamManagementEnabled =
 783                        bound.findChild("enabled", Namespace.STREAM_MANAGEMENT);
 784                final Element carbonsEnabled = bound.findChild("enabled", Namespace.CARBONS);
 785                final boolean waitForDisco;
 786                if (streamManagementEnabled != null) {
 787                    processEnabled(streamManagementEnabled);
 788                    waitForDisco = true;
 789                } else {
 790                    //if we did not enable stream management in bind do it now
 791                    waitForDisco = enableStreamManagement();
 792                }
 793                if (carbonsEnabled != null) {
 794                    Log.d(
 795                            Config.LOGTAG,
 796                            account.getJid().asBareJid() + ": successfully enabled carbons");
 797                    features.carbonsEnabled = true;
 798                }
 799                sendPostBindInitialization(waitForDisco, carbonsEnabled != null);
 800                processNopStreamFeatures = true;
 801            } else {
 802                processNopStreamFeatures = false;
 803            }
 804            final HashedToken.Mechanism tokenMechanism;
 805            if (SaslMechanism.hashedToken(currentSaslMechanism)) {
 806                tokenMechanism = ((HashedToken) currentSaslMechanism).getTokenMechanism();
 807            } else if (this.hashTokenRequest != null) {
 808                tokenMechanism = this.hashTokenRequest;
 809            } else {
 810                tokenMechanism = null;
 811            }
 812            if (tokenMechanism != null && !Strings.isNullOrEmpty(token)) {
 813                this.account.setFastToken(tokenMechanism, token);
 814                Log.d(
 815                        Config.LOGTAG,
 816                        account.getJid().asBareJid() + ": storing hashed token " + tokenMechanism);
 817            } else if (this.hashTokenRequest != null) {
 818                Log.w(
 819                        Config.LOGTAG,
 820                        account.getJid().asBareJid()
 821                                + ": no response to our hashed token request "
 822                                + this.hashTokenRequest);
 823            }
 824            // a successful resume will not send stream features
 825            if (processNopStreamFeatures) {
 826                processNopStreamFeatures();
 827            }
 828        }
 829        mXmppConnectionService.databaseBackend.updateAccount(account);
 830        this.quickStartInProgress = false;
 831        if (version == SaslMechanism.Version.SASL) {
 832            tagReader.reset();
 833            sendStartStream(false, true);
 834            final Tag tag = tagReader.readTag();
 835            if (tag != null && tag.isStart("stream", Namespace.STREAMS)) {
 836                processStream();
 837                return true;
 838            } else {
 839                throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
 840            }
 841        } else {
 842            return false;
 843        }
 844    }
 845
 846    private void processNopStreamFeatures() throws IOException {
 847        final Tag tag = tagReader.readTag();
 848        if (tag != null && tag.isStart("features", Namespace.STREAMS)) {
 849            this.streamFeatures = tagReader.readElement(tag);
 850            Log.d(
 851                    Config.LOGTAG,
 852                    account.getJid().asBareJid()
 853                            + ": processed NOP stream features after success: "
 854                            + XmlHelper.printElementNames(this.streamFeatures));
 855        } else {
 856            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received " + tag);
 857            Log.d(
 858                    Config.LOGTAG,
 859                    account.getJid().asBareJid()
 860                            + ": server did not send stream features after SASL2 success");
 861            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 862        }
 863    }
 864
 865    private void processFailure(final Element failure) throws IOException {
 866        final SaslMechanism.Version version;
 867        try {
 868            version = SaslMechanism.Version.of(failure);
 869        } catch (final IllegalArgumentException e) {
 870            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 871        }
 872        Log.d(Config.LOGTAG, failure.toString());
 873        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": login failure " + version);
 874        if (SaslMechanism.hashedToken(this.saslMechanism)) {
 875            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resetting token");
 876            account.resetFastToken();
 877            mXmppConnectionService.databaseBackend.updateAccount(account);
 878        }
 879        if (failure.hasChild("temporary-auth-failure")) {
 880            throw new StateChangingException(Account.State.TEMPORARY_AUTH_FAILURE);
 881        } else if (failure.hasChild("account-disabled")) {
 882            final String text = failure.findChildContent("text");
 883            if (Strings.isNullOrEmpty(text)) {
 884                throw new StateChangingException(Account.State.UNAUTHORIZED);
 885            }
 886            final Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(text);
 887            if (matcher.find()) {
 888                final HttpUrl url;
 889                try {
 890                    url = HttpUrl.get(text.substring(matcher.start(), matcher.end()));
 891                } catch (final IllegalArgumentException e) {
 892                    throw new StateChangingException(Account.State.UNAUTHORIZED);
 893                }
 894                if (url.isHttps()) {
 895                    this.redirectionUrl = url;
 896                    throw new StateChangingException(Account.State.PAYMENT_REQUIRED);
 897                }
 898            }
 899        }
 900        if (SaslMechanism.hashedToken(this.saslMechanism)) {
 901            Log.d(
 902                    Config.LOGTAG,
 903                    account.getJid().asBareJid()
 904                            + ": fast authentication failed. falling back to regular authentication");
 905            authenticate();
 906        } else {
 907            throw new StateChangingException(Account.State.UNAUTHORIZED);
 908        }
 909    }
 910
 911    private static SSLSocket sslSocketOrNull(final Socket socket) {
 912        if (socket instanceof SSLSocket) {
 913            return (SSLSocket) socket;
 914        } else {
 915            return null;
 916        }
 917    }
 918
 919    private void processEnabled(final Element enabled) {
 920        final String streamId;
 921        if (enabled.getAttributeAsBoolean("resume")) {
 922            streamId = enabled.getAttribute("id");
 923            Log.d(
 924                    Config.LOGTAG,
 925                    account.getJid().asBareJid().toString()
 926                            + ": stream management enabled (resumable)");
 927        } else {
 928            Log.d(
 929                    Config.LOGTAG,
 930                    account.getJid().asBareJid().toString() + ": stream management enabled");
 931            streamId = null;
 932        }
 933        this.streamId = streamId;
 934        this.stanzasReceived = 0;
 935        this.inSmacksSession = true;
 936        final RequestPacket r = new RequestPacket();
 937        tagWriter.writeStanzaAsync(r);
 938    }
 939
 940    private void processResumed(final Element resumed) throws StateChangingException {
 941        this.inSmacksSession = true;
 942        this.isBound = true;
 943        this.tagWriter.writeStanzaAsync(new RequestPacket());
 944        lastPacketReceived = SystemClock.elapsedRealtime();
 945        final String h = resumed.getAttribute("h");
 946        if (h == null) {
 947            resetStreamId();
 948            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 949        }
 950        final int serverCount;
 951        try {
 952            serverCount = Integer.parseInt(h);
 953        } catch (final NumberFormatException e) {
 954            resetStreamId();
 955            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 956        }
 957        final ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
 958        final boolean acknowledgedMessages;
 959        synchronized (this.mStanzaQueue) {
 960            if (serverCount < stanzasSent) {
 961                Log.d(
 962                        Config.LOGTAG,
 963                        account.getJid().asBareJid() + ": session resumed with lost packages");
 964                stanzasSent = serverCount;
 965            } else {
 966                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": session resumed");
 967            }
 968            acknowledgedMessages = acknowledgeStanzaUpTo(serverCount);
 969            for (int i = 0; i < this.mStanzaQueue.size(); ++i) {
 970                failedStanzas.add(mStanzaQueue.valueAt(i));
 971            }
 972            mStanzaQueue.clear();
 973        }
 974        if (acknowledgedMessages) {
 975            mXmppConnectionService.updateConversationUi();
 976        }
 977        Log.d(
 978                Config.LOGTAG,
 979                account.getJid().asBareJid() + ": resending " + failedStanzas.size() + " stanzas");
 980        for (final AbstractAcknowledgeableStanza packet : failedStanzas) {
 981            if (packet instanceof MessagePacket) {
 982                MessagePacket message = (MessagePacket) packet;
 983                mXmppConnectionService.markMessage(
 984                        account,
 985                        message.getTo().asBareJid(),
 986                        message.getId(),
 987                        Message.STATUS_UNSEND);
 988            }
 989            sendPacket(packet);
 990        }
 991        changeStatusToOnline();
 992    }
 993
 994    private void changeStatusToOnline() {
 995        Log.d(
 996                Config.LOGTAG,
 997                account.getJid().asBareJid() + ": online with resource " + account.getResource());
 998        changeStatus(Account.State.ONLINE);
 999    }
1000
1001    private void processFailed(final Element failed, final boolean sendBindRequest) {
1002        final int serverCount;
1003        try {
1004            serverCount = Integer.parseInt(failed.getAttribute("h"));
1005        } catch (final NumberFormatException | NullPointerException e) {
1006            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed");
1007            resetStreamId();
1008            if (sendBindRequest) {
1009                sendBindRequest();
1010            }
1011            return;
1012        }
1013        Log.d(
1014                Config.LOGTAG,
1015                account.getJid().asBareJid()
1016                        + ": resumption failed but server acknowledged stanza #"
1017                        + serverCount);
1018        final boolean acknowledgedMessages;
1019        synchronized (this.mStanzaQueue) {
1020            acknowledgedMessages = acknowledgeStanzaUpTo(serverCount);
1021        }
1022        if (acknowledgedMessages) {
1023            mXmppConnectionService.updateConversationUi();
1024        }
1025        resetStreamId();
1026        if (sendBindRequest) {
1027            sendBindRequest();
1028        }
1029    }
1030
1031    private boolean acknowledgeStanzaUpTo(int serverCount) {
1032        if (serverCount > stanzasSent) {
1033            Log.e(
1034                    Config.LOGTAG,
1035                    "server acknowledged more stanzas than we sent. serverCount="
1036                            + serverCount
1037                            + ", ourCount="
1038                            + stanzasSent);
1039        }
1040        boolean acknowledgedMessages = false;
1041        for (int i = 0; i < mStanzaQueue.size(); ++i) {
1042            if (serverCount >= mStanzaQueue.keyAt(i)) {
1043                if (Config.EXTENDED_SM_LOGGING) {
1044                    Log.d(
1045                            Config.LOGTAG,
1046                            account.getJid().asBareJid()
1047                                    + ": server acknowledged stanza #"
1048                                    + mStanzaQueue.keyAt(i));
1049                }
1050                final AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
1051                if (stanza instanceof MessagePacket && acknowledgedListener != null) {
1052                    final MessagePacket packet = (MessagePacket) stanza;
1053                    final String id = packet.getId();
1054                    final Jid to = packet.getTo();
1055                    if (id != null && to != null) {
1056                        acknowledgedMessages |=
1057                                acknowledgedListener.onMessageAcknowledged(account, to, id);
1058                    }
1059                }
1060                mStanzaQueue.removeAt(i);
1061                i--;
1062            }
1063        }
1064        return acknowledgedMessages;
1065    }
1066
1067    private @NonNull Element processPacket(final Tag currentTag, final int packetType)
1068            throws IOException {
1069        final Element element;
1070        switch (packetType) {
1071            case PACKET_IQ:
1072                element = new IqPacket();
1073                break;
1074            case PACKET_MESSAGE:
1075                element = new MessagePacket();
1076                break;
1077            case PACKET_PRESENCE:
1078                element = new PresencePacket();
1079                break;
1080            default:
1081                throw new AssertionError("Should never encounter invalid type");
1082        }
1083        element.setAttributes(currentTag.getAttributes());
1084        Tag nextTag = tagReader.readTag();
1085        if (nextTag == null) {
1086            throw new IOException("interrupted mid tag");
1087        }
1088        while (!nextTag.isEnd(element.getName())) {
1089            if (!nextTag.isNo()) {
1090                element.addChild(tagReader.readElement(nextTag));
1091            }
1092            nextTag = tagReader.readTag();
1093            if (nextTag == null) {
1094                throw new IOException("interrupted mid tag");
1095            }
1096        }
1097        if (stanzasReceived == Integer.MAX_VALUE) {
1098            resetStreamId();
1099            throw new IOException("time to restart the session. cant handle >2 billion pcks");
1100        }
1101        if (inSmacksSession) {
1102            ++stanzasReceived;
1103        } else if (features.sm()) {
1104            Log.d(
1105                    Config.LOGTAG,
1106                    account.getJid().asBareJid()
1107                            + ": not counting stanza("
1108                            + element.getClass().getSimpleName()
1109                            + "). Not in smacks session.");
1110        }
1111        lastPacketReceived = SystemClock.elapsedRealtime();
1112        if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
1113            Log.d(Config.LOGTAG, "[background stanza] " + element);
1114        }
1115        if (element instanceof IqPacket
1116                && (((IqPacket) element).getType() == IqPacket.TYPE.SET)
1117                && element.hasChild("jingle", Namespace.JINGLE)) {
1118            return JinglePacket.upgrade((IqPacket) element);
1119        } else {
1120            return element;
1121        }
1122    }
1123
1124    private void processIq(final Tag currentTag) throws IOException {
1125        final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
1126        if (!packet.valid()) {
1127            Log.e(
1128                    Config.LOGTAG,
1129                    "encountered invalid iq from='"
1130                            + packet.getFrom()
1131                            + "' to='"
1132                            + packet.getTo()
1133                            + "'");
1134            return;
1135        }
1136        if (packet instanceof JinglePacket) {
1137            if (this.jingleListener != null) {
1138                this.jingleListener.onJinglePacketReceived(account, (JinglePacket) packet);
1139            }
1140        } else {
1141            OnIqPacketReceived callback = null;
1142            synchronized (this.packetCallbacks) {
1143                final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple =
1144                        packetCallbacks.get(packet.getId());
1145                if (packetCallbackDuple != null) {
1146                    // Packets to the server should have responses from the server
1147                    if (packetCallbackDuple.first.toServer(account)) {
1148                        if (packet.fromServer(account)) {
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                    } else {
1158                        if (packet.getFrom() != null
1159                                && packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
1160                            callback = packetCallbackDuple.second;
1161                            packetCallbacks.remove(packet.getId());
1162                        } else {
1163                            Log.e(
1164                                    Config.LOGTAG,
1165                                    account.getJid().asBareJid().toString()
1166                                            + ": ignoring spoofed iq packet");
1167                        }
1168                    }
1169                } else if (packet.getType() == IqPacket.TYPE.GET
1170                        || packet.getType() == IqPacket.TYPE.SET) {
1171                    callback = this.unregisteredIqListener;
1172                }
1173            }
1174            if (callback != null) {
1175                try {
1176                    callback.onIqPacketReceived(account, packet);
1177                } catch (StateChangingError error) {
1178                    throw new StateChangingException(error.state);
1179                }
1180            }
1181        }
1182    }
1183
1184    private void processMessage(final Tag currentTag) throws IOException {
1185        final MessagePacket packet = (MessagePacket) processPacket(currentTag, PACKET_MESSAGE);
1186        if (!packet.valid()) {
1187            Log.e(
1188                    Config.LOGTAG,
1189                    "encountered invalid message from='"
1190                            + packet.getFrom()
1191                            + "' to='"
1192                            + packet.getTo()
1193                            + "'");
1194            return;
1195        }
1196        this.messageListener.onMessagePacketReceived(account, packet);
1197    }
1198
1199    private void processPresence(final Tag currentTag) throws IOException {
1200        PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE);
1201        if (!packet.valid()) {
1202            Log.e(
1203                    Config.LOGTAG,
1204                    "encountered invalid presence from='"
1205                            + packet.getFrom()
1206                            + "' to='"
1207                            + packet.getTo()
1208                            + "'");
1209            return;
1210        }
1211        this.presenceListener.onPresencePacketReceived(account, packet);
1212    }
1213
1214    private void sendStartTLS() throws IOException {
1215        final Tag startTLS = Tag.empty("starttls");
1216        startTLS.setAttribute("xmlns", Namespace.TLS);
1217        tagWriter.writeTag(startTLS);
1218    }
1219
1220    private void switchOverToTls() throws XmlPullParserException, IOException {
1221        tagReader.readTag();
1222        final Socket socket = this.socket;
1223        final SSLSocket sslSocket = upgradeSocketToTls(socket);
1224        tagReader.setInputStream(sslSocket.getInputStream());
1225        tagWriter.setOutputStream(sslSocket.getOutputStream());
1226        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS connection established");
1227        final boolean quickStart;
1228        try {
1229            quickStart = establishStream(SSLSockets.version(sslSocket));
1230        } catch (final InterruptedException e) {
1231            return;
1232        }
1233        if (quickStart) {
1234            this.quickStartInProgress = true;
1235        }
1236        features.encryptionEnabled = true;
1237        final Tag tag = tagReader.readTag();
1238        if (tag != null && tag.isStart("stream", Namespace.STREAMS)) {
1239            SSLSockets.log(account, sslSocket);
1240            processStream();
1241        } else {
1242            throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
1243        }
1244        sslSocket.close();
1245    }
1246
1247    private SSLSocket upgradeSocketToTls(final Socket socket) throws IOException {
1248        final SSLSocketFactory sslSocketFactory;
1249        try {
1250            sslSocketFactory = getSSLSocketFactory();
1251        } catch (final NoSuchAlgorithmException | KeyManagementException e) {
1252            throw new StateChangingException(Account.State.TLS_ERROR);
1253        }
1254        final InetAddress address = socket.getInetAddress();
1255        final SSLSocket sslSocket =
1256                (SSLSocket)
1257                        sslSocketFactory.createSocket(
1258                                socket, address.getHostAddress(), socket.getPort(), true);
1259        SSLSockets.setSecurity(sslSocket);
1260        SSLSockets.setHostname(sslSocket, IDN.toASCII(account.getServer()));
1261        SSLSockets.setApplicationProtocol(sslSocket, "xmpp-client");
1262        final XmppDomainVerifier xmppDomainVerifier = new XmppDomainVerifier();
1263        try {
1264            if (!xmppDomainVerifier.verify(
1265                    account.getServer(), this.verifiedHostname, sslSocket.getSession())) {
1266                Log.d(
1267                        Config.LOGTAG,
1268                        account.getJid().asBareJid()
1269                                + ": TLS certificate domain verification failed");
1270                FileBackend.close(sslSocket);
1271                throw new StateChangingException(Account.State.TLS_ERROR_DOMAIN);
1272            }
1273        } catch (final SSLPeerUnverifiedException e) {
1274            FileBackend.close(sslSocket);
1275            throw new StateChangingException(Account.State.TLS_ERROR);
1276        }
1277        return sslSocket;
1278    }
1279
1280    private void processStreamFeatures(final Tag currentTag) throws IOException {
1281        this.streamFeatures = tagReader.readElement(currentTag);
1282        final boolean isSecure = isSecure();
1283        final boolean needsBinding = !isBound && !account.isOptionSet(Account.OPTION_REGISTER);
1284        if (this.quickStartInProgress) {
1285            if (this.streamFeatures.hasChild("authentication", Namespace.SASL_2)) {
1286                Log.d(
1287                        Config.LOGTAG,
1288                        account.getJid().asBareJid()
1289                                + ": quick start in progress. ignoring features: "
1290                                + XmlHelper.printElementNames(this.streamFeatures));
1291                if (SaslMechanism.hashedToken(this.saslMechanism)) {
1292                    return;
1293                }
1294                if (isFastTokenAvailable(
1295                        this.streamFeatures.findChild("authentication", Namespace.SASL_2))) {
1296                    Log.d(
1297                            Config.LOGTAG,
1298                            account.getJid().asBareJid()
1299                                    + ": fast token available; resetting quick start");
1300                    account.setOption(Account.OPTION_QUICKSTART_AVAILABLE, false);
1301                    mXmppConnectionService.databaseBackend.updateAccount(account);
1302                }
1303                return;
1304            }
1305            Log.d(
1306                    Config.LOGTAG,
1307                    account.getJid().asBareJid()
1308                            + ": server lost support for SASL 2. quick start not possible");
1309            this.account.setOption(Account.OPTION_QUICKSTART_AVAILABLE, false);
1310            mXmppConnectionService.databaseBackend.updateAccount(account);
1311            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1312        }
1313        if (this.streamFeatures.hasChild("starttls", Namespace.TLS)
1314                && !features.encryptionEnabled) {
1315            sendStartTLS();
1316        } else if (this.streamFeatures.hasChild("register", Namespace.REGISTER_STREAM_FEATURE)
1317                && account.isOptionSet(Account.OPTION_REGISTER)) {
1318            if (isSecure) {
1319                register();
1320            } else {
1321                Log.d(
1322                        Config.LOGTAG,
1323                        account.getJid().asBareJid()
1324                                + ": unable to find STARTTLS for registration process "
1325                                + XmlHelper.printElementNames(this.streamFeatures));
1326                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1327            }
1328        } else if (!this.streamFeatures.hasChild("register", Namespace.REGISTER_STREAM_FEATURE)
1329                && account.isOptionSet(Account.OPTION_REGISTER)) {
1330            throw new StateChangingException(Account.State.REGISTRATION_NOT_SUPPORTED);
1331        } else if (this.streamFeatures.hasChild("authentication", Namespace.SASL_2)
1332                && shouldAuthenticate
1333                && isSecure) {
1334            authenticate(SaslMechanism.Version.SASL_2);
1335        } else if (this.streamFeatures.hasChild("mechanisms", Namespace.SASL)
1336                && shouldAuthenticate
1337                && isSecure) {
1338            authenticate(SaslMechanism.Version.SASL);
1339        } else if (this.streamFeatures.hasChild("sm", Namespace.STREAM_MANAGEMENT)
1340                && streamId != null
1341                && !inSmacksSession) {
1342            if (Config.EXTENDED_SM_LOGGING) {
1343                Log.d(
1344                        Config.LOGTAG,
1345                        account.getJid().asBareJid()
1346                                + ": resuming after stanza #"
1347                                + stanzasReceived);
1348            }
1349            final ResumePacket resume = new ResumePacket(this.streamId, stanzasReceived);
1350            this.mSmCatchupMessageCounter.set(0);
1351            this.mWaitingForSmCatchup.set(true);
1352            this.tagWriter.writeStanzaAsync(resume);
1353        } else if (needsBinding) {
1354            if (this.streamFeatures.hasChild("bind", Namespace.BIND) && isSecure) {
1355                sendBindRequest();
1356            } else {
1357                Log.d(
1358                        Config.LOGTAG,
1359                        account.getJid().asBareJid()
1360                                + ": unable to find bind feature "
1361                                + XmlHelper.printElementNames(this.streamFeatures));
1362                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1363            }
1364        } else {
1365            Log.d(
1366                    Config.LOGTAG,
1367                    account.getJid().asBareJid()
1368                            + ": received NOP stream features: "
1369                            + XmlHelper.printElementNames(this.streamFeatures));
1370        }
1371    }
1372
1373    private void authenticate() throws IOException {
1374        final boolean isSecure = isSecure();
1375        if (isSecure && this.streamFeatures.hasChild("authentication", Namespace.SASL_2)) {authenticate(SaslMechanism.Version.SASL_2);
1376        } else if (isSecure && this.streamFeatures.hasChild("mechanisms", Namespace.SASL)) {
1377            authenticate(SaslMechanism.Version.SASL);
1378        } else {
1379            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1380        }
1381    }
1382
1383    private boolean isSecure() {
1384        return features.encryptionEnabled || Config.ALLOW_NON_TLS_CONNECTIONS || account.isOnion();
1385    }
1386
1387    private void authenticate(final SaslMechanism.Version version) throws IOException {
1388        final Element authElement;
1389        if (version == SaslMechanism.Version.SASL) {
1390            authElement = this.streamFeatures.findChild("mechanisms", Namespace.SASL);
1391        } else {
1392            authElement = this.streamFeatures.findChild("authentication", Namespace.SASL_2);
1393        }
1394        final Collection<String> mechanisms = SaslMechanism.mechanisms(authElement);
1395        final Element cbElement =
1396                this.streamFeatures.findChild("sasl-channel-binding", Namespace.CHANNEL_BINDING);
1397        final Collection<ChannelBinding> channelBindings = ChannelBinding.of(cbElement);
1398        final SaslMechanism.Factory factory = new SaslMechanism.Factory(account);
1399        final SaslMechanism saslMechanism = factory.of(mechanisms, channelBindings, version, SSLSockets.version(this.socket));
1400        this.saslMechanism = validate(saslMechanism, mechanisms);
1401        final boolean quickStartAvailable;
1402        final String firstMessage = this.saslMechanism.getClientFirstMessage(sslSocketOrNull(this.socket));
1403        final boolean usingFast = SaslMechanism.hashedToken(this.saslMechanism);
1404        final Element authenticate;
1405        if (version == SaslMechanism.Version.SASL) {
1406            authenticate = new Element("auth", Namespace.SASL);
1407            if (!Strings.isNullOrEmpty(firstMessage)) {
1408                authenticate.setContent(firstMessage);
1409            }
1410            quickStartAvailable = false;
1411        } else if (version == SaslMechanism.Version.SASL_2) {
1412            final Element inline = authElement.findChild("inline", Namespace.SASL_2);
1413            final boolean sm = inline != null && inline.hasChild("sm", Namespace.STREAM_MANAGEMENT);
1414            final HashedToken.Mechanism hashTokenRequest;
1415            if (usingFast) {
1416                hashTokenRequest = null;
1417            } else {
1418                final Element fast = inline == null ? null : inline.findChild("fast", Namespace.FAST);
1419                final Collection<String> fastMechanisms = SaslMechanism.mechanisms(fast);
1420                hashTokenRequest =
1421                        HashedToken.Mechanism.best(fastMechanisms, SSLSockets.version(this.socket));
1422            }
1423            final Collection<String> bindFeatures = Bind2.features(inline);
1424            quickStartAvailable =
1425                    sm
1426                            && bindFeatures != null
1427                            && bindFeatures.containsAll(Bind2.QUICKSTART_FEATURES);
1428            if (bindFeatures != null) {
1429                try {
1430                    mXmppConnectionService.restoredFromDatabaseLatch.await();
1431                } catch (final InterruptedException e) {
1432                    Log.d(
1433                            Config.LOGTAG,
1434                            account.getJid().asBareJid()
1435                                    + ": interrupted while waiting for DB restore during SASL2 bind");
1436                    return;
1437                }
1438            }
1439            this.hashTokenRequest = hashTokenRequest;
1440            authenticate = generateAuthenticationRequest(firstMessage, usingFast, hashTokenRequest, bindFeatures, sm);
1441        } else {
1442            throw new AssertionError("Missing implementation for " + version);
1443        }
1444
1445        if (account.setOption(Account.OPTION_QUICKSTART_AVAILABLE, quickStartAvailable)) {
1446            mXmppConnectionService.databaseBackend.updateAccount(account);
1447        }
1448
1449        Log.d(
1450                Config.LOGTAG,
1451                account.getJid().toString()
1452                        + ": Authenticating with "
1453                        + version
1454                        + "/"
1455                        + this.saslMechanism.getMechanism());
1456        authenticate.setAttribute("mechanism", this.saslMechanism.getMechanism());
1457        tagWriter.writeElement(authenticate);
1458    }
1459
1460    private static boolean isFastTokenAvailable(final Element authentication) {
1461        final Element inline = authentication == null ? null : authentication.findChild("inline");
1462        return inline != null && inline.hasChild("fast", Namespace.FAST);
1463    }
1464
1465    @NonNull
1466    private SaslMechanism validate(final @Nullable SaslMechanism saslMechanism, Collection<String> mechanisms) throws StateChangingException {
1467        if (saslMechanism == null) {
1468            Log.d(
1469                    Config.LOGTAG,
1470                    account.getJid().asBareJid()
1471                            + ": unable to find supported SASL mechanism in "
1472                            + mechanisms);
1473            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
1474        }
1475        if (SaslMechanism.hashedToken(saslMechanism)) {
1476            return saslMechanism;
1477        }
1478        final int pinnedMechanism = account.getPinnedMechanismPriority();
1479        if (pinnedMechanism > saslMechanism.getPriority()) {
1480            Log.e(
1481                    Config.LOGTAG,
1482                    "Auth failed. Authentication mechanism "
1483                            + saslMechanism.getMechanism()
1484                            + " has lower priority ("
1485                            + saslMechanism.getPriority()
1486                            + ") than pinned priority ("
1487                            + pinnedMechanism
1488                            + "). Possible downgrade attack?");
1489            throw new StateChangingException(Account.State.DOWNGRADE_ATTACK);
1490        }
1491        return saslMechanism;
1492    }
1493
1494    private Element generateAuthenticationRequest(final String firstMessage, final boolean usingFast) {
1495        return generateAuthenticationRequest(firstMessage, usingFast, null, Bind2.QUICKSTART_FEATURES, true);
1496    }
1497
1498    private Element generateAuthenticationRequest(
1499            final String firstMessage,
1500            final boolean usingFast,
1501            final HashedToken.Mechanism hashedTokenRequest,
1502            final Collection<String> bind,
1503            final boolean inlineStreamManagement) {
1504        final Element authenticate = new Element("authenticate", Namespace.SASL_2);
1505        if (!Strings.isNullOrEmpty(firstMessage)) {
1506            authenticate.addChild("initial-response").setContent(firstMessage);
1507        }
1508        final Element userAgent = authenticate.addChild("user-agent");
1509        userAgent.setAttribute("id", account.getUuid());
1510        userAgent
1511                .addChild("software")
1512                .setContent(mXmppConnectionService.getString(R.string.app_name));
1513        if (!PhoneHelper.isEmulator()) {
1514            userAgent
1515                    .addChild("device")
1516                    .setContent(String.format("%s %s", Build.MANUFACTURER, Build.MODEL));
1517        }
1518        if (bind != null) {
1519            authenticate.addChild(generateBindRequest(bind));
1520        }
1521        if (inlineStreamManagement && streamId != null) {
1522            final ResumePacket resume = new ResumePacket(this.streamId, stanzasReceived);
1523            this.mSmCatchupMessageCounter.set(0);
1524            this.mWaitingForSmCatchup.set(true);
1525            authenticate.addChild(resume);
1526        }
1527        if (hashedTokenRequest != null) {
1528            authenticate
1529                    .addChild("request-token", Namespace.FAST)
1530                    .setAttribute("mechanism", hashedTokenRequest.name());
1531        }
1532        if (usingFast) {
1533            authenticate.addChild("fast", Namespace.FAST);
1534        }
1535        return authenticate;
1536    }
1537
1538    private Element generateBindRequest(final Collection<String> bindFeatures) {
1539        Log.d(Config.LOGTAG, "inline bind features: " + bindFeatures);
1540        final Element bind = new Element("bind", Namespace.BIND2);
1541        bind.addChild("tag").setContent(mXmppConnectionService.getString(R.string.app_name));
1542        if (bindFeatures.contains(Namespace.CARBONS)) {
1543            bind.addChild("enable", Namespace.CARBONS);
1544        }
1545        if (bindFeatures.contains(Namespace.STREAM_MANAGEMENT)) {
1546            bind.addChild(new EnablePacket());
1547        }
1548        return bind;
1549    }
1550
1551    private void register() {
1552        final String preAuth = account.getKey(Account.KEY_PRE_AUTH_REGISTRATION_TOKEN);
1553        if (preAuth != null && features.invite()) {
1554            final IqPacket preAuthRequest = new IqPacket(IqPacket.TYPE.SET);
1555            preAuthRequest.addChild("preauth", Namespace.PARS).setAttribute("token", preAuth);
1556            sendUnmodifiedIqPacket(
1557                    preAuthRequest,
1558                    (account, response) -> {
1559                        if (response.getType() == IqPacket.TYPE.RESULT) {
1560                            sendRegistryRequest();
1561                        } else {
1562                            final String error = response.getErrorCondition();
1563                            Log.d(
1564                                    Config.LOGTAG,
1565                                    account.getJid().asBareJid()
1566                                            + ": failed to pre auth. "
1567                                            + error);
1568                            throw new StateChangingError(Account.State.REGISTRATION_INVALID_TOKEN);
1569                        }
1570                    },
1571                    true);
1572        } else {
1573            sendRegistryRequest();
1574        }
1575    }
1576
1577    private void sendRegistryRequest() {
1578        final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
1579        register.query(Namespace.REGISTER);
1580        register.setTo(account.getDomain());
1581        sendUnmodifiedIqPacket(
1582                register,
1583                (account, packet) -> {
1584                    if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1585                        return;
1586                    }
1587                    if (packet.getType() == IqPacket.TYPE.ERROR) {
1588                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1589                    }
1590                    final Element query = packet.query(Namespace.REGISTER);
1591                    if (query.hasChild("username") && (query.hasChild("password"))) {
1592                        final IqPacket register1 = new IqPacket(IqPacket.TYPE.SET);
1593                        final Element username =
1594                                new Element("username").setContent(account.getUsername());
1595                        final Element password =
1596                                new Element("password").setContent(account.getPassword());
1597                        register1.query(Namespace.REGISTER).addChild(username);
1598                        register1.query().addChild(password);
1599                        register1.setFrom(account.getJid().asBareJid());
1600                        sendUnmodifiedIqPacket(register1, registrationResponseListener, true);
1601                    } else if (query.hasChild("x", Namespace.DATA)) {
1602                        final Data data = Data.parse(query.findChild("x", Namespace.DATA));
1603                        final Element blob = query.findChild("data", "urn:xmpp:bob");
1604                        final String id = packet.getId();
1605                        InputStream is;
1606                        if (blob != null) {
1607                            try {
1608                                final String base64Blob = blob.getContent();
1609                                final byte[] strBlob = Base64.decode(base64Blob, Base64.DEFAULT);
1610                                is = new ByteArrayInputStream(strBlob);
1611                            } catch (Exception e) {
1612                                is = null;
1613                            }
1614                        } else {
1615                            final boolean useTor =
1616                                    mXmppConnectionService.useTorToConnect() || account.isOnion();
1617                            try {
1618                                final String url = data.getValue("url");
1619                                final String fallbackUrl = data.getValue("captcha-fallback-url");
1620                                if (url != null) {
1621                                    is = HttpConnectionManager.open(url, useTor);
1622                                } else if (fallbackUrl != null) {
1623                                    is = HttpConnectionManager.open(fallbackUrl, useTor);
1624                                } else {
1625                                    is = null;
1626                                }
1627                            } catch (final IOException e) {
1628                                Log.d(
1629                                        Config.LOGTAG,
1630                                        account.getJid().asBareJid() + ": unable to fetch captcha",
1631                                        e);
1632                                is = null;
1633                            }
1634                        }
1635
1636                        if (is != null) {
1637                            Bitmap captcha = BitmapFactory.decodeStream(is);
1638                            try {
1639                                if (mXmppConnectionService.displayCaptchaRequest(
1640                                        account, id, data, captcha)) {
1641                                    return;
1642                                }
1643                            } catch (Exception e) {
1644                                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1645                            }
1646                        }
1647                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1648                    } else if (query.hasChild("instructions")
1649                            || query.hasChild("x", Namespace.OOB)) {
1650                        final String instructions = query.findChildContent("instructions");
1651                        final Element oob = query.findChild("x", Namespace.OOB);
1652                        final String url = oob == null ? null : oob.findChildContent("url");
1653                        if (url != null) {
1654                            setAccountCreationFailed(url);
1655                        } else if (instructions != null) {
1656                            final Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(instructions);
1657                            if (matcher.find()) {
1658                                setAccountCreationFailed(
1659                                        instructions.substring(matcher.start(), matcher.end()));
1660                            }
1661                        }
1662                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1663                    }
1664                },
1665                true);
1666    }
1667
1668    private void setAccountCreationFailed(final String url) {
1669        final HttpUrl httpUrl = url == null ? null : HttpUrl.parse(url);
1670        if (httpUrl != null && httpUrl.isHttps()) {
1671            this.redirectionUrl = httpUrl;
1672            throw new StateChangingError(Account.State.REGISTRATION_WEB);
1673        }
1674        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1675    }
1676
1677    public HttpUrl getRedirectionUrl() {
1678        return this.redirectionUrl;
1679    }
1680
1681    public void resetEverything() {
1682        resetAttemptCount(true);
1683        resetStreamId();
1684        clearIqCallbacks();
1685        this.stanzasSent = 0;
1686        mStanzaQueue.clear();
1687        this.redirectionUrl = null;
1688        synchronized (this.disco) {
1689            disco.clear();
1690        }
1691        synchronized (this.commands) {
1692            this.commands.clear();
1693        }
1694        this.saslMechanism = null;
1695    }
1696
1697    private void sendBindRequest() {
1698        try {
1699            mXmppConnectionService.restoredFromDatabaseLatch.await();
1700        } catch (InterruptedException e) {
1701            Log.d(
1702                    Config.LOGTAG,
1703                    account.getJid().asBareJid()
1704                            + ": interrupted while waiting for DB restore during bind");
1705            return;
1706        }
1707        clearIqCallbacks();
1708        if (account.getJid().isBareJid()) {
1709            account.setResource(this.createNewResource());
1710        } else {
1711            fixResource(mXmppConnectionService, account);
1712        }
1713        final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
1714        final String resource =
1715                Config.USE_RANDOM_RESOURCE_ON_EVERY_BIND ? nextRandomId() : account.getResource();
1716        iq.addChild("bind", Namespace.BIND).addChild("resource").setContent(resource);
1717        this.sendUnmodifiedIqPacket(
1718                iq,
1719                (account, packet) -> {
1720                    if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1721                        return;
1722                    }
1723                    final Element bind = packet.findChild("bind");
1724                    if (bind != null && packet.getType() == IqPacket.TYPE.RESULT) {
1725                        isBound = true;
1726                        final Element jid = bind.findChild("jid");
1727                        if (jid != null && jid.getContent() != null) {
1728                            try {
1729                                Jid assignedJid = Jid.ofEscaped(jid.getContent());
1730                                if (!account.getJid().getDomain().equals(assignedJid.getDomain())) {
1731                                    Log.d(
1732                                            Config.LOGTAG,
1733                                            account.getJid().asBareJid()
1734                                                    + ": server tried to re-assign domain to "
1735                                                    + assignedJid.getDomain());
1736                                    throw new StateChangingError(Account.State.BIND_FAILURE);
1737                                }
1738                                if (account.setJid(assignedJid)) {
1739                                    Log.d(
1740                                            Config.LOGTAG,
1741                                            account.getJid().asBareJid()
1742                                                    + ": jid changed during bind. updating database");
1743                                    mXmppConnectionService.databaseBackend.updateAccount(account);
1744                                }
1745                                if (streamFeatures.hasChild("session")
1746                                        && !streamFeatures
1747                                                .findChild("session")
1748                                                .hasChild("optional")) {
1749                                    sendStartSession();
1750                                } else {
1751                                    final boolean waitForDisco = enableStreamManagement();
1752                                    sendPostBindInitialization(waitForDisco, false);
1753                                }
1754                                return;
1755                            } catch (final IllegalArgumentException e) {
1756                                Log.d(
1757                                        Config.LOGTAG,
1758                                        account.getJid().asBareJid()
1759                                                + ": server reported invalid jid ("
1760                                                + jid.getContent()
1761                                                + ") on bind");
1762                            }
1763                        } else {
1764                            Log.d(
1765                                    Config.LOGTAG,
1766                                    account.getJid()
1767                                            + ": disconnecting because of bind failure. (no jid)");
1768                        }
1769                    } else {
1770                        Log.d(
1771                                Config.LOGTAG,
1772                                account.getJid()
1773                                        + ": disconnecting because of bind failure ("
1774                                        + packet);
1775                    }
1776                    final Element error = packet.findChild("error");
1777                    if (packet.getType() == IqPacket.TYPE.ERROR
1778                            && error != null
1779                            && error.hasChild("conflict")) {
1780                        account.setResource(createNewResource());
1781                    }
1782                    throw new StateChangingError(Account.State.BIND_FAILURE);
1783                },
1784                true);
1785    }
1786
1787    private void clearIqCallbacks() {
1788        final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.TIMEOUT);
1789        final ArrayList<OnIqPacketReceived> callbacks = new ArrayList<>();
1790        synchronized (this.packetCallbacks) {
1791            if (this.packetCallbacks.size() == 0) {
1792                return;
1793            }
1794            Log.d(
1795                    Config.LOGTAG,
1796                    account.getJid().asBareJid()
1797                            + ": clearing "
1798                            + this.packetCallbacks.size()
1799                            + " iq callbacks");
1800            final Iterator<Pair<IqPacket, OnIqPacketReceived>> iterator =
1801                    this.packetCallbacks.values().iterator();
1802            while (iterator.hasNext()) {
1803                Pair<IqPacket, OnIqPacketReceived> entry = iterator.next();
1804                callbacks.add(entry.second);
1805                iterator.remove();
1806            }
1807        }
1808        for (OnIqPacketReceived callback : callbacks) {
1809            try {
1810                callback.onIqPacketReceived(account, failurePacket);
1811            } catch (StateChangingError error) {
1812                Log.d(
1813                        Config.LOGTAG,
1814                        account.getJid().asBareJid()
1815                                + ": caught StateChangingError("
1816                                + error.state.toString()
1817                                + ") while clearing callbacks");
1818                // ignore
1819            }
1820        }
1821        Log.d(
1822                Config.LOGTAG,
1823                account.getJid().asBareJid()
1824                        + ": done clearing iq callbacks. "
1825                        + this.packetCallbacks.size()
1826                        + " left");
1827    }
1828
1829    public void sendDiscoTimeout() {
1830        if (mWaitForDisco.compareAndSet(true, false)) {
1831            Log.d(
1832                    Config.LOGTAG,
1833                    account.getJid().asBareJid() + ": finalizing bind after disco timeout");
1834            finalizeBind();
1835        }
1836    }
1837
1838    private void sendStartSession() {
1839        Log.d(
1840                Config.LOGTAG,
1841                account.getJid().asBareJid() + ": sending legacy session to outdated server");
1842        final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET);
1843        startSession.addChild("session", "urn:ietf:params:xml:ns:xmpp-session");
1844        this.sendUnmodifiedIqPacket(
1845                startSession,
1846                (account, packet) -> {
1847                    if (packet.getType() == IqPacket.TYPE.RESULT) {
1848                        final boolean waitForDisco = enableStreamManagement();
1849                        sendPostBindInitialization(waitForDisco, false);
1850                    } else if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1851                        throw new StateChangingError(Account.State.SESSION_FAILURE);
1852                    }
1853                },
1854                true);
1855    }
1856
1857    private boolean enableStreamManagement() {
1858        final boolean streamManagement =
1859                this.streamFeatures.hasChild("sm", Namespace.STREAM_MANAGEMENT);
1860        if (streamManagement) {
1861            synchronized (this.mStanzaQueue) {
1862                final EnablePacket enable = new EnablePacket();
1863                tagWriter.writeStanzaAsync(enable);
1864                stanzasSent = 0;
1865                mStanzaQueue.clear();
1866            }
1867            return true;
1868        } else {
1869            return false;
1870        }
1871    }
1872
1873    private void sendPostBindInitialization(
1874            final boolean waitForDisco, final boolean carbonsEnabled) {
1875        features.carbonsEnabled = carbonsEnabled;
1876        features.blockListRequested = false;
1877        synchronized (this.disco) {
1878            this.disco.clear();
1879        }
1880        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": starting service discovery");
1881        mPendingServiceDiscoveries.set(0);
1882        if (!waitForDisco
1883                || Patches.DISCO_EXCEPTIONS.contains(
1884                        account.getJid().getDomain().toEscapedString())) {
1885            Log.d(
1886                    Config.LOGTAG,
1887                    account.getJid().asBareJid() + ": do not wait for service discovery");
1888            mWaitForDisco.set(false);
1889        } else {
1890            mWaitForDisco.set(true);
1891        }
1892        lastDiscoStarted = SystemClock.elapsedRealtime();
1893        mXmppConnectionService.scheduleWakeUpCall(
1894                Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
1895        final Element caps = streamFeatures.findChild("c");
1896        final String hash = caps == null ? null : caps.getAttribute("hash");
1897        final String ver = caps == null ? null : caps.getAttribute("ver");
1898        ServiceDiscoveryResult discoveryResult = null;
1899        if (hash != null && ver != null) {
1900            discoveryResult =
1901                    mXmppConnectionService.getCachedServiceDiscoveryResult(new Pair<>(hash, ver));
1902        }
1903        final boolean requestDiscoItemsFirst =
1904                !account.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY);
1905        if (requestDiscoItemsFirst) {
1906            sendServiceDiscoveryItems(account.getDomain());
1907        }
1908        if (discoveryResult == null) {
1909            sendServiceDiscoveryInfo(account.getDomain());
1910        } else {
1911            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server caps came from cache");
1912            disco.put(account.getDomain(), discoveryResult);
1913        }
1914        discoverMamPreferences();
1915        sendServiceDiscoveryInfo(account.getJid().asBareJid());
1916        if (!requestDiscoItemsFirst) {
1917            sendServiceDiscoveryItems(account.getDomain());
1918        }
1919
1920        if (!mWaitForDisco.get()) {
1921            finalizeBind();
1922        }
1923        this.lastSessionStarted = SystemClock.elapsedRealtime();
1924    }
1925
1926    private void sendServiceDiscoveryInfo(final Jid jid) {
1927        mPendingServiceDiscoveries.incrementAndGet();
1928        final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1929        iq.setTo(jid);
1930        iq.query("http://jabber.org/protocol/disco#info");
1931        this.sendIqPacket(
1932                iq,
1933                (account, packet) -> {
1934                    if (packet.getType() == IqPacket.TYPE.RESULT) {
1935                        boolean advancedStreamFeaturesLoaded;
1936                        synchronized (XmppConnection.this.disco) {
1937                            ServiceDiscoveryResult result = new ServiceDiscoveryResult(packet);
1938                            if (jid.equals(account.getDomain())) {
1939                                mXmppConnectionService.databaseBackend.insertDiscoveryResult(
1940                                        result);
1941                            }
1942                            disco.put(jid, result);
1943                            advancedStreamFeaturesLoaded =
1944                                    disco.containsKey(account.getDomain())
1945                                            && disco.containsKey(account.getJid().asBareJid());
1946                        }
1947                        if (advancedStreamFeaturesLoaded
1948                                && (jid.equals(account.getDomain())
1949                                        || jid.equals(account.getJid().asBareJid()))) {
1950                            enableAdvancedStreamFeatures();
1951                        }
1952                    } else if (packet.getType() == IqPacket.TYPE.ERROR) {
1953                        Log.d(
1954                                Config.LOGTAG,
1955                                account.getJid().asBareJid()
1956                                        + ": could not query disco info for "
1957                                        + jid.toString());
1958                        final boolean serverOrAccount =
1959                                jid.equals(account.getDomain())
1960                                        || jid.equals(account.getJid().asBareJid());
1961                        final boolean advancedStreamFeaturesLoaded;
1962                        if (serverOrAccount) {
1963                            synchronized (XmppConnection.this.disco) {
1964                                disco.put(jid, ServiceDiscoveryResult.empty());
1965                                advancedStreamFeaturesLoaded =
1966                                        disco.containsKey(account.getDomain())
1967                                                && disco.containsKey(account.getJid().asBareJid());
1968                            }
1969                        } else {
1970                            advancedStreamFeaturesLoaded = false;
1971                        }
1972                        if (advancedStreamFeaturesLoaded) {
1973                            enableAdvancedStreamFeatures();
1974                        }
1975                    }
1976                    if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1977                        if (mPendingServiceDiscoveries.decrementAndGet() == 0
1978                                && mWaitForDisco.compareAndSet(true, false)) {
1979                            finalizeBind();
1980                        }
1981                    }
1982                });
1983    }
1984
1985    private void discoverMamPreferences() {
1986        IqPacket request = new IqPacket(IqPacket.TYPE.GET);
1987        request.addChild("prefs", MessageArchiveService.Version.MAM_2.namespace);
1988        sendIqPacket(
1989                request,
1990                (account, response) -> {
1991                    if (response.getType() == IqPacket.TYPE.RESULT) {
1992                        Element prefs =
1993                                response.findChild(
1994                                        "prefs", MessageArchiveService.Version.MAM_2.namespace);
1995                        isMamPreferenceAlways =
1996                                "always"
1997                                        .equals(
1998                                                prefs == null
1999                                                        ? null
2000                                                        : prefs.getAttribute("default"));
2001                    }
2002                });
2003    }
2004
2005    private void discoverCommands() {
2006        final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
2007        request.setTo(account.getDomain());
2008        request.addChild("query", Namespace.DISCO_ITEMS).setAttribute("node", Namespace.COMMANDS);
2009        sendIqPacket(
2010                request,
2011                (account, response) -> {
2012                    if (response.getType() == IqPacket.TYPE.RESULT) {
2013                        final Element query = response.findChild("query", Namespace.DISCO_ITEMS);
2014                        if (query == null) {
2015                            return;
2016                        }
2017                        final HashMap<String, Jid> commands = new HashMap<>();
2018                        for (final Element child : query.getChildren()) {
2019                            if ("item".equals(child.getName())) {
2020                                final String node = child.getAttribute("node");
2021                                final Jid jid = child.getAttributeAsJid("jid");
2022                                if (node != null && jid != null) {
2023                                    commands.put(node, jid);
2024                                }
2025                            }
2026                        }
2027                        synchronized (this.commands) {
2028                            this.commands.clear();
2029                            this.commands.putAll(commands);
2030                        }
2031                    }
2032                });
2033    }
2034
2035    public boolean isMamPreferenceAlways() {
2036        return isMamPreferenceAlways;
2037    }
2038
2039    private void finalizeBind() {
2040        if (bindListener != null) {
2041            bindListener.onBind(account);
2042        }
2043        changeStatusToOnline();
2044    }
2045
2046    private void enableAdvancedStreamFeatures() {
2047        if (getFeatures().blocking() && !features.blockListRequested) {
2048            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Requesting block list");
2049            this.sendIqPacket(
2050                    getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
2051        }
2052        for (final OnAdvancedStreamFeaturesLoaded listener :
2053                advancedStreamFeaturesLoadedListeners) {
2054            listener.onAdvancedStreamFeaturesAvailable(account);
2055        }
2056        if (getFeatures().carbons() && !features.carbonsEnabled) {
2057            sendEnableCarbons();
2058        }
2059        if (getFeatures().commands()) {
2060            discoverCommands();
2061        }
2062    }
2063
2064    private void sendServiceDiscoveryItems(final Jid server) {
2065        mPendingServiceDiscoveries.incrementAndGet();
2066        final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
2067        iq.setTo(server.getDomain());
2068        iq.query("http://jabber.org/protocol/disco#items");
2069        this.sendIqPacket(
2070                iq,
2071                (account, packet) -> {
2072                    if (packet.getType() == IqPacket.TYPE.RESULT) {
2073                        final HashSet<Jid> items = new HashSet<>();
2074                        final List<Element> elements = packet.query().getChildren();
2075                        for (final Element element : elements) {
2076                            if (element.getName().equals("item")) {
2077                                final Jid jid =
2078                                        InvalidJid.getNullForInvalid(
2079                                                element.getAttributeAsJid("jid"));
2080                                if (jid != null && !jid.equals(account.getDomain())) {
2081                                    items.add(jid);
2082                                }
2083                            }
2084                        }
2085                        for (Jid jid : items) {
2086                            sendServiceDiscoveryInfo(jid);
2087                        }
2088                    } else {
2089                        Log.d(
2090                                Config.LOGTAG,
2091                                account.getJid().asBareJid()
2092                                        + ": could not query disco items of "
2093                                        + server);
2094                    }
2095                    if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
2096                        if (mPendingServiceDiscoveries.decrementAndGet() == 0
2097                                && mWaitForDisco.compareAndSet(true, false)) {
2098                            finalizeBind();
2099                        }
2100                    }
2101                });
2102    }
2103
2104    private void sendEnableCarbons() {
2105        final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
2106        iq.addChild("enable", Namespace.CARBONS);
2107        this.sendIqPacket(
2108                iq,
2109                (account, packet) -> {
2110                    if (packet.getType() == IqPacket.TYPE.RESULT) {
2111                        Log.d(
2112                                Config.LOGTAG,
2113                                account.getJid().asBareJid() + ": successfully enabled carbons");
2114                        features.carbonsEnabled = true;
2115                    } else {
2116                        Log.d(
2117                                Config.LOGTAG,
2118                                account.getJid().asBareJid()
2119                                        + ": could not enable carbons "
2120                                        + packet);
2121                    }
2122                });
2123    }
2124
2125    private void processStreamError(final Tag currentTag) throws IOException {
2126        final Element streamError = tagReader.readElement(currentTag);
2127        if (streamError == null) {
2128            return;
2129        }
2130        if (streamError.hasChild("conflict")) {
2131            account.setResource(createNewResource());
2132            Log.d(
2133                    Config.LOGTAG,
2134                    account.getJid().asBareJid()
2135                            + ": switching resource due to conflict ("
2136                            + account.getResource()
2137                            + ")");
2138            throw new IOException();
2139        } else if (streamError.hasChild("host-unknown")) {
2140            throw new StateChangingException(Account.State.HOST_UNKNOWN);
2141        } else if (streamError.hasChild("policy-violation")) {
2142            this.lastConnect = SystemClock.elapsedRealtime();
2143            final String text = streamError.findChildContent("text");
2144            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": policy violation. " + text);
2145            failPendingMessages(text);
2146            throw new StateChangingException(Account.State.POLICY_VIOLATION);
2147        } else {
2148            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": stream error " + streamError);
2149            throw new StateChangingException(Account.State.STREAM_ERROR);
2150        }
2151    }
2152
2153    private void failPendingMessages(final String error) {
2154        synchronized (this.mStanzaQueue) {
2155            for (int i = 0; i < mStanzaQueue.size(); ++i) {
2156                final AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
2157                if (stanza instanceof MessagePacket) {
2158                    final MessagePacket packet = (MessagePacket) stanza;
2159                    final String id = packet.getId();
2160                    final Jid to = packet.getTo();
2161                    mXmppConnectionService.markMessage(
2162                            account, to.asBareJid(), id, Message.STATUS_SEND_FAILED, error);
2163                }
2164            }
2165        }
2166    }
2167
2168    private boolean establishStream(final SSLSockets.Version sslVersion)
2169            throws IOException, InterruptedException {
2170        final SaslMechanism quickStartMechanism =
2171                SaslMechanism.ensureAvailable(account.getQuickStartMechanism(), sslVersion);
2172        final boolean secureConnection = sslVersion != SSLSockets.Version.NONE;
2173        if (secureConnection
2174                && Config.QUICKSTART_ENABLED
2175                && quickStartMechanism != null
2176                && account.isOptionSet(Account.OPTION_QUICKSTART_AVAILABLE)) {
2177            mXmppConnectionService.restoredFromDatabaseLatch.await();
2178            this.saslMechanism = quickStartMechanism;
2179            final boolean usingFast = quickStartMechanism instanceof HashedToken;
2180            final Element authenticate =
2181                    generateAuthenticationRequest(quickStartMechanism.getClientFirstMessage(sslSocketOrNull(this.socket)), usingFast);
2182            authenticate.setAttribute("mechanism", quickStartMechanism.getMechanism());
2183            sendStartStream(true, false);
2184            tagWriter.writeElement(authenticate);
2185            Log.d(
2186                    Config.LOGTAG,
2187                    account.getJid().toString()
2188                            + ": quick start with "
2189                            + quickStartMechanism.getMechanism());
2190            return true;
2191        } else {
2192            sendStartStream(secureConnection, true);
2193            return false;
2194        }
2195    }
2196
2197    private void sendStartStream(final boolean from, final boolean flush) throws IOException {
2198        final Tag stream = Tag.start("stream:stream");
2199        stream.setAttribute("to", account.getServer());
2200        if (from) {
2201            stream.setAttribute("from", account.getJid().asBareJid().toEscapedString());
2202        }
2203        stream.setAttribute("version", "1.0");
2204        stream.setAttribute("xml:lang", LocalizedContent.STREAM_LANGUAGE);
2205        stream.setAttribute("xmlns", "jabber:client");
2206        stream.setAttribute("xmlns:stream", Namespace.STREAMS);
2207        tagWriter.writeTag(stream, flush);
2208    }
2209
2210    private String createNewResource() {
2211        return mXmppConnectionService.getString(R.string.app_name) + '.' + nextRandomId(true);
2212    }
2213
2214    private String nextRandomId() {
2215        return nextRandomId(false);
2216    }
2217
2218    private String nextRandomId(final boolean s) {
2219        return CryptoHelper.random(s ? 3 : 9);
2220    }
2221
2222    public String sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
2223        packet.setFrom(account.getJid());
2224        return this.sendUnmodifiedIqPacket(packet, callback, false);
2225    }
2226
2227    public synchronized String sendUnmodifiedIqPacket(
2228            final IqPacket packet, final OnIqPacketReceived callback, boolean force) {
2229        if (packet.getId() == null) {
2230            packet.setAttribute("id", nextRandomId());
2231        }
2232        if (callback != null) {
2233            synchronized (this.packetCallbacks) {
2234                packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
2235            }
2236        }
2237        this.sendPacket(packet, force);
2238        return packet.getId();
2239    }
2240
2241    public void sendMessagePacket(final MessagePacket packet) {
2242        this.sendPacket(packet);
2243    }
2244
2245    public void sendPresencePacket(final PresencePacket packet) {
2246        this.sendPacket(packet);
2247    }
2248
2249    private synchronized void sendPacket(final AbstractStanza packet) {
2250        sendPacket(packet, false);
2251    }
2252
2253    private synchronized void sendPacket(final AbstractStanza packet, final boolean force) {
2254        if (stanzasSent == Integer.MAX_VALUE) {
2255            resetStreamId();
2256            disconnect(true);
2257            return;
2258        }
2259        synchronized (this.mStanzaQueue) {
2260            if (force || isBound) {
2261                tagWriter.writeStanzaAsync(packet);
2262            } else {
2263                Log.d(
2264                        Config.LOGTAG,
2265                        account.getJid().asBareJid()
2266                                + " do not write stanza to unbound stream "
2267                                + packet.toString());
2268            }
2269            if (packet instanceof AbstractAcknowledgeableStanza) {
2270                AbstractAcknowledgeableStanza stanza = (AbstractAcknowledgeableStanza) packet;
2271
2272                if (this.mStanzaQueue.size() != 0) {
2273                    int currentHighestKey = this.mStanzaQueue.keyAt(this.mStanzaQueue.size() - 1);
2274                    if (currentHighestKey != stanzasSent) {
2275                        throw new AssertionError("Stanza count messed up");
2276                    }
2277                }
2278
2279                ++stanzasSent;
2280                this.mStanzaQueue.append(stanzasSent, stanza);
2281                if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) {
2282                    if (Config.EXTENDED_SM_LOGGING) {
2283                        Log.d(
2284                                Config.LOGTAG,
2285                                account.getJid().asBareJid()
2286                                        + ": requesting ack for message stanza #"
2287                                        + stanzasSent);
2288                    }
2289                    tagWriter.writeStanzaAsync(new RequestPacket());
2290                }
2291            }
2292        }
2293    }
2294
2295    public void sendPing() {
2296        if (!r()) {
2297            final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
2298            iq.setFrom(account.getJid());
2299            iq.addChild("ping", Namespace.PING);
2300            this.sendIqPacket(iq, null);
2301        }
2302        this.lastPingSent = SystemClock.elapsedRealtime();
2303    }
2304
2305    public void setOnMessagePacketReceivedListener(final OnMessagePacketReceived listener) {
2306        this.messageListener = listener;
2307    }
2308
2309    public void setOnUnregisteredIqPacketReceivedListener(final OnIqPacketReceived listener) {
2310        this.unregisteredIqListener = listener;
2311    }
2312
2313    public void setOnPresencePacketReceivedListener(final OnPresencePacketReceived listener) {
2314        this.presenceListener = listener;
2315    }
2316
2317    public void setOnJinglePacketReceivedListener(final OnJinglePacketReceived listener) {
2318        this.jingleListener = listener;
2319    }
2320
2321    public void setOnStatusChangedListener(final OnStatusChanged listener) {
2322        this.statusListener = listener;
2323    }
2324
2325    public void setOnBindListener(final OnBindListener listener) {
2326        this.bindListener = listener;
2327    }
2328
2329    public void setOnMessageAcknowledgeListener(final OnMessageAcknowledged listener) {
2330        this.acknowledgedListener = listener;
2331    }
2332
2333    public void addOnAdvancedStreamFeaturesAvailableListener(
2334            final OnAdvancedStreamFeaturesLoaded listener) {
2335        this.advancedStreamFeaturesLoadedListeners.add(listener);
2336    }
2337
2338    private void forceCloseSocket() {
2339        FileBackend.close(this.socket);
2340        FileBackend.close(this.tagReader);
2341    }
2342
2343    public void interrupt() {
2344        if (this.mThread != null) {
2345            this.mThread.interrupt();
2346        }
2347    }
2348
2349    public void disconnect(final boolean force) {
2350        interrupt();
2351        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": disconnecting force=" + force);
2352        if (force) {
2353            forceCloseSocket();
2354        } else {
2355            final TagWriter currentTagWriter = this.tagWriter;
2356            if (currentTagWriter.isActive()) {
2357                currentTagWriter.finish();
2358                final Socket currentSocket = this.socket;
2359                final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch;
2360                try {
2361                    currentTagWriter.await(1, TimeUnit.SECONDS);
2362                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": closing stream");
2363                    currentTagWriter.writeTag(Tag.end("stream:stream"));
2364                    if (streamCountDownLatch != null) {
2365                        if (streamCountDownLatch.await(1, TimeUnit.SECONDS)) {
2366                            Log.d(
2367                                    Config.LOGTAG,
2368                                    account.getJid().asBareJid() + ": remote ended stream");
2369                        } else {
2370                            Log.d(
2371                                    Config.LOGTAG,
2372                                    account.getJid().asBareJid()
2373                                            + ": remote has not closed socket. force closing");
2374                        }
2375                    }
2376                } catch (InterruptedException e) {
2377                    Log.d(
2378                            Config.LOGTAG,
2379                            account.getJid().asBareJid()
2380                                    + ": interrupted while gracefully closing stream");
2381                } catch (final IOException e) {
2382                    Log.d(
2383                            Config.LOGTAG,
2384                            account.getJid().asBareJid()
2385                                    + ": io exception during disconnect ("
2386                                    + e.getMessage()
2387                                    + ")");
2388                } finally {
2389                    FileBackend.close(currentSocket);
2390                }
2391            } else {
2392                forceCloseSocket();
2393            }
2394        }
2395    }
2396
2397    private void resetStreamId() {
2398        this.streamId = null;
2399    }
2400
2401    private List<Entry<Jid, ServiceDiscoveryResult>> findDiscoItemsByFeature(final String feature) {
2402        synchronized (this.disco) {
2403            final List<Entry<Jid, ServiceDiscoveryResult>> items = new ArrayList<>();
2404            for (final Entry<Jid, ServiceDiscoveryResult> cursor : this.disco.entrySet()) {
2405                if (cursor.getValue().getFeatures().contains(feature)) {
2406                    items.add(cursor);
2407                }
2408            }
2409            return items;
2410        }
2411    }
2412
2413    public Jid findDiscoItemByFeature(final String feature) {
2414        final List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(feature);
2415        if (items.size() >= 1) {
2416            return items.get(0).getKey();
2417        }
2418        return null;
2419    }
2420
2421    public boolean r() {
2422        if (getFeatures().sm()) {
2423            this.tagWriter.writeStanzaAsync(new RequestPacket());
2424            return true;
2425        } else {
2426            return false;
2427        }
2428    }
2429
2430    public List<String> getMucServersWithholdAccount() {
2431        final List<String> servers = getMucServers();
2432        servers.remove(account.getDomain().toEscapedString());
2433        return servers;
2434    }
2435
2436    public List<String> getMucServers() {
2437        List<String> servers = new ArrayList<>();
2438        synchronized (this.disco) {
2439            for (final Entry<Jid, ServiceDiscoveryResult> cursor : disco.entrySet()) {
2440                final ServiceDiscoveryResult value = cursor.getValue();
2441                if (value.getFeatures().contains("http://jabber.org/protocol/muc")
2442                        && value.hasIdentity("conference", "text")
2443                        && !value.getFeatures().contains("jabber:iq:gateway")
2444                        && !value.hasIdentity("conference", "irc")) {
2445                    servers.add(cursor.getKey().toString());
2446                }
2447            }
2448        }
2449        return servers;
2450    }
2451
2452    public String getMucServer() {
2453        List<String> servers = getMucServers();
2454        return servers.size() > 0 ? servers.get(0) : null;
2455    }
2456
2457    public int getTimeToNextAttempt() {
2458        final int additionalTime =
2459                account.getLastErrorStatus() == Account.State.POLICY_VIOLATION ? 3 : 0;
2460        final int interval = Math.min((int) (25 * Math.pow(1.3, (additionalTime + attempt))), 300);
2461        final int secondsSinceLast =
2462                (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
2463        return interval - secondsSinceLast;
2464    }
2465
2466    public int getAttempt() {
2467        return this.attempt;
2468    }
2469
2470    public Features getFeatures() {
2471        return this.features;
2472    }
2473
2474    public long getLastSessionEstablished() {
2475        final long diff = SystemClock.elapsedRealtime() - this.lastSessionStarted;
2476        return System.currentTimeMillis() - diff;
2477    }
2478
2479    public long getLastConnect() {
2480        return this.lastConnect;
2481    }
2482
2483    public long getLastPingSent() {
2484        return this.lastPingSent;
2485    }
2486
2487    public long getLastDiscoStarted() {
2488        return this.lastDiscoStarted;
2489    }
2490
2491    public long getLastPacketReceived() {
2492        return this.lastPacketReceived;
2493    }
2494
2495    public void sendActive() {
2496        this.sendPacket(new ActivePacket());
2497    }
2498
2499    public void sendInactive() {
2500        this.sendPacket(new InactivePacket());
2501    }
2502
2503    public void resetAttemptCount(boolean resetConnectTime) {
2504        this.attempt = 0;
2505        if (resetConnectTime) {
2506            this.lastConnect = 0;
2507        }
2508    }
2509
2510    public void setInteractive(boolean interactive) {
2511        this.mInteractive = interactive;
2512    }
2513
2514    public Identity getServerIdentity() {
2515        synchronized (this.disco) {
2516            ServiceDiscoveryResult result = disco.get(account.getJid().getDomain());
2517            if (result == null) {
2518                return Identity.UNKNOWN;
2519            }
2520            for (final ServiceDiscoveryResult.Identity id : result.getIdentities()) {
2521                if (id.getType().equals("im")
2522                        && id.getCategory().equals("server")
2523                        && id.getName() != null) {
2524                    switch (id.getName()) {
2525                        case "Prosody":
2526                            return Identity.PROSODY;
2527                        case "ejabberd":
2528                            return Identity.EJABBERD;
2529                        case "Slack-XMPP":
2530                            return Identity.SLACK;
2531                    }
2532                }
2533            }
2534        }
2535        return Identity.UNKNOWN;
2536    }
2537
2538    private IqGenerator getIqGenerator() {
2539        return mXmppConnectionService.getIqGenerator();
2540    }
2541
2542    public enum Identity {
2543        FACEBOOK,
2544        SLACK,
2545        EJABBERD,
2546        PROSODY,
2547        NIMBUZZ,
2548        UNKNOWN
2549    }
2550
2551    private class MyKeyManager implements X509KeyManager {
2552        @Override
2553        public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) {
2554            return account.getPrivateKeyAlias();
2555        }
2556
2557        @Override
2558        public String chooseServerAlias(String s, Principal[] principals, Socket socket) {
2559            return null;
2560        }
2561
2562        @Override
2563        public X509Certificate[] getCertificateChain(String alias) {
2564            Log.d(Config.LOGTAG, "getting certificate chain");
2565            try {
2566                return KeyChain.getCertificateChain(mXmppConnectionService, alias);
2567            } catch (final Exception e) {
2568                Log.d(Config.LOGTAG, "could not get certificate chain", e);
2569                return new X509Certificate[0];
2570            }
2571        }
2572
2573        @Override
2574        public String[] getClientAliases(String s, Principal[] principals) {
2575            final String alias = account.getPrivateKeyAlias();
2576            return alias != null ? new String[] {alias} : new String[0];
2577        }
2578
2579        @Override
2580        public String[] getServerAliases(String s, Principal[] principals) {
2581            return new String[0];
2582        }
2583
2584        @Override
2585        public PrivateKey getPrivateKey(String alias) {
2586            try {
2587                return KeyChain.getPrivateKey(mXmppConnectionService, alias);
2588            } catch (Exception e) {
2589                return null;
2590            }
2591        }
2592    }
2593
2594    private static class StateChangingError extends Error {
2595        private final Account.State state;
2596
2597        public StateChangingError(Account.State state) {
2598            this.state = state;
2599        }
2600    }
2601
2602    private static class StateChangingException extends IOException {
2603        private final Account.State state;
2604
2605        public StateChangingException(Account.State state) {
2606            this.state = state;
2607        }
2608    }
2609
2610    public class Features {
2611        XmppConnection connection;
2612        private boolean carbonsEnabled = false;
2613        private boolean encryptionEnabled = false;
2614        private boolean blockListRequested = false;
2615
2616        public Features(final XmppConnection connection) {
2617            this.connection = connection;
2618        }
2619
2620        private boolean hasDiscoFeature(final Jid server, final String feature) {
2621            synchronized (XmppConnection.this.disco) {
2622                final ServiceDiscoveryResult sdr = connection.disco.get(server);
2623                return sdr != null && sdr.getFeatures().contains(feature);
2624            }
2625        }
2626
2627        public boolean carbons() {
2628            return hasDiscoFeature(account.getDomain(), Namespace.CARBONS);
2629        }
2630
2631        public boolean commands() {
2632            return hasDiscoFeature(account.getDomain(), Namespace.COMMANDS);
2633        }
2634
2635        public boolean easyOnboardingInvites() {
2636            synchronized (commands) {
2637                return commands.containsKey(Namespace.EASY_ONBOARDING_INVITE);
2638            }
2639        }
2640
2641        public boolean bookmarksConversion() {
2642            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.BOOKMARKS_CONVERSION)
2643                    && pepPublishOptions();
2644        }
2645
2646        public boolean avatarConversion() {
2647            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.AVATAR_CONVERSION)
2648                    && pepPublishOptions();
2649        }
2650
2651        public boolean blocking() {
2652            return hasDiscoFeature(account.getDomain(), Namespace.BLOCKING);
2653        }
2654
2655        public boolean spamReporting() {
2656            return hasDiscoFeature(account.getDomain(), "urn:xmpp:reporting:reason:spam:0");
2657        }
2658
2659        public boolean flexibleOfflineMessageRetrieval() {
2660            return hasDiscoFeature(
2661                    account.getDomain(), Namespace.FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL);
2662        }
2663
2664        public boolean register() {
2665            return hasDiscoFeature(account.getDomain(), Namespace.REGISTER);
2666        }
2667
2668        public boolean invite() {
2669            return connection.streamFeatures != null
2670                    && connection.streamFeatures.hasChild("register", Namespace.INVITE);
2671        }
2672
2673        public boolean sm() {
2674            return streamId != null
2675                    || (connection.streamFeatures != null
2676                            && connection.streamFeatures.hasChild("sm", Namespace.STREAM_MANAGEMENT));
2677        }
2678
2679        public boolean csi() {
2680            return connection.streamFeatures != null
2681                    && connection.streamFeatures.hasChild("csi", Namespace.CSI);
2682        }
2683
2684        public boolean pep() {
2685            synchronized (XmppConnection.this.disco) {
2686                ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid());
2687                return info != null && info.hasIdentity("pubsub", "pep");
2688            }
2689        }
2690
2691        public boolean pepPersistent() {
2692            synchronized (XmppConnection.this.disco) {
2693                ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid());
2694                return info != null
2695                        && info.getFeatures()
2696                                .contains("http://jabber.org/protocol/pubsub#persistent-items");
2697            }
2698        }
2699
2700        public boolean pepPublishOptions() {
2701            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.PUBSUB_PUBLISH_OPTIONS);
2702        }
2703
2704        public boolean pepOmemoWhitelisted() {
2705            return hasDiscoFeature(
2706                    account.getJid().asBareJid(), AxolotlService.PEP_OMEMO_WHITELISTED);
2707        }
2708
2709        public boolean mam() {
2710            return MessageArchiveService.Version.has(getAccountFeatures());
2711        }
2712
2713        public List<String> getAccountFeatures() {
2714            ServiceDiscoveryResult result = connection.disco.get(account.getJid().asBareJid());
2715            return result == null ? Collections.emptyList() : result.getFeatures();
2716        }
2717
2718        public boolean push() {
2719            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.PUSH)
2720                    || hasDiscoFeature(account.getDomain(), Namespace.PUSH);
2721        }
2722
2723        public boolean rosterVersioning() {
2724            return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
2725        }
2726
2727        public void setBlockListRequested(boolean value) {
2728            this.blockListRequested = value;
2729        }
2730
2731        public boolean httpUpload(long filesize) {
2732            if (Config.DISABLE_HTTP_UPLOAD) {
2733                return false;
2734            } else {
2735                for (String namespace :
2736                        new String[] {Namespace.HTTP_UPLOAD, Namespace.HTTP_UPLOAD_LEGACY}) {
2737                    List<Entry<Jid, ServiceDiscoveryResult>> items =
2738                            findDiscoItemsByFeature(namespace);
2739                    if (items.size() > 0) {
2740                        try {
2741                            long maxsize =
2742                                    Long.parseLong(
2743                                            items.get(0)
2744                                                    .getValue()
2745                                                    .getExtendedDiscoInformation(
2746                                                            namespace, "max-file-size"));
2747                            if (filesize <= maxsize) {
2748                                return true;
2749                            } else {
2750                                Log.d(
2751                                        Config.LOGTAG,
2752                                        account.getJid().asBareJid()
2753                                                + ": http upload is not available for files with size "
2754                                                + filesize
2755                                                + " (max is "
2756                                                + maxsize
2757                                                + ")");
2758                                return false;
2759                            }
2760                        } catch (Exception e) {
2761                            return true;
2762                        }
2763                    }
2764                }
2765                return false;
2766            }
2767        }
2768
2769        public boolean useLegacyHttpUpload() {
2770            return findDiscoItemByFeature(Namespace.HTTP_UPLOAD) == null
2771                    && findDiscoItemByFeature(Namespace.HTTP_UPLOAD_LEGACY) != null;
2772        }
2773
2774        public long getMaxHttpUploadSize() {
2775            for (String namespace :
2776                    new String[] {Namespace.HTTP_UPLOAD, Namespace.HTTP_UPLOAD_LEGACY}) {
2777                List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(namespace);
2778                if (items.size() > 0) {
2779                    try {
2780                        return Long.parseLong(
2781                                items.get(0)
2782                                        .getValue()
2783                                        .getExtendedDiscoInformation(namespace, "max-file-size"));
2784                    } catch (Exception e) {
2785                        // ignored
2786                    }
2787                }
2788            }
2789            return -1;
2790        }
2791
2792        public boolean stanzaIds() {
2793            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.STANZA_IDS);
2794        }
2795
2796        public boolean bookmarks2() {
2797            return Config
2798                    .USE_BOOKMARKS2 /* || hasDiscoFeature(account.getJid().asBareJid(), Namespace.BOOKMARKS2_COMPAT)*/;
2799        }
2800
2801        public boolean externalServiceDiscovery() {
2802            return hasDiscoFeature(account.getDomain(), Namespace.EXTERNAL_SERVICE_DISCOVERY);
2803        }
2804    }
2805}