push username, homedir and tmpdir in lua too.
[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             if (tok == MIME_SIGN || tok == MIME_SIGNCLEAR)
93                 t |= PGPSIGN;
94
95             tok = mime_which_token(parameter_getval(m->parameter, "format"), -1);
96             if (tok == MIME_KEYS_ONLY)
97                 t |= PGPKEY;
98
99             if (!t)
100                 t |= PGPENCRYPT;        /* not necessarily correct, but... */
101         }
102
103         if (subtype == MIME_PGP_SIGNED)
104             t |= PGPSIGN;
105
106         if (subtype == MIME_PGP_KEYS)
107             t |= PGPKEY;
108     }
109
110     if (m->type == TYPETEXT && subtype == MIME_PLAIN) {
111         const char *p;
112
113         if ((p = parameter_getval(m->parameter, "x-action"))) {
114             int tok = mime_which_token(p, -1);
115             switch (tok) {
116               case MIME_PGP_SIGNED:
117                 t |= PGPSIGN;
118                 break;
119
120               case MIME_PGP_ENCRYPTED:
121                 t |= PGPENCRYPT;
122                 break;
123
124               case MIME_PGP_KEYS:
125                 t |= PGPKEY;
126                 break;
127
128               default:
129                 break;
130             }
131         }
132     }
133
134     return t ? t | PGPINLINE : 0;
135 }
136
137 #include "mutt.h"
138
139 int mutt_is_application_smime (BODY * m)
140 {
141     char *t = NULL;
142     int len, complain = 0;
143
144     if (!m)
145         return 0;
146
147     if ((m->type & TYPEAPPLICATION) && m->subtype) {
148         int subtype = mime_which_token(m->subtype, -1);
149
150         /* S/MIME MIME types don't need x- anymore, see RFC2311 */
151         if (subtype == MIME_X_PKCS7_MIME || subtype == MIME_PKCS7_MIME) {
152             t = parameter_getval(m->parameter, "smime-type");
153
154             if (t) {
155                 switch (mime_which_token(t, -1)) {
156                   case MIME_ENVELOPED_DATA:
157                     return SMIMEENCRYPT;
158
159                   case MIME_SIGNED_DATA:
160                     return SMIMESIGN | SMIMEOPAQUE;
161
162                   default:
163                     return 0;
164                 }
165             }
166
167             complain = 1;
168         }
169
170         if (subtype == MIME_OCTET_STREAM)
171             return 0;
172
173         t = parameter_getval(m->parameter, "name");
174         if (!t)
175             t = m->d_filename;
176         if (!t)
177             t = m->filename;
178
179         if (!t) {
180             if (complain)
181                 mutt_message(_("S/MIME messages with no hints on content are unsupported."));
182             return 0;
183         }
184
185         /* no .p7c, .p10 support yet. */
186
187         len = m_strlen(t) - 4;
188         if (len > 0 && t[len] == '.'
189         &&  tolower((unsigned char)t[len + 1]) == 'p'
190         &&  t[len + 2] == '7')
191         {
192             switch (t[len + 3]) {
193               case 'm': case 'M':
194                 /* Not sure if this is the correct thing to do, but 
195                    it's required for compatibility with Outlook */
196                 return (SMIMESIGN | SMIMEOPAQUE);
197
198               case 's': case 'S':
199                 return (SMIMESIGN | SMIMEOPAQUE);
200             }
201         }
202     }
203
204     return 0;
205 }
206
207