aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/entities/Contact.java
diff options
context:
space:
mode:
authorAndreas Straub <andreas.straub@rwth-aachen.de>2015-06-25 16:56:34 +0200
committerAndreas Straub <andy@strb.org>2015-07-19 21:32:25 +0200
commitf73aa1a2006beb741bc39026bfd10e6166d7951a (patch)
treed69a34e87cdfb880f8ec9ab4ac5bd7fd374d9bc1 /src/main/java/eu/siacs/conversations/entities/Contact.java
parent077932eb558853e4c3c3046c54db18c530495d8c (diff)
Reworked axolotl protocol layer
Numerous fixes
Diffstat (limited to 'src/main/java/eu/siacs/conversations/entities/Contact.java')
-rw-r--r--src/main/java/eu/siacs/conversations/entities/Contact.java62
1 files changed, 43 insertions, 19 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java
index 2f9b375d..45b55e49 100644
--- a/src/main/java/eu/siacs/conversations/entities/Contact.java
+++ b/src/main/java/eu/siacs/conversations/entities/Contact.java
@@ -2,6 +2,7 @@ package eu.siacs.conversations.entities;
import android.content.ContentValues;
import android.database.Cursor;
+import android.util.Base64;
import android.util.Log;
import org.json.JSONArray;
@@ -349,20 +350,38 @@ public class Contact implements ListItem, Blockable {
}
}
- public List<IdentityKey> getTrustedAxolotlIdentityKeys() {
+ public List<IdentityKey> getAxolotlIdentityKeys() {
synchronized (this.keys) {
JSONArray serializedKeyItems = this.keys.optJSONArray("axolotl_identity_key");
List<IdentityKey> identityKeys = new ArrayList<>();
+ List<Integer> toDelete = new ArrayList<>();
if(serializedKeyItems != null) {
for(int i = 0; i<serializedKeyItems.length();++i) {
try {
String serializedKeyItem = serializedKeyItems.getString(i);
- IdentityKey identityKey = new IdentityKey(serializedKeyItem.getBytes(), 0);
+ IdentityKey identityKey = new IdentityKey(Base64.decode(serializedKeyItem, Base64.DEFAULT), 0);
identityKeys.add(identityKey);
} catch (InvalidKeyException e) {
- Log.e(Config.LOGTAG, "Invalid axolotl identity key encountered at" + this.getJid() + ": " + e.getMessage());
+ Log.e(Config.LOGTAG, "Invalid axolotl identity key encountered at contact" + this.getJid() + ": " + e.getMessage() + ", marking for deletion...");
+ toDelete.add(i);
} catch (JSONException e) {
- Log.e(Config.LOGTAG, "Error retrieving axolotl identity key at" + this.getJid() + ": " + e.getMessage());
+ Log.e(Config.LOGTAG, "Error retrieving axolotl identity key at contact " + this.getJid() + ": " + e.getMessage());
+ } catch (IllegalArgumentException e) {
+ Log.e(Config.LOGTAG, "Encountered malformed identity key for contact" + this.getJid() + ": " + e.getMessage() + ", marking for deletion... ");
+ toDelete.add(i);
+ }
+ }
+ if(!toDelete.isEmpty()) {
+ try {
+ JSONArray filteredKeyItems = new JSONArray();
+ for (int i = 0; i < serializedKeyItems.length(); ++i) {
+ if (!toDelete.contains(i)) {
+ filteredKeyItems.put(serializedKeyItems.get(i));
+ }
+ }
+ this.keys.put("axolotl_identity_key", filteredKeyItems);
+ } catch (JSONException e) {
+ //should never happen
}
}
}
@@ -370,23 +389,28 @@ public class Contact implements ListItem, Blockable {
}
}
- public boolean addAxolotlIdentityKey(IdentityKey identityKey, boolean trusted) {
+ public boolean addAxolotlIdentityKey(IdentityKey identityKey) {
synchronized (this.keys) {
- JSONArray keysList;
- try {
- keysList = this.keys.getJSONArray("axolotl_identity_key");
- } catch (JSONException e) {
- keysList = new JSONArray();
- }
- keysList.put(new String(identityKey.serialize()));
- try {
- this.keys.put("axolotl_identity_key", keysList);
- } catch (JSONException e) {
- Log.e(Config.LOGTAG, "Error adding Identity Key to Contact " + this.getJid() + ": " + e.getMessage());
- return false;
- }
+ if(!getAxolotlIdentityKeys().contains(identityKey)) {
+ JSONArray keysList;
+ try {
+ keysList = this.keys.getJSONArray("axolotl_identity_key");
+ } catch (JSONException e) {
+ keysList = new JSONArray();
+ }
+
+ keysList.put(Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT));
+ try {
+ this.keys.put("axolotl_identity_key", keysList);
+ } catch (JSONException e) {
+ Log.e(Config.LOGTAG, "Error adding Identity Key to Contact " + this.getJid() + ": " + e.getMessage());
+ return false;
+ }
+ return true;
+ } else {
+ return false;
+ }
}
- return true;
}