forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgp.c
2077 lines (1801 loc) · 55.3 KB
/
pgp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* PGP sign, encrypt, check routines
*
* @authors
* Copyright (C) 1996-1997,2000,2010 Michael R. Elkins <[email protected]>
* Copyright (C) 1998-2005 Thomas Roessler <[email protected]>
* Copyright (C) 2004 g10 Code GmbH
* Copyright (C) 2019 Pietro Cerutti <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page crypt_pgp PGP sign, encrypt, check routines
*
* Code to sign, encrypt, verify and decrypt PGP messages.
*
* The code accepts messages in either the new PGP/MIME format, or in the older
* Application/Pgp format. It also contains some code to cache the user's
* passphrase for repeat use when decrypting or signing a message.
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "lib.h"
#include "attach/lib.h"
#include "enter/lib.h"
#include "history/lib.h"
#include "question/lib.h"
#include "send/lib.h"
#include "crypt.h"
#include "cryptglue.h"
#include "globals.h" // IWYU pragma: keep
#include "handler.h"
#include "hook.h"
#include "pgpinvoke.h"
#include "pgpkey.h"
#include "pgpmicalg.h"
#ifdef CRYPT_BACKEND_CLASSIC_PGP
#include "pgp.h"
#include "pgplib.h"
#endif
/// Cached PGP Passphrase
static char PgpPass[1024];
/// Unix time when #PgpPass expires
static time_t PgpExptime = 0; /* when does the cached passphrase expire? */
/**
* pgp_class_void_passphrase - Implements CryptModuleSpecs::void_passphrase() - @ingroup crypto_void_passphrase
*/
void pgp_class_void_passphrase(void)
{
memset(PgpPass, 0, sizeof(PgpPass));
PgpExptime = 0;
}
/**
* pgp_class_valid_passphrase - Implements CryptModuleSpecs::valid_passphrase() - @ingroup crypto_valid_passphrase
*/
bool pgp_class_valid_passphrase(void)
{
if (pgp_use_gpg_agent())
{
*PgpPass = '\0';
return true; /* handled by gpg-agent */
}
if (mutt_date_now() < PgpExptime)
{
/* Use cached copy. */
return true;
}
pgp_class_void_passphrase();
struct Buffer *buf = buf_pool_get();
const int rc = mw_get_field(_("Enter PGP passphrase:"), buf,
MUTT_COMP_PASS | MUTT_COMP_UNBUFFERED, HC_OTHER, NULL, NULL);
mutt_str_copy(PgpPass, buf_string(buf), sizeof(PgpPass));
buf_pool_release(&buf);
if (rc == 0)
{
const long c_pgp_timeout = cs_subset_long(NeoMutt->sub, "pgp_timeout");
PgpExptime = mutt_date_add_timeout(mutt_date_now(), c_pgp_timeout);
return true;
}
else
{
PgpExptime = 0;
}
return false;
}
/**
* pgp_use_gpg_agent - Does the user want to use the gpg agent?
* @retval true The user wants to use the gpg agent
*
* @note This functions sets the environment variable `$GPG_TTY`
*/
bool pgp_use_gpg_agent(void)
{
char *tty = NULL;
/* GnuPG 2.1 no longer exports GPG_AGENT_INFO */
const bool c_pgp_use_gpg_agent = cs_subset_bool(NeoMutt->sub, "pgp_use_gpg_agent");
if (!c_pgp_use_gpg_agent)
return false;
tty = ttyname(0);
if (tty)
{
setenv("GPG_TTY", tty, 0);
envlist_set(&EnvList, "GPG_TTY", tty, false);
}
return true;
}
/**
* key_parent - Find a key's parent (if it's a subkey)
* @param k PGP key
* @retval ptr Parent key
*/
static struct PgpKeyInfo *key_parent(struct PgpKeyInfo *k)
{
const bool c_pgp_ignore_subkeys = cs_subset_bool(NeoMutt->sub, "pgp_ignore_subkeys");
if ((k->flags & KEYFLAG_SUBKEY) && k->parent && c_pgp_ignore_subkeys)
k = k->parent;
return k;
}
/**
* pgp_long_keyid - Get a key's long id
* @param k PGP key
* @retval ptr Long key id string
*/
char *pgp_long_keyid(struct PgpKeyInfo *k)
{
k = key_parent(k);
return k->keyid;
}
/**
* pgp_short_keyid - Get a key's short id
* @param k PGP key
* @retval ptr Short key id string
*/
char *pgp_short_keyid(struct PgpKeyInfo *k)
{
k = key_parent(k);
return k->keyid + 8;
}
/**
* pgp_this_keyid - Get the ID of this key
* @param k PGP key
* @retval ptr Long/Short key id string
*
* @note The string returned depends on `$pgp_long_ids`
*/
char *pgp_this_keyid(struct PgpKeyInfo *k)
{
const bool c_pgp_long_ids = cs_subset_bool(NeoMutt->sub, "pgp_long_ids");
if (c_pgp_long_ids)
return k->keyid;
return k->keyid + 8;
}
/**
* pgp_keyid - Get the ID of the main (parent) key
* @param k PGP key
* @retval ptr Long/Short key id string
*/
char *pgp_keyid(struct PgpKeyInfo *k)
{
k = key_parent(k);
return pgp_this_keyid(k);
}
/**
* pgp_fingerprint - Get the key's fingerprint
* @param k PGP key
* @retval ptr Fingerprint string
*/
static char *pgp_fingerprint(struct PgpKeyInfo *k)
{
k = key_parent(k);
return k->fingerprint;
}
/**
* pgp_fpr_or_lkeyid - Get the fingerprint or long keyid
* @param k PGP key
* @retval ptr String fingerprint or long keyid
*
* Grab the longest key identifier available: fingerprint or else
* the long keyid.
*
* The longest available should be used for internally identifying
* the key and for invoking pgp commands.
*/
char *pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
{
char *fingerprint = pgp_fingerprint(k);
return fingerprint ? fingerprint : pgp_long_keyid(k);
}
/* ----------------------------------------------------------------------------
* Routines for handing PGP input.
*/
/**
* pgp_copy_checksig - Copy PGP output and look for signs of a good signature
* @param fp_in File to read from
* @param fp_out File to write to
* @retval 0 Success
* @retval -1 Error
*/
static int pgp_copy_checksig(FILE *fp_in, FILE *fp_out)
{
if (!fp_in || !fp_out)
return -1;
int rc = -1;
const struct Regex *c_pgp_good_sign = cs_subset_regex(NeoMutt->sub, "pgp_good_sign");
if (c_pgp_good_sign && c_pgp_good_sign->regex)
{
char *line = NULL;
size_t linelen;
while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NO_FLAGS)))
{
if (mutt_regex_match(c_pgp_good_sign, line))
{
mutt_debug(LL_DEBUG2, "\"%s\" matches regex\n", line);
rc = 0;
}
else
{
mutt_debug(LL_DEBUG2, "\"%s\" doesn't match regex\n", line);
}
if (mutt_strn_equal(line, "[GNUPG:] ", 9))
continue;
fputs(line, fp_out);
fputc('\n', fp_out);
}
FREE(&line);
}
else
{
mutt_debug(LL_DEBUG2, "No pattern\n");
mutt_file_copy_stream(fp_in, fp_out);
rc = 1;
}
return rc;
}
/**
* pgp_check_pgp_decryption_okay_regex - Check PGP output to look for successful outcome
* @param fp_in File to read from
* @retval 0 Success
* @retval -1 Error
*
* Checks PGP output messages to look for the $pgp_decryption_okay message.
* This protects against messages with multipart/encrypted headers but which
* aren't actually encrypted.
*/
static int pgp_check_pgp_decryption_okay_regex(FILE *fp_in)
{
int rc = -1;
const struct Regex *c_pgp_decryption_okay = cs_subset_regex(NeoMutt->sub, "pgp_decryption_okay");
if (c_pgp_decryption_okay && c_pgp_decryption_okay->regex)
{
char *line = NULL;
size_t linelen;
while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NO_FLAGS)))
{
if (mutt_regex_match(c_pgp_decryption_okay, line))
{
mutt_debug(LL_DEBUG2, "\"%s\" matches regex\n", line);
rc = 0;
break;
}
else
{
mutt_debug(LL_DEBUG2, "\"%s\" doesn't match regex\n", line);
}
}
FREE(&line);
}
else
{
mutt_debug(LL_DEBUG2, "No pattern\n");
rc = 1;
}
return rc;
}
/**
* pgp_check_decryption_okay - Check GPG output for status codes
* @param fp_in File to read from
* @retval 1 - no patterns were matched (if delegated to decryption_okay_regex)
* @retval 0 - DECRYPTION_OKAY was seen, with no PLAINTEXT outside
* @retval -1 - No decryption status codes were encountered
* @retval -2 - PLAINTEXT was encountered outside of DECRYPTION delimiters
* @retval -3 - DECRYPTION_FAILED was encountered
*
* Checks GnuPGP status fd output for various status codes indicating
* an issue. If $pgp_check_gpg_decrypt_status_fd is unset, it falls
* back to the old behavior of just scanning for $pgp_decryption_okay.
*
* pgp_decrypt_part() should fail if the part is not encrypted, so we return
* less than 0 to indicate part or all was NOT actually encrypted.
*
* On the other hand, for pgp_application_pgp_handler(), a
* "BEGIN PGP MESSAGE" could indicate a signed and armored message.
* For that we allow -1 and -2 as "valid" (with a warning).
*/
static int pgp_check_decryption_okay(FILE *fp_in)
{
int rc = -1;
char *line = NULL, *s = NULL;
size_t linelen;
int inside_decrypt = 0;
const bool c_pgp_check_gpg_decrypt_status_fd = cs_subset_bool(NeoMutt->sub, "pgp_check_gpg_decrypt_status_fd");
if (!c_pgp_check_gpg_decrypt_status_fd)
return pgp_check_pgp_decryption_okay_regex(fp_in);
while ((line = mutt_file_read_line(line, &linelen, fp_in, NULL, MUTT_RL_NO_FLAGS)))
{
size_t plen = mutt_str_startswith(line, "[GNUPG:] ");
if (plen == 0)
continue;
s = line + plen;
mutt_debug(LL_DEBUG2, "checking \"%s\"\n", line);
if (mutt_str_startswith(s, "BEGIN_DECRYPTION"))
{
inside_decrypt = 1;
}
else if (mutt_str_startswith(s, "END_DECRYPTION"))
{
inside_decrypt = 0;
}
else if (mutt_str_startswith(s, "PLAINTEXT"))
{
if (!inside_decrypt)
{
mutt_debug(LL_DEBUG2, " PLAINTEXT encountered outside of DECRYPTION\n");
rc = -2;
break;
}
}
else if (mutt_str_startswith(s, "DECRYPTION_FAILED"))
{
mutt_debug(LL_DEBUG2, " DECRYPTION_FAILED encountered. Failure\n");
rc = -3;
break;
}
else if (mutt_str_startswith(s, "DECRYPTION_OKAY"))
{
/* Don't break out because we still have to check for
* PLAINTEXT outside of the decryption boundaries. */
mutt_debug(LL_DEBUG2, " DECRYPTION_OKAY encountered\n");
rc = 0;
}
}
FREE(&line);
return rc;
}
/**
* pgp_copy_clearsigned - Copy a clearsigned message, stripping the signature
* @param fp_in File to read from
* @param state State to use
* @param charset Charset of file
*
* XXX charset handling: We assume that it is safe to do character set
* decoding first, dash decoding second here, while we do it the other way
* around in the main handler.
*
* (Note that we aren't worse than Outlook &c in this, and also note that we
* can successfully handle anything produced by any existing versions of neomutt.)
*/
static void pgp_copy_clearsigned(FILE *fp_in, struct State *state, char *charset)
{
char buf[8192] = { 0 };
bool complete, armor_header;
rewind(fp_in);
/* fromcode comes from the MIME Content-Type charset label. It might
* be a wrong label, so we want the ability to do corrections via
* charset-hooks. Therefore we set flags to MUTT_ICONV_HOOK_FROM. */
struct FgetConv *fc = mutt_ch_fgetconv_open(fp_in, charset, cc_charset(), MUTT_ICONV_HOOK_FROM);
for (complete = true, armor_header = true;
mutt_ch_fgetconvs(buf, sizeof(buf), fc); complete = (strchr(buf, '\n')))
{
if (!complete)
{
if (!armor_header)
state_puts(state, buf);
continue;
}
if (mutt_str_equal(buf, "-----BEGIN PGP SIGNATURE-----\n"))
break;
if (armor_header)
{
char *p = mutt_str_skip_whitespace(buf);
if (*p == '\0')
armor_header = false;
continue;
}
if (state->prefix)
state_puts(state, state->prefix);
if ((buf[0] == '-') && (buf[1] == ' '))
state_puts(state, buf + 2);
else
state_puts(state, buf);
}
mutt_ch_fgetconv_close(&fc);
}
/**
* pgp_class_application_handler - Implements CryptModuleSpecs::application_handler() - @ingroup crypto_application_handler
*/
int pgp_class_application_handler(struct Body *m, struct State *state)
{
bool could_not_decrypt = false;
int decrypt_okay_rc = 0;
int needpass = -1;
bool pgp_keyblock = false;
bool clearsign = false;
int rc = -1;
int c = 1;
long bytes;
LOFF_T last_pos, offset;
char buf[8192] = { 0 };
FILE *fp_pgp_out = NULL, *fp_pgp_in = NULL, *fp_pgp_err = NULL;
FILE *fp_tmp = NULL;
pid_t pid;
struct Buffer *tmpfname = buf_pool_get();
bool maybe_goodsig = true;
bool have_any_sigs = false;
char *gpgcharset = NULL;
char body_charset[256] = { 0 };
mutt_body_get_charset(m, body_charset, sizeof(body_charset));
if (!mutt_file_seek(state->fp_in, m->offset, SEEK_SET))
{
return -1;
}
last_pos = m->offset;
for (bytes = m->length; bytes > 0;)
{
if (!fgets(buf, sizeof(buf), state->fp_in))
break;
offset = ftello(state->fp_in);
bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
last_pos = offset;
size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
if (plen != 0)
{
clearsign = false;
could_not_decrypt = false;
decrypt_okay_rc = 0;
if (mutt_str_startswith(buf + plen, "MESSAGE-----\n"))
{
needpass = 1;
}
else if (mutt_str_startswith(buf + plen, "SIGNED MESSAGE-----\n"))
{
clearsign = true;
needpass = 0;
}
else if (mutt_str_startswith(buf + plen, "PUBLIC KEY BLOCK-----\n"))
{
needpass = 0;
pgp_keyblock = true;
}
else
{
/* XXX we may wish to recode here */
if (state->prefix)
state_puts(state, state->prefix);
state_puts(state, buf);
continue;
}
have_any_sigs = have_any_sigs || (clearsign && (state->flags & STATE_VERIFY));
/* Copy PGP material to temporary file */
buf_mktemp(tmpfname);
fp_tmp = mutt_file_fopen(buf_string(tmpfname), "w+");
if (!fp_tmp)
{
mutt_perror("%s", buf_string(tmpfname));
FREE(&gpgcharset);
goto out;
}
fputs(buf, fp_tmp);
while ((bytes > 0) && fgets(buf, sizeof(buf) - 1, state->fp_in))
{
offset = ftello(state->fp_in);
bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
last_pos = offset;
fputs(buf, fp_tmp);
if ((needpass && mutt_str_equal("-----END PGP MESSAGE-----\n", buf)) ||
(!needpass && (mutt_str_equal("-----END PGP SIGNATURE-----\n", buf) ||
mutt_str_equal("-----END PGP PUBLIC KEY BLOCK-----\n", buf))))
{
break;
}
/* remember optional Charset: armor header as defined by RFC4880 */
if (mutt_str_startswith(buf, "Charset: "))
{
size_t l = 0;
FREE(&gpgcharset);
gpgcharset = mutt_str_dup(buf + 9);
l = mutt_str_len(gpgcharset);
if ((l > 0) && (gpgcharset[l - 1] == '\n'))
gpgcharset[l - 1] = 0;
if (!mutt_ch_check_charset(gpgcharset, false))
mutt_str_replace(&gpgcharset, "UTF-8");
}
}
/* leave fp_tmp open in case we still need it - but flush it! */
fflush(fp_tmp);
/* Invoke PGP if needed */
if (!clearsign || (state->flags & STATE_VERIFY))
{
fp_pgp_out = mutt_file_mkstemp();
if (!fp_pgp_out)
{
mutt_perror(_("Can't create temporary file"));
goto out;
}
fp_pgp_err = mutt_file_mkstemp();
if (!fp_pgp_err)
{
mutt_perror(_("Can't create temporary file"));
goto out;
}
pid = pgp_invoke_decode(&fp_pgp_in, NULL, NULL, -1, fileno(fp_pgp_out),
fileno(fp_pgp_err), buf_string(tmpfname),
(needpass != 0));
if (pid == -1)
{
mutt_file_fclose(&fp_pgp_out);
maybe_goodsig = false;
fp_pgp_in = NULL;
state_attach_puts(state, _("[-- Error: unable to create PGP subprocess --]\n"));
}
else /* PGP started successfully */
{
if (needpass)
{
if (!pgp_class_valid_passphrase())
pgp_class_void_passphrase();
if (pgp_use_gpg_agent())
*PgpPass = '\0';
fprintf(fp_pgp_in, "%s\n", PgpPass);
}
mutt_file_fclose(&fp_pgp_in);
int wait_filter_rc = filter_wait(pid);
fflush(fp_pgp_err);
/* If we are expecting an encrypted message, verify status fd output.
* Note that BEGIN PGP MESSAGE does not guarantee the content is encrypted,
* so we need to be more selective about the value of decrypt_okay_rc.
*
* -3 indicates we actively found a DECRYPTION_FAILED.
* -2 and -1 indicate part or all of the content was plaintext. */
if (needpass)
{
rewind(fp_pgp_err);
decrypt_okay_rc = pgp_check_decryption_okay(fp_pgp_err);
if (decrypt_okay_rc <= -3)
mutt_file_fclose(&fp_pgp_out);
}
if (state->flags & STATE_DISPLAY)
{
rewind(fp_pgp_err);
crypt_current_time(state, "PGP");
int checksig_rc = pgp_copy_checksig(fp_pgp_err, state->fp_out);
if (checksig_rc == 0)
have_any_sigs = true;
/* Sig is bad if
* gpg_good_sign-pattern did not match || pgp_decode_command returned not 0
* Sig _is_ correct if
* gpg_good_sign="" && pgp_decode_command returned 0 */
if (checksig_rc == -1 || (wait_filter_rc != 0))
maybe_goodsig = false;
state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
}
if (pgp_use_gpg_agent())
{
mutt_need_hard_redraw();
}
}
/* treat empty result as sign of failure */
/* TODO: maybe on failure neomutt should include the original undecoded text. */
if (fp_pgp_out)
{
rewind(fp_pgp_out);
c = fgetc(fp_pgp_out);
ungetc(c, fp_pgp_out);
}
if (!clearsign && (!fp_pgp_out || (c == EOF)))
{
could_not_decrypt = true;
pgp_class_void_passphrase();
}
if ((could_not_decrypt || (decrypt_okay_rc <= -3)) && !(state->flags & STATE_DISPLAY))
{
mutt_error(_("Could not decrypt PGP message"));
goto out;
}
}
/* Now, copy cleartext to the screen. */
if (state->flags & STATE_DISPLAY)
{
if (needpass)
state_attach_puts(state, _("[-- BEGIN PGP MESSAGE --]\n\n"));
else if (pgp_keyblock)
state_attach_puts(state, _("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"));
else
state_attach_puts(state, _("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"));
}
if (clearsign)
{
rewind(fp_tmp);
pgp_copy_clearsigned(fp_tmp, state, body_charset);
}
else if (fp_pgp_out)
{
struct FgetConv *fc = NULL;
int ch;
char *expected_charset = (gpgcharset && *gpgcharset) ? gpgcharset : "utf-8";
mutt_debug(LL_DEBUG3, "pgp: recoding inline from [%s] to [%s]\n",
expected_charset, cc_charset());
rewind(fp_pgp_out);
state_set_prefix(state);
fc = mutt_ch_fgetconv_open(fp_pgp_out, expected_charset, cc_charset(),
MUTT_ICONV_HOOK_FROM);
while ((ch = mutt_ch_fgetconv(fc)) != EOF)
state_prefix_putc(state, ch);
mutt_ch_fgetconv_close(&fc);
}
/* Multiple PGP blocks can exist, so these need to be closed and
* unlinked inside the loop. */
mutt_file_fclose(&fp_tmp);
mutt_file_unlink(buf_string(tmpfname));
mutt_file_fclose(&fp_pgp_out);
mutt_file_fclose(&fp_pgp_err);
if (state->flags & STATE_DISPLAY)
{
state_putc(state, '\n');
if (needpass)
{
state_attach_puts(state, _("[-- END PGP MESSAGE --]\n"));
if (could_not_decrypt || (decrypt_okay_rc <= -3))
{
mutt_error(_("Could not decrypt PGP message"));
}
else if (decrypt_okay_rc < 0)
{
/* L10N: You will see this error message if (1) you are decrypting
(not encrypting) something and (2) it is a plaintext. So the
message does not mean "You failed to encrypt the message." */
mutt_error(_("PGP message is not encrypted"));
}
else
{
mutt_message(_("PGP message successfully decrypted"));
}
}
else if (pgp_keyblock)
{
state_attach_puts(state, _("[-- END PGP PUBLIC KEY BLOCK --]\n"));
}
else
{
state_attach_puts(state, _("[-- END PGP SIGNED MESSAGE --]\n"));
}
}
}
else
{
/* A traditional PGP part may mix signed and unsigned content */
/* XXX we may wish to recode here */
if (state->prefix)
state_puts(state, state->prefix);
state_puts(state, buf);
}
}
rc = 0;
out:
m->goodsig = (maybe_goodsig && have_any_sigs);
if (fp_tmp)
{
mutt_file_fclose(&fp_tmp);
mutt_file_unlink(buf_string(tmpfname));
}
mutt_file_fclose(&fp_pgp_out);
mutt_file_fclose(&fp_pgp_err);
buf_pool_release(&tmpfname);
FREE(&gpgcharset);
if (needpass == -1)
{
state_attach_puts(state, _("[-- Error: could not find beginning of PGP message --]\n\n"));
return -1;
}
return rc;
}
/**
* pgp_check_traditional_one_body - Check the body of an inline PGP message
* @param fp File to read
* @param b Body to populate
* @retval true Success
* @retval false Error
*/
static bool pgp_check_traditional_one_body(FILE *fp, struct Body *b)
{
struct Buffer *tempfile = NULL;
char buf[8192] = { 0 };
bool rc = false;
bool sgn = false;
bool enc = false;
bool key = false;
if (b->type != TYPE_TEXT)
goto cleanup;
tempfile = buf_pool_get();
buf_mktemp(tempfile);
if (mutt_decode_save_attachment(fp, b, buf_string(tempfile), STATE_NO_FLAGS,
MUTT_SAVE_NO_FLAGS) != 0)
{
unlink(buf_string(tempfile));
goto cleanup;
}
FILE *fp_tmp = fopen(buf_string(tempfile), "r");
if (!fp_tmp)
{
unlink(buf_string(tempfile));
goto cleanup;
}
while (fgets(buf, sizeof(buf), fp_tmp))
{
size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
if (plen != 0)
{
if (mutt_str_startswith(buf + plen, "MESSAGE-----\n"))
enc = true;
else if (mutt_str_startswith(buf + plen, "SIGNED MESSAGE-----\n"))
sgn = true;
else if (mutt_str_startswith(buf + plen, "PUBLIC KEY BLOCK-----\n"))
key = true;
}
}
mutt_file_fclose(&fp_tmp);
unlink(buf_string(tempfile));
if (!enc && !sgn && !key)
goto cleanup;
/* fix the content type */
mutt_param_set(&b->parameter, "format", "fixed");
if (enc)
mutt_param_set(&b->parameter, "x-action", "pgp-encrypted");
else if (sgn)
mutt_param_set(&b->parameter, "x-action", "pgp-signed");
else if (key)
mutt_param_set(&b->parameter, "x-action", "pgp-keys");
rc = true;
cleanup:
buf_pool_release(&tempfile);
return rc;
}
/**
* pgp_class_check_traditional - Implements CryptModuleSpecs::pgp_check_traditional() - @ingroup crypto_pgp_check_traditional
*/
bool pgp_class_check_traditional(FILE *fp, struct Body *b, bool just_one)
{
bool rc = false;
int r;
for (; b; b = b->next)
{
if (!just_one && is_multipart(b))
{
rc = pgp_class_check_traditional(fp, b->parts, false) || rc;
}
else if (b->type == TYPE_TEXT)
{
r = mutt_is_application_pgp(b);
if (r)
rc = rc || r;
else
rc = pgp_check_traditional_one_body(fp, b) || rc;
}
if (just_one)
break;
}
return rc;
}
/**
* pgp_class_verify_one - Implements CryptModuleSpecs::verify_one() - @ingroup crypto_verify_one
*/
int pgp_class_verify_one(struct Body *sigbdy, struct State *state, const char *tempfile)
{
FILE *fp_pgp_out = NULL;
pid_t pid;
int badsig = -1;
struct Buffer *sigfile = buf_pool_get();
buf_printf(sigfile, "%s.asc", tempfile);
FILE *fp_sig = mutt_file_fopen(buf_string(sigfile), "w");
if (!fp_sig)
{
mutt_perror("%s", buf_string(sigfile));
goto cleanup;
}
if (!mutt_file_seek(state->fp_in, sigbdy->offset, SEEK_SET))
{
mutt_file_fclose(&fp_sig);
goto cleanup;
}
mutt_file_copy_bytes(state->fp_in, fp_sig, sigbdy->length);
mutt_file_fclose(&fp_sig);
FILE *fp_pgp_err = mutt_file_mkstemp();
if (!fp_pgp_err)
{
mutt_perror(_("Can't create temporary file"));
unlink(buf_string(sigfile));
goto cleanup;
}
crypt_current_time(state, "PGP");
pid = pgp_invoke_verify(NULL, &fp_pgp_out, NULL, -1, -1, fileno(fp_pgp_err),
tempfile, buf_string(sigfile));
if (pid != -1)
{
if (pgp_copy_checksig(fp_pgp_out, state->fp_out) >= 0)
badsig = 0;
mutt_file_fclose(&fp_pgp_out);
fflush(fp_pgp_err);
rewind(fp_pgp_err);
if (pgp_copy_checksig(fp_pgp_err, state->fp_out) >= 0)
badsig = 0;
const int rv = filter_wait(pid);
if (rv)
badsig = -1;
mutt_debug(LL_DEBUG1, "filter_wait returned %d\n", rv);
}
mutt_file_fclose(&fp_pgp_err);
state_attach_puts(state, _("[-- End of PGP output --]\n\n"));
mutt_file_unlink(buf_string(sigfile));
cleanup:
buf_pool_release(&sigfile);
mutt_debug(LL_DEBUG1, "returning %d\n", badsig);
return badsig;
}
/**
* pgp_extract_keys_from_attachment - Extract pgp keys from messages/attachments
* @param fp File to read from
* @param top Top Attachment
*/
static void pgp_extract_keys_from_attachment(FILE *fp, struct Body *top)
{
struct State state = { 0 };
struct Buffer *tempfname = buf_pool_get();
buf_mktemp(tempfname);
FILE *fp_tmp = mutt_file_fopen(buf_string(tempfname), "w");
if (!fp_tmp)
{
mutt_perror("%s", buf_string(tempfname));
goto cleanup;
}
state.fp_in = fp;
state.fp_out = fp_tmp;
mutt_body_handler(top, &state);
mutt_file_fclose(&fp_tmp);
pgp_class_invoke_import(buf_string(tempfname));
mutt_any_key_to_continue(NULL);
mutt_file_unlink(buf_string(tempfname));
cleanup:
buf_pool_release(&tempfname);
}