-
Notifications
You must be signed in to change notification settings - Fork 9
/
tls.js
382 lines (373 loc) · 10.4 KB
/
tls.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"use strict";
function tlsClient(chan, psk) {
const RandomSize = 32;
const MasterSecretSize = 48;
const MAXFRAG = 16384;
const DigestStateSize = 336;
const FChangeCipherSpec = 20;
const FAlert = 21;
const FHandshake = 22;
const FApplicationData = 23;
const Fragment = Struct([
'type', U8,
'version', U16,
'fragment', OpaqueVector(U16)
]);
const AAD = Struct([
'recnum', U64,
'type', U8,
'version', U16,
'length', U16
]);
const HClientHello = 1;
const ClientHello = Struct([
'client_version', U16,
'random', Bytes(RandomSize),
'session_id', Vector(U8, U8),
'cipher_suites', Vector(U16, U16),
'compression_methods', Vector(U8, U8),
'extensions', Vector(U16, Struct([
'type', U16,
'data', Vector(U8, U16)
]))
]);
const HServerHello = 2;
const ServerHello = Struct([
'server_version', U16,
'random', Bytes(RandomSize),
'session_id', Vector(U8, U8),
'cipher_suite', U16,
'compression_method', U8,
'extensions', Optional(Vector(U16, Struct([
'type', U16,
'data', Vector(U8, U16)
])))
]);
const HServerHelloDone = 14;
const ServerHelloDone = Struct([]);
const HClientKeyExchange = 16;
const ClientKeyExchange = Struct([
'psk_identity', VariableString(U16)
]);
const HFinished = 20;
const Finished = Struct([
'verify_data', Bytes(12)
]);
const Handshake = Struct([
'msg_type', U8,
null, Length(U24, Select(o => o.msg_type, {
1: ClientHello,
2: ServerHello,
14: ServerHelloDone,
16: ClientKeyExchange,
20: Finished
}))
]);
let crandom = function(){
var s;
s = new Uint8Array(RandomSize);
let t = Date.now() / 1000 | 0;
s[0] = t >> 24; s[1] = t >> 16; s[2] = t >> 8; s[3] = t;
window.crypto.getRandomValues(s.subarray(4));
return s;
}();
let srandom;
function nullCipher() { }
nullCipher.prototype.decrypt = function(f){};
nullCipher.prototype.encrypt = function(f){};
function aeadChacha20Poly1305(mackey, key, iv){
this.iv = iv;
this.iv_array = () => Module.HEAPU8.subarray(this.iv, this.iv+12);
this.state = C.mallocz(26 * 4, 1);
C.setupChachastate(this.state, key, 32, iv, 12, 20);
}
aeadChacha20Poly1305.mac_key_length = 0;
aeadChacha20Poly1305.enc_key_length = 32;
aeadChacha20Poly1305.fixed_iv_length = 12;
aeadChacha20Poly1305.prototype.decrypt = function(f){
let nonce = new Uint8Array(12);
for(var i = 0; i < 12; i++)
nonce[i] = f.recnum / 2**(8*(11-i)) & 255 ^ this.iv_array()[i];
C.chacha_setiv(this.state, nonce);
f.length -= 16;
let aad = pack(AAD, f).data();
f.fragment = withBuf(f.fragment.length, (buf, buf_array) => {
buf_array().set(f.fragment);
if(C.ccpoly_decrypt(buf, f.length, aad, aad.length, buf + f.length, this.state) < 0)
throw new Error("bad MAC");
return buf_array().slice(0, f.length);
});
};
aeadChacha20Poly1305.prototype.encrypt = function(f){
let nonce = new Uint8Array(12);
for(var i = 0; i < 12; i++)
nonce[i] = f.recnum / 2**(8*(11-i)) & 255 ^ this.iv_array()[i];
C.chacha_setiv(this.state, nonce);
let aad = pack(AAD, f).data();
f.fragment = withBuf(f.fragment.length + 16, (buf, buf_array) => {
buf_array().set(f.fragment);
C.ccpoly_encrypt(buf, f.fragment.length, aad, aad.length, buf + f.fragment.length, this.state);
return buf_array().slice();
});
};
var handshake = new Packet();
var application = new Packet();
var rxCipher = new nullCipher();
var txCipher = new nullCipher();
var nextTxCipher = null;
var nextRxCipher = null;
var masterSecret;
var sessionKeys;
var txRecNum;
var rxRecNum;
var handhash = C.mallocz(DigestStateSize*2, 1);
var closed = false;
function calcMasterSecret() {
masterSecret = C.mallocz(MasterSecretSize, 1);
var n = psk.length;
withBuf(2 * n + 4, (buf, buf_array) =>
withBuf(2 * RandomSize, (seed, seed_array) => {
{
let a = buf_array();
a[0] = n >> 8;
a[1] = n;
a[n+2] = n >> 8;
a[n+3] = n;
a.set(psk, n+4);
}
seed_array().set(crandom);
seed_array().set(srandom, RandomSize);
let label = 'master secret';
C.p_sha256(masterSecret, MasterSecretSize, buf, 2 * n + 4, label, label.length, seed, 2 * RandomSize);
}));
psk.fill(0);
}
function calcSessionKeys(mac_key_length, enc_key_length, fixed_iv_length) {
var n = 2 * mac_key_length + 2 * enc_key_length + 2 * fixed_iv_length;
if(sessionKeys !== undefined){
C.memset(sessionKeys.buf, 0, sessionKeys.length);
C.free(sessionKeys.buf);
}
let buf = C.mallocz(n, 1);
sessionKeys = {
length: n,
buf: buf,
cMACkey: buf,
sMACkey: buf + mac_key_length,
cKey: buf + 2 * mac_key_length,
sKey: buf + 2 * mac_key_length + enc_key_length,
cIV: buf + 2 * mac_key_length + 2 * enc_key_length,
sIV: buf + 2 * mac_key_length + 2 * enc_key_length + fixed_iv_length
};
let label = 'key expansion';
withBuf(2 * RandomSize, (seed, seed_array) => {
seed_array().set(srandom);
seed_array().set(crandom, RandomSize);
C.p_sha256(sessionKeys.buf, n, masterSecret, MasterSecretSize, label, label.length, seed, 2 * RandomSize);
});
}
function botch() {
throw new Error("TLS botch");
}
function alert(f) {
var alerts = {
0: 'close notify',
1: 'unexpected message',
20: 'bad record MAC',
22: 'record overflow',
30: 'decompression failure',
40: 'handshake failure',
42: 'bad certificate',
43: 'unsupported certificate',
44: 'certificate revoked',
45: 'certificate expired',
46: 'certificate unknown',
47: 'illegal parameter',
48: 'unknown ca',
49: 'access denied',
50: 'decode error',
51: 'decrypt error',
70: 'protocol version',
71: 'insufficient security',
80: 'internal error',
90: 'user canceled',
100: 'no renegotiation',
110: 'unsupported extension',
};
if(f.length != 2) botch();
if(!(f[1] in alerts)) botch();
switch(f[0]){
case 1:
console.log('TLS ALERT: WARNING: ' + alerts[f[1]]);
break;
case 2:
throw new Error('TLS ALERT: FATAL: ' + alerts[f[1]]);
}
}
function recvFragment() {
function recLen(b) {
if(b.length < 5) return -1;
return 5 + (b[3] << 8 | b[4]);
}
chan.read(recLen)
.then(b => {
if(b === undefined) return;
let r = unpack(Fragment, b);
if(r.version != 0x0303) botch();
r.recnum = rxRecNum++;
r.length = r.fragment.length;
rxCipher.decrypt(r);
switch(r.type){
case FApplicationData: application.write(r.fragment); break;
case FHandshake: handshake.write(r.fragment); break;
case FChangeCipherSpec:
if(nextRxCipher === null) botch();
rxCipher = new nextRxCipher(sessionKeys.sMACkey, sessionKeys.sKey, sessionKeys.sIV);
nextRxCipher = null;
rxRecNum = 0;
break;
case FAlert:
if(r.fragment.length == 2 && r.fragment[0] == 1 && r.fragment[1] == 0){
return sendData(new VBuffer(new Uint8Array([1,0])))
.then(() => {
handshake.close();
application.close();
chan.close();
});
}
alert(r.fragment);
break;
default: throw new Error("TLS unknown protocol " + r.type.toString());
}
return recvFragment();
});
}
function recvHandshake() {
function handLen(b) {
if(b.length < 4) return -1;
return 4 + (b[1] << 16 | b[2] << 8 | b[3]);
}
return handshake.read(handLen).then(b => {
if(b[0] != HFinished)
C.sha2_256(b, b.length, 0, handhash);
return unpack(Handshake, b)
});
}
function sendData(b, type) {
let p = Promise.resolve();
for(var n = 0; n < b.p; n += MAXFRAG){
let i = n;
let e = n+MAXFRAG > b.p ? b.p : n+MAXFRAG;
p = p.then(() => {
var f = {
type: type,
version: 0x0303,
fragment: b.a.subarray(i, e),
length: e - i,
recnum: txRecNum++
};
txCipher.encrypt(f);
return chan.write(pack(Fragment, f).data());
});
}
return p;
}
function sendHandshake(data) {
var b = pack(Handshake, data);
C.sha2_256(b.data(), b.p, 0, handhash);
return sendData(b, FHandshake);
}
function sendClientHello() {
return sendHandshake({
msg_type: HClientHello,
client_version: 0x0303,
gmx_unix_time: Date.now() / 1000 | 0,
random: crandom,
session_id: [],
cipher_suites: [0xccab],
compression_methods: [0],
extensions: []
});
}
function recvServerHello() {
return recvHandshake().then(m => {
if(m.msg_type != HServerHello) botch();
if(m.server_version != 0x0303) botch();
if(m.session_id.length != 0) botch();
if(m.cipher_suite != 0xccab) botch();
if(m.compression_method != 0) botch();
srandom = m.random;
nextTxCipher = nextRxCipher = aeadChacha20Poly1305;
});
}
function recvServerHelloDone() {
return recvHandshake().then(m => {
if(m.msg_type != HServerHelloDone) botch();
});
}
function sendClientKeyExchange() {
return sendHandshake({
msg_type: HClientKeyExchange,
psk_identity: "p9secret"
});
}
function sendChangeCipherSpec() {
var p = new VBuffer();
p.put([1]);
return sendData(p, FChangeCipherSpec)
.then(() => {
if(nextTxCipher === null) botch();
txCipher = new nextTxCipher(sessionKeys.cMACkey, sessionKeys.cKey, sessionKeys.cIV);
nextTxCipher = null;
txRecNum = 0;
});
}
function verifyData(label) {
return withBuf(32, (hash, hash_array) =>
withBuf(12, (data, data_array) => {
C.memmove(handhash + DigestStateSize, handhash, DigestStateSize);
C.sha2_256([], 0, hash, handhash + DigestStateSize);
C.p_sha256(data, 12, masterSecret, MasterSecretSize, label, label.length, hash, 32);
return data_array().slice();
}));
}
function sendFinished() {
return sendHandshake({
msg_type: HFinished,
verify_data: verifyData('client finished')
});
}
function recvFinished() {
return recvHandshake().then(m => {
if(m.msg_type != HFinished) botch();
let data = verifyData('server finished');
if(data.length !== m.verify_data.length) botch();
for(var i = 0; i < data.length; i++)
if(data[i] != m.verify_data[i])
botch();
});
}
function runHandshake() {
return sendClientHello()
.then(recvServerHello)
.then(recvServerHelloDone)
.then(sendClientKeyExchange)
.then(() => {
calcMasterSecret();
calcSessionKeys(nextTxCipher.mac_key_length, nextTxCipher.enc_key_length, nextTxCipher.fixed_iv_length);
})
.then(sendChangeCipherSpec)
.then(sendFinished)
.then(recvFinished);
}
function TlsConn() { }
TlsConn.prototype.read = function(check) {
return application.read(check);
};
TlsConn.prototype.write = function(b) {
if(!(b instanceof VBuffer)) b = new VBuffer(b);
return sendData(b, FApplicationData);
};
recvFragment();
return runHandshake().then(() => new TlsConn());
}