XmppConnection.java

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