NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
application_handler()

Manage the MIME type "application/pgp" or "application/smime". More...

+ Collaboration diagram for application_handler():

Functions

int pgp_gpgme_application_handler (struct Body *b, struct State *state)
 Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.
 
int smime_gpgme_application_handler (struct Body *b, struct State *state)
 Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.
 
int pgp_class_application_handler (struct Body *b, struct State *state)
 Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.
 
int smime_class_application_handler (struct Body *b, struct State *state)
 Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.
 

Detailed Description

Manage the MIME type "application/pgp" or "application/smime".

Parameters
bBody of the email
stateState of text being processed
Return values
0Success
-1Error

Function Documentation

◆ pgp_gpgme_application_handler()

int pgp_gpgme_application_handler ( struct Body b,
struct State state 
)

Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.

Definition at line 2427 of file crypt_gpgme.c.

2428{
2429 int needpass = -1;
2430 bool pgp_keyblock = false;
2431 bool clearsign = false;
2432 long bytes;
2433 LOFF_T last_pos;
2434 LOFF_T block_begin;
2435 LOFF_T block_end;
2436 char buf[8192] = { 0 };
2437 FILE *fp_out = NULL;
2438
2439 gpgme_error_t err = 0;
2440 gpgme_data_t armored_data = NULL;
2441
2442 bool maybe_goodsig = true;
2443 bool have_any_sigs = false;
2444
2445 char body_charset[256] = { 0 }; /* Only used for clearsigned messages. */
2446 char *gpgcharset = NULL;
2447
2448 mutt_debug(LL_DEBUG2, "Entering handler\n");
2449
2450 /* For clearsigned messages we won't be able to get a character set
2451 * but we know that this may only be text thus we assume Latin-1 here. */
2452 if (!mutt_body_get_charset(b, body_charset, sizeof(body_charset)))
2453 mutt_str_copy(body_charset, "iso-8859-1", sizeof(body_charset));
2454
2455 if (!mutt_file_seek(state->fp_in, b->offset, SEEK_SET))
2456 {
2457 return -1;
2458 }
2459 last_pos = b->offset;
2460
2461 for (bytes = b->length; bytes > 0;)
2462 {
2463 // record before the fgets in case it is a BEGIN block
2464 block_begin = last_pos;
2465
2466 if (!fgets(buf, sizeof(buf), state->fp_in))
2467 break;
2468
2469 LOFF_T offset = ftello(state->fp_in);
2470 if (offset < 0)
2471 {
2472 mutt_debug(LL_DEBUG1, "ftello() failed on fd %d\n", fileno(state->fp_in));
2473 offset = 0;
2474 }
2475 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
2476 last_pos = offset;
2477
2478 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
2479 if (plen != 0)
2480 {
2481 needpass = 0;
2482 clearsign = false;
2483 pgp_keyblock = false;
2484
2485 if (MESSAGE(buf + plen))
2486 {
2487 needpass = 1;
2488 }
2489 else if (SIGNED_MESSAGE(buf + plen))
2490 {
2491 clearsign = true;
2492 }
2493 else if (PUBLIC_KEY_BLOCK(buf + plen))
2494 {
2495 pgp_keyblock = true;
2496 }
2497 else
2498 {
2499 /* XXX we may wish to recode here */
2500 if (state->prefix)
2501 state_puts(state, state->prefix);
2502 state_puts(state, buf);
2503 continue;
2504 }
2505
2506 /* Find the end of armored block. */
2507 while ((bytes > 0) && (fgets(buf, sizeof(buf) - 1, state->fp_in) != NULL))
2508 {
2509 offset = ftello(state->fp_in);
2510 if (offset < 0)
2511 {
2512 mutt_debug(LL_DEBUG1, "ftello() failed on fd %d\n", fileno(state->fp_in));
2513 offset = 0;
2514 }
2515 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
2516 last_pos = offset;
2517
2518 if (needpass && mutt_str_equal("-----END PGP MESSAGE-----\n", buf))
2519 {
2520 break;
2521 }
2522
2523 if (!needpass && (mutt_str_equal("-----END PGP SIGNATURE-----\n", buf) ||
2524 mutt_str_equal("-----END PGP PUBLIC KEY BLOCK-----\n", buf)))
2525 {
2526 break;
2527 }
2528
2529 // remember optional Charset: armor header as defined by rfc4880
2530 if (mutt_strn_equal("Charset: ", buf, 9))
2531 {
2532 size_t l = 0;
2533 FREE(&gpgcharset);
2534 gpgcharset = mutt_str_dup(buf + 9);
2535 if ((l = mutt_str_len(gpgcharset)) > 0 && gpgcharset[l - 1] == '\n')
2536 gpgcharset[l - 1] = 0;
2537 if (!mutt_ch_check_charset(gpgcharset, 0))
2538 mutt_str_replace(&gpgcharset, "UTF-8");
2539 }
2540 }
2541 block_end = ftello(state->fp_in);
2542 if (block_end < 0)
2543 {
2544 mutt_debug(LL_DEBUG1, "ftello() failed on fd %d\n", fileno(state->fp_in));
2545 block_end = 0;
2546 }
2547
2548 have_any_sigs = (have_any_sigs || (clearsign && (state->flags & STATE_VERIFY)));
2549
2550 /* Copy PGP material to an data container */
2551 armored_data = file_to_data_object(state->fp_in, block_begin, block_end - block_begin);
2552 fseeko(state->fp_in, block_end, 0);
2553
2554 /* Invoke PGP if needed */
2555 if (pgp_keyblock)
2556 {
2557 pgp_gpgme_extract_keys(armored_data, &fp_out);
2558 }
2559 else if (!clearsign || (state->flags & STATE_VERIFY))
2560 {
2561 gpgme_data_t plaintext = create_gpgme_data();
2562 gpgme_ctx_t ctx = create_gpgme_context(false);
2563
2564 if (clearsign)
2565 {
2566 err = gpgme_op_verify(ctx, armored_data, NULL, plaintext);
2567 }
2568 else
2569 {
2570 err = gpgme_op_decrypt_verify(ctx, armored_data, plaintext);
2571 if (gpg_err_code(err) == GPG_ERR_NO_DATA)
2572 {
2573 /* Decrypt verify can't handle signed only messages. */
2574 gpgme_data_seek(armored_data, 0, SEEK_SET);
2575 /* Must release plaintext so that we supply an uninitialized object. */
2576 gpgme_data_release(plaintext);
2577 plaintext = create_gpgme_data();
2578 err = gpgme_op_verify(ctx, armored_data, NULL, plaintext);
2579 }
2580 }
2581 redraw_if_needed(ctx);
2582
2583 if (err != 0)
2584 {
2585 char errbuf[200] = { 0 };
2586
2587 snprintf(errbuf, sizeof(errbuf) - 1,
2588 _("Error: decryption/verification failed: %s\n"), gpgme_strerror(err));
2589 state_puts(state, errbuf);
2590 }
2591 else
2592 {
2593 /* Decryption/Verification succeeded */
2594
2595 mutt_message(_("PGP message successfully decrypted"));
2596
2597 bool sig_stat = false;
2598 char *tmpfname = NULL;
2599
2600 /* Check whether signatures have been verified. */
2601 gpgme_verify_result_t verify_result = gpgme_op_verify_result(ctx);
2602 if (verify_result->signatures)
2603 sig_stat = true;
2604
2605 have_any_sigs = false;
2606 maybe_goodsig = false;
2607 if ((state->flags & STATE_DISPLAY) && sig_stat)
2608 {
2609 int res, idx;
2610 bool anybad = false;
2611
2612 state_attach_puts(state, _("[-- Begin signature information --]\n"));
2613 have_any_sigs = true;
2614 for (idx = 0; (res = show_one_sig_status(ctx, idx, state)) != -1; idx++)
2615 {
2616 if (res == 1)
2617 anybad = true;
2618 }
2619 if (!anybad && idx)
2620 maybe_goodsig = true;
2621
2622 state_attach_puts(state, _("[-- End signature information --]\n\n"));
2623 }
2624
2625 tmpfname = data_object_to_tempfile(plaintext, &fp_out);
2626 if (tmpfname)
2627 {
2628 unlink(tmpfname);
2629 FREE(&tmpfname);
2630 }
2631 else
2632 {
2633 mutt_file_fclose(&fp_out);
2634 state_puts(state, _("Error: copy data failed\n"));
2635 }
2636 }
2637 gpgme_data_release(plaintext);
2638 gpgme_release(ctx);
2639 }
2640
2641 /* Now, copy cleartext to the screen. NOTE - we expect that PGP
2642 * outputs utf-8 cleartext. This may not always be true, but it
2643 * seems to be a reasonable guess. */
2644 if (state->flags & STATE_DISPLAY)
2645 {
2646 if (needpass)
2647 state_attach_puts(state, _("[-- BEGIN PGP MESSAGE --]\n\n"));
2648 else if (pgp_keyblock)
2649 state_attach_puts(state, _("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"));
2650 else
2651 state_attach_puts(state, _("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"));
2652 }
2653
2654 if (clearsign)
2655 {
2656 copy_clearsigned(armored_data, state, body_charset);
2657 }
2658 else if (fp_out)
2659 {
2660 int c;
2661 char *expected_charset = gpgcharset && *gpgcharset ? gpgcharset : "utf-8";
2662 rewind(fp_out);
2663 struct FgetConv *fc = mutt_ch_fgetconv_open(fp_out, expected_charset,
2665 while ((c = mutt_ch_fgetconv(fc)) != EOF)
2666 {
2667 state_putc(state, c);
2668 if ((c == '\n') && state->prefix)
2669 state_puts(state, state->prefix);
2670 }
2672 }
2673
2674 if (state->flags & STATE_DISPLAY)
2675 {
2676 state_putc(state, '\n');
2677 if (needpass)
2678 state_attach_puts(state, _("[-- END PGP MESSAGE --]\n"));
2679 else if (pgp_keyblock)
2680 state_attach_puts(state, _("[-- END PGP PUBLIC KEY BLOCK --]\n"));
2681 else
2682 state_attach_puts(state, _("[-- END PGP SIGNED MESSAGE --]\n"));
2683 }
2684
2685 // Multiple PGP blocks can exist, so clean these up in each loop
2686 gpgme_data_release(armored_data);
2687 mutt_file_fclose(&fp_out);
2688 }
2689 else
2690 {
2691 /* A traditional PGP part may mix signed and unsigned content */
2692 /* XXX we may wish to recode here */
2693 if (state->prefix)
2694 state_puts(state, state->prefix);
2695 state_puts(state, buf);
2696 }
2697 }
2698 FREE(&gpgcharset);
2699
2700 b->goodsig = (maybe_goodsig && have_any_sigs);
2701
2702 if (needpass == -1)
2703 {
2704 state_attach_puts(state, _("[-- Error: could not find beginning of PGP message --]\n\n"));
2705 return 1;
2706 }
2707 mutt_debug(LL_DEBUG2, "Leaving handler\n");
2708
2709 return err;
2710}
const char * cc_charset(void)
Get the cached value of $charset.
Definition: config_cache.c:116
static gpgme_data_t create_gpgme_data(void)
Create a new GPGME data object.
Definition: crypt_gpgme.c:400
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:361
#define SIGNED_MESSAGE(_y)
Definition: crypt_gpgme.c:102
static gpgme_data_t file_to_data_object(FILE *fp, long offset, size_t length)
Create GPGME data object from file.
Definition: crypt_gpgme.c:493
#define PUBLIC_KEY_BLOCK(_y)
Definition: crypt_gpgme.c:103
static char * data_object_to_tempfile(gpgme_data_t data, FILE **fp_ret)
Copy a data object to a temporary file.
Definition: crypt_gpgme.c:561
static void redraw_if_needed(gpgme_ctx_t ctx)
Accommodate for a redraw if needed.
Definition: crypt_gpgme.c:121
static int pgp_gpgme_extract_keys(gpgme_data_t keydata, FILE **fp)
Write PGP keys to a file.
Definition: crypt_gpgme.c:2068
static void copy_clearsigned(gpgme_data_t data, struct State *state, char *charset)
Copy a clearsigned message.
Definition: crypt_gpgme.c:2371
static int show_one_sig_status(gpgme_ctx_t ctx, int idx, struct State *state)
Show information about one signature.
Definition: crypt_gpgme.c:1419
#define MESSAGE(_y)
Definition: crypt_gpgme.c:101
char * mutt_body_get_charset(struct Body *b, char *buf, size_t buflen)
Get a body's character set.
Definition: body.c:132
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:775
#define mutt_file_fclose(FP)
Definition: file.h:147
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define FREE(x)
Definition: memory.h:45
bool mutt_ch_check_charset(const char *cs, bool strict)
Does iconv understand a character set?
Definition: charset.c:893
int mutt_ch_fgetconv(struct FgetConv *fc)
Convert a file's character set.
Definition: charset.c:982
struct FgetConv * mutt_ch_fgetconv_open(FILE *fp, const char *from, const char *to, uint8_t flags)
Prepare a file for charset conversion.
Definition: charset.c:932
void mutt_ch_fgetconv_close(struct FgetConv **ptr)
Close an fgetconv handle.
Definition: charset.c:964
#define MUTT_ICONV_HOOK_FROM
apply charset-hooks to fromcode
Definition: charset.h:73
#define _(a)
Definition: message.h:28
void state_attach_puts(struct State *state, const char *t)
Write a string to the state.
Definition: state.c:103
#define state_puts(STATE, STR)
Definition: state.h:58
#define STATE_DISPLAY
Output is displayed to the user.
Definition: state.h:33
#define state_putc(STATE, STR)
Definition: state.h:59
#define STATE_VERIFY
Perform signature verification.
Definition: state.h:34
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:709
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:474
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:545
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:630
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:329
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
bool goodsig
Good cryptographic signature.
Definition: body.h:45
Cursor for converting a file's encoding.
Definition: charset.h:42
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition: state.h:52
FILE * fp_in
File to read from.
Definition: state.h:49
const char * prefix
String to add to the beginning of each output line.
Definition: state.h:51
+ Here is the call graph for this function:

◆ smime_gpgme_application_handler()

int smime_gpgme_application_handler ( struct Body b,
struct State state 
)

Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.

Definition at line 2809 of file crypt_gpgme.c.

2810{
2811 int is_signed = 0;
2812 int rc = 0;
2813
2814 mutt_debug(LL_DEBUG2, "Entering handler\n");
2815
2816 /* clear out any mime headers before the handler, so they can't be spoofed. */
2818 b->warnsig = false;
2819 FILE *fp_out = mutt_file_mkstemp();
2820 if (!fp_out)
2821 {
2822 mutt_perror(_("Can't create temporary file"));
2823 if (state->flags & STATE_DISPLAY)
2824 {
2825 state_attach_puts(state, _("[-- Error: could not create temporary file --]\n"));
2826 }
2827 return -1;
2828 }
2829
2830 struct Body *tattach = decrypt_part(b, state, fp_out, true, &is_signed);
2831 if (tattach)
2832 {
2833 tattach->goodsig = is_signed > 0;
2834
2835 if (state->flags & STATE_DISPLAY)
2836 {
2837 state_attach_puts(state, is_signed ?
2838 _("[-- The following data is S/MIME signed --]\n\n") :
2839 _("[-- The following data is S/MIME encrypted --]\n\n"));
2840 mutt_protected_headers_handler(tattach, state);
2841 }
2842
2843 /* Store any protected headers in the parent so they can be
2844 * accessed for index updates after the handler recursion is done.
2845 * This is done before the handler to prevent a nested encrypted
2846 * handler from freeing the headers. */
2848 b->mime_headers = tattach->mime_headers;
2849 tattach->mime_headers = NULL;
2850
2851 FILE *fp_save = state->fp_in;
2852 state->fp_in = fp_out;
2853 rc = mutt_body_handler(tattach, state);
2854 state->fp_in = fp_save;
2855
2856 /* Embedded multipart signed protected headers override the
2857 * encrypted headers. We need to do this after the handler so
2858 * they can be printed in the pager. */
2859 if (mutt_is_multipart_signed(tattach) && tattach->parts && tattach->parts->mime_headers)
2860 {
2862 b->mime_headers = tattach->parts->mime_headers;
2863 tattach->parts->mime_headers = NULL;
2864 }
2865
2866 /* if a multipart/signed is the _only_ sub-part of a multipart/encrypted,
2867 * cache signature verification status. */
2868 if (mutt_is_multipart_signed(tattach) && !tattach->next)
2869 {
2870 b->goodsig = tattach->goodsig;
2871 if (!b->goodsig)
2872 b->warnsig = tattach->warnsig;
2873 }
2874 else if (tattach->goodsig)
2875 {
2876 b->goodsig = true;
2877 b->warnsig = tattach->warnsig;
2878 }
2879
2880 if (state->flags & STATE_DISPLAY)
2881 {
2882 state_puts(state, "\n");
2883 state_attach_puts(state, is_signed ? _("[-- End of S/MIME signed data --]\n") :
2884 _("[-- End of S/MIME encrypted data --]\n"));
2885 }
2886
2887 mutt_body_free(&tattach);
2888 }
2889
2890 mutt_file_fclose(&fp_out);
2891 mutt_debug(LL_DEBUG2, "Leaving handler\n");
2892
2893 return rc;
2894}
SecurityFlags mutt_is_multipart_signed(struct Body *b)
Is a message signed?
Definition: crypt.c:394
static struct Body * decrypt_part(struct Body *b, struct State *state, FILE *fp_out, bool is_smime, int *r_is_signed)
Decrypt a PGP or SMIME message.
Definition: crypt_gpgme.c:1693
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:58
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:126
int mutt_protected_headers_handler(struct Body *b_email, struct State *state)
Handler for protected headers - Implements handler_t -.
Definition: crypt.c:1103
#define mutt_perror(...)
Definition: logging2.h:93
int mutt_body_handler(struct Body *b, struct State *state)
Handler for the Body of an email.
Definition: handler.c:1631
The body of an email.
Definition: body.h:36
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:72
struct Envelope * mime_headers
Memory hole protected headers.
Definition: body.h:75
struct Body * next
next attachment in the list
Definition: body.h:71
bool warnsig
Maybe good signature.
Definition: body.h:48
#define mutt_file_mkstemp()
Definition: tmp.h:36
+ Here is the call graph for this function:

◆ pgp_class_application_handler()

int pgp_class_application_handler ( struct Body b,
struct State state 
)

Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.

Definition at line 472 of file pgp.c.

473{
474 bool could_not_decrypt = false;
475 int decrypt_okay_rc = 0;
476 int needpass = -1;
477 bool pgp_keyblock = false;
478 bool clearsign = false;
479 int rc = -1;
480 int c = 1;
481 long bytes;
482 LOFF_T last_pos, offset;
483 char buf[8192] = { 0 };
484 FILE *fp_pgp_out = NULL, *fp_pgp_in = NULL, *fp_pgp_err = NULL;
485 FILE *fp_tmp = NULL;
486 pid_t pid;
487 struct Buffer *tmpfname = buf_pool_get();
488
489 bool maybe_goodsig = true;
490 bool have_any_sigs = false;
491
492 char *gpgcharset = NULL;
493 char body_charset[256] = { 0 };
494 mutt_body_get_charset(b, body_charset, sizeof(body_charset));
495
496 if (!mutt_file_seek(state->fp_in, b->offset, SEEK_SET))
497 {
498 return -1;
499 }
500 last_pos = b->offset;
501
502 for (bytes = b->length; bytes > 0;)
503 {
504 if (!fgets(buf, sizeof(buf), state->fp_in))
505 break;
506
507 offset = ftello(state->fp_in);
508 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
509 last_pos = offset;
510
511 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
512 if (plen != 0)
513 {
514 needpass = false;
515 clearsign = false;
516 could_not_decrypt = false;
517 decrypt_okay_rc = 0;
518
519 if (mutt_str_startswith(buf + plen, "MESSAGE-----\n"))
520 {
521 needpass = 1;
522 }
523 else if (mutt_str_startswith(buf + plen, "SIGNED MESSAGE-----\n"))
524 {
525 clearsign = true;
526 }
527 else if (mutt_str_startswith(buf + plen, "PUBLIC KEY BLOCK-----\n"))
528 {
529 pgp_keyblock = true;
530 }
531 else
532 {
533 /* XXX we may wish to recode here */
534 if (state->prefix)
535 state_puts(state, state->prefix);
536 state_puts(state, buf);
537 continue;
538 }
539
540 have_any_sigs = have_any_sigs || (clearsign && (state->flags & STATE_VERIFY));
541
542 /* Copy PGP material to temporary file */
543 buf_mktemp(tmpfname);
544 fp_tmp = mutt_file_fopen(buf_string(tmpfname), "w+");
545 if (!fp_tmp)
546 {
547 mutt_perror("%s", buf_string(tmpfname));
548 FREE(&gpgcharset);
549 goto out;
550 }
551
552 fputs(buf, fp_tmp);
553 while ((bytes > 0) && fgets(buf, sizeof(buf) - 1, state->fp_in))
554 {
555 offset = ftello(state->fp_in);
556 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
557 last_pos = offset;
558
559 fputs(buf, fp_tmp);
560
561 if ((needpass && mutt_str_equal("-----END PGP MESSAGE-----\n", buf)) ||
562 (!needpass && (mutt_str_equal("-----END PGP SIGNATURE-----\n", buf) ||
563 mutt_str_equal("-----END PGP PUBLIC KEY BLOCK-----\n", buf))))
564 {
565 break;
566 }
567 /* remember optional Charset: armor header as defined by RFC4880 */
568 if (mutt_str_startswith(buf, "Charset: "))
569 {
570 size_t l = 0;
571 FREE(&gpgcharset);
572 gpgcharset = mutt_str_dup(buf + 9);
573 l = mutt_str_len(gpgcharset);
574 if ((l > 0) && (gpgcharset[l - 1] == '\n'))
575 gpgcharset[l - 1] = 0;
576 if (!mutt_ch_check_charset(gpgcharset, false))
577 mutt_str_replace(&gpgcharset, "UTF-8");
578 }
579 }
580
581 /* leave fp_tmp open in case we still need it - but flush it! */
582 fflush(fp_tmp);
583
584 /* Invoke PGP if needed */
585 if (!clearsign || (state->flags & STATE_VERIFY))
586 {
587 fp_pgp_out = mutt_file_mkstemp();
588 if (!fp_pgp_out)
589 {
590 mutt_perror(_("Can't create temporary file"));
591 goto out;
592 }
593
594 fp_pgp_err = mutt_file_mkstemp();
595 if (!fp_pgp_err)
596 {
597 mutt_perror(_("Can't create temporary file"));
598 goto out;
599 }
600
601 pid = pgp_invoke_decode(&fp_pgp_in, NULL, NULL, -1, fileno(fp_pgp_out),
602 fileno(fp_pgp_err), buf_string(tmpfname),
603 (needpass != 0));
604 if (pid == -1)
605 {
606 mutt_file_fclose(&fp_pgp_out);
607 maybe_goodsig = false;
608 fp_pgp_in = NULL;
609 state_attach_puts(state, _("[-- Error: unable to create PGP subprocess --]\n"));
610 }
611 else /* PGP started successfully */
612 {
613 if (needpass)
614 {
617 if (pgp_use_gpg_agent())
618 *PgpPass = '\0';
619 fprintf(fp_pgp_in, "%s\n", PgpPass);
620 }
621
622 mutt_file_fclose(&fp_pgp_in);
623
624 int wait_filter_rc = filter_wait(pid);
625
626 fflush(fp_pgp_err);
627 /* If we are expecting an encrypted message, verify status fd output.
628 * Note that BEGIN PGP MESSAGE does not guarantee the content is encrypted,
629 * so we need to be more selective about the value of decrypt_okay_rc.
630 *
631 * -3 indicates we actively found a DECRYPTION_FAILED.
632 * -2 and -1 indicate part or all of the content was plaintext. */
633 if (needpass)
634 {
635 rewind(fp_pgp_err);
636 decrypt_okay_rc = pgp_check_decryption_okay(fp_pgp_err);
637 if (decrypt_okay_rc <= -3)
638 mutt_file_fclose(&fp_pgp_out);
639 }
640
641 if (state->flags & STATE_DISPLAY)
642 {
643 rewind(fp_pgp_err);
644 crypt_current_time(state, "PGP");
645 int checksig_rc = pgp_copy_checksig(fp_pgp_err, state->fp_out);
646
647 if (checksig_rc == 0)
648 have_any_sigs = true;
649 /* Sig is bad if
650 * gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
651 * Sig _is_ correct if
652 * gpg_good_sign="" && pgp_decode_command returned 0 */
653 if (checksig_rc == -1 || (wait_filter_rc != 0))
654 maybe_goodsig = false;
655
656 state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
657 }
658 if (pgp_use_gpg_agent())
659 {
661 }
662 }
663
664 /* treat empty result as sign of failure */
665 /* TODO: maybe on failure neomutt should include the original undecoded text. */
666 if (fp_pgp_out)
667 {
668 rewind(fp_pgp_out);
669 c = fgetc(fp_pgp_out);
670 ungetc(c, fp_pgp_out);
671 }
672 if (!clearsign && (!fp_pgp_out || (c == EOF)))
673 {
674 could_not_decrypt = true;
676 }
677
678 if ((could_not_decrypt || (decrypt_okay_rc <= -3)) && !(state->flags & STATE_DISPLAY))
679 {
680 mutt_error(_("Could not decrypt PGP message"));
681 goto out;
682 }
683 }
684
685 /* Now, copy cleartext to the screen. */
686 if (state->flags & STATE_DISPLAY)
687 {
688 if (needpass)
689 state_attach_puts(state, _("[-- BEGIN PGP MESSAGE --]\n\n"));
690 else if (pgp_keyblock)
691 state_attach_puts(state, _("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"));
692 else
693 state_attach_puts(state, _("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"));
694 }
695
696 if (clearsign)
697 {
698 rewind(fp_tmp);
699 pgp_copy_clearsigned(fp_tmp, state, body_charset);
700 }
701 else if (fp_pgp_out)
702 {
703 struct FgetConv *fc = NULL;
704 int ch;
705 char *expected_charset = (gpgcharset && *gpgcharset) ? gpgcharset : "utf-8";
706
707 mutt_debug(LL_DEBUG3, "pgp: recoding inline from [%s] to [%s]\n",
708 expected_charset, cc_charset());
709
710 rewind(fp_pgp_out);
711 state_set_prefix(state);
712 fc = mutt_ch_fgetconv_open(fp_pgp_out, expected_charset, cc_charset(),
714 while ((ch = mutt_ch_fgetconv(fc)) != EOF)
715 state_prefix_putc(state, ch);
717 }
718
719 /* Multiple PGP blocks can exist, so these need to be closed and
720 * unlinked inside the loop. */
721 mutt_file_fclose(&fp_tmp);
722 mutt_file_unlink(buf_string(tmpfname));
723 mutt_file_fclose(&fp_pgp_out);
724 mutt_file_fclose(&fp_pgp_err);
725
726 if (state->flags & STATE_DISPLAY)
727 {
728 state_putc(state, '\n');
729 if (needpass)
730 {
731 state_attach_puts(state, _("[-- END PGP MESSAGE --]\n"));
732 if (could_not_decrypt || (decrypt_okay_rc <= -3))
733 {
734 mutt_error(_("Could not decrypt PGP message"));
735 }
736 else if (decrypt_okay_rc < 0)
737 {
738 /* L10N: You will see this error message if (1) you are decrypting
739 (not encrypting) something and (2) it is a plaintext. So the
740 message does not mean "You failed to encrypt the message." */
741 mutt_error(_("PGP message is not encrypted"));
742 }
743 else
744 {
745 mutt_message(_("PGP message successfully decrypted"));
746 }
747 }
748 else if (pgp_keyblock)
749 {
750 state_attach_puts(state, _("[-- END PGP PUBLIC KEY BLOCK --]\n"));
751 }
752 else
753 {
754 state_attach_puts(state, _("[-- END PGP SIGNED MESSAGE --]\n"));
755 }
756 }
757 }
758 else
759 {
760 /* A traditional PGP part may mix signed and unsigned content */
761 /* XXX we may wish to recode here */
762 if (state->prefix)
763 state_puts(state, state->prefix);
764 state_puts(state, buf);
765 }
766 }
767
768 rc = 0;
769
770out:
771 b->goodsig = (maybe_goodsig && have_any_sigs);
772
773 if (fp_tmp)
774 {
775 mutt_file_fclose(&fp_tmp);
776 mutt_file_unlink(buf_string(tmpfname));
777 }
778 mutt_file_fclose(&fp_pgp_out);
779 mutt_file_fclose(&fp_pgp_err);
780
781 buf_pool_release(&tmpfname);
782
783 FREE(&gpgcharset);
784
785 if (needpass == -1)
786 {
787 state_attach_puts(state, _("[-- Error: could not find beginning of PGP message --]\n\n"));
788 return -1;
789 }
790
791 return rc;
792}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
void crypt_current_time(struct State *state, const char *app_name)
Print the current time.
Definition: crypt.c:65
void mutt_need_hard_redraw(void)
Force a hard refresh.
Definition: curs_lib.c:100
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:220
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:146
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
bool pgp_class_valid_passphrase(void)
Ensure we have a valid passphrase - Implements CryptModuleSpecs::valid_passphrase() -.
Definition: pgp.c:85
void pgp_class_void_passphrase(void)
Forget the cached passphrase - Implements CryptModuleSpecs::void_passphrase() -.
Definition: pgp.c:76
#define mutt_error(...)
Definition: logging2.h:92
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
void state_prefix_putc(struct State *state, char c)
Write a prefixed character to the state.
Definition: state.c:164
#define state_set_prefix(state)
Definition: state.h:56
static char PgpPass[1024]
Cached PGP Passphrase.
Definition: pgp.c:69
static int pgp_copy_checksig(FILE *fp_in, FILE *fp_out)
Copy PGP output and look for signs of a good signature.
Definition: pgp.c:251
static void pgp_copy_clearsigned(FILE *fp_in, struct State *state, char *charset)
Copy a clearsigned message, stripping the signature.
Definition: pgp.c:424
bool pgp_use_gpg_agent(void)
Does the user want to use the gpg agent?
Definition: pgp.c:127
static int pgp_check_decryption_okay(FILE *fp_in)
Check GPG output for status codes.
Definition: pgp.c:357
pid_t pgp_invoke_decode(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *fname, bool need_passphrase)
Use PGP to decode a message.
Definition: pgpinvoke.c:200
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
String manipulation buffer.
Definition: buffer.h:36
FILE * fp_out
File to write to.
Definition: state.h:50
#define buf_mktemp(buf)
Definition: tmp.h:33
+ Here is the call graph for this function:

◆ smime_class_application_handler()

int smime_class_application_handler ( struct Body b,
struct State state 
)

Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::application_handler() -.

Definition at line 2119 of file smime.c.

2120{
2121 int rc = -1;
2122
2123 /* clear out any mime headers before the handler, so they can't be spoofed. */
2125
2126 struct Body *tattach = smime_handle_entity(b, state, NULL);
2127 if (tattach)
2128 {
2129 rc = 0;
2130 mutt_body_free(&tattach);
2131 }
2132 return rc;
2133}
static struct Body * smime_handle_entity(struct Body *b, struct State *state, FILE *fp_out_file)
Handle type application/pkcs7-mime.
Definition: smime.c:1818
+ Here is the call graph for this function: