Android security implementation Phase 0-3 finalization part 1
This commit is contained in:
+33
-31
@@ -1,15 +1,17 @@
|
||||
import { Capacitor, registerPlugin } from '@capacitor/core';
|
||||
import { createAndroidPgp } from './android-pgp.js';
|
||||
import { Capacitor, registerPlugin } from '@capacitor/core';
|
||||
import { createAndroidPgp } from './android-pgp.js';
|
||||
|
||||
const EpMail = registerPlugin('EpMail');
|
||||
|
||||
if (Capacitor.isNativePlatform() && Capacitor.getPlatform() === 'android') {
|
||||
const daybreakMail = {
|
||||
if (Capacitor.isNativePlatform() && Capacitor.getPlatform() === 'android') {
|
||||
const daybreakMail = {
|
||||
getAppVersion: () => EpMail.getAppVersion().then(result => result.version),
|
||||
getNotificationPrivacy: () => EpMail.getNotificationPrivacy(),
|
||||
setNotificationDetails: enabled => EpMail.setNotificationDetails({ enabled }),
|
||||
getAppLockSettings: () => EpMail.getAppLockSettings(),
|
||||
setAppLockEnabled: enabled => EpMail.setAppLockEnabled({ enabled }),
|
||||
getNotificationPrivacy: () => EpMail.getNotificationPrivacy(),
|
||||
setNotificationDetails: enabled => EpMail.setNotificationDetails({ enabled }),
|
||||
getAppLockSettings: () => EpMail.getAppLockSettings(),
|
||||
setAppLockEnabled: enabled => EpMail.setAppLockEnabled({ enabled }),
|
||||
setAppLockDelay: seconds => EpMail.setAppLockDelay({ seconds }),
|
||||
deleteAllLocalData: () => EpMail.deleteAllLocalData(),
|
||||
getAccount: () => EpMail.getAccount(),
|
||||
listAccounts: () => EpMail.listAccounts(),
|
||||
selectAccount: id => EpMail.selectAccount({ id }),
|
||||
@@ -30,24 +32,24 @@ if (Capacitor.isNativePlatform() && Capacitor.getPlatform() === 'android') {
|
||||
previewAttachment: details => EpMail.previewAttachment(details),
|
||||
saveAttachment: details => EpMail.saveAttachment(details),
|
||||
openExternal: url => EpMail.openExternal({ url }),
|
||||
send: async message => {
|
||||
if (!message?.pgp || (!message.pgp.encrypt && !message.pgp.sign)) {
|
||||
if (message?.pgp?.plaintextFallback) {
|
||||
const { pgp: _options, ...plainMessage } = message;
|
||||
return EpMail.send({ ...plainMessage, pgpRequested: true });
|
||||
}
|
||||
return EpMail.send(message);
|
||||
}
|
||||
try {
|
||||
const { rawBase64, to, from } = await daybreakMail.pgp.buildOutgoing(message);
|
||||
const { pgp: _options, text: _text, attachments: _attachments, ...headers } = message;
|
||||
return await EpMail.send({ ...headers, rawBase64, envelope: { from, to }, text: '', attachments: [] });
|
||||
} catch (error) {
|
||||
return { failed: true, code: error.code || 'PGP_ERROR', message: error.message || 'Could not encrypt this message.' };
|
||||
}
|
||||
},
|
||||
pgpGetMessageSource: details => EpMail.pgpGetMessageSource(details),
|
||||
savePgpAttachment: details => EpMail.savePgpAttachment(details),
|
||||
send: async message => {
|
||||
if (!message?.pgp || (!message.pgp.encrypt && !message.pgp.sign)) {
|
||||
if (message?.pgp?.plaintextFallback) {
|
||||
const { pgp: _options, ...plainMessage } = message;
|
||||
return EpMail.send({ ...plainMessage, pgpRequested: true });
|
||||
}
|
||||
return EpMail.send(message);
|
||||
}
|
||||
try {
|
||||
const { rawBase64, to, from } = await daybreakMail.pgp.buildOutgoing(message);
|
||||
const { pgp: _options, text: _text, attachments: _attachments, ...headers } = message;
|
||||
return await EpMail.send({ ...headers, rawBase64, envelope: { from, to }, text: '', attachments: [] });
|
||||
} catch (error) {
|
||||
return { failed: true, code: error.code || 'PGP_ERROR', message: error.message || 'Could not encrypt this message.' };
|
||||
}
|
||||
},
|
||||
pgpGetMessageSource: details => EpMail.pgpGetMessageSource(details),
|
||||
savePgpAttachment: details => EpMail.savePgpAttachment(details),
|
||||
disconnect: id => EpMail.removeAccount({ id }),
|
||||
davList: () => EpMail.davList(),
|
||||
davConnect: config => EpMail.davConnect(config),
|
||||
@@ -57,8 +59,8 @@ if (Capacitor.isNativePlatform() && Capacitor.getPlatform() === 'android') {
|
||||
davSaveEvent: event => EpMail.davSaveEvent(event),
|
||||
davDeleteEvent: remote => EpMail.davDeleteEvent(remote),
|
||||
davSaveContact: contact => EpMail.davSaveContact(contact),
|
||||
davDeleteContact: remote => EpMail.davDeleteContact(remote)
|
||||
};
|
||||
daybreakMail.pgp = createAndroidPgp({ EpMail, getAccount: () => EpMail.getAccount() });
|
||||
window.daybreakMail = daybreakMail;
|
||||
}
|
||||
davDeleteContact: remote => EpMail.davDeleteContact(remote)
|
||||
};
|
||||
daybreakMail.pgp = createAndroidPgp({ EpMail, getAccount: () => EpMail.getAccount() });
|
||||
window.daybreakMail = daybreakMail;
|
||||
}
|
||||
|
||||
+36
-3
@@ -335,16 +335,26 @@ export function createAndroidPgp({ EpMail, getAccount }) {
|
||||
async function unlock(fingerprint, passphrase) {
|
||||
const entry = (await store()).keys.find(item => item.fingerprint === fingerprint);
|
||||
if (!entry) throw new Error('That key was not found.');
|
||||
const secret = passphrase || await biometricPassphrase(fingerprint);
|
||||
if (!secret) throw Object.assign(new Error(`Enter the passphrase for ${entry.email || 'your key'}.`), { code: 'PASSPHRASE_REQUIRED' });
|
||||
return unlockWithPassphrase(entry, secret);
|
||||
}
|
||||
async function unlockWithPassphrase(entry, passphrase) {
|
||||
try { return await openpgp.decryptKey({ privateKey: await openpgp.readPrivateKey({ armoredKey: entry.privateKey }), passphrase }); }
|
||||
catch { throw new Error('The passphrase is incorrect.'); }
|
||||
}
|
||||
async function biometricPassphrase(fingerprint) {
|
||||
if (typeof EpMail.getPgpBiometricPassphrase !== 'function') return null;
|
||||
const result = await EpMail.getPgpBiometricPassphrase({ fingerprint });
|
||||
return result?.enabled && typeof result.passphrase === 'string' ? result.passphrase : null;
|
||||
}
|
||||
async function signerFor(account, passphrase) {
|
||||
const data = await store();
|
||||
const entry = data.keys.find(item => item.email.toLowerCase() === String(account.email).toLowerCase());
|
||||
if (!entry) throw Object.assign(new Error('Create or import an encryption key for this address first.'), { code: 'NO_SIGNING_KEY' });
|
||||
const secret = passphrase || cached(entry.fingerprint);
|
||||
const secret = passphrase || cached(entry.fingerprint) || await biometricPassphrase(entry.fingerprint);
|
||||
if (!secret) throw Object.assign(new Error(`Enter the passphrase for ${entry.email} to sign this message.`), { code: 'PASSPHRASE_REQUIRED', fingerprint: entry.fingerprint, email: entry.email });
|
||||
try { const key = await unlock(entry.fingerprint, secret); cache(entry.fingerprint, secret); return { key, entry }; }
|
||||
try { const key = await unlockWithPassphrase(entry, secret); cache(entry.fingerprint, secret); return { key, entry }; }
|
||||
catch { clear(entry.fingerprint); throw Object.assign(new Error('The passphrase is incorrect.'), { code: 'BAD_PASSPHRASE', fingerprint: entry.fingerprint, email: entry.email }); }
|
||||
}
|
||||
async function buildOutgoing(message) {
|
||||
@@ -425,6 +435,7 @@ export function createAndroidPgp({ EpMail, getAccount }) {
|
||||
selected = candidates.find(entry => cached(entry.fingerprint)) || selected;
|
||||
secret = cached(selected.fingerprint);
|
||||
}
|
||||
if (!secret) secret = await biometricPassphrase(selected.fingerprint);
|
||||
if (!secret) return { failed: true, code: 'PASSPHRASE_REQUIRED', fingerprint: selected.fingerprint, email: selected.email, message: 'Enter your key passphrase to read this message.' };
|
||||
let privateKey;
|
||||
try { privateKey = await unlock(selected.fingerprint, secret); }
|
||||
@@ -529,6 +540,23 @@ export function createAndroidPgp({ EpMail, getAccount }) {
|
||||
entry.privateKey = protectedKey.armor(); await persist(data); clear(fingerprint); return { changed: true };
|
||||
},
|
||||
async remove(fingerprint, passphrase) { const data = await store(); const entry = data.keys.find(item => item.fingerprint === fingerprint); if (!entry) throw new Error('That key was not found.'); await unlock(fingerprint, passphrase); data.keys = data.keys.filter(item => item.fingerprint !== fingerprint); await persist(data); clear(fingerprint); return { removed: true }; },
|
||||
async enableBiometricUnlock({ fingerprint, passphrase }) {
|
||||
if (typeof EpMail.setPgpBiometricUnlock !== 'function') throw new Error('Biometric key unlock is not available on this device.');
|
||||
const entry = (await store()).keys.find(item => item.fingerprint === fingerprint);
|
||||
if (!entry) throw new Error('That key was not found.');
|
||||
if (!passphrase) throw new Error('Enter the key passphrase first.');
|
||||
await unlockWithPassphrase(entry, passphrase);
|
||||
return EpMail.setPgpBiometricUnlock({ fingerprint, passphrase });
|
||||
},
|
||||
async biometricUnlockEnabled(fingerprint) {
|
||||
if (typeof EpMail.pgpBiometricUnlockEnabled !== 'function') return false;
|
||||
return Boolean((await EpMail.pgpBiometricUnlockEnabled({ fingerprint }))?.enabled);
|
||||
},
|
||||
async disableBiometricUnlock(fingerprint) {
|
||||
if (typeof EpMail.removePgpBiometricUnlock !== 'function') throw new Error('Biometric key unlock is not available on this device.');
|
||||
clear(fingerprint);
|
||||
return EpMail.removePgpBiometricUnlock({ fingerprint });
|
||||
},
|
||||
async contacts() { return (await store()).contacts.map(({ fingerprint, name, emails, trust, source, addedAt }) => ({ fingerprint, name, emails, trust, source, addedAt })); },
|
||||
async addContact(input = {}) {
|
||||
const armoredKey = input.armoredKey || prompt('Paste an ASCII-armored OpenPGP public key (up to 200 KB):') || '';
|
||||
@@ -547,8 +575,13 @@ export function createAndroidPgp({ EpMail, getAccount }) {
|
||||
const { keys, missing } = await resolveRecipients(addresses);
|
||||
const account = await getAccount();
|
||||
const entry = sign ? (await store()).keys.find(item => item.email.toLowerCase() === String(account?.email || '').toLowerCase()) : null;
|
||||
let biometricUnlockEnabled = false;
|
||||
if (entry && typeof EpMail.pgpBiometricUnlockEnabled === 'function') {
|
||||
biometricUnlockEnabled = Boolean((await EpMail.pgpBiometricUnlockEnabled({ fingerprint: entry.fingerprint }))?.enabled);
|
||||
}
|
||||
return { recipients: keys.map(item => ({ address: item.address, fingerprint: item.fingerprint, trust: item.trust })), missing,
|
||||
signer: entry ? { fingerprint: entry.fingerprint, email: entry.email, name: entry.name, unlocked: Boolean(cached(entry.fingerprint)) } : null };
|
||||
signer: entry ? { fingerprint: entry.fingerprint, email: entry.email, name: entry.name,
|
||||
unlocked: Boolean(cached(entry.fingerprint)), biometricUnlockEnabled } : null };
|
||||
},
|
||||
decryptMessage,
|
||||
verifyMessage,
|
||||
|
||||
Reference in New Issue
Block a user