forked from WICG/webpackage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-sign.ts
More file actions
435 lines (396 loc) · 14.6 KB
/
Copy pathcli-sign.ts
File metadata and controls
435 lines (396 loc) · 14.6 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
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
import crypto, { KeyObject } from 'crypto';
import * as fs from 'fs';
import { createRequire } from 'module';
import { Command } from 'commander';
import pc from 'picocolors';
import {
errorLog,
greenConsoleLog,
infoLog,
parseMaybeEncryptedKey,
parseMaybeEncryptedKeyFromFile,
warnLog,
} from './utils/cli-utils.js';
import {
getRawPublicKey,
isPureWebBundle,
isSignedWebBundle,
} from './utils/utils.js';
import { NodeCryptoSigningStrategy, SignedWebBundle } from './wbn-sign.js';
const require = createRequire(import.meta.url);
const { name, version } = require('../package.json');
// Get output file path depending on options: bundle path if in-place and provided path otherwise.
function getOutputPath(
bundlePath: string,
options: { inPlace?: boolean; output?: string }
) {
if (options.inPlace && options.output != null) {
throw new Error(
"Options '--in-place' and '--output' are mutually exclusive."
);
}
if (!options.inPlace && options.output == null) {
throw new Error("One of '--in-place' and '--output' options must be used.");
}
if (options?.output && fs.existsSync(options.output)) {
warnLog(
`The file in output path ${options.output} already exists. Overwriting.`
);
}
return options.output ?? bundlePath;
}
// That key may be either provided by Bytes encoded in base64 or path to file with key. This function checks that and parses key.
async function parseRemovalKey(keyInput: string): Promise<Uint8Array> {
if (keyInput.endsWith('.pem') || fs.existsSync(keyInput)) {
if (!fs.existsSync(keyInput)) {
throw new Error(`The key file "${keyInput}" does not exist.`);
}
const data = await fs.promises.readFile(keyInput);
try {
// Try parsing as private key (it handles encrypted ones too)
const privateKey = await parseMaybeEncryptedKey(data, keyInput);
return getRawPublicKey(privateKey);
} catch {
// Try parsing as public key
try {
const pubKeyObject = crypto.createPublicKey(data);
return getRawPublicKey(pubKeyObject);
} catch (err) {
throw new Error(
`Failed to parse key from file "${keyInput}". Ensure it is a valid PEM-encoded Ed25519 or ECDSA P-256 key.`,
{ cause: err }
);
}
}
} else {
// Assume Base64 string
// Base64 regex check (simplified, but sufficient for raw keys)
if (!/^[A-Za-z0-9+/=]+$/.test(keyInput)) {
throw new Error(
`The provided key "${keyInput}" is neither a valid file path nor a proper Base64-encoded string.`
);
}
return new Uint8Array(Buffer.from(keyInput, 'base64'));
}
}
const program = new Command()
.name(name)
.version(version, '-V, --version', 'Display version')
.description(
'CLI tool for managing signatures and keys for web bundles. Operates on `wbn` and `swbn` files.'
)
.helpOption('-h, --help', 'Display help');
async function parseArguments(): Promise<void> {
program.commandsGroup('General commands');
program.helpCommand('help [command]', 'Display help for command');
program
.command('info')
.summary('Display integrity block information for a signed web bundle.')
.description(
'Display integrity block information for a signed web bundle, including the Web Bundle ID and signatures. \n\n' +
'WARNING: Experimental. The output format is subject to change. Do not use in scripts or automation.'
)
.argument('<web_bundle>', 'Path to a signed web bundle `*.swbn`')
.action(async (webBundlePath) => {
const webBundle = await fs.promises.readFile(webBundlePath);
const signedWebBundle = SignedWebBundle.fromBytes(webBundle);
signedWebBundle.printInfo();
const validations = signedWebBundle.validateSignatures();
validations.forEach((val, i) => {
if (val.status === 'error') {
console.log(pc.red(`Signature ${i} validation failed: ${val.error}`));
} else {
console.log(
`Signature ${i} derived Web Bundle ID: ${val.derivedBundleId}`
);
console.log(
`Signature ${i} is correct: ${
val.isValid ? pc.green('Yes') : pc.red('No')
}`
);
}
});
});
program.commandsGroup('Signature management commands');
program
.command('add-signature')
.summary('Add signatures to an existing signed web bundle.')
.description(
'Add one or more signatures to the integrity block of a signed web bundle. ' +
'This updates the bundle’s metadata with new signatures without altering ' +
'the bundled resources.\n\n' +
'For encrypted keys, you will be prompted for a password unless the ' +
'WEB_BUNDLE_SIGNING_PASSPHRASE environment variable is set.'
)
.argument('<signed_web_bundle>', 'Path to a signed web bundle (*.swbn).')
.argument(
'<private_keys...>',
'*.pem files containing ecdsaP256 or ed25519 private keys.'
)
.option(
'-i, --in-place',
'Overwrite the input file with the new signatures. Incompatible with --output.',
false
)
.option(
'-o, --output <file>',
'Path for the new signed output file. Incompatible with --in-place.'
)
.action(async (webBundlePath, keyFilesPaths, options) => {
const outputPath = getOutputPath(webBundlePath, options);
const webBundle = await fs.promises.readFile(webBundlePath);
const signedWebBundle = SignedWebBundle.fromBytes(webBundle);
for (const keyPath of keyFilesPaths) {
const privateKey = await parseMaybeEncryptedKeyFromFile(keyPath);
await signedWebBundle.addSignature(
new NodeCryptoSigningStrategy(privateKey)
);
}
await fs.promises.writeFile(
outputPath,
signedWebBundle.getSignedWebBundleBytes()
);
greenConsoleLog(
`Signature${keyFilesPaths.length > 1 ? 's' : ''} added successfully.`
);
});
program
.command('remove-signature')
.summary('Remove signatures from a signed web bundle.')
.description(
'Remove one or more signatures from the integrity block of a signed web bundle. ' +
'Signatures are identified by providing their associated public or private keys.\n\n' +
'Tip: Use the `info` command to list the public keys currently present in a bundle.\n\n' +
'For encrypted private keys, you will be prompted for a password unless ' +
'the WEB_BUNDLE_SIGNING_PASSPHRASE environment variable is set.'
)
.argument('<signed_web_bundle>', 'Path to a signed web bundle (*.swbn).')
.argument(
'<keys...>',
'Public keys (Base64 strings or .pem files) or private keys (.pem) ' +
'used to identify signatures for removal.'
)
.option(
'-i, --in-place',
'Overwrite the input file. Incompatible with --output.',
false
)
.option(
'-o, --output <file>',
'Path for the new signed output file. Incompatible with --in-place.'
)
.action(async (webBundlePath, keyInputs, options) => {
const outputPath = getOutputPath(webBundlePath, options);
const webBundle = await fs.promises.readFile(webBundlePath);
const signedWebBundle = SignedWebBundle.fromBytes(webBundle);
for (const keyInput of keyInputs) {
const publicKey = await parseRemovalKey(keyInput);
signedWebBundle.removeSignature(publicKey);
}
await fs.promises.writeFile(
outputPath,
signedWebBundle.getSignedWebBundleBytes()
);
greenConsoleLog(
`Signature${keyInputs.length > 1 ? 's' : ''} removed successfully.`
);
});
program
.command('replace-signature')
.summary('Replace an existing signature with a new one.')
.description(
'Replace a specific signature in the integrity block with a new signature. ' +
'This is a convenience command equivalent to performing an `add-signature` ' +
'followed by a `remove-signature`.\n\n' +
'Tip: Use the `info` command to identify the public key of the signature you wish to replace.\n\n' +
'For encrypted private keys, you will be prompted for a password unless ' +
'the WEB_BUNDLE_SIGNING_PASSPHRASE environment variable is set.'
)
.argument('<signed_web_bundle>', 'Path to a signed web bundle (*.swbn).')
.argument(
'<old_key>',
'The public key (Base64 or .pem) or private key (.pem) of the signature to replace.'
)
.argument(
'<new_private_key>',
'The new *.pem file (ecdsaP256 or ed25519) to sign the bundle with.'
)
.option(
'-i, --in-place',
'Overwrite the input file. Incompatible with --output.',
false
)
.option(
'-o, --output <file>',
'Path for the new signed output file. Incompatible with --in-place.'
)
.action(async (webBundlePath, oldKeyInput, newKeyPath, options) => {
const outputPath = getOutputPath(webBundlePath, options);
const webBundle = await fs.promises.readFile(webBundlePath);
const signedWebBundle = SignedWebBundle.fromBytes(webBundle);
const oldPublicKey = await parseRemovalKey(oldKeyInput);
const newPrivateKey = await parseMaybeEncryptedKeyFromFile(newKeyPath);
await signedWebBundle.addSignature(
new NodeCryptoSigningStrategy(newPrivateKey)
);
signedWebBundle.removeSignature(oldPublicKey);
await fs.promises.writeFile(
outputPath,
signedWebBundle.getSignedWebBundleBytes()
);
greenConsoleLog('Signature replaced successfully.');
});
program
.command('sign')
.summary(
'Sign a web bundle with private key(s). Produces signed web bundle.'
)
.description(
`Sign a web bundle using one or more private keys to produce a signed web bundle (.swbn). Only .pem files are supported.`
)
.argument('<web_bundle>', 'The *.wbn file to sign')
.argument(
'<private_keys...>',
'*.pem files containing ecdsaP256 or ed25519 private keys.' +
'For encrypted keys, you will be prompted for a password' +
'unless the WEB_BUNDLE_SIGNING_PASSPHRASE env var is set.'
)
.option(
'-o, --output <file>',
'Path for the signed web bundle output file',
/*defaultValue=*/ 'signed.swbn'
)
.option(
'--web-bundle-id <web-bundle-id>',
'Web bundle ID. Derived from the first key if not specified.'
)
.showHelpAfterError()
.action(async (webBundlePath, keyFilesPaths, options) => {
if (!options.webBundleId) {
infoLog(
`The bundle id was not specified. It will be derived from the ${
keyFilesPaths.length > 1 ? 'first ' : ''
}given key.`
);
}
if (fs.existsSync(options.output)) {
warnLog(`'${options.output}' file already exists. Overwriting.`);
}
await readVerifyAndSignWebBundle(
webBundlePath,
keyFilesPaths,
options.output,
options.webBundleId
);
});
program
.command('sing', { hidden: true })
.argument('[anything...]')
.action(() => {
greenConsoleLog('🎶 Never gonna let you down, lalala la lala... 🎶 \n');
errorLog("Unrecognized command 'sing'. Use 'sign' instead.\n");
process.exit(1);
});
// This default command provides backward compatibility.
// The tool in the past only supported signing and didn't use commands.
program
.command('backward-compatibility-sign', { isDefault: true, hidden: true })
// That's the workaround for proper error message, when an improper command is used.
// It's then interpreted as an argument of the default command, so falls here.
.argument('[...]', '')
.option('-i, --input <file>', 'input web bundle to be signed (required)')
.option(
'-k, --private-key <file...>',
'paths to Ed25519 / ECDSA P-256 private key(s) (required)'
)
.option(
'-o, --output <file>',
'signed web bundle output file',
/*defaultValue=*/ 'signed.swbn'
)
.option('--web-bundle-id <web-bundle-id>', 'web bundle ID')
// Command-specific error message on parsing error (e.g. no value, or incorrect option)
.showHelpAfterError()
.action(async (args, options, command) => {
// Wrong command
if (args.length > 0) {
// Help quits the program, so not need to do it manually.
program.help();
}
// Does it seem like backward-compatible format usage? If not just show help.
if (
!('input' in options) &&
!('privateKey' in options) &&
!('webBundleId' in options)
) {
program.help();
}
// Backward-compatible mode
warnLog(
'This `wbn-sign` usage is deprecated. Please check `wbn-sign help`. This CLI usage form may be not supported in the future.'
);
if (!('input' in options) || !('privateKey' in options)) {
errorLog(
`input and private key options are required! Please, consider using new cli (see \`wbn-sign help\`)`
);
command.help();
}
if (options.privateKey.length > 1 && !options.webBundleId) {
errorLog(
`--web-bundle-id must be specified if there's more than 1 signing key involved.`
);
command.help();
}
await readVerifyAndSignWebBundle(
options.input,
options.privateKey,
options.output,
options.webBundleId
);
});
// All errors that happen during command execution are caught here
try {
await program.parseAsync(process.argv);
} catch (err) {
if (err instanceof Error) {
errorLog(err.message);
}
process.exit(1);
}
}
async function readVerifyAndSignWebBundle(
wbnFilePath: string,
keyFilesPaths: string[],
outputFilePath: string,
maybeWebBundleId?: string
) {
const webBundle = await fs.promises.readFile(wbnFilePath);
if (isSignedWebBundle(webBundle)) {
throw new Error(
'Web bundle already signed. Use `add-signature` command instead.'
);
}
if (!isPureWebBundle(webBundle)) {
throw new Error('Not a web bundle.');
}
const privateKeys = new Array<KeyObject>();
for (const privateKey of keyFilesPaths) {
privateKeys.push(await parseMaybeEncryptedKeyFromFile(privateKey));
}
const signingStrategies = privateKeys.map(
(privateKey) => new NodeCryptoSigningStrategy(privateKey)
);
const signedWebBundle = await SignedWebBundle.fromWebBundle(
Uint8Array.from(webBundle),
signingStrategies,
maybeWebBundleId ? { webBundleId: maybeWebBundleId } : undefined
);
greenConsoleLog(`${signedWebBundle.getWebBundleId()}`);
await fs.promises.writeFile(
outputFilePath,
signedWebBundle.getSignedWebBundleBytes()
);
}
export async function main() {
await parseArguments();
}