diff --git a/briar-android/src/org/briarproject/android/contact/ConversationActivity.java b/briar-android/src/org/briarproject/android/contact/ConversationActivity.java index 747c08caafb6398ce53dbd3a856dc29cc3ea41a6..d10a3d31cc0631683cc96e0c52bdc9958ada49fd 100644 --- a/briar-android/src/org/briarproject/android/contact/ConversationActivity.java +++ b/briar-android/src/org/briarproject/android/contact/ConversationActivity.java @@ -663,9 +663,9 @@ public class ConversationActivity extends BriarActivity public void run() { try { if (accept) { - introductionManager.acceptIntroduction(sessionId); + introductionManager.acceptIntroduction(contactId, sessionId); } else { - introductionManager.declineIntroduction(sessionId); + introductionManager.declineIntroduction(contactId, sessionId); } loadMessages(); } catch (DbException e) { diff --git a/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java b/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java index 882ccbf112a67be92c15f711fbc869384a3cb029..f83581ed7bab584dc006f85218197a233347ab94 100644 --- a/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java +++ b/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java @@ -8,6 +8,7 @@ import org.briarproject.api.db.DbException; import org.briarproject.api.db.Transaction; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.Group; +import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.MessageId; import java.util.Collection; @@ -26,14 +27,14 @@ public interface IntroductionManager { /** * Accept an introduction that had been made */ - void acceptIntroduction(final SessionId sessionId) - throws DbException, FormatException; + void acceptIntroduction(final ContactId contactId, + final SessionId sessionId) throws DbException, FormatException; /** * Decline an introduction that had been made */ - void declineIntroduction(final SessionId sessionId) - throws DbException, FormatException; + void declineIntroduction(final ContactId contactId, + final SessionId sessionId) throws DbException, FormatException; /** * Get all introduction messages for the contact with this contactId @@ -46,8 +47,8 @@ public interface IntroductionManager { /** Get the session state for the given session ID */ - BdfDictionary getSessionState(Transaction txn, byte[] sessionId) - throws DbException, FormatException; + BdfDictionary getSessionState(Transaction txn, GroupId groupId, + byte[] sessionId) throws DbException, FormatException; /** Gets the group used for introductions with Contact c */ Group getIntroductionGroup(Contact c); diff --git a/briar-core/src/org/briarproject/introduction/IntroduceeManager.java b/briar-core/src/org/briarproject/introduction/IntroduceeManager.java index be16ff1e495206ec06006de66e8e4708b808a19c..174d2af21ab841f577b689eec6a83680100d52b4 100644 --- a/briar-core/src/org/briarproject/introduction/IntroduceeManager.java +++ b/briar-core/src/org/briarproject/introduction/IntroduceeManager.java @@ -28,6 +28,7 @@ import org.briarproject.api.introduction.IntroductionManager; import org.briarproject.api.introduction.SessionId; import org.briarproject.api.properties.TransportProperties; import org.briarproject.api.properties.TransportPropertyManager; +import org.briarproject.api.sync.Group; import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; @@ -157,11 +158,14 @@ class IntroduceeManager { processStateUpdate(txn, engine.onMessageReceived(state, message)); } - public void acceptIntroduction(Transaction txn, + public void acceptIntroduction(Transaction txn, final ContactId contactId, final SessionId sessionId) throws DbException, FormatException { - BdfDictionary state = - introductionManager.getSessionState(txn, sessionId.getBytes()); + Contact c = contactManager.getContact(contactId); + Group g = introductionManager.getIntroductionGroup(c); + + BdfDictionary state = introductionManager + .getSessionState(txn, g.getId(), sessionId.getBytes()); // get data to connect and derive a shared secret later long now = clock.currentTimeMillis(); @@ -188,11 +192,14 @@ class IntroduceeManager { processStateUpdate(txn, engine.onLocalAction(state, localAction)); } - public void declineIntroduction(Transaction txn, final SessionId sessionId) - throws DbException, FormatException { + public void declineIntroduction(Transaction txn, final ContactId contactId, + final SessionId sessionId) throws DbException, FormatException { + + Contact c = contactManager.getContact(contactId); + Group g = introductionManager.getIntroductionGroup(c); - BdfDictionary state = - introductionManager.getSessionState(txn, sessionId.getBytes()); + BdfDictionary state = introductionManager + .getSessionState(txn, g.getId(), sessionId.getBytes()); // update session state state.put(ACCEPT, false); diff --git a/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java b/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java index e28af29e4d1c0ffbc886e342739d6afc977d3b98..30cfe7083f8375c59fed663539a27d3658bb8c32 100644 --- a/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java +++ b/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java @@ -62,6 +62,8 @@ import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_ID import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_ID_2; import static org.briarproject.api.introduction.IntroductionConstants.EXISTS; import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID; +import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_1; +import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_2; import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_TIME; import static org.briarproject.api.introduction.IntroductionConstants.MSG; import static org.briarproject.api.introduction.IntroductionConstants.NAME; @@ -227,7 +229,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook else if (type == TYPE_RESPONSE || type == TYPE_ACK || type == TYPE_ABORT) { BdfDictionary state; try { - state = getSessionState(txn, + state = getSessionState(txn, groupId, message.getRaw(SESSION_ID, new byte[0])); } catch (FormatException e) { LOG.warning("Could not find state for message, deleting..."); @@ -279,12 +281,12 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook } @Override - public void acceptIntroduction(final SessionId sessionId) - throws DbException, FormatException { + public void acceptIntroduction(final ContactId contactId, + final SessionId sessionId) throws DbException, FormatException { Transaction txn = db.startTransaction(false); try { - introduceeManager.acceptIntroduction(txn, sessionId); + introduceeManager.acceptIntroduction(txn, contactId, sessionId); txn.setComplete(); } finally { db.endTransaction(txn); @@ -292,12 +294,12 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook } @Override - public void declineIntroduction(final SessionId sessionId) - throws DbException, FormatException { + public void declineIntroduction(final ContactId contactId, + final SessionId sessionId) throws DbException, FormatException { Transaction txn = db.startTransaction(false); try { - introduceeManager.declineIntroduction(txn, sessionId); + introduceeManager.declineIntroduction(txn, contactId, sessionId); txn.setComplete(); } finally { db.endTransaction(txn); @@ -322,8 +324,6 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook statuses = db.getMessageStatus(txn, contactId, g); // turn messages into classes for the UI - Map<SessionId, BdfDictionary> sessionStates = - new HashMap<SessionId, BdfDictionary>(); for (MessageStatus s : statuses) { MessageId messageId = s.getMessageId(); BdfDictionary msg = metadata.get(messageId); @@ -335,11 +335,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook // get session state SessionId sessionId = new SessionId(msg.getRaw(SESSION_ID)); - BdfDictionary state = sessionStates.get(sessionId); - if (state == null) { - state = getSessionState(txn, sessionId.getBytes()); - } - sessionStates.put(sessionId, state); + BdfDictionary state = + getSessionState(txn, g, sessionId.getBytes()); boolean local; long time = msg.getLong(MESSAGE_TIME); @@ -453,19 +450,31 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook } } - public BdfDictionary getSessionState(Transaction txn, byte[] sessionId) - throws DbException, FormatException { + public BdfDictionary getSessionState(Transaction txn, GroupId groupId, + byte[] sessionId) throws DbException, FormatException { try { - return clientHelper.getMessageMetadataAsDictionary(txn, - new MessageId(sessionId)); + // See if we can find the state directly for the introducer + BdfDictionary state = clientHelper + .getMessageMetadataAsDictionary(txn, + new MessageId(sessionId)); + GroupId g1 = new GroupId(state.getRaw(GROUP_ID_1)); + GroupId g2 = new GroupId(state.getRaw(GROUP_ID_2)); + if (!g1.equals(groupId) && !g2.equals(groupId)) { + throw new NoSuchMessageException(); + } + return state; } catch (NoSuchMessageException e) { + // State not found directly, so iterate over all states + // to find state for introducee Map<MessageId, BdfDictionary> map = clientHelper .getMessageMetadataAsDictionary(txn, localGroup.getId()); for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) { if (Arrays.equals(m.getValue().getRaw(SESSION_ID), sessionId)) { - return m.getValue(); + BdfDictionary state = m.getValue(); + GroupId g = new GroupId(state.getRaw(GROUP_ID)); + if (g.equals(groupId)) return state; } } if (LOG.isLoggable(WARNING)) {