3719895c2f66144a0bcafeb80fed898c408a4188
[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         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 = g_hash_table_lookup(parent->parameters, "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         
166         gpgme_data_new_from_mem(&textdata, textstr->str, textstr->len, 0);
167         signature = (MimeInfo *) mimeinfo->node->next->data;
168         gpgme_data_new_from_filepart(&sigdata,
169                 signature->filename,
170                 NULL,
171                 signature->offset,
172                 signature->length);
173
174         data->sigstatus =
175                 sgpgme_verify_signature (data->ctx, sigdata, textdata);
176         
177         gpgme_data_release(sigdata);
178         gpgme_data_release(textdata);
179         g_string_free(textstr, TRUE);
180         
181         return 0;
182 }
183
184 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
185 {
186         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
187         
188         g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
189
190         return sgpgme_sigstat_gpgme_to_privacy(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         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
200 }
201
202 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
203 {
204         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
205         
206         g_return_val_if_fail(data != NULL, g_strdup("Error"));
207
208         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
209 }
210
211 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
212 {
213         MimeInfo *tmpinfo;
214         
215         if (mimeinfo->type != MIMETYPE_MULTIPART)
216                 return FALSE;
217         if (g_strcasecmp(mimeinfo->subtype, "encrypted"))
218                 return FALSE;
219         if (g_strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "protocol"), "application/pgp-encrypted"))
220                 return FALSE;
221         if (g_node_n_children(mimeinfo->node) != 2)
222                 return FALSE;
223         
224         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
225         if (tmpinfo->type != MIMETYPE_APPLICATION)
226                 return FALSE;
227         if (g_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
228                 return FALSE;
229         
230         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
231         if (tmpinfo->type != MIMETYPE_APPLICATION)
232                 return FALSE;
233         if (g_strcasecmp(tmpinfo->subtype, "octet-stream"))
234                 return FALSE;
235         
236         return TRUE;
237 }
238
239 static PrivacySystem pgpmime_system = {
240         "PGP/Mime",                     /* name */
241
242         pgpmime_free_privacydata,       /* free_privacydata */
243
244         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
245         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
246         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
247         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
248         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
249
250         /* NOT YET */
251         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
252         NULL,                           /* decrypt(MimeInfo *) */
253 };
254
255 void pgpmime_init()
256 {
257         privacy_register_system(&pgpmime_system);
258 }
259
260 void pgpmime_done()
261 {
262         privacy_unregister_system(&pgpmime_system);
263 }
264
265 #endif /* USE_GPGME */