Coverage Report

Created: 2026-09-01 06:58

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
47.2k
{
96
47.2k
  unsigned short a;
97
47.2k
  a = (unsigned short)iobuf_get_noeof (inp) << 8;
98
47.2k
  a |= iobuf_get_noeof (inp);
99
47.2k
  return a;
100
47.2k
}
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
234k
{
107
234k
  unsigned long a;
108
234k
  a = (unsigned long)iobuf_get_noeof (inp) << 24;
109
234k
  a |= iobuf_get_noeof (inp) << 16;
110
234k
  a |= iobuf_get_noeof (inp) << 8;
111
234k
  a |= iobuf_get_noeof (inp);
112
234k
  return a;
113
234k
}
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
46.8k
{
131
46.8k
  int c, c1, c2, i;
132
46.8k
  unsigned int nmax = *ret_nread;
133
46.8k
  unsigned int nbits, nbytes;
134
46.8k
  size_t nread = 0;
135
46.8k
  gcry_mpi_t a = NULL;
136
46.8k
  byte *buf = NULL;
137
46.8k
  byte *p;
138
139
46.8k
  if (!nmax)
140
463
    goto overflow;
141
142
46.3k
  if ((c = c1 = iobuf_get (inp)) == -1)
143
291
    goto leave;
144
46.0k
  if (++nread == nmax)
145
3
    goto overflow;
146
46.0k
  nbits = c << 8;
147
46.0k
  if ((c = c2 = iobuf_get (inp)) == -1)
148
110
    goto leave;
149
45.9k
  ++nread;
150
45.9k
  nbits |= c;
151
45.9k
  if (nbits > MAX_EXTERN_MPI_BITS)
152
1.71k
    {
153
1.71k
      log_error ("mpi too large (%u bits)\n", nbits);
154
1.71k
      goto leave;
155
1.71k
    }
156
157
44.2k
  nbytes = (nbits + 7) / 8;
158
44.2k
  buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2);
159
44.2k
  p = buf;
160
44.2k
  p[0] = c1;
161
44.2k
  p[1] = c2;
162
630k
  for (i = 0; i < nbytes; i++)
163
587k
    {
164
587k
      if (nread == nmax)
165
707
  goto overflow;
166
167
586k
      c = iobuf_get (inp);
168
586k
      if (c == -1)
169
701
  goto leave;
170
171
586k
      p[i + 2] = c;
172
586k
      nread ++;
173
586k
    }
174
175
42.8k
  if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread))
176
0
    a = NULL;
177
178
42.8k
  *ret_nread = nread;
179
42.8k
  gcry_free(buf);
180
42.8k
  return a;
181
182
1.17k
 overflow:
183
1.17k
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
184
3.98k
 leave:
185
3.98k
  *ret_nread = nread;
186
3.98k
  gcry_free(buf);
187
3.98k
  return a;
188
1.17k
}
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
102
{
208
102
  gpg_error_t err;
209
102
  int c, i;
210
102
  byte *buf = NULL;
211
102
  byte *p;
212
213
102
  *r_data = NULL;
214
215
102
  if ((nbytes && nlength)
216
102
      || (!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
102
  if (nlength)
223
102
    {
224
453
      for (i = 0; i < nlength; i++)
225
359
        {
226
359
          if (!*pktlen)
227
2
            {
228
2
              err = gpg_error (GPG_ERR_INV_PACKET);
229
2
              goto leave;
230
2
            }
231
357
          c = iobuf_readbyte (inp);
232
357
          if (c < 0)
233
6
            {
234
6
              err =  gpg_error (GPG_ERR_INV_PACKET);
235
6
              goto leave;
236
6
            }
237
351
          --*pktlen;
238
351
          nbytes <<= 8;
239
351
          nbytes |= c;
240
351
        }
241
242
94
      if (!nbytes)
243
3
        {
244
3
          err =  gpg_error (GPG_ERR_INV_PACKET);
245
3
          goto leave;
246
3
        }
247
94
    }
248
249
91
  if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS)
250
35
      || (nbytes*8 < nbytes))
251
56
    {
252
56
      log_error ("octet string too large (%u octets)\n", nbytes);
253
56
      err = gpg_error (GPG_ERR_TOO_LARGE);
254
56
      goto leave;
255
56
    }
256
257
35
  if (nbytes > *pktlen)
258
6
    {
259
6
      log_error ("octet string larger than packet (%u octets)\n", nbytes);
260
6
      err = gpg_error (GPG_ERR_INV_PACKET);
261
6
      goto leave;
262
6
    }
263
264
29
  buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes);
265
29
  if (!buf)
266
0
    {
267
0
      err = gpg_error_from_syserror ();
268
0
      goto leave;
269
0
    }
270
29
  p = buf;
271
708
  for (i = 0; i < nbytes; i++)
272
693
    {
273
693
      c = iobuf_get (inp);
274
693
      if (c == -1)
275
14
        {
276
14
          err = gpg_error (GPG_ERR_INV_PACKET);
277
14
          goto leave;
278
14
        }
279
280
679
      p[i] = c;
281
679
      --*pktlen;
282
679
    }
283
284
15
  *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8);
285
15
  gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2);
286
15
  return 0;
287
288
87
 leave:
289
87
  gcry_free (buf);
290
87
  return err;
291
29
}
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
18.1k
{
308
18.1k
  int c, c1, c2, i;
309
18.1k
  unsigned int nmax = *ret_nread;
310
18.1k
  unsigned int nbits, nbytes;
311
18.1k
  size_t nread = 0;
312
18.1k
  gcry_mpi_t a = NULL;
313
18.1k
  byte *buf = NULL;
314
18.1k
  byte *p;
315
316
18.1k
  if (!nmax)
317
21
    goto overflow;
318
319
18.0k
  if ((c = c1 = iobuf_get (inp)) == -1)
320
61
    goto leave;
321
18.0k
  if (++nread == nmax)
322
3
    goto overflow;
323
18.0k
  nbits = c << 8;
324
18.0k
  if ((c = c2 = iobuf_get (inp)) == -1)
325
41
    goto leave;
326
17.9k
  ++nread;
327
17.9k
  nbits |= c;
328
17.9k
  if (nbits > MAX_EXTERN_MPI_BITS)
329
83
    {
330
83
      log_error ("mpi too large (%u bits)\n", nbits);
331
83
      goto leave;
332
83
    }
333
334
17.8k
  nbytes = (nbits + 7) / 8;
335
17.8k
  buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes);
336
17.8k
  p = buf;
337
385k
  for (i = 0; i < nbytes; i++)
338
367k
    {
339
367k
      if (nread == nmax)
340
43
        goto overflow;
341
342
367k
      c = iobuf_get (inp);
343
367k
      if (c == -1)
344
93
        goto leave;
345
346
367k
      p[i] = c;
347
367k
      nread ++;
348
367k
    }
349
350
17.7k
  a = gcry_mpi_set_opaque (NULL, buf, nbits);
351
17.7k
  gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2);
352
17.7k
  *ret_nread = nread;
353
17.7k
  return a;
354
355
67
 overflow:
356
67
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
357
345
 leave:
358
345
  *ret_nread = nread;
359
345
  gcry_free(buf);
360
345
  return a;
361
67
}
362
363
364
/* Register STRING as a known critical notation name.  */
365
void
366
register_known_notation (const char *string)
367
138
{
368
138
  strlist_t sl;
369
370
138
  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
138
  if (!string)
377
138
    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
1.12k
{
395
1.12k
  int old = list_mode;
396
1.12k
  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
1.12k
  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
1.12k
  return old;
430
1.12k
}
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
94.2k
{
438
94.2k
  static byte unknown_pubkey_algos[256];
439
440
  /* First check whether the algorithm is usable but not suitable for
441
     encryption/signing.  */
442
94.2k
  if (pubkey_get_npkey (algo))
443
7.24k
    {
444
7.24k
      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
7.24k
    }
454
86.9k
  else
455
86.9k
    {
456
86.9k
      algo &= 0xff;
457
86.9k
      if (!unknown_pubkey_algos[algo])
458
173
        {
459
173
          if (opt.verbose && !glo_ctrl.silence_parse_warnings)
460
173
            log_info (_("can't handle public key algorithm %d\n"), algo);
461
173
          unknown_pubkey_algos[algo] = 1;
462
173
        }
463
86.9k
    }
464
94.2k
}
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
720k
{
472
720k
  int skip, rc;
473
474
720k
  do
475
720k
    {
476
720k
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l);
477
720k
    }
478
720k
  while (skip && ! rc);
479
720k
  return rc;
480
720k
}
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
720k
{
735
720k
  int rc = 0;
736
720k
  iobuf_t inp;
737
720k
  int c, ctb, pkttype, lenbytes;
738
720k
  unsigned long pktlen;
739
720k
  byte hdr[8];
740
720k
  int hdrlen;
741
720k
  int new_ctb = 0, partial = 0;
742
720k
  int with_uid = (onlykeypkts == 2);
743
720k
  off_t pos;
744
745
720k
  *skip = 0;
746
720k
  inp = ctx->inp;
747
748
758k
 again:
749
758k
  log_assert (!pkt->pkt.generic);
750
758k
  if (retpos || list_mode)
751
0
    {
752
0
      pos = iobuf_tell (inp);
753
0
      if (retpos)
754
0
        *retpos = pos;
755
0
    }
756
758k
  else
757
758k
    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
758k
  if ((ctb = iobuf_get (inp)) == -1)
762
13.4k
    {
763
13.4k
      rc = -1;
764
13.4k
      goto leave;
765
13.4k
    }
766
744k
  ctx->last_ctb = ctb;
767
744k
  hdrlen = 0;
768
744k
  hdr[hdrlen++] = ctb;
769
770
744k
  if (!(ctb & 0x80))
771
42.6k
    {
772
42.6k
      log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb);
773
42.6k
      rc = gpg_error (GPG_ERR_INV_PACKET);
774
42.6k
      goto leave;
775
42.6k
    }
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
701k
  pktlen = 0;
785
701k
  new_ctb = !!(ctb & 0x40);
786
701k
  if (new_ctb)
787
399k
    pkttype = ctb & 0x3f;
788
302k
  else
789
302k
    pkttype = (ctb >> 2) & 0xf;
790
791
701k
  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
701k
  if (new_ctb)
799
399k
    {
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
399k
      if ((c = iobuf_get (inp)) == -1)
805
645
  {
806
645
    log_error ("%s: 1st length byte missing\n", iobuf_where (inp));
807
645
    rc = gpg_error (GPG_ERR_INV_PACKET);
808
645
    goto leave;
809
645
  }
810
811
812
398k
      hdr[hdrlen++] = c;
813
398k
      if (c < 192)
814
303k
        pktlen = c;
815
95.3k
      else if (c < 224)
816
29.0k
        {
817
29.0k
          pktlen = (c - 192) * 256;
818
29.0k
          if ((c = iobuf_get (inp)) == -1)
819
153
            {
820
153
              log_error ("%s: 2nd length byte missing\n",
821
153
                         iobuf_where (inp));
822
153
              rc = gpg_error (GPG_ERR_INV_PACKET);
823
153
              goto leave;
824
153
            }
825
28.8k
          hdr[hdrlen++] = c;
826
28.8k
          pktlen += c + 192;
827
28.8k
        }
828
66.2k
      else if (c == 255)
829
1.84k
        {
830
1.84k
    int i;
831
1.84k
    char value[4];
832
833
9.13k
    for (i = 0; i < 4; i ++)
834
7.33k
            {
835
7.33k
              if ((c = iobuf_get (inp)) == -1)
836
50
                {
837
50
                  log_error ("%s: 4 byte length invalid\n", iobuf_where (inp));
838
50
                  rc = gpg_error (GPG_ERR_INV_PACKET);
839
50
                  goto leave;
840
50
                }
841
7.28k
              value[i] = hdr[hdrlen++] = c;
842
7.28k
            }
843
844
1.79k
    pktlen = buf32_to_ulong (value);
845
1.79k
        }
846
64.4k
      else /* Partial body length.  */
847
64.4k
        {
848
64.4k
          switch (pkttype)
849
64.4k
            {
850
2.26k
            case PKT_PLAINTEXT:
851
2.52k
            case PKT_ENCRYPTED:
852
3.24k
            case PKT_ENCRYPTED_MDC:
853
5.43k
            case PKT_ENCRYPTED_AEAD:
854
64.1k
            case PKT_COMPRESSED:
855
64.1k
              iobuf_set_partial_body_length_mode (inp, c & 0xff);
856
64.1k
              pktlen = 0; /* To indicate partial length.  */
857
64.1k
              partial = 1;
858
64.1k
              break;
859
860
294
            default:
861
294
              log_error ("%s: partial length invalid for"
862
294
                         " packet type %d\n", iobuf_where (inp), pkttype);
863
294
              rc = gpg_error (GPG_ERR_INV_PACKET);
864
294
              goto leave;
865
64.4k
            }
866
64.4k
        }
867
868
398k
    }
869
302k
  else /* This is an old format packet.  */
870
302k
    {
871
      /* The type of length encoding is encoded in bits 0-1 of the
872
   tag.  */
873
302k
      lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3));
874
302k
      if (!lenbytes)
875
56.4k
  {
876
56.4k
    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
56.4k
    partial = 1;
880
56.4k
    if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT
881
55.9k
        && pkttype != PKT_COMPRESSED)
882
304
      {
883
304
        log_error ("%s: indeterminate length for invalid"
884
304
       " packet type %d\n", iobuf_where (inp), pkttype);
885
304
        rc = gpg_error (GPG_ERR_INV_PACKET);
886
304
        goto leave;
887
304
      }
888
56.4k
  }
889
246k
      else
890
246k
  {
891
511k
    for (; lenbytes; lenbytes--)
892
265k
      {
893
265k
        pktlen <<= 8;
894
265k
        c = iobuf_get (inp);
895
265k
        if (c == -1)
896
218
    {
897
218
      log_error ("%s: length invalid\n", iobuf_where (inp));
898
218
      rc = gpg_error (GPG_ERR_INV_PACKET);
899
218
      goto leave;
900
218
    }
901
265k
        pktlen |= hdr[hdrlen++] = c;
902
265k
      }
903
246k
  }
904
302k
    }
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
700k
  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
700k
  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
700k
  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
700k
  else if (do_skip
946
     /* type==0 is not allowed.  This is an invalid packet.  */
947
700k
     || !pkttype
948
     /* When ONLYKEYPKTS is set, we don't skip keys.  */
949
700k
     || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
950
0
         && pkttype != PKT_PUBLIC_KEY
951
0
         && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY))
952
152
    {
953
152
      iobuf_skip_rest (inp, pktlen, partial);
954
152
      *skip = 1;
955
152
      rc = 0;
956
152
      goto leave;
957
152
    }
958
959
700k
  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
700k
  if (list_mode)
973
700k
    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
700k
  ctx->n_parsed_packets++;
980
981
700k
  pkt->pkttype = pkttype;
982
700k
  rc = GPG_ERR_UNKNOWN_PACKET;  /* default error */
983
700k
  switch (pkttype)
984
700k
    {
985
35.2k
    case PKT_PUBLIC_KEY:
986
44.4k
    case PKT_PUBLIC_SUBKEY:
987
54.2k
    case PKT_SECRET_KEY:
988
65.3k
    case PKT_SECRET_SUBKEY:
989
65.3k
      pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key);
990
65.3k
      rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt);
991
65.3k
      break;
992
49.7k
    case PKT_SYMKEY_ENC:
993
49.7k
      rc = parse_symkeyenc (inp, pkttype, pktlen, pkt);
994
49.7k
      break;
995
27.5k
    case PKT_PUBKEY_ENC:
996
27.5k
      rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt);
997
27.5k
      break;
998
64.5k
    case PKT_SIGNATURE:
999
64.5k
      pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature);
1000
64.5k
      rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature);
1001
64.5k
      break;
1002
2.99k
    case PKT_ONEPASS_SIG:
1003
2.99k
      pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig);
1004
2.99k
      rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig);
1005
2.99k
      break;
1006
136k
    case PKT_USER_ID:
1007
136k
      rc = parse_user_id (inp, pkttype, pktlen, pkt);
1008
136k
      break;
1009
12.2k
    case PKT_ATTRIBUTE:
1010
12.2k
      pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
1011
12.2k
      rc = parse_attribute (inp, pkttype, pktlen, pkt);
1012
12.2k
      break;
1013
1.52k
    case PKT_OLD_COMMENT:
1014
2.45k
    case PKT_COMMENT:
1015
2.45k
      rc = parse_comment (inp, pkttype, pktlen, pkt);
1016
2.45k
      break;
1017
37.5k
    case PKT_RING_TRUST:
1018
37.5k
      {
1019
37.5k
        rc = parse_ring_trust (ctx, pktlen);
1020
37.5k
        if (!rc)
1021
37.5k
          goto again; /* Directly read the next packet.  */
1022
37.5k
      }
1023
0
      break;
1024
13.7k
    case PKT_PLAINTEXT:
1025
13.7k
      rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial);
1026
13.7k
      break;
1027
116k
    case PKT_COMPRESSED:
1028
116k
      rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb);
1029
116k
      break;
1030
55.9k
    case PKT_ENCRYPTED:
1031
57.5k
    case PKT_ENCRYPTED_MDC:
1032
57.5k
      rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial);
1033
57.5k
      break;
1034
7.02k
    case PKT_MDC:
1035
7.02k
      rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb);
1036
7.02k
      break;
1037
93.0k
    case PKT_ENCRYPTED_AEAD:
1038
93.0k
      rc = parse_encrypted_aead (inp, pkttype, pktlen, pkt, partial);
1039
93.0k
      break;
1040
4.22k
    case PKT_GPG_CONTROL:
1041
4.22k
      rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial);
1042
4.22k
      break;
1043
3.28k
    case PKT_MARKER:
1044
3.28k
      rc = parse_marker (inp, pkttype, pktlen);
1045
3.28k
      break;
1046
4.95k
    default:
1047
      /* Unknown packet.  Skip it.  */
1048
4.95k
      skip_packet (inp, pkttype, pktlen, partial);
1049
4.95k
      break;
1050
700k
    }
1051
1052
  /* Store a shallow copy of certain packets in the context.  */
1053
662k
  free_packet (NULL, ctx);
1054
662k
  if (!rc && (pkttype == PKT_PUBLIC_KEY
1055
564k
              || pkttype == PKT_SECRET_KEY
1056
559k
              || pkttype == PKT_USER_ID
1057
411k
              || pkttype == PKT_ATTRIBUTE
1058
411k
              || pkttype == PKT_SIGNATURE))
1059
238k
    {
1060
238k
      ctx->last_pkt = *pkt;
1061
238k
    }
1062
1063
720k
 leave:
1064
  /* FIXME: We leak in case of an error (see the xmalloc's above).  */
1065
720k
  if (!rc && iobuf_error (inp))
1066
1.21k
    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
720k
  return rc == -1? -1 : gpg_err_code (rc);
1072
662k
}
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
4.95k
{
1151
4.95k
  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
4.95k
  iobuf_skip_rest (inp, pktlen, partial);
1178
4.95k
}
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
70.3k
{
1188
70.3k
  int c;
1189
70.3k
  byte *buf, *p;
1190
1191
70.3k
  buf = xtrymalloc (pktlen);
1192
70.3k
  if (!buf)
1193
10.6k
    {
1194
10.6k
      gpg_error_t err = gpg_error_from_syserror ();
1195
10.6k
      log_error ("error reading rest of packet: %s\n", gpg_strerror (err));
1196
10.6k
      return NULL;
1197
10.6k
    }
1198
435k
  for (p = buf; pktlen; pktlen--)
1199
386k
    {
1200
386k
      c = iobuf_get (inp);
1201
386k
      if (c == -1)
1202
11.2k
        {
1203
11.2k
          log_error ("premature eof while reading rest of packet\n");
1204
11.2k
          xfree (buf);
1205
11.2k
          return NULL;
1206
11.2k
        }
1207
375k
      *p++ = c;
1208
375k
    }
1209
1210
48.5k
  return buf;
1211
59.7k
}
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
10.5k
{
1222
10.5k
  char buffer[256];
1223
10.5k
  char *tmpbuf;
1224
10.5k
  int i, c, nbytes;
1225
1226
10.5k
  *r_data = NULL;
1227
1228
10.5k
  if (!*pktlen)
1229
3
    return gpg_error (GPG_ERR_INV_PACKET);
1230
10.5k
  c = iobuf_readbyte (inp);
1231
10.5k
  if (c < 0)
1232
38
    return gpg_error (GPG_ERR_INV_PACKET);
1233
10.4k
  --*pktlen;
1234
10.4k
  nbytes = c;
1235
10.4k
  if (nbytes < 2 || nbytes > 254)
1236
7
    return gpg_error (GPG_ERR_INV_PACKET);
1237
10.4k
  if (nbytes > *pktlen)
1238
18
    return gpg_error (GPG_ERR_INV_PACKET);
1239
1240
10.4k
  buffer[0] = nbytes;
1241
1242
96.1k
  for (i = 0; i < nbytes; i++)
1243
85.7k
    {
1244
85.7k
      c = iobuf_get (inp);
1245
85.7k
      if (c < 0)
1246
41
        return gpg_error (GPG_ERR_INV_PACKET);
1247
85.7k
      --*pktlen;
1248
85.7k
      buffer[1+i] = c;
1249
85.7k
    }
1250
1251
10.4k
  tmpbuf = xtrymalloc (1 + nbytes);
1252
10.4k
  if (!tmpbuf)
1253
0
    return gpg_error_from_syserror ();
1254
10.4k
  memcpy (tmpbuf, buffer, 1 + nbytes);
1255
10.4k
  *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes));
1256
10.4k
  if (!*r_data)
1257
0
    {
1258
0
      xfree (tmpbuf);
1259
0
      return gpg_error_from_syserror ();
1260
0
    }
1261
10.4k
  return 0;
1262
10.4k
}
1263
1264
1265
/* Parse a marker packet.  */
1266
static int
1267
parse_marker (IOBUF inp, int pkttype, unsigned long pktlen)
1268
3.28k
{
1269
3.28k
  (void) pkttype;
1270
1271
3.28k
  if (pktlen != 3)
1272
404
    goto fail;
1273
1274
2.88k
  if (iobuf_get (inp) != 'P')
1275
715
    {
1276
715
      pktlen--;
1277
715
      goto fail;
1278
715
    }
1279
1280
2.16k
  if (iobuf_get (inp) != 'G')
1281
99
    {
1282
99
      pktlen--;
1283
99
      goto fail;
1284
99
    }
1285
1286
2.07k
  if (iobuf_get (inp) != 'P')
1287
1.68k
    {
1288
1.68k
      pktlen--;
1289
1.68k
      goto fail;
1290
1.68k
    }
1291
1292
390
  if (list_mode)
1293
390
    es_fputs (":marker packet: PGP\n", listfp);
1294
1295
390
  return 0;
1296
1297
2.89k
 fail:
1298
2.89k
  log_error ("invalid marker packet\n");
1299
2.89k
  if (list_mode)
1300
2.89k
    es_fputs (":marker packet: [invalid]\n", listfp);
1301
2.89k
  iobuf_skip_rest (inp, pktlen, 0);
1302
2.89k
  return GPG_ERR_INV_PACKET;
1303
2.07k
}
1304
1305
1306
static int
1307
parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1308
     PACKET * packet)
1309
49.7k
{
1310
49.7k
  PKT_symkey_enc *k;
1311
49.7k
  int rc = 0;
1312
49.7k
  int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen;
1313
1314
49.7k
  if (pktlen < 4)
1315
1.17k
    goto too_short;
1316
48.6k
  version = iobuf_get_noeof (inp);
1317
48.6k
  pktlen--;
1318
48.6k
  if (version == 4)
1319
14.7k
    ;
1320
33.8k
  else if (version == 5)
1321
31.7k
    ;
1322
2.05k
  else
1323
2.05k
    {
1324
2.05k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
1325
2.05k
      if (list_mode)
1326
2.05k
        es_fprintf (listfp, ":symkey enc packet: [unknown version]\n");
1327
2.05k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1328
2.05k
      goto leave;
1329
2.05k
    }
1330
46.5k
  if (pktlen > 200)
1331
24.0k
    {       /* (we encode the seskeylen in a byte) */
1332
24.0k
      log_error ("packet(%d) too large\n", pkttype);
1333
24.0k
      if (list_mode)
1334
24.0k
        es_fprintf (listfp, ":symkey enc packet: [too large]\n");
1335
24.0k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1336
24.0k
      goto leave;
1337
24.0k
    }
1338
22.4k
  cipher_algo = iobuf_get_noeof (inp);
1339
22.4k
  pktlen--;
1340
22.4k
  if (version == 5)
1341
7.74k
    {
1342
7.74k
      aead_algo = iobuf_get_noeof (inp);
1343
7.74k
      pktlen--;
1344
7.74k
    }
1345
14.7k
  else
1346
14.7k
    aead_algo = 0;
1347
22.4k
  if (pktlen < 2)
1348
5
    goto too_short;
1349
22.4k
  s2kmode = iobuf_get_noeof (inp);
1350
22.4k
  pktlen--;
1351
22.4k
  hash_algo = iobuf_get_noeof (inp);
1352
22.4k
  pktlen--;
1353
22.4k
  switch (s2kmode)
1354
22.4k
    {
1355
19.6k
    case 0: /* Simple S2K.  */
1356
19.6k
      minlen = 0;
1357
19.6k
      break;
1358
575
    case 1: /* Salted S2K.  */
1359
575
      minlen = 8;
1360
575
      break;
1361
341
    case 3: /* Iterated+salted S2K.  */
1362
341
      minlen = 9;
1363
341
      break;
1364
1.91k
    default:
1365
1.91k
      log_error ("unknown S2K mode %d\n", s2kmode);
1366
1.91k
      if (list_mode)
1367
1.91k
        es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n");
1368
1.91k
      goto leave;
1369
22.4k
    }
1370
20.5k
  if (minlen > pktlen)
1371
250
    {
1372
250
      log_error ("packet with S2K %d too short\n", s2kmode);
1373
250
      if (list_mode)
1374
250
        es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1375
250
      rc = gpg_error (GPG_ERR_INV_PACKET);
1376
250
      goto leave;
1377
250
    }
1378
20.3k
  seskeylen = pktlen - minlen;
1379
20.3k
  k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc);
1380
20.3k
  k->version = version;
1381
20.3k
  k->cipher_algo = cipher_algo;
1382
20.3k
  k->aead_algo = aead_algo;
1383
20.3k
  k->s2k.mode = s2kmode;
1384
20.3k
  k->s2k.hash_algo = hash_algo;
1385
20.3k
  if (s2kmode == 1 || s2kmode == 3)
1386
666
    {
1387
5.99k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
1388
5.32k
  k->s2k.salt[i] = iobuf_get_noeof (inp);
1389
666
    }
1390
20.3k
  if (s2kmode == 3)
1391
339
    {
1392
339
      k->s2k.count = iobuf_get_noeof (inp);
1393
339
      pktlen--;
1394
339
    }
1395
20.3k
  k->seskeylen = seskeylen;
1396
20.3k
  if (k->seskeylen)
1397
6.59k
    {
1398
6.59k
      k->seskey = xcalloc (1, seskeylen);
1399
19.6k
      for (i = 0; i < seskeylen && pktlen; i++, pktlen--)
1400
13.0k
  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
6.59k
      if (s2kmode != 1 && s2kmode != 3)
1406
6.59k
  log_info (_("WARNING: potentially insecure symmetrically"
1407
6.23k
        " encrypted session key\n"));
1408
6.59k
    }
1409
20.3k
  log_assert (!pktlen);
1410
1411
20.3k
  if (list_mode)
1412
0
    {
1413
0
      es_fprintf (listfp,
1414
0
                  ":symkey enc packet: version %d, cipher %d, aead %d,"
1415
0
                  " s2k %d, hash %d",
1416
0
                  version, cipher_algo, aead_algo, s2kmode, hash_algo);
1417
0
      if (seskeylen)
1418
0
        {
1419
          /* To compute the size of the session key we need to know
1420
           * the size of the AEAD nonce which we may not know.  Thus
1421
           * we show only the size of the entire encrypted session
1422
           * key.  */
1423
0
          if (aead_algo)
1424
0
            es_fprintf (listfp, ", encrypted seskey %d bytes", seskeylen);
1425
0
          else
1426
0
            es_fprintf (listfp, ", seskey %d bits", (seskeylen - 1) * 8);
1427
0
        }
1428
0
      es_fprintf (listfp, "\n");
1429
0
      if (s2kmode == 1 || s2kmode == 3)
1430
0
  {
1431
0
    es_fprintf (listfp, "\tsalt ");
1432
0
          es_write_hexstring (listfp, k->s2k.salt, 8, 0, NULL);
1433
0
    if (s2kmode == 3)
1434
0
      es_fprintf (listfp, ", count %lu (%lu)",
1435
0
                        S2K_DECODE_COUNT ((ulong) k->s2k.count),
1436
0
                        (ulong) k->s2k.count);
1437
0
    es_fprintf (listfp, "\n");
1438
0
  }
1439
0
    }
1440
1441
49.7k
 leave:
1442
49.7k
  iobuf_skip_rest (inp, pktlen, 0);
1443
49.7k
  return rc;
1444
1445
1.18k
 too_short:
1446
1.18k
  log_error ("packet(%d) too short\n", pkttype);
1447
1.18k
  if (list_mode)
1448
1.18k
    es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1449
1.18k
  rc = gpg_error (GPG_ERR_INV_PACKET);
1450
1.18k
  goto leave;
1451
20.3k
}
1452
1453
1454
/* Parse a public key encrypted packet (Tag 1).  */
1455
static int
1456
parse_pubkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1457
     PACKET * packet)
1458
27.5k
{
1459
27.5k
  int rc = 0;
1460
27.5k
  int i, ndata;
1461
27.5k
  unsigned int n;
1462
27.5k
  PKT_pubkey_enc *k;
1463
1464
27.5k
  k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc);
1465
27.5k
  if (pktlen < 12)
1466
279
    {
1467
279
      log_error ("packet(%d) too short\n", pkttype);
1468
279
      if (list_mode)
1469
279
        es_fputs (":pubkey enc packet: [too short]\n", listfp);
1470
279
      rc = gpg_error (GPG_ERR_INV_PACKET);
1471
279
      goto leave;
1472
279
    }
1473
27.3k
  k->version = iobuf_get_noeof (inp);
1474
27.3k
  pktlen--;
1475
27.3k
  if (k->version != 2 && k->version != 3)
1476
190
    {
1477
190
      log_error ("packet(%d) with unknown version %d\n", pkttype, k->version);
1478
190
      if (list_mode)
1479
190
        es_fputs (":pubkey enc packet: [unknown version]\n", listfp);
1480
190
      rc = gpg_error (GPG_ERR_INV_PACKET);
1481
190
      goto leave;
1482
190
    }
1483
27.1k
  k->keyid[0] = read_32 (inp);
1484
27.1k
  pktlen -= 4;
1485
27.1k
  k->keyid[1] = read_32 (inp);
1486
27.1k
  pktlen -= 4;
1487
27.1k
  k->pubkey_algo = iobuf_get_noeof (inp);
1488
27.1k
  pktlen--;
1489
27.1k
  k->throw_keyid = 0;  /* Only used as flag for build_packet.  */
1490
27.1k
  if (list_mode)
1491
27.1k
    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
27.1k
  ndata = pubkey_get_nenc (k->pubkey_algo);
1497
27.1k
  if (!ndata)
1498
24.6k
    {
1499
24.6k
      if (list_mode)
1500
24.6k
  es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo);
1501
24.6k
      unknown_pubkey_warning (k->pubkey_algo);
1502
24.6k
      k->data[0] = NULL; /* No need to store the encrypted data.  */
1503
24.6k
    }
1504
2.49k
  else if (k->pubkey_algo == PUBKEY_ALGO_ECDH)
1505
348
    {
1506
348
      log_assert (ndata == 2);
1507
      /* Get the ephemeral public key.  */
1508
348
      n = pktlen;
1509
348
      k->data[0] = sos_read (inp, &n, 0);
1510
348
      pktlen -= n;
1511
348
      if (!k->data[0])
1512
44
        {
1513
44
          rc = gpg_error (GPG_ERR_INV_PACKET);
1514
44
          goto leave;
1515
44
        }
1516
      /* Get the wrapped symmetric key.  */
1517
304
      rc = read_sized_octet_string (inp, &pktlen, k->data + 1);
1518
304
      if (rc)
1519
18
        goto leave;
1520
304
    }
1521
2.14k
  else if (k->pubkey_algo == PUBKEY_ALGO_KYBER)
1522
98
    {
1523
98
      log_assert (ndata == 3);
1524
      /* Get the ephemeral public key.  */
1525
98
      n = pktlen;
1526
98
      k->data[0] = sos_read (inp, &n, 0);
1527
98
      pktlen -= n;
1528
98
      if (!k->data[0])
1529
10
        {
1530
10
          rc = gpg_error (GPG_ERR_INV_PACKET);
1531
10
          goto leave;
1532
10
        }
1533
      /* Get the Kyber ciphertext.  */
1534
88
      rc = read_octet_string (inp, &pktlen, 4, 0, 0, k->data + 1);
1535
88
      if (rc)
1536
75
        goto leave;
1537
      /* Get the algorithm id for the session key.  */
1538
13
      if (!pktlen)
1539
0
        {
1540
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
1541
0
          goto leave;
1542
0
        }
1543
13
      k->seskey_algo = iobuf_get_noeof (inp);
1544
13
      pktlen--;
1545
      /* Get the encrypted symmetric key.  */
1546
13
      rc = read_octet_string (inp, &pktlen, 1, 0, 0, k->data + 2);
1547
13
      if (rc)
1548
11
        goto leave;
1549
13
    }
1550
2.04k
  else
1551
2.04k
    {
1552
4.33k
      for (i = 0; i < ndata; i++)
1553
2.28k
        {
1554
2.28k
          n = pktlen;
1555
2.28k
          k->data[i] = mpi_read (inp, &n, 0);
1556
2.28k
          pktlen -= n;
1557
2.28k
          if (!k->data[i])
1558
212
            rc = gpg_error (GPG_ERR_INV_PACKET);
1559
2.28k
        }
1560
2.04k
      if (rc)
1561
187
        goto leave;
1562
2.04k
    }
1563
26.7k
  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
27.5k
 leave:
1577
27.5k
  iobuf_skip_rest (inp, pktlen, 0);
1578
27.5k
  return rc;
1579
26.7k
}
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
43.9k
{
1890
43.9k
  switch (type)
1891
43.9k
    {
1892
1.26k
    case SIGSUBPKT_REV_KEY:
1893
1.26k
      if (n < 22)
1894
0
  break;
1895
1.26k
      return 0;
1896
7.65k
    case SIGSUBPKT_SIG_CREATED:
1897
8.18k
    case SIGSUBPKT_SIG_EXPIRE:
1898
9.66k
    case SIGSUBPKT_KEY_EXPIRE:
1899
9.66k
      if (n < 4)
1900
183
  break;
1901
9.47k
      return 0;
1902
1.72k
    case SIGSUBPKT_KEY_FLAGS:
1903
2.55k
    case SIGSUBPKT_KS_FLAGS:
1904
3.73k
    case SIGSUBPKT_PREF_SYM:
1905
4.01k
    case SIGSUBPKT_PREF_AEAD:
1906
4.99k
    case SIGSUBPKT_PREF_HASH:
1907
6.14k
    case SIGSUBPKT_PREF_COMPR:
1908
6.65k
    case SIGSUBPKT_POLICY:
1909
7.66k
    case SIGSUBPKT_PREF_KS:
1910
9.69k
    case SIGSUBPKT_FEATURES:
1911
9.76k
    case SIGSUBPKT_REGEXP:
1912
9.76k
    case SIGSUBPKT_ATTST_SIGS:
1913
9.76k
      return 0;
1914
652
    case SIGSUBPKT_SIGNATURE:
1915
1.57k
    case SIGSUBPKT_EXPORTABLE:
1916
2.86k
    case SIGSUBPKT_REVOCABLE:
1917
2.86k
    case SIGSUBPKT_REVOC_REASON:
1918
2.86k
      if (!n)
1919
508
  break;
1920
2.36k
      return 0;
1921
4.96k
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1922
4.96k
      if (n < 8)
1923
64
  break;
1924
4.90k
      return 0;
1925
5.26k
    case SIGSUBPKT_ISSUER_FPR:  /* issuer key fingerprint */
1926
5.26k
      if (n < 21)
1927
1.86k
  break;
1928
3.39k
      return 0;
1929
204
    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
204
      if (n < 8
1934
147
    || 8 + ((buffer[4] << 8) | buffer[5]) +
1935
147
    ((buffer[6] << 8) | buffer[7]) != n)
1936
86
  break;
1937
118
      return 0;
1938
559
    case SIGSUBPKT_PRIMARY_UID:
1939
559
      if (n != 1)
1940
33
  break;
1941
526
      return 0;
1942
1.20k
    case SIGSUBPKT_TRUST:
1943
1.20k
      if (n != 2)
1944
1.06k
  break;
1945
142
      return 0;
1946
1.14k
    case SIGSUBPKT_KEY_BLOCK:
1947
1.14k
      if (n && buffer[0])
1948
554
        return -1; /* Unknown version - ignore.  */
1949
592
      if (n < 50)
1950
590
  break;  /* Definitely too short to carry a key block.  */
1951
2
      return 0;
1952
7.00k
    default:
1953
7.00k
      return 0;
1954
43.9k
    }
1955
4.39k
  return -2;
1956
43.9k
}
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
138
{
1963
138
  strlist_t sl;
1964
1965
138
  register_known_notation (NULL); /* Make sure it is initialized.  */
1966
1967
276
  for (sl = known_notations_list; sl; sl = sl->next)
1968
138
    if (sl->flags == len && !memcmp (sl->d, name, len))
1969
0
      return 1; /* Known */
1970
1971
138
  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
138
  return 0; /* Unknown.  */
1979
138
}
1980
1981
1982
static int
1983
can_handle_critical (const byte * buffer, size_t n, int type)
1984
14.3k
{
1985
14.3k
  switch (type)
1986
14.3k
    {
1987
213
    case SIGSUBPKT_NOTATION:
1988
213
      if (n >= 8)
1989
156
  {
1990
156
    size_t notation_len = ((buffer[4] << 8) | buffer[5]);
1991
156
    if (n - 8 >= notation_len)
1992
138
      return can_handle_critical_notation (buffer + 8, notation_len);
1993
156
  }
1994
75
      return 0;
1995
2
    case SIGSUBPKT_SIGNATURE:
1996
286
    case SIGSUBPKT_SIG_CREATED:
1997
316
    case SIGSUBPKT_SIG_EXPIRE:
1998
514
    case SIGSUBPKT_KEY_EXPIRE:
1999
547
    case SIGSUBPKT_EXPORTABLE:
2000
555
    case SIGSUBPKT_REVOCABLE:
2001
1.47k
    case SIGSUBPKT_REV_KEY:
2002
1.51k
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
2003
2.09k
    case SIGSUBPKT_ISSUER_FPR:  /* issuer fingerprint */
2004
2.69k
    case SIGSUBPKT_PREF_SYM:
2005
3.21k
    case SIGSUBPKT_PREF_AEAD:
2006
3.24k
    case SIGSUBPKT_PREF_HASH:
2007
3.47k
    case SIGSUBPKT_PREF_COMPR:
2008
3.61k
    case SIGSUBPKT_KEY_FLAGS:
2009
3.91k
    case SIGSUBPKT_PRIMARY_UID:
2010
4.01k
    case SIGSUBPKT_FEATURES:
2011
4.09k
    case SIGSUBPKT_TRUST:
2012
4.37k
    case SIGSUBPKT_REGEXP:
2013
4.47k
    case SIGSUBPKT_ATTST_SIGS:
2014
      /* Is it enough to show the policy or keyserver? */
2015
5.00k
    case SIGSUBPKT_POLICY:
2016
5.49k
    case SIGSUBPKT_PREF_KS:
2017
5.56k
    case SIGSUBPKT_REVOC_REASON: /* At least we know about it.  */
2018
5.56k
      return 1;
2019
2020
1.13k
    case SIGSUBPKT_KEY_BLOCK:
2021
1.13k
      if (n && !buffer[0])
2022
0
        return 1;
2023
1.13k
      else
2024
1.13k
        return 0;
2025
2026
7.43k
    default:
2027
7.43k
      return 0;
2028
14.3k
    }
2029
14.3k
}
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
417k
{
2036
417k
  const byte *buffer;
2037
417k
  int buflen;
2038
417k
  int type;
2039
417k
  int critical_dummy;
2040
417k
  int offset;
2041
417k
  size_t n;
2042
417k
  const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed;
2043
417k
  int seq = 0;
2044
417k
  int reqseq = start ? *start : 0;
2045
2046
417k
  if (!critical)
2047
417k
    critical = &critical_dummy;
2048
2049
417k
  if (!pktbuf || reqseq == -1)
2050
50.8k
    {
2051
50.8k
      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
50.8k
      return reqtype ==  SIGSUBPKT_TEST_CRITICAL ? dummy : NULL;
2055
50.8k
    }
2056
366k
  buffer = pktbuf->data;
2057
366k
  buflen = pktbuf->len;
2058
1.21M
  while (buflen)
2059
1.05M
    {
2060
1.05M
      n = *buffer++;
2061
1.05M
      buflen--;
2062
1.05M
      if (n == 255) /* 4 byte length header.  */
2063
49.9k
  {
2064
49.9k
    if (buflen < 4)
2065
8.15k
      goto too_short;
2066
41.8k
    n = buf32_to_size_t (buffer);
2067
41.8k
    buffer += 4;
2068
41.8k
    buflen -= 4;
2069
41.8k
  }
2070
1.00M
      else if (n >= 192) /* 4 byte special encoded length header.  */
2071
15.0k
  {
2072
15.0k
    if (buflen < 2)
2073
6.76k
      goto too_short;
2074
8.28k
    n = ((n - 192) << 8) + *buffer + 192;
2075
8.28k
    buffer++;
2076
8.28k
    buflen--;
2077
8.28k
  }
2078
1.03M
      if (buflen < n)
2079
122k
  goto too_short;
2080
916k
      if (!buflen)
2081
13.4k
        goto no_type_byte;
2082
903k
      type = *buffer;
2083
903k
      if (type & 0x80)
2084
216k
  {
2085
216k
    type &= 0x7f;
2086
216k
    *critical = 1;
2087
216k
  }
2088
686k
      else
2089
686k
  *critical = 0;
2090
903k
      if (!(++seq > reqseq))
2091
11.5k
  ;
2092
891k
      else if (reqtype == SIGSUBPKT_TEST_CRITICAL)
2093
61.2k
  {
2094
61.2k
    if (*critical)
2095
14.4k
      {
2096
14.4k
        if (n - 1 > buflen + 1)
2097
132
    goto too_short;
2098
14.3k
        if (!can_handle_critical (buffer + 1, n - 1, type))
2099
8.78k
    {
2100
8.78k
      if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2101
8.78k
        log_info (_("subpacket of type %d has "
2102
0
        "critical bit set\n"), type);
2103
8.78k
      if (start)
2104
0
        *start = seq;
2105
8.78k
      return NULL; /* This is an error.  */
2106
8.78k
    }
2107
14.3k
      }
2108
61.2k
  }
2109
830k
      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
830k
      else if (type == reqtype) /* Found.  */
2113
45.4k
  {
2114
45.4k
    buffer++;
2115
45.4k
    n--;
2116
45.4k
    if (n > buflen)
2117
1.49k
      goto too_short;
2118
43.9k
    if (ret_n)
2119
24.1k
      *ret_n = n;
2120
43.9k
    offset = parse_one_sig_subpkt (buffer, n, type);
2121
43.9k
    switch (offset)
2122
43.9k
      {
2123
4.39k
      case -2:
2124
4.39k
        log_error ("subpacket of type %d too short\n", type);
2125
4.39k
        return NULL;
2126
554
      case -1:
2127
554
        return NULL;
2128
38.9k
      default:
2129
38.9k
        break;
2130
43.9k
      }
2131
38.9k
    if (start)
2132
2.38k
      *start = seq;
2133
38.9k
    return buffer + offset;
2134
43.9k
  }
2135
848k
      buffer += n;
2136
848k
      buflen -= n;
2137
848k
    }
2138
161k
  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
13.6k
    return buffer;
2144
2145
  /* Critical bit we don't understand. */
2146
147k
  if (start)
2147
2.22k
    *start = -1;
2148
147k
  return NULL;  /* End of packets; not found.  */
2149
2150
139k
 too_short:
2151
139k
  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
139k
  if (start)
2160
801
    *start = -1;
2161
139k
  return NULL;
2162
2163
13.4k
 no_type_byte:
2164
13.4k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2165
13.4k
    log_info ("type octet missing in subpacket\n");
2166
13.4k
  if (start)
2167
221
    *start = -1;
2168
13.4k
  return NULL;
2169
161k
}
2170
2171
2172
const byte *
2173
parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype,
2174
      size_t *ret_n)
2175
411k
{
2176
411k
  return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL);
2177
411k
}
2178
2179
2180
const byte *
2181
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype)
2182
42.1k
{
2183
42.1k
  const byte *p;
2184
2185
42.1k
  p = parse_sig_subpkt (sig, 1, reqtype, NULL);
2186
42.1k
  if (!p)
2187
40.9k
    p = parse_sig_subpkt (sig, 0, reqtype, NULL);
2188
42.1k
  return p;
2189
42.1k
}
2190
2191
2192
/* Find all revocation keys.  Look in hashed area only.  */
2193
void
2194
parse_revkeys (PKT_signature * sig)
2195
1.61k
{
2196
1.61k
  const byte *revkey;
2197
1.61k
  int seq = 0;
2198
1.61k
  size_t len;
2199
2200
1.61k
  if (sig->sig_class != 0x1F)
2201
0
    return;
2202
2203
2.88k
  while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY,
2204
2.88k
                                    &len, &seq, NULL)))
2205
1.26k
    {
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.26k
      if ((len == 22 || len == 34)
2210
1.26k
          && (revkey[0] & 0x80))
2211
516
  {
2212
516
    sig->revkey = xrealloc (sig->revkey,
2213
516
          sizeof (struct revocation_key) *
2214
516
          (sig->numrevkeys + 1));
2215
2216
516
    sig->revkey[sig->numrevkeys].class = revkey[0];
2217
516
    sig->revkey[sig->numrevkeys].algid = revkey[1];
2218
516
          len -= 2;
2219
516
    sig->revkey[sig->numrevkeys].fprlen = len;
2220
516
    memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len);
2221
516
    memset (sig->revkey[sig->numrevkeys].fpr+len, 0,
2222
516
                  sizeof (sig->revkey[sig->numrevkeys].fpr) - len);
2223
516
    sig->numrevkeys++;
2224
516
  }
2225
1.26k
    }
2226
1.61k
}
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
65.1k
{
2237
65.1k
  int md5_len = 0;
2238
65.1k
  unsigned n;
2239
65.1k
  int is_v4or5 = 0;
2240
65.1k
  int rc = 0;
2241
65.1k
  int i, ndata;
2242
2243
65.1k
  if (pktlen < 16)
2244
6.87k
    {
2245
6.87k
      log_error ("packet(%d) too short\n", pkttype);
2246
6.87k
      if (list_mode)
2247
6.87k
        es_fputs (":signature packet: [too short]\n", listfp);
2248
6.87k
      goto leave;
2249
6.87k
    }
2250
58.2k
  sig->version = iobuf_get_noeof (inp);
2251
58.2k
  pktlen--;
2252
58.2k
  if (sig->version == 4 || sig->version == 5)
2253
23.7k
    is_v4or5 = 1;
2254
34.5k
  else if (sig->version != 2 && sig->version != 3)
2255
348
    {
2256
348
      log_error ("packet(%d) with unknown version %d\n",
2257
348
     pkttype, sig->version);
2258
348
      if (list_mode)
2259
348
        es_fputs (":signature packet: [unknown version]\n", listfp);
2260
348
      rc = gpg_error (GPG_ERR_INV_PACKET);
2261
348
      goto leave;
2262
348
    }
2263
2264
57.9k
  if (!is_v4or5)
2265
34.1k
    {
2266
34.1k
      if (pktlen == 0)
2267
0
  goto underflow;
2268
34.1k
      md5_len = iobuf_get_noeof (inp);
2269
34.1k
      pktlen--;
2270
34.1k
    }
2271
57.9k
  if (pktlen == 0)
2272
0
    goto underflow;
2273
57.9k
  sig->sig_class = iobuf_get_noeof (inp);
2274
57.9k
  pktlen--;
2275
57.9k
  if (!is_v4or5)
2276
34.1k
    {
2277
34.1k
      if (pktlen < 12)
2278
0
  goto underflow;
2279
34.1k
      sig->timestamp = read_32 (inp);
2280
34.1k
      pktlen -= 4;
2281
34.1k
      sig->keyid[0] = read_32 (inp);
2282
34.1k
      pktlen -= 4;
2283
34.1k
      sig->keyid[1] = read_32 (inp);
2284
34.1k
      pktlen -= 4;
2285
34.1k
    }
2286
57.9k
  if (pktlen < 2)
2287
7
    goto underflow;
2288
57.9k
  sig->pubkey_algo = iobuf_get_noeof (inp);
2289
57.9k
  pktlen--;
2290
57.9k
  sig->digest_algo = iobuf_get_noeof (inp);
2291
57.9k
  pktlen--;
2292
57.9k
  sig->flags.exportable = 1;
2293
57.9k
  sig->flags.revocable = 1;
2294
57.9k
  if (is_v4or5) /* Read subpackets.  */
2295
23.7k
    {
2296
23.7k
      if (pktlen < 2)
2297
0
  goto underflow;
2298
23.7k
      n = read_16 (inp);
2299
23.7k
      pktlen -= 2;  /* Length of hashed data. */
2300
23.7k
      if (pktlen < n)
2301
315
  goto underflow;
2302
23.4k
      if (n > 30000)
2303
53
  {
2304
53
    log_error ("signature packet: hashed data too long (%u)\n", n);
2305
53
          if (list_mode)
2306
53
            es_fprintf (listfp,
2307
0
                        ":signature packet: [hashed data too long (%u)]\n", n);
2308
53
          rc = GPG_ERR_INV_PACKET;
2309
53
    goto leave;
2310
53
  }
2311
23.3k
      if (n)
2312
22.9k
  {
2313
22.9k
    sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1);
2314
22.9k
    sig->hashed->size = n;
2315
22.9k
    sig->hashed->len = n;
2316
22.9k
    if (iobuf_read (inp, sig->hashed->data, n) != n)
2317
296
      {
2318
296
        log_error ("premature eof while reading "
2319
296
       "hashed signature data\n");
2320
296
              if (list_mode)
2321
296
                es_fputs (":signature packet: [premature eof]\n", listfp);
2322
296
        rc = -1;
2323
296
        goto leave;
2324
296
      }
2325
22.6k
    pktlen -= n;
2326
22.6k
  }
2327
23.0k
      if (pktlen < 2)
2328
3
  goto underflow;
2329
23.0k
      n = read_16 (inp);
2330
23.0k
      pktlen -= 2;  /* Length of unhashed data.  */
2331
23.0k
      if (pktlen < n)
2332
359
  goto underflow;
2333
22.7k
      if (n > 10000)
2334
35
  {
2335
35
    log_error ("signature packet: unhashed data too long (%u)\n", n);
2336
35
          if (list_mode)
2337
35
            es_fprintf (listfp,
2338
0
                        ":signature packet: [unhashed data too long (%u)]\n",
2339
0
                        n);
2340
35
          rc = GPG_ERR_INV_PACKET;
2341
35
    goto leave;
2342
35
  }
2343
22.6k
      if (n)
2344
10.7k
  {
2345
10.7k
    sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1);
2346
10.7k
    sig->unhashed->size = n;
2347
10.7k
    sig->unhashed->len = n;
2348
10.7k
    if (iobuf_read (inp, sig->unhashed->data, n) != n)
2349
58
      {
2350
58
        log_error ("premature eof while reading "
2351
58
       "unhashed signature data\n");
2352
58
              if (list_mode)
2353
58
                es_fputs (":signature packet: [premature eof]\n", listfp);
2354
58
        rc = -1;
2355
58
        goto leave;
2356
58
      }
2357
10.6k
    pktlen -= n;
2358
10.6k
  }
2359
22.6k
    }
2360
2361
56.8k
  if (pktlen < 2)
2362
8
    goto underflow;
2363
56.7k
  sig->digest_start[0] = iobuf_get_noeof (inp);
2364
56.7k
  pktlen--;
2365
56.7k
  sig->digest_start[1] = iobuf_get_noeof (inp);
2366
56.7k
  pktlen--;
2367
2368
56.7k
  if (is_v4or5 && sig->pubkey_algo)  /* Extract required information.  */
2369
22.5k
    {
2370
22.5k
      const byte *p;
2371
22.5k
      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
22.5k
      if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL)
2376
8.32k
    || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL))
2377
15.1k
  sig->flags.unknown_critical = 1;
2378
2379
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL);
2380
22.5k
      if (p)
2381
7.64k
  sig->timestamp = buf32_to_u32 (p);
2382
14.9k
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2383
13.3k
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2384
14.9k
        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
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len);
2390
22.5k
      if (p && len == 21 && p[0] == 4)
2391
3.02k
        {
2392
3.02k
          sig->keyid[0] = buf32_to_u32 (p + 1 + 12);
2393
3.02k
    sig->keyid[1] = buf32_to_u32 (p + 1 + 16);
2394
3.02k
  }
2395
19.5k
      else if (p && len == 33 && p[0] == 5)
2396
1
        {
2397
1
          sig->keyid[0] = buf32_to_u32 (p + 1 );
2398
1
    sig->keyid[1] = buf32_to_u32 (p + 1 + 4);
2399
1
  }
2400
19.5k
      else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER)))
2401
4.90k
        {
2402
4.90k
          sig->keyid[0] = buf32_to_u32 (p);
2403
4.90k
    sig->keyid[1] = buf32_to_u32 (p + 4);
2404
4.90k
  }
2405
14.6k
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2406
13.0k
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2407
14.6k
  log_info ("signature packet without keyid\n");
2408
2409
      /* Get the intended recipient (revocation subject) fpr. */
2410
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_INT_RCP_FPR, &len);
2411
22.5k
      if (p && len == 21 && p[0] == 4)
2412
1
        {
2413
1
          sig->rev_subject_info = xmalloc_clear (sizeof *sig->rev_subject_info);
2414
2415
1
          sig->rev_subject_info->fprlen = 20;
2416
1
          memcpy (sig->rev_subject_info->fpr, p + 1, 20);
2417
1
  }
2418
22.5k
      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
22.5k
      else
2426
22.5k
        {
2427
22.5k
         sig->rev_subject_info = NULL;
2428
22.5k
        }
2429
2430
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL);
2431
22.5k
      if (p && buf32_to_u32 (p))
2432
348
  sig->expiredate = sig->timestamp + buf32_to_u32 (p);
2433
22.5k
      if (sig->expiredate && sig->expiredate <= make_timestamp ())
2434
239
  sig->flags.expired = 1;
2435
2436
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL);
2437
22.5k
      if (p)
2438
513
  sig->flags.policy_url = 1;
2439
2440
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL);
2441
22.5k
      if (p)
2442
537
  sig->flags.pref_ks = 1;
2443
2444
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len);
2445
22.5k
      if (p && len)
2446
6.12k
        {
2447
6.12k
          char *mbox;
2448
2449
6.12k
          sig->signers_uid = try_make_printable_string (p, len, 0);
2450
6.12k
          if (!sig->signers_uid)
2451
0
            {
2452
0
              rc = gpg_error_from_syserror ();
2453
0
              goto leave;
2454
0
            }
2455
6.12k
          mbox = mailbox_from_userid (sig->signers_uid, 0);
2456
6.12k
          if (mbox)
2457
1.47k
            {
2458
1.47k
              xfree (sig->signers_uid);
2459
1.47k
              sig->signers_uid = mbox;
2460
1.47k
            }
2461
6.12k
        }
2462
2463
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL);
2464
22.5k
      if (p)
2465
2
        sig->flags.key_block = 1;
2466
2467
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL);
2468
22.5k
      if (p)
2469
118
  sig->flags.notation = 1;
2470
2471
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL);
2472
22.5k
      if (p && *p == 0)
2473
454
  sig->flags.revocable = 0;
2474
2475
22.5k
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len);
2476
22.5k
      if (p && len == 2)
2477
142
  {
2478
142
    sig->trust_depth = p[0];
2479
142
    sig->trust_value = p[1];
2480
2481
    /* Only look for a regexp if there is also a trust
2482
       subpacket. */
2483
142
    sig->trust_regexp =
2484
142
      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
142
    if (len == 0)
2489
73
      sig->trust_regexp = NULL;
2490
142
  }
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
22.5k
      p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE);
2498
22.5k
      if (p && *p == 0)
2499
528
  sig->flags.exportable = 0;
2500
2501
      /* Find all revocation keys.  */
2502
22.5k
      if (sig->sig_class == 0x1F)
2503
1.61k
  parse_revkeys (sig);
2504
22.5k
    }
2505
2506
56.7k
  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
56.7k
  ndata = pubkey_get_nsig (sig->pubkey_algo);
2523
56.7k
  if (!ndata)
2524
47.2k
    {
2525
47.2k
      if (list_mode)
2526
47.2k
  es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo);
2527
47.2k
      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
47.2k
      if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8))
2532
30
  {
2533
    /* We include a limit to avoid too trivial DoS attacks by
2534
       having gpg allocate too much memory.  */
2535
30
    log_error ("signature packet: too much data\n");
2536
30
    rc = GPG_ERR_INV_PACKET;
2537
30
  }
2538
47.2k
      else
2539
47.2k
  {
2540
47.2k
          void *tmpp;
2541
2542
47.2k
          tmpp = read_rest (inp, pktlen);
2543
47.2k
    sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2544
47.2k
    pktlen = 0;
2545
47.2k
  }
2546
47.2k
    }
2547
9.52k
  else
2548
9.52k
    {
2549
26.3k
      for (i = 0; i < ndata; i++)
2550
16.8k
  {
2551
16.8k
    n = pktlen;
2552
16.8k
          if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA
2553
15.3k
              || sig->pubkey_algo == PUBKEY_ALGO_EDDSA)
2554
7.93k
            sig->data[i] = sos_read (inp, &n, 0);
2555
8.88k
          else
2556
8.88k
            sig->data[i] = mpi_read (inp, &n, 0);
2557
16.8k
    pktlen -= n;
2558
16.8k
    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
16.8k
    if (!sig->data[i])
2565
2.46k
      rc = GPG_ERR_INV_PACKET;
2566
16.8k
  }
2567
9.52k
    }
2568
2569
64.4k
 leave:
2570
64.4k
  iobuf_skip_rest (inp, pktlen, 0);
2571
64.4k
  return rc;
2572
2573
692
 underflow:
2574
692
  log_error ("packet(%d) too short\n", pkttype);
2575
692
  if (list_mode)
2576
692
    es_fputs (":signature packet: [too short]\n", listfp);
2577
2578
692
  iobuf_skip_rest (inp, pktlen, 0);
2579
2580
692
  return GPG_ERR_INV_PACKET;
2581
56.7k
}
2582
2583
2584
static int
2585
parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen,
2586
       PKT_onepass_sig * ops)
2587
2.99k
{
2588
2.99k
  int version;
2589
2.99k
  int rc = 0;
2590
2591
2.99k
  if (pktlen < 13)
2592
2.56k
    {
2593
2.56k
      log_error ("packet(%d) too short\n", pkttype);
2594
2.56k
      if (list_mode)
2595
2.56k
        es_fputs (":onepass_sig packet: [too short]\n", listfp);
2596
2.56k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2597
2.56k
      goto leave;
2598
2.56k
    }
2599
433
  version = iobuf_get_noeof (inp);
2600
433
  pktlen--;
2601
433
  if (version != 3)
2602
76
    {
2603
76
      log_error ("onepass_sig with unknown version %d\n", version);
2604
76
      if (list_mode)
2605
76
        es_fputs (":onepass_sig packet: [unknown version]\n", listfp);
2606
76
      rc = gpg_error (GPG_ERR_INV_PACKET);
2607
76
      goto leave;
2608
76
    }
2609
357
  ops->sig_class = iobuf_get_noeof (inp);
2610
357
  pktlen--;
2611
357
  ops->digest_algo = iobuf_get_noeof (inp);
2612
357
  pktlen--;
2613
357
  ops->pubkey_algo = iobuf_get_noeof (inp);
2614
357
  pktlen--;
2615
357
  ops->keyid[0] = read_32 (inp);
2616
357
  pktlen -= 4;
2617
357
  ops->keyid[1] = read_32 (inp);
2618
357
  pktlen -= 4;
2619
357
  ops->last = iobuf_get_noeof (inp);
2620
357
  pktlen--;
2621
357
  if (list_mode)
2622
357
    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
2.99k
 leave:
2632
2.99k
  iobuf_skip_rest (inp, pktlen, 0);
2633
2.99k
  return rc;
2634
357
}
2635
2636
2637
static int
2638
parse_key (IOBUF inp, int pkttype, unsigned long pktlen,
2639
     byte * hdr, int hdrlen, PACKET * pkt)
2640
65.3k
{
2641
65.3k
  gpg_error_t err = 0;
2642
65.3k
  int i, version, algorithm;
2643
65.3k
  unsigned long timestamp, expiredate, max_expiredate;
2644
65.3k
  int npkey, nskey;
2645
65.3k
  u32 keyid[2];
2646
65.3k
  PKT_public_key *pk;
2647
65.3k
  int is_v5;
2648
65.3k
  unsigned int pkbytes; /* For v5 keys: Number of bytes in the public
2649
                         * key material.  For v4 keys: 0.  */
2650
2651
65.3k
  (void) hdr;
2652
2653
65.3k
  pk = pkt->pkt.public_key; /* PK has been cleared. */
2654
2655
65.3k
  version = iobuf_get_noeof (inp);
2656
65.3k
  pktlen--;
2657
65.3k
  if (pkttype == PKT_PUBLIC_SUBKEY && version == '#')
2658
4.56k
    {
2659
      /* Early versions of G10 used the old PGP comments packets;
2660
       * luckily all those comments are started by a hash.  */
2661
4.56k
      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
4.56k
      iobuf_skip_rest (inp, pktlen, 0);
2678
4.56k
      return 0;
2679
4.56k
    }
2680
60.8k
  else if (version == 4)
2681
43.5k
    is_v5 = 0;
2682
17.2k
  else if (version == 5)
2683
8.30k
    is_v5 = 1;
2684
8.95k
  else if (version == 2 || version == 3)
2685
640
    {
2686
      /* Not anymore supported since 2.1.  Use an older gpg version
2687
       * (i.e. gpg 1.4) to parse v3 packets.  */
2688
640
      if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2689
640
        log_info ("packet(%d) with obsolete version %d\n", pkttype, version);
2690
640
      if (list_mode)
2691
640
        es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version);
2692
640
      pk->version = version;
2693
640
      err = gpg_error (GPG_ERR_LEGACY_KEY);
2694
640
      goto leave;
2695
640
    }
2696
8.31k
  else
2697
8.31k
    {
2698
8.31k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
2699
8.31k
      if (list_mode)
2700
8.31k
        es_fputs (":key packet: [unknown version]\n", listfp);
2701
8.31k
      err = gpg_error (GPG_ERR_INV_PACKET);
2702
8.31k
      goto leave;
2703
8.31k
    }
2704
2705
51.8k
  if (pktlen < (is_v5? 15:11))
2706
20
    {
2707
20
      log_error ("packet(%d) too short\n", pkttype);
2708
20
      if (list_mode)
2709
20
        es_fputs (":key packet: [too short]\n", listfp);
2710
20
      err = gpg_error (GPG_ERR_INV_PACKET);
2711
20
      goto leave;
2712
20
    }
2713
51.8k
  else if (pktlen > MAX_KEY_PACKET_LENGTH)
2714
3.58k
    {
2715
3.58k
      log_error ("packet(%d) too large\n", pkttype);
2716
3.58k
      if (list_mode)
2717
3.58k
        es_fputs (":key packet: [too large]\n", listfp);
2718
3.58k
      err = gpg_error (GPG_ERR_INV_PACKET);
2719
3.58k
      goto leave;
2720
3.58k
    }
2721
2722
48.2k
  timestamp = read_32 (inp);
2723
48.2k
  pktlen -= 4;
2724
48.2k
  expiredate = 0;   /* have to get it from the selfsignature */
2725
48.2k
  max_expiredate = 0;
2726
48.2k
  algorithm = iobuf_get_noeof (inp);
2727
48.2k
  pktlen--;
2728
48.2k
  if (is_v5)
2729
8.27k
    {
2730
8.27k
      pkbytes = read_32 (inp);
2731
8.27k
      pktlen -= 4;
2732
8.27k
    }
2733
39.9k
  else
2734
39.9k
    pkbytes = 0;
2735
2736
48.2k
  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
48.2k
  pk->timestamp = timestamp;
2752
48.2k
  pk->expiredate = expiredate;
2753
48.2k
  pk->max_expiredate = max_expiredate;
2754
48.2k
  pk->hdrbytes = hdrlen;
2755
48.2k
  pk->version = version;
2756
48.2k
  pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY);
2757
48.2k
  pk->pubkey_algo = algorithm;
2758
2759
48.2k
  nskey = pubkey_get_nskey (algorithm);
2760
48.2k
  npkey = pubkey_get_npkey (algorithm);
2761
48.2k
  if (!npkey)
2762
22.3k
    {
2763
22.3k
      if (list_mode)
2764
22.3k
  es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm);
2765
22.3k
      unknown_pubkey_warning (algorithm);
2766
22.3k
    }
2767
2768
48.2k
  if (!npkey)
2769
22.3k
    {
2770
      /* Unknown algorithm - put data into an opaque MPI.  */
2771
22.3k
      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
22.3k
      pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2775
22.3k
      pktlen = 0;
2776
22.3k
      goto leave;
2777
22.3k
    }
2778
25.9k
  else
2779
25.9k
    {
2780
77.8k
      for (i = 0; i < npkey; i++)
2781
53.3k
        {
2782
53.3k
          if (    (algorithm == PUBKEY_ALGO_ECDSA && (i == 0))
2783
49.1k
               || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0))
2784
43.9k
               || (algorithm == PUBKEY_ALGO_ECDH  && (i == 0 || i == 2))
2785
43.1k
               || (algorithm == PUBKEY_ALGO_KYBER && (i == 0)))
2786
10.2k
            {
2787
              /* Read the OID (i==0) or the KDF params (i==2).  */
2788
10.2k
        err = read_sized_octet_string (inp, &pktlen, pk->pkey+i);
2789
10.2k
            }
2790
43.1k
          else if (algorithm == PUBKEY_ALGO_KYBER && i == 2)
2791
1
            {
2792
              /* Read the four-octet count prefixed Kyber public key.  */
2793
1
        err = read_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i);
2794
1
            }
2795
43.1k
          else
2796
43.1k
            {
2797
              /* Read MPI or SOS.  */
2798
43.1k
              unsigned int n = pktlen;
2799
43.1k
              if (algorithm == PUBKEY_ALGO_ECDSA
2800
39.0k
                  || algorithm == PUBKEY_ALGO_EDDSA
2801
33.8k
                  || algorithm == PUBKEY_ALGO_ECDH
2802
33.4k
                  || algorithm == PUBKEY_ALGO_KYBER)
2803
9.72k
                pk->pkey[i] = sos_read (inp, &n, 0);
2804
33.4k
              else
2805
33.4k
                pk->pkey[i] = mpi_read (inp, &n, 0);
2806
43.1k
              pktlen -= n;
2807
43.1k
              if (!pk->pkey[i])
2808
1.31k
                err = gpg_error (GPG_ERR_INV_PACKET);
2809
43.1k
            }
2810
53.3k
          if (err)
2811
1.40k
            goto leave;
2812
53.3k
        }
2813
24.5k
      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
24.5k
    }
2842
24.5k
  if (list_mode)
2843
0
    keyid_from_pk (pk, keyid);
2844
2845
24.5k
  if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY)
2846
12.3k
    {
2847
12.3k
      struct seckey_info *ski;
2848
12.3k
      byte temp[16];
2849
12.3k
      size_t snlen = 0;
2850
12.3k
      unsigned int skbytes;
2851
2852
12.3k
      if (pktlen < 1)
2853
3
        {
2854
3
          err = gpg_error (GPG_ERR_INV_PACKET);
2855
3
          goto leave;
2856
3
        }
2857
2858
12.3k
      pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
2859
12.3k
      if (!pk->seckey_info)
2860
0
        {
2861
0
          err = gpg_error_from_syserror ();
2862
0
          goto leave;
2863
0
        }
2864
2865
12.3k
      ski->algo = iobuf_get_noeof (inp);
2866
12.3k
      pktlen--;
2867
2868
12.3k
      if (is_v5)
2869
1.40k
        {
2870
1.40k
          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
1.40k
          if (!pktlen)
2875
44
            {
2876
44
              err = gpg_error (GPG_ERR_INV_PACKET);
2877
44
              goto leave;
2878
44
            }
2879
1.35k
          protcount = iobuf_get_noeof (inp);
2880
1.35k
          pktlen--;
2881
1.35k
          if (list_mode)
2882
1.35k
            es_fprintf (listfp, "\tprotbytes: %u\n", protcount);
2883
1.35k
        }
2884
2885
12.3k
      if (ski->algo)
2886
11.6k
  {
2887
11.6k
    ski->is_protected = 1;
2888
11.6k
    ski->s2k.count = 0;
2889
11.6k
    if (ski->algo == 253)
2890
3
      {
2891
3
              if (list_mode)
2892
3
                es_fprintf (listfp,
2893
0
                            "\tS2K pseudo algo %d is not yet supported\n",
2894
0
                            ski->algo);
2895
3
              err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2896
3
              goto leave;
2897
3
            }
2898
11.6k
          else if (ski->algo == 254 || ski->algo == 255)
2899
9.91k
      {
2900
9.91k
              if (pktlen < 3)
2901
5
    {
2902
5
      err = gpg_error (GPG_ERR_INV_PACKET);
2903
5
      goto leave;
2904
5
    }
2905
2906
9.90k
              ski->sha1chk = (ski->algo == 254);
2907
9.90k
        ski->algo = iobuf_get_noeof (inp);
2908
9.90k
        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
9.90k
        ski->s2k.mode = iobuf_get_noeof (inp);
2913
9.90k
        pktlen--;
2914
9.90k
        ski->s2k.hash_algo = iobuf_get_noeof (inp);
2915
9.90k
        pktlen--;
2916
        /* Check for the special GNU extension.  */
2917
9.90k
        if (ski->s2k.mode == 101)
2918
8.99k
    {
2919
44.7k
      for (i = 0; i < 4 && pktlen; i++, pktlen--)
2920
35.7k
        temp[i] = iobuf_get_noeof (inp);
2921
8.99k
      if (i < 4 || memcmp (temp, "GNU", 3))
2922
261
        {
2923
261
          if (list_mode)
2924
261
      es_fprintf (listfp, "\tunknown S2K %d\n",
2925
0
                                    ski->s2k.mode);
2926
261
          err = gpg_error (GPG_ERR_INV_PACKET);
2927
261
          goto leave;
2928
261
        }
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
8.73k
      ski->s2k.mode = 1000 + temp[3];
2934
8.73k
    }
2935
2936
              /* Read the salt.  */
2937
9.64k
        if (ski->s2k.mode == 3 || ski->s2k.mode == 1)
2938
802
    {
2939
3.96k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
2940
3.16k
        temp[i] = iobuf_get_noeof (inp);
2941
802
                  if (i < 8)
2942
407
                    {
2943
407
          err = gpg_error (GPG_ERR_INV_PACKET);
2944
407
          goto leave;
2945
407
                    }
2946
395
      memcpy (ski->s2k.salt, temp, 8);
2947
395
    }
2948
2949
              /* Check the mode.  */
2950
9.23k
        switch (ski->s2k.mode)
2951
9.23k
    {
2952
34
    case 0:
2953
34
      if (list_mode)
2954
34
        es_fprintf (listfp, "\tsimple S2K");
2955
34
      break;
2956
9
    case 1:
2957
9
      if (list_mode)
2958
9
        es_fprintf (listfp, "\tsalted S2K");
2959
9
      break;
2960
386
    case 3:
2961
386
      if (list_mode)
2962
386
        es_fprintf (listfp, "\titer+salt S2K");
2963
386
      break;
2964
350
    case 1001:
2965
350
      if (list_mode)
2966
350
        es_fprintf (listfp, "\tgnu-dummy");
2967
350
      break;
2968
7.67k
    case 1002:
2969
7.67k
      if (list_mode)
2970
7.67k
        es_fprintf (listfp, "\tgnu-divert-to-card");
2971
7.67k
      break;
2972
671
    case 1003:
2973
671
      if (list_mode)
2974
671
        es_fprintf (listfp, "\tgnu-mode1003");
2975
671
      break;
2976
109
    default:
2977
109
      if (list_mode)
2978
109
        es_fprintf (listfp, "\tunknown %sS2K %d\n",
2979
0
                                ski->s2k.mode < 1000 ? "" : "GNU ",
2980
0
                                ski->s2k.mode);
2981
109
      err = gpg_error (GPG_ERR_INV_PACKET);
2982
109
      goto leave;
2983
9.23k
    }
2984
2985
              /* Print some info.  */
2986
9.12k
        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
9.12k
              if (list_mode)
2999
9.12k
                es_putc ('\n', listfp);
3000
3001
              /* Read remaining protection parameters.  */
3002
9.12k
        if (ski->s2k.mode == 3)
3003
386
    {
3004
386
      if (pktlen < 1)
3005
164
        {
3006
164
          err = gpg_error (GPG_ERR_INV_PACKET);
3007
164
          goto leave;
3008
164
        }
3009
222
      ski->s2k.count = iobuf_get_noeof (inp);
3010
222
      pktlen--;
3011
222
      if (list_mode)
3012
222
        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
222
    }
3016
8.74k
        else if (ski->s2k.mode == 1002)
3017
7.67k
    {
3018
      /* Read the serial number. */
3019
7.67k
      if (pktlen < 1)
3020
74
        {
3021
74
          err = gpg_error (GPG_ERR_INV_PACKET);
3022
74
          goto leave;
3023
74
        }
3024
7.60k
      snlen = iobuf_get (inp);
3025
7.60k
      pktlen--;
3026
7.60k
      if (pktlen < snlen || snlen == (size_t)(-1))
3027
2.01k
        {
3028
2.01k
          err = gpg_error (GPG_ERR_INV_PACKET);
3029
2.01k
          goto leave;
3030
2.01k
        }
3031
7.60k
    }
3032
9.12k
      }
3033
1.71k
    else /* Old version; no S2K, so we set mode to 0, hash MD5.  */
3034
1.71k
      {
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
1.71k
        ski->s2k.mode = 0;
3039
1.71k
        ski->s2k.hash_algo = DIGEST_ALGO_MD5;
3040
1.71k
        if (list_mode)
3041
1.71k
    es_fprintf (listfp, "\tprotect algo: %d  (hash algo: %d)\n",
3042
0
                            ski->algo, ski->s2k.hash_algo);
3043
1.71k
      }
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
8.58k
    ski->ivlen = openpgp_cipher_blocklen (ski->algo);
3055
8.58k
    log_assert (ski->ivlen <= sizeof (temp));
3056
3057
8.58k
    if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003)
3058
1.02k
      ski->ivlen = 0;
3059
7.56k
    else if (ski->s2k.mode == 1002)
3060
5.58k
      ski->ivlen = snlen < 16 ? snlen : 16;
3061
3062
8.58k
    if (pktlen < ski->ivlen)
3063
208
      {
3064
208
              err = gpg_error (GPG_ERR_INV_PACKET);
3065
208
        goto leave;
3066
208
      }
3067
58.4k
    for (i = 0; i < ski->ivlen; i++, pktlen--)
3068
50.0k
      temp[i] = iobuf_get_noeof (inp);
3069
8.37k
    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
8.37k
    memcpy (ski->iv, temp, ski->ivlen);
3079
8.37k
  }
3080
3081
      /* Skip count of secret key material.  */
3082
9.06k
      if (is_v5)
3083
1.33k
        {
3084
1.33k
          if (pktlen < 4)
3085
88
            {
3086
88
              err = gpg_error (GPG_ERR_INV_PACKET);
3087
88
              goto leave;
3088
88
            }
3089
1.25k
          skbytes = read_32 (inp);
3090
1.25k
          pktlen -= 4;
3091
1.25k
          if (list_mode)
3092
1.25k
            es_fprintf (listfp, "\tskbytes: %u\n", skbytes);
3093
1.25k
        }
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
8.97k
      if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002)
3100
5.93k
  {
3101
    /* Better set some dummy stuff here.  */
3102
5.93k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3103
5.93k
             xstrdup ("dummydata"),
3104
5.93k
             10 * 8);
3105
5.93k
    pktlen = 0;
3106
5.93k
  }
3107
3.04k
      else if (ski->s2k.mode == 1003)
3108
671
  {
3109
671
          void *tmpp;
3110
3111
671
    if (pktlen < 2) /* At least two bytes for parenthesis.  */
3112
438
      {
3113
438
              err = gpg_error (GPG_ERR_INV_PACKET);
3114
438
        goto leave;
3115
438
      }
3116
3117
233
          tmpp = read_rest (inp, pktlen);
3118
233
          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
233
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3134
233
             tmpp, tmpp? pktlen * 8 : 0);
3135
233
          pktlen = 0;
3136
233
  }
3137
2.36k
      else if (ski->is_protected)
3138
1.75k
  {
3139
1.75k
          void *tmpp;
3140
3141
1.75k
    if (pktlen < 2) /* At least two bytes for the length.  */
3142
1.19k
      {
3143
1.19k
              err = gpg_error (GPG_ERR_INV_PACKET);
3144
1.19k
        goto leave;
3145
1.19k
      }
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
564
          tmpp = read_rest (inp, pktlen);
3153
564
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3154
564
             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
564
          if (pk->pkey[npkey])
3159
564
            gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1);
3160
564
    pktlen = 0;
3161
564
    if (list_mode)
3162
564
            es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey);
3163
564
  }
3164
610
      else
3165
610
  {
3166
          /* Not encrypted.  */
3167
2.84k
    for (i = npkey; i < nskey; i++)
3168
2.29k
      {
3169
3170
2.29k
              if (pktlen < 2) /* At least two bytes for the length.  */
3171
55
                {
3172
55
                  err = gpg_error (GPG_ERR_INV_PACKET);
3173
55
                  goto leave;
3174
55
                }
3175
2.23k
              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
2.23k
              else
3182
2.23k
                {
3183
2.23k
                  unsigned int n = pktlen;
3184
3185
2.23k
                  if (algorithm == PUBKEY_ALGO_ECDSA
3186
2.23k
                      || algorithm == PUBKEY_ALGO_EDDSA
3187
2.23k
                      || algorithm == PUBKEY_ALGO_ECDH
3188
2.23k
                      || algorithm == PUBKEY_ALGO_KYBER)
3189
2
                    pk->pkey[i] = sos_read (inp, &n, 0);
3190
2.23k
                  else
3191
2.23k
                    pk->pkey[i] = mpi_read (inp, &n, 0);
3192
2.23k
                  pktlen -= n;
3193
2.23k
                }
3194
3195
2.23k
              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
2.23k
        if (!pk->pkey[i])
3203
292
    err = gpg_error (GPG_ERR_INV_PACKET);
3204
2.23k
      }
3205
555
    if (err)
3206
75
      goto leave;
3207
3208
480
    if (pktlen < 2)
3209
3
      {
3210
3
              if (opt.verbose)
3211
3
                log_info ("checksum is missing (remaining bytes: %lu)\n",
3212
0
                          pktlen);
3213
3
              err = gpg_error (GPG_ERR_INV_PACKET);
3214
3
        goto leave;
3215
3
      }
3216
477
    ski->csum = read_16 (inp);
3217
477
    pktlen -= 2;
3218
477
    if (list_mode)
3219
477
            es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum);
3220
477
  }
3221
8.97k
    }
3222
3223
  /* Note that KEYID below has been initialized above in list_mode.  */
3224
19.3k
  if (list_mode)
3225
19.3k
    es_fprintf (listfp, "\tkeyid: %08lX%08lX\n",
3226
0
                (ulong) keyid[0], (ulong) keyid[1]);
3227
3228
60.8k
 leave:
3229
60.8k
  iobuf_skip_rest (inp, pktlen, 0);
3230
60.8k
  return err;
3231
19.3k
}
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
12.1k
{
3240
12.1k
  size_t n;
3241
12.1k
  int count = 0;
3242
12.1k
  struct user_attribute *attribs = NULL;
3243
12.1k
  const byte *buffer = uid->attrib_data;
3244
12.1k
  int buflen = uid->attrib_len;
3245
12.1k
  byte type;
3246
3247
12.1k
  xfree (uid->attribs);
3248
3249
15.2k
  while (buflen)
3250
6.29k
    {
3251
6.29k
      n = *buffer++;
3252
6.29k
      buflen--;
3253
6.29k
      if (n == 255)  /* 4 byte length header.  */
3254
329
  {
3255
329
    if (buflen < 4)
3256
157
      goto too_short;
3257
172
    n = buf32_to_size_t (buffer);
3258
172
    buffer += 4;
3259
172
    buflen -= 4;
3260
172
  }
3261
5.96k
      else if (n >= 192)  /* 2 byte special encoded length header.  */
3262
1.37k
  {
3263
1.37k
    if (buflen < 2)
3264
1.14k
      goto too_short;
3265
236
    n = ((n - 192) << 8) + *buffer + 192;
3266
236
    buffer++;
3267
236
    buflen--;
3268
236
  }
3269
4.99k
      if (buflen < n)
3270
1.79k
  goto too_short;
3271
3272
3.19k
      if (!n)
3273
137
        {
3274
          /* Too short to encode the subpacket type.  */
3275
137
          if (opt.verbose)
3276
137
            log_info ("attribute subpacket too short\n");
3277
137
          break;
3278
137
        }
3279
3280
3.06k
      attribs = xrealloc (attribs,
3281
3.06k
                          (count + 1) * sizeof (struct user_attribute));
3282
3.06k
      memset (&attribs[count], 0, sizeof (struct user_attribute));
3283
3284
3.06k
      type = *buffer;
3285
3.06k
      buffer++;
3286
3.06k
      buflen--;
3287
3.06k
      n--;
3288
3289
3.06k
      attribs[count].type = type;
3290
3.06k
      attribs[count].data = buffer;
3291
3.06k
      attribs[count].len = n;
3292
3.06k
      buffer += n;
3293
3.06k
      buflen -= n;
3294
3.06k
      count++;
3295
3.06k
    }
3296
3297
9.09k
  uid->attribs = attribs;
3298
9.09k
  uid->numattribs = count;
3299
9.09k
  return count;
3300
3301
3.09k
 too_short:
3302
3.09k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
3303
3.09k
    log_info ("buffer shorter than attribute subpacket\n");
3304
3.09k
  uid->attribs = attribs;
3305
3.09k
  uid->numattribs = count;
3306
3.09k
  return count;
3307
12.1k
}
3308
3309
3310
static int
3311
parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3312
136k
{
3313
136k
  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
136k
  if (pktlen > MAX_UID_PACKET_LENGTH)
3323
1.28k
    {
3324
1.28k
      log_error ("packet(%d) too large\n", pkttype);
3325
1.28k
      if (list_mode)
3326
1.28k
        es_fprintf (listfp, ":user ID packet: [too large]\n");
3327
1.28k
      iobuf_skip_rest (inp, pktlen, 0);
3328
1.28k
      return GPG_ERR_INV_PACKET;
3329
1.28k
    }
3330
3331
135k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen);
3332
135k
  packet->pkt.user_id->len = pktlen;
3333
135k
  packet->pkt.user_id->ref = 1;
3334
3335
135k
  p = packet->pkt.user_id->name;
3336
398k
  for (; pktlen; pktlen--, p++)
3337
263k
    *p = iobuf_get_noeof (inp);
3338
135k
  *p = 0;
3339
3340
135k
  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
135k
  return 0;
3355
136k
}
3356
3357
3358
void
3359
make_attribute_uidname (PKT_user_id * uid, size_t max_namelen)
3360
12.1k
{
3361
12.1k
  log_assert (max_namelen > 70);
3362
12.1k
  if (uid->numattribs <= 0)
3363
9.49k
    sprintf (uid->name, "[bad attribute packet of size %lu]",
3364
9.49k
       uid->attrib_len);
3365
2.69k
  else if (uid->numattribs > 1)
3366
302
    sprintf (uid->name, "[%d attributes of size %lu]",
3367
302
       uid->numattribs, uid->attrib_len);
3368
2.39k
  else
3369
2.39k
    {
3370
      /* Only one attribute, so list it as the "user id" */
3371
3372
2.39k
      if (uid->attribs->type == ATTRIB_IMAGE)
3373
1.76k
  {
3374
1.76k
    u32 len;
3375
1.76k
    byte type;
3376
3377
1.76k
    if (parse_image_header (uid->attribs, &type, &len))
3378
837
      sprintf (uid->name, "[%.20s image of size %lu]",
3379
837
         image_type_to_string (type, 1), (ulong) len);
3380
925
    else
3381
925
      sprintf (uid->name, "[invalid image]");
3382
1.76k
  }
3383
630
      else
3384
630
  sprintf (uid->name, "[unknown attribute of size %lu]",
3385
630
     (ulong) uid->attribs->len);
3386
2.39k
    }
3387
3388
12.1k
  uid->len = strlen (uid->name);
3389
12.1k
}
3390
3391
3392
static int
3393
parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen,
3394
     PACKET * packet)
3395
12.2k
{
3396
12.2k
  byte *p;
3397
3398
12.2k
  (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
12.2k
  if (pktlen > MAX_ATTR_PACKET_LENGTH)
3404
23
    {
3405
23
      log_error ("packet(%d) too large\n", pkttype);
3406
23
      if (list_mode)
3407
23
        es_fprintf (listfp, ":attribute packet: [too large]\n");
3408
23
      iobuf_skip_rest (inp, pktlen, 0);
3409
23
      return GPG_ERR_INV_PACKET;
3410
23
    }
3411
3412
12.1k
#define EXTRA_UID_NAME_SPACE 71
3413
12.1k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id
3414
12.1k
               + EXTRA_UID_NAME_SPACE);
3415
12.1k
  packet->pkt.user_id->ref = 1;
3416
12.1k
  packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1);
3417
12.1k
  packet->pkt.user_id->attrib_len = pktlen;
3418
3419
12.1k
  p = packet->pkt.user_id->attrib_data;
3420
464M
  for (; pktlen; pktlen--, p++)
3421
464M
    *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
12.1k
  parse_attribute_subpkts (packet->pkt.user_id);
3427
3428
12.1k
  make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE);
3429
3430
12.1k
  if (list_mode)
3431
0
    {
3432
0
      es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name);
3433
0
    }
3434
12.1k
  return 0;
3435
12.2k
}
3436
3437
3438
static int
3439
parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3440
2.45k
{
3441
2.45k
  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
2.45k
  if (pktlen > MAX_COMMENT_PACKET_LENGTH)
3448
11
    {
3449
11
      log_error ("packet(%d) too large\n", pkttype);
3450
11
      if (list_mode)
3451
11
        es_fprintf (listfp, ":%scomment packet: [too large]\n",
3452
0
                    pkttype == PKT_OLD_COMMENT ? "OpenPGP draft " : "");
3453
11
      iobuf_skip_rest (inp, pktlen, 0);
3454
11
      return GPG_ERR_INV_PACKET;
3455
11
    }
3456
2.44k
  packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
3457
2.44k
  packet->pkt.comment->len = pktlen;
3458
2.44k
  p = packet->pkt.comment->data;
3459
129k
  for (; pktlen; pktlen--, p++)
3460
127k
    *p = iobuf_get_noeof (inp);
3461
3462
2.44k
  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
2.44k
  return 0;
3477
2.45k
}
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
37.5k
{
3487
37.5k
  gpg_error_t err;
3488
37.5k
  iobuf_t inp = ctx->inp;
3489
37.5k
  PKT_ring_trust rt = {0};
3490
37.5k
  int c;
3491
37.5k
  int not_gpg = 0;
3492
3493
37.5k
  if (!pktlen)
3494
2.77k
    {
3495
2.77k
      if (list_mode)
3496
2.77k
  es_fprintf (listfp, ":trust packet: empty\n");
3497
2.77k
      err = 0;
3498
2.77k
      goto leave;
3499
2.77k
    }
3500
3501
34.7k
  c = iobuf_get_noeof (inp);
3502
34.7k
  pktlen--;
3503
34.7k
  rt.trustval = c;
3504
34.7k
  if (pktlen)
3505
34.6k
    {
3506
34.6k
      if (!c)
3507
18.9k
        {
3508
18.9k
          c = iobuf_get_noeof (inp);
3509
          /* We require that bit 7 of the sigcache is 0 (easier
3510
           * eof handling).  */
3511
18.9k
          if (!(c & 0x80))
3512
18.8k
            rt.sigcache = c;
3513
18.9k
        }
3514
15.6k
      else
3515
15.6k
        iobuf_get_noeof (inp);  /* Dummy read.  */
3516
34.6k
      pktlen--;
3517
34.6k
    }
3518
3519
  /* Next is the optional subtype.  */
3520
34.7k
  if (pktlen > 3)
3521
31.5k
    {
3522
31.5k
      char tmp[4];
3523
31.5k
      tmp[0] = iobuf_get_noeof (inp);
3524
31.5k
      tmp[1] = iobuf_get_noeof (inp);
3525
31.5k
      tmp[2] = iobuf_get_noeof (inp);
3526
31.5k
      tmp[3] = iobuf_get_noeof (inp);
3527
31.5k
      pktlen -= 4;
3528
31.5k
      if (!memcmp (tmp, "gpg", 3))
3529
12.7k
        rt.subtype = tmp[3];
3530
18.8k
      else
3531
18.8k
        not_gpg = 1;
3532
31.5k
    }
3533
  /* If it is a key or uid subtype read the remaining data.  */
3534
34.7k
  if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID)
3535
12.7k
      && pktlen >= 6 )
3536
6.04k
    {
3537
6.04k
      int i;
3538
6.04k
      unsigned int namelen;
3539
3540
6.04k
      rt.keyorg = iobuf_get_noeof (inp);
3541
6.04k
      pktlen--;
3542
6.04k
      rt.keyupdate = read_32 (inp);
3543
6.04k
      pktlen -= 4;
3544
6.04k
      namelen = iobuf_get_noeof (inp);
3545
6.04k
      pktlen--;
3546
6.04k
      if (namelen && pktlen)
3547
4.31k
        {
3548
4.31k
          rt.url = xtrymalloc (namelen + 1);
3549
4.31k
          if (!rt.url)
3550
0
            {
3551
0
              err = gpg_error_from_syserror ();
3552
0
              goto leave;
3553
0
            }
3554
14.7k
          for (i = 0; pktlen && i < namelen; pktlen--, i++)
3555
10.3k
            rt.url[i] = iobuf_get_noeof (inp);
3556
4.31k
          rt.url[i] = 0;
3557
4.31k
        }
3558
6.04k
    }
3559
3560
34.7k
  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
34.7k
  if (!ctx->last_pkt.pkt.generic || ctx->skip_meta)
3596
2.84k
    ;
3597
31.9k
  else if (rt.subtype == RING_TRUST_SIG
3598
20.8k
           && ctx->last_pkt.pkttype == PKT_SIGNATURE)
3599
18.8k
    {
3600
18.8k
      PKT_signature *sig = ctx->last_pkt.pkt.signature;
3601
3602
18.8k
      if ((rt.sigcache & 1))
3603
18.2k
        {
3604
18.2k
          sig->flags.checked = 1;
3605
18.2k
          sig->flags.valid = !!(rt.sigcache & 2);
3606
18.2k
        }
3607
18.8k
    }
3608
13.1k
  else if (rt.subtype == RING_TRUST_UID
3609
3.04k
           && (ctx->last_pkt.pkttype == PKT_USER_ID
3610
1
               || ctx->last_pkt.pkttype == PKT_ATTRIBUTE))
3611
3.04k
    {
3612
3.04k
      PKT_user_id *uid = ctx->last_pkt.pkt.user_id;
3613
3614
3.04k
      uid->keyorg = rt.keyorg;
3615
3.04k
      uid->keyupdate = rt.keyupdate;
3616
3.04k
      uid->updateurl = rt.url;
3617
3.04k
      rt.url = NULL;
3618
3.04k
    }
3619
10.0k
  else if (rt.subtype == RING_TRUST_KEY
3620
8.02k
           && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY
3621
6.53k
               || ctx->last_pkt.pkttype == PKT_SECRET_KEY))
3622
5.49k
    {
3623
5.49k
      PKT_public_key *pk = ctx->last_pkt.pkt.public_key;
3624
3625
5.49k
      pk->keyorg = rt.keyorg;
3626
5.49k
      pk->keyupdate = rt.keyupdate;
3627
5.49k
      pk->updateurl = rt.url;
3628
5.49k
      rt.url = NULL;
3629
5.49k
    }
3630
3631
34.7k
  err = 0;
3632
3633
37.5k
 leave:
3634
37.5k
  xfree (rt.url);
3635
37.5k
  free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL.  */
3636
37.5k
  iobuf_skip_rest (inp, pktlen, 0);
3637
37.5k
  return err;
3638
34.7k
}
3639
3640
3641
static int
3642
parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen,
3643
     PACKET * pkt, int new_ctb, int partial)
3644
13.7k
{
3645
13.7k
  int rc = 0;
3646
13.7k
  int mode, namelen;
3647
13.7k
  PKT_plaintext *pt;
3648
13.7k
  byte *p;
3649
13.7k
  int c, i;
3650
3651
13.7k
  if (!partial && pktlen < 6)
3652
263
    {
3653
263
      log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen);
3654
263
      if (list_mode)
3655
263
        es_fputs (":literal data packet: [too short]\n", listfp);
3656
263
      rc = gpg_error (GPG_ERR_INV_PACKET);
3657
263
      goto leave;
3658
263
    }
3659
13.5k
  mode = iobuf_get_noeof (inp);
3660
13.5k
  if (pktlen)
3661
10.9k
    pktlen--;
3662
13.5k
  namelen = iobuf_get_noeof (inp);
3663
13.5k
  if (pktlen)
3664
10.9k
    pktlen--;
3665
  /* Note that namelen will never exceed 255 bytes. */
3666
13.5k
  pt = pkt->pkt.plaintext =
3667
13.5k
    xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1);
3668
13.5k
  pt->new_ctb = new_ctb;
3669
13.5k
  pt->mode = mode;
3670
13.5k
  pt->namelen = namelen;
3671
13.5k
  pt->is_partial = partial;
3672
13.5k
  if (pktlen)
3673
10.9k
    {
3674
338k
      for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++)
3675
327k
  pt->name[i] = iobuf_get_noeof (inp);
3676
10.9k
    }
3677
2.60k
  else
3678
2.60k
    {
3679
25.1k
      for (i = 0; i < namelen; i++)
3680
22.9k
  if ((c = iobuf_get (inp)) == -1)
3681
383
    break;
3682
22.5k
  else
3683
22.5k
    pt->name[i] = c;
3684
2.60k
    }
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
511k
  for (; i < namelen; i++)
3689
497k
    pt->name[i] = 0;
3690
3691
13.5k
  pt->timestamp = read_32 (inp);
3692
13.5k
  if (pktlen)
3693
10.9k
    pktlen -= 4;
3694
13.5k
  pt->len = pktlen;
3695
13.5k
  pt->buf = inp;
3696
3697
13.5k
  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
13.7k
 leave:
3718
13.7k
  return rc;
3719
13.5k
}
3720
3721
3722
static int
3723
parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen,
3724
      PACKET * pkt, int new_ctb)
3725
116k
{
3726
116k
  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
116k
  (void) pkttype;
3732
116k
  (void) pktlen;
3733
3734
116k
  zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed);
3735
116k
  zd->algorithm = iobuf_get_noeof (inp);
3736
116k
  zd->len = 0;      /* not used */
3737
116k
  zd->new_ctb = new_ctb;
3738
116k
  zd->buf = inp;
3739
116k
  if (list_mode)
3740
116k
    es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
3741
116k
  return 0;
3742
116k
}
3743
3744
3745
static int
3746
parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen,
3747
     PACKET * pkt, int new_ctb, int partial)
3748
57.5k
{
3749
57.5k
  int rc = 0;
3750
57.5k
  PKT_encrypted *ed;
3751
57.5k
  unsigned long orig_pktlen = pktlen;
3752
3753
57.5k
  ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted);
3754
  /* ed->len is set below.  */
3755
57.5k
  ed->extralen = 0;  /* Unknown here; only used in build_packet.  */
3756
57.5k
  ed->buf = NULL;
3757
57.5k
  ed->new_ctb = new_ctb;
3758
57.5k
  ed->is_partial = partial;
3759
57.5k
  ed->aead_algo = 0;
3760
57.5k
  ed->cipher_algo = 0; /* Only used with AEAD.  */
3761
57.5k
  ed->chunkbyte = 0;   /* Only used with AEAD.  */
3762
57.5k
  if (pkttype == PKT_ENCRYPTED_MDC)
3763
1.53k
    {
3764
      /* Fixme: add some pktlen sanity checks.  */
3765
1.53k
      int version;
3766
3767
1.53k
      version = iobuf_get_noeof (inp);
3768
1.53k
      if (orig_pktlen)
3769
348
  pktlen--;
3770
1.53k
      if (version != 1)
3771
392
  {
3772
392
    log_error ("encrypted_mdc packet with unknown version %d\n",
3773
392
         version);
3774
392
          if (list_mode)
3775
392
            es_fputs (":encrypted data packet: [unknown version]\n", listfp);
3776
    /*skip_rest(inp, pktlen); should we really do this? */
3777
392
    rc = gpg_error (GPG_ERR_INV_PACKET);
3778
392
    goto leave;
3779
392
  }
3780
1.14k
      ed->mdc_method = DIGEST_ALGO_SHA1;
3781
1.14k
    }
3782
55.9k
  else
3783
55.9k
    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
57.1k
  if (orig_pktlen && pktlen < 10)
3789
92
    {
3790
      /* Actually this is blocksize+2.  */
3791
92
      log_error ("packet(%d) too short\n", pkttype);
3792
92
      if (list_mode)
3793
92
        es_fputs (":encrypted data packet: [too short]\n", listfp);
3794
92
      rc = GPG_ERR_INV_PACKET;
3795
92
      iobuf_skip_rest (inp, pktlen, partial);
3796
92
      goto leave;
3797
92
    }
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
57.0k
  ed->len = pktlen;
3803
3804
57.0k
  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
57.0k
  ed->buf = inp;
3816
3817
57.5k
 leave:
3818
57.5k
  return rc;
3819
57.0k
}
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
7.02k
{
3829
7.02k
  int rc = 0;
3830
7.02k
  PKT_mdc *mdc;
3831
7.02k
  byte *p;
3832
3833
7.02k
  (void) pkttype;
3834
3835
7.02k
  mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc);
3836
7.02k
  if (list_mode)
3837
7.02k
    es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
3838
7.02k
  if (!new_ctb || pktlen != 20)
3839
264
    {
3840
264
      log_error ("mdc_packet with invalid encoding\n");
3841
264
      rc = gpg_error (GPG_ERR_INV_PACKET);
3842
264
      goto leave;
3843
264
    }
3844
6.76k
  p = mdc->hash;
3845
141k
  for (; pktlen; pktlen--, p++)
3846
135k
    *p = iobuf_get_noeof (inp);
3847
3848
7.02k
 leave:
3849
7.02k
  return rc;
3850
6.76k
}
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
93.0k
{
3857
93.0k
  int rc = 0;
3858
93.0k
  PKT_encrypted *ed;
3859
93.0k
  unsigned long orig_pktlen = pktlen;
3860
93.0k
  int version;
3861
3862
93.0k
  ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted);
3863
93.0k
  if (!ed)
3864
0
    return gpg_error_from_syserror ();
3865
93.0k
  ed->len = 0;
3866
93.0k
  ed->extralen = 0;  /* (only used in build_packet.)  */
3867
93.0k
  ed->buf = NULL;
3868
93.0k
  ed->new_ctb = 1;   /* (packet number requires a new CTB anyway.)  */
3869
93.0k
  ed->is_partial = partial;
3870
93.0k
  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
93.0k
  if (orig_pktlen && pktlen < 19)
3874
777
    {
3875
777
      log_error ("packet(%d) too short\n", pkttype);
3876
777
      if (list_mode)
3877
777
        es_fputs (":aead encrypted packet: [too short]\n", listfp);
3878
777
      rc = gpg_error (GPG_ERR_INV_PACKET);
3879
777
      iobuf_skip_rest (inp, pktlen, partial);
3880
777
      goto leave;
3881
777
    }
3882
3883
92.3k
  version = iobuf_get_noeof (inp);
3884
92.3k
  if (orig_pktlen)
3885
253
    pktlen--;
3886
92.3k
  if (version != 1)
3887
7.61k
    {
3888
7.61k
      log_error ("aead encrypted packet with unknown version %d\n",
3889
7.61k
                 version);
3890
7.61k
      if (list_mode)
3891
7.61k
        es_fputs (":aead encrypted packet: [unknown version]\n", listfp);
3892
      /*skip_rest(inp, pktlen); should we really do this? */
3893
7.61k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3894
7.61k
      goto leave;
3895
7.61k
    }
3896
3897
84.6k
  ed->cipher_algo = iobuf_get_noeof (inp);
3898
84.6k
  if (orig_pktlen)
3899
17
    pktlen--;
3900
84.6k
  ed->aead_algo = iobuf_get_noeof (inp);
3901
84.6k
  if (orig_pktlen)
3902
17
    pktlen--;
3903
84.6k
  ed->chunkbyte = iobuf_get_noeof (inp);
3904
84.6k
  if (orig_pktlen)
3905
17
    pktlen--;
3906
3907
  /* Store the remaining length of the encrypted data.  We read the
3908
   * rest during decryption.  */
3909
84.6k
  ed->len = pktlen;
3910
3911
84.6k
  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
84.6k
  ed->buf = inp;
3922
3923
93.0k
 leave:
3924
93.0k
  return rc;
3925
84.6k
}
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
4.22k
{
3943
4.22k
  byte *p;
3944
4.22k
  const byte *sesmark;
3945
4.22k
  size_t sesmarklen;
3946
4.22k
  int i;
3947
3948
4.22k
  (void) pkttype;
3949
3950
4.22k
  if (list_mode)
3951
4.22k
    es_fprintf (listfp, ":packet 63: length %lu ", pktlen);
3952
3953
4.22k
  sesmark = get_session_marker (&sesmarklen);
3954
4.22k
  if (pktlen < sesmarklen + 1)  /* 1 is for the control bytes */
3955
832
    goto skipit;
3956
34.4k
  for (i = 0; i < sesmarklen; i++, pktlen--)
3957
32.4k
    {
3958
32.4k
      if (sesmark[i] != iobuf_get_noeof (inp))
3959
1.47k
  goto skipit;
3960
32.4k
    }
3961
1.91k
  if (pktlen > 4096)
3962
0
    goto skipit;  /* Definitely too large.  We skip it to avoid an
3963
                     overflow in the malloc.  */
3964
1.91k
  if (list_mode)
3965
1.91k
    es_fputs ("- gpg control packet", listfp);
3966
3967
1.91k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
3968
1.91k
             + pktlen - 1);
3969
1.91k
  packet->pkt.gpg_control->control = iobuf_get_noeof (inp);
3970
1.91k
  pktlen--;
3971
1.91k
  packet->pkt.gpg_control->datalen = pktlen;
3972
1.91k
  p = packet->pkt.gpg_control->data;
3973
5.81k
  for (; pktlen; pktlen--, p++)
3974
3.90k
    *p = iobuf_get_noeof (inp);
3975
3976
1.91k
  return 0;
3977
3978
2.30k
 skipit:
3979
2.30k
  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
2.30k
  iobuf_skip_rest (inp, pktlen, 0);
4002
2.30k
  return gpg_error (GPG_ERR_INV_PACKET);
4003
1.91k
}
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
12.8k
{
4010
12.8k
  PACKET *packet;
4011
12.8k
  byte *p;
4012
4013
12.8k
  if (!data)
4014
0
    datalen = 0;
4015
4016
12.8k
  packet = xmalloc (sizeof *packet);
4017
12.8k
  init_packet (packet);
4018
12.8k
  packet->pkttype = PKT_GPG_CONTROL;
4019
12.8k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen);
4020
12.8k
  packet->pkt.gpg_control->control = type;
4021
12.8k
  packet->pkt.gpg_control->datalen = datalen;
4022
12.8k
  p = packet->pkt.gpg_control->data;
4023
899k
  for (; datalen; datalen--, p++)
4024
887k
    *p = *data++;
4025
4026
12.8k
  return packet;
4027
12.8k
}