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.
30 #ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
33 #ifdef HAVE_SYS_RESOURCE_H
34 # include <sys/resource.h>
37 #include <lib-lib/str.h>
38 #include <lib-lib/file.h>
39 #include <lib-lib/ascii.h>
40 #include <lib-lib/mem.h>
41 #include <lib-lib/macros.h>
43 #include <lib-mime/mime.h>
45 #include <lib-ui/curses.h>
54 /* print the current time to avoid spoofing of the signature output */
55 void crypt_current_time (STATE * s, const char *app_name)
58 char p[STRING], tmp[STRING];
60 if (option (OPTCRYPTTIMESTAMP)) {
62 setlocale (LC_TIME, "");
63 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
64 setlocale (LC_TIME, "C");
69 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
70 NONULL (app_name), p);
71 state_attach_puts (tmp, s);
76 void crypt_forget_passphrase (void)
78 crypt_pgp_void_passphrase ();
79 crypt_smime_void_passphrase ();
80 mutt_message _("Passphrase(s) forgotten.");
84 #if defined(HAVE_SETRLIMIT)
86 static void disable_coredumps (void)
88 struct rlimit rl = { 0, 0 };
89 static short done = 0;
92 setrlimit (RLIMIT_CORE, &rl);
97 #endif /* HAVE_SETRLIMIT */
100 int crypt_valid_passphrase (int flags)
104 # if defined(HAVE_SETRLIMIT)
105 disable_coredumps ();
108 if (flags & APPLICATION_PGP)
109 ret = crypt_pgp_valid_passphrase ();
111 if (flags & APPLICATION_SMIME)
112 ret = crypt_smime_valid_passphrase ();
119 int mutt_protect (HEADER * msg, char *keylist)
121 BODY *pbody = NULL, *tmp_pbody = NULL;
122 BODY *tmp_smime_pbody = NULL;
123 BODY *tmp_pgp_pbody = NULL;
124 int flags = msg->security;
127 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
130 if ((msg->security & PGPINLINE) == PGPINLINE) {
131 /* they really want to send it inline... go for it */
133 mutt_endwin _("Invoking PGP...");
135 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
137 msg->content = pbody;
141 /* otherwise inline won't work...ask for revert */
143 query_quadoption (OPT_PGPMIMEAUTO,
145 ("Message can't be sent inline. Revert to using PGP/MIME?")))
147 mutt_error _("Mail not sent.");
152 /* go ahead with PGP/MIME */
158 tmp_smime_pbody = msg->content;
159 tmp_pgp_pbody = msg->content;
161 if (msg->security & SIGN) {
162 if (msg->security & APPLICATION_SMIME) {
163 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
165 pbody = tmp_smime_pbody = tmp_pbody;
168 if ((msg->security & APPLICATION_PGP)
169 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
170 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
174 pbody = tmp_pgp_pbody = tmp_pbody;
177 if ((msg->security & APPLICATION_SMIME)
178 && (msg->security & APPLICATION_PGP)) {
179 /* here comes the draft ;-) */
184 if (msg->security & ENCRYPT) {
185 if ((msg->security & APPLICATION_SMIME)) {
186 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
188 /* signed ? free it! */
191 /* free tmp_body if messages was signed AND encrypted ... */
192 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
193 /* detatch and dont't delete msg->content,
194 which tmp_smime_pbody->parts after signing. */
195 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
196 msg->content->next = NULL;
197 body_list_wipe(&tmp_smime_pbody);
202 if ((msg->security & APPLICATION_PGP)) {
203 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
206 /* did we perform a retainable signature? */
207 if (flags != msg->security) {
208 /* remove the outer multipart layer */
209 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
210 /* get rid of the signature */
211 body_list_wipe(&tmp_pgp_pbody->next);
217 /* destroy temporary signature envelope when doing retainable
221 if (flags != msg->security) {
222 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
223 body_list_wipe(&tmp_pgp_pbody->next);
229 msg->content = pbody;
235 int crypt_query (BODY * m)
242 if (m->type == TYPEAPPLICATION) {
243 t |= mutt_is_application_pgp (m);
245 t |= mutt_is_application_smime (m);
251 else if (m->type == TYPETEXT) {
252 t |= mutt_is_application_pgp (m);
257 if (m->type == TYPEMULTIPART) {
258 t |= mutt_is_multipart_encrypted (m);
259 t |= mutt_is_multipart_signed (m);
265 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
269 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
270 w = 0; /* Bits set in any part */
272 for (p = m->parts; p; p = p->next) {
277 t |= u | (w & ~GOODSIGN);
279 if ((w & GOODSIGN) && !(u & GOODSIGN))
287 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
294 if (!(fp = safe_fopen (tempfile, "w"))) {
295 mutt_perror (tempfile);
299 fseeko (s->fpin, a->hdr_offset, 0);
300 bytes = a->length + a->offset - a->hdr_offset;
303 if ((c = fgetc (s->fpin)) == EOF)
311 if (c == '\n' && !hadcr)
327 void convert_to_7bit (BODY * a)
330 if (a->type == TYPEMULTIPART) {
331 if (a->encoding != ENC7BIT) {
332 a->encoding = ENC7BIT;
333 convert_to_7bit (a->parts);
335 else if (option (OPTPGPSTRICTENC))
336 convert_to_7bit (a->parts);
338 else if (a->type == TYPEMESSAGE &&
339 m_strcasecmp(a->subtype, "delivery-status")) {
340 if (a->encoding != ENC7BIT)
341 mutt_message_to_7bit (a, NULL);
343 else if (a->encoding == ENC8BIT)
344 a->encoding = ENCQUOTEDPRINTABLE;
345 else if (a->encoding == ENCBINARY)
346 a->encoding = ENCBASE64;
347 else if (a->content && a->encoding != ENCBASE64 &&
348 (a->content->from || (a->content->space &&
349 option (OPTPGPSTRICTENC))))
350 a->encoding = ENCQUOTEDPRINTABLE;
356 void crypt_extract_keys_from_messages (HEADER * h)
359 char tempfname[_POSIX_PATH_MAX], *mbox;
360 address_t *tmp = NULL;
363 mutt_mktemp (tempfname);
364 if (!(fpout = safe_fopen (tempfname, "w"))) {
365 mutt_perror (tempfname);
369 set_option (OPTDONTHANDLEPGPKEYS);
372 for (i = 0; i < Context->vcount; i++) {
373 if (Context->hdrs[Context->v2r[i]]->tagged) {
374 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
375 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
376 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
382 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP) {
383 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
384 M_CM_DECODE | M_CM_CHARCONV, 0);
387 mutt_endwin (_("Trying to extract PGP keys...\n"));
388 crypt_pgp_invoke_import (tempfname);
391 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME) {
392 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
393 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
394 M_CM_NOHEADER | M_CM_DECODE_CRYPT
395 | M_CM_DECODE_SMIME, 0);
397 mutt_copy_message (fpout, Context,
398 Context->hdrs[Context->v2r[i]], 0, 0);
401 if (Context->hdrs[Context->v2r[i]]->env->from)
402 tmp = mutt_expand_aliases (h->env->from);
403 else if (Context->hdrs[Context->v2r[i]]->env->sender)
404 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
406 mbox = tmp ? tmp->mailbox : NULL;
408 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
409 crypt_smime_invoke_import (tempfname, mbox);
419 mutt_parse_mime_message (Context, h);
420 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
421 if (h->security & APPLICATION_PGP) {
422 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
424 mutt_endwin (_("Trying to extract PGP keys...\n"));
425 crypt_pgp_invoke_import (tempfname);
428 if (h->security & APPLICATION_SMIME) {
429 if (h->security & ENCRYPT)
430 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
431 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
433 mutt_copy_message (fpout, Context, h, 0, 0);
437 tmp = mutt_expand_aliases (h->env->from);
438 else if (h->env->sender)
439 tmp = mutt_expand_aliases (h->env->sender);
440 mbox = tmp ? tmp->mailbox : NULL;
441 if (mbox) { /* else ? */
442 mutt_message (_("Trying to extract S/MIME certificates...\n"));
443 crypt_smime_invoke_import (tempfname, mbox);
451 mutt_any_key_to_continue (NULL);
453 mutt_unlink (tempfname);
455 unset_option (OPTDONTHANDLEPGPKEYS);
460 int crypt_get_keys (HEADER * msg, char **keylist)
462 /* Do a quick check to make sure that we can find all of the encryption
463 * keys if the user has requested this service.
466 set_option (OPTPGPCHECKTRUST);
470 if (msg->security & ENCRYPT) {
471 if (msg->security & APPLICATION_PGP) {
472 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
473 msg->env->bcc)) == NULL)
475 unset_option (OPTPGPCHECKTRUST);
477 if (msg->security & APPLICATION_SMIME) {
478 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
479 msg->env->bcc)) == NULL)
489 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
491 for (; a; a = a->next) {
492 if (a->type == TYPEMULTIPART)
493 crypt_fetch_signatures (signatures, a->parts, n);
496 p_realloc(signatures, *n + 6);
498 (*signatures)[(*n)++] = a;
505 * This routine verifies a "multipart/signed" body.
508 int mutt_signed_handler (BODY * a, STATE * s)
510 char tempfile[_POSIX_PATH_MAX];
512 int protocol_major = TYPEOTHER;
513 char *protocol_minor = NULL;
516 BODY **signatures = NULL;
522 protocol = parameter_getval(a->parameter, "protocol");
525 /* extract the protocol information */
531 if ((protocol_minor = strchr (protocol, '/')))
534 m_strcpy(major, sizeof(major), protocol);
535 if ((t = strchr (major, '/')))
538 protocol_major = mutt_check_mime_type (major);
541 /* consistency check */
543 if (!(a && a->next && a->next->type == protocol_major &&
544 !m_strcasecmp(a->next->subtype, protocol_minor))) {
545 state_attach_puts (_("[-- Error: "
546 "Inconsistent multipart/signed structure! --]\n\n"),
548 return mutt_body_handler (a, s);
552 if (protocol_major == TYPEAPPLICATION
553 && !m_strcasecmp(protocol_minor, "pgp-signature"));
554 else if (protocol_major == TYPEAPPLICATION
555 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
556 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
557 else if (protocol_major == TYPEMULTIPART
558 && !m_strcasecmp(protocol_minor, "mixed"));
560 state_printf (s, _("[-- Error: "
561 "Unknown multipart/signed protocol %s! --]\n\n"),
563 return mutt_body_handler (a, s);
566 if (s->flags & M_DISPLAY) {
568 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
571 mutt_mktemp (tempfile);
572 if (crypt_write_signed (a, s, tempfile) == 0) {
573 for (i = 0; i < sigcnt; i++) {
574 if (signatures[i]->type == TYPEAPPLICATION
575 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
576 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
582 if (signatures[i]->type == TYPEAPPLICATION
583 && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
584 || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
586 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
592 state_printf (s, _("[-- Warning: "
593 "We can't verify %s/%s signatures. --]\n\n"),
594 TYPE (signatures[i]), signatures[i]->subtype);
598 mutt_unlink (tempfile);
600 b->goodsig = goodsig;
601 b->badsig = !goodsig;
603 /* Now display the signed body */
604 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
607 p_delete(&signatures);
610 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
614 rc = mutt_body_handler (a, s);
616 if (s->flags & M_DISPLAY && sigcnt)
617 state_attach_puts (_("\n[-- End of signed data --]\n"), s);