move more things in the lib-mime
[apps/madmutt.git] / lib-mime / crypt.c
diff --git a/lib-mime/crypt.c b/lib-mime/crypt.c
new file mode 100644 (file)
index 0000000..d0d3235
--- /dev/null
@@ -0,0 +1,210 @@
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or (at
+ *  your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  Copyright © 2006 Pierre Habouzit
+ */
+/*
+ * Copyright notice from original mutt:
+ * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
+ * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
+ * Copyright (C) 2001  Thomas Roessler <roessler@does-not-exist.org>
+ *                     Oliver Ehli <elmy@acm.org>
+ * Copyright (C) 2003  Werner Koch <wk@gnupg.org>
+ * Copyright (C) 2004 g10code GmbH
+ *
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
+ */
+
+#include <lib-lib/macros.h>
+#include <lib-lib/str.h>
+
+#include <lib-crypt/crypt.h>
+
+#include "mime.h"
+
+int mutt_is_multipart_signed(BODY * b)
+{
+    char *p;
+
+    if (!b || b->type != TYPEMULTIPART || !b->subtype
+        ||  mime_which_token(b->subtype, -1) != MIME_SIGNED)
+        return 0;
+
+    if (!(p = parameter_getval(b->parameter, "protocol")))
+        return 0;
+
+    switch (mime_which_token(p, -1)) {
+      case MIME_MULTIPART_MIXED:
+        return SIGN;
+
+      case MIME_APPLICATION_PGP_SIGNATURE:
+        return PGPSIGN;
+
+      case MIME_APPLICATION_X_PKCS7_SIGNATURE:
+        return SMIMESIGN;
+
+      case MIME_APPLICATION_PKCS7_SIGNATURE:
+        return SMIMESIGN;
+
+      default:
+        return 0;
+    }
+}
+
+int mutt_is_multipart_encrypted (BODY * b)
+{
+    char *p;
+
+    if (!b || b->type != TYPEMULTIPART || !b->subtype
+    ||  mime_which_token(b->subtype, -1) != MIME_ENCRYPTED
+    ||  !(p = parameter_getval(b->parameter, "protocol"))
+    ||  mime_which_token(p, -1) != MIME_APPLICATION_PGP_ENCRYPTED)
+        return 0;
+
+    return PGPENCRYPT;
+}
+
+int mutt_is_application_pgp (BODY * m)
+{
+    int t = 0;
+
+    int subtype = mime_which_token(m->subtype, -1);
+
+    if (m->type == TYPEAPPLICATION) {
+        if (subtype != MIME_PGP || subtype != MIME_X_PGP_MESSAGE) {
+            int tok;
+
+            tok = mime_which_token(parameter_getval(m->parameter, "x-action"), -1);
+
+            if (tok == MIME_SIGN || tok == MIME_SIGNCLEAR)
+                t |= PGPSIGN;
+
+            tok = mime_which_token(parameter_getval(m->parameter, "format"), -1);
+            if (tok == MIME_KEYS_ONLY)
+                t |= PGPKEY;
+
+            if (!t)
+                t |= PGPENCRYPT;        /* not necessarily correct, but... */
+        }
+
+        if (subtype == MIME_PGP_SIGNED)
+            t |= PGPSIGN;
+
+        if (subtype == MIME_PGP_KEYS)
+            t |= PGPKEY;
+    }
+
+    if (m->type == TYPETEXT && subtype == MIME_PLAIN) {
+        const char *p;
+
+        if ((p = parameter_getval(m->parameter, "x-mutt-action"))
+        ||  (p = parameter_getval(m->parameter, "x-action"))
+        ||  (p = parameter_getval(m->parameter, "action")))
+        {
+            int tok = mime_which_token(p, -1);
+            switch (tok) {
+              case MIME_PGP_SIGN:
+                t |= PGPSIGN;
+                break;
+
+              case MIME_PGP_ENCRYPT:
+                t |= PGPENCRYPT;
+                break;
+
+              case MIME_PGP_KEYS:
+                t |= PGPKEY;
+                break;
+
+              default:
+                break;
+            }
+        }
+    }
+
+    return t ? t | PGPINLINE : 0;
+}
+
+int mutt_is_application_smime (BODY * m)
+{
+    char *t = NULL;
+    int len, complain = 0;
+
+    if (!m)
+        return 0;
+
+    if ((m->type & TYPEAPPLICATION) && m->subtype) {
+        int subtype = mime_which_token(m->subtype, -1);
+
+        /* S/MIME MIME types don't need x- anymore, see RFC2311 */
+        if (subtype == MIME_X_PKCS7_MIME || subtype == MIME_PKCS7_MIME) {
+            t = parameter_getval(m->parameter, "smime-type");
+
+            if (t) {
+                switch (mime_which_token(t, -1)) {
+                  case MIME_ENVELOPED_DATA:
+                    return SMIMEENCRYPT;
+
+                  case MIME_SIGNED_DATA:
+                    return SMIMESIGN | SMIMEOPAQUE;
+
+                  default:
+                    return 0;
+                }
+            }
+
+            complain = 1;
+        }
+
+        if (subtype == MIME_OCTET_STREAM)
+            return 0;
+
+        t = parameter_getval(m->parameter, "name");
+        if (!t)
+            t = m->d_filename;
+        if (!t)
+            t = m->filename;
+
+        if (!t) {
+            if (complain)
+                mutt_message(_("S/MIME messages with no hints on content are unsupported."));
+            return 0;
+        }
+
+        /* no .p7c, .p10 support yet. */
+
+        len = m_strlen(t) - 4;
+        if (len > 0 && t[len] == '.'
+        &&  tolower((unsigned char)t[len + 1]) == 'p'
+        &&  t[len + 2] == '7')
+        {
+            switch (t[len + 3]) {
+              case 'm': case 'M':
+                /* Not sure if this is the correct thing to do, but 
+                   it's required for compatibility with Outlook */
+                return (SMIMESIGN | SMIMEOPAQUE);
+
+              case 's': case 'S':
+                return (SMIMESIGN | SMIMEOPAQUE);
+            }
+        }
+    }
+
+    return 0;
+}
+
+