-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt-gpgme.c
4562 lines (3966 loc) · 115 KB
/
crypt-gpgme.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* crypt-gpgme.c - GPGME based crypto operations
* Copyright (C) 1996-7,2007 Michael R. Elkins <[email protected]>
* Copyright (C) 1998,1999,2000 Thomas Roessler <[email protected]>
* Copyright (C) 2001 Thomas Roessler <[email protected]>
* Oliver Ehli <[email protected]>
* Copyright (C) 2002, 2003, 2004 g10 Code GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef CRYPT_BACKEND_GPGME
#include "mutt.h"
#include "mutt_crypt.h"
#include "mutt_menu.h"
#include "mutt_curses.h"
#include "mime.h"
#include "copy.h"
#include "pager.h"
#include "sort.h"
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include <gpgme.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_LANGINFO_D_T_FMT
#include <langinfo.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
/*
* Helper macros.
*/
#define digitp(p) (*(p) >= '0' && *(p) <= '9')
#define hexdigitp(a) (digitp (a) \
|| (*(a) >= 'A' && *(a) <= 'F') \
|| (*(a) >= 'a' && *(a) <= 'f'))
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
#define PKA_NOTATION_NAME "[email protected]"
#define is_pka_notation(notation) (! strcmp ((notation)->name, \
PKA_NOTATION_NAME))
/* Values used for comparing addresses. */
#define CRYPT_KV_VALID 1
#define CRYPT_KV_ADDR 2
#define CRYPT_KV_STRING 4
#define CRYPT_KV_STRONGID 8
#define CRYPT_KV_MATCH (CRYPT_KV_ADDR|CRYPT_KV_STRING)
/* static local variables */
static int GpgmeLocaleSet = 0;
/*
* Type definitions.
*/
struct crypt_cache
{
char *what;
char *dflt;
struct crypt_cache *next;
};
struct dn_array_s
{
char *key;
char *value;
};
/* We work based on user IDs, getting from a user ID to the key is
check and does not need any memory (gpgme uses reference counting). */
typedef struct crypt_keyinfo
{
struct crypt_keyinfo *next;
gpgme_key_t kobj;
int idx; /* and the user ID at this index */
const char *uid; /* and for convenience point to this user ID */
unsigned int flags; /* global and per uid flags (for convenience)*/
} crypt_key_t;
typedef struct crypt_entry
{
size_t num;
crypt_key_t *key;
} crypt_entry_t;
static struct crypt_cache *id_defaults = NULL;
static gpgme_key_t signature_key = NULL;
static char *current_sender = NULL;
/*
* General helper functions.
*/
/* return true when S pints to a didgit or letter. */
static int
digit_or_letter (const unsigned char *s)
{
return ( (*s >= '0' && *s < '9')
|| (*s >= 'A' && *s <= 'Z')
|| (*s >= 'a' && *s <= 'z'));
}
/* Print the utf-8 encoded string BUF of length LEN bytes to stream
FP. Convert the character set. */
static void
print_utf8 (FILE *fp, const char *buf, size_t len)
{
char *tstr;
tstr = safe_malloc (len+1);
memcpy (tstr, buf, len);
tstr[len] = 0;
/* fromcode "utf-8" is sure, so we don't want
* charset-hook corrections: flags must be 0.
*/
mutt_convert_string (&tstr, "utf-8", Charset, 0);
fputs (tstr, fp);
FREE (&tstr);
}
/*
* Key management.
*/
/* Return the keyID for the key K. Note that this string is valid as
long as K is valid */
static const char *crypt_keyid (crypt_key_t *k)
{
const char *s = "????????";
if (k->kobj && k->kobj->subkeys)
{
s = k->kobj->subkeys->keyid;
if ((! option (OPTPGPLONGIDS)) && (strlen (s) == 16))
/* Return only the short keyID. */
s += 8;
}
return s;
}
/* Return the hexstring fingerprint from the key K. */
static const char *crypt_fpr (crypt_key_t *k)
{
const char *s = "";
if (k->kobj && k->kobj->subkeys)
s = k->kobj->subkeys->fpr;
return s;
}
/* Parse FLAGS and return a statically allocated(!) string with them. */
static char *crypt_key_abilities (int flags)
{
static char buff[3];
if (!(flags & KEYFLAG_CANENCRYPT))
buff[0] = '-';
else if (flags & KEYFLAG_PREFER_SIGNING)
buff[0] = '.';
else
buff[0] = 'e';
if (!(flags & KEYFLAG_CANSIGN))
buff[1] = '-';
else if (flags & KEYFLAG_PREFER_ENCRYPTION)
buff[1] = '.';
else
buff[1] = 's';
buff[2] = '\0';
return buff;
}
/* Parse FLAGS and return a character describing the most important flag. */
static char crypt_flags (int flags)
{
if (flags & KEYFLAG_REVOKED)
return 'R';
else if (flags & KEYFLAG_EXPIRED)
return 'X';
else if (flags & KEYFLAG_DISABLED)
return 'd';
else if (flags & KEYFLAG_CRITICAL)
return 'c';
else
return ' ';
}
/* Return a copy of KEY. */
static crypt_key_t *crypt_copy_key (crypt_key_t *key)
{
crypt_key_t *k;
k = safe_calloc (1, sizeof *k);
k->kobj = key->kobj;
gpgme_key_ref (key->kobj);
k->idx = key->idx;
k->uid = key->uid;
k->flags = key->flags;
return k;
}
/* Release all the keys at the address of KEYLIST and set the address
to NULL. */
static void crypt_free_key (crypt_key_t **keylist)
{
while (*keylist)
{
crypt_key_t *k = (*keylist)->next;
FREE (&k);
*keylist = k;
}
}
/* Return trute when key K is valid. */
static int crypt_key_is_valid (crypt_key_t *k)
{
if (k->flags & KEYFLAG_CANTUSE)
return 0;
return 1;
}
/* Return true whe validity of KEY is sufficient. */
static int crypt_id_is_strong (crypt_key_t *key)
{
gpgme_validity_t val = GPGME_VALIDITY_UNKNOWN;
gpgme_user_id_t uid = NULL;
unsigned int is_strong = 0;
unsigned int i = 0;
if ((key->flags & KEYFLAG_ISX509))
return 1;
for (i = 0, uid = key->kobj->uids; (i < key->idx) && uid;
i++, uid = uid->next)
;
if (uid)
val = uid->validity;
switch (val)
{
case GPGME_VALIDITY_UNKNOWN:
case GPGME_VALIDITY_UNDEFINED:
case GPGME_VALIDITY_NEVER:
case GPGME_VALIDITY_MARGINAL:
is_strong = 0;
break;
case GPGME_VALIDITY_FULL:
case GPGME_VALIDITY_ULTIMATE:
is_strong = 1;
break;
}
return is_strong;
}
/* Return true when the KEY is valid, i.e. not marked as unusable. */
static int crypt_id_is_valid (crypt_key_t *key)
{
return ! (key->flags & KEYFLAG_CANTUSE);
}
/* Return a bit vector describing how well the addresses ADDR and
U_ADDR match and whether KEY is valid. */
static int crypt_id_matches_addr (ADDRESS *addr, ADDRESS *u_addr,
crypt_key_t *key)
{
int rv = 0;
if (crypt_id_is_valid (key))
rv |= CRYPT_KV_VALID;
if (crypt_id_is_strong (key))
rv |= CRYPT_KV_STRONGID;
if (addr->mailbox && u_addr->mailbox
&& mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
rv |= CRYPT_KV_ADDR;
if (addr->personal && u_addr->personal
&& mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
rv |= CRYPT_KV_STRING;
return rv;
}
/*
* GPGME convenient functions.
*/
/* Create a new gpgme context and return it. With FOR_SMIME set to
true, the protocol of the context is set to CMS. */
static gpgme_ctx_t create_gpgme_context (int for_smime)
{
gpgme_error_t err;
gpgme_ctx_t ctx;
if (!GpgmeLocaleSet)
{
gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
#ifdef ENABLE_NLS
gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
#endif
GpgmeLocaleSet = 1;
}
err = gpgme_new (&ctx);
if (err)
{
mutt_error (_("error creating gpgme context: %s\n"), gpgme_strerror (err));
sleep (2);
mutt_exit (1);
}
if (for_smime)
{
err = gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
if (err)
{
mutt_error (_("error enabling CMS protocol: %s\n"),
gpgme_strerror (err));
sleep (2);
mutt_exit (1);
}
}
return ctx;
}
/* Create a new gpgme data object. This is a wrapper to die on
error. */
static gpgme_data_t create_gpgme_data (void)
{
gpgme_error_t err;
gpgme_data_t data;
err = gpgme_data_new (&data);
if (err)
{
mutt_error (_("error creating gpgme data object: %s\n"),
gpgme_strerror (err));
sleep (2);
mutt_exit (1);
}
return data;
}
/* Create a new GPGME Data object from the mail body A. With CONVERT
passed as true, the lines are converted to CR,LF if required.
Return NULL on error or the gpgme_data_t object on success. */
static gpgme_data_t body_to_data_object (BODY *a, int convert)
{
char tempfile[_POSIX_PATH_MAX];
FILE *fptmp;
int err = 0;
gpgme_data_t data;
mutt_mktemp (tempfile, sizeof (tempfile));
fptmp = safe_fopen (tempfile, "w+");
if (!fptmp)
{
mutt_perror (tempfile);
return NULL;
}
mutt_write_mime_header (a, fptmp);
fputc ('\n', fptmp);
mutt_write_mime_body (a, fptmp);
if (convert)
{
int c, hadcr = 0;
unsigned char buf[1];
data = create_gpgme_data ();
rewind (fptmp);
while ((c = fgetc (fptmp)) != EOF)
{
if (c == '\r')
hadcr = 1;
else
{
if (c == '\n' && !hadcr)
{
buf[0] = '\r';
gpgme_data_write (data, buf, 1);
}
hadcr = 0;
}
/* FIXME: This is quite suboptimal */
buf[0] = c;
gpgme_data_write (data, buf, 1);
}
safe_fclose (&fptmp);
gpgme_data_seek (data, 0, SEEK_SET);
}
else
{
safe_fclose (&fptmp);
err = gpgme_data_new_from_file (&data, tempfile, 1);
}
unlink (tempfile);
if (err)
{
mutt_error (_("error allocating data object: %s\n"), gpgme_strerror (err));
return NULL;
}
return data;
}
/* Create a GPGME data object from the stream FP but limit the object
to LENGTH bytes starting at OFFSET bytes from the beginning of the
file. */
static gpgme_data_t file_to_data_object (FILE *fp, long offset, long length)
{
int err = 0;
gpgme_data_t data;
err = gpgme_data_new_from_filepart (&data, NULL, fp, offset, length);
if (err)
{
mutt_error (_("error allocating data object: %s\n"), gpgme_strerror (err));
return NULL;
}
return data;
}
/* Write a GPGME data object to the stream FP. */
static int data_object_to_stream (gpgme_data_t data, FILE *fp)
{
int err;
char buf[4096], *p;
ssize_t nread;
err = ((gpgme_data_seek (data, 0, SEEK_SET) == -1)
? gpgme_error_from_errno (errno) : 0);
if (err)
{
mutt_error (_("error rewinding data object: %s\n"), gpgme_strerror (err));
return -1;
}
while ((nread = gpgme_data_read (data, buf, sizeof (buf))))
{
/* fixme: we are not really converting CRLF to LF but just
skipping CR. Doing it correctly needs a more complex logic */
for (p=buf; nread; p++, nread--)
{
if (*p != '\r')
putc (*p, fp);
}
if (ferror (fp))
{
mutt_perror ("[tempfile]");
return -1;
}
}
if (nread == -1)
{
mutt_error (_("error reading data object: %s\n"), strerror (errno));
return -1;
}
return 0;
}
/* Copy a data object to a newly created temporay file and return that
filename. Caller must free. With RET_FP not NULL, don't close the
stream but return it there. */
static char *data_object_to_tempfile (gpgme_data_t data, FILE **ret_fp)
{
int err;
char tempfile[_POSIX_PATH_MAX];
FILE *fp;
size_t nread = 0;
mutt_mktemp (tempfile, sizeof (tempfile));
fp = safe_fopen (tempfile, "w+");
if (!fp)
{
mutt_perror (tempfile);
return NULL;
}
err = ((gpgme_data_seek (data, 0, SEEK_SET) == -1)
? gpgme_error_from_errno (errno) : 0);
if (!err)
{
char buf[4096];
while ((nread = gpgme_data_read (data, buf, sizeof (buf))))
{
if (fwrite (buf, nread, 1, fp) != 1)
{
mutt_perror (tempfile);
safe_fclose (&fp);
unlink (tempfile);
return NULL;
}
}
}
if (ret_fp)
rewind (fp);
else
safe_fclose (&fp);
if (nread == -1)
{
mutt_error (_("error reading data object: %s\n"), gpgme_strerror (err));
unlink (tempfile);
safe_fclose (&fp);
return NULL;
}
if (ret_fp)
*ret_fp = fp;
return safe_strdup (tempfile);
}
/* Create a GpgmeRecipientSet from the keys in the string KEYLIST.
The keys must be space delimited. */
static gpgme_key_t *create_recipient_set (const char *keylist,
gpgme_protocol_t protocol)
{
int err;
const char *s;
char buf[100];
int i;
gpgme_key_t *rset = NULL;
unsigned int rset_n = 0;
gpgme_key_t key = NULL;
gpgme_ctx_t context = NULL;
err = gpgme_new (&context);
if (! err)
err = gpgme_set_protocol (context, protocol);
if (! err)
{
s = keylist;
do {
while (*s == ' ')
s++;
for (i=0; *s && *s != ' ' && i < sizeof(buf)-1;)
buf[i++] = *s++;
buf[i] = 0;
if (*buf)
{
if (i>1 && buf[i-1] == '!')
{
/* The user selected to override the valididy of that
key. */
buf[i-1] = 0;
err = gpgme_get_key (context, buf, &key, 0);
if (! err)
key->uids->validity = GPGME_VALIDITY_FULL;
buf[i-1] = '!';
}
else
err = gpgme_get_key (context, buf, &key, 0);
if (! err)
{
safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
rset[rset_n++] = key;
}
else
{
mutt_error (_("error adding recipient `%s': %s\n"),
buf, gpgme_strerror (err));
FREE (&rset);
return NULL;
}
}
} while (*s);
}
/* NULL terminate. */
safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
rset[rset_n++] = NULL;
if (context)
gpgme_release (context);
return rset;
}
/* Make sure that the correct signer is set. Returns 0 on success. */
static int set_signer (gpgme_ctx_t ctx, int for_smime)
{
char *signid = for_smime ? SmimeDefaultKey: PgpSignAs;
gpgme_error_t err;
gpgme_ctx_t listctx;
gpgme_key_t key, key2;
if (!signid || !*signid)
return 0;
listctx = create_gpgme_context (for_smime);
err = gpgme_op_keylist_start (listctx, signid, 1);
if (!err)
err = gpgme_op_keylist_next (listctx, &key);
if (err)
{
gpgme_release (listctx);
mutt_error (_("secret key `%s' not found: %s\n"),
signid, gpgme_strerror (err));
return -1;
}
err = gpgme_op_keylist_next (listctx, &key2);
if (!err)
{
gpgme_key_release (key);
gpgme_key_release (key2);
gpgme_release (listctx);
mutt_error (_("ambiguous specification of secret key `%s'\n"),
signid);
return -1;
}
gpgme_op_keylist_end (listctx);
gpgme_release (listctx);
gpgme_signers_clear (ctx);
err = gpgme_signers_add (ctx, key);
gpgme_key_release (key);
if (err)
{
mutt_error (_("error setting secret key `%s': %s\n"),
signid, gpgme_strerror (err));
return -1;
}
return 0;
}
static gpgme_error_t
set_pka_sig_notation (gpgme_ctx_t ctx)
{
gpgme_error_t err;
err = gpgme_sig_notation_add (ctx,
PKA_NOTATION_NAME, current_sender, 0);
if (err)
{
mutt_error (_("error setting PKA signature notation: %s\n"),
gpgme_strerror (err));
mutt_sleep (2);
}
return err;
}
/* Encrypt the gpgme data object PLAINTEXT to the recipients in RSET
and return an allocated filename to a temporary file containing the
enciphered text. With USE_SMIME set to true, the smime backend is
used. With COMBINED_SIGNED a PGP message is signed and
encrypted. Returns NULL in case of error */
static char *encrypt_gpgme_object (gpgme_data_t plaintext, gpgme_key_t *rset,
int use_smime, int combined_signed)
{
gpgme_error_t err;
gpgme_ctx_t ctx;
gpgme_data_t ciphertext;
char *outfile;
ctx = create_gpgme_context (use_smime);
if (!use_smime)
gpgme_set_armor (ctx, 1);
ciphertext = create_gpgme_data ();
if (combined_signed)
{
if (set_signer (ctx, use_smime))
{
gpgme_data_release (ciphertext);
gpgme_release (ctx);
return NULL;
}
if (option (OPTCRYPTUSEPKA))
{
err = set_pka_sig_notation (ctx);
if (err)
{
gpgme_data_release (ciphertext);
gpgme_release (ctx);
return NULL;
}
}
err = gpgme_op_encrypt_sign (ctx, rset, GPGME_ENCRYPT_ALWAYS_TRUST,
plaintext, ciphertext);
}
else
err = gpgme_op_encrypt (ctx, rset, GPGME_ENCRYPT_ALWAYS_TRUST,
plaintext, ciphertext);
mutt_need_hard_redraw ();
if (err)
{
mutt_error (_("error encrypting data: %s\n"), gpgme_strerror (err));
gpgme_data_release (ciphertext);
gpgme_release (ctx);
return NULL;
}
gpgme_release (ctx);
outfile = data_object_to_tempfile (ciphertext, NULL);
gpgme_data_release (ciphertext);
return outfile;
}
/* Find the "micalg" parameter from the last Gpgme operation on
context CTX. It is expected that this operation was a sign
operation. Return the algorithm name as a C string in buffer BUF
which must have been allocated by the caller with size BUFLEN.
Returns 0 on success or -1 in case of an error. The return string
is truncted to BUFLEN - 1. */
static int get_micalg (gpgme_ctx_t ctx, int use_smime, char *buf, size_t buflen)
{
gpgme_sign_result_t result = NULL;
const char *algorithm_name = NULL;
if (buflen < 5)
return -1;
*buf = 0;
result = gpgme_op_sign_result (ctx);
if (result && result->signatures)
{
algorithm_name = gpgme_hash_algo_name (result->signatures->hash_algo);
if (algorithm_name)
{
if (use_smime)
{
/* convert GPGME raw hash name to RFC 2633 format */
snprintf (buf, buflen, "%s", algorithm_name);
ascii_strlower (buf);
} else {
/* convert GPGME raw hash name to RFC 3156 format */
snprintf (buf, buflen, "pgp-%s", algorithm_name);
ascii_strlower (buf + 4);
}
}
}
return *buf? 0:-1;
}
static void print_time(time_t t, STATE *s)
{
char p[STRING];
setlocale (LC_TIME, "");
#ifdef HAVE_LANGINFO_D_T_FMT
strftime (p, sizeof (p), nl_langinfo (D_T_FMT), localtime (&t));
#else
strftime (p, sizeof (p), "%c", localtime (&t));
#endif
setlocale (LC_TIME, "C");
state_attach_puts (p, s);
}
/*
* Implementation of `sign_message'.
*/
/* Sign the MESSAGE in body A either using OpenPGP or S/MIME when
USE_SMIME is passed as true. Returns the new body or NULL on
error. */
static BODY *sign_message (BODY *a, int use_smime)
{
BODY *t;
char *sigfile;
int err = 0;
char buf[100];
gpgme_ctx_t ctx;
gpgme_data_t message, signature;
convert_to_7bit (a); /* Signed data _must_ be in 7-bit format. */
message = body_to_data_object (a, 1);
if (!message)
return NULL;
signature = create_gpgme_data ();
ctx = create_gpgme_context (use_smime);
if (!use_smime)
gpgme_set_armor (ctx, 1);
if (set_signer (ctx, use_smime))
{
gpgme_data_release (signature);
gpgme_release (ctx);
return NULL;
}
if (option (OPTCRYPTUSEPKA))
{
err = set_pka_sig_notation (ctx);
if (err)
{
gpgme_data_release (signature);
gpgme_data_release (message);
gpgme_release (ctx);
return NULL;
}
}
err = gpgme_op_sign (ctx, message, signature, GPGME_SIG_MODE_DETACH );
mutt_need_hard_redraw ();
gpgme_data_release (message);
if (err)
{
gpgme_data_release (signature);
gpgme_release (ctx);
mutt_error (_("error signing data: %s\n"), gpgme_strerror (err));
return NULL;
}
sigfile = data_object_to_tempfile (signature, NULL);
gpgme_data_release (signature);
if (!sigfile)
{
gpgme_release (ctx);
return NULL;
}
t = mutt_new_body ();
t->type = TYPEMULTIPART;
t->subtype = safe_strdup ("signed");
t->encoding = ENC7BIT;
t->use_disp = 0;
t->disposition = DISPINLINE;
mutt_generate_boundary (&t->parameter);
mutt_set_parameter ("protocol",
use_smime? "application/pkcs7-signature"
: "application/pgp-signature",
&t->parameter);
/* Get the micalg from gpgme. Old gpgme versions don't support this
for S/MIME so we assume sha-1 in this case. */
if (!get_micalg (ctx, use_smime, buf, sizeof buf))
mutt_set_parameter ("micalg", buf, &t->parameter);
else if (use_smime)
mutt_set_parameter ("micalg", "sha1", &t->parameter);
gpgme_release (ctx);
t->parts = a;
a = t;
t->parts->next = mutt_new_body ();
t = t->parts->next;
t->type = TYPEAPPLICATION;
if (use_smime)
{
t->subtype = safe_strdup ("pkcs7-signature");
mutt_set_parameter ("name", "smime.p7s", &t->parameter);
t->encoding = ENCBASE64;
t->use_disp = 1;
t->disposition = DISPATTACH;
t->d_filename = safe_strdup ("smime.p7s");
}
else
{
t->subtype = safe_strdup ("pgp-signature");
t->use_disp = 0;
t->disposition = DISPNONE;
t->encoding = ENC7BIT;
}
t->filename = sigfile;
t->unlink = 1; /* ok to remove this file after sending. */
return a;
}
BODY *pgp_gpgme_sign_message (BODY *a)
{
return sign_message (a, 0);
}
BODY *smime_gpgme_sign_message (BODY *a)
{
return sign_message (a, 1);
}
/*
* Implementation of `encrypt_message'.
*/
/* Encrypt the mail body A to all keys given as space separated keyids
or fingerprints in KEYLIST and return the encrypted body. */
BODY *pgp_gpgme_encrypt_message (BODY *a, char *keylist, int sign)
{
char *outfile = NULL;
BODY *t;
gpgme_key_t *rset = NULL;
gpgme_data_t plaintext;
rset = create_recipient_set (keylist, GPGME_PROTOCOL_OpenPGP);
if (!rset)
return NULL;
if (sign)
convert_to_7bit (a);
plaintext = body_to_data_object (a, 0);
if (!plaintext)
{
FREE (&rset);
return NULL;
}
outfile = encrypt_gpgme_object (plaintext, rset, 0, sign);
gpgme_data_release (plaintext);
FREE (&rset);
if (!outfile)
return NULL;
t = mutt_new_body ();
t->type = TYPEMULTIPART;
t->subtype = safe_strdup ("encrypted");
t->encoding = ENC7BIT;
t->use_disp = 0;
t->disposition = DISPINLINE;
mutt_generate_boundary(&t->parameter);
mutt_set_parameter("protocol", "application/pgp-encrypted", &t->parameter);
t->parts = mutt_new_body ();
t->parts->type = TYPEAPPLICATION;
t->parts->subtype = safe_strdup ("pgp-encrypted");
t->parts->encoding = ENC7BIT;
t->parts->next = mutt_new_body ();
t->parts->next->type = TYPEAPPLICATION;
t->parts->next->subtype = safe_strdup ("octet-stream");
t->parts->next->encoding = ENC7BIT;
t->parts->next->filename = outfile;
t->parts->next->use_disp = 1;
t->parts->next->disposition = DISPATTACH;
t->parts->next->unlink = 1; /* delete after sending the message */
t->parts->next->d_filename = safe_strdup ("msg.asc"); /* non pgp/mime
can save */
return t;
}