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