-
Notifications
You must be signed in to change notification settings - Fork 1
/
inlineVerify.js
74 lines (69 loc) · 2 KB
/
inlineVerify.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
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 inlineVerify = async (certfiles, verificationsOut) => {
const verificationKeys = await utils.load_certs(...certfiles);
const data = await utils.read_stdin();
let message;
try {
message = await openpgp.readMessage({ binaryMessage: data });
} catch (e) {
try {
message = await openpgp.readMessage({ armoredMessage: data.toString('utf8') });
} catch (e) {
try {
message = await openpgp.readCleartextMessage({ cleartextMessage: data.toString('utf8') });
} catch (e) {
console.error(e.message);
return process.exit(BAD_DATA);
}
}
}
const options = {
message,
verificationKeys
};
openpgp.verify(options).then(async (sig) => {
let count = 0;
let verifications = '';
for (const s of sig.signatures) {
let verified;
try {
verified = await s.verified;
} catch (e) {
console.error(e.message);
verified = false;
}
if (verified) {
count++;
const signature = await s.signature;
const timestamp = utils.format_date(signature.packets[0].created);
for (const cert of verificationKeys) {
const signingKey = await cert.getSigningKey(s.keyID, null).catch(() => null);
if (signingKey) {
verifications +=
timestamp
+ ' ' + signingKey.getFingerprint().toUpperCase()
+ ' ' + cert.getFingerprint().toUpperCase()
+ '\n';
break;
}
}
}
}
if (count == 0) {
return process.exit(NO_SIGNATURE);
}
if (verificationsOut) {
fs.writeFileSync(verificationsOut, verifications);
}
process.stdout.write(sig.data);
}).catch((e) => {
console.error(e.message);
return process.exit(BAD_DATA);
});
};
module.exports = inlineVerify;