2008-12-15 [colin] 3.6.1cvs78
[claws.git] / src / plugins / pgpmime / pgpmime.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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         g_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         g_return_val_if_fail(mimeinfo != NULL, -1);
177         g_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         g_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                 return 0;
203         }
204         textstr = get_canonical_content(fp, boundary);
205
206         err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0);
207         if (err) {
208                 debug_print ("gpgme_data_new_from_mem failed: %s\n",
209                    gpgme_strerror (err));
210         }
211         signature = (MimeInfo *) mimeinfo->node->next->data;
212         sigdata = sgpgme_data_from_mimeinfo(signature);
213
214         err = 0;
215         if (signature->encoding_type == ENC_BASE64) {
216                 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
217         }
218         
219         if (err) {
220                 debug_print ("gpgme_data_set_encoding failed: %s\n",
221                         gpgme_strerror (err));
222         }
223
224         data->sigstatus =
225                 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
226
227         gpgme_data_release(sigdata);
228         gpgme_data_release(textdata);
229         g_free(textstr);
230         fclose(fp);
231         
232         return 0;
233 }
234
235 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
236 {
237         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
238         
239         g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
240
241         if (data->sigstatus == NULL && 
242             prefs_gpg_get_config()->auto_check_signatures)
243                 pgpmime_check_signature(mimeinfo);
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         g_return_val_if_fail(data != NULL, g_strdup("Error"));
253
254         if (data->sigstatus == NULL && 
255             prefs_gpg_get_config()->auto_check_signatures)
256                 pgpmime_check_signature(mimeinfo);
257         
258         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
259 }
260
261 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
262 {
263         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
264         
265         g_return_val_if_fail(data != NULL, g_strdup("Error"));
266
267         if (data->sigstatus == NULL && 
268             prefs_gpg_get_config()->auto_check_signatures)
269                 pgpmime_check_signature(mimeinfo);
270         
271         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
272 }
273
274 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
275 {
276         MimeInfo *tmpinfo;
277         const gchar *tmpstr;
278         
279         if (mimeinfo->type != MIMETYPE_MULTIPART)
280                 return FALSE;
281         if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
282                 return FALSE;
283         tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
284         if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
285                 return FALSE;
286         if (g_node_n_children(mimeinfo->node) != 2)
287                 return FALSE;
288         
289         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
290         if (tmpinfo->type != MIMETYPE_APPLICATION)
291                 return FALSE;
292         if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
293                 return FALSE;
294         
295         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
296         if (tmpinfo->type != MIMETYPE_APPLICATION)
297                 return FALSE;
298         if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
299                 return FALSE;
300         
301         return TRUE;
302 }
303
304 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
305 {
306         MimeInfo *encinfo, *decinfo, *parseinfo;
307         gpgme_data_t cipher = NULL, plain = NULL;
308         static gint id = 0;
309         FILE *dstfp;
310         gchar *fname;
311         gpgme_verify_result_t sigstat = NULL;
312         PrivacyDataPGP *data = NULL;
313         gpgme_ctx_t ctx;
314         gchar *chars;
315         size_t len;
316         gpgme_error_t err;
317
318         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
319                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
320                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
321                 return NULL;
322         }
323         
324         g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
325         
326         encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
327
328         cipher = sgpgme_data_from_mimeinfo(encinfo);
329         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
330
331         gpgme_data_release(cipher);
332         if (plain == NULL) {
333                 debug_print("plain is null!\n");
334                 gpgme_release(ctx);
335                 return NULL;
336         }
337
338         fname = g_strdup_printf("%s%cplaintext.%08x",
339                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
340
341         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
342                 FILE_OP_ERROR(fname, "fopen");
343                 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
344                 g_free(fname);
345                 gpgme_data_release(plain);
346                 gpgme_release(ctx);
347                 debug_print("can't open!\n");
348                 return NULL;
349         }
350
351         if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
352                 FILE_OP_ERROR(fname, "fprintf");
353                 privacy_set_error(_("Couldn't write to 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         chars = sgpgme_data_release_and_get_mem(plain, &len);
362         if (len > 0) {
363                 if (fwrite(chars, 1, len, dstfp) < len) {
364                         FILE_OP_ERROR(fname, "fwrite");
365                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
366                         g_free(fname);
367                         gpgme_data_release(plain);
368                         gpgme_release(ctx);
369                         debug_print("can't open!\n");
370                         return NULL;
371                 }
372         }
373         if (fclose(dstfp) == EOF) {
374                 FILE_OP_ERROR(fname, "fclose");
375                 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
376                 g_free(fname);
377                 gpgme_data_release(plain);
378                 gpgme_release(ctx);
379                 debug_print("can't open!\n");
380                 return NULL;
381         }
382
383         parseinfo = procmime_scan_file(fname);
384         g_free(fname);
385         if (parseinfo == NULL) {
386                 gpgme_release(ctx);
387                 privacy_set_error(_("Couldn't parse decrypted file."));
388                 return NULL;
389         }
390         decinfo = g_node_first_child(parseinfo->node) != NULL ?
391                 g_node_first_child(parseinfo->node)->data : NULL;
392         if (decinfo == NULL) {
393                 privacy_set_error(_("Couldn't parse decrypted file parts."));
394                 gpgme_release(ctx);
395                 return NULL;
396         }
397
398         g_node_unlink(decinfo->node);
399         procmime_mimeinfo_free_all(parseinfo);
400
401         decinfo->tmp = TRUE;
402
403         if (sigstat != NULL && sigstat->signatures != NULL) {
404                 if (decinfo->privacy != NULL) {
405                         data = (PrivacyDataPGP *) decinfo->privacy;
406                 } else {
407                         data = pgpmime_new_privacydata();
408                         decinfo->privacy = (PrivacyData *) data;        
409                 }
410                 data->done_sigtest = TRUE;
411                 data->is_signed = TRUE;
412                 data->sigstatus = sigstat;
413                 if (data->ctx)
414                         gpgme_release(data->ctx);
415                 data->ctx = ctx;
416         } else
417                 gpgme_release(ctx);
418
419         return decinfo;
420 }
421
422 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
423 {
424         MimeInfo *msgcontent, *sigmultipart, *newinfo;
425         gchar *textstr, *micalg;
426         FILE *fp;
427         gchar *boundary = NULL;
428         gchar *sigcontent;
429         gpgme_ctx_t ctx;
430         gpgme_data_t gpgtext, gpgsig;
431         gpgme_error_t err;
432         size_t len;
433         struct passphrase_cb_info_s info;
434         gpgme_sign_result_t result = NULL;
435         gchar *test_msg;
436         
437         fp = my_tmpfile();
438         if (fp == NULL) {
439                 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
440                 return FALSE;
441         }
442         procmime_write_mimeinfo(mimeinfo, fp);
443         rewind(fp);
444
445         /* read temporary file into memory */
446         test_msg = file_read_stream_to_str(fp);
447         fclose(fp);
448         
449         memset (&info, 0, sizeof info);
450
451         /* remove content node from message */
452         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
453         g_node_unlink(msgcontent->node);
454
455         /* create temporary multipart for content */
456         sigmultipart = procmime_mimeinfo_new();
457         sigmultipart->type = MIMETYPE_MULTIPART;
458         sigmultipart->subtype = g_strdup("signed");
459         
460         do {
461                 g_free(boundary);
462                 boundary = generate_mime_boundary("Sig");
463         } while (strstr(test_msg, boundary) != NULL);
464         
465         g_free(test_msg);
466
467         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
468                             g_strdup(boundary));
469         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
470                             g_strdup("application/pgp-signature"));
471         g_node_append(sigmultipart->node, msgcontent->node);
472         g_node_append(mimeinfo->node, sigmultipart->node);
473
474         /* write message content to temporary file */
475         fp = my_tmpfile();
476         if (fp == NULL) {
477                 perror("my_tmpfile");
478                 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
479                 return FALSE;
480         }
481         procmime_write_mimeinfo(sigmultipart, fp);
482         rewind(fp);
483
484         /* read temporary file into memory */
485         textstr = get_canonical_content(fp, boundary);
486
487         fclose(fp);
488
489         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
490         gpgme_data_new(&gpgsig);
491         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
492                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
493                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
494                 return FALSE;
495         }
496         gpgme_set_textmode(ctx, 1);
497         gpgme_set_armor(ctx, 1);
498         gpgme_signers_clear (ctx);
499
500         if (!sgpgme_setup_signers(ctx, account, from_addr)) {
501                 gpgme_release(ctx);
502                 return FALSE;
503         }
504
505         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
506         if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
507                 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
508         } else {
509                 info.c = ctx;
510                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
511         }
512
513         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
514         if (err != GPG_ERR_NO_ERROR) {
515                 if (err == GPG_ERR_CANCELED) {
516                         /* ignore cancelled signing */
517                         privacy_reset_error();
518                         debug_print("gpgme_op_sign cancelled\n");
519                 } else {
520                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
521                         debug_print("gpgme_op_sign error : %x\n", err);
522                 }
523                 gpgme_release(ctx);
524                 return FALSE;
525         }
526         result = gpgme_op_sign_result(ctx);
527         if (result && result->signatures) {
528                 gpgme_new_signature_t sig = result->signatures;
529                 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
530                         micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
531                                 result->signatures->hash_algo));
532                 } else {
533                         micalg = g_strdup(gpgme_hash_algo_name(
534                                 result->signatures->hash_algo));
535                 }
536                 while (sig) {
537                         debug_print("valid signature: %s\n", sig->fpr);
538                         sig = sig->next;
539                 }
540         } else if (result && result->invalid_signers) {
541                 gpgme_invalid_key_t invalid = result->invalid_signers;
542                 while (invalid) {
543                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
544                                 gpgme_strerror(invalid->reason));
545                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
546                                 gpgme_strerror(invalid->reason));
547                         invalid = invalid->next;
548                 }
549                 gpgme_release(ctx);
550                 return FALSE;
551         } else {
552                 /* can't get result (maybe no signing key?) */
553                 debug_print("gpgme_op_sign_result error\n");
554                 privacy_set_error(_("Data signing failed, no results."));
555                 gpgme_release(ctx);
556                 return FALSE;
557         }
558
559         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
560         gpgme_data_release(gpgtext);
561         g_free(textstr);
562
563         if (sigcontent == NULL || len <= 0) {
564                 g_warning("sgpgme_data_release_and_get_mem failed");
565                 privacy_set_error(_("Data signing failed, no contents."));
566                 return FALSE;
567         }
568
569         /* add signature */
570         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
571                             micalg);
572
573         newinfo = procmime_mimeinfo_new();
574         newinfo->type = MIMETYPE_APPLICATION;
575         newinfo->subtype = g_strdup("pgp-signature");
576         g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
577                              g_strdup("signature.asc"));
578         newinfo->content = MIMECONTENT_MEM;
579         newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
580         g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
581                             g_strdup("signature.asc"));
582         newinfo->data.mem = g_malloc(len + 1);
583         g_memmove(newinfo->data.mem, sigcontent, len);
584         newinfo->data.mem[len] = '\0';
585         g_node_append(sigmultipart->node, newinfo->node);
586
587         g_free(sigcontent);
588         gpgme_release(ctx);
589
590         return TRUE;
591 }
592 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
593 {
594         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
595 }
596
597 static const gchar *pgpmime_get_encrypt_warning(void)
598 {
599         if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
600                 return NULL;
601         else
602                 return _("Please note that email headers, like Subject, "
603                          "are not encrypted by the PGP/Mime system.");
604 }
605
606 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
607 {
608         if (inhibit)
609                 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
610         else
611                 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
612 }
613
614 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
615 {
616         MimeInfo *msgcontent, *encmultipart, *newinfo;
617         FILE *fp;
618         gchar *boundary, *enccontent;
619         size_t len;
620         gchar *textstr;
621         gpgme_data_t gpgtext = NULL, gpgenc = NULL;
622         gpgme_ctx_t ctx = NULL;
623         gpgme_key_t *kset = NULL;
624         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
625         gint i = 0;
626         gpgme_error_t err;
627         
628         while (fprs[i] && strlen(fprs[i])) {
629                 i++;
630         }
631         
632         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
633         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
634         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
635                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
636                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
637                 return FALSE;
638         }
639         i = 0;
640         while (fprs[i] && strlen(fprs[i])) {
641                 gpgme_key_t key;
642                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
643                 if (err) {
644                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
645                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
646                         return FALSE;
647                 }
648                 debug_print("found %s at %d\n", fprs[i], i);
649                 kset[i] = key;
650                 i++;
651         }
652         
653         debug_print("Encrypting message content\n");
654
655         /* remove content node from message */
656         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
657         g_node_unlink(msgcontent->node);
658
659         /* create temporary multipart for content */
660         encmultipart = procmime_mimeinfo_new();
661         encmultipart->type = MIMETYPE_MULTIPART;
662         encmultipart->subtype = g_strdup("encrypted");
663         boundary = generate_mime_boundary("Encrypt");
664         g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
665                             g_strdup(boundary));
666         g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
667                             g_strdup("application/pgp-encrypted"));
668         g_node_append(encmultipart->node, msgcontent->node);
669
670         /* write message content to temporary file */
671         fp = my_tmpfile();
672         if (fp == NULL) {
673                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
674                 return FALSE;
675         }
676         procmime_write_mimeinfo(encmultipart, fp);
677         rewind(fp);
678
679         /* read temporary file into memory */
680         textstr = get_canonical_content(fp, boundary);
681
682         fclose(fp);
683
684         /* encrypt data */
685         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
686         gpgme_data_new(&gpgenc);
687         gpgme_set_armor(ctx, 1);
688         gpgme_data_rewind(gpgtext);
689         
690         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
691
692         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
693         gpgme_data_release(gpgtext);
694         g_free(textstr);
695
696         if (enccontent == NULL || len <= 0) {
697                 g_warning("sgpgme_data_release_and_get_mem failed");
698                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
699                 gpgme_release(ctx);
700                 return FALSE;
701         }
702
703         /* create encrypted multipart */
704         g_node_unlink(msgcontent->node);
705         procmime_mimeinfo_free_all(msgcontent);
706         g_node_append(mimeinfo->node, encmultipart->node);
707
708         newinfo = procmime_mimeinfo_new();
709         newinfo->type = MIMETYPE_APPLICATION;
710         newinfo->subtype = g_strdup("pgp-encrypted");
711         newinfo->content = MIMECONTENT_MEM;
712         newinfo->data.mem = g_strdup("Version: 1\n");
713         g_node_append(encmultipart->node, newinfo->node);
714
715         newinfo = procmime_mimeinfo_new();
716         newinfo->type = MIMETYPE_APPLICATION;
717         newinfo->subtype = g_strdup("octet-stream");
718         newinfo->content = MIMECONTENT_MEM;
719         newinfo->data.mem = g_malloc(len + 1);
720         g_memmove(newinfo->data.mem, enccontent, len);
721         newinfo->data.mem[len] = '\0';
722         g_node_append(encmultipart->node, newinfo->node);
723
724         g_free(enccontent);
725         gpgme_release(ctx);
726
727         return TRUE;
728 }
729
730 static PrivacySystem pgpmime_system = {
731         "pgpmime",                      /* id */
732         "PGP MIME",                     /* name */
733
734         pgpmime_free_privacydata,       /* free_privacydata */
735
736         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
737         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
738         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
739         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
740         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
741
742         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
743         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
744
745         TRUE,
746         pgpmime_sign,
747
748         TRUE,
749         pgpmime_get_encrypt_data,
750         pgpmime_encrypt,
751         pgpmime_get_encrypt_warning,
752         pgpmime_inhibit_encrypt_warning,
753 };
754
755 void pgpmime_init()
756 {
757         privacy_register_system(&pgpmime_system);
758 }
759
760 void pgpmime_done()
761 {
762         privacy_unregister_system(&pgpmime_system);
763 }
764
765 struct PluginFeature *plugin_provides(void)
766 {
767         static struct PluginFeature features[] = 
768                 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
769                   {PLUGIN_NOTHING, NULL}};
770         return features;
771 }
772 #endif /* USE_GPGME */