aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/parser/IqParser.java
blob: 189df4a7b4fd676824a75fec047bb97b6a8261b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package eu.siacs.conversations.parser;

import android.support.annotation.NonNull;
import android.util.Base64;
import android.util.Log;
import android.util.Pair;

import org.whispersystems.libaxolotl.IdentityKey;
import org.whispersystems.libaxolotl.ecc.Curve;
import org.whispersystems.libaxolotl.ecc.ECPublicKey;
import org.whispersystems.libaxolotl.state.PreKeyBundle;

import java.io.ByteArrayInputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import eu.siacs.conversations.Config;
import eu.siacs.conversations.crypto.axolotl.AxolotlService;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.Xmlns;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;

public class IqParser extends AbstractParser implements OnIqPacketReceived {

	public IqParser(final XmppConnectionService service) {
		super(service);
	}

	private void rosterItems(final Account account, final Element query) {
		final String version = query.getAttribute("ver");
		if (version != null) {
			account.getRoster().setVersion(version);
		}
		for (final Element item : query.getChildren()) {
			if (item.getName().equals("item")) {
				final Jid jid = item.getAttributeAsJid("jid");
				if (jid == null) {
					continue;
				}
				final String name = item.getAttribute("name");
				final String subscription = item.getAttribute("subscription");
				final Contact contact = account.getRoster().getContact(jid);
				boolean bothPre = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM);
				if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
					contact.setServerName(name);
					contact.parseGroupsFromElement(item);
				}
				if (subscription != null) {
					if (subscription.equals("remove")) {
						contact.resetOption(Contact.Options.IN_ROSTER);
						contact.resetOption(Contact.Options.DIRTY_DELETE);
						contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
					} else {
						contact.setOption(Contact.Options.IN_ROSTER);
						contact.resetOption(Contact.Options.DIRTY_PUSH);
						contact.parseSubscriptionFromElement(item);
					}
				}
				boolean both = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM);
				if ((both != bothPre) && both) {
					Log.d(Config.LOGTAG,account.getJid().toBareJid()+": gained mutual presence subscription with "+contact.getJid());
					AxolotlService axolotlService = account.getAxolotlService();
					if (axolotlService != null) {
						axolotlService.clearErrorsInFetchStatusMap(contact.getJid());
					}
				}
				mXmppConnectionService.getAvatarService().clear(contact);
			}
		}
		mXmppConnectionService.updateConversationUi();
		mXmppConnectionService.updateRosterUi();
	}

	public String avatarData(final IqPacket packet) {
		final Element pubsub = packet.findChild("pubsub",
				"http://jabber.org/protocol/pubsub");
		if (pubsub == null) {
			return null;
		}
		final Element items = pubsub.findChild("items");
		if (items == null) {
			return null;
		}
		return super.avatarData(items);
	}

	public Element getItem(final IqPacket packet) {
		final Element pubsub = packet.findChild("pubsub",
				"http://jabber.org/protocol/pubsub");
		if (pubsub == null) {
			return null;
		}
		final Element items = pubsub.findChild("items");
		if (items == null) {
			return null;
		}
		return items.findChild("item");
	}

	@NonNull
	public Set<Integer> deviceIds(final Element item) {
		Set<Integer> deviceIds = new HashSet<>();
		if (item != null) {
			final Element list = item.findChild("list");
			if (list != null) {
				for (Element device : list.getChildren()) {
					if (!device.getName().equals("device")) {
						continue;
					}
					try {
						Integer id = Integer.valueOf(device.getAttribute("id"));
						deviceIds.add(id);
					} catch (NumberFormatException e) {
						Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Encountered invalid <device> node in PEP ("+e.getMessage()+"):" + device.toString()+ ", skipping...");
						continue;
					}
				}
			}
		}
		return deviceIds;
	}

	public Integer signedPreKeyId(final Element bundle) {
		final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic");
		if(signedPreKeyPublic == null) {
			return null;
		}
		return Integer.valueOf(signedPreKeyPublic.getAttribute("signedPreKeyId"));
	}

	public ECPublicKey signedPreKeyPublic(final Element bundle) {
		ECPublicKey publicKey = null;
		final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic");
		if(signedPreKeyPublic == null) {
			return null;
		}
		try {
			publicKey = Curve.decodePoint(Base64.decode(signedPreKeyPublic.getContent(),Base64.DEFAULT), 0);
		} catch (Throwable e) {
			Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Invalid signedPreKeyPublic in PEP: " + e.getMessage());
		}
		return publicKey;
	}

	public byte[] signedPreKeySignature(final Element bundle) {
		final Element signedPreKeySignature = bundle.findChild("signedPreKeySignature");
		if(signedPreKeySignature == null) {
			return null;
		}
		try {
			return Base64.decode(signedPreKeySignature.getContent(), Base64.DEFAULT);
		} catch (Throwable e) {
			Log.e(Config.LOGTAG,AxolotlService.LOGPREFIX+" : Invalid base64 in signedPreKeySignature");
			return null;
		}
	}

	public IdentityKey identityKey(final Element bundle) {
		IdentityKey identityKey = null;
		final Element identityKeyElement = bundle.findChild("identityKey");
		if(identityKeyElement == null) {
			return null;
		}
		try {
			identityKey = new IdentityKey(Base64.decode(identityKeyElement.getContent(), Base64.DEFAULT), 0);
		} catch (Throwable e) {
			Log.e(Config.LOGTAG,AxolotlService.LOGPREFIX+" : "+"Invalid identityKey in PEP: "+e.getMessage());
		}
		return identityKey;
	}

	public Map<Integer, ECPublicKey> preKeyPublics(final IqPacket packet) {
		Map<Integer, ECPublicKey> preKeyRecords = new HashMap<>();
		Element item = getItem(packet);
		if (item == null) {
			Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Couldn't find <item> in bundle IQ packet: " + packet);
			return null;
		}
		final Element bundleElement = item.findChild("bundle");
		if(bundleElement == null) {
			return null;
		}
		final Element prekeysElement = bundleElement.findChild("prekeys");
		if(prekeysElement == null) {
			Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Couldn't find <prekeys> in bundle IQ packet: " + packet);
			return null;
		}
		for(Element preKeyPublicElement : prekeysElement.getChildren()) {
			if(!preKeyPublicElement.getName().equals("preKeyPublic")){
				Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Encountered unexpected tag in prekeys list: " + preKeyPublicElement);
				continue;
			}
			Integer preKeyId = Integer.valueOf(preKeyPublicElement.getAttribute("preKeyId"));
			try {
				ECPublicKey preKeyPublic = Curve.decodePoint(Base64.decode(preKeyPublicElement.getContent(), Base64.DEFAULT), 0);
				preKeyRecords.put(preKeyId, preKeyPublic);
			} catch (Throwable e) {
				Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Invalid preKeyPublic (ID="+preKeyId+") in PEP: "+ e.getMessage()+", skipping...");
				continue;
			}
		}
		return preKeyRecords;
	}

	public Pair<X509Certificate[],byte[]> verification(final IqPacket packet) {
		Element item = getItem(packet);
		Element verification = item != null ? item.findChild("verification",AxolotlService.PEP_PREFIX) : null;
		Element chain = verification != null ? verification.findChild("chain") : null;
		Element signature = verification != null ? verification.findChild("signature") : null;
		if (chain != null && signature != null) {
			List<Element> certElements = chain.getChildren();
			X509Certificate[] certificates = new X509Certificate[certElements.size()];
			try {
				CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
				int i = 0;
				for(Element cert : certElements) {
					certificates[i] = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(Base64.decode(cert.getContent(),Base64.DEFAULT)));
					++i;
				}
				return new Pair<>(certificates,Base64.decode(signature.getContent(),Base64.DEFAULT));
			} catch (CertificateException e) {
				return null;
			}
		} else {
			return null;
		}
	}

	public PreKeyBundle bundle(final IqPacket bundle) {
		Element bundleItem = getItem(bundle);
		if(bundleItem == null) {
			return null;
		}
		final Element bundleElement = bundleItem.findChild("bundle");
		if(bundleElement == null) {
			return null;
		}
		ECPublicKey signedPreKeyPublic = signedPreKeyPublic(bundleElement);
		Integer signedPreKeyId = signedPreKeyId(bundleElement);
		byte[] signedPreKeySignature = signedPreKeySignature(bundleElement);
		IdentityKey identityKey = identityKey(bundleElement);
		if(signedPreKeyPublic == null || identityKey == null) {
			return null;
		}

		return new PreKeyBundle(0, 0, 0, null,
				signedPreKeyId, signedPreKeyPublic, signedPreKeySignature, identityKey);
	}

	public List<PreKeyBundle> preKeys(final IqPacket preKeys) {
		List<PreKeyBundle> bundles = new ArrayList<>();
		Map<Integer, ECPublicKey> preKeyPublics = preKeyPublics(preKeys);
		if ( preKeyPublics != null) {
			for (Integer preKeyId : preKeyPublics.keySet()) {
				ECPublicKey preKeyPublic = preKeyPublics.get(preKeyId);
				bundles.add(new PreKeyBundle(0, 0, preKeyId, preKeyPublic,
						0, null, null, null));
			}
		}

		return bundles;
	}

	@Override
	public void onIqPacketReceived(final Account account, final IqPacket packet) {
		if (Config.BACKGROUND_STANZA_LOGGING && (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET)) {
			Element first = packet.getChildren().size() > 0 ? packet.getChildren().get(0) : null;
			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": IQ request from "+packet.getFrom()+(first == null ? "" : " "+first));
		}

		if (packet.getType() == IqPacket.TYPE.ERROR || packet.getType() == IqPacket.TYPE.TIMEOUT) {
			return;
		} else if (packet.hasChild("query", Xmlns.ROSTER) && packet.fromServer(account)) {
			final Element query = packet.findChild("query");
			// If this is in response to a query for the whole roster:
			if (packet.getType() == IqPacket.TYPE.RESULT) {
				account.getRoster().markAllAsNotInRoster();
			}
			this.rosterItems(account, query);
		} else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
				packet.fromServer(account)) {
			// Block list or block push.
			Log.d(Config.LOGTAG, "Received blocklist update from server");
			final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
			final Element block = packet.findChild("block", Xmlns.BLOCKING);
			final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
				(block != null ? block.getChildren() : null);
			// If this is a response to a blocklist query, clear the block list and replace with the new one.
			// Otherwise, just update the existing blocklist.
			if (packet.getType() == IqPacket.TYPE.RESULT) {
				account.clearBlocklist();
				account.getXmppConnection().getFeatures().setBlockListRequested(true);
			}
			if (items != null) {
				final Collection<Jid> jids = new ArrayList<>(items.size());
				// Create a collection of Jids from the packet
				for (final Element item : items) {
					if (item.getName().equals("item")) {
						final Jid jid = item.getAttributeAsJid("jid");
						if (jid != null) {
							jids.add(jid);
						}
					}
				}
				account.getBlocklist().addAll(jids);
			}
			// Update the UI
			mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
		} else if (packet.hasChild("unblock", Xmlns.BLOCKING) &&
				packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) {
			Log.d(Config.LOGTAG, "Received unblock update from server");
			final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
			if (items.size() == 0) {
				// No children to unblock == unblock all
				account.getBlocklist().clear();
			} else {
				final Collection<Jid> jids = new ArrayList<>(items.size());
				for (final Element item : items) {
					if (item.getName().equals("item")) {
						final Jid jid = item.getAttributeAsJid("jid");
						if (jid != null) {
							jids.add(jid);
						}
					}
				}
				account.getBlocklist().removeAll(jids);
			}
			mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
		} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
				|| packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
			mXmppConnectionService.getJingleConnectionManager()
				.deliverIbbPacket(account, packet);
		} else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
			final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
			mXmppConnectionService.sendIqPacket(account, response, null);
		} else if (packet.hasChild("query","jabber:iq:version")) {
			final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet);
			mXmppConnectionService.sendIqPacket(account,response,null);
		} else if (packet.hasChild("ping", "urn:xmpp:ping")) {
			final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
			mXmppConnectionService.sendIqPacket(account, response, null);
		} else {
			if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
				final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
				final Element error = response.addChild("error");
				error.setAttribute("type", "cancel");
				error.addChild("feature-not-implemented","urn:ietf:params:xml:ns:xmpp-stanzas");
				account.getXmppConnection().sendIqPacket(response, null);
			}
		}
	}

}