forked from muttmua/mutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt-gpgme.c
5564 lines (4867 loc) · 143 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-1997,2007 Michael R. Elkins <[email protected]>
* Copyright (C) 1998-2000 Thomas Roessler <[email protected]>
* Copyright (C) 2001 Thomas Roessler <[email protected]>
* Oliver Ehli <[email protected]>
* Copyright (C) 2002-2004, 2018 g10 Code GmbH
* Copyright (C) 2010,2012-2013 Michael R. Elkins <[email protected]>
*
* 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>
#include <locale.h>
#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) ((notation)->name && \
! 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)
/*
* 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)*/
gpgme_validity_t validity; /* uid validity (cached 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 points to a digit 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);
}
/* Compare function for version strings. The return value is
* like strcmp(). LEVEL may be
* 0 - reserved
* 1 - format is "<major><patchlevel>".
* 2 - format is "<major>.<minor><patchlevel>".
* 3 - format is "<major>.<minor>.<micro><patchlevel>".
* To ignore the patchlevel in the comparison add 10 to LEVEL. To get
* a reverse sorting order use a negative number. */
#if GPGRT_VERSION_NUMBER >= 0x012100 /* libgpg-error >= 1.33 */
static int
cmp_version_strings (const char *a, const char *b, int level)
{
return gpgrt_cmp_version (a, b, level);
}
#else /* gpgme < 1.33 */
/* This function parses the first portion of the version number S and
* stores it at NUMBER. On success, this function returns a pointer
* into S starting with the first character, which is not part of the
* initial number portion; on failure, NULL is returned. */
static const char *parse_version_number (const char *s, int *number)
{
int val = 0;
if (*s == '0' && digitp (s+1))
return NULL; /* Leading zeros are not allowed. */
for (; digitp (s); s++)
{
val *= 10;
val += *s - '0';
}
*number = val;
return val < 0 ? NULL : s;
}
/* This function breaks up the complete string-representation of the
* version number S, which is of the following struture: <major
* number>.<minor number>.<micro number><patch level>. The major,
* minor and micro number components will be stored in *MAJOR, *MINOR
* and *MICRO. If MINOR or MICRO is NULL the version number is
* assumed to have just 1 respective 2 parts.
*
* On success, the last component, the patch level, will be returned;
* in failure, NULL will be returned. */
static const char *parse_version_string (const char *s, int *major,
int *minor, int *micro)
{
s = parse_version_number (s, major);
if (!s)
return NULL;
if (!minor)
{
if (*s == '.')
s++;
}
else
{
if (*s != '.')
return NULL;
s++;
s = parse_version_number (s, minor);
if (!s)
return NULL;
if (!micro)
{
if (*s == '.')
s++;
}
else
{
if (*s != '.')
return NULL;
s++;
s = parse_version_number (s, micro);
if (!s)
return NULL;
}
}
return s; /* patchlevel */
}
/* Substitute for the gpgrt based implementation.
* See above for a description. */
static int
cmp_version_strings (const char *a, const char *b, int level)
{
int a_major, a_minor, a_micro;
int b_major, b_minor, b_micro;
const char *a_plvl, *b_plvl;
int r;
int ignore_plvl;
int positive, negative;
if (level < 0)
{
positive = -1;
negative = 1;
level = 0 - level;
}
else
{
positive = 1;
negative = -1;
}
if ((ignore_plvl = (level > 9)))
level %= 10;
a_major = a_minor = a_micro = 0;
a_plvl = parse_version_string (a, &a_major,
level > 1? &a_minor : NULL,
level > 2? &a_micro : NULL);
if (!a_plvl)
a_major = a_minor = a_micro = 0; /* Error. */
b_major = b_minor = b_micro = 0;
b_plvl = parse_version_string (b, &b_major,
level > 1? &b_minor : NULL,
level > 2? &b_micro : NULL);
if (!b_plvl)
b_major = b_minor = b_micro = 0;
if (!ignore_plvl)
{
if (!a_plvl && !b_plvl)
return negative; /* Put invalid strings at the end. */
if (a_plvl && !b_plvl)
return positive;
if (!a_plvl && b_plvl)
return negative;
}
if (a_major > b_major)
return positive;
if (a_major < b_major)
return negative;
if (a_minor > b_minor)
return positive;
if (a_minor < b_minor)
return negative;
if (a_micro > b_micro)
return positive;
if (a_micro < b_micro)
return negative;
if (ignore_plvl)
return 0;
for (; *a_plvl && *b_plvl; a_plvl++, b_plvl++)
{
if (*a_plvl == '.' && *b_plvl == '.')
{
r = strcmp (a_plvl, b_plvl);
if (!r)
return 0;
else if ( r > 0 )
return positive;
else
return negative;
}
else if (*a_plvl == '.')
return negative; /* B is larger. */
else if (*b_plvl == '.')
return positive; /* A is larger. */
else if (*a_plvl != *b_plvl)
break;
}
if (*a_plvl == *b_plvl)
return 0;
else if ((*(signed char *)a_plvl - *(signed char *)b_plvl) > 0)
return positive;
else
return negative;
}
#endif /* gpgme < 1.33 */
/*
* 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 long keyID for the key K. */
static const char *crypt_long_keyid (crypt_key_t *k)
{
const char *s = "????????????????";
if (k->kobj && k->kobj->subkeys)
{
s = k->kobj->subkeys->keyid;
}
return s;
}
/* Return the short keyID for the key K. */
static const char *crypt_short_keyid (crypt_key_t *k)
{
const char *s = "????????";
if (k->kobj && k->kobj->subkeys)
{
s = k->kobj->subkeys->keyid;
if (strlen (s) == 16)
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;
}
/* Returns the fingerprint if available, otherwise
* returns the long keyid.
*/
static const char *crypt_fpr_or_lkeyid(crypt_key_t *k)
{
const char *s = "????????????????";
if (k->kobj && k->kobj->subkeys)
{
if (k->kobj->subkeys->fpr)
s = k->kobj->subkeys->fpr;
else
s = k->kobj->subkeys->keyid;
}
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;
k->validity = key->validity;
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)
{
crypt_key_t *k;
if (!keylist)
return;
while (*keylist)
{
k = *keylist;
*keylist = (*keylist)->next;
gpgme_key_unref (k->kobj);
FREE (&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)
{
unsigned int is_strong = 0;
if ((key->flags & KEYFLAG_ISX509))
return 1;
switch (key->validity)
{
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;
err = gpgme_new (&ctx);
#ifdef USE_AUTOCRYPT
if (!err && option (OPTAUTOCRYPTGPGME))
err = gpgme_ctx_set_engine_info (ctx, GPGME_PROTOCOL_OpenPGP, NULL,
AutocryptDir);
#endif
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;
}
/* Return true if the OpenPGP engine's version is at least VERSION. */
static int have_gpg_version (const char *version)
{
static char *engine_version;
if (!engine_version)
{
gpgme_ctx_t ctx;
gpgme_engine_info_t engineinfo;
ctx = create_gpgme_context (0);
engineinfo = gpgme_ctx_get_engine_info (ctx);
while (engineinfo && engineinfo->protocol != GPGME_PROTOCOL_OpenPGP)
engineinfo = engineinfo->next;
if (!engineinfo)
{
dprint (1, (debugfile, "Error finding GPGME PGP engine\n"));
engine_version = safe_strdup ("0.0.0");
}
else
engine_version = safe_strdup (engineinfo->version);
gpgme_release (ctx);
}
return cmp_version_strings (engine_version, version, 3) >= 0;
}
/* 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)
{
BUFFER *tempfile = NULL;
FILE *fptmp;
int err;
gpgme_data_t data = NULL;
tempfile = mutt_buffer_pool_get ();
mutt_buffer_mktemp (tempfile);
fptmp = safe_fopen (mutt_b2s (tempfile), "w+");
if (!fptmp)
{
mutt_perror (mutt_b2s (tempfile));
goto cleanup;
}
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, mutt_b2s (tempfile), 1);
if (err)
{
mutt_error (_("error allocating data object: %s\n"), gpgme_strerror (err));
gpgme_data_release (data);
data = NULL;
/* fall through to unlink the tempfile */
}
}
unlink (mutt_b2s (tempfile));
cleanup:
mutt_buffer_pool_release (&tempfile);
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, LOFF_T 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 temporary file.
* The tempfile name may be optionally passed in.
* If ret_fp is passed in, the file will be rewound, left open, and returned
* via that parameter.
* The tempfile name is returned, and must be freed.
*/
static char *data_object_to_tempfile (gpgme_data_t data, const char *tempf, FILE **ret_fp)
{
int err;
BUFFER *tempfb = NULL;
char *rv = NULL;
FILE *fp;
size_t nread = 0;
if (!tempf)
{
tempfb = mutt_buffer_pool_get ();
mutt_buffer_mktemp (tempfb);
tempf = mutt_b2s (tempfb);
}
if ((fp = safe_fopen (tempf, tempf == mutt_b2s (tempfb) ? "w+" : "a+")) == NULL)
{
mutt_perror _("Can't create temporary file");
goto cleanup;
}
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 (tempf);
safe_fclose (&fp);
unlink (tempf);
goto cleanup;
}
}
}
if (ret_fp)
rewind (fp);
else
safe_fclose (&fp);
if (nread == -1)
{
mutt_error (_("error reading data object: %s\n"), gpgme_strerror (err));
unlink (tempf);
safe_fclose (&fp);
goto cleanup;
}
if (ret_fp)
*ret_fp = fp;
rv = safe_strdup (tempf);
cleanup:
mutt_buffer_pool_release (&tempfb);
return rv;
}
#if GPGME_VERSION_NUMBER >= 0x010b00 /* gpgme >= 1.11.0 */
static void create_recipient_string (const char *keylist, BUFFER *recpstring,
int use_smime)
{
const char *s;
unsigned int n = 0;
s = keylist;
do
{
while (*s == ' ')
s++;
if (*s)
{
if (!n)
{
if (!use_smime)
mutt_buffer_addstr (recpstring, "--\n");
}
else
mutt_buffer_addch (recpstring, '\n');
n++;
while (*s && *s != ' ')
mutt_buffer_addch (recpstring, *s++);
}
} while (*s);
}
#else
static void free_recipient_set (gpgme_key_t **p_rset)
{
gpgme_key_t *rset, k;
if (!p_rset)
return;
rset = *p_rset;
if (!rset)
return;
while (*rset)
{
k = *rset;
gpgme_key_unref (k);
rset++;
}
FREE (p_rset); /* __FREE_CHECKED__ */
}
/* 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, int use_smime)
{
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;
context = create_gpgme_context (use_smime);
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 validity of that
key. */
buf[i-1] = 0;
err = gpgme_get_key (context, buf, &key, 0);
if (! err && key->uids)
key->uids->validity = GPGME_VALIDITY_FULL;
buf[i-1] = '!';
}
else
err = gpgme_get_key (context, buf, &key, 0);
safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
if (! err)
rset[rset_n++] = key;
else
{
mutt_error (_("error adding recipient `%s': %s\n"),
buf, gpgme_strerror (err));
rset[rset_n] = NULL;
free_recipient_set (&rset);
gpgme_release (context);
return NULL;
}
}
} while (*s);
/* NULL terminate. */
safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
rset[rset_n++] = NULL;
gpgme_release (context);
return rset;
}
#endif /* GPGME_VERSION_NUMBER >= 0x010b00 */
/* 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;
gpgme_error_t err;
gpgme_ctx_t listctx;
gpgme_key_t key, key2;
char *fpr, *fpr2;
if (for_smime)
signid = SmimeSignAs ? SmimeSignAs : SmimeDefaultKey;
#ifdef USE_AUTOCRYPT
else if (option (OPTAUTOCRYPTGPGME))
signid = AutocryptSignAs;
#endif
else
signid = PgpSignAs ? PgpSignAs : PgpDefaultKey;
if (!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;
}
fpr = "fpr1";
if (key->subkeys)
fpr = key->subkeys->fpr ? key->subkeys->fpr : key->subkeys->keyid;
while (! gpgme_op_keylist_next (listctx, &key2))
{
fpr2 = "fpr2";
if (key2->subkeys)
fpr2 = key2->subkeys->fpr ? key2->subkeys->fpr : key2->subkeys->keyid;
if (mutt_strcmp(fpr, fpr2))
{
gpgme_key_unref (key);
gpgme_key_unref (key2);
gpgme_release (listctx);
mutt_error (_("ambiguous specification of secret key `%s'\n"),
signid);
return -1;
} else {