This commit was manufactured by cvs2svn to create branch 'gtk2'.
[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         if (prefs_common.auto_check_signatures)
120                 pgpmime_check_signature(mimeinfo);
121         
122         return TRUE;
123 }
124
125 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
126 {
127         PrivacyDataPGP *data;
128         MimeInfo *parent, *signature;
129         FILE *fp;
130         gchar buf[BUFFSIZE];
131         gchar *boundary;
132         GString *textstr;
133         gint boundary_len;
134         GpgmeData sigdata, textdata;
135         
136         g_return_val_if_fail(mimeinfo != NULL, -1);
137         g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
138         data = (PrivacyDataPGP *) mimeinfo->privacy;
139         
140         debug_print("Checking PGP/MIME signature\n");
141         parent = procmime_mimeinfo_parent(mimeinfo);
142
143         fp = fopen(parent->filename, "rb");
144         g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
145         
146         boundary = g_hash_table_lookup(parent->parameters, "boundary");
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         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
187 }
188
189 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
190 {
191         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
192         
193         g_return_val_if_fail(data != NULL, g_strdup("Error"));
194
195         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
196 }
197
198 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
199 {
200         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
201         
202         g_return_val_if_fail(data != NULL, g_strdup("Error"));
203
204         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
205 }
206
207 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
208 {
209         MimeInfo *tmpinfo;
210         const gchar *tmpstr;
211         
212         if (mimeinfo->type != MIMETYPE_MULTIPART)
213                 return FALSE;
214         if (g_strcasecmp(mimeinfo->subtype, "encrypted"))
215                 return FALSE;
216         tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
217         if ((tmpstr == NULL) || g_strcasecmp(tmpstr, "application/pgp-encrypted"))
218                 return FALSE;
219         if (g_node_n_children(mimeinfo->node) != 2)
220                 return FALSE;
221         
222         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
223         if (tmpinfo->type != MIMETYPE_APPLICATION)
224                 return FALSE;
225         if (g_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
226                 return FALSE;
227         
228         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
229         if (tmpinfo->type != MIMETYPE_APPLICATION)
230                 return FALSE;
231         if (g_strcasecmp(tmpinfo->subtype, "octet-stream"))
232                 return FALSE;
233         
234         return TRUE;
235 }
236
237 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
238 {
239         MimeInfo *encinfo, *decinfo, *parseinfo;
240         GpgmeData cipher, plain;
241         static gint id = 0;
242         FILE *dstfp;
243         gint nread;
244         gchar *fname;
245         gchar buf[BUFFSIZE];
246         
247         g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
248         
249         encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
250
251         cipher = sgpgme_data_from_mimeinfo(encinfo);
252         plain = sgpgme_decrypt(cipher);
253         gpgme_data_release(cipher);
254         if (plain == NULL)
255                 return NULL;
256         
257         fname = g_strdup_printf("%s%cplaintext.%08x",
258                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
259
260         if ((dstfp = fopen(fname, "wb")) == NULL) {
261                 FILE_OP_ERROR(fname, "fopen");
262                 g_free(fname);
263                 gpgme_data_release(plain);
264                 return NULL;
265         }
266
267         fprintf(dstfp, "MIME-Version: 1.0\n");
268         gpgme_data_rewind (plain);
269         while (gpgme_data_read(plain, buf, sizeof(buf), &nread) == GPGME_No_Error) {
270                 fwrite (buf, nread, 1, dstfp);
271         }
272         fclose(dstfp);
273         
274         gpgme_data_release(plain);
275
276         parseinfo = procmime_scan_file(fname);
277         g_free(fname);
278         if (parseinfo == NULL)
279                 return NULL;
280         decinfo = g_node_first_child(parseinfo->node) != NULL ?
281                 g_node_first_child(parseinfo->node)->data : NULL;
282         if (decinfo == NULL)
283                 return NULL;
284
285         g_node_unlink(decinfo->node);
286         procmime_mimeinfo_free_all(parseinfo);
287
288         decinfo->tmpfile = TRUE;
289         
290         return decinfo;
291 }
292
293 static PrivacySystem pgpmime_system = {
294         "pgpmime",                      /* id */
295         "PGP/Mime",                     /* name */
296
297         pgpmime_free_privacydata,       /* free_privacydata */
298
299         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
300         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
301         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
302         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
303         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
304
305         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
306         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
307 };
308
309 void pgpmime_init()
310 {
311         privacy_register_system(&pgpmime_system);
312 }
313
314 void pgpmime_done()
315 {
316         privacy_unregister_system(&pgpmime_system);
317 }
318
319 #endif /* USE_GPGME */