2497{
2498 int needpass = -1;
2499 bool pgp_keyblock = false;
2500 bool clearsign = false;
2501 long bytes;
2502 LOFF_T last_pos;
2503 LOFF_T block_begin;
2504 LOFF_T block_end;
2505 char buf[8192] = { 0 };
2506 FILE *fp_out = NULL;
2507
2508 gpgme_error_t err = GPG_ERR_NO_ERROR;
2509 gpgme_data_t armored_data = NULL;
2510
2511 bool maybe_goodsig = true;
2512 bool have_any_sigs = false;
2513
2514 char body_charset[256] = { 0 };
2515 char *gpgcharset = NULL;
2516
2518
2519
2520
2522 mutt_str_copy(body_charset,
"iso-8859-1",
sizeof(body_charset));
2523
2525 {
2526 return -1;
2527 }
2529
2530 for (bytes = b->
length; bytes > 0;)
2531 {
2532
2533 block_begin = last_pos;
2534
2535 if (!fgets(buf,
sizeof(buf), state->
fp_in))
2536 break;
2537
2538 LOFF_T offset = ftello(state->
fp_in);
2539 if (offset < 0)
2540 {
2542 offset = 0;
2543 }
2544 bytes -= (offset - last_pos);
2545 last_pos = offset;
2546
2548 if (plen != 0)
2549 {
2550 needpass = 0;
2551 clearsign = false;
2552 pgp_keyblock = false;
2553
2555 {
2556 needpass = 1;
2557 }
2559 {
2560 clearsign = true;
2561 }
2563 {
2564 pgp_keyblock = true;
2565 }
2566 else
2567 {
2568
2572 continue;
2573 }
2574
2575
2576 while ((bytes > 0) && (fgets(buf,
sizeof(buf) - 1, state->
fp_in) != NULL))
2577 {
2578 offset = ftello(state->
fp_in);
2579 if (offset < 0)
2580 {
2582 offset = 0;
2583 }
2584 bytes -= (offset - last_pos);
2585 last_pos = offset;
2586
2587 if (needpass &&
mutt_str_equal(
"-----END PGP MESSAGE-----\n", buf))
2588 {
2589 break;
2590 }
2591
2592 if (!needpass && (
mutt_str_equal(
"-----END PGP SIGNATURE-----\n", buf) ||
2594 {
2595 break;
2596 }
2597
2598
2600 {
2601 size_t l = 0;
2604 if ((l =
mutt_str_len(gpgcharset)) > 0 && gpgcharset[l - 1] ==
'\n')
2605 gpgcharset[l - 1] = 0;
2608 }
2609 }
2610 block_end = ftello(state->
fp_in);
2611 if (block_end < 0)
2612 {
2614 block_end = 0;
2615 }
2616
2617 have_any_sigs = (have_any_sigs || (clearsign && (state->
flags &
STATE_VERIFY)));
2618
2619
2621 fseeko(state->
fp_in, block_end, 0);
2622
2623
2624 if (pgp_keyblock)
2625 {
2627 }
2629 {
2632
2633 if (clearsign)
2634 {
2635 err = gpgme_op_verify(ctx, armored_data, NULL, plaintext);
2636 }
2637 else
2638 {
2639 err = gpgme_op_decrypt_verify(ctx, armored_data, plaintext);
2640 if (gpg_err_code(err) == GPG_ERR_NO_DATA)
2641 {
2642
2643 gpgme_data_seek(armored_data, 0, SEEK_SET);
2644
2645 gpgme_data_release(plaintext);
2647 err = gpgme_op_verify(ctx, armored_data, NULL, plaintext);
2648 }
2649 }
2651
2652 gpgme_decrypt_result_t result = gpgme_op_decrypt_result(ctx);
2655
2656 if (err != GPG_ERR_NO_ERROR)
2657 {
2658 char errbuf[200] = { 0 };
2659
2660 snprintf(errbuf, sizeof(errbuf) - 1,
2661 _(
"Error: decryption/verification failed: %s\n"), gpgme_strerror(err));
2663 }
2664 else
2665 {
2666
2667
2669
2670 bool sig_stat = false;
2671 char *tmpfname = NULL;
2672
2673
2674 gpgme_verify_result_t verify_result = gpgme_op_verify_result(ctx);
2675 if (verify_result->signatures)
2676 sig_stat = true;
2677
2678 have_any_sigs = false;
2679 maybe_goodsig = false;
2681 {
2682 int res;
2683 int idx;
2684 bool anybad = false;
2685
2687 have_any_sigs = true;
2689 {
2690 if (res == 1)
2691 anybad = true;
2692 }
2693 if (!anybad && idx)
2694 maybe_goodsig = true;
2695
2697 }
2698
2700 if (tmpfname)
2701 {
2702 unlink(tmpfname);
2704 }
2705 else
2706 {
2708 state_puts(state,
_(
"Error: copy data failed\n"));
2709 }
2710 }
2711 gpgme_data_release(plaintext);
2712 gpgme_release(ctx);
2713 }
2714
2715
2716
2717
2719 {
2720 if (needpass)
2722 else if (pgp_keyblock)
2724 else
2726 }
2727
2728 if (clearsign)
2729 {
2731 }
2732 else if (fp_out)
2733 {
2734 int c;
2735 char *expected_charset = gpgcharset && *gpgcharset ? gpgcharset : "utf-8";
2736 fseek(fp_out, 0, SEEK_SET);
2737 clearerr(fp_out);
2741 {
2743 if ((c ==
'\n') && state->
prefix)
2745 }
2747 }
2748
2750 {
2752 if (needpass)
2754 else if (pgp_keyblock)
2756 else
2758 }
2759
2760
2761 gpgme_data_release(armored_data);
2763 }
2764 else
2765 {
2766
2767
2771 }
2772 }
2774
2775 b->
goodsig = (maybe_goodsig && have_any_sigs);
2776
2777 if (needpass == -1)
2778 {
2779 state_attach_puts(state,
_(
"[-- Error: could not find beginning of PGP message --]\n\n"));
2780 return 1;
2781 }
2783
2784 return err;
2785}
const char * cc_charset(void)
Get the cached value of $charset.
static void show_encryption_info(struct State *state, gpgme_decrypt_result_t result)
Show encryption information.
static gpgme_data_t create_gpgme_data(void)
Create a new GPGME data object.
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
#define SIGNED_MESSAGE(_y)
static gpgme_data_t file_to_data_object(FILE *fp, long offset, size_t length)
Create GPGME data object from file.
#define PUBLIC_KEY_BLOCK(_y)
static char * data_object_to_tempfile(gpgme_data_t data, FILE **fp_ret)
Copy a data object to a temporary file.
static void redraw_if_needed(gpgme_ctx_t ctx)
Accommodate for a redraw if needed.
static int pgp_gpgme_extract_keys(gpgme_data_t keydata, FILE **fp)
Write PGP keys to a file.
static void copy_clearsigned(gpgme_data_t data, struct State *state, char *charset)
Copy a clearsigned message.
static int show_one_sig_status(gpgme_ctx_t ctx, int idx, struct State *state)
Show information about one signature.
char * mutt_body_get_charset(struct Body *b, char *buf, size_t buflen)
Get a body's character set.
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
#define mutt_file_fclose(FP)
#define mutt_message(...)
#define mutt_debug(LEVEL,...)
@ LL_DEBUG2
Log at debug level 2.
@ LL_DEBUG1
Log at debug level 1.
#define FREE(x)
Free memory and set the pointer to NULL.
bool mutt_ch_check_charset(const char *cs, bool strict)
Does iconv understand a character set?
int mutt_ch_fgetconv(struct FgetConv *fc)
Convert a file's character set.
struct FgetConv * mutt_ch_fgetconv_open(FILE *fp, const char *from, const char *to, uint8_t flags)
Prepare a file for charset conversion.
void mutt_ch_fgetconv_close(struct FgetConv **ptr)
Close an fgetconv handle.
#define MUTT_ICONV_HOOK_FROM
apply charset-hooks to fromcode
void state_attach_puts(struct State *state, const char *t)
Write a string to the state.
#define state_puts(STATE, STR)
#define state_putc(STATE, STR)
@ STATE_VERIFY
Perform signature verification.
@ STATE_DISPLAY
Output is displayed to the user.
char * mutt_str_dup(const char *str)
Copy a string, safely.
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination).
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
LOFF_T offset
offset where the actual data begins
LOFF_T length
length (in bytes) of attachment
bool goodsig
Good cryptographic signature.
Cursor for converting a file's encoding.
StateFlags flags
Flags, e.g. STATE_DISPLAY.
FILE * fp_in
File to read from.
const char * prefix
String to add to the beginning of each output line.