From 576172ff50f9dd94dd2f5cc91d247c1e50dbe7fc Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Thu, 5 Apr 2007 01:46:14 +0200 Subject: [PATCH] merge crypt back into $top_builddir :) Signed-off-by: Pierre Habouzit --- Makefile.am | 8 +- attach.c | 2 +- commands.c | 2 +- compose.c | 2 +- configure.ac | 1 - copy.c | 4 +- lib-crypt/crypt-gpgme.c => crypt.c | 385 +++++++++++++++++++++++++++ lib-crypt/crypt.h => crypt.h | 35 +-- handler.c | 2 +- headers.c | 2 +- hook.c | 2 +- init.c | 2 +- keymap.c | 2 +- lib-crypt/Makefile.am | 7 - lib-crypt/crypt.c | 406 ----------------------------- lib-mime/crypt.c | 3 +- lib-mx/mx.c | 2 +- lib-ui/curs_main.c | 3 +- lib-ui/hdrline.c | 2 +- main.c | 2 +- nntp/nntp.c | 3 +- pager.c | 2 +- pattern.c | 2 +- pop/pop.c | 2 +- postpone.c | 2 +- recvattach.c | 2 +- send.c | 2 +- sendlib.c | 3 +- 28 files changed, 416 insertions(+), 476 deletions(-) rename lib-crypt/crypt-gpgme.c => crypt.c (91%) rename lib-crypt/crypt.h => crypt.h (91%) delete mode 100644 lib-crypt/Makefile.am delete mode 100644 lib-crypt/crypt.c diff --git a/Makefile.am b/Makefile.am index c6ab853..2e155df 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,7 +9,7 @@ NNTP_SUBDIR = nntp endif SUBDIRS = tools intl m4 po $(XXXXXXXXXXXX_doc) apidoc contrib \ - lib-lua lib-mime lib-lib lib-mx lib-crypt lib-hash lib-sys lib-ui \ + lib-lua lib-mime lib-lib lib-mx lib-hash lib-sys lib-ui \ pop imap $(NNTP_SUBDIR) BUILT_SOURCES = keymap_defs.h charset.gperf @@ -18,14 +18,13 @@ DISTCLEANFILES = $(BUILT_SOURCES) bin_PROGRAMS = madmutt madmutt_dotlock smime_keysng madmutt_SOURCES = $(BUILT_SOURCES) \ alias.c attach.c base64.c browser.c buffy.c charset.c commands.c \ - compose.c copy.c editmsg.c init.c keymap.c lib.c \ + compose.c copy.c crypt.c editmsg.c init.c keymap.c lib.c \ flags.c from.c handler.c headers.c help.c hook.c \ main.c muttlib.c mutt_idna.c pager.c pattern.c postpone.c recvattach.c recvcmd.c \ score.c send.c sendlib.c sort.c state.c thread.c account.c remailer.c madmutt_LDADD = @MUTT_LIB_OBJECTS@ @LIBOBJS@ \ $(top_builddir)/lib-mime/libmime.a \ - $(top_builddir)/lib-crypt/libcrypt.a \ $(top_builddir)/lib-mx/libmx.a \ $(top_builddir)/imap/libimap.a \ $(top_builddir)/pop/libpop.a \ @@ -42,7 +41,6 @@ madmutt_DEPENDENCIES = \ @MUTT_LIB_OBJECTS@ @LIBOBJS@ \ $(top_builddir)/lib-lib/liblib.a \ $(top_builddir)/lib-mime/libmime.a \ - $(top_builddir)/lib-crypt/libcrypt.a \ $(top_builddir)/lib-hash/libhash.a \ $(top_builddir)/lib-sys/libsys.a \ $(top_builddir)/lib-ui/libui.a \ @@ -74,7 +72,7 @@ EXTRA_madmutt_SOURCES = \ EXTRA_DIST = config.rpath COPYRIGHT GPL OPS TODO \ configure account.h alias.h attach.h recvattach.h handler.h thread.h \ - buffy.h charset.h copy.h dotlock.h functions.def gen_defs \ + buffy.h charset.h copy.h crypt.c dotlock.h functions.def gen_defs \ recvattach.h handler.h thread.h globals.h init.h keymap.h \ mime.h mutt.h mutt_sasl.h pager.h protos.h \ sort.h mime.types autogen.sh remailer.c remailer.h browser.h state.h \ diff --git a/attach.c b/attach.c index db791f5..1750e26 100644 --- a/attach.c +++ b/attach.c @@ -22,7 +22,7 @@ #include "keymap.h" #include "pager.h" #include "copy.h" -#include +#include "crypt.h" int mutt_get_tmp_attachment (BODY * a) { diff --git a/commands.c b/commands.c index f5b4d43..4e810c9 100644 --- a/commands.c +++ b/commands.c @@ -28,7 +28,7 @@ #include "sort.h" #include "copy.h" #include "pager.h" -#include +#include "crypt.h" #include "mutt_idna.h" #include #include diff --git a/compose.c b/compose.c index a5f7094..d8e0694 100644 --- a/compose.c +++ b/compose.c @@ -15,7 +15,6 @@ #include #include -#include #include #include @@ -24,6 +23,7 @@ #include "mutt.h" #include "alias.h" +#include "crypt.h" #include "mutt_idna.h" #include "attach.h" #include "recvattach.h" diff --git a/configure.ac b/configure.ac index f3d2183..0300fa7 100644 --- a/configure.ac +++ b/configure.ac @@ -745,7 +745,6 @@ AC_OUTPUT(Makefile lib-lib/Makefile lib-lua/Makefile lib-mime/Makefile - lib-crypt/Makefile lib-hash/Makefile lib-mx/Makefile lib-sys/Makefile diff --git a/copy.c b/copy.c index 8e8f96f..c3299f0 100644 --- a/copy.c +++ b/copy.c @@ -10,13 +10,13 @@ #include #include +#include #include #include "mutt.h" #include "handler.h" #include "copy.h" -#include -#include +#include "crypt.h" #include "mutt_idna.h" static int address_header_decode (char **str); diff --git a/lib-crypt/crypt-gpgme.c b/crypt.c similarity index 91% rename from lib-crypt/crypt-gpgme.c rename to crypt.c index 5bd3173..b22d616 100644 --- a/lib-crypt/crypt-gpgme.c +++ b/crypt.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "crypt.h" @@ -3907,3 +3908,387 @@ void crypt_smime_getkeys (ENVELOPE * env) { } +/***************************************************************************/ + +void crypt_invoke_message (int type) +{ + if (type & APPLICATION_PGP) { + mutt_message _("Invoking PGP..."); + } + else if (type & APPLICATION_SMIME) { + mutt_message _("Invoking S/MIME..."); + } +} + +int mutt_protect (HEADER * msg, char *keylist) +{ + BODY *pbody = NULL, *tmp_pbody = NULL; + BODY *tmp_smime_pbody = NULL; + BODY *tmp_pgp_pbody = NULL; + int flags = msg->security; + + if (!isendwin ()) + mutt_endwin (NULL); + + tmp_smime_pbody = msg->content; + tmp_pgp_pbody = msg->content; + + if (msg->security & SIGN) { + if (msg->security & APPLICATION_SMIME) { + if (!(tmp_pbody = crypt_smime_sign_message (msg->content))) + return -1; + pbody = tmp_smime_pbody = tmp_pbody; + } + + if ((msg->security & APPLICATION_PGP) + && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) { + if (!(tmp_pbody = crypt_pgp_sign_message (msg->content))) + return -1; + + flags &= ~SIGN; + pbody = tmp_pgp_pbody = tmp_pbody; + } + + if ((msg->security & APPLICATION_SMIME) + && (msg->security & APPLICATION_PGP)) { + /* here comes the draft ;-) */ + } + } + + + if (msg->security & ENCRYPT) { + if ((msg->security & APPLICATION_SMIME)) { + if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody, + keylist))) { + /* signed ? free it! */ + return (-1); + } + /* free tmp_body if messages was signed AND encrypted ... */ + if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) { + /* detatch and dont't delete msg->content, + which tmp_smime_pbody->parts after signing. */ + tmp_smime_pbody->parts = tmp_smime_pbody->parts->next; + msg->content->next = NULL; + body_list_wipe(&tmp_smime_pbody); + } + pbody = tmp_pbody; + } + + if ((msg->security & APPLICATION_PGP)) { + if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist, + flags & SIGN))) { + + /* did we perform a retainable signature? */ + if (flags != msg->security) { + /* remove the outer multipart layer */ + tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody); + /* get rid of the signature */ + body_list_wipe(&tmp_pgp_pbody->next); + } + + return (-1); + } + + /* destroy temporary signature envelope when doing retainable + * signatures. + + */ + if (flags != msg->security) { + tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody); + body_list_wipe(&tmp_pgp_pbody->next); + } + } + } + + if (pbody) + msg->content = pbody; + + return 0; +} + + +int crypt_query (BODY * m) +{ + int t = 0; + + if (!m) + return 0; + + if (m->type == TYPEAPPLICATION) { + t |= mutt_is_application_pgp (m); + + t |= mutt_is_application_smime (m); + if (t && m->goodsig) + t |= GOODSIGN; + if (t && m->badsig) + t |= BADSIGN; + } + else if (m->type == TYPETEXT) { + t |= mutt_is_application_pgp (m); + if (t && m->goodsig) + t |= GOODSIGN; + } + + if (m->type == TYPEMULTIPART) { + t |= mutt_is_multipart_encrypted (m); + t |= mutt_is_multipart_signed (m); + + if (t && m->goodsig) + t |= GOODSIGN; + } + + if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) { + BODY *p; + int u, v, w; + + u = m->parts ? ~0 : 0; /* Bits set in all parts */ + w = 0; /* Bits set in any part */ + + for (p = m->parts; p; p = p->next) { + v = crypt_query (p); + u &= v; + w |= v; + } + t |= u | (w & ~GOODSIGN); + + if ((w & GOODSIGN) && !(u & GOODSIGN)) + t |= PARTSIGN; + } + + return t; +} + + +static void crypt_write_signed(BODY * a, STATE * s, FILE *fp) +{ + int c; + short hadcr; + size_t bytes; + + fseeko (s->fpin, a->hdr_offset, 0); + bytes = a->length + a->offset - a->hdr_offset; + hadcr = 0; + while (bytes > 0) { + if ((c = fgetc (s->fpin)) == EOF) + break; + + bytes--; + + if (c == '\r') + hadcr = 1; + else { + if (c == '\n' && !hadcr) + fputc ('\r', fp); + + hadcr = 0; + } + fputc (c, fp); + } +} + + + +void convert_to_7bit (BODY * a) +{ + while (a) { + if (a->type == TYPEMULTIPART) { + if (a->encoding != ENC7BIT) { + a->encoding = ENC7BIT; + convert_to_7bit (a->parts); + } else { + convert_to_7bit (a->parts); + } + } + else if (a->type == TYPEMESSAGE && + m_strcasecmp(a->subtype, "delivery-status")) { + if (a->encoding != ENC7BIT) + mutt_message_to_7bit (a, NULL); + } + else if (a->encoding == ENC8BIT) + a->encoding = ENCQUOTEDPRINTABLE; + else if (a->encoding == ENCBINARY) + a->encoding = ENCBASE64; + else if (a->content && a->encoding != ENCBASE64 && + (a->content->from || a->content->space)) + a->encoding = ENCQUOTEDPRINTABLE; + a = a->next; + } +} + + +static void extract_keys_aux(FILE *fpout, HEADER *h) +{ + mutt_parse_mime_message (Context, h); + + rewind(fpout); + if (h->security & APPLICATION_PGP) { + mutt_copy_message(fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0); + fflush (fpout); + + mutt_endwin (_("Trying to extract PGP keys...\n")); + } + + if (h->security & APPLICATION_SMIME) { + if (h->security & ENCRYPT) + mutt_copy_message (fpout, Context, h, M_CM_NOHEADER + | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0); + else + mutt_copy_message(fpout, Context, h, 0, 0); + fflush (fpout); + + mutt_message (_("Trying to extract S/MIME certificates...\n")); + } + + rewind(fpout); + crypt_invoke_import(fpout, h->security & APPLICATION_SMIME); +} + +void crypt_extract_keys_from_messages(HEADER * h) +{ + FILE *tmpfp = tmpfile(); + if (!tmpfp) { + mutt_error(_("Could not create temporary file")); + return; + } + + set_option(OPTDONTHANDLEPGPKEYS); + if (!h) { + int i; + for (i = 0; i < Context->vcount; i++) { + if (!Context->hdrs[Context->v2r[i]]->tagged) + continue; + extract_keys_aux(tmpfp, Context->hdrs[Context->v2r[i]]); + } + } else { + extract_keys_aux(tmpfp, h); + } + unset_option(OPTDONTHANDLEPGPKEYS); + m_fclose(&tmpfp); + + if (isendwin()) + mutt_any_key_to_continue(NULL); +} + + + +static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n) +{ + for (; a; a = a->next) { + if (a->type == TYPEMULTIPART) + crypt_fetch_signatures (signatures, a->parts, n); + else { + if ((*n % 5) == 0) + p_realloc(signatures, *n + 6); + + (*signatures)[(*n)++] = a; + } + } +} + + +/* + * This routine verifies a "multipart/signed" body. + */ + +int mutt_signed_handler (BODY * a, STATE * s) +{ + unsigned major, minor; + char *protocol; + int rc, i, goodsig = 1, sigcnt = 0; + BODY *b = a; + + protocol = parameter_getval(a->parameter, "protocol"); + a = a->parts; + + switch (mime_which_token(protocol, -1)) { + case MIME_APPLICATION_PGP_SIGNATURE: + major = TYPEAPPLICATION; + minor = MIME_PGP_SIGNATURE; + break; + case MIME_APPLICATION_X_PKCS7_SIGNATURE: + major = TYPEAPPLICATION; + minor = MIME_X_PKCS7_SIGNATURE; + break; + case MIME_APPLICATION_PKCS7_SIGNATURE: + major = TYPEAPPLICATION; + minor = MIME_PKCS7_SIGNATURE; + break; + case MIME_MULTIPART_MIXED: + major = TYPEMULTIPART; + minor = MIME_MIXED; + break; + + default: + state_printf(s, _("[-- Error: " + "Unknown multipart/signed protocol %s! --]\n\n"), + protocol); + return mutt_body_handler (a, s); + } + + /* consistency check */ + if (!(a && a->next && a->next->type == major && + mime_which_token(a->next->subtype, -1) == minor)) + { + state_attach_puts(_("[-- Error: " + "Inconsistent multipart/signed structure! --]\n\n"), + s); + return mutt_body_handler (a, s); + } + + if (s->flags & M_DISPLAY) { + BODY **sigs = NULL; + + crypt_fetch_signatures (&sigs, a->next, &sigcnt); + if (sigcnt) { + FILE *tmpfp = tmpfile(); + + if (!tmpfp) { + mutt_error(_("Could not create temporary file")); + } else { + crypt_write_signed(a, s, tmpfp); + rewind(tmpfp); + for (i = 0; i < sigcnt; i++) { + if (sigs[i]->type == TYPEAPPLICATION) { + int subtype; + + switch ((subtype = mime_which_token(sigs[i]->subtype, -1))) { + case MIME_PGP_SIGNATURE: + case MIME_X_PKCS7_SIGNATURE: + case MIME_PKCS7_SIGNATURE: + if (crypt_verify_one(sigs[i], s, tmpfp, subtype != MIME_PGP_SIGNATURE) != 0) + goodsig = 0; + + m_fclose(&tmpfp); + continue; + + default: + break; + } + } + + state_printf(s, _("[-- Warning: " + "We can't verify %s/%s signatures. --]\n\n"), + TYPE (sigs[i]), sigs[i]->subtype); + } + } + + b->goodsig = goodsig; + b->badsig = !goodsig; + + /* Now display the signed body */ + state_attach_puts(_("[-- The following data is signed --]\n\n"), s); + + p_delete(&sigs); + } else { + state_attach_puts(_("[-- Warning: Can't find any signatures. --]\n\n"), + s); + } + } + + rc = mutt_body_handler (a, s); + + if (s->flags & M_DISPLAY && sigcnt) + state_attach_puts (_("\n[-- End of signed data --]\n"), s); + + return (rc); +} diff --git a/lib-crypt/crypt.h b/crypt.h similarity index 91% rename from lib-crypt/crypt.h rename to crypt.h index 8d394ca..12acc61 100644 --- a/lib-crypt/crypt.h +++ b/crypt.h @@ -19,10 +19,6 @@ #include #include "state.h" -/* FIXME: They should be pointer to anonymous structures for better - information hiding. */ - - #define ENCRYPT (1 << 0) #define SIGN (1 << 1) #define GOODSIGN (1 << 2) @@ -39,14 +35,11 @@ #define PGPENCRYPT (APPLICATION_PGP | ENCRYPT) #define PGPSIGN (APPLICATION_PGP | SIGN) -#define PGPGOODSIGN (APPLICATION_PGP | GOODSIGN) #define PGPKEY (APPLICATION_PGP | KEYBLOCK) #define PGPINLINE (APPLICATION_PGP | INLINE) #define SMIMEENCRYPT (APPLICATION_SMIME | ENCRYPT) #define SMIMESIGN (APPLICATION_SMIME | SIGN) -#define SMIMEGOODSIGN (APPLICATION_SMIME | GOODSIGN) -#define SMIMEBADSIGN (APPLICATION_SMIME | BADSIGN) #define SMIMEOPAQUE (APPLICATION_SMIME | SIGNOPAQUE) #define KEYFLAG_CANSIGN (1 << 0) @@ -66,29 +59,6 @@ #define KEYFLAG_ABILITIES (KEYFLAG_CANSIGN|KEYFLAG_CANENCRYPT|KEYFLAG_PREFER_ENCRYPTION|KEYFLAG_PREFER_SIGNING) -enum pgp_ring { - PGP_PUBRING, - PGP_SECRING -}; -typedef enum pgp_ring pgp_ring_t; - - -struct pgp_keyinfo; -typedef struct pgp_keyinfo *pgp_key_t; - - - -/* Some prototypes -- old crypt.h. */ - -int mutt_protect (HEADER *, char *); - -int mutt_signed_handler (BODY *, STATE *); - -int mutt_parse_crypt_hdr (char *, int); - -void convert_to_7bit (BODY *); - - /*-- new API --*/ void crypt_invoke_import(FILE *stream, int smime); @@ -98,6 +68,11 @@ int crypt_verify_one(BODY *, STATE *s, FILE *fp, int smime); /*-- crypt.c --*/ +int mutt_protect (HEADER *, char *); +int mutt_signed_handler (BODY *, STATE *); +int mutt_parse_crypt_hdr (char *, int); +void convert_to_7bit (BODY *); + /* Check out the type of encryption used and set the cached status values if there are any. */ int crypt_query (BODY * m); diff --git a/handler.c b/handler.c index abe7f65..28739c1 100644 --- a/handler.c +++ b/handler.c @@ -22,7 +22,7 @@ #include "keymap.h" #include "copy.h" #include "charset.h" -#include +#include "crypt.h" #include "state.h" #include "attach.h" #include "lib.h" diff --git a/headers.c b/headers.c index cd3362b..a067025 100644 --- a/headers.c +++ b/headers.c @@ -12,7 +12,7 @@ #include "mutt.h" #include "alias.h" -#include +#include "crypt.h" #include "mutt_idna.h" void mutt_edit_headers(const char *body, diff --git a/hook.c b/hook.c index 7361bba..315a1c1 100644 --- a/hook.c +++ b/hook.c @@ -12,9 +12,9 @@ #include #include #include -#include #include "alias.h" +#include "crypt.h" #include "pattern.h" typedef struct hook_t { diff --git a/init.c b/init.c index fb2f8a6..0a4d827 100644 --- a/init.c +++ b/init.c @@ -18,10 +18,10 @@ #include #include #include -#include #include "mutt.h" #include "keymap.h" +#include "crypt.h" #include "charset.h" #include "thread.h" #include "mutt_idna.h" diff --git a/keymap.c b/keymap.c index ffbdc93..ab8ad63 100644 --- a/keymap.c +++ b/keymap.c @@ -14,7 +14,7 @@ #include "mutt.h" #include "keymap.h" -#include +#include "crypt.h" #define MUTT_FUNCTIONS_VALUES #include "functions.def" diff --git a/lib-crypt/Makefile.am b/lib-crypt/Makefile.am deleted file mode 100644 index 5e591e6..0000000 --- a/lib-crypt/Makefile.am +++ /dev/null @@ -1,7 +0,0 @@ -noinst_LIBRARIES = libcrypt.a - -libcrypt_a_SOURCES = crypt.h crypt.c crypt-gpgme.c - -noinst_HEADERS = crypt.h - --include $(top_builddir)/tools/cflags.mk diff --git a/lib-crypt/crypt.c b/lib-crypt/crypt.c deleted file mode 100644 index 60349cf..0000000 --- a/lib-crypt/crypt.c +++ /dev/null @@ -1,406 +0,0 @@ -/* - * Copyright notice from original mutt: - * Copyright (C) 1996,1997 Michael R. Elkins - * Copyright (C) 1998-2000 Thomas Roessler - * Copyright (C) 2001 Thomas Roessler - * Oliver Ehli - * Copyright (C) 2003 Werner Koch - * Copyright (C) 2002, 2003, 2004 g10 Code GmbH - */ -/* - * Copyright © 2006 Pierre Habouzit - */ - -#include - -#include -#include -#include - -#include "alias.h" -#include "handler.h" -#include "copy.h" -#include "crypt.h" - -void crypt_invoke_message (int type) -{ - if (type & APPLICATION_PGP) { - mutt_message _("Invoking PGP..."); - } - else if (type & APPLICATION_SMIME) { - mutt_message _("Invoking S/MIME..."); - } -} - -int mutt_protect (HEADER * msg, char *keylist) -{ - BODY *pbody = NULL, *tmp_pbody = NULL; - BODY *tmp_smime_pbody = NULL; - BODY *tmp_pgp_pbody = NULL; - int flags = msg->security; - - if (!isendwin ()) - mutt_endwin (NULL); - - tmp_smime_pbody = msg->content; - tmp_pgp_pbody = msg->content; - - if (msg->security & SIGN) { - if (msg->security & APPLICATION_SMIME) { - if (!(tmp_pbody = crypt_smime_sign_message (msg->content))) - return -1; - pbody = tmp_smime_pbody = tmp_pbody; - } - - if ((msg->security & APPLICATION_PGP) - && (!(flags & ENCRYPT) || option (OPTPGPRETAINABLESIG))) { - if (!(tmp_pbody = crypt_pgp_sign_message (msg->content))) - return -1; - - flags &= ~SIGN; - pbody = tmp_pgp_pbody = tmp_pbody; - } - - if ((msg->security & APPLICATION_SMIME) - && (msg->security & APPLICATION_PGP)) { - /* here comes the draft ;-) */ - } - } - - - if (msg->security & ENCRYPT) { - if ((msg->security & APPLICATION_SMIME)) { - if (!(tmp_pbody = crypt_smime_build_smime_entity (tmp_smime_pbody, - keylist))) { - /* signed ? free it! */ - return (-1); - } - /* free tmp_body if messages was signed AND encrypted ... */ - if (tmp_smime_pbody != msg->content && tmp_smime_pbody != tmp_pbody) { - /* detatch and dont't delete msg->content, - which tmp_smime_pbody->parts after signing. */ - tmp_smime_pbody->parts = tmp_smime_pbody->parts->next; - msg->content->next = NULL; - body_list_wipe(&tmp_smime_pbody); - } - pbody = tmp_pbody; - } - - if ((msg->security & APPLICATION_PGP)) { - if (!(pbody = crypt_pgp_encrypt_message (tmp_pgp_pbody, keylist, - flags & SIGN))) { - - /* did we perform a retainable signature? */ - if (flags != msg->security) { - /* remove the outer multipart layer */ - tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody); - /* get rid of the signature */ - body_list_wipe(&tmp_pgp_pbody->next); - } - - return (-1); - } - - /* destroy temporary signature envelope when doing retainable - * signatures. - - */ - if (flags != msg->security) { - tmp_pgp_pbody = mutt_remove_multipart (tmp_pgp_pbody); - body_list_wipe(&tmp_pgp_pbody->next); - } - } - } - - if (pbody) - msg->content = pbody; - - return 0; -} - - -int crypt_query (BODY * m) -{ - int t = 0; - - if (!m) - return 0; - - if (m->type == TYPEAPPLICATION) { - t |= mutt_is_application_pgp (m); - - t |= mutt_is_application_smime (m); - if (t && m->goodsig) - t |= GOODSIGN; - if (t && m->badsig) - t |= BADSIGN; - } - else if (m->type == TYPETEXT) { - t |= mutt_is_application_pgp (m); - if (t && m->goodsig) - t |= GOODSIGN; - } - - if (m->type == TYPEMULTIPART) { - t |= mutt_is_multipart_encrypted (m); - t |= mutt_is_multipart_signed (m); - - if (t && m->goodsig) - t |= GOODSIGN; - } - - if (m->type == TYPEMULTIPART || m->type == TYPEMESSAGE) { - BODY *p; - int u, v, w; - - u = m->parts ? ~0 : 0; /* Bits set in all parts */ - w = 0; /* Bits set in any part */ - - for (p = m->parts; p; p = p->next) { - v = crypt_query (p); - u &= v; - w |= v; - } - t |= u | (w & ~GOODSIGN); - - if ((w & GOODSIGN) && !(u & GOODSIGN)) - t |= PARTSIGN; - } - - return t; -} - - -static void crypt_write_signed(BODY * a, STATE * s, FILE *fp) -{ - int c; - short hadcr; - size_t bytes; - - fseeko (s->fpin, a->hdr_offset, 0); - bytes = a->length + a->offset - a->hdr_offset; - hadcr = 0; - while (bytes > 0) { - if ((c = fgetc (s->fpin)) == EOF) - break; - - bytes--; - - if (c == '\r') - hadcr = 1; - else { - if (c == '\n' && !hadcr) - fputc ('\r', fp); - - hadcr = 0; - } - fputc (c, fp); - } -} - - - -void convert_to_7bit (BODY * a) -{ - while (a) { - if (a->type == TYPEMULTIPART) { - if (a->encoding != ENC7BIT) { - a->encoding = ENC7BIT; - convert_to_7bit (a->parts); - } else { - convert_to_7bit (a->parts); - } - } - else if (a->type == TYPEMESSAGE && - m_strcasecmp(a->subtype, "delivery-status")) { - if (a->encoding != ENC7BIT) - mutt_message_to_7bit (a, NULL); - } - else if (a->encoding == ENC8BIT) - a->encoding = ENCQUOTEDPRINTABLE; - else if (a->encoding == ENCBINARY) - a->encoding = ENCBASE64; - else if (a->content && a->encoding != ENCBASE64 && - (a->content->from || a->content->space)) - a->encoding = ENCQUOTEDPRINTABLE; - a = a->next; - } -} - - -static void extract_keys_aux(FILE *fpout, HEADER *h) -{ - mutt_parse_mime_message (Context, h); - - rewind(fpout); - if (h->security & APPLICATION_PGP) { - mutt_copy_message(fpout, Context, h, M_CM_DECODE | M_CM_CHARCONV, 0); - fflush (fpout); - - mutt_endwin (_("Trying to extract PGP keys...\n")); - } - - if (h->security & APPLICATION_SMIME) { - if (h->security & ENCRYPT) - mutt_copy_message (fpout, Context, h, M_CM_NOHEADER - | M_CM_DECODE_CRYPT | M_CM_DECODE_SMIME, 0); - else - mutt_copy_message(fpout, Context, h, 0, 0); - fflush (fpout); - - mutt_message (_("Trying to extract S/MIME certificates...\n")); - } - - rewind(fpout); - crypt_invoke_import(fpout, h->security & APPLICATION_SMIME); -} - -void crypt_extract_keys_from_messages(HEADER * h) -{ - FILE *tmpfp = tmpfile(); - if (!tmpfp) { - mutt_error(_("Could not create temporary file")); - return; - } - - set_option(OPTDONTHANDLEPGPKEYS); - if (!h) { - int i; - for (i = 0; i < Context->vcount; i++) { - if (!Context->hdrs[Context->v2r[i]]->tagged) - continue; - extract_keys_aux(tmpfp, Context->hdrs[Context->v2r[i]]); - } - } else { - extract_keys_aux(tmpfp, h); - } - unset_option(OPTDONTHANDLEPGPKEYS); - m_fclose(&tmpfp); - - if (isendwin()) - mutt_any_key_to_continue(NULL); -} - - - -static void crypt_fetch_signatures (BODY ***signatures, BODY * a, int *n) -{ - for (; a; a = a->next) { - if (a->type == TYPEMULTIPART) - crypt_fetch_signatures (signatures, a->parts, n); - else { - if ((*n % 5) == 0) - p_realloc(signatures, *n + 6); - - (*signatures)[(*n)++] = a; - } - } -} - - -/* - * This routine verifies a "multipart/signed" body. - */ - -int mutt_signed_handler (BODY * a, STATE * s) -{ - unsigned major, minor; - char *protocol; - int rc, i, goodsig = 1, sigcnt = 0; - BODY *b = a; - - protocol = parameter_getval(a->parameter, "protocol"); - a = a->parts; - - switch (mime_which_token(protocol, -1)) { - case MIME_APPLICATION_PGP_SIGNATURE: - major = TYPEAPPLICATION; - minor = MIME_PGP_SIGNATURE; - break; - case MIME_APPLICATION_X_PKCS7_SIGNATURE: - major = TYPEAPPLICATION; - minor = MIME_X_PKCS7_SIGNATURE; - break; - case MIME_APPLICATION_PKCS7_SIGNATURE: - major = TYPEAPPLICATION; - minor = MIME_PKCS7_SIGNATURE; - break; - case MIME_MULTIPART_MIXED: - major = TYPEMULTIPART; - minor = MIME_MIXED; - break; - - default: - state_printf(s, _("[-- Error: " - "Unknown multipart/signed protocol %s! --]\n\n"), - protocol); - return mutt_body_handler (a, s); - } - - /* consistency check */ - if (!(a && a->next && a->next->type == major && - mime_which_token(a->next->subtype, -1) == minor)) - { - state_attach_puts(_("[-- Error: " - "Inconsistent multipart/signed structure! --]\n\n"), - s); - return mutt_body_handler (a, s); - } - - if (s->flags & M_DISPLAY) { - BODY **sigs = NULL; - - crypt_fetch_signatures (&sigs, a->next, &sigcnt); - if (sigcnt) { - FILE *tmpfp = tmpfile(); - - if (!tmpfp) { - mutt_error(_("Could not create temporary file")); - } else { - crypt_write_signed(a, s, tmpfp); - rewind(tmpfp); - for (i = 0; i < sigcnt; i++) { - if (sigs[i]->type == TYPEAPPLICATION) { - int subtype; - - switch ((subtype = mime_which_token(sigs[i]->subtype, -1))) { - case MIME_PGP_SIGNATURE: - case MIME_X_PKCS7_SIGNATURE: - case MIME_PKCS7_SIGNATURE: - if (crypt_verify_one(sigs[i], s, tmpfp, subtype != MIME_PGP_SIGNATURE) != 0) - goodsig = 0; - - m_fclose(&tmpfp); - continue; - - default: - break; - } - } - - state_printf(s, _("[-- Warning: " - "We can't verify %s/%s signatures. --]\n\n"), - TYPE (sigs[i]), sigs[i]->subtype); - } - } - - b->goodsig = goodsig; - b->badsig = !goodsig; - - /* Now display the signed body */ - state_attach_puts(_("[-- The following data is signed --]\n\n"), s); - - p_delete(&sigs); - } else { - state_attach_puts(_("[-- Warning: Can't find any signatures. --]\n\n"), - s); - } - } - - rc = mutt_body_handler (a, s); - - if (s->flags & M_DISPLAY && sigcnt) - state_attach_puts (_("\n[-- End of signed data --]\n"), s); - - return (rc); -} diff --git a/lib-mime/crypt.c b/lib-mime/crypt.c index 7c55f2b..af64a2c 100644 --- a/lib-mime/crypt.c +++ b/lib-mime/crypt.c @@ -32,9 +32,8 @@ #include -#include - #include "mime.h" +#include "crypt.h" int mutt_is_multipart_signed(BODY * b) { diff --git a/lib-mx/mx.c b/lib-mx/mx.c index b6fcfc1..b58a621 100644 --- a/lib-mx/mx.c +++ b/lib-mx/mx.c @@ -16,9 +16,9 @@ #include #include #include -#include #include "mutt.h" +#include "crypt.h" #include "pattern.h" #include "buffy.h" #include "mx.h" diff --git a/lib-ui/curs_main.c b/lib-ui/curs_main.c index bf3c5a1..4893833 100644 --- a/lib-ui/curs_main.c +++ b/lib-ui/curs_main.c @@ -14,14 +14,13 @@ #include #include -#include - #include #include "curses.h" #include "menu.h" #include "mutt.h" +#include "crypt.h" #include "pattern.h" #include "alias.h" #include "sort.h" diff --git a/lib-ui/hdrline.c b/lib-ui/hdrline.c index 24aa3de..50049ef 100644 --- a/lib-ui/hdrline.c +++ b/lib-ui/hdrline.c @@ -12,10 +12,10 @@ #include #include #include -#include #include "mutt.h" #include "alias.h" +#include "crypt.h" #include "sort.h" #include "thread.h" #include "charset.h" diff --git a/main.c b/main.c index a02ecbc..4a1fe59 100644 --- a/main.c +++ b/main.c @@ -23,10 +23,10 @@ #include #include #include -#include #include #include "mutt.h" +#include "crypt.h" #include "alias.h" #include "buffy.h" #include "sort.h" diff --git a/nntp/nntp.c b/nntp/nntp.c index b7d83ea..bd1fcf0 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -20,8 +20,7 @@ #include "sort.h" #include "nntp.h" #include "buffy.h" - -#include +#include "crypt.h" #define WANT_LISTGROUP_COMMAND 0 diff --git a/pager.c b/pager.c index be51928..348e679 100644 --- a/pager.c +++ b/pager.c @@ -12,7 +12,6 @@ */ #include -#include #include #include @@ -21,6 +20,7 @@ #include #include "mutt.h" +#include "crypt.h" #include "alias.h" #include "keymap.h" #include "sort.h" diff --git a/pattern.c b/pattern.c index 8d0e4ba..6bcda21 100644 --- a/pattern.c +++ b/pattern.c @@ -10,7 +10,6 @@ #include #include -#include #include #include #include @@ -18,6 +17,7 @@ #include "pattern.h" #include "alias.h" +#include "crypt.h" #include "handler.h" #include "keymap.h" #include "copy.h" diff --git a/pop/pop.c b/pop/pop.c index 0db1cec..a09e1fc 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -14,7 +14,7 @@ #include "mutt.h" #include "pop.h" -#include +#include "crypt.h" /* write line to file */ static int fetch_message (char *line, void *file) diff --git a/postpone.c b/postpone.c index 7f5aff1..485b44f 100644 --- a/postpone.c +++ b/postpone.c @@ -23,7 +23,7 @@ #include "handler.h" #include "sort.h" #include "thread.h" -#include +#include "crypt.h" #include diff --git a/recvattach.c b/recvattach.c index ef8fc18..e2fbd41 100644 --- a/recvattach.c +++ b/recvattach.c @@ -17,9 +17,9 @@ #include #include #include -#include #include "mutt.h" +#include "crypt.h" #include "handler.h" #include "recvattach.h" #include "attach.h" diff --git a/send.c b/send.c index 2da13d7..67f0d7c 100644 --- a/send.c +++ b/send.c @@ -19,7 +19,7 @@ #include "alias.h" #include "keymap.h" #include "copy.h" -#include +#include "crypt.h" #include "mutt_idna.h" #include "attach.h" diff --git a/sendlib.c b/sendlib.c index 152862b..328aa2b 100644 --- a/sendlib.c +++ b/sendlib.c @@ -18,10 +18,9 @@ #include #include -#include - #include "mutt.h" #include "handler.h" +#include "crypt.h" #include "recvattach.h" #include "copy.h" #include "pager.h" -- 2.20.1