-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathverify-signatures-test.js
83 lines (65 loc) · 2.36 KB
/
verify-signatures-test.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
/* eslint no-unused-expressions:0 */
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Path = require('path');
const { dkimSign } = require('../../../lib/dkim/sign');
const { dkimVerify } = require('../../../lib/dkim/verify');
let fs = require('fs');
const curTime = new Date(1528637909000);
const dnsCache = require('./fixtures/dns.json');
const privateKeyEC = fs.readFileSync(Path.join(__dirname, 'fixtures', 'private-ec.key'));
const privateKeyRSA = fs.readFileSync(Path.join(__dirname, 'fixtures', 'private-rsa.key'));
const signedEmail = fs.readFileSync(Path.join(__dirname, 'fixtures', 'signed.eml'));
const formatECPrivateKey = key => {
if (key.length === 44) {
return `-----BEGIN PRIVATE KEY-----
${Buffer.concat([Buffer.from('MC4CAQAwBQYDK2VwBCIEIA==', 'base64'), Buffer.from(key, 'base64')]).toString('base64')}
-----END PRIVATE KEY-----`;
}
return key;
};
const cachedResolver = async (name, rr) => {
let match = dnsCache?.[name]?.[rr];
if (!match) {
let err = new Error('Error');
err.code = 'ENOTFOUND';
throw err;
}
return match;
};
chai.config.includeStack = true;
// FIXME: EC signing does not work!
describe('DKIM EC Signature tests', () => {
it('Should sign an email', async () => {
let ecPrivateKey = formatECPrivateKey(privateKeyEC);
let res = await dkimSign(signedEmail, {
canonicalization: 'relaxed/relaxed',
signTime: curTime,
signatureData: [
{
algorithm: 'rsa-sha256',
signingDomain: 'football.example.com',
selector: 'test',
privateKey: privateKeyRSA
},
{
algorithm: 'ed25519-sha256',
signingDomain: 'football.example.com',
selector: 'brisbane',
privateKey: ecPrivateKey
}
]
});
expect(res.signatures).to.exist;
});
it('Should verify hashes for a signed email', async () => {
let res = await dkimVerify(signedEmail, {
resolver: cachedResolver,
curTime
});
expect(res.results[0].status.result).equal('pass');
expect(res.results[1].status.result).equal('pass');
expect(true).to.equal(true);
});
});