2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
6 * Oliver Ehli <elmy@acm.org>
7 * Copyright (C) 2003 Werner Koch <wk@gnupg.org>
8 * Copyright (C) 2004 g10code GmbH
10 * This file is part of mutt-ng, see http://www.muttng.org/.
11 * It's licensed under the GNU General Public License,
12 * please see the file GPL in the top level source directory.
15 #include <lib-lib/lib-lib.h>
20 #ifdef HAVE_SYS_RESOURCE_H
21 # include <sys/resource.h>
24 #include <lib-mime/mime.h>
26 #include <lib-ui/curses.h>
27 #include <lib-mx/mx.h>
35 static int crypt_write_signed (BODY *, STATE *, FILE *);
37 /* print the current time to avoid spoofing of the signature output */
38 void crypt_current_time (STATE * s, const char *app_name)
41 char p[STRING], tmp[STRING];
43 if (option (OPTCRYPTTIMESTAMP)) {
45 setlocale (LC_TIME, "");
46 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
47 setlocale (LC_TIME, "C");
52 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
53 NONULL (app_name), p);
54 state_attach_puts (tmp, s);
59 void crypt_forget_passphrase (void)
61 crypt_pgp_void_passphrase ();
62 crypt_smime_void_passphrase ();
63 mutt_message _("Passphrase(s) forgotten.");
67 #if defined(HAVE_SETRLIMIT)
69 static void disable_coredumps (void)
71 struct rlimit rl = { 0, 0 };
72 static short done = 0;
75 setrlimit (RLIMIT_CORE, &rl);
80 #endif /* HAVE_SETRLIMIT */
83 int crypt_valid_passphrase (int flags)
87 # if defined(HAVE_SETRLIMIT)
91 if (flags & APPLICATION_PGP)
92 ret = crypt_pgp_valid_passphrase ();
94 if (flags & APPLICATION_SMIME)
95 ret = crypt_smime_valid_passphrase ();
102 int mutt_protect (HEADER * msg, char *keylist)
104 BODY *pbody = NULL, *tmp_pbody = NULL;
105 BODY *tmp_smime_pbody = NULL;
106 BODY *tmp_pgp_pbody = NULL;
107 int flags = msg->security;
110 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
113 if ((msg->security & PGPINLINE) == PGPINLINE) {
114 /* they really want to send it inline... go for it */
116 mutt_endwin _("Invoking PGP...");
118 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
120 msg->content = pbody;
124 /* otherwise inline won't work...ask for revert */
126 query_quadoption (OPT_PGPMIMEAUTO,
128 ("Message can't be sent inline. Revert to using PGP/MIME?")))
130 mutt_error _("Mail not sent.");
135 /* go ahead with PGP/MIME */
141 tmp_smime_pbody = msg->content;
142 tmp_pgp_pbody = msg->content;
144 if (msg->security & SIGN) {
145 if (msg->security & APPLICATION_SMIME) {
146 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
148 pbody = tmp_smime_pbody = tmp_pbody;
151 if ((msg->security & APPLICATION_PGP)
152 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
153 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
157 pbody = tmp_pgp_pbody = tmp_pbody;
160 if ((msg->security & APPLICATION_SMIME)
161 && (msg->security & APPLICATION_PGP)) {
162 /* here comes the draft ;-) */
167 if (msg->security & ENCRYPT) {
168 if ((msg->security & APPLICATION_SMIME)) {
169 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
171 /* signed ? free it! */
174 /* free tmp_body if messages was signed AND encrypted ... */
175 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
176 /* detatch and dont't delete msg->content,
177 which tmp_smime_pbody->parts after signing. */
178 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
179 msg->content->next = NULL;
180 body_list_wipe(&tmp_smime_pbody);
185 if ((msg->security & APPLICATION_PGP)) {
186 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
189 /* did we perform a retainable signature? */
190 if (flags != msg->security) {
191 /* remove the outer multipart layer */
192 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
193 /* get rid of the signature */
194 body_list_wipe(&tmp_pgp_pbody->next);
200 /* destroy temporary signature envelope when doing retainable
204 if (flags != msg->security) {
205 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
206 body_list_wipe(&tmp_pgp_pbody->next);
212 msg->content = pbody;
218 int crypt_query (BODY * m)
225 if (m->type == TYPEAPPLICATION) {
226 t |= mutt_is_application_pgp (m);
228 t |= mutt_is_application_smime (m);
234 else if (m->type == TYPETEXT) {
235 t |= mutt_is_application_pgp (m);
240 if (m->type == TYPEMULTIPART) {
241 t |= mutt_is_multipart_encrypted (m);
242 t |= mutt_is_multipart_signed (m);
248 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
252 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
253 w = 0; /* Bits set in any part */
255 for (p = m->parts; p; p = p->next) {
260 t |= u | (w & ~GOODSIGN);
262 if ((w & GOODSIGN) && !(u & GOODSIGN))
270 static int crypt_write_signed (BODY * a, STATE * s, FILE *fp)
276 fseeko (s->fpin, a->hdr_offset, 0);
277 bytes = a->length + a->offset - a->hdr_offset;
280 if ((c = fgetc (s->fpin)) == EOF)
288 if (c == '\n' && !hadcr)
304 void convert_to_7bit (BODY * a)
307 if (a->type == TYPEMULTIPART) {
308 if (a->encoding != ENC7BIT) {
309 a->encoding = ENC7BIT;
310 convert_to_7bit (a->parts);
312 else if (option (OPTPGPSTRICTENC))
313 convert_to_7bit (a->parts);
315 else if (a->type == TYPEMESSAGE &&
316 m_strcasecmp(a->subtype, "delivery-status")) {
317 if (a->encoding != ENC7BIT)
318 mutt_message_to_7bit (a, NULL);
320 else if (a->encoding == ENC8BIT)
321 a->encoding = ENCQUOTEDPRINTABLE;
322 else if (a->encoding == ENCBINARY)
323 a->encoding = ENCBASE64;
324 else if (a->content && a->encoding != ENCBASE64 &&
325 (a->content->from || (a->content->space &&
326 option (OPTPGPSTRICTENC))))
327 a->encoding = ENCQUOTEDPRINTABLE;
333 void crypt_extract_keys_from_messages (HEADER * h)
336 char tempfname[_POSIX_PATH_MAX], *mbox;
337 address_t *tmp = NULL;
340 fpout = m_tempfile(tempfname, sizeof(tempfname), NONULL(Tempdir), NULL);
342 mutt_error(_("Could not create temporary file"));
346 set_option (OPTDONTHANDLEPGPKEYS);
349 for (i = 0; i < Context->vcount; i++) {
350 if (Context->hdrs[Context->v2r[i]]->tagged) {
351 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
352 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
353 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
359 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP) {
360 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
361 M_CM_DECODE | M_CM_CHARCONV, 0);
364 mutt_endwin (_("Trying to extract PGP keys...\n"));
365 crypt_pgp_invoke_import (tempfname);
368 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME) {
369 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
370 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
371 M_CM_NOHEADER | M_CM_DECODE_CRYPT
372 | M_CM_DECODE_SMIME, 0);
374 mutt_copy_message (fpout, Context,
375 Context->hdrs[Context->v2r[i]], 0, 0);
378 if (Context->hdrs[Context->v2r[i]]->env->from)
379 tmp = mutt_expand_aliases (h->env->from);
380 else if (Context->hdrs[Context->v2r[i]]->env->sender)
381 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
383 mbox = tmp ? tmp->mailbox : NULL;
385 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
386 crypt_smime_invoke_import (tempfname, mbox);
396 mutt_parse_mime_message (Context, h);
397 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
398 if (h->security & APPLICATION_PGP) {
399 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
401 mutt_endwin (_("Trying to extract PGP keys...\n"));
402 crypt_pgp_invoke_import (tempfname);
405 if (h->security & APPLICATION_SMIME) {
406 if (h->security & ENCRYPT)
407 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
408 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
410 mutt_copy_message (fpout, Context, h, 0, 0);
414 tmp = mutt_expand_aliases (h->env->from);
415 else if (h->env->sender)
416 tmp = mutt_expand_aliases (h->env->sender);
417 mbox = tmp ? tmp->mailbox : NULL;
418 if (mbox) { /* else ? */
419 mutt_message (_("Trying to extract S/MIME certificates...\n"));
420 crypt_smime_invoke_import (tempfname, mbox);
428 mutt_any_key_to_continue (NULL);
430 mutt_unlink (tempfname);
432 unset_option (OPTDONTHANDLEPGPKEYS);
437 int crypt_get_keys (HEADER * msg, char **keylist)
439 /* Do a quick check to make sure that we can find all of the encryption
440 * keys if the user has requested this service.
443 set_option (OPTPGPCHECKTRUST);
447 if (msg->security & ENCRYPT) {
448 if (msg->security & APPLICATION_PGP) {
449 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
450 msg->env->bcc)) == NULL)
452 unset_option (OPTPGPCHECKTRUST);
454 if (msg->security & APPLICATION_SMIME) {
455 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
456 msg->env->bcc)) == NULL)
466 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
468 for (; a; a = a->next) {
469 if (a->type == TYPEMULTIPART)
470 crypt_fetch_signatures (signatures, a->parts, n);
473 p_realloc(signatures, *n + 6);
475 (*signatures)[(*n)++] = a;
482 * This routine verifies a "multipart/signed" body.
485 int mutt_signed_handler (BODY * a, STATE * s)
487 char tempfile[_POSIX_PATH_MAX];
490 int protocol_major = TYPEOTHER;
491 char *protocol_minor = NULL;
494 BODY **signatures = NULL;
500 protocol = parameter_getval(a->parameter, "protocol");
503 /* extract the protocol information */
509 if ((protocol_minor = strchr (protocol, '/')))
512 m_strcpy(major, sizeof(major), protocol);
513 if ((t = strchr (major, '/')))
516 protocol_major = mutt_check_mime_type (major);
519 /* consistency check */
521 if (!(a && a->next && a->next->type == protocol_major &&
522 !m_strcasecmp(a->next->subtype, protocol_minor))) {
523 state_attach_puts (_("[-- Error: "
524 "Inconsistent multipart/signed structure! --]\n\n"),
526 return mutt_body_handler (a, s);
530 if (protocol_major == TYPEAPPLICATION
531 && !m_strcasecmp(protocol_minor, "pgp-signature"));
532 else if (protocol_major == TYPEAPPLICATION
533 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
534 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
535 else if (protocol_major == TYPEMULTIPART
536 && !m_strcasecmp(protocol_minor, "mixed"));
538 state_printf (s, _("[-- Error: "
539 "Unknown multipart/signed protocol %s! --]\n\n"),
541 return mutt_body_handler (a, s);
544 if (s->flags & M_DISPLAY) {
546 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
549 tempfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
551 mutt_perror (tempfile);
554 if (crypt_write_signed (a, s, tempfp) == 0) {
555 for (i = 0; i < sigcnt; i++) {
556 if (signatures[i]->type == TYPEAPPLICATION
557 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
558 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
564 if (signatures[i]->type == TYPEAPPLICATION
565 && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
566 || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
568 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
574 state_printf (s, _("[-- Warning: "
575 "We can't verify %s/%s signatures. --]\n\n"),
576 TYPE (signatures[i]), signatures[i]->subtype);
581 mutt_unlink (tempfile);
583 b->goodsig = goodsig;
584 b->badsig = !goodsig;
586 /* Now display the signed body */
587 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
590 p_delete(&signatures);
593 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
597 rc = mutt_body_handler (a, s);
599 if (s->flags & M_DISPLAY && sigcnt)
600 state_attach_puts (_("\n[-- End of signed data --]\n"), s);