Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getKeysProved and getKeysClaimed methods to MatrixEvent. #206

Merged
merged 4 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/crypto/OlmDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,21 +514,23 @@ OlmDevice.prototype.getOutboundGroupSessionKey = function(sessionId) {
* store an InboundGroupSession in the session store
*
* @param {string} roomId
* @param {string} senderKey
* @param {string} senderCurve25519Key
* @param {string} sessionId
* @param {Olm.InboundGroupSession} session
* @param {object} keysClaimed Other keys the sender claims.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It's a shame that this doesn't specify the format of the object. But that doesn't matter too much, because it's a private method.
  2. It does't appear to be set by either addInboundGroupSession or decryptGroupMessage.

* @private
*/
OlmDevice.prototype._saveInboundGroupSession = function(
roomId, senderKey, sessionId, session
roomId, senderCurve25519Key, sessionId, session, keysClaimed
) {
var r = {
room_id: roomId,
session: session.pickle(this._pickleKey),
keysClaimed: keysClaimed,
};

this._sessionStore.storeEndToEndInboundGroupSession(
senderKey, sessionId, JSON.stringify(r)
senderCurve25519Key, sessionId, JSON.stringify(r)
);
};

Expand Down Expand Up @@ -569,7 +571,12 @@ OlmDevice.prototype._getInboundGroupSession = function(
var session = new Olm.InboundGroupSession();
try {
session.unpickle(this._pickleKey, r.session);
return {sessionExists: true, result: func(session)};
return {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jsdoc is now a lie.

sessionExists: true,
result: func(session),
keysProved: {curve25519: senderKey},
keysClaimed: r.keysClaimed || {},
};
} finally {
session.free();
}
Expand Down
7 changes: 4 additions & 3 deletions lib/crypto/algorithms/megolm.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ utils.inherits(MegolmDecryption, base.DecryptionAlgorithm);
* @param {object} event raw event
*
* @return {object} object with 'result' key with decrypted payload (with
* properties 'type', 'content') and a 'sessionKey' key.
* properties 'type', 'content') and a 'sessionExists' key.
*
* @throws {module:crypto/algorithms/base.DecryptionError} if there is a
* problem decrypting the event
Expand All @@ -382,7 +382,8 @@ MegolmDecryption.prototype.decryptEvent = function(event) {
event.room_id, content.sender_key, content.session_id, content.ciphertext
);
if (res.sessionExists) {
return {result: JSON.parse(res.result), sessionExists: true};
res.result = JSON.parse(res.result);
return res;
} else {
return {sessionExists: false};
}
Expand Down Expand Up @@ -411,7 +412,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {

this._olmDevice.addInboundGroupSession(
content.room_id, event.getSenderKey(), content.session_id,
content.session_key, content.chain_index
content.session_key, content.chain_index, event.getKeysClaimed()
);
};

Expand Down
19 changes: 18 additions & 1 deletion lib/crypto/algorithms/olm.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {
room_id: room.roomId,
type: eventType,
content: content,
// Include the ED25519 key so that the recipient knows what
// device this message came from.
// We don't need to include the curve25519 key since the
// recipient will already know this from the olm headers.
// When combined with the device keys retrieved from the
// homeserver signed by the ed25519 key this proves that
// the curve25519 key and the ed25519 key are owned by
// the same device.
keys: {
"ed25519": self._olmDevice.deviceEd25519Key
},
}
);
});
Expand Down Expand Up @@ -200,7 +211,13 @@ OlmDecryption.prototype.decryptEvent = function(event) {
// TODO: Check the sender user id matches the sender key.
// TODO: check the room_id and fingerprint
if (payloadString !== null) {
return {result: JSON.parse(payloadString), sessionExists: true};
var payload = JSON.parse(payloadString);
return {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, the jsdoc is now a lie

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see also algorithms/base.js which has yet another copy of the jsdoc. sorry.)

result: payload,
sessionExists: true,
keysProved: {curve25519: deviceKey},
keysClaimed: payload.keys || {}
};
} else {
throw new base.DecryptionError("Bad Encrypted Message");
}
Expand Down
5 changes: 4 additions & 1 deletion lib/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,11 @@ Crypto.prototype.decryptEvent = function(event) {
olmDevice: this._olmDevice,
});
var r = alg.decryptEvent(event);
var payload = r.result;
payload.keysClaimed = r.keysClaimed;
payload.keysProved = r.keysProved;
if (r.sessionExists) {
return r.result;
return payload;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdoc needs an update

} else {
// We've got a message for a session we don't have.
// Maybe the sender forgot to tell us about the session.
Expand Down
26 changes: 21 additions & 5 deletions lib/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,28 @@ module.exports.MatrixEvent.prototype = {
return Boolean(this._clearEvent.type);
},

/**
* The curve25519 key that sent this event
* @return {string}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/string/string?/

*/
getSenderKey: function() {
if (!this.isEncrypted()) {
return null;
}
var c = this.getWireContent();
return c.sender_key;
return this.getKeysProved().curve25519 || null;
},

/**
* The keys that must have been owned by the sender of this encrypted event.
* @return {object}
*/
getKeysProved: function() {
return this._clearEvent.keysProved || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not happy with messing about with the contents of _clearEvent. It's supposed to be exactly what was sent on the wire. keysProved and keysClaimed should be in different properties.

},

/**
* The additional keys the sender of this encrypted event claims to possess
* @return {object}
*/
getKeysClaimed: function() {
return this._clearEvent.keysClaimed || {};
},

getUnsigned: function() {
Expand Down