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

Factor out a function for doing olm encryption #177

Merged
merged 1 commit into from
Aug 22, 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
16 changes: 16 additions & 0 deletions lib/OlmDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ OlmDevice.prototype.getSessionIdsForDevice = function(theirDeviceIdentityKey) {
return utils.keys(sessions);
};

/**
* Get the right olm session id for encrypting messages to the given identity key
*
* @param {string} theirDeviceIdentityKey Curve25519 identity key for the
* remote device
* @return {string?} session id, or null if no established session
*/
OlmDevice.prototype.getSessionIdForDevice = function(theirDeviceIdentityKey) {
var sessionIds = this.getSessionIdsForDevice(theirDeviceIdentityKey);
if (sessionIds.length === 0) {
return null;
}
// Use the session with the lowest ID.
sessionIds.sort();
return sessionIds[0];
};

/**
* Encrypt an outgoing message using an existing session
Expand Down
9 changes: 5 additions & 4 deletions lib/crypto-algorithms/megolm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ limitations under the License.
var q = require("q");

var utils = require("../utils");
var olmlib = require("../olmlib");
var base = require("./base");

var MEGOLM_ALGORITHM = "m.megolm.v1.aes-sha2";

/**
* Megolm encryption implementation
*
Expand Down Expand Up @@ -114,7 +113,7 @@ MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) {
);

var encryptedContent = {
algorithm: MEGOLM_ALGORITHM,
algorithm: olmlib.MEGOLM_ALGORITHM,
sender_key: self._olmDevice.deviceCurve25519Key,
body: ciphertext,
session_id: self._outboundSessionId,
Expand Down Expand Up @@ -171,4 +170,6 @@ MegolmDecryption.prototype.decryptEvent = function(event) {
}
};

base.registerAlgorithm(MEGOLM_ALGORITHM, MegolmEncryption, MegolmDecryption);
base.registerAlgorithm(
olmlib.MEGOLM_ALGORITHM, MegolmEncryption, MegolmDecryption
);
59 changes: 19 additions & 40 deletions lib/crypto-algorithms/olm.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ limitations under the License.
var q = require('q');

var utils = require("../utils");
var olmlib = require("../olmlib");

var base = require("./base");

var OLM_ALGORITHM = "m.olm.v1.curve25519-aes-sha2";

/**
* Olm encryption implementation
*
Expand Down Expand Up @@ -89,47 +88,27 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {

for (var keyId in dev.keys) {
if (keyId.indexOf("curve25519:") === 0) {
participantKeys.push(dev.keys[keyId]);
var key = dev.keys[keyId];

// don't send to ourselves.
if (key != this._olmDevice.deviceCurve25519Key) {
participantKeys.push(key);
}
}
}
}
}
participantKeys.sort();
var participantHash = ""; // Olm.sha256(participantKeys.join());
var payloadJson = {
room_id: room.roomId,
type: eventType,
fingerprint: participantHash,
sender_device: this._deviceId,
content: content
};
var ciphertext = {};
var payloadString = JSON.stringify(payloadJson);
for (i = 0; i < participantKeys.length; ++i) {
var deviceKey = participantKeys[i];
if (deviceKey == this._olmDevice.deviceCurve25519Key) {
continue;
}
var sessionIds = this._olmDevice.getSessionIdsForDevice(deviceKey);
// Use the session with the lowest ID.
sessionIds.sort();
if (sessionIds.length === 0) {
// If we don't have a session for a device then
// we can't encrypt a message for it.
continue;
}
var sessionId = sessionIds[0];
console.log("Using sessionid " + sessionId + " for device " + deviceKey);
ciphertext[deviceKey] = this._olmDevice.encryptMessage(
deviceKey, sessionId, payloadString
);
}
var encryptedContent = {
algorithm: OLM_ALGORITHM,
sender_key: this._olmDevice.deviceCurve25519Key,
ciphertext: ciphertext
};
return q(encryptedContent);

return q(
olmlib.encryptMessageForDevices(
this._deviceId, this._olmDevice, participantKeys, {
room_id: room.roomId,
type: eventType,
content: content,
}
)
);

};

/**
Expand Down Expand Up @@ -211,4 +190,4 @@ OlmDecryption.prototype.decryptEvent = function(event) {
}
};

base.registerAlgorithm(OLM_ALGORITHM, OlmEncryption, OlmDecryption);
base.registerAlgorithm(olmlib.OLM_ALGORITHM, OlmEncryption, OlmDecryption);
9 changes: 4 additions & 5 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ var anotherjson = require('another-json');
var q = require("q");

var OlmDevice = require("./OlmDevice");
var olmlib = require("./olmlib");

var algorithms = require("./crypto-algorithms");

var OLM_ALGORITHM = "m.olm.v1.curve25519-aes-sha2";

var DeviceInfo = require("./crypto-deviceinfo");
var DeviceVerification = DeviceInfo.DeviceVerification;

Expand Down Expand Up @@ -69,7 +68,7 @@ function Crypto(baseApis, sessionStore, userId, deviceId) {
// add our own deviceinfo to the sessionstore
var deviceInfo = {
keys: this._deviceKeys,
algorithms: [OLM_ALGORITHM],
algorithms: [olmlib.OLM_ALGORITHM],
verified: DeviceVerification.VERIFIED,
};
var myDevices = this._sessionStore.getEndToEndDevicesForUser(
Expand Down Expand Up @@ -122,7 +121,7 @@ function _uploadDeviceKeys(crypto) {
var deviceId = crypto._deviceId;

var deviceKeys = {
algorithms: [OLM_ALGORITHM],
algorithms: [olmlib.OLM_ALGORITHM],
device_id: deviceId,
keys: crypto._deviceKeys,
user_id: userId,
Expand Down Expand Up @@ -411,7 +410,7 @@ Crypto.prototype.listDeviceKeys = function(userId) {
* @return {module:crypto-deviceinfo?}
*/
Crypto.prototype.getDeviceByIdentityKey = function(userId, algorithm, sender_key) {
if (algorithm !== OLM_ALGORITHM) {
if (algorithm !== olmlib.OLM_ALGORITHM) {
// we only deal in olm keys
return null;
}
Expand Down
78 changes: 78 additions & 0 deletions lib/olmlib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2016 OpenMarket Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
* @module olmlib
*
* Utilities common to olm encryption algorithms
*/

var utils = require("./utils");

/**
* matrix algorithm tag for olm
*/
module.exports.OLM_ALGORITHM = "m.olm.v1.curve25519-aes-sha2";

/**
* matrix algorithm tag for megolm
*/
module.exports.MEGOLM_ALGORITHM = "m.megolm.v1.aes-sha2";


/**
* Encrypt an event payload for a list of devices
*
* @param {string} ourDeviceId
* @param {module:OlmDevice} olmDevice olm.js wrapper
* @param {string[]} participantKeys list of curve25519 keys to encrypt for
* @param {object} payloadFields fields to include in the encrypted payload
*
* @return {object} content for an m.room.encrypted event
*/
module.exports.encryptMessageForDevices = function(
ourDeviceId, olmDevice, participantKeys, payloadFields
) {
participantKeys.sort();
var participantHash = ""; // Olm.sha256(participantKeys.join());
var payloadJson = {
fingerprint: participantHash,
sender_device: ourDeviceId,
};
utils.extend(payloadJson, payloadFields);

var ciphertext = {};
var payloadString = JSON.stringify(payloadJson);
for (var i = 0; i < participantKeys.length; ++i) {
var deviceKey = participantKeys[i];
var sessionId = olmDevice.getSessionIdForDevice(deviceKey);
if (sessionId === null) {
// If we don't have a session for a device then
// we can't encrypt a message for it.
continue;
}
console.log("Using sessionid " + sessionId + " for device " + deviceKey);
ciphertext[deviceKey] = olmDevice.encryptMessage(
deviceKey, sessionId, payloadString
);
}
var encryptedContent = {
algorithm: module.exports.OLM_ALGORITHM,
sender_key: olmDevice.deviceCurve25519Key,
ciphertext: ciphertext
};
return encryptedContent;
};