aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/entities
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-02-16 16:32:15 +0100
committerDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-02-16 16:32:15 +0100
commit9c7cacdbddf6ba1a34fd56b71718ba4e44e20efa (patch)
treea9f138b3bd8395b3a198235a1c8d559d71347ef4 /src/de/gultsch/chat/entities
parent42c4c1789a75c87b67c38ef9ca3f57ddd10f0548 (diff)
show unknown otr fingerprint warining
Diffstat (limited to 'src/de/gultsch/chat/entities')
-rw-r--r--src/de/gultsch/chat/entities/Account.java24
-rw-r--r--src/de/gultsch/chat/entities/Contact.java71
-rw-r--r--src/de/gultsch/chat/entities/Conversation.java44
-rw-r--r--src/de/gultsch/chat/entities/Message.java4
4 files changed, 127 insertions, 16 deletions
diff --git a/src/de/gultsch/chat/entities/Account.java b/src/de/gultsch/chat/entities/Account.java
index 7f14b090..1bced45f 100644
--- a/src/de/gultsch/chat/entities/Account.java
+++ b/src/de/gultsch/chat/entities/Account.java
@@ -1,5 +1,10 @@
package de.gultsch.chat.entities;
+import java.security.interfaces.DSAPublicKey;
+
+import net.java.otr4j.crypto.OtrCryptoEngineImpl;
+import net.java.otr4j.crypto.OtrCryptoException;
+
import org.json.JSONException;
import org.json.JSONObject;
@@ -48,6 +53,8 @@ public class Account extends AbstractEntity{
transient OtrEngine otrEngine = null;
transient XmppConnection xmppConnection = null;
+
+ private String otrFingerprint;
public Account() {
this.uuid = "0";
@@ -177,4 +184,21 @@ public class Account extends AbstractEntity{
public String getFullJid() {
return this.getJid()+"/"+this.resource;
}
+
+ public String getOtrFingerprint() {
+ if (this.otrFingerprint == null) {
+ try {
+ DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
+ StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
+ builder.insert(8, " ");
+ builder.insert(17, " ");
+ builder.insert(26, " ");
+ builder.insert(35, " ");
+ this.otrFingerprint = builder.toString();
+ } catch (OtrCryptoException e) {
+
+ }
+ }
+ return this.otrFingerprint;
+ }
}
diff --git a/src/de/gultsch/chat/entities/Contact.java b/src/de/gultsch/chat/entities/Contact.java
index 9c987e09..5d5710a5 100644
--- a/src/de/gultsch/chat/entities/Contact.java
+++ b/src/de/gultsch/chat/entities/Contact.java
@@ -1,7 +1,13 @@
package de.gultsch.chat.entities;
import java.io.Serializable;
+import java.util.HashSet;
import java.util.Hashtable;
+import java.util.Set;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
import android.content.ContentValues;
import android.database.Cursor;
@@ -16,7 +22,7 @@ public class Contact extends AbstractEntity implements Serializable {
public static final String SUBSCRIPTION = "subscription";
public static final String SYSTEMACCOUNT = "systemaccount";
public static final String PHOTOURI = "photouri";
- public static final String OPENPGPKEY = "pgpkey";
+ public static final String KEYS = "pgpkey";
public static final String PRESENCES = "presences";
public static final String ACCOUNT = "accountUuid";
@@ -26,7 +32,7 @@ public class Contact extends AbstractEntity implements Serializable {
protected String subscription;
protected String systemAccount;
protected String photoUri;
- protected String openPGPKey;
+ protected JSONObject keys;
protected Presences presences = new Presences();
protected Account account;
@@ -45,7 +51,7 @@ public class Contact extends AbstractEntity implements Serializable {
public Contact(String uuid, String account, String displayName, String jid,
String subscription, String photoUri, String systemAccount,
- String pgpKey,String presences) {
+ String keys, String presences) {
this.uuid = uuid;
this.accountUuid = account;
this.displayName = displayName;
@@ -53,7 +59,14 @@ public class Contact extends AbstractEntity implements Serializable {
this.subscription = subscription;
this.photoUri = photoUri;
this.systemAccount = systemAccount;
- this.openPGPKey = pgpKey;
+ if (keys == null) {
+ keys = "";
+ }
+ try {
+ this.keys = new JSONObject(keys);
+ } catch (JSONException e) {
+ this.keys = new JSONObject();
+ }
this.presences = Presences.fromJsonString(presences);
}
@@ -84,7 +97,7 @@ public class Contact extends AbstractEntity implements Serializable {
values.put(SUBSCRIPTION, subscription);
values.put(SYSTEMACCOUNT, systemAccount);
values.put(PHOTOURI, photoUri);
- values.put(OPENPGPKEY, openPGPKey);
+ values.put(KEYS, keys.toString());
values.put(PRESENCES, presences.toJsonString());
return values;
}
@@ -97,14 +110,14 @@ public class Contact extends AbstractEntity implements Serializable {
cursor.getString(cursor.getColumnIndex(SUBSCRIPTION)),
cursor.getString(cursor.getColumnIndex(PHOTOURI)),
cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
- cursor.getString(cursor.getColumnIndex(OPENPGPKEY)),
+ cursor.getString(cursor.getColumnIndex(KEYS)),
cursor.getString(cursor.getColumnIndex(PRESENCES)));
}
public void setSubscription(String subscription) {
this.subscription = subscription;
}
-
+
public String getSubscription() {
return this.subscription;
}
@@ -141,11 +154,11 @@ public class Contact extends AbstractEntity implements Serializable {
}
}
}
-
+
public Hashtable<String, Integer> getPresences() {
return this.presences.getPresences();
}
-
+
public void updatePresence(String resource, int status) {
this.presences.updatePresence(resource, status);
}
@@ -153,19 +166,19 @@ public class Contact extends AbstractEntity implements Serializable {
public void removePresence(String resource) {
this.presences.removePresence(resource);
}
-
+
public int getMostAvailableStatus() {
return this.presences.getMostAvailableStatus();
}
public void setPresences(Presences pres) {
- this.presences = pres;
+ this.presences = pres;
}
-
+
public void setPhotoUri(String uri) {
this.photoUri = uri;
}
-
+
public void setDisplayName(String name) {
this.displayName = name;
}
@@ -173,4 +186,36 @@ public class Contact extends AbstractEntity implements Serializable {
public String getSystemAccount() {
return systemAccount;
}
+
+ public Set<String> getOtrFingerprints() {
+ Set<String> set = new HashSet<String>();
+ try {
+ if (this.keys.has("otr_fingerprints")) {
+ JSONArray fingerprints = this.keys.getJSONArray("otr_fingerprints");
+ for (int i = 0; i < fingerprints.length(); ++i) {
+ set.add(fingerprints.getString(i));
+ }
+ }
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return set;
+ }
+
+ public void addOtrFingerprint(String print) {
+ try {
+ JSONArray fingerprints;
+ if (!this.keys.has("otr_fingerprints")) {
+ fingerprints = new JSONArray();
+
+ } else {
+ fingerprints = this.keys.getJSONArray("otr_fingerprints");
+ }
+ fingerprints.put(print);
+ this.keys.put("otr_fingerprints", fingerprints);
+ } catch (JSONException e) {
+
+ }
+ }
}
diff --git a/src/de/gultsch/chat/entities/Conversation.java b/src/de/gultsch/chat/entities/Conversation.java
index 7339aadb..343e83a4 100644
--- a/src/de/gultsch/chat/entities/Conversation.java
+++ b/src/de/gultsch/chat/entities/Conversation.java
@@ -1,5 +1,6 @@
package de.gultsch.chat.entities;
+import java.security.interfaces.DSAPublicKey;
import java.util.ArrayList;
import java.util.List;
@@ -7,6 +8,8 @@ import de.gultsch.chat.crypto.OtrEngine;
import de.gultsch.chat.xmpp.XmppConnection;
import net.java.otr4j.OtrException;
+import net.java.otr4j.crypto.OtrCryptoEngineImpl;
+import net.java.otr4j.crypto.OtrCryptoException;
import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus;
@@ -51,7 +54,10 @@ public class Conversation extends AbstractEntity {
private transient Contact contact;
private transient SessionImpl otrSession;
- private transient String foreignOtrPresence;
+
+ private transient String otrFingerprint = null;
+
+ public int nextMessageEncryption = Message.ENCRYPTION_NONE;
public Conversation(String name, Account account,
String contactJid, int mode) {
@@ -209,6 +215,11 @@ public class Conversation extends AbstractEntity {
Log.d("xmppService","starting otr session with "+presence);
SessionID sessionId = new SessionID(this.getContactJid(),presence,"xmpp");
this.otrSession = new SessionImpl(sessionId, getAccount().getOtrEngine(context));
+ try {
+ this.otrSession.startSession();
+ } catch (OtrException e) {
+ Log.d("xmppServic","couldnt start otr");
+ }
}
public SessionImpl getOtrSession() {
@@ -225,9 +236,36 @@ public class Conversation extends AbstractEntity {
this.otrSession.endSession();
}
}
+ this.resetOtrSession();
}
- public boolean hasOtrSession() {
- return (this.otrSession!=null);
+ public boolean hasValidOtrSession() {
+ if (this.otrSession == null) {
+ return false;
+ } else {
+ String foreignPresence = this.otrSession.getSessionID().getUserID();
+ if (!getContact().getPresences().containsKey(foreignPresence)) {
+ this.resetOtrSession();
+ return false;
+ }
+ return true;
+ }
+ }
+
+ public String getOtrFingerprint() {
+ if (this.otrFingerprint == null) {
+ try {
+ DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession().getRemotePublicKey();
+ StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(remotePubKey));
+ builder.insert(8, " ");
+ builder.insert(17, " ");
+ builder.insert(26, " ");
+ builder.insert(35, " ");
+ this.otrFingerprint = builder.toString();
+ } catch (OtrCryptoException e) {
+
+ }
+ }
+ return this.otrFingerprint;
}
}
diff --git a/src/de/gultsch/chat/entities/Message.java b/src/de/gultsch/chat/entities/Message.java
index a168c645..73a6e46c 100644
--- a/src/de/gultsch/chat/entities/Message.java
+++ b/src/de/gultsch/chat/entities/Message.java
@@ -131,5 +131,9 @@ public class Message extends AbstractEntity {
public void setTime(long time) {
this.timeSent = time;
}
+
+ public void setEncryption(int encryption) {
+ this.encryption = encryption;
+ }
}