aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java
blob: 896dafce22680c26e0d96f3abf99e8fd18270a0a (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
package de.pixart.messenger.crypto.axolotl;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log;

import org.whispersystems.libsignal.DuplicateMessageException;
import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.InvalidKeyException;
import org.whispersystems.libsignal.InvalidKeyIdException;
import org.whispersystems.libsignal.InvalidMessageException;
import org.whispersystems.libsignal.InvalidVersionException;
import org.whispersystems.libsignal.LegacyMessageException;
import org.whispersystems.libsignal.NoSessionException;
import org.whispersystems.libsignal.SessionCipher;
import org.whispersystems.libsignal.SignalProtocolAddress;
import org.whispersystems.libsignal.UntrustedIdentityException;
import org.whispersystems.libsignal.protocol.CiphertextMessage;
import org.whispersystems.libsignal.protocol.PreKeySignalMessage;
import org.whispersystems.libsignal.protocol.SignalMessage;
import org.whispersystems.libsignal.util.guava.Optional;

import java.util.Iterator;
import java.util.List;

import de.pixart.messenger.Config;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.utils.CryptoHelper;

public class XmppAxolotlSession implements Comparable<XmppAxolotlSession> {
    private final SessionCipher cipher;
    private final SQLiteAxolotlStore sqLiteAxolotlStore;
    private final SignalProtocolAddress remoteAddress;
    private final Account account;
    private IdentityKey identityKey;
    private Integer preKeyId = null;
    private boolean fresh = true;

    public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, SignalProtocolAddress remoteAddress, IdentityKey identityKey) {
        this(account, store, remoteAddress);
        this.identityKey = identityKey;
    }

    public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, SignalProtocolAddress remoteAddress) {
        this.cipher = new SessionCipher(store, remoteAddress);
        this.remoteAddress = remoteAddress;
        this.sqLiteAxolotlStore = store;
        this.account = account;
    }

    public Integer getPreKeyIdAndReset() {
        final Integer preKeyId = this.preKeyId;
        this.preKeyId = null;
        return preKeyId;
    }

    public String getFingerprint() {
        return identityKey == null ? null : CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize());
    }

    public IdentityKey getIdentityKey() {
        return identityKey;
    }

    public SignalProtocolAddress getRemoteAddress() {
        return remoteAddress;
    }

    public boolean isFresh() {
        return fresh;
    }

    public void setNotFresh() {
        this.fresh = false;
    }

    protected void setTrust(FingerprintStatus status) {
        sqLiteAxolotlStore.setFingerprintStatus(getFingerprint(), status);
    }

    public FingerprintStatus getTrust() {
        FingerprintStatus status = sqLiteAxolotlStore.getFingerprintStatus(getFingerprint());
        return (status == null) ? FingerprintStatus.createActiveUndecided() : status;
    }

    @Nullable
    byte[] processReceiving(List<AxolotlKey> possibleKeys) throws CryptoFailedException {
        byte[] plaintext = null;
        FingerprintStatus status = getTrust();
        if (!status.isCompromised()) {
            Iterator<AxolotlKey> iterator = possibleKeys.iterator();
            while (iterator.hasNext()) {
                AxolotlKey encryptedKey = iterator.next();
                try {
                    if (encryptedKey.prekey) {
                        PreKeySignalMessage preKeySignalMessage = new PreKeySignalMessage(encryptedKey.key);
                        Optional<Integer> optionalPreKeyId = preKeySignalMessage.getPreKeyId();
                        IdentityKey identityKey = preKeySignalMessage.getIdentityKey();
                        if (!optionalPreKeyId.isPresent()) {
                            if (iterator.hasNext()) {
                                continue;
                            }
                            throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId");
                        }
                        preKeyId = optionalPreKeyId.get();
                        if (this.identityKey != null && !this.identityKey.equals(identityKey)) {
                            if (iterator.hasNext()) {
                                continue;
                            }
                            throw new CryptoFailedException("Received PreKeyWhisperMessage but preexisting identity key changed.");
                        }
                        this.identityKey = identityKey;
                        plaintext = cipher.decrypt(preKeySignalMessage);
                    } else {
                        SignalMessage signalMessage = new SignalMessage(encryptedKey.key);
                        try {
                            plaintext = cipher.decrypt(signalMessage);
                        } catch (InvalidMessageException e) {
                            if (iterator.hasNext()) {
                                Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring crypto exception because possible keys left to try", e);
                                continue;
                            }
                            throw new BrokenSessionException(this.remoteAddress, e);
                        } catch (NoSessionException e) {
                            if (iterator.hasNext()) {
                                Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring crypto exception because possible keys left to try", e);
                                continue;
                            }
                            throw new BrokenSessionException(this.remoteAddress, e);
                        }
                        preKeyId = null; //better safe than sorry because we use that to do special after prekey handling
                    }
                } catch (InvalidVersionException | InvalidKeyException | LegacyMessageException | InvalidMessageException | DuplicateMessageException | InvalidKeyIdException | UntrustedIdentityException e) {
                    if (iterator.hasNext()) {
                        Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring crypto exception because possible keys left to try", e);
                        continue;
                    }
                    throw new CryptoFailedException("Error decrypting SignalMessage", e);
                }
                if (iterator.hasNext()) {
                    break;
                }
            }
            if (!status.isActive()) {
                setTrust(status.toActive());
                //TODO: also (re)add to device list?
            }
        } else {
            throw new CryptoFailedException("not encrypting omemo message from fingerprint " + getFingerprint() + " because it was marked as compromised");
        }
        return plaintext;
    }

    @Nullable
    public AxolotlKey processSending(@NonNull byte[] outgoingMessage, boolean ignoreSessionTrust) {
        FingerprintStatus status = getTrust();
        if (ignoreSessionTrust || status.isTrustedAndActive()) {
            try {
                CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage);
                return new AxolotlKey(getRemoteAddress().getDeviceId(), ciphertextMessage.serialize(), ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE);
            } catch (UntrustedIdentityException e) {
                return null;
            }
        } else {
            return null;
        }
    }

    public Account getAccount() {
        return account;
    }

    @Override
    public int compareTo(XmppAxolotlSession o) {
        return getTrust().compareTo(o.getTrust());
    }

    public static class AxolotlKey {


        public final byte[] key;
        public final boolean prekey;
        public final int deviceId;

        public AxolotlKey(int deviceId, byte[] key, boolean prekey) {
            this.deviceId = deviceId;
            this.key = key;
            this.prekey = prekey;
        }
    }
}