Skip to content

Commit

Permalink
feat(manifest sign): better passphrase handling in manifest sign command
Browse files Browse the repository at this point in the history
JST-396
  • Loading branch information
pgrzy-golem committed Sep 11, 2023
1 parent e2454e7 commit 176636e
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/manifest/manifest-sign.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,39 @@ export async function manifestSignAction(options: ManifestSignOptions): Promise<
const manifestBase64 = manifestBuffer.toString("base64");

const keyFile = await readFile(options.keyFile);
let passphraseRequired = keyFile.toString("ascii").includes("BEGIN ENCRYPTED PRIVATE KEY");

Check failure on line 17 in src/manifest/manifest-sign.action.ts

View workflow job for this annotation

GitHub Actions / Build and unit-test on supported platforms and NodeJS versions (16.x, ubuntu-latest)

'passphraseRequired' is never reassigned. Use 'const' instead

Check failure on line 17 in src/manifest/manifest-sign.action.ts

View workflow job for this annotation

GitHub Actions / Build and unit-test on supported platforms and NodeJS versions (18.x, ubuntu-latest)

'passphraseRequired' is never reassigned. Use 'const' instead

Check failure on line 17 in src/manifest/manifest-sign.action.ts

View workflow job for this annotation

GitHub Actions / Build and unit-test on supported platforms and NodeJS versions (20.x, ubuntu-latest)

'passphraseRequired' is never reassigned. Use 'const' instead

// Parse key file to KeyObject?
if (passphraseRequired && !options.passphrase) {
console.error("Error: Private key file is encrypted and no passphrase was provided. Use --passphrase option.");
process.exit(1);
} else if (!passphraseRequired && options.passphrase) {
console.error("Error: Private key file is not encrypted and passphrase was provided. Remove --passphrase option.");
process.exit(1);
}

// Sign the manifest.
let signature: Buffer;
const sign = createSign("RSA-SHA256");
sign.update(manifestBase64);
const signature = sign.sign({
key: keyFile,
// FIXME: Allow secure passphrase input and detect if a passphrase is needed.
passphrase: options.passphrase,
});

try {
signature = sign.sign({
key: keyFile,
passphrase: options.passphrase,
});
} catch (e) {
if (e instanceof Error && "code" in e) {
if (e.code === "ERR_OSSL_BAD_DECRYPT") {
console.error(`Error: Wrong passphrase provided for the private key ${options.keyFile}.`);
process.exit(1);
} else if (e.code === "ERR_OSSL_UNSUPPORTED") {
console.error(`Error: Private key file ${options.keyFile} is not supported.`);
process.exit(1);
}
}

throw e;
}

// write signature to options.signatureFile.
await writeFile(options.signatureFile, Buffer.from(signature).toString("base64"), "ascii");
Expand Down

0 comments on commit 176636e

Please sign in to comment.