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/lib-lib.h>
39 #include <lib-mime/mime.h>
41 #include <lib-ui/curses.h>
50 /* print the current time to avoid spoofing of the signature output */
51 void crypt_current_time (STATE * s, const char *app_name)
54 char p[STRING], tmp[STRING];
56 if (option (OPTCRYPTTIMESTAMP)) {
58 setlocale (LC_TIME, "");
59 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
60 setlocale (LC_TIME, "C");
65 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
66 NONULL (app_name), p);
67 state_attach_puts (tmp, s);
72 void crypt_forget_passphrase (void)
74 crypt_pgp_void_passphrase ();
75 crypt_smime_void_passphrase ();
76 mutt_message _("Passphrase(s) forgotten.");
80 #if defined(HAVE_SETRLIMIT)
82 static void disable_coredumps (void)
84 struct rlimit rl = { 0, 0 };
85 static short done = 0;
88 setrlimit (RLIMIT_CORE, &rl);
93 #endif /* HAVE_SETRLIMIT */
96 int crypt_valid_passphrase (int flags)
100 # if defined(HAVE_SETRLIMIT)
101 disable_coredumps ();
104 if (flags & APPLICATION_PGP)
105 ret = crypt_pgp_valid_passphrase ();
107 if (flags & APPLICATION_SMIME)
108 ret = crypt_smime_valid_passphrase ();
115 int mutt_protect (HEADER * msg, char *keylist)
117 BODY *pbody = NULL, *tmp_pbody = NULL;
118 BODY *tmp_smime_pbody = NULL;
119 BODY *tmp_pgp_pbody = NULL;
120 int flags = msg->security;
123 if ((msg->security & SIGN) && !crypt_valid_passphrase (msg->security))
126 if ((msg->security & PGPINLINE) == PGPINLINE) {
127 /* they really want to send it inline... go for it */
129 mutt_endwin _("Invoking PGP...");
131 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
133 msg->content = pbody;
137 /* otherwise inline won't work...ask for revert */
139 query_quadoption (OPT_PGPMIMEAUTO,
141 ("Message can't be sent inline. Revert to using PGP/MIME?")))
143 mutt_error _("Mail not sent.");
148 /* go ahead with PGP/MIME */
154 tmp_smime_pbody = msg->content;
155 tmp_pgp_pbody = msg->content;
157 if (msg->security & SIGN) {
158 if (msg->security & APPLICATION_SMIME) {
159 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
161 pbody = tmp_smime_pbody = tmp_pbody;
164 if ((msg->security & APPLICATION_PGP)
165 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
166 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
170 pbody = tmp_pgp_pbody = tmp_pbody;
173 if ((msg->security & APPLICATION_SMIME)
174 && (msg->security & APPLICATION_PGP)) {
175 /* here comes the draft ;-) */
180 if (msg->security & ENCRYPT) {
181 if ((msg->security & APPLICATION_SMIME)) {
182 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
184 /* signed ? free it! */
187 /* free tmp_body if messages was signed AND encrypted ... */
188 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
189 /* detatch and dont't delete msg->content,
190 which tmp_smime_pbody->parts after signing. */
191 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
192 msg->content->next = NULL;
193 body_list_wipe(&tmp_smime_pbody);
198 if ((msg->security & APPLICATION_PGP)) {
199 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
202 /* did we perform a retainable signature? */
203 if (flags != msg->security) {
204 /* remove the outer multipart layer */
205 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
206 /* get rid of the signature */
207 body_list_wipe(&tmp_pgp_pbody->next);
213 /* destroy temporary signature envelope when doing retainable
217 if (flags != msg->security) {
218 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
219 body_list_wipe(&tmp_pgp_pbody->next);
225 msg->content = pbody;
231 int crypt_query (BODY * m)
238 if (m->type == TYPEAPPLICATION) {
239 t |= mutt_is_application_pgp (m);
241 t |= mutt_is_application_smime (m);
247 else if (m->type == TYPETEXT) {
248 t |= mutt_is_application_pgp (m);
253 if (m->type == TYPEMULTIPART) {
254 t |= mutt_is_multipart_encrypted (m);
255 t |= mutt_is_multipart_signed (m);
261 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
265 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
266 w = 0; /* Bits set in any part */
268 for (p = m->parts; p; p = p->next) {
273 t |= u | (w & ~GOODSIGN);
275 if ((w & GOODSIGN) && !(u & GOODSIGN))
283 int crypt_write_signed (BODY * a, STATE * s, const char *tempfile)
290 if (!(fp = safe_fopen (tempfile, "w"))) {
291 mutt_perror (tempfile);
295 fseeko (s->fpin, a->hdr_offset, 0);
296 bytes = a->length + a->offset - a->hdr_offset;
299 if ((c = fgetc (s->fpin)) == EOF)
307 if (c == '\n' && !hadcr)
323 void convert_to_7bit (BODY * a)
326 if (a->type == TYPEMULTIPART) {
327 if (a->encoding != ENC7BIT) {
328 a->encoding = ENC7BIT;
329 convert_to_7bit (a->parts);
331 else if (option (OPTPGPSTRICTENC))
332 convert_to_7bit (a->parts);
334 else if (a->type == TYPEMESSAGE &&
335 m_strcasecmp(a->subtype, "delivery-status")) {
336 if (a->encoding != ENC7BIT)
337 mutt_message_to_7bit (a, NULL);
339 else if (a->encoding == ENC8BIT)
340 a->encoding = ENCQUOTEDPRINTABLE;
341 else if (a->encoding == ENCBINARY)
342 a->encoding = ENCBASE64;
343 else if (a->content && a->encoding != ENCBASE64 &&
344 (a->content->from || (a->content->space &&
345 option (OPTPGPSTRICTENC))))
346 a->encoding = ENCQUOTEDPRINTABLE;
352 void crypt_extract_keys_from_messages (HEADER * h)
355 char tempfname[_POSIX_PATH_MAX], *mbox;
356 address_t *tmp = NULL;
359 mutt_mktemp (tempfname);
360 if (!(fpout = safe_fopen (tempfname, "w"))) {
361 mutt_perror (tempfname);
365 set_option (OPTDONTHANDLEPGPKEYS);
368 for (i = 0; i < Context->vcount; i++) {
369 if (Context->hdrs[Context->v2r[i]]->tagged) {
370 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
371 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
372 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
378 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP) {
379 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
380 M_CM_DECODE | M_CM_CHARCONV, 0);
383 mutt_endwin (_("Trying to extract PGP keys...\n"));
384 crypt_pgp_invoke_import (tempfname);
387 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME) {
388 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
389 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
390 M_CM_NOHEADER | M_CM_DECODE_CRYPT
391 | M_CM_DECODE_SMIME, 0);
393 mutt_copy_message (fpout, Context,
394 Context->hdrs[Context->v2r[i]], 0, 0);
397 if (Context->hdrs[Context->v2r[i]]->env->from)
398 tmp = mutt_expand_aliases (h->env->from);
399 else if (Context->hdrs[Context->v2r[i]]->env->sender)
400 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
402 mbox = tmp ? tmp->mailbox : NULL;
404 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
405 crypt_smime_invoke_import (tempfname, mbox);
415 mutt_parse_mime_message (Context, h);
416 if (!(h->security & ENCRYPT && !crypt_valid_passphrase (h->security))) {
417 if (h->security & APPLICATION_PGP) {
418 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
420 mutt_endwin (_("Trying to extract PGP keys...\n"));
421 crypt_pgp_invoke_import (tempfname);
424 if (h->security & APPLICATION_SMIME) {
425 if (h->security & ENCRYPT)
426 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
427 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
429 mutt_copy_message (fpout, Context, h, 0, 0);
433 tmp = mutt_expand_aliases (h->env->from);
434 else if (h->env->sender)
435 tmp = mutt_expand_aliases (h->env->sender);
436 mbox = tmp ? tmp->mailbox : NULL;
437 if (mbox) { /* else ? */
438 mutt_message (_("Trying to extract S/MIME certificates...\n"));
439 crypt_smime_invoke_import (tempfname, mbox);
447 mutt_any_key_to_continue (NULL);
449 mutt_unlink (tempfname);
451 unset_option (OPTDONTHANDLEPGPKEYS);
456 int crypt_get_keys (HEADER * msg, char **keylist)
458 /* Do a quick check to make sure that we can find all of the encryption
459 * keys if the user has requested this service.
462 set_option (OPTPGPCHECKTRUST);
466 if (msg->security & ENCRYPT) {
467 if (msg->security & APPLICATION_PGP) {
468 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
469 msg->env->bcc)) == NULL)
471 unset_option (OPTPGPCHECKTRUST);
473 if (msg->security & APPLICATION_SMIME) {
474 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
475 msg->env->bcc)) == NULL)
485 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
487 for (; a; a = a->next) {
488 if (a->type == TYPEMULTIPART)
489 crypt_fetch_signatures (signatures, a->parts, n);
492 p_realloc(signatures, *n + 6);
494 (*signatures)[(*n)++] = a;
501 * This routine verifies a "multipart/signed" body.
504 int mutt_signed_handler (BODY * a, STATE * s)
506 char tempfile[_POSIX_PATH_MAX];
508 int protocol_major = TYPEOTHER;
509 char *protocol_minor = NULL;
512 BODY **signatures = NULL;
518 protocol = parameter_getval(a->parameter, "protocol");
521 /* extract the protocol information */
527 if ((protocol_minor = strchr (protocol, '/')))
530 m_strcpy(major, sizeof(major), protocol);
531 if ((t = strchr (major, '/')))
534 protocol_major = mutt_check_mime_type (major);
537 /* consistency check */
539 if (!(a && a->next && a->next->type == protocol_major &&
540 !m_strcasecmp(a->next->subtype, protocol_minor))) {
541 state_attach_puts (_("[-- Error: "
542 "Inconsistent multipart/signed structure! --]\n\n"),
544 return mutt_body_handler (a, s);
548 if (protocol_major == TYPEAPPLICATION
549 && !m_strcasecmp(protocol_minor, "pgp-signature"));
550 else if (protocol_major == TYPEAPPLICATION
551 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
552 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
553 else if (protocol_major == TYPEMULTIPART
554 && !m_strcasecmp(protocol_minor, "mixed"));
556 state_printf (s, _("[-- Error: "
557 "Unknown multipart/signed protocol %s! --]\n\n"),
559 return mutt_body_handler (a, s);
562 if (s->flags & M_DISPLAY) {
564 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
567 mutt_mktemp (tempfile);
568 if (crypt_write_signed (a, s, tempfile) == 0) {
569 for (i = 0; i < sigcnt; i++) {
570 if (signatures[i]->type == TYPEAPPLICATION
571 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
572 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
578 if (signatures[i]->type == TYPEAPPLICATION
579 && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
580 || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
582 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
588 state_printf (s, _("[-- Warning: "
589 "We can't verify %s/%s signatures. --]\n\n"),
590 TYPE (signatures[i]), signatures[i]->subtype);
594 mutt_unlink (tempfile);
596 b->goodsig = goodsig;
597 b->badsig = !goodsig;
599 /* Now display the signed body */
600 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
603 p_delete(&signatures);
606 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
610 rc = mutt_body_handler (a, s);
612 if (s->flags & M_DISPLAY && sigcnt)
613 state_attach_puts (_("\n[-- End of signed data --]\n"), s);