Skip to content

Commit

Permalink
Merge pull request #20 from golemfactory/feature/JST-396/Better-passp…
Browse files Browse the repository at this point in the history
…hrase-handling

Feature/jst 396/better passphrase handling
  • Loading branch information
pgrzy-golem authored Sep 11, 2023
2 parents c19f60c + cea82f2 commit 157f889
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);
const passphraseRequired = keyFile.toString("ascii").includes("BEGIN ENCRYPTED PRIVATE KEY");

// 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 157f889

Please sign in to comment.