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