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