2011-08-31 [colin] 3.7.10cvs14
[claws.git] / src / plugins / pgpmime / pgpmime.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 <glib/gi18n.h>
29 #include <gpgme.h>
30 #include <ctype.h>
31 #include <errno.h>
32
33 #include "utils.h"
34 #include "privacy.h"
35 #include "procmime.h"
36 #include "plugin.h"
37
38 #include "pgpmime.h"
39 #include <plugins/pgpcore/sgpgme.h>
40 #include <plugins/pgpcore/prefs_gpg.h>
41 #include <plugins/pgpcore/passphrase.h>
42
43 #include "prefs_common.h"
44
45 typedef struct _PrivacyDataPGP PrivacyDataPGP;
46
47 struct _PrivacyDataPGP
48 {
49         PrivacyData     data;
50         
51         gboolean        done_sigtest;
52         gboolean        is_signed;
53         gpgme_verify_result_t   sigstatus;
54         gpgme_ctx_t     ctx;
55 };
56
57 static PrivacySystem pgpmime_system;
58
59 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
60
61 static PrivacyDataPGP *pgpmime_new_privacydata()
62 {
63         PrivacyDataPGP *data;
64         gpgme_error_t err;
65
66         data = g_new0(PrivacyDataPGP, 1);
67         data->data.system = &pgpmime_system;
68         data->done_sigtest = FALSE;
69         data->is_signed = FALSE;
70         data->sigstatus = NULL;
71         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
72                 g_warning(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
73                 return NULL;
74         }
75         
76         return data;
77 }
78
79 static void pgpmime_free_privacydata(PrivacyData *_data)
80 {
81         PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
82         gpgme_release(data->ctx);
83         g_free(data);
84 }
85
86 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
87 {
88         MimeInfo *parent;
89         MimeInfo *signature;
90         const gchar *protocol;
91         PrivacyDataPGP *data = NULL;
92         
93         cm_return_val_if_fail(mimeinfo != NULL, FALSE);
94         if (mimeinfo->privacy != NULL) {
95                 data = (PrivacyDataPGP *) mimeinfo->privacy;
96                 if (data->done_sigtest)
97                         return data->is_signed;
98         }
99         
100         /* check parent */
101         parent = procmime_mimeinfo_parent(mimeinfo);
102         if (parent == NULL)
103                 return FALSE;
104         if ((parent->type != MIMETYPE_MULTIPART) ||
105             g_ascii_strcasecmp(parent->subtype, "signed"))
106                 return FALSE;
107         protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
108         if ((protocol == NULL) || 
109             (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
110                 return FALSE;
111
112         /* check if mimeinfo is the first child */
113         if (parent->node->children->data != mimeinfo)
114                 return FALSE;
115
116         /* check signature */
117         signature = parent->node->children->next != NULL ? 
118             (MimeInfo *) parent->node->children->next->data : NULL;
119         if (signature == NULL)
120                 return FALSE;
121         if ((signature->type != MIMETYPE_APPLICATION) ||
122             (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
123                 return FALSE;
124
125         if (data == NULL) {
126                 data = pgpmime_new_privacydata();
127                 mimeinfo->privacy = (PrivacyData *) data;
128         }
129         
130         data->done_sigtest = TRUE;
131         data->is_signed = TRUE;
132
133         return TRUE;
134 }
135
136 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
137 {
138         gchar *ret;
139         GString *textbuffer;
140         guint boundary_len;
141         gchar buf[BUFFSIZE];
142
143         boundary_len = strlen(boundary);
144         while (fgets(buf, sizeof(buf), fp) != NULL)
145                 if (IS_BOUNDARY(buf, boundary, boundary_len))
146                         break;
147
148         textbuffer = g_string_new("");
149         while (fgets(buf, sizeof(buf), fp) != NULL) {
150                 gchar *buf2;
151
152                 if (IS_BOUNDARY(buf, boundary, boundary_len))
153                         break;
154                 
155                 buf2 = canonicalize_str(buf);
156                 g_string_append(textbuffer, buf2);
157                 g_free(buf2);
158         }
159         g_string_truncate(textbuffer, textbuffer->len - 2);
160                 
161         ret = textbuffer->str;
162         g_string_free(textbuffer, FALSE);
163
164         return ret;
165 }
166
167 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
168 {
169         PrivacyDataPGP *data;
170         MimeInfo *parent, *signature;
171         FILE *fp;
172         gchar *boundary;
173         gchar *textstr;
174         gpgme_data_t sigdata = NULL, textdata = NULL;
175         gpgme_error_t err;
176         cm_return_val_if_fail(mimeinfo != NULL, -1);
177         cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
178         data = (PrivacyDataPGP *) mimeinfo->privacy;
179         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
180                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
181                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
182                 return 0;
183         }
184
185         
186         debug_print("Checking PGP/MIME signature\n");
187
188         err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
189
190         if (err) {
191                 debug_print ("gpgme_set_protocol failed: %s\n",
192                    gpgme_strerror (err));
193         }
194         parent = procmime_mimeinfo_parent(mimeinfo);
195
196         fp = g_fopen(parent->data.filename, "rb");
197         cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
198         
199         boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
200         if (!boundary) {
201                 privacy_set_error(_("Signature boundary not found."));
202                 fclose(fp);
203                 return 0;
204         }
205         textstr = get_canonical_content(fp, boundary);
206
207         err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0);
208         if (err) {
209                 debug_print ("gpgme_data_new_from_mem failed: %s\n",
210                    gpgme_strerror (err));
211         }
212         signature = (MimeInfo *) mimeinfo->node->next->data;
213         sigdata = sgpgme_data_from_mimeinfo(signature);
214
215         err = 0;
216         if (signature->encoding_type == ENC_BASE64) {
217                 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
218         }
219         
220         if (err) {
221                 debug_print ("gpgme_data_set_encoding failed: %s\n",
222                         gpgme_strerror (err));
223         }
224
225         data->sigstatus =
226                 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
227
228         gpgme_data_release(sigdata);
229         gpgme_data_release(textdata);
230         g_free(textstr);
231         fclose(fp);
232         
233         return 0;
234 }
235
236 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
237 {
238         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
239         
240         cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
241
242         if (data->sigstatus == NULL && 
243             prefs_gpg_get_config()->auto_check_signatures)
244                 pgpmime_check_signature(mimeinfo);
245         
246         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
247 }
248
249 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
250 {
251         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
252         
253         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
254
255         if (data->sigstatus == NULL && 
256             prefs_gpg_get_config()->auto_check_signatures)
257                 pgpmime_check_signature(mimeinfo);
258         
259         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
260 }
261
262 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
263 {
264         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
265         
266         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
267
268         if (data->sigstatus == NULL && 
269             prefs_gpg_get_config()->auto_check_signatures)
270                 pgpmime_check_signature(mimeinfo);
271         
272         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
273 }
274
275 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
276 {
277         MimeInfo *tmpinfo;
278         const gchar *tmpstr;
279         
280         if (mimeinfo->type != MIMETYPE_MULTIPART)
281                 return FALSE;
282         if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
283                 return FALSE;
284         tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
285         if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
286                 return FALSE;
287         if (g_node_n_children(mimeinfo->node) != 2)
288                 return FALSE;
289         
290         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
291         if (tmpinfo->type != MIMETYPE_APPLICATION)
292                 return FALSE;
293         if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
294                 return FALSE;
295         
296         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
297         if (tmpinfo->type != MIMETYPE_APPLICATION)
298                 return FALSE;
299         if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
300                 return FALSE;
301         
302         return TRUE;
303 }
304
305 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
306 {
307         MimeInfo *encinfo, *decinfo, *parseinfo;
308         gpgme_data_t cipher = NULL, plain = NULL;
309         static gint id = 0;
310         FILE *dstfp;
311         gchar *fname;
312         gpgme_verify_result_t sigstat = NULL;
313         PrivacyDataPGP *data = NULL;
314         gpgme_ctx_t ctx;
315         gchar *chars;
316         size_t len;
317         gpgme_error_t err;
318
319         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
320                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
321                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
322                 return NULL;
323         }
324         
325         cm_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
326         
327         encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
328
329         cipher = sgpgme_data_from_mimeinfo(encinfo);
330         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
331
332         gpgme_data_release(cipher);
333         if (plain == NULL) {
334                 debug_print("plain is null!\n");
335                 gpgme_release(ctx);
336                 return NULL;
337         }
338
339         fname = g_strdup_printf("%s%cplaintext.%08x",
340                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
341
342         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
343                 FILE_OP_ERROR(fname, "fopen");
344                 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
345                 g_free(fname);
346                 gpgme_data_release(plain);
347                 gpgme_release(ctx);
348                 debug_print("can't open!\n");
349                 return NULL;
350         }
351
352         if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
353                 FILE_OP_ERROR(fname, "fprintf");
354                 fclose(dstfp);
355                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
356                 g_free(fname);
357                 gpgme_data_release(plain);
358                 gpgme_release(ctx);
359                 debug_print("can't open!\n");
360                 return NULL;
361         }
362
363         chars = sgpgme_data_release_and_get_mem(plain, &len);
364         if (len > 0) {
365                 if (fwrite(chars, 1, len, dstfp) < len) {
366                         FILE_OP_ERROR(fname, "fwrite");
367                         g_free(chars);
368                         fclose(dstfp);
369                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
370                         g_free(fname);
371                         gpgme_data_release(plain);
372                         gpgme_release(ctx);
373                         debug_print("can't open!\n");
374                         return NULL;
375                 }
376         }
377         g_free(chars);
378
379         if (fclose(dstfp) == EOF) {
380                 FILE_OP_ERROR(fname, "fclose");
381                 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
382                 g_free(fname);
383                 gpgme_data_release(plain);
384                 gpgme_release(ctx);
385                 debug_print("can't open!\n");
386                 return NULL;
387         }
388
389         parseinfo = procmime_scan_file(fname);
390         g_free(fname);
391         if (parseinfo == NULL) {
392                 gpgme_release(ctx);
393                 privacy_set_error(_("Couldn't parse decrypted file."));
394                 return NULL;
395         }
396         decinfo = g_node_first_child(parseinfo->node) != NULL ?
397                 g_node_first_child(parseinfo->node)->data : NULL;
398         if (decinfo == NULL) {
399                 privacy_set_error(_("Couldn't parse decrypted file parts."));
400                 gpgme_release(ctx);
401                 return NULL;
402         }
403
404         g_node_unlink(decinfo->node);
405         procmime_mimeinfo_free_all(parseinfo);
406
407         decinfo->tmp = TRUE;
408
409         if (sigstat != NULL && sigstat->signatures != NULL) {
410                 if (decinfo->privacy != NULL) {
411                         data = (PrivacyDataPGP *) decinfo->privacy;
412                 } else {
413                         data = pgpmime_new_privacydata();
414                         decinfo->privacy = (PrivacyData *) data;        
415                 }
416                 data->done_sigtest = TRUE;
417                 data->is_signed = TRUE;
418                 data->sigstatus = sigstat;
419                 if (data->ctx)
420                         gpgme_release(data->ctx);
421                 data->ctx = ctx;
422         } else
423                 gpgme_release(ctx);
424
425         return decinfo;
426 }
427
428 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
429 {
430         MimeInfo *msgcontent, *sigmultipart, *newinfo;
431         gchar *textstr, *micalg = NULL;
432         FILE *fp;
433         gchar *boundary = NULL;
434         gchar *sigcontent;
435         gpgme_ctx_t ctx;
436         gpgme_data_t gpgtext, gpgsig;
437         gpgme_error_t err;
438         size_t len;
439         struct passphrase_cb_info_s info;
440         gpgme_sign_result_t result = NULL;
441         gchar *test_msg;
442         
443         fp = my_tmpfile();
444         if (fp == NULL) {
445                 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
446                 return FALSE;
447         }
448         procmime_write_mimeinfo(mimeinfo, fp);
449         rewind(fp);
450
451         /* read temporary file into memory */
452         test_msg = file_read_stream_to_str(fp);
453         fclose(fp);
454         
455         memset (&info, 0, sizeof info);
456
457         /* remove content node from message */
458         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
459         g_node_unlink(msgcontent->node);
460
461         /* create temporary multipart for content */
462         sigmultipart = procmime_mimeinfo_new();
463         sigmultipart->type = MIMETYPE_MULTIPART;
464         sigmultipart->subtype = g_strdup("signed");
465         
466         do {
467                 g_free(boundary);
468                 boundary = generate_mime_boundary("Sig");
469         } while (strstr(test_msg, boundary) != NULL);
470         
471         g_free(test_msg);
472
473         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
474                             g_strdup(boundary));
475         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
476                             g_strdup("application/pgp-signature"));
477         g_node_append(sigmultipart->node, msgcontent->node);
478         g_node_append(mimeinfo->node, sigmultipart->node);
479
480         /* write message content to temporary file */
481         fp = my_tmpfile();
482         if (fp == NULL) {
483                 perror("my_tmpfile");
484                 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
485                 return FALSE;
486         }
487         procmime_write_mimeinfo(sigmultipart, fp);
488         rewind(fp);
489
490         /* read temporary file into memory */
491         textstr = get_canonical_content(fp, boundary);
492
493         fclose(fp);
494
495         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
496         gpgme_data_new(&gpgsig);
497         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
498                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
499                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
500                 return FALSE;
501         }
502         gpgme_set_textmode(ctx, 1);
503         gpgme_set_armor(ctx, 1);
504         gpgme_signers_clear (ctx);
505
506         if (!sgpgme_setup_signers(ctx, account, from_addr)) {
507                 gpgme_release(ctx);
508                 return FALSE;
509         }
510
511         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
512         if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
513                 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
514         } else {
515                 info.c = ctx;
516                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
517         }
518
519         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
520         if (err != GPG_ERR_NO_ERROR) {
521                 if (err == GPG_ERR_CANCELED) {
522                         /* ignore cancelled signing */
523                         privacy_reset_error();
524                         debug_print("gpgme_op_sign cancelled\n");
525                 } else {
526                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
527                         debug_print("gpgme_op_sign error : %x\n", err);
528                 }
529                 gpgme_release(ctx);
530                 return FALSE;
531         }
532         result = gpgme_op_sign_result(ctx);
533         if (result && result->signatures) {
534                 gpgme_new_signature_t sig = result->signatures;
535                 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
536                         micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
537                                 result->signatures->hash_algo));
538                 } else {
539                         micalg = g_strdup(gpgme_hash_algo_name(
540                                 result->signatures->hash_algo));
541                 }
542                 while (sig) {
543                         debug_print("valid signature: %s\n", sig->fpr);
544                         sig = sig->next;
545                 }
546         } else if (result && result->invalid_signers) {
547                 gpgme_invalid_key_t invalid = result->invalid_signers;
548                 while (invalid) {
549                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
550                                 gpgme_strerror(invalid->reason));
551                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
552                                 gpgme_strerror(invalid->reason));
553                         invalid = invalid->next;
554                 }
555                 gpgme_release(ctx);
556                 return FALSE;
557         } else {
558                 /* can't get result (maybe no signing key?) */
559                 debug_print("gpgme_op_sign_result error\n");
560                 privacy_set_error(_("Data signing failed, no results."));
561                 gpgme_release(ctx);
562                 return FALSE;
563         }
564
565         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
566         gpgme_data_release(gpgtext);
567         g_free(textstr);
568
569         if (sigcontent == NULL || len <= 0) {
570                 g_warning("sgpgme_data_release_and_get_mem failed");
571                 privacy_set_error(_("Data signing failed, no contents."));
572                 g_free(micalg);
573                 return FALSE;
574         }
575
576         /* add signature */
577         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
578                             micalg);
579
580         newinfo = procmime_mimeinfo_new();
581         newinfo->type = MIMETYPE_APPLICATION;
582         newinfo->subtype = g_strdup("pgp-signature");
583         g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
584                              g_strdup("signature.asc"));
585         newinfo->content = MIMECONTENT_MEM;
586         newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
587         g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
588                             g_strdup("signature.asc"));
589         newinfo->data.mem = g_malloc(len + 1);
590         g_memmove(newinfo->data.mem, sigcontent, len);
591         newinfo->data.mem[len] = '\0';
592         g_node_append(sigmultipart->node, newinfo->node);
593
594         g_free(sigcontent);
595         gpgme_release(ctx);
596
597         return TRUE;
598 }
599 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
600 {
601         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
602 }
603
604 static const gchar *pgpmime_get_encrypt_warning(void)
605 {
606         if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
607                 return NULL;
608         else
609                 return _("Please note that email headers, like Subject, "
610                          "are not encrypted by the PGP/Mime system.");
611 }
612
613 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
614 {
615         if (inhibit)
616                 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
617         else
618                 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
619 }
620
621 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
622 {
623         MimeInfo *msgcontent, *encmultipart, *newinfo;
624         FILE *fp;
625         gchar *boundary, *enccontent;
626         size_t len;
627         gchar *textstr;
628         gpgme_data_t gpgtext = NULL, gpgenc = NULL;
629         gpgme_ctx_t ctx = NULL;
630         gpgme_key_t *kset = NULL;
631         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
632         gint i = 0;
633         gpgme_error_t err;
634         
635         while (fprs[i] && strlen(fprs[i])) {
636                 i++;
637         }
638         
639         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
640         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
641         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
642                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
643                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
644                 g_free(kset);
645                 return FALSE;
646         }
647         i = 0;
648         while (fprs[i] && strlen(fprs[i])) {
649                 gpgme_key_t key;
650                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
651                 if (err) {
652                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
653                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
654                         g_free(kset);
655                         return FALSE;
656                 }
657                 debug_print("found %s at %d\n", fprs[i], i);
658                 kset[i] = key;
659                 i++;
660         }
661         
662         debug_print("Encrypting message content\n");
663
664         /* remove content node from message */
665         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
666         g_node_unlink(msgcontent->node);
667
668         /* create temporary multipart for content */
669         encmultipart = procmime_mimeinfo_new();
670         encmultipart->type = MIMETYPE_MULTIPART;
671         encmultipart->subtype = g_strdup("encrypted");
672         boundary = generate_mime_boundary("Encrypt");
673         g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
674                             g_strdup(boundary));
675         g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
676                             g_strdup("application/pgp-encrypted"));
677         g_node_append(encmultipart->node, msgcontent->node);
678
679         /* write message content to temporary file */
680         fp = my_tmpfile();
681         if (fp == NULL) {
682                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
683                 g_free(kset);
684                 return FALSE;
685         }
686         procmime_write_mimeinfo(encmultipart, fp);
687         rewind(fp);
688
689         /* read temporary file into memory */
690         textstr = get_canonical_content(fp, boundary);
691
692         fclose(fp);
693
694         /* encrypt data */
695         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
696         gpgme_data_new(&gpgenc);
697         gpgme_set_armor(ctx, 1);
698         cm_gpgme_data_rewind(gpgtext);
699         
700         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
701
702         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
703         gpgme_data_release(gpgtext);
704         g_free(textstr);
705         g_free(kset);
706
707         if (enccontent == NULL || len <= 0) {
708                 g_warning("sgpgme_data_release_and_get_mem failed");
709                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
710                 gpgme_release(ctx);
711                 return FALSE;
712         }
713
714         /* create encrypted multipart */
715         g_node_unlink(msgcontent->node);
716         procmime_mimeinfo_free_all(msgcontent);
717         g_node_append(mimeinfo->node, encmultipart->node);
718
719         newinfo = procmime_mimeinfo_new();
720         newinfo->type = MIMETYPE_APPLICATION;
721         newinfo->subtype = g_strdup("pgp-encrypted");
722         newinfo->content = MIMECONTENT_MEM;
723         newinfo->data.mem = g_strdup("Version: 1\n");
724         g_node_append(encmultipart->node, newinfo->node);
725
726         newinfo = procmime_mimeinfo_new();
727         newinfo->type = MIMETYPE_APPLICATION;
728         newinfo->subtype = g_strdup("octet-stream");
729         newinfo->content = MIMECONTENT_MEM;
730         newinfo->data.mem = g_malloc(len + 1);
731         g_memmove(newinfo->data.mem, enccontent, len);
732         newinfo->data.mem[len] = '\0';
733         g_node_append(encmultipart->node, newinfo->node);
734
735         g_free(enccontent);
736         gpgme_release(ctx);
737
738         return TRUE;
739 }
740
741 static PrivacySystem pgpmime_system = {
742         "pgpmime",                      /* id */
743         "PGP MIME",                     /* name */
744
745         pgpmime_free_privacydata,       /* free_privacydata */
746
747         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
748         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
749         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
750         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
751         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
752
753         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
754         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
755
756         TRUE,
757         pgpmime_sign,
758
759         TRUE,
760         pgpmime_get_encrypt_data,
761         pgpmime_encrypt,
762         pgpmime_get_encrypt_warning,
763         pgpmime_inhibit_encrypt_warning,
764 };
765
766 void pgpmime_init()
767 {
768         privacy_register_system(&pgpmime_system);
769 }
770
771 void pgpmime_done()
772 {
773         privacy_unregister_system(&pgpmime_system);
774 }
775
776 struct PluginFeature *plugin_provides(void)
777 {
778         static struct PluginFeature features[] = 
779                 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
780                   {PLUGIN_NOTHING, NULL}};
781         return features;
782 }
783 #endif /* USE_GPGME */