mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
#89 Decrypt and verify with OpenPGP.js and GnuPG
This commit is contained in:
parent
85f9209176
commit
e0e490c64f
8 changed files with 354 additions and 315 deletions
|
|
@ -21,7 +21,7 @@ export function ParseMime(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
get bodyRaw() {
|
get bodyRaw() {
|
||||||
return text.slice(this.body_start, this.body_end);
|
return text.slice(this.bodyStart, this.bodyEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
get body() {
|
get body() {
|
||||||
|
|
@ -99,9 +99,10 @@ export function ParseMime(text)
|
||||||
part.headers = headers;
|
part.headers = headers;
|
||||||
|
|
||||||
// get body
|
// get body
|
||||||
part.body_start = start_pos + head.length;
|
part.bodyStart = start_pos + head.length;
|
||||||
part.body_end = start_pos + mimePart.length;
|
part.bodyEnd = start_pos + mimePart.length;
|
||||||
|
|
||||||
|
// get child parts
|
||||||
let boundary = headers['content-type'].params.boundary;
|
let boundary = headers['content-type'].params.boundary;
|
||||||
if (boundary) {
|
if (boundary) {
|
||||||
part.boundary = boundary;
|
part.boundary = boundary;
|
||||||
|
|
@ -109,16 +110,16 @@ export function ParseMime(text)
|
||||||
let regex = new RegExp('(?:^|\r?\n)--' + boundary + '(?:--)?(?:\r?\n|$)', 'g'),
|
let regex = new RegExp('(?:^|\r?\n)--' + boundary + '(?:--)?(?:\r?\n|$)', 'g'),
|
||||||
body = mimePart.slice(head.length),
|
body = mimePart.slice(head.length),
|
||||||
bodies = body.split(regex),
|
bodies = body.split(regex),
|
||||||
pos = part.body_start;
|
pos = part.bodyStart;
|
||||||
[...body.matchAll(regex)].forEach(([boundary], index) => {
|
[...body.matchAll(regex)].forEach(([boundary], index) => {
|
||||||
if (!index) {
|
if (!index) {
|
||||||
part.body_text = bodies[0];
|
// Mostly something like: "This is a multi-part message in MIME format."
|
||||||
|
part.bodyText = bodies[0];
|
||||||
}
|
}
|
||||||
pos += bodies[index].length;
|
// Not the end?
|
||||||
pos += boundary.length;
|
if ('--' != boundary.trim().slice(-2)) {
|
||||||
part.parts.push(ParsePart(bodies[1+index], pos, ((id ? id + '.' : '') + (1+index))));
|
pos += bodies[index].length + boundary.length;
|
||||||
if ('--' == boundary.trim().slice(-2)) {
|
part.parts.push(ParsePart(bodies[1+index], pos, ((id ? id + '.' : '') + (1+index))));
|
||||||
// end
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
76
dev/Mime/Utils.js
Normal file
76
dev/Mime/Utils.js
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
|
||||||
|
import { ParseMime } from 'Mime/Parser';
|
||||||
|
import { AttachmentModel } from 'Model/Attachment';
|
||||||
|
import { FileInfo } from 'Common/File';
|
||||||
|
import { BEGIN_PGP_MESSAGE } from 'Stores/User/Pgp';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string data
|
||||||
|
* @param MessageModel message
|
||||||
|
*/
|
||||||
|
export function MimeToMessage(data, message)
|
||||||
|
{
|
||||||
|
let signed;
|
||||||
|
const struct = ParseMime(data);
|
||||||
|
if (struct.headers) {
|
||||||
|
let html = struct.getByContentType('text/html');
|
||||||
|
html = html ? html.body : '';
|
||||||
|
|
||||||
|
struct.forEach(part => {
|
||||||
|
let cd = part.header('content-disposition'),
|
||||||
|
cid = part.header('content-id'),
|
||||||
|
type = part.header('content-type');
|
||||||
|
if (cid || cd) {
|
||||||
|
// if (cd && 'attachment' === cd.value) {
|
||||||
|
let attachment = new AttachmentModel;
|
||||||
|
attachment.mimeType = type.value;
|
||||||
|
attachment.fileName = type.name || (cd && cd.params.filename) || '';
|
||||||
|
attachment.fileNameExt = attachment.fileName.replace(/^.+(\.[a-z]+)$/, '$1');
|
||||||
|
attachment.fileType = FileInfo.getType('', type.value);
|
||||||
|
attachment.url = part.dataUrl;
|
||||||
|
attachment.friendlySize = FileInfo.friendlySize(part.body.length);
|
||||||
|
/*
|
||||||
|
attachment.isThumbnail = false;
|
||||||
|
attachment.contentLocation = '';
|
||||||
|
attachment.download = '';
|
||||||
|
attachment.folder = '';
|
||||||
|
attachment.uid = '';
|
||||||
|
attachment.mimeIndex = part.id;
|
||||||
|
attachment.framed = false;
|
||||||
|
*/
|
||||||
|
attachment.cid = cid ? cid.value : '';
|
||||||
|
if (cid && html) {
|
||||||
|
let cid = 'cid:' + attachment.contentId(),
|
||||||
|
found = html.includes(cid);
|
||||||
|
attachment.isInline(found);
|
||||||
|
attachment.isLinked(found);
|
||||||
|
found && (html = html
|
||||||
|
.replace('src="' + cid + '"', 'src="' + attachment.url + '"')
|
||||||
|
.replace("src='" + cid + "'", "src='" + attachment.url + "'")
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
message.attachments.push(attachment);
|
||||||
|
}
|
||||||
|
} else if ('multipart/signed' === type.value && 'application/pgp-signature' === type.params.protocol) {
|
||||||
|
signed = {
|
||||||
|
MicAlg: type.micalg,
|
||||||
|
BodyPart: part.parts[0],
|
||||||
|
SigPart: part.parts[1]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const text = struct.getByContentType('text/plain');
|
||||||
|
message.plain(text ? text.body : '');
|
||||||
|
message.html(html);
|
||||||
|
} else {
|
||||||
|
message.plain(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!signed && message.plain().includes(BEGIN_PGP_MESSAGE)) {
|
||||||
|
signed = true;
|
||||||
|
}
|
||||||
|
message.pgpSigned(signed);
|
||||||
|
|
||||||
|
// TODO: Verify instantly?
|
||||||
|
}
|
||||||
|
|
@ -459,8 +459,7 @@ export class MessageModel extends AbstractModel {
|
||||||
body.classList.toggle('plain', 1);
|
body.classList.toggle('plain', 1);
|
||||||
body.innerHTML = plainToHtml(
|
body.innerHTML = plainToHtml(
|
||||||
this.plain()
|
this.plain()
|
||||||
.replace(/-----BEGIN PGP SIGNATURE-----[\s\S]*/, '')
|
.replace(/-----BEGIN PGP (SIGNED MESSAGE-----(\r?\n[a-z][^\r\n]+)+|SIGNATURE-----[\s\S]*)/, '')
|
||||||
.replace(/-----BEGIN PGP SIGNED MESSAGE-----(\r?\n[a-z][^\r\n]+)+/i, '')
|
|
||||||
.trim()
|
.trim()
|
||||||
)
|
)
|
||||||
.replace(url, '$1<a href="$2" target="_blank">$2</a>')
|
.replace(url, '$1<a href="$2" target="_blank">$2</a>')
|
||||||
|
|
|
||||||
|
|
@ -197,10 +197,15 @@ export const GnuPGUserStore = new class {
|
||||||
async verify(message) {
|
async verify(message) {
|
||||||
let data = message.pgpSigned(); // { BodyPartId: "1", SigPartId: "2", MicAlg: "pgp-sha256" }
|
let data = message.pgpSigned(); // { BodyPartId: "1", SigPartId: "2", MicAlg: "pgp-sha256" }
|
||||||
if (data) {
|
if (data) {
|
||||||
|
data = { ...data }; // clone
|
||||||
// const sender = message.from[0].email;
|
// const sender = message.from[0].email;
|
||||||
// let mode = await this.hasPublicKeyForEmails([sender]);
|
// let mode = await this.hasPublicKeyForEmails([sender]);
|
||||||
data.Folder = message.folder;
|
data.Folder = message.folder;
|
||||||
data.Uid = message.uid;
|
data.Uid = message.uid;
|
||||||
|
if (data.BodyPart) {
|
||||||
|
data.BodyPart = data.BodyPart.raw;
|
||||||
|
data.SigPart = data.SigPart.body;
|
||||||
|
}
|
||||||
let response = await Remote.post('MessagePgpVerify', null, data);
|
let response = await Remote.post('MessagePgpVerify', null, data);
|
||||||
if (response && response.Result) {
|
if (response && response.Result) {
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -214,9 +214,14 @@ export const OpenPGPUserStore = new class {
|
||||||
data.Folder = message.folder;
|
data.Folder = message.folder;
|
||||||
data.Uid = message.uid;
|
data.Uid = message.uid;
|
||||||
data.GnuPG = 0;
|
data.GnuPG = 0;
|
||||||
let response = data.SigPartId
|
let response;
|
||||||
? await Remote.post('MessagePgpVerify', null, data)
|
if (data.SigPartId) {
|
||||||
: { Result: { text: message.plain(), signature: null } };
|
response = await Remote.post('MessagePgpVerify', null, data);
|
||||||
|
} else if (data.BodyPart) {
|
||||||
|
response = { Result: { text: data.BodyPart.raw, signature: data.SigPart.body } };
|
||||||
|
} else {
|
||||||
|
response = { Result: { text: message.plain(), signature: null } };
|
||||||
|
}
|
||||||
if (response) {
|
if (response) {
|
||||||
const signature = response.Result.signature
|
const signature = response.Result.signature
|
||||||
? await openpgp.readSignature({ armoredSignature: response.Result.signature })
|
? await openpgp.readSignature({ armoredSignature: response.Result.signature })
|
||||||
|
|
|
||||||
|
|
@ -10,223 +10,228 @@ import { staticLink } from 'Common/Links';
|
||||||
import { GnuPGUserStore } from 'Stores/User/GnuPG';
|
import { GnuPGUserStore } from 'Stores/User/GnuPG';
|
||||||
import { OpenPGPUserStore } from 'Stores/User/OpenPGP';
|
import { OpenPGPUserStore } from 'Stores/User/OpenPGP';
|
||||||
|
|
||||||
export const PgpUserStore = new class {
|
export const
|
||||||
constructor() {
|
BEGIN_PGP_MESSAGE = '-----BEGIN PGP MESSAGE-----',
|
||||||
// https://mailvelope.github.io/mailvelope/Keyring.html
|
// BEGIN_PGP_SIGNATURE = '-----BEGIN PGP SIGNATURE-----',
|
||||||
this.mailvelopeKeyring = null;
|
// BEGIN_PGP_SIGNED = '-----BEGIN PGP SIGNED MESSAGE-----',
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
PgpUserStore = new class {
|
||||||
if (SettingsCapa(Capa.OpenPGP) && window.crypto && crypto.getRandomValues) {
|
constructor() {
|
||||||
const script = createElement('script', {src:staticLink('js/min/openpgp.min.js')});
|
// https://mailvelope.github.io/mailvelope/Keyring.html
|
||||||
script.onload = () => this.loadKeyrings();
|
this.mailvelopeKeyring = null;
|
||||||
script.onerror = () => {
|
|
||||||
this.loadKeyrings();
|
|
||||||
console.error(script.src);
|
|
||||||
};
|
|
||||||
doc.head.append(script);
|
|
||||||
} else {
|
|
||||||
this.loadKeyrings();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
loadKeyrings(identifier) {
|
init() {
|
||||||
identifier = identifier || SettingsGet('Email');
|
if (SettingsCapa(Capa.OpenPGP) && window.crypto && crypto.getRandomValues) {
|
||||||
if (window.mailvelope) {
|
const script = createElement('script', {src:staticLink('js/min/openpgp.min.js')});
|
||||||
const fn = keyring => {
|
script.onload = () => this.loadKeyrings();
|
||||||
this.mailvelopeKeyring = keyring;
|
script.onerror = () => {
|
||||||
console.log('mailvelope ready');
|
this.loadKeyrings();
|
||||||
|
console.error(script.src);
|
||||||
};
|
};
|
||||||
mailvelope.getKeyring().then(fn, err => {
|
doc.head.append(script);
|
||||||
if (identifier) {
|
} else {
|
||||||
// attempt to create a new keyring for this app/user
|
this.loadKeyrings();
|
||||||
mailvelope.createKeyring(identifier).then(fn, err => console.error(err));
|
|
||||||
} else {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
addEventListener('mailvelope-disconnect', event => {
|
|
||||||
alert('Mailvelope is updated to version ' + event.detail.version + '. Reload page');
|
|
||||||
}, false);
|
|
||||||
} else {
|
|
||||||
addEventListener('mailvelope', () => this.loadKeyrings(identifier));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (OpenPGPUserStore.isSupported()) {
|
|
||||||
OpenPGPUserStore.loadKeyrings();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SettingsCapa(Capa.GnuPG)) {
|
|
||||||
GnuPGUserStore.loadKeyrings();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
isSupported() {
|
|
||||||
return !!(OpenPGPUserStore.isSupported() || GnuPGUserStore.isSupported() || window.mailvelope);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
isEncrypted(text) {
|
|
||||||
return 0 === text.trim().indexOf('-----BEGIN PGP MESSAGE-----');
|
|
||||||
}
|
|
||||||
|
|
||||||
async mailvelopeHasPublicKeyForEmails(recipients) {
|
|
||||||
const
|
|
||||||
keyring = this.mailvelopeKeyring,
|
|
||||||
mailvelope = keyring && await keyring.validKeyForAddress(recipients)
|
|
||||||
/*.then(LookupResult => Object.entries(LookupResult))*/,
|
|
||||||
entries = mailvelope && Object.entries(mailvelope);
|
|
||||||
return entries && entries.filter(value => value[1]).length === recipients.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if verifying/encrypting a message is possible with given email addresses.
|
|
||||||
* Returns the first library that can.
|
|
||||||
*/
|
|
||||||
async hasPublicKeyForEmails(recipients) {
|
|
||||||
const count = recipients.length;
|
|
||||||
if (count) {
|
|
||||||
if (GnuPGUserStore.hasPublicKeyForEmails(recipients)) {
|
|
||||||
return 'gnupg';
|
|
||||||
}
|
|
||||||
if (OpenPGPUserStore.hasPublicKeyForEmails(recipients)) {
|
|
||||||
return 'openpgp';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getMailvelopePrivateKeyFor(email/*, sign*/) {
|
loadKeyrings(identifier) {
|
||||||
let keyring = this.mailvelopeKeyring;
|
identifier = identifier || SettingsGet('Email');
|
||||||
if (keyring && await keyring.hasPrivateKey({email:email})) {
|
if (window.mailvelope) {
|
||||||
return ['mailvelope', email];
|
const fn = keyring => {
|
||||||
}
|
this.mailvelopeKeyring = keyring;
|
||||||
return false;
|
console.log('mailvelope ready');
|
||||||
}
|
};
|
||||||
|
mailvelope.getKeyring().then(fn, err => {
|
||||||
/**
|
if (identifier) {
|
||||||
* Checks if signing a message is possible with given email address.
|
// attempt to create a new keyring for this app/user
|
||||||
* Returns the first library that can.
|
mailvelope.createKeyring(identifier).then(fn, err => console.error(err));
|
||||||
*/
|
|
||||||
async getKeyForSigning(email) {
|
|
||||||
let key = OpenPGPUserStore.getPrivateKeyFor(email, 1);
|
|
||||||
if (key) {
|
|
||||||
return ['openpgp', key];
|
|
||||||
}
|
|
||||||
|
|
||||||
key = GnuPGUserStore.getPrivateKeyFor(email, 1);
|
|
||||||
if (key) {
|
|
||||||
return ['gnupg', key];
|
|
||||||
}
|
|
||||||
|
|
||||||
// return await this.getMailvelopePrivateKeyFor(email, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
async decrypt(message) {
|
|
||||||
const sender = message.from[0].email,
|
|
||||||
armoredText = message.plain();
|
|
||||||
|
|
||||||
if (!this.isEncrypted(armoredText)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try OpenPGP.js
|
|
||||||
let result = await OpenPGPUserStore.decrypt(armoredText, sender);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try Mailvelope (does not support inline images)
|
|
||||||
try {
|
|
||||||
let key = await this.getMailvelopePrivateKeyFor(message.to[0].email);
|
|
||||||
if (key) {
|
|
||||||
/**
|
|
||||||
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createEncryptedFormContainer
|
|
||||||
* Creates an iframe to display an encrypted form
|
|
||||||
*/
|
|
||||||
// mailvelope.createEncryptedFormContainer('#mailvelope-form');
|
|
||||||
/**
|
|
||||||
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createDisplayContainer
|
|
||||||
* Creates an iframe to display the decrypted content of the encrypted mail.
|
|
||||||
*/
|
|
||||||
const body = message.body;
|
|
||||||
body.textContent = '';
|
|
||||||
result = await mailvelope.createDisplayContainer(
|
|
||||||
'#'+body.id,
|
|
||||||
armoredText,
|
|
||||||
this.mailvelopeKeyring,
|
|
||||||
{
|
|
||||||
senderAddress: sender
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (result) {
|
|
||||||
if (result.error && result.error.message) {
|
|
||||||
if ('PWD_DIALOG_CANCEL' !== result.error.code) {
|
|
||||||
alert(result.error.code + ': ' + result.error.message);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
body.classList.add('mailvelope');
|
console.error(err);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
addEventListener('mailvelope-disconnect', event => {
|
||||||
|
alert('Mailvelope is updated to version ' + event.detail.version + '. Reload page');
|
||||||
|
}, false);
|
||||||
|
} else {
|
||||||
|
addEventListener('mailvelope', () => this.loadKeyrings(identifier));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (OpenPGPUserStore.isSupported()) {
|
||||||
|
OpenPGPUserStore.loadKeyrings();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SettingsCapa(Capa.GnuPG)) {
|
||||||
|
GnuPGUserStore.loadKeyrings();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
isSupported() {
|
||||||
|
return !!(OpenPGPUserStore.isSupported() || GnuPGUserStore.isSupported() || window.mailvelope);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
isEncrypted(text) {
|
||||||
|
return 0 === text.trim().indexOf(BEGIN_PGP_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
async mailvelopeHasPublicKeyForEmails(recipients) {
|
||||||
|
const
|
||||||
|
keyring = this.mailvelopeKeyring,
|
||||||
|
mailvelope = keyring && await keyring.validKeyForAddress(recipients)
|
||||||
|
/*.then(LookupResult => Object.entries(LookupResult))*/,
|
||||||
|
entries = mailvelope && Object.entries(mailvelope);
|
||||||
|
return entries && entries.filter(value => value[1]).length === recipients.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if verifying/encrypting a message is possible with given email addresses.
|
||||||
|
* Returns the first library that can.
|
||||||
|
*/
|
||||||
|
async hasPublicKeyForEmails(recipients) {
|
||||||
|
const count = recipients.length;
|
||||||
|
if (count) {
|
||||||
|
if (GnuPGUserStore.hasPublicKeyForEmails(recipients)) {
|
||||||
|
return 'gnupg';
|
||||||
|
}
|
||||||
|
if (OpenPGPUserStore.hasPublicKeyForEmails(recipients)) {
|
||||||
|
return 'openpgp';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
return false;
|
||||||
console.error(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now try GnuPG
|
async getMailvelopePrivateKeyFor(email/*, sign*/) {
|
||||||
return GnuPGUserStore.decrypt(message);
|
let keyring = this.mailvelopeKeyring;
|
||||||
}
|
if (keyring && await keyring.hasPrivateKey({email:email})) {
|
||||||
|
return ['mailvelope', email];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
async verify(message) {
|
/**
|
||||||
const signed = message.pgpSigned();
|
* Checks if signing a message is possible with given email address.
|
||||||
if (signed) {
|
* Returns the first library that can.
|
||||||
|
*/
|
||||||
|
async getKeyForSigning(email) {
|
||||||
|
let key = OpenPGPUserStore.getPrivateKeyFor(email, 1);
|
||||||
|
if (key) {
|
||||||
|
return ['openpgp', key];
|
||||||
|
}
|
||||||
|
|
||||||
|
key = GnuPGUserStore.getPrivateKeyFor(email, 1);
|
||||||
|
if (key) {
|
||||||
|
return ['gnupg', key];
|
||||||
|
}
|
||||||
|
|
||||||
|
// return await this.getMailvelopePrivateKeyFor(email, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
async decrypt(message) {
|
||||||
const sender = message.from[0].email,
|
const sender = message.from[0].email,
|
||||||
gnupg = GnuPGUserStore.hasPublicKeyForEmails([sender]),
|
armoredText = message.plain();
|
||||||
openpgp = OpenPGPUserStore.hasPublicKeyForEmails([sender]);
|
|
||||||
// Detached signature use GnuPG first, else we must download whole message
|
if (!this.isEncrypted(armoredText)) {
|
||||||
if (gnupg && signed.SigPartId) {
|
return;
|
||||||
return GnuPGUserStore.verify(message);
|
|
||||||
}
|
}
|
||||||
if (openpgp) {
|
|
||||||
return OpenPGPUserStore.verify(message);
|
// Try OpenPGP.js
|
||||||
|
let result = await OpenPGPUserStore.decrypt(armoredText, sender);
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
if (gnupg) {
|
|
||||||
return GnuPGUserStore.verify(message);
|
// Try Mailvelope (does not support inline images)
|
||||||
|
try {
|
||||||
|
let key = await this.getMailvelopePrivateKeyFor(message.to[0].email);
|
||||||
|
if (key) {
|
||||||
|
/**
|
||||||
|
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createEncryptedFormContainer
|
||||||
|
* Creates an iframe to display an encrypted form
|
||||||
|
*/
|
||||||
|
// mailvelope.createEncryptedFormContainer('#mailvelope-form');
|
||||||
|
/**
|
||||||
|
* https://mailvelope.github.io/mailvelope/Mailvelope.html#createDisplayContainer
|
||||||
|
* Creates an iframe to display the decrypted content of the encrypted mail.
|
||||||
|
*/
|
||||||
|
const body = message.body;
|
||||||
|
body.textContent = '';
|
||||||
|
result = await mailvelope.createDisplayContainer(
|
||||||
|
'#'+body.id,
|
||||||
|
armoredText,
|
||||||
|
this.mailvelopeKeyring,
|
||||||
|
{
|
||||||
|
senderAddress: sender
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (result) {
|
||||||
|
if (result.error && result.error.message) {
|
||||||
|
if ('PWD_DIALOG_CANCEL' !== result.error.code) {
|
||||||
|
alert(result.error.code + ': ' + result.error.message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
body.classList.add('mailvelope');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
}
|
}
|
||||||
// Mailvelope can't
|
|
||||||
// https://github.com/mailvelope/mailvelope/issues/434
|
// Now try GnuPG
|
||||||
|
return GnuPGUserStore.decrypt(message);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
async verify(message) {
|
||||||
* Returns headers that should be added to an outgoing email.
|
const signed = message.pgpSigned();
|
||||||
* So far this is only the autocrypt header.
|
if (signed) {
|
||||||
*/
|
const sender = message.from[0].email,
|
||||||
/*
|
gnupg = GnuPGUserStore.hasPublicKeyForEmails([sender]),
|
||||||
this.mailvelopeKeyring.additionalHeadersForOutgoingEmail(headers)
|
openpgp = OpenPGPUserStore.hasPublicKeyForEmails([sender]);
|
||||||
this.mailvelopeKeyring.addSyncHandler(syncHandlerObj)
|
// Detached signature use GnuPG first, else we must download whole message
|
||||||
this.mailvelopeKeyring.createKeyBackupContainer(selector, options)
|
if (gnupg && signed.SigPartId) {
|
||||||
this.mailvelopeKeyring.createKeyGenContainer(selector, {
|
return GnuPGUserStore.verify(message);
|
||||||
// userIds: [],
|
}
|
||||||
keySize: 4096
|
if (openpgp) {
|
||||||
})
|
return OpenPGPUserStore.verify(message);
|
||||||
|
}
|
||||||
|
if (gnupg) {
|
||||||
|
return GnuPGUserStore.verify(message);
|
||||||
|
}
|
||||||
|
// Mailvelope can't
|
||||||
|
// https://github.com/mailvelope/mailvelope/issues/434
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.mailvelopeKeyring.exportOwnPublicKey(emailAddr).then(<AsciiArmored, Error>)
|
/**
|
||||||
this.mailvelopeKeyring.importPublicKey(armored)
|
* Returns headers that should be added to an outgoing email.
|
||||||
|
* So far this is only the autocrypt header.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
this.mailvelopeKeyring.additionalHeadersForOutgoingEmail(headers)
|
||||||
|
this.mailvelopeKeyring.addSyncHandler(syncHandlerObj)
|
||||||
|
this.mailvelopeKeyring.createKeyBackupContainer(selector, options)
|
||||||
|
this.mailvelopeKeyring.createKeyGenContainer(selector, {
|
||||||
|
// userIds: [],
|
||||||
|
keySize: 4096
|
||||||
|
})
|
||||||
|
|
||||||
// https://mailvelope.github.io/mailvelope/global.html#SyncHandlerObject
|
this.mailvelopeKeyring.exportOwnPublicKey(emailAddr).then(<AsciiArmored, Error>)
|
||||||
this.mailvelopeKeyring.addSyncHandler({
|
this.mailvelopeKeyring.importPublicKey(armored)
|
||||||
uploadSync
|
|
||||||
downloadSync
|
|
||||||
backup
|
|
||||||
restore
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
|
|
||||||
};
|
// https://mailvelope.github.io/mailvelope/global.html#SyncHandlerObject
|
||||||
|
this.mailvelopeKeyring.addSyncHandler({
|
||||||
|
uploadSync
|
||||||
|
downloadSync
|
||||||
|
backup
|
||||||
|
restore
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ import { SMAudio } from 'Common/Audio';
|
||||||
|
|
||||||
import { i18n } from 'Common/Translator';
|
import { i18n } from 'Common/Translator';
|
||||||
import { attachmentDownload } from 'Common/Links';
|
import { attachmentDownload } from 'Common/Links';
|
||||||
import { FileInfo } from 'Common/File';
|
|
||||||
|
|
||||||
import { MessageFlagsCache } from 'Common/Cache';
|
import { MessageFlagsCache } from 'Common/Cache';
|
||||||
|
|
||||||
|
|
@ -53,73 +52,12 @@ import { AbstractViewRight } from 'Knoin/AbstractViews';
|
||||||
|
|
||||||
import { PgpUserStore } from 'Stores/User/Pgp';
|
import { PgpUserStore } from 'Stores/User/Pgp';
|
||||||
|
|
||||||
import { ParseMime } from 'Mime/Parser';
|
import { MimeToMessage } from 'Mime/Utils';
|
||||||
import { AttachmentModel } from 'Model/Attachment';
|
|
||||||
|
|
||||||
const
|
const
|
||||||
oMessageScrollerDom = () => elementById('messageItem') || {},
|
oMessageScrollerDom = () => elementById('messageItem') || {},
|
||||||
|
|
||||||
currentMessage = () => MessageUserStore.message(),
|
currentMessage = () => MessageUserStore.message();
|
||||||
|
|
||||||
mimeToMessage = (data, message) => {
|
|
||||||
const headers = data.split(/\r?\n\r?\n/)[0];
|
|
||||||
if (/Content-Type:/i.test(headers)) {
|
|
||||||
const struct = ParseMime(data),
|
|
||||||
text = struct.getByContentType('text/plain');
|
|
||||||
let html = struct.getByContentType('text/html');
|
|
||||||
html = html ? html.body : '';
|
|
||||||
|
|
||||||
// TODO: Check multipart/signed application/pgp-signature application/pgp-keys
|
|
||||||
struct.forEach(part => {
|
|
||||||
let cd = part.header('content-disposition'),
|
|
||||||
cid = part.header('content-id'),
|
|
||||||
type = part.header('content-type');
|
|
||||||
if (cid || cd) {
|
|
||||||
// if (cd && 'attachment' === cd.value) {
|
|
||||||
let attachment = new AttachmentModel;
|
|
||||||
attachment.mimeType = type.value;
|
|
||||||
attachment.fileName = type.name || (cd && cd.params.filename) || '';
|
|
||||||
attachment.fileNameExt = attachment.fileName.replace(/^.+(\.[a-z]+)$/, '$1');
|
|
||||||
attachment.fileType = FileInfo.getType('', type.value);
|
|
||||||
attachment.url = part.dataUrl;
|
|
||||||
attachment.friendlySize = FileInfo.friendlySize(part.body.length);
|
|
||||||
/*
|
|
||||||
attachment.isThumbnail = false;
|
|
||||||
attachment.contentLocation = '';
|
|
||||||
attachment.download = '';
|
|
||||||
attachment.folder = '';
|
|
||||||
attachment.uid = '';
|
|
||||||
attachment.mimeIndex = part.id;
|
|
||||||
attachment.framed = false;
|
|
||||||
*/
|
|
||||||
attachment.cid = cid ? cid.value : '';
|
|
||||||
if (cid && html) {
|
|
||||||
let cid = 'cid:' + attachment.contentId(),
|
|
||||||
found = html.includes(cid);
|
|
||||||
attachment.isInline(found);
|
|
||||||
attachment.isLinked(found);
|
|
||||||
found && (html = html
|
|
||||||
.replace('src="' + cid + '"', 'src="' + attachment.url + '"')
|
|
||||||
.replace("src='" + cid + "'", "src='" + attachment.url + "'")
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
message.attachments.push(attachment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
message.plain(text ? text.body : '');
|
|
||||||
if (html) {
|
|
||||||
message.html(html.replace(/<\/?script[\s\S]*?>/gi, ''));
|
|
||||||
message.viewHtml();
|
|
||||||
} else {
|
|
||||||
message.viewPlain();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
message.plain(data);
|
|
||||||
message.viewPlain();
|
|
||||||
};
|
|
||||||
|
|
||||||
export class MailMessageView extends AbstractViewRight {
|
export class MailMessageView extends AbstractViewRight {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
@ -647,7 +585,8 @@ export class MailMessageView extends AbstractViewRight {
|
||||||
const oMessage = currentMessage();
|
const oMessage = currentMessage();
|
||||||
PgpUserStore.decrypt(oMessage).then(result => {
|
PgpUserStore.decrypt(oMessage).then(result => {
|
||||||
if (result && result.data) {
|
if (result && result.data) {
|
||||||
mimeToMessage(result.data, oMessage);
|
MimeToMessage(result.data, oMessage);
|
||||||
|
oMessage.html() ? oMessage.viewHtml() : oMessage.viewPlain();
|
||||||
if (result.signatures && result.signatures.length) {
|
if (result.signatures && result.signatures.length) {
|
||||||
oMessage.pgpSigned(true);
|
oMessage.pgpSigned(true);
|
||||||
oMessage.pgpVerified({
|
oMessage.pgpVerified({
|
||||||
|
|
|
||||||
|
|
@ -660,48 +660,57 @@ trait Messages
|
||||||
{
|
{
|
||||||
$sFolderName = $this->GetActionParam('Folder', '');
|
$sFolderName = $this->GetActionParam('Folder', '');
|
||||||
$iUid = (int) $this->GetActionParam('Uid', 0);
|
$iUid = (int) $this->GetActionParam('Uid', 0);
|
||||||
$sBodyPartId = $this->GetActionParam('BodyPartId', '');
|
$sBodyPart = $this->GetActionParam('BodyPart', '');
|
||||||
$sSigPartId = $this->GetActionParam('SigPartId', '');
|
$sSigPart = $this->GetActionParam('SigPart', '');
|
||||||
// $sMicAlg = $this->GetActionParam('MicAlg', '');
|
if ($sBodyPart) {
|
||||||
|
|
||||||
$oAccount = $this->initMailClientConnection();
|
|
||||||
|
|
||||||
$oImapClient = $this->MailClient()->ImapClient();
|
|
||||||
$oImapClient->FolderExamine($sFolderName);
|
|
||||||
|
|
||||||
$aParts = [
|
|
||||||
FetchType::BODY_PEEK.'['.$sBodyPartId.']',
|
|
||||||
// An empty section specification refers to the entire message, including the header.
|
|
||||||
// But Dovecot does not return it with BODY.PEEK[1], so we also use BODY.PEEK[1.MIME].
|
|
||||||
FetchType::BODY_PEEK.'['.$sBodyPartId.'.MIME]'
|
|
||||||
];
|
|
||||||
if ($sSigPartId) {
|
|
||||||
$aParts[] = FetchType::BODY_PEEK.'['.$sSigPartId.']';
|
|
||||||
}
|
|
||||||
|
|
||||||
$oFetchResponse = $oImapClient->Fetch($aParts, $iUid, true)[0];
|
|
||||||
|
|
||||||
$sBodyMime = $oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sBodyPartId.'.MIME]');
|
|
||||||
if ($sSigPartId) {
|
|
||||||
$result = [
|
$result = [
|
||||||
'text' => \preg_replace('/\\R/s', "\r\n",
|
'text' => \preg_replace('/\\R/s', "\r\n", $sBodyPart),
|
||||||
$sBodyMime . $oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sBodyPartId.']')
|
'signature' => $this->GetActionParam('SigPart', '')
|
||||||
),
|
|
||||||
'signature' => preg_replace('/[^\x00-\x7F]/', '',
|
|
||||||
$oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sSigPartId.']')
|
|
||||||
)
|
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
// clearsigned text
|
$sBodyPartId = $this->GetActionParam('BodyPartId', '');
|
||||||
$result = [
|
$sSigPartId = $this->GetActionParam('SigPartId', '');
|
||||||
'text' => $oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sBodyPartId.']'),
|
// $sMicAlg = $this->GetActionParam('MicAlg', '');
|
||||||
'signature' => ''
|
|
||||||
|
$oAccount = $this->initMailClientConnection();
|
||||||
|
|
||||||
|
$oImapClient = $this->MailClient()->ImapClient();
|
||||||
|
$oImapClient->FolderExamine($sFolderName);
|
||||||
|
|
||||||
|
$aParts = [
|
||||||
|
FetchType::BODY_PEEK.'['.$sBodyPartId.']',
|
||||||
|
// An empty section specification refers to the entire message, including the header.
|
||||||
|
// But Dovecot does not return it with BODY.PEEK[1], so we also use BODY.PEEK[1.MIME].
|
||||||
|
FetchType::BODY_PEEK.'['.$sBodyPartId.'.MIME]'
|
||||||
];
|
];
|
||||||
$decode = (new \MailSo\Mime\HeaderCollection($sBodyMime))->ValueByName(\MailSo\Mime\Enumerations\Header::CONTENT_TRANSFER_ENCODING);
|
if ($sSigPartId) {
|
||||||
if ('base64' === $decode) {
|
$aParts[] = FetchType::BODY_PEEK.'['.$sSigPartId.']';
|
||||||
$result['text'] = \base64_decode($result['text']);
|
}
|
||||||
} else if ('quoted-printable' === $decode) {
|
|
||||||
$result['text'] = \quoted_printable_decode($result['text']);
|
$oFetchResponse = $oImapClient->Fetch($aParts, $iUid, true)[0];
|
||||||
|
|
||||||
|
$sBodyMime = $oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sBodyPartId.'.MIME]');
|
||||||
|
if ($sSigPartId) {
|
||||||
|
$result = [
|
||||||
|
'text' => \preg_replace('/\\R/s', "\r\n",
|
||||||
|
$sBodyMime . $oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sBodyPartId.']')
|
||||||
|
),
|
||||||
|
'signature' => preg_replace('/[^\x00-\x7F]/', '',
|
||||||
|
$oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sSigPartId.']')
|
||||||
|
)
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
// clearsigned text
|
||||||
|
$result = [
|
||||||
|
'text' => $oFetchResponse->GetFetchValue(FetchType::BODY.'['.$sBodyPartId.']'),
|
||||||
|
'signature' => ''
|
||||||
|
];
|
||||||
|
$decode = (new \MailSo\Mime\HeaderCollection($sBodyMime))->ValueByName(\MailSo\Mime\Enumerations\Header::CONTENT_TRANSFER_ENCODING);
|
||||||
|
if ('base64' === $decode) {
|
||||||
|
$result['text'] = \base64_decode($result['text']);
|
||||||
|
} else if ('quoted-printable' === $decode) {
|
||||||
|
$result['text'] = \quoted_printable_decode($result['text']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue