Coverage Report

Created: 2026-07-30 06:13

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
38.7M
{
96
38.7M
  unsigned short a;
97
38.7M
  a = (unsigned short)iobuf_get_noeof (inp) << 8;
98
38.7M
  a |= iobuf_get_noeof (inp);
99
38.7M
  return a;
100
38.7M
}
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
2.18M
{
107
2.18M
  unsigned long a;
108
2.18M
  a = (unsigned long)iobuf_get_noeof (inp) << 24;
109
2.18M
  a |= iobuf_get_noeof (inp) << 16;
110
2.18M
  a |= iobuf_get_noeof (inp) << 8;
111
2.18M
  a |= iobuf_get_noeof (inp);
112
2.18M
  return a;
113
2.18M
}
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
446k
{
131
446k
  int c, c1, c2, i;
132
446k
  unsigned int nmax = *ret_nread;
133
446k
  unsigned int nbits, nbytes;
134
446k
  size_t nread = 0;
135
446k
  gcry_mpi_t a = NULL;
136
446k
  byte *buf = NULL;
137
446k
  byte *p;
138
139
446k
  if (!nmax)
140
1.12k
    goto overflow;
141
142
445k
  if ((c = c1 = iobuf_get (inp)) == -1)
143
977
    goto leave;
144
444k
  if (++nread == nmax)
145
1.52k
    goto overflow;
146
442k
  nbits = c << 8;
147
442k
  if ((c = c2 = iobuf_get (inp)) == -1)
148
1.04k
    goto leave;
149
441k
  ++nread;
150
441k
  nbits |= c;
151
441k
  if (nbits > MAX_EXTERN_MPI_BITS)
152
8.55k
    {
153
8.55k
      log_error ("mpi too large (%u bits)\n", nbits);
154
8.55k
      goto leave;
155
8.55k
    }
156
157
433k
  nbytes = (nbits + 7) / 8;
158
433k
  buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2);
159
433k
  p = buf;
160
433k
  p[0] = c1;
161
433k
  p[1] = c2;
162
15.2M
  for (i = 0; i < nbytes; i++)
163
14.8M
    {
164
14.8M
      if (nread == nmax)
165
8.04k
  goto overflow;
166
167
14.8M
      c = iobuf_get (inp);
168
14.8M
      if (c == -1)
169
1.17k
  goto leave;
170
171
14.8M
      p[i + 2] = c;
172
14.8M
      nread ++;
173
14.8M
    }
174
175
424k
  if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread))
176
0
    a = NULL;
177
178
424k
  *ret_nread = nread;
179
424k
  gcry_free(buf);
180
424k
  return a;
181
182
10.6k
 overflow:
183
10.6k
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
184
22.4k
 leave:
185
22.4k
  *ret_nread = nread;
186
22.4k
  gcry_free(buf);
187
22.4k
  return a;
188
10.6k
}
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
1.39k
{
208
1.39k
  gpg_error_t err;
209
1.39k
  int c, i;
210
1.39k
  byte *buf = NULL;
211
1.39k
  byte *p;
212
213
1.39k
  *r_data = NULL;
214
215
1.39k
  if ((nbytes && nlength)
216
1.39k
      || (!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
1.39k
  if (nlength)
223
1.39k
    {
224
6.91k
      for (i = 0; i < nlength; i++)
225
5.54k
        {
226
5.54k
          if (!*pktlen)
227
1
            {
228
1
              err = gpg_error (GPG_ERR_INV_PACKET);
229
1
              goto leave;
230
1
            }
231
5.53k
          c = iobuf_readbyte (inp);
232
5.53k
          if (c < 0)
233
23
            {
234
23
              err =  gpg_error (GPG_ERR_INV_PACKET);
235
23
              goto leave;
236
23
            }
237
5.51k
          --*pktlen;
238
5.51k
          nbytes <<= 8;
239
5.51k
          nbytes |= c;
240
5.51k
        }
241
242
1.37k
      if (!nbytes)
243
7
        {
244
7
          err =  gpg_error (GPG_ERR_INV_PACKET);
245
7
          goto leave;
246
7
        }
247
1.37k
    }
248
249
1.36k
  if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS)
250
244
      || (nbytes*8 < nbytes))
251
1.12k
    {
252
1.12k
      log_error ("octet string too large (%u octets)\n", nbytes);
253
1.12k
      err = gpg_error (GPG_ERR_TOO_LARGE);
254
1.12k
      goto leave;
255
1.12k
    }
256
257
241
  if (nbytes > *pktlen)
258
110
    {
259
110
      log_error ("octet string larger than packet (%u octets)\n", nbytes);
260
110
      err = gpg_error (GPG_ERR_INV_PACKET);
261
110
      goto leave;
262
110
    }
263
264
131
  buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes);
265
131
  if (!buf)
266
0
    {
267
0
      err = gpg_error_from_syserror ();
268
0
      goto leave;
269
0
    }
270
131
  p = buf;
271
6.26k
  for (i = 0; i < nbytes; i++)
272
6.17k
    {
273
6.17k
      c = iobuf_get (inp);
274
6.17k
      if (c == -1)
275
40
        {
276
40
          err = gpg_error (GPG_ERR_INV_PACKET);
277
40
          goto leave;
278
40
        }
279
280
6.13k
      p[i] = c;
281
6.13k
      --*pktlen;
282
6.13k
    }
283
284
91
  *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8);
285
91
  gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2);
286
91
  return 0;
287
288
1.30k
 leave:
289
1.30k
  gcry_free (buf);
290
1.30k
  return err;
291
131
}
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
38.9M
{
308
38.9M
  int c, c1, c2, i;
309
38.9M
  unsigned int nmax = *ret_nread;
310
38.9M
  unsigned int nbits, nbytes;
311
38.9M
  size_t nread = 0;
312
38.9M
  gcry_mpi_t a = NULL;
313
38.9M
  byte *buf = NULL;
314
38.9M
  byte *p;
315
316
38.9M
  if (!nmax)
317
471
    goto overflow;
318
319
38.9M
  if ((c = c1 = iobuf_get (inp)) == -1)
320
574
    goto leave;
321
38.9M
  if (++nread == nmax)
322
369
    goto overflow;
323
38.9M
  nbits = c << 8;
324
38.9M
  if ((c = c2 = iobuf_get (inp)) == -1)
325
183
    goto leave;
326
38.9M
  ++nread;
327
38.9M
  nbits |= c;
328
38.9M
  if (nbits > MAX_EXTERN_MPI_BITS)
329
2.97k
    {
330
2.97k
      log_error ("mpi too large (%u bits)\n", nbits);
331
2.97k
      goto leave;
332
2.97k
    }
333
334
38.9M
  nbytes = (nbits + 7) / 8;
335
38.9M
  buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes);
336
38.9M
  p = buf;
337
1.28G
  for (i = 0; i < nbytes; i++)
338
1.24G
    {
339
1.24G
      if (nread == nmax)
340
4.94k
        goto overflow;
341
342
1.24G
      c = iobuf_get (inp);
343
1.24G
      if (c == -1)
344
408
        goto leave;
345
346
1.24G
      p[i] = c;
347
1.24G
      nread ++;
348
1.24G
    }
349
350
38.9M
  a = gcry_mpi_set_opaque (NULL, buf, nbits);
351
38.9M
  gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2);
352
38.9M
  *ret_nread = nread;
353
38.9M
  return a;
354
355
5.78k
 overflow:
356
5.78k
  log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax);
357
9.92k
 leave:
358
9.92k
  *ret_nread = nread;
359
9.92k
  gcry_free(buf);
360
9.92k
  return a;
361
5.78k
}
362
363
364
/* Register STRING as a known critical notation name.  */
365
void
366
register_known_notation (const char *string)
367
1.00k
{
368
1.00k
  strlist_t sl;
369
370
1.00k
  if (!known_notations_list)
371
4
    {
372
4
      sl = add_to_strlist (&known_notations_list,
373
4
                           "preferred-email-encoding@pgp.com");
374
4
      sl->flags = 32;  /* Length of the string.  */
375
4
    }
376
1.00k
  if (!string)
377
1.00k
    return; /* Only initialized the default known notations.  */
378
379
  /* In --set-notation we use an exclamation mark to indicate a
380
   * critical notation.  As a convenience skip this here.  */
381
0
  if (*string == '!')
382
0
    string++;
383
384
0
  if (!*string || strlist_find (known_notations_list, string))
385
0
    return; /* Empty string or already registered.  */
386
387
0
  sl = add_to_strlist (&known_notations_list, string);
388
0
  sl->flags = strlen (string);
389
0
}
390
391
392
int
393
set_packet_list_mode (int mode)
394
24.6k
{
395
24.6k
  int old = list_mode;
396
24.6k
  list_mode = mode;
397
398
  /* We use stdout only if invoked by the --list-packets command
399
     but switch to stderr in all other cases.  This breaks the
400
     previous behaviour but that seems to be more of a bug than
401
     intentional.  I don't believe that any application makes use of
402
     this long standing annoying way of printing to stdout except when
403
     doing a --list-packets. If this assumption fails, it will be easy
404
     to add an option for the listing stream.  Note that we initialize
405
     it only once; mainly because there is code which switches
406
     opt.list_mode back to 1 and we want to have all output to the
407
     same stream.  The MPI_PRINT_MODE will be enabled if the
408
     corresponding debug flag is set or if we are in --list-packets
409
     and --verbose is given.
410
411
     Using stderr is not actually very clean because it bypasses the
412
     logging code but it is a special thing anyway.  I am not sure
413
     whether using log_stream() would be better.  Perhaps we should
414
     enable the list mode only with a special option. */
415
24.6k
  if (!listfp)
416
3
    {
417
3
      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
3
      else
424
3
        listfp = es_stderr;
425
426
3
      if (DBG_MPI)
427
0
        mpi_print_mode = 1;
428
3
    }
429
24.6k
  return old;
430
24.6k
}
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
492k
{
438
492k
  static byte unknown_pubkey_algos[256];
439
440
  /* First check whether the algorithm is usable but not suitable for
441
     encryption/signing.  */
442
492k
  if (pubkey_get_npkey (algo))
443
187k
    {
444
187k
      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
187k
    }
454
305k
  else
455
305k
    {
456
305k
      algo &= 0xff;
457
305k
      if (!unknown_pubkey_algos[algo])
458
708
        {
459
708
          if (opt.verbose && !glo_ctrl.silence_parse_warnings)
460
708
            log_info (_("can't handle public key algorithm %d\n"), algo);
461
708
          unknown_pubkey_algos[algo] = 1;
462
708
        }
463
305k
    }
464
492k
}
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
23.4M
{
472
23.4M
  int skip, rc;
473
474
23.4M
  do
475
23.4M
    {
476
23.4M
      rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l);
477
23.4M
    }
478
23.4M
  while (skip && ! rc);
479
23.4M
  return rc;
480
23.4M
}
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
23.4M
{
735
23.4M
  int rc = 0;
736
23.4M
  iobuf_t inp;
737
23.4M
  int c, ctb, pkttype, lenbytes;
738
23.4M
  unsigned long pktlen;
739
23.4M
  byte hdr[8];
740
23.4M
  int hdrlen;
741
23.4M
  int new_ctb = 0, partial = 0;
742
23.4M
  int with_uid = (onlykeypkts == 2);
743
23.4M
  off_t pos;
744
745
23.4M
  *skip = 0;
746
23.4M
  inp = ctx->inp;
747
748
42.9M
 again:
749
42.9M
  log_assert (!pkt->pkt.generic);
750
42.9M
  if (retpos || list_mode)
751
0
    {
752
0
      pos = iobuf_tell (inp);
753
0
      if (retpos)
754
0
        *retpos = pos;
755
0
    }
756
42.9M
  else
757
42.9M
    pos = 0; /* (silence compiler warning) */
758
759
  /* The first byte of a packet is the so-called tag.  The highest bit
760
     must be set.  */
761
42.9M
  if ((ctb = iobuf_get (inp)) == -1)
762
62.5k
    {
763
62.5k
      rc = -1;
764
62.5k
      goto leave;
765
62.5k
    }
766
42.8M
  ctx->last_ctb = ctb;
767
42.8M
  hdrlen = 0;
768
42.8M
  hdr[hdrlen++] = ctb;
769
770
42.8M
  if (!(ctb & 0x80))
771
311k
    {
772
311k
      log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb);
773
311k
      rc = gpg_error (GPG_ERR_INV_PACKET);
774
311k
      goto leave;
775
311k
    }
776
777
  /* Immediately following the header is the length.  There are two
778
   * formats: the old format and the new format.  If bit 6 (where the
779
   * least significant bit is bit 0) is set in the tag, then we are
780
   * dealing with a new format packet.  Otherwise, it is an old format
781
   * packet.  In the new format the packet's type is encoded in the 6
782
   * least significant bits of the tag; in the old format it is
783
   * encoded in bits 2-5.  */
784
42.5M
  pktlen = 0;
785
42.5M
  new_ctb = !!(ctb & 0x40);
786
42.5M
  if (new_ctb)
787
1.33M
    pkttype = ctb & 0x3f;
788
41.2M
  else
789
41.2M
    pkttype = (ctb >> 2) & 0xf;
790
791
42.5M
  if (ctx->only_fookey_enc
792
0
      && !(pkttype == PKT_SYMKEY_ENC || pkttype == PKT_PUBKEY_ENC))
793
0
    {
794
0
      rc = gpg_error (GPG_ERR_TRUE);
795
0
      goto leave;
796
0
    }
797
798
42.5M
  if (new_ctb)
799
1.33M
    {
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
1.33M
      if ((c = iobuf_get (inp)) == -1)
805
1.94k
  {
806
1.94k
    log_error ("%s: 1st length byte missing\n", iobuf_where (inp));
807
1.94k
    rc = gpg_error (GPG_ERR_INV_PACKET);
808
1.94k
    goto leave;
809
1.94k
  }
810
811
812
1.33M
      hdr[hdrlen++] = c;
813
1.33M
      if (c < 192)
814
1.01M
        pktlen = c;
815
320k
      else if (c < 224)
816
77.1k
        {
817
77.1k
          pktlen = (c - 192) * 256;
818
77.1k
          if ((c = iobuf_get (inp)) == -1)
819
955
            {
820
955
              log_error ("%s: 2nd length byte missing\n",
821
955
                         iobuf_where (inp));
822
955
              rc = gpg_error (GPG_ERR_INV_PACKET);
823
955
              goto leave;
824
955
            }
825
76.2k
          hdr[hdrlen++] = c;
826
76.2k
          pktlen += c + 192;
827
76.2k
        }
828
243k
      else if (c == 255)
829
5.03k
        {
830
5.03k
    int i;
831
5.03k
    char value[4];
832
833
24.7k
    for (i = 0; i < 4; i ++)
834
19.8k
            {
835
19.8k
              if ((c = iobuf_get (inp)) == -1)
836
185
                {
837
185
                  log_error ("%s: 4 byte length invalid\n", iobuf_where (inp));
838
185
                  rc = gpg_error (GPG_ERR_INV_PACKET);
839
185
                  goto leave;
840
185
                }
841
19.6k
              value[i] = hdr[hdrlen++] = c;
842
19.6k
            }
843
844
4.85k
    pktlen = buf32_to_ulong (value);
845
4.85k
        }
846
238k
      else /* Partial body length.  */
847
238k
        {
848
238k
          switch (pkttype)
849
238k
            {
850
11.9k
            case PKT_PLAINTEXT:
851
13.1k
            case PKT_ENCRYPTED:
852
18.7k
            case PKT_ENCRYPTED_MDC:
853
21.8k
            case PKT_ENCRYPTED_AEAD:
854
234k
            case PKT_COMPRESSED:
855
234k
              iobuf_set_partial_body_length_mode (inp, c & 0xff);
856
234k
              pktlen = 0; /* To indicate partial length.  */
857
234k
              partial = 1;
858
234k
              break;
859
860
4.48k
            default:
861
4.48k
              log_error ("%s: partial length invalid for"
862
4.48k
                         " packet type %d\n", iobuf_where (inp), pkttype);
863
4.48k
              rc = gpg_error (GPG_ERR_INV_PACKET);
864
4.48k
              goto leave;
865
238k
            }
866
238k
        }
867
868
1.33M
    }
869
41.2M
  else /* This is an old format packet.  */
870
41.2M
    {
871
      /* The type of length encoding is encoded in bits 0-1 of the
872
   tag.  */
873
41.2M
      lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3));
874
41.2M
      if (!lenbytes)
875
385k
  {
876
385k
    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
385k
    partial = 1;
880
385k
    if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT
881
383k
        && pkttype != PKT_COMPRESSED)
882
6.69k
      {
883
6.69k
        log_error ("%s: indeterminate length for invalid"
884
6.69k
       " packet type %d\n", iobuf_where (inp), pkttype);
885
6.69k
        rc = gpg_error (GPG_ERR_INV_PACKET);
886
6.69k
        goto leave;
887
6.69k
      }
888
385k
  }
889
40.8M
      else
890
40.8M
  {
891
81.7M
    for (; lenbytes; lenbytes--)
892
40.9M
      {
893
40.9M
        pktlen <<= 8;
894
40.9M
        c = iobuf_get (inp);
895
40.9M
        if (c == -1)
896
4.78k
    {
897
4.78k
      log_error ("%s: length invalid\n", iobuf_where (inp));
898
4.78k
      rc = gpg_error (GPG_ERR_INV_PACKET);
899
4.78k
      goto leave;
900
4.78k
    }
901
40.9M
        pktlen |= hdr[hdrlen++] = c;
902
40.9M
      }
903
40.8M
  }
904
41.2M
    }
905
906
  /* Sometimes the decompressing layer enters an error state in which
907
     it simply outputs 0xff for every byte read.  If we have a stream
908
     of 0xff bytes, then it will be detected as a new format packet
909
     with type 63 and a 4-byte encoded length that is 4G-1.  Since
910
     packets with type 63 are private and we use them as a control
911
     packet, which won't be 4 GB, we reject such packets as
912
     invalid.  */
913
42.5M
  if (pkttype == 63 && pktlen == 0xFFFFFFFF)
914
0
    {
915
      /* With some probability this is caused by a problem in the
916
       * the uncompressing layer - in some error cases it just loops
917
       * and spits out 0xff bytes. */
918
0
      log_error ("%s: garbled packet detected\n", iobuf_where (inp));
919
0
      g10_exit (2);
920
0
    }
921
922
42.5M
  if (out && pkttype)
923
0
    {
924
      /* This type of copying won't work if the packet uses a partial
925
   body length.  (In other words, this only works if HDR is
926
   actually the length.)  Currently, no callers require this
927
   functionality so we just log this as an error.  */
928
0
      if (partial)
929
0
  {
930
0
    log_error ("parse: Can't copy partial packet.  Aborting.\n");
931
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
932
0
    goto leave;
933
0
  }
934
935
0
      rc = iobuf_write (out, hdr, hdrlen);
936
0
      if (!rc)
937
0
  rc = copy_packet (inp, out, pkttype, pktlen, partial);
938
0
      goto leave;
939
0
    }
940
941
42.5M
  if (with_uid && pkttype == PKT_USER_ID)
942
    /* If ONLYKEYPKTS is set to 2, then we never skip user id packets,
943
       even if DO_SKIP is set.  */
944
0
    ;
945
42.5M
  else if (do_skip
946
     /* type==0 is not allowed.  This is an invalid packet.  */
947
42.5M
     || !pkttype
948
     /* When ONLYKEYPKTS is set, we don't skip keys.  */
949
42.5M
     || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
950
0
         && pkttype != PKT_PUBLIC_KEY
951
0
         && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY))
952
28.4k
    {
953
28.4k
      iobuf_skip_rest (inp, pktlen, partial);
954
28.4k
      *skip = 1;
955
28.4k
      rc = 0;
956
28.4k
      goto leave;
957
28.4k
    }
958
959
42.5M
  if (DBG_PACKET)
960
0
    {
961
0
#if DEBUG_PARSE_PACKET
962
0
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n",
963
0
     iobuf_id (inp), pkttype, pktlen, new_ctb ? " (new_ctb)" : "",
964
0
     dbg_w, dbg_f, dbg_l);
965
#else
966
      log_debug ("parse_packet(iob=%d): type=%d length=%lu%s\n",
967
     iobuf_id (inp), pkttype, pktlen,
968
     new_ctb ? " (new_ctb)" : "");
969
#endif
970
0
    }
971
972
42.5M
  if (list_mode)
973
42.5M
    es_fprintf (listfp, "# off=%lu ctb=%02x tag=%d hlen=%d plen=%lu%s%s\n",
974
0
                (unsigned long)pos, ctb, pkttype, hdrlen, pktlen,
975
0
                partial? (new_ctb ? " partial" : " indeterminate") :"",
976
0
                new_ctb? " new-ctb":"");
977
978
  /* Count it.  */
979
42.5M
  ctx->n_parsed_packets++;
980
981
42.5M
  pkt->pkttype = pkttype;
982
42.5M
  rc = GPG_ERR_UNKNOWN_PACKET;  /* default error */
983
42.5M
  switch (pkttype)
984
42.5M
    {
985
81.6k
    case PKT_PUBLIC_KEY:
986
1.25M
    case PKT_PUBLIC_SUBKEY:
987
1.28M
    case PKT_SECRET_KEY:
988
1.37M
    case PKT_SECRET_SUBKEY:
989
1.37M
      pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key);
990
1.37M
      rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt);
991
1.37M
      break;
992
133k
    case PKT_SYMKEY_ENC:
993
133k
      rc = parse_symkeyenc (inp, pkttype, pktlen, pkt);
994
133k
      break;
995
81.7k
    case PKT_PUBKEY_ENC:
996
81.7k
      rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt);
997
81.7k
      break;
998
20.0M
    case PKT_SIGNATURE:
999
20.0M
      pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature);
1000
20.0M
      rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature);
1001
20.0M
      break;
1002
55.3k
    case PKT_ONEPASS_SIG:
1003
55.3k
      pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig);
1004
55.3k
      rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig);
1005
55.3k
      break;
1006
198k
    case PKT_USER_ID:
1007
198k
      rc = parse_user_id (inp, pkttype, pktlen, pkt);
1008
198k
      break;
1009
34.6k
    case PKT_ATTRIBUTE:
1010
34.6k
      pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
1011
34.6k
      rc = parse_attribute (inp, pkttype, pktlen, pkt);
1012
34.6k
      break;
1013
13.2k
    case PKT_OLD_COMMENT:
1014
15.6k
    case PKT_COMMENT:
1015
15.6k
      rc = parse_comment (inp, pkttype, pktlen, pkt);
1016
15.6k
      break;
1017
19.4M
    case PKT_RING_TRUST:
1018
19.4M
      {
1019
19.4M
        rc = parse_ring_trust (ctx, pktlen);
1020
19.4M
        if (!rc)
1021
19.4M
          goto again; /* Directly read the next packet.  */
1022
19.4M
      }
1023
0
      break;
1024
172k
    case PKT_PLAINTEXT:
1025
172k
      rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial);
1026
172k
      break;
1027
634k
    case PKT_COMPRESSED:
1028
634k
      rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb);
1029
634k
      break;
1030
66.3k
    case PKT_ENCRYPTED:
1031
78.3k
    case PKT_ENCRYPTED_MDC:
1032
78.3k
      rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial);
1033
78.3k
      break;
1034
20.6k
    case PKT_MDC:
1035
20.6k
      rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb);
1036
20.6k
      break;
1037
38.8k
    case PKT_ENCRYPTED_AEAD:
1038
38.8k
      rc = parse_encrypted_aead (inp, pkttype, pktlen, pkt, partial);
1039
38.8k
      break;
1040
22.8k
    case PKT_GPG_CONTROL:
1041
22.8k
      rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial);
1042
22.8k
      break;
1043
60.3k
    case PKT_MARKER:
1044
60.3k
      rc = parse_marker (inp, pkttype, pktlen);
1045
60.3k
      break;
1046
60.7k
    default:
1047
      /* Unknown packet.  Skip it.  */
1048
60.7k
      skip_packet (inp, pkttype, pktlen, partial);
1049
60.7k
      break;
1050
42.5M
    }
1051
1052
  /* Store a shallow copy of certain packets in the context.  */
1053
23.0M
  free_packet (NULL, ctx);
1054
23.0M
  if (!rc && (pkttype == PKT_PUBLIC_KEY
1055
22.6M
              || pkttype == PKT_SECRET_KEY
1056
22.6M
              || pkttype == PKT_USER_ID
1057
22.3M
              || pkttype == PKT_ATTRIBUTE
1058
22.3M
              || pkttype == PKT_SIGNATURE))
1059
20.3M
    {
1060
20.3M
      ctx->last_pkt = *pkt;
1061
20.3M
    }
1062
1063
23.4M
 leave:
1064
  /* FIXME: We leak in case of an error (see the xmalloc's above).  */
1065
23.4M
  if (!rc && iobuf_error (inp))
1066
11.6k
    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
23.4M
  return rc == -1? -1 : gpg_err_code (rc);
1072
23.0M
}
1073
1074
1075
static void
1076
dump_hex_line (int c, int *i)
1077
0
{
1078
0
  if (*i && !(*i % 8))
1079
0
    {
1080
0
      if (*i && !(*i % 24))
1081
0
  es_fprintf (listfp, "\n%4d:", *i);
1082
0
      else
1083
0
  es_putc (' ', listfp);
1084
0
    }
1085
0
  if (c == -1)
1086
0
    es_fprintf (listfp, " EOF");
1087
0
  else
1088
0
    es_fprintf (listfp, " %02x", c);
1089
0
  ++*i;
1090
0
}
1091
1092
1093
/* Copy the contents of a packet from the pipeline IN to the pipeline
1094
   OUT.
1095
1096
   The header and length have already been read from INP and the
1097
   decoded values are given as PKGTYPE and PKTLEN.
1098
1099
   If the packet is a partial body length packet (RFC 4880, Section
1100
   4.2.2.4), then iobuf_set_partial_block_modeiobuf_set_partial_block_mode
1101
   should already have been called on INP and PARTIAL should be set.
1102
1103
   If PARTIAL is set or PKTLEN is 0 and PKTTYPE is PKT_COMPRESSED,
1104
   copy until the first EOF is encountered on INP.
1105
1106
   Returns 0 on success and an error code if an error occurs.  */
1107
static int
1108
copy_packet (IOBUF inp, IOBUF out, int pkttype,
1109
       unsigned long pktlen, int partial)
1110
0
{
1111
0
  int rc;
1112
0
  int n;
1113
0
  char buf[100];
1114
1115
0
  if (partial)
1116
0
    {
1117
0
      while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1)
1118
0
  if ((rc = iobuf_write (out, buf, n)))
1119
0
    return rc;   /* write error */
1120
0
    }
1121
0
  else if (!pktlen && pkttype == PKT_COMPRESSED)
1122
0
    {
1123
0
      log_debug ("copy_packet: compressed!\n");
1124
      /* compressed packet, copy till EOF */
1125
0
      while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1)
1126
0
  if ((rc = iobuf_write (out, buf, n)))
1127
0
    return rc;   /* write error */
1128
0
    }
1129
0
  else
1130
0
    {
1131
0
      for (; pktlen; pktlen -= n)
1132
0
  {
1133
0
    n = pktlen > sizeof (buf) ? sizeof (buf) : pktlen;
1134
0
    n = iobuf_read (inp, buf, n);
1135
0
    if (n == -1)
1136
0
      return gpg_error (GPG_ERR_EOF);
1137
0
    if ((rc = iobuf_write (out, buf, n)))
1138
0
      return rc;   /* write error */
1139
0
  }
1140
0
    }
1141
0
  return 0;
1142
0
}
1143
1144
1145
/* Skip an unknown packet.  PKTTYPE is the packet's type, PKTLEN is
1146
   the length of the packet's content and PARTIAL is whether partial
1147
   body length encoding in used (in this case PKTLEN is ignored).  */
1148
static void
1149
skip_packet (IOBUF inp, int pkttype, unsigned long pktlen, int partial)
1150
60.7k
{
1151
60.7k
  if (list_mode)
1152
0
    {
1153
0
      es_fprintf (listfp, ":unknown packet: type %2d, length %lu\n",
1154
0
                  pkttype, pktlen);
1155
0
      if (pkttype)
1156
0
  {
1157
0
    int c, i = 0;
1158
0
    es_fputs ("dump:", listfp);
1159
0
    if (partial)
1160
0
      {
1161
0
        while ((c = iobuf_get (inp)) != -1)
1162
0
    dump_hex_line (c, &i);
1163
0
      }
1164
0
    else
1165
0
      {
1166
0
        for (; pktlen; pktlen--)
1167
0
    {
1168
0
      dump_hex_line ((c = iobuf_get (inp)), &i);
1169
0
      if (c == -1)
1170
0
        break;
1171
0
    }
1172
0
      }
1173
0
    es_putc ('\n', listfp);
1174
0
    return;
1175
0
  }
1176
0
    }
1177
60.7k
  iobuf_skip_rest (inp, pktlen, partial);
1178
60.7k
}
1179
1180
1181
/* Read PKTLEN bytes from INP and return them in a newly allocated
1182
 * buffer.  In case of an error (including reading fewer than PKTLEN
1183
 * bytes from INP before EOF is returned), NULL is returned and an
1184
 * error message is logged.  */
1185
static void *
1186
read_rest (IOBUF inp, size_t pktlen)
1187
453k
{
1188
453k
  int c;
1189
453k
  byte *buf, *p;
1190
1191
453k
  buf = xtrymalloc (pktlen);
1192
453k
  if (!buf)
1193
290k
    {
1194
290k
      gpg_error_t err = gpg_error_from_syserror ();
1195
290k
      log_error ("error reading rest of packet: %s\n", gpg_strerror (err));
1196
290k
      return NULL;
1197
290k
    }
1198
2.60M
  for (p = buf; pktlen; pktlen--)
1199
2.46M
    {
1200
2.46M
      c = iobuf_get (inp);
1201
2.46M
      if (c == -1)
1202
22.1k
        {
1203
22.1k
          log_error ("premature eof while reading rest of packet\n");
1204
22.1k
          xfree (buf);
1205
22.1k
          return NULL;
1206
22.1k
        }
1207
2.43M
      *p++ = c;
1208
2.43M
    }
1209
1210
140k
  return buf;
1211
163k
}
1212
1213
1214
/* Read a special size+body from INP.  On success store an opaque MPI
1215
 * with it at R_DATA.  The caller shall store the remaining size of
1216
 * the packet at PKTLEN.  On error return an error code and store NULL
1217
 * at R_DATA.  Even in the error case store the number of read bytes
1218
 * at PKTLEN is updated.  */
1219
static gpg_error_t
1220
read_sized_octet_string (iobuf_t inp, unsigned long *pktlen, gcry_mpi_t *r_data)
1221
2.37M
{
1222
2.37M
  char buffer[256];
1223
2.37M
  char *tmpbuf;
1224
2.37M
  int i, c, nbytes;
1225
1226
2.37M
  *r_data = NULL;
1227
1228
2.37M
  if (!*pktlen)
1229
2.65k
    return gpg_error (GPG_ERR_INV_PACKET);
1230
2.36M
  c = iobuf_readbyte (inp);
1231
2.36M
  if (c < 0)
1232
193
    return gpg_error (GPG_ERR_INV_PACKET);
1233
2.36M
  --*pktlen;
1234
2.36M
  nbytes = c;
1235
2.36M
  if (nbytes < 2 || nbytes > 254)
1236
3.11k
    return gpg_error (GPG_ERR_INV_PACKET);
1237
2.36M
  if (nbytes > *pktlen)
1238
2.25k
    return gpg_error (GPG_ERR_INV_PACKET);
1239
1240
2.36M
  buffer[0] = nbytes;
1241
1242
17.6M
  for (i = 0; i < nbytes; i++)
1243
15.3M
    {
1244
15.3M
      c = iobuf_get (inp);
1245
15.3M
      if (c < 0)
1246
291
        return gpg_error (GPG_ERR_INV_PACKET);
1247
15.3M
      --*pktlen;
1248
15.3M
      buffer[1+i] = c;
1249
15.3M
    }
1250
1251
2.36M
  tmpbuf = xtrymalloc (1 + nbytes);
1252
2.36M
  if (!tmpbuf)
1253
0
    return gpg_error_from_syserror ();
1254
2.36M
  memcpy (tmpbuf, buffer, 1 + nbytes);
1255
2.36M
  *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes));
1256
2.36M
  if (!*r_data)
1257
0
    {
1258
0
      xfree (tmpbuf);
1259
0
      return gpg_error_from_syserror ();
1260
0
    }
1261
2.36M
  return 0;
1262
2.36M
}
1263
1264
1265
/* Parse a marker packet.  */
1266
static int
1267
parse_marker (IOBUF inp, int pkttype, unsigned long pktlen)
1268
60.3k
{
1269
60.3k
  (void) pkttype;
1270
1271
60.3k
  if (pktlen != 3)
1272
12.7k
    goto fail;
1273
1274
47.5k
  if (iobuf_get (inp) != 'P')
1275
6.14k
    {
1276
6.14k
      pktlen--;
1277
6.14k
      goto fail;
1278
6.14k
    }
1279
1280
41.3k
  if (iobuf_get (inp) != 'G')
1281
354
    {
1282
354
      pktlen--;
1283
354
      goto fail;
1284
354
    }
1285
1286
41.0k
  if (iobuf_get (inp) != 'P')
1287
2.46k
    {
1288
2.46k
      pktlen--;
1289
2.46k
      goto fail;
1290
2.46k
    }
1291
1292
38.5k
  if (list_mode)
1293
38.5k
    es_fputs (":marker packet: PGP\n", listfp);
1294
1295
38.5k
  return 0;
1296
1297
21.7k
 fail:
1298
21.7k
  log_error ("invalid marker packet\n");
1299
21.7k
  if (list_mode)
1300
21.7k
    es_fputs (":marker packet: [invalid]\n", listfp);
1301
21.7k
  iobuf_skip_rest (inp, pktlen, 0);
1302
21.7k
  return GPG_ERR_INV_PACKET;
1303
41.0k
}
1304
1305
1306
static int
1307
parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen,
1308
     PACKET * packet)
1309
133k
{
1310
133k
  PKT_symkey_enc *k;
1311
133k
  int rc = 0;
1312
133k
  int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen;
1313
1314
133k
  if (pktlen < 4)
1315
21.5k
    goto too_short;
1316
112k
  version = iobuf_get_noeof (inp);
1317
112k
  pktlen--;
1318
112k
  if (version == 4)
1319
49.2k
    ;
1320
62.9k
  else if (version == 5)
1321
56.2k
    ;
1322
6.66k
  else
1323
6.66k
    {
1324
6.66k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
1325
6.66k
      if (list_mode)
1326
6.66k
        es_fprintf (listfp, ":symkey enc packet: [unknown version]\n");
1327
6.66k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1328
6.66k
      goto leave;
1329
6.66k
    }
1330
105k
  if (pktlen > 200)
1331
39.2k
    {       /* (we encode the seskeylen in a byte) */
1332
39.2k
      log_error ("packet(%d) too large\n", pkttype);
1333
39.2k
      if (list_mode)
1334
39.2k
        es_fprintf (listfp, ":symkey enc packet: [too large]\n");
1335
39.2k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1336
39.2k
      goto leave;
1337
39.2k
    }
1338
66.2k
  cipher_algo = iobuf_get_noeof (inp);
1339
66.2k
  pktlen--;
1340
66.2k
  if (version == 5)
1341
17.2k
    {
1342
17.2k
      aead_algo = iobuf_get_noeof (inp);
1343
17.2k
      pktlen--;
1344
17.2k
    }
1345
48.9k
  else
1346
48.9k
    aead_algo = 0;
1347
66.2k
  if (pktlen < 2)
1348
4.88k
    goto too_short;
1349
61.3k
  s2kmode = iobuf_get_noeof (inp);
1350
61.3k
  pktlen--;
1351
61.3k
  hash_algo = iobuf_get_noeof (inp);
1352
61.3k
  pktlen--;
1353
61.3k
  switch (s2kmode)
1354
61.3k
    {
1355
38.7k
    case 0: /* Simple S2K.  */
1356
38.7k
      minlen = 0;
1357
38.7k
      break;
1358
2.63k
    case 1: /* Salted S2K.  */
1359
2.63k
      minlen = 8;
1360
2.63k
      break;
1361
11.5k
    case 3: /* Iterated+salted S2K.  */
1362
11.5k
      minlen = 9;
1363
11.5k
      break;
1364
8.39k
    default:
1365
8.39k
      log_error ("unknown S2K mode %d\n", s2kmode);
1366
8.39k
      if (list_mode)
1367
8.39k
        es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n");
1368
8.39k
      goto leave;
1369
61.3k
    }
1370
52.9k
  if (minlen > pktlen)
1371
1.07k
    {
1372
1.07k
      log_error ("packet with S2K %d too short\n", s2kmode);
1373
1.07k
      if (list_mode)
1374
1.07k
        es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1375
1.07k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1376
1.07k
      goto leave;
1377
1.07k
    }
1378
51.8k
  seskeylen = pktlen - minlen;
1379
51.8k
  k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc);
1380
51.8k
  k->version = version;
1381
51.8k
  k->cipher_algo = cipher_algo;
1382
51.8k
  k->aead_algo = aead_algo;
1383
51.8k
  k->s2k.mode = s2kmode;
1384
51.8k
  k->s2k.hash_algo = hash_algo;
1385
51.8k
  if (s2kmode == 1 || s2kmode == 3)
1386
13.0k
    {
1387
117k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
1388
104k
  k->s2k.salt[i] = iobuf_get_noeof (inp);
1389
13.0k
    }
1390
51.8k
  if (s2kmode == 3)
1391
10.9k
    {
1392
10.9k
      k->s2k.count = iobuf_get_noeof (inp);
1393
10.9k
      pktlen--;
1394
10.9k
    }
1395
51.8k
  k->seskeylen = seskeylen;
1396
51.8k
  if (k->seskeylen)
1397
10.8k
    {
1398
10.8k
      k->seskey = xcalloc (1, seskeylen);
1399
141k
      for (i = 0; i < seskeylen && pktlen; i++, pktlen--)
1400
130k
  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
10.8k
      if (s2kmode != 1 && s2kmode != 3)
1406
10.8k
  log_info (_("WARNING: potentially insecure symmetrically"
1407
6.00k
        " encrypted session key\n"));
1408
10.8k
    }
1409
51.8k
  log_assert (!pktlen);
1410
1411
51.8k
  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
133k
 leave:
1442
133k
  iobuf_skip_rest (inp, pktlen, 0);
1443
133k
  return rc;
1444
1445
26.4k
 too_short:
1446
26.4k
  log_error ("packet(%d) too short\n", pkttype);
1447
26.4k
  if (list_mode)
1448
26.4k
    es_fprintf (listfp, ":symkey enc packet: [too short]\n");
1449
26.4k
  rc = gpg_error (GPG_ERR_INV_PACKET);
1450
26.4k
  goto leave;
1451
51.8k
}
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
81.7k
{
1459
81.7k
  int rc = 0;
1460
81.7k
  int i, ndata;
1461
81.7k
  unsigned int n;
1462
81.7k
  PKT_pubkey_enc *k;
1463
1464
81.7k
  k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc);
1465
81.7k
  if (pktlen < 12)
1466
9.15k
    {
1467
9.15k
      log_error ("packet(%d) too short\n", pkttype);
1468
9.15k
      if (list_mode)
1469
9.15k
        es_fputs (":pubkey enc packet: [too short]\n", listfp);
1470
9.15k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1471
9.15k
      goto leave;
1472
9.15k
    }
1473
72.5k
  k->version = iobuf_get_noeof (inp);
1474
72.5k
  pktlen--;
1475
72.5k
  if (k->version != 2 && k->version != 3)
1476
3.58k
    {
1477
3.58k
      log_error ("packet(%d) with unknown version %d\n", pkttype, k->version);
1478
3.58k
      if (list_mode)
1479
3.58k
        es_fputs (":pubkey enc packet: [unknown version]\n", listfp);
1480
3.58k
      rc = gpg_error (GPG_ERR_INV_PACKET);
1481
3.58k
      goto leave;
1482
3.58k
    }
1483
68.9k
  k->keyid[0] = read_32 (inp);
1484
68.9k
  pktlen -= 4;
1485
68.9k
  k->keyid[1] = read_32 (inp);
1486
68.9k
  pktlen -= 4;
1487
68.9k
  k->pubkey_algo = iobuf_get_noeof (inp);
1488
68.9k
  pktlen--;
1489
68.9k
  k->throw_keyid = 0;  /* Only used as flag for build_packet.  */
1490
68.9k
  if (list_mode)
1491
68.9k
    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
68.9k
  ndata = pubkey_get_nenc (k->pubkey_algo);
1497
68.9k
  if (!ndata)
1498
51.0k
    {
1499
51.0k
      if (list_mode)
1500
51.0k
  es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo);
1501
51.0k
      unknown_pubkey_warning (k->pubkey_algo);
1502
51.0k
      k->data[0] = NULL; /* No need to store the encrypted data.  */
1503
51.0k
    }
1504
17.9k
  else if (k->pubkey_algo == PUBKEY_ALGO_ECDH)
1505
13.5k
    {
1506
13.5k
      log_assert (ndata == 2);
1507
      /* Get the ephemeral public key.  */
1508
13.5k
      n = pktlen;
1509
13.5k
      k->data[0] = sos_read (inp, &n, 0);
1510
13.5k
      pktlen -= n;
1511
13.5k
      if (!k->data[0])
1512
5.27k
        {
1513
5.27k
          rc = gpg_error (GPG_ERR_INV_PACKET);
1514
5.27k
          goto leave;
1515
5.27k
        }
1516
      /* Get the wrapped symmetric key.  */
1517
8.25k
      rc = read_sized_octet_string (inp, &pktlen, k->data + 1);
1518
8.25k
      if (rc)
1519
7.70k
        goto leave;
1520
8.25k
    }
1521
4.37k
  else if (k->pubkey_algo == PUBKEY_ALGO_KYBER)
1522
50
    {
1523
50
      log_assert (ndata == 3);
1524
      /* Get the ephemeral public key.  */
1525
50
      n = pktlen;
1526
50
      k->data[0] = sos_read (inp, &n, 0);
1527
50
      pktlen -= n;
1528
50
      if (!k->data[0])
1529
20
        {
1530
20
          rc = gpg_error (GPG_ERR_INV_PACKET);
1531
20
          goto leave;
1532
20
        }
1533
      /* Get the Kyber ciphertext.  */
1534
30
      rc = read_octet_string (inp, &pktlen, 4, 0, 0, k->data + 1);
1535
30
      if (rc)
1536
30
        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
4.32k
  else
1551
4.32k
    {
1552
11.5k
      for (i = 0; i < ndata; i++)
1553
7.20k
        {
1554
7.20k
          n = pktlen;
1555
7.20k
          k->data[i] = mpi_read (inp, &n, 0);
1556
7.20k
          pktlen -= n;
1557
7.20k
          if (!k->data[i])
1558
2.87k
            rc = gpg_error (GPG_ERR_INV_PACKET);
1559
7.20k
        }
1560
4.32k
      if (rc)
1561
2.32k
        goto leave;
1562
4.32k
    }
1563
53.6k
  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
81.7k
 leave:
1577
81.7k
  iobuf_skip_rest (inp, pktlen, 0);
1578
81.7k
  return rc;
1579
53.6k
}
1580
1581
1582
/* Dump a subpacket to LISTFP.  BUFFER contains the subpacket in
1583
 * question and points to the type field in the subpacket header (not
1584
 * the start of the header).  TYPE is the subpacket's type with the
1585
 * critical bit cleared.  CRITICAL is the value of the CRITICAL bit.
1586
 * BUFLEN is the length of the buffer and LENGTH is the length of the
1587
 * subpacket according to the subpacket's header.  DIGEST_ALGO is the
1588
 * digest algo of the signature.  */
1589
static void
1590
dump_sig_subpkt (int hashed, int type, int critical,
1591
     const byte * buffer, size_t buflen, size_t length,
1592
                 int digest_algo)
1593
0
{
1594
0
  const char *p = NULL;
1595
0
  int i;
1596
0
  int nprinted;
1597
1598
  /* The CERT has warning out with explains how to use GNUPG to detect
1599
   * the ARRs - we print our old message here when it is a faked ARR
1600
   * and add an additional notice.  */
1601
0
  if (type == SIGSUBPKT_ARR && !hashed)
1602
0
    {
1603
0
      es_fprintf (listfp,
1604
0
                  "\tsubpkt %d len %u (additional recipient request)\n"
1605
0
                  "WARNING: PGP versions > 5.0 and < 6.5.8 will automagically "
1606
0
                  "encrypt to this key and thereby reveal the plaintext to "
1607
0
                  "the owner of this ARR key. Detailed info follows:\n",
1608
0
                  type, (unsigned) length);
1609
0
    }
1610
1611
0
  buffer++;
1612
0
  length--;
1613
1614
0
  nprinted = es_fprintf (listfp, "\t%s%ssubpkt %d len %u (", /*) */
1615
0
                         critical ? "critical " : "",
1616
0
                         hashed ? "hashed " : "", type, (unsigned) length);
1617
0
  if (nprinted < 1)
1618
0
    nprinted = 1; /*(we use (nprinted-1) later.)*/
1619
0
  if (length > buflen)
1620
0
    {
1621
0
      es_fprintf (listfp, "too short: buffer is only %u)\n", (unsigned) buflen);
1622
0
      return;
1623
0
    }
1624
0
  switch (type)
1625
0
    {
1626
0
    case SIGSUBPKT_SIG_CREATED:
1627
0
      if (length >= 4)
1628
0
  es_fprintf (listfp, "sig created %s",
1629
0
                    strtimestamp (buf32_to_u32 (buffer)));
1630
0
      break;
1631
0
    case SIGSUBPKT_SIG_EXPIRE:
1632
0
      if (length >= 4)
1633
0
  {
1634
0
    if (buf32_to_u32 (buffer))
1635
0
      es_fprintf (listfp, "sig expires after %s",
1636
0
                        strtimevalue (buf32_to_u32 (buffer)));
1637
0
    else
1638
0
      es_fprintf (listfp, "sig does not expire");
1639
0
  }
1640
0
      break;
1641
0
    case SIGSUBPKT_EXPORTABLE:
1642
0
      if (length)
1643
0
  es_fprintf (listfp, "%sexportable", *buffer ? "" : "not ");
1644
0
      break;
1645
0
    case SIGSUBPKT_TRUST:
1646
0
      if (length != 2)
1647
0
  p = "[invalid trust subpacket]";
1648
0
      else
1649
0
  es_fprintf (listfp, "trust signature of depth %d, value %d", buffer[0],
1650
0
                    buffer[1]);
1651
0
      break;
1652
0
    case SIGSUBPKT_REGEXP:
1653
0
      if (!length)
1654
0
  p = "[invalid regexp subpacket]";
1655
0
      else
1656
0
        {
1657
0
          es_fprintf (listfp, "regular expression: \"");
1658
0
          es_write_sanitized (listfp, buffer, length, "\"", NULL);
1659
0
          p = "\"";
1660
0
        }
1661
0
      break;
1662
0
    case SIGSUBPKT_REVOCABLE:
1663
0
      if (length)
1664
0
  es_fprintf (listfp, "%srevocable", *buffer ? "" : "not ");
1665
0
      break;
1666
0
    case SIGSUBPKT_KEY_EXPIRE:
1667
0
      if (length >= 4)
1668
0
  {
1669
0
    if (buf32_to_u32 (buffer))
1670
0
      es_fprintf (listfp, "key expires after %s",
1671
0
                        strtimevalue (buf32_to_u32 (buffer)));
1672
0
    else
1673
0
      es_fprintf (listfp, "key does not expire");
1674
0
  }
1675
0
      break;
1676
0
    case SIGSUBPKT_PREF_SYM:
1677
0
      es_fputs ("pref-sym-algos:", listfp);
1678
0
      for (i = 0; i < length; i++)
1679
0
  es_fprintf (listfp, " %d", buffer[i]);
1680
0
      break;
1681
0
    case SIGSUBPKT_PREF_AEAD:
1682
0
      es_fputs ("pref-aead-algos:", listfp);
1683
0
      for (i = 0; i < length; i++)
1684
0
        es_fprintf (listfp, " %d", buffer[i]);
1685
0
      break;
1686
0
    case SIGSUBPKT_REV_KEY:
1687
0
      es_fputs ("revocation key: ", listfp);
1688
0
      if (length < 22)
1689
0
  p = "[too short]";
1690
0
      else
1691
0
  {
1692
0
    es_fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1]);
1693
0
    for (i = 2; i < length; i++)
1694
0
      es_fprintf (listfp, "%02X", buffer[i]);
1695
0
  }
1696
0
      break;
1697
0
    case SIGSUBPKT_ISSUER:
1698
0
      if (length >= 8)
1699
0
  es_fprintf (listfp, "issuer key ID %08lX%08lX",
1700
0
                    (ulong) buf32_to_u32 (buffer),
1701
0
                    (ulong) buf32_to_u32 (buffer + 4));
1702
0
      break;
1703
0
    case SIGSUBPKT_ISSUER_FPR:
1704
0
      if (length >= 21)
1705
0
        {
1706
0
          char *tmp;
1707
0
          es_fprintf (listfp, "issuer fpr v%d ", buffer[0]);
1708
0
          tmp = bin2hex (buffer+1, length-1, NULL);
1709
0
          if (tmp)
1710
0
            {
1711
0
              es_fputs (tmp, listfp);
1712
0
              xfree (tmp);
1713
0
            }
1714
0
        }
1715
0
      break;
1716
0
    case SIGSUBPKT_INT_RCP_FPR:
1717
0
      if (length >= 21)
1718
0
        {
1719
0
          char *tmp;
1720
0
          es_fprintf (listfp, "intended recipient (revocation subject) fpr v%d ",
1721
0
                      buffer[0]);
1722
0
          tmp = bin2hex (buffer+1, length -1, NULL);
1723
0
          if (tmp)
1724
0
            {
1725
0
              es_fputs (tmp, listfp);
1726
0
              xfree (tmp);
1727
0
            }
1728
0
        }
1729
0
      break;
1730
0
    case SIGSUBPKT_NOTATION:
1731
0
      {
1732
0
  es_fputs ("notation: ", listfp);
1733
0
  if (length < 8)
1734
0
    p = "[too short]";
1735
0
  else
1736
0
    {
1737
0
      const byte *s = buffer;
1738
0
      size_t n1, n2;
1739
1740
0
      n1 = (s[4] << 8) | s[5];
1741
0
      n2 = (s[6] << 8) | s[7];
1742
0
      s += 8;
1743
0
      if (8 + n1 + n2 != length)
1744
0
        p = "[error]";
1745
0
      else
1746
0
        {
1747
0
    es_write_sanitized (listfp, s, n1, ")", NULL);
1748
0
    es_putc ('=', listfp);
1749
1750
0
    if (*buffer & 0x80)
1751
0
      es_write_sanitized (listfp, s + n1, n2, ")", NULL);
1752
0
    else
1753
0
      p = "[not human readable]";
1754
0
        }
1755
0
    }
1756
0
      }
1757
0
      break;
1758
0
    case SIGSUBPKT_PREF_HASH:
1759
0
      es_fputs ("pref-hash-algos:", listfp);
1760
0
      for (i = 0; i < length; i++)
1761
0
  es_fprintf (listfp, " %d", buffer[i]);
1762
0
      break;
1763
0
    case SIGSUBPKT_PREF_COMPR:
1764
0
      es_fputs ("pref-zip-algos:", listfp);
1765
0
      for (i = 0; i < length; i++)
1766
0
  es_fprintf (listfp, " %d", buffer[i]);
1767
0
      break;
1768
0
    case SIGSUBPKT_KS_FLAGS:
1769
0
      es_fputs ("keyserver preferences:", listfp);
1770
0
      for (i = 0; i < length; i++)
1771
0
  es_fprintf (listfp, " %02X", buffer[i]);
1772
0
      break;
1773
0
    case SIGSUBPKT_PREF_KS:
1774
0
      es_fputs ("preferred keyserver: ", listfp);
1775
0
      es_write_sanitized (listfp, buffer, length, ")", NULL);
1776
0
      break;
1777
0
    case SIGSUBPKT_PRIMARY_UID:
1778
0
      p = "primary user ID";
1779
0
      break;
1780
0
    case SIGSUBPKT_POLICY:
1781
0
      es_fputs ("policy: ", listfp);
1782
0
      es_write_sanitized (listfp, buffer, length, ")", NULL);
1783
0
      break;
1784
0
    case SIGSUBPKT_KEY_FLAGS:
1785
0
      es_fputs ("key flags:", listfp);
1786
0
      for (i = 0; i < length; i++)
1787
0
  es_fprintf (listfp, " %02X", buffer[i]);
1788
0
      break;
1789
0
    case SIGSUBPKT_SIGNERS_UID:
1790
0
      p = "signer's user ID";
1791
0
      break;
1792
0
    case SIGSUBPKT_REVOC_REASON:
1793
0
      if (length)
1794
0
  {
1795
0
    es_fprintf (listfp, "revocation reason 0x%02x (", *buffer);
1796
0
    es_write_sanitized (listfp, buffer + 1, length - 1, ")", NULL);
1797
0
    p = ")";
1798
0
  }
1799
0
      break;
1800
0
    case SIGSUBPKT_ARR:
1801
0
      es_fputs ("Big Brother's key (ignored): ", listfp);
1802
0
      if (length < 22)
1803
0
  p = "[too short]";
1804
0
      else
1805
0
  {
1806
0
    es_fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1]);
1807
0
          if (length > 2)
1808
0
            es_write_hexstring (listfp, buffer+2, length-2, 0, NULL);
1809
0
  }
1810
0
      break;
1811
0
    case SIGSUBPKT_FEATURES:
1812
0
      es_fputs ("features:", listfp);
1813
0
      for (i = 0; i < length; i++)
1814
0
  es_fprintf (listfp, " %02x", buffer[i]);
1815
0
      break;
1816
0
    case SIGSUBPKT_SIGNATURE:
1817
0
      es_fputs ("signature: ", listfp);
1818
0
      if (length < 17)
1819
0
  p = "[too short]";
1820
0
      else
1821
0
  es_fprintf (listfp, "v%d, class 0x%02X, algo %d, digest algo %d",
1822
0
                    buffer[0],
1823
0
                    buffer[0] == 3 ? buffer[2] : buffer[1],
1824
0
                    buffer[0] == 3 ? buffer[15] : buffer[2],
1825
0
                    buffer[0] == 3 ? buffer[16] : buffer[3]);
1826
0
      break;
1827
1828
0
    case SIGSUBPKT_ATTST_SIGS:
1829
0
      {
1830
0
        unsigned int hlen;
1831
1832
0
  es_fputs ("attst-sigs: ", listfp);
1833
0
        hlen = gcry_md_get_algo_dlen (map_md_openpgp_to_gcry (digest_algo));
1834
0
  if (!hlen)
1835
0
    p = "[unknown digest algo]";
1836
0
        else if ((length % hlen))
1837
0
    p = "[invalid length]";
1838
0
  else
1839
0
    {
1840
0
            es_fprintf (listfp, "%u", (unsigned int)length/hlen);
1841
0
            while (length)
1842
0
              {
1843
0
                es_fprintf (listfp, "\n\t%*s", nprinted-1, "");
1844
0
                es_write_hexstring (listfp, buffer, hlen, 0, NULL);
1845
0
                buffer += hlen;
1846
0
                length -= hlen;
1847
0
              }
1848
0
    }
1849
0
      }
1850
0
      break;
1851
1852
0
    case SIGSUBPKT_KEY_BLOCK:
1853
0
      es_fputs ("key-block: ", listfp);
1854
0
      if (length && buffer[0])
1855
0
        p = "[unknown reserved octet]";
1856
0
      else if (length < 50)  /* 50 is an arbitrary min. length.  */
1857
0
        p = "[invalid subpacket]";
1858
0
      else
1859
0
        {
1860
          /* estream_t fp; */
1861
          /* fp = es_fopen ("a.key-block", "wb"); */
1862
          /* log_assert (fp); */
1863
          /* es_fwrite ( buffer+1, length-1, 1, fp); */
1864
          /* es_fclose (fp); */
1865
0
          es_fprintf (listfp, "[%u octets]", (unsigned int)length-1);
1866
0
        }
1867
0
      break;
1868
1869
1870
0
    default:
1871
0
      if (type >= 100 && type <= 110)
1872
0
  p = "experimental / private subpacket";
1873
0
      else
1874
0
  p = "?";
1875
0
      break;
1876
0
    }
1877
1878
0
  es_fprintf (listfp, "%s)\n", p ? p : "");
1879
0
}
1880
1881
1882
/*
1883
 * Returns: >= 0 use this offset into buffer
1884
 *      -1 explicitly reject returning this type
1885
 *      -2 subpacket too short
1886
 */
1887
int
1888
parse_one_sig_subpkt (const byte * buffer, size_t n, int type)
1889
30.9M
{
1890
30.9M
  switch (type)
1891
30.9M
    {
1892
8.63k
    case SIGSUBPKT_REV_KEY:
1893
8.63k
      if (n < 22)
1894
2.14k
  break;
1895
6.49k
      return 0;
1896
11.3M
    case SIGSUBPKT_SIG_CREATED:
1897
11.3M
    case SIGSUBPKT_SIG_EXPIRE:
1898
11.3M
    case SIGSUBPKT_KEY_EXPIRE:
1899
11.3M
      if (n < 4)
1900
4.73k
  break;
1901
11.3M
      return 0;
1902
364k
    case SIGSUBPKT_KEY_FLAGS:
1903
368k
    case SIGSUBPKT_KS_FLAGS:
1904
372k
    case SIGSUBPKT_PREF_SYM:
1905
373k
    case SIGSUBPKT_PREF_AEAD:
1906
377k
    case SIGSUBPKT_PREF_HASH:
1907
381k
    case SIGSUBPKT_PREF_COMPR:
1908
384k
    case SIGSUBPKT_POLICY:
1909
392k
    case SIGSUBPKT_PREF_KS:
1910
398k
    case SIGSUBPKT_FEATURES:
1911
398k
    case SIGSUBPKT_REGEXP:
1912
398k
    case SIGSUBPKT_ATTST_SIGS:
1913
398k
      return 0;
1914
6.88k
    case SIGSUBPKT_SIGNATURE:
1915
56.4k
    case SIGSUBPKT_EXPORTABLE:
1916
59.6k
    case SIGSUBPKT_REVOCABLE:
1917
59.8k
    case SIGSUBPKT_REVOC_REASON:
1918
59.8k
      if (!n)
1919
3.07k
  break;
1920
56.7k
      return 0;
1921
6.56M
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
1922
6.56M
      if (n < 8)
1923
4.84k
  break;
1924
6.56M
      return 0;
1925
12.5M
    case SIGSUBPKT_ISSUER_FPR:  /* issuer key fingerprint */
1926
12.5M
      if (n < 21)
1927
11.5k
  break;
1928
12.4M
      return 0;
1929
2.70k
    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
2.70k
      if (n < 8
1934
1.06k
    || 8 + ((buffer[4] << 8) | buffer[5]) +
1935
1.06k
    ((buffer[6] << 8) | buffer[7]) != n)
1936
2.00k
  break;
1937
705
      return 0;
1938
570
    case SIGSUBPKT_PRIMARY_UID:
1939
570
      if (n != 1)
1940
9
  break;
1941
561
      return 0;
1942
9.38k
    case SIGSUBPKT_TRUST:
1943
9.38k
      if (n != 2)
1944
8.60k
  break;
1945
783
      return 0;
1946
3.18k
    case SIGSUBPKT_KEY_BLOCK:
1947
3.18k
      if (n && buffer[0])
1948
1.60k
        return -1; /* Unknown version - ignore.  */
1949
1.57k
      if (n < 50)
1950
1.57k
  break;  /* Definitely too short to carry a key block.  */
1951
2
      return 0;
1952
36.6k
    default:
1953
36.6k
      return 0;
1954
30.9M
    }
1955
38.4k
  return -2;
1956
30.9M
}
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
1.00k
{
1963
1.00k
  strlist_t sl;
1964
1965
1.00k
  register_known_notation (NULL); /* Make sure it is initialized.  */
1966
1967
1.95k
  for (sl = known_notations_list; sl; sl = sl->next)
1968
1.00k
    if (sl->flags == len && !memcmp (sl->d, name, len))
1969
53
      return 1; /* Known */
1970
1971
951
  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
951
  return 0; /* Unknown.  */
1979
1.00k
}
1980
1981
1982
static int
1983
can_handle_critical (const byte * buffer, size_t n, int type)
1984
6.62M
{
1985
6.62M
  switch (type)
1986
6.62M
    {
1987
2.82k
    case SIGSUBPKT_NOTATION:
1988
2.82k
      if (n >= 8)
1989
1.19k
  {
1990
1.19k
    size_t notation_len = ((buffer[4] << 8) | buffer[5]);
1991
1.19k
    if (n - 8 >= notation_len)
1992
1.00k
      return can_handle_critical_notation (buffer + 8, notation_len);
1993
1.19k
  }
1994
1.82k
      return 0;
1995
2.03k
    case SIGSUBPKT_SIGNATURE:
1996
5.76k
    case SIGSUBPKT_SIG_CREATED:
1997
7.35k
    case SIGSUBPKT_SIG_EXPIRE:
1998
163k
    case SIGSUBPKT_KEY_EXPIRE:
1999
165k
    case SIGSUBPKT_EXPORTABLE:
2000
169k
    case SIGSUBPKT_REVOCABLE:
2001
177k
    case SIGSUBPKT_REV_KEY:
2002
184k
    case SIGSUBPKT_ISSUER:  /* issuer key ID */
2003
190k
    case SIGSUBPKT_ISSUER_FPR:  /* issuer fingerprint */
2004
192k
    case SIGSUBPKT_PREF_SYM:
2005
193k
    case SIGSUBPKT_PREF_AEAD:
2006
195k
    case SIGSUBPKT_PREF_HASH:
2007
198k
    case SIGSUBPKT_PREF_COMPR:
2008
212k
    case SIGSUBPKT_KEY_FLAGS:
2009
215k
    case SIGSUBPKT_PRIMARY_UID:
2010
219k
    case SIGSUBPKT_FEATURES:
2011
221k
    case SIGSUBPKT_TRUST:
2012
223k
    case SIGSUBPKT_REGEXP:
2013
224k
    case SIGSUBPKT_ATTST_SIGS:
2014
      /* Is it enough to show the policy or keyserver? */
2015
227k
    case SIGSUBPKT_POLICY:
2016
231k
    case SIGSUBPKT_PREF_KS:
2017
232k
    case SIGSUBPKT_REVOC_REASON: /* At least we know about it.  */
2018
232k
      return 1;
2019
2020
5.29k
    case SIGSUBPKT_KEY_BLOCK:
2021
5.29k
      if (n && !buffer[0])
2022
496
        return 1;
2023
4.79k
      else
2024
4.79k
        return 0;
2025
2026
6.38M
    default:
2027
6.38M
      return 0;
2028
6.62M
    }
2029
6.62M
}
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
305M
{
2036
305M
  const byte *buffer;
2037
305M
  int buflen;
2038
305M
  int type;
2039
305M
  int critical_dummy;
2040
305M
  int offset;
2041
305M
  size_t n;
2042
305M
  const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed;
2043
305M
  int seq = 0;
2044
305M
  int reqseq = start ? *start : 0;
2045
2046
305M
  if (!critical)
2047
305M
    critical = &critical_dummy;
2048
2049
305M
  if (!pktbuf || reqseq == -1)
2050
504k
    {
2051
504k
      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
504k
      return reqtype ==  SIGSUBPKT_TEST_CRITICAL ? dummy : NULL;
2055
504k
    }
2056
304M
  buffer = pktbuf->data;
2057
304M
  buflen = pktbuf->len;
2058
1.03G
  while (buflen)
2059
883M
    {
2060
883M
      n = *buffer++;
2061
883M
      buflen--;
2062
883M
      if (n == 255) /* 4 byte length header.  */
2063
442k
  {
2064
442k
    if (buflen < 4)
2065
145k
      goto too_short;
2066
296k
    n = buf32_to_size_t (buffer);
2067
296k
    buffer += 4;
2068
296k
    buflen -= 4;
2069
296k
  }
2070
882M
      else if (n >= 192) /* 4 byte special encoded length header.  */
2071
1.09M
  {
2072
1.09M
    if (buflen < 2)
2073
138k
      goto too_short;
2074
957k
    n = ((n - 192) << 8) + *buffer + 192;
2075
957k
    buffer++;
2076
957k
    buflen--;
2077
957k
  }
2078
882M
      if (buflen < n)
2079
110M
  goto too_short;
2080
772M
      if (!buflen)
2081
525k
        goto no_type_byte;
2082
771M
      type = *buffer;
2083
771M
      if (type & 0x80)
2084
97.2M
  {
2085
97.2M
    type &= 0x7f;
2086
97.2M
    *critical = 1;
2087
97.2M
  }
2088
674M
      else
2089
674M
  *critical = 0;
2090
771M
      if (!(++seq > reqseq))
2091
71.1k
  ;
2092
771M
      else if (reqtype == SIGSUBPKT_TEST_CRITICAL)
2093
43.0M
  {
2094
43.0M
    if (*critical)
2095
6.63M
      {
2096
6.63M
        if (n - 1 > buflen + 1)
2097
15.3k
    goto too_short;
2098
6.62M
        if (!can_handle_critical (buffer + 1, n - 1, type))
2099
6.38M
    {
2100
6.38M
      if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2101
6.38M
        log_info (_("subpacket of type %d has "
2102
0
        "critical bit set\n"), type);
2103
6.38M
      if (start)
2104
0
        *start = seq;
2105
6.38M
      return NULL; /* This is an error.  */
2106
6.38M
    }
2107
6.62M
      }
2108
43.0M
  }
2109
728M
      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
728M
      else if (type == reqtype) /* Found.  */
2113
30.9M
  {
2114
30.9M
    buffer++;
2115
30.9M
    n--;
2116
30.9M
    if (n > buflen)
2117
12.6k
      goto too_short;
2118
30.9M
    if (ret_n)
2119
12.9M
      *ret_n = n;
2120
30.9M
    offset = parse_one_sig_subpkt (buffer, n, type);
2121
30.9M
    switch (offset)
2122
30.9M
      {
2123
38.4k
      case -2:
2124
38.4k
        log_error ("subpacket of type %d too short\n", type);
2125
38.4k
        return NULL;
2126
1.60k
      case -1:
2127
1.60k
        return NULL;
2128
30.9M
      default:
2129
30.9M
        break;
2130
30.9M
      }
2131
30.9M
    if (start)
2132
16.8k
      *start = seq;
2133
30.9M
    return buffer + offset;
2134
30.9M
  }
2135
734M
      buffer += n;
2136
734M
      buflen -= n;
2137
734M
    }
2138
155M
  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
22.4M
    return buffer;
2144
2145
  /* Critical bit we don't understand. */
2146
133M
  if (start)
2147
729k
    *start = -1;
2148
133M
  return NULL;  /* End of packets; not found.  */
2149
2150
110M
 too_short:
2151
110M
  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
110M
  if (start)
2160
6.91M
    *start = -1;
2161
110M
  return NULL;
2162
2163
525k
 no_type_byte:
2164
525k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
2165
525k
    log_info ("type octet missing in subpacket\n");
2166
525k
  if (start)
2167
1.86k
    *start = -1;
2168
525k
  return NULL;
2169
155M
}
2170
2171
2172
const byte *
2173
parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype,
2174
      size_t *ret_n)
2175
297M
{
2176
297M
  return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL);
2177
297M
}
2178
2179
2180
const byte *
2181
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype)
2182
26.3M
{
2183
26.3M
  const byte *p;
2184
2185
26.3M
  p = parse_sig_subpkt (sig, 1, reqtype, NULL);
2186
26.3M
  if (!p)
2187
26.3M
    p = parse_sig_subpkt (sig, 0, reqtype, NULL);
2188
26.3M
  return p;
2189
26.3M
}
2190
2191
2192
/* Find all revocation keys.  Look in hashed area only.  */
2193
void
2194
parse_revkeys (PKT_signature * sig)
2195
6.92M
{
2196
6.92M
  const byte *revkey;
2197
6.92M
  int seq = 0;
2198
6.92M
  size_t len;
2199
2200
6.92M
  if (sig->sig_class != 0x1F)
2201
0
    return;
2202
2203
6.93M
  while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY,
2204
6.93M
                                    &len, &seq, NULL)))
2205
6.49k
    {
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
6.49k
      if ((len == 22 || len == 34)
2210
5.99k
          && (revkey[0] & 0x80))
2211
2.18k
  {
2212
2.18k
    sig->revkey = xrealloc (sig->revkey,
2213
2.18k
          sizeof (struct revocation_key) *
2214
2.18k
          (sig->numrevkeys + 1));
2215
2216
2.18k
    sig->revkey[sig->numrevkeys].class = revkey[0];
2217
2.18k
    sig->revkey[sig->numrevkeys].algid = revkey[1];
2218
2.18k
          len -= 2;
2219
2.18k
    sig->revkey[sig->numrevkeys].fprlen = len;
2220
2.18k
    memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len);
2221
2.18k
    memset (sig->revkey[sig->numrevkeys].fpr+len, 0,
2222
2.18k
                  sizeof (sig->revkey[sig->numrevkeys].fpr) - len);
2223
2.18k
    sig->numrevkeys++;
2224
2.18k
  }
2225
6.49k
    }
2226
6.92M
}
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
20.0M
{
2237
20.0M
  int md5_len = 0;
2238
20.0M
  unsigned n;
2239
20.0M
  int is_v4or5 = 0;
2240
20.0M
  int rc = 0;
2241
20.0M
  int i, ndata;
2242
2243
20.0M
  if (pktlen < 16)
2244
541k
    {
2245
541k
      log_error ("packet(%d) too short\n", pkttype);
2246
541k
      if (list_mode)
2247
541k
        es_fputs (":signature packet: [too short]\n", listfp);
2248
541k
      goto leave;
2249
541k
    }
2250
19.5M
  sig->version = iobuf_get_noeof (inp);
2251
19.5M
  pktlen--;
2252
19.5M
  if (sig->version == 4 || sig->version == 5)
2253
19.3M
    is_v4or5 = 1;
2254
129k
  else if (sig->version != 2 && sig->version != 3)
2255
2.88k
    {
2256
2.88k
      log_error ("packet(%d) with unknown version %d\n",
2257
2.88k
     pkttype, sig->version);
2258
2.88k
      if (list_mode)
2259
2.88k
        es_fputs (":signature packet: [unknown version]\n", listfp);
2260
2.88k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2261
2.88k
      goto leave;
2262
2.88k
    }
2263
2264
19.5M
  if (!is_v4or5)
2265
126k
    {
2266
126k
      if (pktlen == 0)
2267
0
  goto underflow;
2268
126k
      md5_len = iobuf_get_noeof (inp);
2269
126k
      pktlen--;
2270
126k
    }
2271
19.5M
  if (pktlen == 0)
2272
0
    goto underflow;
2273
19.5M
  sig->sig_class = iobuf_get_noeof (inp);
2274
19.5M
  pktlen--;
2275
19.5M
  if (!is_v4or5)
2276
126k
    {
2277
126k
      if (pktlen < 12)
2278
0
  goto underflow;
2279
126k
      sig->timestamp = read_32 (inp);
2280
126k
      pktlen -= 4;
2281
126k
      sig->keyid[0] = read_32 (inp);
2282
126k
      pktlen -= 4;
2283
126k
      sig->keyid[1] = read_32 (inp);
2284
126k
      pktlen -= 4;
2285
126k
    }
2286
19.5M
  if (pktlen < 2)
2287
64
    goto underflow;
2288
19.5M
  sig->pubkey_algo = iobuf_get_noeof (inp);
2289
19.5M
  pktlen--;
2290
19.5M
  sig->digest_algo = iobuf_get_noeof (inp);
2291
19.5M
  pktlen--;
2292
19.5M
  sig->flags.exportable = 1;
2293
19.5M
  sig->flags.revocable = 1;
2294
19.5M
  if (is_v4or5) /* Read subpackets.  */
2295
19.3M
    {
2296
19.3M
      if (pktlen < 2)
2297
0
  goto underflow;
2298
19.3M
      n = read_16 (inp);
2299
19.3M
      pktlen -= 2;  /* Length of hashed data. */
2300
19.3M
      if (pktlen < n)
2301
4.47k
  goto underflow;
2302
19.3M
      if (n > 30000)
2303
220
  {
2304
220
    log_error ("signature packet: hashed data too long (%u)\n", n);
2305
220
          if (list_mode)
2306
220
            es_fprintf (listfp,
2307
0
                        ":signature packet: [hashed data too long (%u)]\n", n);
2308
220
          rc = GPG_ERR_INV_PACKET;
2309
220
    goto leave;
2310
220
  }
2311
19.3M
      if (n)
2312
19.3M
  {
2313
19.3M
    sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1);
2314
19.3M
    sig->hashed->size = n;
2315
19.3M
    sig->hashed->len = n;
2316
19.3M
    if (iobuf_read (inp, sig->hashed->data, n) != n)
2317
1.15k
      {
2318
1.15k
        log_error ("premature eof while reading "
2319
1.15k
       "hashed signature data\n");
2320
1.15k
              if (list_mode)
2321
1.15k
                es_fputs (":signature packet: [premature eof]\n", listfp);
2322
1.15k
        rc = -1;
2323
1.15k
        goto leave;
2324
1.15k
      }
2325
19.3M
    pktlen -= n;
2326
19.3M
  }
2327
19.3M
      if (pktlen < 2)
2328
1.19k
  goto underflow;
2329
19.3M
      n = read_16 (inp);
2330
19.3M
      pktlen -= 2;  /* Length of unhashed data.  */
2331
19.3M
      if (pktlen < n)
2332
20.0k
  goto underflow;
2333
19.3M
      if (n > 10000)
2334
290
  {
2335
290
    log_error ("signature packet: unhashed data too long (%u)\n", n);
2336
290
          if (list_mode)
2337
290
            es_fprintf (listfp,
2338
0
                        ":signature packet: [unhashed data too long (%u)]\n",
2339
0
                        n);
2340
290
          rc = GPG_ERR_INV_PACKET;
2341
290
    goto leave;
2342
290
  }
2343
19.3M
      if (n)
2344
19.2M
  {
2345
19.2M
    sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1);
2346
19.2M
    sig->unhashed->size = n;
2347
19.2M
    sig->unhashed->len = n;
2348
19.2M
    if (iobuf_read (inp, sig->unhashed->data, n) != n)
2349
281
      {
2350
281
        log_error ("premature eof while reading "
2351
281
       "unhashed signature data\n");
2352
281
              if (list_mode)
2353
281
                es_fputs (":signature packet: [premature eof]\n", listfp);
2354
281
        rc = -1;
2355
281
        goto leave;
2356
281
      }
2357
19.2M
    pktlen -= n;
2358
19.2M
  }
2359
19.3M
    }
2360
2361
19.4M
  if (pktlen < 2)
2362
1.56k
    goto underflow;
2363
19.4M
  sig->digest_start[0] = iobuf_get_noeof (inp);
2364
19.4M
  pktlen--;
2365
19.4M
  sig->digest_start[1] = iobuf_get_noeof (inp);
2366
19.4M
  pktlen--;
2367
2368
19.4M
  if (is_v4or5 && sig->pubkey_algo)  /* Extract required information.  */
2369
19.3M
    {
2370
19.3M
      const byte *p;
2371
19.3M
      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
19.3M
      if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL)
2376
11.4M
    || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL))
2377
8.23M
  sig->flags.unknown_critical = 1;
2378
2379
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL);
2380
19.3M
      if (p)
2381
11.3M
  sig->timestamp = buf32_to_u32 (p);
2382
7.99M
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2383
7.99M
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2384
7.99M
        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
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len);
2390
19.3M
      if (p && len == 21 && p[0] == 4)
2391
12.3M
        {
2392
12.3M
          sig->keyid[0] = buf32_to_u32 (p + 1 + 12);
2393
12.3M
    sig->keyid[1] = buf32_to_u32 (p + 1 + 16);
2394
12.3M
  }
2395
6.98M
      else if (p && len == 33 && p[0] == 5)
2396
145
        {
2397
145
          sig->keyid[0] = buf32_to_u32 (p + 1 );
2398
145
    sig->keyid[1] = buf32_to_u32 (p + 1 + 4);
2399
145
  }
2400
6.98M
      else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER)))
2401
6.56M
        {
2402
6.56M
          sig->keyid[0] = buf32_to_u32 (p);
2403
6.56M
    sig->keyid[1] = buf32_to_u32 (p + 4);
2404
6.56M
  }
2405
426k
      else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110)
2406
422k
         && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2407
426k
  log_info ("signature packet without keyid\n");
2408
2409
      /* Get the intended recipient (revocation subject) fpr. */
2410
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_INT_RCP_FPR, &len);
2411
19.3M
      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
19.3M
      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
19.3M
      else
2426
19.3M
        {
2427
19.3M
         sig->rev_subject_info = NULL;
2428
19.3M
        }
2429
2430
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL);
2431
19.3M
      if (p && buf32_to_u32 (p))
2432
19.1k
  sig->expiredate = sig->timestamp + buf32_to_u32 (p);
2433
19.3M
      if (sig->expiredate && sig->expiredate <= make_timestamp ())
2434
18.7k
  sig->flags.expired = 1;
2435
2436
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL);
2437
19.3M
      if (p)
2438
2.40k
  sig->flags.policy_url = 1;
2439
2440
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL);
2441
19.3M
      if (p)
2442
4.69k
  sig->flags.pref_ks = 1;
2443
2444
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len);
2445
19.3M
      if (p && len)
2446
34.2k
        {
2447
34.2k
          char *mbox;
2448
2449
34.2k
          sig->signers_uid = try_make_printable_string (p, len, 0);
2450
34.2k
          if (!sig->signers_uid)
2451
0
            {
2452
0
              rc = gpg_error_from_syserror ();
2453
0
              goto leave;
2454
0
            }
2455
34.2k
          mbox = mailbox_from_userid (sig->signers_uid, 0);
2456
34.2k
          if (mbox)
2457
8.92k
            {
2458
8.92k
              xfree (sig->signers_uid);
2459
8.92k
              sig->signers_uid = mbox;
2460
8.92k
            }
2461
34.2k
        }
2462
2463
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL);
2464
19.3M
      if (p)
2465
2
        sig->flags.key_block = 1;
2466
2467
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL);
2468
19.3M
      if (p)
2469
705
  sig->flags.notation = 1;
2470
2471
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL);
2472
19.3M
      if (p && *p == 0)
2473
1.03k
  sig->flags.revocable = 0;
2474
2475
19.3M
      p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len);
2476
19.3M
      if (p && len == 2)
2477
783
  {
2478
783
    sig->trust_depth = p[0];
2479
783
    sig->trust_value = p[1];
2480
2481
    /* Only look for a regexp if there is also a trust
2482
       subpacket. */
2483
783
    sig->trust_regexp =
2484
783
      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
783
    if (len == 0)
2489
163
      sig->trust_regexp = NULL;
2490
783
  }
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
19.3M
      p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE);
2498
19.3M
      if (p && *p == 0)
2499
1.38k
  sig->flags.exportable = 0;
2500
2501
      /* Find all revocation keys.  */
2502
19.3M
      if (sig->sig_class == 0x1F)
2503
6.92M
  parse_revkeys (sig);
2504
19.3M
    }
2505
2506
19.4M
  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
19.4M
  ndata = pubkey_get_nsig (sig->pubkey_algo);
2523
19.4M
  if (!ndata)
2524
380k
    {
2525
380k
      if (list_mode)
2526
380k
  es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo);
2527
380k
      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
380k
      if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8))
2532
268
  {
2533
    /* We include a limit to avoid too trivial DoS attacks by
2534
       having gpg allocate too much memory.  */
2535
268
    log_error ("signature packet: too much data\n");
2536
268
    rc = GPG_ERR_INV_PACKET;
2537
268
  }
2538
380k
      else
2539
380k
  {
2540
380k
          void *tmpp;
2541
2542
380k
          tmpp = read_rest (inp, pktlen);
2543
380k
    sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2544
380k
    pktlen = 0;
2545
380k
  }
2546
380k
    }
2547
19.1M
  else
2548
19.1M
    {
2549
57.0M
      for (i = 0; i < ndata; i++)
2550
37.9M
  {
2551
37.9M
    n = pktlen;
2552
37.9M
          if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA
2553
36.9M
              || sig->pubkey_algo == PUBKEY_ALGO_EDDSA)
2554
37.7M
            sig->data[i] = sos_read (inp, &n, 0);
2555
264k
          else
2556
264k
            sig->data[i] = mpi_read (inp, &n, 0);
2557
37.9M
    pktlen -= n;
2558
37.9M
    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
37.9M
    if (!sig->data[i])
2565
5.57k
      rc = GPG_ERR_INV_PACKET;
2566
37.9M
  }
2567
19.1M
    }
2568
2569
20.0M
 leave:
2570
20.0M
  iobuf_skip_rest (inp, pktlen, 0);
2571
20.0M
  return rc;
2572
2573
27.3k
 underflow:
2574
27.3k
  log_error ("packet(%d) too short\n", pkttype);
2575
27.3k
  if (list_mode)
2576
27.3k
    es_fputs (":signature packet: [too short]\n", listfp);
2577
2578
27.3k
  iobuf_skip_rest (inp, pktlen, 0);
2579
2580
27.3k
  return GPG_ERR_INV_PACKET;
2581
19.4M
}
2582
2583
2584
static int
2585
parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen,
2586
       PKT_onepass_sig * ops)
2587
55.3k
{
2588
55.3k
  int version;
2589
55.3k
  int rc = 0;
2590
2591
55.3k
  if (pktlen < 13)
2592
15.9k
    {
2593
15.9k
      log_error ("packet(%d) too short\n", pkttype);
2594
15.9k
      if (list_mode)
2595
15.9k
        es_fputs (":onepass_sig packet: [too short]\n", listfp);
2596
15.9k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2597
15.9k
      goto leave;
2598
15.9k
    }
2599
39.3k
  version = iobuf_get_noeof (inp);
2600
39.3k
  pktlen--;
2601
39.3k
  if (version != 3)
2602
2.11k
    {
2603
2.11k
      log_error ("onepass_sig with unknown version %d\n", version);
2604
2.11k
      if (list_mode)
2605
2.11k
        es_fputs (":onepass_sig packet: [unknown version]\n", listfp);
2606
2.11k
      rc = gpg_error (GPG_ERR_INV_PACKET);
2607
2.11k
      goto leave;
2608
2.11k
    }
2609
37.2k
  ops->sig_class = iobuf_get_noeof (inp);
2610
37.2k
  pktlen--;
2611
37.2k
  ops->digest_algo = iobuf_get_noeof (inp);
2612
37.2k
  pktlen--;
2613
37.2k
  ops->pubkey_algo = iobuf_get_noeof (inp);
2614
37.2k
  pktlen--;
2615
37.2k
  ops->keyid[0] = read_32 (inp);
2616
37.2k
  pktlen -= 4;
2617
37.2k
  ops->keyid[1] = read_32 (inp);
2618
37.2k
  pktlen -= 4;
2619
37.2k
  ops->last = iobuf_get_noeof (inp);
2620
37.2k
  pktlen--;
2621
37.2k
  if (list_mode)
2622
37.2k
    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
55.3k
 leave:
2632
55.3k
  iobuf_skip_rest (inp, pktlen, 0);
2633
55.3k
  return rc;
2634
37.2k
}
2635
2636
2637
static int
2638
parse_key (IOBUF inp, int pkttype, unsigned long pktlen,
2639
     byte * hdr, int hdrlen, PACKET * pkt)
2640
1.37M
{
2641
1.37M
  gpg_error_t err = 0;
2642
1.37M
  int i, version, algorithm;
2643
1.37M
  unsigned long timestamp, expiredate, max_expiredate;
2644
1.37M
  int npkey, nskey;
2645
1.37M
  u32 keyid[2];
2646
1.37M
  PKT_public_key *pk;
2647
1.37M
  int is_v5;
2648
1.37M
  unsigned int pkbytes; /* For v5 keys: Number of bytes in the public
2649
                         * key material.  For v4 keys: 0.  */
2650
2651
1.37M
  (void) hdr;
2652
2653
1.37M
  pk = pkt->pkt.public_key; /* PK has been cleared. */
2654
2655
1.37M
  version = iobuf_get_noeof (inp);
2656
1.37M
  pktlen--;
2657
1.37M
  if (pkttype == PKT_PUBLIC_SUBKEY && version == '#')
2658
10.9k
    {
2659
      /* Early versions of G10 used the old PGP comments packets;
2660
       * luckily all those comments are started by a hash.  */
2661
10.9k
      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
10.9k
      iobuf_skip_rest (inp, pktlen, 0);
2678
10.9k
      return 0;
2679
10.9k
    }
2680
1.36M
  else if (version == 4)
2681
1.33M
    is_v5 = 0;
2682
29.0k
  else if (version == 5)
2683
14.4k
    is_v5 = 1;
2684
14.5k
  else if (version == 2 || version == 3)
2685
7.62k
    {
2686
      /* Not anymore supported since 2.1.  Use an older gpg version
2687
       * (i.e. gpg 1.4) to parse v3 packets.  */
2688
7.62k
      if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings)
2689
7.62k
        log_info ("packet(%d) with obsolete version %d\n", pkttype, version);
2690
7.62k
      if (list_mode)
2691
7.62k
        es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version);
2692
7.62k
      pk->version = version;
2693
7.62k
      err = gpg_error (GPG_ERR_LEGACY_KEY);
2694
7.62k
      goto leave;
2695
7.62k
    }
2696
6.92k
  else
2697
6.92k
    {
2698
6.92k
      log_error ("packet(%d) with unknown version %d\n", pkttype, version);
2699
6.92k
      if (list_mode)
2700
6.92k
        es_fputs (":key packet: [unknown version]\n", listfp);
2701
6.92k
      err = gpg_error (GPG_ERR_INV_PACKET);
2702
6.92k
      goto leave;
2703
6.92k
    }
2704
2705
1.34M
  if (pktlen < (is_v5? 15:11))
2706
604
    {
2707
604
      log_error ("packet(%d) too short\n", pkttype);
2708
604
      if (list_mode)
2709
604
        es_fputs (":key packet: [too short]\n", listfp);
2710
604
      err = gpg_error (GPG_ERR_INV_PACKET);
2711
604
      goto leave;
2712
604
    }
2713
1.34M
  else if (pktlen > MAX_KEY_PACKET_LENGTH)
2714
341
    {
2715
341
      log_error ("packet(%d) too large\n", pkttype);
2716
341
      if (list_mode)
2717
341
        es_fputs (":key packet: [too large]\n", listfp);
2718
341
      err = gpg_error (GPG_ERR_INV_PACKET);
2719
341
      goto leave;
2720
341
    }
2721
2722
1.34M
  timestamp = read_32 (inp);
2723
1.34M
  pktlen -= 4;
2724
1.34M
  expiredate = 0;   /* have to get it from the selfsignature */
2725
1.34M
  max_expiredate = 0;
2726
1.34M
  algorithm = iobuf_get_noeof (inp);
2727
1.34M
  pktlen--;
2728
1.34M
  if (is_v5)
2729
14.1k
    {
2730
14.1k
      pkbytes = read_32 (inp);
2731
14.1k
      pktlen -= 4;
2732
14.1k
    }
2733
1.33M
  else
2734
1.33M
    pkbytes = 0;
2735
2736
1.34M
  if (list_mode)
2737
0
    {
2738
0
      es_fprintf (listfp, ":%s key packet:\n"
2739
0
                  "\tversion %d, algo %d, created %lu, expires %lu",
2740
0
                  pkttype == PKT_PUBLIC_KEY ? "public" :
2741
0
                  pkttype == PKT_SECRET_KEY ? "secret" :
2742
0
                  pkttype == PKT_PUBLIC_SUBKEY ? "public sub" :
2743
0
                  pkttype == PKT_SECRET_SUBKEY ? "secret sub" : "??",
2744
0
                  version, algorithm, timestamp, expiredate);
2745
0
      if (is_v5)
2746
0
        es_fprintf (listfp, ", pkbytes %u\n", pkbytes);
2747
0
      else
2748
0
        es_fprintf (listfp, "\n");
2749
0
    }
2750
2751
1.34M
  pk->timestamp = timestamp;
2752
1.34M
  pk->expiredate = expiredate;
2753
1.34M
  pk->max_expiredate = max_expiredate;
2754
1.34M
  pk->hdrbytes = hdrlen;
2755
1.34M
  pk->version = version;
2756
1.34M
  pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY);
2757
1.34M
  pk->pubkey_algo = algorithm;
2758
2759
1.34M
  nskey = pubkey_get_nskey (algorithm);
2760
1.34M
  npkey = pubkey_get_npkey (algorithm);
2761
1.34M
  if (!npkey)
2762
60.0k
    {
2763
60.0k
      if (list_mode)
2764
60.0k
  es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm);
2765
60.0k
      unknown_pubkey_warning (algorithm);
2766
60.0k
    }
2767
2768
1.34M
  if (!npkey)
2769
60.0k
    {
2770
      /* Unknown algorithm - put data into an opaque MPI.  */
2771
60.0k
      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
60.0k
      pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0);
2775
60.0k
      pktlen = 0;
2776
60.0k
      goto leave;
2777
60.0k
    }
2778
1.28M
  else
2779
1.28M
    {
2780
5.00M
      for (i = 0; i < npkey; i++)
2781
3.73M
        {
2782
3.73M
          if (    (algorithm == PUBKEY_ALGO_ECDSA && (i == 0))
2783
3.72M
               || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0))
2784
3.68M
               || (algorithm == PUBKEY_ALGO_ECDH  && (i == 0 || i == 2))
2785
1.37M
               || (algorithm == PUBKEY_ALGO_KYBER && (i == 0)))
2786
2.36M
            {
2787
              /* Read the OID (i==0) or the KDF params (i==2).  */
2788
2.36M
        err = read_sized_octet_string (inp, &pktlen, pk->pkey+i);
2789
2.36M
            }
2790
1.37M
          else if (algorithm == PUBKEY_ALGO_KYBER && i == 2)
2791
1.36k
            {
2792
              /* Read the four-octet count prefixed Kyber public key.  */
2793
1.36k
        err = read_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i);
2794
1.36k
            }
2795
1.36M
          else
2796
1.36M
            {
2797
              /* Read MPI or SOS.  */
2798
1.36M
              unsigned int n = pktlen;
2799
1.36M
              if (algorithm == PUBKEY_ALGO_ECDSA
2800
1.35M
                  || algorithm == PUBKEY_ALGO_EDDSA
2801
1.32M
                  || algorithm == PUBKEY_ALGO_ECDH
2802
164k
                  || algorithm == PUBKEY_ALGO_KYBER)
2803
1.20M
                pk->pkey[i] = sos_read (inp, &n, 0);
2804
162k
              else
2805
162k
                pk->pkey[i] = mpi_read (inp, &n, 0);
2806
1.36M
              pktlen -= n;
2807
1.36M
              if (!pk->pkey[i])
2808
12.3k
                err = gpg_error (GPG_ERR_INV_PACKET);
2809
1.36M
            }
2810
3.73M
          if (err)
2811
14.4k
            goto leave;
2812
3.73M
        }
2813
1.27M
      if (list_mode)
2814
0
        {  /* Again so that we have all parameters in pkey[] and can
2815
            * do a look forward.  We use a hack for Kyber because the
2816
            * commonly used function pubkey_string requires an extra
2817
            * buffer and, more important, its result depends on an
2818
            * configure option.  */
2819
0
          for (i = 0; i < npkey; i++)
2820
0
            {
2821
0
              es_fprintf (listfp, "\tpkey[%d]: ", i);
2822
0
              mpi_print (listfp, pk->pkey[i], mpi_print_mode);
2823
0
              if ((algorithm == PUBKEY_ALGO_ECDSA
2824
0
                   || algorithm == PUBKEY_ALGO_EDDSA
2825
0
                   || algorithm == PUBKEY_ALGO_ECDH
2826
0
                   || algorithm == PUBKEY_ALGO_KYBER) && i==0)
2827
0
                {
2828
0
                  char *curve = openpgp_oid_to_str (pk->pkey[0]);
2829
0
                  const char *name = openpgp_oid_to_curve (curve, 2);
2830
2831
0
                  if (algorithm == PUBKEY_ALGO_KYBER)
2832
0
                    es_fprintf (listfp, " ky%u_%s (%s)",
2833
0
                                nbits_from_pk (pk), name?name:"", curve);
2834
0
                  else
2835
0
                    es_fprintf (listfp, " %s (%s)", name?name:"", curve);
2836
0
                  xfree (curve);
2837
0
                }
2838
0
              es_putc ('\n', listfp);
2839
0
            }
2840
0
        }
2841
1.27M
    }
2842
1.27M
  if (list_mode)
2843
0
    keyid_from_pk (pk, keyid);
2844
2845
1.27M
  if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY)
2846
68.0k
    {
2847
68.0k
      struct seckey_info *ski;
2848
68.0k
      byte temp[16];
2849
68.0k
      size_t snlen = 0;
2850
68.0k
      unsigned int skbytes;
2851
2852
68.0k
      if (pktlen < 1)
2853
518
        {
2854
518
          err = gpg_error (GPG_ERR_INV_PACKET);
2855
518
          goto leave;
2856
518
        }
2857
2858
67.5k
      pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
2859
67.5k
      if (!pk->seckey_info)
2860
0
        {
2861
0
          err = gpg_error_from_syserror ();
2862
0
          goto leave;
2863
0
        }
2864
2865
67.5k
      ski->algo = iobuf_get_noeof (inp);
2866
67.5k
      pktlen--;
2867
2868
67.5k
      if (is_v5)
2869
4.21k
        {
2870
4.21k
          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
4.21k
          if (!pktlen)
2875
509
            {
2876
509
              err = gpg_error (GPG_ERR_INV_PACKET);
2877
509
              goto leave;
2878
509
            }
2879
3.70k
          protcount = iobuf_get_noeof (inp);
2880
3.70k
          pktlen--;
2881
3.70k
          if (list_mode)
2882
3.70k
            es_fprintf (listfp, "\tprotbytes: %u\n", protcount);
2883
3.70k
        }
2884
2885
67.0k
      if (ski->algo)
2886
57.2k
  {
2887
57.2k
    ski->is_protected = 1;
2888
57.2k
    ski->s2k.count = 0;
2889
57.2k
    if (ski->algo == 253)
2890
86
      {
2891
86
              if (list_mode)
2892
86
                es_fprintf (listfp,
2893
0
                            "\tS2K pseudo algo %d is not yet supported\n",
2894
0
                            ski->algo);
2895
86
              err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2896
86
              goto leave;
2897
86
            }
2898
57.1k
          else if (ski->algo == 254 || ski->algo == 255)
2899
48.2k
      {
2900
48.2k
              if (pktlen < 3)
2901
2.28k
    {
2902
2.28k
      err = gpg_error (GPG_ERR_INV_PACKET);
2903
2.28k
      goto leave;
2904
2.28k
    }
2905
2906
45.9k
              ski->sha1chk = (ski->algo == 254);
2907
45.9k
        ski->algo = iobuf_get_noeof (inp);
2908
45.9k
        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
45.9k
        ski->s2k.mode = iobuf_get_noeof (inp);
2913
45.9k
        pktlen--;
2914
45.9k
        ski->s2k.hash_algo = iobuf_get_noeof (inp);
2915
45.9k
        pktlen--;
2916
        /* Check for the special GNU extension.  */
2917
45.9k
        if (ski->s2k.mode == 101)
2918
31.9k
    {
2919
158k
      for (i = 0; i < 4 && pktlen; i++, pktlen--)
2920
126k
        temp[i] = iobuf_get_noeof (inp);
2921
31.9k
      if (i < 4 || memcmp (temp, "GNU", 3))
2922
3.32k
        {
2923
3.32k
          if (list_mode)
2924
3.32k
      es_fprintf (listfp, "\tunknown S2K %d\n",
2925
0
                                    ski->s2k.mode);
2926
3.32k
          err = gpg_error (GPG_ERR_INV_PACKET);
2927
3.32k
          goto leave;
2928
3.32k
        }
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
28.5k
      ski->s2k.mode = 1000 + temp[3];
2934
28.5k
    }
2935
2936
              /* Read the salt.  */
2937
42.5k
        if (ski->s2k.mode == 3 || ski->s2k.mode == 1)
2938
8.80k
    {
2939
75.4k
      for (i = 0; i < 8 && pktlen; i++, pktlen--)
2940
66.6k
        temp[i] = iobuf_get_noeof (inp);
2941
8.80k
                  if (i < 8)
2942
482
                    {
2943
482
          err = gpg_error (GPG_ERR_INV_PACKET);
2944
482
          goto leave;
2945
482
                    }
2946
8.32k
      memcpy (ski->s2k.salt, temp, 8);
2947
8.32k
    }
2948
2949
              /* Check the mode.  */
2950
42.1k
        switch (ski->s2k.mode)
2951
42.1k
    {
2952
1.47k
    case 0:
2953
1.47k
      if (list_mode)
2954
1.47k
        es_fprintf (listfp, "\tsimple S2K");
2955
1.47k
      break;
2956
2.51k
    case 1:
2957
2.51k
      if (list_mode)
2958
2.51k
        es_fprintf (listfp, "\tsalted S2K");
2959
2.51k
      break;
2960
5.81k
    case 3:
2961
5.81k
      if (list_mode)
2962
5.81k
        es_fprintf (listfp, "\titer+salt S2K");
2963
5.81k
      break;
2964
2.18k
    case 1001:
2965
2.18k
      if (list_mode)
2966
2.18k
        es_fprintf (listfp, "\tgnu-dummy");
2967
2.18k
      break;
2968
18.9k
    case 1002:
2969
18.9k
      if (list_mode)
2970
18.9k
        es_fprintf (listfp, "\tgnu-divert-to-card");
2971
18.9k
      break;
2972
1.94k
    case 1003:
2973
1.94k
      if (list_mode)
2974
1.94k
        es_fprintf (listfp, "\tgnu-mode1003");
2975
1.94k
      break;
2976
9.23k
    default:
2977
9.23k
      if (list_mode)
2978
9.23k
        es_fprintf (listfp, "\tunknown %sS2K %d\n",
2979
0
                                ski->s2k.mode < 1000 ? "" : "GNU ",
2980
0
                                ski->s2k.mode);
2981
9.23k
      err = gpg_error (GPG_ERR_INV_PACKET);
2982
9.23k
      goto leave;
2983
42.1k
    }
2984
2985
              /* Print some info.  */
2986
32.8k
        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
32.8k
              if (list_mode)
2999
32.8k
                es_putc ('\n', listfp);
3000
3001
              /* Read remaining protection parameters.  */
3002
32.8k
        if (ski->s2k.mode == 3)
3003
5.81k
    {
3004
5.81k
      if (pktlen < 1)
3005
961
        {
3006
961
          err = gpg_error (GPG_ERR_INV_PACKET);
3007
961
          goto leave;
3008
961
        }
3009
4.85k
      ski->s2k.count = iobuf_get_noeof (inp);
3010
4.85k
      pktlen--;
3011
4.85k
      if (list_mode)
3012
4.85k
        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
4.85k
    }
3016
27.0k
        else if (ski->s2k.mode == 1002)
3017
18.9k
    {
3018
      /* Read the serial number. */
3019
18.9k
      if (pktlen < 1)
3020
6.20k
        {
3021
6.20k
          err = gpg_error (GPG_ERR_INV_PACKET);
3022
6.20k
          goto leave;
3023
6.20k
        }
3024
12.7k
      snlen = iobuf_get (inp);
3025
12.7k
      pktlen--;
3026
12.7k
      if (pktlen < snlen || snlen == (size_t)(-1))
3027
1.81k
        {
3028
1.81k
          err = gpg_error (GPG_ERR_INV_PACKET);
3029
1.81k
          goto leave;
3030
1.81k
        }
3031
12.7k
    }
3032
32.8k
      }
3033
8.93k
    else /* Old version; no S2K, so we set mode to 0, hash MD5.  */
3034
8.93k
      {
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
8.93k
        ski->s2k.mode = 0;
3039
8.93k
        ski->s2k.hash_algo = DIGEST_ALGO_MD5;
3040
8.93k
        if (list_mode)
3041
8.93k
    es_fprintf (listfp, "\tprotect algo: %d  (hash algo: %d)\n",
3042
0
                            ski->algo, ski->s2k.hash_algo);
3043
8.93k
      }
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
32.8k
    ski->ivlen = openpgp_cipher_blocklen (ski->algo);
3055
32.8k
    log_assert (ski->ivlen <= sizeof (temp));
3056
3057
32.8k
    if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003)
3058
4.12k
      ski->ivlen = 0;
3059
28.7k
    else if (ski->s2k.mode == 1002)
3060
10.9k
      ski->ivlen = snlen < 16 ? snlen : 16;
3061
3062
32.8k
    if (pktlen < ski->ivlen)
3063
2.16k
      {
3064
2.16k
              err = gpg_error (GPG_ERR_INV_PACKET);
3065
2.16k
        goto leave;
3066
2.16k
      }
3067
201k
    for (i = 0; i < ski->ivlen; i++, pktlen--)
3068
170k
      temp[i] = iobuf_get_noeof (inp);
3069
30.6k
    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
30.6k
    memcpy (ski->iv, temp, ski->ivlen);
3079
30.6k
  }
3080
3081
      /* Skip count of secret key material.  */
3082
40.4k
      if (is_v5)
3083
3.48k
        {
3084
3.48k
          if (pktlen < 4)
3085
653
            {
3086
653
              err = gpg_error (GPG_ERR_INV_PACKET);
3087
653
              goto leave;
3088
653
            }
3089
2.83k
          skbytes = read_32 (inp);
3090
2.83k
          pktlen -= 4;
3091
2.83k
          if (list_mode)
3092
2.83k
            es_fprintf (listfp, "\tskbytes: %u\n", skbytes);
3093
2.83k
        }
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
39.8k
      if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002)
3100
13.1k
  {
3101
    /* Better set some dummy stuff here.  */
3102
13.1k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3103
13.1k
             xstrdup ("dummydata"),
3104
13.1k
             10 * 8);
3105
13.1k
    pktlen = 0;
3106
13.1k
  }
3107
26.7k
      else if (ski->s2k.mode == 1003)
3108
1.94k
  {
3109
1.94k
          void *tmpp;
3110
3111
1.94k
    if (pktlen < 2) /* At least two bytes for parenthesis.  */
3112
1.22k
      {
3113
1.22k
              err = gpg_error (GPG_ERR_INV_PACKET);
3114
1.22k
        goto leave;
3115
1.22k
      }
3116
3117
721
          tmpp = read_rest (inp, pktlen);
3118
721
          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
721
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3134
721
             tmpp, tmpp? pktlen * 8 : 0);
3135
721
          pktlen = 0;
3136
721
  }
3137
24.7k
      else if (ski->is_protected)
3138
15.6k
  {
3139
15.6k
          void *tmpp;
3140
3141
15.6k
    if (pktlen < 2) /* At least two bytes for the length.  */
3142
3.38k
      {
3143
3.38k
              err = gpg_error (GPG_ERR_INV_PACKET);
3144
3.38k
        goto leave;
3145
3.38k
      }
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
12.2k
          tmpp = read_rest (inp, pktlen);
3153
12.2k
    pk->pkey[npkey] = gcry_mpi_set_opaque (NULL,
3154
12.2k
             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
12.2k
          if (pk->pkey[npkey])
3159
12.2k
            gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1);
3160
12.2k
    pktlen = 0;
3161
12.2k
    if (list_mode)
3162
12.2k
            es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey);
3163
12.2k
  }
3164
9.18k
      else
3165
9.18k
  {
3166
          /* Not encrypted.  */
3167
24.4k
    for (i = npkey; i < nskey; i++)
3168
17.5k
      {
3169
3170
17.5k
              if (pktlen < 2) /* At least two bytes for the length.  */
3171
2.28k
                {
3172
2.28k
                  err = gpg_error (GPG_ERR_INV_PACKET);
3173
2.28k
                  goto leave;
3174
2.28k
                }
3175
15.2k
              if (algorithm == PUBKEY_ALGO_KYBER && i == npkey+1)
3176
4
                {
3177
4
                  err = read_octet_string (inp, &pktlen, 4, 0, 1, pk->pkey+i);
3178
4
                  if (err)
3179
3
                    goto leave;
3180
4
                }
3181
15.2k
              else
3182
15.2k
                {
3183
15.2k
                  unsigned int n = pktlen;
3184
3185
15.2k
                  if (algorithm == PUBKEY_ALGO_ECDSA
3186
14.1k
                      || algorithm == PUBKEY_ALGO_EDDSA
3187
12.2k
                      || algorithm == PUBKEY_ALGO_ECDH
3188
11.8k
                      || algorithm == PUBKEY_ALGO_KYBER)
3189
3.34k
                    pk->pkey[i] = sos_read (inp, &n, 0);
3190
11.8k
                  else
3191
11.8k
                    pk->pkey[i] = mpi_read (inp, &n, 0);
3192
15.2k
                  pktlen -= n;
3193
15.2k
                }
3194
3195
15.2k
              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
15.2k
        if (!pk->pkey[i])
3203
6.25k
    err = gpg_error (GPG_ERR_INV_PACKET);
3204
15.2k
      }
3205
6.89k
    if (err)
3206
2.02k
      goto leave;
3207
3208
4.86k
    if (pktlen < 2)
3209
78
      {
3210
78
              if (opt.verbose)
3211
78
                log_info ("checksum is missing (remaining bytes: %lu)\n",
3212
0
                          pktlen);
3213
78
              err = gpg_error (GPG_ERR_INV_PACKET);
3214
78
        goto leave;
3215
78
      }
3216
4.78k
    ski->csum = read_16 (inp);
3217
4.78k
    pktlen -= 2;
3218
4.78k
    if (list_mode)
3219
4.78k
            es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum);
3220
4.78k
  }
3221
39.8k
    }
3222
3223
  /* Note that KEYID below has been initialized above in list_mode.  */
3224
1.23M
  if (list_mode)
3225
1.23M
    es_fprintf (listfp, "\tkeyid: %08lX%08lX\n",
3226
0
                (ulong) keyid[0], (ulong) keyid[1]);
3227
3228
1.36M
 leave:
3229
1.36M
  iobuf_skip_rest (inp, pktlen, 0);
3230
1.36M
  return err;
3231
1.23M
}
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
34.2k
{
3240
34.2k
  size_t n;
3241
34.2k
  int count = 0;
3242
34.2k
  struct user_attribute *attribs = NULL;
3243
34.2k
  const byte *buffer = uid->attrib_data;
3244
34.2k
  int buflen = uid->attrib_len;
3245
34.2k
  byte type;
3246
3247
34.2k
  xfree (uid->attribs);
3248
3249
55.3k
  while (buflen)
3250
29.5k
    {
3251
29.5k
      n = *buffer++;
3252
29.5k
      buflen--;
3253
29.5k
      if (n == 255)  /* 4 byte length header.  */
3254
1.65k
  {
3255
1.65k
    if (buflen < 4)
3256
713
      goto too_short;
3257
938
    n = buf32_to_size_t (buffer);
3258
938
    buffer += 4;
3259
938
    buflen -= 4;
3260
938
  }
3261
27.8k
      else if (n >= 192)  /* 2 byte special encoded length header.  */
3262
3.62k
  {
3263
3.62k
    if (buflen < 2)
3264
2.16k
      goto too_short;
3265
1.45k
    n = ((n - 192) << 8) + *buffer + 192;
3266
1.45k
    buffer++;
3267
1.45k
    buflen--;
3268
1.45k
  }
3269
26.6k
      if (buflen < n)
3270
4.11k
  goto too_short;
3271
3272
22.5k
      if (!n)
3273
1.40k
        {
3274
          /* Too short to encode the subpacket type.  */
3275
1.40k
          if (opt.verbose)
3276
1.40k
            log_info ("attribute subpacket too short\n");
3277
1.40k
          break;
3278
1.40k
        }
3279
3280
21.1k
      attribs = xrealloc (attribs,
3281
21.1k
                          (count + 1) * sizeof (struct user_attribute));
3282
21.1k
      memset (&attribs[count], 0, sizeof (struct user_attribute));
3283
3284
21.1k
      type = *buffer;
3285
21.1k
      buffer++;
3286
21.1k
      buflen--;
3287
21.1k
      n--;
3288
3289
21.1k
      attribs[count].type = type;
3290
21.1k
      attribs[count].data = buffer;
3291
21.1k
      attribs[count].len = n;
3292
21.1k
      buffer += n;
3293
21.1k
      buflen -= n;
3294
21.1k
      count++;
3295
21.1k
    }
3296
3297
27.2k
  uid->attribs = attribs;
3298
27.2k
  uid->numattribs = count;
3299
27.2k
  return count;
3300
3301
6.99k
 too_short:
3302
6.99k
  if (opt.verbose && !glo_ctrl.silence_parse_warnings)
3303
6.99k
    log_info ("buffer shorter than attribute subpacket\n");
3304
6.99k
  uid->attribs = attribs;
3305
6.99k
  uid->numattribs = count;
3306
6.99k
  return count;
3307
34.2k
}
3308
3309
3310
static int
3311
parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3312
198k
{
3313
198k
  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
198k
  if (pktlen > MAX_UID_PACKET_LENGTH)
3323
2.11k
    {
3324
2.11k
      log_error ("packet(%d) too large\n", pkttype);
3325
2.11k
      if (list_mode)
3326
2.11k
        es_fprintf (listfp, ":user ID packet: [too large]\n");
3327
2.11k
      iobuf_skip_rest (inp, pktlen, 0);
3328
2.11k
      return GPG_ERR_INV_PACKET;
3329
2.11k
    }
3330
3331
196k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen);
3332
196k
  packet->pkt.user_id->len = pktlen;
3333
196k
  packet->pkt.user_id->ref = 1;
3334
3335
196k
  p = packet->pkt.user_id->name;
3336
1.25M
  for (; pktlen; pktlen--, p++)
3337
1.05M
    *p = iobuf_get_noeof (inp);
3338
196k
  *p = 0;
3339
3340
196k
  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
196k
  return 0;
3355
198k
}
3356
3357
3358
void
3359
make_attribute_uidname (PKT_user_id * uid, size_t max_namelen)
3360
34.2k
{
3361
34.2k
  log_assert (max_namelen > 70);
3362
34.2k
  if (uid->numattribs <= 0)
3363
25.2k
    sprintf (uid->name, "[bad attribute packet of size %lu]",
3364
25.2k
       uid->attrib_len);
3365
9.01k
  else if (uid->numattribs > 1)
3366
590
    sprintf (uid->name, "[%d attributes of size %lu]",
3367
590
       uid->numattribs, uid->attrib_len);
3368
8.42k
  else
3369
8.42k
    {
3370
      /* Only one attribute, so list it as the "user id" */
3371
3372
8.42k
      if (uid->attribs->type == ATTRIB_IMAGE)
3373
6.43k
  {
3374
6.43k
    u32 len;
3375
6.43k
    byte type;
3376
3377
6.43k
    if (parse_image_header (uid->attribs, &type, &len))
3378
4.21k
      sprintf (uid->name, "[%.20s image of size %lu]",
3379
4.21k
         image_type_to_string (type, 1), (ulong) len);
3380
2.22k
    else
3381
2.22k
      sprintf (uid->name, "[invalid image]");
3382
6.43k
  }
3383
1.99k
      else
3384
1.99k
  sprintf (uid->name, "[unknown attribute of size %lu]",
3385
1.99k
     (ulong) uid->attribs->len);
3386
8.42k
    }
3387
3388
34.2k
  uid->len = strlen (uid->name);
3389
34.2k
}
3390
3391
3392
static int
3393
parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen,
3394
     PACKET * packet)
3395
34.6k
{
3396
34.6k
  byte *p;
3397
3398
34.6k
  (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
34.6k
  if (pktlen > MAX_ATTR_PACKET_LENGTH)
3404
371
    {
3405
371
      log_error ("packet(%d) too large\n", pkttype);
3406
371
      if (list_mode)
3407
371
        es_fprintf (listfp, ":attribute packet: [too large]\n");
3408
371
      iobuf_skip_rest (inp, pktlen, 0);
3409
371
      return GPG_ERR_INV_PACKET;
3410
371
    }
3411
3412
34.2k
#define EXTRA_UID_NAME_SPACE 71
3413
34.2k
  packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id
3414
34.2k
               + EXTRA_UID_NAME_SPACE);
3415
34.2k
  packet->pkt.user_id->ref = 1;
3416
34.2k
  packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1);
3417
34.2k
  packet->pkt.user_id->attrib_len = pktlen;
3418
3419
34.2k
  p = packet->pkt.user_id->attrib_data;
3420
2.69G
  for (; pktlen; pktlen--, p++)
3421
2.69G
    *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
34.2k
  parse_attribute_subpkts (packet->pkt.user_id);
3427
3428
34.2k
  make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE);
3429
3430
34.2k
  if (list_mode)
3431
0
    {
3432
0
      es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name);
3433
0
    }
3434
34.2k
  return 0;
3435
34.6k
}
3436
3437
3438
static int
3439
parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet)
3440
15.6k
{
3441
15.6k
  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
15.6k
  if (pktlen > MAX_COMMENT_PACKET_LENGTH)
3448
685
    {
3449
685
      log_error ("packet(%d) too large\n", pkttype);
3450
685
      if (list_mode)
3451
685
        es_fprintf (listfp, ":%scomment packet: [too large]\n",
3452
0
                    pkttype == PKT_OLD_COMMENT ? "OpenPGP draft " : "");
3453
685
      iobuf_skip_rest (inp, pktlen, 0);
3454
685
      return GPG_ERR_INV_PACKET;
3455
685
    }
3456
14.9k
  packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
3457
14.9k
  packet->pkt.comment->len = pktlen;
3458
14.9k
  p = packet->pkt.comment->data;
3459
2.49M
  for (; pktlen; pktlen--, p++)
3460
2.48M
    *p = iobuf_get_noeof (inp);
3461
3462
14.9k
  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
14.9k
  return 0;
3477
15.6k
}
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
19.4M
{
3487
19.4M
  gpg_error_t err;
3488
19.4M
  iobuf_t inp = ctx->inp;
3489
19.4M
  PKT_ring_trust rt = {0};
3490
19.4M
  int c;
3491
19.4M
  int not_gpg = 0;
3492
3493
19.4M
  if (!pktlen)
3494
11.1k
    {
3495
11.1k
      if (list_mode)
3496
11.1k
  es_fprintf (listfp, ":trust packet: empty\n");
3497
11.1k
      err = 0;
3498
11.1k
      goto leave;
3499
11.1k
    }
3500
3501
19.4M
  c = iobuf_get_noeof (inp);
3502
19.4M
  pktlen--;
3503
19.4M
  rt.trustval = c;
3504
19.4M
  if (pktlen)
3505
19.4M
    {
3506
19.4M
      if (!c)
3507
19.3M
        {
3508
19.3M
          c = iobuf_get_noeof (inp);
3509
          /* We require that bit 7 of the sigcache is 0 (easier
3510
           * eof handling).  */
3511
19.3M
          if (!(c & 0x80))
3512
19.3M
            rt.sigcache = c;
3513
19.3M
        }
3514
97.6k
      else
3515
97.6k
        iobuf_get_noeof (inp);  /* Dummy read.  */
3516
19.4M
      pktlen--;
3517
19.4M
    }
3518
3519
  /* Next is the optional subtype.  */
3520
19.4M
  if (pktlen > 3)
3521
19.4M
    {
3522
19.4M
      char tmp[4];
3523
19.4M
      tmp[0] = iobuf_get_noeof (inp);
3524
19.4M
      tmp[1] = iobuf_get_noeof (inp);
3525
19.4M
      tmp[2] = iobuf_get_noeof (inp);
3526
19.4M
      tmp[3] = iobuf_get_noeof (inp);
3527
19.4M
      pktlen -= 4;
3528
19.4M
      if (!memcmp (tmp, "gpg", 3))
3529
19.3M
        rt.subtype = tmp[3];
3530
80.0k
      else
3531
80.0k
        not_gpg = 1;
3532
19.4M
    }
3533
  /* If it is a key or uid subtype read the remaining data.  */
3534
19.4M
  if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID)
3535
71.0k
      && pktlen >= 6 )
3536
59.4k
    {
3537
59.4k
      int i;
3538
59.4k
      unsigned int namelen;
3539
3540
59.4k
      rt.keyorg = iobuf_get_noeof (inp);
3541
59.4k
      pktlen--;
3542
59.4k
      rt.keyupdate = read_32 (inp);
3543
59.4k
      pktlen -= 4;
3544
59.4k
      namelen = iobuf_get_noeof (inp);
3545
59.4k
      pktlen--;
3546
59.4k
      if (namelen && pktlen)
3547
13.7k
        {
3548
13.7k
          rt.url = xtrymalloc (namelen + 1);
3549
13.7k
          if (!rt.url)
3550
0
            {
3551
0
              err = gpg_error_from_syserror ();
3552
0
              goto leave;
3553
0
            }
3554
162k
          for (i = 0; pktlen && i < namelen; pktlen--, i++)
3555
148k
            rt.url[i] = iobuf_get_noeof (inp);
3556
13.7k
          rt.url[i] = 0;
3557
13.7k
        }
3558
59.4k
    }
3559
3560
19.4M
  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
19.4M
  if (!ctx->last_pkt.pkt.generic || ctx->skip_meta)
3596
63.4k
    ;
3597
19.3M
  else if (rt.subtype == RING_TRUST_SIG
3598
19.3M
           && ctx->last_pkt.pkttype == PKT_SIGNATURE)
3599
19.3M
    {
3600
19.3M
      PKT_signature *sig = ctx->last_pkt.pkt.signature;
3601
3602
19.3M
      if ((rt.sigcache & 1))
3603
11.0M
        {
3604
11.0M
          sig->flags.checked = 1;
3605
11.0M
          sig->flags.valid = !!(rt.sigcache & 2);
3606
11.0M
        }
3607
19.3M
    }
3608
65.1k
  else if (rt.subtype == RING_TRUST_UID
3609
36.0k
           && (ctx->last_pkt.pkttype == PKT_USER_ID
3610
8.04k
               || ctx->last_pkt.pkttype == PKT_ATTRIBUTE))
3611
27.9k
    {
3612
27.9k
      PKT_user_id *uid = ctx->last_pkt.pkt.user_id;
3613
3614
27.9k
      uid->keyorg = rt.keyorg;
3615
27.9k
      uid->keyupdate = rt.keyupdate;
3616
27.9k
      uid->updateurl = rt.url;
3617
27.9k
      rt.url = NULL;
3618
27.9k
    }
3619
37.2k
  else if (rt.subtype == RING_TRUST_KEY
3620
26.5k
           && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY
3621
13.0k
               || ctx->last_pkt.pkttype == PKT_SECRET_KEY))
3622
18.8k
    {
3623
18.8k
      PKT_public_key *pk = ctx->last_pkt.pkt.public_key;
3624
3625
18.8k
      pk->keyorg = rt.keyorg;
3626
18.8k
      pk->keyupdate = rt.keyupdate;
3627
18.8k
      pk->updateurl = rt.url;
3628
18.8k
      rt.url = NULL;
3629
18.8k
    }
3630
3631
19.4M
  err = 0;
3632
3633
19.4M
 leave:
3634
19.4M
  xfree (rt.url);
3635
19.4M
  free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL.  */
3636
19.4M
  iobuf_skip_rest (inp, pktlen, 0);
3637
19.4M
  return err;
3638
19.4M
}
3639
3640
3641
static int
3642
parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen,
3643
     PACKET * pkt, int new_ctb, int partial)
3644
172k
{
3645
172k
  int rc = 0;
3646
172k
  int mode, namelen;
3647
172k
  PKT_plaintext *pt;
3648
172k
  byte *p;
3649
172k
  int c, i;
3650
3651
172k
  if (!partial && pktlen < 6)
3652
7.78k
    {
3653
7.78k
      log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen);
3654
7.78k
      if (list_mode)
3655
7.78k
        es_fputs (":literal data packet: [too short]\n", listfp);
3656
7.78k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3657
7.78k
      goto leave;
3658
7.78k
    }
3659
164k
  mode = iobuf_get_noeof (inp);
3660
164k
  if (pktlen)
3661
151k
    pktlen--;
3662
164k
  namelen = iobuf_get_noeof (inp);
3663
164k
  if (pktlen)
3664
151k
    pktlen--;
3665
  /* Note that namelen will never exceed 255 bytes. */
3666
164k
  pt = pkt->pkt.plaintext =
3667
164k
    xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1);
3668
164k
  pt->new_ctb = new_ctb;
3669
164k
  pt->mode = mode;
3670
164k
  pt->namelen = namelen;
3671
164k
  pt->is_partial = partial;
3672
164k
  if (pktlen)
3673
151k
    {
3674
1.02M
      for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++)
3675
871k
  pt->name[i] = iobuf_get_noeof (inp);
3676
151k
    }
3677
13.6k
  else
3678
13.6k
    {
3679
60.5k
      for (i = 0; i < namelen; i++)
3680
48.4k
  if ((c = iobuf_get (inp)) == -1)
3681
1.56k
    break;
3682
46.8k
  else
3683
46.8k
    pt->name[i] = c;
3684
13.6k
    }
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
7.46M
  for (; i < namelen; i++)
3689
7.30M
    pt->name[i] = 0;
3690
3691
164k
  pt->timestamp = read_32 (inp);
3692
164k
  if (pktlen)
3693
151k
    pktlen -= 4;
3694
164k
  pt->len = pktlen;
3695
164k
  pt->buf = inp;
3696
3697
164k
  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
172k
 leave:
3718
172k
  return rc;
3719
164k
}
3720
3721
3722
static int
3723
parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen,
3724
      PACKET * pkt, int new_ctb)
3725
634k
{
3726
634k
  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
634k
  (void) pkttype;
3732
634k
  (void) pktlen;
3733
3734
634k
  zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed);
3735
634k
  zd->algorithm = iobuf_get_noeof (inp);
3736
634k
  zd->len = 0;      /* not used */
3737
634k
  zd->new_ctb = new_ctb;
3738
634k
  zd->buf = inp;
3739
634k
  if (list_mode)
3740
634k
    es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
3741
634k
  return 0;
3742
634k
}
3743
3744
3745
static int
3746
parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen,
3747
     PACKET * pkt, int new_ctb, int partial)
3748
78.3k
{
3749
78.3k
  int rc = 0;
3750
78.3k
  PKT_encrypted *ed;
3751
78.3k
  unsigned long orig_pktlen = pktlen;
3752
3753
78.3k
  ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted);
3754
  /* ed->len is set below.  */
3755
78.3k
  ed->extralen = 0;  /* Unknown here; only used in build_packet.  */
3756
78.3k
  ed->buf = NULL;
3757
78.3k
  ed->new_ctb = new_ctb;
3758
78.3k
  ed->is_partial = partial;
3759
78.3k
  ed->aead_algo = 0;
3760
78.3k
  ed->cipher_algo = 0; /* Only used with AEAD.  */
3761
78.3k
  ed->chunkbyte = 0;   /* Only used with AEAD.  */
3762
78.3k
  if (pkttype == PKT_ENCRYPTED_MDC)
3763
12.0k
    {
3764
      /* Fixme: add some pktlen sanity checks.  */
3765
12.0k
      int version;
3766
3767
12.0k
      version = iobuf_get_noeof (inp);
3768
12.0k
      if (orig_pktlen)
3769
3.71k
  pktlen--;
3770
12.0k
      if (version != 1)
3771
5.23k
  {
3772
5.23k
    log_error ("encrypted_mdc packet with unknown version %d\n",
3773
5.23k
         version);
3774
5.23k
          if (list_mode)
3775
5.23k
            es_fputs (":encrypted data packet: [unknown version]\n", listfp);
3776
    /*skip_rest(inp, pktlen); should we really do this? */
3777
5.23k
    rc = gpg_error (GPG_ERR_INV_PACKET);
3778
5.23k
    goto leave;
3779
5.23k
  }
3780
6.81k
      ed->mdc_method = DIGEST_ALGO_SHA1;
3781
6.81k
    }
3782
66.3k
  else
3783
66.3k
    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
73.1k
  if (orig_pktlen && pktlen < 10)
3789
691
    {
3790
      /* Actually this is blocksize+2.  */
3791
691
      log_error ("packet(%d) too short\n", pkttype);
3792
691
      if (list_mode)
3793
691
        es_fputs (":encrypted data packet: [too short]\n", listfp);
3794
691
      rc = GPG_ERR_INV_PACKET;
3795
691
      iobuf_skip_rest (inp, pktlen, partial);
3796
691
      goto leave;
3797
691
    }
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
72.4k
  ed->len = pktlen;
3803
3804
72.4k
  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
72.4k
  ed->buf = inp;
3816
3817
78.3k
 leave:
3818
78.3k
  return rc;
3819
72.4k
}
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
20.6k
{
3829
20.6k
  int rc = 0;
3830
20.6k
  PKT_mdc *mdc;
3831
20.6k
  byte *p;
3832
3833
20.6k
  (void) pkttype;
3834
3835
20.6k
  mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc);
3836
20.6k
  if (list_mode)
3837
20.6k
    es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
3838
20.6k
  if (!new_ctb || pktlen != 20)
3839
8.76k
    {
3840
8.76k
      log_error ("mdc_packet with invalid encoding\n");
3841
8.76k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3842
8.76k
      goto leave;
3843
8.76k
    }
3844
11.9k
  p = mdc->hash;
3845
250k
  for (; pktlen; pktlen--, p++)
3846
238k
    *p = iobuf_get_noeof (inp);
3847
3848
20.6k
 leave:
3849
20.6k
  return rc;
3850
11.9k
}
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
38.8k
{
3857
38.8k
  int rc = 0;
3858
38.8k
  PKT_encrypted *ed;
3859
38.8k
  unsigned long orig_pktlen = pktlen;
3860
38.8k
  int version;
3861
3862
38.8k
  ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted);
3863
38.8k
  if (!ed)
3864
0
    return gpg_error_from_syserror ();
3865
38.8k
  ed->len = 0;
3866
38.8k
  ed->extralen = 0;  /* (only used in build_packet.)  */
3867
38.8k
  ed->buf = NULL;
3868
38.8k
  ed->new_ctb = 1;   /* (packet number requires a new CTB anyway.)  */
3869
38.8k
  ed->is_partial = partial;
3870
38.8k
  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
38.8k
  if (orig_pktlen && pktlen < 19)
3874
771
    {
3875
771
      log_error ("packet(%d) too short\n", pkttype);
3876
771
      if (list_mode)
3877
771
        es_fputs (":aead encrypted packet: [too short]\n", listfp);
3878
771
      rc = gpg_error (GPG_ERR_INV_PACKET);
3879
771
      iobuf_skip_rest (inp, pktlen, partial);
3880
771
      goto leave;
3881
771
    }
3882
3883
38.0k
  version = iobuf_get_noeof (inp);
3884
38.0k
  if (orig_pktlen)
3885
1.94k
    pktlen--;
3886
38.0k
  if (version != 1)
3887
6.52k
    {
3888
6.52k
      log_error ("aead encrypted packet with unknown version %d\n",
3889
6.52k
                 version);
3890
6.52k
      if (list_mode)
3891
6.52k
        es_fputs (":aead encrypted packet: [unknown version]\n", listfp);
3892
      /*skip_rest(inp, pktlen); should we really do this? */
3893
6.52k
      rc = gpg_error (GPG_ERR_INV_PACKET);
3894
6.52k
      goto leave;
3895
6.52k
    }
3896
3897
31.5k
  ed->cipher_algo = iobuf_get_noeof (inp);
3898
31.5k
  if (orig_pktlen)
3899
351
    pktlen--;
3900
31.5k
  ed->aead_algo = iobuf_get_noeof (inp);
3901
31.5k
  if (orig_pktlen)
3902
351
    pktlen--;
3903
31.5k
  ed->chunkbyte = iobuf_get_noeof (inp);
3904
31.5k
  if (orig_pktlen)
3905
351
    pktlen--;
3906
3907
  /* Store the remaining length of the encrypted data.  We read the
3908
   * rest during decryption.  */
3909
31.5k
  ed->len = pktlen;
3910
3911
31.5k
  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
31.5k
  ed->buf = inp;
3922
3923
38.8k
 leave:
3924
38.8k
  return rc;
3925
31.5k
}
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
22.8k
{
3943
22.8k
  byte *p;
3944
22.8k
  const byte *sesmark;
3945
22.8k
  size_t sesmarklen;
3946
22.8k
  int i;
3947
3948
22.8k
  (void) pkttype;
3949
3950
22.8k
  if (list_mode)
3951
22.8k
    es_fprintf (listfp, ":packet 63: length %lu ", pktlen);
3952
3953
22.8k
  sesmark = get_session_marker (&sesmarklen);
3954
22.8k
  if (pktlen < sesmarklen + 1)  /* 1 is for the control bytes */
3955
6.96k
    goto skipit;
3956
178k
  for (i = 0; i < sesmarklen; i++, pktlen--)
3957
167k
    {
3958
167k
      if (sesmark[i] != iobuf_get_noeof (inp))
3959
5.78k
  goto skipit;
3960
167k
    }
3961
10.1k
  if (pktlen > 4096)
3962
3
    goto skipit;  /* Definitely too large.  We skip it to avoid an
3963
                     overflow in the malloc.  */
3964
10.1k
  if (list_mode)
3965
10.1k
    es_fputs ("- gpg control packet", listfp);
3966
3967
10.1k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
3968
10.1k
             + pktlen - 1);
3969
10.1k
  packet->pkt.gpg_control->control = iobuf_get_noeof (inp);
3970
10.1k
  pktlen--;
3971
10.1k
  packet->pkt.gpg_control->datalen = pktlen;
3972
10.1k
  p = packet->pkt.gpg_control->data;
3973
33.0k
  for (; pktlen; pktlen--, p++)
3974
22.8k
    *p = iobuf_get_noeof (inp);
3975
3976
10.1k
  return 0;
3977
3978
12.7k
 skipit:
3979
12.7k
  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
12.7k
  iobuf_skip_rest (inp, pktlen, 0);
4002
12.7k
  return gpg_error (GPG_ERR_INV_PACKET);
4003
10.1k
}
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
161k
{
4010
161k
  PACKET *packet;
4011
161k
  byte *p;
4012
4013
161k
  if (!data)
4014
0
    datalen = 0;
4015
4016
161k
  packet = xmalloc (sizeof *packet);
4017
161k
  init_packet (packet);
4018
161k
  packet->pkttype = PKT_GPG_CONTROL;
4019
161k
  packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen);
4020
161k
  packet->pkt.gpg_control->control = type;
4021
161k
  packet->pkt.gpg_control->datalen = datalen;
4022
161k
  p = packet->pkt.gpg_control->data;
4023
9.16M
  for (; datalen; datalen--, p++)
4024
9.00M
    *p = *data++;
4025
4026
161k
  return packet;
4027
161k
}