IqParser.java

  1package eu.siacs.conversations.parser;
  2
  3import android.support.annotation.NonNull;
  4import android.util.Base64;
  5import android.util.Log;
  6
  7import org.whispersystems.libaxolotl.IdentityKey;
  8import org.whispersystems.libaxolotl.InvalidKeyException;
  9import org.whispersystems.libaxolotl.ecc.Curve;
 10import org.whispersystems.libaxolotl.ecc.ECPublicKey;
 11import org.whispersystems.libaxolotl.state.PreKeyBundle;
 12
 13import java.util.ArrayList;
 14import java.util.Collection;
 15import java.util.HashMap;
 16import java.util.HashSet;
 17import java.util.List;
 18import java.util.Map;
 19import java.util.Set;
 20
 21import eu.siacs.conversations.Config;
 22import eu.siacs.conversations.entities.Account;
 23import eu.siacs.conversations.entities.Contact;
 24import eu.siacs.conversations.services.XmppConnectionService;
 25import eu.siacs.conversations.utils.Xmlns;
 26import eu.siacs.conversations.xml.Element;
 27import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 28import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 29import eu.siacs.conversations.xmpp.jid.Jid;
 30import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 31
 32public class IqParser extends AbstractParser implements OnIqPacketReceived {
 33
 34	public IqParser(final XmppConnectionService service) {
 35		super(service);
 36	}
 37
 38	private void rosterItems(final Account account, final Element query) {
 39		final String version = query.getAttribute("ver");
 40		if (version != null) {
 41			account.getRoster().setVersion(version);
 42		}
 43		for (final Element item : query.getChildren()) {
 44			if (item.getName().equals("item")) {
 45				final Jid jid = item.getAttributeAsJid("jid");
 46				if (jid == null) {
 47					continue;
 48				}
 49				final String name = item.getAttribute("name");
 50				final String subscription = item.getAttribute("subscription");
 51				final Contact contact = account.getRoster().getContact(jid);
 52				if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
 53					contact.setServerName(name);
 54					contact.parseGroupsFromElement(item);
 55				}
 56				if (subscription != null) {
 57					if (subscription.equals("remove")) {
 58						contact.resetOption(Contact.Options.IN_ROSTER);
 59						contact.resetOption(Contact.Options.DIRTY_DELETE);
 60						contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
 61					} else {
 62						contact.setOption(Contact.Options.IN_ROSTER);
 63						contact.resetOption(Contact.Options.DIRTY_PUSH);
 64						contact.parseSubscriptionFromElement(item);
 65					}
 66				}
 67				mXmppConnectionService.getAvatarService().clear(contact);
 68			}
 69		}
 70		mXmppConnectionService.updateConversationUi();
 71		mXmppConnectionService.updateRosterUi();
 72	}
 73
 74	public String avatarData(final IqPacket packet) {
 75		final Element pubsub = packet.findChild("pubsub",
 76				"http://jabber.org/protocol/pubsub");
 77		if (pubsub == null) {
 78			return null;
 79		}
 80		final Element items = pubsub.findChild("items");
 81		if (items == null) {
 82			return null;
 83		}
 84		return super.avatarData(items);
 85	}
 86
 87	public Element getItem(final IqPacket packet) {
 88		final Element pubsub = packet.findChild("pubsub",
 89				"http://jabber.org/protocol/pubsub");
 90		if (pubsub == null) {
 91			return null;
 92		}
 93		final Element items = pubsub.findChild("items");
 94		if (items == null) {
 95			return null;
 96		}
 97		return items.findChild("item");
 98	}
 99
100	@NonNull
101	public Set<Integer> deviceIds(final Element item) {
102		Set<Integer> deviceIds = new HashSet<>();
103		if (item != null) {
104			final Element list = item.findChild("list");
105			if (list != null) {
106				for (Element device : list.getChildren()) {
107					if (!device.getName().equals("device")) {
108						continue;
109					}
110					try {
111						Integer id = Integer.valueOf(device.getAttribute("id"));
112						deviceIds.add(id);
113					} catch (NumberFormatException e) {
114						Log.e(Config.LOGTAG, "Encountered nvalid <device> node in PEP:" + device.toString()
115								+ ", skipping...");
116						continue;
117					}
118				}
119			}
120		}
121		return deviceIds;
122	}
123
124	public Integer signedPreKeyId(final Element bundle) {
125		final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic");
126		if(signedPreKeyPublic == null) {
127			return null;
128		}
129		return Integer.valueOf(signedPreKeyPublic.getAttribute("signedPreKeyId"));
130	}
131
132	public ECPublicKey signedPreKeyPublic(final Element bundle) {
133		ECPublicKey publicKey = null;
134		final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic");
135		if(signedPreKeyPublic == null) {
136			return null;
137		}
138		try {
139			publicKey = Curve.decodePoint(Base64.decode(signedPreKeyPublic.getContent(),Base64.DEFAULT), 0);
140		} catch (InvalidKeyException e) {
141			Log.e(Config.LOGTAG, "Invalid signedPreKeyPublic in PEP: " + e.getMessage());
142		}
143		return publicKey;
144	}
145
146	public byte[] signedPreKeySignature(final Element bundle) {
147		final Element signedPreKeySignature = bundle.findChild("signedPreKeySignature");
148		if(signedPreKeySignature == null) {
149			return null;
150		}
151		return Base64.decode(signedPreKeySignature.getContent(),Base64.DEFAULT);
152	}
153
154	public IdentityKey identityKey(final Element bundle) {
155		IdentityKey identityKey = null;
156		final Element identityKeyElement = bundle.findChild("identityKey");
157		if(identityKeyElement == null) {
158			return null;
159		}
160		try {
161			identityKey = new IdentityKey(Base64.decode(identityKeyElement.getContent(), Base64.DEFAULT), 0);
162		} catch (InvalidKeyException e) {
163			Log.e(Config.LOGTAG,"Invalid identityKey in PEP: "+e.getMessage());
164		}
165		return identityKey;
166	}
167
168	public Map<Integer, ECPublicKey> preKeyPublics(final IqPacket packet) {
169		Map<Integer, ECPublicKey> preKeyRecords = new HashMap<>();
170		Element item = getItem(packet);
171		if (item == null) {
172			Log.d(Config.LOGTAG, "Couldn't find <item> in bundle IQ packet: " + packet);
173			return null;
174		}
175		final Element bundleElement = item.findChild("bundle");
176		if(bundleElement == null) {
177			return null;
178		}
179		final Element prekeysElement = bundleElement.findChild("prekeys");
180		if(prekeysElement == null) {
181			Log.d(Config.LOGTAG, "Couldn't find <prekeys> in bundle IQ packet: " + packet);
182			return null;
183		}
184		for(Element preKeyPublicElement : prekeysElement.getChildren()) {
185			if(!preKeyPublicElement.getName().equals("preKeyPublic")){
186				Log.d(Config.LOGTAG, "Encountered unexpected tag in prekeys list: " + preKeyPublicElement);
187				continue;
188			}
189			Integer preKeyId = Integer.valueOf(preKeyPublicElement.getAttribute("preKeyId"));
190			try {
191				ECPublicKey preKeyPublic = Curve.decodePoint(Base64.decode(preKeyPublicElement.getContent(), Base64.DEFAULT), 0);
192				preKeyRecords.put(preKeyId, preKeyPublic);
193			} catch (InvalidKeyException e) {
194				Log.e(Config.LOGTAG, "Invalid preKeyPublic (ID="+preKeyId+") in PEP: "+ e.getMessage()+", skipping...");
195				continue;
196			}
197		}
198		return preKeyRecords;
199	}
200
201	public PreKeyBundle bundle(final IqPacket bundle) {
202		Element bundleItem = getItem(bundle);
203		if(bundleItem == null) {
204			return null;
205		}
206		final Element bundleElement = bundleItem.findChild("bundle");
207		if(bundleElement == null) {
208			return null;
209		}
210		ECPublicKey signedPreKeyPublic = signedPreKeyPublic(bundleElement);
211		Integer signedPreKeyId = signedPreKeyId(bundleElement);
212		byte[] signedPreKeySignature = signedPreKeySignature(bundleElement);
213		IdentityKey identityKey = identityKey(bundleElement);
214		if(signedPreKeyPublic == null || identityKey == null) {
215			return null;
216		}
217
218		return new PreKeyBundle(0, 0, 0, null,
219				signedPreKeyId, signedPreKeyPublic, signedPreKeySignature, identityKey);
220	}
221
222	public List<PreKeyBundle> preKeys(final IqPacket preKeys) {
223		List<PreKeyBundle> bundles = new ArrayList<>();
224		Map<Integer, ECPublicKey> preKeyPublics = preKeyPublics(preKeys);
225		if ( preKeyPublics != null) {
226			for (Integer preKeyId : preKeyPublics.keySet()) {
227				ECPublicKey preKeyPublic = preKeyPublics.get(preKeyId);
228				bundles.add(new PreKeyBundle(0, 0, preKeyId, preKeyPublic,
229						0, null, null, null));
230			}
231		}
232
233		return bundles;
234	}
235
236	@Override
237	public void onIqPacketReceived(final Account account, final IqPacket packet) {
238		if (packet.hasChild("query", Xmlns.ROSTER) && packet.fromServer(account)) {
239			final Element query = packet.findChild("query");
240			// If this is in response to a query for the whole roster:
241			if (packet.getType() == IqPacket.TYPE.RESULT) {
242				account.getRoster().markAllAsNotInRoster();
243			}
244			this.rosterItems(account, query);
245		} else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
246				packet.fromServer(account)) {
247			// Block list or block push.
248			Log.d(Config.LOGTAG, "Received blocklist update from server");
249			final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
250			final Element block = packet.findChild("block", Xmlns.BLOCKING);
251			final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
252				(block != null ? block.getChildren() : null);
253			// If this is a response to a blocklist query, clear the block list and replace with the new one.
254			// Otherwise, just update the existing blocklist.
255			if (packet.getType() == IqPacket.TYPE.RESULT) {
256				account.clearBlocklist();
257				account.getXmppConnection().getFeatures().setBlockListRequested(true);
258			}
259			if (items != null) {
260				final Collection<Jid> jids = new ArrayList<>(items.size());
261				// Create a collection of Jids from the packet
262				for (final Element item : items) {
263					if (item.getName().equals("item")) {
264						final Jid jid = item.getAttributeAsJid("jid");
265						if (jid != null) {
266							jids.add(jid);
267						}
268					}
269				}
270				account.getBlocklist().addAll(jids);
271			}
272			// Update the UI
273			mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
274		} else if (packet.hasChild("unblock", Xmlns.BLOCKING) &&
275				packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) {
276			Log.d(Config.LOGTAG, "Received unblock update from server");
277			final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
278			if (items.size() == 0) {
279				// No children to unblock == unblock all
280				account.getBlocklist().clear();
281			} else {
282				final Collection<Jid> jids = new ArrayList<>(items.size());
283				for (final Element item : items) {
284					if (item.getName().equals("item")) {
285						final Jid jid = item.getAttributeAsJid("jid");
286						if (jid != null) {
287							jids.add(jid);
288						}
289					}
290				}
291				account.getBlocklist().removeAll(jids);
292			}
293			mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
294		} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
295				|| packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
296			mXmppConnectionService.getJingleConnectionManager()
297				.deliverIbbPacket(account, packet);
298		} else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
299			final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
300			mXmppConnectionService.sendIqPacket(account, response, null);
301		} else if (packet.hasChild("query","jabber:iq:version")) {
302			final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet);
303			mXmppConnectionService.sendIqPacket(account,response,null);
304		} else if (packet.hasChild("ping", "urn:xmpp:ping")) {
305			final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
306			mXmppConnectionService.sendIqPacket(account, response, null);
307		} else {
308			if ((packet.getType() == IqPacket.TYPE.GET)
309					|| (packet.getType() == IqPacket.TYPE.SET)) {
310				final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
311				final Element error = response.addChild("error");
312				error.setAttribute("type", "cancel");
313				error.addChild("feature-not-implemented",
314						"urn:ietf:params:xml:ns:xmpp-stanzas");
315				account.getXmppConnection().sendIqPacket(response, null);
316					}
317		}
318	}
319
320}