forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkim.js
567 lines (511 loc) · 18.8 KB
/
dkim.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
'use strict';
var crypto = require('crypto');
var Stream = require('stream').Stream;
var indexOfLF = require('./utils').indexOfLF;
var util = require('util');
var dns = require('dns');
var fs = require('fs');
//////////////////////
// Common functions //
//////////////////////
function md5(str) {
if (!str) str = '';
var h = crypto.createHash('md5');
return h.update(str).digest('hex');
}
function Buf() {
this.bar = [];
this.blen = 0;
this.pop = function (buf) {
if (!this.bar.length) {
if (!buf) buf = new Buffer('');
return buf;
}
if (buf && buf.length) {
this.bar.push(buf);
this.blen += buf.length;
}
var nb = Buffer.concat(this.bar, this.blen);
this.bar = [];
this.blen = 0;
return nb;
};
this.push = function (buf) {
if (buf.length) {
this.bar.push(buf);
this.blen += buf.length;
}
};
}
////////////////
// DKIMObject //
////////////////
// There is one DKIMObject created for each signature found
function DKIMObject(header, header_idx, cb, timeout) {
this.cb = cb;
this.sig = header;
this.sig_md5 = md5(header);
this.run_cb = false;
this.header_idx = header_idx;
this.timeout = timeout;
this.fields = {};
this.headercanon = this.bodycanon = 'simple';
this.signed_headers = [];
this.identity = 'unknown';
this.line_buffer = new Buf();
this.last2octets = new Buffer(2);
this.dns_fields = {
'v': 'DKIM1',
'k': 'rsa',
'g': '*',
};
var m = /^([^:]+):\s*((?:.|[\r\n])*)$/.exec(header);
var sig = m[2].trim().replace(/\s+/,'');
var keys = sig.split(';');
for (var k=0; k<keys.length; k++) {
var key = keys[k].trim();
if (!key) continue; // skip empty keys
m = /^([^= ]+)=((?:.|[\r\n])+)$/.exec(key);
if (m) {
this.fields[m[1]] = m[2];
}
else {
return this.result('header parse error', 'invalid');
}
}
/////////////////////
// Validate fields //
/////////////////////
if (this.fields.v) {
if (this.fields.v !== '1') {
return this.result('incompatible version', 'invalid');
}
}
else {
return this.result('missing version', 'invalid');
}
if (this.fields.a) {
switch (this.fields.a) {
case 'rsa-sha1':
this.bh = crypto.createHash('SHA1');
this.verifier = crypto.createVerify('RSA-SHA1');
break;
case 'rsa-sha256':
this.bh = crypto.createHash('SHA256');
this.verifier = crypto.createVerify('RSA-SHA256');
break;
default:
this.debug('Invalid algorithm: ' + this.fields.a);
return this.result('invalid algorithm', 'invalid');
}
}
else {
return this.result('missing algorithm', 'invalid');
}
if (!this.fields.b) return this.result('signature missing', 'invalid');
if (!this.fields.bh) return this.result('body hash missing', 'invalid');
if (this.fields.c) {
var c = this.fields.c.split('/');
if (c[0]) this.headercanon = c[0];
if (c[1]) this.bodycanon = c[1];
}
if (!this.fields.d) return this.result('domain missing', 'invalid');
if (this.fields.h) {
var headers = this.fields.h.split(':');
for (var h=0; h<headers.length; h++) {
var header2 = headers[h].trim().toLowerCase();
this.signed_headers.push(header2);
}
if (this.signed_headers.indexOf('from') === -1) {
return this.result('from field not signed', 'invalid');
}
}
else {
return this.result('signed headers missing', 'invalid');
}
if (this.fields.i) {
// Make sure that this is a sub-domain of the 'd' field
var dom = this.fields.i.substr(this.fields.i.length - this.fields.d.length);
if (dom !== this.fields.d) {
return this.result('domain mismatch', 'invalid');
}
}
else {
this.fields.i = '@' + this.fields.d;
}
this.identity = this.fields.i;
if (this.fields.q && this.fields.q !== 'dns/txt') {
return this.result('unknown query method', 'invalid');
}
var now = new Date().getTime()/1000;
if (this.fields.t) {
if (this.fields.t > now) {
return this.result('bad creation date', 'invalid');
}
}
if (this.fields.x) {
if (this.fields.t && parseInt(this.fields.x) < parseInt(this.fields.t)) {
return this.result('invalid expiration date', 'invalid');
}
if (now > this.fields.x) {
return this.result('signature expired', 'invalid');
}
}
this.debug(this.identity + ': DKIM fields validated OK');
this.debug([
this.identity + ':',
'a=' + this.fields.a,
'c=' + this.headercanon + '/' + this.bodycanon,
'h=' + this.signed_headers,
].join(' '));
}
DKIMObject.prototype.debug = function (str) {
util.debug(str);
};
DKIMObject.prototype.header_canon_relaxed = function (header) {
var m = /^([^:]+):\s*((?:.|[\r\n])*)$/.exec(header);
if (!m) return header;
var hc = m[1].toLowerCase() + ':' + m[2];
hc = hc.replace(/\r\n([\t ]+)/g, "$1");
hc = hc.replace(/[\t ]+/g, ' ');
hc = hc.replace(/[\t ]+(\r?\n)$/, "$1");
return hc;
};
DKIMObject.prototype.add_body_line = function (line) {
if (this.run_cb) return;
// Buffer any lines
if ((line.length === 2 && line[0] === 0x0d && line[1] === 0x0a) ||
(line.length === 1 && line[0] === 0x0a))
{
// Store any empty lines as both canonicalization alogoriths
// ignore all empty lines at the end of the message body.
this.line_buffer.push(line);
}
else {
var l;
if (this.bodycanon === 'simple') {
l = this.line_buffer.pop(line);
if (l.length >= 2) {
this.last2octets[0] = l[l.length-2];
this.last2octets[1] = l[l.length-1];
}
this.bh.update(l);
}
else if (this.bodycanon === 'relaxed') {
l = this.line_buffer.pop(line).toString('binary');
l = l.replace(/[\t ]+(\r?\n)$/,"$1");
l = l.replace(/[\t ]+/g,' ');
l = this.line_buffer.pop(new Buffer(l));
this.bh.update(l);
}
}
};
DKIMObject.prototype.result = function (error, result) {
this.run_cb = true;
return this.cb(
((error) ? new Error(error) : null),
{
identity: this.identity,
selector: this.fields.s,
domain: this.fields.d,
result: result
}
);
};
DKIMObject.prototype.end = function () {
if (this.run_cb) return;
if (this.bodycanon === 'simple') {
// Add CRLF if there was no body or no trailing CRLF
if (!(this.last2octets[0] === 0x0d && this.last2octets[1] === 0x0a)) {
this.debug(this.identity + ': adding CRLF for simple body canonicalization');
this.bh.update(new Buffer("\r\n"));
}
}
var bh = this.bh.digest('base64');
this.debug(this.identity + ':' +
' bodyhash=' + this.fields.bh +
' computed=' + bh);
if (bh !== this.fields.bh) {
return this.result('body hash did not verify', 'fail');
}
// Now we canonicalize the specified headers
for (var h=0; h<this.signed_headers.length; h++) {
var header = this.signed_headers[h];
this.debug(this.identity + ': canonicalize header: ' + header);
if (this.header_idx[header]) {
for (var i=0; i<this.header_idx[header].length; i++) {
// Skip this signature if dkim-signature is specified
if (header === 'dkim-signature') {
var h_md5 = md5(this.header_idx[header][i]);
if (h_md5 === this.sig_md5) {
this.debug(this.identity + ': skipped our own DKIM-Signature');
continue;
}
}
if (this.headercanon === 'simple') {
this.verifier.update(this.header_idx[header][i]);
}
else if (this.headercanon === 'relaxed') {
var hc = this.header_canon_relaxed(this.header_idx[header][i]);
this.verifier.update(hc);
}
}
}
}
// Now add in our original DKIM-Signature header without the b= and trailing CRLF
var our_sig = this.sig.replace(/b=([^;]+)/,'b=');
if (this.headercanon === 'relaxed') {
our_sig = this.header_canon_relaxed(our_sig);
}
our_sig = our_sig.replace(/\r\n$/,'');
this.verifier.update(our_sig);
// Do the DNS lookup to retrieve the public key
var self = this;
var timeout = false;
var timer = setTimeout(function () {
timeout = true;
return self.result('DNS timeout', 'tempfail');
}, this.timeout * 1000);
var lookup = this.fields.s + '._domainkey.' + this.fields.d;
this.debug(this.identity + ': DNS lookup ' + lookup + ' (timeout=' + this.timeout + 's)');
dns.resolveTxt(lookup, function (err, res) {
if (timeout) return;
clearTimeout(timer);
if (err) {
switch (err.code) {
case 'ENOTFOUND':
case 'ENODATA':
case dns.NXDOMAIN:
return self.result('no key for signature', 'invalid');
default:
self.debug(self.identity + ': DNS lookup error: ' + err.code);
return self.result('key unavailable', 'tempfail');
}
}
if (!res) return self.result('no key for signature', 'invalid');
for (var r=0; r<res.length; r++) {
var record = res[r];
// Node 0.11.x compatibility
if (Array.isArray(record)) {
record = record.join('');
}
if (record.indexOf('p=') === -1) {
self.debug(self.identity + ': ignoring TXT record: ' + record);
continue;
}
self.debug(self.identity + ': got DNS record: ' + record);
var rec = record.replace(/\r?\n/g, '').replace(/\s+/g,'');
var split = rec.split(';');
for (var i=0; i<split.length; i++) {
var split2 = split[i].split('=');
if (split2[0]) self.dns_fields[split2[0]] = split2[1];
}
// Validate
if (!self.dns_fields.v || self.dns_fields.v !== 'DKIM1') {
return self.result('invalid version', 'invalid');
}
if (self.dns_fields.g) {
if (self.dns_fields.g !== '*') {
var s = self.dns_fields.g;
// Escape any special regexp characters
s = s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
// Make * a non-greedy match against anything except @
s = s.replace('\\*','[^@]*?');
var reg = new RegExp('^' + s + '@');
self.debug(self.identity + ': matching ' + self.dns_fields.g +
' against i=' + self.fields.i + ' regexp=' + reg.toString());
if (!reg.test(self.fields.i)) {
return self.result('inapplicable key', 'invalid');
}
}
}
else {
return self.result('inapplicable key', 'invalid');
}
if (self.dns_fields.h) {
var hashes = self.dns_fields.h.split(':');
for (var h=0; h<hashes.length; h++) {
var hash = hashes[h].trim();
if (self.fields.a.indexOf(hash) === -1) {
return self.result('inappropriate hash algorithm', 'invalid');
}
}
}
if (self.dns_fields.k) {
if (self.fields.a.indexOf(self.dns_fields.k) === -1) {
return self.result('inappropriate key type', 'invalid');
}
}
if (self.dns_fields.t) {
var flags = self.dns_fields.t.split(':');
for (var f=0; f<flags.length; f++) {
var flag = flags[f].trim();
if (flag === 'y') {
// Test mode
self.test_mode = true;
}
else if (flag === 's') {
// 'i' and 'd' domain much match exactly
var j = self.fields.i;
j = j.substr(j.indexOf('@')+1, j.length);
if (j !== self.fields.d) {
return self.result('domain mismatch', 'invalid');
}
}
}
}
if (!self.dns_fields.p) return self.result('key revoked', 'invalid');
// crypto.verifier requires the key in PEM format
self.public_key = '-----BEGIN PUBLIC KEY-----\r\n' +
self.dns_fields.p.replace(/(.{1,76})/g, '$1\r\n') +
'-----END PUBLIC KEY-----\r\n';
var verified;
try {
verified = self.verifier.verify(self.public_key, self.fields.b, 'base64');
self.debug(self.identity + ': verified=' + verified);
}
catch (e) {
self.debug(self.identity + ': verification error: ' + e.message);
return self.result('verification error', 'invalid');
}
return self.result(null, ((verified) ? 'pass' : 'fail'));
}
// We didn't find a valid DKIM record for this signature
return self.result('no key for signature', 'invalid');
});
};
exports.DKIMObject = DKIMObject;
//////////////////////
// DKIMVerifyStream //
//////////////////////
function DKIMVerifyStream(cb, timeout) {
Stream.call(this);
this.run_cb = false;
var self = this;
this.cb = function (err, result, results) {
if (!self.run_cb) {
self.run_cb = true;
return cb(err, result, results);
}
};
this._in_body = false;
this._no_signatures_found = false;
this.buffer = new Buf();
this.headers = [];
this.header_idx = {};
this.dkim_objects = [];
this.results = [];
this.result = 'none';
this.pending = 0;
this.writable = true;
this.timeout = timeout || 30;
}
util.inherits(DKIMVerifyStream, Stream);
DKIMVerifyStream.prototype.debug = function (str) {
util.debug(str);
};
DKIMVerifyStream.prototype.handle_buf = function (buf) {
var self = this;
// Abort any further processing if the headers
// did not contain any DKIM-Signature fields.
if (this._in_body && this._no_signatures_found) {
return true;
}
buf = this.buffer.pop(buf);
var callback = function (err, result) {
self.pending--;
if (result) {
self.results.push({
identity: result.identity,
domain: result.domain,
selector: result.selector,
result: result.result,
error: ((err) ? err.message : null)
});
// Set the overall result based on this precedence order
var rr = ['pass','tempfail','fail','invalid','none'];
for (var r=0; r<rr.length; r++) {
if (!self.result || (self.result && self.result !== rr[r] && result.result === rr[r])) {
self.result = rr[r];
}
}
}
self.debug(JSON.stringify(result));
if (self.pending === 0 && self.cb) {
return self.cb(null, self.result, self.results);
}
};
// Process input buffer into lines
var offset = 0;
while ((offset = indexOfLF(buf)) !== -1) {
var line = buf.slice(0, offset+1);
if (buf.length > offset) {
buf = buf.slice(offset+1);
}
// Check for LF line endings and convert to CRLF if necessary
if (line[line.length-2] !== 0x0d) {
line = Buffer.concat([ line.slice(0, line.length-1), new Buffer("\r\n") ], line.length+1);
}
// Look for CRLF
if (line.length === 2 && line[0] === 0x0d && line[1] === 0x0a) {
// Look for end of headers marker
if (!this._in_body) {
this._in_body = true;
// Parse the headers
for (var h=0; h<this.headers.length; h++) {
var header;
if ((header = /^([^: ]+):\s*((:?.|[\r\n])*)/.exec(this.headers[h]))) {
var hn = header[1].toLowerCase();
if (!this.header_idx[hn]) this.header_idx[hn] = [];
this.header_idx[hn].push(this.headers[h]);
}
}
if (!this.header_idx['dkim-signature']) {
this._no_signatures_found = true;
return this.cb(null, this.result);
}
else {
// Create new DKIM objects for each header
var dkim_headers = this.header_idx['dkim-signature'];
this.debug('Found ' + dkim_headers.length + ' DKIM signatures');
this.pending = dkim_headers.length;
for (var d=0; d<dkim_headers.length; d++) {
this.dkim_objects.push(new DKIMObject(dkim_headers[d], this.header_idx, callback, this.timeout));
}
if (this.pending === 0) {
if (this.cb) this.cb(new Error('no signatures found'));
}
}
continue; // while()
}
}
if (!this._in_body) {
// Parse headers
if (line[0] === 0x20 || line[0] === 0x09) {
// Header continuation
this.headers[this.headers.length-1] += line.toString('ascii');
}
else {
this.headers.push(line.toString('ascii'));
}
}
else {
for (var e=0; e<this.dkim_objects.length; e++) {
this.dkim_objects[e].add_body_line(line);
}
}
}
this.buffer.push(buf);
return true;
};
DKIMVerifyStream.prototype.write = function(buf) {
return this.handle_buf(buf);
};
DKIMVerifyStream.prototype.end = function(buf) {
if (buf) this.handle_buf(buf);
for (var d=0; d<this.dkim_objects.length; d++) {
this.dkim_objects[d].end();
}
if (this.pending === 0 && this._no_signatures_found === false) this.cb(null, this.result);
};
exports.DKIMVerifyStream = DKIMVerifyStream;