NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
crypt_gpgme.c
Go to the documentation of this file.
1
35#include "config.h"
36#include <errno.h>
37#include <gpg-error.h>
38#include <gpgme.h>
39#include <langinfo.h>
40#include <locale.h>
41#include <stdbool.h>
42#include <stdio.h>
43#include <string.h>
44#include <time.h>
45#include <unistd.h>
46#include "private.h"
47#include "mutt/lib.h"
48#include "address/lib.h"
49#include "config/lib.h"
50#include "email/lib.h"
51#include "core/lib.h"
52#include "alias/lib.h"
53#include "gui/lib.h"
54#include "mutt.h"
55#include "crypt_gpgme.h"
56#include "lib.h"
57#include "attach/lib.h"
58#include "editor/lib.h"
59#include "history/lib.h"
60#include "question/lib.h"
61#include "send/lib.h"
62#include "crypt.h"
63#include "globals.h"
64#include "gpgme_functions.h"
65#include "handler.h"
66#include "hook.h"
67#include "mutt_logging.h"
68#ifdef USE_AUTOCRYPT
69#include "autocrypt/lib.h"
70#endif
71
72// clang-format off
73/* Values used for comparing addresses. */
74#define CRYPT_KV_VALID (1 << 0)
75#define CRYPT_KV_ADDR (1 << 1)
76#define CRYPT_KV_STRING (1 << 2)
77#define CRYPT_KV_STRONGID (1 << 3)
78#define CRYPT_KV_MATCH (CRYPT_KV_ADDR | CRYPT_KV_STRING)
79// clang-format on
80
85{
86 char *what;
87 char *dflt;
89};
90
92static struct CryptCache *IdDefaults = NULL;
94static gpgme_key_t SignatureKey = NULL;
96static char *CurrentSender = NULL;
97
98#define PKA_NOTATION_NAME "pka-address@gnupg.org"
99
100#define _LINE_COMPARE(_x, _y) line_compare(_x, sizeof(_x) - 1, _y)
101#define MESSAGE(_y) _LINE_COMPARE("MESSAGE-----", _y)
102#define SIGNED_MESSAGE(_y) _LINE_COMPARE("SIGNED MESSAGE-----", _y)
103#define PUBLIC_KEY_BLOCK(_y) _LINE_COMPARE("PUBLIC KEY BLOCK-----", _y)
104#define BEGIN_PGP_SIGNATURE(_y) \
105 _LINE_COMPARE("-----BEGIN PGP SIGNATURE-----", _y)
106
112static bool is_pka_notation(gpgme_sig_notation_t notation)
113{
114 return mutt_str_equal(notation->name, PKA_NOTATION_NAME);
115}
116
121static void redraw_if_needed(gpgme_ctx_t ctx)
122{
123 const char *s = gpgme_get_ctx_flag(ctx, "redraw");
124 if (!s /* flag not known */ || *s /* flag true */)
125 {
127 }
128}
129
138const char *crypt_keyid(struct CryptKeyInfo *k)
139{
140 const char *s = "????????";
141
142 if (k->kobj && k->kobj->subkeys)
143 {
144 s = k->kobj->subkeys->keyid;
145 const bool c_pgp_long_ids = cs_subset_bool(NeoMutt->sub, "pgp_long_ids");
146 if ((!c_pgp_long_ids) && (strlen(s) == 16))
147 {
148 /* Return only the short keyID. */
149 s += 8;
150 }
151 }
152
153 return s;
154}
155
163static const char *crypt_long_keyid(struct CryptKeyInfo *k)
164{
165 const char *s = "????????????????";
166
167 if (k->kobj && k->kobj->subkeys)
168 {
169 s = k->kobj->subkeys->keyid;
170 }
171
172 return s;
173}
174
180static const char *crypt_short_keyid(struct CryptKeyInfo *k)
181{
182 const char *s = "????????";
183
184 if (k->kobj && k->kobj->subkeys)
185 {
186 s = k->kobj->subkeys->keyid;
187 if (strlen(s) == 16)
188 s += 8;
189 }
190
191 return s;
192}
193
199static const char *crypt_fpr(struct CryptKeyInfo *k)
200{
201 const char *s = "";
202
203 if (k->kobj && k->kobj->subkeys)
204 s = k->kobj->subkeys->fpr;
205
206 return s;
207}
208
214const char *crypt_fpr_or_lkeyid(struct CryptKeyInfo *k)
215{
216 const char *s = "????????????????";
217
218 if (k->kobj && k->kobj->subkeys)
219 {
220 if (k->kobj->subkeys->fpr)
221 s = k->kobj->subkeys->fpr;
222 else
223 s = k->kobj->subkeys->keyid;
224 }
225
226 return s;
227}
228
235{
236 struct CryptKeyInfo *k = NULL;
237
238 k = mutt_mem_calloc(1, sizeof(*k));
239 k->kobj = key->kobj;
240 gpgme_key_ref(key->kobj);
241 k->idx = key->idx;
242 k->uid = key->uid;
243 k->flags = key->flags;
244 k->validity = key->validity;
245
246 return k;
247}
248
253static void crypt_key_free(struct CryptKeyInfo **keylist)
254{
255 if (!keylist)
256 return;
257
258 struct CryptKeyInfo *k = NULL;
259
260 while (*keylist)
261 {
262 k = *keylist;
263 *keylist = (*keylist)->next;
264
265 gpgme_key_unref(k->kobj);
266 FREE(&k);
267 }
268}
269
276{
277 if (!key)
278 return false;
279
280 bool is_strong = false;
281
282 if ((key->flags & KEYFLAG_ISX509))
283 return true;
284
285 switch (key->validity)
286 {
287 case GPGME_VALIDITY_MARGINAL:
288 case GPGME_VALIDITY_NEVER:
289 case GPGME_VALIDITY_UNDEFINED:
290 case GPGME_VALIDITY_UNKNOWN:
291 is_strong = false;
292 break;
293
294 case GPGME_VALIDITY_FULL:
295 case GPGME_VALIDITY_ULTIMATE:
296 is_strong = true;
297 break;
298 }
299
300 return is_strong;
301}
302
311{
312 if (!key)
313 return 0;
314
315 return !(key->flags & KEYFLAG_CANTUSE);
316}
317
328static int crypt_id_matches_addr(struct Address *addr, struct Address *u_addr,
329 struct CryptKeyInfo *key)
330{
331 int rc = 0;
332
333 if (crypt_id_is_valid(key))
334 rc |= CRYPT_KV_VALID;
335
336 if (crypt_id_is_strong(key))
337 rc |= CRYPT_KV_STRONGID;
338
339 if (addr && u_addr)
340 {
341 if (addr->mailbox && u_addr->mailbox && buf_istr_equal(addr->mailbox, u_addr->mailbox))
342 {
343 rc |= CRYPT_KV_ADDR;
344 }
345
346 if (addr->personal && u_addr->personal &&
347 buf_istr_equal(addr->personal, u_addr->personal))
348 {
349 rc |= CRYPT_KV_STRING;
350 }
351 }
352
353 return rc;
354}
355
361gpgme_ctx_t create_gpgme_context(bool for_smime)
362{
363 gpgme_ctx_t ctx = NULL;
364
365 gpgme_error_t err = gpgme_new(&ctx);
366
367#ifdef USE_AUTOCRYPT
368 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
369 if ((err == GPG_ERR_NO_ERROR) && OptAutocryptGpgme)
370 err = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_OpenPGP, NULL, c_autocrypt_dir);
371#endif
372
373 if (err != GPG_ERR_NO_ERROR)
374 {
375 mutt_error(_("error creating GPGME context: %s"), gpgme_strerror(err));
376 mutt_exit(1);
377 }
378
379 if (for_smime)
380 {
381 err = gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
382 if (err != GPG_ERR_NO_ERROR)
383 {
384 mutt_error(_("error enabling CMS protocol: %s"), gpgme_strerror(err));
385 mutt_exit(1);
386 }
387 }
388
389 return ctx;
390}
391
400static gpgme_data_t create_gpgme_data(void)
401{
402 gpgme_data_t data = NULL;
403
404 gpgme_error_t err = gpgme_data_new(&data);
405 if (err != GPG_ERR_NO_ERROR)
406 {
407 mutt_error(_("error creating GPGME data object: %s"), gpgme_strerror(err));
408 mutt_exit(1);
409 }
410 return data;
411}
412
419static gpgme_data_t body_to_data_object(struct Body *b, bool convert)
420{
421 gpgme_error_t err = GPG_ERR_NO_ERROR;
422 gpgme_data_t data = NULL;
423
424 struct Buffer *tempfile = buf_pool_get();
425 buf_mktemp(tempfile);
426 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "w+");
427 if (!fp_tmp)
428 {
429 mutt_perror("%s", buf_string(tempfile));
430 goto cleanup;
431 }
432
434 fputc('\n', fp_tmp);
435 mutt_write_mime_body(b, fp_tmp, NeoMutt->sub);
436
437 if (convert)
438 {
439 int c, hadcr = 0;
440 unsigned char buf[1];
441
443 rewind(fp_tmp);
444 while ((c = fgetc(fp_tmp)) != EOF)
445 {
446 if (c == '\r')
447 {
448 hadcr = 1;
449 }
450 else
451 {
452 if ((c == '\n') && !hadcr)
453 {
454 buf[0] = '\r';
455 gpgme_data_write(data, buf, 1);
456 }
457
458 hadcr = 0;
459 }
460 /* FIXME: This is quite suboptimal */
461 buf[0] = c;
462 gpgme_data_write(data, buf, 1);
463 }
464 mutt_file_fclose(&fp_tmp);
465 gpgme_data_seek(data, 0, SEEK_SET);
466 }
467 else
468 {
469 mutt_file_fclose(&fp_tmp);
470 err = gpgme_data_new_from_file(&data, buf_string(tempfile), 1);
471 if (err != GPG_ERR_NO_ERROR)
472 {
473 mutt_error(_("error allocating data object: %s"), gpgme_strerror(err));
474 gpgme_data_release(data);
475 data = NULL;
476 /* fall through to unlink the tempfile */
477 }
478 }
479 unlink(buf_string(tempfile));
480
481cleanup:
482 buf_pool_release(&tempfile);
483 return data;
484}
485
493static gpgme_data_t file_to_data_object(FILE *fp, long offset, size_t length)
494{
495 gpgme_data_t data = NULL;
496
497 gpgme_error_t err = gpgme_data_new_from_filepart(&data, NULL, fp, offset, length);
498 if (err != GPG_ERR_NO_ERROR)
499 {
500 mutt_error(_("error allocating data object: %s"), gpgme_strerror(err));
501 return NULL;
502 }
503
504 return data;
505}
506
514static int data_object_to_stream(gpgme_data_t data, FILE *fp)
515{
516 char buf[4096] = { 0 };
517 ssize_t nread;
518
519 gpgme_error_t err = ((gpgme_data_seek(data, 0, SEEK_SET) == -1) ?
520 gpgme_error_from_errno(errno) :
521 GPG_ERR_NO_ERROR);
522 if (err != GPG_ERR_NO_ERROR)
523 {
524 mutt_error(_("error rewinding data object: %s"), gpgme_strerror(err));
525 return -1;
526 }
527
528 while ((nread = gpgme_data_read(data, buf, sizeof(buf))) > 0)
529 {
530 /* fixme: we are not really converting CRLF to LF but just
531 * skipping CR. Doing it correctly needs a more complex logic */
532 for (char *p = buf; nread; p++, nread--)
533 {
534 if (*p != '\r')
535 putc(*p, fp);
536 }
537
538 if (ferror(fp))
539 {
540 mutt_perror(_("[tempfile]"));
541 return -1;
542 }
543 }
544 if (nread == -1)
545 {
546 mutt_error(_("error reading data object: %s"), strerror(errno));
547 return -1;
548 }
549 return 0;
550}
551
563static char *data_object_to_tempfile(gpgme_data_t data, FILE **fp_ret)
564{
565 ssize_t nread = 0;
566 char *rv = NULL;
567 struct Buffer *tempf = buf_pool_get();
568
569 buf_mktemp(tempf);
570
571 FILE *fp = mutt_file_fopen(buf_string(tempf), "w+");
572 if (!fp)
573 {
574 mutt_perror(_("Can't create temporary file"));
575 goto cleanup;
576 }
577
578 gpgme_error_t err = ((gpgme_data_seek(data, 0, SEEK_SET) == -1) ?
579 gpgme_error_from_errno(errno) :
580 GPG_ERR_NO_ERROR);
581 if (err == GPG_ERR_NO_ERROR)
582 {
583 char buf[4096] = { 0 };
584
585 while ((nread = gpgme_data_read(data, buf, sizeof(buf))) > 0)
586 {
587 if (fwrite(buf, nread, 1, fp) != 1)
588 {
589 mutt_perror("%s", buf_string(tempf));
590 mutt_file_fclose(&fp);
591 unlink(buf_string(tempf));
592 goto cleanup;
593 }
594 }
595 }
596 if (fp_ret)
597 rewind(fp);
598 else
599 mutt_file_fclose(&fp);
600 if (nread == -1)
601 {
602 mutt_error(_("error reading data object: %s"), gpgme_strerror(err));
603 unlink(buf_string(tempf));
604 mutt_file_fclose(&fp);
605 goto cleanup;
606 }
607 if (fp_ret)
608 *fp_ret = fp;
609 rv = buf_strdup(tempf);
610
611cleanup:
612 buf_pool_release(&tempf);
613 return rv;
614}
615
622static void create_recipient_string(const char *keylist, struct Buffer *recpstring, int use_smime)
623{
624 unsigned int n = 0;
625
626 const char *s = keylist;
627 do
628 {
629 while (*s == ' ')
630 s++;
631 if (*s != '\0')
632 {
633 if (n == 0)
634 {
635 if (!use_smime)
636 buf_addstr(recpstring, "--\n");
637 }
638 else
639 {
640 buf_addch(recpstring, '\n');
641 }
642 n++;
643
644 while ((*s != '\0') && (*s != ' '))
645 buf_addch(recpstring, *s++);
646 }
647 } while (*s != '\0');
648}
649
658static bool set_signer_from_address(gpgme_ctx_t ctx, const char *address, bool for_smime)
659{
660 gpgme_key_t key = NULL;
661 gpgme_key_t key2 = NULL;
662
663 gpgme_ctx_t listctx = create_gpgme_context(for_smime);
664 gpgme_error_t err = gpgme_op_keylist_start(listctx, address, 1);
665 if (err == GPG_ERR_NO_ERROR)
666 err = gpgme_op_keylist_next(listctx, &key);
667 if (err != GPG_ERR_NO_ERROR)
668 {
669 gpgme_release(listctx);
670 mutt_error(_("secret key '%s' not found: %s"), address, gpgme_strerror(err));
671 return false;
672 }
673
674 char *fpr = "fpr1";
675 if (key->subkeys)
676 fpr = key->subkeys->fpr ? key->subkeys->fpr : key->subkeys->keyid;
677 while (gpgme_op_keylist_next(listctx, &key2) == 0)
678 {
679 char *fpr2 = "fpr2";
680 if (key2->subkeys)
681 fpr2 = key2->subkeys->fpr ? key2->subkeys->fpr : key2->subkeys->keyid;
682 if (!mutt_str_equal(fpr, fpr2))
683 {
684 gpgme_key_unref(key);
685 gpgme_key_unref(key2);
686 gpgme_release(listctx);
687 mutt_error(_("ambiguous specification of secret key '%s'"), address);
688 return false;
689 }
690 else
691 {
692 gpgme_key_unref(key2);
693 }
694 }
695 gpgme_op_keylist_end(listctx);
696 gpgme_release(listctx);
697
698 gpgme_signers_clear(ctx);
699 err = gpgme_signers_add(ctx, key);
700 gpgme_key_unref(key);
701 if (err != GPG_ERR_NO_ERROR)
702 {
703 mutt_error(_("error setting secret key '%s': %s"), address, gpgme_strerror(err));
704 return false;
705 }
706 return true;
707}
708
717static int set_signer(gpgme_ctx_t ctx, const struct AddressList *al, bool for_smime)
718{
719 const char *signid = NULL;
720
721 const char *const c_smime_sign_as = cs_subset_string(NeoMutt->sub, "smime_sign_as");
722 const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
723 const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
724 const char *const c_smime_default_key = cs_subset_string(NeoMutt->sub, "smime_default_key");
725 if (for_smime)
726 signid = c_smime_sign_as ? c_smime_sign_as : c_smime_default_key;
727#ifdef USE_AUTOCRYPT
728 else if (OptAutocryptGpgme)
729 signid = AutocryptSignAs;
730#endif
731 else
732 signid = c_pgp_sign_as ? c_pgp_sign_as : c_pgp_default_key;
733
734 /* Try getting the signing key from config entries */
735 if (signid && set_signer_from_address(ctx, signid, for_smime))
736 {
737 return 0;
738 }
739
740 /* Try getting the signing key from the From line */
741 if (al)
742 {
743 struct Address *a;
744 TAILQ_FOREACH(a, al, entries)
745 {
746 if (a->mailbox && set_signer_from_address(ctx, buf_string(a->mailbox), for_smime))
747 {
748 return 0;
749 }
750 }
751 }
752
753 return (!signid && !al) ? 0 : -1;
754}
755
761static gpgme_error_t set_pka_sig_notation(gpgme_ctx_t ctx)
762{
763 gpgme_error_t err = gpgme_sig_notation_add(ctx, PKA_NOTATION_NAME, CurrentSender, 0);
764 if (err != GPG_ERR_NO_ERROR)
765 {
766 mutt_error(_("error setting PKA signature notation: %s"), gpgme_strerror(err));
767 }
768
769 return err;
770}
771
781static char *encrypt_gpgme_object(gpgme_data_t plaintext, char *keylist, bool use_smime,
782 bool combined_signed, const struct AddressList *from)
783{
784 gpgme_error_t err = GPG_ERR_NO_ERROR;
785 gpgme_ctx_t ctx = NULL;
786 gpgme_data_t ciphertext = NULL;
787 char *outfile = NULL;
788
789 struct Buffer *recpstring = buf_pool_get();
790 create_recipient_string(keylist, recpstring, use_smime);
791 if (buf_is_empty(recpstring))
792 {
793 buf_pool_release(&recpstring);
794 return NULL;
795 }
796
797 ctx = create_gpgme_context(use_smime);
798 if (!use_smime)
799 gpgme_set_armor(ctx, 1);
800
801 ciphertext = create_gpgme_data();
802
803 if (combined_signed)
804 {
805 if (set_signer(ctx, from, use_smime))
806 goto cleanup;
807
808 const bool c_crypt_use_pka = cs_subset_bool(NeoMutt->sub, "crypt_use_pka");
809 if (c_crypt_use_pka)
810 {
811 err = set_pka_sig_notation(ctx);
812 if (err != GPG_ERR_NO_ERROR)
813 goto cleanup;
814 }
815
816 err = gpgme_op_encrypt_sign_ext(ctx, NULL, buf_string(recpstring),
817 GPGME_ENCRYPT_ALWAYS_TRUST, plaintext, ciphertext);
818 }
819 else
820 {
821 err = gpgme_op_encrypt_ext(ctx, NULL, buf_string(recpstring),
822 GPGME_ENCRYPT_ALWAYS_TRUST, plaintext, ciphertext);
823 }
824
825 redraw_if_needed(ctx);
826 if (err != GPG_ERR_NO_ERROR)
827 {
828 mutt_error(_("error encrypting data: %s"), gpgme_strerror(err));
829 goto cleanup;
830 }
831
832 outfile = data_object_to_tempfile(ciphertext, NULL);
833
834cleanup:
835 buf_pool_release(&recpstring);
836 gpgme_release(ctx);
837 gpgme_data_release(ciphertext);
838 return outfile;
839}
840
853static int get_micalg(gpgme_ctx_t ctx, int use_smime, char *buf, size_t buflen)
854{
855 gpgme_sign_result_t result = NULL;
856 const char *algorithm_name = NULL;
857
858 if (buflen < 5)
859 return -1;
860
861 *buf = '\0';
862 result = gpgme_op_sign_result(ctx);
863 if (result && result->signatures)
864 {
865 algorithm_name = gpgme_hash_algo_name(result->signatures->hash_algo);
866 if (algorithm_name)
867 {
868 if (use_smime)
869 {
870 /* convert GPGME raw hash name to RFC2633 format */
871 snprintf(buf, buflen, "%s", algorithm_name);
872 mutt_str_lower(buf);
873 }
874 else
875 {
876 /* convert GPGME raw hash name to RFC3156 format */
877 snprintf(buf, buflen, "pgp-%s", algorithm_name);
878 mutt_str_lower(buf + 4);
879 }
880 }
881 }
882
883 return (buf[0] != '\0') ? 0 : -1;
884}
885
891static void print_time(time_t t, struct State *state)
892{
893 char p[256] = { 0 };
894 mutt_date_localtime_format(p, sizeof(p), nl_langinfo(D_T_FMT), t);
895 state_puts(state, p);
896}
897
906static struct Body *sign_message(struct Body *b, const struct AddressList *from, bool use_smime)
907{
908 struct Body *b_sign = NULL;
909 char *sigfile = NULL;
910 gpgme_error_t err = GPG_ERR_NO_ERROR;
911 char buf[100] = { 0 };
912 gpgme_ctx_t ctx = NULL;
913 gpgme_data_t message = NULL, signature = NULL;
914 gpgme_sign_result_t sigres = NULL;
915
916 crypt_convert_to_7bit(b); /* Signed data _must_ be in 7-bit format. */
917
918 message = body_to_data_object(b, true);
919 if (!message)
920 return NULL;
921 signature = create_gpgme_data();
922
923 ctx = create_gpgme_context(use_smime);
924 if (!use_smime)
925 gpgme_set_armor(ctx, 1);
926
927 if (set_signer(ctx, from, use_smime))
928 {
929 gpgme_data_release(signature);
930 gpgme_data_release(message);
931 gpgme_release(ctx);
932 return NULL;
933 }
934
935 const bool c_crypt_use_pka = cs_subset_bool(NeoMutt->sub, "crypt_use_pka");
936 if (c_crypt_use_pka)
937 {
938 err = set_pka_sig_notation(ctx);
939 if (err != GPG_ERR_NO_ERROR)
940 {
941 gpgme_data_release(signature);
942 gpgme_data_release(message);
943 gpgme_release(ctx);
944 return NULL;
945 }
946 }
947
948 err = gpgme_op_sign(ctx, message, signature, GPGME_SIG_MODE_DETACH);
949 redraw_if_needed(ctx);
950 gpgme_data_release(message);
951 if (err != GPG_ERR_NO_ERROR)
952 {
953 gpgme_data_release(signature);
954 gpgme_release(ctx);
955 mutt_error(_("error signing data: %s"), gpgme_strerror(err));
956 return NULL;
957 }
958 /* Check for zero signatures generated. This can occur when $pgp_sign_as is
959 * unset and there is no default key specified in ~/.gnupg/gpg.conf */
960 sigres = gpgme_op_sign_result(ctx);
961 if (!sigres->signatures)
962 {
963 gpgme_data_release(signature);
964 gpgme_release(ctx);
965 mutt_error(_("$pgp_sign_as unset and no default key specified in ~/.gnupg/gpg.conf"));
966 return NULL;
967 }
968
969 sigfile = data_object_to_tempfile(signature, NULL);
970 gpgme_data_release(signature);
971 if (!sigfile)
972 {
973 gpgme_release(ctx);
974 return NULL;
975 }
976
977 b_sign = mutt_body_new();
978 b_sign->type = TYPE_MULTIPART;
979 b_sign->subtype = mutt_str_dup("signed");
980 b_sign->encoding = ENC_7BIT;
981 b_sign->use_disp = false;
982 b_sign->disposition = DISP_INLINE;
983
985 mutt_param_set(&b_sign->parameter, "protocol",
986 use_smime ? "application/pkcs7-signature" : "application/pgp-signature");
987 /* Get the micalg from GPGME. Old gpgme versions don't support this
988 * for S/MIME so we assume sha-1 in this case. */
989 if (get_micalg(ctx, use_smime, buf, sizeof(buf)) == 0)
990 mutt_param_set(&b_sign->parameter, "micalg", buf);
991 else if (use_smime)
992 mutt_param_set(&b_sign->parameter, "micalg", "sha1");
993 gpgme_release(ctx);
994
995 b_sign->parts = b;
996 b = b_sign;
997
998 b_sign->parts->next = mutt_body_new();
999 b_sign = b_sign->parts->next;
1000 b_sign->type = TYPE_APPLICATION;
1001 if (use_smime)
1002 {
1003 b_sign->subtype = mutt_str_dup("pkcs7-signature");
1004 mutt_param_set(&b_sign->parameter, "name", "smime.p7s");
1005 b_sign->encoding = ENC_BASE64;
1006 b_sign->use_disp = true;
1007 b_sign->disposition = DISP_ATTACH;
1008 b_sign->d_filename = mutt_str_dup("smime.p7s");
1009 }
1010 else
1011 {
1012 b_sign->subtype = mutt_str_dup("pgp-signature");
1013 mutt_param_set(&b_sign->parameter, "name", "signature.asc");
1014 b_sign->use_disp = false;
1015 b_sign->disposition = DISP_NONE;
1016 b_sign->encoding = ENC_7BIT;
1017 }
1018 b_sign->filename = sigfile;
1019 b_sign->unlink = true; /* ok to remove this file after sending. */
1020
1021 return b;
1022}
1023
1027struct Body *pgp_gpgme_sign_message(struct Body *b, const struct AddressList *from)
1028{
1029 return sign_message(b, from, false);
1030}
1031
1035struct Body *smime_gpgme_sign_message(struct Body *b, const struct AddressList *from)
1036{
1037 return sign_message(b, from, true);
1038}
1039
1043struct Body *pgp_gpgme_encrypt_message(struct Body *b, char *keylist, bool sign,
1044 const struct AddressList *from)
1045{
1046 if (sign)
1048 gpgme_data_t plaintext = body_to_data_object(b, false);
1049 if (!plaintext)
1050 return NULL;
1051
1052 char *outfile = encrypt_gpgme_object(plaintext, keylist, false, sign, from);
1053 gpgme_data_release(plaintext);
1054 if (!outfile)
1055 return NULL;
1056
1057 struct Body *b_enc = mutt_body_new();
1058 b_enc->type = TYPE_MULTIPART;
1059 b_enc->subtype = mutt_str_dup("encrypted");
1060 b_enc->encoding = ENC_7BIT;
1061 b_enc->use_disp = false;
1062 b_enc->disposition = DISP_INLINE;
1063
1065 mutt_param_set(&b_enc->parameter, "protocol", "application/pgp-encrypted");
1066
1067 b_enc->parts = mutt_body_new();
1068 b_enc->parts->type = TYPE_APPLICATION;
1069 b_enc->parts->subtype = mutt_str_dup("pgp-encrypted");
1070 b_enc->parts->encoding = ENC_7BIT;
1071
1072 b_enc->parts->next = mutt_body_new();
1073 b_enc->parts->next->type = TYPE_APPLICATION;
1074 b_enc->parts->next->subtype = mutt_str_dup("octet-stream");
1075 b_enc->parts->next->encoding = ENC_7BIT;
1076 b_enc->parts->next->filename = outfile;
1077 b_enc->parts->next->use_disp = true;
1078 b_enc->parts->next->disposition = DISP_ATTACH;
1079 b_enc->parts->next->unlink = true; /* delete after sending the message */
1080 b_enc->parts->next->d_filename = mutt_str_dup("msg.asc"); /* non pgp/mime
1081 can save */
1082
1083 return b_enc;
1084}
1085
1089struct Body *smime_gpgme_build_smime_entity(struct Body *b, char *keylist)
1090{
1091 /* OpenSSL converts line endings to crlf when encrypting. Some clients
1092 * depend on this for signed+encrypted messages: they do not convert line
1093 * endings between decrypting and checking the signature. */
1094 gpgme_data_t plaintext = body_to_data_object(b, true);
1095 if (!plaintext)
1096 return NULL;
1097
1098 char *outfile = encrypt_gpgme_object(plaintext, keylist, true, false, NULL);
1099 gpgme_data_release(plaintext);
1100 if (!outfile)
1101 return NULL;
1102
1103 struct Body *b_enc = mutt_body_new();
1104 b_enc->type = TYPE_APPLICATION;
1105 b_enc->subtype = mutt_str_dup("pkcs7-mime");
1106 mutt_param_set(&b_enc->parameter, "name", "smime.p7m");
1107 mutt_param_set(&b_enc->parameter, "smime-type", "enveloped-data");
1108 b_enc->encoding = ENC_BASE64; /* The output of OpenSSL SHOULD be binary */
1109 b_enc->use_disp = true;
1110 b_enc->disposition = DISP_ATTACH;
1111 b_enc->d_filename = mutt_str_dup("smime.p7m");
1112 b_enc->filename = outfile;
1113 b_enc->unlink = true; /* delete after sending the message */
1114 b_enc->parts = 0;
1115 b_enc->next = 0;
1116
1117 return b_enc;
1118}
1119
1133static int show_sig_summary(unsigned long sum, gpgme_ctx_t ctx, gpgme_key_t key,
1134 int idx, struct State *state, gpgme_signature_t sig)
1135{
1136 if (!key)
1137 return 1;
1138
1139 bool severe = false;
1140
1141 if ((sum & GPGME_SIGSUM_KEY_REVOKED))
1142 {
1143 state_puts(state, _("Warning: One of the keys has been revoked\n"));
1144 severe = true;
1145 }
1146
1147 if ((sum & GPGME_SIGSUM_KEY_EXPIRED))
1148 {
1149 time_t at = key->subkeys->expires ? key->subkeys->expires : 0;
1150 if (at)
1151 {
1152 state_puts(state, _("Warning: The key used to create the signature expired at: "));
1153 print_time(at, state);
1154 state_puts(state, "\n");
1155 }
1156 else
1157 {
1158 state_puts(state, _("Warning: At least one certification key has expired\n"));
1159 }
1160 }
1161
1162 if ((sum & GPGME_SIGSUM_SIG_EXPIRED))
1163 {
1164 gpgme_signature_t sig2 = NULL;
1165 unsigned int i;
1166
1167 gpgme_verify_result_t result = gpgme_op_verify_result(ctx);
1168
1169 for (sig2 = result->signatures, i = 0; sig2 && (i < idx); sig2 = sig2->next, i++)
1170 ; // do nothing
1171
1172 state_puts(state, _("Warning: The signature expired at: "));
1173 print_time(sig2 ? sig2->exp_timestamp : 0, state);
1174 state_puts(state, "\n");
1175 }
1176
1177 if ((sum & GPGME_SIGSUM_KEY_MISSING))
1178 {
1179 state_puts(state, _("Can't verify due to a missing key or certificate\n"));
1180 }
1181
1182 if ((sum & GPGME_SIGSUM_CRL_MISSING))
1183 {
1184 state_puts(state, _("The CRL is not available\n"));
1185 severe = true;
1186 }
1187
1188 if ((sum & GPGME_SIGSUM_CRL_TOO_OLD))
1189 {
1190 state_puts(state, _("Available CRL is too old\n"));
1191 severe = true;
1192 }
1193
1194 if ((sum & GPGME_SIGSUM_BAD_POLICY))
1195 state_puts(state, _("A policy requirement was not met\n"));
1196
1197 if ((sum & GPGME_SIGSUM_SYS_ERROR))
1198 {
1199 const char *t0 = NULL, *t1 = NULL;
1200 gpgme_verify_result_t result = NULL;
1201 gpgme_signature_t sig2 = NULL;
1202 unsigned int i;
1203
1204 state_puts(state, _("A system error occurred"));
1205
1206 /* Try to figure out some more detailed system error information. */
1207 result = gpgme_op_verify_result(ctx);
1208 for (sig2 = result->signatures, i = 0; sig2 && (i < idx); sig2 = sig2->next, i++)
1209 ; // do nothing
1210
1211 if (sig2)
1212 {
1213 t0 = "";
1214 t1 = sig2->wrong_key_usage ? "Wrong_Key_Usage" : "";
1215 }
1216
1217 if (t0 || t1)
1218 {
1219 state_puts(state, ": ");
1220 if (t0)
1221 state_puts(state, t0);
1222 if (t1 && !(t0 && (mutt_str_equal(t0, t1))))
1223 {
1224 if (t0)
1225 state_puts(state, ",");
1226 state_puts(state, t1);
1227 }
1228 }
1229 state_puts(state, "\n");
1230 }
1231
1232 const bool c_crypt_use_pka = cs_subset_bool(NeoMutt->sub, "crypt_use_pka");
1233 if (c_crypt_use_pka)
1234 {
1235 if ((sig->pka_trust == 1) && sig->pka_address)
1236 {
1237 state_puts(state, _("WARNING: PKA entry does not match signer's address: "));
1238 state_puts(state, sig->pka_address);
1239 state_puts(state, "\n");
1240 }
1241 else if ((sig->pka_trust == 2) && sig->pka_address)
1242 {
1243 state_puts(state, _("PKA verified signer's address is: "));
1244 state_puts(state, sig->pka_address);
1245 state_puts(state, "\n");
1246 }
1247 }
1248
1249 return severe;
1250}
1251
1257static void show_fingerprint(gpgme_key_t key, struct State *state)
1258{
1259 if (!key)
1260 return;
1261
1262 const char *prefix = _("Fingerprint: ");
1263
1264 const char *s = key->subkeys ? key->subkeys->fpr : NULL;
1265 if (!s)
1266 return;
1267 bool is_pgp = (key->protocol == GPGME_PROTOCOL_OpenPGP);
1268
1269 char *buf = mutt_mem_malloc(strlen(prefix) + strlen(s) * 4 + 2);
1270 strcpy(buf, prefix);
1271 char *p = buf + strlen(buf);
1272 if (is_pgp && (strlen(s) == 40))
1273 { /* PGP v4 style formatted. */
1274 for (int i = 0; *s && s[1] && s[2] && s[3] && s[4]; s += 4, i++)
1275 {
1276 *p++ = s[0];
1277 *p++ = s[1];
1278 *p++ = s[2];
1279 *p++ = s[3];
1280 *p++ = ' ';
1281 if (i == 4)
1282 *p++ = ' ';
1283 }
1284 }
1285 else
1286 {
1287 for (int i = 0; *s && s[1] && s[2]; s += 2, i++)
1288 {
1289 *p++ = s[0];
1290 *p++ = s[1];
1291 *p++ = is_pgp ? ' ' : ':';
1292 if (is_pgp && (i == 7))
1293 *p++ = ' ';
1294 }
1295 }
1296
1297 /* just in case print remaining odd digits */
1298 for (; *s; s++)
1299 *p++ = *s;
1300 *p++ = '\n';
1301 *p = '\0';
1302 state_puts(state, buf);
1303 FREE(&buf);
1304}
1305
1312static void show_one_sig_validity(gpgme_ctx_t ctx, int idx, struct State *state)
1313{
1314 gpgme_signature_t sig = NULL;
1315 const char *txt = NULL;
1316
1317 gpgme_verify_result_t result = gpgme_op_verify_result(ctx);
1318 if (result)
1319 for (sig = result->signatures; sig && (idx > 0); sig = sig->next, idx--)
1320 ; // do nothing
1321
1322 switch (sig ? sig->validity : 0)
1323 {
1324 case GPGME_VALIDITY_UNKNOWN:
1325 txt = _("WARNING: We have NO indication whether the key belongs to the person named as shown above\n");
1326 break;
1327 case GPGME_VALIDITY_UNDEFINED:
1328 break;
1329 case GPGME_VALIDITY_NEVER:
1330 txt = _("WARNING: The key does NOT BELONG to the person named as shown above\n");
1331 break;
1332 case GPGME_VALIDITY_MARGINAL:
1333 txt = _("WARNING: It is NOT certain that the key belongs to the person named as shown above\n");
1334 break;
1335 case GPGME_VALIDITY_FULL:
1336 case GPGME_VALIDITY_ULTIMATE:
1337 txt = NULL;
1338 break;
1339 }
1340 if (txt)
1341 state_puts(state, txt);
1342}
1343
1351static void print_smime_keyinfo(const char *msg, gpgme_signature_t sig,
1352 gpgme_key_t key, struct State *state)
1353{
1354 int msgwid;
1355
1356 state_puts(state, msg);
1357 state_puts(state, " ");
1358 /* key is NULL when not present in the user's keyring */
1359 if (key)
1360 {
1361 bool aka = false;
1362 for (gpgme_user_id_t uids = key->uids; uids; uids = uids->next)
1363 {
1364 if (uids->revoked)
1365 continue;
1366 if (aka)
1367 {
1368 msgwid = mutt_strwidth(msg) - mutt_strwidth(_("aka: ")) + 1;
1369 if (msgwid < 0)
1370 msgwid = 0;
1371 for (int i = 0; i < msgwid; i++)
1372 state_puts(state, " ");
1373 state_puts(state, _("aka: "));
1374 }
1375 state_puts(state, uids->uid);
1376 state_puts(state, "\n");
1377
1378 aka = true;
1379 }
1380 }
1381 else
1382 {
1383 if (sig->fpr)
1384 {
1385 state_puts(state, _("KeyID "));
1386 state_puts(state, sig->fpr);
1387 }
1388 else
1389 {
1390 /* L10N: You will see this message in place of "KeyID "
1391 if the S/MIME key has no ID. This is quite an error. */
1392 state_puts(state, _("no signature fingerprint available"));
1393 }
1394 state_puts(state, "\n");
1395 }
1396
1397 /* timestamp is 0 when verification failed.
1398 * "Jan 1 1970" is not the created date. */
1399 if (sig->timestamp)
1400 {
1401 msgwid = mutt_strwidth(msg) - mutt_strwidth(_("created: ")) + 1;
1402 if (msgwid < 0)
1403 msgwid = 0;
1404 for (int i = 0; i < msgwid; i++)
1405 state_puts(state, " ");
1406 state_puts(state, _("created: "));
1407 print_time(sig->timestamp, state);
1408 state_puts(state, "\n");
1409 }
1410}
1411
1424static int show_one_sig_status(gpgme_ctx_t ctx, int idx, struct State *state)
1425{
1426 const char *fpr = NULL;
1427 gpgme_key_t key = NULL;
1428 bool anybad = false, anywarn = false;
1429 gpgme_signature_t sig = NULL;
1430 gpgme_error_t err = GPG_ERR_NO_ERROR;
1431
1432 gpgme_verify_result_t result = gpgme_op_verify_result(ctx);
1433 if (result)
1434 {
1435 /* FIXME: this code should use a static variable and remember
1436 * the current position in the list of signatures, IMHO.
1437 * -moritz. */
1438 int i;
1439 for (i = 0, sig = result->signatures; sig && (i < idx); i++, sig = sig->next)
1440 ; // do nothing
1441
1442 if (!sig)
1443 return -1; /* Signature not found. */
1444
1445 if (SignatureKey)
1446 {
1447 gpgme_key_unref(SignatureKey);
1448 SignatureKey = NULL;
1449 }
1450
1451 fpr = sig->fpr;
1452 const unsigned int sum = sig->summary;
1453
1454 if (gpg_err_code(sig->status) != GPG_ERR_NO_ERROR)
1455 anybad = true;
1456
1457 if (gpg_err_code(sig->status) != GPG_ERR_NO_PUBKEY)
1458 {
1459 err = gpgme_get_key(ctx, fpr, &key, 0); /* secret key? */
1460 if (err == GPG_ERR_NO_ERROR)
1461 {
1462 if (!SignatureKey)
1463 SignatureKey = key;
1464 }
1465 else
1466 {
1467 key = NULL; /* Old GPGME versions did not set KEY to NULL on
1468 error. Do it here to avoid a double free. */
1469 }
1470 }
1471 else
1472 {
1473 /* pubkey not present */
1474 }
1475
1476 if (!state || !state->fp_out || !(state->flags & STATE_DISPLAY))
1477 {
1478 ; /* No state information so no way to print anything. */
1479 }
1480 else if (err != GPG_ERR_NO_ERROR)
1481 {
1482 char buf[1024] = { 0 };
1483 snprintf(buf, sizeof(buf), _("Error getting key information for KeyID %s: %s\n"),
1484 fpr, gpgme_strerror(err));
1485 state_puts(state, buf);
1486 anybad = true;
1487 }
1488 else if ((sum & GPGME_SIGSUM_GREEN))
1489 {
1490 print_smime_keyinfo(_("Good signature from:"), sig, key, state);
1491 if (show_sig_summary(sum, ctx, key, idx, state, sig))
1492 anywarn = true;
1493 show_one_sig_validity(ctx, idx, state);
1494 }
1495 else if ((sum & GPGME_SIGSUM_RED))
1496 {
1497 print_smime_keyinfo(_("*BAD* signature from:"), sig, key, state);
1498 show_sig_summary(sum, ctx, key, idx, state, sig);
1499 }
1500 else if (!anybad && key && (key->protocol == GPGME_PROTOCOL_OpenPGP))
1501 { /* We can't decide (yellow) but this is a PGP key with a good
1502 signature, so we display what a PGP user expects: The name,
1503 fingerprint and the key validity (which is neither fully or
1504 ultimate). */
1505 print_smime_keyinfo(_("Good signature from:"), sig, key, state);
1506 show_one_sig_validity(ctx, idx, state);
1507 show_fingerprint(key, state);
1508 if (show_sig_summary(sum, ctx, key, idx, state, sig))
1509 anywarn = true;
1510 }
1511 else /* can't decide (yellow) */
1512 {
1513 print_smime_keyinfo(_("Problem signature from:"), sig, key, state);
1514 /* 0 indicates no expiration */
1515 if (sig->exp_timestamp)
1516 {
1517 /* L10N: This is trying to match the width of the
1518 "Problem signature from:" translation just above. */
1519 state_puts(state, _(" expires: "));
1520 print_time(sig->exp_timestamp, state);
1521 state_puts(state, "\n");
1522 }
1523 show_sig_summary(sum, ctx, key, idx, state, sig);
1524 anywarn = true;
1525 }
1526
1527 if (key != SignatureKey)
1528 gpgme_key_unref(key);
1529 }
1530
1531 return anybad ? 1 : anywarn ? 2 : 0;
1532}
1533
1547static int verify_one(struct Body *b, struct State *state, const char *tempfile, bool is_smime)
1548{
1549 int badsig = -1;
1550 int anywarn = 0;
1551 gpgme_ctx_t ctx = NULL;
1552 gpgme_data_t message = NULL;
1553
1554 gpgme_data_t signature = file_to_data_object(state->fp_in, b->offset, b->length);
1555 if (!signature)
1556 return -1;
1557
1558 /* We need to tell GPGME about the encoding because the backend can't
1559 * auto-detect plain base-64 encoding which is used by S/MIME. */
1560 if (is_smime)
1561 gpgme_data_set_encoding(signature, GPGME_DATA_ENCODING_BASE64);
1562
1563 gpgme_error_t err = gpgme_data_new_from_file(&message, tempfile, 1);
1564 if (err != GPG_ERR_NO_ERROR)
1565 {
1566 gpgme_data_release(signature);
1567 mutt_error(_("error allocating data object: %s"), gpgme_strerror(err));
1568 return -1;
1569 }
1570 ctx = create_gpgme_context(is_smime);
1571
1572 /* Note: We don't need a current time output because GPGME avoids
1573 * such an attack by separating the meta information from the data. */
1574 state_attach_puts(state, _("[-- Begin signature information --]\n"));
1575
1576 err = gpgme_op_verify(ctx, signature, message, NULL);
1577 gpgme_data_release(message);
1578 gpgme_data_release(signature);
1579
1580 redraw_if_needed(ctx);
1581 if (err != GPG_ERR_NO_ERROR)
1582 {
1583 char buf[200] = { 0 };
1584
1585 snprintf(buf, sizeof(buf) - 1, _("Error: verification failed: %s\n"),
1586 gpgme_strerror(err));
1587 state_puts(state, buf);
1588 }
1589 else
1590 { /* Verification succeeded, see what the result is. */
1591 gpgme_verify_result_t verify_result = NULL;
1592
1593 if (SignatureKey)
1594 {
1595 gpgme_key_unref(SignatureKey);
1596 SignatureKey = NULL;
1597 }
1598
1599 verify_result = gpgme_op_verify_result(ctx);
1600 if (verify_result && verify_result->signatures)
1601 {
1602 bool anybad = false;
1603 int res;
1604 for (int idx = 0; (res = show_one_sig_status(ctx, idx, state)) != -1; idx++)
1605 {
1606 if (res == 1)
1607 anybad = true;
1608 else if (res == 2)
1609 anywarn = 2;
1610 }
1611 if (!anybad)
1612 badsig = 0;
1613 }
1614 }
1615
1616 if (badsig == 0)
1617 {
1618 gpgme_verify_result_t result = NULL;
1619 gpgme_sig_notation_t notation = NULL;
1620 gpgme_signature_t sig = NULL;
1621
1622 result = gpgme_op_verify_result(ctx);
1623 if (result)
1624 {
1625 for (sig = result->signatures; sig; sig = sig->next)
1626 {
1627 int non_pka_notations = 0;
1628 for (notation = sig->notations; notation; notation = notation->next)
1629 if (!is_pka_notation(notation))
1630 non_pka_notations++;
1631
1632 if (non_pka_notations)
1633 {
1634 char buf[128] = { 0 };
1635 snprintf(buf, sizeof(buf),
1636 _("*** Begin Notation (signature by: %s) ***\n"), sig->fpr);
1637 state_puts(state, buf);
1638 for (notation = sig->notations; notation; notation = notation->next)
1639 {
1640 if (is_pka_notation(notation))
1641 continue;
1642
1643 if (notation->name)
1644 {
1645 state_puts(state, notation->name);
1646 state_puts(state, "=");
1647 }
1648 if (notation->value)
1649 {
1650 state_puts(state, notation->value);
1651 if (!(*notation->value && (notation->value[strlen(notation->value) - 1] == '\n')))
1652 state_puts(state, "\n");
1653 }
1654 }
1655 state_puts(state, _("*** End Notation ***\n"));
1656 }
1657 }
1658 }
1659 }
1660
1661 gpgme_release(ctx);
1662
1663 state_attach_puts(state, _("[-- End signature information --]\n\n"));
1664 mutt_debug(LL_DEBUG1, "returning %d\n", badsig);
1665
1666 return badsig ? 1 : anywarn ? 2 : 0;
1667}
1668
1672int pgp_gpgme_verify_one(struct Body *b, struct State *state, const char *tempfile)
1673{
1674 return verify_one(b, state, tempfile, false);
1675}
1676
1680int smime_gpgme_verify_one(struct Body *b, struct State *state, const char *tempfile)
1681{
1682 return verify_one(b, state, tempfile, true);
1683}
1684
1698static struct Body *decrypt_part(struct Body *b, struct State *state,
1699 FILE *fp_out, bool is_smime, int *r_is_signed)
1700{
1701 if (!b || !state || !fp_out)
1702 return NULL;
1703
1704 struct Body *tattach = NULL;
1705 gpgme_error_t err = GPG_ERR_NO_ERROR;
1706 gpgme_data_t ciphertext = NULL, plaintext = NULL;
1707 bool maybe_signed = false;
1708 bool anywarn = false;
1709 int sig_stat = 0;
1710
1711 if (r_is_signed)
1712 *r_is_signed = 0;
1713
1714 gpgme_ctx_t ctx = NULL;
1715restart:
1716 ctx = create_gpgme_context(is_smime);
1717
1718 if (b->length < 0)
1719 return NULL;
1720 /* Make a data object from the body, create context etc. */
1721 ciphertext = file_to_data_object(state->fp_in, b->offset, b->length);
1722 if (!ciphertext)
1723 goto cleanup;
1724 plaintext = create_gpgme_data();
1725
1726 /* Do the decryption or the verification in case of the S/MIME hack. */
1727 if ((!is_smime) || maybe_signed)
1728 {
1729 if (!is_smime)
1730 err = gpgme_op_decrypt_verify(ctx, ciphertext, plaintext);
1731 else if (maybe_signed)
1732 err = gpgme_op_verify(ctx, ciphertext, NULL, plaintext);
1733
1734 if (err == GPG_ERR_NO_ERROR)
1735 {
1736 /* Check whether signatures have been verified. */
1737 gpgme_verify_result_t verify_result = gpgme_op_verify_result(ctx);
1738 if (verify_result->signatures)
1739 sig_stat = 1;
1740 }
1741 }
1742 else
1743 {
1744 err = gpgme_op_decrypt(ctx, ciphertext, plaintext);
1745 }
1746 gpgme_data_release(ciphertext);
1747 ciphertext = NULL;
1748 if (err != GPG_ERR_NO_ERROR)
1749 {
1750#ifdef USE_AUTOCRYPT
1751 /* Abort right away and silently.
1752 * Autocrypt will retry on the normal keyring. */
1754 goto cleanup;
1755#endif
1756 if (is_smime && !maybe_signed && (gpg_err_code(err) == GPG_ERR_NO_DATA))
1757 {
1758 /* Check whether this might be a signed message despite what the mime
1759 * header told us. Retry then. gpgsm returns the error information
1760 * "unsupported Algorithm '?'" but GPGME will not store this unknown
1761 * algorithm, thus we test that it has not been set. */
1762 gpgme_decrypt_result_t result = NULL;
1763
1764 result = gpgme_op_decrypt_result(ctx);
1765 if (result && !result->unsupported_algorithm)
1766 {
1767 maybe_signed = true;
1768 gpgme_data_release(plaintext);
1769 plaintext = NULL;
1770 /* gpgsm ends the session after an error; restart it */
1771 gpgme_release(ctx);
1772 goto restart;
1773 }
1774 }
1775 redraw_if_needed(ctx);
1776 if ((state->flags & STATE_DISPLAY))
1777 {
1778 char buf[200] = { 0 };
1779
1780 snprintf(buf, sizeof(buf) - 1,
1781 _("[-- Error: decryption failed: %s --]\n\n"), gpgme_strerror(err));
1782 state_attach_puts(state, buf);
1783 }
1784 goto cleanup;
1785 }
1786 redraw_if_needed(ctx);
1787
1788 /* Read the output from GPGME, and make sure to change CRLF to LF,
1789 * otherwise read_mime_header has a hard time parsing the message. */
1790 if (data_object_to_stream(plaintext, fp_out))
1791 {
1792 goto cleanup;
1793 }
1794 gpgme_data_release(plaintext);
1795 plaintext = NULL;
1796
1797 if (sig_stat)
1798 {
1799 int res, idx;
1800 int anybad = 0;
1801
1802 if (r_is_signed)
1803 *r_is_signed = -1; /* A signature exists. */
1804
1805 if ((state->flags & STATE_DISPLAY))
1806 {
1807 state_attach_puts(state, _("[-- Begin signature information --]\n"));
1808 }
1809 for (idx = 0; (res = show_one_sig_status(ctx, idx, state)) != -1; idx++)
1810 {
1811 if (res == 1)
1812 anybad = 1;
1813 else if (res == 2)
1814 anywarn = true;
1815 }
1816 if (!anybad && idx && r_is_signed && *r_is_signed)
1817 *r_is_signed = anywarn ? 2 : 1; /* Good signature. */
1818
1819 if ((state->flags & STATE_DISPLAY))
1820 {
1821 state_attach_puts(state, _("[-- End signature information --]\n\n"));
1822 }
1823 }
1824 gpgme_release(ctx);
1825 ctx = NULL;
1826
1827 fflush(fp_out);
1828 rewind(fp_out);
1829 const long size = mutt_file_get_size_fp(fp_out);
1830 if (size == 0)
1831 {
1832 goto cleanup;
1833 }
1834 tattach = mutt_read_mime_header(fp_out, 0);
1835 if (tattach)
1836 {
1837 /* Need to set the length of this body part. */
1838 tattach->length = size - tattach->offset;
1839
1840 tattach->warnsig = anywarn;
1841
1842 /* See if we need to recurse on this MIME part. */
1843 mutt_parse_part(fp_out, tattach);
1844 }
1845
1846cleanup:
1847 gpgme_data_release(ciphertext);
1848 gpgme_data_release(plaintext);
1849 gpgme_release(ctx);
1850
1851 return tattach;
1852}
1853
1857int pgp_gpgme_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
1858{
1859 struct State state = { 0 };
1860 struct Body *first_part = b;
1861 int is_signed = 0;
1862 bool need_decode = false;
1863 LOFF_T saved_offset = 0;
1864 size_t saved_length = 0;
1865 FILE *fp_decoded = NULL;
1866 int rc = 0;
1867
1868 first_part->goodsig = false;
1869 first_part->warnsig = false;
1870
1872 {
1873 b = b->parts->next;
1874 /* Some clients improperly encode the octetstream part. */
1875 if (b->encoding != ENC_7BIT)
1876 need_decode = true;
1877 }
1879 {
1880 b = b->parts->next->next;
1881 need_decode = true;
1882 }
1883 else
1884 {
1885 return -1;
1886 }
1887
1888 state.fp_in = fp_in;
1889
1890 if (need_decode)
1891 {
1892 saved_offset = b->offset;
1893 saved_length = b->length;
1894
1895 fp_decoded = mutt_file_mkstemp();
1896 if (!fp_decoded)
1897 {
1898 mutt_perror(_("Can't create temporary file"));
1899 return -1;
1900 }
1901
1902 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1903 {
1904 rc = -1;
1905 goto bail;
1906 }
1907 state.fp_out = fp_decoded;
1908
1909 mutt_decode_attachment(b, &state);
1910
1911 fflush(fp_decoded);
1912 b->length = ftello(fp_decoded);
1913 b->offset = 0;
1914 rewind(fp_decoded);
1915 state.fp_in = fp_decoded;
1916 state.fp_out = 0;
1917 }
1918
1919 *fp_out = mutt_file_mkstemp();
1920 if (!*fp_out)
1921 {
1922 mutt_perror(_("Can't create temporary file"));
1923 rc = -1;
1924 goto bail;
1925 }
1926
1927 *b_dec = decrypt_part(b, &state, *fp_out, false, &is_signed);
1928 if (*b_dec)
1929 {
1930 rewind(*fp_out);
1931 if (is_signed > 0)
1932 first_part->goodsig = true;
1933 }
1934 else
1935 {
1936 rc = -1;
1937 mutt_file_fclose(fp_out);
1938 }
1939
1940bail:
1941 if (need_decode)
1942 {
1943 b->length = saved_length;
1944 b->offset = saved_offset;
1945 mutt_file_fclose(&fp_decoded);
1946 }
1947
1948 return rc;
1949}
1950
1954int smime_gpgme_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
1955{
1956 struct State state = { 0 };
1957 int is_signed;
1958 LOFF_T saved_b_offset;
1959 size_t saved_b_length;
1960
1962 return -1;
1963
1964 if (b->parts)
1965 return -1;
1966
1967 /* Decode the body - we need to pass binary CMS to the
1968 * backend. The backend allows for Base64 encoded data but it does
1969 * not allow for QP which I have seen in some messages. So better
1970 * do it here. */
1971 saved_b_offset = b->offset;
1972 saved_b_length = b->length;
1973 state.fp_in = fp_in;
1974 if (!mutt_file_seek(state.fp_in, b->offset, SEEK_SET))
1975 {
1976 return -1;
1977 }
1978 FILE *fp_tmp = mutt_file_mkstemp();
1979 if (!fp_tmp)
1980 {
1981 mutt_perror(_("Can't create temporary file"));
1982 return -1;
1983 }
1984
1985 state.fp_out = fp_tmp;
1986 mutt_decode_attachment(b, &state);
1987 fflush(fp_tmp);
1988 b->length = ftello(state.fp_out);
1989 b->offset = 0;
1990 rewind(fp_tmp);
1991
1992 memset(&state, 0, sizeof(state));
1993 state.fp_in = fp_tmp;
1994 state.fp_out = 0;
1996 if (!*fp_out)
1997 {
1998 mutt_perror(_("Can't create temporary file"));
1999 return -1;
2000 }
2001
2002 *b_dec = decrypt_part(b, &state, *fp_out, true, &is_signed);
2003 if (*b_dec)
2004 (*b_dec)->goodsig = is_signed > 0;
2005 b->length = saved_b_length;
2006 b->offset = saved_b_offset;
2007 mutt_file_fclose(&fp_tmp);
2008 rewind(*fp_out);
2009 if (*b_dec && !is_signed && !(*b_dec)->parts && mutt_is_application_smime(*b_dec))
2010 {
2011 /* Assume that this is a opaque signed s/mime message. This is an ugly way
2012 * of doing it but we have anyway a problem with arbitrary encoded S/MIME
2013 * messages: Only the outer part may be encrypted. The entire mime parsing
2014 * should be revamped, probably by keeping the temporary files so that we
2015 * don't need to decrypt them all the time. Inner parts of an encrypted
2016 * part can then point into this file and there won't ever be a need to
2017 * decrypt again. This needs a partial rewrite of the MIME engine. */
2018 struct Body *bb = *b_dec;
2019
2020 saved_b_offset = bb->offset;
2021 saved_b_length = bb->length;
2022 memset(&state, 0, sizeof(state));
2023 state.fp_in = *fp_out;
2024 if (!mutt_file_seek(state.fp_in, bb->offset, SEEK_SET))
2025 {
2026 return -1;
2027 }
2028 FILE *fp_tmp2 = mutt_file_mkstemp();
2029 if (!fp_tmp2)
2030 {
2031 mutt_perror(_("Can't create temporary file"));
2032 return -1;
2033 }
2034
2035 state.fp_out = fp_tmp2;
2036 mutt_decode_attachment(bb, &state);
2037 fflush(fp_tmp2);
2038 bb->length = ftello(state.fp_out);
2039 bb->offset = 0;
2040 rewind(fp_tmp2);
2041 mutt_file_fclose(fp_out);
2042
2043 memset(&state, 0, sizeof(state));
2044 state.fp_in = fp_tmp2;
2045 state.fp_out = 0;
2046 *fp_out = mutt_file_mkstemp();
2047 if (!*fp_out)
2048 {
2049 mutt_perror(_("Can't create temporary file"));
2050 return -1;
2051 }
2052
2053 struct Body *b_tmp = decrypt_part(bb, &state, *fp_out, true, &is_signed);
2054 if (b_tmp)
2055 b_tmp->goodsig = is_signed > 0;
2056 bb->length = saved_b_length;
2057 bb->offset = saved_b_offset;
2058 mutt_file_fclose(&fp_tmp2);
2059 rewind(*fp_out);
2060 mutt_body_free(b_dec);
2061 *b_dec = b_tmp;
2062 }
2063 return *b_dec ? 0 : -1;
2064}
2065
2073static int pgp_gpgme_extract_keys(gpgme_data_t keydata, FILE **fp)
2074{
2075 gpgme_ctx_t tmpctx = NULL;
2076 gpgme_key_t key = NULL;
2077 gpgme_user_id_t uid = NULL;
2078 gpgme_subkey_t subkey = NULL;
2079 const char *shortid = NULL;
2080 size_t len;
2081 char date[256] = { 0 };
2082 bool more;
2083 int rc = -1;
2084 time_t tt;
2085
2086 *fp = mutt_file_mkstemp();
2087 if (!*fp)
2088 {
2089 mutt_perror(_("Can't create temporary file"));
2090 return -1;
2091 }
2092
2093 tmpctx = create_gpgme_context(false);
2094
2095 gpgme_error_t err = gpgme_op_keylist_from_data_start(tmpctx, keydata, 0);
2096 while (err == GPG_ERR_NO_ERROR)
2097 {
2098 err = gpgme_op_keylist_next(tmpctx, &key);
2099 if (err != GPG_ERR_NO_ERROR)
2100 break;
2101 uid = key->uids;
2102 subkey = key->subkeys;
2103 more = false;
2104 while (subkey)
2105 {
2106 shortid = subkey->keyid;
2107 len = mutt_str_len(subkey->keyid);
2108 if (len > 8)
2109 shortid += len - 8;
2110 tt = subkey->timestamp;
2111 mutt_date_localtime_format(date, sizeof(date), "%Y-%m-%d", tt);
2112
2113 fprintf(*fp, "%s %5.5s %u/%8s %s\n", more ? "sub" : "pub",
2114 gpgme_pubkey_algo_name(subkey->pubkey_algo), subkey->length, shortid, date);
2115 if (!more)
2116 {
2117 while (uid)
2118 {
2119 fprintf(*fp, "uid %s\n", NONULL(uid->uid));
2120 uid = uid->next;
2121 }
2122 }
2123 subkey = subkey->next;
2124 more = true;
2125 }
2126 gpgme_key_unref(key);
2127 }
2128 if (gpg_err_code(err) != GPG_ERR_EOF)
2129 {
2130 mutt_debug(LL_DEBUG1, "Error listing keys\n");
2131 goto err_fp;
2132 }
2133
2134 rc = 0;
2135
2136err_fp:
2137 if (rc)
2138 mutt_file_fclose(fp);
2139
2140 gpgme_release(tmpctx);
2141
2142 return rc;
2143}
2144
2156static int line_compare(const char *a, size_t n, const char *b)
2157{
2158 if (mutt_strn_equal(a, b, n))
2159 {
2160 /* at this point we know that 'b' is at least 'n' chars long */
2161 if ((b[n] == '\n') || ((b[n] == '\r') && (b[n + 1] == '\n')))
2162 return true;
2163 }
2164 return false;
2165}
2166
2174static int pgp_check_traditional_one_body(FILE *fp, struct Body *b)
2175{
2176 char buf[8192] = { 0 };
2177 bool rc = false;
2178
2179 bool sgn = false;
2180 bool enc = false;
2181
2182 if (b->type != TYPE_TEXT)
2183 return 0;
2184
2185 struct Buffer *tempfile = buf_pool_get();
2186 buf_mktemp(tempfile);
2188 MUTT_SAVE_NO_FLAGS) != 0)
2189 {
2190 unlink(buf_string(tempfile));
2191 goto cleanup;
2192 }
2193
2194 FILE *fp_tmp = mutt_file_fopen(buf_string(tempfile), "r");
2195 if (!fp_tmp)
2196 {
2197 unlink(buf_string(tempfile));
2198 goto cleanup;
2199 }
2200
2201 while (fgets(buf, sizeof(buf), fp_tmp))
2202 {
2203 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
2204 if (plen != 0)
2205 {
2206 if (MESSAGE(buf + plen))
2207 {
2208 enc = true;
2209 break;
2210 }
2211 else if (SIGNED_MESSAGE(buf + plen))
2212 {
2213 sgn = true;
2214 break;
2215 }
2216 }
2217 }
2218 mutt_file_fclose(&fp_tmp);
2219 unlink(buf_string(tempfile));
2220
2221 if (!enc && !sgn)
2222 goto cleanup;
2223
2224 /* fix the content type */
2225
2226 mutt_param_set(&b->parameter, "format", "fixed");
2227 mutt_param_set(&b->parameter, "x-action", enc ? "pgp-encrypted" : "pgp-signed");
2228
2229 rc = true;
2230
2231cleanup:
2232 buf_pool_release(&tempfile);
2233 return rc;
2234}
2235
2239bool pgp_gpgme_check_traditional(FILE *fp, struct Body *b, bool just_one)
2240{
2241 bool rc = false;
2242 for (; b; b = b->next)
2243 {
2244 if (!just_one && is_multipart(b))
2245 {
2246 rc = (pgp_gpgme_check_traditional(fp, b->parts, false) || rc);
2247 }
2248 else if (b->type == TYPE_TEXT)
2249 {
2251 if (r)
2252 rc = (rc || r);
2253 else
2254 rc = (pgp_check_traditional_one_body(fp, b) || rc);
2255 }
2256
2257 if (just_one)
2258 break;
2259 }
2260 return rc;
2261}
2262
2266void pgp_gpgme_invoke_import(const char *fname)
2267{
2268 gpgme_ctx_t ctx = create_gpgme_context(false);
2269 gpgme_data_t keydata = NULL;
2270 gpgme_import_result_t impres = NULL;
2271 gpgme_import_status_t st = NULL;
2272 bool any;
2273
2274 FILE *fp_in = mutt_file_fopen(fname, "r");
2275 if (!fp_in)
2276 {
2277 mutt_perror("%s", fname);
2278 goto leave;
2279 }
2280 /* Note that the stream, "fp_in", needs to be kept open while the keydata
2281 * is used. */
2282 gpgme_error_t err = gpgme_data_new_from_stream(&keydata, fp_in);
2283 if (err != GPG_ERR_NO_ERROR)
2284 {
2285 mutt_error(_("error allocating data object: %s"), gpgme_strerror(err));
2286 goto leave;
2287 }
2288
2289 err = gpgme_op_import(ctx, keydata);
2290 if (err != GPG_ERR_NO_ERROR)
2291 {
2292 mutt_error(_("Error importing key: %s"), gpgme_strerror(err));
2293 goto leave;
2294 }
2295
2296 /* Print infos about the imported keys to stdout. */
2297 impres = gpgme_op_import_result(ctx);
2298 if (!impres)
2299 {
2300 fputs("oops: no import result returned\n", stdout);
2301 goto leave;
2302 }
2303
2304 for (st = impres->imports; st; st = st->next)
2305 {
2306 if (st->result)
2307 continue;
2308 printf("key %s imported (", NONULL(st->fpr));
2309 /* Note that we use the singular even if it is possible that
2310 * several uids etc are new. This simply looks better. */
2311 any = false;
2312 if (st->status & GPGME_IMPORT_SECRET)
2313 {
2314 printf("secret parts");
2315 any = true;
2316 }
2317 if ((st->status & GPGME_IMPORT_NEW))
2318 {
2319 printf("%snew key", any ? ", " : "");
2320 any = true;
2321 }
2322 if ((st->status & GPGME_IMPORT_UID))
2323 {
2324 printf("%snew uid", any ? ", " : "");
2325 any = true;
2326 }
2327 if ((st->status & GPGME_IMPORT_SIG))
2328 {
2329 printf("%snew sig", any ? ", " : "");
2330 any = true;
2331 }
2332 if ((st->status & GPGME_IMPORT_SUBKEY))
2333 {
2334 printf("%snew subkey", any ? ", " : "");
2335 any = true;
2336 }
2337 printf("%s)\n", any ? "" : "not changed");
2338 /* Fixme: Should we lookup each imported key and print more infos? */
2339 }
2340 /* Now print keys which failed the import. Unfortunately in most
2341 * cases gpg will bail out early and not tell GPGME about. */
2342 /* FIXME: We could instead use the new GPGME_AUDITLOG_DIAG to show
2343 * the actual gpg diagnostics. But I fear that would clutter the
2344 * output too much. Maybe a dedicated prompt or option to do this
2345 * would be helpful. */
2346 for (st = impres->imports; st; st = st->next)
2347 {
2348 if (st->result == 0)
2349 continue;
2350 printf("key %s import failed: %s\n", NONULL(st->fpr), gpgme_strerror(st->result));
2351 }
2352 fflush(stdout);
2353
2354leave:
2355 gpgme_release(ctx);
2356 gpgme_data_release(keydata);
2357 mutt_file_fclose(&fp_in);
2358}
2359
2375static void copy_clearsigned(gpgme_data_t data, struct State *state, char *charset)
2376{
2377 char buf[8192] = { 0 };
2378 bool complete, armor_header;
2379 FILE *fp = NULL;
2380
2381 char *fname = data_object_to_tempfile(data, &fp);
2382 if (!fname)
2383 {
2384 mutt_file_fclose(&fp);
2385 return;
2386 }
2387 unlink(fname);
2388 FREE(&fname);
2389
2390 /* fromcode comes from the MIME Content-Type charset label. It might
2391 * be a wrong label, so we want the ability to do corrections via
2392 * charset-hooks. Therefore we set flags to MUTT_ICONV_HOOK_FROM. */
2394
2395 for (complete = true, armor_header = true;
2396 mutt_ch_fgetconvs(buf, sizeof(buf), fc); complete = (strchr(buf, '\n')))
2397 {
2398 if (!complete)
2399 {
2400 if (!armor_header)
2401 state_puts(state, buf);
2402 continue;
2403 }
2404
2405 if (BEGIN_PGP_SIGNATURE(buf))
2406 break;
2407
2408 if (armor_header)
2409 {
2410 if (buf[0] == '\n')
2411 armor_header = false;
2412 continue;
2413 }
2414
2415 if (state->prefix)
2416 state_puts(state, state->prefix);
2417
2418 if ((buf[0] == '-') && (buf[1] == ' '))
2419 state_puts(state, buf + 2);
2420 else
2421 state_puts(state, buf);
2422 }
2423
2426}
2427
2431int pgp_gpgme_application_handler(struct Body *b, struct State *state)
2432{
2433 int needpass = -1;
2434 bool pgp_keyblock = false;
2435 bool clearsign = false;
2436 long bytes;
2437 LOFF_T last_pos;
2438 LOFF_T block_begin;
2439 LOFF_T block_end;
2440 char buf[8192] = { 0 };
2441 FILE *fp_out = NULL;
2442
2443 gpgme_error_t err = GPG_ERR_NO_ERROR;
2444 gpgme_data_t armored_data = NULL;
2445
2446 bool maybe_goodsig = true;
2447 bool have_any_sigs = false;
2448
2449 char body_charset[256] = { 0 }; /* Only used for clearsigned messages. */
2450 char *gpgcharset = NULL;
2451
2452 mutt_debug(LL_DEBUG2, "Entering handler\n");
2453
2454 /* For clearsigned messages we won't be able to get a character set
2455 * but we know that this may only be text thus we assume Latin-1 here. */
2456 if (!mutt_body_get_charset(b, body_charset, sizeof(body_charset)))
2457 mutt_str_copy(body_charset, "iso-8859-1", sizeof(body_charset));
2458
2459 if (!mutt_file_seek(state->fp_in, b->offset, SEEK_SET))
2460 {
2461 return -1;
2462 }
2463 last_pos = b->offset;
2464
2465 for (bytes = b->length; bytes > 0;)
2466 {
2467 // record before the fgets in case it is a BEGIN block
2468 block_begin = last_pos;
2469
2470 if (!fgets(buf, sizeof(buf), state->fp_in))
2471 break;
2472
2473 LOFF_T offset = ftello(state->fp_in);
2474 if (offset < 0)
2475 {
2476 mutt_debug(LL_DEBUG1, "ftello() failed on fd %d\n", fileno(state->fp_in));
2477 offset = 0;
2478 }
2479 bytes -= (offset - last_pos); /* don't rely on mutt_str_len(buf) */
2480 last_pos = offset;
2481
2482 size_t plen = mutt_str_startswith(buf, "-----BEGIN PGP ");
2483 if (plen != 0)
2484 {
2485 needpass = 0;
2486 clearsign = false;
2487 pgp_keyblock = false;
2488
2489 if (MESSAGE(buf + plen))
2490 {
2491 needpass = 1;
2492 }
2493 else if (SIGNED_MESSAGE(buf + plen))
2494 {
2495 clearsign = true;
2496 }
2497 else if (PUBLIC_KEY_BLOCK(buf + plen))
2498 {
2499 pgp_keyblock = true;
2500 }
2501 else
2502 {
2503 /* XXX we may wish to recode here */
2504 if (state->prefix)
2505 state_puts(state, state->prefix);
2506 state_puts(state, buf);
2507 continue;
2508 }
2509
2510 /* Find the end of armored block. */
2511 while ((bytes > 0) && (fgets(buf, sizeof(buf) - 1, state->fp_in) != NULL))
2512 {
2513 offset = ftello(state->fp_in);
2514 if (offset < 0)
2515 {
2516 mutt_debug(LL_DEBUG1, "ftello() failed on fd %d\n", fileno(state->fp_in));
2517 offset = 0;
2518 }
2519 bytes -= (offset - last_pos); /* don't rely on mutt_strlen(buf) */
2520 last_pos = offset;
2521
2522 if (needpass && mutt_str_equal("-----END PGP MESSAGE-----\n", buf))
2523 {
2524 break;
2525 }
2526
2527 if (!needpass && (mutt_str_equal("-----END PGP SIGNATURE-----\n", buf) ||
2528 mutt_str_equal("-----END PGP PUBLIC KEY BLOCK-----\n", buf)))
2529 {
2530 break;
2531 }
2532
2533 // remember optional Charset: armor header as defined by rfc4880
2534 if (mutt_strn_equal("Charset: ", buf, 9))
2535 {
2536 size_t l = 0;
2537 FREE(&gpgcharset);
2538 gpgcharset = mutt_str_dup(buf + 9);
2539 if ((l = mutt_str_len(gpgcharset)) > 0 && gpgcharset[l - 1] == '\n')
2540 gpgcharset[l - 1] = 0;
2541 if (!mutt_ch_check_charset(gpgcharset, 0))
2542 mutt_str_replace(&gpgcharset, "UTF-8");
2543 }
2544 }
2545 block_end = ftello(state->fp_in);
2546 if (block_end < 0)
2547 {
2548 mutt_debug(LL_DEBUG1, "ftello() failed on fd %d\n", fileno(state->fp_in));
2549 block_end = 0;
2550 }
2551
2552 have_any_sigs = (have_any_sigs || (clearsign && (state->flags & STATE_VERIFY)));
2553
2554 /* Copy PGP material to an data container */
2555 armored_data = file_to_data_object(state->fp_in, block_begin, block_end - block_begin);
2556 fseeko(state->fp_in, block_end, 0);
2557
2558 /* Invoke PGP if needed */
2559 if (pgp_keyblock)
2560 {
2561 pgp_gpgme_extract_keys(armored_data, &fp_out);
2562 }
2563 else if (!clearsign || (state->flags & STATE_VERIFY))
2564 {
2565 gpgme_data_t plaintext = create_gpgme_data();
2566 gpgme_ctx_t ctx = create_gpgme_context(false);
2567
2568 if (clearsign)
2569 {
2570 err = gpgme_op_verify(ctx, armored_data, NULL, plaintext);
2571 }
2572 else
2573 {
2574 err = gpgme_op_decrypt_verify(ctx, armored_data, plaintext);
2575 if (gpg_err_code(err) == GPG_ERR_NO_DATA)
2576 {
2577 /* Decrypt verify can't handle signed only messages. */
2578 gpgme_data_seek(armored_data, 0, SEEK_SET);
2579 /* Must release plaintext so that we supply an uninitialized object. */
2580 gpgme_data_release(plaintext);
2581 plaintext = create_gpgme_data();
2582 err = gpgme_op_verify(ctx, armored_data, NULL, plaintext);
2583 }
2584 }
2585 redraw_if_needed(ctx);
2586
2587 if (err != GPG_ERR_NO_ERROR)
2588 {
2589 char errbuf[200] = { 0 };
2590
2591 snprintf(errbuf, sizeof(errbuf) - 1,
2592 _("Error: decryption/verification failed: %s\n"), gpgme_strerror(err));
2593 state_puts(state, errbuf);
2594 }
2595 else
2596 {
2597 /* Decryption/Verification succeeded */
2598
2599 mutt_message(_("PGP message successfully decrypted"));
2600
2601 bool sig_stat = false;
2602 char *tmpfname = NULL;
2603
2604 /* Check whether signatures have been verified. */
2605 gpgme_verify_result_t verify_result = gpgme_op_verify_result(ctx);
2606 if (verify_result->signatures)
2607 sig_stat = true;
2608
2609 have_any_sigs = false;
2610 maybe_goodsig = false;
2611 if ((state->flags & STATE_DISPLAY) && sig_stat)
2612 {
2613 int res, idx;
2614 bool anybad = false;
2615
2616 state_attach_puts(state, _("[-- Begin signature information --]\n"));
2617 have_any_sigs = true;
2618 for (idx = 0; (res = show_one_sig_status(ctx, idx, state)) != -1; idx++)
2619 {
2620 if (res == 1)
2621 anybad = true;
2622 }
2623 if (!anybad && idx)
2624 maybe_goodsig = true;
2625
2626 state_attach_puts(state, _("[-- End signature information --]\n\n"));
2627 }
2628
2629 tmpfname = data_object_to_tempfile(plaintext, &fp_out);
2630 if (tmpfname)
2631 {
2632 unlink(tmpfname);
2633 FREE(&tmpfname);
2634 }
2635 else
2636 {
2637 mutt_file_fclose(&fp_out);
2638 state_puts(state, _("Error: copy data failed\n"));
2639 }
2640 }
2641 gpgme_data_release(plaintext);
2642 gpgme_release(ctx);
2643 }
2644
2645 /* Now, copy cleartext to the screen. NOTE - we expect that PGP
2646 * outputs utf-8 cleartext. This may not always be true, but it
2647 * seems to be a reasonable guess. */
2648 if (state->flags & STATE_DISPLAY)
2649 {
2650 if (needpass)
2651 state_attach_puts(state, _("[-- BEGIN PGP MESSAGE --]\n\n"));
2652 else if (pgp_keyblock)
2653 state_attach_puts(state, _("[-- BEGIN PGP PUBLIC KEY BLOCK --]\n"));
2654 else
2655 state_attach_puts(state, _("[-- BEGIN PGP SIGNED MESSAGE --]\n\n"));
2656 }
2657
2658 if (clearsign)
2659 {
2660 copy_clearsigned(armored_data, state, body_charset);
2661 }
2662 else if (fp_out)
2663 {
2664 int c;
2665 char *expected_charset = gpgcharset && *gpgcharset ? gpgcharset : "utf-8";
2666 rewind(fp_out);
2667 struct FgetConv *fc = mutt_ch_fgetconv_open(fp_out, expected_charset,
2669 while ((c = mutt_ch_fgetconv(fc)) != EOF)
2670 {
2671 state_putc(state, c);
2672 if ((c == '\n') && state->prefix)
2673 state_puts(state, state->prefix);
2674 }
2676 }
2677
2678 if (state->flags & STATE_DISPLAY)
2679 {
2680 state_putc(state, '\n');
2681 if (needpass)
2682 state_attach_puts(state, _("[-- END PGP MESSAGE --]\n"));
2683 else if (pgp_keyblock)
2684 state_attach_puts(state, _("[-- END PGP PUBLIC KEY BLOCK --]\n"));
2685 else
2686 state_attach_puts(state, _("[-- END PGP SIGNED MESSAGE --]\n"));
2687 }
2688
2689 // Multiple PGP blocks can exist, so clean these up in each loop
2690 gpgme_data_release(armored_data);
2691 mutt_file_fclose(&fp_out);
2692 }
2693 else
2694 {
2695 /* A traditional PGP part may mix signed and unsigned content */
2696 /* XXX we may wish to recode here */
2697 if (state->prefix)
2698 state_puts(state, state->prefix);
2699 state_puts(state, buf);
2700 }
2701 }
2702 FREE(&gpgcharset);
2703
2704 b->goodsig = (maybe_goodsig && have_any_sigs);
2705
2706 if (needpass == -1)
2707 {
2708 state_attach_puts(state, _("[-- Error: could not find beginning of PGP message --]\n\n"));
2709 return 1;
2710 }
2711 mutt_debug(LL_DEBUG2, "Leaving handler\n");
2712
2713 return err;
2714}
2715
2722int pgp_gpgme_encrypted_handler(struct Body *b, struct State *state)
2723{
2724 int is_signed;
2725 int rc = 0;
2726
2727 mutt_debug(LL_DEBUG2, "Entering handler\n");
2728
2729 FILE *fp_out = mutt_file_mkstemp();
2730 if (!fp_out)
2731 {
2732 mutt_perror(_("Can't create temporary file"));
2733 if (state->flags & STATE_DISPLAY)
2734 {
2735 state_attach_puts(state, _("[-- Error: could not create temporary file --]\n"));
2736 }
2737 return -1;
2738 }
2739
2740 struct Body *tattach = decrypt_part(b, state, fp_out, false, &is_signed);
2741 if (tattach)
2742 {
2743 tattach->goodsig = is_signed > 0;
2744
2745 if (state->flags & STATE_DISPLAY)
2746 {
2747 state_attach_puts(state, is_signed ?
2748 _("[-- The following data is PGP/MIME signed and encrypted --]\n\n") :
2749 _("[-- The following data is PGP/MIME encrypted --]\n\n"));
2750 mutt_protected_headers_handler(tattach, state);
2751 }
2752
2753 /* Store any protected headers in the parent so they can be
2754 * accessed for index updates after the handler recursion is done.
2755 * This is done before the handler to prevent a nested encrypted
2756 * handler from freeing the headers. */
2758 b->mime_headers = tattach->mime_headers;
2759 tattach->mime_headers = NULL;
2760
2761 FILE *fp_save = state->fp_in;
2762 state->fp_in = fp_out;
2763 rc = mutt_body_handler(tattach, state);
2764 state->fp_in = fp_save;
2765
2766 /* Embedded multipart signed protected headers override the
2767 * encrypted headers. We need to do this after the handler so
2768 * they can be printed in the pager. */
2769 if (mutt_is_multipart_signed(tattach) && tattach->parts && tattach->parts->mime_headers)
2770 {
2772 b->mime_headers = tattach->parts->mime_headers;
2773 tattach->parts->mime_headers = NULL;
2774 }
2775
2776 /* if a multipart/signed is the _only_ sub-part of a
2777 * multipart/encrypted, cache signature verification
2778 * status. */
2779 if (mutt_is_multipart_signed(tattach) && !tattach->next)
2780 b->goodsig |= tattach->goodsig;
2781
2782 if (state->flags & STATE_DISPLAY)
2783 {
2784 state_puts(state, "\n");
2785 state_attach_puts(state, is_signed ?
2786 _("[-- End of PGP/MIME signed and encrypted data --]\n") :
2787 _("[-- End of PGP/MIME encrypted data --]\n"));
2788 }
2789
2790 mutt_body_free(&tattach);
2791 mutt_message(_("PGP message successfully decrypted"));
2792 }
2793 else
2794 {
2795#ifdef USE_AUTOCRYPT
2796 if (!OptAutocryptGpgme)
2797#endif
2798 {
2799 mutt_error(_("Could not decrypt PGP message"));
2800 }
2801 rc = -1;
2802 }
2803
2804 mutt_file_fclose(&fp_out);
2805 mutt_debug(LL_DEBUG2, "Leaving handler\n");
2806
2807 return rc;
2808}
2809
2813int smime_gpgme_application_handler(struct Body *b, struct State *state)
2814{
2815 int is_signed = 0;
2816 int rc = 0;
2817
2818 mutt_debug(LL_DEBUG2, "Entering handler\n");
2819
2820 /* clear out any mime headers before the handler, so they can't be spoofed. */
2822 b->warnsig = false;
2823 FILE *fp_out = mutt_file_mkstemp();
2824 if (!fp_out)
2825 {
2826 mutt_perror(_("Can't create temporary file"));
2827 if (state->flags & STATE_DISPLAY)
2828 {
2829 state_attach_puts(state, _("[-- Error: could not create temporary file --]\n"));
2830 }
2831 return -1;
2832 }
2833
2834 struct Body *tattach = decrypt_part(b, state, fp_out, true, &is_signed);
2835 if (tattach)
2836 {
2837 tattach->goodsig = is_signed > 0;
2838
2839 if (state->flags & STATE_DISPLAY)
2840 {
2841 state_attach_puts(state, is_signed ?
2842 _("[-- The following data is S/MIME signed --]\n\n") :
2843 _("[-- The following data is S/MIME encrypted --]\n\n"));
2844 mutt_protected_headers_handler(tattach, state);
2845 }
2846
2847 /* Store any protected headers in the parent so they can be
2848 * accessed for index updates after the handler recursion is done.
2849 * This is done before the handler to prevent a nested encrypted
2850 * handler from freeing the headers. */
2852 b->mime_headers = tattach->mime_headers;
2853 tattach->mime_headers = NULL;
2854
2855 FILE *fp_save = state->fp_in;
2856 state->fp_in = fp_out;
2857 rc = mutt_body_handler(tattach, state);
2858 state->fp_in = fp_save;
2859
2860 /* Embedded multipart signed protected headers override the
2861 * encrypted headers. We need to do this after the handler so
2862 * they can be printed in the pager. */
2863 if (mutt_is_multipart_signed(tattach) && tattach->parts && tattach->parts->mime_headers)
2864 {
2866 b->mime_headers = tattach->parts->mime_headers;
2867 tattach->parts->mime_headers = NULL;
2868 }
2869
2870 /* if a multipart/signed is the _only_ sub-part of a multipart/encrypted,
2871 * cache signature verification status. */
2872 if (mutt_is_multipart_signed(tattach) && !tattach->next)
2873 {
2874 b->goodsig = tattach->goodsig;
2875 if (!b->goodsig)
2876 b->warnsig = tattach->warnsig;
2877 }
2878 else if (tattach->goodsig)
2879 {
2880 b->goodsig = true;
2881 b->warnsig = tattach->warnsig;
2882 }
2883
2884 if (state->flags & STATE_DISPLAY)
2885 {
2886 state_puts(state, "\n");
2887 state_attach_puts(state, is_signed ? _("[-- End of S/MIME signed data --]\n") :
2888 _("[-- End of S/MIME encrypted data --]\n"));
2889 }
2890
2891 mutt_body_free(&tattach);
2892 }
2893
2894 mutt_file_fclose(&fp_out);
2895 mutt_debug(LL_DEBUG2, "Leaving handler\n");
2896
2897 return rc;
2898}
2899
2906unsigned int key_check_cap(gpgme_key_t key, enum KeyCap cap)
2907{
2908 unsigned int rc = 0;
2909
2910 switch (cap)
2911 {
2913 rc = key->can_encrypt;
2914 if (rc == 0)
2915 {
2916 for (gpgme_subkey_t subkey = key->subkeys; subkey; subkey = subkey->next)
2917 {
2918 rc = subkey->can_encrypt;
2919 if (rc != 0)
2920 break;
2921 }
2922 }
2923 break;
2924 case KEY_CAP_CAN_SIGN:
2925 rc = key->can_sign;
2926 if (rc == 0)
2927 {
2928 for (gpgme_subkey_t subkey = key->subkeys; subkey; subkey = subkey->next)
2929 {
2930 rc = subkey->can_sign;
2931 if (rc != 0)
2932 break;
2933 }
2934 }
2935 break;
2937 rc = key->can_certify;
2938 if (rc == 0)
2939 {
2940 for (gpgme_subkey_t subkey = key->subkeys; subkey; subkey = subkey->next)
2941 {
2942 rc = subkey->can_certify;
2943 if (rc != 0)
2944 break;
2945 }
2946 }
2947 break;
2948 }
2949
2950 return rc;
2951}
2952
2962static char *list_to_pattern(struct ListHead *list)
2963{
2964 char *pattern = NULL, *p = NULL;
2965 const char *s = NULL;
2966 size_t n;
2967
2968 n = 0;
2969 struct ListNode *np = NULL;
2970 STAILQ_FOREACH(np, list, entries)
2971 {
2972 for (s = np->data; *s; s++)
2973 {
2974 if ((*s == '%') || (*s == '+'))
2975 n += 2;
2976 n++;
2977 }
2978 n++; /* delimiter or end of string */
2979 }
2980 n++; /* make sure to allocate at least one byte */
2981 p = mutt_mem_calloc(1, n);
2982 pattern = p;
2983 STAILQ_FOREACH(np, list, entries)
2984 {
2985 s = np->data;
2986 if (*s)
2987 {
2988 if (np != STAILQ_FIRST(list))
2989 *p++ = ' ';
2990 for (s = np->data; *s; s++)
2991 {
2992 if (*s == '%')
2993 {
2994 *p++ = '%';
2995 *p++ = '2';
2996 *p++ = '5';
2997 }
2998 else if (*s == '+')
2999 {
3000 *p++ = '%';
3001 *p++ = '2';
3002 *p++ = 'B';
3003 }
3004 else if (*s == ' ')
3005 {
3006 *p++ = '+';
3007 }
3008 else
3009 {
3010 *p++ = *s;
3011 }
3012 }
3013 }
3014 }
3015 *p = '\0';
3016 return pattern;
3017}
3018
3029static struct CryptKeyInfo *get_candidates(struct ListHead *hints, SecurityFlags app, int secret)
3030{
3031 struct CryptKeyInfo *db = NULL, *k = NULL, **kend = NULL;
3032 gpgme_error_t err = GPG_ERR_NO_ERROR;
3033 gpgme_ctx_t ctx = NULL;
3034 gpgme_key_t key = NULL;
3035 int idx;
3036 gpgme_user_id_t uid = NULL;
3037
3038 char *pattern = list_to_pattern(hints);
3039 if (!pattern)
3040 return NULL;
3041
3042 ctx = create_gpgme_context(0);
3043 db = NULL;
3044 kend = &db;
3045
3046 if ((app & APPLICATION_PGP))
3047 {
3048 /* It's all a mess. That old GPGME expects different things depending on
3049 * the protocol. For gpg we don't need percent escaped pappert but simple
3050 * strings passed in an array to the keylist_ext_start function. */
3051 size_t n = 0;
3052 struct ListNode *np = NULL;
3053 STAILQ_FOREACH(np, hints, entries)
3054 {
3055 if (np->data && *np->data)
3056 n++;
3057 }
3058 if (n == 0)
3059 goto no_pgphints;
3060
3061 char **patarr = mutt_mem_calloc(n + 1, sizeof(*patarr));
3062 n = 0;
3063 STAILQ_FOREACH(np, hints, entries)
3064 {
3065 if (np->data && *np->data)
3066 patarr[n++] = mutt_str_dup(np->data);
3067 }
3068 patarr[n] = NULL;
3069 err = gpgme_op_keylist_ext_start(ctx, (const char **) patarr, secret, 0);
3070 for (n = 0; patarr[n]; n++)
3071 FREE(&patarr[n]);
3072 FREE(&patarr);
3073 if (err != GPG_ERR_NO_ERROR)
3074 {
3075 mutt_error(_("gpgme_op_keylist_start failed: %s"), gpgme_strerror(err));
3076 gpgme_release(ctx);
3077 FREE(&pattern);
3078 return NULL;
3079 }
3080
3081 while ((err = gpgme_op_keylist_next(ctx, &key)) == GPG_ERR_NO_ERROR)
3082 {
3083 KeyFlags flags = KEYFLAG_NO_FLAGS;
3084
3086 flags |= KEYFLAG_CANENCRYPT;
3088 flags |= KEYFLAG_CANSIGN;
3089
3090 if (key->revoked)
3091 flags |= KEYFLAG_REVOKED;
3092 if (key->expired)
3093 flags |= KEYFLAG_EXPIRED;
3094 if (key->disabled)
3095 flags |= KEYFLAG_DISABLED;
3096
3097 for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next)
3098 {
3099 k = mutt_mem_calloc(1, sizeof(*k));
3100 k->kobj = key;
3101 gpgme_key_ref(k->kobj);
3102 k->idx = idx;
3103 k->uid = uid->uid;
3104 k->flags = flags;
3105 if (uid->revoked)
3106 k->flags |= KEYFLAG_REVOKED;
3107 k->validity = uid->validity;
3108 *kend = k;
3109 kend = &k->next;
3110 }
3111 gpgme_key_unref(key);
3112 }
3113 if (gpg_err_code(err) != GPG_ERR_EOF)
3114 mutt_error(_("gpgme_op_keylist_next failed: %s"), gpgme_strerror(err));
3115 gpgme_op_keylist_end(ctx);
3116 no_pgphints:;
3117 }
3118
3119 if ((app & APPLICATION_SMIME))
3120 {
3121 /* and now look for x509 certificates */
3122 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
3123 err = gpgme_op_keylist_start(ctx, pattern, 0);
3124 if (err != GPG_ERR_NO_ERROR)
3125 {
3126 mutt_error(_("gpgme_op_keylist_start failed: %s"), gpgme_strerror(err));
3127 gpgme_release(ctx);
3128 FREE(&pattern);
3129 return NULL;
3130 }
3131
3132 while ((err = gpgme_op_keylist_next(ctx, &key)) == GPG_ERR_NO_ERROR)
3133 {
3134 KeyFlags flags = KEYFLAG_ISX509;
3135
3137 flags |= KEYFLAG_CANENCRYPT;
3139 flags |= KEYFLAG_CANSIGN;
3140
3141 if (key->revoked)
3142 flags |= KEYFLAG_REVOKED;
3143 if (key->expired)
3144 flags |= KEYFLAG_EXPIRED;
3145 if (key->disabled)
3146 flags |= KEYFLAG_DISABLED;
3147
3148 for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next)
3149 {
3150 k = mutt_mem_calloc(1, sizeof(*k));
3151 k->kobj = key;
3152 gpgme_key_ref(k->kobj);
3153 k->idx = idx;
3154 k->uid = uid->uid;
3155 k->flags = flags;
3156 if (uid->revoked)
3157 k->flags |= KEYFLAG_REVOKED;
3158 k->validity = uid->validity;
3159 *kend = k;
3160 kend = &k->next;
3161 }
3162 gpgme_key_unref(key);
3163 }
3164 if (gpg_err_code(err) != GPG_ERR_EOF)
3165 mutt_error(_("gpgme_op_keylist_next failed: %s"), gpgme_strerror(err));
3166 gpgme_op_keylist_end(ctx);
3167 }
3168
3169 gpgme_release(ctx);
3170 FREE(&pattern);
3171 return db;
3172}
3173
3182static void crypt_add_string_to_hints(const char *str, struct ListHead *hints)
3183{
3184 char *scratch = mutt_str_dup(str);
3185 if (!scratch)
3186 return;
3187
3188 for (char *t = strtok(scratch, " ,.:\"()<>\n"); t; t = strtok(NULL, " ,.:\"()<>\n"))
3189 {
3190 if (strlen(t) > 3)
3192 }
3193
3194 FREE(&scratch);
3195}
3196
3206static struct CryptKeyInfo *crypt_getkeybyaddr(struct Address *a,
3207 KeyFlags abilities, unsigned int app,
3208 bool *forced_valid, bool oppenc_mode)
3209{
3210 struct ListHead hints = STAILQ_HEAD_INITIALIZER(hints);
3211
3212 int multi = false;
3213 int this_key_has_strong = false;
3214 int this_key_has_addr_match = false;
3215 int match = false;
3216
3217 struct CryptKeyInfo *keys = NULL, *k = NULL;
3218 struct CryptKeyInfo *the_strong_valid_key = NULL;
3219 struct CryptKeyInfo *a_valid_addrmatch_key = NULL;
3220 struct CryptKeyInfo *matches = NULL;
3221 struct CryptKeyInfo **matches_endp = &matches;
3222
3223 if (a && a->mailbox)
3225 if (a && a->personal)
3227
3228 if (!oppenc_mode)
3229 mutt_message(_("Looking for keys matching \"%s\"..."), a ? buf_string(a->mailbox) : "");
3230 keys = get_candidates(&hints, app, (abilities & KEYFLAG_CANSIGN));
3231
3232 mutt_list_free(&hints);
3233
3234 if (!keys)
3235 return NULL;
3236
3237 mutt_debug(LL_DEBUG5, "looking for %s <%s>\n",
3238 a ? buf_string(a->personal) : "", a ? buf_string(a->mailbox) : "");
3239
3240 for (k = keys; k; k = k->next)
3241 {
3242 mutt_debug(LL_DEBUG5, " looking at key: %s '%.15s'\n", crypt_keyid(k), k->uid);
3243
3244 if (abilities && !(k->flags & abilities))
3245 {
3246 mutt_debug(LL_DEBUG2, " insufficient abilities: Has %x, want %x\n", k->flags, abilities);
3247 continue;
3248 }
3249
3250 this_key_has_strong = false; /* strong and valid match */
3251 this_key_has_addr_match = false;
3252 match = false; /* any match */
3253
3254 struct AddressList alist = TAILQ_HEAD_INITIALIZER(alist);
3255 mutt_addrlist_parse(&alist, k->uid);
3256 struct Address *ka = NULL;
3257 TAILQ_FOREACH(ka, &alist, entries)
3258 {
3259 int validity = crypt_id_matches_addr(a, ka, k);
3260
3261 if (validity & CRYPT_KV_MATCH) /* something matches */
3262 {
3263 match = true;
3264
3265 if ((validity & CRYPT_KV_VALID) && (validity & CRYPT_KV_ADDR))
3266 {
3267 if (validity & CRYPT_KV_STRONGID)
3268 {
3269 if (the_strong_valid_key && (the_strong_valid_key->kobj != k->kobj))
3270 multi = true;
3271 this_key_has_strong = true;
3272 }
3273 else
3274 {
3275 this_key_has_addr_match = true;
3276 }
3277 }
3278 }
3279 }
3280 mutt_addrlist_clear(&alist);
3281
3282 if (match)
3283 {
3284 struct CryptKeyInfo *tmp = crypt_copy_key(k);
3285 *matches_endp = tmp;
3286 matches_endp = &tmp->next;
3287
3288 if (this_key_has_strong)
3289 the_strong_valid_key = tmp;
3290 else if (this_key_has_addr_match)
3291 a_valid_addrmatch_key = tmp;
3292 }
3293 }
3294
3295 crypt_key_free(&keys);
3296
3297 if (matches)
3298 {
3299 if (oppenc_mode || !isatty(STDIN_FILENO))
3300 {
3301 const bool c_crypt_opportunistic_encrypt_strong_keys =
3302 cs_subset_bool(NeoMutt->sub, "crypt_opportunistic_encrypt_strong_keys");
3303 if (the_strong_valid_key)
3304 k = crypt_copy_key(the_strong_valid_key);
3305 else if (a_valid_addrmatch_key && !c_crypt_opportunistic_encrypt_strong_keys)
3306 k = crypt_copy_key(a_valid_addrmatch_key);
3307 else
3308 k = NULL;
3309 }
3310 else if (the_strong_valid_key && !multi)
3311 {
3312 /* There was precisely one strong match on a valid ID.
3313 * Proceed without asking the user. */
3314 k = crypt_copy_key(the_strong_valid_key);
3315 }
3316 else
3317 {
3318 /* Else: Ask the user. */
3319 k = dlg_gpgme(matches, a, NULL, app, forced_valid);
3320 }
3321
3322 crypt_key_free(&matches);
3323 }
3324 else
3325 {
3326 k = NULL;
3327 }
3328
3329 return k;
3330}
3331
3340static struct CryptKeyInfo *crypt_getkeybystr(const char *p, KeyFlags abilities,
3341 unsigned int app, bool *forced_valid)
3342{
3343 struct ListHead hints = STAILQ_HEAD_INITIALIZER(hints);
3344 struct CryptKeyInfo *matches = NULL;
3345 struct CryptKeyInfo **matches_endp = &matches;
3346 struct CryptKeyInfo *k = NULL;
3347 const char *ps = NULL, *pl = NULL, *phint = NULL;
3348
3349 mutt_message(_("Looking for keys matching \"%s\"..."), p);
3350
3351 const char *pfcopy = crypt_get_fingerprint_or_id(p, &phint, &pl, &ps);
3352 crypt_add_string_to_hints(phint, &hints);
3353 struct CryptKeyInfo *keys = get_candidates(&hints, app, (abilities & KEYFLAG_CANSIGN));
3354 mutt_list_free(&hints);
3355
3356 if (!keys)
3357 {
3358 FREE(&pfcopy);
3359 return NULL;
3360 }
3361
3362 for (k = keys; k; k = k->next)
3363 {
3364 if (abilities && !(k->flags & abilities))
3365 continue;
3366
3367 mutt_debug(LL_DEBUG5, "matching \"%s\" against key %s, \"%s\": ", p,
3368 crypt_long_keyid(k), k->uid);
3369
3370 if ((*p == '\0') || (pfcopy && mutt_istr_equal(pfcopy, crypt_fpr(k))) ||
3371 (pl && mutt_istr_equal(pl, crypt_long_keyid(k))) ||
3372 (ps && mutt_istr_equal(ps, crypt_short_keyid(k))) || mutt_istr_find(k->uid, p))
3373 {
3374 mutt_debug(LL_DEBUG5, "match\n");
3375
3376 struct CryptKeyInfo *tmp = crypt_copy_key(k);
3377 *matches_endp = tmp;
3378 matches_endp = &tmp->next;
3379 }
3380 else
3381 {
3382 mutt_debug(LL_DEBUG5, "no match\n");
3383 }
3384 }
3385
3386 FREE(&pfcopy);
3387 crypt_key_free(&keys);
3388
3389 if (matches)
3390 {
3391 if (isatty(STDIN_FILENO))
3392 {
3393 k = dlg_gpgme(matches, NULL, p, app, forced_valid);
3394
3395 crypt_key_free(&matches);
3396 return k;
3397 }
3398 else
3399 {
3400 if (crypt_keys_are_valid(matches))
3401 return matches;
3402
3403 crypt_key_free(&matches);
3404 return NULL;
3405 }
3406 }
3407
3408 return NULL;
3409}
3410
3423static struct CryptKeyInfo *crypt_ask_for_key(const char *tag, const char *whatfor,
3424 KeyFlags abilities,
3425 unsigned int app, bool *forced_valid)
3426{
3427 struct CryptKeyInfo *key = NULL;
3428 struct CryptCache *l = NULL;
3429 struct Buffer *resp = buf_pool_get();
3430
3432
3433 if (whatfor)
3434 {
3435 for (l = IdDefaults; l; l = l->next)
3436 {
3437 if (mutt_istr_equal(whatfor, l->what))
3438 {
3439 buf_strcpy(resp, l->dflt);
3440 break;
3441 }
3442 }
3443 }
3444
3445 while (true)
3446 {
3447 buf_reset(resp);
3448 if (mw_get_field(tag, resp, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) != 0)
3449 {
3450 goto done;
3451 }
3452
3453 if (whatfor)
3454 {
3455 if (l)
3456 {
3457 mutt_str_replace(&l->dflt, buf_string(resp));
3458 }
3459 else
3460 {
3461 l = mutt_mem_malloc(sizeof(struct CryptCache));
3462 l->next = IdDefaults;
3463 IdDefaults = l;
3464 l->what = mutt_str_dup(whatfor);
3465 l->dflt = buf_strdup(resp);
3466 }
3467 }
3468
3469 key = crypt_getkeybystr(buf_string(resp), abilities, app, forced_valid);
3470 if (key)
3471 goto done;
3472
3473 mutt_error(_("No matching keys found for \"%s\""), buf_string(resp));
3474 }
3475
3476done:
3477 buf_pool_release(&resp);
3478 return key;
3479}
3480
3492static char *find_keys(const struct AddressList *addrlist, unsigned int app, bool oppenc_mode)
3493{
3494 struct ListHead crypt_hook_list = STAILQ_HEAD_INITIALIZER(crypt_hook_list);
3495 struct ListNode *crypt_hook = NULL;
3496 const char *keyid = NULL;
3497 char *keylist = NULL;
3498 size_t keylist_size = 0;
3499 size_t keylist_used = 0;
3500 struct Address *p = NULL;
3501 struct CryptKeyInfo *k_info = NULL;
3502 const char *fqdn = mutt_fqdn(true, NeoMutt->sub);
3503 char buf[1024] = { 0 };
3504 bool forced_valid = false;
3505 bool key_selected;
3506 struct AddressList hookal = TAILQ_HEAD_INITIALIZER(hookal);
3507
3508 struct Address *a = NULL;
3509 const bool c_crypt_confirm_hook = cs_subset_bool(NeoMutt->sub, "crypt_confirm_hook");
3510 TAILQ_FOREACH(a, addrlist, entries)
3511 {
3512 key_selected = false;
3513 mutt_crypt_hook(&crypt_hook_list, a);
3514 crypt_hook = STAILQ_FIRST(&crypt_hook_list);
3515 do
3516 {
3517 p = a;
3518 forced_valid = false;
3519 k_info = NULL;
3520
3521 if (crypt_hook)
3522 {
3523 keyid = crypt_hook->data;
3524 enum QuadOption ans = MUTT_YES;
3525 if (!oppenc_mode && c_crypt_confirm_hook && isatty(STDIN_FILENO))
3526 {
3527 snprintf(buf, sizeof(buf), _("Use keyID = \"%s\" for %s?"), keyid,
3528 buf_string(p->mailbox));
3529 ans = query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "crypt_confirm_hook");
3530 }
3531 if (ans == MUTT_YES)
3532 {
3533 if (crypt_is_numerical_keyid(keyid))
3534 {
3535 if (mutt_strn_equal(keyid, "0x", 2))
3536 keyid += 2;
3537 goto bypass_selection; /* you don't see this. */
3538 }
3539
3540 /* check for e-mail address */
3541 mutt_addrlist_clear(&hookal);
3542 if (strchr(keyid, '@') && (mutt_addrlist_parse(&hookal, keyid) != 0))
3543 {
3544 mutt_addrlist_qualify(&hookal, fqdn);
3545 p = TAILQ_FIRST(&hookal);
3546 }
3547 else if (!oppenc_mode)
3548 {
3549 k_info = crypt_getkeybystr(keyid, KEYFLAG_CANENCRYPT, app, &forced_valid);
3550 }
3551 }
3552 else if (ans == MUTT_NO)
3553 {
3554 if (key_selected || STAILQ_NEXT(crypt_hook, entries))
3555 {
3556 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
3557 continue;
3558 }
3559 }
3560 else if (ans == MUTT_ABORT)
3561 {
3562 FREE(&keylist);
3563 mutt_addrlist_clear(&hookal);
3564 mutt_list_free(&crypt_hook_list);
3565 return NULL;
3566 }
3567 }
3568
3569 if (!k_info)
3570 {
3571 k_info = crypt_getkeybyaddr(p, KEYFLAG_CANENCRYPT, app, &forced_valid, oppenc_mode);
3572 }
3573
3574 if (!k_info && !oppenc_mode && isatty(STDIN_FILENO))
3575 {
3576 snprintf(buf, sizeof(buf), _("Enter keyID for %s: "), buf_string(p->mailbox));
3577
3578 k_info = crypt_ask_for_key(buf, buf_string(p->mailbox),
3579 KEYFLAG_CANENCRYPT, app, &forced_valid);
3580 }
3581
3582 if (!k_info)
3583 {
3584 FREE(&keylist);
3585 mutt_addrlist_clear(&hookal);
3586 mutt_list_free(&crypt_hook_list);
3587 return NULL;
3588 }
3589
3590 keyid = crypt_fpr_or_lkeyid(k_info);
3591
3592 bypass_selection:
3593 keylist_size += mutt_str_len(keyid) + 4 + 1;
3594 mutt_mem_realloc(&keylist, keylist_size);
3595 sprintf(keylist + keylist_used, "%s0x%s%s", keylist_used ? " " : "",
3596 keyid, forced_valid ? "!" : "");
3597 keylist_used = mutt_str_len(keylist);
3598
3599 key_selected = true;
3600
3601 crypt_key_free(&k_info);
3602 mutt_addrlist_clear(&hookal);
3603
3604 if (crypt_hook)
3605 crypt_hook = STAILQ_NEXT(crypt_hook, entries);
3606
3607 } while (crypt_hook);
3608
3609 mutt_list_free(&crypt_hook_list);
3610 }
3611 return keylist;
3612}
3613
3617char *pgp_gpgme_find_keys(const struct AddressList *addrlist, bool oppenc_mode)
3618{
3619 return find_keys(addrlist, APPLICATION_PGP, oppenc_mode);
3620}
3621
3625char *smime_gpgme_find_keys(const struct AddressList *addrlist, bool oppenc_mode)
3626{
3627 return find_keys(addrlist, APPLICATION_SMIME, oppenc_mode);
3628}
3629
3630#ifdef USE_AUTOCRYPT
3644{
3645 int rc = -1;
3646 gpgme_error_t err = GPG_ERR_NO_ERROR;
3647 gpgme_key_t key = NULL;
3648 gpgme_user_id_t uid = NULL;
3649 struct CryptKeyInfo *results = NULL, *k = NULL;
3650 struct CryptKeyInfo **kend = NULL;
3651 struct CryptKeyInfo *choice = NULL;
3652
3653 gpgme_ctx_t ctx = create_gpgme_context(false);
3654
3655 /* list all secret keys */
3656 if (gpgme_op_keylist_start(ctx, NULL, 1))
3657 goto cleanup;
3658
3659 kend = &results;
3660
3661 while ((err = gpgme_op_keylist_next(ctx, &key)) == GPG_ERR_NO_ERROR)
3662 {
3664
3669
3670 if (key->revoked)
3672 if (key->expired)
3674 if (key->disabled)
3676
3677 int idx;
3678 for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next)
3679 {
3680 k = mutt_mem_calloc(1, sizeof(*k));
3681 k->kobj = key;
3682 gpgme_key_ref(k->kobj);
3683 k->idx = idx;
3684 k->uid = uid->uid;
3685 k->flags = flags;
3686 if (uid->revoked)
3687 k->flags |= KEYFLAG_REVOKED;
3688 k->validity = uid->validity;
3689 *kend = k;
3690 kend = &k->next;
3691 }
3692 gpgme_key_unref(key);
3693 }
3694 if (gpg_err_code(err) != GPG_ERR_EOF)
3695 mutt_error(_("gpgme_op_keylist_next failed: %s"), gpgme_strerror(err));
3696 gpgme_op_keylist_end(ctx);
3697
3698 if (!results)
3699 {
3700 /* L10N: mutt_gpgme_select_secret_key() tries to list all secret keys to choose
3701 from. This error is displayed if no results were found. */
3702 mutt_error(_("No secret keys found"));
3703 goto cleanup;
3704 }
3705
3706 choice = dlg_gpgme(results, NULL, "*", APPLICATION_PGP, NULL);
3707 if (!(choice && choice->kobj && choice->kobj->subkeys && choice->kobj->subkeys->fpr))
3708 goto cleanup;
3709 buf_strcpy(keyid, choice->kobj->subkeys->fpr);
3710
3711 rc = 0;
3712
3713cleanup:
3714 crypt_key_free(&choice);
3715 crypt_key_free(&results);
3716 gpgme_release(ctx);
3717 return rc;
3718}
3719#endif
3720
3725{
3726 gpgme_ctx_t ctx = NULL;
3727 gpgme_key_t export_keys[2] = { 0 };
3728 gpgme_data_t keydata = NULL;
3729 struct Body *att = NULL;
3730 char buf[1024] = { 0 };
3731
3732 OptPgpCheckTrust = false;
3733
3734 struct CryptKeyInfo *key = crypt_ask_for_key(_("Please enter the key ID: "), NULL,
3736 if (!key)
3737 goto bail;
3738 export_keys[0] = key->kobj;
3739 export_keys[1] = NULL;
3740
3741 ctx = create_gpgme_context(false);
3742 gpgme_set_armor(ctx, 1);
3743 keydata = create_gpgme_data();
3744 gpgme_error_t err = gpgme_op_export_keys(ctx, export_keys, 0, keydata);
3745 if (err != GPG_ERR_NO_ERROR)
3746 {
3747 mutt_error(_("Error exporting key: %s"), gpgme_strerror(err));
3748 goto bail;
3749 }
3750
3751 char *tempf = data_object_to_tempfile(keydata, NULL);
3752 if (!tempf)
3753 goto bail;
3754
3755 att = mutt_body_new();
3756 /* tempf is a newly allocated string, so this is correct: */
3757 att->filename = tempf;
3758 att->unlink = true;
3759 att->use_disp = false;
3760 att->type = TYPE_APPLICATION;
3761 att->subtype = mutt_str_dup("pgp-keys");
3762 /* L10N: MIME description for exported (attached) keys.
3763 You can translate this entry to a non-ASCII string (it will be encoded),
3764 but it may be safer to keep it untranslated. */
3765 snprintf(buf, sizeof(buf), _("PGP Key 0x%s"), crypt_keyid(key));
3766 att->description = mutt_str_dup(buf);
3768
3769 att->length = mutt_file_get_size(tempf);
3770
3771bail:
3772 crypt_key_free(&key);
3773 gpgme_data_release(keydata);
3774 gpgme_release(ctx);
3775
3776 return att;
3777}
3778
3782static void init_common(void)
3783{
3784 /* this initialization should only run one time, but it may be called by
3785 * either pgp_gpgme_init or smime_gpgme_init */
3786 static bool has_run = false;
3787 if (has_run)
3788 return;
3789
3790 gpgme_check_version(NULL);
3791 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
3792#ifdef ENABLE_NLS
3793 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
3794#endif
3795 has_run = true;
3796}
3797
3801static void init_pgp(void)
3802{
3803 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) != GPG_ERR_NO_ERROR)
3804 {
3805 mutt_error(_("GPGME: OpenPGP protocol not available"));
3806 }
3807}
3808
3812static void init_smime(void)
3813{
3814 if (gpgme_engine_check_version(GPGME_PROTOCOL_CMS) != GPG_ERR_NO_ERROR)
3815 {
3816 mutt_error(_("GPGME: CMS protocol not available"));
3817 }
3818}
3819
3824{
3825 init_common();
3826 init_pgp();
3827}
3828
3833{
3834 init_common();
3835 init_smime();
3836}
3837
3844static SecurityFlags gpgme_send_menu(struct Email *e, bool is_smime)
3845{
3846 struct CryptKeyInfo *p = NULL;
3847 const char *prompt = NULL;
3848 const char *letters = NULL;
3849 const char *choices = NULL;
3850 int choice;
3851
3852 if (is_smime)
3854 else
3856
3857 /* Opportunistic encrypt is controlling encryption.
3858 * NOTE: "Signing" and "Clearing" only adjust the sign bit, so we have different
3859 * letter choices for those.
3860 */
3861 const bool c_crypt_opportunistic_encrypt = cs_subset_bool(NeoMutt->sub, "crypt_opportunistic_encrypt");
3862 if (c_crypt_opportunistic_encrypt && (e->security & SEC_OPPENCRYPT))
3863 {
3864 if (is_smime)
3865 {
3866 /* L10N: S/MIME options (opportunistic encryption is on) */
3867 prompt = _("S/MIME (s)ign, sign (a)s, (p)gp, (c)lear, or (o)ppenc mode off?");
3868 /* L10N: S/MIME options (opportunistic encryption is on) */
3869 letters = _("sapco");
3870 choices = "SapCo";
3871 }
3872 else
3873 {
3874 /* L10N: PGP options (opportunistic encryption is on) */
3875 prompt = _("PGP (s)ign, sign (a)s, s/(m)ime, (c)lear, or (o)ppenc mode off?");
3876 /* L10N: PGP options (opportunistic encryption is on) */
3877 letters = _("samco");
3878 choices = "SamCo";
3879 }
3880 }
3881 else if (c_crypt_opportunistic_encrypt)
3882 {
3883 /* Opportunistic encryption option is set, but is toggled off for this message. */
3884 if (is_smime)
3885 {
3886 /* L10N: S/MIME options (opportunistic encryption is off) */
3887 prompt = _("S/MIME (e)ncrypt, (s)ign, sign (a)s, (b)oth, (p)gp, (c)lear, or (o)ppenc mode?");
3888 /* L10N: S/MIME options (opportunistic encryption is off) */
3889 letters = _("esabpco");
3890 choices = "esabpcO";
3891 }
3892 else
3893 {
3894 /* L10N: PGP options (opportunistic encryption is off) */
3895 prompt = _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, s/(m)ime, (c)lear, or (o)ppenc mode?");
3896 /* L10N: PGP options (opportunistic encryption is off) */
3897 letters = _("esabmco");
3898 choices = "esabmcO";
3899 }
3900 }
3901 else
3902 {
3903 /* Opportunistic encryption is unset */
3904 if (is_smime)
3905 {
3906 /* L10N: S/MIME options */
3907 prompt = _("S/MIME (e)ncrypt, (s)ign, sign (a)s, (b)oth, (p)gp or (c)lear?");
3908 /* L10N: S/MIME options */
3909 letters = _("esabpc");
3910 choices = "esabpc";
3911 }
3912 else
3913 {
3914 /* L10N: PGP options */
3915 prompt = _("PGP (e)ncrypt, (s)ign, sign (a)s, (b)oth, s/(m)ime or (c)lear?");
3916 /* L10N: PGP options */
3917 letters = _("esabmc");
3918 choices = "esabmc";
3919 }
3920 }
3921
3922 choice = mw_multi_choice(prompt, letters);
3923 if (choice > 0)
3924 {
3925 switch (choices[choice - 1])
3926 {
3927 case 'a': /* sign (a)s */
3928 p = crypt_ask_for_key(_("Sign as: "), NULL, KEYFLAG_CANSIGN,
3929 is_smime ? APPLICATION_SMIME : APPLICATION_PGP, NULL);
3930 if (p)
3931 {
3932 char input_signas[128] = { 0 };
3933 snprintf(input_signas, sizeof(input_signas), "0x%s", crypt_fpr_or_lkeyid(p));
3934
3935 if (is_smime)
3936 cs_subset_str_string_set(NeoMutt->sub, "smime_sign_as", input_signas, NULL);
3937 else
3938 cs_subset_str_string_set(NeoMutt->sub, "pgp_sign_as", input_signas, NULL);
3939
3940 crypt_key_free(&p);
3941
3942 e->security |= SEC_SIGN;
3943 }
3944 break;
3945
3946 case 'b': /* (b)oth */
3947 e->security |= (SEC_ENCRYPT | SEC_SIGN);
3948 break;
3949
3950 case 'C':
3951 e->security &= ~SEC_SIGN;
3952 break;
3953
3954 case 'c': /* (c)lear */
3955 e->security &= ~(SEC_ENCRYPT | SEC_SIGN);
3956 break;
3957
3958 case 'e': /* (e)ncrypt */
3959 e->security |= SEC_ENCRYPT;
3960 e->security &= ~SEC_SIGN;
3961 break;
3962
3963 case 'm': /* (p)gp or s/(m)ime */
3964 case 'p':
3965 is_smime = !is_smime;
3966 if (is_smime)
3967 {
3968 e->security &= ~APPLICATION_PGP;
3970 }
3971 else
3972 {
3973 e->security &= ~APPLICATION_SMIME;
3975 }
3977 break;
3978
3979 case 'O': /* oppenc mode on */
3982 break;
3983
3984 case 'o': /* oppenc mode off */
3985 e->security &= ~SEC_OPPENCRYPT;
3986 break;
3987
3988 case 'S': /* (s)ign in oppenc mode */
3989 e->security |= SEC_SIGN;
3990 break;
3991
3992 case 's': /* (s)ign */
3993 e->security &= ~SEC_ENCRYPT;
3994 e->security |= SEC_SIGN;
3995 break;
3996 }
3997 }
3998
3999 return e->security;
4000}
4001
4006{
4007 return gpgme_send_menu(e, false);
4008}
4009
4014{
4015 return gpgme_send_menu(e, true);
4016}
4017
4023static bool verify_sender(struct Email *e)
4024{
4025 struct Address *sender = NULL;
4026 bool rc = true;
4027
4028 if (!TAILQ_EMPTY(&e->env->from))
4029 {
4031 sender = TAILQ_FIRST(&e->env->from);
4032 }
4033 else if (!TAILQ_EMPTY(&e->env->sender))
4034 {
4036 sender = TAILQ_FIRST(&e->env->sender);
4037 }
4038
4039 if (sender)
4040 {
4041 if (SignatureKey)
4042 {
4043 gpgme_key_t key = SignatureKey;
4044 gpgme_user_id_t uid = NULL;
4045 int sender_length = buf_len(sender->mailbox);
4046 for (uid = key->uids; uid && rc; uid = uid->next)
4047 {
4048 int uid_length = strlen(uid->email);
4049 if ((uid->email[0] == '<') && (uid->email[uid_length - 1] == '>') &&
4050 (uid_length == (sender_length + 2)))
4051 {
4052 const char *at_sign = strchr(uid->email + 1, '@');
4053 if (at_sign)
4054 {
4055 /* Assume address is 'mailbox@domainname'.
4056 * The mailbox part is case-sensitive,
4057 * the domainname is not. (RFC2821) */
4058 const char *tmp_email = uid->email + 1;
4059 const char *tmp_sender = buf_string(sender->mailbox);
4060 /* length of mailbox part including '@' */
4061 int mailbox_length = at_sign - tmp_email + 1;
4062 int domainname_length = sender_length - mailbox_length;
4063 int mailbox_match, domainname_match;
4064
4065 mailbox_match = mutt_strn_equal(tmp_email, tmp_sender, mailbox_length);
4066 tmp_email += mailbox_length;
4067 tmp_sender += mailbox_length;
4068 domainname_match = (mutt_istrn_cmp(tmp_email, tmp_sender, domainname_length) == 0);
4069 if (mailbox_match && domainname_match)
4070 rc = false;
4071 }
4072 else
4073 {
4074 if (mutt_strn_equal(uid->email + 1, buf_string(sender->mailbox), sender_length))
4075 rc = false;
4076 }
4077 }
4078 }
4079 }
4080 else
4081 {
4082 mutt_any_key_to_continue(_("Failed to verify sender"));
4083 }
4084 }
4085 else
4086 {
4087 mutt_any_key_to_continue(_("Failed to figure out sender"));
4088 }
4089
4090 if (SignatureKey)
4091 {
4092 gpgme_key_unref(SignatureKey);
4093 SignatureKey = NULL;
4094 }
4095
4096 return rc;
4097}
4098
4102int smime_gpgme_verify_sender(struct Email *e, struct Message *msg)
4103{
4104 return verify_sender(e);
4105}
4106
4110void pgp_gpgme_set_sender(const char *sender)
4111{
4112 mutt_debug(LL_DEBUG2, "setting to: %s\n", sender);
4114 CurrentSender = mutt_str_dup(sender);
4115}
4116
4122{
4123 return GPGME_VERSION;
4124}
void mutt_addrlist_qualify(struct AddressList *al, const char *host)
Expand local names in an Address list using a hostname.
Definition: address.c:680
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1460
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition: address.c:480
Email Address Handling.
Email Aliases.
void mutt_expand_aliases(struct AddressList *al)
Expand aliases in a List of Addresses.
Definition: alias.c:295
GUI display the mailboxes in a side panel.
char * AutocryptSignAs
Autocrypt Key id to sign as.
Definition: config.c:37
Autocrypt end-to-end encryption.
bool buf_istr_equal(const struct Buffer *a, const struct Buffer *b)
Return if two buffers are equal, case insensitive.
Definition: buffer.c:696
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:490
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:290
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:570
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
Convenience wrapper for the config headers.
const char * cc_charset(void)
Get the cached value of $charset.
Definition: config_cache.c:116
Convenience wrapper for the core headers.
void crypt_opportunistic_encrypt(struct Email *e)
Can all recipients be determined.
Definition: crypt.c:1031
bool crypt_is_numerical_keyid(const char *s)
Is this a numerical keyid.
Definition: crypt.c:1364
SecurityFlags mutt_is_multipart_signed(struct Body *b)
Is a message signed?
Definition: crypt.c:394
SecurityFlags mutt_is_application_smime(struct Body *b)
Does the message use S/MIME?
Definition: crypt.c:595
int mutt_is_valid_multipart_pgp_encrypted(struct Body *b)
Is this a valid multi-part encrypted message?
Definition: crypt.c:453
const char * crypt_get_fingerprint_or_id(const char *p, const char **pphint, const char **ppl, const char **pps)
Get the fingerprint or long key ID.
Definition: crypt.c:1275
SecurityFlags mutt_is_malformed_multipart_pgp_encrypted(struct Body *b)
Check for malformed layout.
Definition: crypt.c:490
void crypt_convert_to_7bit(struct Body *b)
Convert an email to 7bit encoding.
Definition: crypt.c:795
SecurityFlags mutt_is_application_pgp(const struct Body *b)
Does the message use PGP?
Definition: crypt.c:534
Signing/encryption multiplexor.
static const char * crypt_short_keyid(struct CryptKeyInfo *k)
Get the short keyID for a key.
Definition: crypt_gpgme.c:180
static gpgme_error_t set_pka_sig_notation(gpgme_ctx_t ctx)
Set the signature notation.
Definition: crypt_gpgme.c:761
static struct CryptKeyInfo * crypt_getkeybyaddr(struct Address *a, KeyFlags abilities, unsigned int app, bool *forced_valid, bool oppenc_mode)
Find a key by email address.
Definition: crypt_gpgme.c:3206
static char * find_keys(const struct AddressList *addrlist, unsigned int app, bool oppenc_mode)
Find keys of the recipients of the message.
Definition: crypt_gpgme.c:3492
static bool verify_sender(struct Email *e)
Verify the sender of a message.
Definition: crypt_gpgme.c:4023
static void init_common(void)
Initialise code common to PGP and SMIME parts of GPGME.
Definition: crypt_gpgme.c:3782
static struct CryptCache * IdDefaults
Cache of GPGME keys.
Definition: crypt_gpgme.c:92
struct CryptKeyInfo * crypt_copy_key(struct CryptKeyInfo *key)
Return a copy of KEY.
Definition: crypt_gpgme.c:234
#define CRYPT_KV_STRING
Definition: crypt_gpgme.c:76
static int verify_one(struct Body *b, struct State *state, const char *tempfile, bool is_smime)
Do the actual verification step.
Definition: crypt_gpgme.c:1547
static char * CurrentSender
Email address of the sender.
Definition: crypt_gpgme.c:96
int mutt_gpgme_select_secret_key(struct Buffer *keyid)
Select a private Autocrypt key for a new account.
Definition: crypt_gpgme.c:3643
const char * mutt_gpgme_print_version(void)
Get version of GPGME.
Definition: crypt_gpgme.c:4121
static gpgme_data_t body_to_data_object(struct Body *b, bool convert)
Create GPGME object from the mail body.
Definition: crypt_gpgme.c:419
int crypt_id_is_valid(struct CryptKeyInfo *key)
Is key ID valid.
Definition: crypt_gpgme.c:310
static gpgme_data_t create_gpgme_data(void)
Create a new GPGME data object.
Definition: crypt_gpgme.c:400
static int show_sig_summary(unsigned long sum, gpgme_ctx_t ctx, gpgme_key_t key, int idx, struct State *state, gpgme_signature_t sig)
Show a signature summary.
Definition: crypt_gpgme.c:1133
static void show_fingerprint(gpgme_key_t key, struct State *state)
Write a key's fingerprint.
Definition: crypt_gpgme.c:1257
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:361
static void create_recipient_string(const char *keylist, struct Buffer *recpstring, int use_smime)
Create a string of recipients.
Definition: crypt_gpgme.c:622
static struct CryptKeyInfo * crypt_getkeybystr(const char *p, KeyFlags abilities, unsigned int app, bool *forced_valid)
Find a key by string.
Definition: crypt_gpgme.c:3340
static void print_smime_keyinfo(const char *msg, gpgme_signature_t sig, gpgme_key_t key, struct State *state)
Print key info about an SMIME key.
Definition: crypt_gpgme.c:1351
static int crypt_id_matches_addr(struct Address *addr, struct Address *u_addr, struct CryptKeyInfo *key)
Does the key ID match the address.
Definition: crypt_gpgme.c:328
bool crypt_id_is_strong(struct CryptKeyInfo *key)
Is the key strong.
Definition: crypt_gpgme.c:275
static void crypt_add_string_to_hints(const char *str, struct ListHead *hints)
Split a string and add the parts to a List.
Definition: crypt_gpgme.c:3182
const char * crypt_fpr_or_lkeyid(struct CryptKeyInfo *k)
Find the fingerprint of a key.
Definition: crypt_gpgme.c:214
static char * encrypt_gpgme_object(gpgme_data_t plaintext, char *keylist, bool use_smime, bool combined_signed, const struct AddressList *from)
Encrypt the GPGPME data object.
Definition: crypt_gpgme.c:781
#define BEGIN_PGP_SIGNATURE(_y)
Definition: crypt_gpgme.c:104
#define SIGNED_MESSAGE(_y)
Definition: crypt_gpgme.c:102
#define PKA_NOTATION_NAME
Definition: crypt_gpgme.c:98
static char * list_to_pattern(struct ListHead *list)
Convert STailQ to GPGME-compatible pattern.
Definition: crypt_gpgme.c:2962
#define CRYPT_KV_VALID
Definition: crypt_gpgme.c:74
static int get_micalg(gpgme_ctx_t ctx, int use_smime, char *buf, size_t buflen)
Find the "micalg" parameter from the last GPGME operation.
Definition: crypt_gpgme.c:853
static int data_object_to_stream(gpgme_data_t data, FILE *fp)
Write a GPGME data object to a file.
Definition: crypt_gpgme.c:514
static int pgp_check_traditional_one_body(FILE *fp, struct Body *b)
Check one inline PGP body part.
Definition: crypt_gpgme.c:2174
static const char * crypt_long_keyid(struct CryptKeyInfo *k)
Find the Long ID for the key.
Definition: crypt_gpgme.c:163
#define CRYPT_KV_STRONGID
Definition: crypt_gpgme.c:77
static struct CryptKeyInfo * get_candidates(struct ListHead *hints, SecurityFlags app, int secret)
Get a list of keys which are candidates for the selection.
Definition: crypt_gpgme.c:3029
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
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:1698
#define PUBLIC_KEY_BLOCK(_y)
Definition: crypt_gpgme.c:103
static int set_signer(gpgme_ctx_t ctx, const struct AddressList *al, bool for_smime)
Make sure that the correct signer is set.
Definition: crypt_gpgme.c:717
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:563
static void show_one_sig_validity(gpgme_ctx_t ctx, int idx, struct State *state)
Show the validity of a key used for one signature.
Definition: crypt_gpgme.c:1312
static void init_smime(void)
Initialise the SMIME crypto backend.
Definition: crypt_gpgme.c:3812
static struct CryptKeyInfo * crypt_ask_for_key(const char *tag, const char *whatfor, KeyFlags abilities, unsigned int app, bool *forced_valid)
Ask the user for a key.
Definition: crypt_gpgme.c:3423
unsigned int key_check_cap(gpgme_key_t key, enum KeyCap cap)
Check the capabilities of a key.
Definition: crypt_gpgme.c:2906
static const char * crypt_fpr(struct CryptKeyInfo *k)
Get the hexstring fingerprint from a key.
Definition: crypt_gpgme.c:199
const char * crypt_keyid(struct CryptKeyInfo *k)
Find the ID for the key.
Definition: crypt_gpgme.c:138
static SecurityFlags gpgme_send_menu(struct Email *e, bool is_smime)
Show the user the encryption/signing menu.
Definition: crypt_gpgme.c:3844
#define CRYPT_KV_ADDR
Definition: crypt_gpgme.c:75
static struct Body * sign_message(struct Body *b, const struct AddressList *from, bool use_smime)
Sign a message.
Definition: crypt_gpgme.c:906
static bool is_pka_notation(gpgme_sig_notation_t notation)
Is this the standard pka email address.
Definition: crypt_gpgme.c:112
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:2073
static int line_compare(const char *a, size_t n, const char *b)
Compare two strings ignore line endings.
Definition: crypt_gpgme.c:2156
static gpgme_key_t SignatureKey
PGP Key to sign with.
Definition: crypt_gpgme.c:94
static void crypt_key_free(struct CryptKeyInfo **keylist)
Release all the keys in a list.
Definition: crypt_gpgme.c:253
static bool set_signer_from_address(gpgme_ctx_t ctx, const char *address, bool for_smime)
Try to set the context's signer from the address.
Definition: crypt_gpgme.c:658
#define CRYPT_KV_MATCH
Definition: crypt_gpgme.c:78
static void init_pgp(void)
Initialise the PGP crypto backend.
Definition: crypt_gpgme.c:3801
static void copy_clearsigned(gpgme_data_t data, struct State *state, char *charset)
Copy a clearsigned message.
Definition: crypt_gpgme.c:2375
static int show_one_sig_status(gpgme_ctx_t ctx, int idx, struct State *state)
Show information about one signature.
Definition: crypt_gpgme.c:1424
static void print_time(time_t t, struct State *state)
Print the date/time according to the locale.
Definition: crypt_gpgme.c:891
#define MESSAGE(_y)
Definition: crypt_gpgme.c:101
Wrapper for PGP/SMIME calls to GPGME.
KeyCap
PGP/SMIME Key Capabilities.
Definition: crypt_gpgme.h:75
@ KEY_CAP_CAN_CERTIFY
Key can be used to certify.
Definition: crypt_gpgme.h:78
@ KEY_CAP_CAN_ENCRYPT
Key can be used for encryption.
Definition: crypt_gpgme.h:76
@ KEY_CAP_CAN_SIGN
Key can be used for signing.
Definition: crypt_gpgme.h:77
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition: curs_lib.c:173
void mutt_need_hard_redraw(void)
Force a hard refresh.
Definition: curs_lib.c:100
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition: curs_lib.c:443
Edit a string.
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:58
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:44
char * mutt_body_get_charset(struct Body *b, char *buf, size_t buflen)
Get a body's character set.
Definition: body.c:132
Structs that make up an email.
void mutt_parse_part(FILE *fp, struct Body *b)
Parse a MIME part.
Definition: parse.c:1817
struct Body * mutt_read_mime_header(FILE *fp, bool digest)
Parse a MIME header.
Definition: parse.c:1357
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:126
long mutt_file_get_size_fp(FILE *fp)
Get the size of a file.
Definition: file.c:1534
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:775
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1516
#define mutt_file_fclose(FP)
Definition: file.h:147
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:146
bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition: globals.c:62
bool OptPgpCheckTrust
(pseudo) used by dlg_pgp()
Definition: globals.c:73
bool crypt_keys_are_valid(struct CryptKeyInfo *keys)
Are all these keys valid?
Gpgme functions.
int pgp_gpgme_application_handler(struct Body *b, struct State *state)
Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::applicat...
Definition: crypt_gpgme.c:2431
int smime_gpgme_application_handler(struct Body *b, struct State *state)
Manage the MIME type "application/pgp" or "application/smime" - Implements CryptModuleSpecs::applicat...
Definition: crypt_gpgme.c:2813
int smime_gpgme_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
Definition: crypt_gpgme.c:1954
int pgp_gpgme_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
Definition: crypt_gpgme.c:1857
int pgp_gpgme_encrypted_handler(struct Body *b, struct State *state)
Manage a PGP or S/MIME encrypted MIME part - Implements CryptModuleSpecs::encrypted_handler() -.
Definition: crypt_gpgme.c:2722
char * smime_gpgme_find_keys(const struct AddressList *addrlist, bool oppenc_mode)
Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.
Definition: crypt_gpgme.c:3625
char * pgp_gpgme_find_keys(const struct AddressList *addrlist, bool oppenc_mode)
Find the keyids of the recipients of a message - Implements CryptModuleSpecs::find_keys() -.
Definition: crypt_gpgme.c:3617
void smime_gpgme_init(void)
Initialise the crypto module - Implements CryptModuleSpecs::init() -.
Definition: crypt_gpgme.c:3832
void pgp_gpgme_init(void)
Initialise the crypto module - Implements CryptModuleSpecs::init() -.
Definition: crypt_gpgme.c:3823
bool pgp_gpgme_check_traditional(FILE *fp, struct Body *b, bool just_one)
Look for inline (non-MIME) PGP content - Implements CryptModuleSpecs::pgp_check_traditional() -.
Definition: crypt_gpgme.c:2239
struct Body * pgp_gpgme_encrypt_message(struct Body *b, char *keylist, bool sign, const struct AddressList *from)
PGP encrypt an email - Implements CryptModuleSpecs::pgp_encrypt_message() -.
Definition: crypt_gpgme.c:1043
void pgp_gpgme_invoke_import(const char *fname)
Import a key from a message into the user's public key ring - Implements CryptModuleSpecs::pgp_invoke...
Definition: crypt_gpgme.c:2266
struct Body * pgp_gpgme_make_key_attachment(void)
Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.
Definition: crypt_gpgme.c:3724
SecurityFlags pgp_gpgme_send_menu(struct Email *e)
Ask the user whether to sign and/or encrypt the email - Implements CryptModuleSpecs::send_menu() -.
Definition: crypt_gpgme.c:4005
SecurityFlags smime_gpgme_send_menu(struct Email *e)
Ask the user whether to sign and/or encrypt the email - Implements CryptModuleSpecs::send_menu() -.
Definition: crypt_gpgme.c:4013
void pgp_gpgme_set_sender(const char *sender)
Set the sender of the email - Implements CryptModuleSpecs::set_sender() -.
Definition: crypt_gpgme.c:4110
struct Body * smime_gpgme_sign_message(struct Body *b, const struct AddressList *from)
Cryptographically sign the Body of a message - Implements CryptModuleSpecs::sign_message() -.
Definition: crypt_gpgme.c:1035
struct Body * pgp_gpgme_sign_message(struct Body *b, const struct AddressList *from)
Cryptographically sign the Body of a message - Implements CryptModuleSpecs::sign_message() -.
Definition: crypt_gpgme.c:1027
struct Body * smime_gpgme_build_smime_entity(struct Body *b, char *keylist)
Encrypt the email body to all recipients - Implements CryptModuleSpecs::smime_build_smime_entity() -.
Definition: crypt_gpgme.c:1089
int smime_gpgme_verify_sender(struct Email *e, struct Message *msg)
Does the sender match the certificate? - Implements CryptModuleSpecs::smime_verify_sender() -.
Definition: crypt_gpgme.c:4102
int pgp_gpgme_verify_one(struct Body *b, struct State *state, const char *tempfile)
Check a signed MIME part against a signature - Implements CryptModuleSpecs::verify_one() -.
Definition: crypt_gpgme.c:1672
int smime_gpgme_verify_one(struct Body *b, struct State *state, const char *tempfile)
Check a signed MIME part against a signature - Implements CryptModuleSpecs::verify_one() -.
Definition: crypt_gpgme.c:1680
struct CryptKeyInfo * dlg_gpgme(struct CryptKeyInfo *keys, struct Address *p, const char *s, unsigned int app, bool *forced_valid)
Get the user to select a key -.
Definition: dlg_gpgme.c:637
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition: window.c:274
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition: question.c:64
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_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
Convenience wrapper for the gui headers.
int mutt_body_handler(struct Body *b, struct State *state)
Handler for the Body of an email.
Definition: handler.c:1631
void mutt_decode_attachment(const struct Body *b, struct State *state)
Decode an email's attachment.
Definition: handler.c:1905
Decide how to display email content.
int mutt_write_mime_header(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Create a MIME header.
Definition: header.c:756
Read/write command history from/to a file.
@ HC_OTHER
Miscellaneous strings.
Definition: lib.h:56
void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
Find crypto hooks for an Address.
Definition: hook.c:870
Parse and execute user-defined hooks.
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:268
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define FREE(x)
Definition: memory.h:45
@ ENC_7BIT
7-bit text
Definition: mime.h:49
@ ENC_BASE64
Base-64 encoded text.
Definition: mime.h:52
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition: mime.h:37
@ TYPE_APPLICATION
Type: 'application/*'.
Definition: mime.h:33
@ TYPE_TEXT
Type: 'text/*'.
Definition: mime.h:38
@ DISP_ATTACH
Content is attached.
Definition: mime.h:63
@ DISP_INLINE
Content is inline.
Definition: mime.h:62
@ DISP_NONE
No preferred disposition.
Definition: mime.h:65
#define is_multipart(body)
Definition: mime.h:82
void mutt_generate_boundary(struct ParameterList *pl)
Create a unique boundary id for a MIME part.
Definition: multipart.c:86
bool mutt_ch_check_charset(const char *cs, bool strict)
Does iconv understand a character set?
Definition: charset.c:894
int mutt_ch_fgetconv(struct FgetConv *fc)
Convert a file's character set.
Definition: charset.c:983
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:933
char * mutt_ch_fgetconvs(char *buf, size_t buflen, struct FgetConv *fc)
Convert a file's charset into a string buffer.
Definition: charset.c:1045
void mutt_ch_fgetconv_close(struct FgetConv **ptr)
Close an fgetconv handle.
Definition: charset.c:965
#define MUTT_ICONV_HOOK_FROM
apply charset-hooks to fromcode
Definition: charset.h:73
size_t mutt_date_localtime_format(char *buf, size_t buflen, const char *format, time_t t)
Format localtime.
Definition: date.c:950
Convenience wrapper for the library headers.
#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
#define STATE_NO_FLAGS
No flags are set.
Definition: state.h:32
int mutt_istrn_cmp(const char *a, const char *b, size_t num)
Compare two strings ignoring case (to a maximum), safely.
Definition: string.c:433
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:666
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition: string.c:307
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:654
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:419
const char * mutt_istr_find(const char *haystack, const char *needle)
Find first occurrence of string (ignoring case)
Definition: string.c:515
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:490
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:575
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:274
Many unsorted constants and some structs.
#define MUTT_COMP_NO_FLAGS
No flags are set.
Definition: mutt.h:56
int mutt_decode_save_attachment(FILE *fp, struct Body *b, const char *path, StateFlags flags, enum SaveAttach opt)
Decode, then save an attachment.
Definition: mutt_attach.c:1039
@ MUTT_SAVE_NO_FLAGS
No flags set.
Definition: mutt_attach.h:58
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
#define KEYFLAG_EXPIRED
Key is expired.
Definition: lib.h:131
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition: lib.h:76
#define SEC_OPPENCRYPT
Opportunistic encrypt mode.
Definition: lib.h:86
#define KEYFLAG_ISX509
Key is an X.509 key.
Definition: lib.h:129
uint16_t KeyFlags
Flags describing PGP/SMIME keys, e.g. KEYFLAG_CANSIGN.
Definition: lib.h:125
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition: lib.h:128
#define KEYFLAG_CANTUSE
Definition: lib.h:139
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:91
#define KEYFLAG_NO_FLAGS
No flags are set.
Definition: lib.h:126
#define SEC_NO_FLAGS
No flags are set.
Definition: lib.h:77
#define KEYFLAG_DISABLED
Key is marked disabled.
Definition: lib.h:133
#define SEC_ENCRYPT
Email is encrypted.
Definition: lib.h:78
#define KEYFLAG_REVOKED
Key is revoked.
Definition: lib.h:132
#define SEC_SIGN
Email is signed.
Definition: lib.h:79
#define KEYFLAG_CANSIGN
Key is suitable for signing.
Definition: lib.h:127
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition: parameter.c:111
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
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
Definition: quad.h:37
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition: quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
Ask the user a question.
enum QuadOption query_yesorno_help(const char *prompt, enum QuadOption def, struct ConfigSubset *sub, const char *name)
Ask the user a Yes/No question offering help.
Definition: question.c:342
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
#define TAILQ_EMPTY(head)
Definition: queue.h:721
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
int mutt_write_mime_body(struct Body *b, FILE *fp, struct ConfigSubset *sub)
Write a MIME part.
Definition: body.c:300
Convenience wrapper for the send headers.
void mutt_update_encoding(struct Body *b, struct ConfigSubset *sub)
Update the encoding type.
Definition: sendlib.c:421
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition: sendlib.c:706
GUI display the mailboxes in a side panel.
Key value store.
#define NONULL(x)
Definition: string2.h:37
An email address.
Definition: address.h:36
struct Buffer * personal
Real name of address.
Definition: address.h:37
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
The body of an email.
Definition: body.h:36
char * d_filename
filename to be used for the content-disposition header If NULL, filename is used instead.
Definition: body.h:56
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:72
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition: body.h:67
bool badsig
Bad cryptographic signature (needed to check encrypted s/mime-signatures)
Definition: body.h:43
struct Envelope * mime_headers
Memory hole protected headers.
Definition: body.h:75
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
struct ParameterList parameter
Parameters of the content-type.
Definition: body.h:62
bool use_disp
Content-Disposition uses filename= ?
Definition: body.h:47
char * description
content-description
Definition: body.h:55
unsigned int disposition
content-disposition, ContentDisposition
Definition: body.h:42
struct Body * next
next attachment in the list
Definition: body.h:71
char * subtype
content-type subtype
Definition: body.h:60
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition: body.h:41
bool goodsig
Good cryptographic signature.
Definition: body.h:45
bool warnsig
Maybe good signature.
Definition: body.h:48
unsigned int type
content-type primary type, ContentType
Definition: body.h:40
char * filename
When sending a message, this is the file to which this structure refers.
Definition: body.h:58
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
Internal cache for GPGME.
Definition: crypt_gpgme.c:85
char * what
Definition: crypt_gpgme.c:86
char * dflt
Definition: crypt_gpgme.c:87
struct CryptCache * next
Definition: crypt_gpgme.c:88
A stored PGP key.
Definition: crypt_gpgme.h:44
gpgme_validity_t validity
uid validity (cached for convenience)
Definition: crypt_gpgme.h:50
KeyFlags flags
global and per uid flags (for convenience)
Definition: crypt_gpgme.h:49
int idx
and the user ID at this index
Definition: crypt_gpgme.h:47
struct CryptKeyInfo * next
Linked list.
Definition: crypt_gpgme.h:45
const char * uid
and for convenience point to this user ID
Definition: crypt_gpgme.h:48
gpgme_key_t kobj
Definition: crypt_gpgme.h:46
The envelope/body of an email.
Definition: email.h:39
struct Envelope * env
Envelope information.
Definition: email.h:68
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:43
struct AddressList sender
Email's sender.
Definition: envelope.h:63
struct AddressList from
Email's 'From' list.
Definition: envelope.h:59
Cursor for converting a file's encoding.
Definition: charset.h:42
FILE * fp
Definition: charset.h:43
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
A local copy of an email.
Definition: message.h:34
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
Keep track when processing files.
Definition: state.h:48
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition: state.h:52
FILE * fp_out
File to write to.
Definition: state.h:50
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
int cs_subset_str_string_set(const struct ConfigSubset *sub, const char *name, const char *value, struct Buffer *err)
Set a config item by string.
Definition: subset.c:386
#define buf_mktemp(buf)
Definition: tmp.h:33
#define mutt_file_mkstemp()
Definition: tmp.h:36