Coverage Report

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