Add OpenPGP key passphrase changes
Allow desktop users to re-encrypt an existing key after verifying its current passphrase. Preserve key identity and revocation data, clear its cached passphrase, and document the feature. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -43,6 +43,52 @@ test('export requires the right passphrase and round-trips through import', asyn
|
||||
assert.ok((await second.exportPublic(key.fingerprint)).includes('BEGIN PGP PUBLIC KEY BLOCK'));
|
||||
});
|
||||
|
||||
test('changes a key passphrase without changing its public identity or revocation certificate', async () => {
|
||||
const storage = memoryStore();
|
||||
const keys = createKeyStore(storage);
|
||||
const { key, revocationCertificate } = await keys.generate({ name: 'Ada', email: 'ada@example.com', passphrase });
|
||||
const original = JSON.parse(JSON.stringify((await storage.load()).keys[0]));
|
||||
const newPassphrase = 'new passphrase securely';
|
||||
|
||||
await assert.rejects(keys.changePassphrase(key.fingerprint, 'wrong passphrase', newPassphrase), /incorrect/);
|
||||
await assert.rejects(keys.changePassphrase(key.fingerprint, passphrase, 'short'), /at least 8/);
|
||||
assert.deepEqual((await storage.load()).keys[0], original);
|
||||
|
||||
assert.deepEqual(await keys.changePassphrase(key.fingerprint, passphrase, newPassphrase), { changed: true });
|
||||
const changed = (await storage.load()).keys[0];
|
||||
assert.equal(changed.fingerprint, original.fingerprint);
|
||||
assert.equal(changed.publicKey, original.publicKey);
|
||||
assert.equal(changed.name, original.name);
|
||||
assert.equal(changed.email, original.email);
|
||||
assert.equal(changed.accountId, original.accountId);
|
||||
assert.equal(changed.createdAt, original.createdAt);
|
||||
assert.equal(changed.algorithm, original.algorithm);
|
||||
assert.equal(changed.revocationCertificate, original.revocationCertificate);
|
||||
assert.notEqual(changed.privateKey, original.privateKey);
|
||||
await assert.rejects(keys.exportPrivate(key.fingerprint, passphrase), /incorrect/);
|
||||
assert.equal(await keys.exportPrivate(key.fingerprint, newPassphrase), changed.privateKey);
|
||||
assert.equal(await keys.exportRevocation(key.fingerprint, newPassphrase), revocationCertificate);
|
||||
});
|
||||
|
||||
test('does not mutate the loaded key if saving a changed passphrase fails', async () => {
|
||||
let data = null;
|
||||
let failSave = false;
|
||||
const storage = {
|
||||
load: async () => data,
|
||||
save: async value => {
|
||||
if (failSave) throw new Error('Storage unavailable.');
|
||||
data = JSON.parse(JSON.stringify(value));
|
||||
}
|
||||
};
|
||||
const keys = createKeyStore(storage);
|
||||
const { key } = await keys.generate({ name: 'Ada', email: 'ada@example.com', passphrase });
|
||||
const original = JSON.parse(JSON.stringify(data.keys[0]));
|
||||
failSave = true;
|
||||
|
||||
await assert.rejects(keys.changePassphrase(key.fingerprint, passphrase, 'new passphrase securely'), /Storage unavailable/);
|
||||
assert.deepEqual(data.keys[0], original);
|
||||
});
|
||||
|
||||
test('imports an unprotected key by encrypting it with the given passphrase', async () => {
|
||||
const { privateKey } = await openpgp.generateKey({ type: 'curve25519', userIDs: [{ email: 'bob@example.com' }], format: 'armored' });
|
||||
const keys = createKeyStore(memoryStore());
|
||||
|
||||
Reference in New Issue
Block a user