2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1998-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) 2002, 2003, 2004 g10 Code GmbH
11 * Copyright © 2006 Pierre Habouzit
14 #include <lib-lib/lib-lib.h>
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-mx/mx.h>
25 void crypt_invoke_message (int type)
27 if (type & APPLICATION_PGP) {
28 mutt_message _("Invoking PGP...");
30 else if (type & APPLICATION_SMIME) {
31 mutt_message _("Invoking S/MIME...");
35 int mutt_protect (HEADER * msg, char *keylist)
37 BODY *pbody = NULL, *tmp_pbody = NULL;
38 BODY *tmp_smime_pbody = NULL;
39 BODY *tmp_pgp_pbody = NULL;
40 int flags = msg->security;
45 tmp_smime_pbody = msg->content;
46 tmp_pgp_pbody = msg->content;
48 if (msg->security & SIGN) {
49 if (msg->security & APPLICATION_SMIME) {
50 if (!(tmp_pbody = crypt_smime_sign_message (msg->content)))
52 pbody = tmp_smime_pbody = tmp_pbody;
55 if ((msg->security & APPLICATION_PGP)
56 && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) {
57 if (!(tmp_pbody = crypt_pgp_sign_message (msg->content)))
61 pbody = tmp_pgp_pbody = tmp_pbody;
64 if ((msg->security & APPLICATION_SMIME)
65 && (msg->security & APPLICATION_PGP)) {
66 /* here comes the draft ;-) */
71 if (msg->security & ENCRYPT) {
72 if ((msg->security & APPLICATION_SMIME)) {
73 if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody,
75 /* signed ? free it! */
78 /* free tmp_body if messages was signed AND encrypted ... */
79 if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) {
80 /* detatch and dont't delete msg->content,
81 which tmp_smime_pbody->parts after signing. */
82 tmp_smime_pbody->parts = tmp_smime_pbody->parts->next;
83 msg->content->next = NULL;
84 body_list_wipe(&tmp_smime_pbody);
89 if ((msg->security & APPLICATION_PGP)) {
90 if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist,
93 /* did we perform a retainable signature? */
94 if (flags != msg->security) {
95 /* remove the outer multipart layer */
96 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
97 /* get rid of the signature */
98 body_list_wipe(&tmp_pgp_pbody->next);
104 /* destroy temporary signature envelope when doing retainable
108 if (flags != msg->security) {
109 tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody);
110 body_list_wipe(&tmp_pgp_pbody->next);
116 msg->content = pbody;
122 int crypt_query (BODY * m)
129 if (m->type == TYPEAPPLICATION) {
130 t |= mutt_is_application_pgp (m);
132 t |= mutt_is_application_smime (m);
138 else if (m->type == TYPETEXT) {
139 t |= mutt_is_application_pgp (m);
144 if (m->type == TYPEMULTIPART) {
145 t |= mutt_is_multipart_encrypted (m);
146 t |= mutt_is_multipart_signed (m);
152 if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) {
156 u = m->parts ? 0xffffffff : 0; /* Bits set in all parts */
157 w = 0; /* Bits set in any part */
159 for (p = m->parts; p; p = p->next) {
164 t |= u | (w & ~GOODSIGN);
166 if ((w & GOODSIGN) && !(u & GOODSIGN))
174 static void crypt_write_signed(BODY * a, STATE * s, FILE *fp)
180 fseeko (s->fpin, a->hdr_offset, 0);
181 bytes = a->length + a->offset - a->hdr_offset;
184 if ((c = fgetc (s->fpin)) == EOF)
192 if (c == '\n' && !hadcr)
203 void convert_to_7bit (BODY * a)
206 if (a->type == TYPEMULTIPART) {
207 if (a->encoding != ENC7BIT) {
208 a->encoding = ENC7BIT;
209 convert_to_7bit (a->parts);
211 convert_to_7bit (a->parts);
214 else if (a->type == TYPEMESSAGE &&
215 m_strcasecmp(a->subtype, "delivery-status")) {
216 if (a->encoding != ENC7BIT)
217 mutt_message_to_7bit (a, NULL);
219 else if (a->encoding == ENC8BIT)
220 a->encoding = ENCQUOTEDPRINTABLE;
221 else if (a->encoding == ENCBINARY)
222 a->encoding = ENCBASE64;
223 else if (a->content && a->encoding != ENCBASE64 &&
224 (a->content->from || a->content->space))
225 a->encoding = ENCQUOTEDPRINTABLE;
231 static void extract_keys_aux(FILE *fpout, HEADER *h)
233 mutt_parse_mime_message (Context, h);
236 if (h->security & APPLICATION_PGP) {
237 mutt_copy_message(fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0);
240 mutt_endwin (_("Trying to extract PGP keys...\n"));
243 if (h->security & APPLICATION_SMIME) {
244 if (h->security & ENCRYPT)
245 mutt_copy_message (fpout, Context, h, M_CM_NOHEADER
246 | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0);
248 mutt_copy_message(fpout, Context, h, 0, 0);
251 mutt_message (_("Trying to extract S/MIME certificates...\n"));
255 crypt_invoke_import(fpout, h->security & APPLICATION_SMIME);
258 void crypt_extract_keys_from_messages(HEADER * h)
260 FILE *tmpfp = tmpfile();
262 mutt_error(_("Could not create temporary file"));
266 set_option(OPTDONTHANDLEPGPKEYS);
269 for (i = 0; i < Context->vcount; i++) {
270 if (!Context->hdrs[Context->v2r[i]]->tagged)
272 extract_keys_aux(tmpfp, Context->hdrs[Context->v2r[i]]);
275 extract_keys_aux(tmpfp, h);
277 unset_option(OPTDONTHANDLEPGPKEYS);
281 mutt_any_key_to_continue(NULL);
286 int crypt_get_keys (HEADER * msg, char **keylist)
288 /* Do a quick check to make sure that we can find all of the encryption
289 * keys if the user has requested this service.
292 set_option (OPTPGPCHECKTRUST);
296 if (msg->security & ENCRYPT) {
297 if (msg->security & APPLICATION_PGP) {
298 if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
299 msg->env->bcc)) == NULL)
301 unset_option (OPTPGPCHECKTRUST);
303 if (msg->security & APPLICATION_SMIME) {
304 if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
305 msg->env->bcc)) == NULL)
315 static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n)
317 for (; a; a = a->next) {
318 if (a->type == TYPEMULTIPART)
319 crypt_fetch_signatures (signatures, a->parts, n);
322 p_realloc(signatures, *n + 6);
324 (*signatures)[(*n)++] = a;
331 * This routine verifies a "multipart/signed" body.
334 int mutt_signed_handler (BODY * a, STATE * s)
336 char tempfile[_POSIX_PATH_MAX];
339 int protocol_major = TYPEOTHER;
340 char *protocol_minor = NULL;
343 BODY **signatures = NULL;
349 protocol = parameter_getval(a->parameter, "protocol");
352 /* extract the protocol information */
358 if ((protocol_minor = strchr (protocol, '/')))
361 m_strcpy(major, sizeof(major), protocol);
362 if ((t = strchr (major, '/')))
365 protocol_major = mutt_check_mime_type (major);
368 /* consistency check */
370 if (!(a && a->next && a->next->type == protocol_major &&
371 !m_strcasecmp(a->next->subtype, protocol_minor))) {
372 state_attach_puts (_("[-- Error: "
373 "Inconsistent multipart/signed structure! --]\n\n"),
375 return mutt_body_handler (a, s);
379 if (protocol_major == TYPEAPPLICATION
380 && !m_strcasecmp(protocol_minor, "pgp-signature"));
381 else if (protocol_major == TYPEAPPLICATION
382 && !(m_strcasecmp(protocol_minor, "x-pkcs7-signature")
383 && m_strcasecmp(protocol_minor, "pkcs7-signature")));
384 else if (protocol_major == TYPEMULTIPART
385 && !m_strcasecmp(protocol_minor, "mixed"));
387 state_printf (s, _("[-- Error: "
388 "Unknown multipart/signed protocol %s! --]\n\n"),
390 return mutt_body_handler (a, s);
393 if (s->flags & M_DISPLAY) {
395 crypt_fetch_signatures (&signatures, a->next, &sigcnt);
398 tempfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
400 mutt_error(_("Could not create temporary file"));
402 crypt_write_signed(a, s, tempfp);
404 for (i = 0; i < sigcnt; i++) {
405 if (signatures[i]->type == TYPEAPPLICATION
406 && !m_strcasecmp(signatures[i]->subtype, "pgp-signature")) {
407 if (crypt_pgp_verify_one (signatures[i], s, tempfile) != 0)
413 if (signatures[i]->type == TYPEAPPLICATION
414 && (!m_strcasecmp(signatures[i]->subtype, "x-pkcs7-signature")
415 || !m_strcasecmp(signatures[i]->subtype, "pkcs7-signature")))
417 if (crypt_smime_verify_one (signatures[i], s, tempfile) != 0)
423 state_printf (s, _("[-- Warning: "
424 "We can't verify %s/%s signatures. --]\n\n"),
425 TYPE (signatures[i]), signatures[i]->subtype);
429 mutt_unlink (tempfile);
431 b->goodsig = goodsig;
432 b->badsig = !goodsig;
434 /* Now display the signed body */
435 state_attach_puts (_("[-- The following data is signed --]\n\n"), s);
438 p_delete(&signatures);
441 state_attach_puts (_("[-- Warning: Can't find any signatures. --]\n\n"),
445 rc = mutt_body_handler (a, s);
447 if (s->flags & M_DISPLAY && sigcnt)
448 state_attach_puts (_("\n[-- End of signed data --]\n"), s);