Coverage Report

Created: 2026-06-07 06:25

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