919c3bbd08b6388b0e7d48ab9dc87f8402a579bc
[apps/madmutt.git] / lib-mime / crypt.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * Copyright notice from original mutt:
21  * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
22  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
23  * Copyright (C) 2001  Thomas Roessler <roessler@does-not-exist.org>
24  *                     Oliver Ehli <elmy@acm.org>
25  * Copyright (C) 2003  Werner Koch <wk@gnupg.org>
26  * Copyright (C) 2004 g10code GmbH
27  *
28  * This file is part of mutt-ng, see http://www.muttng.org/.
29  * It's licensed under the GNU General Public License,
30  * please see the file GPL in the top level source directory.
31  */
32
33 #include <lib-lib/lib-lib.h>
34
35 #include <lib-crypt/crypt.h>
36
37 #include "mime.h"
38
39 int mutt_is_multipart_signed(BODY * b)
40 {
41     char *p;
42
43     if (!b || b->type != TYPEMULTIPART || !b->subtype
44         ||  mime_which_token(b->subtype, -1) != MIME_SIGNED)
45         return 0;
46
47     if (!(p = parameter_getval(b->parameter, "protocol")))
48         return 0;
49
50     switch (mime_which_token(p, -1)) {
51       case MIME_MULTIPART_MIXED:
52         return SIGN;
53
54       case MIME_APPLICATION_PGP_SIGNATURE:
55         return PGPSIGN;
56
57       case MIME_APPLICATION_X_PKCS7_SIGNATURE:
58         return SMIMESIGN;
59
60       case MIME_APPLICATION_PKCS7_SIGNATURE:
61         return SMIMESIGN;
62
63       default:
64         return 0;
65     }
66 }
67
68 int mutt_is_multipart_encrypted (BODY * b)
69 {
70     char *p;
71
72     if (!b || b->type != TYPEMULTIPART || !b->subtype
73     ||  mime_which_token(b->subtype, -1) != MIME_ENCRYPTED
74     ||  !(p = parameter_getval(b->parameter, "protocol"))
75     ||  mime_which_token(p, -1) != MIME_APPLICATION_PGP_ENCRYPTED)
76         return 0;
77
78     return PGPENCRYPT;
79 }
80
81 int mutt_is_application_pgp (BODY * m)
82 {
83     int t = 0;
84
85     int subtype = mime_which_token(m->subtype, -1);
86
87     if (m->type == TYPEAPPLICATION) {
88         if (subtype != MIME_PGP || subtype != MIME_X_PGP_MESSAGE) {
89             int tok;
90
91             tok = mime_which_token(parameter_getval(m->parameter, "x-action"), -1);
92
93             if (tok == MIME_SIGN || tok == MIME_SIGNCLEAR)
94                 t |= PGPSIGN;
95
96             tok = mime_which_token(parameter_getval(m->parameter, "format"), -1);
97             if (tok == MIME_KEYS_ONLY)
98                 t |= PGPKEY;
99
100             if (!t)
101                 t |= PGPENCRYPT;        /* not necessarily correct, but... */
102         }
103
104         if (subtype == MIME_PGP_SIGNED)
105             t |= PGPSIGN;
106
107         if (subtype == MIME_PGP_KEYS)
108             t |= PGPKEY;
109     }
110
111     if (m->type == TYPETEXT && subtype == MIME_PLAIN) {
112         const char *p;
113
114         if ((p = parameter_getval(m->parameter, "x-mutt-action"))
115         ||  (p = parameter_getval(m->parameter, "x-action"))
116         ||  (p = parameter_getval(m->parameter, "action")))
117         {
118             int tok = mime_which_token(p, -1);
119             switch (tok) {
120               case MIME_PGP_SIGN:
121                 t |= PGPSIGN;
122                 break;
123
124               case MIME_PGP_ENCRYPT:
125                 t |= PGPENCRYPT;
126                 break;
127
128               case MIME_PGP_KEYS:
129                 t |= PGPKEY;
130                 break;
131
132               default:
133                 break;
134             }
135         }
136     }
137
138     return t ? t | PGPINLINE : 0;
139 }
140
141 int mutt_is_application_smime (BODY * m)
142 {
143     char *t = NULL;
144     int len, complain = 0;
145
146     if (!m)
147         return 0;
148
149     if ((m->type & TYPEAPPLICATION) && m->subtype) {
150         int subtype = mime_which_token(m->subtype, -1);
151
152         /* S/MIME MIME types don't need x- anymore, see RFC2311 */
153         if (subtype == MIME_X_PKCS7_MIME || subtype == MIME_PKCS7_MIME) {
154             t = parameter_getval(m->parameter, "smime-type");
155
156             if (t) {
157                 switch (mime_which_token(t, -1)) {
158                   case MIME_ENVELOPED_DATA:
159                     return SMIMEENCRYPT;
160
161                   case MIME_SIGNED_DATA:
162                     return SMIMESIGN | SMIMEOPAQUE;
163
164                   default:
165                     return 0;
166                 }
167             }
168
169             complain = 1;
170         }
171
172         if (subtype == MIME_OCTET_STREAM)
173             return 0;
174
175         t = parameter_getval(m->parameter, "name");
176         if (!t)
177             t = m->d_filename;
178         if (!t)
179             t = m->filename;
180
181         if (!t) {
182             if (complain)
183                 mutt_message(_("S/MIME messages with no hints on content are unsupported."));
184             return 0;
185         }
186
187         /* no .p7c, .p10 support yet. */
188
189         len = m_strlen(t) - 4;
190         if (len > 0 && t[len] == '.'
191         &&  tolower((unsigned char)t[len + 1]) == 'p'
192         &&  t[len + 2] == '7')
193         {
194             switch (t[len + 3]) {
195               case 'm': case 'M':
196                 /* Not sure if this is the correct thing to do, but 
197                    it's required for compatibility with Outlook */
198                 return (SMIMESIGN | SMIMEOPAQUE);
199
200               case 's': case 'S':
201                 return (SMIMESIGN | SMIMEOPAQUE);
202             }
203         }
204     }
205
206     return 0;
207 }
208
209