Coverage Report

Created: 2026-08-31 07:22

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
27.8M
{
96
27.8M
  unsigned short a;
97
27.8M
  a = (unsigned short)iobuf_get_noeof (inp) << 8;
98
27.8M
  a |= iobuf_get_noeof (inp);
99
27.8M
  return a;
100
27.8M
}
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
933k
{
107
933k
  unsigned long a;
108
933k
  a = (unsigned long)iobuf_get_noeof (inp) << 24;
109
933k
  a |= iobuf_get_noeof (inp) << 16;
110
933k
  a |= iobuf_get_noeof (inp) << 8;
111
933k
  a |= iobuf_get_noeof (inp);
112
933k
  return a;
113
933k
}
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
359k
{
131
359k
  int c, c1, c2, i;
132
359k
  unsigned int nmax = *ret_nread;
133
359k
  unsigned int nbits, nbytes;
134
359k
  size_t nread = 0;
135
359k
  gcry_mpi_t a = NULL;
136
359k
  byte *buf = NULL;
137
359k
  byte *p;
138
139
359k
  if (!nmax)
140
5
    goto overflow;
141
142
359k
  if ((c = c1 = iobuf_get (inp)) == -1)
143
115
    goto leave;
144
359k
  if (++nread == nmax)
145
1
    goto overflow;
146
359k
  nbits = c << 8;
147
359k
  if ((c = c2 = iobuf_get (inp)) == -1)
148
32
    goto leave;
149
359k
  ++nread;
150
359k
  nbits |= c;
151
359k
  if (nbits > MAX_EXTERN_MPI_BITS)
152
36
    {
153
36
      log_error ("mpi too large (%u bits)\n", nbits);
154
36
      goto leave;
155
36
    }
156
157
359k
  nbytes = (nbits + 7) / 8;
158
359k
  buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2);
159
359k
  p = buf;
160
359k
  p[0] = c1;
161
359k
  p[1] = c2;
162
12.1M
  for (i = 0; i < nbytes; i++)
163
11.8M
    {
164
11.8M
      if (nread == nmax)
165
24
  goto overflow;
166
167
11.8M
      c = iobuf_get (inp);
168
11.8M
      if (c == -1)
169
37
  goto leave;
170
171
11.8M
      p[i + 2] = c;
172
11.8M
      nread ++;
173
11.8M
    }
174
175
359k
  if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread))
176
0
    a = NULL;
177
178
359k
  *ret_nread = nread;
179
359k
  gcry_free(buf);
180
359k
  return a;
181
182
30
 overflow:
183
30
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
184
250
 leave:
185
250
  *ret_nread = nread;
186
250
  gcry_free(buf);
187
250
  return a;
188
30
}
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
49
{
208
49
  gpg_error_t err;
209
49
  int c, i;
210
49
  byte *buf = NULL;
211
49
  byte *p;
212
213
49
  *r_data = NULL;
214
215
49
  if ((nbytes && nlength)
216
49
      || (!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
49
  if (nlength)
223
49
    {
224
238
      for (i = 0; i < nlength; i++)
225
193
        {
226
193
          if (!*pktlen)
227
1
            {
228
1
              err = gpg_error (GPG_ERR_INV_PACKET);
229
1
              goto leave;
230
1
            }
231
192
          c = iobuf_readbyte (inp);
232
192
          if (c < 0)
233
3
            {
234
3
              err =  gpg_error (GPG_ERR_INV_PACKET);
235
3
              goto leave;
236
3
            }
237
189
          --*pktlen;
238
189
          nbytes <<= 8;
239
189
          nbytes |= c;
240
189
        }
241
242
45
      if (!nbytes)
243
1
        {
244
1
          err =  gpg_error (GPG_ERR_INV_PACKET);
245
1
          goto leave;
246
1
        }
247
45
    }
248
249
44
  if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS)
250
19
      || (nbytes*8 < nbytes))
251
25
    {
252
25
      log_error ("octet string too large (%u octets)\n", nbytes);
253
25
      err = gpg_error (GPG_ERR_TOO_LARGE);
254
25
      goto leave;
255
25
    }
256
257
19
  if (nbytes > *pktlen)
258
1
    {
259
1
      log_error ("octet string larger than packet (%u octets)\n", nbytes);
260
1
      err = gpg_error (GPG_ERR_INV_PACKET);
261
1
      goto leave;
262
1
    }
263
264
18
  buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes);
265
18
  if (!buf)
266
0
    {
267
0
      err = gpg_error_from_syserror ();
268
0
      goto leave;
269
0
    }
270
18
  p = buf;
271
589
  for (i = 0; i < nbytes; i++)
272
585
    {
273
585
      c = iobuf_get (inp);
274
585
      if (c == -1)
275
14
        {
276
14
          err = gpg_error (GPG_ERR_INV_PACKET);
277
14
          goto leave;
278
14
        }
279
280
571
      p[i] = c;
281
571
      --*pktlen;
282
571
    }
283
284
4
  *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8);
285
4
  gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2);
286
4
  return 0;
287
288
45
 leave:
289
45
  gcry_free (buf);
290
45
  return err;
291
18
}
292
293
294
/* Read an external representation of an SOS and return the opaque MPI
295
   with GCRYMPI_FLAG_USER2.  The external format is a 16-bit unsigned
296
   value stored in network byte order giving information for the
297
   following octets.
298
299
   The caller must set *RET_NREAD to the maximum number of bytes to
300
   read from the pipeline INP.  This function sets *RET_NREAD to be
301
   the number of bytes actually read from the pipeline.
302
303
   If SECURE is true, the integer is stored in secure memory
304
   (allocated using gcry_xmalloc_secure).  */
305
static gcry_mpi_t
306
sos_read (iobuf_t inp, unsigned int *ret_nread, int secure)
307
27.7M
{
308
27.7M
  int c, c1, c2, i;
309
27.7M
  unsigned int nmax = *ret_nread;
310
27.7M
  unsigned int nbits, nbytes;
311
27.7M
  size_t nread = 0;
312
27.7M
  gcry_mpi_t a = NULL;
313
27.7M
  byte *buf = NULL;
314
27.7M
  byte *p;
315
316
27.7M
  if (!nmax)
317
6
    goto overflow;
318
319
27.7M
  if ((c = c1 = iobuf_get (inp)) == -1)
320
62
    goto leave;
321
27.7M
  if (++nread == nmax)
322
1
    goto overflow;
323
27.7M
  nbits = c << 8;
324
27.7M
  if ((c = c2 = iobuf_get (inp)) == -1)
325
22
    goto leave;
326
27.7M
  ++nread;
327
27.7M
  nbits |= c;
328
27.7M
  if (nbits > MAX_EXTERN_MPI_BITS)
329
35
    {
330
35
      log_error ("mpi too large (%u bits)\n", nbits);
331
35
      goto leave;
332
35
    }
333
334
27.7M
  nbytes = (nbits + 7) / 8;
335
27.7M
  buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes);
336
27.7M
  p = buf;
337
914M
  for (i = 0; i < nbytes; i++)
338
887M
    {
339
887M
      if (nread == nmax)
340
19
        goto overflow;
341
342
887M
      c = iobuf_get (inp);
343
887M
      if (c == -1)
344
88
        goto leave;
345
346
887M
      p[i] = c;
347
887M
      nread ++;
348
887M
    }
349
350
27.7M
  a = gcry_mpi_set_opaque (NULL, buf, nbits);
351
27.7M
  gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2);
352
27.7M
  *ret_nread = nread;
353
27.7M
  return a;
354
355
26
 overflow:
356
26
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
357
233
 leave:
358
233
  *ret_nread = nread;
359
233
  gcry_free(buf);
360
233
  return a;
361
26
}
362
363
364
/* Register STRING as a known critical notation name.  */
365
void
366
register_known_notation (const char *string)
367
154
{
368
154
  strlist_t sl;
369
370
154
  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
154
  if (!string)
377
154
    return; /* Only initialized the default known notations.  */
378
379
  /* In --set-notation we use an exclamation mark to indicate a
380
   * critical notation.  As a convenience skip this here.  */
381
0
  if (*string == '!')
382
0
    string++;
383
384
0
  if (!*string || strlist_find (known_notations_list, string))
385
0
    return; /* Empty string or already registered.  */
386
387
0
  sl = add_to_strlist (&known_notations_list, string);
388
0
  sl->flags = strlen (string);
389
0
}
390
391
392
int
393
set_packet_list_mode (int mode)
394
21.3k
{
395
21.3k
  int old = list_mode;
396
21.3k
  list_mode = mode;
397
398
  /* We use stdout only if invoked by the --list-packets command
399
     but switch to stderr in all other cases.  This breaks the
400
     previous behaviour but that seems to be more of a bug than
401
     intentional.  I don't believe that any application makes use of
402
     this long standing annoying way of printing to stdout except when
403
     doing a --list-packets. If this assumption fails, it will be easy
404
     to add an option for the listing stream.  Note that we initialize
405
     it only once; mainly because there is code which switches
406
     opt.list_mode back to 1 and we want to have all output to the
407
     same stream.  The MPI_PRINT_MODE will be enabled if the
408
     corresponding debug flag is set or if we are in --list-packets
409
     and --verbose is given.
410
411
     Using stderr is not actually very clean because it bypasses the
412
     logging code but it is a special thing anyway.  I am not sure
413
     whether using log_stream() would be better.  Perhaps we should
414
     enable the list mode only with a special option. */
415
21.3k
  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
21.3k
  return old;
430
21.3k
}
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
138k
{
438
138k
  static byte unknown_pubkey_algos[256];
439
440
  /* First check whether the algorithm is usable but not suitable for
441
     encryption/signing.  */
442
138k
  if (pubkey_get_npkey (algo))
443
102k
    {
444
102k
      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
102k
    }
454
35.6k
  else
455
35.6k
    {
456
35.6k
      algo &= 0xff;
457
35.6k
      if (!unknown_pubkey_algos[algo])
458
177
        {
459
177
          if (opt.verbose && !glo_ctrl.silence_parse_warnings)
460
177
            log_info (_("can't handle public key algorithm %d\n"), algo);
461
177
          unknown_pubkey_algos[algo] = 1;
462
177
        }
463
35.6k
    }
464
138k
}
465
466
467
#if DEBUG_PARSE_PACKET
468
int
469
dbg_parse_packet (parse_packet_ctx_t ctx, PACKET *pkt,
470
                  const char *dbg_f, int dbg_l)
471
14.8M
{
472
14.8M
  int skip, rc;
473
474
14.8M
  do
475
14.8M
    {
476
14.8M
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l);
477
14.8M
    }
478
14.8M
  while (skip && ! rc);
479
14.8M
  return rc;
480
14.8M
}
481
#else /*!DEBUG_PARSE_PACKET*/
482
int
483
parse_packet (parse_packet_ctx_t ctx, PACKET *pkt)
484
{
485
  int skip, rc;
486
487
  do
488
    {
489
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0);
490
    }
491
  while (skip && ! rc);
492
  return rc;
493
}
494
#endif /*!DEBUG_PARSE_PACKET*/
495
496
497
/*
498
 * Like parse packet, but only return secret or public (sub)key
499
 * packets.
500
 */
501
#if DEBUG_PARSE_PACKET
502
int
503
dbg_search_packet (parse_packet_ctx_t ctx, PACKET *pkt,
504
                   off_t * retpos, int with_uid,
505
       const char *dbg_f, int dbg_l)
506
0
{
507
0
  int skip, rc;
508
509
0
  do
510
0
    {
511
0
      rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0, "search",
512
0
                  dbg_f, dbg_l);
513
0
    }
514
0
  while (skip && ! rc);
515
0
  return rc;
516
0
}
517
#else /*!DEBUG_PARSE_PACKET*/
518
int
519
search_packet (parse_packet_ctx_t ctx, PACKET *pkt,
520
               off_t * retpos, int with_uid)
521
{
522
  int skip, rc;
523
524
  do
525
    {
526
      rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0);
527
    }
528
  while (skip && ! rc);
529
  return rc;
530
}
531
#endif /*!DEBUG_PARSE_PACKET*/
532
533
534
/*
535
 * Copy all packets from INP to OUT, thereby removing unused spaces.
536
 */
537
#if DEBUG_PARSE_PACKET
538
int
539
dbg_copy_all_packets (iobuf_t inp, iobuf_t out, const char *dbg_f, int dbg_l)
540
0
{
541
0
  PACKET pkt;
542
0
  struct parse_packet_ctx_s parsectx;
543
0
  int skip, rc = 0;
544
545
0
  if (! out)
546
0
    log_bug ("copy_all_packets: OUT may not be NULL.\n");
547
548
0
  init_parse_packet (&parsectx, inp);
549
550
0
  do
551
0
    {
552
0
      init_packet (&pkt);
553
0
    }
554
0
  while (!
555
0
   (rc =
556
0
    parse (&parsectx, &pkt, 0, NULL, &skip, out, 0, "copy",
557
0
                 dbg_f, dbg_l)));
558
559
0
  deinit_parse_packet (&parsectx);
560
561
0
  return rc;
562
0
}
563
#else /*!DEBUG_PARSE_PACKET*/
564
int
565
copy_all_packets (iobuf_t inp, iobuf_t out)
566
{
567
  PACKET pkt;
568
  struct parse_packet_ctx_s parsectx;
569
  int skip, rc = 0;
570
571
  if (! out)
572
    log_bug ("copy_all_packets: OUT may not be NULL.\n");
573
574
  init_parse_packet (&parsectx, inp);
575
576
  do
577
    {
578
      init_packet (&pkt);
579
    }
580
  while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0)));
581
582
  deinit_parse_packet (&parsectx);
583
584
  return rc;
585
}
586
#endif /*!DEBUG_PARSE_PACKET*/
587
588
589
/*
590
 * Copy some packets from INP to OUT, thereby removing unused spaces.
591
 * Stop at offset STOPoff (i.e. don't copy packets at this or later
592
 * offsets)
593
 */
594
#if DEBUG_PARSE_PACKET
595
int
596
dbg_copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff,
597
           const char *dbg_f, int dbg_l)
598
0
{
599
0
  int rc = 0;
600
0
  PACKET pkt;
601
0
  int skip;
602
0
  struct parse_packet_ctx_s parsectx;
603
604
0
  init_parse_packet (&parsectx, inp);
605
606
0
  do
607
0
    {
608
0
      if (iobuf_tell (inp) >= stopoff)
609
0
        {
610
0
          deinit_parse_packet (&parsectx);
611
0
          return 0;
612
0
        }
613
0
      init_packet (&pkt);
614
0
    }
615
0
  while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0,
616
0
           "some", dbg_f, dbg_l)));
617
618
0
  deinit_parse_packet (&parsectx);
619
620
0
  return rc;
621
0
}
622
#else /*!DEBUG_PARSE_PACKET*/
623
int
624
copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff)
625
{
626
  int rc = 0;
627
  PACKET pkt;
628
  struct parse_packet_ctx_s parsectx;
629
  int skip;
630
631
  init_parse_packet (&parsectx, inp);
632
633
  do
634
    {
635
      if (iobuf_tell (inp) >= stopoff)
636
        {
637
          deinit_parse_packet (&parsectx);
638
          return 0;
639
        }
640
      init_packet (&pkt);
641
    }
642
  while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0)));
643
644
  deinit_parse_packet (&parsectx);
645
646
  return rc;
647
}
648
#endif /*!DEBUG_PARSE_PACKET*/
649
650
651
/*
652
 * Skip over N packets
653
 */
654
#if DEBUG_PARSE_PACKET
655
int
656
dbg_skip_some_packets (iobuf_t inp, unsigned n, const char *dbg_f, int dbg_l)
657
0
{
658
0
  int rc = 0;
659
0
  int skip;
660
0
  PACKET pkt;
661
0
  struct parse_packet_ctx_s parsectx;
662
663
0
  init_parse_packet (&parsectx, inp);
664
665
0
  for (; n && !rc; n--)
666
0
    {
667
0
      init_packet (&pkt);
668
0
      rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1, "skip",
669
0
                  dbg_f, dbg_l);
670
0
    }
671
672
0
  deinit_parse_packet (&parsectx);
673
674
0
  return rc;
675
0
}
676
#else /*!DEBUG_PARSE_PACKET*/
677
int
678
skip_some_packets (iobuf_t inp, unsigned int n)
679
{
680
  int rc = 0;
681
  int skip;
682
  PACKET pkt;
683
  struct parse_packet_ctx_s parsectx;
684
685
  init_parse_packet (&parsectx, inp);
686
687
  for (; n && !rc; n--)
688
    {
689
      init_packet (&pkt);
690
      rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1);
691
    }
692
693
  deinit_parse_packet (&parsectx);
694
695
  return rc;
696
}
697
#endif /*!DEBUG_PARSE_PACKET*/
698
699
700
/* Parse a packet and save it in *PKT.
701
702
   If OUT is not NULL and the packet is valid (its type is not 0),
703
   then the header, the initial length field and the packet's contents
704
   are written to OUT.  In this case, the packet is not saved in *PKT.
705
706
   ONLYKEYPKTS is a simple packet filter.  If ONLYKEYPKTS is set to 1,
707
   then only public subkey packets, public key packets, private subkey
708
   packets and private key packets are parsed.  The rest are skipped
709
   (i.e., the header and the contents are read from the pipeline and
710
   discarded).  If ONLYKEYPKTS is set to 2, then in addition to the
711
   above 4 types of packets, user id packets are also accepted.
712
713
   DO_SKIP is a more coarse grained filter.  Unless ONLYKEYPKTS is set
714
   to 2 and the packet is a user id packet, all packets are skipped.
715
716
   Finally, if a packet is invalid (it's type is 0), it is skipped.
717
718
   If a packet is skipped and SKIP is not NULL, then *SKIP is set to
719
   1.
720
721
   Note: ONLYKEYPKTS and DO_SKIP are only respected if OUT is NULL,
722
   i.e., the packets are not simply being copied.
723
724
   If RETPOS is not NULL, then the position of CTX->INP (as returned by
725
   iobuf_tell) is saved there before any data is read from CTX->INP.
726
  */
727
static int
728
parse (parse_packet_ctx_t ctx, PACKET *pkt, int onlykeypkts, off_t * retpos,
729
       int *skip, IOBUF out, int do_skip
730
#if DEBUG_PARSE_PACKET
731
       , const char *dbg_w, const char *dbg_f, int dbg_l
732
#endif
733
       )
734
14.8M
{
735
14.8M
  int rc = 0;
736
14.8M
  iobuf_t inp;
737
14.8M
  int c, ctb, pkttype, lenbytes;
738
14.8M
  unsigned long pktlen;
739
14.8M
  byte hdr[8];
740
14.8M
  int hdrlen;
741
14.8M
  int new_ctb = 0, partial = 0;
742
14.8M
  int with_uid = (onlykeypkts == 2);
743
14.8M
  off_t pos;
744
745
14.8M
  *skip = 0;
746
14.8M
  inp = ctx->inp;
747
748
28.7M
 again:
749
28.7M
  log_assert (!pkt->pkt.generic);
750
28.7M
  if (retpos || list_mode)
751
0
    {
752
0
      pos = iobuf_tell (inp);
753
0
      if (retpos)
754
0
        *retpos = pos;
755
0
    }
756
28.7M
  else
757
28.7M
    pos = 0; /* (silence compiler warning) */
758
759
  /* The first byte of a packet is the so-called tag.  The highest bit
760
     must be set.  */
761
28.7M
  if ((ctb = iobuf_get (inp)) == -1)
762
14.9k
    {
763
14.9k
      rc = -1;
764
14.9k
      goto leave;
765
14.9k
    }
766
28.7M
  ctx->last_ctb = ctb;
767
28.7M
  hdrlen = 0;
768
28.7M
  hdr[hdrlen++] = ctb;
769
770
28.7M
  if (!(ctb & 0x80))
771
380
    {
772
380
      log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb);
773
380
      rc = gpg_error (GPG_ERR_INV_PACKET);
774
380
      goto leave;
775
380
    }
776
777
  /* Immediately following the header is the length.  There are two
778
   * formats: the old format and the new format.  If bit 6 (where the
779
   * least significant bit is bit 0) is set in the tag, then we are
780
   * dealing with a new format packet.  Otherwise, it is an old format
781
   * packet.  In the new format the packet's type is encoded in the 6
782
   * least significant bits of the tag; in the old format it is
783
   * encoded in bits 2-5.  */
784
28.7M
  pktlen = 0;
785
28.7M
  new_ctb = !!(ctb & 0x40);
786
28.7M
  if (new_ctb)
787
47.4k
    pkttype = ctb & 0x3f;
788
28.7M
  else
789
28.7M
    pkttype = (ctb >> 2) & 0xf;
790
791
28.7M
  if (ctx->only_fookey_enc
792
0
      && !(pkttype == PKT_SYMKEY_ENC || pkttype == PKT_PUBKEY_ENC))
793
0
    {
794
0
      rc = gpg_error (GPG_ERR_TRUE);
795
0
      goto leave;
796
0
    }
797
798
28.7M
  if (new_ctb)
799
47.4k
    {
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
47.4k
      if ((c = iobuf_get (inp)) == -1)
805
49
  {
806
49
    log_error ("%s: 1st length byte missing\n", iobuf_where (inp));
807
49
    rc = gpg_error (GPG_ERR_INV_PACKET);
808
49
    goto leave;
809
49
  }
810
811
812
47.4k
      hdr[hdrlen++] = c;
813
47.4k
      if (c < 192)
814
43.6k
        pktlen = c;
815
3.75k
      else if (c < 224)
816
295
        {
817
295
          pktlen = (c - 192) * 256;
818
295
          if ((c = iobuf_get (inp)) == -1)
819
8
            {
820
8
              log_error ("%s: 2nd length byte missing\n",
821
8
                         iobuf_where (inp));
822
8
              rc = gpg_error (GPG_ERR_INV_PACKET);
823
8
              goto leave;
824
8
            }
825
287
          hdr[hdrlen++] = c;
826
287
          pktlen += c + 192;
827
287
        }
828
3.45k
      else if (c == 255)
829
934
        {
830
934
    int i;
831
934
    char value[4];
832
833
4.62k
    for (i = 0; i < 4; i ++)
834
3.71k
            {
835
3.71k
              if ((c = iobuf_get (inp)) == -1)
836
18
                {
837
18
                  log_error ("%s: 4 byte length invalid\n", iobuf_where (inp));
838
18
                  rc = gpg_error (GPG_ERR_INV_PACKET);
839
18
                  goto leave;
840
18
                }
841
3.69k
              value[i] = hdr[hdrlen++] = c;
842
3.69k
            }
843
844
916
    pktlen = buf32_to_ulong (value);
845
916
        }
846
2.52k
      else /* Partial body length.  */
847
2.52k
        {
848
2.52k
          switch (pkttype)
849
2.52k
            {
850
283
            case PKT_PLAINTEXT:
851
328
            case PKT_ENCRYPTED:
852
2.31k
            case PKT_ENCRYPTED_MDC:
853
2.41k
            case PKT_ENCRYPTED_AEAD:
854
2.50k
            case PKT_COMPRESSED:
855
2.50k
              iobuf_set_partial_body_length_mode (inp, c & 0xff);
856
2.50k
              pktlen = 0; /* To indicate partial length.  */
857
2.50k
              partial = 1;
858
2.50k
              break;
859
860
21
            default:
861
21
              log_error ("%s: partial length invalid for"
862
21
                         " packet type %d\n", iobuf_where (inp), pkttype);
863
21
              rc = gpg_error (GPG_ERR_INV_PACKET);
864
21
              goto leave;
865
2.52k
            }
866
2.52k
        }
867
868
47.4k
    }
869
28.7M
  else /* This is an old format packet.  */
870
28.7M
    {
871
      /* The type of length encoding is encoded in bits 0-1 of the
872
   tag.  */
873
28.7M
      lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3));
874
28.7M
      if (!lenbytes)
875
161
  {
876
161
    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
161
    partial = 1;
880
161
    if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT
881
24
        && pkttype != PKT_COMPRESSED)
882
21
      {
883
21
        log_error ("%s: indeterminate length for invalid"
884
21
       " packet type %d\n", iobuf_where (inp), pkttype);
885
21
        rc = gpg_error (GPG_ERR_INV_PACKET);
886
21
        goto leave;
887
21
      }
888
161
  }
889
28.7M
      else
890
28.7M
  {
891
57.4M
    for (; lenbytes; lenbytes--)
892
28.7M
      {
893
28.7M
        pktlen <<= 8;
894
28.7M
        c = iobuf_get (inp);
895
28.7M
        if (c == -1)
896
53
    {
897
53
      log_error ("%s: length invalid\n", iobuf_where (inp));
898
53
      rc = gpg_error (GPG_ERR_INV_PACKET);
899
53
      goto leave;
900
53
    }
901
28.7M
        pktlen |= hdr[hdrlen++] = c;
902
28.7M
      }
903
28.7M
  }
904
28.7M
    }
905
906
  /* Sometimes the decompressing layer enters an error state in which
907
     it simply outputs 0xff for every byte read.  If we have a stream
908
     of 0xff bytes, then it will be detected as a new format packet
909
     with type 63 and a 4-byte encoded length that is 4G-1.  Since
910
     packets with type 63 are private and we use them as a control
911
     packet, which won't be 4 GB, we reject such packets as
912
     invalid.  */
913
28.7M
  if (pkttype == 63 && pktlen == 0xFFFFFFFF)
914
0
    {
915
      /* With some probability this is caused by a problem in the
916
       * the uncompressing layer - in some error cases it just loops
917
       * and spits out 0xff bytes. */
918
0
      log_error ("%s: garbled packet detected\n", iobuf_where (inp));
919
0
      g10_exit (2);
920
0
    }
921
922
28.7M
  if (out && pkttype)
923
0
    {
924
      /* This type of copying won't work if the packet uses a partial
925
   body length.  (In other words, this only works if HDR is
926
   actually the length.)  Currently, no callers require this
927
   functionality so we just log this as an error.  */
928
0
      if (partial)
929
0
  {
930
0
    log_error ("parse: Can't copy partial packet.  Aborting.\n");
931
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
932
0
    goto leave;
933
0
  }
934
935
0
      rc = iobuf_write (out, hdr, hdrlen);
936
0
      if (!rc)
937
0
  rc = copy_packet (inp, out, pkttype, pktlen, partial);
938
0
      goto leave;
939
0
    }
940
941
28.7M
  if (with_uid && pkttype == PKT_USER_ID)
942
    /* If ONLYKEYPKTS is set to 2, then we never skip user id packets,
943
       even if DO_SKIP is set.  */
944
0
    ;
945
28.7M
  else if (do_skip
946
     /* type==0 is not allowed.  This is an invalid packet.  */
947
28.7M
     || !pkttype
948
     /* When ONLYKEYPKTS is set, we don't skip keys.  */
949
28.7M
     || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
950
0
         && pkttype != PKT_PUBLIC_KEY
951
0
         && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY))
952
913
    {
953
913
      iobuf_skip_rest (inp, pktlen, partial);
954
913
      *skip = 1;
955
913
      rc = 0;
956
913
      goto leave;
957
913
    }
958
959
28.7M
  if (DBG_PACKET)
960
0
    {
961
0
#if DEBUG_PARSE_PACKET
962
0
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n",
963
0
     iobuf_id (inp), pkttype, pktlen, new_ctb ? " (new_ctb)" : "",
964
0
     dbg_w, dbg_f, dbg_l);
965
#else
966
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s\n",
967
     iobuf_id (inp), pkttype, pktlen,
968
     new_ctb ? " (new_ctb)" : "");
969
#endif
970
0
    }
971
972
28.7M
  if (list_mode)
973
28.7M
    es_fprintf (listfp, "# off=%lu ctb=%02x tag=%d hlen=%d plen=%lu%s%s\n",
974
0
                (unsigned long)pos, ctb, pkttype, hdrlen, pktlen,
975
0
                partial? (new_ctb ? " partial" : " indeterminate") :"",
976
0
                new_ctb? " new-ctb":"");
977
978
  /* Count it.  */
979
28.7M
  ctx->n_parsed_packets++;
980
981
28.7M
  pkt->pkttype = pkttype;
982
28.7M
  rc = GPG_ERR_UNKNOWN_PACKET;  /* default error */
983
28.7M
  switch (pkttype)
984
28.7M
    {
985
21.3k
    case PKT_PUBLIC_KEY:
986
786k
    case PKT_PUBLIC_SUBKEY:
987
790k
    case PKT_SECRET_KEY:
988
792k
    case PKT_SECRET_SUBKEY:
989
792k
      pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key);
990
792k
      rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt);
991
792k
      break;
992
2.71k
    case PKT_SYMKEY_ENC:
993
2.71k
      rc = parse_symkeyenc (inp, pkttype, pktlen, pkt);
994
2.71k
      break;
995
915
    case PKT_PUBKEY_ENC:
996
915
      rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt);
997
915
      break;
998
13.9M
    case PKT_SIGNATURE:
999
13.9M
      pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature);
1000
13.9M
      rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature);
1001
13.9M
      break;
1002
478
    case PKT_ONEPASS_SIG:
1003
478
      pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig);
1004
478
      rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig);
1005
478
      break;
1006
35.4k
    case PKT_USER_ID:
1007
35.4k
      rc = parse_user_id (inp, pkttype, pktlen, pkt);
1008
35.4k
      break;
1009
8.90k
    case PKT_ATTRIBUTE:
1010
8.90k
      pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
1011
8.90k
      rc = parse_attribute (inp, pkttype, pktlen, pkt);
1012
8.90k
      break;
1013
3.11k
    case PKT_OLD_COMMENT:
1014
4.98k
    case PKT_COMMENT:
1015
4.98k
      rc = parse_comment (inp, pkttype, pktlen, pkt);
1016
4.98k
      break;
1017
13.9M
    case PKT_RING_TRUST:
1018
13.9M
      {
1019
13.9M
        rc = parse_ring_trust (ctx, pktlen);
1020
13.9M
        if (!rc)
1021
13.9M
          goto again; /* Directly read the next packet.  */
1022
13.9M
      }
1023
0
      break;
1024
938
    case PKT_PLAINTEXT:
1025
938
      rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial);
1026
938
      break;
1027
132
    case PKT_COMPRESSED:
1028
132
      rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb);
1029
132
      break;
1030
691
    case PKT_ENCRYPTED:
1031
2.97k
    case PKT_ENCRYPTED_MDC:
1032
2.97k
      rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial);
1033
2.97k
      break;
1034
652
    case PKT_MDC:
1035
652
      rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb);
1036
652
      break;
1037
3.18k
    case PKT_ENCRYPTED_AEAD:
1038
3.18k
      rc = parse_encrypted_aead (inp, pkttype, pktlen, pkt, partial);
1039
3.18k
      break;
1040
77
    case PKT_GPG_CONTROL:
1041
77
      rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial);
1042
77
      break;
1043
763
    case PKT_MARKER:
1044
763
      rc = parse_marker (inp, pkttype, pktlen);
1045
763
      break;
1046
1.33k
    default:
1047
      /* Unknown packet.  Skip it.  */
1048
1.33k
      skip_packet (inp, pkttype, pktlen, partial);
1049
1.33k
      break;
1050
28.7M
    }
1051
1052
  /* Store a shallow copy of certain packets in the context.  */
1053
14.8M
  free_packet (NULL, ctx);
1054
14.8M
  if (!rc && (pkttype == PKT_PUBLIC_KEY
1055
14.7M
              || pkttype == PKT_SECRET_KEY
1056
14.7M
              || pkttype == PKT_USER_ID
1057
14.7M
              || pkttype == PKT_ATTRIBUTE
1058
14.7M
              || pkttype == PKT_SIGNATURE))
1059
14.0M
    {
1060
14.0M
      ctx->last_pkt = *pkt;
1061
14.0M
    }
1062
1063
14.8M
 leave:
1064
  /* FIXME: We leak in case of an error (see the xmalloc's above).  */
1065
14.8M
  if (!rc && iobuf_error (inp))
1066
137
    rc = GPG_ERR_INV_KEYRING;
1067
1068
  /* FIXME: We use only the error code for now to avoid problems with
1069
     callers which have not been checked to always use gpg_err_code()
1070
     when comparing error codes.  */
1071
14.8M
  return rc == -1? -1 : gpg_err_code (rc);
1072
14.8M
}
1073
1074
1075
static void
1076
dump_hex_line (int c, int *i)
1077
0
{
1078
0
  if (*i && !(*i % 8))
1079
0
    {
1080
0
      if (*i && !(*i % 24))
1081
0
  es_fprintf (listfp, "\n%4d:", *i);
1082
0
      else
1083
0
  es_putc (' ', listfp);
1084
0
    }
1085
0
  if (c == -1)
1086
0
    es_fprintf (listfp, " EOF");
1087
0
  else
1088
0
    es_fprintf (listfp, " %02x", c);
1089
0
  ++*i;
1090
0
}
1091
1092
1093
/* Copy the contents of a packet from the pipeline IN to the pipeline
1094
   OUT.
1095
1096
   The header and length have already been read from INP and the
1097
   decoded values are given as PKGTYPE and PKTLEN.
1098
1099
   If the packet is a partial body length packet (RFC 4880, Section
1100
   4.2.2.4), then iobuf_set_partial_block_modeiobuf_set_partial_block_mode
1101
   should already have been called on INP and PARTIAL should be set.
1102
1103
   If PARTIAL is set or PKTLEN is 0 and PKTTYPE is PKT_COMPRESSED,
1104
   copy until the first EOF is encountered on INP.
1105
1106
   Returns 0 on success and an error code if an error occurs.  */
1107
static int
1108
copy_packet (IOBUF inp, IOBUF out, int pkttype,
1109
       unsigned long pktlen, int partial)
1110
0
{
1111
0
  int rc;
1112
0
  int n;
1113
0
  char buf[100];
1114
1115
0
  if (partial)
1116
0
    {
1117
0
      while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1)
1118
0
  if ((rc = iobuf_write (out, buf, n)))
1119
0
    return rc;   /* write error */
1120
0
    }
1121
0
  else if (!pktlen && pkttype == PKT_COMPRESSED)
1122
0
    {
1123
0
      log_debug ("copy_packet: compressed!\n");
1124
      /* compressed packet, copy till EOF */
1125
0
      while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1)
1126
0
  if ((rc = iobuf_write (out, buf, n)))
1127
0
    return rc;   /* write error */
1128
0
    }
1129
0
  else
1130
0
    {
1131
0
      for (; pktlen; pktlen -= n)
1132
0
  {
1133
0
    n = pktlen > sizeof (buf) ? sizeof (buf) : pktlen;
1134
0
    n = iobuf_read (inp, buf, n);
1135
0
    if (n == -1)
1136
0
      return gpg_error (GPG_ERR_EOF);
1137
0
    if ((rc = iobuf_write (out, buf, n)))
1138
0
      return rc;   /* write error */
1139
0
  }
1140
0
    }
1141
0
  return 0;
1142
0
}
1143
1144
1145
/* Skip an unknown packet.  PKTTYPE is the packet's type, PKTLEN is
1146
   the length of the packet's content and PARTIAL is whether partial
1147
   body length encoding in used (in this case PKTLEN is ignored).  */
1148
static void
1149
skip_packet (IOBUF inp, int pkttype, unsigned long pktlen, int partial)
1150
1.33k
{
1151
1.33k
  if (list_mode)
1152
0
    {
1153
0
      es_fprintf (listfp, ":unknown packet: type %2d, length %lu\n",
1154
0
                  pkttype, pktlen);
1155
0
      if (pkttype)
1156
0
  {
1157
0
    int c, i = 0;
1158
0
    es_fputs ("dump:", listfp);
1159
0
    if (partial)
1160
0
      {
1161
0
        while ((c = iobuf_get (inp)) != -1)
1162
0
    dump_hex_line (c, &i);
1163
0
      }
1164
0
    else
1165
0
      {
1166
0
        for (; pktlen; pktlen--)
1167
0
    {
1168
0
      dump_hex_line ((c = iobuf_get (inp)), &i);
1169
0
      if (c == -1)
1170
0
        break;
1171
0
    }
1172
0
      }
1173
0
    es_putc ('\n', listfp);
1174
0
    return;
1175
0
  }
1176
0
    }
1177
1.33k
  iobuf_skip_rest (inp, pktlen, partial);
1178
1.33k
}
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
140k
{
1188
140k
  int c;
1189
140k
  byte *buf, *p;
1190
1191
140k
  buf = xtrymalloc (pktlen);
1192
140k
  if (!buf)
1193
121k
    {
1194
121k
      gpg_error_t err = gpg_error_from_syserror ();
1195
121k
      log_error ("error reading rest of packet: %s\n", gpg_strerror (err));
1196
121k
      return NULL;
1197
121k
    }
1198
514k
  for (p = buf; pktlen; pktlen--)
1199
496k
    {
1200
496k
      c = iobuf_get (inp);
1201
496k
      if (c == -1)
1202
933
        {
1203
933
          log_error ("premature eof while reading rest of packet\n");
1204
933
          xfree (buf);
1205
933
          return NULL;
1206
933
        }
1207
495k
      *p++ = c;
1208
495k
    }
1209
1210
18.2k
  return buf;
1211
19.1k
}
1212
1213
1214
/* Read a special size+body from INP.  On success store an opaque MPI
1215
 * with it at R_DATA.  The caller shall store the remaining size of
1216
 * the packet at PKTLEN.  On error return an error code and store NULL
1217
 * at R_DATA.  Even in the error case store the number of read bytes
1218
 * at PKTLEN is updated.  */
1219
static gpg_error_t
1220
read_sized_octet_string (iobuf_t inp, unsigned long *pktlen, gcry_mpi_t *r_data)
1221
1.53M
{
1222
1.53M
  char buffer[256];
1223
1.53M
  char *tmpbuf;
1224
1.53M
  int i, c, nbytes;
1225
1226
1.53M
  *r_data = NULL;
1227
1228
1.53M
  if (!*pktlen)
1229
1
    return gpg_error (GPG_ERR_INV_PACKET);
1230
1.53M
  c = iobuf_readbyte (inp);
1231
1.53M
  if (c < 0)
1232
14
    return gpg_error (GPG_ERR_INV_PACKET);
1233
1.53M
  --*pktlen;
1234
1.53M
  nbytes = c;
1235
1.53M
  if (nbytes < 2 || nbytes > 254)
1236
3
    return gpg_error (GPG_ERR_INV_PACKET);
1237
1.53M
  if (nbytes > *pktlen)
1238
16
    return gpg_error (GPG_ERR_INV_PACKET);
1239
1240
1.53M
  buffer[0] = nbytes;
1241
1242
11.5M
  for (i = 0; i < nbytes; i++)
1243
10.0M
    {
1244
10.0M
      c = iobuf_get (inp);
1245
10.0M
      if (c < 0)
1246
23
        return gpg_error (GPG_ERR_INV_PACKET);
1247
10.0M
      --*pktlen;
1248
10.0M
      buffer[1+i] = c;
1249
10.0M
    }
1250
1251
1.53M
  tmpbuf = xtrymalloc (1 + nbytes);
1252
1.53M
  if (!tmpbuf)
1253
0
    return gpg_error_from_syserror ();
1254
1.53M
  memcpy (tmpbuf, buffer, 1 + nbytes);
1255
1.53M
  *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes));
1256
1.53M
  if (!*r_data)
1257
0
    {
1258
0
      xfree (tmpbuf);
1259
0
      return gpg_error_from_syserror ();
1260
0
    }
1261
1.53M
  return 0;
1262
1.53M
}
1263
1264
1265
/* Parse a marker packet.  */
1266
static int
1267
parse_marker (IOBUF inp, int pkttype, unsigned long pktlen)
1268
763
{
1269
763
  (void) pkttype;
1270
1271
763
  if (pktlen != 3)
1272
42
    goto fail;
1273
1274
721
  if (iobuf_get (inp) != 'P')
1275
9
    {
1276
9
      pktlen--;
1277
9
      goto fail;
1278
9
    }
1279
1280
712
  if (iobuf_get (inp) != 'G')
1281
18
    {
1282
18
      pktlen--;
1283
18
      goto fail;
1284
18
    }
1285
1286
694
  if (iobuf_get (inp) != 'P')
1287
7
    {
1288
7
      pktlen--;
1289
7
      goto fail;
1290
7
    }
1291
1292
687
  if (list_mode)
1293
687
    es_fputs (":marker packet: PGP\n", listfp);
1294
1295
687
  return 0;
1296
1297
76
 fail:
1298
76
  log_error ("invalid marker packet\n");
1299
76
  if (list_mode)
1300
76
    es_fputs (":marker packet: [invalid]\n", listfp);
1301
76
  iobuf_skip_rest (inp, pktlen, 0);
1302
76
  return GPG_ERR_INV_PACKET;
1303
694
}
1304
1305
1306
static int
1307
parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1308
     PACKET * packet)
1309
2.71k
{
1310
2.71k
  PKT_symkey_enc *k;
1311
2.71k
  int rc = 0;
1312
2.71k
  int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen;
1313
1314
2.71k
  if (pktlen < 4)
1315
1
    goto too_short;
1316
2.71k
  version = iobuf_get_noeof (inp);
1317
2.71k
  pktlen--;
1318
2.71k
  if (version == 4)
1319
2.43k
    ;
1320
276
  else if (version == 5)
1321
239
    ;
1322
37
  else
1323
37
    {
1324
37
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
1325
37
      if (list_mode)
1326
37
        es_fprintf (listfp, ":symkey enc packet: [unknown version]\n");
1327
37
      rc = gpg_error (GPG_ERR_INV_PACKET);
1328
37
      goto leave;
1329
37
    }
1330
2.67k
  if (pktlen > 200)
1331
1
    {       /* (we encode the seskeylen in a byte) */
1332
1
      log_error ("packet(%d) too large\n", pkttype);
1333
1
      if (list_mode)
1334
1
        es_fprintf (listfp, ":symkey enc packet: [too large]\n");
1335
1
      rc = gpg_error (GPG_ERR_INV_PACKET);
1336
1
      goto leave;
1337
1
    }
1338
2.67k
  cipher_algo = iobuf_get_noeof (inp);
1339
2.67k
  pktlen--;
1340
2.67k
  if (version == 5)
1341
239
    {
1342
239
      aead_algo = iobuf_get_noeof (inp);
1343
239
      pktlen--;
1344
239
    }
1345
2.43k
  else
1346
2.43k
    aead_algo = 0;
1347
2.67k
  if (pktlen < 2)
1348
1
    goto too_short;
1349
2.67k
  s2kmode = iobuf_get_noeof (inp);
1350
2.67k
  pktlen--;
1351
2.67k
  hash_algo = iobuf_get_noeof (inp);
1352
2.67k
  pktlen--;
1353
2.67k
  switch (s2kmode)
1354
2.67k
    {
1355
184
    case 0: /* Simple S2K.  */
1356
184
      minlen = 0;
1357
184
      break;
1358
4
    case 1: /* Salted S2K.  */
1359
4
      minlen = 8;
1360
4
      break;
1361
2.08k
    case 3: /* Iterated+salted S2K.  */
1362
2.08k
      minlen = 9;
1363
2.08k
      break;
1364
400
    default:
1365
400
      log_error ("unknown S2K mode %d\n", s2kmode);
1366
400
      if (list_mode)
1367
400
        es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n");
1368
400
      goto leave;
1369
2.67k
    }
1370
2.27k
  if (minlen > pktlen)
1371
3
    {
1372
3
      log_error ("packet with S2K %d too short\n", s2kmode);
1373
3
      if (list_mode)
1374
3
        es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1375
3
      rc = gpg_error (GPG_ERR_INV_PACKET);
1376
3
      goto leave;
1377
3
    }
1378
2.27k
  seskeylen = pktlen - minlen;
1379
2.27k
  k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc);
1380
2.27k
  k->version = version;
1381
2.27k
  k->cipher_algo = cipher_algo;
1382
2.27k
  k->aead_algo = aead_algo;
1383
2.27k
  k->s2k.mode = s2kmode;
1384
2.27k
  k->s2k.hash_algo = hash_algo;
1385
2.27k
  if (s2kmode == 1 || s2kmode == 3)
1386
2.08k
    {
1387
18.7k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
1388
16.6k
  k->s2k.salt[i] = iobuf_get_noeof (inp);
1389
2.08k
    }
1390
2.27k
  if (s2kmode == 3)
1391
2.08k
    {
1392
2.08k
      k->s2k.count = iobuf_get_noeof (inp);
1393
2.08k
      pktlen--;
1394
2.08k
    }
1395
2.27k
  k->seskeylen = seskeylen;
1396
2.27k
  if (k->seskeylen)
1397
1.01k
    {
1398
1.01k
      k->seskey = xcalloc (1, seskeylen);
1399
26.6k
      for (i = 0; i < seskeylen && pktlen; i++, pktlen--)
1400
25.6k
  k->seskey[i] = iobuf_get_noeof (inp);
1401
1402
      /* What we're watching out for here is a session key decryptor
1403
         with no salt.  The RFC says that using salt for this is a
1404
         MUST. */
1405
1.01k
      if (s2kmode != 1 && s2kmode != 3)
1406
1.01k
  log_info (_("WARNING: potentially insecure symmetrically"
1407
85
        " encrypted session key\n"));
1408
1.01k
    }
1409
2.27k
  log_assert (!pktlen);
1410
1411
2.27k
  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
2.71k
 leave:
1442
2.71k
  iobuf_skip_rest (inp, pktlen, 0);
1443
2.71k
  return rc;
1444
1445
2
 too_short:
1446
2
  log_error ("packet(%d) too short\n", pkttype);
1447
2
  if (list_mode)
1448
2
    es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1449
2
  rc = gpg_error (GPG_ERR_INV_PACKET);
1450
2
  goto leave;
1451
2.27k
}
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
915
{
1459
915
  int rc = 0;
1460
915
  int i, ndata;
1461
915
  unsigned int n;
1462
915
  PKT_pubkey_enc *k;
1463
1464
915
  k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc);
1465
915
  if (pktlen < 12)
1466
6
    {
1467
6
      log_error ("packet(%d) too short\n", pkttype);
1468
6
      if (list_mode)
1469
6
        es_fputs (":pubkey enc packet: [too short]\n", listfp);
1470
6
      rc = gpg_error (GPG_ERR_INV_PACKET);
1471
6
      goto leave;
1472
6
    }
1473
909
  k->version = iobuf_get_noeof (inp);
1474
909
  pktlen--;
1475
909
  if (k->version != 2 && k->version != 3)
1476
41
    {
1477
41
      log_error ("packet(%d) with unknown version %d\n", pkttype, k->version);
1478
41
      if (list_mode)
1479
41
        es_fputs (":pubkey enc packet: [unknown version]\n", listfp);
1480
41
      rc = gpg_error (GPG_ERR_INV_PACKET);
1481
41
      goto leave;
1482
41
    }
1483
868
  k->keyid[0] = read_32 (inp);
1484
868
  pktlen -= 4;
1485
868
  k->keyid[1] = read_32 (inp);
1486
868
  pktlen -= 4;
1487
868
  k->pubkey_algo = iobuf_get_noeof (inp);
1488
868
  pktlen--;
1489
868
  k->throw_keyid = 0;  /* Only used as flag for build_packet.  */
1490
868
  if (list_mode)
1491
868
    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
868
  ndata = pubkey_get_nenc (k->pubkey_algo);
1497
868
  if (!ndata)
1498
497
    {
1499
497
      if (list_mode)
1500
497
  es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo);
1501
497
      unknown_pubkey_warning (k->pubkey_algo);
1502
497
      k->data[0] = NULL; /* No need to store the encrypted data.  */
1503
497
    }
1504
371
  else if (k->pubkey_algo == PUBKEY_ALGO_ECDH)
1505
29
    {
1506
29
      log_assert (ndata == 2);
1507
      /* Get the ephemeral public key.  */
1508
29
      n = pktlen;
1509
29
      k->data[0] = sos_read (inp, &n, 0);
1510
29
      pktlen -= n;
1511
29
      if (!k->data[0])
1512
19
        {
1513
19
          rc = gpg_error (GPG_ERR_INV_PACKET);
1514
19
          goto leave;
1515
19
        }
1516
      /* Get the wrapped symmetric key.  */
1517
10
      rc = read_sized_octet_string (inp, &pktlen, k->data + 1);
1518
10
      if (rc)
1519
8
        goto leave;
1520
10
    }
1521
342
  else if (k->pubkey_algo == PUBKEY_ALGO_KYBER)
1522
0
    {
1523
0
      log_assert (ndata == 3);
1524
      /* Get the ephemeral public key.  */
1525
0
      n = pktlen;
1526
0
      k->data[0] = sos_read (inp, &n, 0);
1527
0
      pktlen -= n;
1528
0
      if (!k->data[0])
1529
0
        {
1530
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
1531
0
          goto leave;
1532
0
        }
1533
      /* Get the Kyber ciphertext.  */
1534
0
      rc = read_octet_string (inp, &pktlen, 4, 0, 0, k->data + 1);
1535
0
      if (rc)
1536
0
        goto leave;
1537
      /* Get the algorithm id for the session key.  */
1538
0
      if (!pktlen)
1539
0
        {
1540
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
1541
0
          goto leave;
1542
0
        }
1543
0
      k->seskey_algo = iobuf_get_noeof (inp);
1544
0
      pktlen--;
1545
      /* Get the encrypted symmetric key.  */
1546
0
      rc = read_octet_string (inp, &pktlen, 1, 0, 0, k->data + 2);
1547
0
      if (rc)
1548
0
        goto leave;
1549
0
    }
1550
342
  else
1551
342
    {
1552
945
      for (i = 0; i < ndata; i++)
1553
603
        {
1554
603
          n = pktlen;
1555
603
          k->data[i] = mpi_read (inp, &n, 0);
1556
603
          pktlen -= n;
1557
603
          if (!k->data[i])
1558
18
            rc = gpg_error (GPG_ERR_INV_PACKET);
1559
603
        }
1560
342
      if (rc)
1561
15
        goto leave;
1562
342
    }
1563
826
  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
915
 leave:
1577
915
  iobuf_skip_rest (inp, pktlen, 0);
1578
915
  return rc;
1579
826
}
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
22.3M
{
1890
22.3M
  switch (type)
1891
22.3M
    {
1892
1.40k
    case SIGSUBPKT_REV_KEY:
1893
1.40k
      if (n < 22)
1894
11
  break;
1895
1.39k
      return 0;
1896
8.29M
    case SIGSUBPKT_SIG_CREATED:
1897
8.30M
    case SIGSUBPKT_SIG_EXPIRE:
1898
8.30M
    case SIGSUBPKT_KEY_EXPIRE:
1899
8.30M
      if (n < 4)
1900
1.80k
  break;
1901
8.29M
      return 0;
1902
313k
    case SIGSUBPKT_KEY_FLAGS:
1903
315k
    case SIGSUBPKT_KS_FLAGS:
1904
316k
    case SIGSUBPKT_PREF_SYM:
1905
316k
    case SIGSUBPKT_PREF_AEAD:
1906
318k
    case SIGSUBPKT_PREF_HASH:
1907
320k
    case SIGSUBPKT_PREF_COMPR:
1908
320k
    case SIGSUBPKT_POLICY:
1909
321k
    case SIGSUBPKT_PREF_KS:
1910
324k
    case SIGSUBPKT_FEATURES:
1911
324k
    case SIGSUBPKT_REGEXP:
1912
324k
    case SIGSUBPKT_ATTST_SIGS:
1913
324k
      return 0;
1914
7.07k
    case SIGSUBPKT_SIGNATURE:
1915
23.1k
    case SIGSUBPKT_EXPORTABLE:
1916
23.8k
    case SIGSUBPKT_REVOCABLE:
1917
24.0k
    case SIGSUBPKT_REVOC_REASON:
1918
24.0k
      if (!n)
1919
31
  break;
1920
23.9k
      return 0;
1921
4.42M
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1922
4.42M
      if (n < 8)
1923
597
  break;
1924
4.42M
      return 0;
1925
9.30M
    case SIGSUBPKT_ISSUER_FPR:  /* issuer key fingerprint */
1926
9.30M
      if (n < 21)
1927
203
  break;
1928
9.30M
      return 0;
1929
345
    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
345
      if (n < 8
1934
183
    || 8 + ((buffer[4] << 8) | buffer[5]) +
1935
183
    ((buffer[6] << 8) | buffer[7]) != n)
1936
205
  break;
1937
140
      return 0;
1938
183
    case SIGSUBPKT_PRIMARY_UID:
1939
183
      if (n != 1)
1940
0
  break;
1941
183
      return 0;
1942
1.43k
    case SIGSUBPKT_TRUST:
1943
1.43k
      if (n != 2)
1944
1.32k
  break;
1945
110
      return 0;
1946
319
    case SIGSUBPKT_KEY_BLOCK:
1947
319
      if (n && buffer[0])
1948
40
        return -1; /* Unknown version - ignore.  */
1949
279
      if (n < 50)
1950
279
  break;  /* Definitely too short to carry a key block.  */
1951
0
      return 0;
1952
2.96k
    default:
1953
2.96k
      return 0;
1954
22.3M
    }
1955
4.45k
  return -2;
1956
22.3M
}
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
154
{
1963
154
  strlist_t sl;
1964
1965
154
  register_known_notation (NULL); /* Make sure it is initialized.  */
1966
1967
308
  for (sl = known_notations_list; sl; sl = sl->next)
1968
154
    if (sl->flags == len && !memcmp (sl->d, name, len))
1969
0
      return 1; /* Known */
1970
1971
154
  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
154
  return 0; /* Unknown.  */
1979
154
}
1980
1981
1982
static int
1983
can_handle_critical (const byte * buffer, size_t n, int type)
1984
4.28M
{
1985
4.28M
  switch (type)
1986
4.28M
    {
1987
348
    case SIGSUBPKT_NOTATION:
1988
348
      if (n >= 8)
1989
191
  {
1990
191
    size_t notation_len = ((buffer[4] << 8) | buffer[5]);
1991
191
    if (n - 8 >= notation_len)
1992
154
      return can_handle_critical_notation (buffer + 8, notation_len);
1993
191
  }
1994
194
      return 0;
1995
624
    case SIGSUBPKT_SIGNATURE:
1996
3.05k
    case SIGSUBPKT_SIG_CREATED:
1997
3.17k
    case SIGSUBPKT_SIG_EXPIRE:
1998
161k
    case SIGSUBPKT_KEY_EXPIRE:
1999
161k
    case SIGSUBPKT_EXPORTABLE:
2000
163k
    case SIGSUBPKT_REVOCABLE:
2001
164k
    case SIGSUBPKT_REV_KEY:
2002
167k
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
2003
168k
    case SIGSUBPKT_ISSUER_FPR:  /* issuer fingerprint */
2004
169k
    case SIGSUBPKT_PREF_SYM:
2005
169k
    case SIGSUBPKT_PREF_AEAD:
2006
170k
    case SIGSUBPKT_PREF_HASH:
2007
172k
    case SIGSUBPKT_PREF_COMPR:
2008
174k
    case SIGSUBPKT_KEY_FLAGS:
2009
174k
    case SIGSUBPKT_PRIMARY_UID:
2010
176k
    case SIGSUBPKT_FEATURES:
2011
176k
    case SIGSUBPKT_TRUST:
2012
176k
    case SIGSUBPKT_REGEXP:
2013
176k
    case SIGSUBPKT_ATTST_SIGS:
2014
      /* Is it enough to show the policy or keyserver? */
2015
176k
    case SIGSUBPKT_POLICY:
2016
177k
    case SIGSUBPKT_PREF_KS:
2017
177k
    case SIGSUBPKT_REVOC_REASON: /* At least we know about it.  */
2018
177k
      return 1;
2019
2020
1.33k
    case SIGSUBPKT_KEY_BLOCK:
2021
1.33k
      if (n && !buffer[0])
2022
189
        return 1;
2023
1.14k
      else
2024
1.14k
        return 0;
2025
2026
4.10M
    default:
2027
4.10M
      return 0;
2028
4.28M
    }
2029
4.28M
}
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
218M
{
2036
218M
  const byte *buffer;
2037
218M
  int buflen;
2038
218M
  int type;
2039
218M
  int critical_dummy;
2040
218M
  int offset;
2041
218M
  size_t n;
2042
218M
  const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed;
2043
218M
  int seq = 0;
2044
218M
  int reqseq = start ? *start : 0;
2045
2046
218M
  if (!critical)
2047
218M
    critical = &critical_dummy;
2048
2049
218M
  if (!pktbuf || reqseq == -1)
2050
85.7k
    {
2051
85.7k
      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
85.7k
      return reqtype ==  SIGSUBPKT_TEST_CRITICAL ? dummy : NULL;
2055
85.7k
    }
2056
218M
  buffer = pktbuf->data;
2057
218M
  buflen = pktbuf->len;
2058
722M
  while (buflen)
2059
609M
    {
2060
609M
      n = *buffer++;
2061
609M
      buflen--;
2062
609M
      if (n == 255) /* 4 byte length header.  */
2063
255k
  {
2064
255k
    if (buflen < 4)
2065
83.8k
      goto too_short;
2066
171k
    n = buf32_to_size_t (buffer);
2067
171k
    buffer += 4;
2068
171k
    buflen -= 4;
2069
171k
  }
2070
608M
      else if (n >= 192) /* 4 byte special encoded length header.  */
2071
302k
  {
2072
302k
    if (buflen < 2)
2073
6.84k
      goto too_short;
2074
295k
    n = ((n - 192) << 8) + *buffer + 192;
2075
295k
    buffer++;
2076
295k
    buflen--;
2077
295k
  }
2078
608M
      if (buflen < n)
2079
77.7M
  goto too_short;
2080
531M
      if (!buflen)
2081
649k
        goto no_type_byte;
2082
530M
      type = *buffer;
2083
530M
      if (type & 0x80)
2084
62.2M
  {
2085
62.2M
    type &= 0x7f;
2086
62.2M
    *critical = 1;
2087
62.2M
  }
2088
468M
      else
2089
468M
  *critical = 0;
2090
530M
      if (!(++seq > reqseq))
2091
13.7k
  ;
2092
530M
      else if (reqtype == SIGSUBPKT_TEST_CRITICAL)
2093
31.0M
  {
2094
31.0M
    if (*critical)
2095
4.29M
      {
2096
4.29M
        if (n - 1 > buflen + 1)
2097
8.31k
    goto too_short;
2098
4.28M
        if (!can_handle_critical (buffer + 1, n - 1, type))
2099
4.10M
    {
2100
4.10M
      if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2101
4.10M
        log_info (_("subpacket of type %d has "
2102
0
        "critical bit set\n"), type);
2103
4.10M
      if (start)
2104
0
        *start = seq;
2105
4.10M
      return NULL; /* This is an error.  */
2106
4.10M
    }
2107
4.28M
      }
2108
31.0M
  }
2109
499M
      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
499M
      else if (type == reqtype) /* Found.  */
2113
22.3M
  {
2114
22.3M
    buffer++;
2115
22.3M
    n--;
2116
22.3M
    if (n > buflen)
2117
2.45k
      goto too_short;
2118
22.3M
    if (ret_n)
2119
9.64M
      *ret_n = n;
2120
22.3M
    offset = parse_one_sig_subpkt (buffer, n, type);
2121
22.3M
    switch (offset)
2122
22.3M
      {
2123
4.45k
      case -2:
2124
4.45k
        log_error ("subpacket of type %d too short\n", type);
2125
4.45k
        return NULL;
2126
40
      case -1:
2127
40
        return NULL;
2128
22.3M
      default:
2129
22.3M
        break;
2130
22.3M
      }
2131
22.3M
    if (start)
2132
8.67k
      *start = seq;
2133
22.3M
    return buffer + offset;
2134
22.3M
  }
2135
504M
      buffer += n;
2136
504M
      buflen -= n;
2137
504M
    }
2138
113M
  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
16.4M
    return buffer;
2144
2145
  /* Critical bit we don't understand. */
2146
96.8M
  if (start)
2147
624k
    *start = -1;
2148
96.8M
  return NULL;  /* End of packets; not found.  */
2149
2150
77.8M
 too_short:
2151
77.8M
  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
77.8M
  if (start)
2160
4.64M
    *start = -1;
2161
77.8M
  return NULL;
2162
2163
649k
 no_type_byte:
2164
649k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2165
649k
    log_info ("type octet missing in subpacket\n");
2166
649k
  if (start)
2167
474
    *start = -1;
2168
649k
  return NULL;
2169
113M
}
2170
2171
2172
const byte *
2173
parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype,
2174
      size_t *ret_n)
2175
213M
{
2176
213M
  return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL);
2177
213M
}
2178
2179
2180
const byte *
2181
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype)
2182
18.6M
{
2183
18.6M
  const byte *p;
2184
2185
18.6M
  p = parse_sig_subpkt (sig, 1, reqtype, NULL);
2186
18.6M
  if (!p)
2187
18.6M
    p = parse_sig_subpkt (sig, 0, reqtype, NULL);
2188
18.6M
  return p;
2189
18.6M
}
2190
2191
2192
/* Find all revocation keys.  Look in hashed area only.  */
2193
void
2194
parse_revkeys (PKT_signature * sig)
2195
4.64M
{
2196
4.64M
  const byte *revkey;
2197
4.64M
  int seq = 0;
2198
4.64M
  size_t len;
2199
2200
4.64M
  if (sig->sig_class != 0x1F)
2201
0
    return;
2202
2203
4.65M
  while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY,
2204
4.65M
                                    &len, &seq, NULL)))
2205
1.39k
    {
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.39k
      if ((len == 22 || len == 34)
2210
1.37k
          && (revkey[0] & 0x80))
2211
462
  {
2212
462
    sig->revkey = xrealloc (sig->revkey,
2213
462
          sizeof (struct revocation_key) *
2214
462
          (sig->numrevkeys + 1));
2215
2216
462
    sig->revkey[sig->numrevkeys].class = revkey[0];
2217
462
    sig->revkey[sig->numrevkeys].algid = revkey[1];
2218
462
          len -= 2;
2219
462
    sig->revkey[sig->numrevkeys].fprlen = len;
2220
462
    memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len);
2221
462
    memset (sig->revkey[sig->numrevkeys].fpr+len, 0,
2222
462
                  sizeof (sig->revkey[sig->numrevkeys].fpr) - len);
2223
462
    sig->numrevkeys++;
2224
462
  }
2225
1.39k
    }
2226
4.64M
}
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
13.9M
{
2237
13.9M
  int md5_len = 0;
2238
13.9M
  unsigned n;
2239
13.9M
  int is_v4or5 = 0;
2240
13.9M
  int rc = 0;
2241
13.9M
  int i, ndata;
2242
2243
13.9M
  if (pktlen < 16)
2244
6.34k
    {
2245
6.34k
      log_error ("packet(%d) too short\n", pkttype);
2246
6.34k
      if (list_mode)
2247
6.34k
        es_fputs (":signature packet: [too short]\n", listfp);
2248
6.34k
      goto leave;
2249
6.34k
    }
2250
13.9M
  sig->version = iobuf_get_noeof (inp);
2251
13.9M
  pktlen--;
2252
13.9M
  if (sig->version == 4 || sig->version == 5)
2253
13.9M
    is_v4or5 = 1;
2254
35.6k
  else if (sig->version != 2 && sig->version != 3)
2255
63
    {
2256
63
      log_error ("packet(%d) with unknown version %d\n",
2257
63
     pkttype, sig->version);
2258
63
      if (list_mode)
2259
63
        es_fputs (":signature packet: [unknown version]\n", listfp);
2260
63
      rc = gpg_error (GPG_ERR_INV_PACKET);
2261
63
      goto leave;
2262
63
    }
2263
2264
13.9M
  if (!is_v4or5)
2265
35.6k
    {
2266
35.6k
      if (pktlen == 0)
2267
0
  goto underflow;
2268
35.6k
      md5_len = iobuf_get_noeof (inp);
2269
35.6k
      pktlen--;
2270
35.6k
    }
2271
13.9M
  if (pktlen == 0)
2272
0
    goto underflow;
2273
13.9M
  sig->sig_class = iobuf_get_noeof (inp);
2274
13.9M
  pktlen--;
2275
13.9M
  if (!is_v4or5)
2276
35.6k
    {
2277
35.6k
      if (pktlen < 12)
2278
0
  goto underflow;
2279
35.6k
      sig->timestamp = read_32 (inp);
2280
35.6k
      pktlen -= 4;
2281
35.6k
      sig->keyid[0] = read_32 (inp);
2282
35.6k
      pktlen -= 4;
2283
35.6k
      sig->keyid[1] = read_32 (inp);
2284
35.6k
      pktlen -= 4;
2285
35.6k
    }
2286
13.9M
  if (pktlen < 2)
2287
2
    goto underflow;
2288
13.9M
  sig->pubkey_algo = iobuf_get_noeof (inp);
2289
13.9M
  pktlen--;
2290
13.9M
  sig->digest_algo = iobuf_get_noeof (inp);
2291
13.9M
  pktlen--;
2292
13.9M
  sig->flags.exportable = 1;
2293
13.9M
  sig->flags.revocable = 1;
2294
13.9M
  if (is_v4or5) /* Read subpackets.  */
2295
13.9M
    {
2296
13.9M
      if (pktlen < 2)
2297
0
  goto underflow;
2298
13.9M
      n = read_16 (inp);
2299
13.9M
      pktlen -= 2;  /* Length of hashed data. */
2300
13.9M
      if (pktlen < n)
2301
116
  goto underflow;
2302
13.9M
      if (n > 30000)
2303
19
  {
2304
19
    log_error ("signature packet: hashed data too long (%u)\n", n);
2305
19
          if (list_mode)
2306
19
            es_fprintf (listfp,
2307
0
                        ":signature packet: [hashed data too long (%u)]\n", n);
2308
19
          rc = GPG_ERR_INV_PACKET;
2309
19
    goto leave;
2310
19
  }
2311
13.9M
      if (n)
2312
13.9M
  {
2313
13.9M
    sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1);
2314
13.9M
    sig->hashed->size = n;
2315
13.9M
    sig->hashed->len = n;
2316
13.9M
    if (iobuf_read (inp, sig->hashed->data, n) != n)
2317
189
      {
2318
189
        log_error ("premature eof while reading "
2319
189
       "hashed signature data\n");
2320
189
              if (list_mode)
2321
189
                es_fputs (":signature packet: [premature eof]\n", listfp);
2322
189
        rc = -1;
2323
189
        goto leave;
2324
189
      }
2325
13.9M
    pktlen -= n;
2326
13.9M
  }
2327
13.9M
      if (pktlen < 2)
2328
2
  goto underflow;
2329
13.9M
      n = read_16 (inp);
2330
13.9M
      pktlen -= 2;  /* Length of unhashed data.  */
2331
13.9M
      if (pktlen < n)
2332
93
  goto underflow;
2333
13.9M
      if (n > 10000)
2334
11
  {
2335
11
    log_error ("signature packet: unhashed data too long (%u)\n", n);
2336
11
          if (list_mode)
2337
11
            es_fprintf (listfp,
2338
0
                        ":signature packet: [unhashed data too long (%u)]\n",
2339
0
                        n);
2340
11
          rc = GPG_ERR_INV_PACKET;
2341
11
    goto leave;
2342
11
  }
2343
13.9M
      if (n)
2344
13.8M
  {
2345
13.8M
    sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1);
2346
13.8M
    sig->unhashed->size = n;
2347
13.8M
    sig->unhashed->len = n;
2348
13.8M
    if (iobuf_read (inp, sig->unhashed->data, n) != n)
2349
39
      {
2350
39
        log_error ("premature eof while reading "
2351
39
       "unhashed signature data\n");
2352
39
              if (list_mode)
2353
39
                es_fputs (":signature packet: [premature eof]\n", listfp);
2354
39
        rc = -1;
2355
39
        goto leave;
2356
39
      }
2357
13.8M
    pktlen -= n;
2358
13.8M
  }
2359
13.9M
    }
2360
2361
13.9M
  if (pktlen < 2)
2362
2
    goto underflow;
2363
13.9M
  sig->digest_start[0] = iobuf_get_noeof (inp);
2364
13.9M
  pktlen--;
2365
13.9M
  sig->digest_start[1] = iobuf_get_noeof (inp);
2366
13.9M
  pktlen--;
2367
2368
13.9M
  if (is_v4or5 && sig->pubkey_algo)  /* Extract required information.  */
2369
13.9M
    {
2370
13.9M
      const byte *p;
2371
13.9M
      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
13.9M
      if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL)
2376
8.29M
    || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL))
2377
5.71M
  sig->flags.unknown_critical = 1;
2378
2379
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL);
2380
13.9M
      if (p)
2381
8.28M
  sig->timestamp = buf32_to_u32 (p);
2382
5.61M
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2383
5.61M
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2384
5.61M
        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
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len);
2390
13.9M
      if (p && len == 21 && p[0] == 4)
2391
9.19M
        {
2392
9.19M
          sig->keyid[0] = buf32_to_u32 (p + 1 + 12);
2393
9.19M
    sig->keyid[1] = buf32_to_u32 (p + 1 + 16);
2394
9.19M
  }
2395
4.71M
      else if (p && len == 33 && p[0] == 5)
2396
31
        {
2397
31
          sig->keyid[0] = buf32_to_u32 (p + 1 );
2398
31
    sig->keyid[1] = buf32_to_u32 (p + 1 + 4);
2399
31
  }
2400
4.71M
      else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER)))
2401
4.42M
        {
2402
4.42M
          sig->keyid[0] = buf32_to_u32 (p);
2403
4.42M
    sig->keyid[1] = buf32_to_u32 (p + 4);
2404
4.42M
  }
2405
286k
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2406
286k
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2407
286k
  log_info ("signature packet without keyid\n");
2408
2409
      /* Get the intended recipient (revocation subject) fpr. */
2410
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_INT_RCP_FPR, &len);
2411
13.9M
      if (p && len == 21 && p[0] == 4)
2412
2
        {
2413
2
          sig->rev_subject_info = xmalloc_clear (sizeof *sig->rev_subject_info);
2414
2415
2
          sig->rev_subject_info->fprlen = 20;
2416
2
          memcpy (sig->rev_subject_info->fpr, p + 1, 20);
2417
2
  }
2418
13.9M
      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
13.9M
      else
2426
13.9M
        {
2427
13.9M
         sig->rev_subject_info = NULL;
2428
13.9M
        }
2429
2430
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL);
2431
13.9M
      if (p && buf32_to_u32 (p))
2432
8.60k
  sig->expiredate = sig->timestamp + buf32_to_u32 (p);
2433
13.9M
      if (sig->expiredate && sig->expiredate <= make_timestamp ())
2434
8.55k
  sig->flags.expired = 1;
2435
2436
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL);
2437
13.9M
      if (p)
2438
57
  sig->flags.policy_url = 1;
2439
2440
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL);
2441
13.9M
      if (p)
2442
782
  sig->flags.pref_ks = 1;
2443
2444
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len);
2445
13.9M
      if (p && len)
2446
2.70k
        {
2447
2.70k
          char *mbox;
2448
2449
2.70k
          sig->signers_uid = try_make_printable_string (p, len, 0);
2450
2.70k
          if (!sig->signers_uid)
2451
0
            {
2452
0
              rc = gpg_error_from_syserror ();
2453
0
              goto leave;
2454
0
            }
2455
2.70k
          mbox = mailbox_from_userid (sig->signers_uid, 0);
2456
2.70k
          if (mbox)
2457
287
            {
2458
287
              xfree (sig->signers_uid);
2459
287
              sig->signers_uid = mbox;
2460
287
            }
2461
2.70k
        }
2462
2463
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL);
2464
13.9M
      if (p)
2465
0
        sig->flags.key_block = 1;
2466
2467
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL);
2468
13.9M
      if (p)
2469
140
  sig->flags.notation = 1;
2470
2471
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL);
2472
13.9M
      if (p && *p == 0)
2473
354
  sig->flags.revocable = 0;
2474
2475
13.9M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len);
2476
13.9M
      if (p && len == 2)
2477
110
  {
2478
110
    sig->trust_depth = p[0];
2479
110
    sig->trust_value = p[1];
2480
2481
    /* Only look for a regexp if there is also a trust
2482
       subpacket. */
2483
110
    sig->trust_regexp =
2484
110
      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
110
    if (len == 0)
2489
1
      sig->trust_regexp = NULL;
2490
110
  }
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
13.9M
      p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE);
2498
13.9M
      if (p && *p == 0)
2499
229
  sig->flags.exportable = 0;
2500
2501
      /* Find all revocation keys.  */
2502
13.9M
      if (sig->sig_class == 0x1F)
2503
4.64M
  parse_revkeys (sig);
2504
13.9M
    }
2505
2506
13.9M
  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
13.9M
  ndata = pubkey_get_nsig (sig->pubkey_algo);
2523
13.9M
  if (!ndata)
2524
131k
    {
2525
131k
      if (list_mode)
2526
131k
  es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo);
2527
131k
      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
131k
      if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8))
2532
14
  {
2533
    /* We include a limit to avoid too trivial DoS attacks by
2534
       having gpg allocate too much memory.  */
2535
14
    log_error ("signature packet: too much data\n");
2536
14
    rc = GPG_ERR_INV_PACKET;
2537
14
  }
2538
131k
      else
2539
131k
  {
2540
131k
          void *tmpp;
2541
2542
131k
          tmpp = read_rest (inp, pktlen);
2543
131k
    sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2544
131k
    pktlen = 0;
2545
131k
  }
2546
131k
    }
2547
13.8M
  else
2548
13.8M
    {
2549
41.1M
      for (i = 0; i < ndata; i++)
2550
27.2M
  {
2551
27.2M
    n = pktlen;
2552
27.2M
          if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA
2553
26.7M
              || sig->pubkey_algo == PUBKEY_ALGO_EDDSA)
2554
26.9M
            sig->data[i] = sos_read (inp, &n, 0);
2555
344k
          else
2556
344k
            sig->data[i] = mpi_read (inp, &n, 0);
2557
27.2M
    pktlen -= n;
2558
27.2M
    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
27.2M
    if (!sig->data[i])
2565
218
      rc = GPG_ERR_INV_PACKET;
2566
27.2M
  }
2567
13.8M
    }
2568
2569
13.9M
 leave:
2570
13.9M
  iobuf_skip_rest (inp, pktlen, 0);
2571
13.9M
  return rc;
2572
2573
215
 underflow:
2574
215
  log_error ("packet(%d) too short\n", pkttype);
2575
215
  if (list_mode)
2576
215
    es_fputs (":signature packet: [too short]\n", listfp);
2577
2578
215
  iobuf_skip_rest (inp, pktlen, 0);
2579
2580
215
  return GPG_ERR_INV_PACKET;
2581
13.9M
}
2582
2583
2584
static int
2585
parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen,
2586
       PKT_onepass_sig * ops)
2587
478
{
2588
478
  int version;
2589
478
  int rc = 0;
2590
2591
478
  if (pktlen < 13)
2592
4
    {
2593
4
      log_error ("packet(%d) too short\n", pkttype);
2594
4
      if (list_mode)
2595
4
        es_fputs (":onepass_sig packet: [too short]\n", listfp);
2596
4
      rc = gpg_error (GPG_ERR_INV_PACKET);
2597
4
      goto leave;
2598
4
    }
2599
474
  version = iobuf_get_noeof (inp);
2600
474
  pktlen--;
2601
474
  if (version != 3)
2602
21
    {
2603
21
      log_error ("onepass_sig with unknown version %d\n", version);
2604
21
      if (list_mode)
2605
21
        es_fputs (":onepass_sig packet: [unknown version]\n", listfp);
2606
21
      rc = gpg_error (GPG_ERR_INV_PACKET);
2607
21
      goto leave;
2608
21
    }
2609
453
  ops->sig_class = iobuf_get_noeof (inp);
2610
453
  pktlen--;
2611
453
  ops->digest_algo = iobuf_get_noeof (inp);
2612
453
  pktlen--;
2613
453
  ops->pubkey_algo = iobuf_get_noeof (inp);
2614
453
  pktlen--;
2615
453
  ops->keyid[0] = read_32 (inp);
2616
453
  pktlen -= 4;
2617
453
  ops->keyid[1] = read_32 (inp);
2618
453
  pktlen -= 4;
2619
453
  ops->last = iobuf_get_noeof (inp);
2620
453
  pktlen--;
2621
453
  if (list_mode)
2622
453
    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
478
 leave:
2632
478
  iobuf_skip_rest (inp, pktlen, 0);
2633
478
  return rc;
2634
453
}
2635
2636
2637
static int
2638
parse_key (IOBUF inp, int pkttype, unsigned long pktlen,
2639
     byte * hdr, int hdrlen, PACKET * pkt)
2640
792k
{
2641
792k
  gpg_error_t err = 0;
2642
792k
  int i, version, algorithm;
2643
792k
  unsigned long timestamp, expiredate, max_expiredate;
2644
792k
  int npkey, nskey;
2645
792k
  u32 keyid[2];
2646
792k
  PKT_public_key *pk;
2647
792k
  int is_v5;
2648
792k
  unsigned int pkbytes; /* For v5 keys: Number of bytes in the public
2649
                         * key material.  For v4 keys: 0.  */
2650
2651
792k
  (void) hdr;
2652
2653
792k
  pk = pkt->pkt.public_key; /* PK has been cleared. */
2654
2655
792k
  version = iobuf_get_noeof (inp);
2656
792k
  pktlen--;
2657
792k
  if (pkttype == PKT_PUBLIC_SUBKEY && version == '#')
2658
1.35k
    {
2659
      /* Early versions of G10 used the old PGP comments packets;
2660
       * luckily all those comments are started by a hash.  */
2661
1.35k
      if (list_mode)
2662
0
  {
2663
0
    es_fprintf (listfp, ":rfc1991 comment packet: \"");
2664
0
    for (; pktlen; pktlen--)
2665
0
      {
2666
0
        int c;
2667
0
        c = iobuf_get (inp);
2668
0
              if (c == -1)
2669
0
                break; /* Ooops: shorter than indicated.  */
2670
0
        if (c >= ' ' && c <= 'z')
2671
0
    es_putc (c, listfp);
2672
0
        else
2673
0
    es_fprintf (listfp, "\\x%02x", c);
2674
0
      }
2675
0
    es_fprintf (listfp, "\"\n");
2676
0
  }
2677
1.35k
      iobuf_skip_rest (inp, pktlen, 0);
2678
1.35k
      return 0;
2679
1.35k
    }
2680
791k
  else if (version == 4)
2681
790k
    is_v5 = 0;
2682
1.49k
  else if (version == 5)
2683
428
    is_v5 = 1;
2684
1.06k
  else if (version == 2 || version == 3)
2685
939
    {
2686
      /* Not anymore supported since 2.1.  Use an older gpg version
2687
       * (i.e. gpg 1.4) to parse v3 packets.  */
2688
939
      if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2689
939
        log_info ("packet(%d) with obsolete version %d\n", pkttype, version);
2690
939
      if (list_mode)
2691
939
        es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version);
2692
939
      pk->version = version;
2693
939
      err = gpg_error (GPG_ERR_LEGACY_KEY);
2694
939
      goto leave;
2695
939
    }
2696
126
  else
2697
126
    {
2698
126
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
2699
126
      if (list_mode)
2700
126
        es_fputs (":key packet: [unknown version]\n", listfp);
2701
126
      err = gpg_error (GPG_ERR_INV_PACKET);
2702
126
      goto leave;
2703
126
    }
2704
2705
790k
  if (pktlen < (is_v5? 15:11))
2706
2
    {
2707
2
      log_error ("packet(%d) too short\n", pkttype);
2708
2
      if (list_mode)
2709
2
        es_fputs (":key packet: [too short]\n", listfp);
2710
2
      err = gpg_error (GPG_ERR_INV_PACKET);
2711
2
      goto leave;
2712
2
    }
2713
790k
  else if (pktlen > MAX_KEY_PACKET_LENGTH)
2714
8
    {
2715
8
      log_error ("packet(%d) too large\n", pkttype);
2716
8
      if (list_mode)
2717
8
        es_fputs (":key packet: [too large]\n", listfp);
2718
8
      err = gpg_error (GPG_ERR_INV_PACKET);
2719
8
      goto leave;
2720
8
    }
2721
2722
790k
  timestamp = read_32 (inp);
2723
790k
  pktlen -= 4;
2724
790k
  expiredate = 0;   /* have to get it from the selfsignature */
2725
790k
  max_expiredate = 0;
2726
790k
  algorithm = iobuf_get_noeof (inp);
2727
790k
  pktlen--;
2728
790k
  if (is_v5)
2729
426
    {
2730
426
      pkbytes = read_32 (inp);
2731
426
      pktlen -= 4;
2732
426
    }
2733
790k
  else
2734
790k
    pkbytes = 0;
2735
2736
790k
  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
790k
  pk->timestamp = timestamp;
2752
790k
  pk->expiredate = expiredate;
2753
790k
  pk->max_expiredate = max_expiredate;
2754
790k
  pk->hdrbytes = hdrlen;
2755
790k
  pk->version = version;
2756
790k
  pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY);
2757
790k
  pk->pubkey_algo = algorithm;
2758
2759
790k
  nskey = pubkey_get_nskey (algorithm);
2760
790k
  npkey = pubkey_get_npkey (algorithm);
2761
790k
  if (!npkey)
2762
6.88k
    {
2763
6.88k
      if (list_mode)
2764
6.88k
  es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm);
2765
6.88k
      unknown_pubkey_warning (algorithm);
2766
6.88k
    }
2767
2768
790k
  if (!npkey)
2769
6.88k
    {
2770
      /* Unknown algorithm - put data into an opaque MPI.  */
2771
6.88k
      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
6.88k
      pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2775
6.88k
      pktlen = 0;
2776
6.88k
      goto leave;
2777
6.88k
    }
2778
783k
  else
2779
783k
    {
2780
3.11M
      for (i = 0; i < npkey; i++)
2781
2.33M
        {
2782
2.33M
          if (    (algorithm == PUBKEY_ALGO_ECDSA && (i == 0))
2783
2.33M
               || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0))
2784
2.31M
               || (algorithm == PUBKEY_ALGO_ECDH  && (i == 0 || i == 2))
2785
791k
               || (algorithm == PUBKEY_ALGO_KYBER && (i == 0)))
2786
1.53M
            {
2787
              /* Read the OID (i==0) or the KDF params (i==2).  */
2788
1.53M
        err = read_sized_octet_string (inp, &pktlen, pk->pkey+i);
2789
1.53M
            }
2790
791k
          else if (algorithm == PUBKEY_ALGO_KYBER && i == 2)
2791
49
            {
2792
              /* Read the four-octet count prefixed Kyber public key.  */
2793
49
        err = read_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i);
2794
49
            }
2795
791k
          else
2796
791k
            {
2797
              /* Read MPI or SOS.  */
2798
791k
              unsigned int n = pktlen;
2799
791k
              if (algorithm == PUBKEY_ALGO_ECDSA
2800
790k
                  || algorithm == PUBKEY_ALGO_EDDSA
2801
776k
                  || algorithm == PUBKEY_ALGO_ECDH
2802
14.1k
                  || algorithm == PUBKEY_ALGO_KYBER)
2803
777k
                pk->pkey[i] = sos_read (inp, &n, 0);
2804
14.0k
              else
2805
14.0k
                pk->pkey[i] = mpi_read (inp, &n, 0);
2806
791k
              pktlen -= n;
2807
791k
              if (!pk->pkey[i])
2808
134
                err = gpg_error (GPG_ERR_INV_PACKET);
2809
791k
            }
2810
2.33M
          if (err)
2811
228
            goto leave;
2812
2.33M
        }
2813
783k
      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
783k
    }
2842
783k
  if (list_mode)
2843
0
    keyid_from_pk (pk, keyid);
2844
2845
783k
  if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY)
2846
4.61k
    {
2847
4.61k
      struct seckey_info *ski;
2848
4.61k
      byte temp[16];
2849
4.61k
      size_t snlen = 0;
2850
4.61k
      unsigned int skbytes;
2851
2852
4.61k
      if (pktlen < 1)
2853
1
        {
2854
1
          err = gpg_error (GPG_ERR_INV_PACKET);
2855
1
          goto leave;
2856
1
        }
2857
2858
4.61k
      pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
2859
4.61k
      if (!pk->seckey_info)
2860
0
        {
2861
0
          err = gpg_error_from_syserror ();
2862
0
          goto leave;
2863
0
        }
2864
2865
4.61k
      ski->algo = iobuf_get_noeof (inp);
2866
4.61k
      pktlen--;
2867
2868
4.61k
      if (is_v5)
2869
376
        {
2870
376
          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
376
          if (!pktlen)
2875
1
            {
2876
1
              err = gpg_error (GPG_ERR_INV_PACKET);
2877
1
              goto leave;
2878
1
            }
2879
375
          protcount = iobuf_get_noeof (inp);
2880
375
          pktlen--;
2881
375
          if (list_mode)
2882
375
            es_fprintf (listfp, "\tprotbytes: %u\n", protcount);
2883
375
        }
2884
2885
4.61k
      if (ski->algo)
2886
3.78k
  {
2887
3.78k
    ski->is_protected = 1;
2888
3.78k
    ski->s2k.count = 0;
2889
3.78k
    if (ski->algo == 253)
2890
1
      {
2891
1
              if (list_mode)
2892
1
                es_fprintf (listfp,
2893
0
                            "\tS2K pseudo algo %d is not yet supported\n",
2894
0
                            ski->algo);
2895
1
              err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2896
1
              goto leave;
2897
1
            }
2898
3.78k
          else if (ski->algo == 254 || ski->algo == 255)
2899
2.94k
      {
2900
2.94k
              if (pktlen < 3)
2901
2
    {
2902
2
      err = gpg_error (GPG_ERR_INV_PACKET);
2903
2
      goto leave;
2904
2
    }
2905
2906
2.94k
              ski->sha1chk = (ski->algo == 254);
2907
2.94k
        ski->algo = iobuf_get_noeof (inp);
2908
2.94k
        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
2.94k
        ski->s2k.mode = iobuf_get_noeof (inp);
2913
2.94k
        pktlen--;
2914
2.94k
        ski->s2k.hash_algo = iobuf_get_noeof (inp);
2915
2.94k
        pktlen--;
2916
        /* Check for the special GNU extension.  */
2917
2.94k
        if (ski->s2k.mode == 101)
2918
2.17k
    {
2919
10.8k
      for (i = 0; i < 4 && pktlen; i++, pktlen--)
2920
8.70k
        temp[i] = iobuf_get_noeof (inp);
2921
2.17k
      if (i < 4 || memcmp (temp, "GNU", 3))
2922
41
        {
2923
41
          if (list_mode)
2924
41
      es_fprintf (listfp, "\tunknown S2K %d\n",
2925
0
                                    ski->s2k.mode);
2926
41
          err = gpg_error (GPG_ERR_INV_PACKET);
2927
41
          goto leave;
2928
41
        }
2929
      /* Here we know that it is a GNU extension.  What
2930
       * follows is the GNU protection mode: All values
2931
       * have special meanings and they are mapped to MODE
2932
       * with a base of 1000.  */
2933
2.13k
      ski->s2k.mode = 1000 + temp[3];
2934
2.13k
    }
2935
2936
              /* Read the salt.  */
2937
2.90k
        if (ski->s2k.mode == 3 || ski->s2k.mode == 1)
2938
622
    {
2939
5.57k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
2940
4.95k
        temp[i] = iobuf_get_noeof (inp);
2941
622
                  if (i < 8)
2942
4
                    {
2943
4
          err = gpg_error (GPG_ERR_INV_PACKET);
2944
4
          goto leave;
2945
4
                    }
2946
618
      memcpy (ski->s2k.salt, temp, 8);
2947
618
    }
2948
2949
              /* Check the mode.  */
2950
2.89k
        switch (ski->s2k.mode)
2951
2.89k
    {
2952
116
    case 0:
2953
116
      if (list_mode)
2954
116
        es_fprintf (listfp, "\tsimple S2K");
2955
116
      break;
2956
115
    case 1:
2957
115
      if (list_mode)
2958
115
        es_fprintf (listfp, "\tsalted S2K");
2959
115
      break;
2960
503
    case 3:
2961
503
      if (list_mode)
2962
503
        es_fprintf (listfp, "\titer+salt S2K");
2963
503
      break;
2964
573
    case 1001:
2965
573
      if (list_mode)
2966
573
        es_fprintf (listfp, "\tgnu-dummy");
2967
573
      break;
2968
687
    case 1002:
2969
687
      if (list_mode)
2970
687
        es_fprintf (listfp, "\tgnu-divert-to-card");
2971
687
      break;
2972
869
    case 1003:
2973
869
      if (list_mode)
2974
869
        es_fprintf (listfp, "\tgnu-mode1003");
2975
869
      break;
2976
33
    default:
2977
33
      if (list_mode)
2978
33
        es_fprintf (listfp, "\tunknown %sS2K %d\n",
2979
0
                                ski->s2k.mode < 1000 ? "" : "GNU ",
2980
0
                                ski->s2k.mode);
2981
33
      err = gpg_error (GPG_ERR_INV_PACKET);
2982
33
      goto leave;
2983
2.89k
    }
2984
2985
              /* Print some info.  */
2986
2.86k
        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
2.86k
              if (list_mode)
2999
2.86k
                es_putc ('\n', listfp);
3000
3001
              /* Read remaining protection parameters.  */
3002
2.86k
        if (ski->s2k.mode == 3)
3003
503
    {
3004
503
      if (pktlen < 1)
3005
1
        {
3006
1
          err = gpg_error (GPG_ERR_INV_PACKET);
3007
1
          goto leave;
3008
1
        }
3009
502
      ski->s2k.count = iobuf_get_noeof (inp);
3010
502
      pktlen--;
3011
502
      if (list_mode)
3012
502
        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
502
    }
3016
2.36k
        else if (ski->s2k.mode == 1002)
3017
687
    {
3018
      /* Read the serial number. */
3019
687
      if (pktlen < 1)
3020
0
        {
3021
0
          err = gpg_error (GPG_ERR_INV_PACKET);
3022
0
          goto leave;
3023
0
        }
3024
687
      snlen = iobuf_get (inp);
3025
687
      pktlen--;
3026
687
      if (pktlen < snlen || snlen == (size_t)(-1))
3027
5
        {
3028
5
          err = gpg_error (GPG_ERR_INV_PACKET);
3029
5
          goto leave;
3030
5
        }
3031
687
    }
3032
2.86k
      }
3033
841
    else /* Old version; no S2K, so we set mode to 0, hash MD5.  */
3034
841
      {
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
841
        ski->s2k.mode = 0;
3039
841
        ski->s2k.hash_algo = DIGEST_ALGO_MD5;
3040
841
        if (list_mode)
3041
841
    es_fprintf (listfp, "\tprotect algo: %d  (hash algo: %d)\n",
3042
0
                            ski->algo, ski->s2k.hash_algo);
3043
841
      }
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
3.69k
    ski->ivlen = openpgp_cipher_blocklen (ski->algo);
3055
3.69k
    log_assert (ski->ivlen <= sizeof (temp));
3056
3057
3.69k
    if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003)
3058
1.44k
      ski->ivlen = 0;
3059
2.25k
    else if (ski->s2k.mode == 1002)
3060
682
      ski->ivlen = snlen < 16 ? snlen : 16;
3061
3062
3.69k
    if (pktlen < ski->ivlen)
3063
3
      {
3064
3
              err = gpg_error (GPG_ERR_INV_PACKET);
3065
3
        goto leave;
3066
3
      }
3067
17.7k
    for (i = 0; i < ski->ivlen; i++, pktlen--)
3068
14.0k
      temp[i] = iobuf_get_noeof (inp);
3069
3.69k
    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
3.69k
    memcpy (ski->iv, temp, ski->ivlen);
3079
3.69k
  }
3080
3081
      /* Skip count of secret key material.  */
3082
4.52k
      if (is_v5)
3083
369
        {
3084
369
          if (pktlen < 4)
3085
4
            {
3086
4
              err = gpg_error (GPG_ERR_INV_PACKET);
3087
4
              goto leave;
3088
4
            }
3089
365
          skbytes = read_32 (inp);
3090
365
          pktlen -= 4;
3091
365
          if (list_mode)
3092
365
            es_fprintf (listfp, "\tskbytes: %u\n", skbytes);
3093
365
        }
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
4.51k
      if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002)
3100
1.25k
  {
3101
    /* Better set some dummy stuff here.  */
3102
1.25k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3103
1.25k
             xstrdup ("dummydata"),
3104
1.25k
             10 * 8);
3105
1.25k
    pktlen = 0;
3106
1.25k
  }
3107
3.26k
      else if (ski->s2k.mode == 1003)
3108
869
  {
3109
869
          void *tmpp;
3110
3111
869
    if (pktlen < 2) /* At least two bytes for parenthesis.  */
3112
0
      {
3113
0
              err = gpg_error (GPG_ERR_INV_PACKET);
3114
0
        goto leave;
3115
0
      }
3116
3117
869
          tmpp = read_rest (inp, pktlen);
3118
869
          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
869
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3134
869
             tmpp, tmpp? pktlen * 8 : 0);
3135
869
          pktlen = 0;
3136
869
  }
3137
2.39k
      else if (ski->is_protected)
3138
1.56k
  {
3139
1.56k
          void *tmpp;
3140
3141
1.56k
    if (pktlen < 2) /* At least two bytes for the length.  */
3142
6
      {
3143
6
              err = gpg_error (GPG_ERR_INV_PACKET);
3144
6
        goto leave;
3145
6
      }
3146
3147
    /* Ugly: The length is encrypted too, so we read all stuff
3148
     * up to the end of the packet into the first SKEY
3149
     * element.
3150
           * FIXME: We can do better for v5 keys.  */
3151
3152
1.56k
          tmpp = read_rest (inp, pktlen);
3153
1.56k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3154
1.56k
             tmpp, tmpp? pktlen * 8 : 0);
3155
          /* Mark that MPI as protected - we need this information for
3156
           * importing a key.  The OPAQUE flag can't be used because
3157
           * we also store public EdDSA values in opaque MPIs.  */
3158
1.56k
          if (pk->pkey[npkey])
3159
1.56k
            gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1);
3160
1.56k
    pktlen = 0;
3161
1.56k
    if (list_mode)
3162
1.56k
            es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey);
3163
1.56k
  }
3164
825
      else
3165
825
  {
3166
          /* Not encrypted.  */
3167
2.05k
    for (i = npkey; i < nskey; i++)
3168
1.23k
      {
3169
3170
1.23k
              if (pktlen < 2) /* At least two bytes for the length.  */
3171
6
                {
3172
6
                  err = gpg_error (GPG_ERR_INV_PACKET);
3173
6
                  goto leave;
3174
6
                }
3175
1.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
1.23k
              else
3182
1.23k
                {
3183
1.23k
                  unsigned int n = pktlen;
3184
3185
1.23k
                  if (algorithm == PUBKEY_ALGO_ECDSA
3186
1.09k
                      || algorithm == PUBKEY_ALGO_EDDSA
3187
1.03k
                      || algorithm == PUBKEY_ALGO_ECDH
3188
929
                      || algorithm == PUBKEY_ALGO_KYBER)
3189
303
                    pk->pkey[i] = sos_read (inp, &n, 0);
3190
929
                  else
3191
929
                    pk->pkey[i] = mpi_read (inp, &n, 0);
3192
1.23k
                  pktlen -= n;
3193
1.23k
                }
3194
3195
1.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
1.23k
        if (!pk->pkey[i])
3203
94
    err = gpg_error (GPG_ERR_INV_PACKET);
3204
1.23k
      }
3205
819
    if (err)
3206
33
      goto leave;
3207
3208
786
    if (pktlen < 2)
3209
1
      {
3210
1
              if (opt.verbose)
3211
1
                log_info ("checksum is missing (remaining bytes: %lu)\n",
3212
0
                          pktlen);
3213
1
              err = gpg_error (GPG_ERR_INV_PACKET);
3214
1
        goto leave;
3215
1
      }
3216
785
    ski->csum = read_16 (inp);
3217
785
    pktlen -= 2;
3218
785
    if (list_mode)
3219
785
            es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum);
3220
785
  }
3221
4.51k
    }
3222
3223
  /* Note that KEYID below has been initialized above in list_mode.  */
3224
783k
  if (list_mode)
3225
783k
    es_fprintf (listfp, "\tkeyid: %08lX%08lX\n",
3226
0
                (ulong) keyid[0], (ulong) keyid[1]);
3227
3228
791k
 leave:
3229
791k
  iobuf_skip_rest (inp, pktlen, 0);
3230
791k
  return err;
3231
783k
}
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
8.90k
{
3240
8.90k
  size_t n;
3241
8.90k
  int count = 0;
3242
8.90k
  struct user_attribute *attribs = NULL;
3243
8.90k
  const byte *buffer = uid->attrib_data;
3244
8.90k
  int buflen = uid->attrib_len;
3245
8.90k
  byte type;
3246
3247
8.90k
  xfree (uid->attribs);
3248
3249
11.7k
  while (buflen)
3250
4.30k
    {
3251
4.30k
      n = *buffer++;
3252
4.30k
      buflen--;
3253
4.30k
      if (n == 255)  /* 4 byte length header.  */
3254
255
  {
3255
255
    if (buflen < 4)
3256
125
      goto too_short;
3257
130
    n = buf32_to_size_t (buffer);
3258
130
    buffer += 4;
3259
130
    buflen -= 4;
3260
130
  }
3261
4.05k
      else if (n >= 192)  /* 2 byte special encoded length header.  */
3262
577
  {
3263
577
    if (buflen < 2)
3264
253
      goto too_short;
3265
324
    n = ((n - 192) << 8) + *buffer + 192;
3266
324
    buffer++;
3267
324
    buflen--;
3268
324
  }
3269
3.92k
      if (buflen < n)
3270
994
  goto too_short;
3271
3272
2.93k
      if (!n)
3273
84
        {
3274
          /* Too short to encode the subpacket type.  */
3275
84
          if (opt.verbose)
3276
84
            log_info ("attribute subpacket too short\n");
3277
84
          break;
3278
84
        }
3279
3280
2.84k
      attribs = xrealloc (attribs,
3281
2.84k
                          (count + 1) * sizeof (struct user_attribute));
3282
2.84k
      memset (&attribs[count], 0, sizeof (struct user_attribute));
3283
3284
2.84k
      type = *buffer;
3285
2.84k
      buffer++;
3286
2.84k
      buflen--;
3287
2.84k
      n--;
3288
3289
2.84k
      attribs[count].type = type;
3290
2.84k
      attribs[count].data = buffer;
3291
2.84k
      attribs[count].len = n;
3292
2.84k
      buffer += n;
3293
2.84k
      buflen -= n;
3294
2.84k
      count++;
3295
2.84k
    }
3296
3297
7.53k
  uid->attribs = attribs;
3298
7.53k
  uid->numattribs = count;
3299
7.53k
  return count;
3300
3301
1.37k
 too_short:
3302
1.37k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
3303
1.37k
    log_info ("buffer shorter than attribute subpacket\n");
3304
1.37k
  uid->attribs = attribs;
3305
1.37k
  uid->numattribs = count;
3306
1.37k
  return count;
3307
8.90k
}
3308
3309
3310
static int
3311
parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3312
35.4k
{
3313
35.4k
  byte *p;
3314
3315
  /* Cap the size of a user ID at 2k: a value absurdly large enough
3316
     that there is no sane user ID string (which is printable text
3317
     as of RFC2440bis) that won't fit in it, but yet small enough to
3318
     avoid allocation problems.  A large pktlen may not be
3319
     allocatable, and a very large pktlen could actually cause our
3320
     allocation to wrap around in xmalloc to a small number. */
3321
3322
35.4k
  if (pktlen > MAX_UID_PACKET_LENGTH)
3323
35
    {
3324
35
      log_error ("packet(%d) too large\n", pkttype);
3325
35
      if (list_mode)
3326
35
        es_fprintf (listfp, ":user ID packet: [too large]\n");
3327
35
      iobuf_skip_rest (inp, pktlen, 0);
3328
35
      return GPG_ERR_INV_PACKET;
3329
35
    }
3330
3331
35.3k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen);
3332
35.3k
  packet->pkt.user_id->len = pktlen;
3333
35.3k
  packet->pkt.user_id->ref = 1;
3334
3335
35.3k
  p = packet->pkt.user_id->name;
3336
460k
  for (; pktlen; pktlen--, p++)
3337
425k
    *p = iobuf_get_noeof (inp);
3338
35.3k
  *p = 0;
3339
3340
35.3k
  if (list_mode)
3341
0
    {
3342
0
      int n = packet->pkt.user_id->len;
3343
0
      es_fprintf (listfp, ":user ID packet: \"");
3344
      /* fixme: Hey why don't we replace this with es_write_sanitized?? */
3345
0
      for (p = packet->pkt.user_id->name; n; p++, n--)
3346
0
  {
3347
0
    if (*p >= ' ' && *p <= 'z')
3348
0
      es_putc (*p, listfp);
3349
0
    else
3350
0
      es_fprintf (listfp, "\\x%02x", *p);
3351
0
  }
3352
0
      es_fprintf (listfp, "\"\n");
3353
0
    }
3354
35.3k
  return 0;
3355
35.4k
}
3356
3357
3358
void
3359
make_attribute_uidname (PKT_user_id * uid, size_t max_namelen)
3360
8.90k
{
3361
8.90k
  log_assert (max_namelen > 70);
3362
8.90k
  if (uid->numattribs <= 0)
3363
6.45k
    sprintf (uid->name, "[bad attribute packet of size %lu]",
3364
6.45k
       uid->attrib_len);
3365
2.44k
  else if (uid->numattribs > 1)
3366
286
    sprintf (uid->name, "[%d attributes of size %lu]",
3367
286
       uid->numattribs, uid->attrib_len);
3368
2.15k
  else
3369
2.15k
    {
3370
      /* Only one attribute, so list it as the "user id" */
3371
3372
2.15k
      if (uid->attribs->type == ATTRIB_IMAGE)
3373
1.91k
  {
3374
1.91k
    u32 len;
3375
1.91k
    byte type;
3376
3377
1.91k
    if (parse_image_header (uid->attribs, &type, &len))
3378
1.01k
      sprintf (uid->name, "[%.20s image of size %lu]",
3379
1.01k
         image_type_to_string (type, 1), (ulong) len);
3380
893
    else
3381
893
      sprintf (uid->name, "[invalid image]");
3382
1.91k
  }
3383
245
      else
3384
245
  sprintf (uid->name, "[unknown attribute of size %lu]",
3385
245
     (ulong) uid->attribs->len);
3386
2.15k
    }
3387
3388
8.90k
  uid->len = strlen (uid->name);
3389
8.90k
}
3390
3391
3392
static int
3393
parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen,
3394
     PACKET * packet)
3395
8.90k
{
3396
8.90k
  byte *p;
3397
3398
8.90k
  (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
8.90k
  if (pktlen > MAX_ATTR_PACKET_LENGTH)
3404
5
    {
3405
5
      log_error ("packet(%d) too large\n", pkttype);
3406
5
      if (list_mode)
3407
5
        es_fprintf (listfp, ":attribute packet: [too large]\n");
3408
5
      iobuf_skip_rest (inp, pktlen, 0);
3409
5
      return GPG_ERR_INV_PACKET;
3410
5
    }
3411
3412
8.90k
#define EXTRA_UID_NAME_SPACE 71
3413
8.90k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id
3414
8.90k
               + EXTRA_UID_NAME_SPACE);
3415
8.90k
  packet->pkt.user_id->ref = 1;
3416
8.90k
  packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1);
3417
8.90k
  packet->pkt.user_id->attrib_len = pktlen;
3418
3419
8.90k
  p = packet->pkt.user_id->attrib_data;
3420
291M
  for (; pktlen; pktlen--, p++)
3421
291M
    *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
8.90k
  parse_attribute_subpkts (packet->pkt.user_id);
3427
3428
8.90k
  make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE);
3429
3430
8.90k
  if (list_mode)
3431
0
    {
3432
0
      es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name);
3433
0
    }
3434
8.90k
  return 0;
3435
8.90k
}
3436
3437
3438
static int
3439
parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3440
4.98k
{
3441
4.98k
  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
4.98k
  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
4.97k
  packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
3457
4.97k
  packet->pkt.comment->len = pktlen;
3458
4.97k
  p = packet->pkt.comment->data;
3459
279k
  for (; pktlen; pktlen--, p++)
3460
274k
    *p = iobuf_get_noeof (inp);
3461
3462
4.97k
  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
4.97k
  return 0;
3477
4.98k
}
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
13.9M
{
3487
13.9M
  gpg_error_t err;
3488
13.9M
  iobuf_t inp = ctx->inp;
3489
13.9M
  PKT_ring_trust rt = {0};
3490
13.9M
  int c;
3491
13.9M
  int not_gpg = 0;
3492
3493
13.9M
  if (!pktlen)
3494
697
    {
3495
697
      if (list_mode)
3496
697
  es_fprintf (listfp, ":trust packet: empty\n");
3497
697
      err = 0;
3498
697
      goto leave;
3499
697
    }
3500
3501
13.9M
  c = iobuf_get_noeof (inp);
3502
13.9M
  pktlen--;
3503
13.9M
  rt.trustval = c;
3504
13.9M
  if (pktlen)
3505
13.9M
    {
3506
13.9M
      if (!c)
3507
13.9M
        {
3508
13.9M
          c = iobuf_get_noeof (inp);
3509
          /* We require that bit 7 of the sigcache is 0 (easier
3510
           * eof handling).  */
3511
13.9M
          if (!(c & 0x80))
3512
13.9M
            rt.sigcache = c;
3513
13.9M
        }
3514
1.15k
      else
3515
1.15k
        iobuf_get_noeof (inp);  /* Dummy read.  */
3516
13.9M
      pktlen--;
3517
13.9M
    }
3518
3519
  /* Next is the optional subtype.  */
3520
13.9M
  if (pktlen > 3)
3521
13.9M
    {
3522
13.9M
      char tmp[4];
3523
13.9M
      tmp[0] = iobuf_get_noeof (inp);
3524
13.9M
      tmp[1] = iobuf_get_noeof (inp);
3525
13.9M
      tmp[2] = iobuf_get_noeof (inp);
3526
13.9M
      tmp[3] = iobuf_get_noeof (inp);
3527
13.9M
      pktlen -= 4;
3528
13.9M
      if (!memcmp (tmp, "gpg", 3))
3529
13.9M
        rt.subtype = tmp[3];
3530
350
      else
3531
350
        not_gpg = 1;
3532
13.9M
    }
3533
  /* If it is a key or uid subtype read the remaining data.  */
3534
13.9M
  if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID)
3535
31.5k
      && pktlen >= 6 )
3536
31.4k
    {
3537
31.4k
      int i;
3538
31.4k
      unsigned int namelen;
3539
3540
31.4k
      rt.keyorg = iobuf_get_noeof (inp);
3541
31.4k
      pktlen--;
3542
31.4k
      rt.keyupdate = read_32 (inp);
3543
31.4k
      pktlen -= 4;
3544
31.4k
      namelen = iobuf_get_noeof (inp);
3545
31.4k
      pktlen--;
3546
31.4k
      if (namelen && pktlen)
3547
114
        {
3548
114
          rt.url = xtrymalloc (namelen + 1);
3549
114
          if (!rt.url)
3550
0
            {
3551
0
              err = gpg_error_from_syserror ();
3552
0
              goto leave;
3553
0
            }
3554
2.00k
          for (i = 0; pktlen && i < namelen; pktlen--, i++)
3555
1.89k
            rt.url[i] = iobuf_get_noeof (inp);
3556
114
          rt.url[i] = 0;
3557
114
        }
3558
31.4k
    }
3559
3560
13.9M
  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
13.9M
  if (!ctx->last_pkt.pkt.generic || ctx->skip_meta)
3596
1.57k
    ;
3597
13.9M
  else if (rt.subtype == RING_TRUST_SIG
3598
13.9M
           && ctx->last_pkt.pkttype == PKT_SIGNATURE)
3599
13.9M
    {
3600
13.9M
      PKT_signature *sig = ctx->last_pkt.pkt.signature;
3601
3602
13.9M
      if ((rt.sigcache & 1))
3603
8.12M
        {
3604
8.12M
          sig->flags.checked = 1;
3605
8.12M
          sig->flags.valid = !!(rt.sigcache & 2);
3606
8.12M
        }
3607
13.9M
    }
3608
30.9k
  else if (rt.subtype == RING_TRUST_UID
3609
20.3k
           && (ctx->last_pkt.pkttype == PKT_USER_ID
3610
0
               || ctx->last_pkt.pkttype == PKT_ATTRIBUTE))
3611
20.3k
    {
3612
20.3k
      PKT_user_id *uid = ctx->last_pkt.pkt.user_id;
3613
3614
20.3k
      uid->keyorg = rt.keyorg;
3615
20.3k
      uid->keyupdate = rt.keyupdate;
3616
20.3k
      uid->updateurl = rt.url;
3617
20.3k
      rt.url = NULL;
3618
20.3k
    }
3619
10.6k
  else if (rt.subtype == RING_TRUST_KEY
3620
10.6k
           && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY
3621
0
               || ctx->last_pkt.pkttype == PKT_SECRET_KEY))
3622
10.6k
    {
3623
10.6k
      PKT_public_key *pk = ctx->last_pkt.pkt.public_key;
3624
3625
10.6k
      pk->keyorg = rt.keyorg;
3626
10.6k
      pk->keyupdate = rt.keyupdate;
3627
10.6k
      pk->updateurl = rt.url;
3628
10.6k
      rt.url = NULL;
3629
10.6k
    }
3630
3631
13.9M
  err = 0;
3632
3633
13.9M
 leave:
3634
13.9M
  xfree (rt.url);
3635
13.9M
  free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL.  */
3636
13.9M
  iobuf_skip_rest (inp, pktlen, 0);
3637
13.9M
  return err;
3638
13.9M
}
3639
3640
3641
static int
3642
parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen,
3643
     PACKET * pkt, int new_ctb, int partial)
3644
938
{
3645
938
  int rc = 0;
3646
938
  int mode, namelen;
3647
938
  PKT_plaintext *pt;
3648
938
  byte *p;
3649
938
  int c, i;
3650
3651
938
  if (!partial && pktlen < 6)
3652
1
    {
3653
1
      log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen);
3654
1
      if (list_mode)
3655
1
        es_fputs (":literal data packet: [too short]\n", listfp);
3656
1
      rc = gpg_error (GPG_ERR_INV_PACKET);
3657
1
      goto leave;
3658
1
    }
3659
937
  mode = iobuf_get_noeof (inp);
3660
937
  if (pktlen)
3661
528
    pktlen--;
3662
937
  namelen = iobuf_get_noeof (inp);
3663
937
  if (pktlen)
3664
528
    pktlen--;
3665
  /* Note that namelen will never exceed 255 bytes. */
3666
937
  pt = pkt->pkt.plaintext =
3667
937
    xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1);
3668
937
  pt->new_ctb = new_ctb;
3669
937
  pt->mode = mode;
3670
937
  pt->namelen = namelen;
3671
937
  pt->is_partial = partial;
3672
937
  if (pktlen)
3673
528
    {
3674
28.0k
      for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++)
3675
27.4k
  pt->name[i] = iobuf_get_noeof (inp);
3676
528
    }
3677
409
  else
3678
409
    {
3679
2.54k
      for (i = 0; i < namelen; i++)
3680
2.23k
  if ((c = iobuf_get (inp)) == -1)
3681
104
    break;
3682
2.13k
  else
3683
2.13k
    pt->name[i] = c;
3684
409
    }
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
30.0k
  for (; i < namelen; i++)
3689
29.0k
    pt->name[i] = 0;
3690
3691
937
  pt->timestamp = read_32 (inp);
3692
937
  if (pktlen)
3693
528
    pktlen -= 4;
3694
937
  pt->len = pktlen;
3695
937
  pt->buf = inp;
3696
3697
937
  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
938
 leave:
3718
938
  return rc;
3719
937
}
3720
3721
3722
static int
3723
parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen,
3724
      PACKET * pkt, int new_ctb)
3725
132
{
3726
132
  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
132
  (void) pkttype;
3732
132
  (void) pktlen;
3733
3734
132
  zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed);
3735
132
  zd->algorithm = iobuf_get_noeof (inp);
3736
132
  zd->len = 0;      /* not used */
3737
132
  zd->new_ctb = new_ctb;
3738
132
  zd->buf = inp;
3739
132
  if (list_mode)
3740
132
    es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
3741
132
  return 0;
3742
132
}
3743
3744
3745
static int
3746
parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen,
3747
     PACKET * pkt, int new_ctb, int partial)
3748
2.97k
{
3749
2.97k
  int rc = 0;
3750
2.97k
  PKT_encrypted *ed;
3751
2.97k
  unsigned long orig_pktlen = pktlen;
3752
3753
2.97k
  ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted);
3754
  /* ed->len is set below.  */
3755
2.97k
  ed->extralen = 0;  /* Unknown here; only used in build_packet.  */
3756
2.97k
  ed->buf = NULL;
3757
2.97k
  ed->new_ctb = new_ctb;
3758
2.97k
  ed->is_partial = partial;
3759
2.97k
  ed->aead_algo = 0;
3760
2.97k
  ed->cipher_algo = 0; /* Only used with AEAD.  */
3761
2.97k
  ed->chunkbyte = 0;   /* Only used with AEAD.  */
3762
2.97k
  if (pkttype == PKT_ENCRYPTED_MDC)
3763
2.28k
    {
3764
      /* Fixme: add some pktlen sanity checks.  */
3765
2.28k
      int version;
3766
3767
2.28k
      version = iobuf_get_noeof (inp);
3768
2.28k
      if (orig_pktlen)
3769
111
  pktlen--;
3770
2.28k
      if (version != 1)
3771
58
  {
3772
58
    log_error ("encrypted_mdc packet with unknown version %d\n",
3773
58
         version);
3774
58
          if (list_mode)
3775
58
            es_fputs (":encrypted data packet: [unknown version]\n", listfp);
3776
    /*skip_rest(inp, pktlen); should we really do this? */
3777
58
    rc = gpg_error (GPG_ERR_INV_PACKET);
3778
58
    goto leave;
3779
58
  }
3780
2.22k
      ed->mdc_method = DIGEST_ALGO_SHA1;
3781
2.22k
    }
3782
691
  else
3783
691
    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
2.91k
  if (orig_pktlen && pktlen < 10)
3789
5
    {
3790
      /* Actually this is blocksize+2.  */
3791
5
      log_error ("packet(%d) too short\n", pkttype);
3792
5
      if (list_mode)
3793
5
        es_fputs (":encrypted data packet: [too short]\n", listfp);
3794
5
      rc = GPG_ERR_INV_PACKET;
3795
5
      iobuf_skip_rest (inp, pktlen, partial);
3796
5
      goto leave;
3797
5
    }
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
2.91k
  ed->len = pktlen;
3803
3804
2.91k
  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
2.91k
  ed->buf = inp;
3816
3817
2.97k
 leave:
3818
2.97k
  return rc;
3819
2.91k
}
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
652
{
3829
652
  int rc = 0;
3830
652
  PKT_mdc *mdc;
3831
652
  byte *p;
3832
3833
652
  (void) pkttype;
3834
3835
652
  mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc);
3836
652
  if (list_mode)
3837
652
    es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
3838
652
  if (!new_ctb || pktlen != 20)
3839
22
    {
3840
22
      log_error ("mdc_packet with invalid encoding\n");
3841
22
      rc = gpg_error (GPG_ERR_INV_PACKET);
3842
22
      goto leave;
3843
22
    }
3844
630
  p = mdc->hash;
3845
13.2k
  for (; pktlen; pktlen--, p++)
3846
12.6k
    *p = iobuf_get_noeof (inp);
3847
3848
652
 leave:
3849
652
  return rc;
3850
630
}
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
3.18k
{
3857
3.18k
  int rc = 0;
3858
3.18k
  PKT_encrypted *ed;
3859
3.18k
  unsigned long orig_pktlen = pktlen;
3860
3.18k
  int version;
3861
3862
3.18k
  ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted);
3863
3.18k
  if (!ed)
3864
0
    return gpg_error_from_syserror ();
3865
3.18k
  ed->len = 0;
3866
3.18k
  ed->extralen = 0;  /* (only used in build_packet.)  */
3867
3.18k
  ed->buf = NULL;
3868
3.18k
  ed->new_ctb = 1;   /* (packet number requires a new CTB anyway.)  */
3869
3.18k
  ed->is_partial = partial;
3870
3.18k
  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
3.18k
  if (orig_pktlen && pktlen < 19)
3874
4
    {
3875
4
      log_error ("packet(%d) too short\n", pkttype);
3876
4
      if (list_mode)
3877
4
        es_fputs (":aead encrypted packet: [too short]\n", listfp);
3878
4
      rc = gpg_error (GPG_ERR_INV_PACKET);
3879
4
      iobuf_skip_rest (inp, pktlen, partial);
3880
4
      goto leave;
3881
4
    }
3882
3883
3.17k
  version = iobuf_get_noeof (inp);
3884
3.17k
  if (orig_pktlen)
3885
105
    pktlen--;
3886
3.17k
  if (version != 1)
3887
50
    {
3888
50
      log_error ("aead encrypted packet with unknown version %d\n",
3889
50
                 version);
3890
50
      if (list_mode)
3891
50
        es_fputs (":aead encrypted packet: [unknown version]\n", listfp);
3892
      /*skip_rest(inp, pktlen); should we really do this? */
3893
50
      rc = gpg_error (GPG_ERR_INV_PACKET);
3894
50
      goto leave;
3895
50
    }
3896
3897
3.12k
  ed->cipher_algo = iobuf_get_noeof (inp);
3898
3.12k
  if (orig_pktlen)
3899
81
    pktlen--;
3900
3.12k
  ed->aead_algo = iobuf_get_noeof (inp);
3901
3.12k
  if (orig_pktlen)
3902
81
    pktlen--;
3903
3.12k
  ed->chunkbyte = iobuf_get_noeof (inp);
3904
3.12k
  if (orig_pktlen)
3905
81
    pktlen--;
3906
3907
  /* Store the remaining length of the encrypted data.  We read the
3908
   * rest during decryption.  */
3909
3.12k
  ed->len = pktlen;
3910
3911
3.12k
  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
3.12k
  ed->buf = inp;
3922
3923
3.18k
 leave:
3924
3.18k
  return rc;
3925
3.12k
}
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
77
{
3943
77
  byte *p;
3944
77
  const byte *sesmark;
3945
77
  size_t sesmarklen;
3946
77
  int i;
3947
3948
77
  (void) pkttype;
3949
3950
77
  if (list_mode)
3951
77
    es_fprintf (listfp, ":packet 63: length %lu ", pktlen);
3952
3953
77
  sesmark = get_session_marker (&sesmarklen);
3954
77
  if (pktlen < sesmarklen + 1)  /* 1 is for the control bytes */
3955
8
    goto skipit;
3956
69
  for (i = 0; i < sesmarklen; i++, pktlen--)
3957
69
    {
3958
69
      if (sesmark[i] != iobuf_get_noeof (inp))
3959
69
  goto skipit;
3960
69
    }
3961
0
  if (pktlen > 4096)
3962
0
    goto skipit;  /* Definitely too large.  We skip it to avoid an
3963
                     overflow in the malloc.  */
3964
0
  if (list_mode)
3965
0
    es_fputs ("- gpg control packet", listfp);
3966
3967
0
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
3968
0
             + pktlen - 1);
3969
0
  packet->pkt.gpg_control->control = iobuf_get_noeof (inp);
3970
0
  pktlen--;
3971
0
  packet->pkt.gpg_control->datalen = pktlen;
3972
0
  p = packet->pkt.gpg_control->data;
3973
0
  for (; pktlen; pktlen--, p++)
3974
0
    *p = iobuf_get_noeof (inp);
3975
3976
0
  return 0;
3977
3978
77
 skipit:
3979
77
  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
77
  iobuf_skip_rest (inp, pktlen, 0);
4002
77
  return gpg_error (GPG_ERR_INV_PACKET);
4003
0
}
4004
4005
4006
/* Create a GPG control packet to be used internally as a placeholder.  */
4007
PACKET *
4008
create_gpg_control (ctrlpkttype_t type, const byte * data, size_t datalen)
4009
0
{
4010
0
  PACKET *packet;
4011
0
  byte *p;
4012
4013
0
  if (!data)
4014
0
    datalen = 0;
4015
4016
0
  packet = xmalloc (sizeof *packet);
4017
0
  init_packet (packet);
4018
0
  packet->pkttype = PKT_GPG_CONTROL;
4019
0
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen);
4020
0
  packet->pkt.gpg_control->control = type;
4021
0
  packet->pkt.gpg_control->datalen = datalen;
4022
0
  p = packet->pkt.gpg_control->data;
4023
0
  for (; datalen; datalen--, p++)
4024
0
    *p = *data++;
4025
4026
0
  return packet;
4027
0
}