Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
add_room_key_request
Browse files Browse the repository at this point in the history
  • Loading branch information
ylecollen committed Oct 25, 2017
1 parent 628f49b commit 7d0c224
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -517,11 +518,12 @@ public String encryptGroupMessage(String sessionId, String payloadString) {
* @param sessionKey base64-encoded secret key.
* @param roomId the id of the room in which this session will be used.
* @param senderKey the base64-encoded curve25519 key of the sender.
* @param forwardingCurve25519KeyChain Devices involved in forwarding this session to us.
* @param keysClaimed Other keys the sender claims.
* @param exportFormat true if the megolm keys are in export format
* @return true if the operation succeeds.
*/
public boolean addInboundGroupSession(String sessionId, String sessionKey, String roomId, String senderKey, Map<String, String> keysClaimed, boolean exportFormat) {
public boolean addInboundGroupSession(String sessionId, String sessionKey, String roomId, String senderKey, List<String> forwardingCurve25519KeyChain, Map<String, String> keysClaimed, boolean exportFormat) {
if (null != getInboundGroupSession(sessionId, senderKey, roomId)) {
// If we already have this session, consider updating it
Log.e(LOG_TAG, "## addInboundGroupSession() : Update for megolm session " + senderKey + "/" + sessionId);
Expand Down Expand Up @@ -551,6 +553,7 @@ public boolean addInboundGroupSession(String sessionId, String sessionKey, Strin
session.mSenderKey = senderKey;
session.mRoomId = roomId;
session.mKeysClaimed = keysClaimed;
session.mForwardingCurve25519KeyChain = forwardingCurve25519KeyChain;

mStore.storeInboundGroupSession(session);

Expand Down Expand Up @@ -759,7 +762,7 @@ private OlmSession getSessionForDevice(String theirDeviceIdentityKey, String ses
* @param senderKey the base64-encoded curve25519 key of the sender.
* @return the inbound group session.
*/
private MXOlmInboundGroupSession2 getInboundGroupSession(String sessionId, String senderKey, String roomId) {
public MXOlmInboundGroupSession2 getInboundGroupSession(String sessionId, String senderKey, String roomId) {
mInboundGroupSessionWithIdError = null;

MXOlmInboundGroupSession2 session = mStore.getInboundGroupSession(sessionId, senderKey);
Expand Down Expand Up @@ -790,26 +793,4 @@ private MXOlmInboundGroupSession2 getInboundGroupSession(String sessionId, Strin
public boolean hasInboundSessionKeys(String roomId, String senderKey, String sessionId) {
return null != getInboundGroupSession(sessionId, senderKey, roomId);
}


/**
* Extract the keys to a given megolm session, for sharing
* @param roomId room in which the message was received
* @param senderKey Base64-encoded curve25519 key of the sender
* @param sessionId session identifier
* @return a Pair<chain_index, key is a base64-encoded megolm key in export format>
*/
public Pair<Long, String> getInboundGroupSessionKey(String roomId, String senderKey, String sessionId) {
MXOlmInboundGroupSession2 session = getInboundGroupSession(sessionId, senderKey, roomId);

if (null != session) {
Long index = session.getFirstKnownIndex();

if (null != index) {
return new Pair<>(index, session.exportSession(index));
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.matrix.androidsdk.crypto.IncomingRoomKeyRequest;
import org.matrix.androidsdk.crypto.MXCryptoAlgorithms;
import org.matrix.androidsdk.crypto.data.MXDeviceInfo;
import org.matrix.androidsdk.crypto.data.MXOlmInboundGroupSession2;
import org.matrix.androidsdk.crypto.data.MXOlmSessionResult;
import org.matrix.androidsdk.crypto.data.MXUsersDevicesMap;
import org.matrix.androidsdk.rest.callback.ApiCallback;
Expand All @@ -43,6 +44,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -178,6 +180,7 @@ public void onRoomKeyEvent(Event roomKeyEvent) {
String sessionKey = roomKeyContent.session_key;
String senderKey = roomKeyEvent.senderKey();
Map<String, String> keysClaimed = new HashMap<>();
List<String> forwarding_curve25519_key_chain = null;

if (TextUtils.isEmpty(roomId) || TextUtils.isEmpty(sessionId) || TextUtils.isEmpty(sessionKey)) {
Log.e(LOG_TAG, "## onRoomKeyEvent() : Key event is missing fields");
Expand All @@ -188,12 +191,29 @@ public void onRoomKeyEvent(Event roomKeyEvent) {
Log.d(LOG_TAG, "## onRoomKeyEvent(), Adding key : roomId " + roomId + " sessionId " + sessionId + " sessionKey " + sessionKey); // from " + event);
ForwardedRoomKeyContent forwardedRoomKeyContent = JsonUtils.toForwardedRoomKeyContent(roomKeyEvent.getContentAsJsonObject());

if (null == forwarding_curve25519_key_chain) {
forwarding_curve25519_key_chain = new ArrayList<>();
} else {
forwarding_curve25519_key_chain = new ArrayList<>(forwardedRoomKeyContent.forwarding_curve25519_key_chain);
}

forwarding_curve25519_key_chain.add(senderKey);

exportFormat = true;
senderKey = forwardedRoomKeyContent.sender_key;
if (null == senderKey) {
Log.e(LOG_TAG, "## onRoomKeyEvent() : forwarded_room_key event is missing sender_key field");
return;
}

String ed25519Key = forwardedRoomKeyContent.sender_claimed_ed25519_key;

if (null == ed25519Key) {
Log.e(LOG_TAG , "## forwarded_room_key_event is missing sender_claimed_ed25519_key field");
return;
}

keysClaimed.put("ed25519", ed25519Key);
} else {
Log.d(LOG_TAG, "## onRoomKeyEvent(), Adding key : roomId " + roomId + " sessionId " + sessionId + " sessionKey " + sessionKey); // from " + event);

Expand All @@ -206,7 +226,7 @@ public void onRoomKeyEvent(Event roomKeyEvent) {
keysClaimed = roomKeyEvent.getKeysClaimed();
}

mOlmDevice.addInboundGroupSession(sessionId, sessionKey, roomId, senderKey, keysClaimed, exportFormat);
mOlmDevice.addInboundGroupSession(sessionId, sessionKey, roomId, senderKey, forwarding_curve25519_key_chain, keysClaimed, exportFormat);
onNewSession(roomKeyEvent.senderKey(), sessionId);
}

Expand Down Expand Up @@ -288,20 +308,11 @@ public void onSuccess(MXUsersDevicesMap<MXOlmSessionResult> map) {

Log.d(LOG_TAG, "## shareKeysWithDevice() : sharing keys for session " + body.sender_key + "|" + body.session_id + " with device " + userId + ":" + deviceId);

Pair<Long, String> key = mSession.getCrypto().getOlmDevice().getInboundGroupSessionKey(body.room_id, body.sender_key, body.session_id);
MXOlmInboundGroupSession2 inboundGroupSession = mSession.getCrypto().getOlmDevice().getInboundGroupSession(body.room_id, body.sender_key, body.session_id);

Map<String, Object> payloadJson = new HashMap<>();
payloadJson.put("type", Event.EVENT_TYPE_FORWARDED_ROOM_KEY);

Map<String, Object> contentMap = new HashMap<>();
payloadJson.put("content", contentMap);

contentMap.put("algorithm", MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_MEGOLM);
contentMap.put("room_id", body.room_id);
contentMap.put("sender_key", body.sender_key);
contentMap.put("session_id", body.session_id);
contentMap.put("session_key", key.second);
contentMap.put("chain_index", key.first);
payloadJson.put("content", inboundGroupSession.exportKeys());

Map<String, Object> encodedPayload = mSession.getCrypto().encryptMessage(payloadJson, Arrays.asList(deviceInfo));
MXUsersDevicesMap<Map<String, Object>> sendToDeviceMap = new MXUsersDevicesMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private MXOutboundSessionInfo prepareNewSessionInRoom() {
HashMap<String, String> keysClaimedMap = new HashMap<>();
keysClaimedMap.put("ed25519", olmDevice.getDeviceEd25519Key());

olmDevice.addInboundGroupSession(sessionId, olmDevice.getSessionKey(sessionId), mRoomId, olmDevice.getDeviceCurve25519Key(), keysClaimedMap, false);
olmDevice.addInboundGroupSession(sessionId, olmDevice.getSessionKey(sessionId), mRoomId, olmDevice.getDeviceCurve25519Key(), new ArrayList<String>(), keysClaimedMap, false);

return new MXOutboundSessionInfo(sessionId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


Expand All @@ -53,6 +55,8 @@ public class MXOlmInboundGroupSession2 implements Serializable {
// Other keys the sender claims.
public Map<String, String> mKeysClaimed;

// Devices which forwarded this session to us (normally empty).
public List<String> mForwardingCurve25519KeyChain = new ArrayList<>();

/**
* Constructor
Expand Down Expand Up @@ -109,6 +113,12 @@ public Map<String, Object> exportKeys() {
HashMap<String, Object> map = new HashMap<>();

try {
if (null == mForwardingCurve25519KeyChain) {
mForwardingCurve25519KeyChain = new ArrayList<>();
}

map.put("sender_claimed_ed25519_key", mKeysClaimed.get("ed25519"));
map.put("forwardingCurve25519KeyChain", mForwardingCurve25519KeyChain);
map.put("sender_key", mSenderKey);
map.put("sender_claimed_keys", mKeysClaimed);
map.put("room_id", mRoomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.matrix.androidsdk.rest.model;

import java.util.List;

/**
* Class representing the forward room key request body content
*/
Expand All @@ -28,4 +30,8 @@ public class ForwardedRoomKeyContent implements java.io.Serializable {
public String session_id;

public String session_key;

public List<String> forwarding_curve25519_key_chain;

public String sender_claimed_ed25519_key;
}

0 comments on commit 7d0c224

Please sign in to comment.