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:
@@ -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));
|
||||
|
||||
@@ -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.');
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user