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