[keyman] keep the passphrase off argv, and recover a missing .pub

Generate prompted for the passphrase itself and passed it as `-N <value>`,
so it sat in this process's argv — readable by any user on the box through
`ps` for the length of the spawn — and in keyman's memory before that.
Verified that omitting `-N` makes ssh-keygen prompt *and* confirm, so the
prompt and the flag are both gone and the spawn inherits stdio. keyman no
longer learns the passphrase, which is strictly better than handling it more
carefully, and it deletes code.

The other half is the missing `.pub`. The selection list is built from
private keys, so an orphan is offered like any other, and copyFileSync
discovered the absent sibling only *after* age had written the encrypted
key: a vault entry with no public key, and an exception that took the rest
of the batch with it. It is now derived with `ssh-keygen -y -f`, before the
vault directory is created. Verified against real binaries that the derived
key matches the original byte for byte, that an encrypted key prompts (on
stderr — hence stdout piped, stdin and stderr inherited), and that a refused
derivation degrades to storing the private key alone rather than failing.

encrypt's loop now isolates per key and reports which ones did not make it,
except for ToolNotFoundError: age missing is not a per-key problem and nine
more identical errors help nobody.

storeInVault is the shared write path both callers had a copy of. It also
undoes its own mess: age has to write into a directory that already exists,
so a failure could leave an empty directory or a truncated .age — which list
counts as a vault entry and decrypt offers. The .age is removed because we
named it, the directory only while empty, since one holding an earlier key
is not ours to delete.
This commit is contained in:
Benjamin Diedrichsen
2026-07-30 15:01:22 +02:00
parent da9df57e11
commit 764f890900
6 changed files with 395 additions and 52 deletions
+23 -9
View File
@@ -1,7 +1,8 @@
import fs from 'node:fs';
import path from 'node:path';
import inquirer from 'inquirer';
import { runTool } from './keyman.utils.js';
import { ToolNotFoundError } from './keyman.utils.js';
import { storeInVault } from './keyman.vault.js';
/**
* Private keys in a directory that may not exist.
@@ -35,17 +36,30 @@ export async function encryptKeys(sshDir: string, keysDir: string, tmpDir: strin
},
]);
const failed: string[] = [];
for (const key of selectedKeys) {
const keyPath = path.join(tmpKeys.includes(key) ? tmpDir : sshDir, key);
const vaultPath = path.join(keysDir, key.replace('id_', ''));
fs.mkdirSync(vaultPath, { recursive: true, mode: 0o700 });
// Encrypt key using `age`
await runTool('age', ['-r', pubkey, '-o', path.join(vaultPath, `${key}.age`), keyPath]);
try {
await storeInVault(keyPath, keysDir, pubkey);
} catch (error) {
// One bad key costs one key. Selecting ten and losing the last nine to an
// unreadable first one was the old behaviour, and nothing afterwards said
// which of the ten had made it into the vault.
if (error instanceof ToolNotFoundError) {
// Not a per-key problem: age is missing for all of them, so nine more
// identical failures would tell the user nothing new.
throw error;
}
failed.push(key);
console.error(`${key}: ${error instanceof Error ? error.message : error}`);
}
}
// Copy public key and create README
fs.copyFileSync(`${keyPath}.pub`, path.join(vaultPath, `${key}.pub`));
console.log(`🔒 Encrypted and stored: ${vaultPath}/${key}`);
if (failed.length > 0) {
console.log(
`\n⚠️ ${failed.length} of ${selectedKeys.length} selected keys were not stored: ${failed.join(', ')}`
);
}
}
+24 -31
View File
@@ -1,7 +1,8 @@
import fs from 'node:fs';
import path from 'node:path';
import { execa } from 'execa';
import inquirer from 'inquirer';
import { runTool } from './keyman.utils.js';
import { storeInVault } from './keyman.vault.js';
export async function generateKey(tmpDir: string, keysDir: string, pubkey: string) {
const { algorithm } = await inquirer.prompt<{ algorithm: string }>([
@@ -23,15 +24,6 @@ export async function generateKey(tmpDir: string, keysDir: string, pubkey: strin
},
]);
const { password } = await inquirer.prompt<{ password: string }>([
{
type: 'password',
name: 'password',
message: 'Enter passphrase (leave empty for no passphrase):',
mask: '*',
},
]);
const { identity } = await inquirer.prompt<{ identity: string }>([
{
type: 'input',
@@ -48,30 +40,31 @@ export async function generateKey(tmpDir: string, keysDir: string, pubkey: strin
return;
}
const args = ['-t', algorithm, '-f', keyPath, '-C', identity];
if (algorithm === 'rsa') {
args.push('-b', '4096');
}
try {
console.log(`Generating ${algorithm} key pair...`);
const args = ['-t', algorithm, '-f', keyPath, '-N', password, '-C', identity];
if (algorithm === 'rsa') {
args.push('-b', '4096');
}
await execa('ssh-keygen', args);
// No `-N`, and stdio inherited: ssh-keygen asks for the passphrase itself and
// confirms it. keyman used to prompt for it and pass it as `-N <value>`,
// which put the passphrase in this process's argv — readable by any user on
// the box via `ps` for as long as the spawn lived, and in keyman's memory
// before that. A passphrase keyman never learns cannot be leaked by keyman.
await runTool('ssh-keygen', args, { stdio: 'inherit' });
console.log(`✅ Key generated: ${keyPath}`);
// Encrypt the key
const folderName = fileName.replace('id_', '');
const vaultPath = path.join(keysDir, folderName);
fs.mkdirSync(vaultPath, { recursive: true });
// Encrypt key using `age`
await execa('age', ['-r', pubkey, '-o', path.join(vaultPath, `${fileName}.age`), keyPath]);
// Copy public key
fs.copyFileSync(`${keyPath}.pub`, path.join(vaultPath, `${fileName}.pub`));
console.log(`🔒 Encrypted and stored: ${vaultPath}`);
} catch (error) {
console.error(`❌ Error generating/encrypting key: ${error}`);
console.error(`❌ Error generating key: ${error instanceof Error ? error.message : error}`);
return;
}
try {
await storeInVault(keyPath, keysDir, pubkey);
} catch (error) {
// The private key is still in tmpDir, so this is recoverable by encrypting it
// — which is why it does not read as having lost the key.
console.error(`❌ Error encrypting key: ${error instanceof Error ? error.message : error}`);
console.error(` ${keyPath} was generated; encrypt it once the problem is fixed.`);
}
}
+88
View File
@@ -0,0 +1,88 @@
import fs from 'node:fs';
import path from 'node:path';
import { runTool } from './keyman.utils.js';
/**
* The public half of a private key, derived if the sibling file is missing.
*
* `encrypt` builds its selection list from private keys only, so a key whose
* `.pub` was deleted is offered like any other. Reading the sibling blindly meant
* finding out it was absent *after* `age` had written the encrypted key — a vault
* entry with no public key, and an exception that killed the rest of the batch.
*
* @returns the public key text, or null with the reason already reported
*/
async function publicKeyFor(keyPath: string): Promise<string | null> {
const sibling = `${keyPath}.pub`;
if (fs.existsSync(sibling)) {
return fs.readFileSync(sibling, 'utf-8');
}
const fileName = path.basename(keyPath);
console.log(`${fileName} has no .pub file — deriving it with ssh-keygen.`);
try {
// stdin and stderr inherited, stdout piped: verified that `ssh-keygen -y`
// prompts for the passphrase of an encrypted key, and that it prompts on
// *stderr*. Capturing everything would hide the prompt and then fail on the
// passphrase nobody was asked for; inheriting everything would lose the key.
const { stdout } = await runTool('ssh-keygen', ['-y', '-f', keyPath], {
stdio: ['inherit', 'pipe', 'inherit'],
});
const derived = stdout.trim();
if (derived) {
return `${derived}\n`;
}
} catch (error) {
console.warn(`⚠️ ${fileName}: ${error instanceof Error ? error.message : error}`);
}
console.warn(`⚠️ ${fileName}: no public key could be derived; storing the private key alone.`);
return null;
}
/**
* Encrypts one private key into `<keysDir>/<name>/`, alongside its public half.
*
* Shared by `encrypt` and `generate`, which were two copies of it.
*
* @returns the vault directory the key was stored in
*/
export async function storeInVault(
keyPath: string,
keysDir: string,
pubkey: string
): Promise<string> {
const fileName = path.basename(keyPath);
const vaultPath = path.join(keysDir, fileName.replace(/^id_/, ''));
// Before the directory exists, so a key that cannot be read does not leave one.
const publicKey = await publicKeyFor(keyPath);
fs.mkdirSync(vaultPath, { recursive: true, mode: 0o700 });
const encryptedKey = path.join(vaultPath, `${fileName}.age`);
try {
await runTool('age', ['-r', pubkey, '-o', encryptedKey, keyPath]);
} catch (error) {
// age writes into a directory that has to exist already, so a failure here
// leaves one behind — and possibly a truncated .age file, which `list` would
// count as a vault entry and `decrypt` would offer. Both are ours: the file
// because we named it, the directory only while it is empty, since one
// holding an earlier key is not.
fs.rmSync(encryptedKey, { force: true });
try {
fs.rmdirSync(vaultPath);
} catch {
// ENOTEMPTY — something else was already stored here.
}
throw error;
}
if (publicKey !== null) {
fs.writeFileSync(path.join(vaultPath, `${fileName}.pub`), publicKey);
}
console.log(`🔒 Encrypted and stored: ${path.join(vaultPath, `${fileName}.age`)}`);
return vaultPath;
}