Coverage Report

Created: 2026-03-03 06:42

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
26.1M
{
96
26.1M
  unsigned short a;
97
26.1M
  a = (unsigned short)iobuf_get_noeof (inp) << 8;
98
26.1M
  a |= iobuf_get_noeof (inp);
99
26.1M
  return a;
100
26.1M
}
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.16M
{
107
1.16M
  unsigned long a;
108
1.16M
  a = (unsigned long)iobuf_get_noeof (inp) << 24;
109
1.16M
  a |= iobuf_get_noeof (inp) << 16;
110
1.16M
  a |= iobuf_get_noeof (inp) << 8;
111
1.16M
  a |= iobuf_get_noeof (inp);
112
1.16M
  return a;
113
1.16M
}
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
291k
{
131
291k
  int c, c1, c2, i;
132
291k
  unsigned int nmax = *ret_nread;
133
291k
  unsigned int nbits, nbytes;
134
291k
  size_t nread = 0;
135
291k
  gcry_mpi_t a = NULL;
136
291k
  byte *buf = NULL;
137
291k
  byte *p;
138
139
291k
  if (!nmax)
140
937
    goto overflow;
141
142
290k
  if ((c = c1 = iobuf_get (inp)) == -1)
143
558
    goto leave;
144
289k
  if (++nread == nmax)
145
3.70k
    goto overflow;
146
286k
  nbits = c << 8;
147
286k
  if ((c = c2 = iobuf_get (inp)) == -1)
148
487
    goto leave;
149
285k
  ++nread;
150
285k
  nbits |= c;
151
285k
  if (nbits > MAX_EXTERN_MPI_BITS)
152
3.46k
    {
153
3.46k
      log_error ("mpi too large (%u bits)\n", nbits);
154
3.46k
      goto leave;
155
3.46k
    }
156
157
282k
  nbytes = (nbits + 7) / 8;
158
282k
  buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2);
159
282k
  p = buf;
160
282k
  p[0] = c1;
161
282k
  p[1] = c2;
162
11.9M
  for (i = 0; i < nbytes; i++)
163
11.6M
    {
164
11.6M
      if (nread == nmax)
165
6.57k
  goto overflow;
166
167
11.6M
      c = iobuf_get (inp);
168
11.6M
      if (c == -1)
169
599
  goto leave;
170
171
11.6M
      p[i + 2] = c;
172
11.6M
      nread ++;
173
11.6M
    }
174
175
274k
  if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread))
176
0
    a = NULL;
177
178
274k
  *ret_nread = nread;
179
274k
  gcry_free(buf);
180
274k
  return a;
181
182
11.2k
 overflow:
183
11.2k
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
184
16.3k
 leave:
185
16.3k
  *ret_nread = nread;
186
16.3k
  gcry_free(buf);
187
16.3k
  return a;
188
11.2k
}
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
197
{
208
197
  gpg_error_t err;
209
197
  int c, i;
210
197
  byte *buf = NULL;
211
197
  byte *p;
212
213
197
  *r_data = NULL;
214
215
197
  if ((nbytes && nlength)
216
197
      || (!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
197
  if (nlength)
223
197
    {
224
933
      for (i = 0; i < nlength; i++)
225
753
        {
226
753
          if (!*pktlen)
227
0
            {
228
0
              err = gpg_error (GPG_ERR_INV_PACKET);
229
0
              goto leave;
230
0
            }
231
753
          c = iobuf_readbyte (inp);
232
753
          if (c < 0)
233
17
            {
234
17
              err =  gpg_error (GPG_ERR_INV_PACKET);
235
17
              goto leave;
236
17
            }
237
736
          --*pktlen;
238
736
          nbytes <<= 8;
239
736
          nbytes |= c;
240
736
        }
241
242
180
      if (!nbytes)
243
5
        {
244
5
          err =  gpg_error (GPG_ERR_INV_PACKET);
245
5
          goto leave;
246
5
        }
247
180
    }
248
249
175
  if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS)
250
56
      || (nbytes*8 < nbytes))
251
122
    {
252
122
      log_error ("octet string too large (%u octets)\n", nbytes);
253
122
      err = gpg_error (GPG_ERR_TOO_LARGE);
254
122
      goto leave;
255
122
    }
256
257
53
  if (nbytes > *pktlen)
258
2
    {
259
2
      log_error ("octet string larger than packet (%u octets)\n", nbytes);
260
2
      err = gpg_error (GPG_ERR_INV_PACKET);
261
2
      goto leave;
262
2
    }
263
264
51
  buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes);
265
51
  if (!buf)
266
0
    {
267
0
      err = gpg_error_from_syserror ();
268
0
      goto leave;
269
0
    }
270
51
  p = buf;
271
2.35k
  for (i = 0; i < nbytes; i++)
272
2.34k
    {
273
2.34k
      c = iobuf_get (inp);
274
2.34k
      if (c == -1)
275
33
        {
276
33
          err = gpg_error (GPG_ERR_INV_PACKET);
277
33
          goto leave;
278
33
        }
279
280
2.30k
      p[i] = c;
281
2.30k
      --*pktlen;
282
2.30k
    }
283
284
18
  *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8);
285
18
  gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2);
286
18
  return 0;
287
288
179
 leave:
289
179
  gcry_free (buf);
290
179
  return err;
291
51
}
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
26.0M
{
308
26.0M
  int c, c1, c2, i;
309
26.0M
  unsigned int nmax = *ret_nread;
310
26.0M
  unsigned int nbits, nbytes;
311
26.0M
  size_t nread = 0;
312
26.0M
  gcry_mpi_t a = NULL;
313
26.0M
  byte *buf = NULL;
314
26.0M
  byte *p;
315
316
26.0M
  if (!nmax)
317
208
    goto overflow;
318
319
26.0M
  if ((c = c1 = iobuf_get (inp)) == -1)
320
532
    goto leave;
321
26.0M
  if (++nread == nmax)
322
55
    goto overflow;
323
26.0M
  nbits = c << 8;
324
26.0M
  if ((c = c2 = iobuf_get (inp)) == -1)
325
126
    goto leave;
326
26.0M
  ++nread;
327
26.0M
  nbits |= c;
328
26.0M
  if (nbits > MAX_EXTERN_MPI_BITS)
329
2.45k
    {
330
2.45k
      log_error ("mpi too large (%u bits)\n", nbits);
331
2.45k
      goto leave;
332
2.45k
    }
333
334
26.0M
  nbytes = (nbits + 7) / 8;
335
26.0M
  buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes);
336
26.0M
  p = buf;
337
858M
  for (i = 0; i < nbytes; i++)
338
832M
    {
339
832M
      if (nread == nmax)
340
1.64k
        goto overflow;
341
342
832M
      c = iobuf_get (inp);
343
832M
      if (c == -1)
344
251
        goto leave;
345
346
832M
      p[i] = c;
347
832M
      nread ++;
348
832M
    }
349
350
26.0M
  a = gcry_mpi_set_opaque (NULL, buf, nbits);
351
26.0M
  gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2);
352
26.0M
  *ret_nread = nread;
353
26.0M
  return a;
354
355
1.91k
 overflow:
356
1.91k
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
357
5.27k
 leave:
358
5.27k
  *ret_nread = nread;
359
5.27k
  gcry_free(buf);
360
5.27k
  return a;
361
1.91k
}
362
363
364
/* Register STRING as a known critical notation name.  */
365
void
366
register_known_notation (const char *string)
367
1.05k
{
368
1.05k
  strlist_t sl;
369
370
1.05k
  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.05k
  if (!string)
377
1.05k
    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
21.4k
{
395
21.4k
  int old = list_mode;
396
21.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
21.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
21.4k
  return old;
430
21.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
310k
{
438
310k
  static byte unknown_pubkey_algos[256];
439
440
  /* First check whether the algorithm is usable but not suitable for
441
     encryption/signing.  */
442
310k
  if (pubkey_get_npkey (algo))
443
188k
    {
444
188k
      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
188k
    }
454
121k
  else
455
121k
    {
456
121k
      algo &= 0xff;
457
121k
      if (!unknown_pubkey_algos[algo])
458
503
        {
459
503
          if (opt.verbose && !glo_ctrl.silence_parse_warnings)
460
503
            log_info (_("can't handle public key algorithm %d\n"), algo);
461
503
          unknown_pubkey_algos[algo] = 1;
462
503
        }
463
121k
    }
464
310k
}
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
15.3M
{
472
15.3M
  int skip, rc;
473
474
15.3M
  do
475
15.3M
    {
476
15.3M
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l);
477
15.3M
    }
478
15.3M
  while (skip && ! rc);
479
15.3M
  return rc;
480
15.3M
}
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
15.3M
{
735
15.3M
  int rc = 0;
736
15.3M
  iobuf_t inp;
737
15.3M
  int c, ctb, pkttype, lenbytes;
738
15.3M
  unsigned long pktlen;
739
15.3M
  byte hdr[8];
740
15.3M
  int hdrlen;
741
15.3M
  int new_ctb = 0, partial = 0;
742
15.3M
  int with_uid = (onlykeypkts == 2);
743
15.3M
  off_t pos;
744
745
15.3M
  *skip = 0;
746
15.3M
  inp = ctx->inp;
747
748
28.5M
 again:
749
28.5M
  log_assert (!pkt->pkt.generic);
750
28.5M
  if (retpos || list_mode)
751
0
    {
752
0
      pos = iobuf_tell (inp);
753
0
      if (retpos)
754
0
        *retpos = pos;
755
0
    }
756
28.5M
  else
757
28.5M
    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
28.5M
  if ((ctb = iobuf_get (inp)) == -1)
762
41.1k
    {
763
41.1k
      rc = -1;
764
41.1k
      goto leave;
765
41.1k
    }
766
28.4M
  ctx->last_ctb = ctb;
767
28.4M
  hdrlen = 0;
768
28.4M
  hdr[hdrlen++] = ctb;
769
770
28.4M
  if (!(ctb & 0x80))
771
204k
    {
772
204k
      log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb);
773
204k
      rc = gpg_error (GPG_ERR_INV_PACKET);
774
204k
      goto leave;
775
204k
    }
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
28.2M
  pktlen = 0;
785
28.2M
  new_ctb = !!(ctb & 0x40);
786
28.2M
  if (new_ctb)
787
707k
    pkttype = ctb & 0x3f;
788
27.5M
  else
789
27.5M
    pkttype = (ctb >> 2) & 0xf;
790
791
28.2M
  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
28.2M
  if (new_ctb)
799
707k
    {
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
707k
      if ((c = iobuf_get (inp)) == -1)
805
731
  {
806
731
    log_error ("%s: 1st length byte missing\n", iobuf_where (inp));
807
731
    rc = gpg_error (GPG_ERR_INV_PACKET);
808
731
    goto leave;
809
731
  }
810
811
812
706k
      hdr[hdrlen++] = c;
813
706k
      if (c < 192)
814
585k
        pktlen = c;
815
121k
      else if (c < 224)
816
4.25k
        {
817
4.25k
          pktlen = (c - 192) * 256;
818
4.25k
          if ((c = iobuf_get (inp)) == -1)
819
810
            {
820
810
              log_error ("%s: 2nd length byte missing\n",
821
810
                         iobuf_where (inp));
822
810
              rc = gpg_error (GPG_ERR_INV_PACKET);
823
810
              goto leave;
824
810
            }
825
3.44k
          hdr[hdrlen++] = c;
826
3.44k
          pktlen += c + 192;
827
3.44k
        }
828
116k
      else if (c == 255)
829
2.87k
        {
830
2.87k
    int i;
831
2.87k
    char value[4];
832
833
14.0k
    for (i = 0; i < 4; i ++)
834
11.3k
            {
835
11.3k
              if ((c = iobuf_get (inp)) == -1)
836
202
                {
837
202
                  log_error ("%s: 4 byte length invalid\n", iobuf_where (inp));
838
202
                  rc = gpg_error (GPG_ERR_INV_PACKET);
839
202
                  goto leave;
840
202
                }
841
11.1k
              value[i] = hdr[hdrlen++] = c;
842
11.1k
            }
843
844
2.67k
    pktlen = buf32_to_ulong (value);
845
2.67k
        }
846
114k
      else /* Partial body length.  */
847
114k
        {
848
114k
          switch (pkttype)
849
114k
            {
850
8.45k
            case PKT_PLAINTEXT:
851
10.3k
            case PKT_ENCRYPTED:
852
19.3k
            case PKT_ENCRYPTED_MDC:
853
20.0k
            case PKT_ENCRYPTED_AEAD:
854
111k
            case PKT_COMPRESSED:
855
111k
              iobuf_set_partial_body_length_mode (inp, c & 0xff);
856
111k
              pktlen = 0; /* To indicate partial length.  */
857
111k
              partial = 1;
858
111k
              break;
859
860
2.55k
            default:
861
2.55k
              log_error ("%s: partial length invalid for"
862
2.55k
                         " packet type %d\n", iobuf_where (inp), pkttype);
863
2.55k
              rc = gpg_error (GPG_ERR_INV_PACKET);
864
2.55k
              goto leave;
865
114k
            }
866
114k
        }
867
868
706k
    }
869
27.5M
  else /* This is an old format packet.  */
870
27.5M
    {
871
      /* The type of length encoding is encoded in bits 0-1 of the
872
   tag.  */
873
27.5M
      lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3));
874
27.5M
      if (!lenbytes)
875
279k
  {
876
279k
    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
279k
    partial = 1;
880
279k
    if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT
881
278k
        && pkttype != PKT_COMPRESSED)
882
3.52k
      {
883
3.52k
        log_error ("%s: indeterminate length for invalid"
884
3.52k
       " packet type %d\n", iobuf_where (inp), pkttype);
885
3.52k
        rc = gpg_error (GPG_ERR_INV_PACKET);
886
3.52k
        goto leave;
887
3.52k
      }
888
279k
  }
889
27.3M
      else
890
27.3M
  {
891
54.6M
    for (; lenbytes; lenbytes--)
892
27.3M
      {
893
27.3M
        pktlen <<= 8;
894
27.3M
        c = iobuf_get (inp);
895
27.3M
        if (c == -1)
896
1.75k
    {
897
1.75k
      log_error ("%s: length invalid\n", iobuf_where (inp));
898
1.75k
      rc = gpg_error (GPG_ERR_INV_PACKET);
899
1.75k
      goto leave;
900
1.75k
    }
901
27.3M
        pktlen |= hdr[hdrlen++] = c;
902
27.3M
      }
903
27.3M
  }
904
27.5M
    }
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
28.2M
  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
28.2M
  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
28.2M
  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
28.2M
  else if (do_skip
946
     /* type==0 is not allowed.  This is an invalid packet.  */
947
28.2M
     || !pkttype
948
     /* When ONLYKEYPKTS is set, we don't skip keys.  */
949
28.2M
     || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
950
0
         && pkttype != PKT_PUBLIC_KEY
951
0
         && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY))
952
18.9k
    {
953
18.9k
      iobuf_skip_rest (inp, pktlen, partial);
954
18.9k
      *skip = 1;
955
18.9k
      rc = 0;
956
18.9k
      goto leave;
957
18.9k
    }
958
959
28.2M
  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
28.2M
  if (list_mode)
973
28.2M
    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
28.2M
  ctx->n_parsed_packets++;
980
981
28.2M
  pkt->pkttype = pkttype;
982
28.2M
  rc = GPG_ERR_UNKNOWN_PACKET;  /* default error */
983
28.2M
  switch (pkttype)
984
28.2M
    {
985
52.1k
    case PKT_PUBLIC_KEY:
986
659k
    case PKT_PUBLIC_SUBKEY:
987
681k
    case PKT_SECRET_KEY:
988
737k
    case PKT_SECRET_SUBKEY:
989
737k
      pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key);
990
737k
      rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt);
991
737k
      break;
992
49.2k
    case PKT_SYMKEY_ENC:
993
49.2k
      rc = parse_symkeyenc (inp, pkttype, pktlen, pkt);
994
49.2k
      break;
995
26.9k
    case PKT_PUBKEY_ENC:
996
26.9k
      rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt);
997
26.9k
      break;
998
13.5M
    case PKT_SIGNATURE:
999
13.5M
      pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature);
1000
13.5M
      rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature);
1001
13.5M
      break;
1002
42.0k
    case PKT_ONEPASS_SIG:
1003
42.0k
      pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig);
1004
42.0k
      rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig);
1005
42.0k
      break;
1006
52.5k
    case PKT_USER_ID:
1007
52.5k
      rc = parse_user_id (inp, pkttype, pktlen, pkt);
1008
52.5k
      break;
1009
16.0k
    case PKT_ATTRIBUTE:
1010
16.0k
      pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
1011
16.0k
      rc = parse_attribute (inp, pkttype, pktlen, pkt);
1012
16.0k
      break;
1013
7.55k
    case PKT_OLD_COMMENT:
1014
8.92k
    case PKT_COMMENT:
1015
8.92k
      rc = parse_comment (inp, pkttype, pktlen, pkt);
1016
8.92k
      break;
1017
13.1M
    case PKT_RING_TRUST:
1018
13.1M
      {
1019
13.1M
        rc = parse_ring_trust (ctx, pktlen);
1020
13.1M
        if (!rc)
1021
13.1M
          goto again; /* Directly read the next packet.  */
1022
13.1M
      }
1023
0
      break;
1024
100k
    case PKT_PLAINTEXT:
1025
100k
      rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial);
1026
100k
      break;
1027
384k
    case PKT_COMPRESSED:
1028
384k
      rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb);
1029
384k
      break;
1030
18.1k
    case PKT_ENCRYPTED:
1031
27.9k
    case PKT_ENCRYPTED_MDC:
1032
27.9k
      rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial);
1033
27.9k
      break;
1034
9.20k
    case PKT_MDC:
1035
9.20k
      rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb);
1036
9.20k
      break;
1037
15.6k
    case PKT_ENCRYPTED_AEAD:
1038
15.6k
      rc = parse_encrypted_aead (inp, pkttype, pktlen, pkt, partial);
1039
15.6k
      break;
1040
15.4k
    case PKT_GPG_CONTROL:
1041
15.4k
      rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial);
1042
15.4k
      break;
1043
9.63k
    case PKT_MARKER:
1044
9.63k
      rc = parse_marker (inp, pkttype, pktlen);
1045
9.63k
      break;
1046
37.3k
    default:
1047
      /* Unknown packet.  Skip it.  */
1048
37.3k
      skip_packet (inp, pkttype, pktlen, partial);
1049
37.3k
      break;
1050
28.2M
    }
1051
1052
  /* Store a shallow copy of certain packets in the context.  */
1053
15.0M
  free_packet (NULL, ctx);
1054
15.0M
  if (!rc && (pkttype == PKT_PUBLIC_KEY
1055
14.8M
              || pkttype == PKT_SECRET_KEY
1056
14.8M
              || pkttype == PKT_USER_ID
1057
14.7M
              || pkttype == PKT_ATTRIBUTE
1058
14.7M
              || pkttype == PKT_SIGNATURE))
1059
13.6M
    {
1060
13.6M
      ctx->last_pkt = *pkt;
1061
13.6M
    }
1062
1063
15.3M
 leave:
1064
  /* FIXME: We leak in case of an error (see the xmalloc's above).  */
1065
15.3M
  if (!rc && iobuf_error (inp))
1066
6.95k
    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
15.3M
  return rc == -1? -1 : gpg_err_code (rc);
1072
15.0M
}
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
37.3k
{
1151
37.3k
  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
37.3k
  iobuf_skip_rest (inp, pktlen, partial);
1178
37.3k
}
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
307k
{
1188
307k
  int c;
1189
307k
  byte *buf, *p;
1190
1191
307k
  buf = xtrymalloc (pktlen);
1192
307k
  if (!buf)
1193
221k
    {
1194
221k
      gpg_error_t err = gpg_error_from_syserror ();
1195
221k
      log_error ("error reading rest of packet: %s\n", gpg_strerror (err));
1196
221k
      return NULL;
1197
221k
    }
1198
1.94M
  for (p = buf; pktlen; pktlen--)
1199
1.85M
    {
1200
1.85M
      c = iobuf_get (inp);
1201
1.85M
      if (c == -1)
1202
2.86k
        {
1203
2.86k
          log_error ("premature eof while reading rest of packet\n");
1204
2.86k
          xfree (buf);
1205
2.86k
          return NULL;
1206
2.86k
        }
1207
1.85M
      *p++ = c;
1208
1.85M
    }
1209
1210
83.1k
  return buf;
1211
85.9k
}
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.24M
{
1222
1.24M
  char buffer[256];
1223
1.24M
  char *tmpbuf;
1224
1.24M
  int i, c, nbytes;
1225
1226
1.24M
  *r_data = NULL;
1227
1228
1.24M
  if (!*pktlen)
1229
221
    return gpg_error (GPG_ERR_INV_PACKET);
1230
1.24M
  c = iobuf_readbyte (inp);
1231
1.24M
  if (c < 0)
1232
136
    return gpg_error (GPG_ERR_INV_PACKET);
1233
1.24M
  --*pktlen;
1234
1.24M
  nbytes = c;
1235
1.24M
  if (nbytes < 2 || nbytes > 254)
1236
1.14k
    return gpg_error (GPG_ERR_INV_PACKET);
1237
1.24M
  if (nbytes > *pktlen)
1238
523
    return gpg_error (GPG_ERR_INV_PACKET);
1239
1240
1.24M
  buffer[0] = nbytes;
1241
1242
9.21M
  for (i = 0; i < nbytes; i++)
1243
7.97M
    {
1244
7.97M
      c = iobuf_get (inp);
1245
7.97M
      if (c < 0)
1246
213
        return gpg_error (GPG_ERR_INV_PACKET);
1247
7.97M
      --*pktlen;
1248
7.97M
      buffer[1+i] = c;
1249
7.97M
    }
1250
1251
1.24M
  tmpbuf = xtrymalloc (1 + nbytes);
1252
1.24M
  if (!tmpbuf)
1253
0
    return gpg_error_from_syserror ();
1254
1.24M
  memcpy (tmpbuf, buffer, 1 + nbytes);
1255
1.24M
  *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes));
1256
1.24M
  if (!*r_data)
1257
0
    {
1258
0
      xfree (tmpbuf);
1259
0
      return gpg_error_from_syserror ();
1260
0
    }
1261
1.24M
  return 0;
1262
1.24M
}
1263
1264
1265
/* Parse a marker packet.  */
1266
static int
1267
parse_marker (IOBUF inp, int pkttype, unsigned long pktlen)
1268
9.63k
{
1269
9.63k
  (void) pkttype;
1270
1271
9.63k
  if (pktlen != 3)
1272
7.33k
    goto fail;
1273
1274
2.29k
  if (iobuf_get (inp) != 'P')
1275
700
    {
1276
700
      pktlen--;
1277
700
      goto fail;
1278
700
    }
1279
1280
1.59k
  if (iobuf_get (inp) != 'G')
1281
284
    {
1282
284
      pktlen--;
1283
284
      goto fail;
1284
284
    }
1285
1286
1.31k
  if (iobuf_get (inp) != 'P')
1287
86
    {
1288
86
      pktlen--;
1289
86
      goto fail;
1290
86
    }
1291
1292
1.22k
  if (list_mode)
1293
1.22k
    es_fputs (":marker packet: PGP\n", listfp);
1294
1295
1.22k
  return 0;
1296
1297
8.40k
 fail:
1298
8.40k
  log_error ("invalid marker packet\n");
1299
8.40k
  if (list_mode)
1300
8.40k
    es_fputs (":marker packet: [invalid]\n", listfp);
1301
8.40k
  iobuf_skip_rest (inp, pktlen, 0);
1302
8.40k
  return GPG_ERR_INV_PACKET;
1303
1.31k
}
1304
1305
1306
static int
1307
parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1308
     PACKET * packet)
1309
49.2k
{
1310
49.2k
  PKT_symkey_enc *k;
1311
49.2k
  int rc = 0;
1312
49.2k
  int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen;
1313
1314
49.2k
  if (pktlen < 4)
1315
14.9k
    goto too_short;
1316
34.2k
  version = iobuf_get_noeof (inp);
1317
34.2k
  pktlen--;
1318
34.2k
  if (version == 4)
1319
19.9k
    ;
1320
14.3k
  else if (version == 5)
1321
11.7k
    ;
1322
2.60k
  else
1323
2.60k
    {
1324
2.60k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
1325
2.60k
      if (list_mode)
1326
2.60k
        es_fprintf (listfp, ":symkey enc packet: [unknown version]\n");
1327
2.60k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1328
2.60k
      goto leave;
1329
2.60k
    }
1330
31.6k
  if (pktlen > 200)
1331
377
    {       /* (we encode the seskeylen in a byte) */
1332
377
      log_error ("packet(%d) too large\n", pkttype);
1333
377
      if (list_mode)
1334
377
        es_fprintf (listfp, ":symkey enc packet: [too large]\n");
1335
377
      rc = gpg_error (GPG_ERR_INV_PACKET);
1336
377
      goto leave;
1337
377
    }
1338
31.2k
  cipher_algo = iobuf_get_noeof (inp);
1339
31.2k
  pktlen--;
1340
31.2k
  if (version == 5)
1341
11.6k
    {
1342
11.6k
      aead_algo = iobuf_get_noeof (inp);
1343
11.6k
      pktlen--;
1344
11.6k
    }
1345
19.5k
  else
1346
19.5k
    aead_algo = 0;
1347
31.2k
  if (pktlen < 2)
1348
6.58k
    goto too_short;
1349
24.6k
  s2kmode = iobuf_get_noeof (inp);
1350
24.6k
  pktlen--;
1351
24.6k
  hash_algo = iobuf_get_noeof (inp);
1352
24.6k
  pktlen--;
1353
24.6k
  switch (s2kmode)
1354
24.6k
    {
1355
13.0k
    case 0: /* Simple S2K.  */
1356
13.0k
      minlen = 0;
1357
13.0k
      break;
1358
2.07k
    case 1: /* Salted S2K.  */
1359
2.07k
      minlen = 8;
1360
2.07k
      break;
1361
4.70k
    case 3: /* Iterated+salted S2K.  */
1362
4.70k
      minlen = 9;
1363
4.70k
      break;
1364
4.88k
    default:
1365
4.88k
      log_error ("unknown S2K mode %d\n", s2kmode);
1366
4.88k
      if (list_mode)
1367
4.88k
        es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n");
1368
4.88k
      goto leave;
1369
24.6k
    }
1370
19.7k
  if (minlen > pktlen)
1371
804
    {
1372
804
      log_error ("packet with S2K %d too short\n", s2kmode);
1373
804
      if (list_mode)
1374
804
        es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1375
804
      rc = gpg_error (GPG_ERR_INV_PACKET);
1376
804
      goto leave;
1377
804
    }
1378
18.9k
  seskeylen = pktlen - minlen;
1379
18.9k
  k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc);
1380
18.9k
  k->version = version;
1381
18.9k
  k->cipher_algo = cipher_algo;
1382
18.9k
  k->aead_algo = aead_algo;
1383
18.9k
  k->s2k.mode = s2kmode;
1384
18.9k
  k->s2k.hash_algo = hash_algo;
1385
18.9k
  if (s2kmode == 1 || s2kmode == 3)
1386
5.96k
    {
1387
53.7k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
1388
47.7k
  k->s2k.salt[i] = iobuf_get_noeof (inp);
1389
5.96k
    }
1390
18.9k
  if (s2kmode == 3)
1391
4.15k
    {
1392
4.15k
      k->s2k.count = iobuf_get_noeof (inp);
1393
4.15k
      pktlen--;
1394
4.15k
    }
1395
18.9k
  k->seskeylen = seskeylen;
1396
18.9k
  if (k->seskeylen)
1397
4.60k
    {
1398
4.60k
      k->seskey = xcalloc (1, seskeylen);
1399
63.2k
      for (i = 0; i < seskeylen && pktlen; i++, pktlen--)
1400
58.6k
  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.60k
      if (s2kmode != 1 && s2kmode != 3)
1406
4.60k
  log_info (_("WARNING: potentially insecure symmetrically"
1407
2.15k
        " encrypted session key\n"));
1408
4.60k
    }
1409
18.9k
  log_assert (!pktlen);
1410
1411
18.9k
  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
49.2k
 leave:
1442
49.2k
  iobuf_skip_rest (inp, pktlen, 0);
1443
49.2k
  return rc;
1444
1445
21.5k
 too_short:
1446
21.5k
  log_error ("packet(%d) too short\n", pkttype);
1447
21.5k
  if (list_mode)
1448
21.5k
    es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1449
21.5k
  rc = gpg_error (GPG_ERR_INV_PACKET);
1450
21.5k
  goto leave;
1451
18.9k
}
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
26.9k
{
1459
26.9k
  int rc = 0;
1460
26.9k
  int i, ndata;
1461
26.9k
  unsigned int n;
1462
26.9k
  PKT_pubkey_enc *k;
1463
1464
26.9k
  k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc);
1465
26.9k
  if (pktlen < 12)
1466
6.34k
    {
1467
6.34k
      log_error ("packet(%d) too short\n", pkttype);
1468
6.34k
      if (list_mode)
1469
6.34k
        es_fputs (":pubkey enc packet: [too short]\n", listfp);
1470
6.34k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1471
6.34k
      goto leave;
1472
6.34k
    }
1473
20.6k
  k->version = iobuf_get_noeof (inp);
1474
20.6k
  pktlen--;
1475
20.6k
  if (k->version != 2 && k->version != 3)
1476
857
    {
1477
857
      log_error ("packet(%d) with unknown version %d\n", pkttype, k->version);
1478
857
      if (list_mode)
1479
857
        es_fputs (":pubkey enc packet: [unknown version]\n", listfp);
1480
857
      rc = gpg_error (GPG_ERR_INV_PACKET);
1481
857
      goto leave;
1482
857
    }
1483
19.7k
  k->keyid[0] = read_32 (inp);
1484
19.7k
  pktlen -= 4;
1485
19.7k
  k->keyid[1] = read_32 (inp);
1486
19.7k
  pktlen -= 4;
1487
19.7k
  k->pubkey_algo = iobuf_get_noeof (inp);
1488
19.7k
  pktlen--;
1489
19.7k
  k->throw_keyid = 0;  /* Only used as flag for build_packet.  */
1490
19.7k
  if (list_mode)
1491
19.7k
    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
19.7k
  ndata = pubkey_get_nenc (k->pubkey_algo);
1497
19.7k
  if (!ndata)
1498
10.7k
    {
1499
10.7k
      if (list_mode)
1500
10.7k
  es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo);
1501
10.7k
      unknown_pubkey_warning (k->pubkey_algo);
1502
10.7k
      k->data[0] = NULL; /* No need to store the encrypted data.  */
1503
10.7k
    }
1504
9.02k
  else if (k->pubkey_algo == PUBKEY_ALGO_ECDH)
1505
3.36k
    {
1506
3.36k
      log_assert (ndata == 2);
1507
      /* Get the ephemeral public key.  */
1508
3.36k
      n = pktlen;
1509
3.36k
      k->data[0] = sos_read (inp, &n, 0);
1510
3.36k
      pktlen -= n;
1511
3.36k
      if (!k->data[0])
1512
1.50k
        {
1513
1.50k
          rc = gpg_error (GPG_ERR_INV_PACKET);
1514
1.50k
          goto leave;
1515
1.50k
        }
1516
      /* Get the wrapped symmetric key.  */
1517
1.86k
      rc = read_sized_octet_string (inp, &pktlen, k->data + 1);
1518
1.86k
      if (rc)
1519
1.68k
        goto leave;
1520
1.86k
    }
1521
5.66k
  else if (k->pubkey_algo == PUBKEY_ALGO_KYBER)
1522
9
    {
1523
9
      log_assert (ndata == 3);
1524
      /* Get the ephemeral public key.  */
1525
9
      n = pktlen;
1526
9
      k->data[0] = sos_read (inp, &n, 0);
1527
9
      pktlen -= n;
1528
9
      if (!k->data[0])
1529
4
        {
1530
4
          rc = gpg_error (GPG_ERR_INV_PACKET);
1531
4
          goto leave;
1532
4
        }
1533
      /* Get the Kyber ciphertext.  */
1534
5
      rc = read_octet_string (inp, &pktlen, 4, 0, 0, k->data + 1);
1535
5
      if (rc)
1536
5
        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
5.65k
  else
1551
5.65k
    {
1552
16.3k
      for (i = 0; i < ndata; i++)
1553
10.6k
        {
1554
10.6k
          n = pktlen;
1555
10.6k
          k->data[i] = mpi_read (inp, &n, 0);
1556
10.6k
          pktlen -= n;
1557
10.6k
          if (!k->data[i])
1558
4.89k
            rc = gpg_error (GPG_ERR_INV_PACKET);
1559
10.6k
        }
1560
5.65k
      if (rc)
1561
4.34k
        goto leave;
1562
5.65k
    }
1563
12.2k
  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
26.9k
 leave:
1577
26.9k
  iobuf_skip_rest (inp, pktlen, 0);
1578
26.9k
  return rc;
1579
12.2k
}
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
21.6M
{
1876
21.6M
  switch (type)
1877
21.6M
    {
1878
7.80k
    case SIGSUBPKT_REV_KEY:
1879
7.80k
      if (n < 22)
1880
3.13k
  break;
1881
4.67k
      return 0;
1882
8.39M
    case SIGSUBPKT_SIG_CREATED:
1883
8.41M
    case SIGSUBPKT_SIG_EXPIRE:
1884
8.41M
    case SIGSUBPKT_KEY_EXPIRE:
1885
8.41M
      if (n < 4)
1886
5.64k
  break;
1887
8.41M
      return 0;
1888
183k
    case SIGSUBPKT_KEY_FLAGS:
1889
186k
    case SIGSUBPKT_KS_FLAGS:
1890
189k
    case SIGSUBPKT_PREF_SYM:
1891
189k
    case SIGSUBPKT_PREF_AEAD:
1892
192k
    case SIGSUBPKT_PREF_HASH:
1893
196k
    case SIGSUBPKT_PREF_COMPR:
1894
196k
    case SIGSUBPKT_POLICY:
1895
203k
    case SIGSUBPKT_PREF_KS:
1896
207k
    case SIGSUBPKT_FEATURES:
1897
207k
    case SIGSUBPKT_REGEXP:
1898
207k
    case SIGSUBPKT_ATTST_SIGS:
1899
207k
      return 0;
1900
8.40k
    case SIGSUBPKT_SIGNATURE:
1901
51.5k
    case SIGSUBPKT_EXPORTABLE:
1902
53.2k
    case SIGSUBPKT_REVOCABLE:
1903
53.4k
    case SIGSUBPKT_REVOC_REASON:
1904
53.4k
      if (!n)
1905
2.60k
  break;
1906
50.8k
      return 0;
1907
3.52M
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1908
3.52M
      if (n < 8)
1909
5.91k
  break;
1910
3.52M
      return 0;
1911
9.38M
    case SIGSUBPKT_ISSUER_FPR:  /* issuer key fingerprint */
1912
9.38M
      if (n < 21)
1913
820
  break;
1914
9.38M
      return 0;
1915
2.91k
    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
2.91k
      if (n < 8
1920
1.19k
    || 8 + ((buffer[4] << 8) | buffer[5]) +
1921
1.19k
    ((buffer[6] << 8) | buffer[7]) != n)
1922
2.03k
  break;
1923
884
      return 0;
1924
278
    case SIGSUBPKT_PRIMARY_UID:
1925
278
      if (n != 1)
1926
0
  break;
1927
278
      return 0;
1928
12.1k
    case SIGSUBPKT_TRUST:
1929
12.1k
      if (n != 2)
1930
11.4k
  break;
1931
660
      return 0;
1932
1.07k
    case SIGSUBPKT_KEY_BLOCK:
1933
1.07k
      if (n && buffer[0])
1934
210
        return -1; /* Unknown version - ignore.  */
1935
863
      if (n < 50)
1936
863
  break;  /* Definitely too short to carry a key block.  */
1937
0
      return 0;
1938
18.1k
    default:
1939
18.1k
      return 0;
1940
21.6M
    }
1941
32.4k
  return -2;
1942
21.6M
}
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.05k
{
1949
1.05k
  strlist_t sl;
1950
1951
1.05k
  register_known_notation (NULL); /* Make sure it is initialized.  */
1952
1953
2.08k
  for (sl = known_notations_list; sl; sl = sl->next)
1954
1.05k
    if (sl->flags == len && !memcmp (sl->d, name, len))
1955
32
      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.05k
}
1966
1967
1968
static int
1969
can_handle_critical (const byte * buffer, size_t n, int type)
1970
3.56M
{
1971
3.56M
  switch (type)
1972
3.56M
    {
1973
2.95k
    case SIGSUBPKT_NOTATION:
1974
2.95k
      if (n >= 8)
1975
1.23k
  {
1976
1.23k
    size_t notation_len = ((buffer[4] << 8) | buffer[5]);
1977
1.23k
    if (n - 8 >= notation_len)
1978
1.05k
      return can_handle_critical_notation (buffer + 8, notation_len);
1979
1.23k
  }
1980
1.89k
      return 0;
1981
2.12k
    case SIGSUBPKT_SIGNATURE:
1982
5.05k
    case SIGSUBPKT_SIG_CREATED:
1983
6.36k
    case SIGSUBPKT_SIG_EXPIRE:
1984
102k
    case SIGSUBPKT_KEY_EXPIRE:
1985
105k
    case SIGSUBPKT_EXPORTABLE:
1986
111k
    case SIGSUBPKT_REVOCABLE:
1987
117k
    case SIGSUBPKT_REV_KEY:
1988
126k
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1989
129k
    case SIGSUBPKT_ISSUER_FPR:  /* issuer fingerprint */
1990
131k
    case SIGSUBPKT_PREF_SYM:
1991
131k
    case SIGSUBPKT_PREF_AEAD:
1992
133k
    case SIGSUBPKT_PREF_HASH:
1993
135k
    case SIGSUBPKT_PREF_COMPR:
1994
145k
    case SIGSUBPKT_KEY_FLAGS:
1995
146k
    case SIGSUBPKT_PRIMARY_UID:
1996
149k
    case SIGSUBPKT_FEATURES:
1997
150k
    case SIGSUBPKT_TRUST:
1998
150k
    case SIGSUBPKT_REGEXP:
1999
151k
    case SIGSUBPKT_ATTST_SIGS:
2000
      /* Is it enough to show the policy or keyserver? */
2001
151k
    case SIGSUBPKT_POLICY:
2002
155k
    case SIGSUBPKT_PREF_KS:
2003
155k
    case SIGSUBPKT_REVOC_REASON: /* At least we know about it.  */
2004
155k
      return 1;
2005
2006
1.62k
    case SIGSUBPKT_KEY_BLOCK:
2007
1.62k
      if (n && !buffer[0])
2008
122
        return 1;
2009
1.49k
      else
2010
1.49k
        return 0;
2011
2012
3.40M
    default:
2013
3.40M
      return 0;
2014
3.56M
    }
2015
3.56M
}
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
190M
{
2022
190M
  const byte *buffer;
2023
190M
  int buflen;
2024
190M
  int type;
2025
190M
  int critical_dummy;
2026
190M
  int offset;
2027
190M
  size_t n;
2028
190M
  const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed;
2029
190M
  int seq = 0;
2030
190M
  int reqseq = start ? *start : 0;
2031
2032
190M
  if (!critical)
2033
190M
    critical = &critical_dummy;
2034
2035
190M
  if (!pktbuf || reqseq == -1)
2036
278k
    {
2037
278k
      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
278k
      return reqtype ==  SIGSUBPKT_TEST_CRITICAL ? dummy : NULL;
2041
278k
    }
2042
190M
  buffer = pktbuf->data;
2043
190M
  buflen = pktbuf->len;
2044
597M
  while (buflen)
2045
492M
    {
2046
492M
      n = *buffer++;
2047
492M
      buflen--;
2048
492M
      if (n == 255) /* 4 byte length header.  */
2049
312k
  {
2050
312k
    if (buflen < 4)
2051
91.0k
      goto too_short;
2052
221k
    n = buf32_to_size_t (buffer);
2053
221k
    buffer += 4;
2054
221k
    buflen -= 4;
2055
221k
  }
2056
492M
      else if (n >= 192) /* 4 byte special encoded length header.  */
2057
461k
  {
2058
461k
    if (buflen < 2)
2059
135k
      goto too_short;
2060
326k
    n = ((n - 192) << 8) + *buffer + 192;
2061
326k
    buffer++;
2062
326k
    buflen--;
2063
326k
  }
2064
492M
      if (buflen < n)
2065
58.4M
  goto too_short;
2066
433M
      if (!buflen)
2067
1.18M
        goto no_type_byte;
2068
432M
      type = *buffer;
2069
432M
      if (type & 0x80)
2070
48.7M
  {
2071
48.7M
    type &= 0x7f;
2072
48.7M
    *critical = 1;
2073
48.7M
  }
2074
383M
      else
2075
383M
  *critical = 0;
2076
432M
      if (!(++seq > reqseq))
2077
59.5k
  ;
2078
432M
      else if (reqtype == SIGSUBPKT_TEST_CRITICAL)
2079
30.9M
  {
2080
30.9M
    if (*critical)
2081
3.57M
      {
2082
3.57M
        if (n - 1 > buflen + 1)
2083
8.59k
    goto too_short;
2084
3.56M
        if (!can_handle_critical (buffer + 1, n - 1, type))
2085
3.41M
    {
2086
3.41M
      if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2087
3.41M
        log_info (_("subpacket of type %d has "
2088
0
        "critical bit set\n"), type);
2089
3.41M
      if (start)
2090
0
        *start = seq;
2091
3.41M
      return NULL; /* This is an error.  */
2092
3.41M
    }
2093
3.56M
      }
2094
30.9M
  }
2095
401M
      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
401M
      else if (type == reqtype) /* Found.  */
2099
21.6M
  {
2100
21.6M
    buffer++;
2101
21.6M
    n--;
2102
21.6M
    if (n > buflen)
2103
7.24k
      goto too_short;
2104
21.6M
    if (ret_n)
2105
9.63M
      *ret_n = n;
2106
21.6M
    offset = parse_one_sig_subpkt (buffer, n, type);
2107
21.6M
    switch (offset)
2108
21.6M
      {
2109
32.4k
      case -2:
2110
32.4k
        log_error ("subpacket of type %d too short\n", type);
2111
32.4k
        return NULL;
2112
210
      case -1:
2113
210
        return NULL;
2114
21.5M
      default:
2115
21.5M
        break;
2116
21.6M
      }
2117
21.5M
    if (start)
2118
16.2k
      *start = seq;
2119
21.5M
    return buffer + offset;
2120
21.6M
  }
2121
407M
      buffer += n;
2122
407M
      buflen -= n;
2123
407M
    }
2124
105M
  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
16.6M
    return buffer;
2130
2131
  /* Critical bit we don't understand. */
2132
88.7M
  if (start)
2133
367k
    *start = -1;
2134
88.7M
  return NULL;  /* End of packets; not found.  */
2135
2136
58.7M
 too_short:
2137
58.7M
  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
58.7M
  if (start)
2146
3.57M
    *start = -1;
2147
58.7M
  return NULL;
2148
2149
1.18M
 no_type_byte:
2150
1.18M
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2151
1.18M
    log_info ("type octet missing in subpacket\n");
2152
1.18M
  if (start)
2153
1.13k
    *start = -1;
2154
1.18M
  return NULL;
2155
105M
}
2156
2157
2158
const byte *
2159
parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype,
2160
      size_t *ret_n)
2161
186M
{
2162
186M
  return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL);
2163
186M
}
2164
2165
2166
const byte *
2167
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype)
2168
16.9M
{
2169
16.9M
  const byte *p;
2170
2171
16.9M
  p = parse_sig_subpkt (sig, 1, reqtype, NULL);
2172
16.9M
  if (!p)
2173
16.9M
    p = parse_sig_subpkt (sig, 0, reqtype, NULL);
2174
16.9M
  return p;
2175
16.9M
}
2176
2177
2178
/* Find all revocation keys.  Look in hashed area only.  */
2179
void
2180
parse_revkeys (PKT_signature * sig)
2181
3.57M
{
2182
3.57M
  const byte *revkey;
2183
3.57M
  int seq = 0;
2184
3.57M
  size_t len;
2185
2186
3.57M
  if (sig->sig_class != 0x1F)
2187
0
    return;
2188
2189
3.58M
  while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY,
2190
3.58M
                                    &len, &seq, NULL)))
2191
4.67k
    {
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.67k
      if ((len == 22 || len == 34)
2196
4.14k
          && (revkey[0] & 0x80))
2197
1.80k
  {
2198
1.80k
    sig->revkey = xrealloc (sig->revkey,
2199
1.80k
          sizeof (struct revocation_key) *
2200
1.80k
          (sig->numrevkeys + 1));
2201
2202
1.80k
    sig->revkey[sig->numrevkeys].class = revkey[0];
2203
1.80k
    sig->revkey[sig->numrevkeys].algid = revkey[1];
2204
1.80k
          len -= 2;
2205
1.80k
    sig->revkey[sig->numrevkeys].fprlen = len;
2206
1.80k
    memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len);
2207
1.80k
    memset (sig->revkey[sig->numrevkeys].fpr+len, 0,
2208
1.80k
                  sizeof (sig->revkey[sig->numrevkeys].fpr) - len);
2209
1.80k
    sig->numrevkeys++;
2210
1.80k
  }
2211
4.67k
    }
2212
3.57M
}
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
13.5M
{
2223
13.5M
  int md5_len = 0;
2224
13.5M
  unsigned n;
2225
13.5M
  int is_v4or5 = 0;
2226
13.5M
  int rc = 0;
2227
13.5M
  int i, ndata;
2228
2229
13.5M
  if (pktlen < 16)
2230
406k
    {
2231
406k
      log_error ("packet(%d) too short\n", pkttype);
2232
406k
      if (list_mode)
2233
406k
        es_fputs (":signature packet: [too short]\n", listfp);
2234
406k
      goto leave;
2235
406k
    }
2236
13.1M
  sig->version = iobuf_get_noeof (inp);
2237
13.1M
  pktlen--;
2238
13.1M
  if (sig->version == 4 || sig->version == 5)
2239
13.0M
    is_v4or5 = 1;
2240
62.5k
  else if (sig->version != 2 && sig->version != 3)
2241
2.05k
    {
2242
2.05k
      log_error ("packet(%d) with unknown version %d\n",
2243
2.05k
     pkttype, sig->version);
2244
2.05k
      if (list_mode)
2245
2.05k
        es_fputs (":signature packet: [unknown version]\n", listfp);
2246
2.05k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2247
2.05k
      goto leave;
2248
2.05k
    }
2249
2250
13.1M
  if (!is_v4or5)
2251
60.4k
    {
2252
60.4k
      if (pktlen == 0)
2253
0
  goto underflow;
2254
60.4k
      md5_len = iobuf_get_noeof (inp);
2255
60.4k
      pktlen--;
2256
60.4k
    }
2257
13.1M
  if (pktlen == 0)
2258
0
    goto underflow;
2259
13.1M
  sig->sig_class = iobuf_get_noeof (inp);
2260
13.1M
  pktlen--;
2261
13.1M
  if (!is_v4or5)
2262
60.4k
    {
2263
60.4k
      if (pktlen < 12)
2264
0
  goto underflow;
2265
60.4k
      sig->timestamp = read_32 (inp);
2266
60.4k
      pktlen -= 4;
2267
60.4k
      sig->keyid[0] = read_32 (inp);
2268
60.4k
      pktlen -= 4;
2269
60.4k
      sig->keyid[1] = read_32 (inp);
2270
60.4k
      pktlen -= 4;
2271
60.4k
    }
2272
13.1M
  if (pktlen < 2)
2273
53
    goto underflow;
2274
13.1M
  sig->pubkey_algo = iobuf_get_noeof (inp);
2275
13.1M
  pktlen--;
2276
13.1M
  sig->digest_algo = iobuf_get_noeof (inp);
2277
13.1M
  pktlen--;
2278
13.1M
  sig->flags.exportable = 1;
2279
13.1M
  sig->flags.revocable = 1;
2280
13.1M
  if (is_v4or5) /* Read subpackets.  */
2281
13.0M
    {
2282
13.0M
      if (pktlen < 2)
2283
0
  goto underflow;
2284
13.0M
      n = read_16 (inp);
2285
13.0M
      pktlen -= 2;  /* Length of hashed data. */
2286
13.0M
      if (pktlen < n)
2287
3.21k
  goto underflow;
2288
13.0M
      if (n > 30000)
2289
129
  {
2290
129
    log_error ("signature packet: hashed data too long (%u)\n", n);
2291
129
          if (list_mode)
2292
129
            es_fprintf (listfp,
2293
0
                        ":signature packet: [hashed data too long (%u)]\n", n);
2294
129
          rc = GPG_ERR_INV_PACKET;
2295
129
    goto leave;
2296
129
  }
2297
13.0M
      if (n)
2298
13.0M
  {
2299
13.0M
    sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1);
2300
13.0M
    sig->hashed->size = n;
2301
13.0M
    sig->hashed->len = n;
2302
13.0M
    if (iobuf_read (inp, sig->hashed->data, n) != n)
2303
1.12k
      {
2304
1.12k
        log_error ("premature eof while reading "
2305
1.12k
       "hashed signature data\n");
2306
1.12k
              if (list_mode)
2307
1.12k
                es_fputs (":signature packet: [premature eof]\n", listfp);
2308
1.12k
        rc = -1;
2309
1.12k
        goto leave;
2310
1.12k
      }
2311
13.0M
    pktlen -= n;
2312
13.0M
  }
2313
13.0M
      if (pktlen < 2)
2314
540
  goto underflow;
2315
13.0M
      n = read_16 (inp);
2316
13.0M
      pktlen -= 2;  /* Length of unhashed data.  */
2317
13.0M
      if (pktlen < n)
2318
8.09k
  goto underflow;
2319
13.0M
      if (n > 10000)
2320
178
  {
2321
178
    log_error ("signature packet: unhashed data too long (%u)\n", n);
2322
178
          if (list_mode)
2323
178
            es_fprintf (listfp,
2324
0
                        ":signature packet: [unhashed data too long (%u)]\n",
2325
0
                        n);
2326
178
          rc = GPG_ERR_INV_PACKET;
2327
178
    goto leave;
2328
178
  }
2329
13.0M
      if (n)
2330
12.9M
  {
2331
12.9M
    sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1);
2332
12.9M
    sig->unhashed->size = n;
2333
12.9M
    sig->unhashed->len = n;
2334
12.9M
    if (iobuf_read (inp, sig->unhashed->data, n) != n)
2335
170
      {
2336
170
        log_error ("premature eof while reading "
2337
170
       "unhashed signature data\n");
2338
170
              if (list_mode)
2339
170
                es_fputs (":signature packet: [premature eof]\n", listfp);
2340
170
        rc = -1;
2341
170
        goto leave;
2342
170
      }
2343
12.9M
    pktlen -= n;
2344
12.9M
  }
2345
13.0M
    }
2346
2347
13.1M
  if (pktlen < 2)
2348
144
    goto underflow;
2349
13.1M
  sig->digest_start[0] = iobuf_get_noeof (inp);
2350
13.1M
  pktlen--;
2351
13.1M
  sig->digest_start[1] = iobuf_get_noeof (inp);
2352
13.1M
  pktlen--;
2353
2354
13.1M
  if (is_v4or5 && sig->pubkey_algo)  /* Extract required information.  */
2355
13.0M
    {
2356
13.0M
      const byte *p;
2357
13.0M
      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
13.0M
      if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL)
2362
8.42M
    || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL))
2363
4.76M
  sig->flags.unknown_critical = 1;
2364
2365
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL);
2366
13.0M
      if (p)
2367
8.39M
  sig->timestamp = buf32_to_u32 (p);
2368
4.68M
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2369
4.68M
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2370
4.68M
        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
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len);
2376
13.0M
      if (p && len == 21 && p[0] == 4)
2377
9.23M
        {
2378
9.23M
          sig->keyid[0] = buf32_to_u32 (p + 1 + 12);
2379
9.23M
    sig->keyid[1] = buf32_to_u32 (p + 1 + 16);
2380
9.23M
  }
2381
3.84M
      else if (p && len == 33 && p[0] == 5)
2382
85
        {
2383
85
          sig->keyid[0] = buf32_to_u32 (p + 1 );
2384
85
    sig->keyid[1] = buf32_to_u32 (p + 1 + 4);
2385
85
  }
2386
3.84M
      else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER)))
2387
3.52M
        {
2388
3.52M
          sig->keyid[0] = buf32_to_u32 (p);
2389
3.52M
    sig->keyid[1] = buf32_to_u32 (p + 4);
2390
3.52M
  }
2391
322k
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2392
319k
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2393
322k
  log_info ("signature packet without keyid\n");
2394
2395
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL);
2396
13.0M
      if (p && buf32_to_u32 (p))
2397
17.4k
  sig->expiredate = sig->timestamp + buf32_to_u32 (p);
2398
13.0M
      if (sig->expiredate && sig->expiredate <= make_timestamp ())
2399
17.1k
  sig->flags.expired = 1;
2400
2401
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL);
2402
13.0M
      if (p)
2403
542
  sig->flags.policy_url = 1;
2404
2405
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL);
2406
13.0M
      if (p)
2407
3.67k
  sig->flags.pref_ks = 1;
2408
2409
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len);
2410
13.0M
      if (p && len)
2411
17.1k
        {
2412
17.1k
          char *mbox;
2413
2414
17.1k
          sig->signers_uid = try_make_printable_string (p, len, 0);
2415
17.1k
          if (!sig->signers_uid)
2416
0
            {
2417
0
              rc = gpg_error_from_syserror ();
2418
0
              goto leave;
2419
0
            }
2420
17.1k
          mbox = mailbox_from_userid (sig->signers_uid, 0);
2421
17.1k
          if (mbox)
2422
1.69k
            {
2423
1.69k
              xfree (sig->signers_uid);
2424
1.69k
              sig->signers_uid = mbox;
2425
1.69k
            }
2426
17.1k
        }
2427
2428
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL);
2429
13.0M
      if (p)
2430
0
        sig->flags.key_block = 1;
2431
2432
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL);
2433
13.0M
      if (p)
2434
884
  sig->flags.notation = 1;
2435
2436
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL);
2437
13.0M
      if (p && *p == 0)
2438
735
  sig->flags.revocable = 0;
2439
2440
13.0M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len);
2441
13.0M
      if (p && len == 2)
2442
660
  {
2443
660
    sig->trust_depth = p[0];
2444
660
    sig->trust_value = p[1];
2445
2446
    /* Only look for a regexp if there is also a trust
2447
       subpacket. */
2448
660
    sig->trust_regexp =
2449
660
      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
660
    if (len == 0)
2454
43
      sig->trust_regexp = NULL;
2455
660
  }
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
13.0M
      p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE);
2463
13.0M
      if (p && *p == 0)
2464
973
  sig->flags.exportable = 0;
2465
2466
      /* Find all revocation keys.  */
2467
13.0M
      if (sig->sig_class == 0x1F)
2468
3.57M
  parse_revkeys (sig);
2469
13.0M
    }
2470
2471
13.1M
  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
13.1M
  ndata = pubkey_get_nsig (sig->pubkey_algo);
2488
13.1M
  if (!ndata)
2489
273k
    {
2490
273k
      if (list_mode)
2491
273k
  es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo);
2492
273k
      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
273k
      if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8))
2497
134
  {
2498
    /* We include a limit to avoid too trivial DoS attacks by
2499
       having gpg allocate too much memory.  */
2500
134
    log_error ("signature packet: too much data\n");
2501
134
    rc = GPG_ERR_INV_PACKET;
2502
134
  }
2503
273k
      else
2504
273k
  {
2505
273k
          void *tmpp;
2506
2507
273k
          tmpp = read_rest (inp, pktlen);
2508
273k
    sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2509
273k
    pktlen = 0;
2510
273k
  }
2511
273k
    }
2512
12.8M
  else
2513
12.8M
    {
2514
38.4M
      for (i = 0; i < ndata; i++)
2515
25.5M
  {
2516
25.5M
    n = pktlen;
2517
25.5M
          if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA
2518
25.3M
              || sig->pubkey_algo == PUBKEY_ALGO_EDDSA)
2519
25.4M
            sig->data[i] = sos_read (inp, &n, 0);
2520
165k
          else
2521
165k
            sig->data[i] = mpi_read (inp, &n, 0);
2522
25.5M
    pktlen -= n;
2523
25.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
25.5M
    if (!sig->data[i])
2530
2.25k
      rc = GPG_ERR_INV_PACKET;
2531
25.5M
  }
2532
12.8M
    }
2533
2534
13.5M
 leave:
2535
13.5M
  iobuf_skip_rest (inp, pktlen, 0);
2536
13.5M
  return rc;
2537
2538
12.0k
 underflow:
2539
12.0k
  log_error ("packet(%d) too short\n", pkttype);
2540
12.0k
  if (list_mode)
2541
12.0k
    es_fputs (":signature packet: [too short]\n", listfp);
2542
2543
12.0k
  iobuf_skip_rest (inp, pktlen, 0);
2544
2545
12.0k
  return GPG_ERR_INV_PACKET;
2546
13.1M
}
2547
2548
2549
static int
2550
parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen,
2551
       PKT_onepass_sig * ops)
2552
42.0k
{
2553
42.0k
  int version;
2554
42.0k
  int rc = 0;
2555
2556
42.0k
  if (pktlen < 13)
2557
13.9k
    {
2558
13.9k
      log_error ("packet(%d) too short\n", pkttype);
2559
13.9k
      if (list_mode)
2560
13.9k
        es_fputs (":onepass_sig packet: [too short]\n", listfp);
2561
13.9k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2562
13.9k
      goto leave;
2563
13.9k
    }
2564
28.0k
  version = iobuf_get_noeof (inp);
2565
28.0k
  pktlen--;
2566
28.0k
  if (version != 3)
2567
1.32k
    {
2568
1.32k
      log_error ("onepass_sig with unknown version %d\n", version);
2569
1.32k
      if (list_mode)
2570
1.32k
        es_fputs (":onepass_sig packet: [unknown version]\n", listfp);
2571
1.32k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2572
1.32k
      goto leave;
2573
1.32k
    }
2574
26.7k
  ops->sig_class = iobuf_get_noeof (inp);
2575
26.7k
  pktlen--;
2576
26.7k
  ops->digest_algo = iobuf_get_noeof (inp);
2577
26.7k
  pktlen--;
2578
26.7k
  ops->pubkey_algo = iobuf_get_noeof (inp);
2579
26.7k
  pktlen--;
2580
26.7k
  ops->keyid[0] = read_32 (inp);
2581
26.7k
  pktlen -= 4;
2582
26.7k
  ops->keyid[1] = read_32 (inp);
2583
26.7k
  pktlen -= 4;
2584
26.7k
  ops->last = iobuf_get_noeof (inp);
2585
26.7k
  pktlen--;
2586
26.7k
  if (list_mode)
2587
26.7k
    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
42.0k
 leave:
2597
42.0k
  iobuf_skip_rest (inp, pktlen, 0);
2598
42.0k
  return rc;
2599
26.7k
}
2600
2601
2602
static int
2603
parse_key (IOBUF inp, int pkttype, unsigned long pktlen,
2604
     byte * hdr, int hdrlen, PACKET * pkt)
2605
737k
{
2606
737k
  gpg_error_t err = 0;
2607
737k
  int i, version, algorithm;
2608
737k
  unsigned long timestamp, expiredate, max_expiredate;
2609
737k
  int npkey, nskey;
2610
737k
  u32 keyid[2];
2611
737k
  PKT_public_key *pk;
2612
737k
  int is_v5;
2613
737k
  unsigned int pkbytes; /* For v5 keys: Number of bytes in the public
2614
                         * key material.  For v4 keys: 0.  */
2615
2616
737k
  (void) hdr;
2617
2618
737k
  pk = pkt->pkt.public_key; /* PK has been cleared. */
2619
2620
737k
  version = iobuf_get_noeof (inp);
2621
737k
  pktlen--;
2622
737k
  if (pkttype == PKT_PUBLIC_SUBKEY && version == '#')
2623
5.14k
    {
2624
      /* Early versions of G10 used the old PGP comments packets;
2625
       * luckily all those comments are started by a hash.  */
2626
5.14k
      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
5.14k
      iobuf_skip_rest (inp, pktlen, 0);
2643
5.14k
      return 0;
2644
5.14k
    }
2645
732k
  else if (version == 4)
2646
719k
    is_v5 = 0;
2647
13.1k
  else if (version == 5)
2648
4.55k
    is_v5 = 1;
2649
8.56k
  else if (version == 2 || version == 3)
2650
4.71k
    {
2651
      /* Not anymore supported since 2.1.  Use an older gpg version
2652
       * (i.e. gpg 1.4) to parse v3 packets.  */
2653
4.71k
      if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2654
4.71k
        log_info ("packet(%d) with obsolete version %d\n", pkttype, version);
2655
4.71k
      if (list_mode)
2656
4.71k
        es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version);
2657
4.71k
      pk->version = version;
2658
4.71k
      err = gpg_error (GPG_ERR_LEGACY_KEY);
2659
4.71k
      goto leave;
2660
4.71k
    }
2661
3.84k
  else
2662
3.84k
    {
2663
3.84k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
2664
3.84k
      if (list_mode)
2665
3.84k
        es_fputs (":key packet: [unknown version]\n", listfp);
2666
3.84k
      err = gpg_error (GPG_ERR_INV_PACKET);
2667
3.84k
      goto leave;
2668
3.84k
    }
2669
2670
723k
  if (pktlen < (is_v5? 15:11))
2671
541
    {
2672
541
      log_error ("packet(%d) too short\n", pkttype);
2673
541
      if (list_mode)
2674
541
        es_fputs (":key packet: [too short]\n", listfp);
2675
541
      err = gpg_error (GPG_ERR_INV_PACKET);
2676
541
      goto leave;
2677
541
    }
2678
723k
  else if (pktlen > MAX_KEY_PACKET_LENGTH)
2679
47
    {
2680
47
      log_error ("packet(%d) too large\n", pkttype);
2681
47
      if (list_mode)
2682
47
        es_fputs (":key packet: [too large]\n", listfp);
2683
47
      err = gpg_error (GPG_ERR_INV_PACKET);
2684
47
      goto leave;
2685
47
    }
2686
2687
723k
  timestamp = read_32 (inp);
2688
723k
  pktlen -= 4;
2689
723k
  expiredate = 0;   /* have to get it from the selfsignature */
2690
723k
  max_expiredate = 0;
2691
723k
  algorithm = iobuf_get_noeof (inp);
2692
723k
  pktlen--;
2693
723k
  if (is_v5)
2694
4.07k
    {
2695
4.07k
      pkbytes = read_32 (inp);
2696
4.07k
      pktlen -= 4;
2697
4.07k
    }
2698
719k
  else
2699
719k
    pkbytes = 0;
2700
2701
723k
  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
723k
  pk->timestamp = timestamp;
2717
723k
  pk->expiredate = expiredate;
2718
723k
  pk->max_expiredate = max_expiredate;
2719
723k
  pk->hdrbytes = hdrlen;
2720
723k
  pk->version = version;
2721
723k
  pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY);
2722
723k
  pk->pubkey_algo = algorithm;
2723
2724
723k
  nskey = pubkey_get_nskey (algorithm);
2725
723k
  npkey = pubkey_get_npkey (algorithm);
2726
723k
  if (!npkey)
2727
25.6k
    {
2728
25.6k
      if (list_mode)
2729
25.6k
  es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm);
2730
25.6k
      unknown_pubkey_warning (algorithm);
2731
25.6k
    }
2732
2733
723k
  if (!npkey)
2734
25.6k
    {
2735
      /* Unknown algorithm - put data into an opaque MPI.  */
2736
25.6k
      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
25.6k
      pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2740
25.6k
      pktlen = 0;
2741
25.6k
      goto leave;
2742
25.6k
    }
2743
697k
  else
2744
697k
    {
2745
2.68M
      for (i = 0; i < npkey; i++)
2746
1.99M
        {
2747
1.99M
          if (    (algorithm == PUBKEY_ALGO_ECDSA && (i == 0))
2748
1.98M
               || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0))
2749
1.94M
               || (algorithm == PUBKEY_ALGO_ECDH  && (i == 0 || i == 2))
2750
751k
               || (algorithm == PUBKEY_ALGO_KYBER && (i == 0)))
2751
1.24M
            {
2752
              /* Read the OID (i==0) or the KDF params (i==2).  */
2753
1.24M
        err = read_sized_octet_string (inp, &pktlen, pk->pkey+i);
2754
1.24M
            }
2755
751k
          else if (algorithm == PUBKEY_ALGO_KYBER && i == 2)
2756
186
            {
2757
              /* Read the four-octet count prefixed Kyber public key.  */
2758
186
        err = read_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i);
2759
186
            }
2760
751k
          else
2761
751k
            {
2762
              /* Read MPI or SOS.  */
2763
751k
              unsigned int n = pktlen;
2764
751k
              if (algorithm == PUBKEY_ALGO_ECDSA
2765
747k
                  || algorithm == PUBKEY_ALGO_EDDSA
2766
708k
                  || algorithm == PUBKEY_ALGO_ECDH
2767
109k
                  || algorithm == PUBKEY_ALGO_KYBER)
2768
642k
                pk->pkey[i] = sos_read (inp, &n, 0);
2769
108k
              else
2770
108k
                pk->pkey[i] = mpi_read (inp, &n, 0);
2771
751k
              pktlen -= n;
2772
751k
              if (!pk->pkey[i])
2773
9.00k
                err = gpg_error (GPG_ERR_INV_PACKET);
2774
751k
            }
2775
1.99M
          if (err)
2776
9.72k
            goto leave;
2777
1.99M
        }
2778
687k
      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
687k
    }
2807
687k
  if (list_mode)
2808
0
    keyid_from_pk (pk, keyid);
2809
2810
687k
  if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY)
2811
46.8k
    {
2812
46.8k
      struct seckey_info *ski;
2813
46.8k
      byte temp[16];
2814
46.8k
      size_t snlen = 0;
2815
46.8k
      unsigned int skbytes;
2816
2817
46.8k
      if (pktlen < 1)
2818
626
        {
2819
626
          err = gpg_error (GPG_ERR_INV_PACKET);
2820
626
          goto leave;
2821
626
        }
2822
2823
46.2k
      pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
2824
46.2k
      if (!pk->seckey_info)
2825
0
        {
2826
0
          err = gpg_error_from_syserror ();
2827
0
          goto leave;
2828
0
        }
2829
2830
46.2k
      ski->algo = iobuf_get_noeof (inp);
2831
46.2k
      pktlen--;
2832
2833
46.2k
      if (is_v5)
2834
2.30k
        {
2835
2.30k
          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.30k
          if (!pktlen)
2840
470
            {
2841
470
              err = gpg_error (GPG_ERR_INV_PACKET);
2842
470
              goto leave;
2843
470
            }
2844
1.83k
          protcount = iobuf_get_noeof (inp);
2845
1.83k
          pktlen--;
2846
1.83k
          if (list_mode)
2847
1.83k
            es_fprintf (listfp, "\tprotbytes: %u\n", protcount);
2848
1.83k
        }
2849
2850
45.7k
      if (ski->algo)
2851
39.1k
  {
2852
39.1k
    ski->is_protected = 1;
2853
39.1k
    ski->s2k.count = 0;
2854
39.1k
    if (ski->algo == 253)
2855
108
      {
2856
108
              if (list_mode)
2857
108
                es_fprintf (listfp,
2858
0
                            "\tS2K pseudo algo %d is not yet supported\n",
2859
0
                            ski->algo);
2860
108
              err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2861
108
              goto leave;
2862
108
            }
2863
39.0k
          else if (ski->algo == 254 || ski->algo == 255)
2864
32.9k
      {
2865
32.9k
              if (pktlen < 3)
2866
2.38k
    {
2867
2.38k
      err = gpg_error (GPG_ERR_INV_PACKET);
2868
2.38k
      goto leave;
2869
2.38k
    }
2870
2871
30.5k
              ski->sha1chk = (ski->algo == 254);
2872
30.5k
        ski->algo = iobuf_get_noeof (inp);
2873
30.5k
        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
30.5k
        ski->s2k.mode = iobuf_get_noeof (inp);
2878
30.5k
        pktlen--;
2879
30.5k
        ski->s2k.hash_algo = iobuf_get_noeof (inp);
2880
30.5k
        pktlen--;
2881
        /* Check for the special GNU extension.  */
2882
30.5k
        if (ski->s2k.mode == 101)
2883
20.6k
    {
2884
102k
      for (i = 0; i < 4 && pktlen; i++, pktlen--)
2885
82.2k
        temp[i] = iobuf_get_noeof (inp);
2886
20.6k
      if (i < 4 || memcmp (temp, "GNU", 3))
2887
2.97k
        {
2888
2.97k
          if (list_mode)
2889
2.97k
      es_fprintf (listfp, "\tunknown S2K %d\n",
2890
0
                                    ski->s2k.mode);
2891
2.97k
          err = gpg_error (GPG_ERR_INV_PACKET);
2892
2.97k
          goto leave;
2893
2.97k
        }
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
17.7k
      ski->s2k.mode = 1000 + temp[3];
2899
17.7k
    }
2900
2901
              /* Read the salt.  */
2902
27.5k
        if (ski->s2k.mode == 3 || ski->s2k.mode == 1)
2903
4.84k
    {
2904
41.6k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
2905
36.8k
        temp[i] = iobuf_get_noeof (inp);
2906
4.84k
                  if (i < 8)
2907
245
                    {
2908
245
          err = gpg_error (GPG_ERR_INV_PACKET);
2909
245
          goto leave;
2910
245
                    }
2911
4.59k
      memcpy (ski->s2k.salt, temp, 8);
2912
4.59k
    }
2913
2914
              /* Check the mode.  */
2915
27.3k
        switch (ski->s2k.mode)
2916
27.3k
    {
2917
1.09k
    case 0:
2918
1.09k
      if (list_mode)
2919
1.09k
        es_fprintf (listfp, "\tsimple S2K");
2920
1.09k
      break;
2921
1.16k
    case 1:
2922
1.16k
      if (list_mode)
2923
1.16k
        es_fprintf (listfp, "\tsalted S2K");
2924
1.16k
      break;
2925
3.43k
    case 3:
2926
3.43k
      if (list_mode)
2927
3.43k
        es_fprintf (listfp, "\titer+salt S2K");
2928
3.43k
      break;
2929
805
    case 1001:
2930
805
      if (list_mode)
2931
805
        es_fprintf (listfp, "\tgnu-dummy");
2932
805
      break;
2933
11.0k
    case 1002:
2934
11.0k
      if (list_mode)
2935
11.0k
        es_fprintf (listfp, "\tgnu-divert-to-card");
2936
11.0k
      break;
2937
1.09k
    case 1003:
2938
1.09k
      if (list_mode)
2939
1.09k
        es_fprintf (listfp, "\tgnu-mode1003");
2940
1.09k
      break;
2941
8.69k
    default:
2942
8.69k
      if (list_mode)
2943
8.69k
        es_fprintf (listfp, "\tunknown %sS2K %d\n",
2944
0
                                ski->s2k.mode < 1000 ? "" : "GNU ",
2945
0
                                ski->s2k.mode);
2946
8.69k
      err = gpg_error (GPG_ERR_INV_PACKET);
2947
8.69k
      goto leave;
2948
27.3k
    }
2949
2950
              /* Print some info.  */
2951
18.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
18.6k
              if (list_mode)
2964
18.6k
                es_putc ('\n', listfp);
2965
2966
              /* Read remaining protection parameters.  */
2967
18.6k
        if (ski->s2k.mode == 3)
2968
3.43k
    {
2969
3.43k
      if (pktlen < 1)
2970
830
        {
2971
830
          err = gpg_error (GPG_ERR_INV_PACKET);
2972
830
          goto leave;
2973
830
        }
2974
2.60k
      ski->s2k.count = iobuf_get_noeof (inp);
2975
2.60k
      pktlen--;
2976
2.60k
      if (list_mode)
2977
2.60k
        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
2.60k
    }
2981
15.2k
        else if (ski->s2k.mode == 1002)
2982
11.0k
    {
2983
      /* Read the serial number. */
2984
11.0k
      if (pktlen < 1)
2985
5.58k
        {
2986
5.58k
          err = gpg_error (GPG_ERR_INV_PACKET);
2987
5.58k
          goto leave;
2988
5.58k
        }
2989
5.48k
      snlen = iobuf_get (inp);
2990
5.48k
      pktlen--;
2991
5.48k
      if (pktlen < snlen || snlen == (size_t)(-1))
2992
417
        {
2993
417
          err = gpg_error (GPG_ERR_INV_PACKET);
2994
417
          goto leave;
2995
417
        }
2996
5.48k
    }
2997
18.6k
      }
2998
6.11k
    else /* Old version; no S2K, so we set mode to 0, hash MD5.  */
2999
6.11k
      {
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
6.11k
        ski->s2k.mode = 0;
3004
6.11k
        ski->s2k.hash_algo = DIGEST_ALGO_MD5;
3005
6.11k
        if (list_mode)
3006
6.11k
    es_fprintf (listfp, "\tprotect algo: %d  (hash algo: %d)\n",
3007
0
                            ski->algo, ski->s2k.hash_algo);
3008
6.11k
      }
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
17.9k
    ski->ivlen = openpgp_cipher_blocklen (ski->algo);
3020
17.9k
    log_assert (ski->ivlen <= sizeof (temp));
3021
3022
17.9k
    if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003)
3023
1.89k
      ski->ivlen = 0;
3024
16.0k
    else if (ski->s2k.mode == 1002)
3025
5.06k
      ski->ivlen = snlen < 16 ? snlen : 16;
3026
3027
17.9k
    if (pktlen < ski->ivlen)
3028
1.87k
      {
3029
1.87k
              err = gpg_error (GPG_ERR_INV_PACKET);
3030
1.87k
        goto leave;
3031
1.87k
      }
3032
107k
    for (i = 0; i < ski->ivlen; i++, pktlen--)
3033
91.5k
      temp[i] = iobuf_get_noeof (inp);
3034
16.0k
    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
16.0k
    memcpy (ski->iv, temp, ski->ivlen);
3044
16.0k
  }
3045
3046
      /* Skip count of secret key material.  */
3047
22.6k
      if (is_v5)
3048
1.66k
        {
3049
1.66k
          if (pktlen < 4)
3050
812
            {
3051
812
              err = gpg_error (GPG_ERR_INV_PACKET);
3052
812
              goto leave;
3053
812
            }
3054
849
          skbytes = read_32 (inp);
3055
849
          pktlen -= 4;
3056
849
          if (list_mode)
3057
849
            es_fprintf (listfp, "\tskbytes: %u\n", skbytes);
3058
849
        }
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
21.8k
      if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002)
3065
5.86k
  {
3066
    /* Better set some dummy stuff here.  */
3067
5.86k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3068
5.86k
             xstrdup ("dummydata"),
3069
5.86k
             10 * 8);
3070
5.86k
    pktlen = 0;
3071
5.86k
  }
3072
16.0k
      else if (ski->s2k.mode == 1003)
3073
1.09k
  {
3074
1.09k
          void *tmpp;
3075
3076
1.09k
    if (pktlen < 2) /* At least two bytes for parenthesis.  */
3077
69
      {
3078
69
              err = gpg_error (GPG_ERR_INV_PACKET);
3079
69
        goto leave;
3080
69
      }
3081
3082
1.02k
          tmpp = read_rest (inp, pktlen);
3083
1.02k
          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
1.02k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3099
1.02k
             tmpp, tmpp? pktlen * 8 : 0);
3100
1.02k
          pktlen = 0;
3101
1.02k
  }
3102
14.9k
      else if (ski->is_protected)
3103
9.10k
  {
3104
9.10k
          void *tmpp;
3105
3106
9.10k
    if (pktlen < 2) /* At least two bytes for the length.  */
3107
1.65k
      {
3108
1.65k
              err = gpg_error (GPG_ERR_INV_PACKET);
3109
1.65k
        goto leave;
3110
1.65k
      }
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
7.44k
          tmpp = read_rest (inp, pktlen);
3118
7.44k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3119
7.44k
             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
7.44k
          if (pk->pkey[npkey])
3124
7.44k
            gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1);
3125
7.44k
    pktlen = 0;
3126
7.44k
    if (list_mode)
3127
7.44k
            es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey);
3128
7.44k
  }
3129
5.81k
      else
3130
5.81k
  {
3131
          /* Not encrypted.  */
3132
14.7k
    for (i = npkey; i < nskey; i++)
3133
9.66k
      {
3134
3135
9.66k
              if (pktlen < 2) /* At least two bytes for the length.  */
3136
747
                {
3137
747
                  err = gpg_error (GPG_ERR_INV_PACKET);
3138
747
                  goto leave;
3139
747
                }
3140
8.91k
              if (algorithm == PUBKEY_ALGO_KYBER && i == npkey+1)
3141
6
                {
3142
6
                  err = read_octet_string (inp, &pktlen, 4, 0, 1, pk->pkey+i);
3143
6
                  if (err)
3144
5
                    goto leave;
3145
6
                }
3146
8.90k
              else
3147
8.90k
                {
3148
8.90k
                  unsigned int n = pktlen;
3149
3150
8.90k
                  if (algorithm == PUBKEY_ALGO_ECDSA
3151
7.72k
                      || algorithm == PUBKEY_ALGO_EDDSA
3152
6.69k
                      || algorithm == PUBKEY_ALGO_ECDH
3153
6.44k
                      || algorithm == PUBKEY_ALGO_KYBER)
3154
2.46k
                    pk->pkey[i] = sos_read (inp, &n, 0);
3155
6.43k
                  else
3156
6.43k
                    pk->pkey[i] = mpi_read (inp, &n, 0);
3157
8.90k
                  pktlen -= n;
3158
8.90k
                }
3159
3160
8.90k
              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
8.90k
        if (!pk->pkey[i])
3168
3.93k
    err = gpg_error (GPG_ERR_INV_PACKET);
3169
8.90k
      }
3170
5.06k
    if (err)
3171
1.95k
      goto leave;
3172
3173
3.11k
    if (pktlen < 2)
3174
111
      {
3175
111
              err = gpg_error (GPG_ERR_INV_PACKET);
3176
111
        goto leave;
3177
111
      }
3178
3.00k
    ski->csum = read_16 (inp);
3179
3.00k
    pktlen -= 2;
3180
3.00k
    if (list_mode)
3181
3.00k
            es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum);
3182
3.00k
  }
3183
21.8k
    }
3184
3185
  /* Note that KEYID below has been initialized above in list_mode.  */
3186
658k
  if (list_mode)
3187
658k
    es_fprintf (listfp, "\tkeyid: %08lX%08lX\n",
3188
0
                (ulong) keyid[0], (ulong) keyid[1]);
3189
3190
732k
 leave:
3191
732k
  iobuf_skip_rest (inp, pktlen, 0);
3192
732k
  return err;
3193
658k
}
3194
3195
3196
/* Attribute subpackets have the same format as v4 signature
3197
   subpackets.  This is not part of OpenPGP, but is done in several
3198
   versions of PGP nevertheless.  */
3199
int
3200
parse_attribute_subpkts (PKT_user_id * uid)
3201
15.7k
{
3202
15.7k
  size_t n;
3203
15.7k
  int count = 0;
3204
15.7k
  struct user_attribute *attribs = NULL;
3205
15.7k
  const byte *buffer = uid->attrib_data;
3206
15.7k
  int buflen = uid->attrib_len;
3207
15.7k
  byte type;
3208
3209
15.7k
  xfree (uid->attribs);
3210
3211
21.2k
  while (buflen)
3212
8.41k
    {
3213
8.41k
      n = *buffer++;
3214
8.41k
      buflen--;
3215
8.41k
      if (n == 255)  /* 4 byte length header.  */
3216
996
  {
3217
996
    if (buflen < 4)
3218
400
      goto too_short;
3219
596
    n = buf32_to_size_t (buffer);
3220
596
    buffer += 4;
3221
596
    buflen -= 4;
3222
596
  }
3223
7.42k
      else if (n >= 192)  /* 2 byte special encoded length header.  */
3224
921
  {
3225
921
    if (buflen < 2)
3226
278
      goto too_short;
3227
643
    n = ((n - 192) << 8) + *buffer + 192;
3228
643
    buffer++;
3229
643
    buflen--;
3230
643
  }
3231
7.73k
      if (buflen < n)
3232
1.44k
  goto too_short;
3233
3234
6.29k
      if (!n)
3235
837
        {
3236
          /* Too short to encode the subpacket type.  */
3237
837
          if (opt.verbose)
3238
837
            log_info ("attribute subpacket too short\n");
3239
837
          break;
3240
837
        }
3241
3242
5.45k
      attribs = xrealloc (attribs,
3243
5.45k
                          (count + 1) * sizeof (struct user_attribute));
3244
5.45k
      memset (&attribs[count], 0, sizeof (struct user_attribute));
3245
3246
5.45k
      type = *buffer;
3247
5.45k
      buffer++;
3248
5.45k
      buflen--;
3249
5.45k
      n--;
3250
3251
5.45k
      attribs[count].type = type;
3252
5.45k
      attribs[count].data = buffer;
3253
5.45k
      attribs[count].len = n;
3254
5.45k
      buffer += n;
3255
5.45k
      buflen -= n;
3256
5.45k
      count++;
3257
5.45k
    }
3258
3259
13.6k
  uid->attribs = attribs;
3260
13.6k
  uid->numattribs = count;
3261
13.6k
  return count;
3262
3263
2.12k
 too_short:
3264
2.12k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
3265
2.12k
    log_info ("buffer shorter than attribute subpacket\n");
3266
2.12k
  uid->attribs = attribs;
3267
2.12k
  uid->numattribs = count;
3268
2.12k
  return count;
3269
15.7k
}
3270
3271
3272
static int
3273
parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3274
52.5k
{
3275
52.5k
  byte *p;
3276
3277
  /* Cap the size of a user ID at 2k: a value absurdly large enough
3278
     that there is no sane user ID string (which is printable text
3279
     as of RFC2440bis) that won't fit in it, but yet small enough to
3280
     avoid allocation problems.  A large pktlen may not be
3281
     allocatable, and a very large pktlen could actually cause our
3282
     allocation to wrap around in xmalloc to a small number. */
3283
3284
52.5k
  if (pktlen > MAX_UID_PACKET_LENGTH)
3285
1.72k
    {
3286
1.72k
      log_error ("packet(%d) too large\n", pkttype);
3287
1.72k
      if (list_mode)
3288
1.72k
        es_fprintf (listfp, ":user ID packet: [too large]\n");
3289
1.72k
      iobuf_skip_rest (inp, pktlen, 0);
3290
1.72k
      return GPG_ERR_INV_PACKET;
3291
1.72k
    }
3292
3293
50.8k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen);
3294
50.8k
  packet->pkt.user_id->len = pktlen;
3295
50.8k
  packet->pkt.user_id->ref = 1;
3296
3297
50.8k
  p = packet->pkt.user_id->name;
3298
730k
  for (; pktlen; pktlen--, p++)
3299
680k
    *p = iobuf_get_noeof (inp);
3300
50.8k
  *p = 0;
3301
3302
50.8k
  if (list_mode)
3303
0
    {
3304
0
      int n = packet->pkt.user_id->len;
3305
0
      es_fprintf (listfp, ":user ID packet: \"");
3306
      /* fixme: Hey why don't we replace this with es_write_sanitized?? */
3307
0
      for (p = packet->pkt.user_id->name; n; p++, n--)
3308
0
  {
3309
0
    if (*p >= ' ' && *p <= 'z')
3310
0
      es_putc (*p, listfp);
3311
0
    else
3312
0
      es_fprintf (listfp, "\\x%02x", *p);
3313
0
  }
3314
0
      es_fprintf (listfp, "\"\n");
3315
0
    }
3316
50.8k
  return 0;
3317
52.5k
}
3318
3319
3320
void
3321
make_attribute_uidname (PKT_user_id * uid, size_t max_namelen)
3322
15.7k
{
3323
15.7k
  log_assert (max_namelen > 70);
3324
15.7k
  if (uid->numattribs <= 0)
3325
11.4k
    sprintf (uid->name, "[bad attribute packet of size %lu]",
3326
11.4k
       uid->attrib_len);
3327
4.29k
  else if (uid->numattribs > 1)
3328
325
    sprintf (uid->name, "[%d attributes of size %lu]",
3329
325
       uid->numattribs, uid->attrib_len);
3330
3.96k
  else
3331
3.96k
    {
3332
      /* Only one attribute, so list it as the "user id" */
3333
3334
3.96k
      if (uid->attribs->type == ATTRIB_IMAGE)
3335
3.15k
  {
3336
3.15k
    u32 len;
3337
3.15k
    byte type;
3338
3339
3.15k
    if (parse_image_header (uid->attribs, &type, &len))
3340
2.06k
      sprintf (uid->name, "[%.20s image of size %lu]",
3341
2.06k
         image_type_to_string (type, 1), (ulong) len);
3342
1.08k
    else
3343
1.08k
      sprintf (uid->name, "[invalid image]");
3344
3.15k
  }
3345
813
      else
3346
813
  sprintf (uid->name, "[unknown attribute of size %lu]",
3347
813
     (ulong) uid->attribs->len);
3348
3.96k
    }
3349
3350
15.7k
  uid->len = strlen (uid->name);
3351
15.7k
}
3352
3353
3354
static int
3355
parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen,
3356
     PACKET * packet)
3357
16.0k
{
3358
16.0k
  byte *p;
3359
3360
16.0k
  (void) pkttype;
3361
3362
  /* We better cap the size of an attribute packet to make DoS not too
3363
     easy.  16MB should be more then enough for one attribute packet
3364
     (ie. a photo).  */
3365
16.0k
  if (pktlen > MAX_ATTR_PACKET_LENGTH)
3366
275
    {
3367
275
      log_error ("packet(%d) too large\n", pkttype);
3368
275
      if (list_mode)
3369
275
        es_fprintf (listfp, ":attribute packet: [too large]\n");
3370
275
      iobuf_skip_rest (inp, pktlen, 0);
3371
275
      return GPG_ERR_INV_PACKET;
3372
275
    }
3373
3374
15.7k
#define EXTRA_UID_NAME_SPACE 71
3375
15.7k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id
3376
15.7k
               + EXTRA_UID_NAME_SPACE);
3377
15.7k
  packet->pkt.user_id->ref = 1;
3378
15.7k
  packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1);
3379
15.7k
  packet->pkt.user_id->attrib_len = pktlen;
3380
3381
15.7k
  p = packet->pkt.user_id->attrib_data;
3382
1.36G
  for (; pktlen; pktlen--, p++)
3383
1.36G
    *p = iobuf_get_noeof (inp);
3384
3385
  /* Now parse out the individual attribute subpackets.  This is
3386
     somewhat pointless since there is only one currently defined
3387
     attribute type (jpeg), but it is correct by the spec. */
3388
15.7k
  parse_attribute_subpkts (packet->pkt.user_id);
3389
3390
15.7k
  make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE);
3391
3392
15.7k
  if (list_mode)
3393
0
    {
3394
0
      es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name);
3395
0
    }
3396
15.7k
  return 0;
3397
16.0k
}
3398
3399
3400
static int
3401
parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3402
8.92k
{
3403
8.92k
  byte *p;
3404
3405
  /* Cap comment packet at a reasonable value to avoid an integer
3406
     overflow in the malloc below.  Comment packets are actually not
3407
     anymore define my OpenPGP and we even stopped to use our
3408
     private comment packet.  */
3409
8.92k
  if (pktlen > MAX_COMMENT_PACKET_LENGTH)
3410
658
    {
3411
658
      log_error ("packet(%d) too large\n", pkttype);
3412
658
      if (list_mode)
3413
658
        es_fprintf (listfp, ":%scomment packet: [too large]\n",
3414
0
                    pkttype == PKT_OLD_COMMENT ? "OpenPGP draft " : "");
3415
658
      iobuf_skip_rest (inp, pktlen, 0);
3416
658
      return GPG_ERR_INV_PACKET;
3417
658
    }
3418
8.26k
  packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
3419
8.26k
  packet->pkt.comment->len = pktlen;
3420
8.26k
  p = packet->pkt.comment->data;
3421
2.02M
  for (; pktlen; pktlen--, p++)
3422
2.01M
    *p = iobuf_get_noeof (inp);
3423
3424
8.26k
  if (list_mode)
3425
0
    {
3426
0
      int n = packet->pkt.comment->len;
3427
0
      es_fprintf (listfp, ":%scomment packet: \"", pkttype == PKT_OLD_COMMENT ?
3428
0
                  "OpenPGP draft " : "");
3429
0
      for (p = packet->pkt.comment->data; n; p++, n--)
3430
0
  {
3431
0
    if (*p >= ' ' && *p <= 'z')
3432
0
      es_putc (*p, listfp);
3433
0
    else
3434
0
      es_fprintf (listfp, "\\x%02x", *p);
3435
0
  }
3436
0
      es_fprintf (listfp, "\"\n");
3437
0
    }
3438
8.26k
  return 0;
3439
8.92k
}
3440
3441
3442
/* Parse a ring trust packet RFC4880 (5.10).
3443
 *
3444
 * This parser is special in that the packet is not stored as a packet
3445
 * but its content is merged into the previous packet.  */
3446
static gpg_error_t
3447
parse_ring_trust (parse_packet_ctx_t ctx, unsigned long pktlen)
3448
13.1M
{
3449
13.1M
  gpg_error_t err;
3450
13.1M
  iobuf_t inp = ctx->inp;
3451
13.1M
  PKT_ring_trust rt = {0};
3452
13.1M
  int c;
3453
13.1M
  int not_gpg = 0;
3454
3455
13.1M
  if (!pktlen)
3456
5.61k
    {
3457
5.61k
      if (list_mode)
3458
5.61k
  es_fprintf (listfp, ":trust packet: empty\n");
3459
5.61k
      err = 0;
3460
5.61k
      goto leave;
3461
5.61k
    }
3462
3463
13.1M
  c = iobuf_get_noeof (inp);
3464
13.1M
  pktlen--;
3465
13.1M
  rt.trustval = c;
3466
13.1M
  if (pktlen)
3467
13.1M
    {
3468
13.1M
      if (!c)
3469
13.0M
        {
3470
13.0M
          c = iobuf_get_noeof (inp);
3471
          /* We require that bit 7 of the sigcache is 0 (easier
3472
           * eof handling).  */
3473
13.0M
          if (!(c & 0x80))
3474
13.0M
            rt.sigcache = c;
3475
13.0M
        }
3476
70.6k
      else
3477
70.6k
        iobuf_get_noeof (inp);  /* Dummy read.  */
3478
13.1M
      pktlen--;
3479
13.1M
    }
3480
3481
  /* Next is the optional subtype.  */
3482
13.1M
  if (pktlen > 3)
3483
13.1M
    {
3484
13.1M
      char tmp[4];
3485
13.1M
      tmp[0] = iobuf_get_noeof (inp);
3486
13.1M
      tmp[1] = iobuf_get_noeof (inp);
3487
13.1M
      tmp[2] = iobuf_get_noeof (inp);
3488
13.1M
      tmp[3] = iobuf_get_noeof (inp);
3489
13.1M
      pktlen -= 4;
3490
13.1M
      if (!memcmp (tmp, "gpg", 3))
3491
13.1M
        rt.subtype = tmp[3];
3492
38.9k
      else
3493
38.9k
        not_gpg = 1;
3494
13.1M
    }
3495
  /* If it is a key or uid subtype read the remaining data.  */
3496
13.1M
  if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID)
3497
72.8k
      && pktlen >= 6 )
3498
69.5k
    {
3499
69.5k
      int i;
3500
69.5k
      unsigned int namelen;
3501
3502
69.5k
      rt.keyorg = iobuf_get_noeof (inp);
3503
69.5k
      pktlen--;
3504
69.5k
      rt.keyupdate = read_32 (inp);
3505
69.5k
      pktlen -= 4;
3506
69.5k
      namelen = iobuf_get_noeof (inp);
3507
69.5k
      pktlen--;
3508
69.5k
      if (namelen && pktlen)
3509
11.5k
        {
3510
11.5k
          rt.url = xtrymalloc (namelen + 1);
3511
11.5k
          if (!rt.url)
3512
0
            {
3513
0
              err = gpg_error_from_syserror ();
3514
0
              goto leave;
3515
0
            }
3516
200k
          for (i = 0; pktlen && i < namelen; pktlen--, i++)
3517
189k
            rt.url[i] = iobuf_get_noeof (inp);
3518
11.5k
          rt.url[i] = 0;
3519
11.5k
        }
3520
69.5k
    }
3521
3522
13.1M
  if (list_mode)
3523
0
    {
3524
0
      if (rt.subtype == RING_TRUST_SIG)
3525
0
        es_fprintf (listfp, ":trust packet: sig flag=%02x sigcache=%02x\n",
3526
0
                    rt.trustval, rt.sigcache);
3527
0
      else if (rt.subtype == RING_TRUST_UID || rt.subtype == RING_TRUST_KEY)
3528
0
        {
3529
0
          unsigned char *p;
3530
3531
0
          es_fprintf (listfp, ":trust packet: %s upd=%lu src=%d%s",
3532
0
                      (rt.subtype == RING_TRUST_UID? "uid" : "key"),
3533
0
                      (unsigned long)rt.keyupdate,
3534
0
                      rt.keyorg,
3535
0
                      (rt.url? " url=":""));
3536
0
          if (rt.url)
3537
0
            {
3538
0
              for (p = rt.url; *p; p++)
3539
0
                {
3540
0
                  if (*p >= ' ' && *p <= 'z')
3541
0
                    es_putc (*p, listfp);
3542
0
                  else
3543
0
                    es_fprintf (listfp, "\\x%02x", *p);
3544
0
                }
3545
0
            }
3546
0
          es_putc ('\n', listfp);
3547
0
        }
3548
0
      else if (not_gpg)
3549
0
        es_fprintf (listfp, ":trust packet: not created by gpg\n");
3550
0
      else
3551
0
        es_fprintf (listfp, ":trust packet: subtype=%02x\n",
3552
0
                    rt.subtype);
3553
0
    }
3554
3555
  /* Now transfer the data to the respective packet.  Do not do this
3556
   * if SKIP_META is set.  */
3557
13.1M
  if (!ctx->last_pkt.pkt.generic || ctx->skip_meta)
3558
45.7k
    ;
3559
13.1M
  else if (rt.subtype == RING_TRUST_SIG
3560
13.0M
           && ctx->last_pkt.pkttype == PKT_SIGNATURE)
3561
13.0M
    {
3562
13.0M
      PKT_signature *sig = ctx->last_pkt.pkt.signature;
3563
3564
13.0M
      if ((rt.sigcache & 1))
3565
8.22M
        {
3566
8.22M
          sig->flags.checked = 1;
3567
8.22M
          sig->flags.valid = !!(rt.sigcache & 2);
3568
8.22M
        }
3569
13.0M
    }
3570
61.7k
  else if (rt.subtype == RING_TRUST_UID
3571
33.1k
           && (ctx->last_pkt.pkttype == PKT_USER_ID
3572
11.1k
               || ctx->last_pkt.pkttype == PKT_ATTRIBUTE))
3573
21.9k
    {
3574
21.9k
      PKT_user_id *uid = ctx->last_pkt.pkt.user_id;
3575
3576
21.9k
      uid->keyorg = rt.keyorg;
3577
21.9k
      uid->keyupdate = rt.keyupdate;
3578
21.9k
      uid->updateurl = rt.url;
3579
21.9k
      rt.url = NULL;
3580
21.9k
    }
3581
39.8k
  else if (rt.subtype == RING_TRUST_KEY
3582
28.2k
           && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY
3583
17.1k
               || ctx->last_pkt.pkttype == PKT_SECRET_KEY))
3584
11.0k
    {
3585
11.0k
      PKT_public_key *pk = ctx->last_pkt.pkt.public_key;
3586
3587
11.0k
      pk->keyorg = rt.keyorg;
3588
11.0k
      pk->keyupdate = rt.keyupdate;
3589
11.0k
      pk->updateurl = rt.url;
3590
11.0k
      rt.url = NULL;
3591
11.0k
    }
3592
3593
13.1M
  err = 0;
3594
3595
13.1M
 leave:
3596
13.1M
  xfree (rt.url);
3597
13.1M
  free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL.  */
3598
13.1M
  iobuf_skip_rest (inp, pktlen, 0);
3599
13.1M
  return err;
3600
13.1M
}
3601
3602
3603
static int
3604
parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen,
3605
     PACKET * pkt, int new_ctb, int partial)
3606
100k
{
3607
100k
  int rc = 0;
3608
100k
  int mode, namelen;
3609
100k
  PKT_plaintext *pt;
3610
100k
  byte *p;
3611
100k
  int c, i;
3612
3613
100k
  if (!partial && pktlen < 6)
3614
7.28k
    {
3615
7.28k
      log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen);
3616
7.28k
      if (list_mode)
3617
7.28k
        es_fputs (":literal data packet: [too short]\n", listfp);
3618
7.28k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3619
7.28k
      goto leave;
3620
7.28k
    }
3621
92.9k
  mode = iobuf_get_noeof (inp);
3622
92.9k
  if (pktlen)
3623
83.3k
    pktlen--;
3624
92.9k
  namelen = iobuf_get_noeof (inp);
3625
92.9k
  if (pktlen)
3626
83.3k
    pktlen--;
3627
  /* Note that namelen will never exceed 255 bytes. */
3628
92.9k
  pt = pkt->pkt.plaintext =
3629
92.9k
    xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1);
3630
92.9k
  pt->new_ctb = new_ctb;
3631
92.9k
  pt->mode = mode;
3632
92.9k
  pt->namelen = namelen;
3633
92.9k
  pt->is_partial = partial;
3634
92.9k
  if (pktlen)
3635
83.3k
    {
3636
434k
      for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++)
3637
350k
  pt->name[i] = iobuf_get_noeof (inp);
3638
83.3k
    }
3639
9.62k
  else
3640
9.62k
    {
3641
35.7k
      for (i = 0; i < namelen; i++)
3642
27.2k
  if ((c = iobuf_get (inp)) == -1)
3643
1.13k
    break;
3644
26.1k
  else
3645
26.1k
    pt->name[i] = c;
3646
9.62k
    }
3647
  /* Fill up NAME so that a check with valgrind won't complain about
3648
   * reading from uninitialized memory.  This case may be triggred by
3649
   * corrupted packets.  */
3650
4.38M
  for (; i < namelen; i++)
3651
4.28M
    pt->name[i] = 0;
3652
3653
92.9k
  pt->timestamp = read_32 (inp);
3654
92.9k
  if (pktlen)
3655
83.3k
    pktlen -= 4;
3656
92.9k
  pt->len = pktlen;
3657
92.9k
  pt->buf = inp;
3658
3659
92.9k
  if (list_mode)
3660
0
    {
3661
0
      es_fprintf (listfp, ":literal data packet:\n"
3662
0
                  "\tmode %c (%X), created %lu, name=\"",
3663
0
                  mode >= ' ' && mode < 'z' ? mode : '?', mode,
3664
0
                  (ulong) pt->timestamp);
3665
0
      for (p = pt->name, i = 0; i < namelen; p++, i++)
3666
0
  {
3667
0
    if (*p >= ' ' && *p <= 'z')
3668
0
      es_putc (*p, listfp);
3669
0
    else
3670
0
      es_fprintf (listfp, "\\x%02x", *p);
3671
0
  }
3672
0
      es_fprintf (listfp, "\",\n\traw data: ");
3673
0
      if (partial)
3674
0
  es_fprintf (listfp, "unknown length\n");
3675
0
      else
3676
0
  es_fprintf (listfp, "%lu bytes\n", (ulong) pt->len);
3677
0
    }
3678
3679
100k
 leave:
3680
100k
  return rc;
3681
92.9k
}
3682
3683
3684
static int
3685
parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen,
3686
      PACKET * pkt, int new_ctb)
3687
384k
{
3688
384k
  PKT_compressed *zd;
3689
3690
  /* PKTLEN is here 0, but data follows (this should be the last
3691
     object in a file or the compress algorithm should know the
3692
     length).  */
3693
384k
  (void) pkttype;
3694
384k
  (void) pktlen;
3695
3696
384k
  zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed);
3697
384k
  zd->algorithm = iobuf_get_noeof (inp);
3698
384k
  zd->len = 0;      /* not used */
3699
384k
  zd->new_ctb = new_ctb;
3700
384k
  zd->buf = inp;
3701
384k
  if (list_mode)
3702
384k
    es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
3703
384k
  return 0;
3704
384k
}
3705
3706
3707
static int
3708
parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen,
3709
     PACKET * pkt, int new_ctb, int partial)
3710
27.9k
{
3711
27.9k
  int rc = 0;
3712
27.9k
  PKT_encrypted *ed;
3713
27.9k
  unsigned long orig_pktlen = pktlen;
3714
3715
27.9k
  ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted);
3716
  /* ed->len is set below.  */
3717
27.9k
  ed->extralen = 0;  /* Unknown here; only used in build_packet.  */
3718
27.9k
  ed->buf = NULL;
3719
27.9k
  ed->new_ctb = new_ctb;
3720
27.9k
  ed->is_partial = partial;
3721
27.9k
  ed->aead_algo = 0;
3722
27.9k
  ed->cipher_algo = 0; /* Only used with AEAD.  */
3723
27.9k
  ed->chunkbyte = 0;   /* Only used with AEAD.  */
3724
27.9k
  if (pkttype == PKT_ENCRYPTED_MDC)
3725
9.80k
    {
3726
      /* Fixme: add some pktlen sanity checks.  */
3727
9.80k
      int version;
3728
3729
9.80k
      version = iobuf_get_noeof (inp);
3730
9.80k
      if (orig_pktlen)
3731
591
  pktlen--;
3732
9.80k
      if (version != 1)
3733
6.27k
  {
3734
6.27k
    log_error ("encrypted_mdc packet with unknown version %d\n",
3735
6.27k
         version);
3736
6.27k
          if (list_mode)
3737
6.27k
            es_fputs (":encrypted data packet: [unknown version]\n", listfp);
3738
    /*skip_rest(inp, pktlen); should we really do this? */
3739
6.27k
    rc = gpg_error (GPG_ERR_INV_PACKET);
3740
6.27k
    goto leave;
3741
6.27k
  }
3742
3.53k
      ed->mdc_method = DIGEST_ALGO_SHA1;
3743
3.53k
    }
3744
18.1k
  else
3745
18.1k
    ed->mdc_method = 0;
3746
3747
  /* A basic sanity check.  We need at least an 8 byte IV plus the 2
3748
     detection bytes.  Note that we don't known the algorithm and thus
3749
     we may only check against the minimum blocksize.  */
3750
21.7k
  if (orig_pktlen && pktlen < 10)
3751
410
    {
3752
      /* Actually this is blocksize+2.  */
3753
410
      log_error ("packet(%d) too short\n", pkttype);
3754
410
      if (list_mode)
3755
410
        es_fputs (":encrypted data packet: [too short]\n", listfp);
3756
410
      rc = GPG_ERR_INV_PACKET;
3757
410
      iobuf_skip_rest (inp, pktlen, partial);
3758
410
      goto leave;
3759
410
    }
3760
3761
  /* Store the remaining length of the encrypted data (i.e. without
3762
     the MDC version number but with the IV etc.).  This value is
3763
     required during decryption.  */
3764
21.3k
  ed->len = pktlen;
3765
3766
21.3k
  if (list_mode)
3767
0
    {
3768
0
      if (orig_pktlen)
3769
0
  es_fprintf (listfp, ":encrypted data packet:\n\tlength: %lu\n",
3770
0
                    orig_pktlen);
3771
0
      else
3772
0
  es_fprintf (listfp, ":encrypted data packet:\n\tlength: unknown\n");
3773
0
      if (ed->mdc_method)
3774
0
  es_fprintf (listfp, "\tmdc_method: %d\n", ed->mdc_method);
3775
0
    }
3776
3777
21.3k
  ed->buf = inp;
3778
3779
27.9k
 leave:
3780
27.9k
  return rc;
3781
21.3k
}
3782
3783
3784
/* Note, that this code is not anymore used in real life because the
3785
   MDC checking is now done right after the decryption in
3786
   decrypt_data.  */
3787
static int
3788
parse_mdc (IOBUF inp, int pkttype, unsigned long pktlen,
3789
     PACKET * pkt, int new_ctb)
3790
9.20k
{
3791
9.20k
  int rc = 0;
3792
9.20k
  PKT_mdc *mdc;
3793
9.20k
  byte *p;
3794
3795
9.20k
  (void) pkttype;
3796
3797
9.20k
  mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc);
3798
9.20k
  if (list_mode)
3799
9.20k
    es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
3800
9.20k
  if (!new_ctb || pktlen != 20)
3801
3.64k
    {
3802
3.64k
      log_error ("mdc_packet with invalid encoding\n");
3803
3.64k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3804
3.64k
      goto leave;
3805
3.64k
    }
3806
5.55k
  p = mdc->hash;
3807
116k
  for (; pktlen; pktlen--, p++)
3808
111k
    *p = iobuf_get_noeof (inp);
3809
3810
9.20k
 leave:
3811
9.20k
  return rc;
3812
5.55k
}
3813
3814
3815
static gpg_error_t
3816
parse_encrypted_aead (iobuf_t inp, int pkttype, unsigned long pktlen,
3817
                      PACKET *pkt, int partial)
3818
15.6k
{
3819
15.6k
  int rc = 0;
3820
15.6k
  PKT_encrypted *ed;
3821
15.6k
  unsigned long orig_pktlen = pktlen;
3822
15.6k
  int version;
3823
3824
15.6k
  ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted);
3825
15.6k
  if (!ed)
3826
0
    return gpg_error_from_syserror ();
3827
15.6k
  ed->len = 0;
3828
15.6k
  ed->extralen = 0;  /* (only used in build_packet.)  */
3829
15.6k
  ed->buf = NULL;
3830
15.6k
  ed->new_ctb = 1;   /* (packet number requires a new CTB anyway.)  */
3831
15.6k
  ed->is_partial = partial;
3832
15.6k
  ed->mdc_method = 0;
3833
  /* A basic sanity check.  We need one version byte, one algo byte,
3834
   * one aead algo byte, one chunkbyte, at least 15 byte IV.  */
3835
15.6k
  if (orig_pktlen && pktlen < 19)
3836
782
    {
3837
782
      log_error ("packet(%d) too short\n", pkttype);
3838
782
      if (list_mode)
3839
782
        es_fputs (":aead encrypted packet: [too short]\n", listfp);
3840
782
      rc = gpg_error (GPG_ERR_INV_PACKET);
3841
782
      iobuf_skip_rest (inp, pktlen, partial);
3842
782
      goto leave;
3843
782
    }
3844
3845
14.9k
  version = iobuf_get_noeof (inp);
3846
14.9k
  if (orig_pktlen)
3847
948
    pktlen--;
3848
14.9k
  if (version != 1)
3849
1.40k
    {
3850
1.40k
      log_error ("aead encrypted packet with unknown version %d\n",
3851
1.40k
                 version);
3852
1.40k
      if (list_mode)
3853
1.40k
        es_fputs (":aead encrypted packet: [unknown version]\n", listfp);
3854
      /*skip_rest(inp, pktlen); should we really do this? */
3855
1.40k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3856
1.40k
      goto leave;
3857
1.40k
    }
3858
3859
13.5k
  ed->cipher_algo = iobuf_get_noeof (inp);
3860
13.5k
  if (orig_pktlen)
3861
257
    pktlen--;
3862
13.5k
  ed->aead_algo = iobuf_get_noeof (inp);
3863
13.5k
  if (orig_pktlen)
3864
257
    pktlen--;
3865
13.5k
  ed->chunkbyte = iobuf_get_noeof (inp);
3866
13.5k
  if (orig_pktlen)
3867
257
    pktlen--;
3868
3869
  /* Store the remaining length of the encrypted data.  We read the
3870
   * rest during decryption.  */
3871
13.5k
  ed->len = pktlen;
3872
3873
13.5k
  if (list_mode)
3874
0
    {
3875
0
      es_fprintf (listfp, ":aead encrypted packet: cipher=%u aead=%u cb=%u\n",
3876
0
                  ed->cipher_algo, ed->aead_algo, ed->chunkbyte);
3877
0
      if (orig_pktlen)
3878
0
  es_fprintf (listfp, "\tlength: %lu\n", orig_pktlen);
3879
0
      else
3880
0
  es_fprintf (listfp, "\tlength: unknown\n");
3881
0
    }
3882
3883
13.5k
  ed->buf = inp;
3884
3885
15.6k
 leave:
3886
15.6k
  return rc;
3887
13.5k
}
3888
3889
3890
/*
3891
 * This packet is internally generated by us (in armor.c) to transfer
3892
 * some information to the lower layer.  To make sure that this packet
3893
 * is really a GPG faked one and not one coming from outside, we
3894
 * first check that there is a unique tag in it.
3895
 *
3896
 * The format of such a control packet is:
3897
 *   n byte  session marker
3898
 *   1 byte  control type CTRLPKT_xxxxx
3899
 *   m byte  control data
3900
 */
3901
static int
3902
parse_gpg_control (IOBUF inp, int pkttype, unsigned long pktlen,
3903
       PACKET * packet, int partial)
3904
15.4k
{
3905
15.4k
  byte *p;
3906
15.4k
  const byte *sesmark;
3907
15.4k
  size_t sesmarklen;
3908
15.4k
  int i;
3909
3910
15.4k
  (void) pkttype;
3911
3912
15.4k
  if (list_mode)
3913
15.4k
    es_fprintf (listfp, ":packet 63: length %lu ", pktlen);
3914
3915
15.4k
  sesmark = get_session_marker (&sesmarklen);
3916
15.4k
  if (pktlen < sesmarklen + 1)  /* 1 is for the control bytes */
3917
5.09k
    goto skipit;
3918
129k
  for (i = 0; i < sesmarklen; i++, pktlen--)
3919
122k
    {
3920
122k
      if (sesmark[i] != iobuf_get_noeof (inp))
3921
2.91k
  goto skipit;
3922
122k
    }
3923
7.44k
  if (pktlen > 4096)
3924
4
    goto skipit;  /* Definitely too large.  We skip it to avoid an
3925
                     overflow in the malloc.  */
3926
7.44k
  if (list_mode)
3927
7.44k
    es_fputs ("- gpg control packet", listfp);
3928
3929
7.44k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
3930
7.44k
             + pktlen - 1);
3931
7.44k
  packet->pkt.gpg_control->control = iobuf_get_noeof (inp);
3932
7.44k
  pktlen--;
3933
7.44k
  packet->pkt.gpg_control->datalen = pktlen;
3934
7.44k
  p = packet->pkt.gpg_control->data;
3935
25.8k
  for (; pktlen; pktlen--, p++)
3936
18.4k
    *p = iobuf_get_noeof (inp);
3937
3938
7.44k
  return 0;
3939
3940
8.01k
 skipit:
3941
8.01k
  if (list_mode)
3942
0
    {
3943
0
      int c;
3944
3945
0
      i = 0;
3946
0
      es_fprintf (listfp, "- private (rest length %lu)\n", pktlen);
3947
0
      if (partial)
3948
0
  {
3949
0
    while ((c = iobuf_get (inp)) != -1)
3950
0
      dump_hex_line (c, &i);
3951
0
  }
3952
0
      else
3953
0
  {
3954
0
    for (; pktlen; pktlen--)
3955
0
      {
3956
0
        dump_hex_line ((c = iobuf_get (inp)), &i);
3957
0
        if (c == -1)
3958
0
    break;
3959
0
      }
3960
0
  }
3961
0
      es_putc ('\n', listfp);
3962
0
    }
3963
8.01k
  iobuf_skip_rest (inp, pktlen, 0);
3964
8.01k
  return gpg_error (GPG_ERR_INV_PACKET);
3965
7.44k
}
3966
3967
3968
/* Create a GPG control packet to be used internally as a placeholder.  */
3969
PACKET *
3970
create_gpg_control (ctrlpkttype_t type, const byte * data, size_t datalen)
3971
91.4k
{
3972
91.4k
  PACKET *packet;
3973
91.4k
  byte *p;
3974
3975
91.4k
  if (!data)
3976
0
    datalen = 0;
3977
3978
91.4k
  packet = xmalloc (sizeof *packet);
3979
91.4k
  init_packet (packet);
3980
91.4k
  packet->pkttype = PKT_GPG_CONTROL;
3981
91.4k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen);
3982
91.4k
  packet->pkt.gpg_control->control = type;
3983
91.4k
  packet->pkt.gpg_control->datalen = datalen;
3984
91.4k
  p = packet->pkt.gpg_control->data;
3985
5.19M
  for (; datalen; datalen--, p++)
3986
5.10M
    *p = *data++;
3987
3988
91.4k
  return packet;
3989
91.4k
}