From e32a927300a96fc59adcabb2ebc16318e67de226 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 4 Jan 2015 12:09:39 +0100 Subject: Use packet callbacks only for IqPackets. Removed unnecessary code --- .../siacs/conversations/xmpp/XmppConnection.java | 104 +++++++-------------- 1 file changed, 35 insertions(+), 69 deletions(-) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index f7f0c346..ff3210a4 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -104,7 +104,7 @@ public class XmppConnection implements Runnable { private long lastConnect = 0; private long lastSessionStarted = 0; private int attempt = 0; - private final Map packetCallbacks = new Hashtable<>(); + private final Map packetCallbacks = new Hashtable<>(); private OnPresencePacketReceived presenceListener = null; private OnJinglePacketReceived jingleListener = null; private OnIqPacketReceived unregisteredIqListener = null; @@ -444,50 +444,24 @@ public class XmppConnection implements Runnable { } } else { if (packetCallbacks.containsKey(packet.getId())) { - if (packetCallbacks.get(packet.getId()) instanceof OnIqPacketReceived) { - ((OnIqPacketReceived) packetCallbacks.get(packet.getId())) - .onIqPacketReceived(account, packet); - } - + packetCallbacks.get(packet.getId()).onIqPacketReceived(account, packet); packetCallbacks.remove(packet.getId()); } else if ((packet.getType() == IqPacket.TYPE_GET || packet .getType() == IqPacket.TYPE_SET) && this.unregisteredIqListener != null) { this.unregisteredIqListener.onIqPacketReceived(account, packet); - } + } } } - private void processMessage(final Tag currentTag) throws XmlPullParserException, - IOException { - final MessagePacket packet = (MessagePacket) processPacket(currentTag, - PACKET_MESSAGE); - final String id = packet.getAttribute("id"); - if ((id != null) && (packetCallbacks.containsKey(id))) { - if (packetCallbacks.get(id) instanceof OnMessagePacketReceived) { - ((OnMessagePacketReceived) packetCallbacks.get(id)) - .onMessagePacketReceived(account, packet); - } - packetCallbacks.remove(id); - } else if (this.messageListener != null) { - this.messageListener.onMessagePacketReceived(account, packet); - } + private void processMessage(final Tag currentTag) throws XmlPullParserException, IOException { + final MessagePacket packet = (MessagePacket) processPacket(currentTag,PACKET_MESSAGE); + this.messageListener.onMessagePacketReceived(account, packet); } - private void processPresence(final Tag currentTag) throws XmlPullParserException, - IOException { - PresencePacket packet = (PresencePacket) processPacket(currentTag, - PACKET_PRESENCE); - final String id = packet.getAttribute("id"); - if ((id != null) && (packetCallbacks.containsKey(id))) { - if (packetCallbacks.get(id) instanceof OnPresencePacketReceived) { - ((OnPresencePacketReceived) packetCallbacks.get(id)) - .onPresencePacketReceived(account, packet); - } - packetCallbacks.remove(id); - } else if (this.presenceListener != null) { - this.presenceListener.onPresencePacketReceived(account, packet); - } + private void processPresence(final Tag currentTag) throws XmlPullParserException, IOException { + PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE); + this.presenceListener.onPresencePacketReceived(account, packet); } private void sendStartTLS() throws IOException { @@ -497,8 +471,7 @@ public class XmppConnection implements Runnable { } private SharedPreferences getPreferences() { - return PreferenceManager - .getDefaultSharedPreferences(applicationContext); + return PreferenceManager.getDefaultSharedPreferences(applicationContext); } private boolean enableLegacySSL() { @@ -676,7 +649,7 @@ public class XmppConnection implements Runnable { final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind") .addChild("resource").setContent(account.getResource()); - this.sendUnboundIqPacket(iq, new OnIqPacketReceived() { + this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() { @Override public void onIqPacketReceived(final Account account, final IqPacket packet) { final Element bind = packet.findChild("bind"); @@ -719,12 +692,10 @@ public class XmppConnection implements Runnable { } }); if (this.streamFeatures.hasChild("session")) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() - + ": sending deprecated session"); + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending deprecated session"); final IqPacket startSession = new IqPacket(IqPacket.TYPE_SET); - startSession.addChild("session", - "urn:ietf:params:xml:ns:xmpp-session"); - this.sendUnboundIqPacket(startSession, null); + startSession.addChild("session","urn:ietf:params:xml:ns:xmpp-session"); + this.sendUnmodifiedIqPacket(startSession, null); } } @@ -845,49 +816,44 @@ public class XmppConnection implements Runnable { return new BigInteger(50, mXmppConnectionService.getRNG()).toString(32); } - public void sendIqPacket(final IqPacket packet, final PacketReceived callback) { - if (packet.getId() == null) { - final String id = nextRandomId(); - packet.setAttribute("id", id); - } + public void sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) { packet.setFrom(account.getJid()); - this.sendPacket(packet, callback); + this.sendUnmodifiedIqPacket(packet,callback); + } - public void sendUnboundIqPacket(final IqPacket packet, final PacketReceived callback) { + private void sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback) { if (packet.getId() == null) { final String id = nextRandomId(); packet.setAttribute("id", id); } - this.sendPacket(packet, callback); + if (callback != null) { + if (packet.getId() == null) { + packet.setId(nextRandomId()); + } + packetCallbacks.put(packet.getId(), callback); + } + this.sendPacket(packet); } public void sendMessagePacket(final MessagePacket packet) { - this.sendPacket(packet, null); + this.sendPacket(packet); } public void sendPresencePacket(final PresencePacket packet) { - this.sendPacket(packet, null); + this.sendPacket(packet); } - private synchronized void sendPacket(final AbstractStanza packet, final PacketReceived callback) { - if (packet.getName().equals("iq") || packet.getName().equals("message") - || packet.getName().equals("presence")) { + private synchronized void sendPacket(final AbstractStanza packet) { + final String name = packet.getName(); + if (name.equals("iq") || name.equals("message") || name.equals("presence")) { ++stanzasSent; - } + } tagWriter.writeStanzaAsync(packet); - if (packet instanceof MessagePacket && packet.getId() != null - && this.streamId != null) { - Log.d(Config.LOGTAG, "request delivery report for stanza " - + stanzasSent); + if (packet instanceof MessagePacket && packet.getId() != null && this.streamId != null) { + Log.d(Config.LOGTAG, "request delivery report for stanza " + stanzasSent); this.messageReceipts.put(stanzasSent, packet.getId()); tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion)); - } - if (callback != null) { - if (packet.getId() == null) { - packet.setId(nextRandomId()); - } - packetCallbacks.put(packet.getId(), callback); } } @@ -1044,11 +1010,11 @@ public class XmppConnection implements Runnable { } public void sendActive() { - this.sendPacket(new ActivePacket(), null); + this.sendPacket(new ActivePacket()); } public void sendInactive() { - this.sendPacket(new InactivePacket(), null); + this.sendPacket(new InactivePacket()); } public class Features { -- cgit v1.2.3 From eb7e683403e9335165341ed65cf949cb60319ebc Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Tue, 30 Dec 2014 08:16:25 -0500 Subject: Make IqPacket type an enum --- .../siacs/conversations/xmpp/XmppConnection.java | 46 +++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index ff3210a4..d954c0d4 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -372,7 +372,7 @@ public class XmppConnection implements Runnable { private void sendInitialPing() { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping"); - final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); + final IqPacket iq = new IqPacket(IqPacket.TYPE.GET); iq.setFrom(account.getJid()); iq.addChild("ping", "urn:xmpp:ping"); this.sendIqPacket(iq, new OnIqPacketReceived() { @@ -446,11 +446,11 @@ public class XmppConnection implements Runnable { if (packetCallbacks.containsKey(packet.getId())) { packetCallbacks.get(packet.getId()).onIqPacketReceived(account, packet); packetCallbacks.remove(packet.getId()); - } else if ((packet.getType() == IqPacket.TYPE_GET || packet - .getType() == IqPacket.TYPE_SET) + } else if ((packet.getType() == IqPacket.TYPE.GET || packet + .getType() == IqPacket.TYPE.SET) && this.unregisteredIqListener != null) { this.unregisteredIqListener.onIqPacketReceived(account, packet); - } + } } } @@ -479,19 +479,19 @@ public class XmppConnection implements Runnable { } private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException { - tagReader.readTag(); - try { + tagReader.readTag(); + try { final SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null,new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},mXmppConnectionService.getRNG()); final SSLSocketFactory factory = sc.getSocketFactory(); final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier()); - final InetAddress address = socket == null ? null : socket.getInetAddress(); + final InetAddress address = socket == null ? null : socket.getInetAddress(); if (factory == null || address == null || verifier == null) { throw new IOException("could not setup ssl"); } - final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true); + final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true); if (sslSocket == null) { throw new IOException("could not initialize ssl socket"); @@ -506,13 +506,13 @@ public class XmppConnection implements Runnable { supportedProtocols.remove("SSLv3"); supportProtocols = new String[supportedProtocols.size()]; supportedProtocols.toArray(supportProtocols); - } + } sslSocket.setEnabledProtocols(supportProtocols); - if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); - disconnect(true); - changeStatus(Account.State.SECURITY_ERROR); + if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) { + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); + disconnect(true); + changeStatus(Account.State.SECURITY_ERROR); } tagReader.setInputStream(sslSocket.getInputStream()); tagWriter.setOutputStream(sslSocket.getOutputStream()); @@ -521,11 +521,11 @@ public class XmppConnection implements Runnable { enabledEncryption = true; processStream(tagReader.readTag()); sslSocket.close(); - } catch (final NoSuchAlgorithmException | KeyManagementException e1) { + } catch (final NoSuchAlgorithmException | KeyManagementException e1) { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); disconnect(true); changeStatus(Account.State.SECURITY_ERROR); - } + } } private void processStreamFeatures(final Tag currentTag) @@ -598,7 +598,7 @@ public class XmppConnection implements Runnable { } private void sendRegistryRequest() { - final IqPacket register = new IqPacket(IqPacket.TYPE_GET); + final IqPacket register = new IqPacket(IqPacket.TYPE.GET); register.query("jabber:iq:register"); register.setTo(account.getServer()); sendIqPacket(register, new OnIqPacketReceived() { @@ -608,7 +608,7 @@ public class XmppConnection implements Runnable { final Element instructions = packet.query().findChild("instructions"); if (packet.query().hasChild("username") && (packet.query().hasChild("password"))) { - final IqPacket register = new IqPacket(IqPacket.TYPE_SET); + final IqPacket register = new IqPacket(IqPacket.TYPE.SET); final Element username = new Element("username") .setContent(account.getUsername()); final Element password = new Element("password") @@ -619,7 +619,7 @@ public class XmppConnection implements Runnable { @Override public void onIqPacketReceived(final Account account, final IqPacket packet) { - if (packet.getType() == IqPacket.TYPE_RESULT) { + if (packet.getType() == IqPacket.TYPE.RESULT) { account.setOption(Account.OPTION_REGISTER, false); changeStatus(Account.State.REGISTRATION_SUCCESSFUL); @@ -646,7 +646,7 @@ public class XmppConnection implements Runnable { } private void sendBindRequest() { - final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); + final IqPacket iq = new IqPacket(IqPacket.TYPE.SET); iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind") .addChild("resource").setContent(account.getResource()); this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() { @@ -705,7 +705,7 @@ public class XmppConnection implements Runnable { enableAdvancedStreamFeatures(); } } else { - final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); + final IqPacket iq = new IqPacket(IqPacket.TYPE.GET); iq.setTo(server.toDomainJid()); iq.query("http://jabber.org/protocol/disco#info"); this.sendIqPacket(iq, new OnIqPacketReceived() { @@ -750,7 +750,7 @@ public class XmppConnection implements Runnable { } private void sendServiceDiscoveryItems(final Jid server) { - final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); + final IqPacket iq = new IqPacket(IqPacket.TYPE.GET); iq.setTo(server.toDomainJid()); iq.query("http://jabber.org/protocol/disco#items"); this.sendIqPacket(iq, new OnIqPacketReceived() { @@ -771,7 +771,7 @@ public class XmppConnection implements Runnable { } private void sendEnableCarbons() { - final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); + final IqPacket iq = new IqPacket(IqPacket.TYPE.SET); iq.addChild("enable", "urn:xmpp:carbons:2"); this.sendIqPacket(iq, new OnIqPacketReceived() { @@ -861,7 +861,7 @@ public class XmppConnection implements Runnable { if (streamFeatures.hasChild("sm")) { tagWriter.writeStanzaAsync(new RequestPacket(smVersion)); } else { - final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); + final IqPacket iq = new IqPacket(IqPacket.TYPE.GET); iq.setFrom(account.getJid()); iq.addChild("ping", "urn:xmpp:ping"); this.sendIqPacket(iq, null); -- cgit v1.2.3 From 88704ce5cd68047008375c2eba6751e2e3cdd8c0 Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Tue, 30 Dec 2014 08:50:51 -0500 Subject: Verify IQ responses Fixes #20 Move fromServer/toServer to AbstractStanza --- .../siacs/conversations/xmpp/XmppConnection.java | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index d954c0d4..56d3e560 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -9,6 +9,7 @@ import android.os.PowerManager.WakeLock; import android.os.SystemClock; import android.preference.PreferenceManager; import android.util.Log; +import android.util.Pair; import android.util.SparseArray; import org.apache.http.conn.ssl.StrictHostnameVerifier; @@ -104,7 +105,7 @@ public class XmppConnection implements Runnable { private long lastConnect = 0; private long lastSessionStarted = 0; private int attempt = 0; - private final Map packetCallbacks = new Hashtable<>(); + private final Map> packetCallbacks = new Hashtable<>(); private OnPresencePacketReceived presenceListener = null; private OnJinglePacketReceived jingleListener = null; private OnIqPacketReceived unregisteredIqListener = null; @@ -444,8 +445,21 @@ public class XmppConnection implements Runnable { } } else { if (packetCallbacks.containsKey(packet.getId())) { - packetCallbacks.get(packet.getId()).onIqPacketReceived(account, packet); - packetCallbacks.remove(packet.getId()); + final Pair packetCallbackDuple = packetCallbacks.get(packet.getId()); + // Packets to the server should have responses from the server + if (packetCallbackDuple.first.toServer(account)) { + if (packet.fromServer(account)) { + packetCallbackDuple.second + .onIqPacketReceived(account, packet); + packetCallbacks.remove(packet.getId()); + } + } else { + if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) { + packetCallbackDuple.second + .onIqPacketReceived(account, packet); + packetCallbacks.remove(packet.getId()); + } + } } else if ((packet.getType() == IqPacket.TYPE.GET || packet .getType() == IqPacket.TYPE.SET) && this.unregisteredIqListener != null) { @@ -693,7 +707,7 @@ public class XmppConnection implements Runnable { }); if (this.streamFeatures.hasChild("session")) { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending deprecated session"); - final IqPacket startSession = new IqPacket(IqPacket.TYPE_SET); + final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET); startSession.addChild("session","urn:ietf:params:xml:ns:xmpp-session"); this.sendUnmodifiedIqPacket(startSession, null); } @@ -831,7 +845,7 @@ public class XmppConnection implements Runnable { if (packet.getId() == null) { packet.setId(nextRandomId()); } - packetCallbacks.put(packet.getId(), callback); + packetCallbacks.put(packet.getId(), new Pair<>(packet, callback)); } this.sendPacket(packet); } -- cgit v1.2.3 From 0d6d09b7e97709f0978a5527454ab6bbd9ad9443 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 4 Jan 2015 15:40:09 +0100 Subject: code cleanup + logging of spoofed iq packets --- .../siacs/conversations/xmpp/XmppConnection.java | 68 +++++++++++----------- 1 file changed, 33 insertions(+), 35 deletions(-) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 56d3e560..714b9a51 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -430,42 +430,40 @@ public class XmppConnection implements Runnable { return element; } - private void processIq(final Tag currentTag) throws XmlPullParserException, - IOException { - final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ); + private void processIq(final Tag currentTag) throws XmlPullParserException, IOException { + final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ); - if (packet.getId() == null) { - return; // an iq packet without id is definitely invalid - } + if (packet.getId() == null) { + return; // an iq packet without id is definitely invalid + } - if (packet instanceof JinglePacket) { - if (this.jingleListener != null) { - this.jingleListener.onJinglePacketReceived(account, - (JinglePacket) packet); - } - } else { - if (packetCallbacks.containsKey(packet.getId())) { - final Pair packetCallbackDuple = packetCallbacks.get(packet.getId()); - // Packets to the server should have responses from the server - if (packetCallbackDuple.first.toServer(account)) { - if (packet.fromServer(account)) { - packetCallbackDuple.second - .onIqPacketReceived(account, packet); - packetCallbacks.remove(packet.getId()); - } - } else { - if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) { - packetCallbackDuple.second - .onIqPacketReceived(account, packet); - packetCallbacks.remove(packet.getId()); - } - } - } else if ((packet.getType() == IqPacket.TYPE.GET || packet - .getType() == IqPacket.TYPE.SET) - && this.unregisteredIqListener != null) { - this.unregisteredIqListener.onIqPacketReceived(account, packet); - } - } + if (packet instanceof JinglePacket) { + if (this.jingleListener != null) { + this.jingleListener.onJinglePacketReceived(account,(JinglePacket) packet); + } + } else { + if (packetCallbacks.containsKey(packet.getId())) { + final Pair packetCallbackDuple = packetCallbacks.get(packet.getId()); + // Packets to the server should have responses from the server + if (packetCallbackDuple.first.toServer(account)) { + if (packet.fromServer(account)) { + packetCallbackDuple.second.onIqPacketReceived(account, packet); + packetCallbacks.remove(packet.getId()); + } else { + Log.e(Config.LOGTAG,account.getJid().toBareJid().toString()+": ignoring spoofed iq packet"); + } + } else { + if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) { + packetCallbackDuple.second.onIqPacketReceived(account, packet); + packetCallbacks.remove(packet.getId()); + } else { + Log.e(Config.LOGTAG,account.getJid().toBareJid().toString()+": ignoring spoofed iq packet"); + } + } + } else if (packet.getType() == IqPacket.TYPE.GET|| packet.getType() == IqPacket.TYPE.SET) { + this.unregisteredIqListener.onIqPacketReceived(account, packet); + } + } } private void processMessage(final Tag currentTag) throws XmlPullParserException, IOException { @@ -836,7 +834,7 @@ public class XmppConnection implements Runnable { } - private void sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback) { + private synchronized void sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback) { if (packet.getId() == null) { final String id = nextRandomId(); packet.setAttribute("id", id); -- cgit v1.2.3 From 85f24c91061ecc6b9d038837cd304164f59135bb Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 5 Jan 2015 16:17:05 +0100 Subject: avoid requesting blocklist after stream resumption --- .../siacs/conversations/xmpp/XmppConnection.java | 50 ++++++++++------------ 1 file changed, 23 insertions(+), 27 deletions(-) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 714b9a51..30437575 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -95,12 +95,9 @@ public class XmppConnection implements Runnable { private int smVersion = 3; private final SparseArray messageReceipts = new SparseArray<>(); - private boolean enabledEncryption = false; - private boolean enabledCarbons = false; - private int stanzasReceived = 0; private int stanzasSent = 0; - private long lastPaketReceived = 0; + private long lastPacketReceived = 0; private long lastPingSent = 0; private long lastConnect = 0; private long lastSessionStarted = 0; @@ -147,13 +144,12 @@ public class XmppConnection implements Runnable { protected void connect() { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting"); - enabledEncryption = false; + features.encryptionEnabled = false; lastConnect = SystemClock.elapsedRealtime(); lastPingSent = SystemClock.elapsedRealtime(); this.attempt++; try { - shouldAuthenticate = shouldBind = !account - .isOptionSet(Account.OPTION_REGISTER); + shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER); tagReader = new XmlReader(wakeLock); tagWriter = new TagWriter(); packetCallbacks.clear(); @@ -304,7 +300,7 @@ public class XmppConnection implements Runnable { final RequestPacket r = new RequestPacket(smVersion); tagWriter.writeStanzaAsync(r); } else if (nextTag.isStart("resumed")) { - lastPaketReceived = SystemClock.elapsedRealtime(); + lastPacketReceived = SystemClock.elapsedRealtime(); final Element resumed = tagReader.readElement(nextTag); final String h = resumed.getAttribute("h"); try { @@ -337,7 +333,7 @@ public class XmppConnection implements Runnable { tagWriter.writeStanzaAsync(ack); } else if (nextTag.isStart("a")) { final Element ack = tagReader.readElement(nextTag); - lastPaketReceived = SystemClock.elapsedRealtime(); + lastPacketReceived = SystemClock.elapsedRealtime(); final int serverSequence = Integer.parseInt(ack.getAttribute("h")); final String msgId = this.messageReceipts.get(serverSequence); if (msgId != null) { @@ -426,7 +422,7 @@ public class XmppConnection implements Runnable { } } ++stanzasReceived; - lastPaketReceived = SystemClock.elapsedRealtime(); + lastPacketReceived = SystemClock.elapsedRealtime(); return element; } @@ -530,7 +526,7 @@ public class XmppConnection implements Runnable { tagWriter.setOutputStream(sslSocket.getOutputStream()); sendStartStream(); Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": TLS connection established"); - enabledEncryption = true; + features.encryptionEnabled = true; processStream(tagReader.readTag()); sslSocket.close(); } catch (final NoSuchAlgorithmException | KeyManagementException e1) { @@ -543,18 +539,18 @@ public class XmppConnection implements Runnable { private void processStreamFeatures(final Tag currentTag) throws XmlPullParserException, IOException { this.streamFeatures = tagReader.readElement(currentTag); - if (this.streamFeatures.hasChild("starttls") && !enabledEncryption) { + if (this.streamFeatures.hasChild("starttls") && !features.encryptionEnabled) { sendStartTLS(); } else if (this.streamFeatures.hasChild("register") && account.isOptionSet(Account.OPTION_REGISTER) - && enabledEncryption) { + && features.encryptionEnabled) { sendRegistryRequest(); } else if (!this.streamFeatures.hasChild("register") && account.isOptionSet(Account.OPTION_REGISTER)) { changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED); disconnect(true); } else if (this.streamFeatures.hasChild("mechanisms") - && shouldAuthenticate && enabledEncryption) { + && shouldAuthenticate && features.encryptionEnabled) { final List mechanisms = extractMechanisms(streamFeatures .findChild("mechanisms")); final Element auth = new Element("auth"); @@ -679,15 +675,15 @@ public class XmppConnection implements Runnable { tagWriter.writeStanzaAsync(enable); stanzasSent = 0; messageReceipts.clear(); - } else if (streamFeatures.hasChild("sm", - "urn:xmpp:sm:2")) { + } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) { smVersion = 2; final EnablePacket enable = new EnablePacket(smVersion); tagWriter.writeStanzaAsync(enable); stanzasSent = 0; messageReceipts.clear(); } - enabledCarbons = false; + features.carbonsEnabled = false; + features.blockListRequested = false; disco.clear(); sendServiceDiscoveryInfo(account.getServer()); sendServiceDiscoveryItems(account.getServer()); @@ -750,12 +746,10 @@ public class XmppConnection implements Runnable { } private void enableAdvancedStreamFeatures() { - if (getFeatures().carbons()) { - if (!enabledCarbons) { - sendEnableCarbons(); - } + if (getFeatures().carbons() && !features.carbonsEnabled) { + sendEnableCarbons(); } - if (getFeatures().blocking()) { + if (getFeatures().blocking() && !features.blockListRequested) { Log.d(Config.LOGTAG, "Requesting block list"); this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser()); } @@ -792,7 +786,7 @@ public class XmppConnection implements Runnable { if (!packet.hasChild("error")) { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": successfully enabled carbons"); - enabledCarbons = true; + features.carbonsEnabled = true; } else { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": error enableing carbons " + packet.toString()); @@ -1018,7 +1012,7 @@ public class XmppConnection implements Runnable { } public long getLastPacketReceived() { - return this.lastPaketReceived; + return this.lastPacketReceived; } public void sendActive() { @@ -1031,6 +1025,9 @@ public class XmppConnection implements Runnable { public class Features { XmppConnection connection; + private boolean carbonsEnabled = false; + private boolean encryptionEnabled = false; + private boolean blockListRequested = false; public Features(final XmppConnection connection) { this.connection = connection; @@ -1078,9 +1075,8 @@ public class XmppConnection implements Runnable { return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver"); } - public boolean streamhost() { - return connection - .findDiscoItemByFeature("http://jabber.org/protocol/bytestreams") != null; + public void setBlockListRequested(boolean value) { + this.blockListRequested = value; } } -- cgit v1.2.3 From 548a585b2c4af9b4c2a98faabb8855fcb260daf2 Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Wed, 14 Jan 2015 12:20:02 -0500 Subject: Harden the TLS connection cipher suites --- src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 30437575..6424b1d5 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -53,6 +53,7 @@ import eu.siacs.conversations.crypto.sasl.ScramSha1; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.generator.IqGenerator; import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.DNSHelper; import eu.siacs.conversations.utils.Xmlns; import eu.siacs.conversations.xml.Element; @@ -514,6 +515,12 @@ public class XmppConnection implements Runnable { supportedProtocols.remove("SSLv3"); supportProtocols = new String[supportedProtocols.size()]; supportedProtocols.toArray(supportProtocols); + + final String[] cipherSuites = CryptoHelper.getSupportedCipherSuites( + sslSocket.getSupportedCipherSuites()); + if (cipherSuites.length > 0) { + sslSocket.setEnabledCipherSuites(cipherSuites); + } } sslSocket.setEnabledProtocols(supportProtocols); -- cgit v1.2.3 From 503237087ffc5585876dfdb96ef167960bef0f15 Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Sat, 17 Jan 2015 13:40:15 -0500 Subject: Remove legacy SSL support --- .../siacs/conversations/xmpp/XmppConnection.java | 26 +++++++++------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 6424b1d5..c16ac32b 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -507,23 +507,19 @@ public class XmppConnection implements Runnable { } final String[] supportProtocols; - if (enableLegacySSL()) { - supportProtocols = sslSocket.getSupportedProtocols(); - } else { - final Collection supportedProtocols = new LinkedList<>( - Arrays.asList(sslSocket.getSupportedProtocols())); - supportedProtocols.remove("SSLv3"); - supportProtocols = new String[supportedProtocols.size()]; - supportedProtocols.toArray(supportProtocols); - - final String[] cipherSuites = CryptoHelper.getSupportedCipherSuites( - sslSocket.getSupportedCipherSuites()); - if (cipherSuites.length > 0) { - sslSocket.setEnabledCipherSuites(cipherSuites); - } - } + final Collection supportedProtocols = new LinkedList<>( + Arrays.asList(sslSocket.getSupportedProtocols())); + supportedProtocols.remove("SSLv3"); + supportProtocols = supportedProtocols.toArray(new String[supportedProtocols.size()]); + sslSocket.setEnabledProtocols(supportProtocols); + final String[] cipherSuites = CryptoHelper.getSupportedCipherSuites( + sslSocket.getSupportedCipherSuites()); + if (cipherSuites.length > 0) { + sslSocket.setEnabledCipherSuites(cipherSuites); + } + if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); disconnect(true); -- cgit v1.2.3 From 3a52f318248134a49dda7d6704ba43bc91784318 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Thu, 29 Jan 2015 23:35:31 +0100 Subject: don't count when attempt failed completly --- src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java') diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index c16ac32b..19e271b2 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -229,6 +229,7 @@ public class XmppConnection implements Runnable { } catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage()); this.changeStatus(Account.State.OFFLINE); + this.attempt--; //don't count attempt when reconnecting instantly anyway } finally { if (wakeLock.isHeld()) { try { -- cgit v1.2.3