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 /* print the current time to avoid spoofing of the signature output */
36 void crypt_current_time (STATE * s, const char *app_name)
39 char p[STRING], tmp[STRING];
41 if (option (OPTCRYPTTIMESTAMP)) {
43 setlocale (LC_TIME, "");
44 strftime (p, sizeof (p), _(" (current time: %c)"), localtime (&t));
45 setlocale (LC_TIME, "C");
50 snprintf (tmp, sizeof (tmp), _("[-- %s output follows%s --]\n"),
51 NONULL (app_name), p);
52 state_attach_puts (tmp, s);
55 #if defined(HAVE_SETRLIMIT)
57 static void disable_coredumps (void)
59 struct rlimit rl = { 0, 0 };
60 static short done = 0;
63 setrlimit (RLIMIT_CORE, &rl);
68 #endif /* HAVE_SETRLIMIT */
70 int mutt_protect (HEADER * msg, char *keylist)
72 BODY *pbody = NULL, *tmp_pbody = NULL;
73 BODY *tmp_smime_pbody = NULL;
74 BODY *tmp_pgp_pbody = NULL;
75 int flags = msg->security;
78 if ((msg->security & PGPINLINE) == PGPINLINE) {
79 /* they really want to send it inline... go for it */
81 mutt_endwin _("Invoking PGP...");
83 pbody = crypt_pgp_traditional_encryptsign (msg->content, flags, keylist);
89 /* otherwise inline won't work...ask for revert */
90 if ((i = query_quadoption(OPT_PGPMIMEAUTO,
91 _("Message can't be sent inline. Revert to using PGP/MIME?")))
93 mutt_error _("Mail not sent.");
98 /* go ahead with PGP/MIME */
104 tmp_smime_pbody = msg->content;
105 tmp_pgp_pbody = msg->content;
107 if (msg->security & SIGN) {
108 if (msg->security & APPLICATION_SMIME) {
109 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
111 pbody = tmp_smime_pbody = tmp_pbody;
114 if ((msg->security & APPLICATION_PGP)
115 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
116 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
120 pbody = tmp_pgp_pbody = tmp_pbody;
123 if ((msg->security & APPLICATION_SMIME)
124 && (msg->security & APPLICATION_PGP)) {
125 /* here comes the draft ;-) */
130 if (msg->security & ENCRYPT) {
131 if ((msg->security & APPLICATION_SMIME)) {
132 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
134 /* signed ? free it! */
137 /* free tmp_body if messages was signed AND encrypted ... */
138 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
139 /* detatch and dont't delete msg->content,
140 which tmp_smime_pbody->parts after signing. */
141 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
142 msg->content->next = NULL;
143 body_list_wipe(&tmp_smime_pbody);
148 if ((msg->security & APPLICATION_PGP)) {
149 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
152 /* did we perform a retainable signature? */
153 if (flags != msg->security) {
154 /* remove the outer multipart layer */
155 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
156 /* get rid of the signature */
157 body_list_wipe(&tmp_pgp_pbody->next);
163 /* destroy temporary signature envelope when doing retainable
167 if (flags != msg->security) {
168 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
169 body_list_wipe(&tmp_pgp_pbody->next);
175 msg->content = pbody;
181 int crypt_query (BODY * m)
188 if (m->type == TYPEAPPLICATION) {
189 t |= mutt_is_application_pgp (m);
191 t |= mutt_is_application_smime (m);
197 else if (m->type == TYPETEXT) {
198 t |= mutt_is_application_pgp (m);
203 if (m->type == TYPEMULTIPART) {
204 t |= mutt_is_multipart_encrypted (m);
205 t |= mutt_is_multipart_signed (m);
211 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
215 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
216 w = 0; /* Bits set in any part */
218 for (p = m->parts; p; p = p->next) {
223 t |= u | (w & ~GOODSIGN);
225 if ((w & GOODSIGN) && !(u & GOODSIGN))
233 static void crypt_write_signed(BODY * a, STATE * s, FILE *fp)
239 fseeko (s->fpin, a->hdr_offset, 0);
240 bytes = a->length + a->offset - a->hdr_offset;
243 if ((c = fgetc (s->fpin)) == EOF)
251 if (c == '\n' && !hadcr)
262 void convert_to_7bit (BODY * a)
265 if (a->type == TYPEMULTIPART) {
266 if (a->encoding != ENC7BIT) {
267 a->encoding = ENC7BIT;
268 convert_to_7bit (a->parts);
270 convert_to_7bit (a->parts);
273 else if (a->type == TYPEMESSAGE &&
274 m_strcasecmp(a->subtype, "delivery-status")) {
275 if (a->encoding != ENC7BIT)
276 mutt_message_to_7bit (a, NULL);
278 else if (a->encoding == ENC8BIT)
279 a->encoding = ENCQUOTEDPRINTABLE;
280 else if (a->encoding == ENCBINARY)
281 a->encoding = ENCBASE64;
282 else if (a->content && a->encoding != ENCBASE64 &&
283 (a->content->from || a->content->space))
284 a->encoding = ENCQUOTEDPRINTABLE;
290 void crypt_extract_keys_from_messages (HEADER * h)
293 char tempfname[_POSIX_PATH_MAX], *mbox;
294 address_t *tmp = NULL;
297 fpout = m_tempfile(tempfname, sizeof(tempfname), NONULL(MCore.tmpdir), NULL);
299 mutt_error(_("Could not create temporary file"));
303 set_option (OPTDONTHANDLEPGPKEYS);
306 for (i = 0; i < Context->vcount; i++) {
307 if (Context->hdrs[Context->v2r[i]]->tagged) {
308 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
309 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_PGP) {
310 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
311 M_CM_DECODE | M_CM_CHARCONV, 0);
314 mutt_endwin (_("Trying to extract PGP keys...\n"));
315 crypt_pgp_invoke_import (tempfname);
318 if (Context->hdrs[Context->v2r[i]]->security & APPLICATION_SMIME) {
319 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT)
320 mutt_copy_message (fpout, Context, Context->hdrs[Context->v2r[i]],
321 M_CM_NOHEADER | M_CM_DECODE_CRYPT
322 | M_CM_DECODE_SMIME, 0);
324 mutt_copy_message (fpout, Context,
325 Context->hdrs[Context->v2r[i]], 0, 0);
328 if (Context->hdrs[Context->v2r[i]]->env->from)
329 tmp = mutt_expand_aliases (h->env->from);
330 else if (Context->hdrs[Context->v2r[i]]->env->sender)
331 tmp = mutt_expand_aliases (Context->hdrs[Context->v2r[i]]
333 mbox = tmp ? tmp->mailbox : NULL;
335 mutt_endwin (_("Trying to extract S/MIME certificates...\n"));
336 crypt_smime_invoke_import (tempfname, mbox);
346 mutt_parse_mime_message (Context, h);
347 if (h->security & APPLICATION_PGP) {
348 mutt_copy_message (fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
350 mutt_endwin (_("Trying to extract PGP keys...\n"));
351 crypt_pgp_invoke_import (tempfname);
354 if (h->security & APPLICATION_SMIME) {
355 if (h->security & ENCRYPT)
356 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
357 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
359 mutt_copy_message (fpout, Context, h, 0, 0);
363 tmp = mutt_expand_aliases (h->env->from);
364 else if (h->env->sender)
365 tmp = mutt_expand_aliases (h->env->sender);
366 mbox = tmp ? tmp->mailbox : NULL;
367 if (mbox) { /* else ? */
368 mutt_message (_("Trying to extract S/MIME certificates...\n"));
369 crypt_smime_invoke_import (tempfname, mbox);
376 mutt_any_key_to_continue (NULL);
378 mutt_unlink (tempfname);
380 unset_option (OPTDONTHANDLEPGPKEYS);
385 int crypt_get_keys (HEADER * msg, char **keylist)
387 /* Do a quick check to make sure that we can find all of the encryption
388 * keys if the user has requested this service.
391 set_option (OPTPGPCHECKTRUST);
395 if (msg->security & ENCRYPT) {
396 if (msg->security & APPLICATION_PGP) {
397 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
398 msg->env->bcc)) == NULL)
400 unset_option (OPTPGPCHECKTRUST);
402 if (msg->security & APPLICATION_SMIME) {
403 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
404 msg->env->bcc)) == NULL)
414 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
416 for (; a; a = a->next) {
417 if (a->type == TYPEMULTIPART)
418 crypt_fetch_signatures (signatures, a->parts, n);
421 p_realloc(signatures, *n + 6);
423 (*signatures)[(*n)++] = a;
430 * This routine verifies a "multipart/signed" body.
433 int mutt_signed_handler (BODY * a, STATE * s)
435 char tempfile[_POSIX_PATH_MAX];
438 int protocol_major = TYPEOTHER;
439 char *protocol_minor = NULL;
442 BODY **signatures = NULL;
448 protocol = parameter_getval(a->parameter, "protocol");
451 /* extract the protocol information */
457 if ((protocol_minor = strchr (protocol, '/')))
460 m_strcpy(major, sizeof(major), protocol);
461 if ((t = strchr (major, '/')))
464 protocol_major = mutt_check_mime_type (major);
467 /* consistency check */
469 if (!(a && a->next && a->next->type == protocol_major &&
470 !m_strcasecmp(a->next->subtype, protocol_minor))) {
471 state_attach_puts (_("[-- Error: "
472 "Inconsistent multipart/signed structure! --]\n\n"),
474 return mutt_body_handler (a, s);
478 if (protocol_major == TYPEAPPLICATION
479 && !m_strcasecmp(protocol_minor, "pgp-signature"));
480 else if (protocol_major == TYPEAPPLICATION
481 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
482 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
483 else if (protocol_major == TYPEMULTIPART
484 && !m_strcasecmp(protocol_minor, "mixed"));
486 state_printf (s, _("[-- Error: "
487 "Unknown multipart/signed protocol %s! --]\n\n"),
489 return mutt_body_handler (a, s);
492 if (s->flags & M_DISPLAY) {
494 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
497 tempfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
499 mutt_error(_("Could not create temporary file"));
501 crypt_write_signed(a, s, tempfp);
503 for (i = 0; i < sigcnt; i++) {
504 if (signatures[i]->type == TYPEAPPLICATION
505 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
506 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
512 if (signatures[i]->type == TYPEAPPLICATION
513 && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
514 || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
516 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
522 state_printf (s, _("[-- Warning: "
523 "We can't verify %s/%s signatures. --]\n\n"),
524 TYPE (signatures[i]), signatures[i]->subtype);
528 mutt_unlink (tempfile);
530 b->goodsig = goodsig;
531 b->badsig = !goodsig;
533 /* Now display the signed body */
534 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
537 p_delete(&signatures);
540 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
544 rc = mutt_body_handler (a, s);
546 if (s->flags & M_DISPLAY && sigcnt)
547 state_attach_puts (_("\n[-- End of signed data --]\n"), s);