1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Factor out a function for doing olm encryption

Make a library file with some constants and a function to pack olm-encrypted
events (which we are going to use from megolm)
This commit is contained in:
Richard van der Hoff
2016-08-19 16:52:04 +01:00
parent 4877edb79b
commit 0234410b43
5 changed files with 122 additions and 49 deletions

View File

@@ -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
*
@@ -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,
}
)
);
};
/**
@@ -211,4 +190,4 @@ OlmDecryption.prototype.decryptEvent = function(event) {
}
};
base.registerAlgorithm(OLM_ALGORITHM, OlmEncryption, OlmDecryption);
base.registerAlgorithm(olmlib.OLM_ALGORITHM, OlmEncryption, OlmDecryption);