diff --git a/ENCRYPTION_PLAN.md b/ENCRYPTION_PLAN.md
index e5c4f05..4f19114 100644
--- a/ENCRYPTION_PLAN.md
+++ b/ENCRYPTION_PLAN.md
@@ -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.
diff --git a/TODO.md b/TODO.md
index 4fcdd2c..08ad6de 100644
--- a/TODO.md
+++ b/TODO.md
@@ -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.
diff --git a/desktop/main.cjs b/desktop/main.cjs
index b903f92..f0b54bb 100644
--- a/desktop/main.cjs
+++ b/desktop/main.cjs
@@ -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));
diff --git a/desktop/pgp-keystore.cjs b/desktop/pgp-keystore.cjs
index d748276..ce39dd1 100644
--- a/desktop/pgp-keystore.cjs
+++ b/desktop/pgp-keystore.cjs
@@ -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.');
diff --git a/desktop/preload.cjs b/desktop/preload.cjs
index d51ddf6..522bc22 100644
--- a/desktop/preload.cjs
+++ b/desktop/preload.cjs
@@ -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),
diff --git a/pgp-ui.js b/pgp-ui.js
index da93cff..443d464 100644
--- a/pgp-ui.js
+++ b/pgp-ui.js
@@ -7,6 +7,11 @@ function openEncryptionKeys() {
root.innerHTML = `
Encryption keys
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.
Loading keys…
+
Change a key passphrase
+
+
+
+
Select Change passphrase on a key below. Its public key and revocation certificate will not change.
Contact public keys
Loading contacts…
@@ -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 };
diff --git a/tests/pgp-keystore.test.cjs b/tests/pgp-keystore.test.cjs
index d97cdf6..07d82fe 100644
--- a/tests/pgp-keystore.test.cjs
+++ b/tests/pgp-keystore.test.cjs
@@ -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());