From f625abe0b209b65b15a6506f90e4bc9f1259fefb Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Wed, 2 Feb 2022 16:50:27 +0100 Subject: [PATCH] #89 OpenPGP.js sign cleartext --- dev/Stores/User/OpenPGP.js | 17 ++++++++++++ dev/Stores/User/Pgp.js | 7 ++--- dev/View/Popup/Compose.js | 54 ++++++++++++++++++++++---------------- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/dev/Stores/User/OpenPGP.js b/dev/Stores/User/OpenPGP.js index aae020852..6e7281764 100644 --- a/dev/Stores/User/OpenPGP.js +++ b/dev/Stores/User/OpenPGP.js @@ -236,4 +236,21 @@ export const OpenPGPUserStore = new class { } } + async signCleartext(text, privateKey) { + const passphrase = prompt('OpenPGP.js Passphrase for ' + privateKey.id + ' ' + privateKey.emails[0]); + if (null !== passphrase) { + privateKey = await openpgp.decryptKey({ + privateKey: privateKey.key, + passphrase + }); + const unsignedMessage = await openpgp.createCleartextMessage({ text: text }); + return await openpgp.sign({ + message: unsignedMessage, // CleartextMessage or Message object + signingKeys: privateKey +// detached: false + }); + } + return false; + } + }; diff --git a/dev/Stores/User/Pgp.js b/dev/Stores/User/Pgp.js index 5d4b989ff..58e90ca83 100644 --- a/dev/Stores/User/Pgp.js +++ b/dev/Stores/User/Pgp.js @@ -106,17 +106,18 @@ export const PgpUserStore = new class { * Returns the first library that can. */ async getKeyForSigning(email) { +/* let key = GnuPGUserStore.getPrivateKeyFor(email, 1); if (key) { return ['gnupg', key]; } - - key = OpenPGPUserStore.getPrivateKeyFor(email, 1); +*/ + let key = OpenPGPUserStore.getPrivateKeyFor(email, 1); if (key) { return ['openpgp', key]; } - return await this.getMailvelopePrivateKeyFor(email, 1); +// return await this.getMailvelopePrivateKeyFor(email, 1); } async decrypt(message) { diff --git a/dev/View/Popup/Compose.js b/dev/View/Popup/Compose.js index a6c8f1a0a..b229939ad 100644 --- a/dev/View/Popup/Compose.js +++ b/dev/View/Popup/Compose.js @@ -282,7 +282,7 @@ class ComposePopupView extends AbstractViewPopup { this.canPgpSign(false); value && PgpUserStore.getKeyForSigning(value.email()).then(result => { console.log({canPgpSign:result}); - this.canPgpSign(!!result) + this.canPgpSign(result) }); }, @@ -405,7 +405,7 @@ class ComposePopupView extends AbstractViewPopup { if (!sSentFolder) { showScreenPopup(FolderSystemPopupView, [SetSystemFoldersNotification.Sent]); - } else { + } else try { this.sendError(false); this.sending(true); @@ -452,28 +452,30 @@ class ComposePopupView extends AbstractViewPopup { 30000 ); - let pgpPromise = null, - cfg = { - data: params.Text, - }; - if ('openpgp' == sign) { - let privateKey, sender = this.currentIdentity().email(); - try { - const key = OpenPGPUserStore.getPrivateKeyFor(sender); - if (key) { - key.decrypt(window.prompt('Passphrase')); - cfg.privateKey = privateKey = key; - } - } catch (e) { - console.error(e); - privateKey = null; - } - if (!privateKey) { - this.sendError(true); - this.sendErrorDesc(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND_FOR', { EMAIL: sender })); - return; - } + if (encrypt) { + throw 'Encryption not yet implemented'; } + if (sign && 'openpgp' != sign[0]) { + throw 'Signing with ' + sign[0] + ' not yet implemented'; + } + if (sign && 'openpgp' == sign[0]) { + if (params.TextIsHtml) { + throw i18n('PGP_NOTIFICATIONS/PGP_ERROR', { ERROR: "Can't sign HTML" }); + } + OpenPGPUserStore.signCleartext(params.Text, sign[1]).then(text => { + if (text) { + params.Text = text; + send(); + } else { + this.sendError(true); + this.sendErrorDesc(i18n('PGP_NOTIFICATIONS/PGP_ERROR', { ERROR: 'Signing failed' })); + this.sending(false); + } + }); + } else { + send(); + } +/* if (encrypt && sign && encrypt != sign) { // error 'sign and encrypt must be same engine'; } else if ('openpgp' == encrypt) { @@ -498,6 +500,12 @@ class ComposePopupView extends AbstractViewPopup { this.sendErrorDesc(i18n('PGP_NOTIFICATIONS/PGP_ERROR', { ERROR: '' + e })); }) : send(); +*/ + } catch (e) { + console.error(e); + this.sendError(true); + this.sendErrorDesc(e); + this.sending(false); } } }