Coverage Report

Created: 2026-09-14 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/misc.c
Line
Count
Source
1
/* misc.c - miscellaneous functions
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3
 *               2008, 2009, 2010 Free Software Foundation, Inc.
4
 * Copyright (C) 2014 Werner Koch
5
 *
6
 * This file is part of GnuPG.
7
 *
8
 * GnuPG is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * GnuPG is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <config.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <unistd.h>
27
#include <errno.h>
28
#if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
29
#include <asm/sysinfo.h>
30
#include <asm/unistd.h>
31
#endif
32
#ifdef HAVE_SETRLIMIT
33
#include <time.h>
34
#include <sys/time.h>
35
#include <sys/resource.h>
36
#endif
37
#ifdef ENABLE_SELINUX_HACKS
38
#include <sys/stat.h>
39
#endif
40
41
#ifdef HAVE_W32_SYSTEM
42
#include <time.h>
43
#include <process.h>
44
#ifdef HAVE_WINSOCK2_H
45
# define WIN32_LEAN_AND_MEAN 1
46
# include <winsock2.h>
47
#endif
48
#include <windows.h>
49
#include <shlobj.h>
50
#ifndef CSIDL_APPDATA
51
#define CSIDL_APPDATA 0x001a
52
#endif
53
#ifndef CSIDL_LOCAL_APPDATA
54
#define CSIDL_LOCAL_APPDATA 0x001c
55
#endif
56
#ifndef CSIDL_FLAG_CREATE
57
#define CSIDL_FLAG_CREATE 0x8000
58
#endif
59
#endif /*HAVE_W32_SYSTEM*/
60
61
#include "gpg.h"
62
#ifdef HAVE_W32_SYSTEM
63
# include "../common/status.h"
64
#endif /*HAVE_W32_SYSTEM*/
65
#include "../common/util.h"
66
#include "main.h"
67
#include "photoid.h"
68
#include "options.h"
69
#include "call-agent.h"
70
#include "../common/i18n.h"
71
#include "../common/zb32.h"
72
73
74
/* FIXME: Libgcrypt 1.9 will support EAX.  Until we name this a
75
 * requirement we hardwire the enum used for EAX.  */
76
0
#define MY_GCRY_CIPHER_MODE_EAX 14
77
78
79
#ifdef ENABLE_SELINUX_HACKS
80
/* A object and a global variable to keep track of files marked as
81
   secured. */
82
struct secured_file_item
83
{
84
  struct secured_file_item *next;
85
  ino_t ino;
86
  dev_t dev;
87
};
88
static struct secured_file_item *secured_files;
89
#endif /*ENABLE_SELINUX_HACKS*/
90
91
92
93
94
/* For the sake of SELinux we want to restrict access through gpg to
95
   certain files we keep under our own control.  This function
96
   registers such a file and is_secured_file may then be used to
97
   check whether a file has ben registered as secured. */
98
void
99
register_secured_file (const char *fname)
100
0
{
101
#ifdef ENABLE_SELINUX_HACKS
102
  struct stat buf;
103
  struct secured_file_item *sf;
104
105
  /* Note that we stop immediately if something goes wrong here. */
106
  if (gnupg_stat (fname, &buf))
107
    log_fatal (_("fstat of '%s' failed in %s: %s\n"), fname,
108
               "register_secured_file", strerror (errno));
109
/*   log_debug ("registering '%s' i=%lu.%lu\n", fname, */
110
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
111
  for (sf=secured_files; sf; sf = sf->next)
112
    {
113
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
114
        return; /* Already registered.  */
115
    }
116
117
  sf = xmalloc (sizeof *sf);
118
  sf->ino = buf.st_ino;
119
  sf->dev = buf.st_dev;
120
  sf->next = secured_files;
121
  secured_files = sf;
122
#else /*!ENABLE_SELINUX_HACKS*/
123
0
  (void)fname;
124
0
#endif /*!ENABLE_SELINUX_HACKS*/
125
0
}
126
127
/* Remove a file registered as secure. */
128
void
129
unregister_secured_file (const char *fname)
130
0
{
131
#ifdef ENABLE_SELINUX_HACKS
132
  struct stat buf;
133
  struct secured_file_item *sf, *sfprev;
134
135
  if (gnupg_stat (fname, &buf))
136
    {
137
      log_error (_("fstat of '%s' failed in %s: %s\n"), fname,
138
                 "unregister_secured_file", strerror (errno));
139
      return;
140
    }
141
/*   log_debug ("unregistering '%s' i=%lu.%lu\n", fname,  */
142
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
143
  for (sfprev=NULL,sf=secured_files; sf; sfprev=sf, sf = sf->next)
144
    {
145
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
146
        {
147
          if (sfprev)
148
            sfprev->next = sf->next;
149
          else
150
            secured_files = sf->next;
151
          xfree (sf);
152
          return;
153
        }
154
    }
155
#else /*!ENABLE_SELINUX_HACKS*/
156
0
  (void)fname;
157
0
#endif /*!ENABLE_SELINUX_HACKS*/
158
0
}
159
160
/* Return true if FD is corresponds to a secured file.  Using -1 for
161
   FS is allowed and will return false. */
162
int
163
is_secured_file (gnupg_fd_t fd)
164
10.6k
{
165
#ifdef ENABLE_SELINUX_HACKS
166
  struct stat buf;
167
  struct secured_file_item *sf;
168
169
  if (fd == -1)
170
    return 0; /* No file descriptor so it can't be secured either.  */
171
172
  /* Note that we print out a error here and claim that a file is
173
     secure if something went wrong. */
174
  if (fstat (fd, &buf))
175
    {
176
      log_error (_("fstat(%d) failed in %s: %s\n"), fd,
177
                 "is_secured_file", strerror (errno));
178
      return 1;
179
    }
180
/*   log_debug ("is_secured_file (%d) i=%lu.%lu\n", fd, */
181
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
182
  for (sf=secured_files; sf; sf = sf->next)
183
    {
184
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
185
        return 1; /* Yes.  */
186
    }
187
#else /*!ENABLE_SELINUX_HACKS*/
188
10.6k
  (void)fd;
189
10.6k
#endif /*!ENABLE_SELINUX_HACKS*/
190
10.6k
  return 0; /* No. */
191
10.6k
}
192
193
/* Return true if FNAME is corresponds to a secured file.  Using NULL,
194
   "" or "-" for FS is allowed and will return false. This function is
195
   used before creating a file, thus it won't fail if the file does
196
   not exist. */
197
int
198
is_secured_filename (const char *fname)
199
2
{
200
#ifdef ENABLE_SELINUX_HACKS
201
  struct stat buf;
202
  struct secured_file_item *sf;
203
204
  if (iobuf_is_pipe_filename (fname) || !*fname)
205
    return 0;
206
207
  /* Note that we print out a error here and claim that a file is
208
     secure if something went wrong. */
209
  if (gnupg_stat (fname, &buf))
210
    {
211
      if (errno == ENOENT || errno == EPERM || errno == EACCES)
212
        return 0;
213
      log_error (_("fstat of '%s' failed in %s: %s\n"), fname,
214
                 "is_secured_filename", strerror (errno));
215
      return 1;
216
    }
217
/*   log_debug ("is_secured_filename (%s) i=%lu.%lu\n", fname, */
218
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
219
  for (sf=secured_files; sf; sf = sf->next)
220
    {
221
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
222
        return 1; /* Yes.  */
223
    }
224
#else /*!ENABLE_SELINUX_HACKS*/
225
2
  (void)fname;
226
2
#endif /*!ENABLE_SELINUX_HACKS*/
227
2
  return 0; /* No. */
228
2
}
229
230
231
232
u16
233
checksum_u16( unsigned n )
234
0
{
235
0
    u16 a;
236
237
0
    a  = (n >> 8) & 0xff;
238
0
    a += n & 0xff;
239
0
    return a;
240
0
}
241
242
243
u16
244
checksum (const byte *p, unsigned n)
245
0
{
246
0
    u16 a;
247
248
0
    for(a=0; n; n-- )
249
0
  a += *p++;
250
0
    return a;
251
0
}
252
253
u16
254
checksum_mpi (gcry_mpi_t a)
255
0
{
256
0
  u16 csum;
257
0
  byte *buffer;
258
0
  size_t nbytes;
259
260
  /*
261
   * This code can be skipped when gcry_mpi_print
262
   * supports opaque MPI.
263
   */
264
0
  if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
265
0
    {
266
0
      const byte *p;
267
0
      unsigned int nbits;
268
269
0
      p = gcry_mpi_get_opaque (a, &nbits);
270
0
      if (!p)
271
0
        return 0;
272
273
0
      csum = nbits >> 8;
274
0
      csum += (nbits & 0xff);
275
0
      csum += checksum (p, (nbits+7)/8);
276
0
      return csum;
277
0
    }
278
279
0
  if ( gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &nbytes, a) )
280
0
    BUG ();
281
  /* Fixme: For numbers not in secure memory we should use a stack
282
   * based buffer and only allocate a larger one if mpi_print returns
283
   * an error. */
284
0
  buffer = (gcry_is_secure(a)?
285
0
            gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes));
286
0
  if ( gcry_mpi_print (GCRYMPI_FMT_PGP, buffer, nbytes, NULL, a) )
287
0
    BUG ();
288
0
  csum = checksum (buffer, nbytes);
289
0
  xfree (buffer);
290
0
  return csum;
291
0
}
292
293
294
void
295
print_pubkey_algo_note (pubkey_algo_t algo)
296
0
{
297
0
  if(algo >= 100 && algo <= 110)
298
0
    {
299
0
      static int warn=0;
300
0
      if(!warn)
301
0
  {
302
0
    warn=1;
303
0
          es_fflush (es_stdout);
304
0
    log_info (_("WARNING: using experimental public key algorithm %s\n"),
305
0
        openpgp_pk_algo_name (algo));
306
0
  }
307
0
    }
308
0
  else if (algo == PUBKEY_ALGO_ELGAMAL)
309
0
    {
310
0
      es_fflush (es_stdout);
311
0
      log_info (_("WARNING: Elgamal sign+encrypt keys are deprecated\n"));
312
0
    }
313
0
}
314
315
void
316
print_cipher_algo_note (cipher_algo_t algo)
317
0
{
318
0
  if(algo >= 100 && algo <= 110)
319
0
    {
320
0
      static int warn=0;
321
0
      if(!warn)
322
0
  {
323
0
    warn=1;
324
0
          es_fflush (es_stdout);
325
0
    log_info (_("WARNING: using experimental cipher algorithm %s\n"),
326
0
                    openpgp_cipher_algo_name (algo));
327
0
  }
328
0
    }
329
0
}
330
331
void
332
print_digest_algo_note (digest_algo_t algo)
333
0
{
334
0
  if(algo >= 100 && algo <= 110)
335
0
    {
336
0
      static int warn=0;
337
0
      const enum gcry_md_algos galgo = map_md_openpgp_to_gcry (algo);
338
339
0
      if(!warn)
340
0
  {
341
0
    warn=1;
342
0
          es_fflush (es_stdout);
343
0
    log_info (_("WARNING: using experimental digest algorithm %s\n"),
344
0
                    gcry_md_algo_name (galgo));
345
0
  }
346
0
    }
347
0
  else if (is_weak_digest (algo))
348
0
    {
349
0
      const enum gcry_md_algos galgo = map_md_openpgp_to_gcry (algo);
350
0
      es_fflush (es_stdout);
351
0
      log_info (_("WARNING: digest algorithm %s is deprecated\n"),
352
0
                gcry_md_algo_name (galgo));
353
0
    }
354
0
}
355
356
357
void
358
print_digest_rejected_note (enum gcry_md_algos algo)
359
0
{
360
0
  struct weakhash* weak;
361
0
  int show = 1;
362
363
0
  if (opt.quiet)
364
0
    return;
365
366
0
  for (weak = opt.weak_digests; weak; weak = weak->next)
367
0
    if (weak->algo == algo)
368
0
      {
369
0
        if (weak->rejection_shown)
370
0
          show = 0;
371
0
        else
372
0
          weak->rejection_shown = 1;
373
0
        break;
374
0
      }
375
376
0
  if (show)
377
0
    {
378
0
      es_fflush (es_stdout);
379
0
      log_info
380
0
        (_("Note: signatures using the %s algorithm are rejected\n"),
381
0
         gcry_md_algo_name(algo));
382
0
    }
383
0
}
384
385
386
void
387
print_sha1_keysig_rejected_note (void)
388
0
{
389
0
  static int shown;
390
391
0
  if (shown || opt.quiet)
392
0
    return;
393
394
0
  shown = 1;
395
0
  es_fflush (es_stdout);
396
0
  log_info (_("Note: third-party key signatures using"
397
0
              " the %s algorithm are rejected\n"),
398
0
            gcry_md_algo_name (GCRY_MD_SHA1));
399
0
  if (!opt.quiet)
400
0
    log_info (_("(use option \"%s\" to override)\n"),
401
0
              "--allow-weak-key-signatures");
402
0
}
403
404
405
/* Print a message
406
 *  "(reported error: %s)\n
407
 * in verbose mode to further explain an error.  If the error code has
408
 * the value IGNORE_EC no message is printed.  A message is also not
409
 * printed if ERR is 0.  */
410
void
411
print_reported_error (gpg_error_t err, gpg_err_code_t ignore_ec)
412
0
{
413
0
  if (!opt.verbose)
414
0
    return;
415
416
0
  if (!gpg_err_code (err))
417
0
    ;
418
0
  else if (gpg_err_code (err) == ignore_ec)
419
0
    ;
420
0
  else if (gpg_err_source (err) == GPG_ERR_SOURCE_DEFAULT)
421
0
    log_info (_("(reported error: %s)\n"),
422
0
              gpg_strerror (err));
423
0
  else
424
0
    log_info (_("(reported error: %s <%s>)\n"),
425
0
              gpg_strerror (err), gpg_strsource (err));
426
427
0
}
428
429
430
/* Print a message
431
 *   "(further info: %s)\n
432
 * in verbose mode to further explain an error.  That message is
433
 * intended to help debug a problem and should not be translated.
434
 */
435
void
436
print_further_info (const char *format, ...)
437
0
{
438
0
  va_list arg_ptr;
439
440
0
  if (!opt.verbose)
441
0
    return;
442
443
0
  log_info (_("(further info: "));
444
0
  va_start (arg_ptr, format);
445
0
  log_logv (GPGRT_LOGLVL_CONT, format, arg_ptr);
446
0
  va_end (arg_ptr);
447
0
  log_printf (")\n");
448
0
}
449
450
451
/* Map OpenPGP algo numbers to those used by Libgcrypt.  We need to do
452
   this for algorithms we implemented in Libgcrypt after they become
453
   part of OpenPGP.  */
454
enum gcry_cipher_algos
455
map_cipher_openpgp_to_gcry (cipher_algo_t algo)
456
0
{
457
0
  switch (algo)
458
0
    {
459
0
    case CIPHER_ALGO_NONE:        return GCRY_CIPHER_NONE;
460
461
0
#ifdef GPG_USE_IDEA
462
0
    case CIPHER_ALGO_IDEA:        return GCRY_CIPHER_IDEA;
463
#else
464
    case CIPHER_ALGO_IDEA:        return 0;
465
#endif
466
467
0
    case CIPHER_ALGO_3DES:    return GCRY_CIPHER_3DES;
468
469
0
#ifdef GPG_USE_CAST5
470
0
    case CIPHER_ALGO_CAST5:   return GCRY_CIPHER_CAST5;
471
#else
472
    case CIPHER_ALGO_CAST5:   return 0;
473
#endif
474
475
0
#ifdef GPG_USE_BLOWFISH
476
0
    case CIPHER_ALGO_BLOWFISH:    return GCRY_CIPHER_BLOWFISH;
477
#else
478
    case CIPHER_ALGO_BLOWFISH:    return 0;
479
#endif
480
481
0
#ifdef GPG_USE_AES128
482
0
    case CIPHER_ALGO_AES:         return GCRY_CIPHER_AES;
483
#else
484
    case CIPHER_ALGO_AES:         return 0;
485
#endif
486
487
0
#ifdef GPG_USE_AES192
488
0
    case CIPHER_ALGO_AES192:      return GCRY_CIPHER_AES192;
489
#else
490
    case CIPHER_ALGO_AES192:      return 0;
491
#endif
492
493
0
#ifdef GPG_USE_AES256
494
0
    case CIPHER_ALGO_AES256:      return GCRY_CIPHER_AES256;
495
#else
496
    case CIPHER_ALGO_AES256:      return 0;
497
#endif
498
499
0
#ifdef GPG_USE_TWOFISH
500
0
    case CIPHER_ALGO_TWOFISH:     return GCRY_CIPHER_TWOFISH;
501
#else
502
    case CIPHER_ALGO_TWOFISH:     return 0;
503
#endif
504
505
0
#ifdef GPG_USE_CAMELLIA128
506
0
    case CIPHER_ALGO_CAMELLIA128: return GCRY_CIPHER_CAMELLIA128;
507
#else
508
    case CIPHER_ALGO_CAMELLIA128: return 0;
509
#endif
510
511
0
#ifdef GPG_USE_CAMELLIA192
512
0
    case CIPHER_ALGO_CAMELLIA192: return GCRY_CIPHER_CAMELLIA192;
513
#else
514
    case CIPHER_ALGO_CAMELLIA192: return 0;
515
#endif
516
517
0
#ifdef GPG_USE_CAMELLIA256
518
0
    case CIPHER_ALGO_CAMELLIA256: return GCRY_CIPHER_CAMELLIA256;
519
#else
520
    case CIPHER_ALGO_CAMELLIA256: return 0;
521
#endif
522
0
    default: return 0;
523
0
    }
524
0
}
525
526
/* The inverse function of above.  */
527
static cipher_algo_t
528
map_cipher_gcry_to_openpgp (enum gcry_cipher_algos algo)
529
0
{
530
0
  switch (algo)
531
0
    {
532
0
    case GCRY_CIPHER_NONE:        return CIPHER_ALGO_NONE;
533
0
    case GCRY_CIPHER_IDEA:        return CIPHER_ALGO_IDEA;
534
0
    case GCRY_CIPHER_3DES:        return CIPHER_ALGO_3DES;
535
0
    case GCRY_CIPHER_CAST5:       return CIPHER_ALGO_CAST5;
536
0
    case GCRY_CIPHER_BLOWFISH:    return CIPHER_ALGO_BLOWFISH;
537
0
    case GCRY_CIPHER_AES:         return CIPHER_ALGO_AES;
538
0
    case GCRY_CIPHER_AES192:      return CIPHER_ALGO_AES192;
539
0
    case GCRY_CIPHER_AES256:      return CIPHER_ALGO_AES256;
540
0
    case GCRY_CIPHER_TWOFISH:     return CIPHER_ALGO_TWOFISH;
541
0
    case GCRY_CIPHER_CAMELLIA128: return CIPHER_ALGO_CAMELLIA128;
542
0
    case GCRY_CIPHER_CAMELLIA192: return CIPHER_ALGO_CAMELLIA192;
543
0
    case GCRY_CIPHER_CAMELLIA256: return CIPHER_ALGO_CAMELLIA256;
544
0
    default: return 0;
545
0
    }
546
0
}
547
548
549
/* Return the block length of an OpenPGP cipher algorithm.  */
550
int
551
openpgp_cipher_blocklen (cipher_algo_t algo)
552
11.3k
{
553
  /* We use the numbers from OpenPGP to be sure that we get the right
554
     block length.  This is so that the packet parsing code works even
555
     for unknown algorithms (for which we assume 8 due to tradition).
556
557
     NOTE: If you change the returned blocklen above 16, check
558
     the callers because they may use a fixed size buffer of that
559
     size. */
560
11.3k
  switch (algo)
561
11.3k
    {
562
31
    case CIPHER_ALGO_AES:
563
55
    case CIPHER_ALGO_AES192:
564
55
    case CIPHER_ALGO_AES256:
565
55
    case CIPHER_ALGO_TWOFISH:
566
55
    case CIPHER_ALGO_CAMELLIA128:
567
123
    case CIPHER_ALGO_CAMELLIA192:
568
125
    case CIPHER_ALGO_CAMELLIA256:
569
125
      return 16;
570
571
11.2k
    default:
572
11.2k
      return 8;
573
11.3k
    }
574
11.3k
}
575
576
/****************
577
 * Wrapper around the libgcrypt function with additional checks on
578
 * the OpenPGP constraints for the algo ID.
579
 */
580
int
581
openpgp_cipher_test_algo (cipher_algo_t algo)
582
0
{
583
0
  enum gcry_cipher_algos ga;
584
585
0
  ga = map_cipher_openpgp_to_gcry (algo);
586
0
  if (!ga)
587
0
    return gpg_error (GPG_ERR_CIPHER_ALGO);
588
589
0
  return gcry_cipher_test_algo (ga);
590
0
}
591
592
/* Map the OpenPGP cipher algorithm whose ID is contained in ALGORITHM to a
593
   string representation of the algorithm name.  For unknown algorithm
594
   IDs this function returns "?".  */
595
const char *
596
openpgp_cipher_algo_name (cipher_algo_t algo)
597
0
{
598
0
  switch (algo)
599
0
    {
600
0
    case CIPHER_ALGO_IDEA:        return "IDEA";
601
0
    case CIPHER_ALGO_3DES:    return "3DES";
602
0
    case CIPHER_ALGO_CAST5:   return "CAST5";
603
0
    case CIPHER_ALGO_BLOWFISH:    return "BLOWFISH";
604
0
    case CIPHER_ALGO_AES:         return "AES";
605
0
    case CIPHER_ALGO_AES192:      return "AES192";
606
0
    case CIPHER_ALGO_AES256:      return "AES256";
607
0
    case CIPHER_ALGO_TWOFISH:     return "TWOFISH";
608
0
    case CIPHER_ALGO_CAMELLIA128: return "CAMELLIA128";
609
0
    case CIPHER_ALGO_CAMELLIA192: return "CAMELLIA192";
610
0
    case CIPHER_ALGO_CAMELLIA256: return "CAMELLIA256";
611
0
    case CIPHER_ALGO_NONE:
612
0
    default: return "?";
613
0
    }
614
0
}
615
616
617
/* Same as openpgp_cipher_algo_name but returns a string in the form
618
 * "ALGO.MODE".  If AEAD is 0 "CFB" is used for the mode.  */
619
const char *
620
openpgp_cipher_algo_mode_name (cipher_algo_t algo, aead_algo_t aead)
621
0
{
622
0
  return map_static_strings ("openpgp_cipher_algo_mode_name", algo, aead,
623
0
                             openpgp_cipher_algo_name (algo),
624
0
                             ".",
625
0
                             aead? openpgp_aead_algo_name (aead) : "CFB",
626
0
                             NULL);
627
0
}
628
629
630
/* Return 0 if ALGO is supported.  Return an error if not. */
631
gpg_error_t
632
openpgp_aead_test_algo (aead_algo_t algo)
633
0
{
634
  /* FIXME: We currently have no easy way to test whether libgcrypt
635
   * implements a mode.  The only way we can do this is to open a
636
   * cipher context with that mode and close it immediately.  That is
637
   * a bit costly.  Thus in case we add another algo we need to look
638
   * at the libgcrypt version and assume nothing has been patched out.  */
639
0
  switch (algo)
640
0
    {
641
0
    case AEAD_ALGO_NONE:
642
0
      break;
643
644
0
    case AEAD_ALGO_GCM:
645
0
      if (RFC9980)
646
0
        return 0;
647
0
      break;
648
649
0
    case AEAD_ALGO_EAX:
650
0
    case AEAD_ALGO_OCB:
651
0
      return 0;
652
0
    }
653
654
0
  return gpg_error (GPG_ERR_INV_CIPHER_MODE);
655
0
}
656
657
658
/* Map the OpenPGP AEAD algorithm with ID ALGO to a string
659
 * representation of the algorithm name.  For unknown algorithm IDs
660
 * this function returns "?".  */
661
const char *
662
openpgp_aead_algo_name (aead_algo_t algo)
663
0
{
664
0
  switch (algo)
665
0
    {
666
0
    case AEAD_ALGO_NONE:  break;
667
0
    case AEAD_ALGO_EAX:   return gnupg_cipher_mode_name (GCRY_CIPHER_MODE_EAX);
668
0
    case AEAD_ALGO_OCB:   return gnupg_cipher_mode_name (GCRY_CIPHER_MODE_OCB);
669
0
    case AEAD_ALGO_GCM:   return gnupg_cipher_mode_name (GCRY_CIPHER_MODE_GCM);
670
0
    }
671
672
0
  return "?";
673
0
}
674
675
676
/* Return information for the AEAD algorithm ALGO.  The corresponding
677
 * Libgcrypt ciphermode is stored at R_MODE and the required number of
678
 * octets for the nonce at R_NONCELEN.  On error and error code is
679
 * returned.  Note that the taglen is always 128 bits.  */
680
gpg_error_t
681
openpgp_aead_algo_info (aead_algo_t algo, enum gcry_cipher_modes *r_mode,
682
                        unsigned int *r_noncelen)
683
0
{
684
0
  switch (algo)
685
0
    {
686
0
    case AEAD_ALGO_OCB:
687
0
      *r_mode = GCRY_CIPHER_MODE_OCB;
688
0
      *r_noncelen = 15;
689
0
      break;
690
691
0
    case AEAD_ALGO_EAX:
692
0
      *r_mode = MY_GCRY_CIPHER_MODE_EAX;
693
0
      *r_noncelen = 16;
694
0
      break;
695
696
0
    case AEAD_ALGO_GCM:
697
0
      if (RFC9980)
698
0
        {
699
0
          *r_mode = GCRY_CIPHER_MODE_GCM;
700
0
          *r_noncelen = 12;
701
0
          break;
702
0
        }
703
      /* FALLTHRU */
704
705
0
    default:
706
0
      log_error ("unsupported AEAD algo %d\n", algo);
707
0
      return gpg_error (GPG_ERR_INV_CIPHER_MODE);
708
0
    }
709
0
  return 0;
710
0
}
711
712
713
/* Return 0 if ALGO is a supported OpenPGP public key algorithm.  */
714
int
715
openpgp_pk_test_algo (pubkey_algo_t algo)
716
4.60k
{
717
4.60k
  return openpgp_pk_test_algo2 (algo, 0);
718
4.60k
}
719
720
721
/* Return 0 if ALGO is a supported OpenPGP public key algorithm and
722
   allows the usage USE.  */
723
int
724
openpgp_pk_test_algo2 (pubkey_algo_t algo, unsigned int use)
725
4.60k
{
726
4.60k
  enum gcry_pk_algos ga = 0;
727
4.60k
  size_t use_buf = use;
728
729
4.60k
  switch (algo)
730
4.60k
    {
731
0
#ifdef GPG_USE_RSA
732
1.02k
    case PUBKEY_ALGO_RSA:       ga = GCRY_PK_RSA;   break;
733
132
    case PUBKEY_ALGO_RSA_E:     ga = GCRY_PK_RSA_E; break;
734
73
    case PUBKEY_ALGO_RSA_S:     ga = GCRY_PK_RSA_S; break;
735
#else
736
    case PUBKEY_ALGO_RSA:       break;
737
    case PUBKEY_ALGO_RSA_E:     break;
738
    case PUBKEY_ALGO_RSA_S:     break;
739
#endif
740
741
1.76k
    case PUBKEY_ALGO_ELGAMAL_E: ga = GCRY_PK_ELG;   break;
742
4
    case PUBKEY_ALGO_DSA:       ga = GCRY_PK_DSA;   break;
743
744
0
#ifdef GPG_USE_ECDH
745
1.30k
    case PUBKEY_ALGO_ECDH:      ga = GCRY_PK_ECC;   break;
746
#else
747
    case PUBKEY_ALGO_ECDH:      break;
748
#endif
749
750
0
#ifdef GPG_USE_ECDSA
751
100
    case PUBKEY_ALGO_ECDSA:     ga = GCRY_PK_ECC;   break;
752
#else
753
    case PUBKEY_ALGO_ECDSA:     break;
754
#endif
755
756
0
#ifdef GPG_USE_EDDSA
757
32
    case PUBKEY_ALGO_EDDSA:     ga = GCRY_PK_ECC;   break;
758
#else
759
    case PUBKEY_ALGO_EDDSA:     break;
760
#endif
761
762
0
    case PUBKEY_ALGO_ELGAMAL:
763
      /* Don't allow type 20 keys unless in rfc2440 mode.  */
764
0
      if (RFC2440)
765
0
        ga = GCRY_PK_ELG;
766
0
      break;
767
768
0
    case PUBKEY_ALGO_KYBER:     ga = GCRY_PK_KEM; break;
769
770
0
    case PUBKEY_ALGO_ED25519:
771
0
      if (RFC9980)
772
0
        ga = GCRY_PK_EDDSA;
773
0
      break;
774
775
0
    case PUBKEY_ALGO_X25519:
776
0
      if (RFC9980)
777
0
        ga = GCRY_PK_KEM;
778
0
      break;
779
780
0
    case PUBKEY_ALGO_MLK768_25519:
781
0
    case PUBKEY_ALGO_MLK768_NP384:
782
2
    case PUBKEY_ALGO_MLK768_BP384:
783
2
    case PUBKEY_ALGO_MLK1024_448:
784
2
    case PUBKEY_ALGO_MLK1024_NP521:
785
2
    case PUBKEY_ALGO_MLK1024_BP512:
786
2
      if (RFC9980)
787
0
        ga = GCRY_PK_KEM;
788
2
      break;
789
790
171
    default:
791
171
      break;
792
4.60k
    }
793
4.60k
  if (!ga)
794
173
    return gpg_error (GPG_ERR_PUBKEY_ALGO);
795
796
  /* Elgamal in OpenPGP used to support signing and Libgcrypt still
797
   * does.  However, we removed the signing capability from gpg ages
798
   * ago.  This function should reflect this so that errors are thrown
799
   * early and not only when we try to sign using Elgamal.  */
800
4.43k
  if (ga == GCRY_PK_ELG && (use & (PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG)))
801
0
    return gpg_error (GPG_ERR_WRONG_PUBKEY_ALGO);
802
803
  /* Now check whether Libgcrypt has support for the algorithm.  */
804
4.43k
  return gcry_pk_algo_info (ga, GCRYCTL_TEST_ALGO, NULL, &use_buf);
805
4.43k
}
806
807
808
int
809
openpgp_pk_algo_usage ( int algo )
810
0
{
811
0
    int use = 0;
812
813
    /* They are hardwired in gpg 1.0. */
814
0
    switch ( algo ) {
815
0
      case PUBKEY_ALGO_RSA:
816
0
          use = (PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG
817
0
                 | PUBKEY_USAGE_ENC | PUBKEY_USAGE_RENC | PUBKEY_USAGE_AUTH);
818
0
          break;
819
0
      case PUBKEY_ALGO_RSA_E:
820
0
      case PUBKEY_ALGO_ECDH:
821
0
          use = PUBKEY_USAGE_ENC | PUBKEY_USAGE_RENC;
822
0
          break;
823
0
      case PUBKEY_ALGO_RSA_S:
824
0
          use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG;
825
0
          break;
826
0
      case PUBKEY_ALGO_ELGAMAL:
827
0
          if (RFC2440)
828
0
             use = PUBKEY_USAGE_ENC | PUBKEY_USAGE_RENC;
829
0
          break;
830
0
      case PUBKEY_ALGO_ELGAMAL_E:
831
0
          use = PUBKEY_USAGE_ENC | PUBKEY_USAGE_RENC;
832
0
          break;
833
0
      case PUBKEY_ALGO_DSA:
834
0
          use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG | PUBKEY_USAGE_AUTH;
835
0
          break;
836
0
      case PUBKEY_ALGO_ECDSA:
837
0
      case PUBKEY_ALGO_EDDSA:
838
0
          use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG | PUBKEY_USAGE_AUTH;
839
0
          break;
840
0
      case PUBKEY_ALGO_ED25519:
841
0
        if (RFC9980)
842
0
          use= PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG | PUBKEY_USAGE_AUTH;
843
0
        break;
844
0
      case PUBKEY_ALGO_X25519:
845
0
        if (RFC9980)
846
0
          use= PUBKEY_USAGE_ENC | PUBKEY_USAGE_RENC;
847
0
        break;
848
849
0
      case PUBKEY_ALGO_KYBER:
850
0
      case PUBKEY_ALGO_MLK768_25519:
851
0
      case PUBKEY_ALGO_MLK768_NP384:
852
0
      case PUBKEY_ALGO_MLK768_BP384:
853
0
      case PUBKEY_ALGO_MLK1024_448:
854
0
      case PUBKEY_ALGO_MLK1024_NP521:
855
0
      case PUBKEY_ALGO_MLK1024_BP512:
856
0
          use = PUBKEY_USAGE_ENC | PUBKEY_USAGE_RENC;
857
0
          break;
858
859
0
      case PUBKEY_ALGO_MLD65_25519:
860
0
      case PUBKEY_ALGO_MLD87_448:
861
0
          use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG;
862
0
          break;
863
864
0
      default:
865
0
          break;
866
0
    }
867
0
    return use;
868
0
}
869
870
/* Map the OpenPGP pubkey algorithm whose ID is contained in ALGO to a
871
   string representation of the algorithm name.  For unknown algorithm
872
   IDs this function returns "?".  */
873
const char *
874
openpgp_pk_algo_name (pubkey_algo_t algo)
875
55.6k
{
876
55.6k
  switch (algo)
877
55.6k
    {
878
1.03k
    case PUBKEY_ALGO_RSA:
879
1.16k
    case PUBKEY_ALGO_RSA_E:
880
1.23k
    case PUBKEY_ALGO_RSA_S:     return "RSA";
881
0
    case PUBKEY_ALGO_ELGAMAL:
882
2.25k
    case PUBKEY_ALGO_ELGAMAL_E: return "ELG";
883
40
    case PUBKEY_ALGO_DSA:       return "DSA";
884
1.30k
    case PUBKEY_ALGO_ECDH:      return "ECDH";
885
100
    case PUBKEY_ALGO_ECDSA:     return "ECDSA";
886
32
    case PUBKEY_ALGO_EDDSA:     return "EDDSA";
887
0
    case PUBKEY_ALGO_KYBER:     return "Kyber";
888
0
    case PUBKEY_ALGO_X25519:    return "ietf25";
889
0
    case PUBKEY_ALGO_ED25519:   return "ietf27";
890
0
    case PUBKEY_ALGO_MLK768_25519: return "mlk768";
891
0
    case PUBKEY_ALGO_MLK768_NP384: return "mlk768_np384";
892
2
    case PUBKEY_ALGO_MLK768_BP384: return "mlk768_bp384";
893
2
    case PUBKEY_ALGO_MLK1024_448:  return "mlk1024";
894
0
    case PUBKEY_ALGO_MLK1024_NP521:return "mlk1024_np521";
895
0
    case PUBKEY_ALGO_MLK1024_BP512:return "mlk1024_bp512";
896
50.7k
    default: return "?";
897
55.6k
    }
898
55.6k
}
899
900
901
/* Explicit mapping of OpenPGP digest algos to Libgcrypt.  */
902
/* FIXME: We do not yet use it everywhere.  */
903
enum gcry_md_algos
904
map_md_openpgp_to_gcry (digest_algo_t algo)
905
115k
{
906
115k
  switch (algo)
907
115k
    {
908
0
#ifdef GPG_USE_MD5
909
973
    case DIGEST_ALGO_MD5:    return GCRY_MD_MD5;
910
#else
911
    case DIGEST_ALGO_MD5:    return 0;
912
#endif
913
914
2.77k
    case DIGEST_ALGO_SHA1:   return GCRY_MD_SHA1;
915
916
0
#ifdef GPG_USE_RMD160
917
6.19k
    case DIGEST_ALGO_RMD160: return GCRY_MD_RMD160;
918
#else
919
    case DIGEST_ALGO_RMD160: return 0;
920
#endif
921
922
0
#ifdef GPG_USE_SHA224
923
639
    case DIGEST_ALGO_SHA224: return GCRY_MD_SHA224;
924
#else
925
    case DIGEST_ALGO_SHA224: return 0;
926
#endif
927
928
912
    case DIGEST_ALGO_SHA256: return GCRY_MD_SHA256;
929
930
0
#ifdef GPG_USE_SHA384
931
2.26k
    case DIGEST_ALGO_SHA384: return GCRY_MD_SHA384;
932
#else
933
    case DIGEST_ALGO_SHA384: return 0;
934
#endif
935
936
0
#ifdef GPG_USE_SHA512
937
2.85k
    case DIGEST_ALGO_SHA512: return GCRY_MD_SHA512;
938
#else
939
    case DIGEST_ALGO_SHA512: return 0;
940
#endif
941
99.2k
    default: return 0;
942
115k
    }
943
115k
}
944
945
946
/* Return 0 if ALGO is suitable and implemented OpenPGP hash
947
   algorithm.  */
948
int
949
openpgp_md_test_algo (digest_algo_t algo)
950
114k
{
951
114k
  enum gcry_md_algos ga;
952
953
114k
  ga = map_md_openpgp_to_gcry (algo);
954
114k
  if (!ga)
955
99.2k
    return gpg_error (GPG_ERR_DIGEST_ALGO);
956
957
15.7k
  return gcry_md_test_algo (ga);
958
114k
}
959
960
961
/* Map the OpenPGP digest algorithm whose ID is contained in ALGO to a
962
   string representation of the algorithm name.  For unknown algorithm
963
   IDs this function returns "?".  */
964
const char *
965
openpgp_md_algo_name (int algo)
966
0
{
967
0
  switch (algo)
968
0
    {
969
0
    case DIGEST_ALGO_MD5:    return "MD5";
970
0
    case DIGEST_ALGO_SHA1:   return "SHA1";
971
0
    case DIGEST_ALGO_RMD160: return "RIPEMD160";
972
0
    case DIGEST_ALGO_SHA256: return "SHA256";
973
0
    case DIGEST_ALGO_SHA384: return "SHA384";
974
0
    case DIGEST_ALGO_SHA512: return "SHA512";
975
0
    case DIGEST_ALGO_SHA224: return "SHA224";
976
0
    }
977
0
  return "?";
978
0
}
979
980
981
static unsigned long
982
get_signature_count (PKT_public_key *pk)
983
0
{
984
#ifdef ENABLE_CARD_SUPPORT
985
  struct agent_card_info_s info;
986
987
  (void)pk;
988
  if (!agent_scd_getattr ("SIG-COUNTER",&info))
989
    return info.sig_counter;
990
  else
991
    return 0;
992
#else
993
0
  (void)pk;
994
0
  return 0;
995
0
#endif
996
0
}
997
998
/* Expand %-strings.  Returns a string which must be xfreed.  Returns
999
   NULL if the string cannot be expanded (too large). */
1000
char *
1001
pct_expando (ctrl_t ctrl, const char *string,struct expando_args *args)
1002
0
{
1003
0
  const char *ch=string;
1004
0
  int idx=0,maxlen=0,done=0;
1005
0
  u32 pk_keyid[2]={0,0},sk_keyid[2]={0,0};
1006
0
  char *ret=NULL;
1007
1008
  /* The parser below would return NULL for an empty string, thus we
1009
   * catch it here.  Also catch NULL here. */
1010
0
  if (!string || !*string)
1011
0
    return xstrdup ("");
1012
1013
0
  if(args->pk)
1014
0
    keyid_from_pk(args->pk,pk_keyid);
1015
1016
0
  if(args->pksk)
1017
0
    keyid_from_pk (args->pksk, sk_keyid);
1018
1019
  /* This is used so that %k works in photoid command strings in
1020
     --list-secret-keys (which of course has a sk, but no pk). */
1021
0
  if(!args->pk && args->pksk)
1022
0
    keyid_from_pk (args->pksk, pk_keyid);
1023
1024
0
  while(*ch!='\0')
1025
0
    {
1026
0
      if(!done)
1027
0
  {
1028
    /* 8192 is way bigger than we'll need here */
1029
0
    if(maxlen>=8192)
1030
0
      goto fail;
1031
1032
0
    maxlen+=1024;
1033
0
    ret=xrealloc(ret,maxlen);
1034
0
  }
1035
1036
0
      done=0;
1037
1038
0
      if(*ch=='%')
1039
0
  {
1040
0
    switch(*(ch+1))
1041
0
      {
1042
0
      case 's': /* short key id */
1043
0
        if(idx+8<maxlen)
1044
0
    {
1045
0
      sprintf(&ret[idx],"%08lX",(ulong)sk_keyid[1]);
1046
0
      idx+=8;
1047
0
      done=1;
1048
0
    }
1049
0
        break;
1050
1051
0
      case 'S': /* long key id */
1052
0
        if(idx+16<maxlen)
1053
0
    {
1054
0
      sprintf(&ret[idx],"%08lX%08lX",
1055
0
        (ulong)sk_keyid[0],(ulong)sk_keyid[1]);
1056
0
      idx+=16;
1057
0
      done=1;
1058
0
    }
1059
0
        break;
1060
1061
0
      case 'k': /* short key id */
1062
0
        if(idx+8<maxlen)
1063
0
    {
1064
0
      sprintf(&ret[idx],"%08lX",(ulong)pk_keyid[1]);
1065
0
      idx+=8;
1066
0
      done=1;
1067
0
    }
1068
0
        break;
1069
1070
0
      case 'K': /* long key id */
1071
0
        if(idx+16<maxlen)
1072
0
    {
1073
0
      sprintf(&ret[idx],"%08lX%08lX",
1074
0
        (ulong)pk_keyid[0],(ulong)pk_keyid[1]);
1075
0
      idx+=16;
1076
0
      done=1;
1077
0
    }
1078
0
        break;
1079
1080
0
      case 'U': /* z-base-32 encoded user id hash. */
1081
0
              if (args->namehash)
1082
0
                {
1083
0
                  char *tmp = zb32_encode (args->namehash, 8*20);
1084
0
                  if (tmp)
1085
0
                    {
1086
0
                      if (idx + strlen (tmp) < maxlen)
1087
0
                        {
1088
0
                          strcpy (ret+idx, tmp);
1089
0
                          idx += strlen (tmp);
1090
0
                        }
1091
0
                      xfree (tmp);
1092
0
                      done = 1;
1093
0
                    }
1094
0
                }
1095
0
        break;
1096
1097
0
      case 'c': /* signature count from card, if any. */
1098
0
        if(idx+10<maxlen)
1099
0
    {
1100
0
      sprintf (&ret[idx],"%lu", get_signature_count (args->pksk));
1101
0
      idx+=strlen(&ret[idx]);
1102
0
      done=1;
1103
0
    }
1104
0
        break;
1105
1106
0
      case 'f': /* Fingerprint of key being signed */
1107
0
      case 'p': /* Fingerprint of the primary key making the signature. */
1108
0
      case 'g': /* Fingerprint of the key making the signature.  */
1109
0
        {
1110
0
    byte array[MAX_FINGERPRINT_LEN];
1111
0
    size_t len;
1112
0
    int i;
1113
1114
0
    if ((*(ch+1))=='f' && args->pk)
1115
0
      fingerprint_from_pk (args->pk, array, &len);
1116
0
    else if ((*(ch+1))=='p' && args->pksk)
1117
0
      {
1118
0
        if(args->pksk->flags.primary)
1119
0
          fingerprint_from_pk (args->pksk, array, &len);
1120
0
        else if (args->pksk->main_keyid[0]
1121
0
                             || args->pksk->main_keyid[1])
1122
0
          {
1123
                        /* Not the primary key: Find the fingerprint
1124
                           of the primary key.  */
1125
0
      PKT_public_key *pk=
1126
0
        xmalloc_clear(sizeof(PKT_public_key));
1127
1128
0
      if (!get_pubkey_fast (ctrl, pk,args->pksk->main_keyid))
1129
0
        fingerprint_from_pk (pk, array, &len);
1130
0
      else
1131
0
        memset (array, 0, (len=MAX_FINGERPRINT_LEN));
1132
0
      free_public_key (pk);
1133
0
          }
1134
0
        else /* Oops: info about the primary key missing.  */
1135
0
          memset(array,0,(len=MAX_FINGERPRINT_LEN));
1136
0
      }
1137
0
    else if((*(ch+1))=='g' && args->pksk)
1138
0
      fingerprint_from_pk (args->pksk, array, &len);
1139
0
    else
1140
0
      memset(array,0,(len=MAX_FINGERPRINT_LEN));
1141
1142
0
    if(idx+(len*2)<maxlen)
1143
0
      {
1144
0
        for(i=0;i<len;i++)
1145
0
          {
1146
0
      sprintf(&ret[idx],"%02X",array[i]);
1147
0
      idx+=2;
1148
0
          }
1149
0
        done=1;
1150
0
      }
1151
0
        }
1152
0
        break;
1153
1154
0
      case 'v': /* validity letters */
1155
0
        if(args->validity_info && idx+1<maxlen)
1156
0
    {
1157
0
      ret[idx++]=args->validity_info;
1158
0
      ret[idx]='\0';
1159
0
      done=1;
1160
0
    }
1161
0
        break;
1162
1163
        /* The text string types */
1164
0
      case 't':
1165
0
      case 'T':
1166
0
      case 'V':
1167
0
        {
1168
0
    const char *str=NULL;
1169
1170
0
    switch(*(ch+1))
1171
0
      {
1172
0
      case 't': /* e.g. "jpg" */
1173
0
        str=image_type_to_string(args->imagetype,0);
1174
0
        break;
1175
1176
0
      case 'T': /* e.g. "image/jpeg" */
1177
0
        str=image_type_to_string(args->imagetype,2);
1178
0
        break;
1179
1180
0
      case 'V': /* e.g. "full", "expired", etc. */
1181
0
        str=args->validity_string;
1182
0
        break;
1183
0
      }
1184
1185
0
    if(str && idx+strlen(str)<maxlen)
1186
0
      {
1187
0
        strcpy(&ret[idx],str);
1188
0
        idx+=strlen(str);
1189
0
        done=1;
1190
0
      }
1191
0
        }
1192
0
        break;
1193
1194
0
      case '%':
1195
0
        if(idx+1<maxlen)
1196
0
    {
1197
0
      ret[idx++]='%';
1198
0
      ret[idx]='\0';
1199
0
      done=1;
1200
0
    }
1201
0
        break;
1202
1203
        /* Any unknown %-keys (like %i, %o, %I, and %O) are
1204
     passed through for later expansion.  Note this also
1205
     handles the case where the last character in the
1206
     string is a '%' - the terminating \0 will end up here
1207
     and properly terminate the string. */
1208
0
      default:
1209
0
        if(idx+2<maxlen)
1210
0
    {
1211
0
      ret[idx++]='%';
1212
0
      ret[idx++]=*(ch+1);
1213
0
      ret[idx]='\0';
1214
0
      done=1;
1215
0
    }
1216
0
        break;
1217
0
        }
1218
1219
0
    if(done)
1220
0
      ch++;
1221
0
  }
1222
0
      else
1223
0
  {
1224
0
    if(idx+1<maxlen)
1225
0
      {
1226
0
        ret[idx++]=*ch;
1227
0
        ret[idx]='\0';
1228
0
        done=1;
1229
0
      }
1230
0
  }
1231
1232
0
      if(done)
1233
0
  ch++;
1234
0
    }
1235
1236
0
  return ret;
1237
1238
0
 fail:
1239
0
  xfree(ret);
1240
0
  return NULL;
1241
0
}
1242
1243
void
1244
deprecated_warning(const char *configname,unsigned int configlineno,
1245
       const char *option,const char *repl1,const char *repl2)
1246
0
{
1247
0
  if(configname)
1248
0
    {
1249
0
      if(strncmp("--",option,2)==0)
1250
0
  option+=2;
1251
1252
0
      if(strncmp("--",repl1,2)==0)
1253
0
  repl1+=2;
1254
1255
0
      log_info(_("%s:%d: deprecated option \"%s\"\n"),
1256
0
         configname,configlineno,option);
1257
0
    }
1258
0
  else
1259
0
    log_info(_("WARNING: \"%s\" is a deprecated option\n"),option);
1260
1261
0
  log_info(_("please use \"%s%s\" instead\n"),repl1,repl2);
1262
0
}
1263
1264
1265
void
1266
deprecated_command (const char *name)
1267
0
{
1268
0
  log_info(_("WARNING: \"%s\" is a deprecated command - do not use it\n"),
1269
0
           name);
1270
0
}
1271
1272
1273
void
1274
obsolete_scdaemon_option (const char *configname, unsigned int configlineno,
1275
                          const char *name)
1276
0
{
1277
0
  if (configname)
1278
0
    log_info (_("%s:%u: \"%s\" is obsolete in this file"
1279
0
                " - it only has effect in %s\n"),
1280
0
              configname, configlineno, name, SCDAEMON_NAME EXTSEP_S "conf");
1281
0
  else
1282
0
    log_info (_("WARNING: \"%s%s\" is an obsolete option"
1283
0
                " - it has no effect except on %s\n"),
1284
0
              "--", name, SCDAEMON_NAME);
1285
0
}
1286
1287
1288
/*
1289
 * Wrapper around gcry_cipher_map_name to provide a fallback using the
1290
 * "Sn" syntax as used by the preference strings.  Note that only the
1291
 * second syntax does a check on the actual availibily of the
1292
 * algorithm.  That might make a difference in case Libgcrypt is
1293
 * running in FIPS mode.
1294
 */
1295
int
1296
string_to_cipher_algo (const char *string)
1297
0
{
1298
0
  int val;
1299
1300
0
  val = map_cipher_gcry_to_openpgp (gcry_cipher_map_name (string));
1301
0
  if (!val && string && (string[0]=='S' || string[0]=='s'))
1302
0
    {
1303
0
      char *endptr;
1304
1305
0
      string++;
1306
0
      val = strtol (string, &endptr, 10);
1307
0
      if (!*string || *endptr || openpgp_cipher_test_algo (val))
1308
0
        val = 0;
1309
0
    }
1310
1311
0
  return val;
1312
0
}
1313
1314
1315
/*
1316
 * Map an AEAD mode string to a an AEAD algorithm number as defined by
1317
 * rfc4880bis.  Also support the "An" syntax as used by the preference
1318
 * strings.
1319
 */
1320
aead_algo_t
1321
string_to_aead_algo (const char *string)
1322
0
{
1323
0
  int result;
1324
1325
0
  if (!string)
1326
0
    result = 0;
1327
0
  else if (!ascii_strcasecmp (string, "EAX"))
1328
0
    result = 1;
1329
0
  else if (!ascii_strcasecmp (string, "OCB"))
1330
0
    result = 2;
1331
0
  else if ((string[0]=='A' || string[0]=='a'))
1332
0
    {
1333
0
      char *endptr;
1334
1335
0
      string++;
1336
0
      result = strtol (string, &endptr, 10);
1337
0
      if (!*string || *endptr || result < 1 || result > 2)
1338
0
        result = 0;
1339
0
    }
1340
0
  else
1341
0
    result = 0;
1342
1343
0
  return result;
1344
0
}
1345
1346
1347
/*
1348
 * Wrapper around gcry_md_map_name to provide a fallback using the
1349
 * "Hn" syntax as used by the preference strings.
1350
 */
1351
int
1352
string_to_digest_algo (const char *string)
1353
0
{
1354
0
  int val;
1355
1356
  /* FIXME: We should make use of our wrapper function and not assume
1357
     that there is a 1 to 1 mapping between OpenPGP and Libgcrypt.  */
1358
0
  val = gcry_md_map_name (string);
1359
0
  if (!val && string && (string[0]=='H' || string[0]=='h'))
1360
0
    {
1361
0
      char *endptr;
1362
1363
0
      string++;
1364
0
      val = strtol (string, &endptr, 10);
1365
0
      if (!*string || *endptr || openpgp_md_test_algo (val))
1366
0
        val = 0;
1367
0
    }
1368
1369
0
  return val;
1370
0
}
1371
1372
1373
1374
const char *
1375
compress_algo_to_string(int algo)
1376
0
{
1377
0
  const char *s=NULL;
1378
1379
0
  switch(algo)
1380
0
    {
1381
0
    case COMPRESS_ALGO_NONE:
1382
0
      s=_("Uncompressed");
1383
0
      break;
1384
1385
0
    case COMPRESS_ALGO_ZIP:
1386
0
      s="ZIP";
1387
0
      break;
1388
1389
0
    case COMPRESS_ALGO_ZLIB:
1390
0
      s="ZLIB";
1391
0
      break;
1392
1393
#ifdef HAVE_BZIP2
1394
    case COMPRESS_ALGO_BZIP2:
1395
      s="BZIP2";
1396
      break;
1397
#endif
1398
0
    }
1399
1400
0
  return s;
1401
0
}
1402
1403
int
1404
string_to_compress_algo(const char *string)
1405
0
{
1406
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
1407
0
  if(match_multistr(_("uncompressed|none"),string))
1408
0
    return 0;
1409
0
  else if(ascii_strcasecmp(string,"uncompressed")==0)
1410
0
    return 0;
1411
0
  else if(ascii_strcasecmp(string,"none")==0)
1412
0
    return 0;
1413
0
  else if(ascii_strcasecmp(string,"zip")==0)
1414
0
    return 1;
1415
0
  else if(ascii_strcasecmp(string,"zlib")==0)
1416
0
    return 2;
1417
#ifdef HAVE_BZIP2
1418
  else if(ascii_strcasecmp(string,"bzip2")==0)
1419
    return 3;
1420
#endif
1421
0
  else if(ascii_strcasecmp(string,"z0")==0)
1422
0
    return 0;
1423
0
  else if(ascii_strcasecmp(string,"z1")==0)
1424
0
    return 1;
1425
0
  else if(ascii_strcasecmp(string,"z2")==0)
1426
0
    return 2;
1427
#ifdef HAVE_BZIP2
1428
  else if(ascii_strcasecmp(string,"z3")==0)
1429
    return 3;
1430
#endif
1431
0
  else
1432
0
    return -1;
1433
0
}
1434
1435
int
1436
check_compress_algo(int algo)
1437
311k
{
1438
311k
  switch (algo)
1439
311k
    {
1440
311k
    case 0: return 0;
1441
#ifdef HAVE_ZIP
1442
    case 1:
1443
    case 2: return 0;
1444
#endif
1445
#ifdef HAVE_BZIP2
1446
    case 3: return 0;
1447
#endif
1448
187
    default: return GPG_ERR_COMPR_ALGO;
1449
311k
    }
1450
311k
}
1451
1452
int
1453
default_cipher_algo(void)
1454
0
{
1455
0
  if(opt.def_cipher_algo)
1456
0
    return opt.def_cipher_algo;
1457
0
  else if(opt.personal_cipher_prefs)
1458
0
    return opt.personal_cipher_prefs[0].value;
1459
0
  else
1460
0
    return opt.s2k_cipher_algo;
1461
0
}
1462
1463
1464
/* There is no default_digest_algo function, but see
1465
   sign.c:hash_for() */
1466
1467
int
1468
default_compress_algo(void)
1469
0
{
1470
0
  if(opt.compress_algo!=-1)
1471
0
    return opt.compress_algo;
1472
0
  else if(opt.personal_compress_prefs)
1473
0
    return opt.personal_compress_prefs[0].value;
1474
0
  else
1475
0
    return DEFAULT_COMPRESS_ALGO;
1476
0
}
1477
1478
1479
void
1480
compliance_failure(void)
1481
0
{
1482
0
  char *ver="???";
1483
1484
0
  switch(opt.compliance)
1485
0
    {
1486
0
    case CO_GNUPG:
1487
0
      ver="GnuPG";
1488
0
      break;
1489
1490
0
    case CO_RFC4880:
1491
0
      ver="OpenPGP";
1492
0
      break;
1493
1494
0
    case CO_RFC2440:
1495
0
      ver="OpenPGP (older)";
1496
0
      break;
1497
1498
0
    case CO_PGP7:
1499
0
      ver="PGP 7.x";
1500
0
      break;
1501
1502
0
    case CO_PGP8:
1503
0
      ver="PGP 8.x";
1504
0
      break;
1505
1506
0
    case CO_DE_VS:
1507
0
      ver="DE-VS applications";
1508
0
      break;
1509
1510
0
    case CO_FIPS:
1511
0
      ver="FIPS applications";
1512
0
      break;
1513
0
    }
1514
1515
0
  log_info(_("this message may not be usable by %s\n"),ver);
1516
0
  opt.compliance=CO_GNUPG;
1517
0
}
1518
1519
/* Break a string into successive option pieces.  Accepts single word
1520
   options and key=value argument options. */
1521
char *
1522
optsep(char **stringp)
1523
0
{
1524
0
  char *tok,*end;
1525
1526
0
  tok=*stringp;
1527
0
  if(tok)
1528
0
    {
1529
0
      end=strpbrk(tok," ,=");
1530
0
      if(end)
1531
0
  {
1532
0
    int sawequals=0;
1533
0
    char *ptr=end;
1534
1535
    /* what we need to do now is scan along starting with *end,
1536
       If the next character we see (ignoring spaces) is an =
1537
       sign, then there is an argument. */
1538
1539
0
    while(*ptr)
1540
0
      {
1541
0
        if(*ptr=='=')
1542
0
    sawequals=1;
1543
0
        else if(*ptr!=' ')
1544
0
    break;
1545
0
        ptr++;
1546
0
      }
1547
1548
    /* There is an argument, so grab that too.  At this point,
1549
       ptr points to the first character of the argument. */
1550
0
    if(sawequals)
1551
0
      {
1552
        /* Is it a quoted argument? */
1553
0
        if(*ptr=='"')
1554
0
    {
1555
0
      ptr++;
1556
0
      end=strchr(ptr,'"');
1557
0
      if(end)
1558
0
        end++;
1559
0
    }
1560
0
        else
1561
0
    end=strpbrk(ptr," ,");
1562
0
      }
1563
1564
0
    if(end && *end)
1565
0
      {
1566
0
        *end='\0';
1567
0
        *stringp=end+1;
1568
0
      }
1569
0
    else
1570
0
      *stringp=NULL;
1571
0
  }
1572
0
      else
1573
0
  *stringp=NULL;
1574
0
    }
1575
1576
0
  return tok;
1577
0
}
1578
1579
/* Breaks an option value into key and value.  Returns NULL if there
1580
   is no value.  Note that "string" is modified to remove the =value
1581
   part. */
1582
char *
1583
argsplit(char *string)
1584
0
{
1585
0
  char *equals,*arg=NULL;
1586
1587
0
  equals=strchr(string,'=');
1588
0
  if(equals)
1589
0
    {
1590
0
      char *quote,*space;
1591
1592
0
      *equals='\0';
1593
0
      arg=equals+1;
1594
1595
      /* Quoted arg? */
1596
0
      quote=strchr(arg,'"');
1597
0
      if(quote)
1598
0
  {
1599
0
    arg=quote+1;
1600
1601
0
    quote=strchr(arg,'"');
1602
0
    if(quote)
1603
0
      *quote='\0';
1604
0
  }
1605
0
      else
1606
0
  {
1607
0
    size_t spaces;
1608
1609
    /* Trim leading spaces off of the arg */
1610
0
    spaces=strspn(arg," ");
1611
0
    arg+=spaces;
1612
0
  }
1613
1614
      /* Trim tailing spaces off of the tag */
1615
0
      space=strchr(string,' ');
1616
0
      if(space)
1617
0
  *space='\0';
1618
0
    }
1619
1620
0
  return arg;
1621
0
}
1622
1623
/* Return the length of the initial token, leaving off any
1624
   argument. */
1625
static size_t
1626
optlen(const char *s)
1627
0
{
1628
0
  char *end=strpbrk(s," =");
1629
1630
0
  if(end)
1631
0
    return end-s;
1632
0
  else
1633
0
    return strlen(s);
1634
0
}
1635
1636
1637
/* Note: This function returns true on success.  */
1638
int
1639
parse_options(char *str,unsigned int *options,
1640
        struct parse_options *opts,int noisy)
1641
0
{
1642
0
  char *tok;
1643
1644
0
  if (str && (!strcmp (str, "help")
1645
0
              || !strcmp (str, "full-help") || !strcmp (str, "fullhelp")))
1646
0
    {
1647
0
      int i,maxlen=0;
1648
0
      int full = *str == 'f';
1649
0
      int set;
1650
1651
      /* Figure out the longest option name so we can line these up
1652
   neatly. */
1653
0
      for(i=0;opts[i].name;i++)
1654
0
  if((full || opts[i].help) && maxlen<strlen(opts[i].name))
1655
0
    maxlen=strlen(opts[i].name);
1656
1657
0
      for(i=0;opts[i].name;i++)
1658
0
        if(opts[i].help)
1659
0
          {
1660
0
            set = (*options & opts[i].bit);
1661
0
            es_printf("%s%*s%s%s%s%s\n",opts[i].name,
1662
0
                      maxlen+2-(int)strlen(opts[i].name),"",_(opts[i].help),
1663
0
                      set?"  [":"", set? _("enabled"):"", set?"]":"");
1664
0
          }
1665
1666
0
      if (full)
1667
0
        for (i=0; opts[i].name; i++)
1668
0
          if(!opts[i].help)
1669
0
            {
1670
0
              set = (*options & opts[i].bit);
1671
0
              es_printf("%s%*s%s%s%s\n",opts[i].name,
1672
0
                        set? (maxlen+2-(int)strlen(opts[i].name)):0,"",
1673
0
                        set?"[":"", set? _("enabled"):"", set?"]":"");
1674
0
            }
1675
1676
0
      g10_exit(0);
1677
0
    }
1678
1679
0
  while((tok=optsep(&str)))
1680
0
    {
1681
0
      int i,rev=0;
1682
0
      char *otok=tok;
1683
1684
0
      if(tok[0]=='\0')
1685
0
  continue;
1686
1687
0
      if(ascii_strncasecmp("no-",tok,3)==0)
1688
0
  {
1689
0
    rev=1;
1690
0
    tok+=3;
1691
0
  }
1692
1693
0
      for(i=0;opts[i].name;i++)
1694
0
  {
1695
0
    size_t toklen=optlen(tok);
1696
1697
0
    if(ascii_strncasecmp(opts[i].name,tok,toklen)==0)
1698
0
      {
1699
        /* We have a match, but it might be incomplete */
1700
0
        if(toklen!=strlen(opts[i].name))
1701
0
    {
1702
0
      int j;
1703
1704
0
      for(j=i+1;opts[j].name;j++)
1705
0
        {
1706
0
          if(ascii_strncasecmp(opts[j].name,tok,toklen)==0)
1707
0
      {
1708
0
        if(noisy)
1709
0
          log_info(_("ambiguous option '%s'\n"),otok);
1710
0
        return 0;
1711
0
      }
1712
0
        }
1713
0
    }
1714
1715
0
        if(rev)
1716
0
    {
1717
0
      *options&=~opts[i].bit;
1718
0
      if(opts[i].value)
1719
0
        *opts[i].value=NULL;
1720
0
    }
1721
0
        else
1722
0
    {
1723
0
      *options|=opts[i].bit;
1724
0
      if(opts[i].value)
1725
0
        *opts[i].value=argsplit(tok);
1726
0
    }
1727
0
        break;
1728
0
      }
1729
0
  }
1730
1731
0
      if(!opts[i].name)
1732
0
  {
1733
0
    if(noisy)
1734
0
      log_info(_("unknown option '%s'\n"),otok);
1735
0
    return 0;
1736
0
  }
1737
0
    }
1738
1739
0
  return 1;
1740
0
}
1741
1742
1743
/* Similar to access(2), but uses PATH to find the file. */
1744
int
1745
path_access(const char *file,int mode)
1746
0
{
1747
0
  char *envpath;
1748
0
  int ret=-1;
1749
1750
0
  envpath=getenv("PATH");
1751
1752
0
  if(!envpath
1753
#ifdef HAVE_DRIVE_LETTERS
1754
     || (((file[0]>='A' && file[0]<='Z')
1755
    || (file[0]>='a' && file[0]<='z'))
1756
   && file[1]==':')
1757
#else
1758
0
     || file[0]=='/'
1759
0
#endif
1760
0
     )
1761
0
    return access(file,mode);
1762
0
  else
1763
0
    {
1764
      /* At least as large as, but most often larger than we need. */
1765
0
      char *buffer=xmalloc(strlen(envpath)+1+strlen(file)+1);
1766
0
      char *split,*item,*path=xstrdup(envpath);
1767
1768
0
      split=path;
1769
1770
0
      while((item=strsep(&split,PATHSEP_S)))
1771
0
  {
1772
0
    strcpy(buffer,item);
1773
0
    strcat(buffer,"/");
1774
0
    strcat(buffer,file);
1775
0
    ret=access(buffer,mode);
1776
0
    if(ret==0)
1777
0
      break;
1778
0
  }
1779
1780
0
      xfree(path);
1781
0
      xfree(buffer);
1782
0
    }
1783
1784
0
  return ret;
1785
0
}
1786
1787
1788

1789
/* Return the number of public key parameters as used by OpenPGP.  */
1790
int
1791
pubkey_get_npkey (pubkey_algo_t algo)
1792
145k
{
1793
145k
  switch (algo)
1794
145k
    {
1795
35.6k
    case PUBKEY_ALGO_RSA:
1796
36.1k
    case PUBKEY_ALGO_RSA_E:
1797
36.2k
    case PUBKEY_ALGO_RSA_S:     return 2;
1798
4.90k
    case PUBKEY_ALGO_ELGAMAL_E: return 3;
1799
32
    case PUBKEY_ALGO_DSA:       return 4;
1800
1.97k
    case PUBKEY_ALGO_ECDH:      return 3;
1801
2.02k
    case PUBKEY_ALGO_ECDSA:     return 2;
1802
727
    case PUBKEY_ALGO_ELGAMAL:   return 3;
1803
2.03k
    case PUBKEY_ALGO_EDDSA:     return 2;
1804
1.06k
    case PUBKEY_ALGO_KYBER:     return 3;
1805
2
    case PUBKEY_ALGO_X25519:       return 1;
1806
0
    case PUBKEY_ALGO_ED25519:      return 1;
1807
96.1k
    default:
1808
96.1k
      if (IS_PUBKEY_ALGO_MLK (algo))
1809
2.59k
        return 2;
1810
93.5k
      return 0;
1811
145k
    }
1812
145k
}
1813
1814
1815
/* Return the number of secret key parameters as used by OpenPGP.  */
1816
int
1817
pubkey_get_nskey (pubkey_algo_t algo)
1818
78.9k
{
1819
78.9k
  switch (algo)
1820
78.9k
    {
1821
57.9k
    case PUBKEY_ALGO_RSA:
1822
58.9k
    case PUBKEY_ALGO_RSA_E:
1823
58.9k
    case PUBKEY_ALGO_RSA_S:     return 6;
1824
1.09k
    case PUBKEY_ALGO_ELGAMAL_E: return 4;
1825
40
    case PUBKEY_ALGO_DSA:       return 5;
1826
444
    case PUBKEY_ALGO_ECDH:      return 4;
1827
1.68k
    case PUBKEY_ALGO_ECDSA:     return 3;
1828
1.10k
    case PUBKEY_ALGO_ELGAMAL:   return 4;
1829
3.68k
    case PUBKEY_ALGO_EDDSA:     return 3;
1830
614
    case PUBKEY_ALGO_KYBER:     return 5;
1831
4
    case PUBKEY_ALGO_X25519:    return 2;
1832
0
    case PUBKEY_ALGO_ED25519:   return 2;
1833
11.2k
    default:
1834
11.2k
      if (IS_PUBKEY_ALGO_MLK (algo))
1835
8
        return 4;
1836
11.2k
      return 0;
1837
78.9k
    }
1838
78.9k
}
1839
1840
/* Temporary helper. */
1841
int
1842
pubkey_get_nsig (pubkey_algo_t algo)
1843
437k
{
1844
437k
  switch (algo)
1845
437k
    {
1846
3.10k
    case PUBKEY_ALGO_RSA:
1847
4.35k
    case PUBKEY_ALGO_RSA_E:
1848
4.60k
    case PUBKEY_ALGO_RSA_S:     return 1;
1849
6.39k
    case PUBKEY_ALGO_ELGAMAL_E: return 0;
1850
2.03k
    case PUBKEY_ALGO_DSA:       return 2;
1851
2.84k
    case PUBKEY_ALGO_ECDH:      return 0;
1852
672
    case PUBKEY_ALGO_ECDSA:     return 2;
1853
27
    case PUBKEY_ALGO_ELGAMAL:   return 2;
1854
462
    case PUBKEY_ALGO_EDDSA:     return 2;
1855
0
    case PUBKEY_ALGO_ED25519:   return 1;
1856
420k
    default: return 0;
1857
437k
    }
1858
437k
}
1859
1860
1861
/* Temporary helper. */
1862
int
1863
pubkey_get_nenc (pubkey_algo_t algo)
1864
22.6k
{
1865
22.6k
  switch (algo)
1866
22.6k
    {
1867
56
    case PUBKEY_ALGO_RSA:
1868
82
    case PUBKEY_ALGO_RSA_E:
1869
104
    case PUBKEY_ALGO_RSA_S:     return 1;
1870
3.22k
    case PUBKEY_ALGO_ELGAMAL_E: return 2;
1871
0
    case PUBKEY_ALGO_DSA:       return 0;
1872
10.3k
    case PUBKEY_ALGO_ECDH:      return 2;
1873
4
    case PUBKEY_ALGO_ECDSA:     return 0;
1874
40
    case PUBKEY_ALGO_ELGAMAL:   return 2;
1875
0
    case PUBKEY_ALGO_EDDSA:     return 0;
1876
44
    case PUBKEY_ALGO_KYBER:     return 3;
1877
0
    case PUBKEY_ALGO_X25519:    return 2;
1878
8.93k
    default:
1879
8.93k
      if (IS_PUBKEY_ALGO_MLK (algo))
1880
0
        return 3;
1881
8.93k
      return 0;
1882
22.6k
    }
1883
22.6k
}
1884
1885
1886
/* Temporary helper. */
1887
unsigned int
1888
pubkey_nbits( int algo, gcry_mpi_t *key )
1889
0
{
1890
0
  int rc, nbits;
1891
0
  gcry_sexp_t sexp;
1892
1893
0
  if (algo == PUBKEY_ALGO_DSA
1894
0
      && key[0] && key[1] && key[2] && key[3])
1895
0
    {
1896
0
      rc = gcry_sexp_build (&sexp, NULL,
1897
0
                            "(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
1898
0
                            key[0], key[1], key[2], key[3] );
1899
0
    }
1900
0
  else if ((algo == PUBKEY_ALGO_ELGAMAL || algo == PUBKEY_ALGO_ELGAMAL_E)
1901
0
           && key[0] && key[1] && key[2])
1902
0
    {
1903
0
      rc = gcry_sexp_build (&sexp, NULL,
1904
0
                            "(public-key(elg(p%m)(g%m)(y%m)))",
1905
0
                            key[0], key[1], key[2] );
1906
0
    }
1907
0
  else if (is_RSA (algo)
1908
0
           && key[0] && key[1])
1909
0
    {
1910
0
      rc = gcry_sexp_build (&sexp, NULL,
1911
0
                            "(public-key(rsa(n%m)(e%m)))",
1912
0
                            key[0], key[1] );
1913
0
    }
1914
0
  else if ((algo == PUBKEY_ALGO_ECDSA || algo == PUBKEY_ALGO_ECDH
1915
0
            || algo == PUBKEY_ALGO_EDDSA)
1916
0
           && key[0] && key[1])
1917
0
    {
1918
0
      char *curve = openpgp_oid_to_str (key[0]);
1919
0
      if (!curve)
1920
0
        rc = gpg_error_from_syserror ();
1921
0
      else
1922
0
        {
1923
0
          rc = gcry_sexp_build (&sexp, NULL,
1924
0
                                "(public-key(ecc(curve%s)(q%m)))",
1925
0
                                curve, key[1]);
1926
0
          xfree (curve);
1927
0
        }
1928
0
    }
1929
0
  else
1930
0
    return 0;
1931
1932
0
  if (rc)
1933
0
    BUG ();
1934
1935
0
  nbits = gcry_pk_get_nbits (sexp);
1936
0
  gcry_sexp_release (sexp);
1937
0
  return nbits;
1938
0
}
1939
1940
1941
1942
int
1943
mpi_print (estream_t fp, gcry_mpi_t a, int mode)
1944
0
{
1945
0
  int n = 0;
1946
0
  size_t nwritten;
1947
1948
0
  if (!a)
1949
0
    return es_fprintf (fp, "[MPI_NULL]");
1950
0
  if (!mode)
1951
0
    {
1952
0
      unsigned int n1;
1953
0
      n1 = gcry_mpi_get_nbits(a);
1954
0
      n += es_fprintf (fp, "[%u bits]", n1);
1955
0
    }
1956
0
  else if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
1957
0
    {
1958
0
      unsigned int nbits;
1959
0
      unsigned char *p = gcry_mpi_get_opaque (a, &nbits);
1960
0
      if (!p)
1961
0
        n += es_fprintf (fp, "[invalid opaque value]");
1962
0
      else
1963
0
        {
1964
0
          if (!es_write_hexstring (fp, p, (nbits + 7)/8, 0, &nwritten))
1965
0
            n += nwritten;
1966
0
        }
1967
0
    }
1968
0
  else
1969
0
    {
1970
0
      unsigned char *buffer;
1971
0
      size_t buflen;
1972
1973
0
      if (gcry_mpi_aprint (GCRYMPI_FMT_USG, &buffer, &buflen, a))
1974
0
        BUG ();
1975
0
      if (!es_write_hexstring (fp, buffer, buflen, 0, &nwritten))
1976
0
        n += nwritten;
1977
0
      gcry_free (buffer);
1978
0
    }
1979
0
  return n;
1980
0
}
1981
1982
1983
/* pkey[1] or skey[1] is Q for ECDSA, which is an uncompressed point,
1984
   i.e.  04 <x> <y> */
1985
unsigned int
1986
ecdsa_qbits_from_Q (unsigned int qbits)
1987
0
{
1988
0
  if ((qbits%8) > 3)
1989
0
    {
1990
0
      log_error (_("ECDSA public key is expected to be in SEC encoding "
1991
0
                   "multiple of 8 bits\n"));
1992
0
      return 0;
1993
0
    }
1994
0
  qbits -= qbits%8;
1995
0
  qbits /= 2;
1996
0
  return qbits;
1997
0
}
1998
1999
2000
/* Ignore signatures and certifications made over certain digest
2001
 * algorithms by default, MD5 is considered weak.  This allows users
2002
 * to deprecate support for other algorithms as well.
2003
 */
2004
void
2005
additional_weak_digest (const char* digestname)
2006
0
{
2007
0
  struct weakhash *weak = NULL;
2008
0
  const enum gcry_md_algos algo = string_to_digest_algo(digestname);
2009
2010
0
  if (algo == GCRY_MD_NONE)
2011
0
    {
2012
0
      log_error (_("unknown weak digest '%s'\n"), digestname);
2013
0
      return;
2014
0
    }
2015
2016
  /* Check to ensure it's not already present.  */
2017
0
  for (weak = opt.weak_digests; weak; weak = weak->next)
2018
0
    if (algo == weak->algo)
2019
0
      return;
2020
2021
  /* Add it to the head of the list.  */
2022
0
  weak = xmalloc(sizeof(*weak));
2023
0
  weak->algo = algo;
2024
0
  weak->rejection_shown = 0;
2025
0
  weak->next = opt.weak_digests;
2026
0
  opt.weak_digests = weak;
2027
0
}
2028
2029
2030
/* Return true if ALGO is in the list of weak digests.  */
2031
int
2032
is_weak_digest (digest_algo_t algo)
2033
0
{
2034
0
  const enum gcry_md_algos galgo = map_md_openpgp_to_gcry (algo);
2035
0
  const struct weakhash *weak;
2036
2037
0
  for (weak = opt.weak_digests; weak; weak = weak->next)
2038
0
    if (weak->algo == galgo)
2039
0
      return 1;
2040
0
  return 0;
2041
0
}
2042
2043
2044
/* We keep a list of partial files so that they are handled later.
2045
 * When it fails at the end, all the partial files will be removed.
2046
 * If it succeeds, those partial files are renamed as valid ones.  */
2047
struct partial_file_item
2048
{
2049
  struct partial_file_item *next;
2050
  char *fname_part;
2051
  char *fname;
2052
};
2053
2054
/* List of partial files, registered.  */
2055
static struct partial_file_item *registered_file_list;
2056
2057
/* Remember a partial file so that it will be handled later.
2058
 * FNAME_PART is a name of a partial file.  FNAME is a name of
2059
 * original file.  */
2060
gpg_error_t
2061
gnupg_register_partial_file (const char *fname_part, const char *fname)
2062
0
{
2063
0
  struct partial_file_item *pfi;
2064
2065
0
  if (!(pfi = xtrymalloc (sizeof (*pfi))))
2066
0
    return gpg_error_from_syserror ();
2067
2068
0
  if (!(pfi->fname_part = xtrystrdup (fname_part)))
2069
0
    {
2070
0
      xfree (pfi);
2071
0
      return gpg_error_from_syserror ();
2072
0
    }
2073
2074
0
  if (!(pfi->fname = xtrystrdup (fname)))
2075
0
    {
2076
0
      xfree (pfi->fname_part);
2077
0
      xfree (pfi);
2078
0
      return gpg_error_from_syserror ();
2079
0
    }
2080
2081
0
  pfi->next = registered_file_list;
2082
0
  registered_file_list = pfi;
2083
0
  return 0;
2084
0
}
2085
2086
2087
/* Remove the partial files. */
2088
gpg_error_t
2089
gnupg_rollback_partial_file (void)
2090
0
{
2091
0
  gpg_error_t err = 0;
2092
0
  struct partial_file_item *pfi = registered_file_list;
2093
0
  struct partial_file_item *next;
2094
2095
0
  registered_file_list = NULL;
2096
2097
0
  while (pfi)
2098
0
    {
2099
0
      next = pfi->next;
2100
2101
0
      err = gnupg_remove (pfi->fname_part);
2102
0
      if (opt.verbose)
2103
0
        log_info (_("Note: partial file '%s' removed\n"), pfi->fname_part);
2104
2105
0
      xfree (pfi->fname_part);
2106
0
      xfree (pfi->fname);
2107
0
      xfree (pfi);
2108
2109
0
      pfi = next;
2110
0
    }
2111
2112
0
  return err;
2113
0
}
2114
2115
2116
/* Rename the partial files. */
2117
gpg_error_t
2118
gnupg_commit_partial_file (void)
2119
0
{
2120
0
  gpg_error_t err = 0;
2121
0
  struct partial_file_item *pfi = registered_file_list;
2122
0
  struct partial_file_item *next;
2123
2124
0
  registered_file_list = NULL;
2125
2126
0
  while (pfi)
2127
0
    {
2128
0
      next = pfi->next;
2129
2130
0
      err = gnupg_rename_file (pfi->fname_part, pfi->fname, NULL);
2131
2132
0
      xfree (pfi->fname_part);
2133
0
      xfree (pfi->fname);
2134
0
      xfree (pfi);
2135
2136
0
      pfi = next;
2137
0
    }
2138
2139
0
  return err;
2140
0
}