* src/summaryview.c
[claws.git] / src / 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         
176         return 0;
177 }
178
179 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
180 {
181         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
182         
183         g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
184
185         return sgpgme_sigstat_gpgme_to_privacy(data->sigstatus);
186 }
187
188 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
189 {
190         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
191         
192         g_return_val_if_fail(data != NULL, g_strdup("Error"));
193
194         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
195 }
196
197 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
198 {
199         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
200         
201         g_return_val_if_fail(data != NULL, g_strdup("Error"));
202
203         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
204 }
205
206 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
207 {
208         MimeInfo *tmpinfo;
209         const gchar *tmpstr;
210         
211         if (mimeinfo->type != MIMETYPE_MULTIPART)
212                 return FALSE;
213         if (g_strcasecmp(mimeinfo->subtype, "encrypted"))
214                 return FALSE;
215         tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
216         if ((tmpstr == NULL) || g_strcasecmp(tmpstr, "application/pgp-encrypted"))
217                 return FALSE;
218         if (g_node_n_children(mimeinfo->node) != 2)
219                 return FALSE;
220         
221         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
222         if (tmpinfo->type != MIMETYPE_APPLICATION)
223                 return FALSE;
224         if (g_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
225                 return FALSE;
226         
227         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
228         if (tmpinfo->type != MIMETYPE_APPLICATION)
229                 return FALSE;
230         if (g_strcasecmp(tmpinfo->subtype, "octet-stream"))
231                 return FALSE;
232         
233         return TRUE;
234 }
235
236 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
237 {
238         MimeInfo *encinfo, *decinfo, *parseinfo;
239         GpgmeData cipher, plain;
240         static gint id = 0;
241         FILE *dstfp;
242         gint nread;
243         gchar *fname;
244         gchar buf[BUFFSIZE];
245         
246         g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
247         
248         encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
249
250         cipher = sgpgme_data_from_mimeinfo(encinfo);
251         plain = sgpgme_decrypt(cipher);
252         gpgme_data_release(cipher);
253         if (plain == NULL)
254                 return NULL;
255         
256         fname = g_strdup_printf("%s%cplaintext.%08x",
257                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
258
259         if ((dstfp = fopen(fname, "wb")) == NULL) {
260                 FILE_OP_ERROR(fname, "fopen");
261                 g_free(fname);
262                 gpgme_data_release(plain);
263                 return NULL;
264         }
265
266         gpgme_data_rewind (plain);
267         while (gpgme_data_read(plain, buf, sizeof(buf), &nread) == GPGME_No_Error) {
268                 fwrite (buf, nread, 1, dstfp);
269         }
270         fclose(dstfp);
271         
272         gpgme_data_release(plain);
273
274         parseinfo = procmime_scan_file(fname);
275         g_free(fname);
276         if (parseinfo == NULL)
277                 return NULL;
278         decinfo = g_node_first_child(parseinfo->node) != NULL ?
279                 g_node_first_child(parseinfo->node)->data : NULL;
280         if (decinfo == NULL)
281                 return NULL;
282
283         g_node_unlink(decinfo->node);
284         procmime_mimeinfo_free_all(parseinfo);
285
286         decinfo->tmpfile = TRUE;
287         
288         return decinfo;
289 }
290
291 static PrivacySystem pgpmime_system = {
292         "pgpmime",                      /* id */
293         "PGP/Mime",                     /* name */
294
295         pgpmime_free_privacydata,       /* free_privacydata */
296
297         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
298         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
299         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
300         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
301         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
302
303         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
304         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
305 };
306
307 void pgpmime_init()
308 {
309         privacy_register_system(&pgpmime_system);
310 }
311
312 void pgpmime_done()
313 {
314         privacy_unregister_system(&pgpmime_system);
315 }
316
317 #endif /* USE_GPGME */