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