-
Notifications
You must be signed in to change notification settings - Fork 0
/
handshake.js
465 lines (382 loc) · 15.9 KB
/
handshake.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* jshint strict: true */
var constants = require('./constants')
, cio = require('./constants-io');
// private Extension functions
var readExtensions = function(buf, handshake) {
handshake.ExtensionsLength = cio.readUInt16(buf);
handshake.Extensions = [];
var extension_cursor = 0;
while (extension_cursor < handshake.ExtensionsLength) {
var extension = {};
extension.Type = cio.readExtensionType(buf, false);
extension.Length = cio.readUInt16(buf);
if (extension.Length > 0)
extension.Bytes = cio.readBytes(buf, extension.Length);
extension_cursor += constants.sizeOf.ExtensionType + 2 + extension.Length;
handshake.Extensions.push(extension);
}
};
var writeExtensions = function(buf, handshake) {
cio.writeUInt16(buf, handshake.ExtensionsLength);
var extensionsLengthCheck = 0;
handshake.Extensions.forEach(function(Extension) {
cio.writeExtensionType(buf, Extension.Type, false);
cio.writeUInt16(buf, Extension.Length);
if (Extension.Length > 0) {
cio.writeBytes(buf, Extension.Bytes);
extensionsLengthCheck += Extension.Bytes.length;
}
extensionsLengthCheck += constants.sizeOf.ExtensionType;
extensionsLengthCheck += 2; // Length uint16
});
if (extensionsLengthCheck != handshake.ExtensionsLength)
throw new Error('invalid ExtensionsLength '+extensionsLengthCheck+' != '+handshake.ExtensionsLength);
};
var fixExtensions = function(handshake) {
var size = 0;
if (Array.isArray(handshake.Extensions)) {
size += 2; // length uint16
var extlen = 0;
handshake.Extensions.forEach(function(Extension){
if (Extension.Length === undefined) {
if (Buffer.isBuffer(Extension.Bytes)) {
Extension.Length = Extension.Bytes.length;
} else {
throw new Error('Extension.Length or Extension.Bytes is required');
}
}
if (Extension.Length !== 0 && (!Buffer.isBuffer(Extension.Bytes) || Extension.Bytes.length != Extension.Length))
throw new Error('Extension.Bytes are invalid '+Extension.Type);
extlen += constants.sizeOf.ExtensionType;
extlen += 2; // Length uint16
extlen += Extension.Length;
});
handshake.ExtensionsLength = extlen;
size += extlen;
} else if (handshake.ExtensionsLength == 0) {
handshake.Extensions = [];
size += 2; // length uint16
}
return size;
};
var read = {};
var fix = {};
var write = {};
// ## client hello -------------------------------------------------------------
read.client_hello = function(buf, handshake, options) {
var handshake_start_cursor = buf.cursor;
handshake.ProtocolVersion = cio.readProtocolVersion(buf);
handshake.Time = cio.readUInt32(buf);
handshake.Random = cio.readBytes(buf, 28);
handshake.SessionIDLength = cio.readUInt8(buf);
if (handshake.SessionIDLength != 0) throw new Error('not implemented');
handshake.CipherSuitesLength = cio.readUInt16(buf);
if (handshake.CipherSuitesLength % 2 != 0) throw new Error('invalid CipherSuitesLength');
handshake.CipherSuites = [];
for (var i=0; i<handshake.CipherSuitesLength; i+=2) {
handshake.CipherSuites.push(cio.readCipherSuite(buf, false));
}
handshake.CompressionMethodsLength = cio.readUInt8(buf);
handshake.CompressionMethods = [];
for (var i=0; i<handshake.CompressionMethodsLength; i++) {
handshake.CompressionMethods.push(cio.readCompressionMethod(buf));
}
// if the packet is longer then parse the extensions
if (handshake.Length >= buf.cursor-handshake_start_cursor+2) {
readExtensions(buf, handshake);
}
};
fix.client_hello = function(msg, options) {
var handshake = msg.handshake;
var size = 0;
if (!handshake.ProtocolVersion) handshake.ProtocolVersion = '1.1';
size += constants.sizeOf.ProtocolVersion;
if (!handshake.Time) handshake.Time = Math.floor((new Date()).getTime()/1000);
size += 4; // Time uint32
if (!Buffer.isBuffer(handshake.Random)) {
handshake.Random = new Buffer(handshake.Random);
if (handshake.Random.length != 28)
handshake.Random = new Buffer(28);
}
size += 28; // random
if (!handshake.SessionIDLength) handshake.SessionIDLength = 0;
size += 1; // SessionIDLength uint8
if (handshake.SessionIDLength) {
size += handshake.SessionIDLength;
if (!Buffer.isBuffer(handshake.SessionID)) {
handshake.SessionID = new Buffer(handshake.SessionID);
if (handshake.SessionID.length != handshake.SessionIDLength)
handshake.SessionID = new Buffer(handshake.SessionIDLength);
}
}
size += 2; // CipherSuitesLength uint16
if (Array.isArray(handshake.CipherSuites)) {
handshake.CipherSuitesLength = handshake.CipherSuites.length * constants.sizeOf.CipherSuite;
} else {
handshake.CipherSuites = [];
handshake.CipherSuitesLength = 0;
}
size += handshake.CipherSuitesLength;
size += 1; // CompressionMethodsLength unit8
if (Array.isArray(handshake.CompressionMethods)) {
handshake.CompressionMethodsLength = handshake.CompressionMethods.length * constants.sizeOf.CompressionMethod;
} else {
handshake.CompressionMethods = [ 'null' ];
handshake.CompressionMethodsLength = 1;
}
size += handshake.CompressionMethodsLength;
size += fixExtensions(handshake);
return size;
};
write.client_hello = function(buf, handshake, options) {
cio.writeProtocolVersion(buf, handshake.ProtocolVersion);
cio.writeUInt32(buf, handshake.Time);
cio.writeBytes(buf, handshake.Random);
cio.writeUInt8(buf, handshake.SessionIDLength);
if (handshake.SessionIDLength != 0) throw new Error('not implemented');
cio.writeUInt16(buf, handshake.CipherSuitesLength);
var cipherLengthCheck = 0;
handshake.CipherSuites.forEach(function(CipherSuite) {
cio.writeCipherSuite(buf, CipherSuite);
cipherLengthCheck += 2;
});
if (cipherLengthCheck != handshake.CipherSuitesLength)
throw new Error('invalid CipherSuitesLength');
cio.writeUInt8(buf, handshake.CompressionMethodsLength);
var compressionLengthCheck = 0;
handshake.CompressionMethods.forEach(function(CompressionMethod) {
cio.writeCompressionMethod(buf, CompressionMethod);
compressionLengthCheck += 1;
});
if (compressionLengthCheck != handshake.CompressionMethodsLength)
throw new Error('invalid CompressionMethodsLength');
if (handshake.ExtensionsLength !== undefined) {
writeExtensions(buf, handshake);
}
};
// ## server hello -------------------------------------------------------------
read.server_hello = function(buf, handshake, options) {
var handshake_start_cursor = buf.cursor;
handshake.ProtocolVersion = cio.readProtocolVersion(buf);
handshake.Time = cio.readUInt32(buf);
handshake.Random = cio.readBytes(buf, 28);
handshake.SessionIDLength = cio.readUInt8(buf);
if (handshake.SessionIDLength > 0) {
if (handshake.SessionIDLength > 32) throw new Error('invalid SessionIDLength');
handshake.SessionID = cio.readBytes(buf, handshake.SessionIDLength);
}
handshake.CipherSuite = cio.readCipherSuite(buf, false);
handshake.CompressionMethod = cio.readCompressionMethod(buf);
// if the packet is longer then parse the extensions
if (handshake.Length >= buf.cursor-handshake_start_cursor+2) {
readExtensions(buf, handshake);
}
};
fix.server_hello = function(msg, options) {
var handshake = msg.handshake;
var size = 0;
if (!handshake.ProtocolVersion) handshake.ProtocolVersion = '1.1';
size += constants.sizeOf.ProtocolVersion;
if (!handshake.Time) handshake.Time = Math.floor((new Date()).getTime()/1000);
size += 4; // Time uint32
if (!Buffer.isBuffer(handshake.Random)) {
handshake.Random = new Buffer(handshake.Random);
if (handshake.Random.length != 28)
handshake.Random = new Buffer(28);
}
size += 28; // random
if (!handshake.SessionIDLength) handshake.SessionIDLength = 0;
size += 1; // SessionIDLength uint8
if (handshake.SessionIDLength) {
size += handshake.SessionIDLength;
if (!Buffer.isBuffer(handshake.SessionID)) {
handshake.SessionID = new Buffer(handshake.SessionID);
if (handshake.SessionID.length != handshake.SessionIDLength)
handshake.SessionID = new Buffer(handshake.SessionIDLength);
}
}
if (!handshake.CipherSuite) throw new Error('handshake.CipherSuite is required');
size += constants.sizeOf.CipherSuite;
if (!handshake.CompressionMethod) handshake.CompressionMethod = 'null';
size += 1; // CompressionMethodsLength unit8
size += fixExtensions(handshake);
return size;
};
write.server_hello = function(buf, handshake, options) {
cio.writeProtocolVersion(buf, handshake.ProtocolVersion);
cio.writeUInt32(buf, handshake.Time);
cio.writeBytes(buf, handshake.Random);
cio.writeUInt8(buf, handshake.SessionIDLength);
if (handshake.SessionIDLength != 0) throw new Error('not implemented');
cio.writeCipherSuite(buf, handshake.CipherSuite);
cio.writeCompressionMethod(buf, handshake.CompressionMethod);
if (handshake.ExtensionsLength !== undefined) {
writeExtensions(buf, handshake);
}
};
// ## certificate --------------------------------------------------------------
read.certificate = function(buf, handshake, options) {
var handshake_start_cursor = buf.cursor;
handshake.CertificatesLength = cio.readUInt24(buf);
handshake.Certificates = [];
var certificate_cursor = 0;
while (certificate_cursor < handshake.CertificatesLength) {
var certificate = {};
certificate.Length = cio.readUInt24(buf);
if (certificate.Length > 0)
certificate.Bytes = cio.readBytes(buf, certificate.Length);
certificate_cursor += certificate.Length + 3 // length uint24;
handshake.Certificates.push(certificate);
}
// if the packet is longer then parse the extensions
if (handshake.Length >= buf.cursor-handshake_start_cursor+2) {
readExtensions(buf, handshake);
}
};
fix.certificate = function(msg, options) {
var handshake = msg.handshake;
var size = 0;
size += 3; // CertificatesLength uint24
if (Array.isArray(handshake.Certificates)) {
var certlen = 0;
handshake.Certificates.forEach(function(Certificate){
if (Buffer.isBuffer(Certificate.Bytes)) {
Certificate.Length = Certificate.Bytes.length;
certlen += 3; // CertificateLength uint24
certlen += Certificate.Length;
} else {
throw new Error('Certificate.Bytes is required');
}
});
handshake.CertificatesLength = certlen;
size += certlen;
} else {
handshake.CertificatesLength = 0;
handshake.Certificates = [];
}
size += fixExtensions(handshake);
return size;
};
write.certificate = function(buf, handshake, options) {
cio.writeUInt24(buf, handshake.CertificatesLength);
var certificatesLengthCheck = 0;
handshake.Certificates.forEach(function(Certificate) {
cio.writeUInt24(buf, Certificate.Length);
cio.writeBytes(buf, Certificate.Bytes);
certificatesLengthCheck += Certificate.Bytes.length + 3; // Length uint24
});
if (certificatesLengthCheck != handshake.CertificatesLength)
throw new Error('invalid CertificatesLength '+ certificatesLengthCheck +' '+ handshake.CertificatesLength);
if (handshake.ExtensionsLength !== undefined) {
writeExtensions(buf, handshake);
}
};
// ## server key exchange ------------------------------------------------------
read.server_key_exchange = function(buf, handshake, options) {
var handshake_start_cursor = buf.cursor;
handshake.Bytes = cio.readBytes(buf, handshake.Length);
// if the packet is longer then parse the extensions
if (handshake.Length >= buf.cursor-handshake_start_cursor+2) {
readExtensions(buf, handshake);
}
};
fix.server_key_exchange = function(msg, options) {
if (!Buffer.isBuffer(msg.handshake.Bytes))
throw new Error('server_key_exchange Bytes are required');
msg.handshake.Length = msg.handshake.Bytes.length;
return msg.handshake.Length;
};
write.server_key_exchange = function(buf, handshake, options) {
cio.writeBytes(buf, handshake.Bytes);
if (handshake.ExtensionsLength !== undefined) {
writeExtensions(buf, handshake);
}
};
// ## server hello done --------------------------------------------------------
read.server_hello_done = function(buf, handshake, options) {
// if the packet is longer then parse the extensions
if (handshake.Length >= buf.cursor-buf.cursor+2) {
readExtensions(buf, handshake);
}
};
fix.server_hello_done = function(msg, options) {
msg.handshake.Length = 0;
return msg.handshake.Length;
};
write.server_hello_done = function(buf, handshake, options) {
if (handshake.ExtensionsLength !== undefined) {
writeExtensions(buf, handshake);
}
};
// ## client key exchange ------------------------------------------------------
read.client_key_exchange = function(buf, handshake, options) {
if (options.client_key_exchange == 'RSA') {
handshake.Content = {};
handshake.Content.EncryptedPreMasterSecretLength = cio.readUInt16(buf);
handshake.Content.EncryptedPreMasterSecret = cio.readBytes(buf, handshake.Content.EncryptedPreMasterSecretLength);
} else if (options.client_key_exchange == 'DH') {
handshake.Content = {};
handshake.Content.ClientDiffieHellmanPublicLength = cio.readUInt8(buf);
handshake.Content.ClientDiffieHellmanPublic = cio.readBytes(buf, handshake.Content.ClientDiffieHellmanPublicLength);
} else {
handshake.Content = cio.readBytes(buf, handshake.Length);
}
};
fix.client_key_exchange = function(msg, options) {
var handshake = msg.handshake;
var size = 0;
if (Buffer.isBuffer(handshake.Content)) {
size = handshake.Content.length;
} else if (handshake.Content && Buffer.isBuffer(handshake.Content.EncryptedPreMasterSecret)) {
handshake.Content.EncryptedPreMasterSecretLength = handshake.Content.EncryptedPreMasterSecret.length;
size = handshake.Content.EncryptedPreMasterSecretLength + 2; // Length uint16
} else if (handshake.Content && Buffer.isBuffer(handshake.Content.ClientDiffieHellmanPublic)) {
handshake.Content.ClientDiffieHellmanPublicLength = handshake.Content.ClientDiffieHellmanPublic.length;
size = handshake.Content.ClientDiffieHellmanPublicLength + 1; // Length uint8
} else {
handshake.Content = new Buffer(0);
}
return size;
};
write.client_key_exchange = function(buf, handshake, options) {
if (Buffer.isBuffer(handshake.Content)) {
cio.writeBytes(buf, handshake.Content);
} else if (handshake.Content && Buffer.isBuffer(handshake.Content.EncryptedPreMasterSecret)) {
cio.writeUInt16(buf, handshake.Content.EncryptedPreMasterSecretLength);
cio.writeBytes(buf, handshake.Content.EncryptedPreMasterSecret);
} else if (handshake.Content && Buffer.isBuffer(handshake.Content.ClientDiffieHellmanPublic)) {
cio.writeUInt8(buf, handshake.Content.ClientDiffieHellmanPublicLength);
cio.writeBytes(buf, handshake.Content.ClientDiffieHellmanPublic);
} else {
throw new Error('invalid client_key_exchange Content');
}
};
// # public functions ----------------------------------------------------------
exports.read = function(buf, msg, options) {
var handshake = {};
handshake.HandshakeType = cio.readHandshakeType(buf);
handshake.Length = cio.readUInt24(buf);
read[handshake.HandshakeType](buf, handshake, options);
msg.handshake = handshake;
return msg;
};
exports.size = function(msg, options) {
var size = 0;
size += constants.sizeOf.HandshakeType;
size += 3; // Length uint24
var handshake = msg.handshake;
if (handshake.Length) size += handshake.Length;
return size;
};
exports.fix = function(msg, options) {
var handshake = msg.handshake;
if (!handshake.HandshakeType) throw new Error('HandshakeType is required');
handshake.Length = fix[handshake.HandshakeType](msg, options);
return handshake.Length + constants.sizeOf.HandshakeType + 3; // Length uint24
};
exports.write = function(buf, msg, options) {
var handshake = msg.handshake;
cio.writeHandshakeType(buf, handshake.HandshakeType);
cio.writeUInt24(buf, handshake.Length);
write[handshake.HandshakeType](buf, handshake, options);
};