Add OpenPGP key management foundation (encryption phase 1) and encryption plan, v0.2.4
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -193,6 +193,27 @@ async function reconcileIdleWatchers() {
|
||||
for (const account of desiredAccounts) startIdleWatcher(account);
|
||||
}
|
||||
|
||||
const { createKeyStore } = require('./pgp-keystore.cjs');
|
||||
|
||||
const pgpKeysPath = () => path.join(app.getPath('userData'), 'pgp-keys.enc');
|
||||
const pgpKeys = createKeyStore({
|
||||
async load() {
|
||||
try {
|
||||
const { result } = await safeStorage.decryptStringAsync(await fs.readFile(pgpKeysPath()));
|
||||
return JSON.parse(result);
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') return { keys: [] };
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
async save(data) {
|
||||
if (!await safeStorage.isAsyncEncryptionAvailable()) throw new Error('Secure key storage is unavailable.');
|
||||
await fs.mkdir(path.dirname(pgpKeysPath()), { recursive: true });
|
||||
await fs.writeFile(`${pgpKeysPath()}.tmp`, await safeStorage.encryptStringAsync(JSON.stringify(data)), { mode: 0o600 });
|
||||
await fs.rename(`${pgpKeysPath()}.tmp`, pgpKeysPath());
|
||||
}
|
||||
});
|
||||
|
||||
async function readDavStore() {
|
||||
try {
|
||||
const encrypted = await fs.readFile(davCredentialsPath());
|
||||
@@ -1206,6 +1227,49 @@ ipcMain.handle('mail:preview-attachment', async (_event, input) => {
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('pgp:list', () => pgpKeys.list());
|
||||
ipcMain.handle('pgp:generate', (_event, input) => pgpKeys.generate({
|
||||
name: input?.name, email: input?.email, passphrase: input?.passphrase, accountId: input?.accountId || null
|
||||
}));
|
||||
ipcMain.handle('pgp:import', async (event, input) => {
|
||||
let armoredKey = input?.armoredKey;
|
||||
if (!armoredKey) {
|
||||
const result = await dialog.showOpenDialog(BrowserWindow.fromWebContents(event.sender), {
|
||||
properties: ['openFile'], filters: [{ name: 'OpenPGP keys', extensions: ['asc', 'gpg', 'pgp', 'key', 'txt'] }]
|
||||
});
|
||||
if (result.canceled || !result.filePaths[0]) return { canceled: true };
|
||||
const stat = await fs.stat(result.filePaths[0]);
|
||||
if (stat.size > 200000) throw new Error('That key file is too large.');
|
||||
armoredKey = await fs.readFile(result.filePaths[0], 'utf8');
|
||||
}
|
||||
return { key: await pgpKeys.import({ armoredKey, passphrase: input?.passphrase, accountId: input?.accountId || null }) };
|
||||
});
|
||||
|
||||
async function saveKeyFile(event, defaultName, content) {
|
||||
const result = await dialog.showSaveDialog(BrowserWindow.fromWebContents(event.sender), { defaultPath: defaultName });
|
||||
if (result.canceled || !result.filePath) return { canceled: true };
|
||||
await fs.writeFile(result.filePath, content, { mode: 0o600 });
|
||||
return { saved: true };
|
||||
}
|
||||
const pgpFingerprint = value => {
|
||||
const fingerprint = String(value || '').toUpperCase();
|
||||
if (!/^[0-9A-F]{40,64}$/.test(fingerprint)) throw new Error('That key could not be identified.');
|
||||
return fingerprint;
|
||||
};
|
||||
ipcMain.handle('pgp:export-public', async (event, fingerprint) => {
|
||||
const id = pgpFingerprint(fingerprint);
|
||||
return saveKeyFile(event, `ep-mail-public-${id.slice(-16)}.asc`, await pgpKeys.exportPublic(id));
|
||||
});
|
||||
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: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));
|
||||
});
|
||||
ipcMain.handle('pgp:delete', (_event, fingerprint, passphrase) => pgpKeys.remove(pgpFingerprint(fingerprint), passphrase));
|
||||
|
||||
ipcMain.handle('mail:open-external', async (event, rawUrl) => {
|
||||
let target;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user