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