Coverage Report

Created: 2026-09-01 07:00

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