aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel@gultsch.de>2016-11-14 22:27:41 +0100
committerDaniel Gultsch <daniel@gultsch.de>2016-11-14 22:27:41 +0100
commit05fc15be3d45815e98f3d1fe0b527990fccf4fa6 (patch)
treee4b92adf3e4feeee9c5d13ff4b89c51d634c6a54 /src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java
parent6da8b50d951ce8998986d2082fa27fc862ded03c (diff)
refactore trust enum to be FingerprintStatus class with trust and active
Diffstat (limited to 'src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java')
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java156
1 files changed, 34 insertions, 122 deletions
diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java
index b7d11ec0..706a5c46 100644
--- a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java
+++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java
@@ -19,9 +19,6 @@ import org.whispersystems.libaxolotl.protocol.CiphertextMessage;
import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage;
import org.whispersystems.libaxolotl.protocol.WhisperMessage;
-import java.util.HashMap;
-import java.util.Map;
-
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
@@ -34,76 +31,6 @@ public class XmppAxolotlSession {
private Integer preKeyId = null;
private boolean fresh = true;
- public enum Trust {
- UNDECIDED(0),
- TRUSTED(1),
- UNTRUSTED(2),
- COMPROMISED(3),
- INACTIVE_TRUSTED(4),
- INACTIVE_UNDECIDED(5),
- INACTIVE_UNTRUSTED(6),
- TRUSTED_X509(7),
- INACTIVE_TRUSTED_X509(8);
-
- private static final Map<Integer, Trust> trustsByValue = new HashMap<>();
-
- static {
- for (Trust trust : Trust.values()) {
- trustsByValue.put(trust.getCode(), trust);
- }
- }
-
- private final int code;
-
- Trust(int code) {
- this.code = code;
- }
-
- public int getCode() {
- return this.code;
- }
-
- public String toString() {
- switch (this) {
- case UNDECIDED:
- return "Trust undecided " + getCode();
- case TRUSTED:
- return "Trusted " + getCode();
- case COMPROMISED:
- return "Compromised " + getCode();
- case INACTIVE_TRUSTED:
- return "Inactive (Trusted)" + getCode();
- case INACTIVE_UNDECIDED:
- return "Inactive (Undecided)" + getCode();
- case INACTIVE_UNTRUSTED:
- return "Inactive (Untrusted)" + getCode();
- case TRUSTED_X509:
- return "Trusted (X509) " + getCode();
- case INACTIVE_TRUSTED_X509:
- return "Inactive (Trusted (X509)) " + getCode();
- case UNTRUSTED:
- default:
- return "Untrusted " + getCode();
- }
- }
-
- public static Trust fromBoolean(Boolean trusted) {
- return trusted ? TRUSTED : UNTRUSTED;
- }
-
- public static Trust fromCode(int code) {
- return trustsByValue.get(code);
- }
-
- public boolean trusted() {
- return this == TRUSTED_X509 || this == TRUSTED;
- }
-
- public boolean trustedInactive() {
- return this == INACTIVE_TRUSTED_X509 || this == INACTIVE_TRUSTED;
- }
- }
-
public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress, IdentityKey identityKey) {
this(account, store, remoteAddress);
this.identityKey = identityKey;
@@ -145,75 +72,60 @@ public class XmppAxolotlSession {
this.fresh = false;
}
- protected void setTrust(Trust trust) {
- sqLiteAxolotlStore.setFingerprintTrust(getFingerprint(), trust);
+ protected void setTrust(FingerprintStatus status) {
+ sqLiteAxolotlStore.setFingerprintTrust(getFingerprint(), status);
}
- protected Trust getTrust() {
- Trust trust = sqLiteAxolotlStore.getFingerprintTrust(getFingerprint());
- return (trust == null) ? Trust.UNDECIDED : trust;
+ protected FingerprintStatus getTrust() {
+ FingerprintStatus status = sqLiteAxolotlStore.getFingerprintStatus(getFingerprint());
+ return (status == null) ? FingerprintStatus.createActiveUndecided() : status;
}
@Nullable
public byte[] processReceiving(byte[] encryptedKey) {
byte[] plaintext = null;
- Trust trust = getTrust();
- switch (trust) {
- case INACTIVE_TRUSTED:
- case UNDECIDED:
- case UNTRUSTED:
- case TRUSTED:
- case INACTIVE_TRUSTED_X509:
- case TRUSTED_X509:
+ FingerprintStatus status = getTrust();
+ if (!status.isCompromised()) {
+ try {
try {
- try {
- PreKeyWhisperMessage message = new PreKeyWhisperMessage(encryptedKey);
- if (!message.getPreKeyId().isPresent()) {
- Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage did not contain a PreKeyId");
- break;
- }
- Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage received, new session ID:" + message.getSignedPreKeyId() + "/" + message.getPreKeyId());
- IdentityKey msgIdentityKey = message.getIdentityKey();
- if (this.identityKey != null && !this.identityKey.equals(msgIdentityKey)) {
- Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Had session with fingerprint " + this.getFingerprint() + ", received message with fingerprint " + msgIdentityKey.getFingerprint());
- } else {
- this.identityKey = msgIdentityKey;
- plaintext = cipher.decrypt(message);
- preKeyId = message.getPreKeyId().get();
- }
- } catch (InvalidMessageException | InvalidVersionException e) {
- Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "WhisperMessage received");
- WhisperMessage message = new WhisperMessage(encryptedKey);
+ PreKeyWhisperMessage message = new PreKeyWhisperMessage(encryptedKey);
+ if (!message.getPreKeyId().isPresent()) {
+ Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage did not contain a PreKeyId");
+ return null;
+ }
+ Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage received, new session ID:" + message.getSignedPreKeyId() + "/" + message.getPreKeyId());
+ IdentityKey msgIdentityKey = message.getIdentityKey();
+ if (this.identityKey != null && !this.identityKey.equals(msgIdentityKey)) {
+ Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Had session with fingerprint " + this.getFingerprint() + ", received message with fingerprint " + msgIdentityKey.getFingerprint());
+ } else {
+ this.identityKey = msgIdentityKey;
plaintext = cipher.decrypt(message);
- } catch (InvalidKeyException | InvalidKeyIdException | UntrustedIdentityException e) {
- Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error decrypting axolotl header, " + e.getClass().getName() + ": " + e.getMessage());
+ preKeyId = message.getPreKeyId().get();
}
- } catch (LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException e) {
+ } catch (InvalidMessageException | InvalidVersionException e) {
+ Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "WhisperMessage received");
+ WhisperMessage message = new WhisperMessage(encryptedKey);
+ plaintext = cipher.decrypt(message);
+ } catch (InvalidKeyException | InvalidKeyIdException | UntrustedIdentityException e) {
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error decrypting axolotl header, " + e.getClass().getName() + ": " + e.getMessage());
}
+ } catch (LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException e) {
+ Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error decrypting axolotl header, " + e.getClass().getName() + ": " + e.getMessage());
+ }
- if (plaintext != null) {
- if (trust == Trust.INACTIVE_TRUSTED) {
- setTrust(Trust.TRUSTED);
- } else if (trust == Trust.INACTIVE_TRUSTED_X509) {
- setTrust(Trust.TRUSTED_X509);
- }
+ if (plaintext != null) {
+ if (!status.isActive()) {
+ setTrust(status.toActive());
}
-
- break;
-
- case COMPROMISED:
- default:
- // ignore
- break;
+ }
}
return plaintext;
}
@Nullable
public byte[] processSending(@NonNull byte[] outgoingMessage) {
- Trust trust = getTrust();
- if (trust.trusted()) {
+ FingerprintStatus status = getTrust();
+ if (status.isTrustedAndActive()) {
CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage);
return ciphertextMessage.serialize();
} else {