8e0eca648b227da496304ccce10132942e96a6e1
[claws.git] / src / plugins / pgpmime / sgpgme.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 <time.h>
27 #include <gtk/gtk.h>
28 #include <gpgme.h>
29
30 #include "sgpgme.h"
31 #include "privacy.h"
32 #include "prefs_common.h"
33 #include "utils.h"
34 #include "alertpanel.h"
35 #include "passphrase.h"
36 #include "intl.h"
37
38 static void idle_function_for_gpgme(void)
39 {
40         while (gtk_events_pending())
41                 gtk_main_iteration();
42 }
43
44 static void sgpgme_disable_all(void)
45 {
46     /* FIXME: set a flag, so that we don't bother the user with failed
47      * gpgme messages */
48 }
49
50 GpgmeSigStat sgpgme_verify_signature(GpgmeCtx ctx, GpgmeData sig, 
51                                         GpgmeData plain)
52 {
53         GpgmeSigStat status;
54
55         if (gpgme_op_verify(ctx, sig, plain, &status) != GPGME_No_Error)
56                 return GPGME_SIG_STAT_ERROR;
57
58         return status;
59 }
60
61 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(GpgmeCtx ctx, GpgmeSigStat status)
62 {
63         unsigned long validity = 0;
64         
65         validity = gpgme_get_sig_ulong_attr(ctx, 0,
66                 GPGME_ATTR_VALIDITY, 0);
67
68         switch (status) {
69         case GPGME_SIG_STAT_GOOD:
70                 if ((validity != GPGME_VALIDITY_MARGINAL) &&
71                     (validity != GPGME_VALIDITY_FULL) &&
72                     (validity != GPGME_VALIDITY_ULTIMATE))
73                         return SIGNATURE_WARN;
74                 return SIGNATURE_OK;
75         case GPGME_SIG_STAT_GOOD_EXP:
76         case GPGME_SIG_STAT_GOOD_EXPKEY:
77         case GPGME_SIG_STAT_DIFF:
78                 return SIGNATURE_WARN;
79         case GPGME_SIG_STAT_BAD:
80                 return SIGNATURE_INVALID;
81         case GPGME_SIG_STAT_NOKEY:
82         case GPGME_SIG_STAT_NOSIG:
83         case GPGME_SIG_STAT_ERROR:
84                 return SIGNATURE_CHECK_FAILED;
85         case GPGME_SIG_STAT_NONE:
86                 return SIGNATURE_UNCHECKED;
87         }
88         return SIGNATURE_CHECK_FAILED;
89 }
90
91 static const gchar *get_validity_str(unsigned long validity)
92 {
93         switch (validity) {
94         case GPGME_VALIDITY_UNKNOWN:
95                 return _("Unknown");
96         case GPGME_VALIDITY_UNDEFINED:
97                 return _("Undefined");
98         case GPGME_VALIDITY_NEVER:
99                 return _("Never");
100         case GPGME_VALIDITY_MARGINAL:
101                 return _("Marginal");
102         case GPGME_VALIDITY_FULL:
103                 return _("Full");
104         case GPGME_VALIDITY_ULTIMATE:
105                 return _("Ultimate");
106         default:
107                 return _("Error");
108         }
109 }
110
111 gchar *sgpgme_sigstat_info_short(GpgmeCtx ctx, GpgmeSigStat status)
112 {
113         switch (status) {
114         case GPGME_SIG_STAT_GOOD:
115         {
116                 GpgmeKey key;
117                 unsigned long validity = 0;
118         
119                 if (gpgme_get_sig_key(ctx, 0, &key) != GPGME_No_Error)
120                         return g_strdup(_("Error"));
121
122                 validity = gpgme_get_sig_ulong_attr(ctx, 0,
123                         GPGME_ATTR_VALIDITY, 0);
124                 
125                 return g_strdup_printf(_("Valid signature by %s (Trust: %s)"),
126                         gpgme_key_get_string_attr(key, GPGME_ATTR_NAME, NULL, 0),
127                         get_validity_str(validity));
128         }
129         case GPGME_SIG_STAT_GOOD_EXP:
130                 return g_strdup(_("The signature has expired"));
131         case GPGME_SIG_STAT_GOOD_EXPKEY:
132                 return g_strdup(_("The key that was used to sign this part has expired"));
133         case GPGME_SIG_STAT_DIFF:
134                 return g_strdup(_("Not all signatures are valid"));
135         case GPGME_SIG_STAT_BAD:
136                 return g_strdup(_("This signature is invalid"));
137         case GPGME_SIG_STAT_NOKEY:
138                 return g_strdup(_("You have no key to verify this signature"));
139         case GPGME_SIG_STAT_NOSIG:
140                 return g_strdup(_("No signature found"));
141         case GPGME_SIG_STAT_ERROR:
142                 return g_strdup(_("An error occured"));
143         case GPGME_SIG_STAT_NONE:
144                 return g_strdup(_("The signature has not been checked"));
145         }
146         return g_strdup(_("Error"));
147 }
148
149 gchar *sgpgme_sigstat_info_full(GpgmeCtx ctx, GpgmeSigStat status)
150 {
151         gint i = 0;
152         gchar *ret;
153         GString *siginfo;
154         GpgmeKey key;
155         
156         siginfo = g_string_sized_new(64);
157         while (gpgme_get_sig_key(ctx, i, &key) != GPGME_EOF) {
158                 time_t sigtime, expiretime;
159                 GpgmeSigStat sigstatus;
160                 gchar timestr[64];
161                 const gchar *keytype, *keyid, *uid;
162                 
163                 sigtime = gpgme_get_sig_ulong_attr(ctx, i, GPGME_ATTR_CREATED, 0);
164                 strftime(timestr, 64, "%c", gmtime(&sigtime));
165                 keytype = gpgme_key_get_string_attr(key, GPGME_ATTR_ALGO, NULL, 0);
166                 keyid = gpgme_key_get_string_attr(key, GPGME_ATTR_KEYID, NULL, 0);
167                 g_string_sprintfa(siginfo,
168                         _("Signature made %s using %s key ID %s\n"),
169                         timestr, keytype, keyid);
170                 
171                 sigstatus = gpgme_get_sig_ulong_attr(ctx, i, GPGME_ATTR_SIG_STATUS, 0); 
172                 uid = gpgme_key_get_string_attr(key, GPGME_ATTR_USERID, NULL, 0);
173                 switch (sigstatus) {
174                 case GPGME_SIG_STAT_GOOD:
175                 case GPGME_SIG_STAT_GOOD_EXPKEY:
176                         g_string_sprintfa(siginfo,
177                                 _("Good signature from \"%s\"\n"),
178                                 uid);
179                         break;
180                 case GPGME_SIG_STAT_GOOD_EXP:
181                         g_string_sprintfa(siginfo,
182                                 _("Expired signature from \"%s\"\n"),
183                                 uid);
184                         break;
185                 case GPGME_SIG_STAT_BAD:
186                         g_string_sprintfa(siginfo,
187                                 _("BAD signature from \"%s\"\n"),
188                                 uid);
189                         break;
190                 default:
191                         break;
192                 }
193                 if (sigstatus != GPGME_SIG_STAT_BAD) {
194                         gint j = 1;
195                         
196                         while ((uid = gpgme_key_get_string_attr(key, GPGME_ATTR_USERID, NULL, j)) != 0) {
197                                 g_string_sprintfa(siginfo,
198                                         _("                aka \"%s\"\n"),
199                                         uid);
200                                 j++;
201                         }
202                         g_string_sprintfa(siginfo,
203                                 _("Primary key fingerprint: %s\n"), 
204                                 gpgme_key_get_string_attr(key, GPGME_ATTR_FPR, NULL, 0));
205                 }
206
207                 
208                 expiretime = gpgme_get_sig_ulong_attr(ctx, i, GPGME_ATTR_EXPIRE, 0);
209                 if (expiretime > 0) {
210                         const gchar *format;
211
212                         strftime(timestr, 64, "%c", gmtime(&expiretime));
213                         if (time(NULL) < expiretime)
214                                 format = _("Signature expires %s\n");
215                         else
216                                 format = _("Signature expired %s\n");
217                         g_string_sprintfa(siginfo, format, timestr);
218                 }
219                 
220                 g_string_append(siginfo, "\n");
221                 i++;
222         }
223
224         ret = siginfo->str;
225         g_string_free(siginfo, FALSE);
226         return ret;
227 }
228
229 GpgmeData sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
230 {
231         GpgmeData data;
232         
233         gpgme_data_new_from_filepart(&data,
234                 mimeinfo->filename,
235                 NULL,
236                 mimeinfo->offset,
237                 mimeinfo->length);
238         
239         return data;
240 }
241
242 GpgmeData sgpgme_decrypt_verify(GpgmeData cipher, GpgmeSigStat *status, GpgmeCtx ctx)
243 {
244         struct passphrase_cb_info_s info;
245         GpgmeData plain;
246         GpgmeError err;
247         GpgmeSigStat sigstat;
248
249         memset (&info, 0, sizeof info);
250         
251         if (gpgme_data_new(&plain) != GPGME_No_Error) {
252                 gpgme_release(ctx);
253                 return NULL;
254         }
255         
256         if (!getenv("GPG_AGENT_INFO")) {
257                 info.c = ctx;
258                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
259         }
260
261         err = gpgme_op_decrypt_verify(ctx, cipher, plain, status);
262
263         if (err != GPGME_No_Error) {
264                 gpgmegtk_free_passphrase();
265                 gpgme_data_release(plain);
266                 return NULL;
267         }
268
269         return plain;
270 }
271
272 void sgpgme_init()
273 {
274         if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) != 
275                         GPGME_No_Error) {  /* Also does some gpgme init */
276                 sgpgme_disable_all();
277                 debug_print("gpgme_engine_version:\n%s\n",
278                             gpgme_get_engine_info());
279
280                 if (prefs_common.gpg_warning) {
281                         AlertValue val;
282
283                         val = alertpanel_message_with_disable
284                                 (_("Warning"),
285                                  _("GnuPG is not installed properly, or needs "
286                                    "to be upgraded.\n"
287                                    "OpenPGP support disabled."), ALERT_WARNING);
288                         if (val & G_ALERTDISABLE)
289                                 prefs_common.gpg_warning = FALSE;
290                 }
291         }
292
293         gpgme_register_idle(idle_function_for_gpgme);
294 }
295
296 void sgpgme_done()
297 {
298         gpgmegtk_free_passphrase();
299 }
300
301 #endif /* USE_GPGME */