-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify.js
67 lines (60 loc) · 1.78 KB
/
verify.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
const openpgp = require('./initOpenpgp');
const fs = require('fs');
const process = require('process');
const utils = require('./utils');
const NO_SIGNATURE = 3;
const BAD_DATA = 41;
const verify = async (signature, certfiles) => {
const certs = await utils.load_certs(...certfiles);
const sigBuf = fs.readFileSync(signature);
let sig;
try {
sig = await openpgp.readSignature({ binarySignature: sigBuf });
} catch (e) {
try {
sig = await openpgp.readSignature({ armoredSignature: sigBuf.toString('utf8') });
} catch (e) {
console.error(e.message);
return process.exit(BAD_DATA);
}
}
const data = await utils.read_stdin();
const options = {
message: await openpgp.createMessage({ text: data.toString('utf8') }),
verificationKeys: certs,
signature: sig,
};
openpgp.verify(options).then(async (sig) => {
let count = 0;
for (const s of sig.signatures) {
let verified;
try {
verified = await s.verified;
} catch (e) {
console.error(e.message);
verified = false;
}
if (verified) {
count += 1;
const signature = await s.signature;
const timestamp = utils.format_date(signature.packets[0].created);
for (const cert of certs) {
const signingKey = await cert.getSigningKey(s.keyID, null).catch(() => null);
if (signingKey) {
console.log(timestamp
+ ' ' + signingKey.getFingerprint().toUpperCase()
+ ' ' + cert.getFingerprint().toUpperCase());
break;
}
}
}
}
if (count == 0) {
return process.exit(NO_SIGNATURE);
}
}).catch((e) => {
console.error(e.message);
return process.exit(BAD_DATA);
});
};
module.exports = verify;