Improve Android OpenPGP, drafts and release builds
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
const vm = require('node:vm');
|
||||
const esbuild = require('esbuild');
|
||||
const { webcrypto } = require('node:crypto');
|
||||
@@ -20,6 +21,12 @@ const bundled = esbuild.buildSync({
|
||||
vm.runInThisContext(bundled);
|
||||
const { createAndroidPgp } = global.AndroidPgpBundle;
|
||||
|
||||
function saveMimeFixture(name, rawBase64) {
|
||||
if (process.env.EP_MAIL_PGP_MIME_FIXTURE_DIR) {
|
||||
fs.writeFileSync(path.join(process.env.EP_MAIL_PGP_MIME_FIXTURE_DIR, `${name}.eml`), Buffer.from(rawBase64, 'base64'));
|
||||
}
|
||||
}
|
||||
|
||||
function makeEndpoint(account, { confirm = () => true } = {}) {
|
||||
const vault = { keys: [], contacts: [], settings: {} };
|
||||
let messageSource = null;
|
||||
@@ -36,6 +43,39 @@ function makeEndpoint(account, { confirm = () => true } = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
test('Android imports an armored private key selected from a file', async () => {
|
||||
const source = makeEndpoint({ email: 'alice@example.test' });
|
||||
const target = makeEndpoint({ email: 'alice@example.test' });
|
||||
const passphrase = 'alice test passphrase';
|
||||
const generated = await source.api.generate({ name: 'Alice', email: 'alice@example.test', passphrase });
|
||||
const armoredKey = await source.api.exportPrivate(generated.key.fingerprint, passphrase);
|
||||
target.EpMail.pickPgpPrivateKey = async () => ({ armoredKey });
|
||||
|
||||
await assert.rejects(target.api.importKey({ source: 'file', passphrase: 'incorrect passphrase' }), /passphrase is incorrect/);
|
||||
assert.equal(target.vault.keys.length, 0);
|
||||
const result = await target.api.importKey({ source: 'file', passphrase, accountId: 'account-1' });
|
||||
assert.equal(result.key.fingerprint, generated.key.fingerprint);
|
||||
assert.equal(target.vault.keys[0].accountId, 'account-1');
|
||||
assert.equal((await openpgp.readPrivateKey({ armoredKey: target.vault.keys[0].privateKey })).isDecrypted(), false);
|
||||
await assert.rejects(target.api.importKey({ source: 'file', passphrase }), /already in your keyring/);
|
||||
});
|
||||
|
||||
test('Android file import handles cancellation, picker errors and invalid content', async () => {
|
||||
const target = makeEndpoint({ email: 'alice@example.test' });
|
||||
const options = { source: 'file', passphrase: 'test passphrase' };
|
||||
target.EpMail.pickPgpPrivateKey = async () => ({ canceled: true });
|
||||
assert.deepEqual(await target.api.importKey(options), { canceled: true });
|
||||
target.EpMail.pickPgpPrivateKey = async () => { throw new Error('Picker failed'); };
|
||||
await assert.rejects(target.api.importKey(options), /Picker failed/);
|
||||
target.EpMail.pickPgpPrivateKey = async () => ({ armoredKey: '' });
|
||||
await assert.rejects(target.api.importKey(options), /empty/);
|
||||
target.EpMail.pickPgpPrivateKey = async () => ({ armoredKey: 'not a private key' });
|
||||
await assert.rejects(target.api.importKey(options), /not a valid OpenPGP private key/);
|
||||
target.EpMail.pickPgpPrivateKey = async () => ({ armoredKey: 'x'.repeat(200001) });
|
||||
await assert.rejects(target.api.importKey(options), /too large/);
|
||||
assert.equal(target.vault.keys.length, 0);
|
||||
});
|
||||
|
||||
test('Android OpenPGP encrypts MIME attachments and decrypts with a recipient key', async () => {
|
||||
const sender = makeEndpoint({ email: 'alice@example.test', displayName: 'Alice', signature: '' });
|
||||
const recipient = makeEndpoint({ email: 'bob@example.test', displayName: 'Bob', signature: '' });
|
||||
@@ -55,6 +95,7 @@ test('Android OpenPGP encrypts MIME attachments and decrypts with a recipient ke
|
||||
]
|
||||
});
|
||||
assert.ok(built.rawBase64);
|
||||
saveMimeFixture('encrypted-signed', built.rawBase64);
|
||||
const raw = Buffer.from(built.rawBase64, 'base64').toString('utf8');
|
||||
assert.match(raw, /multipart\/encrypted; protocol="application\/pgp-encrypted"/i);
|
||||
assert.doesNotMatch(raw, /hello from Android|private attachment/);
|
||||
@@ -114,6 +155,7 @@ test('Android verifies PGP/MIME signed-only and clear-signed mail', async () =>
|
||||
to: 'Bob <bob@example.test>', cc: '', subject: 'Signed note', text: 'signed MIME body',
|
||||
pgp: { encrypt: false, sign: true, passphrase: 'alice test passphrase' }, attachments: []
|
||||
});
|
||||
saveMimeFixture('signed', signedMime.rawBase64);
|
||||
recipient.setMessage({ rawBase64: signedMime.rawBase64 });
|
||||
const mimeResult = await recipient.api.verifyMessage({ folder: 'Inbox', uid: 8 });
|
||||
assert.equal(mimeResult.signatures[0].status, 'valid');
|
||||
@@ -169,6 +211,7 @@ test('Android rejects tampered, malformed, and oversized encrypted input', async
|
||||
to: 'bob@example.test', subject: 'Encrypted test', text: 'tamper test',
|
||||
pgp: { encrypt: true }, attachments: []
|
||||
});
|
||||
saveMimeFixture('encrypted', built.rawBase64);
|
||||
const raw = Buffer.from(built.rawBase64, 'base64').toString('utf8');
|
||||
const armor = /-----BEGIN PGP MESSAGE-----[\s\S]+?-----END PGP MESSAGE-----/.exec(raw)?.[0];
|
||||
assert.ok(armor);
|
||||
|
||||
Reference in New Issue
Block a user