-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprotocol.js
More file actions
229 lines (200 loc) · 7.24 KB
/
Copy pathprotocol.js
File metadata and controls
229 lines (200 loc) · 7.24 KB
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
var winston = require('winston');
var uuidV1 = require('uuid/v1');
var fs = require('fs');
var sjcl = require('./sjcl');
sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]();
var form = require('./form');
var SECRET = loadSecret();
var payloadKey = null;
var m4 = null;
exports.handleMessage = function(connection, message) {
message = JSON.parse(message.utf8Data);
winston.debug('[RECV]', message.action);
if (payloadKey && message.action != "authVerify") {
message.payload = payloadKey.decryptPayload(message.payload);
}
function send(message) {
if (payloadKey) {
encpayload = payloadKey.encryptPayload(message.payload);
message.payload = encpayload;
}
connection.send(JSON.stringify(message));
winston.debug("[SENT]", message.action);
}
if (message.action == "hello") {
if (SECRET) {
winston.debug('I have secret, doing authBegin');
doAuthBegin();
}
else {
winston.info("No secret stored, starting pairing process");
send({ action: "authNew",
payload: { method: "auth-sma-hmac256",
alg: "aead-cbchmac-256",
code: 1664 }
});
winston.info("*** Open https://agilebits.com/"
+ "browsers/auth.html in your browser now"
+ " to finalize pairing process *** ");
}
}
else if (message.action == "authRegister") {
SECRET = message.payload.secret;
saveSecret(SECRET);
winston.info("Received authRegister, secret: " + SECRET)
doAuthBegin();
}
else if (message.action == "authBegin") {
doAuthContinue(message.payload);
}
else if (message.action == "authVerify") {
if (m4 != message.payload.M4) {
winston.error("Browser authentication failed");
process.exit();
}
send({ action: "welcome",
payload: { alg: "aead-cbchmac-256" } });
winston.info("Succesfully connected to browser extenstion.");
}
else if (message.action == "showPopup") {
msg = { action: "collectDocuments",
payload: { context: uuidV1()}};
send(msg);
}
else if (message.action == "collectDocumentResults") {
form.fill(message.payload, send);
}
function doAuthBegin() {
send({ action: "authBegin",
payload: { method: "auth-sma-hmac256",
alg: "aead-cbchmac-256" }
});
}
function doAuthContinue(payload) {
var cc = payload.cc;
// need to generate payload with M3, cs and method
// generate CS random number, convert to base64
// concat that with d, the original challenge
// hash 256 that, then hmac with key
d = sjcl.random.randomWords(8);
cs = sjcl.codec.base64.fromBits(d, true, true);
b = sjcl.codec.base64.toBits(cc, true);
a = sjcl.codec.base64.toBits(SECRET, true);
a = new sjcl.misc.hmac(a,sjcl.hash.sha256);
var h = sjcl.hash.sha256.hash(sjcl.bitArray.concat(d, b))
, h = a.encrypt(h);
computedM3 = sjcl.codec.base64.fromBits(h, !0, !0);
outpayload = { M3: computedM3, cs: cs, method: payload.method };
winston.debug("Sending computed payload: ", outpayload);
send({ action: "authContinue", payload: outpayload });
// Now generate key for encryption derived from M3
f = a.encrypt(h);
m4 = r.A(f);
encryptionBits = sjcl.codec.utf8String.toBits('encryption'),
hmacBits = sjcl.codec.utf8String.toBits('hmac'),
c = a.encrypt(h.concat(f).concat(encryptionBits)),
a = a.encrypt(f.concat(h).concat(hmacBits)),
payloadKey = new na(c, a);
}
}
// encryption stuff
var r = new function() {
this.rot13 = this.Kb = function(a) {
for (var c = [], b, d = a.length, e = 'a'.charCodeAt(), f = e + 26, h = 'A'.charCodeAt(), k = h + 26; d--; )
b = a.charCodeAt(d),
c[d] = b >= e && b < f ? String.fromCharCode((b - e + 13) % 26 + e) : b >= h && b < k ? String.fromCharCode((b - h + 13) % 26 + h) : a.charAt(d);
return c.join('')
}
;
this.base64toBits = this.H = function(a) {
return sjcl.codec.base64.toBits(a.replace('\x00', ''), !0)
}
;
this.bitsToBase64 = this.A = function(a) {
return sjcl.codec.base64.fromBits(a, !0, !0)
}
}
function na(a, c) {
function b(a, b, c) {
if (void 0 === a)
throw new sjcl.exception.invalid('iv is required');
if (void 0 === b && void 0 === c)
throw new sjcl.exception.invalid('Either ciphertext or adata is required for hmac calculation.');
if ('string' !== typeof a || void 0 !== b && 'string' !== typeof b || void 0 !== c && 'string' !== typeof c)
throw new sjcl.exception.invalid('Invalid input: ' + typeof a + '/' + typeof b + '/' + typeof c);
a = [a];
void 0 !== b && a.push(b);
void 0 !== c && a.push(c);
return d.encrypt(a.join(''))
}
var d;
this.Ya = new sjcl.cipher.aes(a);
d = new sjcl.misc.hmac(c,sjcl.hash.sha256);
this.aa = 'aead-cbchmac-256';
this.encryptPayload = this.vb = function(a) {
var c, d;
a = 'object' === typeof a ? JSON.stringify(a) : a;
c = sjcl.codec.utf8String.toBits(a);
//a = crypto.getRandomValues(new Uint8Array(16));
//d = sjcl.codec.bytes.toBits(a);
d = sjcl.random.randomWords(4);
a = r.A(d);
c = sjcl.mode.cbc.encrypt(this.Ya, c, d);
d = r.A(c);
c = {
alg: this.aa,
iv: a,
data: d
};
a = b(a, d, void 0);
a = sjcl.bitArray.clamp(a, 96);
c.hmac = r.A(a);
return c
}
;
this.decryptPayload = this.ub = function(a) {
var c, d, k, l, p, m, t, x;
if (a.alg !== this.aa)
throw Error('Mismatched payload algorithm: <' + a.alg + '/' + this.aa + '>');
c = a.iv;
d = a.data;
k = a.adata;
try {
p = a.hmac;
l = r.H(p);
a = k;
var C, D;
void 0 === l && (l = a,
a = void 0);
D = sjcl.bitArray.bitLength(l);
if (96 > D)
throw new sjcl.exception.invalid('The supplied hmac value is invalid.');
C = b(c, d, a);
sjcl.bitArray.bitLength(C) > D && (C = sjcl.bitArray.bitSlice(C, 0, D));
if (!sjcl.bitArray.equal(C, l))
throw new sjcl.exception.corrupt('Failed to validate payload hmac.');
m = sjcl.mode.cbc.decrypt(this.Ya, r.H(d), r.H(c));
t = sjcl.codec.utf8String.fromBits(m);
x = JSON.parse(t)
} catch (X) {
console.error(X)
} finally {
return x
}
}
}
function saveSecret(secret) {
fs.writeFile('.secret', secret, function(err) {
if (err) {
winston.error("error storing secret", err);
return
}
winston.info("secret saved");
});
}
function loadSecret() {
if (!fs.existsSync('.secret')) {
return
}
return fs.readFileSync('.secret', 'utf8');
}