2007-02-23 [colin] 2.7.2cvs63
[claws.git] / src / plugins / pgpmime / pgpmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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, (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         fprintf(dstfp, "MIME-Version: 1.0\n");
352
353         chars = sgpgme_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, (size_t)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         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
481         if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
482                 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
483         } else {
484                 info.c = ctx;
485                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
486         }
487
488         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
489         if (err != GPG_ERR_NO_ERROR) {
490                 if (err == GPG_ERR_CANCELED) {
491                         /* ignore cancelled signing */
492                         privacy_reset_error();
493                         debug_print("gpgme_op_sign cancelled\n");
494                 } else {
495                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
496                         debug_print("gpgme_op_sign error : %x\n", err);
497                 }
498                 gpgme_release(ctx);
499                 return FALSE;
500         }
501         result = gpgme_op_sign_result(ctx);
502         if (result && result->signatures) {
503                 gpgme_new_signature_t sig = result->signatures;
504                 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
505                         micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
506                                 result->signatures->hash_algo));
507                 } else {
508                         micalg = g_strdup(gpgme_hash_algo_name(
509                                 result->signatures->hash_algo));
510                 }
511                 while (sig) {
512                         debug_print("valid signature: %s\n", sig->fpr);
513                         sig = sig->next;
514                 }
515         } else if (result && result->invalid_signers) {
516                 gpgme_invalid_key_t invalid = result->invalid_signers;
517                 while (invalid) {
518                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
519                                 gpgme_strerror(invalid->reason));
520                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
521                                 gpgme_strerror(invalid->reason));
522                         invalid = invalid->next;
523                 }
524                 gpgme_release(ctx);
525                 return FALSE;
526         } else {
527                 /* can't get result (maybe no signing key?) */
528                 debug_print("gpgme_op_sign_result error\n");
529                 privacy_set_error(_("Data signing failed, no results."));
530                 gpgme_release(ctx);
531                 return FALSE;
532         }
533
534         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
535         gpgme_data_release(gpgtext);
536         g_free(textstr);
537
538         if (sigcontent == NULL || len <= 0) {
539                 g_warning("sgpgme_data_release_and_get_mem failed");
540                 privacy_set_error(_("Data signing failed, no contents."));
541                 return FALSE;
542         }
543
544         /* add signature */
545         g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
546                             micalg);
547
548         newinfo = procmime_mimeinfo_new();
549         newinfo->type = MIMETYPE_APPLICATION;
550         newinfo->subtype = g_strdup("pgp-signature");
551         g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
552                              g_strdup("signature.asc"));
553         newinfo->content = MIMECONTENT_MEM;
554         newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
555         g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
556                             g_strdup("signature.asc"));
557         newinfo->data.mem = g_malloc(len + 1);
558         g_memmove(newinfo->data.mem, sigcontent, len);
559         newinfo->data.mem[len] = '\0';
560         g_node_append(sigmultipart->node, newinfo->node);
561
562         g_free(sigcontent);
563         gpgme_release(ctx);
564
565         return TRUE;
566 }
567 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
568 {
569         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
570 }
571
572 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
573 {
574         MimeInfo *msgcontent, *encmultipart, *newinfo;
575         FILE *fp;
576         gchar *boundary, *enccontent;
577         size_t len;
578         gchar *textstr;
579         gpgme_data_t gpgtext = NULL, gpgenc = NULL;
580         gpgme_ctx_t ctx = NULL;
581         gpgme_key_t *kset = NULL;
582         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
583         gint i = 0;
584         gpgme_error_t err;
585         
586         while (fprs[i] && strlen(fprs[i])) {
587                 i++;
588         }
589         
590         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
591         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
592         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
593                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
594                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
595                 return FALSE;
596         }
597         i = 0;
598         while (fprs[i] && strlen(fprs[i])) {
599                 gpgme_key_t key;
600                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
601                 if (err) {
602                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
603                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
604                         return FALSE;
605                 }
606                 debug_print("found %s at %d\n", fprs[i], i);
607                 kset[i] = key;
608                 i++;
609         }
610         
611         debug_print("Encrypting message content\n");
612
613         /* remove content node from message */
614         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
615         g_node_unlink(msgcontent->node);
616
617         /* create temporary multipart for content */
618         encmultipart = procmime_mimeinfo_new();
619         encmultipart->type = MIMETYPE_MULTIPART;
620         encmultipart->subtype = g_strdup("encrypted");
621         boundary = generate_mime_boundary("Encrypt");
622         g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
623                             g_strdup(boundary));
624         g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
625                             g_strdup("application/pgp-encrypted"));
626         g_node_append(encmultipart->node, msgcontent->node);
627
628         /* write message content to temporary file */
629         fp = my_tmpfile();
630         if (fp == NULL) {
631                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
632                 return FALSE;
633         }
634         procmime_write_mimeinfo(encmultipart, fp);
635         rewind(fp);
636
637         /* read temporary file into memory */
638         textstr = get_canonical_content(fp, boundary);
639
640         fclose(fp);
641
642         /* encrypt data */
643         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
644         gpgme_data_new(&gpgenc);
645         gpgme_set_armor(ctx, 1);
646         gpgme_data_rewind(gpgtext);
647         
648         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
649
650         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
651         gpgme_data_release(gpgtext);
652         g_free(textstr);
653
654         if (enccontent == NULL || len <= 0) {
655                 g_warning("sgpgme_data_release_and_get_mem failed");
656                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
657                 gpgme_release(ctx);
658                 return FALSE;
659         }
660
661         /* create encrypted multipart */
662         g_node_unlink(msgcontent->node);
663         procmime_mimeinfo_free_all(msgcontent);
664         g_node_append(mimeinfo->node, encmultipart->node);
665
666         newinfo = procmime_mimeinfo_new();
667         newinfo->type = MIMETYPE_APPLICATION;
668         newinfo->subtype = g_strdup("pgp-encrypted");
669         newinfo->content = MIMECONTENT_MEM;
670         newinfo->data.mem = g_strdup("Version: 1\n");
671         g_node_append(encmultipart->node, newinfo->node);
672
673         newinfo = procmime_mimeinfo_new();
674         newinfo->type = MIMETYPE_APPLICATION;
675         newinfo->subtype = g_strdup("octet-stream");
676         newinfo->content = MIMECONTENT_MEM;
677         newinfo->data.mem = g_malloc(len + 1);
678         g_memmove(newinfo->data.mem, enccontent, len);
679         newinfo->data.mem[len] = '\0';
680         g_node_append(encmultipart->node, newinfo->node);
681
682         g_free(enccontent);
683         gpgme_release(ctx);
684
685         return TRUE;
686 }
687
688 static PrivacySystem pgpmime_system = {
689         "pgpmime",                      /* id */
690         "PGP MIME",                     /* name */
691
692         pgpmime_free_privacydata,       /* free_privacydata */
693
694         pgpmime_is_signed,              /* is_signed(MimeInfo *) */
695         pgpmime_check_signature,        /* check_signature(MimeInfo *) */
696         pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
697         pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
698         pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
699
700         pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
701         pgpmime_decrypt,                /* decrypt(MimeInfo *) */
702
703         TRUE,
704         pgpmime_sign,
705
706         TRUE,
707         pgpmime_get_encrypt_data,
708         pgpmime_encrypt,
709 };
710
711 void pgpmime_init()
712 {
713         privacy_register_system(&pgpmime_system);
714 }
715
716 void pgpmime_done()
717 {
718         privacy_unregister_system(&pgpmime_system);
719 }
720
721 struct PluginFeature *plugin_provides(void)
722 {
723         static struct PluginFeature features[] = 
724                 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
725                   {PLUGIN_NOTHING, NULL}};
726         return features;
727 }
728 #endif /* USE_GPGME */