Coverage Report

Created: 2025-12-14 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/parse-packet.c
Line
Count
Source
1
/* parse-packet.c  - read packets
2
 * Copyright (C) 1998-2007, 2009-2010 Free Software Foundation, Inc.
3
 * Copyright (C) 2014, 2018 Werner Koch
4
 * Copyright (C) 2015 g10 Code GmbH
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
 * SPDX-License-Identifier: GPL-3.0+
21
 */
22
23
#include <config.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include "gpg.h"
29
#include "../common/util.h"
30
#include "packet.h"
31
#include "../common/iobuf.h"
32
#include "filter.h"
33
#include "photoid.h"
34
#include "options.h"
35
#include "main.h"
36
#include "../common/i18n.h"
37
#include "../common/host2net.h"
38
#include "../common/mbox-util.h"
39
40
41
static int mpi_print_mode;
42
static int list_mode;
43
static estream_t listfp;
44
45
/* A linked list of known notation names.  Note that the FLAG is used
46
 * to store the length of the name to speed up the check.  */
47
static strlist_t known_notations_list;
48
49
50
static int parse (parse_packet_ctx_t ctx, PACKET *pkt, int onlykeypkts,
51
      off_t * retpos, int *skip, IOBUF out, int do_skip
52
#if DEBUG_PARSE_PACKET
53
      , const char *dbg_w, const char *dbg_f, int dbg_l
54
#endif
55
  );
56
static int copy_packet (IOBUF inp, IOBUF out, int pkttype,
57
      unsigned long pktlen, int partial);
58
static void skip_packet (IOBUF inp, int pkttype,
59
       unsigned long pktlen, int partial);
60
static void *read_rest (IOBUF inp, size_t pktlen);
61
static int parse_marker (IOBUF inp, int pkttype, unsigned long pktlen);
62
static int parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
63
          PACKET * packet);
64
static int parse_pubkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
65
          PACKET * packet);
66
static int parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen,
67
            PKT_onepass_sig * ops);
68
static int parse_key (IOBUF inp, int pkttype, unsigned long pktlen,
69
          byte * hdr, int hdrlen, PACKET * packet);
70
static int parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen,
71
        PACKET * packet);
72
static int parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen,
73
          PACKET * packet);
74
static int parse_comment (IOBUF inp, int pkttype, unsigned long pktlen,
75
        PACKET * packet);
76
static gpg_error_t parse_ring_trust (parse_packet_ctx_t ctx,
77
                                     unsigned long pktlen);
78
static int parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen,
79
          PACKET * packet, int new_ctb, int partial);
80
static int parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen,
81
           PACKET * packet, int new_ctb);
82
static int parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen,
83
          PACKET * packet, int new_ctb, int partial);
84
static gpg_error_t parse_encrypted_aead (IOBUF inp, int pkttype,
85
                                         unsigned long pktlen, PACKET *packet,
86
                                         int partial);
87
static int parse_mdc (IOBUF inp, int pkttype, unsigned long pktlen,
88
          PACKET * packet, int new_ctb);
89
static int parse_gpg_control (IOBUF inp, int pkttype, unsigned long pktlen,
90
            PACKET * packet, int partial);
91
92
/* Read a 16-bit value in MSB order (big endian) from an iobuf.  */
93
static unsigned short
94
read_16 (IOBUF inp)
95
522
{
96
522
  unsigned short a;
97
522
  a = (unsigned short)iobuf_get_noeof (inp) << 8;
98
522
  a |= iobuf_get_noeof (inp);
99
522
  return a;
100
522
}
101
102
103
/* Read a 32-bit value in MSB order (big endian) from an iobuf.  */
104
static unsigned long
105
read_32 (IOBUF inp)
106
447
{
107
447
  unsigned long a;
108
447
  a = (unsigned long)iobuf_get_noeof (inp) << 24;
109
447
  a |= iobuf_get_noeof (inp) << 16;
110
447
  a |= iobuf_get_noeof (inp) << 8;
111
447
  a |= iobuf_get_noeof (inp);
112
447
  return a;
113
447
}
114
115
116
/* Read an external representation of an MPI and return the MPI.  The
117
   external format is a 16-bit unsigned value stored in network byte
118
   order giving the number of bits for the following integer.  The
119
   integer is stored MSB first and is left padded with zero bits to
120
   align on a byte boundary.
121
122
   The caller must set *RET_NREAD to the maximum number of bytes to
123
   read from the pipeline INP.  This function sets *RET_NREAD to be
124
   the number of bytes actually read from the pipeline.
125
126
   If SECURE is true, the integer is stored in secure memory
127
   (allocated using gcry_xmalloc_secure).  */
128
static gcry_mpi_t
129
mpi_read (iobuf_t inp, unsigned int *ret_nread, int secure)
130
0
{
131
0
  int c, c1, c2, i;
132
0
  unsigned int nmax = *ret_nread;
133
0
  unsigned int nbits, nbytes;
134
0
  size_t nread = 0;
135
0
  gcry_mpi_t a = NULL;
136
0
  byte *buf = NULL;
137
0
  byte *p;
138
139
0
  if (!nmax)
140
0
    goto overflow;
141
142
0
  if ((c = c1 = iobuf_get (inp)) == -1)
143
0
    goto leave;
144
0
  if (++nread == nmax)
145
0
    goto overflow;
146
0
  nbits = c << 8;
147
0
  if ((c = c2 = iobuf_get (inp)) == -1)
148
0
    goto leave;
149
0
  ++nread;
150
0
  nbits |= c;
151
0
  if (nbits > MAX_EXTERN_MPI_BITS)
152
0
    {
153
0
      log_error ("mpi too large (%u bits)\n", nbits);
154
0
      goto leave;
155
0
    }
156
157
0
  nbytes = (nbits + 7) / 8;
158
0
  buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2);
159
0
  p = buf;
160
0
  p[0] = c1;
161
0
  p[1] = c2;
162
0
  for (i = 0; i < nbytes; i++)
163
0
    {
164
0
      if (nread == nmax)
165
0
  goto overflow;
166
167
0
      c = iobuf_get (inp);
168
0
      if (c == -1)
169
0
  goto leave;
170
171
0
      p[i + 2] = c;
172
0
      nread ++;
173
0
    }
174
175
0
  if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread))
176
0
    a = NULL;
177
178
0
  *ret_nread = nread;
179
0
  gcry_free(buf);
180
0
  return a;
181
182
0
 overflow:
183
0
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
184
0
 leave:
185
0
  *ret_nread = nread;
186
0
  gcry_free(buf);
187
0
  return a;
188
0
}
189
190
191
/* If NLENGTH is zero read an octet string of length NBYTES from INP
192
 * and return it at R_DATA.
193
 *
194
 * If NLENGTH is either 1, 2, or 4 and NLENGTH is zero read an
195
 * NLENGTH-octet count and use this count number octets from INP and
196
 * return it at R_DATA.
197
 *
198
 * On error return an error code and store NULL at R_DATA.  PKTLEN
199
 * shall give the current length of the packet and is updated with
200
 * each read. If SECURE is true, the integer is stored in secure
201
 * memory (allocated using gcry_xmalloc_secure).
202
 */
203
static gpg_error_t
204
read_octet_string (iobuf_t inp, unsigned long *pktlen,
205
                   unsigned int nlength, unsigned int nbytes,
206
                   int secure, gcry_mpi_t *r_data)
207
0
{
208
0
  gpg_error_t err;
209
0
  int c, i;
210
0
  byte *buf = NULL;
211
0
  byte *p;
212
213
0
  *r_data = NULL;
214
215
0
  if ((nbytes && nlength)
216
0
      || (!nbytes && !(nlength == 1 || nlength == 2 || nlength == 4)))
217
0
    {
218
0
      err = gpg_error (GPG_ERR_INV_ARG);
219
0
      goto leave;
220
0
    }
221
222
0
  if (nlength)
223
0
    {
224
0
      for (i = 0; i < nlength; i++)
225
0
        {
226
0
          if (!*pktlen)
227
0
            {
228
0
              err = gpg_error (GPG_ERR_INV_PACKET);
229
0
              goto leave;
230
0
            }
231
0
          c = iobuf_readbyte (inp);
232
0
          if (c < 0)
233
0
            {
234
0
              err =  gpg_error (GPG_ERR_INV_PACKET);
235
0
              goto leave;
236
0
            }
237
0
          --*pktlen;
238
0
          nbytes <<= 8;
239
0
          nbytes |= c;
240
0
        }
241
242
0
      if (!nbytes)
243
0
        {
244
0
          err =  gpg_error (GPG_ERR_INV_PACKET);
245
0
          goto leave;
246
0
        }
247
0
    }
248
249
0
  if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS)
250
0
      || (nbytes*8 < nbytes))
251
0
    {
252
0
      log_error ("octet string too large (%u octets)\n", nbytes);
253
0
      err = gpg_error (GPG_ERR_TOO_LARGE);
254
0
      goto leave;
255
0
    }
256
257
0
  if (nbytes > *pktlen)
258
0
    {
259
0
      log_error ("octet string larger than packet (%u octets)\n", nbytes);
260
0
      err = gpg_error (GPG_ERR_INV_PACKET);
261
0
      goto leave;
262
0
    }
263
264
0
  buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes);
265
0
  if (!buf)
266
0
    {
267
0
      err = gpg_error_from_syserror ();
268
0
      goto leave;
269
0
    }
270
0
  p = buf;
271
0
  for (i = 0; i < nbytes; i++)
272
0
    {
273
0
      c = iobuf_get (inp);
274
0
      if (c == -1)
275
0
        {
276
0
          err = gpg_error (GPG_ERR_INV_PACKET);
277
0
          goto leave;
278
0
        }
279
280
0
      p[i] = c;
281
0
      --*pktlen;
282
0
    }
283
284
0
  *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8);
285
0
  gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2);
286
0
  return 0;
287
288
0
 leave:
289
0
  gcry_free (buf);
290
0
  return err;
291
0
}
292
293
294
/* Read an external representation of an SOS and return the opaque MPI
295
   with GCRYMPI_FLAG_USER2.  The external format is a 16-bit unsigned
296
   value stored in network byte order giving information for the
297
   following octets.
298
299
   The caller must set *RET_NREAD to the maximum number of bytes to
300
   read from the pipeline INP.  This function sets *RET_NREAD to be
301
   the number of bytes actually read from the pipeline.
302
303
   If SECURE is true, the integer is stored in secure memory
304
   (allocated using gcry_xmalloc_secure).  */
305
static gcry_mpi_t
306
sos_read (iobuf_t inp, unsigned int *ret_nread, int secure)
307
964
{
308
964
  int c, c1, c2, i;
309
964
  unsigned int nmax = *ret_nread;
310
964
  unsigned int nbits, nbytes;
311
964
  size_t nread = 0;
312
964
  gcry_mpi_t a = NULL;
313
964
  byte *buf = NULL;
314
964
  byte *p;
315
316
964
  if (!nmax)
317
0
    goto overflow;
318
319
964
  if ((c = c1 = iobuf_get (inp)) == -1)
320
0
    goto leave;
321
964
  if (++nread == nmax)
322
0
    goto overflow;
323
964
  nbits = c << 8;
324
964
  if ((c = c2 = iobuf_get (inp)) == -1)
325
0
    goto leave;
326
964
  ++nread;
327
964
  nbits |= c;
328
964
  if (nbits > MAX_EXTERN_MPI_BITS)
329
0
    {
330
0
      log_error ("mpi too large (%u bits)\n", nbits);
331
0
      goto leave;
332
0
    }
333
334
964
  nbytes = (nbits + 7) / 8;
335
964
  buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes);
336
964
  p = buf;
337
32.2k
  for (i = 0; i < nbytes; i++)
338
31.2k
    {
339
31.2k
      if (nread == nmax)
340
0
        goto overflow;
341
342
31.2k
      c = iobuf_get (inp);
343
31.2k
      if (c == -1)
344
0
        goto leave;
345
346
31.2k
      p[i] = c;
347
31.2k
      nread ++;
348
31.2k
    }
349
350
964
  a = gcry_mpi_set_opaque (NULL, buf, nbits);
351
964
  gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2);
352
964
  *ret_nread = nread;
353
964
  return a;
354
355
0
 overflow:
356
0
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
357
0
 leave:
358
0
  *ret_nread = nread;
359
0
  gcry_free(buf);
360
0
  return a;
361
0
}
362
363
364
/* Register STRING as a known critical notation name.  */
365
void
366
register_known_notation (const char *string)
367
0
{
368
0
  strlist_t sl;
369
370
0
  if (!known_notations_list)
371
0
    {
372
0
      sl = add_to_strlist (&known_notations_list,
373
0
                           "preferred-email-encoding@pgp.com");
374
0
      sl->flags = 32;  /* Length of the string.  */
375
0
    }
376
0
  if (!string)
377
0
    return; /* Only initialized the default known notations.  */
378
379
  /* In --set-notation we use an exclamation mark to indicate a
380
   * critical notation.  As a convenience skip this here.  */
381
0
  if (*string == '!')
382
0
    string++;
383
384
0
  if (!*string || strlist_find (known_notations_list, string))
385
0
    return; /* Empty string or already registered.  */
386
387
0
  sl = add_to_strlist (&known_notations_list, string);
388
0
  sl->flags = strlen (string);
389
0
}
390
391
392
int
393
set_packet_list_mode (int mode)
394
0
{
395
0
  int old = list_mode;
396
0
  list_mode = mode;
397
398
  /* We use stdout only if invoked by the --list-packets command
399
     but switch to stderr in all other cases.  This breaks the
400
     previous behaviour but that seems to be more of a bug than
401
     intentional.  I don't believe that any application makes use of
402
     this long standing annoying way of printing to stdout except when
403
     doing a --list-packets. If this assumption fails, it will be easy
404
     to add an option for the listing stream.  Note that we initialize
405
     it only once; mainly because there is code which switches
406
     opt.list_mode back to 1 and we want to have all output to the
407
     same stream.  The MPI_PRINT_MODE will be enabled if the
408
     corresponding debug flag is set or if we are in --list-packets
409
     and --verbose is given.
410
411
     Using stderr is not actually very clean because it bypasses the
412
     logging code but it is a special thing anyway.  I am not sure
413
     whether using log_stream() would be better.  Perhaps we should
414
     enable the list mode only with a special option. */
415
0
  if (!listfp)
416
0
    {
417
0
      if (opt.list_packets)
418
0
        {
419
0
          listfp = es_stdout;
420
0
          if (opt.verbose)
421
0
            mpi_print_mode = 1;
422
0
        }
423
0
      else
424
0
        listfp = es_stderr;
425
426
0
      if (DBG_MPI)
427
0
        mpi_print_mode = 1;
428
0
    }
429
0
  return old;
430
0
}
431
432
433
/* If OPT.VERBOSE is set, print a warning that the algorithm ALGO is
434
   not suitable for signing and encryption.  */
435
static void
436
unknown_pubkey_warning (int algo)
437
0
{
438
0
  static byte unknown_pubkey_algos[256];
439
440
  /* First check whether the algorithm is usable but not suitable for
441
     encryption/signing.  */
442
0
  if (pubkey_get_npkey (algo))
443
0
    {
444
0
      if (opt.verbose && !glo_ctrl.silence_parse_warnings)
445
0
        {
446
0
          if (!pubkey_get_nsig (algo))
447
0
            log_info ("public key algorithm %s not suitable for %s\n",
448
0
                      openpgp_pk_algo_name (algo), "signing");
449
0
          if (!pubkey_get_nenc (algo))
450
0
            log_info ("public key algorithm %s not suitable for %s\n",
451
0
                      openpgp_pk_algo_name (algo), "encryption");
452
0
        }
453
0
    }
454
0
  else
455
0
    {
456
0
      algo &= 0xff;
457
0
      if (!unknown_pubkey_algos[algo])
458
0
        {
459
0
          if (opt.verbose && !glo_ctrl.silence_parse_warnings)
460
0
            log_info (_("can't handle public key algorithm %d\n"), algo);
461
0
          unknown_pubkey_algos[algo] = 1;
462
0
        }
463
0
    }
464
0
}
465
466
467
#if DEBUG_PARSE_PACKET
468
int
469
dbg_parse_packet (parse_packet_ctx_t ctx, PACKET *pkt,
470
                  const char *dbg_f, int dbg_l)
471
6.79k
{
472
6.79k
  int skip, rc;
473
474
6.79k
  do
475
6.79k
    {
476
6.79k
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l);
477
6.79k
    }
478
6.79k
  while (skip && ! rc);
479
6.79k
  return rc;
480
6.79k
}
481
#else /*!DEBUG_PARSE_PACKET*/
482
int
483
parse_packet (parse_packet_ctx_t ctx, PACKET *pkt)
484
{
485
  int skip, rc;
486
487
  do
488
    {
489
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0);
490
    }
491
  while (skip && ! rc);
492
  return rc;
493
}
494
#endif /*!DEBUG_PARSE_PACKET*/
495
496
497
/*
498
 * Like parse packet, but only return secret or public (sub)key
499
 * packets.
500
 */
501
#if DEBUG_PARSE_PACKET
502
int
503
dbg_search_packet (parse_packet_ctx_t ctx, PACKET *pkt,
504
                   off_t * retpos, int with_uid,
505
       const char *dbg_f, int dbg_l)
506
0
{
507
0
  int skip, rc;
508
509
0
  do
510
0
    {
511
0
      rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0, "search",
512
0
                  dbg_f, dbg_l);
513
0
    }
514
0
  while (skip && ! rc);
515
0
  return rc;
516
0
}
517
#else /*!DEBUG_PARSE_PACKET*/
518
int
519
search_packet (parse_packet_ctx_t ctx, PACKET *pkt,
520
               off_t * retpos, int with_uid)
521
{
522
  int skip, rc;
523
524
  do
525
    {
526
      rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0);
527
    }
528
  while (skip && ! rc);
529
  return rc;
530
}
531
#endif /*!DEBUG_PARSE_PACKET*/
532
533
534
/*
535
 * Copy all packets from INP to OUT, thereby removing unused spaces.
536
 */
537
#if DEBUG_PARSE_PACKET
538
int
539
dbg_copy_all_packets (iobuf_t inp, iobuf_t out, const char *dbg_f, int dbg_l)
540
0
{
541
0
  PACKET pkt;
542
0
  struct parse_packet_ctx_s parsectx;
543
0
  int skip, rc = 0;
544
545
0
  if (! out)
546
0
    log_bug ("copy_all_packets: OUT may not be NULL.\n");
547
548
0
  init_parse_packet (&parsectx, inp);
549
550
0
  do
551
0
    {
552
0
      init_packet (&pkt);
553
0
    }
554
0
  while (!
555
0
   (rc =
556
0
    parse (&parsectx, &pkt, 0, NULL, &skip, out, 0, "copy",
557
0
                 dbg_f, dbg_l)));
558
559
0
  deinit_parse_packet (&parsectx);
560
561
0
  return rc;
562
0
}
563
#else /*!DEBUG_PARSE_PACKET*/
564
int
565
copy_all_packets (iobuf_t inp, iobuf_t out)
566
{
567
  PACKET pkt;
568
  struct parse_packet_ctx_s parsectx;
569
  int skip, rc = 0;
570
571
  if (! out)
572
    log_bug ("copy_all_packets: OUT may not be NULL.\n");
573
574
  init_parse_packet (&parsectx, inp);
575
576
  do
577
    {
578
      init_packet (&pkt);
579
    }
580
  while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0)));
581
582
  deinit_parse_packet (&parsectx);
583
584
  return rc;
585
}
586
#endif /*!DEBUG_PARSE_PACKET*/
587
588
589
/*
590
 * Copy some packets from INP to OUT, thereby removing unused spaces.
591
 * Stop at offset STOPoff (i.e. don't copy packets at this or later
592
 * offsets)
593
 */
594
#if DEBUG_PARSE_PACKET
595
int
596
dbg_copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff,
597
           const char *dbg_f, int dbg_l)
598
0
{
599
0
  int rc = 0;
600
0
  PACKET pkt;
601
0
  int skip;
602
0
  struct parse_packet_ctx_s parsectx;
603
604
0
  init_parse_packet (&parsectx, inp);
605
606
0
  do
607
0
    {
608
0
      if (iobuf_tell (inp) >= stopoff)
609
0
        {
610
0
          deinit_parse_packet (&parsectx);
611
0
          return 0;
612
0
        }
613
0
      init_packet (&pkt);
614
0
    }
615
0
  while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0,
616
0
           "some", dbg_f, dbg_l)));
617
618
0
  deinit_parse_packet (&parsectx);
619
620
0
  return rc;
621
0
}
622
#else /*!DEBUG_PARSE_PACKET*/
623
int
624
copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff)
625
{
626
  int rc = 0;
627
  PACKET pkt;
628
  struct parse_packet_ctx_s parsectx;
629
  int skip;
630
631
  init_parse_packet (&parsectx, inp);
632
633
  do
634
    {
635
      if (iobuf_tell (inp) >= stopoff)
636
        {
637
          deinit_parse_packet (&parsectx);
638
          return 0;
639
        }
640
      init_packet (&pkt);
641
    }
642
  while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0)));
643
644
  deinit_parse_packet (&parsectx);
645
646
  return rc;
647
}
648
#endif /*!DEBUG_PARSE_PACKET*/
649
650
651
/*
652
 * Skip over N packets
653
 */
654
#if DEBUG_PARSE_PACKET
655
int
656
dbg_skip_some_packets (iobuf_t inp, unsigned n, const char *dbg_f, int dbg_l)
657
0
{
658
0
  int rc = 0;
659
0
  int skip;
660
0
  PACKET pkt;
661
0
  struct parse_packet_ctx_s parsectx;
662
663
0
  init_parse_packet (&parsectx, inp);
664
665
0
  for (; n && !rc; n--)
666
0
    {
667
0
      init_packet (&pkt);
668
0
      rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1, "skip",
669
0
                  dbg_f, dbg_l);
670
0
    }
671
672
0
  deinit_parse_packet (&parsectx);
673
674
0
  return rc;
675
0
}
676
#else /*!DEBUG_PARSE_PACKET*/
677
int
678
skip_some_packets (iobuf_t inp, unsigned int n)
679
{
680
  int rc = 0;
681
  int skip;
682
  PACKET pkt;
683
  struct parse_packet_ctx_s parsectx;
684
685
  init_parse_packet (&parsectx, inp);
686
687
  for (; n && !rc; n--)
688
    {
689
      init_packet (&pkt);
690
      rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1);
691
    }
692
693
  deinit_parse_packet (&parsectx);
694
695
  return rc;
696
}
697
#endif /*!DEBUG_PARSE_PACKET*/
698
699
700
/* Parse a packet and save it in *PKT.
701
702
   If OUT is not NULL and the packet is valid (its type is not 0),
703
   then the header, the initial length field and the packet's contents
704
   are written to OUT.  In this case, the packet is not saved in *PKT.
705
706
   ONLYKEYPKTS is a simple packet filter.  If ONLYKEYPKTS is set to 1,
707
   then only public subkey packets, public key packets, private subkey
708
   packets and private key packets are parsed.  The rest are skipped
709
   (i.e., the header and the contents are read from the pipeline and
710
   discarded).  If ONLYKEYPKTS is set to 2, then in addition to the
711
   above 4 types of packets, user id packets are also accepted.
712
713
   DO_SKIP is a more coarse grained filter.  Unless ONLYKEYPKTS is set
714
   to 2 and the packet is a user id packet, all packets are skipped.
715
716
   Finally, if a packet is invalid (it's type is 0), it is skipped.
717
718
   If a packet is skipped and SKIP is not NULL, then *SKIP is set to
719
   1.
720
721
   Note: ONLYKEYPKTS and DO_SKIP are only respected if OUT is NULL,
722
   i.e., the packets are not simply being copied.
723
724
   If RETPOS is not NULL, then the position of CTX->INP (as returned by
725
   iobuf_tell) is saved there before any data is read from CTX->INP.
726
  */
727
static int
728
parse (parse_packet_ctx_t ctx, PACKET *pkt, int onlykeypkts, off_t * retpos,
729
       int *skip, IOBUF out, int do_skip
730
#if DEBUG_PARSE_PACKET
731
       , const char *dbg_w, const char *dbg_f, int dbg_l
732
#endif
733
       )
734
6.79k
{
735
6.79k
  int rc = 0;
736
6.79k
  iobuf_t inp;
737
6.79k
  int c, ctb, pkttype, lenbytes;
738
6.79k
  unsigned long pktlen;
739
6.79k
  byte hdr[8];
740
6.79k
  int hdrlen;
741
6.79k
  int new_ctb = 0, partial = 0;
742
6.79k
  int with_uid = (onlykeypkts == 2);
743
6.79k
  off_t pos;
744
745
6.79k
  *skip = 0;
746
6.79k
  inp = ctx->inp;
747
748
6.80k
 again:
749
6.80k
  log_assert (!pkt->pkt.generic);
750
6.80k
  if (retpos || list_mode)
751
0
    {
752
0
      pos = iobuf_tell (inp);
753
0
      if (retpos)
754
0
        *retpos = pos;
755
0
    }
756
6.80k
  else
757
6.80k
    pos = 0; /* (silence compiler warning) */
758
759
  /* The first byte of a packet is the so-called tag.  The highest bit
760
     must be set.  */
761
6.80k
  if ((ctb = iobuf_get (inp)) == -1)
762
10
    {
763
10
      rc = -1;
764
10
      goto leave;
765
10
    }
766
6.79k
  ctx->last_ctb = ctb;
767
6.79k
  hdrlen = 0;
768
6.79k
  hdr[hdrlen++] = ctb;
769
770
6.79k
  if (!(ctb & 0x80))
771
11
    {
772
11
      log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb);
773
11
      rc = gpg_error (GPG_ERR_INV_PACKET);
774
11
      goto leave;
775
11
    }
776
777
  /* Immediately following the header is the length.  There are two
778
   * formats: the old format and the new format.  If bit 6 (where the
779
   * least significant bit is bit 0) is set in the tag, then we are
780
   * dealing with a new format packet.  Otherwise, it is an old format
781
   * packet.  In the new format the packet's type is encoded in the 6
782
   * least significant bits of the tag; in the old format it is
783
   * encoded in bits 2-5.  */
784
6.78k
  pktlen = 0;
785
6.78k
  new_ctb = !!(ctb & 0x40);
786
6.78k
  if (new_ctb)
787
5.54k
    pkttype = ctb & 0x3f;
788
1.23k
  else
789
1.23k
    pkttype = (ctb >> 2) & 0xf;
790
791
6.78k
  if (ctx->only_fookey_enc
792
0
      && !(pkttype == PKT_SYMKEY_ENC || pkttype == PKT_PUBKEY_ENC))
793
0
    {
794
0
      rc = gpg_error (GPG_ERR_TRUE);
795
0
      goto leave;
796
0
    }
797
798
6.78k
  if (new_ctb)
799
5.54k
    {
800
      /* Extract the packet's length.  New format packets have 4 ways
801
   to encode the packet length.  The value of the first byte
802
   determines the encoding and partially determines the length.
803
   See section 4.2.2 of RFC 4880 for details.  */
804
5.54k
      if ((c = iobuf_get (inp)) == -1)
805
0
  {
806
0
    log_error ("%s: 1st length byte missing\n", iobuf_where (inp));
807
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
808
0
    goto leave;
809
0
  }
810
811
812
5.54k
      hdr[hdrlen++] = c;
813
5.54k
      if (c < 192)
814
5.52k
        pktlen = c;
815
28
      else if (c < 224)
816
2
        {
817
2
          pktlen = (c - 192) * 256;
818
2
          if ((c = iobuf_get (inp)) == -1)
819
0
            {
820
0
              log_error ("%s: 2nd length byte missing\n",
821
0
                         iobuf_where (inp));
822
0
              rc = gpg_error (GPG_ERR_INV_PACKET);
823
0
              goto leave;
824
0
            }
825
2
          hdr[hdrlen++] = c;
826
2
          pktlen += c + 192;
827
2
        }
828
26
      else if (c == 255)
829
20
        {
830
20
    int i;
831
20
    char value[4];
832
833
100
    for (i = 0; i < 4; i ++)
834
80
            {
835
80
              if ((c = iobuf_get (inp)) == -1)
836
0
                {
837
0
                  log_error ("%s: 4 byte length invalid\n", iobuf_where (inp));
838
0
                  rc = gpg_error (GPG_ERR_INV_PACKET);
839
0
                  goto leave;
840
0
                }
841
80
              value[i] = hdr[hdrlen++] = c;
842
80
            }
843
844
20
    pktlen = buf32_to_ulong (value);
845
20
        }
846
6
      else /* Partial body length.  */
847
6
        {
848
6
          switch (pkttype)
849
6
            {
850
2
            case PKT_PLAINTEXT:
851
2
            case PKT_ENCRYPTED:
852
2
            case PKT_ENCRYPTED_MDC:
853
2
            case PKT_ENCRYPTED_AEAD:
854
4
            case PKT_COMPRESSED:
855
4
              iobuf_set_partial_body_length_mode (inp, c & 0xff);
856
4
              pktlen = 0; /* To indicate partial length.  */
857
4
              partial = 1;
858
4
              break;
859
860
2
            default:
861
2
              log_error ("%s: partial length invalid for"
862
2
                         " packet type %d\n", iobuf_where (inp), pkttype);
863
2
              rc = gpg_error (GPG_ERR_INV_PACKET);
864
2
              goto leave;
865
6
            }
866
6
        }
867
868
5.54k
    }
869
1.23k
  else /* This is an old format packet.  */
870
1.23k
    {
871
      /* The type of length encoding is encoded in bits 0-1 of the
872
   tag.  */
873
1.23k
      lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3));
874
1.23k
      if (!lenbytes)
875
2
  {
876
2
    pktlen = 0; /* Don't know the value.  */
877
    /* This isn't really partial, but we can treat it the same
878
       in a "read until the end" sort of way.  */
879
2
    partial = 1;
880
2
    if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT
881
2
        && pkttype != PKT_COMPRESSED)
882
0
      {
883
0
        log_error ("%s: indeterminate length for invalid"
884
0
       " packet type %d\n", iobuf_where (inp), pkttype);
885
0
        rc = gpg_error (GPG_ERR_INV_PACKET);
886
0
        goto leave;
887
0
      }
888
2
  }
889
1.23k
      else
890
1.23k
  {
891
2.47k
    for (; lenbytes; lenbytes--)
892
1.23k
      {
893
1.23k
        pktlen <<= 8;
894
1.23k
        c = iobuf_get (inp);
895
1.23k
        if (c == -1)
896
0
    {
897
0
      log_error ("%s: length invalid\n", iobuf_where (inp));
898
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
899
0
      goto leave;
900
0
    }
901
1.23k
        pktlen |= hdr[hdrlen++] = c;
902
1.23k
      }
903
1.23k
  }
904
1.23k
    }
905
906
  /* Sometimes the decompressing layer enters an error state in which
907
     it simply outputs 0xff for every byte read.  If we have a stream
908
     of 0xff bytes, then it will be detected as a new format packet
909
     with type 63 and a 4-byte encoded length that is 4G-1.  Since
910
     packets with type 63 are private and we use them as a control
911
     packet, which won't be 4 GB, we reject such packets as
912
     invalid.  */
913
6.78k
  if (pkttype == 63 && pktlen == 0xFFFFFFFF)
914
0
    {
915
      /* With some probability this is caused by a problem in the
916
       * the uncompressing layer - in some error cases it just loops
917
       * and spits out 0xff bytes. */
918
0
      log_error ("%s: garbled packet detected\n", iobuf_where (inp));
919
0
      g10_exit (2);
920
0
    }
921
922
6.78k
  if (out && pkttype)
923
0
    {
924
      /* This type of copying won't work if the packet uses a partial
925
   body length.  (In other words, this only works if HDR is
926
   actually the length.)  Currently, no callers require this
927
   functionality so we just log this as an error.  */
928
0
      if (partial)
929
0
  {
930
0
    log_error ("parse: Can't copy partial packet.  Aborting.\n");
931
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
932
0
    goto leave;
933
0
  }
934
935
0
      rc = iobuf_write (out, hdr, hdrlen);
936
0
      if (!rc)
937
0
  rc = copy_packet (inp, out, pkttype, pktlen, partial);
938
0
      goto leave;
939
0
    }
940
941
6.78k
  if (with_uid && pkttype == PKT_USER_ID)
942
    /* If ONLYKEYPKTS is set to 2, then we never skip user id packets,
943
       even if DO_SKIP is set.  */
944
0
    ;
945
6.78k
  else if (do_skip
946
     /* type==0 is not allowed.  This is an invalid packet.  */
947
6.78k
     || !pkttype
948
     /* When ONLYKEYPKTS is set, we don't skip keys.  */
949
6.78k
     || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
950
0
         && pkttype != PKT_PUBLIC_KEY
951
0
         && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY))
952
0
    {
953
0
      iobuf_skip_rest (inp, pktlen, partial);
954
0
      *skip = 1;
955
0
      rc = 0;
956
0
      goto leave;
957
0
    }
958
959
6.78k
  if (DBG_PACKET)
960
0
    {
961
0
#if DEBUG_PARSE_PACKET
962
0
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n",
963
0
     iobuf_id (inp), pkttype, pktlen, new_ctb ? " (new_ctb)" : "",
964
0
     dbg_w, dbg_f, dbg_l);
965
#else
966
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s\n",
967
     iobuf_id (inp), pkttype, pktlen,
968
     new_ctb ? " (new_ctb)" : "");
969
#endif
970
0
    }
971
972
6.78k
  if (list_mode)
973
6.78k
    es_fprintf (listfp, "# off=%lu ctb=%02x tag=%d hlen=%d plen=%lu%s%s\n",
974
0
                (unsigned long)pos, ctb, pkttype, hdrlen, pktlen,
975
0
                partial? (new_ctb ? " partial" : " indeterminate") :"",
976
0
                new_ctb? " new-ctb":"");
977
978
  /* Count it.  */
979
6.78k
  ctx->n_parsed_packets++;
980
981
6.78k
  pkt->pkttype = pkttype;
982
6.78k
  rc = GPG_ERR_UNKNOWN_PACKET;  /* default error */
983
6.78k
  switch (pkttype)
984
6.78k
    {
985
242
    case PKT_PUBLIC_KEY:
986
242
    case PKT_PUBLIC_SUBKEY:
987
443
    case PKT_SECRET_KEY:
988
443
    case PKT_SECRET_SUBKEY:
989
443
      pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key);
990
443
      rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt);
991
443
      break;
992
0
    case PKT_SYMKEY_ENC:
993
0
      rc = parse_symkeyenc (inp, pkttype, pktlen, pkt);
994
0
      break;
995
0
    case PKT_PUBKEY_ENC:
996
0
      rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt);
997
0
      break;
998
261
    case PKT_SIGNATURE:
999
261
      pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature);
1000
261
      rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature);
1001
261
      break;
1002
0
    case PKT_ONEPASS_SIG:
1003
0
      pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig);
1004
0
      rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig);
1005
0
      break;
1006
6.05k
    case PKT_USER_ID:
1007
6.05k
      rc = parse_user_id (inp, pkttype, pktlen, pkt);
1008
6.05k
      break;
1009
0
    case PKT_ATTRIBUTE:
1010
0
      pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
1011
0
      rc = parse_attribute (inp, pkttype, pktlen, pkt);
1012
0
      break;
1013
0
    case PKT_OLD_COMMENT:
1014
0
    case PKT_COMMENT:
1015
0
      rc = parse_comment (inp, pkttype, pktlen, pkt);
1016
0
      break;
1017
8
    case PKT_RING_TRUST:
1018
8
      {
1019
8
        rc = parse_ring_trust (ctx, pktlen);
1020
8
        if (!rc)
1021
8
          goto again; /* Directly read the next packet.  */
1022
8
      }
1023
0
      break;
1024
4
    case PKT_PLAINTEXT:
1025
4
      rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial);
1026
4
      break;
1027
4
    case PKT_COMPRESSED:
1028
4
      rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb);
1029
4
      break;
1030
0
    case PKT_ENCRYPTED:
1031
0
    case PKT_ENCRYPTED_MDC:
1032
0
      rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial);
1033
0
      break;
1034
0
    case PKT_MDC:
1035
0
      rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb);
1036
0
      break;
1037
0
    case PKT_ENCRYPTED_AEAD:
1038
0
      rc = parse_encrypted_aead (inp, pkttype, pktlen, pkt, partial);
1039
0
      break;
1040
2
    case PKT_GPG_CONTROL:
1041
2
      rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial);
1042
2
      break;
1043
0
    case PKT_MARKER:
1044
0
      rc = parse_marker (inp, pkttype, pktlen);
1045
0
      break;
1046
2
    default:
1047
      /* Unknown packet.  Skip it.  */
1048
2
      skip_packet (inp, pkttype, pktlen, partial);
1049
2
      break;
1050
6.78k
    }
1051
1052
  /* Store a shallow copy of certain packets in the context.  */
1053
6.77k
  free_packet (NULL, ctx);
1054
6.77k
  if (!rc && (pkttype == PKT_PUBLIC_KEY
1055
6.52k
              || pkttype == PKT_SECRET_KEY
1056
6.32k
              || pkttype == PKT_USER_ID
1057
269
              || pkttype == PKT_ATTRIBUTE
1058
269
              || pkttype == PKT_SIGNATURE))
1059
6.75k
    {
1060
6.75k
      ctx->last_pkt = *pkt;
1061
6.75k
    }
1062
1063
6.79k
 leave:
1064
  /* FIXME: We leak in case of an error (see the xmalloc's above).  */
1065
6.79k
  if (!rc && iobuf_error (inp))
1066
4
    rc = GPG_ERR_INV_KEYRING;
1067
1068
  /* FIXME: We use only the error code for now to avoid problems with
1069
     callers which have not been checked to always use gpg_err_code()
1070
     when comparing error codes.  */
1071
6.79k
  return rc == -1? -1 : gpg_err_code (rc);
1072
6.77k
}
1073
1074
1075
static void
1076
dump_hex_line (int c, int *i)
1077
0
{
1078
0
  if (*i && !(*i % 8))
1079
0
    {
1080
0
      if (*i && !(*i % 24))
1081
0
  es_fprintf (listfp, "\n%4d:", *i);
1082
0
      else
1083
0
  es_putc (' ', listfp);
1084
0
    }
1085
0
  if (c == -1)
1086
0
    es_fprintf (listfp, " EOF");
1087
0
  else
1088
0
    es_fprintf (listfp, " %02x", c);
1089
0
  ++*i;
1090
0
}
1091
1092
1093
/* Copy the contents of a packet from the pipeline IN to the pipeline
1094
   OUT.
1095
1096
   The header and length have already been read from INP and the
1097
   decoded values are given as PKGTYPE and PKTLEN.
1098
1099
   If the packet is a partial body length packet (RFC 4880, Section
1100
   4.2.2.4), then iobuf_set_partial_block_modeiobuf_set_partial_block_mode
1101
   should already have been called on INP and PARTIAL should be set.
1102
1103
   If PARTIAL is set or PKTLEN is 0 and PKTTYPE is PKT_COMPRESSED,
1104
   copy until the first EOF is encountered on INP.
1105
1106
   Returns 0 on success and an error code if an error occurs.  */
1107
static int
1108
copy_packet (IOBUF inp, IOBUF out, int pkttype,
1109
       unsigned long pktlen, int partial)
1110
0
{
1111
0
  int rc;
1112
0
  int n;
1113
0
  char buf[100];
1114
1115
0
  if (partial)
1116
0
    {
1117
0
      while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1)
1118
0
  if ((rc = iobuf_write (out, buf, n)))
1119
0
    return rc;   /* write error */
1120
0
    }
1121
0
  else if (!pktlen && pkttype == PKT_COMPRESSED)
1122
0
    {
1123
0
      log_debug ("copy_packet: compressed!\n");
1124
      /* compressed packet, copy till EOF */
1125
0
      while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1)
1126
0
  if ((rc = iobuf_write (out, buf, n)))
1127
0
    return rc;   /* write error */
1128
0
    }
1129
0
  else
1130
0
    {
1131
0
      for (; pktlen; pktlen -= n)
1132
0
  {
1133
0
    n = pktlen > sizeof (buf) ? sizeof (buf) : pktlen;
1134
0
    n = iobuf_read (inp, buf, n);
1135
0
    if (n == -1)
1136
0
      return gpg_error (GPG_ERR_EOF);
1137
0
    if ((rc = iobuf_write (out, buf, n)))
1138
0
      return rc;   /* write error */
1139
0
  }
1140
0
    }
1141
0
  return 0;
1142
0
}
1143
1144
1145
/* Skip an unknown packet.  PKTTYPE is the packet's type, PKTLEN is
1146
   the length of the packet's content and PARTIAL is whether partial
1147
   body length encoding in used (in this case PKTLEN is ignored).  */
1148
static void
1149
skip_packet (IOBUF inp, int pkttype, unsigned long pktlen, int partial)
1150
2
{
1151
2
  if (list_mode)
1152
0
    {
1153
0
      es_fprintf (listfp, ":unknown packet: type %2d, length %lu\n",
1154
0
                  pkttype, pktlen);
1155
0
      if (pkttype)
1156
0
  {
1157
0
    int c, i = 0;
1158
0
    es_fputs ("dump:", listfp);
1159
0
    if (partial)
1160
0
      {
1161
0
        while ((c = iobuf_get (inp)) != -1)
1162
0
    dump_hex_line (c, &i);
1163
0
      }
1164
0
    else
1165
0
      {
1166
0
        for (; pktlen; pktlen--)
1167
0
    {
1168
0
      dump_hex_line ((c = iobuf_get (inp)), &i);
1169
0
      if (c == -1)
1170
0
        break;
1171
0
    }
1172
0
      }
1173
0
    es_putc ('\n', listfp);
1174
0
    return;
1175
0
  }
1176
0
    }
1177
2
  iobuf_skip_rest (inp, pktlen, partial);
1178
2
}
1179
1180
1181
/* Read PKTLEN bytes from INP and return them in a newly allocated
1182
 * buffer.  In case of an error (including reading fewer than PKTLEN
1183
 * bytes from INP before EOF is returned), NULL is returned and an
1184
 * error message is logged.  */
1185
static void *
1186
read_rest (IOBUF inp, size_t pktlen)
1187
201
{
1188
201
  int c;
1189
201
  byte *buf, *p;
1190
1191
201
  buf = xtrymalloc (pktlen);
1192
201
  if (!buf)
1193
0
    {
1194
0
      gpg_error_t err = gpg_error_from_syserror ();
1195
0
      log_error ("error reading rest of packet: %s\n", gpg_strerror (err));
1196
0
      return NULL;
1197
0
    }
1198
11.7k
  for (p = buf; pktlen; pktlen--)
1199
11.5k
    {
1200
11.5k
      c = iobuf_get (inp);
1201
11.5k
      if (c == -1)
1202
0
        {
1203
0
          log_error ("premature eof while reading rest of packet\n");
1204
0
          xfree (buf);
1205
0
          return NULL;
1206
0
        }
1207
11.5k
      *p++ = c;
1208
11.5k
    }
1209
1210
201
  return buf;
1211
201
}
1212
1213
1214
/* Read a special size+body from INP.  On success store an opaque MPI
1215
 * with it at R_DATA.  The caller shall store the remaining size of
1216
 * the packet at PKTLEN.  On error return an error code and store NULL
1217
 * at R_DATA.  Even in the error case store the number of read bytes
1218
 * at PKTLEN is updated.  */
1219
static gpg_error_t
1220
read_sized_octet_string (iobuf_t inp, unsigned long *pktlen, gcry_mpi_t *r_data)
1221
443
{
1222
443
  char buffer[256];
1223
443
  char *tmpbuf;
1224
443
  int i, c, nbytes;
1225
1226
443
  *r_data = NULL;
1227
1228
443
  if (!*pktlen)
1229
0
    return gpg_error (GPG_ERR_INV_PACKET);
1230
443
  c = iobuf_readbyte (inp);
1231
443
  if (c < 0)
1232
0
    return gpg_error (GPG_ERR_INV_PACKET);
1233
443
  --*pktlen;
1234
443
  nbytes = c;
1235
443
  if (nbytes < 2 || nbytes > 254)
1236
0
    return gpg_error (GPG_ERR_INV_PACKET);
1237
443
  if (nbytes > *pktlen)
1238
0
    return gpg_error (GPG_ERR_INV_PACKET);
1239
1240
443
  buffer[0] = nbytes;
1241
1242
4.42k
  for (i = 0; i < nbytes; i++)
1243
3.98k
    {
1244
3.98k
      c = iobuf_get (inp);
1245
3.98k
      if (c < 0)
1246
1
        return gpg_error (GPG_ERR_INV_PACKET);
1247
3.98k
      --*pktlen;
1248
3.98k
      buffer[1+i] = c;
1249
3.98k
    }
1250
1251
442
  tmpbuf = xtrymalloc (1 + nbytes);
1252
442
  if (!tmpbuf)
1253
0
    return gpg_error_from_syserror ();
1254
442
  memcpy (tmpbuf, buffer, 1 + nbytes);
1255
442
  *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes));
1256
442
  if (!*r_data)
1257
0
    {
1258
0
      xfree (tmpbuf);
1259
0
      return gpg_error_from_syserror ();
1260
0
    }
1261
442
  return 0;
1262
442
}
1263
1264
1265
/* Parse a marker packet.  */
1266
static int
1267
parse_marker (IOBUF inp, int pkttype, unsigned long pktlen)
1268
0
{
1269
0
  (void) pkttype;
1270
1271
0
  if (pktlen != 3)
1272
0
    goto fail;
1273
1274
0
  if (iobuf_get (inp) != 'P')
1275
0
    {
1276
0
      pktlen--;
1277
0
      goto fail;
1278
0
    }
1279
1280
0
  if (iobuf_get (inp) != 'G')
1281
0
    {
1282
0
      pktlen--;
1283
0
      goto fail;
1284
0
    }
1285
1286
0
  if (iobuf_get (inp) != 'P')
1287
0
    {
1288
0
      pktlen--;
1289
0
      goto fail;
1290
0
    }
1291
1292
0
  if (list_mode)
1293
0
    es_fputs (":marker packet: PGP\n", listfp);
1294
1295
0
  return 0;
1296
1297
0
 fail:
1298
0
  log_error ("invalid marker packet\n");
1299
0
  if (list_mode)
1300
0
    es_fputs (":marker packet: [invalid]\n", listfp);
1301
0
  iobuf_skip_rest (inp, pktlen, 0);
1302
0
  return GPG_ERR_INV_PACKET;
1303
0
}
1304
1305
1306
static int
1307
parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1308
     PACKET * packet)
1309
0
{
1310
0
  PKT_symkey_enc *k;
1311
0
  int rc = 0;
1312
0
  int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen;
1313
1314
0
  if (pktlen < 4)
1315
0
    goto too_short;
1316
0
  version = iobuf_get_noeof (inp);
1317
0
  pktlen--;
1318
0
  if (version == 4)
1319
0
    ;
1320
0
  else if (version == 5)
1321
0
    ;
1322
0
  else
1323
0
    {
1324
0
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
1325
0
      if (list_mode)
1326
0
        es_fprintf (listfp, ":symkey enc packet: [unknown version]\n");
1327
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
1328
0
      goto leave;
1329
0
    }
1330
0
  if (pktlen > 200)
1331
0
    {       /* (we encode the seskeylen in a byte) */
1332
0
      log_error ("packet(%d) too large\n", pkttype);
1333
0
      if (list_mode)
1334
0
        es_fprintf (listfp, ":symkey enc packet: [too large]\n");
1335
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
1336
0
      goto leave;
1337
0
    }
1338
0
  cipher_algo = iobuf_get_noeof (inp);
1339
0
  pktlen--;
1340
0
  if (version == 5)
1341
0
    {
1342
0
      aead_algo = iobuf_get_noeof (inp);
1343
0
      pktlen--;
1344
0
    }
1345
0
  else
1346
0
    aead_algo = 0;
1347
0
  if (pktlen < 2)
1348
0
    goto too_short;
1349
0
  s2kmode = iobuf_get_noeof (inp);
1350
0
  pktlen--;
1351
0
  hash_algo = iobuf_get_noeof (inp);
1352
0
  pktlen--;
1353
0
  switch (s2kmode)
1354
0
    {
1355
0
    case 0: /* Simple S2K.  */
1356
0
      minlen = 0;
1357
0
      break;
1358
0
    case 1: /* Salted S2K.  */
1359
0
      minlen = 8;
1360
0
      break;
1361
0
    case 3: /* Iterated+salted S2K.  */
1362
0
      minlen = 9;
1363
0
      break;
1364
0
    default:
1365
0
      log_error ("unknown S2K mode %d\n", s2kmode);
1366
0
      if (list_mode)
1367
0
        es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n");
1368
0
      goto leave;
1369
0
    }
1370
0
  if (minlen > pktlen)
1371
0
    {
1372
0
      log_error ("packet with S2K %d too short\n", s2kmode);
1373
0
      if (list_mode)
1374
0
        es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1375
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
1376
0
      goto leave;
1377
0
    }
1378
0
  seskeylen = pktlen - minlen;
1379
0
  k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc);
1380
0
  k->version = version;
1381
0
  k->cipher_algo = cipher_algo;
1382
0
  k->aead_algo = aead_algo;
1383
0
  k->s2k.mode = s2kmode;
1384
0
  k->s2k.hash_algo = hash_algo;
1385
0
  if (s2kmode == 1 || s2kmode == 3)
1386
0
    {
1387
0
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
1388
0
  k->s2k.salt[i] = iobuf_get_noeof (inp);
1389
0
    }
1390
0
  if (s2kmode == 3)
1391
0
    {
1392
0
      k->s2k.count = iobuf_get_noeof (inp);
1393
0
      pktlen--;
1394
0
    }
1395
0
  k->seskeylen = seskeylen;
1396
0
  if (k->seskeylen)
1397
0
    {
1398
0
      k->seskey = xcalloc (1, seskeylen);
1399
0
      for (i = 0; i < seskeylen && pktlen; i++, pktlen--)
1400
0
  k->seskey[i] = iobuf_get_noeof (inp);
1401
1402
      /* What we're watching out for here is a session key decryptor
1403
         with no salt.  The RFC says that using salt for this is a
1404
         MUST. */
1405
0
      if (s2kmode != 1 && s2kmode != 3)
1406
0
  log_info (_("WARNING: potentially insecure symmetrically"
1407
0
        " encrypted session key\n"));
1408
0
    }
1409
0
  log_assert (!pktlen);
1410
1411
0
  if (list_mode)
1412
0
    {
1413
0
      es_fprintf (listfp,
1414
0
                  ":symkey enc packet: version %d, cipher %d, aead %d,"
1415
0
                  " s2k %d, hash %d",
1416
0
                  version, cipher_algo, aead_algo, s2kmode, hash_algo);
1417
0
      if (seskeylen)
1418
0
        {
1419
          /* To compute the size of the session key we need to know
1420
           * the size of the AEAD nonce which we may not know.  Thus
1421
           * we show only the size of the entire encrypted session
1422
           * key.  */
1423
0
          if (aead_algo)
1424
0
            es_fprintf (listfp, ", encrypted seskey %d bytes", seskeylen);
1425
0
          else
1426
0
            es_fprintf (listfp, ", seskey %d bits", (seskeylen - 1) * 8);
1427
0
        }
1428
0
      es_fprintf (listfp, "\n");
1429
0
      if (s2kmode == 1 || s2kmode == 3)
1430
0
  {
1431
0
    es_fprintf (listfp, "\tsalt ");
1432
0
          es_write_hexstring (listfp, k->s2k.salt, 8, 0, NULL);
1433
0
    if (s2kmode == 3)
1434
0
      es_fprintf (listfp, ", count %lu (%lu)",
1435
0
                        S2K_DECODE_COUNT ((ulong) k->s2k.count),
1436
0
                        (ulong) k->s2k.count);
1437
0
    es_fprintf (listfp, "\n");
1438
0
  }
1439
0
    }
1440
1441
0
 leave:
1442
0
  iobuf_skip_rest (inp, pktlen, 0);
1443
0
  return rc;
1444
1445
0
 too_short:
1446
0
  log_error ("packet(%d) too short\n", pkttype);
1447
0
  if (list_mode)
1448
0
    es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1449
0
  rc = gpg_error (GPG_ERR_INV_PACKET);
1450
0
  goto leave;
1451
0
}
1452
1453
1454
/* Parse a public key encrypted packet (Tag 1).  */
1455
static int
1456
parse_pubkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1457
     PACKET * packet)
1458
0
{
1459
0
  int rc = 0;
1460
0
  int i, ndata;
1461
0
  unsigned int n;
1462
0
  PKT_pubkey_enc *k;
1463
1464
0
  k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc);
1465
0
  if (pktlen < 12)
1466
0
    {
1467
0
      log_error ("packet(%d) too short\n", pkttype);
1468
0
      if (list_mode)
1469
0
        es_fputs (":pubkey enc packet: [too short]\n", listfp);
1470
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
1471
0
      goto leave;
1472
0
    }
1473
0
  k->version = iobuf_get_noeof (inp);
1474
0
  pktlen--;
1475
0
  if (k->version != 2 && k->version != 3)
1476
0
    {
1477
0
      log_error ("packet(%d) with unknown version %d\n", pkttype, k->version);
1478
0
      if (list_mode)
1479
0
        es_fputs (":pubkey enc packet: [unknown version]\n", listfp);
1480
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
1481
0
      goto leave;
1482
0
    }
1483
0
  k->keyid[0] = read_32 (inp);
1484
0
  pktlen -= 4;
1485
0
  k->keyid[1] = read_32 (inp);
1486
0
  pktlen -= 4;
1487
0
  k->pubkey_algo = iobuf_get_noeof (inp);
1488
0
  pktlen--;
1489
0
  k->throw_keyid = 0;  /* Only used as flag for build_packet.  */
1490
0
  if (list_mode)
1491
0
    es_fprintf (listfp,
1492
0
                ":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n",
1493
0
                k->version, k->pubkey_algo, (ulong) k->keyid[0],
1494
0
                (ulong) k->keyid[1]);
1495
1496
0
  ndata = pubkey_get_nenc (k->pubkey_algo);
1497
0
  if (!ndata)
1498
0
    {
1499
0
      if (list_mode)
1500
0
  es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo);
1501
0
      unknown_pubkey_warning (k->pubkey_algo);
1502
0
      k->data[0] = NULL; /* No need to store the encrypted data.  */
1503
0
    }
1504
0
  else if (k->pubkey_algo == PUBKEY_ALGO_ECDH)
1505
0
    {
1506
0
      log_assert (ndata == 2);
1507
      /* Get the ephemeral public key.  */
1508
0
      n = pktlen;
1509
0
      k->data[0] = sos_read (inp, &n, 0);
1510
0
      pktlen -= n;
1511
0
      if (!k->data[0])
1512
0
        {
1513
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
1514
0
          goto leave;
1515
0
        }
1516
      /* Get the wrapped symmetric key.  */
1517
0
      rc = read_sized_octet_string (inp, &pktlen, k->data + 1);
1518
0
      if (rc)
1519
0
        goto leave;
1520
0
    }
1521
0
  else if (k->pubkey_algo == PUBKEY_ALGO_KYBER)
1522
0
    {
1523
0
      log_assert (ndata == 3);
1524
      /* Get the ephemeral public key.  */
1525
0
      n = pktlen;
1526
0
      k->data[0] = sos_read (inp, &n, 0);
1527
0
      pktlen -= n;
1528
0
      if (!k->data[0])
1529
0
        {
1530
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
1531
0
          goto leave;
1532
0
        }
1533
      /* Get the Kyber ciphertext.  */
1534
0
      rc = read_octet_string (inp, &pktlen, 4, 0, 0, k->data + 1);
1535
0
      if (rc)
1536
0
        goto leave;
1537
      /* Get the algorithm id for the session key.  */
1538
0
      if (!pktlen)
1539
0
        {
1540
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
1541
0
          goto leave;
1542
0
        }
1543
0
      k->seskey_algo = iobuf_get_noeof (inp);
1544
0
      pktlen--;
1545
      /* Get the encrypted symmetric key.  */
1546
0
      rc = read_octet_string (inp, &pktlen, 1, 0, 0, k->data + 2);
1547
0
      if (rc)
1548
0
        goto leave;
1549
0
    }
1550
0
  else
1551
0
    {
1552
0
      for (i = 0; i < ndata; i++)
1553
0
        {
1554
0
          n = pktlen;
1555
0
          k->data[i] = mpi_read (inp, &n, 0);
1556
0
          pktlen -= n;
1557
0
          if (!k->data[i])
1558
0
            rc = gpg_error (GPG_ERR_INV_PACKET);
1559
0
        }
1560
0
      if (rc)
1561
0
        goto leave;
1562
0
    }
1563
0
  if (list_mode)
1564
0
    {
1565
0
      if (k->seskey_algo)
1566
0
        es_fprintf (listfp, "\tsession key algo: %d\n", k->seskey_algo);
1567
0
      for (i = 0; i < ndata; i++)
1568
0
        {
1569
0
          es_fprintf (listfp, "\tdata: ");
1570
0
          mpi_print (listfp, k->data[i], mpi_print_mode);
1571
0
          es_putc ('\n', listfp);
1572
0
        }
1573
0
    }
1574
1575
1576
0
 leave:
1577
0
  iobuf_skip_rest (inp, pktlen, 0);
1578
0
  return rc;
1579
0
}
1580
1581
1582
/* Dump a subpacket to LISTFP.  BUFFER contains the subpacket in
1583
 * question and points to the type field in the subpacket header (not
1584
 * the start of the header).  TYPE is the subpacket's type with the
1585
 * critical bit cleared.  CRITICAL is the value of the CRITICAL bit.
1586
 * BUFLEN is the length of the buffer and LENGTH is the length of the
1587
 * subpacket according to the subpacket's header.  DIGEST_ALGO is the
1588
 * digest algo of the signature.  */
1589
static void
1590
dump_sig_subpkt (int hashed, int type, int critical,
1591
     const byte * buffer, size_t buflen, size_t length,
1592
                 int digest_algo)
1593
0
{
1594
0
  const char *p = NULL;
1595
0
  int i;
1596
0
  int nprinted;
1597
1598
  /* The CERT has warning out with explains how to use GNUPG to detect
1599
   * the ARRs - we print our old message here when it is a faked ARR
1600
   * and add an additional notice.  */
1601
0
  if (type == SIGSUBPKT_ARR && !hashed)
1602
0
    {
1603
0
      es_fprintf (listfp,
1604
0
                  "\tsubpkt %d len %u (additional recipient request)\n"
1605
0
                  "WARNING: PGP versions > 5.0 and < 6.5.8 will automagically "
1606
0
                  "encrypt to this key and thereby reveal the plaintext to "
1607
0
                  "the owner of this ARR key. Detailed info follows:\n",
1608
0
                  type, (unsigned) length);
1609
0
    }
1610
1611
0
  buffer++;
1612
0
  length--;
1613
1614
0
  nprinted = es_fprintf (listfp, "\t%s%ssubpkt %d len %u (", /*) */
1615
0
                         critical ? "critical " : "",
1616
0
                         hashed ? "hashed " : "", type, (unsigned) length);
1617
0
  if (nprinted < 1)
1618
0
    nprinted = 1; /*(we use (nprinted-1) later.)*/
1619
0
  if (length > buflen)
1620
0
    {
1621
0
      es_fprintf (listfp, "too short: buffer is only %u)\n", (unsigned) buflen);
1622
0
      return;
1623
0
    }
1624
0
  switch (type)
1625
0
    {
1626
0
    case SIGSUBPKT_SIG_CREATED:
1627
0
      if (length >= 4)
1628
0
  es_fprintf (listfp, "sig created %s",
1629
0
                    strtimestamp (buf32_to_u32 (buffer)));
1630
0
      break;
1631
0
    case SIGSUBPKT_SIG_EXPIRE:
1632
0
      if (length >= 4)
1633
0
  {
1634
0
    if (buf32_to_u32 (buffer))
1635
0
      es_fprintf (listfp, "sig expires after %s",
1636
0
                        strtimevalue (buf32_to_u32 (buffer)));
1637
0
    else
1638
0
      es_fprintf (listfp, "sig does not expire");
1639
0
  }
1640
0
      break;
1641
0
    case SIGSUBPKT_EXPORTABLE:
1642
0
      if (length)
1643
0
  es_fprintf (listfp, "%sexportable", *buffer ? "" : "not ");
1644
0
      break;
1645
0
    case SIGSUBPKT_TRUST:
1646
0
      if (length != 2)
1647
0
  p = "[invalid trust subpacket]";
1648
0
      else
1649
0
  es_fprintf (listfp, "trust signature of depth %d, value %d", buffer[0],
1650
0
                    buffer[1]);
1651
0
      break;
1652
0
    case SIGSUBPKT_REGEXP:
1653
0
      if (!length)
1654
0
  p = "[invalid regexp subpacket]";
1655
0
      else
1656
0
        {
1657
0
          es_fprintf (listfp, "regular expression: \"");
1658
0
          es_write_sanitized (listfp, buffer, length, "\"", NULL);
1659
0
          p = "\"";
1660
0
        }
1661
0
      break;
1662
0
    case SIGSUBPKT_REVOCABLE:
1663
0
      if (length)
1664
0
  es_fprintf (listfp, "%srevocable", *buffer ? "" : "not ");
1665
0
      break;
1666
0
    case SIGSUBPKT_KEY_EXPIRE:
1667
0
      if (length >= 4)
1668
0
  {
1669
0
    if (buf32_to_u32 (buffer))
1670
0
      es_fprintf (listfp, "key expires after %s",
1671
0
                        strtimevalue (buf32_to_u32 (buffer)));
1672
0
    else
1673
0
      es_fprintf (listfp, "key does not expire");
1674
0
  }
1675
0
      break;
1676
0
    case SIGSUBPKT_PREF_SYM:
1677
0
      es_fputs ("pref-sym-algos:", listfp);
1678
0
      for (i = 0; i < length; i++)
1679
0
  es_fprintf (listfp, " %d", buffer[i]);
1680
0
      break;
1681
0
    case SIGSUBPKT_PREF_AEAD:
1682
0
      es_fputs ("pref-aead-algos:", listfp);
1683
0
      for (i = 0; i < length; i++)
1684
0
        es_fprintf (listfp, " %d", buffer[i]);
1685
0
      break;
1686
0
    case SIGSUBPKT_REV_KEY:
1687
0
      es_fputs ("revocation key: ", listfp);
1688
0
      if (length < 22)
1689
0
  p = "[too short]";
1690
0
      else
1691
0
  {
1692
0
    es_fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1]);
1693
0
    for (i = 2; i < length; i++)
1694
0
      es_fprintf (listfp, "%02X", buffer[i]);
1695
0
  }
1696
0
      break;
1697
0
    case SIGSUBPKT_ISSUER:
1698
0
      if (length >= 8)
1699
0
  es_fprintf (listfp, "issuer key ID %08lX%08lX",
1700
0
                    (ulong) buf32_to_u32 (buffer),
1701
0
                    (ulong) buf32_to_u32 (buffer + 4));
1702
0
      break;
1703
0
    case SIGSUBPKT_ISSUER_FPR:
1704
0
      if (length >= 21)
1705
0
        {
1706
0
          char *tmp;
1707
0
          es_fprintf (listfp, "issuer fpr v%d ", buffer[0]);
1708
0
          tmp = bin2hex (buffer+1, length-1, NULL);
1709
0
          if (tmp)
1710
0
            {
1711
0
              es_fputs (tmp, listfp);
1712
0
              xfree (tmp);
1713
0
            }
1714
0
        }
1715
0
      break;
1716
0
    case SIGSUBPKT_NOTATION:
1717
0
      {
1718
0
  es_fputs ("notation: ", listfp);
1719
0
  if (length < 8)
1720
0
    p = "[too short]";
1721
0
  else
1722
0
    {
1723
0
      const byte *s = buffer;
1724
0
      size_t n1, n2;
1725
1726
0
      n1 = (s[4] << 8) | s[5];
1727
0
      n2 = (s[6] << 8) | s[7];
1728
0
      s += 8;
1729
0
      if (8 + n1 + n2 != length)
1730
0
        p = "[error]";
1731
0
      else
1732
0
        {
1733
0
    es_write_sanitized (listfp, s, n1, ")", NULL);
1734
0
    es_putc ('=', listfp);
1735
1736
0
    if (*buffer & 0x80)
1737
0
      es_write_sanitized (listfp, s + n1, n2, ")", NULL);
1738
0
    else
1739
0
      p = "[not human readable]";
1740
0
        }
1741
0
    }
1742
0
      }
1743
0
      break;
1744
0
    case SIGSUBPKT_PREF_HASH:
1745
0
      es_fputs ("pref-hash-algos:", listfp);
1746
0
      for (i = 0; i < length; i++)
1747
0
  es_fprintf (listfp, " %d", buffer[i]);
1748
0
      break;
1749
0
    case SIGSUBPKT_PREF_COMPR:
1750
0
      es_fputs ("pref-zip-algos:", listfp);
1751
0
      for (i = 0; i < length; i++)
1752
0
  es_fprintf (listfp, " %d", buffer[i]);
1753
0
      break;
1754
0
    case SIGSUBPKT_KS_FLAGS:
1755
0
      es_fputs ("keyserver preferences:", listfp);
1756
0
      for (i = 0; i < length; i++)
1757
0
  es_fprintf (listfp, " %02X", buffer[i]);
1758
0
      break;
1759
0
    case SIGSUBPKT_PREF_KS:
1760
0
      es_fputs ("preferred keyserver: ", listfp);
1761
0
      es_write_sanitized (listfp, buffer, length, ")", NULL);
1762
0
      break;
1763
0
    case SIGSUBPKT_PRIMARY_UID:
1764
0
      p = "primary user ID";
1765
0
      break;
1766
0
    case SIGSUBPKT_POLICY:
1767
0
      es_fputs ("policy: ", listfp);
1768
0
      es_write_sanitized (listfp, buffer, length, ")", NULL);
1769
0
      break;
1770
0
    case SIGSUBPKT_KEY_FLAGS:
1771
0
      es_fputs ("key flags:", listfp);
1772
0
      for (i = 0; i < length; i++)
1773
0
  es_fprintf (listfp, " %02X", buffer[i]);
1774
0
      break;
1775
0
    case SIGSUBPKT_SIGNERS_UID:
1776
0
      p = "signer's user ID";
1777
0
      break;
1778
0
    case SIGSUBPKT_REVOC_REASON:
1779
0
      if (length)
1780
0
  {
1781
0
    es_fprintf (listfp, "revocation reason 0x%02x (", *buffer);
1782
0
    es_write_sanitized (listfp, buffer + 1, length - 1, ")", NULL);
1783
0
    p = ")";
1784
0
  }
1785
0
      break;
1786
0
    case SIGSUBPKT_ARR:
1787
0
      es_fputs ("Big Brother's key (ignored): ", listfp);
1788
0
      if (length < 22)
1789
0
  p = "[too short]";
1790
0
      else
1791
0
  {
1792
0
    es_fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1]);
1793
0
          if (length > 2)
1794
0
            es_write_hexstring (listfp, buffer+2, length-2, 0, NULL);
1795
0
  }
1796
0
      break;
1797
0
    case SIGSUBPKT_FEATURES:
1798
0
      es_fputs ("features:", listfp);
1799
0
      for (i = 0; i < length; i++)
1800
0
  es_fprintf (listfp, " %02x", buffer[i]);
1801
0
      break;
1802
0
    case SIGSUBPKT_SIGNATURE:
1803
0
      es_fputs ("signature: ", listfp);
1804
0
      if (length < 17)
1805
0
  p = "[too short]";
1806
0
      else
1807
0
  es_fprintf (listfp, "v%d, class 0x%02X, algo %d, digest algo %d",
1808
0
                    buffer[0],
1809
0
                    buffer[0] == 3 ? buffer[2] : buffer[1],
1810
0
                    buffer[0] == 3 ? buffer[15] : buffer[2],
1811
0
                    buffer[0] == 3 ? buffer[16] : buffer[3]);
1812
0
      break;
1813
1814
0
    case SIGSUBPKT_ATTST_SIGS:
1815
0
      {
1816
0
        unsigned int hlen;
1817
1818
0
  es_fputs ("attst-sigs: ", listfp);
1819
0
        hlen = gcry_md_get_algo_dlen (map_md_openpgp_to_gcry (digest_algo));
1820
0
  if (!hlen)
1821
0
    p = "[unknown digest algo]";
1822
0
        else if ((length % hlen))
1823
0
    p = "[invalid length]";
1824
0
  else
1825
0
    {
1826
0
            es_fprintf (listfp, "%u", (unsigned int)length/hlen);
1827
0
            while (length)
1828
0
              {
1829
0
                es_fprintf (listfp, "\n\t%*s", nprinted-1, "");
1830
0
                es_write_hexstring (listfp, buffer, hlen, 0, NULL);
1831
0
                buffer += hlen;
1832
0
                length -= hlen;
1833
0
              }
1834
0
    }
1835
0
      }
1836
0
      break;
1837
1838
0
    case SIGSUBPKT_KEY_BLOCK:
1839
0
      es_fputs ("key-block: ", listfp);
1840
0
      if (length && buffer[0])
1841
0
        p = "[unknown reserved octet]";
1842
0
      else if (length < 50)  /* 50 is an arbitrary min. length.  */
1843
0
        p = "[invalid subpacket]";
1844
0
      else
1845
0
        {
1846
          /* estream_t fp; */
1847
          /* fp = es_fopen ("a.key-block", "wb"); */
1848
          /* log_assert (fp); */
1849
          /* es_fwrite ( buffer+1, length-1, 1, fp); */
1850
          /* es_fclose (fp); */
1851
0
          es_fprintf (listfp, "[%u octets]", (unsigned int)length-1);
1852
0
        }
1853
0
      break;
1854
1855
1856
0
    default:
1857
0
      if (type >= 100 && type <= 110)
1858
0
  p = "experimental / private subpacket";
1859
0
      else
1860
0
  p = "?";
1861
0
      break;
1862
0
    }
1863
1864
0
  es_fprintf (listfp, "%s)\n", p ? p : "");
1865
0
}
1866
1867
1868
/*
1869
 * Returns: >= 0 use this offset into buffer
1870
 *      -1 explicitly reject returning this type
1871
 *      -2 subpacket too short
1872
 */
1873
int
1874
parse_one_sig_subpkt (const byte * buffer, size_t n, int type)
1875
540
{
1876
540
  switch (type)
1877
540
    {
1878
2
    case SIGSUBPKT_REV_KEY:
1879
2
      if (n < 22)
1880
0
  break;
1881
2
      return 0;
1882
261
    case SIGSUBPKT_SIG_CREATED:
1883
261
    case SIGSUBPKT_SIG_EXPIRE:
1884
275
    case SIGSUBPKT_KEY_EXPIRE:
1885
275
      if (n < 4)
1886
0
  break;
1887
275
      return 0;
1888
0
    case SIGSUBPKT_KEY_FLAGS:
1889
0
    case SIGSUBPKT_KS_FLAGS:
1890
0
    case SIGSUBPKT_PREF_SYM:
1891
0
    case SIGSUBPKT_PREF_AEAD:
1892
0
    case SIGSUBPKT_PREF_HASH:
1893
0
    case SIGSUBPKT_PREF_COMPR:
1894
0
    case SIGSUBPKT_POLICY:
1895
0
    case SIGSUBPKT_PREF_KS:
1896
0
    case SIGSUBPKT_FEATURES:
1897
0
    case SIGSUBPKT_REGEXP:
1898
0
    case SIGSUBPKT_ATTST_SIGS:
1899
0
      return 0;
1900
0
    case SIGSUBPKT_SIGNATURE:
1901
0
    case SIGSUBPKT_EXPORTABLE:
1902
2
    case SIGSUBPKT_REVOCABLE:
1903
2
    case SIGSUBPKT_REVOC_REASON:
1904
2
      if (!n)
1905
0
  break;
1906
2
      return 0;
1907
39
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1908
39
      if (n < 8)
1909
0
  break;
1910
39
      return 0;
1911
222
    case SIGSUBPKT_ISSUER_FPR:  /* issuer key fingerprint */
1912
222
      if (n < 21)
1913
0
  break;
1914
222
      return 0;
1915
0
    case SIGSUBPKT_NOTATION:
1916
      /* minimum length needed, and the subpacket must be well-formed
1917
         where the name length and value length all fit inside the
1918
         packet. */
1919
0
      if (n < 8
1920
0
    || 8 + ((buffer[4] << 8) | buffer[5]) +
1921
0
    ((buffer[6] << 8) | buffer[7]) != n)
1922
0
  break;
1923
0
      return 0;
1924
0
    case SIGSUBPKT_PRIMARY_UID:
1925
0
      if (n != 1)
1926
0
  break;
1927
0
      return 0;
1928
0
    case SIGSUBPKT_TRUST:
1929
0
      if (n != 2)
1930
0
  break;
1931
0
      return 0;
1932
0
    case SIGSUBPKT_KEY_BLOCK:
1933
0
      if (n && buffer[0])
1934
0
        return -1; /* Unknown version - ignore.  */
1935
0
      if (n < 50)
1936
0
  break;  /* Definitely too short to carry a key block.  */
1937
0
      return 0;
1938
0
    default:
1939
0
      return 0;
1940
540
    }
1941
0
  return -2;
1942
540
}
1943
1944
1945
/* Return true if we understand the critical notation.  */
1946
static int
1947
can_handle_critical_notation (const byte *name, size_t len)
1948
0
{
1949
0
  strlist_t sl;
1950
1951
0
  register_known_notation (NULL); /* Make sure it is initialized.  */
1952
1953
0
  for (sl = known_notations_list; sl; sl = sl->next)
1954
0
    if (sl->flags == len && !memcmp (sl->d, name, len))
1955
0
      return 1; /* Known */
1956
1957
0
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
1958
0
    {
1959
0
      log_info(_("Unknown critical signature notation: ") );
1960
0
      print_utf8_buffer (log_get_stream(), name, len);
1961
0
      log_printf ("\n");
1962
0
    }
1963
1964
0
  return 0; /* Unknown.  */
1965
0
}
1966
1967
1968
static int
1969
can_handle_critical (const byte * buffer, size_t n, int type)
1970
0
{
1971
0
  switch (type)
1972
0
    {
1973
0
    case SIGSUBPKT_NOTATION:
1974
0
      if (n >= 8)
1975
0
  {
1976
0
    size_t notation_len = ((buffer[4] << 8) | buffer[5]);
1977
0
    if (n - 8 >= notation_len)
1978
0
      return can_handle_critical_notation (buffer + 8, notation_len);
1979
0
  }
1980
0
      return 0;
1981
0
    case SIGSUBPKT_SIGNATURE:
1982
0
    case SIGSUBPKT_SIG_CREATED:
1983
0
    case SIGSUBPKT_SIG_EXPIRE:
1984
0
    case SIGSUBPKT_KEY_EXPIRE:
1985
0
    case SIGSUBPKT_EXPORTABLE:
1986
0
    case SIGSUBPKT_REVOCABLE:
1987
0
    case SIGSUBPKT_REV_KEY:
1988
0
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1989
0
    case SIGSUBPKT_ISSUER_FPR:  /* issuer fingerprint */
1990
0
    case SIGSUBPKT_PREF_SYM:
1991
0
    case SIGSUBPKT_PREF_AEAD:
1992
0
    case SIGSUBPKT_PREF_HASH:
1993
0
    case SIGSUBPKT_PREF_COMPR:
1994
0
    case SIGSUBPKT_KEY_FLAGS:
1995
0
    case SIGSUBPKT_PRIMARY_UID:
1996
0
    case SIGSUBPKT_FEATURES:
1997
0
    case SIGSUBPKT_TRUST:
1998
0
    case SIGSUBPKT_REGEXP:
1999
0
    case SIGSUBPKT_ATTST_SIGS:
2000
      /* Is it enough to show the policy or keyserver? */
2001
0
    case SIGSUBPKT_POLICY:
2002
0
    case SIGSUBPKT_PREF_KS:
2003
0
    case SIGSUBPKT_REVOC_REASON: /* At least we know about it.  */
2004
0
      return 1;
2005
2006
0
    case SIGSUBPKT_KEY_BLOCK:
2007
0
      if (n && !buffer[0])
2008
0
        return 1;
2009
0
      else
2010
0
        return 0;
2011
2012
0
    default:
2013
0
      return 0;
2014
0
    }
2015
0
}
2016
2017
2018
const byte *
2019
enum_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype,
2020
     size_t *ret_n, int *start, int *critical)
2021
3.86k
{
2022
3.86k
  const byte *buffer;
2023
3.86k
  int buflen;
2024
3.86k
  int type;
2025
3.86k
  int critical_dummy;
2026
3.86k
  int offset;
2027
3.86k
  size_t n;
2028
3.86k
  const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed;
2029
3.86k
  int seq = 0;
2030
3.86k
  int reqseq = start ? *start : 0;
2031
2032
3.86k
  if (!critical)
2033
3.86k
    critical = &critical_dummy;
2034
2035
3.86k
  if (!pktbuf || reqseq == -1)
2036
0
    {
2037
0
      static char dummy[] = "x";
2038
      /* Return a value different from NULL to indicate that
2039
       * there is no critical bit we do not understand.  */
2040
0
      return reqtype ==  SIGSUBPKT_TEST_CRITICAL ? dummy : NULL;
2041
0
    }
2042
3.86k
  buffer = pktbuf->data;
2043
3.86k
  buflen = pktbuf->len;
2044
11.5k
  while (buflen)
2045
8.23k
    {
2046
8.23k
      n = *buffer++;
2047
8.23k
      buflen--;
2048
8.23k
      if (n == 255) /* 4 byte length header.  */
2049
0
  {
2050
0
    if (buflen < 4)
2051
0
      goto too_short;
2052
0
    n = buf32_to_size_t (buffer);
2053
0
    buffer += 4;
2054
0
    buflen -= 4;
2055
0
  }
2056
8.23k
      else if (n >= 192) /* 4 byte special encoded length header.  */
2057
0
  {
2058
0
    if (buflen < 2)
2059
0
      goto too_short;
2060
0
    n = ((n - 192) << 8) + *buffer + 192;
2061
0
    buffer++;
2062
0
    buflen--;
2063
0
  }
2064
8.23k
      if (buflen < n)
2065
0
  goto too_short;
2066
8.23k
      if (!buflen)
2067
0
        goto no_type_byte;
2068
8.23k
      type = *buffer;
2069
8.23k
      if (type & 0x80)
2070
0
  {
2071
0
    type &= 0x7f;
2072
0
    *critical = 1;
2073
0
  }
2074
8.23k
      else
2075
8.23k
  *critical = 0;
2076
8.23k
      if (!(++seq > reqseq))
2077
6
  ;
2078
8.22k
      else if (reqtype == SIGSUBPKT_TEST_CRITICAL)
2079
888
  {
2080
888
    if (*critical)
2081
0
      {
2082
0
        if (n - 1 > buflen + 1)
2083
0
    goto too_short;
2084
0
        if (!can_handle_critical (buffer + 1, n - 1, type))
2085
0
    {
2086
0
      if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2087
0
        log_info (_("subpacket of type %d has "
2088
0
        "critical bit set\n"), type);
2089
0
      if (start)
2090
0
        *start = seq;
2091
0
      return NULL; /* This is an error.  */
2092
0
    }
2093
0
      }
2094
888
  }
2095
7.33k
      else if (reqtype < 0) /* List packets.  */
2096
0
  dump_sig_subpkt (reqtype == SIGSUBPKT_LIST_HASHED,
2097
0
       type, *critical, buffer, buflen, n, sig->digest_algo);
2098
7.33k
      else if (type == reqtype) /* Found.  */
2099
540
  {
2100
540
    buffer++;
2101
540
    n--;
2102
540
    if (n > buflen)
2103
0
      goto too_short;
2104
540
    if (ret_n)
2105
224
      *ret_n = n;
2106
540
    offset = parse_one_sig_subpkt (buffer, n, type);
2107
540
    switch (offset)
2108
540
      {
2109
0
      case -2:
2110
0
        log_error ("subpacket of type %d too short\n", type);
2111
0
        return NULL;
2112
0
      case -1:
2113
0
        return NULL;
2114
540
      default:
2115
540
        break;
2116
540
      }
2117
540
    if (start)
2118
2
      *start = seq;
2119
540
    return buffer + offset;
2120
540
  }
2121
7.69k
      buffer += n;
2122
7.69k
      buflen -= n;
2123
7.69k
    }
2124
3.32k
  if (reqtype == SIGSUBPKT_TEST_CRITICAL)
2125
    /* Returning NULL means we found a subpacket with the critical bit
2126
       set that we don't grok.  We've iterated over all the subpackets
2127
       and haven't found such a packet so we need to return a non-NULL
2128
       value.  */
2129
522
    return buffer;
2130
2131
  /* Critical bit we don't understand. */
2132
2.80k
  if (start)
2133
101
    *start = -1;
2134
2.80k
  return NULL;  /* End of packets; not found.  */
2135
2136
0
 too_short:
2137
0
  if (opt.debug && !glo_ctrl.silence_parse_warnings)
2138
0
    {
2139
0
      es_fflush (es_stdout);
2140
0
      log_printhex (pktbuf->data, pktbuf->len > 16? 16 : pktbuf->len,
2141
0
                    "buffer shorter than subpacket (%zu/%d/%zu); dump:",
2142
0
                    pktbuf->len, buflen, n);
2143
0
    }
2144
2145
0
  if (start)
2146
0
    *start = -1;
2147
0
  return NULL;
2148
2149
0
 no_type_byte:
2150
0
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2151
0
    log_info ("type octet missing in subpacket\n");
2152
0
  if (start)
2153
0
    *start = -1;
2154
0
  return NULL;
2155
3.32k
}
2156
2157
2158
const byte *
2159
parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype,
2160
      size_t *ret_n)
2161
3.76k
{
2162
3.76k
  return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL);
2163
3.76k
}
2164
2165
2166
const byte *
2167
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype)
2168
300
{
2169
300
  const byte *p;
2170
2171
300
  p = parse_sig_subpkt (sig, 1, reqtype, NULL);
2172
300
  if (!p)
2173
300
    p = parse_sig_subpkt (sig, 0, reqtype, NULL);
2174
300
  return p;
2175
300
}
2176
2177
2178
/* Find all revocation keys.  Look in hashed area only.  */
2179
void
2180
parse_revkeys (PKT_signature * sig)
2181
99
{
2182
99
  const byte *revkey;
2183
99
  int seq = 0;
2184
99
  size_t len;
2185
2186
99
  if (sig->sig_class != 0x1F)
2187
0
    return;
2188
2189
101
  while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY,
2190
101
                                    &len, &seq, NULL)))
2191
2
    {
2192
      /* Consider only valid packets.  They must have a length of
2193
       * either 2+20 or 2+32 octets and bit 7 of the class octet must
2194
       * be set.  */
2195
2
      if ((len == 22 || len == 34)
2196
2
          && (revkey[0] & 0x80))
2197
2
  {
2198
2
    sig->revkey = xrealloc (sig->revkey,
2199
2
          sizeof (struct revocation_key) *
2200
2
          (sig->numrevkeys + 1));
2201
2202
2
    sig->revkey[sig->numrevkeys].class = revkey[0];
2203
2
    sig->revkey[sig->numrevkeys].algid = revkey[1];
2204
2
          len -= 2;
2205
2
    sig->revkey[sig->numrevkeys].fprlen = len;
2206
2
    memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len);
2207
2
    memset (sig->revkey[sig->numrevkeys].fpr+len, 0,
2208
2
                  sizeof (sig->revkey[sig->numrevkeys].fpr) - len);
2209
2
    sig->numrevkeys++;
2210
2
  }
2211
2
    }
2212
99
}
2213
2214
2215
int
2216
parse_signature (IOBUF inp, int pkttype, unsigned long pktlen,
2217
     PKT_signature * sig)
2218
261
{
2219
261
  int md5_len = 0;
2220
261
  unsigned n;
2221
261
  int is_v4or5 = 0;
2222
261
  int rc = 0;
2223
261
  int i, ndata;
2224
2225
261
  if (pktlen < 16)
2226
0
    {
2227
0
      log_error ("packet(%d) too short\n", pkttype);
2228
0
      if (list_mode)
2229
0
        es_fputs (":signature packet: [too short]\n", listfp);
2230
0
      goto leave;
2231
0
    }
2232
261
  sig->version = iobuf_get_noeof (inp);
2233
261
  pktlen--;
2234
261
  if (sig->version == 4 || sig->version == 5)
2235
261
    is_v4or5 = 1;
2236
0
  else if (sig->version != 2 && sig->version != 3)
2237
0
    {
2238
0
      log_error ("packet(%d) with unknown version %d\n",
2239
0
     pkttype, sig->version);
2240
0
      if (list_mode)
2241
0
        es_fputs (":signature packet: [unknown version]\n", listfp);
2242
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
2243
0
      goto leave;
2244
0
    }
2245
2246
261
  if (!is_v4or5)
2247
0
    {
2248
0
      if (pktlen == 0)
2249
0
  goto underflow;
2250
0
      md5_len = iobuf_get_noeof (inp);
2251
0
      pktlen--;
2252
0
    }
2253
261
  if (pktlen == 0)
2254
0
    goto underflow;
2255
261
  sig->sig_class = iobuf_get_noeof (inp);
2256
261
  pktlen--;
2257
261
  if (!is_v4or5)
2258
0
    {
2259
0
      if (pktlen < 12)
2260
0
  goto underflow;
2261
0
      sig->timestamp = read_32 (inp);
2262
0
      pktlen -= 4;
2263
0
      sig->keyid[0] = read_32 (inp);
2264
0
      pktlen -= 4;
2265
0
      sig->keyid[1] = read_32 (inp);
2266
0
      pktlen -= 4;
2267
0
    }
2268
261
  if (pktlen < 2)
2269
0
    goto underflow;
2270
261
  sig->pubkey_algo = iobuf_get_noeof (inp);
2271
261
  pktlen--;
2272
261
  sig->digest_algo = iobuf_get_noeof (inp);
2273
261
  pktlen--;
2274
261
  sig->flags.exportable = 1;
2275
261
  sig->flags.revocable = 1;
2276
261
  if (is_v4or5) /* Read subpackets.  */
2277
261
    {
2278
261
      if (pktlen < 2)
2279
0
  goto underflow;
2280
261
      n = read_16 (inp);
2281
261
      pktlen -= 2;  /* Length of hashed data. */
2282
261
      if (pktlen < n)
2283
0
  goto underflow;
2284
261
      if (n > 30000)
2285
0
  {
2286
0
    log_error ("signature packet: hashed data too long (%u)\n", n);
2287
0
          if (list_mode)
2288
0
            es_fprintf (listfp,
2289
0
                        ":signature packet: [hashed data too long (%u)]\n", n);
2290
0
    goto leave;
2291
0
  }
2292
261
      if (n)
2293
261
  {
2294
261
    sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1);
2295
261
    sig->hashed->size = n;
2296
261
    sig->hashed->len = n;
2297
261
    if (iobuf_read (inp, sig->hashed->data, n) != n)
2298
0
      {
2299
0
        log_error ("premature eof while reading "
2300
0
       "hashed signature data\n");
2301
0
              if (list_mode)
2302
0
                es_fputs (":signature packet: [premature eof]\n", listfp);
2303
0
        rc = -1;
2304
0
        goto leave;
2305
0
      }
2306
261
    pktlen -= n;
2307
261
  }
2308
261
      if (pktlen < 2)
2309
0
  goto underflow;
2310
261
      n = read_16 (inp);
2311
261
      pktlen -= 2;  /* Length of unhashed data.  */
2312
261
      if (pktlen < n)
2313
0
  goto underflow;
2314
261
      if (n > 10000)
2315
0
  {
2316
0
    log_error ("signature packet: unhashed data too long (%u)\n", n);
2317
0
          if (list_mode)
2318
0
            es_fprintf (listfp,
2319
0
                        ":signature packet: [unhashed data too long (%u)]\n",
2320
0
                        n);
2321
0
    goto leave;
2322
0
  }
2323
261
      if (n)
2324
261
  {
2325
261
    sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1);
2326
261
    sig->unhashed->size = n;
2327
261
    sig->unhashed->len = n;
2328
261
    if (iobuf_read (inp, sig->unhashed->data, n) != n)
2329
0
      {
2330
0
        log_error ("premature eof while reading "
2331
0
       "unhashed signature data\n");
2332
0
              if (list_mode)
2333
0
                es_fputs (":signature packet: [premature eof]\n", listfp);
2334
0
        rc = -1;
2335
0
        goto leave;
2336
0
      }
2337
261
    pktlen -= n;
2338
261
  }
2339
261
    }
2340
2341
261
  if (pktlen < 2)
2342
0
    goto underflow;
2343
261
  sig->digest_start[0] = iobuf_get_noeof (inp);
2344
261
  pktlen--;
2345
261
  sig->digest_start[1] = iobuf_get_noeof (inp);
2346
261
  pktlen--;
2347
2348
261
  if (is_v4or5 && sig->pubkey_algo)  /* Extract required information.  */
2349
261
    {
2350
261
      const byte *p;
2351
261
      size_t len;
2352
2353
      /* Set sig->flags.unknown_critical if there is a critical bit
2354
       * set for packets which we do not understand.  */
2355
261
      if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL)
2356
261
    || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL))
2357
0
  sig->flags.unknown_critical = 1;
2358
2359
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL);
2360
261
      if (p)
2361
261
  sig->timestamp = buf32_to_u32 (p);
2362
0
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2363
0
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2364
0
        log_info ("signature packet without timestamp\n");
2365
2366
      /* Set the key id.  We first try the issuer fingerprint and if
2367
       * it is a v4 signature the fallback to the issuer.  Note that
2368
       * only the issuer packet is also searched in the unhashed area.  */
2369
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len);
2370
261
      if (p && len == 21 && p[0] == 4)
2371
222
        {
2372
222
          sig->keyid[0] = buf32_to_u32 (p + 1 + 12);
2373
222
    sig->keyid[1] = buf32_to_u32 (p + 1 + 16);
2374
222
  }
2375
39
      else if (p && len == 33 && p[0] == 5)
2376
0
        {
2377
0
          sig->keyid[0] = buf32_to_u32 (p + 1 );
2378
0
    sig->keyid[1] = buf32_to_u32 (p + 1 + 4);
2379
0
  }
2380
39
      else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER)))
2381
39
        {
2382
39
          sig->keyid[0] = buf32_to_u32 (p);
2383
39
    sig->keyid[1] = buf32_to_u32 (p + 4);
2384
39
  }
2385
0
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2386
0
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2387
0
  log_info ("signature packet without keyid\n");
2388
2389
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL);
2390
261
      if (p && buf32_to_u32 (p))
2391
0
  sig->expiredate = sig->timestamp + buf32_to_u32 (p);
2392
261
      if (sig->expiredate && sig->expiredate <= make_timestamp ())
2393
0
  sig->flags.expired = 1;
2394
2395
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL);
2396
261
      if (p)
2397
0
  sig->flags.policy_url = 1;
2398
2399
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL);
2400
261
      if (p)
2401
0
  sig->flags.pref_ks = 1;
2402
2403
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len);
2404
261
      if (p && len)
2405
0
        {
2406
0
          char *mbox;
2407
2408
0
          sig->signers_uid = try_make_printable_string (p, len, 0);
2409
0
          if (!sig->signers_uid)
2410
0
            {
2411
0
              rc = gpg_error_from_syserror ();
2412
0
              goto leave;
2413
0
            }
2414
0
          mbox = mailbox_from_userid (sig->signers_uid, 0);
2415
0
          if (mbox)
2416
0
            {
2417
0
              xfree (sig->signers_uid);
2418
0
              sig->signers_uid = mbox;
2419
0
            }
2420
0
        }
2421
2422
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL);
2423
261
      if (p)
2424
0
        sig->flags.key_block = 1;
2425
2426
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL);
2427
261
      if (p)
2428
0
  sig->flags.notation = 1;
2429
2430
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL);
2431
261
      if (p && *p == 0)
2432
2
  sig->flags.revocable = 0;
2433
2434
261
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len);
2435
261
      if (p && len == 2)
2436
0
  {
2437
0
    sig->trust_depth = p[0];
2438
0
    sig->trust_value = p[1];
2439
2440
    /* Only look for a regexp if there is also a trust
2441
       subpacket. */
2442
0
    sig->trust_regexp =
2443
0
      parse_sig_subpkt (sig, 1, SIGSUBPKT_REGEXP, &len);
2444
2445
    /* If the regular expression is of 0 length, there is no
2446
       regular expression. */
2447
0
    if (len == 0)
2448
0
      sig->trust_regexp = NULL;
2449
0
  }
2450
2451
      /* We accept the exportable subpacket from either the hashed or
2452
         unhashed areas as older versions of gpg put it in the
2453
         unhashed area.  In theory, anyway, we should never see this
2454
         packet off of a local keyring. */
2455
2456
261
      p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE);
2457
261
      if (p && *p == 0)
2458
0
  sig->flags.exportable = 0;
2459
2460
      /* Find all revocation keys.  */
2461
261
      if (sig->sig_class == 0x1F)
2462
99
  parse_revkeys (sig);
2463
261
    }
2464
2465
261
  if (list_mode)
2466
0
    {
2467
0
      es_fprintf (listfp, ":signature packet: algo %d, keyid %08lX%08lX\n"
2468
0
                  "\tversion %d, created %lu, md5len %d, sigclass 0x%02x\n"
2469
0
                  "\tdigest algo %d, begin of digest %02x %02x\n",
2470
0
                  sig->pubkey_algo,
2471
0
                  (ulong) sig->keyid[0], (ulong) sig->keyid[1],
2472
0
                  sig->version, (ulong) sig->timestamp, md5_len, sig->sig_class,
2473
0
                  sig->digest_algo, sig->digest_start[0], sig->digest_start[1]);
2474
0
      if (is_v4or5)
2475
0
  {
2476
0
    parse_sig_subpkt (sig, 1, SIGSUBPKT_LIST_HASHED, NULL);
2477
0
    parse_sig_subpkt (sig, 0, SIGSUBPKT_LIST_UNHASHED, NULL);
2478
0
  }
2479
0
    }
2480
2481
261
  ndata = pubkey_get_nsig (sig->pubkey_algo);
2482
261
  if (!ndata)
2483
0
    {
2484
0
      if (list_mode)
2485
0
  es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo);
2486
0
      unknown_pubkey_warning (sig->pubkey_algo);
2487
2488
      /* We store the plain material in data[0], so that we are able
2489
       * to write it back with build_packet().  */
2490
0
      if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8))
2491
0
  {
2492
    /* We include a limit to avoid too trivial DoS attacks by
2493
       having gpg allocate too much memory.  */
2494
0
    log_error ("signature packet: too much data\n");
2495
0
    rc = GPG_ERR_INV_PACKET;
2496
0
  }
2497
0
      else
2498
0
  {
2499
0
          void *tmpp;
2500
2501
0
          tmpp = read_rest (inp, pktlen);
2502
0
    sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2503
0
    pktlen = 0;
2504
0
  }
2505
0
    }
2506
261
  else
2507
261
    {
2508
783
      for (i = 0; i < ndata; i++)
2509
522
  {
2510
522
    n = pktlen;
2511
522
          if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA
2512
522
              || sig->pubkey_algo == PUBKEY_ALGO_EDDSA)
2513
522
            sig->data[i] = sos_read (inp, &n, 0);
2514
0
          else
2515
0
            sig->data[i] = mpi_read (inp, &n, 0);
2516
522
    pktlen -= n;
2517
522
    if (list_mode)
2518
0
      {
2519
0
        es_fprintf (listfp, "\tdata: ");
2520
0
        mpi_print (listfp, sig->data[i], mpi_print_mode);
2521
0
        es_putc ('\n', listfp);
2522
0
      }
2523
522
    if (!sig->data[i])
2524
0
      rc = GPG_ERR_INV_PACKET;
2525
522
  }
2526
261
    }
2527
2528
261
 leave:
2529
261
  iobuf_skip_rest (inp, pktlen, 0);
2530
261
  return rc;
2531
2532
0
 underflow:
2533
0
  log_error ("packet(%d) too short\n", pkttype);
2534
0
  if (list_mode)
2535
0
    es_fputs (":signature packet: [too short]\n", listfp);
2536
2537
0
  iobuf_skip_rest (inp, pktlen, 0);
2538
2539
0
  return GPG_ERR_INV_PACKET;
2540
261
}
2541
2542
2543
static int
2544
parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen,
2545
       PKT_onepass_sig * ops)
2546
0
{
2547
0
  int version;
2548
0
  int rc = 0;
2549
2550
0
  if (pktlen < 13)
2551
0
    {
2552
0
      log_error ("packet(%d) too short\n", pkttype);
2553
0
      if (list_mode)
2554
0
        es_fputs (":onepass_sig packet: [too short]\n", listfp);
2555
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
2556
0
      goto leave;
2557
0
    }
2558
0
  version = iobuf_get_noeof (inp);
2559
0
  pktlen--;
2560
0
  if (version != 3)
2561
0
    {
2562
0
      log_error ("onepass_sig with unknown version %d\n", version);
2563
0
      if (list_mode)
2564
0
        es_fputs (":onepass_sig packet: [unknown version]\n", listfp);
2565
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
2566
0
      goto leave;
2567
0
    }
2568
0
  ops->sig_class = iobuf_get_noeof (inp);
2569
0
  pktlen--;
2570
0
  ops->digest_algo = iobuf_get_noeof (inp);
2571
0
  pktlen--;
2572
0
  ops->pubkey_algo = iobuf_get_noeof (inp);
2573
0
  pktlen--;
2574
0
  ops->keyid[0] = read_32 (inp);
2575
0
  pktlen -= 4;
2576
0
  ops->keyid[1] = read_32 (inp);
2577
0
  pktlen -= 4;
2578
0
  ops->last = iobuf_get_noeof (inp);
2579
0
  pktlen--;
2580
0
  if (list_mode)
2581
0
    es_fprintf (listfp,
2582
0
                ":onepass_sig packet: keyid %08lX%08lX\n"
2583
0
                "\tversion %d, sigclass 0x%02x, digest %d, pubkey %d, "
2584
0
                "last=%d\n",
2585
0
                (ulong) ops->keyid[0], (ulong) ops->keyid[1],
2586
0
                version, ops->sig_class,
2587
0
                ops->digest_algo, ops->pubkey_algo, ops->last);
2588
2589
2590
0
 leave:
2591
0
  iobuf_skip_rest (inp, pktlen, 0);
2592
0
  return rc;
2593
0
}
2594
2595
2596
static int
2597
parse_key (IOBUF inp, int pkttype, unsigned long pktlen,
2598
     byte * hdr, int hdrlen, PACKET * pkt)
2599
443
{
2600
443
  gpg_error_t err = 0;
2601
443
  int i, version, algorithm;
2602
443
  unsigned long timestamp, expiredate, max_expiredate;
2603
443
  int npkey, nskey;
2604
443
  u32 keyid[2];
2605
443
  PKT_public_key *pk;
2606
443
  int is_v5;
2607
443
  unsigned int pkbytes; /* For v5 keys: Number of bytes in the public
2608
                         * key material.  For v4 keys: 0.  */
2609
2610
443
  (void) hdr;
2611
2612
443
  pk = pkt->pkt.public_key; /* PK has been cleared. */
2613
2614
443
  version = iobuf_get_noeof (inp);
2615
443
  pktlen--;
2616
443
  if (pkttype == PKT_PUBLIC_SUBKEY && version == '#')
2617
0
    {
2618
      /* Early versions of G10 used the old PGP comments packets;
2619
       * luckily all those comments are started by a hash.  */
2620
0
      if (list_mode)
2621
0
  {
2622
0
    es_fprintf (listfp, ":rfc1991 comment packet: \"");
2623
0
    for (; pktlen; pktlen--)
2624
0
      {
2625
0
        int c;
2626
0
        c = iobuf_get (inp);
2627
0
              if (c == -1)
2628
0
                break; /* Ooops: shorter than indicated.  */
2629
0
        if (c >= ' ' && c <= 'z')
2630
0
    es_putc (c, listfp);
2631
0
        else
2632
0
    es_fprintf (listfp, "\\x%02x", c);
2633
0
      }
2634
0
    es_fprintf (listfp, "\"\n");
2635
0
  }
2636
0
      iobuf_skip_rest (inp, pktlen, 0);
2637
0
      return 0;
2638
0
    }
2639
443
  else if (version == 4)
2640
443
    is_v5 = 0;
2641
0
  else if (version == 5)
2642
0
    is_v5 = 1;
2643
0
  else if (version == 2 || version == 3)
2644
0
    {
2645
      /* Not anymore supported since 2.1.  Use an older gpg version
2646
       * (i.e. gpg 1.4) to parse v3 packets.  */
2647
0
      if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2648
0
        log_info ("packet(%d) with obsolete version %d\n", pkttype, version);
2649
0
      if (list_mode)
2650
0
        es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version);
2651
0
      pk->version = version;
2652
0
      err = gpg_error (GPG_ERR_LEGACY_KEY);
2653
0
      goto leave;
2654
0
    }
2655
0
  else
2656
0
    {
2657
0
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
2658
0
      if (list_mode)
2659
0
        es_fputs (":key packet: [unknown version]\n", listfp);
2660
0
      err = gpg_error (GPG_ERR_INV_PACKET);
2661
0
      goto leave;
2662
0
    }
2663
2664
443
  if (pktlen < (is_v5? 15:11))
2665
0
    {
2666
0
      log_error ("packet(%d) too short\n", pkttype);
2667
0
      if (list_mode)
2668
0
        es_fputs (":key packet: [too short]\n", listfp);
2669
0
      err = gpg_error (GPG_ERR_INV_PACKET);
2670
0
      goto leave;
2671
0
    }
2672
443
  else if (pktlen > MAX_KEY_PACKET_LENGTH)
2673
0
    {
2674
0
      log_error ("packet(%d) too large\n", pkttype);
2675
0
      if (list_mode)
2676
0
        es_fputs (":key packet: [too large]\n", listfp);
2677
0
      err = gpg_error (GPG_ERR_INV_PACKET);
2678
0
      goto leave;
2679
0
    }
2680
2681
443
  timestamp = read_32 (inp);
2682
443
  pktlen -= 4;
2683
443
  expiredate = 0;   /* have to get it from the selfsignature */
2684
443
  max_expiredate = 0;
2685
443
  algorithm = iobuf_get_noeof (inp);
2686
443
  pktlen--;
2687
443
  if (is_v5)
2688
0
    {
2689
0
      pkbytes = read_32 (inp);
2690
0
      pktlen -= 4;
2691
0
    }
2692
443
  else
2693
443
    pkbytes = 0;
2694
2695
443
  if (list_mode)
2696
0
    {
2697
0
      es_fprintf (listfp, ":%s key packet:\n"
2698
0
                  "\tversion %d, algo %d, created %lu, expires %lu",
2699
0
                  pkttype == PKT_PUBLIC_KEY ? "public" :
2700
0
                  pkttype == PKT_SECRET_KEY ? "secret" :
2701
0
                  pkttype == PKT_PUBLIC_SUBKEY ? "public sub" :
2702
0
                  pkttype == PKT_SECRET_SUBKEY ? "secret sub" : "??",
2703
0
                  version, algorithm, timestamp, expiredate);
2704
0
      if (is_v5)
2705
0
        es_fprintf (listfp, ", pkbytes %u\n", pkbytes);
2706
0
      else
2707
0
        es_fprintf (listfp, "\n");
2708
0
    }
2709
2710
443
  pk->timestamp = timestamp;
2711
443
  pk->expiredate = expiredate;
2712
443
  pk->max_expiredate = max_expiredate;
2713
443
  pk->hdrbytes = hdrlen;
2714
443
  pk->version = version;
2715
443
  pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY);
2716
443
  pk->pubkey_algo = algorithm;
2717
2718
443
  nskey = pubkey_get_nskey (algorithm);
2719
443
  npkey = pubkey_get_npkey (algorithm);
2720
443
  if (!npkey)
2721
0
    {
2722
0
      if (list_mode)
2723
0
  es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm);
2724
0
      unknown_pubkey_warning (algorithm);
2725
0
    }
2726
2727
443
  if (!npkey)
2728
0
    {
2729
      /* Unknown algorithm - put data into an opaque MPI.  */
2730
0
      void *tmpp = read_rest (inp, pktlen);
2731
      /* Current gcry_mpi_cmp does not handle a (NULL,n>0) nicely and
2732
       * thus we avoid to create such an MPI.  */
2733
0
      pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2734
0
      pktlen = 0;
2735
0
      goto leave;
2736
0
    }
2737
443
  else
2738
443
    {
2739
1.32k
      for (i = 0; i < npkey; i++)
2740
885
        {
2741
885
          if (    (algorithm == PUBKEY_ALGO_ECDSA && (i == 0))
2742
885
               || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0))
2743
442
               || (algorithm == PUBKEY_ALGO_ECDH  && (i == 0 || i == 2))
2744
442
               || (algorithm == PUBKEY_ALGO_KYBER && (i == 0)))
2745
443
            {
2746
              /* Read the OID (i==0) or the KDF params (i==2).  */
2747
443
        err = read_sized_octet_string (inp, &pktlen, pk->pkey+i);
2748
443
            }
2749
442
          else if (algorithm == PUBKEY_ALGO_KYBER && i == 2)
2750
0
            {
2751
              /* Read the four-octet count prefixed Kyber public key.  */
2752
0
        err = read_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i);
2753
0
            }
2754
442
          else
2755
442
            {
2756
              /* Read MPI or SOS.  */
2757
442
              unsigned int n = pktlen;
2758
442
              if (algorithm == PUBKEY_ALGO_ECDSA
2759
442
                  || algorithm == PUBKEY_ALGO_EDDSA
2760
0
                  || algorithm == PUBKEY_ALGO_ECDH
2761
0
                  || algorithm == PUBKEY_ALGO_KYBER)
2762
442
                pk->pkey[i] = sos_read (inp, &n, 0);
2763
0
              else
2764
0
                pk->pkey[i] = mpi_read (inp, &n, 0);
2765
442
              pktlen -= n;
2766
442
              if (!pk->pkey[i])
2767
0
                err = gpg_error (GPG_ERR_INV_PACKET);
2768
442
            }
2769
885
          if (err)
2770
1
            goto leave;
2771
885
        }
2772
442
      if (list_mode)
2773
0
        {  /* Again so that we have all parameters in pkey[] and can
2774
            * do a look forward.  We use a hack for Kyber because the
2775
            * commonly used function pubkey_string requires an extra
2776
            * buffer and, more important, its result depends on an
2777
            * configure option.  */
2778
0
          for (i = 0; i < npkey; i++)
2779
0
            {
2780
0
              es_fprintf (listfp, "\tpkey[%d]: ", i);
2781
0
              mpi_print (listfp, pk->pkey[i], mpi_print_mode);
2782
0
              if ((algorithm == PUBKEY_ALGO_ECDSA
2783
0
                   || algorithm == PUBKEY_ALGO_EDDSA
2784
0
                   || algorithm == PUBKEY_ALGO_ECDH
2785
0
                   || algorithm == PUBKEY_ALGO_KYBER) && i==0)
2786
0
                {
2787
0
                  char *curve = openpgp_oid_to_str (pk->pkey[0]);
2788
0
                  const char *name = openpgp_oid_to_curve (curve, 2);
2789
2790
0
                  if (algorithm == PUBKEY_ALGO_KYBER)
2791
0
                    es_fprintf (listfp, " ky%u_%s (%s)",
2792
0
                                nbits_from_pk (pk), name?name:"", curve);
2793
0
                  else
2794
0
                    es_fprintf (listfp, " %s (%s)", name?name:"", curve);
2795
0
                  xfree (curve);
2796
0
                }
2797
0
              es_putc ('\n', listfp);
2798
0
            }
2799
0
        }
2800
442
    }
2801
442
  if (list_mode)
2802
0
    keyid_from_pk (pk, keyid);
2803
2804
442
  if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY)
2805
201
    {
2806
201
      struct seckey_info *ski;
2807
201
      byte temp[16];
2808
201
      size_t snlen = 0;
2809
201
      unsigned int skbytes;
2810
2811
201
      if (pktlen < 1)
2812
0
        {
2813
0
          err = gpg_error (GPG_ERR_INV_PACKET);
2814
0
          goto leave;
2815
0
        }
2816
2817
201
      pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
2818
201
      if (!pk->seckey_info)
2819
0
        {
2820
0
          err = gpg_error_from_syserror ();
2821
0
          goto leave;
2822
0
        }
2823
2824
201
      ski->algo = iobuf_get_noeof (inp);
2825
201
      pktlen--;
2826
2827
201
      if (is_v5)
2828
0
        {
2829
0
          unsigned int protcount = 0;
2830
2831
          /* Read the one octet count of the following key-protection
2832
           * material.  Only required in case of unknown values. */
2833
0
          if (!pktlen)
2834
0
            {
2835
0
              err = gpg_error (GPG_ERR_INV_PACKET);
2836
0
              goto leave;
2837
0
            }
2838
0
          protcount = iobuf_get_noeof (inp);
2839
0
          pktlen--;
2840
0
          if (list_mode)
2841
0
            es_fprintf (listfp, "\tprotbytes: %u\n", protcount);
2842
0
        }
2843
2844
201
      if (ski->algo)
2845
201
  {
2846
201
    ski->is_protected = 1;
2847
201
    ski->s2k.count = 0;
2848
201
    if (ski->algo == 253)
2849
0
      {
2850
0
              if (list_mode)
2851
0
                es_fprintf (listfp,
2852
0
                            "\tS2K pseudo algo %d is not yet supported\n",
2853
0
                            ski->algo);
2854
0
              err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2855
0
              goto leave;
2856
0
            }
2857
201
          else if (ski->algo == 254 || ski->algo == 255)
2858
165
      {
2859
165
              if (pktlen < 3)
2860
0
    {
2861
0
      err = gpg_error (GPG_ERR_INV_PACKET);
2862
0
      goto leave;
2863
0
    }
2864
2865
165
              ski->sha1chk = (ski->algo == 254);
2866
165
        ski->algo = iobuf_get_noeof (inp);
2867
165
        pktlen--;
2868
        /* Note that a ski->algo > 110 is illegal, but I'm not
2869
         * erroring out here as otherwise there would be no way
2870
         * to delete such a key.  */
2871
165
        ski->s2k.mode = iobuf_get_noeof (inp);
2872
165
        pktlen--;
2873
165
        ski->s2k.hash_algo = iobuf_get_noeof (inp);
2874
165
        pktlen--;
2875
        /* Check for the special GNU extension.  */
2876
165
        if (ski->s2k.mode == 101)
2877
0
    {
2878
0
      for (i = 0; i < 4 && pktlen; i++, pktlen--)
2879
0
        temp[i] = iobuf_get_noeof (inp);
2880
0
      if (i < 4 || memcmp (temp, "GNU", 3))
2881
0
        {
2882
0
          if (list_mode)
2883
0
      es_fprintf (listfp, "\tunknown S2K %d\n",
2884
0
                                    ski->s2k.mode);
2885
0
          err = gpg_error (GPG_ERR_INV_PACKET);
2886
0
          goto leave;
2887
0
        }
2888
      /* Here we know that it is a GNU extension.  What
2889
       * follows is the GNU protection mode: All values
2890
       * have special meanings and they are mapped to MODE
2891
       * with a base of 1000.  */
2892
0
      ski->s2k.mode = 1000 + temp[3];
2893
0
    }
2894
2895
              /* Read the salt.  */
2896
165
        if (ski->s2k.mode == 3 || ski->s2k.mode == 1)
2897
165
    {
2898
1.48k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
2899
1.32k
        temp[i] = iobuf_get_noeof (inp);
2900
165
                  if (i < 8)
2901
0
                    {
2902
0
          err = gpg_error (GPG_ERR_INV_PACKET);
2903
0
          goto leave;
2904
0
                    }
2905
165
      memcpy (ski->s2k.salt, temp, 8);
2906
165
    }
2907
2908
              /* Check the mode.  */
2909
165
        switch (ski->s2k.mode)
2910
165
    {
2911
0
    case 0:
2912
0
      if (list_mode)
2913
0
        es_fprintf (listfp, "\tsimple S2K");
2914
0
      break;
2915
0
    case 1:
2916
0
      if (list_mode)
2917
0
        es_fprintf (listfp, "\tsalted S2K");
2918
0
      break;
2919
165
    case 3:
2920
165
      if (list_mode)
2921
165
        es_fprintf (listfp, "\titer+salt S2K");
2922
165
      break;
2923
0
    case 1001:
2924
0
      if (list_mode)
2925
0
        es_fprintf (listfp, "\tgnu-dummy");
2926
0
      break;
2927
0
    case 1002:
2928
0
      if (list_mode)
2929
0
        es_fprintf (listfp, "\tgnu-divert-to-card");
2930
0
      break;
2931
0
    case 1003:
2932
0
      if (list_mode)
2933
0
        es_fprintf (listfp, "\tgnu-mode1003");
2934
0
      break;
2935
0
    default:
2936
0
      if (list_mode)
2937
0
        es_fprintf (listfp, "\tunknown %sS2K %d\n",
2938
0
                                ski->s2k.mode < 1000 ? "" : "GNU ",
2939
0
                                ski->s2k.mode);
2940
0
      err = gpg_error (GPG_ERR_INV_PACKET);
2941
0
      goto leave;
2942
165
    }
2943
2944
              /* Print some info.  */
2945
165
        if (list_mode && ski->s2k.mode != 1003)
2946
0
    {
2947
0
      es_fprintf (listfp, ", algo: %d,%s hash: %d",
2948
0
                              ski->algo,
2949
0
                              ski->sha1chk ? " SHA1 protection,"
2950
0
                              : " simple checksum,", ski->s2k.hash_algo);
2951
0
      if (ski->s2k.mode == 1 || ski->s2k.mode == 3)
2952
0
        {
2953
0
          es_fprintf (listfp, ", salt: ");
2954
0
                      es_write_hexstring (listfp, ski->s2k.salt, 8, 0, NULL);
2955
0
        }
2956
0
                }
2957
165
              if (list_mode)
2958
165
                es_putc ('\n', listfp);
2959
2960
              /* Read remaining protection parameters.  */
2961
165
        if (ski->s2k.mode == 3)
2962
165
    {
2963
165
      if (pktlen < 1)
2964
0
        {
2965
0
          err = gpg_error (GPG_ERR_INV_PACKET);
2966
0
          goto leave;
2967
0
        }
2968
165
      ski->s2k.count = iobuf_get_noeof (inp);
2969
165
      pktlen--;
2970
165
      if (list_mode)
2971
165
        es_fprintf (listfp, "\tprotect count: %lu (%lu)\n",
2972
0
                                (ulong)S2K_DECODE_COUNT ((ulong)ski->s2k.count),
2973
0
                                (ulong) ski->s2k.count);
2974
165
    }
2975
0
        else if (ski->s2k.mode == 1002)
2976
0
    {
2977
      /* Read the serial number. */
2978
0
      if (pktlen < 1)
2979
0
        {
2980
0
          err = gpg_error (GPG_ERR_INV_PACKET);
2981
0
          goto leave;
2982
0
        }
2983
0
      snlen = iobuf_get (inp);
2984
0
      pktlen--;
2985
0
      if (pktlen < snlen || snlen == (size_t)(-1))
2986
0
        {
2987
0
          err = gpg_error (GPG_ERR_INV_PACKET);
2988
0
          goto leave;
2989
0
        }
2990
0
    }
2991
165
      }
2992
36
    else /* Old version; no S2K, so we set mode to 0, hash MD5.  */
2993
36
      {
2994
              /* Note that a ski->algo > 110 is illegal, but I'm not
2995
                 erroring on it here as otherwise there would be no
2996
                 way to delete such a key.  */
2997
36
        ski->s2k.mode = 0;
2998
36
        ski->s2k.hash_algo = DIGEST_ALGO_MD5;
2999
36
        if (list_mode)
3000
36
    es_fprintf (listfp, "\tprotect algo: %d  (hash algo: %d)\n",
3001
0
                            ski->algo, ski->s2k.hash_algo);
3002
36
      }
3003
3004
    /* It is really ugly that we don't know the size
3005
     * of the IV here in cases we are not aware of the algorithm.
3006
     * so a
3007
     *   ski->ivlen = cipher_get_blocksize (ski->algo);
3008
     * won't work.  The only solution I see is to hardwire it.
3009
     * NOTE: if you change the ivlen above 16, don't forget to
3010
     * enlarge temp.
3011
           * FIXME: For v5 keys we can deduce this info!
3012
           */
3013
201
    ski->ivlen = openpgp_cipher_blocklen (ski->algo);
3014
201
    log_assert (ski->ivlen <= sizeof (temp));
3015
3016
201
    if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003)
3017
0
      ski->ivlen = 0;
3018
201
    else if (ski->s2k.mode == 1002)
3019
0
      ski->ivlen = snlen < 16 ? snlen : 16;
3020
3021
201
    if (pktlen < ski->ivlen)
3022
0
      {
3023
0
              err = gpg_error (GPG_ERR_INV_PACKET);
3024
0
        goto leave;
3025
0
      }
3026
3.12k
    for (i = 0; i < ski->ivlen; i++, pktlen--)
3027
2.92k
      temp[i] = iobuf_get_noeof (inp);
3028
201
    if (list_mode && ski->s2k.mode != 1003)
3029
0
      {
3030
0
        es_fprintf (listfp,
3031
0
                          ski->s2k.mode == 1002 ? "\tserial-number: "
3032
0
                          : "\tprotect IV: ");
3033
0
        for (i = 0; i < ski->ivlen; i++)
3034
0
    es_fprintf (listfp, " %02x", temp[i]);
3035
0
        es_putc ('\n', listfp);
3036
0
      }
3037
201
    memcpy (ski->iv, temp, ski->ivlen);
3038
201
  }
3039
3040
      /* Skip count of secret key material.  */
3041
201
      if (is_v5)
3042
0
        {
3043
0
          if (pktlen < 4)
3044
0
            {
3045
0
              err = gpg_error (GPG_ERR_INV_PACKET);
3046
0
              goto leave;
3047
0
            }
3048
0
          skbytes = read_32 (inp);
3049
0
          pktlen -= 4;
3050
0
          if (list_mode)
3051
0
            es_fprintf (listfp, "\tskbytes: %u\n", skbytes);
3052
0
        }
3053
3054
      /* It does not make sense to read it into secure memory.
3055
       * If the user is so careless, not to protect his secret key,
3056
       * we can assume, that he operates an open system :=(.
3057
       * So we put the key into secure memory when we unprotect it. */
3058
201
      if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002)
3059
0
  {
3060
    /* Better set some dummy stuff here.  */
3061
0
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3062
0
             xstrdup ("dummydata"),
3063
0
             10 * 8);
3064
0
    pktlen = 0;
3065
0
  }
3066
201
      else if (ski->s2k.mode == 1003)
3067
0
  {
3068
0
          void *tmpp;
3069
3070
0
    if (pktlen < 2) /* At least two bytes for parenthesis.  */
3071
0
      {
3072
0
              err = gpg_error (GPG_ERR_INV_PACKET);
3073
0
        goto leave;
3074
0
      }
3075
3076
0
          tmpp = read_rest (inp, pktlen);
3077
0
          if (list_mode)
3078
0
            {
3079
0
              if (mpi_print_mode)
3080
0
                {
3081
0
                  char *tmpsxp = canon_sexp_to_string (tmpp, pktlen);
3082
3083
0
                  es_fprintf (listfp, "\tskey[%d]: %s\n", npkey,
3084
0
                              tmpsxp? trim_trailing_spaces (tmpsxp)
3085
0
                              /*  */: "[invalid S-expression]");
3086
0
                  xfree (tmpsxp);
3087
0
                }
3088
0
              else
3089
0
                es_fprintf (listfp, "\tskey[%d]: [s-expression %lu octets]\n",
3090
0
                            npkey, pktlen);
3091
0
            }
3092
0
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3093
0
             tmpp, tmpp? pktlen * 8 : 0);
3094
0
          pktlen = 0;
3095
0
  }
3096
201
      else if (ski->is_protected)
3097
201
  {
3098
201
          void *tmpp;
3099
3100
201
    if (pktlen < 2) /* At least two bytes for the length.  */
3101
0
      {
3102
0
              err = gpg_error (GPG_ERR_INV_PACKET);
3103
0
        goto leave;
3104
0
      }
3105
3106
    /* Ugly: The length is encrypted too, so we read all stuff
3107
     * up to the end of the packet into the first SKEY
3108
     * element.
3109
           * FIXME: We can do better for v5 keys.  */
3110
3111
201
          tmpp = read_rest (inp, pktlen);
3112
201
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3113
201
             tmpp, tmpp? pktlen * 8 : 0);
3114
          /* Mark that MPI as protected - we need this information for
3115
           * importing a key.  The OPAQUE flag can't be used because
3116
           * we also store public EdDSA values in opaque MPIs.  */
3117
201
          if (pk->pkey[npkey])
3118
201
            gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1);
3119
201
    pktlen = 0;
3120
201
    if (list_mode)
3121
201
            es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey);
3122
201
  }
3123
0
      else
3124
0
  {
3125
          /* Not encrypted.  */
3126
0
    for (i = npkey; i < nskey; i++)
3127
0
      {
3128
3129
0
              if (pktlen < 2) /* At least two bytes for the length.  */
3130
0
                {
3131
0
                  err = gpg_error (GPG_ERR_INV_PACKET);
3132
0
                  goto leave;
3133
0
                }
3134
0
              if (algorithm == PUBKEY_ALGO_KYBER && i == npkey+1)
3135
0
                {
3136
0
                  err = read_octet_string (inp, &pktlen, 4, 0, 1, pk->pkey+i);
3137
0
                  if (err)
3138
0
                    goto leave;
3139
0
                }
3140
0
              else
3141
0
                {
3142
0
                  unsigned int n = pktlen;
3143
3144
0
                  if (algorithm == PUBKEY_ALGO_ECDSA
3145
0
                      || algorithm == PUBKEY_ALGO_EDDSA
3146
0
                      || algorithm == PUBKEY_ALGO_ECDH
3147
0
                      || algorithm == PUBKEY_ALGO_KYBER)
3148
0
                    pk->pkey[i] = sos_read (inp, &n, 0);
3149
0
                  else
3150
0
                    pk->pkey[i] = mpi_read (inp, &n, 0);
3151
0
                  pktlen -= n;
3152
0
                }
3153
3154
0
              if (list_mode)
3155
0
                {
3156
0
                  es_fprintf (listfp, "\tskey[%d]: ", i);
3157
0
                  mpi_print (listfp, pk->pkey[i], mpi_print_mode);
3158
0
                  es_putc ('\n', listfp);
3159
0
                }
3160
3161
0
        if (!pk->pkey[i])
3162
0
    err = gpg_error (GPG_ERR_INV_PACKET);
3163
0
      }
3164
0
    if (err)
3165
0
      goto leave;
3166
3167
0
    if (pktlen < 2)
3168
0
      {
3169
0
              err = gpg_error (GPG_ERR_INV_PACKET);
3170
0
        goto leave;
3171
0
      }
3172
0
    ski->csum = read_16 (inp);
3173
0
    pktlen -= 2;
3174
0
    if (list_mode)
3175
0
            es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum);
3176
0
  }
3177
201
    }
3178
3179
  /* Note that KEYID below has been initialized above in list_mode.  */
3180
442
  if (list_mode)
3181
442
    es_fprintf (listfp, "\tkeyid: %08lX%08lX\n",
3182
0
                (ulong) keyid[0], (ulong) keyid[1]);
3183
3184
443
 leave:
3185
443
  iobuf_skip_rest (inp, pktlen, 0);
3186
443
  return err;
3187
442
}
3188
3189
3190
/* Attribute subpackets have the same format as v4 signature
3191
   subpackets.  This is not part of OpenPGP, but is done in several
3192
   versions of PGP nevertheless.  */
3193
int
3194
parse_attribute_subpkts (PKT_user_id * uid)
3195
0
{
3196
0
  size_t n;
3197
0
  int count = 0;
3198
0
  struct user_attribute *attribs = NULL;
3199
0
  const byte *buffer = uid->attrib_data;
3200
0
  int buflen = uid->attrib_len;
3201
0
  byte type;
3202
3203
0
  xfree (uid->attribs);
3204
3205
0
  while (buflen)
3206
0
    {
3207
0
      n = *buffer++;
3208
0
      buflen--;
3209
0
      if (n == 255)  /* 4 byte length header.  */
3210
0
  {
3211
0
    if (buflen < 4)
3212
0
      goto too_short;
3213
0
    n = buf32_to_size_t (buffer);
3214
0
    buffer += 4;
3215
0
    buflen -= 4;
3216
0
  }
3217
0
      else if (n >= 192)  /* 2 byte special encoded length header.  */
3218
0
  {
3219
0
    if (buflen < 2)
3220
0
      goto too_short;
3221
0
    n = ((n - 192) << 8) + *buffer + 192;
3222
0
    buffer++;
3223
0
    buflen--;
3224
0
  }
3225
0
      if (buflen < n)
3226
0
  goto too_short;
3227
3228
0
      if (!n)
3229
0
        {
3230
          /* Too short to encode the subpacket type.  */
3231
0
          if (opt.verbose)
3232
0
            log_info ("attribute subpacket too short\n");
3233
0
          break;
3234
0
        }
3235
3236
0
      attribs = xrealloc (attribs,
3237
0
                          (count + 1) * sizeof (struct user_attribute));
3238
0
      memset (&attribs[count], 0, sizeof (struct user_attribute));
3239
3240
0
      type = *buffer;
3241
0
      buffer++;
3242
0
      buflen--;
3243
0
      n--;
3244
3245
0
      attribs[count].type = type;
3246
0
      attribs[count].data = buffer;
3247
0
      attribs[count].len = n;
3248
0
      buffer += n;
3249
0
      buflen -= n;
3250
0
      count++;
3251
0
    }
3252
3253
0
  uid->attribs = attribs;
3254
0
  uid->numattribs = count;
3255
0
  return count;
3256
3257
0
 too_short:
3258
0
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
3259
0
    log_info ("buffer shorter than attribute subpacket\n");
3260
0
  uid->attribs = attribs;
3261
0
  uid->numattribs = count;
3262
0
  return count;
3263
0
}
3264
3265
3266
static int
3267
parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3268
6.05k
{
3269
6.05k
  byte *p;
3270
3271
  /* Cap the size of a user ID at 2k: a value absurdly large enough
3272
     that there is no sane user ID string (which is printable text
3273
     as of RFC2440bis) that won't fit in it, but yet small enough to
3274
     avoid allocation problems.  A large pktlen may not be
3275
     allocatable, and a very large pktlen could actually cause our
3276
     allocation to wrap around in xmalloc to a small number. */
3277
3278
6.05k
  if (pktlen > MAX_UID_PACKET_LENGTH)
3279
0
    {
3280
0
      log_error ("packet(%d) too large\n", pkttype);
3281
0
      if (list_mode)
3282
0
        es_fprintf (listfp, ":user ID packet: [too large]\n");
3283
0
      iobuf_skip_rest (inp, pktlen, 0);
3284
0
      return GPG_ERR_INV_PACKET;
3285
0
    }
3286
3287
6.05k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen);
3288
6.05k
  packet->pkt.user_id->len = pktlen;
3289
6.05k
  packet->pkt.user_id->ref = 1;
3290
3291
6.05k
  p = packet->pkt.user_id->name;
3292
29.0k
  for (; pktlen; pktlen--, p++)
3293
23.0k
    *p = iobuf_get_noeof (inp);
3294
6.05k
  *p = 0;
3295
3296
6.05k
  if (list_mode)
3297
0
    {
3298
0
      int n = packet->pkt.user_id->len;
3299
0
      es_fprintf (listfp, ":user ID packet: \"");
3300
      /* fixme: Hey why don't we replace this with es_write_sanitized?? */
3301
0
      for (p = packet->pkt.user_id->name; n; p++, n--)
3302
0
  {
3303
0
    if (*p >= ' ' && *p <= 'z')
3304
0
      es_putc (*p, listfp);
3305
0
    else
3306
0
      es_fprintf (listfp, "\\x%02x", *p);
3307
0
  }
3308
0
      es_fprintf (listfp, "\"\n");
3309
0
    }
3310
6.05k
  return 0;
3311
6.05k
}
3312
3313
3314
void
3315
make_attribute_uidname (PKT_user_id * uid, size_t max_namelen)
3316
0
{
3317
0
  log_assert (max_namelen > 70);
3318
0
  if (uid->numattribs <= 0)
3319
0
    sprintf (uid->name, "[bad attribute packet of size %lu]",
3320
0
       uid->attrib_len);
3321
0
  else if (uid->numattribs > 1)
3322
0
    sprintf (uid->name, "[%d attributes of size %lu]",
3323
0
       uid->numattribs, uid->attrib_len);
3324
0
  else
3325
0
    {
3326
      /* Only one attribute, so list it as the "user id" */
3327
3328
0
      if (uid->attribs->type == ATTRIB_IMAGE)
3329
0
  {
3330
0
    u32 len;
3331
0
    byte type;
3332
3333
0
    if (parse_image_header (uid->attribs, &type, &len))
3334
0
      sprintf (uid->name, "[%.20s image of size %lu]",
3335
0
         image_type_to_string (type, 1), (ulong) len);
3336
0
    else
3337
0
      sprintf (uid->name, "[invalid image]");
3338
0
  }
3339
0
      else
3340
0
  sprintf (uid->name, "[unknown attribute of size %lu]",
3341
0
     (ulong) uid->attribs->len);
3342
0
    }
3343
3344
0
  uid->len = strlen (uid->name);
3345
0
}
3346
3347
3348
static int
3349
parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen,
3350
     PACKET * packet)
3351
0
{
3352
0
  byte *p;
3353
3354
0
  (void) pkttype;
3355
3356
  /* We better cap the size of an attribute packet to make DoS not too
3357
     easy.  16MB should be more then enough for one attribute packet
3358
     (ie. a photo).  */
3359
0
  if (pktlen > MAX_ATTR_PACKET_LENGTH)
3360
0
    {
3361
0
      log_error ("packet(%d) too large\n", pkttype);
3362
0
      if (list_mode)
3363
0
        es_fprintf (listfp, ":attribute packet: [too large]\n");
3364
0
      iobuf_skip_rest (inp, pktlen, 0);
3365
0
      return GPG_ERR_INV_PACKET;
3366
0
    }
3367
3368
0
#define EXTRA_UID_NAME_SPACE 71
3369
0
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id
3370
0
               + EXTRA_UID_NAME_SPACE);
3371
0
  packet->pkt.user_id->ref = 1;
3372
0
  packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1);
3373
0
  packet->pkt.user_id->attrib_len = pktlen;
3374
3375
0
  p = packet->pkt.user_id->attrib_data;
3376
0
  for (; pktlen; pktlen--, p++)
3377
0
    *p = iobuf_get_noeof (inp);
3378
3379
  /* Now parse out the individual attribute subpackets.  This is
3380
     somewhat pointless since there is only one currently defined
3381
     attribute type (jpeg), but it is correct by the spec. */
3382
0
  parse_attribute_subpkts (packet->pkt.user_id);
3383
3384
0
  make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE);
3385
3386
0
  if (list_mode)
3387
0
    {
3388
0
      es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name);
3389
0
    }
3390
0
  return 0;
3391
0
}
3392
3393
3394
static int
3395
parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3396
0
{
3397
0
  byte *p;
3398
3399
  /* Cap comment packet at a reasonable value to avoid an integer
3400
     overflow in the malloc below.  Comment packets are actually not
3401
     anymore define my OpenPGP and we even stopped to use our
3402
     private comment packet.  */
3403
0
  if (pktlen > MAX_COMMENT_PACKET_LENGTH)
3404
0
    {
3405
0
      log_error ("packet(%d) too large\n", pkttype);
3406
0
      if (list_mode)
3407
0
        es_fprintf (listfp, ":%scomment packet: [too large]\n",
3408
0
                    pkttype == PKT_OLD_COMMENT ? "OpenPGP draft " : "");
3409
0
      iobuf_skip_rest (inp, pktlen, 0);
3410
0
      return GPG_ERR_INV_PACKET;
3411
0
    }
3412
0
  packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
3413
0
  packet->pkt.comment->len = pktlen;
3414
0
  p = packet->pkt.comment->data;
3415
0
  for (; pktlen; pktlen--, p++)
3416
0
    *p = iobuf_get_noeof (inp);
3417
3418
0
  if (list_mode)
3419
0
    {
3420
0
      int n = packet->pkt.comment->len;
3421
0
      es_fprintf (listfp, ":%scomment packet: \"", pkttype == PKT_OLD_COMMENT ?
3422
0
                  "OpenPGP draft " : "");
3423
0
      for (p = packet->pkt.comment->data; n; p++, n--)
3424
0
  {
3425
0
    if (*p >= ' ' && *p <= 'z')
3426
0
      es_putc (*p, listfp);
3427
0
    else
3428
0
      es_fprintf (listfp, "\\x%02x", *p);
3429
0
  }
3430
0
      es_fprintf (listfp, "\"\n");
3431
0
    }
3432
0
  return 0;
3433
0
}
3434
3435
3436
/* Parse a ring trust packet RFC4880 (5.10).
3437
 *
3438
 * This parser is special in that the packet is not stored as a packet
3439
 * but its content is merged into the previous packet.  */
3440
static gpg_error_t
3441
parse_ring_trust (parse_packet_ctx_t ctx, unsigned long pktlen)
3442
8
{
3443
8
  gpg_error_t err;
3444
8
  iobuf_t inp = ctx->inp;
3445
8
  PKT_ring_trust rt = {0};
3446
8
  int c;
3447
8
  int not_gpg = 0;
3448
3449
8
  if (!pktlen)
3450
8
    {
3451
8
      if (list_mode)
3452
8
  es_fprintf (listfp, ":trust packet: empty\n");
3453
8
      err = 0;
3454
8
      goto leave;
3455
8
    }
3456
3457
0
  c = iobuf_get_noeof (inp);
3458
0
  pktlen--;
3459
0
  rt.trustval = c;
3460
0
  if (pktlen)
3461
0
    {
3462
0
      if (!c)
3463
0
        {
3464
0
          c = iobuf_get_noeof (inp);
3465
          /* We require that bit 7 of the sigcache is 0 (easier
3466
           * eof handling).  */
3467
0
          if (!(c & 0x80))
3468
0
            rt.sigcache = c;
3469
0
        }
3470
0
      else
3471
0
        iobuf_get_noeof (inp);  /* Dummy read.  */
3472
0
      pktlen--;
3473
0
    }
3474
3475
  /* Next is the optional subtype.  */
3476
0
  if (pktlen > 3)
3477
0
    {
3478
0
      char tmp[4];
3479
0
      tmp[0] = iobuf_get_noeof (inp);
3480
0
      tmp[1] = iobuf_get_noeof (inp);
3481
0
      tmp[2] = iobuf_get_noeof (inp);
3482
0
      tmp[3] = iobuf_get_noeof (inp);
3483
0
      pktlen -= 4;
3484
0
      if (!memcmp (tmp, "gpg", 3))
3485
0
        rt.subtype = tmp[3];
3486
0
      else
3487
0
        not_gpg = 1;
3488
0
    }
3489
  /* If it is a key or uid subtype read the remaining data.  */
3490
0
  if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID)
3491
0
      && pktlen >= 6 )
3492
0
    {
3493
0
      int i;
3494
0
      unsigned int namelen;
3495
3496
0
      rt.keyorg = iobuf_get_noeof (inp);
3497
0
      pktlen--;
3498
0
      rt.keyupdate = read_32 (inp);
3499
0
      pktlen -= 4;
3500
0
      namelen = iobuf_get_noeof (inp);
3501
0
      pktlen--;
3502
0
      if (namelen && pktlen)
3503
0
        {
3504
0
          rt.url = xtrymalloc (namelen + 1);
3505
0
          if (!rt.url)
3506
0
            {
3507
0
              err = gpg_error_from_syserror ();
3508
0
              goto leave;
3509
0
            }
3510
0
          for (i = 0; pktlen && i < namelen; pktlen--, i++)
3511
0
            rt.url[i] = iobuf_get_noeof (inp);
3512
0
          rt.url[i] = 0;
3513
0
        }
3514
0
    }
3515
3516
0
  if (list_mode)
3517
0
    {
3518
0
      if (rt.subtype == RING_TRUST_SIG)
3519
0
        es_fprintf (listfp, ":trust packet: sig flag=%02x sigcache=%02x\n",
3520
0
                    rt.trustval, rt.sigcache);
3521
0
      else if (rt.subtype == RING_TRUST_UID || rt.subtype == RING_TRUST_KEY)
3522
0
        {
3523
0
          unsigned char *p;
3524
3525
0
          es_fprintf (listfp, ":trust packet: %s upd=%lu src=%d%s",
3526
0
                      (rt.subtype == RING_TRUST_UID? "uid" : "key"),
3527
0
                      (unsigned long)rt.keyupdate,
3528
0
                      rt.keyorg,
3529
0
                      (rt.url? " url=":""));
3530
0
          if (rt.url)
3531
0
            {
3532
0
              for (p = rt.url; *p; p++)
3533
0
                {
3534
0
                  if (*p >= ' ' && *p <= 'z')
3535
0
                    es_putc (*p, listfp);
3536
0
                  else
3537
0
                    es_fprintf (listfp, "\\x%02x", *p);
3538
0
                }
3539
0
            }
3540
0
          es_putc ('\n', listfp);
3541
0
        }
3542
0
      else if (not_gpg)
3543
0
        es_fprintf (listfp, ":trust packet: not created by gpg\n");
3544
0
      else
3545
0
        es_fprintf (listfp, ":trust packet: subtype=%02x\n",
3546
0
                    rt.subtype);
3547
0
    }
3548
3549
  /* Now transfer the data to the respective packet.  Do not do this
3550
   * if SKIP_META is set.  */
3551
0
  if (!ctx->last_pkt.pkt.generic || ctx->skip_meta)
3552
0
    ;
3553
0
  else if (rt.subtype == RING_TRUST_SIG
3554
0
           && ctx->last_pkt.pkttype == PKT_SIGNATURE)
3555
0
    {
3556
0
      PKT_signature *sig = ctx->last_pkt.pkt.signature;
3557
3558
0
      if ((rt.sigcache & 1))
3559
0
        {
3560
0
          sig->flags.checked = 1;
3561
0
          sig->flags.valid = !!(rt.sigcache & 2);
3562
0
        }
3563
0
    }
3564
0
  else if (rt.subtype == RING_TRUST_UID
3565
0
           && (ctx->last_pkt.pkttype == PKT_USER_ID
3566
0
               || ctx->last_pkt.pkttype == PKT_ATTRIBUTE))
3567
0
    {
3568
0
      PKT_user_id *uid = ctx->last_pkt.pkt.user_id;
3569
3570
0
      uid->keyorg = rt.keyorg;
3571
0
      uid->keyupdate = rt.keyupdate;
3572
0
      uid->updateurl = rt.url;
3573
0
      rt.url = NULL;
3574
0
    }
3575
0
  else if (rt.subtype == RING_TRUST_KEY
3576
0
           && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY
3577
0
               || ctx->last_pkt.pkttype == PKT_SECRET_KEY))
3578
0
    {
3579
0
      PKT_public_key *pk = ctx->last_pkt.pkt.public_key;
3580
3581
0
      pk->keyorg = rt.keyorg;
3582
0
      pk->keyupdate = rt.keyupdate;
3583
0
      pk->updateurl = rt.url;
3584
0
      rt.url = NULL;
3585
0
    }
3586
3587
0
  err = 0;
3588
3589
8
 leave:
3590
8
  xfree (rt.url);
3591
8
  free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL.  */
3592
8
  iobuf_skip_rest (inp, pktlen, 0);
3593
8
  return err;
3594
0
}
3595
3596
3597
static int
3598
parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen,
3599
     PACKET * pkt, int new_ctb, int partial)
3600
4
{
3601
4
  int rc = 0;
3602
4
  int mode, namelen;
3603
4
  PKT_plaintext *pt;
3604
4
  byte *p;
3605
4
  int c, i;
3606
3607
4
  if (!partial && pktlen < 6)
3608
0
    {
3609
0
      log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen);
3610
0
      if (list_mode)
3611
0
        es_fputs (":literal data packet: [too short]\n", listfp);
3612
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
3613
0
      goto leave;
3614
0
    }
3615
4
  mode = iobuf_get_noeof (inp);
3616
4
  if (pktlen)
3617
2
    pktlen--;
3618
4
  namelen = iobuf_get_noeof (inp);
3619
4
  if (pktlen)
3620
2
    pktlen--;
3621
  /* Note that namelen will never exceed 255 bytes. */
3622
4
  pt = pkt->pkt.plaintext =
3623
4
    xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1);
3624
4
  pt->new_ctb = new_ctb;
3625
4
  pt->mode = mode;
3626
4
  pt->namelen = namelen;
3627
4
  pt->is_partial = partial;
3628
4
  if (pktlen)
3629
2
    {
3630
196
      for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++)
3631
194
  pt->name[i] = iobuf_get_noeof (inp);
3632
2
    }
3633
2
  else
3634
2
    {
3635
406
      for (i = 0; i < namelen; i++)
3636
404
  if ((c = iobuf_get (inp)) == -1)
3637
0
    break;
3638
404
  else
3639
404
    pt->name[i] = c;
3640
2
    }
3641
  /* Fill up NAME so that a check with valgrind won't complain about
3642
   * reading from uninitialized memory.  This case may be triggred by
3643
   * corrupted packets.  */
3644
4
  for (; i < namelen; i++)
3645
0
    pt->name[i] = 0;
3646
3647
4
  pt->timestamp = read_32 (inp);
3648
4
  if (pktlen)
3649
2
    pktlen -= 4;
3650
4
  pt->len = pktlen;
3651
4
  pt->buf = inp;
3652
3653
4
  if (list_mode)
3654
0
    {
3655
0
      es_fprintf (listfp, ":literal data packet:\n"
3656
0
                  "\tmode %c (%X), created %lu, name=\"",
3657
0
                  mode >= ' ' && mode < 'z' ? mode : '?', mode,
3658
0
                  (ulong) pt->timestamp);
3659
0
      for (p = pt->name, i = 0; i < namelen; p++, i++)
3660
0
  {
3661
0
    if (*p >= ' ' && *p <= 'z')
3662
0
      es_putc (*p, listfp);
3663
0
    else
3664
0
      es_fprintf (listfp, "\\x%02x", *p);
3665
0
  }
3666
0
      es_fprintf (listfp, "\",\n\traw data: ");
3667
0
      if (partial)
3668
0
  es_fprintf (listfp, "unknown length\n");
3669
0
      else
3670
0
  es_fprintf (listfp, "%lu bytes\n", (ulong) pt->len);
3671
0
    }
3672
3673
4
 leave:
3674
4
  return rc;
3675
4
}
3676
3677
3678
static int
3679
parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen,
3680
      PACKET * pkt, int new_ctb)
3681
4
{
3682
4
  PKT_compressed *zd;
3683
3684
  /* PKTLEN is here 0, but data follows (this should be the last
3685
     object in a file or the compress algorithm should know the
3686
     length).  */
3687
4
  (void) pkttype;
3688
4
  (void) pktlen;
3689
3690
4
  zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed);
3691
4
  zd->algorithm = iobuf_get_noeof (inp);
3692
4
  zd->len = 0;      /* not used */
3693
4
  zd->new_ctb = new_ctb;
3694
4
  zd->buf = inp;
3695
4
  if (list_mode)
3696
4
    es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
3697
4
  return 0;
3698
4
}
3699
3700
3701
static int
3702
parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen,
3703
     PACKET * pkt, int new_ctb, int partial)
3704
0
{
3705
0
  int rc = 0;
3706
0
  PKT_encrypted *ed;
3707
0
  unsigned long orig_pktlen = pktlen;
3708
3709
0
  ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted);
3710
  /* ed->len is set below.  */
3711
0
  ed->extralen = 0;  /* Unknown here; only used in build_packet.  */
3712
0
  ed->buf = NULL;
3713
0
  ed->new_ctb = new_ctb;
3714
0
  ed->is_partial = partial;
3715
0
  ed->aead_algo = 0;
3716
0
  ed->cipher_algo = 0; /* Only used with AEAD.  */
3717
0
  ed->chunkbyte = 0;   /* Only used with AEAD.  */
3718
0
  if (pkttype == PKT_ENCRYPTED_MDC)
3719
0
    {
3720
      /* Fixme: add some pktlen sanity checks.  */
3721
0
      int version;
3722
3723
0
      version = iobuf_get_noeof (inp);
3724
0
      if (orig_pktlen)
3725
0
  pktlen--;
3726
0
      if (version != 1)
3727
0
  {
3728
0
    log_error ("encrypted_mdc packet with unknown version %d\n",
3729
0
         version);
3730
0
          if (list_mode)
3731
0
            es_fputs (":encrypted data packet: [unknown version]\n", listfp);
3732
    /*skip_rest(inp, pktlen); should we really do this? */
3733
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
3734
0
    goto leave;
3735
0
  }
3736
0
      ed->mdc_method = DIGEST_ALGO_SHA1;
3737
0
    }
3738
0
  else
3739
0
    ed->mdc_method = 0;
3740
3741
  /* A basic sanity check.  We need at least an 8 byte IV plus the 2
3742
     detection bytes.  Note that we don't known the algorithm and thus
3743
     we may only check against the minimum blocksize.  */
3744
0
  if (orig_pktlen && pktlen < 10)
3745
0
    {
3746
      /* Actually this is blocksize+2.  */
3747
0
      log_error ("packet(%d) too short\n", pkttype);
3748
0
      if (list_mode)
3749
0
        es_fputs (":encrypted data packet: [too short]\n", listfp);
3750
0
      rc = GPG_ERR_INV_PACKET;
3751
0
      iobuf_skip_rest (inp, pktlen, partial);
3752
0
      goto leave;
3753
0
    }
3754
3755
  /* Store the remaining length of the encrypted data (i.e. without
3756
     the MDC version number but with the IV etc.).  This value is
3757
     required during decryption.  */
3758
0
  ed->len = pktlen;
3759
3760
0
  if (list_mode)
3761
0
    {
3762
0
      if (orig_pktlen)
3763
0
  es_fprintf (listfp, ":encrypted data packet:\n\tlength: %lu\n",
3764
0
                    orig_pktlen);
3765
0
      else
3766
0
  es_fprintf (listfp, ":encrypted data packet:\n\tlength: unknown\n");
3767
0
      if (ed->mdc_method)
3768
0
  es_fprintf (listfp, "\tmdc_method: %d\n", ed->mdc_method);
3769
0
    }
3770
3771
0
  ed->buf = inp;
3772
3773
0
 leave:
3774
0
  return rc;
3775
0
}
3776
3777
3778
/* Note, that this code is not anymore used in real life because the
3779
   MDC checking is now done right after the decryption in
3780
   decrypt_data.  */
3781
static int
3782
parse_mdc (IOBUF inp, int pkttype, unsigned long pktlen,
3783
     PACKET * pkt, int new_ctb)
3784
0
{
3785
0
  int rc = 0;
3786
0
  PKT_mdc *mdc;
3787
0
  byte *p;
3788
3789
0
  (void) pkttype;
3790
3791
0
  mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc);
3792
0
  if (list_mode)
3793
0
    es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
3794
0
  if (!new_ctb || pktlen != 20)
3795
0
    {
3796
0
      log_error ("mdc_packet with invalid encoding\n");
3797
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
3798
0
      goto leave;
3799
0
    }
3800
0
  p = mdc->hash;
3801
0
  for (; pktlen; pktlen--, p++)
3802
0
    *p = iobuf_get_noeof (inp);
3803
3804
0
 leave:
3805
0
  return rc;
3806
0
}
3807
3808
3809
static gpg_error_t
3810
parse_encrypted_aead (iobuf_t inp, int pkttype, unsigned long pktlen,
3811
                      PACKET *pkt, int partial)
3812
0
{
3813
0
  int rc = 0;
3814
0
  PKT_encrypted *ed;
3815
0
  unsigned long orig_pktlen = pktlen;
3816
0
  int version;
3817
3818
0
  ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted);
3819
0
  if (!ed)
3820
0
    return gpg_error_from_syserror ();
3821
0
  ed->len = 0;
3822
0
  ed->extralen = 0;  /* (only used in build_packet.)  */
3823
0
  ed->buf = NULL;
3824
0
  ed->new_ctb = 1;   /* (packet number requires a new CTB anyway.)  */
3825
0
  ed->is_partial = partial;
3826
0
  ed->mdc_method = 0;
3827
  /* A basic sanity check.  We need one version byte, one algo byte,
3828
   * one aead algo byte, one chunkbyte, at least 15 byte IV.  */
3829
0
  if (orig_pktlen && pktlen < 19)
3830
0
    {
3831
0
      log_error ("packet(%d) too short\n", pkttype);
3832
0
      if (list_mode)
3833
0
        es_fputs (":aead encrypted packet: [too short]\n", listfp);
3834
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
3835
0
      iobuf_skip_rest (inp, pktlen, partial);
3836
0
      goto leave;
3837
0
    }
3838
3839
0
  version = iobuf_get_noeof (inp);
3840
0
  if (orig_pktlen)
3841
0
    pktlen--;
3842
0
  if (version != 1)
3843
0
    {
3844
0
      log_error ("aead encrypted packet with unknown version %d\n",
3845
0
                 version);
3846
0
      if (list_mode)
3847
0
        es_fputs (":aead encrypted packet: [unknown version]\n", listfp);
3848
      /*skip_rest(inp, pktlen); should we really do this? */
3849
0
      rc = gpg_error (GPG_ERR_INV_PACKET);
3850
0
      goto leave;
3851
0
    }
3852
3853
0
  ed->cipher_algo = iobuf_get_noeof (inp);
3854
0
  if (orig_pktlen)
3855
0
    pktlen--;
3856
0
  ed->aead_algo = iobuf_get_noeof (inp);
3857
0
  if (orig_pktlen)
3858
0
    pktlen--;
3859
0
  ed->chunkbyte = iobuf_get_noeof (inp);
3860
0
  if (orig_pktlen)
3861
0
    pktlen--;
3862
3863
  /* Store the remaining length of the encrypted data.  We read the
3864
   * rest during decryption.  */
3865
0
  ed->len = pktlen;
3866
3867
0
  if (list_mode)
3868
0
    {
3869
0
      es_fprintf (listfp, ":aead encrypted packet: cipher=%u aead=%u cb=%u\n",
3870
0
                  ed->cipher_algo, ed->aead_algo, ed->chunkbyte);
3871
0
      if (orig_pktlen)
3872
0
  es_fprintf (listfp, "\tlength: %lu\n", orig_pktlen);
3873
0
      else
3874
0
  es_fprintf (listfp, "\tlength: unknown\n");
3875
0
    }
3876
3877
0
  ed->buf = inp;
3878
3879
0
 leave:
3880
0
  return rc;
3881
0
}
3882
3883
3884
/*
3885
 * This packet is internally generated by us (in armor.c) to transfer
3886
 * some information to the lower layer.  To make sure that this packet
3887
 * is really a GPG faked one and not one coming from outside, we
3888
 * first check that there is a unique tag in it.
3889
 *
3890
 * The format of such a control packet is:
3891
 *   n byte  session marker
3892
 *   1 byte  control type CTRLPKT_xxxxx
3893
 *   m byte  control data
3894
 */
3895
static int
3896
parse_gpg_control (IOBUF inp, int pkttype, unsigned long pktlen,
3897
       PACKET * packet, int partial)
3898
2
{
3899
2
  byte *p;
3900
2
  const byte *sesmark;
3901
2
  size_t sesmarklen;
3902
2
  int i;
3903
3904
2
  (void) pkttype;
3905
3906
2
  if (list_mode)
3907
2
    es_fprintf (listfp, ":packet 63: length %lu ", pktlen);
3908
3909
2
  sesmark = get_session_marker (&sesmarklen);
3910
2
  if (pktlen < sesmarklen + 1)  /* 1 is for the control bytes */
3911
0
    goto skipit;
3912
2
  for (i = 0; i < sesmarklen; i++, pktlen--)
3913
2
    {
3914
2
      if (sesmark[i] != iobuf_get_noeof (inp))
3915
2
  goto skipit;
3916
2
    }
3917
0
  if (pktlen > 4096)
3918
0
    goto skipit;  /* Definitely too large.  We skip it to avoid an
3919
                     overflow in the malloc.  */
3920
0
  if (list_mode)
3921
0
    es_fputs ("- gpg control packet", listfp);
3922
3923
0
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
3924
0
             + pktlen - 1);
3925
0
  packet->pkt.gpg_control->control = iobuf_get_noeof (inp);
3926
0
  pktlen--;
3927
0
  packet->pkt.gpg_control->datalen = pktlen;
3928
0
  p = packet->pkt.gpg_control->data;
3929
0
  for (; pktlen; pktlen--, p++)
3930
0
    *p = iobuf_get_noeof (inp);
3931
3932
0
  return 0;
3933
3934
2
 skipit:
3935
2
  if (list_mode)
3936
0
    {
3937
0
      int c;
3938
3939
0
      i = 0;
3940
0
      es_fprintf (listfp, "- private (rest length %lu)\n", pktlen);
3941
0
      if (partial)
3942
0
  {
3943
0
    while ((c = iobuf_get (inp)) != -1)
3944
0
      dump_hex_line (c, &i);
3945
0
  }
3946
0
      else
3947
0
  {
3948
0
    for (; pktlen; pktlen--)
3949
0
      {
3950
0
        dump_hex_line ((c = iobuf_get (inp)), &i);
3951
0
        if (c == -1)
3952
0
    break;
3953
0
      }
3954
0
  }
3955
0
      es_putc ('\n', listfp);
3956
0
    }
3957
2
  iobuf_skip_rest (inp, pktlen, 0);
3958
2
  return gpg_error (GPG_ERR_INV_PACKET);
3959
0
}
3960
3961
3962
/* Create a GPG control packet to be used internally as a placeholder.  */
3963
PACKET *
3964
create_gpg_control (ctrlpkttype_t type, const byte * data, size_t datalen)
3965
2
{
3966
2
  PACKET *packet;
3967
2
  byte *p;
3968
3969
2
  if (!data)
3970
0
    datalen = 0;
3971
3972
2
  packet = xmalloc (sizeof *packet);
3973
2
  init_packet (packet);
3974
2
  packet->pkttype = PKT_GPG_CONTROL;
3975
2
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen);
3976
2
  packet->pkt.gpg_control->control = type;
3977
2
  packet->pkt.gpg_control->datalen = datalen;
3978
2
  p = packet->pkt.gpg_control->data;
3979
208
  for (; datalen; datalen--, p++)
3980
206
    *p = *data++;
3981
3982
2
  return packet;
3983
2
}