Android security implementation Phase 2 finalization part 1
This commit is contained in:
@@ -4,6 +4,7 @@ const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const esbuild = require('esbuild');
|
||||
const { webcrypto } = require('node:crypto');
|
||||
const openpgp = require('openpgp');
|
||||
|
||||
global.crypto ||= webcrypto;
|
||||
global.document ||= { addEventListener() {} };
|
||||
@@ -37,15 +38,20 @@ function makeEndpoint(account) {
|
||||
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: '' });
|
||||
const thirdParty = makeEndpoint({ email: 'charlie@example.test', displayName: 'Charlie', signature: '' });
|
||||
const alice = await sender.api.generate({ name: 'Alice', email: 'alice@example.test', passphrase: 'alice test passphrase' });
|
||||
const bob = await recipient.api.generate({ name: 'Bob', email: 'bob@example.test', passphrase: 'bob test passphrase' });
|
||||
const charlie = await thirdParty.api.generate({ name: 'Charlie', email: 'charlie@example.test', passphrase: 'charlie test passphrase' });
|
||||
await sender.api.addContact({ armoredKey: await recipient.api.exportPublic(bob.key.fingerprint) });
|
||||
await recipient.api.addContact({ armoredKey: await sender.api.exportPublic(alice.key.fingerprint) });
|
||||
|
||||
const built = await sender.api.buildOutgoing({
|
||||
to: 'Bob <bob@example.test>', cc: '', subject: 'Secret note', text: 'hello from Android',
|
||||
messageId: '<test-message@example.test>', pgp: { encrypt: true, sign: true, passphrase: 'alice test passphrase' },
|
||||
attachments: [{ filename: 'note.txt', contentType: 'text/plain', contentBase64: Buffer.from('private attachment').toString('base64') }]
|
||||
attachments: [
|
||||
{ filename: 'note.txt', contentType: 'text/plain', contentBase64: Buffer.from('private attachment').toString('base64') },
|
||||
{ filename: 'charlie.asc', contentType: 'application/pgp-keys', contentBase64: Buffer.from(await thirdParty.api.exportPublic(charlie.key.fingerprint)).toString('base64') }
|
||||
]
|
||||
});
|
||||
assert.ok(built.rawBase64);
|
||||
const raw = Buffer.from(built.rawBase64, 'base64').toString('utf8');
|
||||
@@ -62,6 +68,8 @@ test('Android OpenPGP encrypts MIME attachments and decrypts with a recipient ke
|
||||
assert.deepEqual(opened.body, ['hello from Android']);
|
||||
assert.equal(opened.attachments[0].filename, 'note.txt');
|
||||
assert.equal(Buffer.from(opened.attachments[0].contentBase64, 'base64').toString(), 'private attachment');
|
||||
assert.equal(opened.attachedKeys[0].fingerprint, charlie.key.fingerprint);
|
||||
assert.deepEqual(opened.attachedKeys[0].emails, ['charlie@example.test']);
|
||||
assert.equal(opened.signatures[0].status, 'valid');
|
||||
await recipient.api.setSettings({ cacheMinutes: 0 });
|
||||
assert.equal((await recipient.api.getSettings()).cacheMinutes, 0);
|
||||
@@ -75,3 +83,87 @@ test('Android OpenPGP refuses encryption when any recipient key is missing', asy
|
||||
to: 'nobody@example.test', text: 'must not escape as plaintext', pgp: { encrypt: true }, attachments: []
|
||||
}), error => error.code === 'NO_RECIPIENT_KEY');
|
||||
});
|
||||
|
||||
test('Android verifies PGP/MIME signed-only and clear-signed mail', async () => {
|
||||
const sender = makeEndpoint({ email: 'alice@example.test', displayName: 'Alice', signature: '' });
|
||||
const recipient = makeEndpoint({ email: 'bob@example.test', displayName: 'Bob', signature: '' });
|
||||
const created = await sender.api.generate({ name: 'Alice', email: 'alice@example.test', passphrase: 'alice test passphrase' });
|
||||
await recipient.api.addContact({ armoredKey: await sender.api.exportPublic(created.key.fingerprint) });
|
||||
|
||||
const signedMime = await sender.api.buildOutgoing({
|
||||
to: 'Bob <bob@example.test>', cc: '', subject: 'Signed note', text: 'signed MIME body',
|
||||
pgp: { encrypt: false, sign: true, passphrase: 'alice test passphrase' }, attachments: []
|
||||
});
|
||||
recipient.setMessage({ rawBase64: signedMime.rawBase64 });
|
||||
const mimeResult = await recipient.api.verifyMessage({ folder: 'Inbox', uid: 8 });
|
||||
assert.equal(mimeResult.signatures[0].status, 'valid');
|
||||
assert.equal(mimeResult.signatures[0].matchesSender, true);
|
||||
|
||||
const protectedPrivateKey = await sender.api.exportPrivate(created.key.fingerprint, 'alice test passphrase');
|
||||
const signingKey = await openpgp.decryptKey({
|
||||
privateKey: await openpgp.readPrivateKey({ armoredKey: protectedPrivateKey }),
|
||||
passphrase: 'alice test passphrase'
|
||||
});
|
||||
const cleartext = await openpgp.createCleartextMessage({ text: 'clear-signed body' });
|
||||
const clearSigned = await openpgp.sign({ message: cleartext, signingKeys: signingKey });
|
||||
const raw = [
|
||||
'From: Alice <alice@example.test>',
|
||||
'To: Bob <bob@example.test>',
|
||||
'Subject: Clear-signed note',
|
||||
'Content-Type: text/plain; charset=utf-8',
|
||||
'',
|
||||
clearSigned,
|
||||
''
|
||||
].join('\r\n');
|
||||
recipient.setMessage({ rawBase64: Buffer.from(raw).toString('base64') });
|
||||
const clearResult = await recipient.api.verifyMessage({ folder: 'Inbox', uid: 9 });
|
||||
assert.equal(clearResult.signatures[0].status, 'valid');
|
||||
assert.equal(clearResult.signatures[0].matchesSender, true);
|
||||
});
|
||||
|
||||
test('Android marks a damaged PGP/MIME signature invalid', async () => {
|
||||
const sender = makeEndpoint({ email: 'alice@example.test', displayName: 'Alice', signature: '' });
|
||||
const recipient = makeEndpoint({ email: 'bob@example.test', displayName: 'Bob', signature: '' });
|
||||
const created = await sender.api.generate({ name: 'Alice', email: 'alice@example.test', passphrase: 'alice test passphrase' });
|
||||
await recipient.api.addContact({ armoredKey: await sender.api.exportPublic(created.key.fingerprint) });
|
||||
const signed = await sender.api.buildOutgoing({
|
||||
to: 'bob@example.test', subject: 'Signed', text: 'change me',
|
||||
pgp: { sign: true, passphrase: 'alice test passphrase' }, attachments: []
|
||||
});
|
||||
const raw = Buffer.from(signed.rawBase64, 'base64').toString('utf8');
|
||||
const armor = /-----BEGIN PGP SIGNATURE-----[\s\S]+?-----END PGP SIGNATURE-----/.exec(raw)?.[0];
|
||||
const signatureLine = armor?.split(/\r?\n/).find(line => /^[A-Za-z0-9+/]{20,}$/.test(line));
|
||||
assert.ok(signatureLine, 'signed MIME output should contain an armored signature');
|
||||
const tampered = raw.replace(signatureLine, `${signatureLine[0] === 'A' ? 'B' : 'A'}${signatureLine.slice(1)}`);
|
||||
recipient.setMessage({ rawBase64: Buffer.from(tampered).toString('base64') });
|
||||
const result = await recipient.api.verifyMessage({ folder: 'Inbox', uid: 10 });
|
||||
assert.equal(result.signatures[0].status, 'invalid');
|
||||
});
|
||||
|
||||
test('Android rejects tampered, malformed, and oversized encrypted input', async () => {
|
||||
const sender = makeEndpoint({ email: 'alice@example.test', displayName: 'Alice', signature: '' });
|
||||
const recipient = makeEndpoint({ email: 'bob@example.test', displayName: 'Bob', signature: '' });
|
||||
const bob = await recipient.api.generate({ name: 'Bob', email: 'bob@example.test', passphrase: 'bob test passphrase' });
|
||||
await sender.api.addContact({ armoredKey: await recipient.api.exportPublic(bob.key.fingerprint) });
|
||||
const built = await sender.api.buildOutgoing({
|
||||
to: 'bob@example.test', subject: 'Encrypted test', text: 'tamper test',
|
||||
pgp: { encrypt: true }, attachments: []
|
||||
});
|
||||
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);
|
||||
const lines = armor.split(/\r?\n/);
|
||||
const armorLineIndex = lines.findIndex(line => /^[A-Za-z0-9+/]{20,}$/.test(line));
|
||||
assert.notEqual(armorLineIndex, -1);
|
||||
lines[armorLineIndex] = `${lines[armorLineIndex][0] === 'A' ? 'B' : 'A'}${lines[armorLineIndex].slice(1)}`;
|
||||
recipient.setMessage({ rawBase64: Buffer.from(raw.replace(armor, lines.join('\r\n'))).toString('base64') });
|
||||
assert.equal((await recipient.api.decryptMessage({ uid: 20, passphrase: 'bob test passphrase' })).code, 'CORRUPT');
|
||||
|
||||
recipient.setMessage({ rawBase64: Buffer.from(
|
||||
'From: Alice <alice@example.test>\r\nContent-Type: multipart/encrypted; boundary="broken"\r\n\r\nnot a valid PGP payload'
|
||||
).toString('base64') });
|
||||
assert.equal((await recipient.api.decryptMessage({ uid: 21, passphrase: 'bob test passphrase' })).code, 'CORRUPT');
|
||||
|
||||
recipient.setMessage({ rawBase64: 'A'.repeat(35_000_000) });
|
||||
assert.equal((await recipient.api.decryptMessage({ uid: 22, passphrase: 'bob test passphrase' })).code, 'TOO_LARGE');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user