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