Coverage Report

Created: 2026-01-10 07:05

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
39.9M
{
96
39.9M
  unsigned short a;
97
39.9M
  a = (unsigned short)iobuf_get_noeof (inp) << 8;
98
39.9M
  a |= iobuf_get_noeof (inp);
99
39.9M
  return a;
100
39.9M
}
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.54M
{
107
1.54M
  unsigned long a;
108
1.54M
  a = (unsigned long)iobuf_get_noeof (inp) << 24;
109
1.54M
  a |= iobuf_get_noeof (inp) << 16;
110
1.54M
  a |= iobuf_get_noeof (inp) << 8;
111
1.54M
  a |= iobuf_get_noeof (inp);
112
1.54M
  return a;
113
1.54M
}
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
319k
{
131
319k
  int c, c1, c2, i;
132
319k
  unsigned int nmax = *ret_nread;
133
319k
  unsigned int nbits, nbytes;
134
319k
  size_t nread = 0;
135
319k
  gcry_mpi_t a = NULL;
136
319k
  byte *buf = NULL;
137
319k
  byte *p;
138
139
319k
  if (!nmax)
140
717
    goto overflow;
141
142
318k
  if ((c = c1 = iobuf_get (inp)) == -1)
143
499
    goto leave;
144
318k
  if (++nread == nmax)
145
1.25k
    goto overflow;
146
316k
  nbits = c << 8;
147
316k
  if ((c = c2 = iobuf_get (inp)) == -1)
148
305
    goto leave;
149
316k
  ++nread;
150
316k
  nbits |= c;
151
316k
  if (nbits > MAX_EXTERN_MPI_BITS)
152
5.43k
    {
153
5.43k
      log_error ("mpi too large (%u bits)\n", nbits);
154
5.43k
      goto leave;
155
5.43k
    }
156
157
311k
  nbytes = (nbits + 7) / 8;
158
311k
  buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2);
159
311k
  p = buf;
160
311k
  p[0] = c1;
161
311k
  p[1] = c2;
162
18.2M
  for (i = 0; i < nbytes; i++)
163
17.9M
    {
164
17.9M
      if (nread == nmax)
165
6.97k
  goto overflow;
166
167
17.9M
      c = iobuf_get (inp);
168
17.9M
      if (c == -1)
169
952
  goto leave;
170
171
17.9M
      p[i + 2] = c;
172
17.9M
      nread ++;
173
17.9M
    }
174
175
303k
  if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread))
176
0
    a = NULL;
177
178
303k
  *ret_nread = nread;
179
303k
  gcry_free(buf);
180
303k
  return a;
181
182
8.95k
 overflow:
183
8.95k
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
184
16.1k
 leave:
185
16.1k
  *ret_nread = nread;
186
16.1k
  gcry_free(buf);
187
16.1k
  return a;
188
8.95k
}
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
156
{
208
156
  gpg_error_t err;
209
156
  int c, i;
210
156
  byte *buf = NULL;
211
156
  byte *p;
212
213
156
  *r_data = NULL;
214
215
156
  if ((nbytes && nlength)
216
156
      || (!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
156
  if (nlength)
223
156
    {
224
766
      for (i = 0; i < nlength; i++)
225
616
        {
226
616
          if (!*pktlen)
227
0
            {
228
0
              err = gpg_error (GPG_ERR_INV_PACKET);
229
0
              goto leave;
230
0
            }
231
616
          c = iobuf_readbyte (inp);
232
616
          if (c < 0)
233
6
            {
234
6
              err =  gpg_error (GPG_ERR_INV_PACKET);
235
6
              goto leave;
236
6
            }
237
610
          --*pktlen;
238
610
          nbytes <<= 8;
239
610
          nbytes |= c;
240
610
        }
241
242
150
      if (!nbytes)
243
2
        {
244
2
          err =  gpg_error (GPG_ERR_INV_PACKET);
245
2
          goto leave;
246
2
        }
247
150
    }
248
249
148
  if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS)
250
36
      || (nbytes*8 < nbytes))
251
114
    {
252
114
      log_error ("octet string too large (%u octets)\n", nbytes);
253
114
      err = gpg_error (GPG_ERR_TOO_LARGE);
254
114
      goto leave;
255
114
    }
256
257
34
  if (nbytes > *pktlen)
258
3
    {
259
3
      log_error ("octet string larger than packet (%u octets)\n", nbytes);
260
3
      err = gpg_error (GPG_ERR_INV_PACKET);
261
3
      goto leave;
262
3
    }
263
264
31
  buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes);
265
31
  if (!buf)
266
0
    {
267
0
      err = gpg_error_from_syserror ();
268
0
      goto leave;
269
0
    }
270
31
  p = buf;
271
6.41k
  for (i = 0; i < nbytes; i++)
272
6.40k
    {
273
6.40k
      c = iobuf_get (inp);
274
6.40k
      if (c == -1)
275
17
        {
276
17
          err = gpg_error (GPG_ERR_INV_PACKET);
277
17
          goto leave;
278
17
        }
279
280
6.38k
      p[i] = c;
281
6.38k
      --*pktlen;
282
6.38k
    }
283
284
14
  *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8);
285
14
  gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2);
286
14
  return 0;
287
288
142
 leave:
289
142
  gcry_free (buf);
290
142
  return err;
291
31
}
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
39.9M
{
308
39.9M
  int c, c1, c2, i;
309
39.9M
  unsigned int nmax = *ret_nread;
310
39.9M
  unsigned int nbits, nbytes;
311
39.9M
  size_t nread = 0;
312
39.9M
  gcry_mpi_t a = NULL;
313
39.9M
  byte *buf = NULL;
314
39.9M
  byte *p;
315
316
39.9M
  if (!nmax)
317
345
    goto overflow;
318
319
39.9M
  if ((c = c1 = iobuf_get (inp)) == -1)
320
567
    goto leave;
321
39.9M
  if (++nread == nmax)
322
58
    goto overflow;
323
39.9M
  nbits = c << 8;
324
39.9M
  if ((c = c2 = iobuf_get (inp)) == -1)
325
139
    goto leave;
326
39.9M
  ++nread;
327
39.9M
  nbits |= c;
328
39.9M
  if (nbits > MAX_EXTERN_MPI_BITS)
329
3.02k
    {
330
3.02k
      log_error ("mpi too large (%u bits)\n", nbits);
331
3.02k
      goto leave;
332
3.02k
    }
333
334
39.9M
  nbytes = (nbits + 7) / 8;
335
39.9M
  buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes);
336
39.9M
  p = buf;
337
1.31G
  for (i = 0; i < nbytes; i++)
338
1.27G
    {
339
1.27G
      if (nread == nmax)
340
1.37k
        goto overflow;
341
342
1.27G
      c = iobuf_get (inp);
343
1.27G
      if (c == -1)
344
244
        goto leave;
345
346
1.27G
      p[i] = c;
347
1.27G
      nread ++;
348
1.27G
    }
349
350
39.9M
  a = gcry_mpi_set_opaque (NULL, buf, nbits);
351
39.9M
  gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2);
352
39.9M
  *ret_nread = nread;
353
39.9M
  return a;
354
355
1.77k
 overflow:
356
1.77k
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
357
5.75k
 leave:
358
5.75k
  *ret_nread = nread;
359
5.75k
  gcry_free(buf);
360
5.75k
  return a;
361
1.77k
}
362
363
364
/* Register STRING as a known critical notation name.  */
365
void
366
register_known_notation (const char *string)
367
3.20k
{
368
3.20k
  strlist_t sl;
369
370
3.20k
  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
3.20k
  if (!string)
377
3.20k
    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
24.7k
{
395
24.7k
  int old = list_mode;
396
24.7k
  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
24.7k
  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
24.7k
  return old;
430
24.7k
}
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
384k
{
438
384k
  static byte unknown_pubkey_algos[256];
439
440
  /* First check whether the algorithm is usable but not suitable for
441
     encryption/signing.  */
442
384k
  if (pubkey_get_npkey (algo))
443
241k
    {
444
241k
      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
241k
    }
454
143k
  else
455
143k
    {
456
143k
      algo &= 0xff;
457
143k
      if (!unknown_pubkey_algos[algo])
458
505
        {
459
505
          if (opt.verbose && !glo_ctrl.silence_parse_warnings)
460
505
            log_info (_("can't handle public key algorithm %d\n"), algo);
461
505
          unknown_pubkey_algos[algo] = 1;
462
505
        }
463
143k
    }
464
384k
}
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
22.6M
{
472
22.6M
  int skip, rc;
473
474
22.6M
  do
475
22.6M
    {
476
22.6M
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l);
477
22.6M
    }
478
22.6M
  while (skip && ! rc);
479
22.6M
  return rc;
480
22.6M
}
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
22.6M
{
735
22.6M
  int rc = 0;
736
22.6M
  iobuf_t inp;
737
22.6M
  int c, ctb, pkttype, lenbytes;
738
22.6M
  unsigned long pktlen;
739
22.6M
  byte hdr[8];
740
22.6M
  int hdrlen;
741
22.6M
  int new_ctb = 0, partial = 0;
742
22.6M
  int with_uid = (onlykeypkts == 2);
743
22.6M
  off_t pos;
744
745
22.6M
  *skip = 0;
746
22.6M
  inp = ctx->inp;
747
748
42.7M
 again:
749
42.7M
  log_assert (!pkt->pkt.generic);
750
42.7M
  if (retpos || list_mode)
751
0
    {
752
0
      pos = iobuf_tell (inp);
753
0
      if (retpos)
754
0
        *retpos = pos;
755
0
    }
756
42.7M
  else
757
42.7M
    pos = 0; /* (silence compiler warning) */
758
759
  /* The first byte of a packet is the so-called tag.  The highest bit
760
     must be set.  */
761
42.7M
  if ((ctb = iobuf_get (inp)) == -1)
762
42.1k
    {
763
42.1k
      rc = -1;
764
42.1k
      goto leave;
765
42.1k
    }
766
42.6M
  ctx->last_ctb = ctb;
767
42.6M
  hdrlen = 0;
768
42.6M
  hdr[hdrlen++] = ctb;
769
770
42.6M
  if (!(ctb & 0x80))
771
194k
    {
772
194k
      log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb);
773
194k
      rc = gpg_error (GPG_ERR_INV_PACKET);
774
194k
      goto leave;
775
194k
    }
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
42.4M
  pktlen = 0;
785
42.4M
  new_ctb = !!(ctb & 0x40);
786
42.4M
  if (new_ctb)
787
712k
    pkttype = ctb & 0x3f;
788
41.7M
  else
789
41.7M
    pkttype = (ctb >> 2) & 0xf;
790
791
42.4M
  if (ctx->only_fookey_enc
792
0
      && !(pkttype == PKT_SYMKEY_ENC || pkttype == PKT_PUBKEY_ENC))
793
0
    {
794
0
      rc = gpg_error (GPG_ERR_TRUE);
795
0
      goto leave;
796
0
    }
797
798
42.4M
  if (new_ctb)
799
712k
    {
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
712k
      if ((c = iobuf_get (inp)) == -1)
805
1.08k
  {
806
1.08k
    log_error ("%s: 1st length byte missing\n", iobuf_where (inp));
807
1.08k
    rc = gpg_error (GPG_ERR_INV_PACKET);
808
1.08k
    goto leave;
809
1.08k
  }
810
811
812
711k
      hdr[hdrlen++] = c;
813
711k
      if (c < 192)
814
559k
        pktlen = c;
815
152k
      else if (c < 224)
816
5.96k
        {
817
5.96k
          pktlen = (c - 192) * 256;
818
5.96k
          if ((c = iobuf_get (inp)) == -1)
819
439
            {
820
439
              log_error ("%s: 2nd length byte missing\n",
821
439
                         iobuf_where (inp));
822
439
              rc = gpg_error (GPG_ERR_INV_PACKET);
823
439
              goto leave;
824
439
            }
825
5.53k
          hdr[hdrlen++] = c;
826
5.53k
          pktlen += c + 192;
827
5.53k
        }
828
146k
      else if (c == 255)
829
2.70k
        {
830
2.70k
    int i;
831
2.70k
    char value[4];
832
833
13.1k
    for (i = 0; i < 4; i ++)
834
10.6k
            {
835
10.6k
              if ((c = iobuf_get (inp)) == -1)
836
164
                {
837
164
                  log_error ("%s: 4 byte length invalid\n", iobuf_where (inp));
838
164
                  rc = gpg_error (GPG_ERR_INV_PACKET);
839
164
                  goto leave;
840
164
                }
841
10.4k
              value[i] = hdr[hdrlen++] = c;
842
10.4k
            }
843
844
2.53k
    pktlen = buf32_to_ulong (value);
845
2.53k
        }
846
143k
      else /* Partial body length.  */
847
143k
        {
848
143k
          switch (pkttype)
849
143k
            {
850
32.1k
            case PKT_PLAINTEXT:
851
32.8k
            case PKT_ENCRYPTED:
852
37.4k
            case PKT_ENCRYPTED_MDC:
853
38.0k
            case PKT_ENCRYPTED_AEAD:
854
140k
            case PKT_COMPRESSED:
855
140k
              iobuf_set_partial_body_length_mode (inp, c & 0xff);
856
140k
              pktlen = 0; /* To indicate partial length.  */
857
140k
              partial = 1;
858
140k
              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
143k
            }
866
143k
        }
867
868
711k
    }
869
41.7M
  else /* This is an old format packet.  */
870
41.7M
    {
871
      /* The type of length encoding is encoded in bits 0-1 of the
872
   tag.  */
873
41.7M
      lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3));
874
41.7M
      if (!lenbytes)
875
261k
  {
876
261k
    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
261k
    partial = 1;
880
261k
    if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT
881
259k
        && pkttype != PKT_COMPRESSED)
882
4.74k
      {
883
4.74k
        log_error ("%s: indeterminate length for invalid"
884
4.74k
       " packet type %d\n", iobuf_where (inp), pkttype);
885
4.74k
        rc = gpg_error (GPG_ERR_INV_PACKET);
886
4.74k
        goto leave;
887
4.74k
      }
888
261k
  }
889
41.4M
      else
890
41.4M
  {
891
83.1M
    for (; lenbytes; lenbytes--)
892
41.6M
      {
893
41.6M
        pktlen <<= 8;
894
41.6M
        c = iobuf_get (inp);
895
41.6M
        if (c == -1)
896
1.46k
    {
897
1.46k
      log_error ("%s: length invalid\n", iobuf_where (inp));
898
1.46k
      rc = gpg_error (GPG_ERR_INV_PACKET);
899
1.46k
      goto leave;
900
1.46k
    }
901
41.6M
        pktlen |= hdr[hdrlen++] = c;
902
41.6M
      }
903
41.4M
  }
904
41.7M
    }
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
42.4M
  if (pkttype == 63 && pktlen == 0xFFFFFFFF)
914
0
    {
915
      /* With some probability this is caused by a problem in the
916
       * the uncompressing layer - in some error cases it just loops
917
       * and spits out 0xff bytes. */
918
0
      log_error ("%s: garbled packet detected\n", iobuf_where (inp));
919
0
      g10_exit (2);
920
0
    }
921
922
42.4M
  if (out && pkttype)
923
0
    {
924
      /* This type of copying won't work if the packet uses a partial
925
   body length.  (In other words, this only works if HDR is
926
   actually the length.)  Currently, no callers require this
927
   functionality so we just log this as an error.  */
928
0
      if (partial)
929
0
  {
930
0
    log_error ("parse: Can't copy partial packet.  Aborting.\n");
931
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
932
0
    goto leave;
933
0
  }
934
935
0
      rc = iobuf_write (out, hdr, hdrlen);
936
0
      if (!rc)
937
0
  rc = copy_packet (inp, out, pkttype, pktlen, partial);
938
0
      goto leave;
939
0
    }
940
941
42.4M
  if (with_uid && pkttype == PKT_USER_ID)
942
    /* If ONLYKEYPKTS is set to 2, then we never skip user id packets,
943
       even if DO_SKIP is set.  */
944
0
    ;
945
42.4M
  else if (do_skip
946
     /* type==0 is not allowed.  This is an invalid packet.  */
947
42.4M
     || !pkttype
948
     /* When ONLYKEYPKTS is set, we don't skip keys.  */
949
42.4M
     || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
950
0
         && pkttype != PKT_PUBLIC_KEY
951
0
         && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY))
952
21.3k
    {
953
21.3k
      iobuf_skip_rest (inp, pktlen, partial);
954
21.3k
      *skip = 1;
955
21.3k
      rc = 0;
956
21.3k
      goto leave;
957
21.3k
    }
958
959
42.4M
  if (DBG_PACKET)
960
0
    {
961
0
#if DEBUG_PARSE_PACKET
962
0
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n",
963
0
     iobuf_id (inp), pkttype, pktlen, new_ctb ? " (new_ctb)" : "",
964
0
     dbg_w, dbg_f, dbg_l);
965
#else
966
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s\n",
967
     iobuf_id (inp), pkttype, pktlen,
968
     new_ctb ? " (new_ctb)" : "");
969
#endif
970
0
    }
971
972
42.4M
  if (list_mode)
973
42.4M
    es_fprintf (listfp, "# off=%lu ctb=%02x tag=%d hlen=%d plen=%lu%s%s\n",
974
0
                (unsigned long)pos, ctb, pkttype, hdrlen, pktlen,
975
0
                partial? (new_ctb ? " partial" : " indeterminate") :"",
976
0
                new_ctb? " new-ctb":"");
977
978
  /* Count it.  */
979
42.4M
  ctx->n_parsed_packets++;
980
981
42.4M
  pkt->pkttype = pkttype;
982
42.4M
  rc = GPG_ERR_UNKNOWN_PACKET;  /* default error */
983
42.4M
  switch (pkttype)
984
42.4M
    {
985
49.1k
    case PKT_PUBLIC_KEY:
986
979k
    case PKT_PUBLIC_SUBKEY:
987
1.01M
    case PKT_SECRET_KEY:
988
1.06M
    case PKT_SECRET_SUBKEY:
989
1.06M
      pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key);
990
1.06M
      rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt);
991
1.06M
      break;
992
48.6k
    case PKT_SYMKEY_ENC:
993
48.6k
      rc = parse_symkeyenc (inp, pkttype, pktlen, pkt);
994
48.6k
      break;
995
25.4k
    case PKT_PUBKEY_ENC:
996
25.4k
      rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt);
997
25.4k
      break;
998
20.4M
    case PKT_SIGNATURE:
999
20.4M
      pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature);
1000
20.4M
      rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature);
1001
20.4M
      break;
1002
46.1k
    case PKT_ONEPASS_SIG:
1003
46.1k
      pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig);
1004
46.1k
      rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig);
1005
46.1k
      break;
1006
50.6k
    case PKT_USER_ID:
1007
50.6k
      rc = parse_user_id (inp, pkttype, pktlen, pkt);
1008
50.6k
      break;
1009
16.6k
    case PKT_ATTRIBUTE:
1010
16.6k
      pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
1011
16.6k
      rc = parse_attribute (inp, pkttype, pktlen, pkt);
1012
16.6k
      break;
1013
8.05k
    case PKT_OLD_COMMENT:
1014
9.47k
    case PKT_COMMENT:
1015
9.47k
      rc = parse_comment (inp, pkttype, pktlen, pkt);
1016
9.47k
      break;
1017
20.0M
    case PKT_RING_TRUST:
1018
20.0M
      {
1019
20.0M
        rc = parse_ring_trust (ctx, pktlen);
1020
20.0M
        if (!rc)
1021
20.0M
          goto again; /* Directly read the next packet.  */
1022
20.0M
      }
1023
0
      break;
1024
136k
    case PKT_PLAINTEXT:
1025
136k
      rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial);
1026
136k
      break;
1027
376k
    case PKT_COMPRESSED:
1028
376k
      rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb);
1029
376k
      break;
1030
104k
    case PKT_ENCRYPTED:
1031
111k
    case PKT_ENCRYPTED_MDC:
1032
111k
      rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial);
1033
111k
      break;
1034
10.0k
    case PKT_MDC:
1035
10.0k
      rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb);
1036
10.0k
      break;
1037
20.0k
    case PKT_ENCRYPTED_AEAD:
1038
20.0k
      rc = parse_encrypted_aead (inp, pkttype, pktlen, pkt, partial);
1039
20.0k
      break;
1040
40.1k
    case PKT_GPG_CONTROL:
1041
40.1k
      rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial);
1042
40.1k
      break;
1043
10.5k
    case PKT_MARKER:
1044
10.5k
      rc = parse_marker (inp, pkttype, pktlen);
1045
10.5k
      break;
1046
35.7k
    default:
1047
      /* Unknown packet.  Skip it.  */
1048
35.7k
      skip_packet (inp, pkttype, pktlen, partial);
1049
35.7k
      break;
1050
42.4M
    }
1051
1052
  /* Store a shallow copy of certain packets in the context.  */
1053
22.4M
  free_packet (NULL, ctx);
1054
22.4M
  if (!rc && (pkttype == PKT_PUBLIC_KEY
1055
22.1M
              || pkttype == PKT_SECRET_KEY
1056
22.1M
              || pkttype == PKT_USER_ID
1057
22.0M
              || pkttype == PKT_ATTRIBUTE
1058
22.0M
              || pkttype == PKT_SIGNATURE))
1059
20.5M
    {
1060
20.5M
      ctx->last_pkt = *pkt;
1061
20.5M
    }
1062
1063
22.6M
 leave:
1064
  /* FIXME: We leak in case of an error (see the xmalloc's above).  */
1065
22.6M
  if (!rc && iobuf_error (inp))
1066
7.17k
    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
22.6M
  return rc == -1? -1 : gpg_err_code (rc);
1072
22.4M
}
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
35.7k
{
1151
35.7k
  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
35.7k
  iobuf_skip_rest (inp, pktlen, partial);
1178
35.7k
}
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
384k
{
1188
384k
  int c;
1189
384k
  byte *buf, *p;
1190
1191
384k
  buf = xtrymalloc (pktlen);
1192
384k
  if (!buf)
1193
317k
    {
1194
317k
      gpg_error_t err = gpg_error_from_syserror ();
1195
317k
      log_error ("error reading rest of packet: %s\n", gpg_strerror (err));
1196
317k
      return NULL;
1197
317k
    }
1198
2.17M
  for (p = buf; pktlen; pktlen--)
1199
2.10M
    {
1200
2.10M
      c = iobuf_get (inp);
1201
2.10M
      if (c == -1)
1202
2.89k
        {
1203
2.89k
          log_error ("premature eof while reading rest of packet\n");
1204
2.89k
          xfree (buf);
1205
2.89k
          return NULL;
1206
2.89k
        }
1207
2.10M
      *p++ = c;
1208
2.10M
    }
1209
1210
64.5k
  return buf;
1211
67.4k
}
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.89M
{
1222
1.89M
  char buffer[256];
1223
1.89M
  char *tmpbuf;
1224
1.89M
  int i, c, nbytes;
1225
1226
1.89M
  *r_data = NULL;
1227
1228
1.89M
  if (!*pktlen)
1229
228
    return gpg_error (GPG_ERR_INV_PACKET);
1230
1.89M
  c = iobuf_readbyte (inp);
1231
1.89M
  if (c < 0)
1232
116
    return gpg_error (GPG_ERR_INV_PACKET);
1233
1.89M
  --*pktlen;
1234
1.89M
  nbytes = c;
1235
1.89M
  if (nbytes < 2 || nbytes > 254)
1236
441
    return gpg_error (GPG_ERR_INV_PACKET);
1237
1.89M
  if (nbytes > *pktlen)
1238
633
    return gpg_error (GPG_ERR_INV_PACKET);
1239
1240
1.89M
  buffer[0] = nbytes;
1241
1242
14.1M
  for (i = 0; i < nbytes; i++)
1243
12.2M
    {
1244
12.2M
      c = iobuf_get (inp);
1245
12.2M
      if (c < 0)
1246
198
        return gpg_error (GPG_ERR_INV_PACKET);
1247
12.2M
      --*pktlen;
1248
12.2M
      buffer[1+i] = c;
1249
12.2M
    }
1250
1251
1.89M
  tmpbuf = xtrymalloc (1 + nbytes);
1252
1.89M
  if (!tmpbuf)
1253
0
    return gpg_error_from_syserror ();
1254
1.89M
  memcpy (tmpbuf, buffer, 1 + nbytes);
1255
1.89M
  *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes));
1256
1.89M
  if (!*r_data)
1257
0
    {
1258
0
      xfree (tmpbuf);
1259
0
      return gpg_error_from_syserror ();
1260
0
    }
1261
1.89M
  return 0;
1262
1.89M
}
1263
1264
1265
/* Parse a marker packet.  */
1266
static int
1267
parse_marker (IOBUF inp, int pkttype, unsigned long pktlen)
1268
10.5k
{
1269
10.5k
  (void) pkttype;
1270
1271
10.5k
  if (pktlen != 3)
1272
6.92k
    goto fail;
1273
1274
3.58k
  if (iobuf_get (inp) != 'P')
1275
408
    {
1276
408
      pktlen--;
1277
408
      goto fail;
1278
408
    }
1279
1280
3.17k
  if (iobuf_get (inp) != 'G')
1281
608
    {
1282
608
      pktlen--;
1283
608
      goto fail;
1284
608
    }
1285
1286
2.57k
  if (iobuf_get (inp) != 'P')
1287
609
    {
1288
609
      pktlen--;
1289
609
      goto fail;
1290
609
    }
1291
1292
1.96k
  if (list_mode)
1293
1.96k
    es_fputs (":marker packet: PGP\n", listfp);
1294
1295
1.96k
  return 0;
1296
1297
8.54k
 fail:
1298
8.54k
  log_error ("invalid marker packet\n");
1299
8.54k
  if (list_mode)
1300
8.54k
    es_fputs (":marker packet: [invalid]\n", listfp);
1301
8.54k
  iobuf_skip_rest (inp, pktlen, 0);
1302
8.54k
  return GPG_ERR_INV_PACKET;
1303
2.57k
}
1304
1305
1306
static int
1307
parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1308
     PACKET * packet)
1309
48.6k
{
1310
48.6k
  PKT_symkey_enc *k;
1311
48.6k
  int rc = 0;
1312
48.6k
  int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen;
1313
1314
48.6k
  if (pktlen < 4)
1315
13.0k
    goto too_short;
1316
35.6k
  version = iobuf_get_noeof (inp);
1317
35.6k
  pktlen--;
1318
35.6k
  if (version == 4)
1319
23.1k
    ;
1320
12.4k
  else if (version == 5)
1321
7.20k
    ;
1322
5.22k
  else
1323
5.22k
    {
1324
5.22k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
1325
5.22k
      if (list_mode)
1326
5.22k
        es_fprintf (listfp, ":symkey enc packet: [unknown version]\n");
1327
5.22k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1328
5.22k
      goto leave;
1329
5.22k
    }
1330
30.4k
  if (pktlen > 200)
1331
251
    {       /* (we encode the seskeylen in a byte) */
1332
251
      log_error ("packet(%d) too large\n", pkttype);
1333
251
      if (list_mode)
1334
251
        es_fprintf (listfp, ":symkey enc packet: [too large]\n");
1335
251
      rc = gpg_error (GPG_ERR_INV_PACKET);
1336
251
      goto leave;
1337
251
    }
1338
30.1k
  cipher_algo = iobuf_get_noeof (inp);
1339
30.1k
  pktlen--;
1340
30.1k
  if (version == 5)
1341
7.18k
    {
1342
7.18k
      aead_algo = iobuf_get_noeof (inp);
1343
7.18k
      pktlen--;
1344
7.18k
    }
1345
22.9k
  else
1346
22.9k
    aead_algo = 0;
1347
30.1k
  if (pktlen < 2)
1348
3.87k
    goto too_short;
1349
26.2k
  s2kmode = iobuf_get_noeof (inp);
1350
26.2k
  pktlen--;
1351
26.2k
  hash_algo = iobuf_get_noeof (inp);
1352
26.2k
  pktlen--;
1353
26.2k
  switch (s2kmode)
1354
26.2k
    {
1355
10.8k
    case 0: /* Simple S2K.  */
1356
10.8k
      minlen = 0;
1357
10.8k
      break;
1358
2.37k
    case 1: /* Salted S2K.  */
1359
2.37k
      minlen = 8;
1360
2.37k
      break;
1361
9.36k
    case 3: /* Iterated+salted S2K.  */
1362
9.36k
      minlen = 9;
1363
9.36k
      break;
1364
3.65k
    default:
1365
3.65k
      log_error ("unknown S2K mode %d\n", s2kmode);
1366
3.65k
      if (list_mode)
1367
3.65k
        es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n");
1368
3.65k
      goto leave;
1369
26.2k
    }
1370
22.6k
  if (minlen > pktlen)
1371
6.23k
    {
1372
6.23k
      log_error ("packet with S2K %d too short\n", s2kmode);
1373
6.23k
      if (list_mode)
1374
6.23k
        es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1375
6.23k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1376
6.23k
      goto leave;
1377
6.23k
    }
1378
16.3k
  seskeylen = pktlen - minlen;
1379
16.3k
  k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc);
1380
16.3k
  k->version = version;
1381
16.3k
  k->cipher_algo = cipher_algo;
1382
16.3k
  k->aead_algo = aead_algo;
1383
16.3k
  k->s2k.mode = s2kmode;
1384
16.3k
  k->s2k.hash_algo = hash_algo;
1385
16.3k
  if (s2kmode == 1 || s2kmode == 3)
1386
5.50k
    {
1387
49.5k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
1388
44.0k
  k->s2k.salt[i] = iobuf_get_noeof (inp);
1389
5.50k
    }
1390
16.3k
  if (s2kmode == 3)
1391
4.57k
    {
1392
4.57k
      k->s2k.count = iobuf_get_noeof (inp);
1393
4.57k
      pktlen--;
1394
4.57k
    }
1395
16.3k
  k->seskeylen = seskeylen;
1396
16.3k
  if (k->seskeylen)
1397
8.44k
    {
1398
8.44k
      k->seskey = xcalloc (1, seskeylen);
1399
55.2k
      for (i = 0; i < seskeylen && pktlen; i++, pktlen--)
1400
46.7k
  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
8.44k
      if (s2kmode != 1 && s2kmode != 3)
1406
8.44k
  log_info (_("WARNING: potentially insecure symmetrically"
1407
6.87k
        " encrypted session key\n"));
1408
8.44k
    }
1409
16.3k
  log_assert (!pktlen);
1410
1411
16.3k
  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
48.6k
 leave:
1442
48.6k
  iobuf_skip_rest (inp, pktlen, 0);
1443
48.6k
  return rc;
1444
1445
16.9k
 too_short:
1446
16.9k
  log_error ("packet(%d) too short\n", pkttype);
1447
16.9k
  if (list_mode)
1448
16.9k
    es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1449
16.9k
  rc = gpg_error (GPG_ERR_INV_PACKET);
1450
16.9k
  goto leave;
1451
16.3k
}
1452
1453
1454
/* Parse a public key encrypted packet (Tag 1).  */
1455
static int
1456
parse_pubkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1457
     PACKET * packet)
1458
25.4k
{
1459
25.4k
  int rc = 0;
1460
25.4k
  int i, ndata;
1461
25.4k
  unsigned int n;
1462
25.4k
  PKT_pubkey_enc *k;
1463
1464
25.4k
  k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc);
1465
25.4k
  if (pktlen < 12)
1466
12.0k
    {
1467
12.0k
      log_error ("packet(%d) too short\n", pkttype);
1468
12.0k
      if (list_mode)
1469
12.0k
        es_fputs (":pubkey enc packet: [too short]\n", listfp);
1470
12.0k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1471
12.0k
      goto leave;
1472
12.0k
    }
1473
13.3k
  k->version = iobuf_get_noeof (inp);
1474
13.3k
  pktlen--;
1475
13.3k
  if (k->version != 2 && k->version != 3)
1476
508
    {
1477
508
      log_error ("packet(%d) with unknown version %d\n", pkttype, k->version);
1478
508
      if (list_mode)
1479
508
        es_fputs (":pubkey enc packet: [unknown version]\n", listfp);
1480
508
      rc = gpg_error (GPG_ERR_INV_PACKET);
1481
508
      goto leave;
1482
508
    }
1483
12.8k
  k->keyid[0] = read_32 (inp);
1484
12.8k
  pktlen -= 4;
1485
12.8k
  k->keyid[1] = read_32 (inp);
1486
12.8k
  pktlen -= 4;
1487
12.8k
  k->pubkey_algo = iobuf_get_noeof (inp);
1488
12.8k
  pktlen--;
1489
12.8k
  k->throw_keyid = 0;  /* Only used as flag for build_packet.  */
1490
12.8k
  if (list_mode)
1491
12.8k
    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
12.8k
  ndata = pubkey_get_nenc (k->pubkey_algo);
1497
12.8k
  if (!ndata)
1498
7.42k
    {
1499
7.42k
      if (list_mode)
1500
7.42k
  es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo);
1501
7.42k
      unknown_pubkey_warning (k->pubkey_algo);
1502
7.42k
      k->data[0] = NULL; /* No need to store the encrypted data.  */
1503
7.42k
    }
1504
5.41k
  else if (k->pubkey_algo == PUBKEY_ALGO_ECDH)
1505
1.55k
    {
1506
1.55k
      log_assert (ndata == 2);
1507
      /* Get the ephemeral public key.  */
1508
1.55k
      n = pktlen;
1509
1.55k
      k->data[0] = sos_read (inp, &n, 0);
1510
1.55k
      pktlen -= n;
1511
1.55k
      if (!k->data[0])
1512
584
        {
1513
584
          rc = gpg_error (GPG_ERR_INV_PACKET);
1514
584
          goto leave;
1515
584
        }
1516
      /* Get the wrapped symmetric key.  */
1517
966
      rc = read_sized_octet_string (inp, &pktlen, k->data + 1);
1518
966
      if (rc)
1519
837
        goto leave;
1520
966
    }
1521
3.86k
  else if (k->pubkey_algo == PUBKEY_ALGO_KYBER)
1522
8
    {
1523
8
      log_assert (ndata == 3);
1524
      /* Get the ephemeral public key.  */
1525
8
      n = pktlen;
1526
8
      k->data[0] = sos_read (inp, &n, 0);
1527
8
      pktlen -= n;
1528
8
      if (!k->data[0])
1529
3
        {
1530
3
          rc = gpg_error (GPG_ERR_INV_PACKET);
1531
3
          goto leave;
1532
3
        }
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
3.85k
  else
1551
3.85k
    {
1552
10.0k
      for (i = 0; i < ndata; i++)
1553
6.15k
        {
1554
6.15k
          n = pktlen;
1555
6.15k
          k->data[i] = mpi_read (inp, &n, 0);
1556
6.15k
          pktlen -= n;
1557
6.15k
          if (!k->data[i])
1558
2.44k
            rc = gpg_error (GPG_ERR_INV_PACKET);
1559
6.15k
        }
1560
3.85k
      if (rc)
1561
1.88k
        goto leave;
1562
3.85k
    }
1563
9.52k
  if (list_mode)
1564
0
    {
1565
0
      if (k->seskey_algo)
1566
0
        es_fprintf (listfp, "\tsession key algo: %d\n", k->seskey_algo);
1567
0
      for (i = 0; i < ndata; i++)
1568
0
        {
1569
0
          es_fprintf (listfp, "\tdata: ");
1570
0
          mpi_print (listfp, k->data[i], mpi_print_mode);
1571
0
          es_putc ('\n', listfp);
1572
0
        }
1573
0
    }
1574
1575
1576
25.4k
 leave:
1577
25.4k
  iobuf_skip_rest (inp, pktlen, 0);
1578
25.4k
  return rc;
1579
9.52k
}
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
32.8M
{
1876
32.8M
  switch (type)
1877
32.8M
    {
1878
6.68k
    case SIGSUBPKT_REV_KEY:
1879
6.68k
      if (n < 22)
1880
2.21k
  break;
1881
4.47k
      return 0;
1882
12.8M
    case SIGSUBPKT_SIG_CREATED:
1883
12.8M
    case SIGSUBPKT_SIG_EXPIRE:
1884
12.9M
    case SIGSUBPKT_KEY_EXPIRE:
1885
12.9M
      if (n < 4)
1886
10.9k
  break;
1887
12.8M
      return 0;
1888
252k
    case SIGSUBPKT_KEY_FLAGS:
1889
256k
    case SIGSUBPKT_KS_FLAGS:
1890
260k
    case SIGSUBPKT_PREF_SYM:
1891
260k
    case SIGSUBPKT_PREF_AEAD:
1892
263k
    case SIGSUBPKT_PREF_HASH:
1893
267k
    case SIGSUBPKT_PREF_COMPR:
1894
267k
    case SIGSUBPKT_POLICY:
1895
274k
    case SIGSUBPKT_PREF_KS:
1896
278k
    case SIGSUBPKT_FEATURES:
1897
279k
    case SIGSUBPKT_REGEXP:
1898
279k
    case SIGSUBPKT_ATTST_SIGS:
1899
279k
      return 0;
1900
5.94k
    case SIGSUBPKT_SIGNATURE:
1901
19.4k
    case SIGSUBPKT_EXPORTABLE:
1902
21.0k
    case SIGSUBPKT_REVOCABLE:
1903
21.2k
    case SIGSUBPKT_REVOC_REASON:
1904
21.2k
      if (!n)
1905
2.69k
  break;
1906
18.5k
      return 0;
1907
5.51M
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1908
5.51M
      if (n < 8)
1909
6.16k
  break;
1910
5.51M
      return 0;
1911
14.0M
    case SIGSUBPKT_ISSUER_FPR:  /* issuer key fingerprint */
1912
14.0M
      if (n < 21)
1913
868
  break;
1914
14.0M
      return 0;
1915
5.38k
    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
5.38k
      if (n < 8
1920
3.36k
    || 8 + ((buffer[4] << 8) | buffer[5]) +
1921
3.36k
    ((buffer[6] << 8) | buffer[7]) != n)
1922
2.36k
  break;
1923
3.01k
      return 0;
1924
315
    case SIGSUBPKT_PRIMARY_UID:
1925
315
      if (n != 1)
1926
0
  break;
1927
315
      return 0;
1928
18.6k
    case SIGSUBPKT_TRUST:
1929
18.6k
      if (n != 2)
1930
18.2k
  break;
1931
377
      return 0;
1932
1.92k
    case SIGSUBPKT_KEY_BLOCK:
1933
1.92k
      if (n && buffer[0])
1934
764
        return -1; /* Unknown version - ignore.  */
1935
1.16k
      if (n < 50)
1936
1.16k
  break;  /* Definitely too short to carry a key block.  */
1937
2
      return 0;
1938
27.8k
    default:
1939
27.8k
      return 0;
1940
32.8M
    }
1941
44.6k
  return -2;
1942
32.8M
}
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
3.20k
{
1949
3.20k
  strlist_t sl;
1950
1951
3.20k
  register_known_notation (NULL); /* Make sure it is initialized.  */
1952
1953
6.39k
  for (sl = known_notations_list; sl; sl = sl->next)
1954
3.20k
    if (sl->flags == len && !memcmp (sl->d, name, len))
1955
13
      return 1; /* Known */
1956
1957
3.19k
  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
3.19k
  return 0; /* Unknown.  */
1965
3.20k
}
1966
1967
1968
static int
1969
can_handle_critical (const byte * buffer, size_t n, int type)
1970
5.65M
{
1971
5.65M
  switch (type)
1972
5.65M
    {
1973
5.42k
    case SIGSUBPKT_NOTATION:
1974
5.42k
      if (n >= 8)
1975
3.40k
  {
1976
3.40k
    size_t notation_len = ((buffer[4] << 8) | buffer[5]);
1977
3.40k
    if (n - 8 >= notation_len)
1978
3.20k
      return can_handle_critical_notation (buffer + 8, notation_len);
1979
3.40k
  }
1980
2.21k
      return 0;
1981
1.33k
    case SIGSUBPKT_SIGNATURE:
1982
5.11k
    case SIGSUBPKT_SIG_CREATED:
1983
7.23k
    case SIGSUBPKT_SIG_EXPIRE:
1984
176k
    case SIGSUBPKT_KEY_EXPIRE:
1985
178k
    case SIGSUBPKT_EXPORTABLE:
1986
190k
    case SIGSUBPKT_REVOCABLE:
1987
197k
    case SIGSUBPKT_REV_KEY:
1988
206k
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1989
209k
    case SIGSUBPKT_ISSUER_FPR:  /* issuer fingerprint */
1990
211k
    case SIGSUBPKT_PREF_SYM:
1991
212k
    case SIGSUBPKT_PREF_AEAD:
1992
214k
    case SIGSUBPKT_PREF_HASH:
1993
217k
    case SIGSUBPKT_PREF_COMPR:
1994
227k
    case SIGSUBPKT_KEY_FLAGS:
1995
228k
    case SIGSUBPKT_PRIMARY_UID:
1996
232k
    case SIGSUBPKT_FEATURES:
1997
235k
    case SIGSUBPKT_TRUST:
1998
235k
    case SIGSUBPKT_REGEXP:
1999
235k
    case SIGSUBPKT_ATTST_SIGS:
2000
      /* Is it enough to show the policy or keyserver? */
2001
236k
    case SIGSUBPKT_POLICY:
2002
240k
    case SIGSUBPKT_PREF_KS:
2003
240k
    case SIGSUBPKT_REVOC_REASON: /* At least we know about it.  */
2004
240k
      return 1;
2005
2006
2.33k
    case SIGSUBPKT_KEY_BLOCK:
2007
2.33k
      if (n && !buffer[0])
2008
44
        return 1;
2009
2.29k
      else
2010
2.29k
        return 0;
2011
2012
5.40M
    default:
2013
5.40M
      return 0;
2014
5.65M
    }
2015
5.65M
}
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
291M
{
2022
291M
  const byte *buffer;
2023
291M
  int buflen;
2024
291M
  int type;
2025
291M
  int critical_dummy;
2026
291M
  int offset;
2027
291M
  size_t n;
2028
291M
  const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed;
2029
291M
  int seq = 0;
2030
291M
  int reqseq = start ? *start : 0;
2031
2032
291M
  if (!critical)
2033
291M
    critical = &critical_dummy;
2034
2035
291M
  if (!pktbuf || reqseq == -1)
2036
416k
    {
2037
416k
      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
416k
      return reqtype ==  SIGSUBPKT_TEST_CRITICAL ? dummy : NULL;
2041
416k
    }
2042
291M
  buffer = pktbuf->data;
2043
291M
  buflen = pktbuf->len;
2044
927M
  while (buflen)
2045
765M
    {
2046
765M
      n = *buffer++;
2047
765M
      buflen--;
2048
765M
      if (n == 255) /* 4 byte length header.  */
2049
363k
  {
2050
363k
    if (buflen < 4)
2051
81.9k
      goto too_short;
2052
281k
    n = buf32_to_size_t (buffer);
2053
281k
    buffer += 4;
2054
281k
    buflen -= 4;
2055
281k
  }
2056
764M
      else if (n >= 192) /* 4 byte special encoded length header.  */
2057
672k
  {
2058
672k
    if (buflen < 2)
2059
159k
      goto too_short;
2060
512k
    n = ((n - 192) << 8) + *buffer + 192;
2061
512k
    buffer++;
2062
512k
    buflen--;
2063
512k
  }
2064
765M
      if (buflen < n)
2065
89.9M
  goto too_short;
2066
675M
      if (!buflen)
2067
898k
        goto no_type_byte;
2068
674M
      type = *buffer;
2069
674M
      if (type & 0x80)
2070
76.8M
  {
2071
76.8M
    type &= 0x7f;
2072
76.8M
    *critical = 1;
2073
76.8M
  }
2074
597M
      else
2075
597M
  *critical = 0;
2076
674M
      if (!(++seq > reqseq))
2077
46.4k
  ;
2078
674M
      else if (reqtype == SIGSUBPKT_TEST_CRITICAL)
2079
46.8M
  {
2080
46.8M
    if (*critical)
2081
5.66M
      {
2082
5.66M
        if (n - 1 > buflen + 1)
2083
8.22k
    goto too_short;
2084
5.65M
        if (!can_handle_critical (buffer + 1, n - 1, type))
2085
5.41M
    {
2086
5.41M
      if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2087
5.41M
        log_info (_("subpacket of type %d has "
2088
0
        "critical bit set\n"), type);
2089
5.41M
      if (start)
2090
0
        *start = seq;
2091
5.41M
      return NULL; /* This is an error.  */
2092
5.41M
    }
2093
5.65M
      }
2094
46.8M
  }
2095
627M
      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
627M
      else if (type == reqtype) /* Found.  */
2099
32.8M
  {
2100
32.8M
    buffer++;
2101
32.8M
    n--;
2102
32.8M
    if (n > buflen)
2103
15.4k
      goto too_short;
2104
32.8M
    if (ret_n)
2105
14.3M
      *ret_n = n;
2106
32.8M
    offset = parse_one_sig_subpkt (buffer, n, type);
2107
32.8M
    switch (offset)
2108
32.8M
      {
2109
44.6k
      case -2:
2110
44.6k
        log_error ("subpacket of type %d too short\n", type);
2111
44.6k
        return NULL;
2112
764
      case -1:
2113
764
        return NULL;
2114
32.7M
      default:
2115
32.7M
        break;
2116
32.8M
      }
2117
32.7M
    if (start)
2118
13.0k
      *start = seq;
2119
32.7M
    return buffer + offset;
2120
32.8M
  }
2121
635M
      buffer += n;
2122
635M
      buflen -= n;
2123
635M
    }
2124
161M
  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
25.6M
    return buffer;
2130
2131
  /* Critical bit we don't understand. */
2132
136M
  if (start)
2133
506k
    *start = -1;
2134
136M
  return NULL;  /* End of packets; not found.  */
2135
2136
90.2M
 too_short:
2137
90.2M
  if (opt.debug && !glo_ctrl.silence_parse_warnings)
2138
0
    {
2139
0
      es_fflush (es_stdout);
2140
0
      log_printhex (pktbuf->data, pktbuf->len > 16? 16 : pktbuf->len,
2141
0
                    "buffer shorter than subpacket (%zu/%d/%zu); dump:",
2142
0
                    pktbuf->len, buflen, n);
2143
0
    }
2144
2145
90.2M
  if (start)
2146
5.68M
    *start = -1;
2147
90.2M
  return NULL;
2148
2149
898k
 no_type_byte:
2150
898k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2151
898k
    log_info ("type octet missing in subpacket\n");
2152
898k
  if (start)
2153
1.45k
    *start = -1;
2154
898k
  return NULL;
2155
161M
}
2156
2157
2158
const byte *
2159
parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype,
2160
      size_t *ret_n)
2161
285M
{
2162
285M
  return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL);
2163
285M
}
2164
2165
2166
const byte *
2167
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype)
2168
26.1M
{
2169
26.1M
  const byte *p;
2170
2171
26.1M
  p = parse_sig_subpkt (sig, 1, reqtype, NULL);
2172
26.1M
  if (!p)
2173
26.1M
    p = parse_sig_subpkt (sig, 0, reqtype, NULL);
2174
26.1M
  return p;
2175
26.1M
}
2176
2177
2178
/* Find all revocation keys.  Look in hashed area only.  */
2179
void
2180
parse_revkeys (PKT_signature * sig)
2181
5.69M
{
2182
5.69M
  const byte *revkey;
2183
5.69M
  int seq = 0;
2184
5.69M
  size_t len;
2185
2186
5.69M
  if (sig->sig_class != 0x1F)
2187
0
    return;
2188
2189
5.69M
  while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY,
2190
5.69M
                                    &len, &seq, NULL)))
2191
4.47k
    {
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.47k
      if ((len == 22 || len == 34)
2196
4.15k
          && (revkey[0] & 0x80))
2197
1.96k
  {
2198
1.96k
    sig->revkey = xrealloc (sig->revkey,
2199
1.96k
          sizeof (struct revocation_key) *
2200
1.96k
          (sig->numrevkeys + 1));
2201
2202
1.96k
    sig->revkey[sig->numrevkeys].class = revkey[0];
2203
1.96k
    sig->revkey[sig->numrevkeys].algid = revkey[1];
2204
1.96k
          len -= 2;
2205
1.96k
    sig->revkey[sig->numrevkeys].fprlen = len;
2206
1.96k
    memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len);
2207
1.96k
    memset (sig->revkey[sig->numrevkeys].fpr+len, 0,
2208
1.96k
                  sizeof (sig->revkey[sig->numrevkeys].fpr) - len);
2209
1.96k
    sig->numrevkeys++;
2210
1.96k
  }
2211
4.47k
    }
2212
5.69M
}
2213
2214
2215
int
2216
parse_signature (IOBUF inp, int pkttype, unsigned long pktlen,
2217
     PKT_signature * sig)
2218
20.4M
{
2219
20.4M
  int md5_len = 0;
2220
20.4M
  unsigned n;
2221
20.4M
  int is_v4or5 = 0;
2222
20.4M
  int rc = 0;
2223
20.4M
  int i, ndata;
2224
2225
20.4M
  if (pktlen < 16)
2226
364k
    {
2227
364k
      log_error ("packet(%d) too short\n", pkttype);
2228
364k
      if (list_mode)
2229
364k
        es_fputs (":signature packet: [too short]\n", listfp);
2230
364k
      goto leave;
2231
364k
    }
2232
20.0M
  sig->version = iobuf_get_noeof (inp);
2233
20.0M
  pktlen--;
2234
20.0M
  if (sig->version == 4 || sig->version == 5)
2235
19.9M
    is_v4or5 = 1;
2236
72.0k
  else if (sig->version != 2 && sig->version != 3)
2237
2.28k
    {
2238
2.28k
      log_error ("packet(%d) with unknown version %d\n",
2239
2.28k
     pkttype, sig->version);
2240
2.28k
      if (list_mode)
2241
2.28k
        es_fputs (":signature packet: [unknown version]\n", listfp);
2242
2.28k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2243
2.28k
      goto leave;
2244
2.28k
    }
2245
2246
20.0M
  if (!is_v4or5)
2247
69.7k
    {
2248
69.7k
      if (pktlen == 0)
2249
0
  goto underflow;
2250
69.7k
      md5_len = iobuf_get_noeof (inp);
2251
69.7k
      pktlen--;
2252
69.7k
    }
2253
20.0M
  if (pktlen == 0)
2254
0
    goto underflow;
2255
20.0M
  sig->sig_class = iobuf_get_noeof (inp);
2256
20.0M
  pktlen--;
2257
20.0M
  if (!is_v4or5)
2258
69.7k
    {
2259
69.7k
      if (pktlen < 12)
2260
0
  goto underflow;
2261
69.7k
      sig->timestamp = read_32 (inp);
2262
69.7k
      pktlen -= 4;
2263
69.7k
      sig->keyid[0] = read_32 (inp);
2264
69.7k
      pktlen -= 4;
2265
69.7k
      sig->keyid[1] = read_32 (inp);
2266
69.7k
      pktlen -= 4;
2267
69.7k
    }
2268
20.0M
  if (pktlen < 2)
2269
55
    goto underflow;
2270
20.0M
  sig->pubkey_algo = iobuf_get_noeof (inp);
2271
20.0M
  pktlen--;
2272
20.0M
  sig->digest_algo = iobuf_get_noeof (inp);
2273
20.0M
  pktlen--;
2274
20.0M
  sig->flags.exportable = 1;
2275
20.0M
  sig->flags.revocable = 1;
2276
20.0M
  if (is_v4or5) /* Read subpackets.  */
2277
19.9M
    {
2278
19.9M
      if (pktlen < 2)
2279
0
  goto underflow;
2280
19.9M
      n = read_16 (inp);
2281
19.9M
      pktlen -= 2;  /* Length of hashed data. */
2282
19.9M
      if (pktlen < n)
2283
3.61k
  goto underflow;
2284
19.9M
      if (n > 30000)
2285
138
  {
2286
138
    log_error ("signature packet: hashed data too long (%u)\n", n);
2287
138
          if (list_mode)
2288
138
            es_fprintf (listfp,
2289
0
                        ":signature packet: [hashed data too long (%u)]\n", n);
2290
138
    goto leave;
2291
138
  }
2292
19.9M
      if (n)
2293
19.9M
  {
2294
19.9M
    sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1);
2295
19.9M
    sig->hashed->size = n;
2296
19.9M
    sig->hashed->len = n;
2297
19.9M
    if (iobuf_read (inp, sig->hashed->data, n) != n)
2298
1.15k
      {
2299
1.15k
        log_error ("premature eof while reading "
2300
1.15k
       "hashed signature data\n");
2301
1.15k
              if (list_mode)
2302
1.15k
                es_fputs (":signature packet: [premature eof]\n", listfp);
2303
1.15k
        rc = -1;
2304
1.15k
        goto leave;
2305
1.15k
      }
2306
19.9M
    pktlen -= n;
2307
19.9M
  }
2308
19.9M
      if (pktlen < 2)
2309
1.02k
  goto underflow;
2310
19.9M
      n = read_16 (inp);
2311
19.9M
      pktlen -= 2;  /* Length of unhashed data.  */
2312
19.9M
      if (pktlen < n)
2313
8.99k
  goto underflow;
2314
19.9M
      if (n > 10000)
2315
156
  {
2316
156
    log_error ("signature packet: unhashed data too long (%u)\n", n);
2317
156
          if (list_mode)
2318
156
            es_fprintf (listfp,
2319
0
                        ":signature packet: [unhashed data too long (%u)]\n",
2320
0
                        n);
2321
156
    goto leave;
2322
156
  }
2323
19.9M
      if (n)
2324
19.8M
  {
2325
19.8M
    sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1);
2326
19.8M
    sig->unhashed->size = n;
2327
19.8M
    sig->unhashed->len = n;
2328
19.8M
    if (iobuf_read (inp, sig->unhashed->data, n) != n)
2329
205
      {
2330
205
        log_error ("premature eof while reading "
2331
205
       "unhashed signature data\n");
2332
205
              if (list_mode)
2333
205
                es_fputs (":signature packet: [premature eof]\n", listfp);
2334
205
        rc = -1;
2335
205
        goto leave;
2336
205
      }
2337
19.8M
    pktlen -= n;
2338
19.8M
  }
2339
19.9M
    }
2340
2341
20.0M
  if (pktlen < 2)
2342
549
    goto underflow;
2343
20.0M
  sig->digest_start[0] = iobuf_get_noeof (inp);
2344
20.0M
  pktlen--;
2345
20.0M
  sig->digest_start[1] = iobuf_get_noeof (inp);
2346
20.0M
  pktlen--;
2347
2348
20.0M
  if (is_v4or5 && sig->pubkey_algo)  /* Extract required information.  */
2349
19.9M
    {
2350
19.9M
      const byte *p;
2351
19.9M
      size_t len;
2352
2353
      /* Set sig->flags.unknown_critical if there is a critical bit
2354
       * set for packets which we do not understand.  */
2355
19.9M
      if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL)
2356
12.9M
    || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL))
2357
7.21M
  sig->flags.unknown_critical = 1;
2358
2359
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL);
2360
19.9M
      if (p)
2361
12.8M
  sig->timestamp = buf32_to_u32 (p);
2362
7.08M
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2363
7.07M
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2364
7.08M
        log_info ("signature packet without timestamp\n");
2365
2366
      /* Set the key id.  We first try the issuer fingerprint and if
2367
       * it is a v4 signature the fallback to the issuer.  Note that
2368
       * only the issuer packet is also searched in the unhashed area.  */
2369
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len);
2370
19.9M
      if (p && len == 21 && p[0] == 4)
2371
13.7M
        {
2372
13.7M
          sig->keyid[0] = buf32_to_u32 (p + 1 + 12);
2373
13.7M
    sig->keyid[1] = buf32_to_u32 (p + 1 + 16);
2374
13.7M
  }
2375
6.16M
      else if (p && len == 33 && p[0] == 5)
2376
73
        {
2377
73
          sig->keyid[0] = buf32_to_u32 (p + 1 );
2378
73
    sig->keyid[1] = buf32_to_u32 (p + 1 + 4);
2379
73
  }
2380
6.16M
      else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER)))
2381
5.51M
        {
2382
5.51M
          sig->keyid[0] = buf32_to_u32 (p);
2383
5.51M
    sig->keyid[1] = buf32_to_u32 (p + 4);
2384
5.51M
  }
2385
654k
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2386
652k
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2387
654k
  log_info ("signature packet without keyid\n");
2388
2389
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL);
2390
19.9M
      if (p && buf32_to_u32 (p))
2391
13.6k
  sig->expiredate = sig->timestamp + buf32_to_u32 (p);
2392
19.9M
      if (sig->expiredate && sig->expiredate <= make_timestamp ())
2393
12.9k
  sig->flags.expired = 1;
2394
2395
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL);
2396
19.9M
      if (p)
2397
667
  sig->flags.policy_url = 1;
2398
2399
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL);
2400
19.9M
      if (p)
2401
3.95k
  sig->flags.pref_ks = 1;
2402
2403
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len);
2404
19.9M
      if (p && len)
2405
27.3k
        {
2406
27.3k
          char *mbox;
2407
2408
27.3k
          sig->signers_uid = try_make_printable_string (p, len, 0);
2409
27.3k
          if (!sig->signers_uid)
2410
0
            {
2411
0
              rc = gpg_error_from_syserror ();
2412
0
              goto leave;
2413
0
            }
2414
27.3k
          mbox = mailbox_from_userid (sig->signers_uid, 0);
2415
27.3k
          if (mbox)
2416
11.3k
            {
2417
11.3k
              xfree (sig->signers_uid);
2418
11.3k
              sig->signers_uid = mbox;
2419
11.3k
            }
2420
27.3k
        }
2421
2422
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL);
2423
19.9M
      if (p)
2424
2
        sig->flags.key_block = 1;
2425
2426
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL);
2427
19.9M
      if (p)
2428
3.01k
  sig->flags.notation = 1;
2429
2430
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL);
2431
19.9M
      if (p && *p == 0)
2432
552
  sig->flags.revocable = 0;
2433
2434
19.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len);
2435
19.9M
      if (p && len == 2)
2436
377
  {
2437
377
    sig->trust_depth = p[0];
2438
377
    sig->trust_value = p[1];
2439
2440
    /* Only look for a regexp if there is also a trust
2441
       subpacket. */
2442
377
    sig->trust_regexp =
2443
377
      parse_sig_subpkt (sig, 1, SIGSUBPKT_REGEXP, &len);
2444
2445
    /* If the regular expression is of 0 length, there is no
2446
       regular expression. */
2447
377
    if (len == 0)
2448
52
      sig->trust_regexp = NULL;
2449
377
  }
2450
2451
      /* We accept the exportable subpacket from either the hashed or
2452
         unhashed areas as older versions of gpg put it in the
2453
         unhashed area.  In theory, anyway, we should never see this
2454
         packet off of a local keyring. */
2455
2456
19.9M
      p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE);
2457
19.9M
      if (p && *p == 0)
2458
579
  sig->flags.exportable = 0;
2459
2460
      /* Find all revocation keys.  */
2461
19.9M
      if (sig->sig_class == 0x1F)
2462
5.69M
  parse_revkeys (sig);
2463
19.9M
    }
2464
2465
20.0M
  if (list_mode)
2466
0
    {
2467
0
      es_fprintf (listfp, ":signature packet: algo %d, keyid %08lX%08lX\n"
2468
0
                  "\tversion %d, created %lu, md5len %d, sigclass 0x%02x\n"
2469
0
                  "\tdigest algo %d, begin of digest %02x %02x\n",
2470
0
                  sig->pubkey_algo,
2471
0
                  (ulong) sig->keyid[0], (ulong) sig->keyid[1],
2472
0
                  sig->version, (ulong) sig->timestamp, md5_len, sig->sig_class,
2473
0
                  sig->digest_algo, sig->digest_start[0], sig->digest_start[1]);
2474
0
      if (is_v4or5)
2475
0
  {
2476
0
    parse_sig_subpkt (sig, 1, SIGSUBPKT_LIST_HASHED, NULL);
2477
0
    parse_sig_subpkt (sig, 0, SIGSUBPKT_LIST_UNHASHED, NULL);
2478
0
  }
2479
0
    }
2480
2481
20.0M
  ndata = pubkey_get_nsig (sig->pubkey_algo);
2482
20.0M
  if (!ndata)
2483
353k
    {
2484
353k
      if (list_mode)
2485
353k
  es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo);
2486
353k
      unknown_pubkey_warning (sig->pubkey_algo);
2487
2488
      /* We store the plain material in data[0], so that we are able
2489
       * to write it back with build_packet().  */
2490
353k
      if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8))
2491
175
  {
2492
    /* We include a limit to avoid too trivial DoS attacks by
2493
       having gpg allocate too much memory.  */
2494
175
    log_error ("signature packet: too much data\n");
2495
175
    rc = GPG_ERR_INV_PACKET;
2496
175
  }
2497
353k
      else
2498
353k
  {
2499
353k
          void *tmpp;
2500
2501
353k
          tmpp = read_rest (inp, pktlen);
2502
353k
    sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2503
353k
    pktlen = 0;
2504
353k
  }
2505
353k
    }
2506
19.6M
  else
2507
19.6M
    {
2508
58.8M
      for (i = 0; i < ndata; i++)
2509
39.1M
  {
2510
39.1M
    n = pktlen;
2511
39.1M
          if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA
2512
39.1M
              || sig->pubkey_algo == PUBKEY_ALGO_EDDSA)
2513
38.9M
            sig->data[i] = sos_read (inp, &n, 0);
2514
193k
          else
2515
193k
            sig->data[i] = mpi_read (inp, &n, 0);
2516
39.1M
    pktlen -= n;
2517
39.1M
    if (list_mode)
2518
0
      {
2519
0
        es_fprintf (listfp, "\tdata: ");
2520
0
        mpi_print (listfp, sig->data[i], mpi_print_mode);
2521
0
        es_putc ('\n', listfp);
2522
0
      }
2523
39.1M
    if (!sig->data[i])
2524
2.46k
      rc = GPG_ERR_INV_PACKET;
2525
39.1M
  }
2526
19.6M
    }
2527
2528
20.3M
 leave:
2529
20.3M
  iobuf_skip_rest (inp, pktlen, 0);
2530
20.3M
  return rc;
2531
2532
14.2k
 underflow:
2533
14.2k
  log_error ("packet(%d) too short\n", pkttype);
2534
14.2k
  if (list_mode)
2535
14.2k
    es_fputs (":signature packet: [too short]\n", listfp);
2536
2537
14.2k
  iobuf_skip_rest (inp, pktlen, 0);
2538
2539
14.2k
  return GPG_ERR_INV_PACKET;
2540
20.0M
}
2541
2542
2543
static int
2544
parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen,
2545
       PKT_onepass_sig * ops)
2546
46.1k
{
2547
46.1k
  int version;
2548
46.1k
  int rc = 0;
2549
2550
46.1k
  if (pktlen < 13)
2551
11.1k
    {
2552
11.1k
      log_error ("packet(%d) too short\n", pkttype);
2553
11.1k
      if (list_mode)
2554
11.1k
        es_fputs (":onepass_sig packet: [too short]\n", listfp);
2555
11.1k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2556
11.1k
      goto leave;
2557
11.1k
    }
2558
35.0k
  version = iobuf_get_noeof (inp);
2559
35.0k
  pktlen--;
2560
35.0k
  if (version != 3)
2561
1.16k
    {
2562
1.16k
      log_error ("onepass_sig with unknown version %d\n", version);
2563
1.16k
      if (list_mode)
2564
1.16k
        es_fputs (":onepass_sig packet: [unknown version]\n", listfp);
2565
1.16k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2566
1.16k
      goto leave;
2567
1.16k
    }
2568
33.8k
  ops->sig_class = iobuf_get_noeof (inp);
2569
33.8k
  pktlen--;
2570
33.8k
  ops->digest_algo = iobuf_get_noeof (inp);
2571
33.8k
  pktlen--;
2572
33.8k
  ops->pubkey_algo = iobuf_get_noeof (inp);
2573
33.8k
  pktlen--;
2574
33.8k
  ops->keyid[0] = read_32 (inp);
2575
33.8k
  pktlen -= 4;
2576
33.8k
  ops->keyid[1] = read_32 (inp);
2577
33.8k
  pktlen -= 4;
2578
33.8k
  ops->last = iobuf_get_noeof (inp);
2579
33.8k
  pktlen--;
2580
33.8k
  if (list_mode)
2581
33.8k
    es_fprintf (listfp,
2582
0
                ":onepass_sig packet: keyid %08lX%08lX\n"
2583
0
                "\tversion %d, sigclass 0x%02x, digest %d, pubkey %d, "
2584
0
                "last=%d\n",
2585
0
                (ulong) ops->keyid[0], (ulong) ops->keyid[1],
2586
0
                version, ops->sig_class,
2587
0
                ops->digest_algo, ops->pubkey_algo, ops->last);
2588
2589
2590
46.1k
 leave:
2591
46.1k
  iobuf_skip_rest (inp, pktlen, 0);
2592
46.1k
  return rc;
2593
33.8k
}
2594
2595
2596
static int
2597
parse_key (IOBUF inp, int pkttype, unsigned long pktlen,
2598
     byte * hdr, int hdrlen, PACKET * pkt)
2599
1.06M
{
2600
1.06M
  gpg_error_t err = 0;
2601
1.06M
  int i, version, algorithm;
2602
1.06M
  unsigned long timestamp, expiredate, max_expiredate;
2603
1.06M
  int npkey, nskey;
2604
1.06M
  u32 keyid[2];
2605
1.06M
  PKT_public_key *pk;
2606
1.06M
  int is_v5;
2607
1.06M
  unsigned int pkbytes; /* For v5 keys: Number of bytes in the public
2608
                         * key material.  For v4 keys: 0.  */
2609
2610
1.06M
  (void) hdr;
2611
2612
1.06M
  pk = pkt->pkt.public_key; /* PK has been cleared. */
2613
2614
1.06M
  version = iobuf_get_noeof (inp);
2615
1.06M
  pktlen--;
2616
1.06M
  if (pkttype == PKT_PUBLIC_SUBKEY && version == '#')
2617
2.33k
    {
2618
      /* Early versions of G10 used the old PGP comments packets;
2619
       * luckily all those comments are started by a hash.  */
2620
2.33k
      if (list_mode)
2621
0
  {
2622
0
    es_fprintf (listfp, ":rfc1991 comment packet: \"");
2623
0
    for (; pktlen; pktlen--)
2624
0
      {
2625
0
        int c;
2626
0
        c = iobuf_get (inp);
2627
0
              if (c == -1)
2628
0
                break; /* Ooops: shorter than indicated.  */
2629
0
        if (c >= ' ' && c <= 'z')
2630
0
    es_putc (c, listfp);
2631
0
        else
2632
0
    es_fprintf (listfp, "\\x%02x", c);
2633
0
      }
2634
0
    es_fprintf (listfp, "\"\n");
2635
0
  }
2636
2.33k
      iobuf_skip_rest (inp, pktlen, 0);
2637
2.33k
      return 0;
2638
2.33k
    }
2639
1.06M
  else if (version == 4)
2640
1.03M
    is_v5 = 0;
2641
22.0k
  else if (version == 5)
2642
12.1k
    is_v5 = 1;
2643
9.96k
  else if (version == 2 || version == 3)
2644
5.16k
    {
2645
      /* Not anymore supported since 2.1.  Use an older gpg version
2646
       * (i.e. gpg 1.4) to parse v3 packets.  */
2647
5.16k
      if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2648
5.16k
        log_info ("packet(%d) with obsolete version %d\n", pkttype, version);
2649
5.16k
      if (list_mode)
2650
5.16k
        es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version);
2651
5.16k
      pk->version = version;
2652
5.16k
      err = gpg_error (GPG_ERR_LEGACY_KEY);
2653
5.16k
      goto leave;
2654
5.16k
    }
2655
4.79k
  else
2656
4.79k
    {
2657
4.79k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
2658
4.79k
      if (list_mode)
2659
4.79k
        es_fputs (":key packet: [unknown version]\n", listfp);
2660
4.79k
      err = gpg_error (GPG_ERR_INV_PACKET);
2661
4.79k
      goto leave;
2662
4.79k
    }
2663
2664
1.05M
  if (pktlen < (is_v5? 15:11))
2665
666
    {
2666
666
      log_error ("packet(%d) too short\n", pkttype);
2667
666
      if (list_mode)
2668
666
        es_fputs (":key packet: [too short]\n", listfp);
2669
666
      err = gpg_error (GPG_ERR_INV_PACKET);
2670
666
      goto leave;
2671
666
    }
2672
1.05M
  else if (pktlen > MAX_KEY_PACKET_LENGTH)
2673
43
    {
2674
43
      log_error ("packet(%d) too large\n", pkttype);
2675
43
      if (list_mode)
2676
43
        es_fputs (":key packet: [too large]\n", listfp);
2677
43
      err = gpg_error (GPG_ERR_INV_PACKET);
2678
43
      goto leave;
2679
43
    }
2680
2681
1.05M
  timestamp = read_32 (inp);
2682
1.05M
  pktlen -= 4;
2683
1.05M
  expiredate = 0;   /* have to get it from the selfsignature */
2684
1.05M
  max_expiredate = 0;
2685
1.05M
  algorithm = iobuf_get_noeof (inp);
2686
1.05M
  pktlen--;
2687
1.05M
  if (is_v5)
2688
11.4k
    {
2689
11.4k
      pkbytes = read_32 (inp);
2690
11.4k
      pktlen -= 4;
2691
11.4k
    }
2692
1.03M
  else
2693
1.03M
    pkbytes = 0;
2694
2695
1.05M
  if (list_mode)
2696
0
    {
2697
0
      es_fprintf (listfp, ":%s key packet:\n"
2698
0
                  "\tversion %d, algo %d, created %lu, expires %lu",
2699
0
                  pkttype == PKT_PUBLIC_KEY ? "public" :
2700
0
                  pkttype == PKT_SECRET_KEY ? "secret" :
2701
0
                  pkttype == PKT_PUBLIC_SUBKEY ? "public sub" :
2702
0
                  pkttype == PKT_SECRET_SUBKEY ? "secret sub" : "??",
2703
0
                  version, algorithm, timestamp, expiredate);
2704
0
      if (is_v5)
2705
0
        es_fprintf (listfp, ", pkbytes %u\n", pkbytes);
2706
0
      else
2707
0
        es_fprintf (listfp, "\n");
2708
0
    }
2709
2710
1.05M
  pk->timestamp = timestamp;
2711
1.05M
  pk->expiredate = expiredate;
2712
1.05M
  pk->max_expiredate = max_expiredate;
2713
1.05M
  pk->hdrbytes = hdrlen;
2714
1.05M
  pk->version = version;
2715
1.05M
  pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY);
2716
1.05M
  pk->pubkey_algo = algorithm;
2717
2718
1.05M
  nskey = pubkey_get_nskey (algorithm);
2719
1.05M
  npkey = pubkey_get_npkey (algorithm);
2720
1.05M
  if (!npkey)
2721
23.8k
    {
2722
23.8k
      if (list_mode)
2723
23.8k
  es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm);
2724
23.8k
      unknown_pubkey_warning (algorithm);
2725
23.8k
    }
2726
2727
1.05M
  if (!npkey)
2728
23.8k
    {
2729
      /* Unknown algorithm - put data into an opaque MPI.  */
2730
23.8k
      void *tmpp = read_rest (inp, pktlen);
2731
      /* Current gcry_mpi_cmp does not handle a (NULL,n>0) nicely and
2732
       * thus we avoid to create such an MPI.  */
2733
23.8k
      pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2734
23.8k
      pktlen = 0;
2735
23.8k
      goto leave;
2736
23.8k
    }
2737
1.02M
  else
2738
1.02M
    {
2739
3.98M
      for (i = 0; i < npkey; i++)
2740
2.97M
        {
2741
2.97M
          if (    (algorithm == PUBKEY_ALGO_ECDSA && (i == 0))
2742
2.96M
               || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0))
2743
2.93M
               || (algorithm == PUBKEY_ALGO_ECDH  && (i == 0 || i == 2))
2744
1.08M
               || (algorithm == PUBKEY_ALGO_KYBER && (i == 0)))
2745
1.89M
            {
2746
              /* Read the OID (i==0) or the KDF params (i==2).  */
2747
1.89M
        err = read_sized_octet_string (inp, &pktlen, pk->pkey+i);
2748
1.89M
            }
2749
1.08M
          else if (algorithm == PUBKEY_ALGO_KYBER && i == 2)
2750
151
            {
2751
              /* Read the four-octet count prefixed Kyber public key.  */
2752
151
        err = read_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i);
2753
151
            }
2754
1.08M
          else
2755
1.08M
            {
2756
              /* Read MPI or SOS.  */
2757
1.08M
              unsigned int n = pktlen;
2758
1.08M
              if (algorithm == PUBKEY_ALGO_ECDSA
2759
1.07M
                  || algorithm == PUBKEY_ALGO_EDDSA
2760
1.03M
                  || algorithm == PUBKEY_ALGO_ECDH
2761
114k
                  || algorithm == PUBKEY_ALGO_KYBER)
2762
967k
                pk->pkey[i] = sos_read (inp, &n, 0);
2763
114k
              else
2764
114k
                pk->pkey[i] = mpi_read (inp, &n, 0);
2765
1.08M
              pktlen -= n;
2766
1.08M
              if (!pk->pkey[i])
2767
13.2k
                err = gpg_error (GPG_ERR_INV_PACKET);
2768
1.08M
            }
2769
2.97M
          if (err)
2770
14.1k
            goto leave;
2771
2.97M
        }
2772
1.01M
      if (list_mode)
2773
0
        {  /* Again so that we have all parameters in pkey[] and can
2774
            * do a look forward.  We use a hack for Kyber because the
2775
            * commonly used function pubkey_string requires an extra
2776
            * buffer and, more important, its result depends on an
2777
            * configure option.  */
2778
0
          for (i = 0; i < npkey; i++)
2779
0
            {
2780
0
              es_fprintf (listfp, "\tpkey[%d]: ", i);
2781
0
              mpi_print (listfp, pk->pkey[i], mpi_print_mode);
2782
0
              if ((algorithm == PUBKEY_ALGO_ECDSA
2783
0
                   || algorithm == PUBKEY_ALGO_EDDSA
2784
0
                   || algorithm == PUBKEY_ALGO_ECDH
2785
0
                   || algorithm == PUBKEY_ALGO_KYBER) && i==0)
2786
0
                {
2787
0
                  char *curve = openpgp_oid_to_str (pk->pkey[0]);
2788
0
                  const char *name = openpgp_oid_to_curve (curve, 2);
2789
2790
0
                  if (algorithm == PUBKEY_ALGO_KYBER)
2791
0
                    es_fprintf (listfp, " ky%u_%s (%s)",
2792
0
                                nbits_from_pk (pk), name?name:"", curve);
2793
0
                  else
2794
0
                    es_fprintf (listfp, " %s (%s)", name?name:"", curve);
2795
0
                  xfree (curve);
2796
0
                }
2797
0
              es_putc ('\n', listfp);
2798
0
            }
2799
0
        }
2800
1.01M
    }
2801
1.01M
  if (list_mode)
2802
0
    keyid_from_pk (pk, keyid);
2803
2804
1.01M
  if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY)
2805
47.6k
    {
2806
47.6k
      struct seckey_info *ski;
2807
47.6k
      byte temp[16];
2808
47.6k
      size_t snlen = 0;
2809
47.6k
      unsigned int skbytes;
2810
2811
47.6k
      if (pktlen < 1)
2812
973
        {
2813
973
          err = gpg_error (GPG_ERR_INV_PACKET);
2814
973
          goto leave;
2815
973
        }
2816
2817
46.6k
      pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
2818
46.6k
      if (!pk->seckey_info)
2819
0
        {
2820
0
          err = gpg_error_from_syserror ();
2821
0
          goto leave;
2822
0
        }
2823
2824
46.6k
      ski->algo = iobuf_get_noeof (inp);
2825
46.6k
      pktlen--;
2826
2827
46.6k
      if (is_v5)
2828
5.22k
        {
2829
5.22k
          unsigned int protcount = 0;
2830
2831
          /* Read the one octet count of the following key-protection
2832
           * material.  Only required in case of unknown values. */
2833
5.22k
          if (!pktlen)
2834
279
            {
2835
279
              err = gpg_error (GPG_ERR_INV_PACKET);
2836
279
              goto leave;
2837
279
            }
2838
4.94k
          protcount = iobuf_get_noeof (inp);
2839
4.94k
          pktlen--;
2840
4.94k
          if (list_mode)
2841
4.94k
            es_fprintf (listfp, "\tprotbytes: %u\n", protcount);
2842
4.94k
        }
2843
2844
46.4k
      if (ski->algo)
2845
35.8k
  {
2846
35.8k
    ski->is_protected = 1;
2847
35.8k
    ski->s2k.count = 0;
2848
35.8k
    if (ski->algo == 253)
2849
9
      {
2850
9
              if (list_mode)
2851
9
                es_fprintf (listfp,
2852
0
                            "\tS2K pseudo algo %d is not yet supported\n",
2853
0
                            ski->algo);
2854
9
              err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2855
9
              goto leave;
2856
9
            }
2857
35.7k
          else if (ski->algo == 254 || ski->algo == 255)
2858
30.7k
      {
2859
30.7k
              if (pktlen < 3)
2860
1.59k
    {
2861
1.59k
      err = gpg_error (GPG_ERR_INV_PACKET);
2862
1.59k
      goto leave;
2863
1.59k
    }
2864
2865
29.1k
              ski->sha1chk = (ski->algo == 254);
2866
29.1k
        ski->algo = iobuf_get_noeof (inp);
2867
29.1k
        pktlen--;
2868
        /* Note that a ski->algo > 110 is illegal, but I'm not
2869
         * erroring out here as otherwise there would be no way
2870
         * to delete such a key.  */
2871
29.1k
        ski->s2k.mode = iobuf_get_noeof (inp);
2872
29.1k
        pktlen--;
2873
29.1k
        ski->s2k.hash_algo = iobuf_get_noeof (inp);
2874
29.1k
        pktlen--;
2875
        /* Check for the special GNU extension.  */
2876
29.1k
        if (ski->s2k.mode == 101)
2877
20.2k
    {
2878
100k
      for (i = 0; i < 4 && pktlen; i++, pktlen--)
2879
80.3k
        temp[i] = iobuf_get_noeof (inp);
2880
20.2k
      if (i < 4 || memcmp (temp, "GNU", 3))
2881
3.02k
        {
2882
3.02k
          if (list_mode)
2883
3.02k
      es_fprintf (listfp, "\tunknown S2K %d\n",
2884
0
                                    ski->s2k.mode);
2885
3.02k
          err = gpg_error (GPG_ERR_INV_PACKET);
2886
3.02k
          goto leave;
2887
3.02k
        }
2888
      /* Here we know that it is a GNU extension.  What
2889
       * follows is the GNU protection mode: All values
2890
       * have special meanings and they are mapped to MODE
2891
       * with a base of 1000.  */
2892
17.1k
      ski->s2k.mode = 1000 + temp[3];
2893
17.1k
    }
2894
2895
              /* Read the salt.  */
2896
26.1k
        if (ski->s2k.mode == 3 || ski->s2k.mode == 1)
2897
4.95k
    {
2898
42.7k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
2899
37.7k
        temp[i] = iobuf_get_noeof (inp);
2900
4.95k
                  if (i < 8)
2901
240
                    {
2902
240
          err = gpg_error (GPG_ERR_INV_PACKET);
2903
240
          goto leave;
2904
240
                    }
2905
4.71k
      memcpy (ski->s2k.salt, temp, 8);
2906
4.71k
    }
2907
2908
              /* Check the mode.  */
2909
25.9k
        switch (ski->s2k.mode)
2910
25.9k
    {
2911
1.22k
    case 0:
2912
1.22k
      if (list_mode)
2913
1.22k
        es_fprintf (listfp, "\tsimple S2K");
2914
1.22k
      break;
2915
810
    case 1:
2916
810
      if (list_mode)
2917
810
        es_fprintf (listfp, "\tsalted S2K");
2918
810
      break;
2919
3.90k
    case 3:
2920
3.90k
      if (list_mode)
2921
3.90k
        es_fprintf (listfp, "\titer+salt S2K");
2922
3.90k
      break;
2923
616
    case 1001:
2924
616
      if (list_mode)
2925
616
        es_fprintf (listfp, "\tgnu-dummy");
2926
616
      break;
2927
12.2k
    case 1002:
2928
12.2k
      if (list_mode)
2929
12.2k
        es_fprintf (listfp, "\tgnu-divert-to-card");
2930
12.2k
      break;
2931
744
    case 1003:
2932
744
      if (list_mode)
2933
744
        es_fprintf (listfp, "\tgnu-mode1003");
2934
744
      break;
2935
6.36k
    default:
2936
6.36k
      if (list_mode)
2937
6.36k
        es_fprintf (listfp, "\tunknown %sS2K %d\n",
2938
0
                                ski->s2k.mode < 1000 ? "" : "GNU ",
2939
0
                                ski->s2k.mode);
2940
6.36k
      err = gpg_error (GPG_ERR_INV_PACKET);
2941
6.36k
      goto leave;
2942
25.9k
    }
2943
2944
              /* Print some info.  */
2945
19.5k
        if (list_mode && ski->s2k.mode != 1003)
2946
0
    {
2947
0
      es_fprintf (listfp, ", algo: %d,%s hash: %d",
2948
0
                              ski->algo,
2949
0
                              ski->sha1chk ? " SHA1 protection,"
2950
0
                              : " simple checksum,", ski->s2k.hash_algo);
2951
0
      if (ski->s2k.mode == 1 || ski->s2k.mode == 3)
2952
0
        {
2953
0
          es_fprintf (listfp, ", salt: ");
2954
0
                      es_write_hexstring (listfp, ski->s2k.salt, 8, 0, NULL);
2955
0
        }
2956
0
                }
2957
19.5k
              if (list_mode)
2958
19.5k
                es_putc ('\n', listfp);
2959
2960
              /* Read remaining protection parameters.  */
2961
19.5k
        if (ski->s2k.mode == 3)
2962
3.90k
    {
2963
3.90k
      if (pktlen < 1)
2964
1.38k
        {
2965
1.38k
          err = gpg_error (GPG_ERR_INV_PACKET);
2966
1.38k
          goto leave;
2967
1.38k
        }
2968
2.51k
      ski->s2k.count = iobuf_get_noeof (inp);
2969
2.51k
      pktlen--;
2970
2.51k
      if (list_mode)
2971
2.51k
        es_fprintf (listfp, "\tprotect count: %lu (%lu)\n",
2972
0
                                (ulong)S2K_DECODE_COUNT ((ulong)ski->s2k.count),
2973
0
                                (ulong) ski->s2k.count);
2974
2.51k
    }
2975
15.6k
        else if (ski->s2k.mode == 1002)
2976
12.2k
    {
2977
      /* Read the serial number. */
2978
12.2k
      if (pktlen < 1)
2979
4.18k
        {
2980
4.18k
          err = gpg_error (GPG_ERR_INV_PACKET);
2981
4.18k
          goto leave;
2982
4.18k
        }
2983
8.07k
      snlen = iobuf_get (inp);
2984
8.07k
      pktlen--;
2985
8.07k
      if (pktlen < snlen || snlen == (size_t)(-1))
2986
559
        {
2987
559
          err = gpg_error (GPG_ERR_INV_PACKET);
2988
559
          goto leave;
2989
559
        }
2990
8.07k
    }
2991
19.5k
      }
2992
5.02k
    else /* Old version; no S2K, so we set mode to 0, hash MD5.  */
2993
5.02k
      {
2994
              /* Note that a ski->algo > 110 is illegal, but I'm not
2995
                 erroring on it here as otherwise there would be no
2996
                 way to delete such a key.  */
2997
5.02k
        ski->s2k.mode = 0;
2998
5.02k
        ski->s2k.hash_algo = DIGEST_ALGO_MD5;
2999
5.02k
        if (list_mode)
3000
5.02k
    es_fprintf (listfp, "\tprotect algo: %d  (hash algo: %d)\n",
3001
0
                            ski->algo, ski->s2k.hash_algo);
3002
5.02k
      }
3003
3004
    /* It is really ugly that we don't know the size
3005
     * of the IV here in cases we are not aware of the algorithm.
3006
     * so a
3007
     *   ski->ivlen = cipher_get_blocksize (ski->algo);
3008
     * won't work.  The only solution I see is to hardwire it.
3009
     * NOTE: if you change the ivlen above 16, don't forget to
3010
     * enlarge temp.
3011
           * FIXME: For v5 keys we can deduce this info!
3012
           */
3013
18.4k
    ski->ivlen = openpgp_cipher_blocklen (ski->algo);
3014
18.4k
    log_assert (ski->ivlen <= sizeof (temp));
3015
3016
18.4k
    if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003)
3017
1.36k
      ski->ivlen = 0;
3018
17.0k
    else if (ski->s2k.mode == 1002)
3019
7.51k
      ski->ivlen = snlen < 16 ? snlen : 16;
3020
3021
18.4k
    if (pktlen < ski->ivlen)
3022
1.59k
      {
3023
1.59k
              err = gpg_error (GPG_ERR_INV_PACKET);
3024
1.59k
        goto leave;
3025
1.59k
      }
3026
123k
    for (i = 0; i < ski->ivlen; i++, pktlen--)
3027
106k
      temp[i] = iobuf_get_noeof (inp);
3028
16.8k
    if (list_mode && ski->s2k.mode != 1003)
3029
0
      {
3030
0
        es_fprintf (listfp,
3031
0
                          ski->s2k.mode == 1002 ? "\tserial-number: "
3032
0
                          : "\tprotect IV: ");
3033
0
        for (i = 0; i < ski->ivlen; i++)
3034
0
    es_fprintf (listfp, " %02x", temp[i]);
3035
0
        es_putc ('\n', listfp);
3036
0
      }
3037
16.8k
    memcpy (ski->iv, temp, ski->ivlen);
3038
16.8k
  }
3039
3040
      /* Skip count of secret key material.  */
3041
27.4k
      if (is_v5)
3042
4.79k
        {
3043
4.79k
          if (pktlen < 4)
3044
4.13k
            {
3045
4.13k
              err = gpg_error (GPG_ERR_INV_PACKET);
3046
4.13k
              goto leave;
3047
4.13k
            }
3048
665
          skbytes = read_32 (inp);
3049
665
          pktlen -= 4;
3050
665
          if (list_mode)
3051
665
            es_fprintf (listfp, "\tskbytes: %u\n", skbytes);
3052
665
        }
3053
3054
      /* It does not make sense to read it into secure memory.
3055
       * If the user is so careless, not to protect his secret key,
3056
       * we can assume, that he operates an open system :=(.
3057
       * So we put the key into secure memory when we unprotect it. */
3058
23.3k
      if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002)
3059
8.13k
  {
3060
    /* Better set some dummy stuff here.  */
3061
8.13k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3062
8.13k
             xstrdup ("dummydata"),
3063
8.13k
             10 * 8);
3064
8.13k
    pktlen = 0;
3065
8.13k
  }
3066
15.2k
      else if (ski->s2k.mode == 1003)
3067
744
  {
3068
744
          void *tmpp;
3069
3070
744
    if (pktlen < 2) /* At least two bytes for parenthesis.  */
3071
235
      {
3072
235
              err = gpg_error (GPG_ERR_INV_PACKET);
3073
235
        goto leave;
3074
235
      }
3075
3076
509
          tmpp = read_rest (inp, pktlen);
3077
509
          if (list_mode)
3078
0
            {
3079
0
              if (mpi_print_mode)
3080
0
                {
3081
0
                  char *tmpsxp = canon_sexp_to_string (tmpp, pktlen);
3082
3083
0
                  es_fprintf (listfp, "\tskey[%d]: %s\n", npkey,
3084
0
                              tmpsxp? trim_trailing_spaces (tmpsxp)
3085
0
                              /*  */: "[invalid S-expression]");
3086
0
                  xfree (tmpsxp);
3087
0
                }
3088
0
              else
3089
0
                es_fprintf (listfp, "\tskey[%d]: [s-expression %lu octets]\n",
3090
0
                            npkey, pktlen);
3091
0
            }
3092
509
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3093
509
             tmpp, tmpp? pktlen * 8 : 0);
3094
509
          pktlen = 0;
3095
509
  }
3096
14.4k
      else if (ski->is_protected)
3097
7.97k
  {
3098
7.97k
          void *tmpp;
3099
3100
7.97k
    if (pktlen < 2) /* At least two bytes for the length.  */
3101
686
      {
3102
686
              err = gpg_error (GPG_ERR_INV_PACKET);
3103
686
        goto leave;
3104
686
      }
3105
3106
    /* Ugly: The length is encrypted too, so we read all stuff
3107
     * up to the end of the packet into the first SKEY
3108
     * element.
3109
           * FIXME: We can do better for v5 keys.  */
3110
3111
7.29k
          tmpp = read_rest (inp, pktlen);
3112
7.29k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3113
7.29k
             tmpp, tmpp? pktlen * 8 : 0);
3114
          /* Mark that MPI as protected - we need this information for
3115
           * importing a key.  The OPAQUE flag can't be used because
3116
           * we also store public EdDSA values in opaque MPIs.  */
3117
7.29k
          if (pk->pkey[npkey])
3118
7.29k
            gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1);
3119
7.29k
    pktlen = 0;
3120
7.29k
    if (list_mode)
3121
7.29k
            es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey);
3122
7.29k
  }
3123
6.48k
      else
3124
6.48k
  {
3125
          /* Not encrypted.  */
3126
14.9k
    for (i = npkey; i < nskey; i++)
3127
9.01k
      {
3128
3129
9.01k
              if (pktlen < 2) /* At least two bytes for the length.  */
3130
528
                {
3131
528
                  err = gpg_error (GPG_ERR_INV_PACKET);
3132
528
                  goto leave;
3133
528
                }
3134
8.48k
              if (algorithm == PUBKEY_ALGO_KYBER && i == npkey+1)
3135
0
                {
3136
0
                  err = read_octet_string (inp, &pktlen, 4, 0, 1, pk->pkey+i);
3137
0
                  if (err)
3138
0
                    goto leave;
3139
0
                }
3140
8.48k
              else
3141
8.48k
                {
3142
8.48k
                  unsigned int n = pktlen;
3143
3144
8.48k
                  if (algorithm == PUBKEY_ALGO_ECDSA
3145
6.74k
                      || algorithm == PUBKEY_ALGO_EDDSA
3146
5.67k
                      || algorithm == PUBKEY_ALGO_ECDH
3147
5.36k
                      || algorithm == PUBKEY_ALGO_KYBER)
3148
3.12k
                    pk->pkey[i] = sos_read (inp, &n, 0);
3149
5.36k
                  else
3150
5.36k
                    pk->pkey[i] = mpi_read (inp, &n, 0);
3151
8.48k
                  pktlen -= n;
3152
8.48k
                }
3153
3154
8.48k
              if (list_mode)
3155
0
                {
3156
0
                  es_fprintf (listfp, "\tskey[%d]: ", i);
3157
0
                  mpi_print (listfp, pk->pkey[i], mpi_print_mode);
3158
0
                  es_putc ('\n', listfp);
3159
0
                }
3160
3161
8.48k
        if (!pk->pkey[i])
3162
3.14k
    err = gpg_error (GPG_ERR_INV_PACKET);
3163
8.48k
      }
3164
5.95k
    if (err)
3165
2.25k
      goto leave;
3166
3167
3.69k
    if (pktlen < 2)
3168
100
      {
3169
100
              err = gpg_error (GPG_ERR_INV_PACKET);
3170
100
        goto leave;
3171
100
      }
3172
3.59k
    ski->csum = read_16 (inp);
3173
3.59k
    pktlen -= 2;
3174
3.59k
    if (list_mode)
3175
3.59k
            es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum);
3176
3.59k
  }
3177
23.3k
    }
3178
3179
  /* Note that KEYID below has been initialized above in list_mode.  */
3180
984k
  if (list_mode)
3181
984k
    es_fprintf (listfp, "\tkeyid: %08lX%08lX\n",
3182
0
                (ulong) keyid[0], (ulong) keyid[1]);
3183
3184
1.06M
 leave:
3185
1.06M
  iobuf_skip_rest (inp, pktlen, 0);
3186
1.06M
  return err;
3187
984k
}
3188
3189
3190
/* Attribute subpackets have the same format as v4 signature
3191
   subpackets.  This is not part of OpenPGP, but is done in several
3192
   versions of PGP nevertheless.  */
3193
int
3194
parse_attribute_subpkts (PKT_user_id * uid)
3195
16.6k
{
3196
16.6k
  size_t n;
3197
16.6k
  int count = 0;
3198
16.6k
  struct user_attribute *attribs = NULL;
3199
16.6k
  const byte *buffer = uid->attrib_data;
3200
16.6k
  int buflen = uid->attrib_len;
3201
16.6k
  byte type;
3202
3203
16.6k
  xfree (uid->attribs);
3204
3205
23.0k
  while (buflen)
3206
11.1k
    {
3207
11.1k
      n = *buffer++;
3208
11.1k
      buflen--;
3209
11.1k
      if (n == 255)  /* 4 byte length header.  */
3210
2.05k
  {
3211
2.05k
    if (buflen < 4)
3212
1.23k
      goto too_short;
3213
816
    n = buf32_to_size_t (buffer);
3214
816
    buffer += 4;
3215
816
    buflen -= 4;
3216
816
  }
3217
9.14k
      else if (n >= 192)  /* 2 byte special encoded length header.  */
3218
1.46k
  {
3219
1.46k
    if (buflen < 2)
3220
770
      goto too_short;
3221
695
    n = ((n - 192) << 8) + *buffer + 192;
3222
695
    buffer++;
3223
695
    buflen--;
3224
695
  }
3225
9.18k
      if (buflen < n)
3226
1.68k
  goto too_short;
3227
3228
7.50k
      if (!n)
3229
1.07k
        {
3230
          /* Too short to encode the subpacket type.  */
3231
1.07k
          if (opt.verbose)
3232
1.07k
            log_info ("attribute subpacket too short\n");
3233
1.07k
          break;
3234
1.07k
        }
3235
3236
6.43k
      attribs = xrealloc (attribs,
3237
6.43k
                          (count + 1) * sizeof (struct user_attribute));
3238
6.43k
      memset (&attribs[count], 0, sizeof (struct user_attribute));
3239
3240
6.43k
      type = *buffer;
3241
6.43k
      buffer++;
3242
6.43k
      buflen--;
3243
6.43k
      n--;
3244
3245
6.43k
      attribs[count].type = type;
3246
6.43k
      attribs[count].data = buffer;
3247
6.43k
      attribs[count].len = n;
3248
6.43k
      buffer += n;
3249
6.43k
      buflen -= n;
3250
6.43k
      count++;
3251
6.43k
    }
3252
3253
12.9k
  uid->attribs = attribs;
3254
12.9k
  uid->numattribs = count;
3255
12.9k
  return count;
3256
3257
3.68k
 too_short:
3258
3.68k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
3259
3.68k
    log_info ("buffer shorter than attribute subpacket\n");
3260
3.68k
  uid->attribs = attribs;
3261
3.68k
  uid->numattribs = count;
3262
3.68k
  return count;
3263
16.6k
}
3264
3265
3266
static int
3267
parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3268
50.6k
{
3269
50.6k
  byte *p;
3270
3271
  /* Cap the size of a user ID at 2k: a value absurdly large enough
3272
     that there is no sane user ID string (which is printable text
3273
     as of RFC2440bis) that won't fit in it, but yet small enough to
3274
     avoid allocation problems.  A large pktlen may not be
3275
     allocatable, and a very large pktlen could actually cause our
3276
     allocation to wrap around in xmalloc to a small number. */
3277
3278
50.6k
  if (pktlen > MAX_UID_PACKET_LENGTH)
3279
647
    {
3280
647
      log_error ("packet(%d) too large\n", pkttype);
3281
647
      if (list_mode)
3282
647
        es_fprintf (listfp, ":user ID packet: [too large]\n");
3283
647
      iobuf_skip_rest (inp, pktlen, 0);
3284
647
      return GPG_ERR_INV_PACKET;
3285
647
    }
3286
3287
49.9k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen);
3288
49.9k
  packet->pkt.user_id->len = pktlen;
3289
49.9k
  packet->pkt.user_id->ref = 1;
3290
3291
49.9k
  p = packet->pkt.user_id->name;
3292
799k
  for (; pktlen; pktlen--, p++)
3293
749k
    *p = iobuf_get_noeof (inp);
3294
49.9k
  *p = 0;
3295
3296
49.9k
  if (list_mode)
3297
0
    {
3298
0
      int n = packet->pkt.user_id->len;
3299
0
      es_fprintf (listfp, ":user ID packet: \"");
3300
      /* fixme: Hey why don't we replace this with es_write_sanitized?? */
3301
0
      for (p = packet->pkt.user_id->name; n; p++, n--)
3302
0
  {
3303
0
    if (*p >= ' ' && *p <= 'z')
3304
0
      es_putc (*p, listfp);
3305
0
    else
3306
0
      es_fprintf (listfp, "\\x%02x", *p);
3307
0
  }
3308
0
      es_fprintf (listfp, "\"\n");
3309
0
    }
3310
49.9k
  return 0;
3311
50.6k
}
3312
3313
3314
void
3315
make_attribute_uidname (PKT_user_id * uid, size_t max_namelen)
3316
16.6k
{
3317
16.6k
  log_assert (max_namelen > 70);
3318
16.6k
  if (uid->numattribs <= 0)
3319
11.8k
    sprintf (uid->name, "[bad attribute packet of size %lu]",
3320
11.8k
       uid->attrib_len);
3321
4.80k
  else if (uid->numattribs > 1)
3322
457
    sprintf (uid->name, "[%d attributes of size %lu]",
3323
457
       uid->numattribs, uid->attrib_len);
3324
4.35k
  else
3325
4.35k
    {
3326
      /* Only one attribute, so list it as the "user id" */
3327
3328
4.35k
      if (uid->attribs->type == ATTRIB_IMAGE)
3329
3.30k
  {
3330
3.30k
    u32 len;
3331
3.30k
    byte type;
3332
3333
3.30k
    if (parse_image_header (uid->attribs, &type, &len))
3334
2.31k
      sprintf (uid->name, "[%.20s image of size %lu]",
3335
2.31k
         image_type_to_string (type, 1), (ulong) len);
3336
993
    else
3337
993
      sprintf (uid->name, "[invalid image]");
3338
3.30k
  }
3339
1.04k
      else
3340
1.04k
  sprintf (uid->name, "[unknown attribute of size %lu]",
3341
1.04k
     (ulong) uid->attribs->len);
3342
4.35k
    }
3343
3344
16.6k
  uid->len = strlen (uid->name);
3345
16.6k
}
3346
3347
3348
static int
3349
parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen,
3350
     PACKET * packet)
3351
16.6k
{
3352
16.6k
  byte *p;
3353
3354
16.6k
  (void) pkttype;
3355
3356
  /* We better cap the size of an attribute packet to make DoS not too
3357
     easy.  16MB should be more then enough for one attribute packet
3358
     (ie. a photo).  */
3359
16.6k
  if (pktlen > MAX_ATTR_PACKET_LENGTH)
3360
36
    {
3361
36
      log_error ("packet(%d) too large\n", pkttype);
3362
36
      if (list_mode)
3363
36
        es_fprintf (listfp, ":attribute packet: [too large]\n");
3364
36
      iobuf_skip_rest (inp, pktlen, 0);
3365
36
      return GPG_ERR_INV_PACKET;
3366
36
    }
3367
3368
16.6k
#define EXTRA_UID_NAME_SPACE 71
3369
16.6k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id
3370
16.6k
               + EXTRA_UID_NAME_SPACE);
3371
16.6k
  packet->pkt.user_id->ref = 1;
3372
16.6k
  packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1);
3373
16.6k
  packet->pkt.user_id->attrib_len = pktlen;
3374
3375
16.6k
  p = packet->pkt.user_id->attrib_data;
3376
2.66G
  for (; pktlen; pktlen--, p++)
3377
2.66G
    *p = iobuf_get_noeof (inp);
3378
3379
  /* Now parse out the individual attribute subpackets.  This is
3380
     somewhat pointless since there is only one currently defined
3381
     attribute type (jpeg), but it is correct by the spec. */
3382
16.6k
  parse_attribute_subpkts (packet->pkt.user_id);
3383
3384
16.6k
  make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE);
3385
3386
16.6k
  if (list_mode)
3387
0
    {
3388
0
      es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name);
3389
0
    }
3390
16.6k
  return 0;
3391
16.6k
}
3392
3393
3394
static int
3395
parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3396
9.47k
{
3397
9.47k
  byte *p;
3398
3399
  /* Cap comment packet at a reasonable value to avoid an integer
3400
     overflow in the malloc below.  Comment packets are actually not
3401
     anymore define my OpenPGP and we even stopped to use our
3402
     private comment packet.  */
3403
9.47k
  if (pktlen > MAX_COMMENT_PACKET_LENGTH)
3404
360
    {
3405
360
      log_error ("packet(%d) too large\n", pkttype);
3406
360
      if (list_mode)
3407
360
        es_fprintf (listfp, ":%scomment packet: [too large]\n",
3408
0
                    pkttype == PKT_OLD_COMMENT ? "OpenPGP draft " : "");
3409
360
      iobuf_skip_rest (inp, pktlen, 0);
3410
360
      return GPG_ERR_INV_PACKET;
3411
360
    }
3412
9.11k
  packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
3413
9.11k
  packet->pkt.comment->len = pktlen;
3414
9.11k
  p = packet->pkt.comment->data;
3415
2.24M
  for (; pktlen; pktlen--, p++)
3416
2.23M
    *p = iobuf_get_noeof (inp);
3417
3418
9.11k
  if (list_mode)
3419
0
    {
3420
0
      int n = packet->pkt.comment->len;
3421
0
      es_fprintf (listfp, ":%scomment packet: \"", pkttype == PKT_OLD_COMMENT ?
3422
0
                  "OpenPGP draft " : "");
3423
0
      for (p = packet->pkt.comment->data; n; p++, n--)
3424
0
  {
3425
0
    if (*p >= ' ' && *p <= 'z')
3426
0
      es_putc (*p, listfp);
3427
0
    else
3428
0
      es_fprintf (listfp, "\\x%02x", *p);
3429
0
  }
3430
0
      es_fprintf (listfp, "\"\n");
3431
0
    }
3432
9.11k
  return 0;
3433
9.47k
}
3434
3435
3436
/* Parse a ring trust packet RFC4880 (5.10).
3437
 *
3438
 * This parser is special in that the packet is not stored as a packet
3439
 * but its content is merged into the previous packet.  */
3440
static gpg_error_t
3441
parse_ring_trust (parse_packet_ctx_t ctx, unsigned long pktlen)
3442
20.0M
{
3443
20.0M
  gpg_error_t err;
3444
20.0M
  iobuf_t inp = ctx->inp;
3445
20.0M
  PKT_ring_trust rt = {0};
3446
20.0M
  int c;
3447
20.0M
  int not_gpg = 0;
3448
3449
20.0M
  if (!pktlen)
3450
5.65k
    {
3451
5.65k
      if (list_mode)
3452
5.65k
  es_fprintf (listfp, ":trust packet: empty\n");
3453
5.65k
      err = 0;
3454
5.65k
      goto leave;
3455
5.65k
    }
3456
3457
20.0M
  c = iobuf_get_noeof (inp);
3458
20.0M
  pktlen--;
3459
20.0M
  rt.trustval = c;
3460
20.0M
  if (pktlen)
3461
20.0M
    {
3462
20.0M
      if (!c)
3463
19.9M
        {
3464
19.9M
          c = iobuf_get_noeof (inp);
3465
          /* We require that bit 7 of the sigcache is 0 (easier
3466
           * eof handling).  */
3467
19.9M
          if (!(c & 0x80))
3468
19.9M
            rt.sigcache = c;
3469
19.9M
        }
3470
62.2k
      else
3471
62.2k
        iobuf_get_noeof (inp);  /* Dummy read.  */
3472
20.0M
      pktlen--;
3473
20.0M
    }
3474
3475
  /* Next is the optional subtype.  */
3476
20.0M
  if (pktlen > 3)
3477
20.0M
    {
3478
20.0M
      char tmp[4];
3479
20.0M
      tmp[0] = iobuf_get_noeof (inp);
3480
20.0M
      tmp[1] = iobuf_get_noeof (inp);
3481
20.0M
      tmp[2] = iobuf_get_noeof (inp);
3482
20.0M
      tmp[3] = iobuf_get_noeof (inp);
3483
20.0M
      pktlen -= 4;
3484
20.0M
      if (!memcmp (tmp, "gpg", 3))
3485
19.9M
        rt.subtype = tmp[3];
3486
38.8k
      else
3487
38.8k
        not_gpg = 1;
3488
20.0M
    }
3489
  /* If it is a key or uid subtype read the remaining data.  */
3490
20.0M
  if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID)
3491
46.4k
      && pktlen >= 6 )
3492
45.6k
    {
3493
45.6k
      int i;
3494
45.6k
      unsigned int namelen;
3495
3496
45.6k
      rt.keyorg = iobuf_get_noeof (inp);
3497
45.6k
      pktlen--;
3498
45.6k
      rt.keyupdate = read_32 (inp);
3499
45.6k
      pktlen -= 4;
3500
45.6k
      namelen = iobuf_get_noeof (inp);
3501
45.6k
      pktlen--;
3502
45.6k
      if (namelen && pktlen)
3503
1.82k
        {
3504
1.82k
          rt.url = xtrymalloc (namelen + 1);
3505
1.82k
          if (!rt.url)
3506
0
            {
3507
0
              err = gpg_error_from_syserror ();
3508
0
              goto leave;
3509
0
            }
3510
36.7k
          for (i = 0; pktlen && i < namelen; pktlen--, i++)
3511
34.9k
            rt.url[i] = iobuf_get_noeof (inp);
3512
1.82k
          rt.url[i] = 0;
3513
1.82k
        }
3514
45.6k
    }
3515
3516
20.0M
  if (list_mode)
3517
0
    {
3518
0
      if (rt.subtype == RING_TRUST_SIG)
3519
0
        es_fprintf (listfp, ":trust packet: sig flag=%02x sigcache=%02x\n",
3520
0
                    rt.trustval, rt.sigcache);
3521
0
      else if (rt.subtype == RING_TRUST_UID || rt.subtype == RING_TRUST_KEY)
3522
0
        {
3523
0
          unsigned char *p;
3524
3525
0
          es_fprintf (listfp, ":trust packet: %s upd=%lu src=%d%s",
3526
0
                      (rt.subtype == RING_TRUST_UID? "uid" : "key"),
3527
0
                      (unsigned long)rt.keyupdate,
3528
0
                      rt.keyorg,
3529
0
                      (rt.url? " url=":""));
3530
0
          if (rt.url)
3531
0
            {
3532
0
              for (p = rt.url; *p; p++)
3533
0
                {
3534
0
                  if (*p >= ' ' && *p <= 'z')
3535
0
                    es_putc (*p, listfp);
3536
0
                  else
3537
0
                    es_fprintf (listfp, "\\x%02x", *p);
3538
0
                }
3539
0
            }
3540
0
          es_putc ('\n', listfp);
3541
0
        }
3542
0
      else if (not_gpg)
3543
0
        es_fprintf (listfp, ":trust packet: not created by gpg\n");
3544
0
      else
3545
0
        es_fprintf (listfp, ":trust packet: subtype=%02x\n",
3546
0
                    rt.subtype);
3547
0
    }
3548
3549
  /* Now transfer the data to the respective packet.  Do not do this
3550
   * if SKIP_META is set.  */
3551
20.0M
  if (!ctx->last_pkt.pkt.generic || ctx->skip_meta)
3552
59.7k
    ;
3553
19.9M
  else if (rt.subtype == RING_TRUST_SIG
3554
19.9M
           && ctx->last_pkt.pkttype == PKT_SIGNATURE)
3555
19.9M
    {
3556
19.9M
      PKT_signature *sig = ctx->last_pkt.pkt.signature;
3557
3558
19.9M
      if ((rt.sigcache & 1))
3559
12.4M
        {
3560
12.4M
          sig->flags.checked = 1;
3561
12.4M
          sig->flags.valid = !!(rt.sigcache & 2);
3562
12.4M
        }
3563
19.9M
    }
3564
43.3k
  else if (rt.subtype == RING_TRUST_UID
3565
26.2k
           && (ctx->last_pkt.pkttype == PKT_USER_ID
3566
2.27k
               || ctx->last_pkt.pkttype == PKT_ATTRIBUTE))
3567
24.0k
    {
3568
24.0k
      PKT_user_id *uid = ctx->last_pkt.pkt.user_id;
3569
3570
24.0k
      uid->keyorg = rt.keyorg;
3571
24.0k
      uid->keyupdate = rt.keyupdate;
3572
24.0k
      uid->updateurl = rt.url;
3573
24.0k
      rt.url = NULL;
3574
24.0k
    }
3575
19.3k
  else if (rt.subtype == RING_TRUST_KEY
3576
16.3k
           && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY
3577
4.04k
               || ctx->last_pkt.pkttype == PKT_SECRET_KEY))
3578
12.3k
    {
3579
12.3k
      PKT_public_key *pk = ctx->last_pkt.pkt.public_key;
3580
3581
12.3k
      pk->keyorg = rt.keyorg;
3582
12.3k
      pk->keyupdate = rt.keyupdate;
3583
12.3k
      pk->updateurl = rt.url;
3584
12.3k
      rt.url = NULL;
3585
12.3k
    }
3586
3587
20.0M
  err = 0;
3588
3589
20.0M
 leave:
3590
20.0M
  xfree (rt.url);
3591
20.0M
  free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL.  */
3592
20.0M
  iobuf_skip_rest (inp, pktlen, 0);
3593
20.0M
  return err;
3594
20.0M
}
3595
3596
3597
static int
3598
parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen,
3599
     PACKET * pkt, int new_ctb, int partial)
3600
136k
{
3601
136k
  int rc = 0;
3602
136k
  int mode, namelen;
3603
136k
  PKT_plaintext *pt;
3604
136k
  byte *p;
3605
136k
  int c, i;
3606
3607
136k
  if (!partial && pktlen < 6)
3608
5.11k
    {
3609
5.11k
      log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen);
3610
5.11k
      if (list_mode)
3611
5.11k
        es_fputs (":literal data packet: [too short]\n", listfp);
3612
5.11k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3613
5.11k
      goto leave;
3614
5.11k
    }
3615
130k
  mode = iobuf_get_noeof (inp);
3616
130k
  if (pktlen)
3617
97.8k
    pktlen--;
3618
130k
  namelen = iobuf_get_noeof (inp);
3619
130k
  if (pktlen)
3620
97.8k
    pktlen--;
3621
  /* Note that namelen will never exceed 255 bytes. */
3622
130k
  pt = pkt->pkt.plaintext =
3623
130k
    xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1);
3624
130k
  pt->new_ctb = new_ctb;
3625
130k
  pt->mode = mode;
3626
130k
  pt->namelen = namelen;
3627
130k
  pt->is_partial = partial;
3628
130k
  if (pktlen)
3629
97.8k
    {
3630
503k
      for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++)
3631
405k
  pt->name[i] = iobuf_get_noeof (inp);
3632
97.8k
    }
3633
33.0k
  else
3634
33.0k
    {
3635
55.1k
      for (i = 0; i < namelen; i++)
3636
22.9k
  if ((c = iobuf_get (inp)) == -1)
3637
840
    break;
3638
22.1k
  else
3639
22.1k
    pt->name[i] = c;
3640
33.0k
    }
3641
  /* Fill up NAME so that a check with valgrind won't complain about
3642
   * reading from uninitialized memory.  This case may be triggred by
3643
   * corrupted packets.  */
3644
3.86M
  for (; i < namelen; i++)
3645
3.73M
    pt->name[i] = 0;
3646
3647
130k
  pt->timestamp = read_32 (inp);
3648
130k
  if (pktlen)
3649
97.8k
    pktlen -= 4;
3650
130k
  pt->len = pktlen;
3651
130k
  pt->buf = inp;
3652
3653
130k
  if (list_mode)
3654
0
    {
3655
0
      es_fprintf (listfp, ":literal data packet:\n"
3656
0
                  "\tmode %c (%X), created %lu, name=\"",
3657
0
                  mode >= ' ' && mode < 'z' ? mode : '?', mode,
3658
0
                  (ulong) pt->timestamp);
3659
0
      for (p = pt->name, i = 0; i < namelen; p++, i++)
3660
0
  {
3661
0
    if (*p >= ' ' && *p <= 'z')
3662
0
      es_putc (*p, listfp);
3663
0
    else
3664
0
      es_fprintf (listfp, "\\x%02x", *p);
3665
0
  }
3666
0
      es_fprintf (listfp, "\",\n\traw data: ");
3667
0
      if (partial)
3668
0
  es_fprintf (listfp, "unknown length\n");
3669
0
      else
3670
0
  es_fprintf (listfp, "%lu bytes\n", (ulong) pt->len);
3671
0
    }
3672
3673
136k
 leave:
3674
136k
  return rc;
3675
130k
}
3676
3677
3678
static int
3679
parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen,
3680
      PACKET * pkt, int new_ctb)
3681
376k
{
3682
376k
  PKT_compressed *zd;
3683
3684
  /* PKTLEN is here 0, but data follows (this should be the last
3685
     object in a file or the compress algorithm should know the
3686
     length).  */
3687
376k
  (void) pkttype;
3688
376k
  (void) pktlen;
3689
3690
376k
  zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed);
3691
376k
  zd->algorithm = iobuf_get_noeof (inp);
3692
376k
  zd->len = 0;      /* not used */
3693
376k
  zd->new_ctb = new_ctb;
3694
376k
  zd->buf = inp;
3695
376k
  if (list_mode)
3696
376k
    es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
3697
376k
  return 0;
3698
376k
}
3699
3700
3701
static int
3702
parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen,
3703
     PACKET * pkt, int new_ctb, int partial)
3704
111k
{
3705
111k
  int rc = 0;
3706
111k
  PKT_encrypted *ed;
3707
111k
  unsigned long orig_pktlen = pktlen;
3708
3709
111k
  ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted);
3710
  /* ed->len is set below.  */
3711
111k
  ed->extralen = 0;  /* Unknown here; only used in build_packet.  */
3712
111k
  ed->buf = NULL;
3713
111k
  ed->new_ctb = new_ctb;
3714
111k
  ed->is_partial = partial;
3715
111k
  ed->aead_algo = 0;
3716
111k
  ed->cipher_algo = 0; /* Only used with AEAD.  */
3717
111k
  ed->chunkbyte = 0;   /* Only used with AEAD.  */
3718
111k
  if (pkttype == PKT_ENCRYPTED_MDC)
3719
6.20k
    {
3720
      /* Fixme: add some pktlen sanity checks.  */
3721
6.20k
      int version;
3722
3723
6.20k
      version = iobuf_get_noeof (inp);
3724
6.20k
      if (orig_pktlen)
3725
513
  pktlen--;
3726
6.20k
      if (version != 1)
3727
1.60k
  {
3728
1.60k
    log_error ("encrypted_mdc packet with unknown version %d\n",
3729
1.60k
         version);
3730
1.60k
          if (list_mode)
3731
1.60k
            es_fputs (":encrypted data packet: [unknown version]\n", listfp);
3732
    /*skip_rest(inp, pktlen); should we really do this? */
3733
1.60k
    rc = gpg_error (GPG_ERR_INV_PACKET);
3734
1.60k
    goto leave;
3735
1.60k
  }
3736
4.60k
      ed->mdc_method = DIGEST_ALGO_SHA1;
3737
4.60k
    }
3738
104k
  else
3739
104k
    ed->mdc_method = 0;
3740
3741
  /* A basic sanity check.  We need at least an 8 byte IV plus the 2
3742
     detection bytes.  Note that we don't known the algorithm and thus
3743
     we may only check against the minimum blocksize.  */
3744
109k
  if (orig_pktlen && pktlen < 10)
3745
451
    {
3746
      /* Actually this is blocksize+2.  */
3747
451
      log_error ("packet(%d) too short\n", pkttype);
3748
451
      if (list_mode)
3749
451
        es_fputs (":encrypted data packet: [too short]\n", listfp);
3750
451
      rc = GPG_ERR_INV_PACKET;
3751
451
      iobuf_skip_rest (inp, pktlen, partial);
3752
451
      goto leave;
3753
451
    }
3754
3755
  /* Store the remaining length of the encrypted data (i.e. without
3756
     the MDC version number but with the IV etc.).  This value is
3757
     required during decryption.  */
3758
109k
  ed->len = pktlen;
3759
3760
109k
  if (list_mode)
3761
0
    {
3762
0
      if (orig_pktlen)
3763
0
  es_fprintf (listfp, ":encrypted data packet:\n\tlength: %lu\n",
3764
0
                    orig_pktlen);
3765
0
      else
3766
0
  es_fprintf (listfp, ":encrypted data packet:\n\tlength: unknown\n");
3767
0
      if (ed->mdc_method)
3768
0
  es_fprintf (listfp, "\tmdc_method: %d\n", ed->mdc_method);
3769
0
    }
3770
3771
109k
  ed->buf = inp;
3772
3773
111k
 leave:
3774
111k
  return rc;
3775
109k
}
3776
3777
3778
/* Note, that this code is not anymore used in real life because the
3779
   MDC checking is now done right after the decryption in
3780
   decrypt_data.  */
3781
static int
3782
parse_mdc (IOBUF inp, int pkttype, unsigned long pktlen,
3783
     PACKET * pkt, int new_ctb)
3784
10.0k
{
3785
10.0k
  int rc = 0;
3786
10.0k
  PKT_mdc *mdc;
3787
10.0k
  byte *p;
3788
3789
10.0k
  (void) pkttype;
3790
3791
10.0k
  mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc);
3792
10.0k
  if (list_mode)
3793
10.0k
    es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
3794
10.0k
  if (!new_ctb || pktlen != 20)
3795
3.74k
    {
3796
3.74k
      log_error ("mdc_packet with invalid encoding\n");
3797
3.74k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3798
3.74k
      goto leave;
3799
3.74k
    }
3800
6.30k
  p = mdc->hash;
3801
132k
  for (; pktlen; pktlen--, p++)
3802
126k
    *p = iobuf_get_noeof (inp);
3803
3804
10.0k
 leave:
3805
10.0k
  return rc;
3806
6.30k
}
3807
3808
3809
static gpg_error_t
3810
parse_encrypted_aead (iobuf_t inp, int pkttype, unsigned long pktlen,
3811
                      PACKET *pkt, int partial)
3812
20.0k
{
3813
20.0k
  int rc = 0;
3814
20.0k
  PKT_encrypted *ed;
3815
20.0k
  unsigned long orig_pktlen = pktlen;
3816
20.0k
  int version;
3817
3818
20.0k
  ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted);
3819
20.0k
  if (!ed)
3820
0
    return gpg_error_from_syserror ();
3821
20.0k
  ed->len = 0;
3822
20.0k
  ed->extralen = 0;  /* (only used in build_packet.)  */
3823
20.0k
  ed->buf = NULL;
3824
20.0k
  ed->new_ctb = 1;   /* (packet number requires a new CTB anyway.)  */
3825
20.0k
  ed->is_partial = partial;
3826
20.0k
  ed->mdc_method = 0;
3827
  /* A basic sanity check.  We need one version byte, one algo byte,
3828
   * one aead algo byte, one chunkbyte, at least 15 byte IV.  */
3829
20.0k
  if (orig_pktlen && pktlen < 19)
3830
449
    {
3831
449
      log_error ("packet(%d) too short\n", pkttype);
3832
449
      if (list_mode)
3833
449
        es_fputs (":aead encrypted packet: [too short]\n", listfp);
3834
449
      rc = gpg_error (GPG_ERR_INV_PACKET);
3835
449
      iobuf_skip_rest (inp, pktlen, partial);
3836
449
      goto leave;
3837
449
    }
3838
3839
19.5k
  version = iobuf_get_noeof (inp);
3840
19.5k
  if (orig_pktlen)
3841
1.18k
    pktlen--;
3842
19.5k
  if (version != 1)
3843
1.62k
    {
3844
1.62k
      log_error ("aead encrypted packet with unknown version %d\n",
3845
1.62k
                 version);
3846
1.62k
      if (list_mode)
3847
1.62k
        es_fputs (":aead encrypted packet: [unknown version]\n", listfp);
3848
      /*skip_rest(inp, pktlen); should we really do this? */
3849
1.62k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3850
1.62k
      goto leave;
3851
1.62k
    }
3852
3853
17.9k
  ed->cipher_algo = iobuf_get_noeof (inp);
3854
17.9k
  if (orig_pktlen)
3855
187
    pktlen--;
3856
17.9k
  ed->aead_algo = iobuf_get_noeof (inp);
3857
17.9k
  if (orig_pktlen)
3858
187
    pktlen--;
3859
17.9k
  ed->chunkbyte = iobuf_get_noeof (inp);
3860
17.9k
  if (orig_pktlen)
3861
187
    pktlen--;
3862
3863
  /* Store the remaining length of the encrypted data.  We read the
3864
   * rest during decryption.  */
3865
17.9k
  ed->len = pktlen;
3866
3867
17.9k
  if (list_mode)
3868
0
    {
3869
0
      es_fprintf (listfp, ":aead encrypted packet: cipher=%u aead=%u cb=%u\n",
3870
0
                  ed->cipher_algo, ed->aead_algo, ed->chunkbyte);
3871
0
      if (orig_pktlen)
3872
0
  es_fprintf (listfp, "\tlength: %lu\n", orig_pktlen);
3873
0
      else
3874
0
  es_fprintf (listfp, "\tlength: unknown\n");
3875
0
    }
3876
3877
17.9k
  ed->buf = inp;
3878
3879
20.0k
 leave:
3880
20.0k
  return rc;
3881
17.9k
}
3882
3883
3884
/*
3885
 * This packet is internally generated by us (in armor.c) to transfer
3886
 * some information to the lower layer.  To make sure that this packet
3887
 * is really a GPG faked one and not one coming from outside, we
3888
 * first check that there is a unique tag in it.
3889
 *
3890
 * The format of such a control packet is:
3891
 *   n byte  session marker
3892
 *   1 byte  control type CTRLPKT_xxxxx
3893
 *   m byte  control data
3894
 */
3895
static int
3896
parse_gpg_control (IOBUF inp, int pkttype, unsigned long pktlen,
3897
       PACKET * packet, int partial)
3898
40.1k
{
3899
40.1k
  byte *p;
3900
40.1k
  const byte *sesmark;
3901
40.1k
  size_t sesmarklen;
3902
40.1k
  int i;
3903
3904
40.1k
  (void) pkttype;
3905
3906
40.1k
  if (list_mode)
3907
40.1k
    es_fprintf (listfp, ":packet 63: length %lu ", pktlen);
3908
3909
40.1k
  sesmark = get_session_marker (&sesmarklen);
3910
40.1k
  if (pktlen < sesmarklen + 1)  /* 1 is for the control bytes */
3911
4.49k
    goto skipit;
3912
533k
  for (i = 0; i < sesmarklen; i++, pktlen--)
3913
502k
    {
3914
502k
      if (sesmark[i] != iobuf_get_noeof (inp))
3915
4.53k
  goto skipit;
3916
502k
    }
3917
31.1k
  if (pktlen > 4096)
3918
3
    goto skipit;  /* Definitely too large.  We skip it to avoid an
3919
                     overflow in the malloc.  */
3920
31.1k
  if (list_mode)
3921
31.1k
    es_fputs ("- gpg control packet", listfp);
3922
3923
31.1k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
3924
31.1k
             + pktlen - 1);
3925
31.1k
  packet->pkt.gpg_control->control = iobuf_get_noeof (inp);
3926
31.1k
  pktlen--;
3927
31.1k
  packet->pkt.gpg_control->datalen = pktlen;
3928
31.1k
  p = packet->pkt.gpg_control->data;
3929
116k
  for (; pktlen; pktlen--, p++)
3930
85.0k
    *p = iobuf_get_noeof (inp);
3931
3932
31.1k
  return 0;
3933
3934
9.03k
 skipit:
3935
9.03k
  if (list_mode)
3936
0
    {
3937
0
      int c;
3938
3939
0
      i = 0;
3940
0
      es_fprintf (listfp, "- private (rest length %lu)\n", pktlen);
3941
0
      if (partial)
3942
0
  {
3943
0
    while ((c = iobuf_get (inp)) != -1)
3944
0
      dump_hex_line (c, &i);
3945
0
  }
3946
0
      else
3947
0
  {
3948
0
    for (; pktlen; pktlen--)
3949
0
      {
3950
0
        dump_hex_line ((c = iobuf_get (inp)), &i);
3951
0
        if (c == -1)
3952
0
    break;
3953
0
      }
3954
0
  }
3955
0
      es_putc ('\n', listfp);
3956
0
    }
3957
9.03k
  iobuf_skip_rest (inp, pktlen, 0);
3958
9.03k
  return gpg_error (GPG_ERR_INV_PACKET);
3959
31.1k
}
3960
3961
3962
/* Create a GPG control packet to be used internally as a placeholder.  */
3963
PACKET *
3964
create_gpg_control (ctrlpkttype_t type, const byte * data, size_t datalen)
3965
129k
{
3966
129k
  PACKET *packet;
3967
129k
  byte *p;
3968
3969
129k
  if (!data)
3970
0
    datalen = 0;
3971
3972
129k
  packet = xmalloc (sizeof *packet);
3973
129k
  init_packet (packet);
3974
129k
  packet->pkttype = PKT_GPG_CONTROL;
3975
129k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen);
3976
129k
  packet->pkt.gpg_control->control = type;
3977
129k
  packet->pkt.gpg_control->datalen = datalen;
3978
129k
  p = packet->pkt.gpg_control->data;
3979
4.93M
  for (; datalen; datalen--, p++)
3980
4.80M
    *p = *data++;
3981
3982
129k
  return packet;
3983
129k
}