2009-02-23 [cleroy] 3.7.0cvs73
[claws.git] / src / plugins / pgpmime / pgpmime.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2009 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                         fclose(dstfp);
368                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
369                         g_free(fname);
370                         gpgme_data_release(plain);
371                         gpgme_release(ctx);
372                         debug_print("can't open!\n");
373                         return NULL;
374                 }
375         }
376         if (fclose(dstfp) == EOF) {
377                 FILE_OP_ERROR(fname, "fclose");
378                 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
379                 g_free(fname);
380                 gpgme_data_release(plain);
381                 gpgme_release(ctx);
382                 debug_print("can't open!\n");
383                 return NULL;
384         }
385
386         parseinfo = procmime_scan_file(fname);
387         g_free(fname);
388         if (parseinfo == NULL) {
389                 gpgme_release(ctx);
390                 privacy_set_error(_("Couldn't parse decrypted file."));
391                 return NULL;
392         }
393         decinfo = g_node_first_child(parseinfo->node) != NULL ?
394                 g_node_first_child(parseinfo->node)->data : NULL;
395         if (decinfo == NULL) {
396                 privacy_set_error(_("Couldn't parse decrypted file parts."));
397                 gpgme_release(ctx);
398                 return NULL;
399         }
400
401         g_node_unlink(decinfo->node);
402         procmime_mimeinfo_free_all(parseinfo);
403
404         decinfo->tmp = TRUE;
405
406         if (sigstat != NULL && sigstat->signatures != NULL) {
407                 if (decinfo->privacy != NULL) {
408                         data = (PrivacyDataPGP *) decinfo->privacy;
409                 } else {
410                         data = pgpmime_new_privacydata();
411                         decinfo->privacy = (PrivacyData *) data;        
412                 }
413                 data->done_sigtest = TRUE;
414                 data->is_signed = TRUE;
415                 data->sigstatus = sigstat;
416                 if (data->ctx)
417                         gpgme_release(data->ctx);
418                 data->ctx = ctx;
419         } else
420                 gpgme_release(ctx);
421
422         return decinfo;
423 }
424
425 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
426 {
427         MimeInfo *msgcontent, *sigmultipart, *newinfo;
428         gchar *textstr, *micalg;
429         FILE *fp;
430         gchar *boundary = NULL;
431         gchar *sigcontent;
432         gpgme_ctx_t ctx;
433         gpgme_data_t gpgtext, gpgsig;
434         gpgme_error_t err;
435         size_t len;
436         struct passphrase_cb_info_s info;
437         gpgme_sign_result_t result = NULL;
438         gchar *test_msg;
439         
440         fp = my_tmpfile();
441         if (fp == NULL) {
442                 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
443                 return FALSE;
444         }
445         procmime_write_mimeinfo(mimeinfo, fp);
446         rewind(fp);
447
448         /* read temporary file into memory */
449         test_msg = file_read_stream_to_str(fp);
450         fclose(fp);
451         
452         memset (&info, 0, sizeof info);
453
454         /* remove content node from message */
455         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
456         g_node_unlink(msgcontent->node);
457
458         /* create temporary multipart for content */
459         sigmultipart = procmime_mimeinfo_new();
460         sigmultipart->type = MIMETYPE_MULTIPART;
461         sigmultipart->subtype = g_strdup("signed");
462         
463         do {
464                 g_free(boundary);
465                 boundary = generate_mime_boundary("Sig");
466         } while (strstr(test_msg, boundary) != NULL);
467         
468         g_free(test_msg);
469
470         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
471                             g_strdup(boundary));
472         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
473                             g_strdup("application/pgp-signature"));
474         g_node_append(sigmultipart->node, msgcontent->node);
475         g_node_append(mimeinfo->node, sigmultipart->node);
476
477         /* write message content to temporary file */
478         fp = my_tmpfile();
479         if (fp == NULL) {
480                 perror("my_tmpfile");
481                 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
482                 return FALSE;
483         }
484         procmime_write_mimeinfo(sigmultipart, fp);
485         rewind(fp);
486
487         /* read temporary file into memory */
488         textstr = get_canonical_content(fp, boundary);
489
490         fclose(fp);
491
492         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
493         gpgme_data_new(&gpgsig);
494         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
495                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
496                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
497                 return FALSE;
498         }
499         gpgme_set_textmode(ctx, 1);
500         gpgme_set_armor(ctx, 1);
501         gpgme_signers_clear (ctx);
502
503         if (!sgpgme_setup_signers(ctx, account, from_addr)) {
504                 gpgme_release(ctx);
505                 return FALSE;
506         }
507
508         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
509         if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
510                 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
511         } else {
512                 info.c = ctx;
513                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
514         }
515
516         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
517         if (err != GPG_ERR_NO_ERROR) {
518                 if (err == GPG_ERR_CANCELED) {
519                         /* ignore cancelled signing */
520                         privacy_reset_error();
521                         debug_print("gpgme_op_sign cancelled\n");
522                 } else {
523                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
524                         debug_print("gpgme_op_sign error : %x\n", err);
525                 }
526                 gpgme_release(ctx);
527                 return FALSE;
528         }
529         result = gpgme_op_sign_result(ctx);
530         if (result && result->signatures) {
531                 gpgme_new_signature_t sig = result->signatures;
532                 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
533                         micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
534                                 result->signatures->hash_algo));
535                 } else {
536                         micalg = g_strdup(gpgme_hash_algo_name(
537                                 result->signatures->hash_algo));
538                 }
539                 while (sig) {
540                         debug_print("valid signature: %s\n", sig->fpr);
541                         sig = sig->next;
542                 }
543         } else if (result && result->invalid_signers) {
544                 gpgme_invalid_key_t invalid = result->invalid_signers;
545                 while (invalid) {
546                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
547                                 gpgme_strerror(invalid->reason));
548                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
549                                 gpgme_strerror(invalid->reason));
550                         invalid = invalid->next;
551                 }
552                 gpgme_release(ctx);
553                 return FALSE;
554         } else {
555                 /* can't get result (maybe no signing key?) */
556                 debug_print("gpgme_op_sign_result error\n");
557                 privacy_set_error(_("Data signing failed, no results."));
558                 gpgme_release(ctx);
559                 return FALSE;
560         }
561
562         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
563         gpgme_data_release(gpgtext);
564         g_free(textstr);
565
566         if (sigcontent == NULL || len <= 0) {
567                 g_warning("sgpgme_data_release_and_get_mem failed");
568                 privacy_set_error(_("Data signing failed, no contents."));
569                 return FALSE;
570         }
571
572         /* add signature */
573         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
574                             micalg);
575
576         newinfo = procmime_mimeinfo_new();
577         newinfo->type = MIMETYPE_APPLICATION;
578         newinfo->subtype = g_strdup("pgp-signature");
579         g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
580                              g_strdup("signature.asc"));
581         newinfo->content = MIMECONTENT_MEM;
582         newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
583         g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
584                             g_strdup("signature.asc"));
585         newinfo->data.mem = g_malloc(len + 1);
586         g_memmove(newinfo->data.mem, sigcontent, len);
587         newinfo->data.mem[len] = '\0';
588         g_node_append(sigmultipart->node, newinfo->node);
589
590         g_free(sigcontent);
591         gpgme_release(ctx);
592
593         return TRUE;
594 }
595 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
596 {
597         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
598 }
599
600 static const gchar *pgpmime_get_encrypt_warning(void)
601 {
602         if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
603                 return NULL;
604         else
605                 return _("Please note that email headers, like Subject, "
606                          "are not encrypted by the PGP/Mime system.");
607 }
608
609 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
610 {
611         if (inhibit)
612                 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
613         else
614                 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
615 }
616
617 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
618 {
619         MimeInfo *msgcontent, *encmultipart, *newinfo;
620         FILE *fp;
621         gchar *boundary, *enccontent;
622         size_t len;
623         gchar *textstr;
624         gpgme_data_t gpgtext = NULL, gpgenc = NULL;
625         gpgme_ctx_t ctx = NULL;
626         gpgme_key_t *kset = NULL;
627         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
628         gint i = 0;
629         gpgme_error_t err;
630         
631         while (fprs[i] && strlen(fprs[i])) {
632                 i++;
633         }
634         
635         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
636         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
637         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
638                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
639                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
640                 return FALSE;
641         }
642         i = 0;
643         while (fprs[i] && strlen(fprs[i])) {
644                 gpgme_key_t key;
645                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
646                 if (err) {
647                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
648                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
649                         return FALSE;
650                 }
651                 debug_print("found %s at %d\n", fprs[i], i);
652                 kset[i] = key;
653                 i++;
654         }
655         
656         debug_print("Encrypting message content\n");
657
658         /* remove content node from message */
659         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
660         g_node_unlink(msgcontent->node);
661
662         /* create temporary multipart for content */
663         encmultipart = procmime_mimeinfo_new();
664         encmultipart->type = MIMETYPE_MULTIPART;
665         encmultipart->subtype = g_strdup("encrypted");
666         boundary = generate_mime_boundary("Encrypt");
667         g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
668                             g_strdup(boundary));
669         g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
670                             g_strdup("application/pgp-encrypted"));
671         g_node_append(encmultipart->node, msgcontent->node);
672
673         /* write message content to temporary file */
674         fp = my_tmpfile();
675         if (fp == NULL) {
676                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
677                 return FALSE;
678         }
679         procmime_write_mimeinfo(encmultipart, fp);
680         rewind(fp);
681
682         /* read temporary file into memory */
683         textstr = get_canonical_content(fp, boundary);
684
685         fclose(fp);
686
687         /* encrypt data */
688         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
689         gpgme_data_new(&gpgenc);
690         gpgme_set_armor(ctx, 1);
691         gpgme_data_rewind(gpgtext);
692         
693         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
694
695         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
696         gpgme_data_release(gpgtext);
697         g_free(textstr);
698
699         if (enccontent == NULL || len <= 0) {
700                 g_warning("sgpgme_data_release_and_get_mem failed");
701                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
702                 gpgme_release(ctx);
703                 return FALSE;
704         }
705
706         /* create encrypted multipart */
707         g_node_unlink(msgcontent->node);
708         procmime_mimeinfo_free_all(msgcontent);
709         g_node_append(mimeinfo->node, encmultipart->node);
710
711         newinfo = procmime_mimeinfo_new();
712         newinfo->type = MIMETYPE_APPLICATION;
713         newinfo->subtype = g_strdup("pgp-encrypted");
714         newinfo->content = MIMECONTENT_MEM;
715         newinfo->data.mem = g_strdup("Version: 1\n");
716         g_node_append(encmultipart->node, newinfo->node);
717
718         newinfo = procmime_mimeinfo_new();
719         newinfo->type = MIMETYPE_APPLICATION;
720         newinfo->subtype = g_strdup("octet-stream");
721         newinfo->content = MIMECONTENT_MEM;
722         newinfo->data.mem = g_malloc(len + 1);
723         g_memmove(newinfo->data.mem, enccontent, len);
724         newinfo->data.mem[len] = '\0';
725         g_node_append(encmultipart->node, newinfo->node);
726
727         g_free(enccontent);
728         gpgme_release(ctx);
729
730         return TRUE;
731 }
732
733 static PrivacySystem pgpmime_system = {
734         "pgpmime",                      /* id */
735         "PGP MIME",                     /* name */
736
737         pgpmime_free_privacydata,       /* free_privacydata */
738
739         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
740         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
741         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
742         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
743         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
744
745         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
746         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
747
748         TRUE,
749         pgpmime_sign,
750
751         TRUE,
752         pgpmime_get_encrypt_data,
753         pgpmime_encrypt,
754         pgpmime_get_encrypt_warning,
755         pgpmime_inhibit_encrypt_warning,
756 };
757
758 void pgpmime_init()
759 {
760         privacy_register_system(&pgpmime_system);
761 }
762
763 void pgpmime_done()
764 {
765         privacy_unregister_system(&pgpmime_system);
766 }
767
768 struct PluginFeature *plugin_provides(void)
769 {
770         static struct PluginFeature features[] = 
771                 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
772                   {PLUGIN_NOTHING, NULL}};
773         return features;
774 }
775 #endif /* USE_GPGME */