Skip to content

Commit e03809b

Browse files
committed
Merge pull request element-hq#16 from matrix-org/markjh/end-to-end
Add basic support for end-to-end crypto using olm.
2 parents 6fc910a + f036a10 commit e03809b

6 files changed

Lines changed: 101 additions & 4 deletions

File tree

‎skins/base/views/molecules/UnknownMessageTile.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ module.exports = React.createClass({
2525
mixins: [UnknownMessageTileController],
2626

2727
render: function() {
28+
var content = this.props.mxEvent.getContent();
2829
return (
2930
<span className="mx_UnknownMessageTile">
30-
?
31+
{content.body}
3132
</span>
3233
);
3334
},

‎skins/base/views/organisms/CreateRoom.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ module.exports = React.createClass({
118118
})
119119
},
120120

121+
onEncryptChanged: function(ev) {
122+
this.setState({
123+
encrypt: ev.target.checked,
124+
});
125+
},
126+
121127
render: function() {
122128
var curr_phase = this.state.phase;
123129
if (curr_phase == this.phases.CREATING) {
@@ -142,7 +148,7 @@ module.exports = React.createClass({
142148
<Presets ref="presets" onChange={this.onPresetChanged} preset={this.state.preset}/> <br />
143149
<label><input type="checkbox" ref="is_private" checked={this.state.is_private} onChange={this.onPrivateChanged}/> Make this room private</label>
144150
<label><input type="checkbox" ref="share_history" checked={this.state.share_history} onChange={this.onShareHistoryChanged}/> Share message history with new users</label>
145-
<label><input type="checkbox"/> Encrypt room</label>
151+
<label><input type="checkbox" ref="encrypt" checked={this.state.encrypt} onChange={this.onEncryptChanged}/> Encrypt room</label>
146152
<CreateRoomButton onCreateRoom={this.onCreateRoom} /> <br />
147153
{error_box}
148154
</div>

‎src/MatrixClientPeg.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ var matrixClient = null;
2323

2424
var localStorage = window.localStorage;
2525

26+
function deviceId() {
27+
var id = Math.floor(Math.random()*16777215).toString(16);
28+
id = "W" + "000000".substring(id.length) + id;
29+
if (localStorage) {
30+
id = localStorage.getItem("mx_device_id") || id;
31+
localStorage.setItem("mx_device_id", id);
32+
}
33+
return id;
34+
}
35+
2636
function createClient(hs_url, is_url, user_id, access_token) {
2737
var opts = {
2838
baseUrl: hs_url,
@@ -31,6 +41,11 @@ function createClient(hs_url, is_url, user_id, access_token) {
3141
userId: user_id
3242
};
3343

44+
if (localStorage) {
45+
opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage);
46+
opts.deviceId = deviceId();
47+
}
48+
3449
matrixClient = Matrix.createClient(opts);
3550
}
3651

‎src/SlashCommands.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818

1919
var MatrixClientPeg = require("./MatrixClientPeg");
2020
var dis = require("./dispatcher");
21+
var encryption = require("./encryption");
2122

2223
var reject = function(msg) {
2324
return {
@@ -42,6 +43,25 @@ var commands = {
4243
return reject("Usage: /nick <display_name>");
4344
},
4445

46+
encrypt: function(room_id, args) {
47+
if (args == "on") {
48+
var client = MatrixClientPeg.get();
49+
var members = client.getRoom(room_id).currentState.members;
50+
var user_ids = Object.keys(members);
51+
return success(
52+
encryption.enableEncryption(client, room_id, user_ids)
53+
);
54+
}
55+
if (args == "off") {
56+
var client = MatrixClientPeg.get();
57+
return success(
58+
encryption.disableEncryption(client, room_id)
59+
);
60+
61+
}
62+
return reject("Usage: encrypt <on/off>");
63+
},
64+
4565
// Change the room topic
4666
topic: function(room_id, args) {
4767
if (args) {
@@ -230,4 +250,4 @@ module.exports = {
230250
}
231251
return null; // not a command
232252
}
233-
};
253+
};

‎src/controllers/organisms/CreateRoom.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ limitations under the License.
1919
var React = require("react");
2020
var MatrixClientPeg = require("../../MatrixClientPeg");
2121
var PresetValues = require('../atoms/create_room/Presets').Presets;
22+
var q = require('q');
23+
var encryption = require("../../encryption");
2224

2325
module.exports = {
2426
propTypes: {
@@ -97,7 +99,20 @@ module.exports = {
9799
return;
98100
}
99101

100-
var deferred = MatrixClientPeg.get().createRoom(options);
102+
var deferred = cli.createRoom(options);
103+
104+
var response;
105+
106+
if (this.state.encrypt) {
107+
deferred = deferred.then(function(res) {
108+
response = res;
109+
return encryption.enableEncryption(
110+
cli, response.roomId, options.invite
111+
);
112+
}).then(function() {
113+
return q(response) }
114+
);
115+
}
101116

102117
this.setState({
103118
phase: this.phases.CREATING,

‎src/encryption.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
function enableEncyption(client, roomId, members) {
20+
members = members.slice(0);
21+
members.push(client.credentials.userId);
22+
// TODO: Check the keys actually match what keys the user has.
23+
// TODO: Don't redownload keys each time.
24+
return client.downloadKeys(members, "forceDownload").then(function(res) {
25+
return client.setRoomEncryption(roomId, {
26+
algorithm: "m.olm.v1.curve25519-aes-sha2",
27+
members: members,
28+
});
29+
})
30+
}
31+
32+
function disableEncryption(client, roomId) {
33+
return client.disableRoomEncryption(roomId);
34+
}
35+
36+
37+
module.exports = {
38+
enableEncryption: enableEncyption,
39+
disableEncryption: disableEncryption,
40+
}

0 commit comments

Comments
 (0)