* Decrypt armored message * @param {String} armored
(armored)
| 343 | * @param {String} armored |
| 344 | */ |
| 345 | async decryptArmored(armored) { |
| 346 | try { |
| 347 | const isEncrypted = /BEGIN\sPGP\sMESSAGE/.test(armored); |
| 348 | const isSigned = /BEGIN\sPGP\sSIGNED\sMESSAGE/.test(armored); |
| 349 | if (!isEncrypted && !isSigned) { |
| 350 | // clear text only — no PGP, no MIME parsing needed. |
| 351 | const text = html2textIfHtml(await mvelo.util.sanitizeHTML(armored)); |
| 352 | this.ports.editor.emit('set-text', {text: this.formatMessage(text, this.options)}); |
| 353 | this.ports.editor.emit('decrypt-end'); |
| 354 | return; |
| 355 | } |
| 356 | const unlockKey = async options => { |
| 357 | const result = await this.unlockKey(options); |
| 358 | if (this.state.popupId) { |
| 359 | this.ports.editor.emit('hide-pwd-dialog'); |
| 360 | } |
| 361 | return result; |
| 362 | }; |
| 363 | let data; |
| 364 | let signatures; |
| 365 | if (isEncrypted) { |
| 366 | const normalized = normalizeArmored(armored, /-----BEGIN PGP MESSAGE-----[\s\S]+?-----END PGP MESSAGE-----/); |
| 367 | ({data, signatures} = await model.decryptMessage({ |
| 368 | armored: normalized, |
| 369 | keyringId: this.state.keyringId, |
| 370 | unlockKey, |
| 371 | selfSigned: Boolean(this.options.armoredDraft), |
| 372 | uiLogSource: 'security_log_editor' |
| 373 | })); |
| 374 | } else { |
| 375 | ({data, signatures} = await model.verifyMessage({ |
| 376 | armored, |
| 377 | keyringId: this.state.keyringId, |
| 378 | uiLogSource: 'security_log_editor' |
| 379 | })); |
| 380 | } |
| 381 | if (this.options.armoredDraft) { |
| 382 | if (!(signatures && signatures.length === 1 && signatures[0].valid)) { |
| 383 | throw {message: 'Restoring of the draft failed due to invalid signature.'}; |
| 384 | } |
| 385 | } |
| 386 | const {message, attachments} = await parseMessage(data, 'text'); |
| 387 | this.ports.editor.emit('set-text', {text: this.formatMessage(message, this.options)}); |
| 388 | if (this.options.keepAttachments) { |
| 389 | for (const attachment of attachments) { |
| 390 | this.ports.editor.emit('set-attachment', {attachment}); |
| 391 | } |
| 392 | } |
| 393 | this.ports.editor.emit('decrypt-end'); |
| 394 | } catch (error) { |
| 395 | this.ports.editor.emit('decrypt-failed', {error: mapError(error)}); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | formatMessage(msg, options) { |
| 400 | if (options.quotedMailIndent) { |
no test coverage detected