Repository navigation
Expand file tree
/
Copy pathpgp.ts
More file actions
94 lines (86 loc) · 2.85 KB
/
Copy pathpgp.ts
File metadata and controls
94 lines (86 loc) · 2.85 KB
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
import { createReadStream, promises as fs } from "fs";
import * as openpgp from "openpgp";
import * as path from "path";
import { Readable } from "stream";
import { errToStr } from "./error/normalize";
import { type Logger } from "./logging/logger";
export type Key = openpgp.Key;
export enum VerificationErrorCode {
/* The signature does not match. */
Invalid = "Invalid",
/* Failed to read the signature or the file to verify. */
Read = "Read",
}
export class VerificationError extends Error {
constructor(
public readonly code: VerificationErrorCode,
message: string,
) {
super(message);
}
summary(): string {
switch (this.code) {
case VerificationErrorCode.Invalid:
return "Signature does not match";
case VerificationErrorCode.Read:
return "Failed to read signature";
}
}
}
/**
* Return the public keys bundled with the plugin.
*/
export async function readPublicKeys(logger?: Logger): Promise<Key[]> {
const keyFile = path.join(__dirname, "../pgp-public.key");
logger?.info("Reading public key", keyFile);
const armoredKeys = await fs.readFile(keyFile, "utf8");
return openpgp.readKeys({ armoredKeys });
}
/**
* Given public keys, a path to a file to verify, and a path to a detached
* signature, verify the file's signature. Throw VerificationError if invalid
* or unable to validate.
*/
export async function verifySignature(
publicKeys: openpgp.Key[],
cliPath: string,
signaturePath: string,
logger?: Logger,
): Promise<void> {
try {
logger?.info("Reading signature", signaturePath);
const armoredSignature = await fs.readFile(signaturePath, "utf8");
const signature = await openpgp.readSignature({ armoredSignature });
logger?.info("Verifying signature of", cliPath);
const message = await openpgp.createMessage({
// openpgpjs only accepts web readable streams.
binary: Readable.toWeb(createReadStream(cliPath)),
});
const verificationResult = await openpgp.verify({
message,
signature,
verificationKeys: publicKeys,
config: {
// Rational https://github.com/coder/vscode-coder/pull/748#issuecomment-3789538490
allowInsecureVerificationWithReformattedKeys: true,
},
});
for await (const _ of verificationResult.data) {
// The docs indicate this data must be consumed; it triggers the
// verification of the data.
}
try {
const { verified } = verificationResult.signatures[0];
await verified; // Throws on invalid signature.
logger?.info("Binary signature matches");
} catch (e) {
const error = `Unable to verify the authenticity of the binary: ${errToStr(e)}. The binary may have been tampered with.`;
logger?.warn(error);
throw new VerificationError(VerificationErrorCode.Invalid, error);
}
} catch (e) {
const error = `Failed to read signature or binary: ${errToStr(e)}.`;
logger?.warn(error);
throw new VerificationError(VerificationErrorCode.Read, error);
}
}