2011-07-30 [mir] 3.7.9cvs38
[claws.git] / src / plugins / pgpmime / pgpmime.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #ifdef USE_GPGME
25
26 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gpgme.h>
30 #include <ctype.h>
31 #include <errno.h>
32
33 #include "utils.h"
34 #include "privacy.h"
35 #include "procmime.h"
36 #include "plugin.h"
37
38 #include "pgpmime.h"
39 #include <plugins/pgpcore/sgpgme.h>
40 #include <plugins/pgpcore/prefs_gpg.h>
41 #include <plugins/pgpcore/passphrase.h>
42
43 #include "prefs_common.h"
44
45 typedef struct _PrivacyDataPGP PrivacyDataPGP;
46
47 struct _PrivacyDataPGP
48 {
49         PrivacyData     data;
50         
51         gboolean        done_sigtest;
52         gboolean        is_signed;
53         gpgme_verify_result_t   sigstatus;
54         gpgme_ctx_t     ctx;
55 };
56
57 static PrivacySystem pgpmime_system;
58
59 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
60
61 static PrivacyDataPGP *pgpmime_new_privacydata()
62 {
63         PrivacyDataPGP *data;
64         gpgme_error_t err;
65
66         data = g_new0(PrivacyDataPGP, 1);
67         data->data.system = &pgpmime_system;
68         data->done_sigtest = FALSE;
69         data->is_signed = FALSE;
70         data->sigstatus = NULL;
71         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
72                 g_warning(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
73                 return NULL;
74         }
75         
76         return data;
77 }
78
79 static void pgpmime_free_privacydata(PrivacyData *_data)
80 {
81         PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
82         gpgme_release(data->ctx);
83         g_free(data);
84 }
85
86 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
87 {
88         MimeInfo *parent;
89         MimeInfo *signature;
90         const gchar *protocol;
91         PrivacyDataPGP *data = NULL;
92         
93         cm_return_val_if_fail(mimeinfo != NULL, FALSE);
94         if (mimeinfo->privacy != NULL) {
95                 data = (PrivacyDataPGP *) mimeinfo->privacy;
96                 if (data->done_sigtest)
97                         return data->is_signed;
98         }
99         
100         /* check parent */
101         parent = procmime_mimeinfo_parent(mimeinfo);
102         if (parent == NULL)
103                 return FALSE;
104         if ((parent->type != MIMETYPE_MULTIPART) ||
105             g_ascii_strcasecmp(parent->subtype, "signed"))
106                 return FALSE;
107         protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
108         if ((protocol == NULL) || 
109             (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
110                 return FALSE;
111
112         /* check if mimeinfo is the first child */
113         if (parent->node->children->data != mimeinfo)
114                 return FALSE;
115
116         /* check signature */
117         signature = parent->node->children->next != NULL ? 
118             (MimeInfo *) parent->node->children->next->data : NULL;
119         if (signature == NULL)
120                 return FALSE;
121         if ((signature->type != MIMETYPE_APPLICATION) ||
122             (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
123                 return FALSE;
124
125         if (data == NULL) {
126                 data = pgpmime_new_privacydata();
127                 mimeinfo->privacy = (PrivacyData *) data;
128         }
129         
130         data->done_sigtest = TRUE;
131         data->is_signed = TRUE;
132
133         return TRUE;
134 }
135
136 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
137 {
138         gchar *ret;
139         GString *textbuffer;
140         guint boundary_len;
141         gchar buf[BUFFSIZE];
142
143         boundary_len = strlen(boundary);
144         while (fgets(buf, sizeof(buf), fp) != NULL)
145                 if (IS_BOUNDARY(buf, boundary, boundary_len))
146                         break;
147
148         textbuffer = g_string_new("");
149         while (fgets(buf, sizeof(buf), fp) != NULL) {
150                 gchar *buf2;
151
152                 if (IS_BOUNDARY(buf, boundary, boundary_len))
153                         break;
154                 
155                 buf2 = canonicalize_str(buf);
156                 g_string_append(textbuffer, buf2);
157                 g_free(buf2);
158         }
159         g_string_truncate(textbuffer, textbuffer->len - 2);
160                 
161         ret = textbuffer->str;
162         g_string_free(textbuffer, FALSE);
163
164         return ret;
165 }
166
167 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
168 {
169         PrivacyDataPGP *data;
170         MimeInfo *parent, *signature;
171         FILE *fp;
172         gchar *boundary;
173         gchar *textstr;
174         gpgme_data_t sigdata = NULL, textdata = NULL;
175         gpgme_error_t err;
176         cm_return_val_if_fail(mimeinfo != NULL, -1);
177         cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
178         data = (PrivacyDataPGP *) mimeinfo->privacy;
179         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
180                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
181                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
182                 return 0;
183         }
184
185         
186         debug_print("Checking PGP/MIME signature\n");
187
188         err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
189
190         if (err) {
191                 debug_print ("gpgme_set_protocol failed: %s\n",
192                    gpgme_strerror (err));
193         }
194         parent = procmime_mimeinfo_parent(mimeinfo);
195
196         fp = g_fopen(parent->data.filename, "rb");
197         cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
198         
199         boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
200         if (!boundary) {
201                 privacy_set_error(_("Signature boundary not found."));
202                 fclose(fp);
203                 return 0;
204         }
205         textstr = get_canonical_content(fp, boundary);
206
207         err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0);
208         if (err) {
209                 debug_print ("gpgme_data_new_from_mem failed: %s\n",
210                    gpgme_strerror (err));
211         }
212         signature = (MimeInfo *) mimeinfo->node->next->data;
213         sigdata = sgpgme_data_from_mimeinfo(signature);
214
215         err = 0;
216         if (signature->encoding_type == ENC_BASE64) {
217                 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
218         }
219         
220         if (err) {
221                 debug_print ("gpgme_data_set_encoding failed: %s\n",
222                         gpgme_strerror (err));
223         }
224
225         data->sigstatus =
226                 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
227
228         gpgme_data_release(sigdata);
229         gpgme_data_release(textdata);
230         g_free(textstr);
231         fclose(fp);
232         
233         return 0;
234 }
235
236 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
237 {
238         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
239         
240         cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
241
242         if (data->sigstatus == NULL && 
243             prefs_gpg_get_config()->auto_check_signatures)
244                 pgpmime_check_signature(mimeinfo);
245         
246         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
247 }
248
249 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
250 {
251         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
252         
253         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
254
255         if (data->sigstatus == NULL && 
256             prefs_gpg_get_config()->auto_check_signatures)
257                 pgpmime_check_signature(mimeinfo);
258         
259         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
260 }
261
262 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
263 {
264         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
265         
266         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
267
268         if (data->sigstatus == NULL && 
269             prefs_gpg_get_config()->auto_check_signatures)
270                 pgpmime_check_signature(mimeinfo);
271         
272         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
273 }
274
275 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
276 {
277         MimeInfo *tmpinfo;
278         const gchar *tmpstr;
279         
280         if (mimeinfo->type != MIMETYPE_MULTIPART)
281                 return FALSE;
282         if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
283                 return FALSE;
284         tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
285         if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
286                 return FALSE;
287         if (g_node_n_children(mimeinfo->node) != 2)
288                 return FALSE;
289         
290         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
291         if (tmpinfo->type != MIMETYPE_APPLICATION)
292                 return FALSE;
293         if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
294                 return FALSE;
295         
296         tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
297         if (tmpinfo->type != MIMETYPE_APPLICATION)
298                 return FALSE;
299         if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
300                 return FALSE;
301         
302         return TRUE;
303 }
304
305 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
306 {
307         MimeInfo *encinfo, *decinfo, *parseinfo;
308         gpgme_data_t cipher = NULL, plain = NULL;
309         static gint id = 0;
310         FILE *dstfp;
311         gchar *fname;
312         gpgme_verify_result_t sigstat = NULL;
313         PrivacyDataPGP *data = NULL;
314         gpgme_ctx_t ctx;
315         gchar *chars;
316         size_t len;
317         gpgme_error_t err;
318
319         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
320                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
321                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
322                 return NULL;
323         }
324         
325         cm_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
326         
327         encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
328
329         cipher = sgpgme_data_from_mimeinfo(encinfo);
330         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
331
332         gpgme_data_release(cipher);
333         if (plain == NULL) {
334                 debug_print("plain is null!\n");
335                 gpgme_release(ctx);
336                 return NULL;
337         }
338
339         fname = g_strdup_printf("%s%cplaintext.%08x",
340                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
341
342         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
343                 FILE_OP_ERROR(fname, "fopen");
344                 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
345                 g_free(fname);
346                 gpgme_data_release(plain);
347                 gpgme_release(ctx);
348                 debug_print("can't open!\n");
349                 return NULL;
350         }
351
352         if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
353                 FILE_OP_ERROR(fname, "fprintf");
354                 fclose(dstfp);
355                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
356                 g_free(fname);
357                 gpgme_data_release(plain);
358                 gpgme_release(ctx);
359                 debug_print("can't open!\n");
360                 return NULL;
361         }
362
363         chars = sgpgme_data_release_and_get_mem(plain, &len);
364         if (len > 0) {
365                 if (fwrite(chars, 1, len, dstfp) < len) {
366                         FILE_OP_ERROR(fname, "fwrite");
367                         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 = NULL;
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                 g_free(micalg);
570                 return FALSE;
571         }
572
573         /* add signature */
574         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
575                             micalg);
576
577         newinfo = procmime_mimeinfo_new();
578         newinfo->type = MIMETYPE_APPLICATION;
579         newinfo->subtype = g_strdup("pgp-signature");
580         g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
581                              g_strdup("signature.asc"));
582         newinfo->content = MIMECONTENT_MEM;
583         newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
584         g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
585                             g_strdup("signature.asc"));
586         newinfo->data.mem = g_malloc(len + 1);
587         g_memmove(newinfo->data.mem, sigcontent, len);
588         newinfo->data.mem[len] = '\0';
589         g_node_append(sigmultipart->node, newinfo->node);
590
591         g_free(sigcontent);
592         gpgme_release(ctx);
593
594         return TRUE;
595 }
596 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
597 {
598         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
599 }
600
601 static const gchar *pgpmime_get_encrypt_warning(void)
602 {
603         if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
604                 return NULL;
605         else
606                 return _("Please note that email headers, like Subject, "
607                          "are not encrypted by the PGP/Mime system.");
608 }
609
610 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
611 {
612         if (inhibit)
613                 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
614         else
615                 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
616 }
617
618 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
619 {
620         MimeInfo *msgcontent, *encmultipart, *newinfo;
621         FILE *fp;
622         gchar *boundary, *enccontent;
623         size_t len;
624         gchar *textstr;
625         gpgme_data_t gpgtext = NULL, gpgenc = NULL;
626         gpgme_ctx_t ctx = NULL;
627         gpgme_key_t *kset = NULL;
628         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
629         gint i = 0;
630         gpgme_error_t err;
631         
632         while (fprs[i] && strlen(fprs[i])) {
633                 i++;
634         }
635         
636         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
637         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
638         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
639                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
640                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
641                 g_free(kset);
642                 return FALSE;
643         }
644         i = 0;
645         while (fprs[i] && strlen(fprs[i])) {
646                 gpgme_key_t key;
647                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
648                 if (err) {
649                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
650                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
651                         g_free(kset);
652                         return FALSE;
653                 }
654                 debug_print("found %s at %d\n", fprs[i], i);
655                 kset[i] = key;
656                 i++;
657         }
658         
659         debug_print("Encrypting message content\n");
660
661         /* remove content node from message */
662         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
663         g_node_unlink(msgcontent->node);
664
665         /* create temporary multipart for content */
666         encmultipart = procmime_mimeinfo_new();
667         encmultipart->type = MIMETYPE_MULTIPART;
668         encmultipart->subtype = g_strdup("encrypted");
669         boundary = generate_mime_boundary("Encrypt");
670         g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
671                             g_strdup(boundary));
672         g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
673                             g_strdup("application/pgp-encrypted"));
674         g_node_append(encmultipart->node, msgcontent->node);
675
676         /* write message content to temporary file */
677         fp = my_tmpfile();
678         if (fp == NULL) {
679                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
680                 g_free(kset);
681                 return FALSE;
682         }
683         procmime_write_mimeinfo(encmultipart, fp);
684         rewind(fp);
685
686         /* read temporary file into memory */
687         textstr = get_canonical_content(fp, boundary);
688
689         fclose(fp);
690
691         /* encrypt data */
692         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
693         gpgme_data_new(&gpgenc);
694         gpgme_set_armor(ctx, 1);
695         cm_gpgme_data_rewind(gpgtext);
696         
697         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
698
699         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
700         gpgme_data_release(gpgtext);
701         g_free(textstr);
702         g_free(kset);
703
704         if (enccontent == NULL || len <= 0) {
705                 g_warning("sgpgme_data_release_and_get_mem failed");
706                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
707                 gpgme_release(ctx);
708                 return FALSE;
709         }
710
711         /* create encrypted multipart */
712         g_node_unlink(msgcontent->node);
713         procmime_mimeinfo_free_all(msgcontent);
714         g_node_append(mimeinfo->node, encmultipart->node);
715
716         newinfo = procmime_mimeinfo_new();
717         newinfo->type = MIMETYPE_APPLICATION;
718         newinfo->subtype = g_strdup("pgp-encrypted");
719         newinfo->content = MIMECONTENT_MEM;
720         newinfo->data.mem = g_strdup("Version: 1\n");
721         g_node_append(encmultipart->node, newinfo->node);
722
723         newinfo = procmime_mimeinfo_new();
724         newinfo->type = MIMETYPE_APPLICATION;
725         newinfo->subtype = g_strdup("octet-stream");
726         newinfo->content = MIMECONTENT_MEM;
727         newinfo->data.mem = g_malloc(len + 1);
728         g_memmove(newinfo->data.mem, enccontent, len);
729         newinfo->data.mem[len] = '\0';
730         g_node_append(encmultipart->node, newinfo->node);
731
732         g_free(enccontent);
733         gpgme_release(ctx);
734
735         return TRUE;
736 }
737
738 static PrivacySystem pgpmime_system = {
739         "pgpmime",                      /* id */
740         "PGP MIME",                     /* name */
741
742         pgpmime_free_privacydata,       /* free_privacydata */
743
744         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
745         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
746         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
747         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
748         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
749
750         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
751         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
752
753         TRUE,
754         pgpmime_sign,
755
756         TRUE,
757         pgpmime_get_encrypt_data,
758         pgpmime_encrypt,
759         pgpmime_get_encrypt_warning,
760         pgpmime_inhibit_encrypt_warning,
761 };
762
763 void pgpmime_init()
764 {
765         privacy_register_system(&pgpmime_system);
766 }
767
768 void pgpmime_done()
769 {
770         privacy_unregister_system(&pgpmime_system);
771 }
772
773 struct PluginFeature *plugin_provides(void)
774 {
775         static struct PluginFeature features[] = 
776                 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
777                   {PLUGIN_NOTHING, NULL}};
778         return features;
779 }
780 #endif /* USE_GPGME */