-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
316 lines (267 loc) · 7.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
var OTR;
if (typeof OTR === 'undefined') {
OTR = require("../index.js");
}
var otr = OTR;
var document = document || {};
var FORCE_SMP = false;
var SEND_BAD_SECRET = false;
var SMP_TEST_DONE = false;
var SMP_TEST_IN_PROGRESS = false;
var SMP_TEST_PASSED = false;
var SMP_TEST_PERFORMED = false;
var SYMKEY_TEST_DONE = false;
var SYMKEY_TEST_IN_PROGRESS = false;
var SYMKEY_TEST_PASSED = false;
var SYMKEY_TEST_VALUES = {};
if (typeof process !== "undefined") {
process.argv.forEach(function (arg) {
if (arg == "--force-smp") FORCE_SMP = true;
if (arg == "--bad-secret") SEND_BAD_SECRET = true;
});
}
var debug = debug || function debug() {
console.log([].join.call(arguments, " "));
};
debug("== loaded libotr version:", otr.version());
var alice_settings = {
accountname: "[email protected]",
protocol: "telechat"
};
var bob_settings = {
accountname: "[email protected]",
protocol: "telechat"
};
///setting up Alice's side of the connection
var alice = new otr.User({
keys: "./alice.keys",
fingerprints: "./alice.fp"
});
alice.name = "Alice";
var alice_account = alice.account(alice_settings.accountname, alice_settings.protocol);
var BOB = alice_account.contact("BOB");
dumpFingerprints(BOB.fingerprints());
var session_a = BOB.openSession({
policy: otr.POLICY.DEFAULT,
secret: 's3cr37',
MTU: 3000
});
///setting up Bob's side of the connection
var bob = new otr.User({
keys: "./bob.keys",
fingerprints: "./bob.fp"
});
bob.name = "Bob";
var bob_account = bob.account(bob_settings.accountname, bob_settings.protocol);
var ALICE = bob_account.contact("ALICE");
dumpFingerprints(ALICE.fingerprints());
var session_b = ALICE.openSession({
policy: otr.POLICY.DEFAULT,
secret: 's3cr37',
MTU: 3000
});
dumpConnContext(session_a, "Alice's ConnContext:");
dumpConnContext(session_b, "Bob's ConnContext:");
//simulate a network connection between two parties
session_a.on("inject_message", function (msg) {
debug("ALICE:", msg);
session_b.recv(msg);
});
session_b.on("inject_message", function (msg) {
debug("BOB:", msg);
session_a.recv(msg);
});
session_a.on("create_privkey", function (a, p) {
debug("Alice doesn't have a key.. creating a new key for:", a, p);
alice_account.generateKey(function (err, key) {
if (!err) {
debug("Alice's Key Generated Successfully");
try {
alice.saveKeysToFS("./alice.keys");
} catch (e) {
debug("not saving keys.. in browser");
}
}
});
});
session_b.on("create_privkey", function (a, p) {
debug("Bob doesn't have a key.. creating a new key for:", a, p);
bob_account.generateKey(function (err, key) {
if (!err) {
debug("Bob's Key Generated Successfully");
try {
bob.saveKeysToFS("./bob.keys");
} catch (e) {
debug("not saving keys.. in browser");
}
}
});
});
session_a.on("create_instag", function (a, p) {
alice_account.generateInstag();
});
session_b.on("create_instag", function (a, p) {
bob_account.generateInstag();
});
session_a.on("gone_secure", function () {
debug("[Alice] Encrypted Connection Established - Gone Secure.");
});
session_b.on("gone_secure", function () {
debug("[Bob] Encrypted Connection Established - Gone Secure.");
});
//output incoming messages to console
function print_message(name, msg, encrypted) {
if (encrypted) {
debug(name + '[ENCRYPTED]:', msg);
} else {
debug(name + '[PLAINTEXT]:', msg);
}
}
session_a.on("msg_event", function (e) {
debug(JSON.stringify(e));
});
session_b.on("msg_event", function (e) {
debug(JSON.stringify(e));
});
//alice received message from bob
session_a.on("message", function (msg, encrypted) {
print_message('<<', msg, encrypted);
session_b.end();
});
//bob received message from alice
session_b.on("message", function (msg, encrypted) {
print_message('>>', msg, encrypted);
this.send("got your message '" + msg + "'");
});
session_a.on("disconnect", function () {
debug("Session was closed remotely");
exit_test("", true);
});
session_b.on("received_symkey", function (use, usedata, key) {
SYMKEY_TEST_IN_PROGRESS = false;
SYMKEY_TEST_DONE = true;
debug("Received Symmetric Key");
debug(" use:", use);
debug("usedata:", ab2str(usedata));
debug(" key:", ab2str(key));
SYMKEY_TEST_PASSED = (
(SYMKEY_TEST_VALUES.use === use) &&
(SYMKEY_TEST_VALUES.usedata === ab2str(usedata)) &&
(SYMKEY_TEST_VALUES.key === ab2str(key))
);
});
session_a.on("write_fingerprints", function () {
alice.writeFingerprints();
try {
debug("Saving Bob's fingerprint");
alice.saveFingerprintsToFS("./alice.fp");
} catch (e) {
debug("not saving fingerprints.. in browser");
}
dumpFingerprints(BOB.fingerprints());
});
session_b.on("write_fingerprints", function () {
bob.writeFingerprints();
try {
debug("Saving Alice's fingerprint");
bob.saveFingerprintsToFS("./bob.fp");
} catch (e) {
debug("not saving fingerprints.. in browser");
}
dumpFingerprints(ALICE.fingerprints());
});
function end_smp_test() {
debug("SMP TEST DONE");
SMP_TEST_PASSED = session_a.isAuthenticated();
SMP_TEST_DONE = true;
SMP_TEST_IN_PROGRESS = false;
}
session_b.on("smp", function (type) {
if (type !== "request") return;
debug("Received SMP Request.");
if (!SEND_BAD_SECRET) {
debug("responding with correct secret");
this.smpRespond('s3cr37');
} else {
debug("responding with wrong secret");
this.smpRespond("!!wrong_secret!!");
}
});
session_a.on("smp", end_smp_test);
//start OTR
session_a.send("?OTR?"); //or session_a.start();
//session_b.send("?OTR?"); //don't start OTR simultaneously on both ends!
var loop = setInterval(function () {
debug("_");
//wait for secure session to be established
if (!session_a.isEncrypted() || !session_b.isEncrypted()) return;
//dont do anything if tests are in progress
if (SMP_TEST_IN_PROGRESS || SYMKEY_TEST_IN_PROGRESS) {
debug("entered loop, tests in progress...");
return;
}
//smp test
if (session_a.isEncrypted() && !SMP_TEST_DONE) {
if (!session_a.isAuthenticated() || FORCE_SMP) {
SMP_TEST_IN_PROGRESS = true;
SMP_TEST_PERFORMED = true;
debug("Starting SMP Test");
session_a.smpStart();
} else {
debug("Skipping SMP Test buddies previously authenticated");
SMP_TEST_DONE = true;
}
return;
}
//start symkey test (after smp test is done)
if (!SYMKEY_TEST_DONE && SMP_TEST_DONE) {
SYMKEY_TEST_IN_PROGRESS = true;
debug("Starting Extra Symmertic Key Test");
SYMKEY_TEST_VALUES = {
'use': 1000,
'usedata': 'ftp://website.net/files.tgz'
};
session_a.extraSymKey(SYMKEY_TEST_VALUES.use, SYMKEY_TEST_VALUES.usedata,
function (key) {
SYMKEY_TEST_VALUES.key = ab2str(key);
});
return;
}
//send an encrypted message
if (session_a.isEncrypted() && SYMKEY_TEST_DONE && SMP_TEST_DONE) {
debug("sending message");
session_a.send("test encrypted message");
session_a.recv("this is an unencrypted message, during a secure session!"); //should raise a msg_event UNENCRYPTED
return;
}
exit_test("Tests did not complete...", false);
}, 500);
function exit_test(msg, TEST_PASSED) {
debug(msg);
if (loop) clearInterval(loop);
dumpConnContext(session_a, "Alice's ConnContext:");
dumpConnContext(session_b, "Bob's ConnContext:");
if (SMP_TEST_PERFORMED) {
debug("SMP TEST PERFORMED");
debug("Trusted connection after SMP? ", SMP_TEST_PASSED);
}
if (SYMKEY_TEST_DONE) debug("SYMKEY TEST", SYMKEY_TEST_PASSED ?
"PASSED" : "FAILED");
if (TEST_PASSED) {
debug("== TEST PASSED ==\n");
} else {
debug("== TEST FAILED ==\n");
}
process.exit();
}
function dumpConnContext(session, msg) {
debug(msg, "\n", session.context);
}
function dumpFingerprints(fingerprints) {
fingerprints.forEach(function (fp) {
debug(fp, fp.trust());
});
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}