Android security implementation Phase 0-3 finalization part 1
This commit is contained in:
+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