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