XmppAxolotlSession.java

  1package eu.siacs.conversations.crypto.axolotl;
  2
  3import android.support.annotation.NonNull;
  4import android.support.annotation.Nullable;
  5import android.util.Log;
  6
  7import org.whispersystems.libaxolotl.AxolotlAddress;
  8import org.whispersystems.libaxolotl.DuplicateMessageException;
  9import org.whispersystems.libaxolotl.IdentityKey;
 10import org.whispersystems.libaxolotl.InvalidKeyException;
 11import org.whispersystems.libaxolotl.InvalidKeyIdException;
 12import org.whispersystems.libaxolotl.InvalidMessageException;
 13import org.whispersystems.libaxolotl.InvalidVersionException;
 14import org.whispersystems.libaxolotl.LegacyMessageException;
 15import org.whispersystems.libaxolotl.NoSessionException;
 16import org.whispersystems.libaxolotl.SessionCipher;
 17import org.whispersystems.libaxolotl.UntrustedIdentityException;
 18import org.whispersystems.libaxolotl.protocol.CiphertextMessage;
 19import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage;
 20import org.whispersystems.libaxolotl.protocol.WhisperMessage;
 21
 22import eu.siacs.conversations.Config;
 23import eu.siacs.conversations.entities.Account;
 24
 25public class XmppAxolotlSession implements Comparable<XmppAxolotlSession> {
 26	private final SessionCipher cipher;
 27	private final SQLiteAxolotlStore sqLiteAxolotlStore;
 28	private final AxolotlAddress remoteAddress;
 29	private final Account account;
 30	private IdentityKey identityKey;
 31	private Integer preKeyId = null;
 32	private boolean fresh = true;
 33
 34	public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress, IdentityKey identityKey) {
 35		this(account, store, remoteAddress);
 36		this.identityKey = identityKey;
 37	}
 38
 39	public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress) {
 40		this.cipher = new SessionCipher(store, remoteAddress);
 41		this.remoteAddress = remoteAddress;
 42		this.sqLiteAxolotlStore = store;
 43		this.account = account;
 44	}
 45
 46	public Integer getPreKeyId() {
 47		return preKeyId;
 48	}
 49
 50	public void resetPreKeyId() {
 51
 52		preKeyId = null;
 53	}
 54
 55	public String getFingerprint() {
 56		return identityKey == null ? null : identityKey.getFingerprint().replaceAll("\\s", "");
 57	}
 58
 59	public IdentityKey getIdentityKey() {
 60		return identityKey;
 61	}
 62
 63	public AxolotlAddress getRemoteAddress() {
 64		return remoteAddress;
 65	}
 66
 67	public boolean isFresh() {
 68		return fresh;
 69	}
 70
 71	public void setNotFresh() {
 72		this.fresh = false;
 73	}
 74
 75	protected void setTrust(FingerprintStatus status) {
 76		sqLiteAxolotlStore.setFingerprintStatus(getFingerprint(), status);
 77	}
 78
 79	public FingerprintStatus getTrust() {
 80		FingerprintStatus status = sqLiteAxolotlStore.getFingerprintStatus(getFingerprint());
 81		return (status == null) ? FingerprintStatus.createActiveUndecided() : status;
 82	}
 83
 84	@Nullable
 85	public byte[] processReceiving(AxolotlKey encryptedKey) throws CryptoFailedException {
 86		byte[] plaintext = null;
 87		FingerprintStatus status = getTrust();
 88		if (!status.isCompromised()) {
 89			try {
 90				try {
 91					PreKeyWhisperMessage message = new PreKeyWhisperMessage(encryptedKey.key);
 92					if (!message.getPreKeyId().isPresent()) {
 93						throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId");
 94					}
 95					Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage received, new session ID:" + message.getSignedPreKeyId() + "/" + message.getPreKeyId());
 96					IdentityKey msgIdentityKey = message.getIdentityKey();
 97					if (this.identityKey != null && !this.identityKey.equals(msgIdentityKey)) {
 98						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Had session with fingerprint " + this.getFingerprint() + ", received message with fingerprint " + msgIdentityKey.getFingerprint());
 99					} else {
100						this.identityKey = msgIdentityKey;
101						plaintext = cipher.decrypt(message);
102						preKeyId = message.getPreKeyId().get();
103					}
104				} catch (InvalidMessageException | InvalidVersionException e) {
105					Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "WhisperMessage received");
106					WhisperMessage message = new WhisperMessage(encryptedKey.key);
107					plaintext = cipher.decrypt(message);
108				} catch (InvalidKeyException | InvalidKeyIdException | UntrustedIdentityException e) {
109					throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()");
110				}
111			} catch (LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException e) {
112				throw new CryptoFailedException("Error decrypting axolotl header, \" + e.getClass().getName() + \": \" + e.getMessage()");
113			}
114			if (plaintext==null) {
115				throw new CryptoFailedException("plaintext unexpectedly null");
116			}
117			if (!status.isActive()) {
118				setTrust(status.toActive());
119			}
120		} else {
121			throw new CryptoFailedException("not encrypting omemo message from fingerprint "+getFingerprint()+" because it was marked as compromised");
122		}
123		return plaintext;
124	}
125
126	@Nullable
127	public AxolotlKey processSending(@NonNull byte[] outgoingMessage) {
128		FingerprintStatus status = getTrust();
129		if (status.isTrustedAndActive()) {
130			CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage);
131			return new AxolotlKey(ciphertextMessage.serialize(),ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE);
132		} else {
133			return null;
134		}
135	}
136
137	public Account getAccount() {
138		return account;
139	}
140
141	@Override
142	public int compareTo(XmppAxolotlSession o) {
143		return getTrust().compareTo(o.getTrust());
144	}
145
146	public static class AxolotlKey {
147
148
149		public final byte[] key;
150		public final boolean prekey;
151
152		public AxolotlKey(byte[] key, boolean prekey) {
153			this.key = key;
154			this.prekey = prekey;
155		}
156	}
157}