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:
2026-10-10 20:44:44 +03:00
co-authored by Copilot
parent f3e68b10bd
commit acba5d641e
7 changed files with 116 additions and 2 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ Decision: OpenPGP only for v1; S/MIME may follow later.
- Android: wrap with a hardware-backed Android Keystore key; optional biometric unlock (Phase 4).
- The passphrase is cached only in memory for a configurable time, then wiped (Phase 4). It is never written to disk.
- Export as ASCII-armored files; a revocation certificate is generated when a key is created and can be exported after unlocking.
- Deleting or exporting a private key requires the passphrase.
- Deleting or exporting a private key requires the passphrase. Desktop users can change a key's passphrase by verifying the current one and entering the new one twice; the public key and revocation certificate are preserved.
**Public keys**
- Per-account contact keyring: address, fingerprint, trust level, source.
+1 -1
View File
@@ -70,4 +70,4 @@
- [x] Add desktop public key server support for finding and publishing OpenPGP keys: keys.openpgp.org (VKS), keys.mailvelope.com (HKP over HTTPS), and user-defined HTTPS HKP/VKS servers. Servers are disabled until selected; lookup checks all enabled servers, and publishing requires one explicitly selected destination. Discovered keys remain unverified until fingerprint verification.
- [ ] Add the ability to attach files to outgoing mails.
- [x] Fix security review findings: confirm key replacement in the main process, enforce the plaintext-send check in the main process, and handle the Linux safeStorage basic_text backend. See SECURITY_REVIEW.md.
- [ ] Add the ability to change the passphrase of an encryption key.
- [x] Add the ability to change the passphrase of an encryption key on desktop.
+6
View File
@@ -1319,6 +1319,12 @@ ipcMain.handle('pgp:export-private', async (event, fingerprint, passphrase) => {
const id = pgpFingerprint(fingerprint);
return saveKeyFile(event, `ep-mail-private-${id.slice(-16)}.asc`, await pgpKeys.exportPrivate(id, passphrase));
});
ipcMain.handle('pgp:change-passphrase', async (_event, input) => {
const id = pgpFingerprint(input?.fingerprint);
const result = await pgpKeys.changePassphrase(id, input?.currentPassphrase, input?.newPassphrase);
pgpCache.clear(id);
return result;
});
ipcMain.handle('pgp:export-revocation', async (event, fingerprint, passphrase) => {
const id = pgpFingerprint(fingerprint);
return saveKeyFile(event, `ep-mail-revocation-${id.slice(-16)}.asc`, await pgpKeys.exportRevocation(id, passphrase));
+18
View File
@@ -305,6 +305,24 @@ function createKeyStore(storage) {
return entry.privateKey;
},
async changePassphrase(fingerprint, currentPassphrase, newPassphrase) {
requirePassphrase(newPassphrase);
const data = await read();
const keyIndex = data.keys.findIndex(key => key.fingerprint === fingerprint);
if (keyIndex < 0) throw new Error('That key was not found.');
const entry = data.keys[keyIndex];
const privateKey = await unlock(entry.privateKey, currentPassphrase);
const protectedKey = await openpgp.encryptKey({ privateKey, passphrase: newPassphrase });
const updated = {
...data,
keys: data.keys.map((key, index) => index === keyIndex
? { ...key, privateKey: protectedKey.armor() }
: key)
};
await storage.save(updated);
return { changed: true };
},
async exportRevocation(fingerprint, passphrase) {
const entry = (await read()).keys.find(key => key.fingerprint === fingerprint);
if (!entry) throw new Error('That key was not found.');
+1
View File
@@ -29,6 +29,7 @@ contextBridge.exposeInMainWorld('daybreakMail', {
importKey: details => ipcRenderer.invoke('pgp:import', details),
exportPublic: fingerprint => ipcRenderer.invoke('pgp:export-public', fingerprint),
exportPrivate: (fingerprint, passphrase) => ipcRenderer.invoke('pgp:export-private', fingerprint, passphrase),
changePassphrase: details => ipcRenderer.invoke('pgp:change-passphrase', details),
exportRevocation: (fingerprint, passphrase) => ipcRenderer.invoke('pgp:export-revocation', fingerprint, passphrase),
remove: (fingerprint, passphrase) => ipcRenderer.invoke('pgp:delete', fingerprint, passphrase),
decryptMessage: details => ipcRenderer.invoke('pgp:decrypt-message', details),
+43
View File
@@ -7,6 +7,11 @@ function openEncryptionKeys() {
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal attachments-modal"><div class="modal-head"><span>Encryption keys</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields">
<p class="account-note">OpenPGP keys are protected by a passphrase and stored encrypted by your operating system. Back up your private key and revocation certificate: without them, encrypted mail cannot be recovered. Contact keys below are used to encrypt mail to your correspondents and to check signatures.</p>
<div id="pgp-key-list" class="attachment-list">Loading keys…</div>
<h3 class="pgp-heading">Change a key passphrase</h3>
<label class="compose-line"><span>Current passphrase</span><input id="pgp-change-current-passphrase" type="password" autocomplete="current-password"></label>
<label class="compose-line"><span>New passphrase</span><input id="pgp-new-passphrase" type="password" autocomplete="new-password" placeholder="At least 8 characters"></label>
<label class="compose-line"><span>Confirm new passphrase</span><input id="pgp-confirm-passphrase" type="password" autocomplete="new-password" placeholder="Enter the new passphrase again"></label>
<p class="account-note">Select Change passphrase on a key below. Its public key and revocation certificate will not change.</p>
<h3 class="pgp-heading">Contact public keys</h3>
<div id="pgp-contact-list" class="attachment-list">Loading contacts…</div>
<div class="pgp-actions"><button class="discard" id="pgp-add-contact">Import public key…</button></div>
@@ -77,6 +82,44 @@ function openEncryptionKeys() {
['Export public', () => window.daybreakMail.pgp.exportPublic(key.fingerprint), false],
['Export private', () => window.daybreakMail.pgp.exportPrivate(key.fingerprint, needPassphrase()), true]
];
const changePassphrase = document.createElement('button');
changePassphrase.className = 'discard';
changePassphrase.textContent = 'Change passphrase…';
changePassphrase.onclick = async () => {
const currentPassphrase = $('#pgp-change-current-passphrase').value;
if (!currentPassphrase) {
status('Enter the current passphrase for this key.');
$('#pgp-change-current-passphrase').focus();
return;
}
const newPassphrase = $('#pgp-new-passphrase').value;
if (newPassphrase.length < 8) {
status('Use a new passphrase of at least 8 characters.');
$('#pgp-new-passphrase').focus();
return;
}
if (newPassphrase !== $('#pgp-confirm-passphrase').value) {
status('The new passphrases do not match.');
$('#pgp-confirm-passphrase').focus();
return;
}
changePassphrase.disabled = true;
try {
await window.daybreakMail.pgp.changePassphrase({
fingerprint: key.fingerprint,
currentPassphrase,
newPassphrase
});
$('#pgp-change-current-passphrase').value = '';
status(`Passphrase changed for ${key.email || key.fingerprint}.`);
} catch (error) { status(error.message || 'Could not change the key passphrase.'); }
finally {
$('#pgp-new-passphrase').value = '';
$('#pgp-confirm-passphrase').value = '';
changePassphrase.disabled = false;
}
};
row.append(changePassphrase);
if (key.hasRevocationCertificate) actions.push(['Revocation cert.', () => window.daybreakMail.pgp.exportRevocation(key.fingerprint, needPassphrase()), true]);
actions.push(['Delete', async () => {
if (!confirm('Delete this key? Encrypted mail that needs it can no longer be read unless you have a backup.')) return { canceled: true };
+46
View File
@@ -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());