-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpacketContent.js
436 lines (385 loc) · 12.8 KB
/
packetContent.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
var basicTypes = require("./basicTypes");
var BufferedStream = require("./bufferedStream");
var consts = require("./consts");
var utils = require("./utils");
function getPacketInfo(tag, body, callback) {
switch(tag)
{
case consts.PKT.PUBLIC_KEY:
getPublicKeyPacketInfo(body, callback);
break;
case consts.PKT.PUBLIC_SUBKEY:
getPublicSubkeyPacketInfo(body, callback);
break;
case consts.PKT.USER_ID:
getIdentityPacketInfo(body, callback);
break;
case consts.PKT.ATTRIBUTE:
getAttributePacketInfo(body, callback);
break;
case consts.PKT.SIGNATURE:
getSignaturePacketInfo(body, callback);
break;
default:
callback(null, { pkt: tag });
}
}
function getPublicKeyPacketInfo(body, callback)
{
var ret = {
pkt: consts.PKT.PUBLIC_KEY,
id: null,
binary : body,
version : body.readUInt8(0),
versionSecurity : null,
expires : null,
date : null,
pkalgo : null,
keyParts : null,
fingerprint : null,
size : null,
sizeSecurity : null,
security : null
};
if(ret.version == 3 || ret.version == 2)
{
ret.versionSecurity = consts.SECURITY.BAD;
ret.date = new Date(body.readUInt32BE(1)*1000);
var expires = body.readUInt16BE(5);
if(expires)
ret.expires = new Date(ret.date.getTime() + expires*86400000);
ret.pkalgo = body.readUInt8(7);
if(ret.pkalgo != consts.PKALGO.RSA_ES && ret.pkalgo == consts.PKALGO.RSA_E && ret.pkalgo != consts.PKALGO.RSA_S)
return callback(new Error("Unknown public key algorithm "+ret.pkalgo));
var keyParts = basicTypes.splitMPIs(body.slice(8), 2);
ret.keyParts = { n : keyParts[0], e : keyParts[1] };
ret.size = ret.keyParts.n.length;
ret.key = body.slice(8, 8+keyParts.bytes);
ret.id = ret.keyParts.n.buffer.toString("hex", ret.keyParts.n.buffer.length-8).toUpperCase();
ret.fingerprint = utils.hash(Buffer.concat([ ret.keyParts.n.buffer, ret.keyParts.e.buffer ]), "md5", "hex").toUpperCase();
}
else if(ret.version == 4)
{
ret.versionSecurity = consts.SECURITY.GOOD;
ret.date = new Date(body.readUInt32BE(1)*1000);
ret.pkalgo = body.readUInt8(5);
var keyParts = null;
if(ret.pkalgo == consts.PKALGO.RSA_ES || ret.pkalgo == consts.PKALGO.RSA_E || ret.pkalgo == consts.PKALGO.RSA_S)
{
keyParts = basicTypes.splitMPIs(body.slice(6), 2);
ret.keyParts = { n : keyParts[0], e : keyParts[1] };
ret.size = ret.keyParts.n.length;
}
else if(ret.pkalgo == consts.PKALGO.ELGAMAL_E)
{
keyParts = basicTypes.splitMPIs(body.slice(6), 3);
ret.keyParts = { p : keyParts[0], g : keyParts[1], y : keyParts[2] };
ret.size = ret.keyParts.p.length;
}
else if(ret.pkalgo == consts.PKALGO.DSA)
{
keyParts = basicTypes.splitMPIs(body.slice(6), 4);
ret.keyParts = { p : keyParts[0], q : keyParts[1], g : keyParts[2], y : keyParts[3] };
ret.size = ret.keyParts.p.length;
}
else
return callback(new Error("Unknown public key algorithm "+ret.pkalgo));
ret.key = body.slice(6, 6+keyParts.bytes);
var fingerprintData = new Buffer(body.length + 3);
fingerprintData.writeUInt8(0x99, 0);
fingerprintData.writeUInt16BE(body.length, 1);
body.copy(fingerprintData, 3);
ret.fingerprint = utils.hash(fingerprintData, "sha1", "hex").toUpperCase();
ret.id = ret.fingerprint.substring(ret.fingerprint.length-16);
}
else
{
callback(new Error("Unknown key version "+ret.version+"."));
return;
}
if(ret.size < 1024)
ret.sizeSecurity = consts.SECURITY.BAD;
else if(ret.size < 2048)
ret.sizeSecurity = consts.SECURITY.MEDIUM;
else
ret.sizeSecurity = consts.SECURITY.GOOD;
ret.security = Math.min(ret.sizeSecurity, ret.versionSecurity);
callback(null, ret);
}
function getPublicSubkeyPacketInfo(body, callback)
{
getPublicKeyPacketInfo(body, function(err, info) {
if(err) { callback(err); return; }
info.pkt = consts.PKT.PUBLIC_SUBKEY;
callback(null, info);
});
}
function getIdentityPacketInfo(body, callback)
{
var content = body.toString("utf8")
var name = content;
var email = null;
var comment = null;
var m = name.match(/^(.*) <(.*)>$/);
if(m)
{
name = m[1];
email = m[2];
}
m = name.match(/^(.*) \((.*)\)$/);
if(m)
{
name = m[1];
comment = m[2];
}
callback(null, {
pkt: consts.PKT.USER_ID,
name : name,
email : email,
comment : comment,
binary : body,
id : content,
nameTrust : 0,
emailTrust : 0
});
}
function getAttributePacketInfo(body, callback)
{
var ret = {
pkt : consts.PKT.ATTRIBUTE,
id : utils.hash(body, "sha1", "base64").substring(0, 27),
subPackets : [ ],
binary : body,
trust : 0
};
var stream = new BufferedStream(body);
var readon = function() {
basicTypes.read125OctetNumber(stream, function(err, subPacketLength) {
if(err)
{
if(err.NOFIRSTBYTE)
callback(null, ret);
else
callback(err);
return;
}
stream.read(subPacketLength, function(err, subPacket) {
if(err) { callback(err); return; }
ret.subPackets.push(getAttributeSubPacketInfo(subPacket.readUInt8(0), subPacket.slice(1)));
readon();
});
});
};
readon();
}
function getAttributeSubPacketInfo(type, body) {
var ret = { binary: body, type: type };
if(type == consts.ATTRSUBPKT.IMAGE)
{
var headerLength = body.readUInt16LE(0); // This has to be Little Endian!
var header = body.slice(2, 2+headerLength);
ret.image = body.slice(headerLength);
var headerVersion = header.readUInt8(0);
if(headerVersion == 1)
ret.imageType = header.readUInt8(1);
}
return ret;
}
function getSignaturePacketInfo(body, callback)
{
var ret = {
pkt : consts.PKT.SIGNATURE,
id : utils.hash(body, "sha1", "base64").substring(0, 27),
sigtype : null,
date : null,
issuer : null,
pkalgo : null,
hashalgo : null,
version : null,
binary : body,
hashedSubPackets : { },
unhashedSubPackets : { },
exportable : true,
expires : null,
hashedPart : null, // The part of the signature that is concatenated to the data that is to be signed when creating the hash
first2HashBytes : null, // The first two bytes of the hash as 16-bit unsigned integer
signature : null, // The signature as buffer object
hashalgoSecurity : null,
security : null,
verified : false,
trustSignature : false
};
var byte1 = body.readUInt8(0);
if(byte1 == 3)
{ // Version 3 signature
var hashedLength = body.readUInt8(1); // Must be 5 according to spec
ret.hashedPart = body.slice(2, 2+hashedLength);
ret.sigtype = ret.hashedPart.readUInt8(0);
ret.date = new Date(ret.hashedPart.readUInt32BE(1)*1000);
var rest = body.slice(2+hashedLength);
ret.issuer = rest.toString("hex", 0, 8).toUpperCase();
ret.pkalgo = rest.readUInt8(8);
ret.hashalgo = rest.readUInt8(9);
ret.version = 3;
ret.first2HashBytes = rest.readUInt16BE(10);
ret.signature = rest.slice(12);
callback(null, ret);
}
else if(byte1 == 4)
{ // Version 4 signature
ret.sigtype = body.readUInt8(1);
ret.pkalgo = body.readUInt8(2);
ret.hashalgo = body.readUInt8(3);
ret.version = 4;
var hashedSubPacketsLength = body.readUInt16BE(4);
var hashedSubPackets = body.slice(6, 6+hashedSubPacketsLength);
var unhashedSubPacketsLength = body.readUInt16BE(6+hashedSubPacketsLength);
var unhashedSubPackets = body.slice(8+hashedSubPacketsLength, 8+hashedSubPacketsLength+unhashedSubPacketsLength);
ret.hashedPart = body.slice(0, 6+hashedSubPacketsLength);
ret.first2HashBytes = body.readUInt16BE(8+hashedSubPacketsLength+unhashedSubPacketsLength);
ret.signature = body.slice(10+hashedSubPacketsLength+unhashedSubPacketsLength);
extractSignatureSubPackets(hashedSubPackets, function(err, info1) {
if(err) { callback(err); return; }
ret.hashedSubPackets = info1;
extractSignatureSubPackets(unhashedSubPackets, function(err, info2) {
if(err) { callback(err); return; }
ret.unhashedSubPackets = info2;
if(ret.hashedSubPackets[consts.SIGSUBPKT.SIG_CREATED])
ret.date = ret.hashedSubPackets[consts.SIGSUBPKT.SIG_CREATED][0].value;
if(ret.hashedSubPackets[consts.SIGSUBPKT.ISSUER])
ret.issuer = ret.hashedSubPackets[consts.SIGSUBPKT.ISSUER][0].value;
else if(ret.unhashedSubPackets[consts.SIGSUBPKT.ISSUER])
ret.issuer = ret.unhashedSubPackets[consts.SIGSUBPKT.ISSUER][0].value;
if(ret.hashedSubPackets[consts.SIGSUBPKT.EXPORTABLE] && !ret.hashedSubPackets[consts.SIGSUBPKT.EXPORTABLE][0].value)
ret.exportable = false;
if(ret.hashedSubPackets[consts.SIGSUBPKT.SIG_EXPIRE] && ret.date)
ret.expires = new Date(ret.date.getTime() + (ret.hashedSubPackets[consts.SIGSUBPKT.SIG_EXPIRE][0].value*1000));
if(ret.hashedSubPackets[consts.SIGSUBPKT.TRUST] && ret.hashedSubPackets[consts.SIGSUBPKT.TRUST][0].value.level > 0 && ret.hashedSubPackets[consts.SIGSUBPKT.TRUST][0].value.amount > 0)
ret.trustSignature = true;
callback(null, ret);
});
});
}
else
callback(new Error("Unknown signature version "+byte1+"."));
if([ consts.HASHALGO.RIPEMD160, consts.HASHALGO.SHA256, consts.HASHALGO.SHA384, consts.HASHALGO.SHA512, consts.HASHALGO.SHA224 ].indexOf(ret.hashalgo) != -1)
ret.hashalgoSecurity = consts.SECURITY.GOOD;
else if(ret.hashalgo == consts.HASHALGO.SHA1)
ret.hashalgoSecurity = consts.SECURITY.MEDIUM;
else if(ret.hashalgo == consts.HASHALGO.MD5)
ret.hashalgoSecurity = consts.SECURITY.BAD;
else
ret.hashalgoSecurity = consts.SECURITY.UNKOWN;
ret.security = ret.hashalgoSecurity;
}
function extractSignatureSubPackets(body, callback)
{
var stream = new BufferedStream(body);
var subPackets = { };
var readon = function() {
basicTypes.read125OctetNumber(stream, function(err, number) {
if(err)
{
if(err.NOFIRSTBYTE)
callback(null, subPackets);
else
callback(err);
return;
}
if(number == 0)
{
readon();
return;
}
stream.read(number, function(err, data2) {
if(err) { callback(err); return; }
var type = data2.readUInt8(0);
var p = { critical : !!(type & consts.SIGSUBPKT.FLAG_CRITICAL), value: null, rawValue: data2.slice(1) };
if(p.critical)
type = type^consts.SIGSUBPKT.FLAG_CRITICAL;
p.value = getValueForSignatureSubPacket(type, p.rawValue);
if(!subPackets[type])
subPackets[type] = [ ];
subPackets[type].unshift(p);
readon();
});
})
};
readon();
}
function getValueForSignatureSubPacket(type, binary) {
switch(type)
{
case consts.SIGSUBPKT.SIG_CREATED:
return new Date(binary.readUInt32BE(0)*1000);
case consts.SIGSUBPKT.SIG_EXPIRE:
return binary.readUInt32BE(0);
case consts.SIGSUBPKT.EXPORTABLE:
case consts.SIGSUBPKT.PRIMARY_UID:
case consts.SIGSUBPKT.REVOCABLE:
return !!binary.readUInt8(0);
case consts.SIGSUBPKT.TRUST:
return { level: binary.readUInt8(0), amount: binary.readUInt8(1) };
case consts.SIGSUBPKT.REGEXP:
var regexp = binary.toString("utf8", 0, binary.length);
var idx = regexp.indexOf("\0");
return (idx == -1 ? regexp : regexp.substr(0, idx));
case consts.SIGSUBPKT.KEY_EXPIRE:
return binary.readUInt32BE(0);
case consts.SIGSUBPKT.PREF_SYM:
case consts.SIGSUBPKT.PREF_HASH:
case consts.SIGSUBPKT.PREF_COMPR:
var prefs = [ ];
for(var i=0; i<binary.length; i++)
prefs.push(binary.readUInt8(i));
return prefs;
case consts.SIGSUBPKT.REV_KEY:
var flags = binary.readUInt8(0);
if(!(flags & 0x80))
return null;
return {
sensitive : !!(flags & 0x40),
pubkeyAlgo : binary.readUInt8(1),
fingerprint : binary.toString("hex", 2, 22).toUpperCase()
};
case consts.SIGSUBPKT.ISSUER:
return binary.toString("hex", 0, 8).toUpperCase();
case consts.SIGSUBPKT.NOTATION:
var flags = binary.readUInt32BE(0);
var readable = !!(flags & 0x80000000);
var nameLength = binary.readUInt16BE(1);
var valueLength = binary.readUInt16BE(3);
return {
name: binary.toString("utf8", 5, 5+nameLength),
value : readable ? binary.toString("utf8", 5+nameLength, 5+nameLength+valueLength) : binary.slice(5+nameLength, 5+nameLength+valueLength),
flags : flags
};
case consts.SIGSUBPKT.KS_FLAGS:
return { noModify : !!(binary.readUInt8(0) & 0x80) };
case consts.SIGSUBPKT.PREF_KS:
case consts.SIGSUBPKT.POLICY:
case consts.SIGSUBPKT.SIGNERS_UID:
return binary.toString("utf8", 0);
case consts.SIGSUBPKT.KEY_FLAGS:
var byte1 = binary.readUInt8(0);
var ret = { };
for(var i in consts.KEYFLAG)
ret[consts.KEYFLAG[i]] = !!(byte1 & consts.KEYFLAG[i]);
return ret;
case consts.SIGSUBPKT.REVOC_REASON:
return { code: binary.readUInt8(0), explanation: binary.toString("utf8", 1) };
case consts.SIGSUBPKT.FEATURES:
var byte1 = binary.readUInt8(0);
var ret = { };
for(var i in consts.FORMATS)
ret[consts.FORMATS[i]] = !!(byte1 & consts.FORMATS[i]);
return ret;
//case consts.SIGSUBPKT.SIGNATURE:
}
}
exports.getPacketInfo = getPacketInfo;
exports.getPublicKeyPacketInfo = getPublicKeyPacketInfo;
exports.getPublicSubkeyPacketInfo = getPublicSubkeyPacketInfo;
exports.getAttributePacketInfo = getAttributePacketInfo;
exports.getIdentityPacketInfo = getIdentityPacketInfo;
exports.getSignaturePacketInfo = getSignaturePacketInfo;