2004-09-08 [colin] 0.9.12cvs99.1
[claws.git] / src / plugins / pgpmime / pgpmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #ifdef USE_GPGME
25
26 #include "defs.h"
27 #include <glib.h>
28 #include <gpgme.h>
29
30 #include "utils.h"
31 #include "privacy.h"
32 #include "procmime.h"
33 #include "pgpmime.h"
34 #include "sgpgme.h"
35 #include "prefs_common.h"
36
37 typedef struct _PrivacyDataPGP PrivacyDataPGP;
38
39 struct _PrivacyDataPGP
40 {
41         PrivacyData     data;
42         
43         gboolean        done_sigtest;
44         gboolean        is_signed;
45         GpgmeSigStat    sigstatus;
46         GpgmeCtx        ctx;
47 };
48
49 static PrivacySystem pgpmime_system;
50
51 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
52
53 static PrivacyDataPGP *pgpmime_new_privacydata()
54 {
55         PrivacyDataPGP *data;
56
57         data = g_new0(PrivacyDataPGP, 1);
58         data->data.system = &pgpmime_system;
59         data->done_sigtest = FALSE;
60         data->is_signed = FALSE;
61         data->sigstatus = GPGME_SIG_STAT_NONE;
62         gpgme_new(&data->ctx);
63         
64         return data;
65 }
66
67 static void pgpmime_free_privacydata(PrivacyData *_data)
68 {
69         PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
70         
71         g_free(data);
72 }
73
74 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
75 {
76         MimeInfo *parent;
77         MimeInfo *signature;
78         const gchar *protocol;
79         PrivacyDataPGP *data = NULL;
80         
81         g_return_val_if_fail(mimeinfo != NULL, FALSE);
82         if (mimeinfo->privacy != NULL) {
83                 data = (PrivacyDataPGP *) mimeinfo->privacy;
84                 if (data->done_sigtest)
85                         return data->is_signed;
86         }
87         
88         /* check parent */
89         parent = procmime_mimeinfo_parent(mimeinfo);
90         if (parent == NULL)
91                 return FALSE;
92         if ((parent->type != MIMETYPE_MULTIPART) ||
93             g_strcasecmp(parent->subtype, "signed"))
94                 return FALSE;
95         protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
96         if ((protocol == NULL) || g_strcasecmp(protocol, "application/pgp-signature"))
97                 return FALSE;
98
99         /* check if mimeinfo is the first child */
100         if (parent->node->children->data != mimeinfo)
101                 return FALSE;
102
103         /* check signature */
104         signature = parent->node->children->next != NULL ? 
105             (MimeInfo *) parent->node->children->next->data : NULL;
106         if (signature == NULL)
107                 return FALSE;
108         if ((signature->type != MIMETYPE_APPLICATION) ||
109             g_strcasecmp(signature->subtype, "pgp-signature"))
110                 return FALSE;
111
112         if (data == NULL) {
113                 data = pgpmime_new_privacydata();
114                 mimeinfo->privacy = (PrivacyData *) data;
115         }
116         data->done_sigtest = TRUE;
117         data->is_signed = TRUE;
118
119         return TRUE;
120 }
121
122 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
123 {
124         PrivacyDataPGP *data;
125         MimeInfo *parent, *signature;
126         FILE *fp;
127         gchar buf[BUFFSIZE];
128         gchar *boundary;
129         GString *textstr;
130         gint boundary_len;
131         GpgmeData sigdata, textdata;
132         
133         g_return_val_if_fail(mimeinfo != NULL, -1);
134         g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
135         data = (PrivacyDataPGP *) mimeinfo->privacy;
136         
137         debug_print("Checking PGP/MIME signature\n");
138         parent = procmime_mimeinfo_parent(mimeinfo);
139
140         fp = fopen(parent->filename, "rb");
141         g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
142         
143         boundary = g_hash_table_lookup(parent->parameters, "boundary");
144         if (!boundary)
145                 return 0;
146
147         boundary_len = strlen(boundary);
148         while (fgets(buf, sizeof(buf), fp) != NULL)
149                 if (IS_BOUNDARY(buf, boundary, boundary_len))
150                         break;
151
152         textstr = g_string_new("");
153         while (fgets(buf, sizeof(buf), fp) != NULL) {
154                 gchar *buf2;
155
156                 if (IS_BOUNDARY(buf, boundary, boundary_len))
157                         break;
158                 
159                 buf2 = canonicalize_str(buf);
160                 g_string_append(textstr, buf2);
161                 g_free(buf2);
162         }
163         g_string_truncate(textstr, textstr->len - 2);
164                 
165         gpgme_data_new_from_mem(&textdata, textstr->str, textstr->len, 0);
166         signature = (MimeInfo *) mimeinfo->node->next->data;
167         sigdata = sgpgme_data_from_mimeinfo(signature);
168
169         data->sigstatus =
170                 sgpgme_verify_signature (data->ctx, sigdata, textdata);
171         
172         gpgme_data_release(sigdata);
173         gpgme_data_release(textdata);
174         g_string_free(textstr, TRUE);
175         fclose(fp);
176         
177         return 0;
178 }
179
180 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
181 {
182         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
183         
184         g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
185
186         if (data->sigstatus == GPGME_SIG_STAT_NONE && 
187             prefs_common.auto_check_signatures)
188                 pgpmime_check_signature(mimeinfo);
189         
190         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
191 }
192
193 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
194 {
195         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
196         
197         g_return_val_if_fail(data != NULL, g_strdup("Error"));
198
199         if (data->sigstatus == GPGME_SIG_STAT_NONE && 
200             prefs_common.auto_check_signatures)
201                 pgpmime_check_signature(mimeinfo);
202         
203         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
204 }
205
206 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
207 {
208         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
209         
210         g_return_val_if_fail(data != NULL, g_strdup("Error"));
211
212         if (data->sigstatus == GPGME_SIG_STAT_NONE && 
213             prefs_common.auto_check_signatures)
214                 pgpmime_check_signature(mimeinfo);
215         
216         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
217 }
218
219 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
220 {
221         MimeInfo *tmpinfo;
222         const gchar *tmpstr;
223         
224         if (mimeinfo->type != MIMETYPE_MULTIPART)
225                 return FALSE;
226         if (g_strcasecmp(mimeinfo->subtype, "encrypted"))
227                 return FALSE;
228         tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
229         if ((tmpstr == NULL) || g_strcasecmp(tmpstr, "application/pgp-encrypted"))
230                 return FALSE;
231         if (g_node_n_children(mimeinfo->node) != 2)
232                 return FALSE;
233         
234         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
235         if (tmpinfo->type != MIMETYPE_APPLICATION)
236                 return FALSE;
237         if (g_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
238                 return FALSE;
239         
240         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
241         if (tmpinfo->type != MIMETYPE_APPLICATION)
242                 return FALSE;
243         if (g_strcasecmp(tmpinfo->subtype, "octet-stream"))
244                 return FALSE;
245         
246         return TRUE;
247 }
248
249 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
250 {
251         MimeInfo *encinfo, *decinfo, *parseinfo;
252         GpgmeData cipher, plain;
253         static gint id = 0;
254         FILE *dstfp;
255         gint nread;
256         gchar *fname;
257         gchar buf[BUFFSIZE];
258         GpgmeSigStat sigstat = 0;
259         PrivacyDataPGP *data = NULL;
260         GpgmeCtx ctx;
261         
262         if (gpgme_new(&ctx) != GPGME_No_Error)
263                 return NULL;
264
265         
266         g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
267         
268         encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
269
270         cipher = sgpgme_data_from_mimeinfo(encinfo);
271         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
272
273         gpgme_data_release(cipher);
274         if (plain == NULL)
275                 return NULL;
276         
277         fname = g_strdup_printf("%s%cplaintext.%08x",
278                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
279
280         if ((dstfp = fopen(fname, "wb")) == NULL) {
281                 FILE_OP_ERROR(fname, "fopen");
282                 g_free(fname);
283                 gpgme_data_release(plain);
284                 return NULL;
285         }
286
287         fprintf(dstfp, "MIME-Version: 1.0\n");
288         gpgme_data_rewind (plain);
289         while (gpgme_data_read(plain, buf, sizeof(buf), &nread) == GPGME_No_Error) {
290                 fwrite (buf, nread, 1, dstfp);
291         }
292         fclose(dstfp);
293         
294         gpgme_data_release(plain);
295
296         parseinfo = procmime_scan_file(fname);
297         g_free(fname);
298         if (parseinfo == NULL)
299                 return NULL;
300         decinfo = g_node_first_child(parseinfo->node) != NULL ?
301                 g_node_first_child(parseinfo->node)->data : NULL;
302         if (decinfo == NULL)
303                 return NULL;
304
305         g_node_unlink(decinfo->node);
306         procmime_mimeinfo_free_all(parseinfo);
307
308         decinfo->tmpfile = TRUE;
309
310         if (sigstat != GPGME_SIG_STAT_NONE) {
311                 if (decinfo->privacy != NULL) {
312                         data = (PrivacyDataPGP *) decinfo->privacy;
313                 } else {
314                         data = pgpmime_new_privacydata();
315                         decinfo->privacy = (PrivacyData *) data;        
316                 }
317                 data->done_sigtest = TRUE;
318                 data->is_signed = TRUE;
319                 data->sigstatus = sigstat;
320                 if (data->ctx)
321                         gpgme_release(data->ctx);
322                 data->ctx = ctx;
323         } else 
324                 gpgme_release(ctx);
325
326         return decinfo;
327 }
328
329 static PrivacySystem pgpmime_system = {
330         "pgpmime",                      /* id */
331         "PGP Mime",                     /* name */
332
333         pgpmime_free_privacydata,       /* free_privacydata */
334
335         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
336         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
337         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
338         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
339         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
340
341         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
342         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
343 };
344
345 void pgpmime_init()
346 {
347         privacy_register_system(&pgpmime_system);
348 }
349
350 void pgpmime_done()
351 {
352         privacy_unregister_system(&pgpmime_system);
353 }
354
355 #endif /* USE_GPGME */