Coverage Report

Created: 2026-09-03 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/free-packet.c
Line
Count
Source
1
/* free-packet.c - cleanup stuff for packets
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3
 *               2005, 2010  Free Software Foundation, Inc.
4
 *
5
 * This file is part of GnuPG.
6
 *
7
 * GnuPG is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * GnuPG is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
#include <config.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "gpg.h"
27
#include "../common/util.h"
28
#include "packet.h"
29
#include "../common/iobuf.h"
30
#include "options.h"
31
32
33
/* This is a wrapper for mpi_copy which handles opaque MPIs with a
34
 * NULL pointer as opaque data; e.g. gcry_mpi_set_opaque(a, NULL, 0).
35
 * It seems that at least gcry_mpi_set_opaque_copy does not yet handle
36
 * this correctly.  */
37
static gcry_mpi_t
38
my_mpi_copy (gcry_mpi_t a)
39
0
{
40
0
  if (a
41
0
      && gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)
42
0
      && !gcry_mpi_get_opaque (a, NULL))
43
0
    return NULL;
44
45
0
  return gcry_mpi_copy (a);
46
0
}
47
48
49
void
50
free_symkey_enc( PKT_symkey_enc *enc )
51
3.25k
{
52
3.25k
  if (enc)
53
3.25k
    xfree (enc->seskey);
54
3.25k
  xfree(enc);
55
3.25k
}
56
57
58
/* This is the core of free_pubkey_enc but does only release the
59
 * allocated members of ENC.  */
60
void
61
release_pubkey_enc_parts (PKT_pubkey_enc *enc)
62
35.5k
{
63
35.5k
  int n, i;
64
35.5k
  n = pubkey_get_nenc( enc->pubkey_algo );
65
35.5k
  if (!n)
66
35.5k
    mpi_release (enc->data[0]);
67
72.3k
  for (i=0; i < n; i++ )
68
36.8k
    mpi_release (enc->data[i]);
69
35.5k
}
70
71
void
72
free_pubkey_enc (PKT_pubkey_enc *enc)
73
35.5k
{
74
35.5k
  release_pubkey_enc_parts (enc);
75
35.5k
  xfree (enc);
76
35.5k
}
77
78
79
/* Copy everything from SRC to DST.  This assumes that DST has been
80
 * malloced or statically allocated. */
81
void
82
copy_pubkey_enc_parts (PKT_pubkey_enc *dst, PKT_pubkey_enc *src)
83
0
{
84
0
  int n, i;
85
86
0
  dst->keyid[0] = src->keyid[0];
87
0
  dst->keyid[1] = src->keyid[1];
88
0
  dst->version  = src->version;
89
0
  dst->pubkey_algo = src->pubkey_algo;
90
0
  dst->seskey_algo = src->seskey_algo;
91
0
  dst->throw_keyid = src->throw_keyid;
92
93
0
  if (!(n = pubkey_get_nenc (dst->pubkey_algo)))
94
0
    n = 1;  /* All data is in the first item as an opaque MPI. */
95
0
  for (i=0; i < n; i++)
96
0
    dst->data[i] = my_mpi_copy (src->data[i]);
97
0
  for (; i < PUBKEY_MAX_NENC; i++)
98
0
    dst->data[i] = NULL;
99
0
}
100
101
102
void
103
free_seckey_enc( PKT_signature *sig )
104
548k
{
105
548k
  int n, i;
106
107
548k
  n = pubkey_get_nsig( sig->pubkey_algo );
108
548k
  if( !n )
109
548k
    mpi_release(sig->data[0]);
110
557k
  for(i=0; i < n; i++ )
111
548k
    mpi_release( sig->data[i] );
112
113
548k
  xfree(sig->revkey);
114
548k
  xfree(sig->hashed);
115
548k
  xfree(sig->unhashed);
116
117
  /* Do not forget to update copy_signature() too. */
118
548k
  xfree(sig->rev_subject_info);
119
548k
  xfree (sig->signers_uid);
120
121
548k
  xfree(sig);
122
548k
}
123
124
125
void
126
release_public_key_parts (PKT_public_key *pk)
127
92.1k
{
128
92.1k
  int n, i;
129
130
92.1k
  if (pk->seckey_info)
131
46.3k
    n = pubkey_get_nskey (pk->pubkey_algo);
132
45.8k
  else
133
45.8k
    n = pubkey_get_npkey (pk->pubkey_algo);
134
92.1k
  if (!n)
135
92.1k
    mpi_release (pk->pkey[0]);
136
387k
  for (i=0; i < n; i++ )
137
295k
    {
138
295k
      mpi_release (pk->pkey[i]);
139
295k
      pk->pkey[i] = NULL;
140
295k
    }
141
92.1k
  if (pk->seckey_info)
142
46.3k
    {
143
46.3k
      xfree (pk->seckey_info);
144
46.3k
      pk->seckey_info = NULL;
145
46.3k
    }
146
92.1k
  if (pk->prefs)
147
0
    {
148
0
      xfree (pk->prefs);
149
0
      pk->prefs = NULL;
150
0
    }
151
92.1k
  free_user_id (pk->user_id);
152
92.1k
  pk->user_id = NULL;
153
92.1k
  if (pk->revkey)
154
0
    {
155
0
      xfree(pk->revkey);
156
0
      pk->revkey=NULL;
157
0
      pk->numrevkeys=0;
158
0
    }
159
92.1k
  if (pk->serialno)
160
0
    {
161
0
      xfree (pk->serialno);
162
0
      pk->serialno = NULL;
163
0
    }
164
92.1k
  if (pk->updateurl)
165
0
    {
166
0
      xfree (pk->updateurl);
167
0
      pk->updateurl = NULL;
168
0
    }
169
92.1k
  if (pk->revoked.reason_comment)
170
0
    {
171
0
      xfree (pk->revoked.reason_comment);
172
0
      pk->revoked.reason_comment = NULL;
173
0
    }
174
92.1k
}
175
176
177
/* Free an allocated public key structure including all parts.
178
   Passing NULL is allowed.  */
179
void
180
free_public_key (PKT_public_key *pk)
181
201k
{
182
201k
  if (pk)
183
92.1k
    {
184
92.1k
      release_public_key_parts (pk);
185
92.1k
      xfree(pk);
186
92.1k
    }
187
201k
}
188
189
190
static subpktarea_t *
191
cp_subpktarea (subpktarea_t *s )
192
0
{
193
0
    subpktarea_t *d;
194
195
0
    if( !s )
196
0
  return NULL;
197
0
    d = xmalloc (sizeof (*d) + s->size - 1 );
198
0
    d->size = s->size;
199
0
    d->len = s->len;
200
0
    memcpy (d->data, s->data, s->len);
201
0
    return d;
202
0
}
203
204
/*
205
 * Return a copy of the preferences
206
 */
207
prefitem_t *
208
copy_prefs (const prefitem_t *prefs)
209
0
{
210
0
    size_t n;
211
0
    prefitem_t *new;
212
213
0
    if (!prefs)
214
0
        return NULL;
215
216
0
    for (n=0; prefs[n].type; n++)
217
0
        ;
218
0
    new = xmalloc ( sizeof (*new) * (n+1));
219
0
    for (n=0; prefs[n].type; n++) {
220
0
        new[n].type = prefs[n].type;
221
0
        new[n].value = prefs[n].value;
222
0
    }
223
0
    new[n].type = PREFTYPE_NONE;
224
0
    new[n].value = 0;
225
226
0
    return new;
227
0
}
228
229
230
/* Copy the public key S to D.  If D is NULL allocate a new public key
231
 * structure.  Only the basic stuff is copied; not any ancillary
232
 * data.  */
233
PKT_public_key *
234
copy_public_key_basics (PKT_public_key *d, PKT_public_key *s)
235
0
{
236
0
  int n, i;
237
238
0
  if (!d)
239
0
    d = xmalloc (sizeof *d);
240
0
  memcpy (d, s, sizeof *d);
241
0
  d->seckey_info = NULL;
242
0
  d->user_id = NULL;
243
0
  d->prefs = NULL;
244
0
  d->revoked.got_reason = 0;
245
0
  d->revoked.reason_code = 0;
246
0
  d->revoked.reason_comment = NULL;
247
0
  d->revoked.reason_comment_len = 0;
248
249
0
  n = pubkey_get_npkey (s->pubkey_algo);
250
0
  i = 0;
251
0
  if (!n)
252
0
    d->pkey[i++] = my_mpi_copy (s->pkey[0]);
253
0
  else
254
0
    {
255
0
      for (; i < n; i++ )
256
0
        d->pkey[i] = my_mpi_copy (s->pkey[i]);
257
0
    }
258
0
  for (; i < PUBKEY_MAX_NSKEY; i++)
259
0
    d->pkey[i] = NULL;
260
261
0
  d->revkey = NULL;
262
0
  d->serialno = NULL;
263
0
  d->updateurl = NULL;
264
265
0
  return d;
266
0
}
267
268
269
/* Copy the public key S to D.  If D is NULL allocate a new public key
270
   structure.  If S has seckret key infos, only the public stuff is
271
   copied.  */
272
PKT_public_key *
273
copy_public_key (PKT_public_key *d, PKT_public_key *s)
274
0
{
275
0
  d = copy_public_key_basics (d, s);
276
0
  d->user_id = scopy_user_id (s->user_id);
277
0
  d->prefs = copy_prefs (s->prefs);
278
279
0
  if (!s->revkey && s->numrevkeys)
280
0
    BUG();
281
0
  if (s->numrevkeys)
282
0
    {
283
0
      d->revkey = xmalloc(sizeof(struct revocation_key)*s->numrevkeys);
284
0
      memcpy(d->revkey,s->revkey,sizeof(struct revocation_key)*s->numrevkeys);
285
0
    }
286
287
0
  if (s->serialno)
288
0
    d->serialno = xstrdup (s->serialno);
289
0
  if (s->updateurl)
290
0
    d->updateurl = xstrdup (s->updateurl);
291
0
  if (s->revoked.got_reason)
292
0
    {
293
0
      d->revoked.got_reason = s->revoked.got_reason;
294
0
      d->revoked.reason_code = s->revoked.reason_code;
295
0
      if (s->revoked.reason_comment_len)
296
0
        {
297
0
          d->revoked.reason_comment = xmalloc (s->revoked.reason_comment_len);
298
0
          memcpy (d->revoked.reason_comment, s->revoked.reason_comment,
299
0
                  s->revoked.reason_comment_len);
300
0
          d->revoked.reason_comment_len = s->revoked.reason_comment_len;
301
0
        }
302
0
    }
303
304
0
  return d;
305
0
}
306
307
308
309
/* When modifying the signature struct adjust copy_signature()
310
 * such that the changes are considered too (e.g. pointers are set to NULL
311
 * if they are not copied. */
312
PKT_signature *
313
copy_signature( PKT_signature *d, PKT_signature *s )
314
0
{
315
0
    int n, i;
316
317
0
    if( !d )
318
0
  d = xmalloc(sizeof *d);
319
0
    memcpy( d, s, sizeof *d );
320
0
    n = pubkey_get_nsig( s->pubkey_algo );
321
0
    if( !n )
322
0
  d->data[0] = my_mpi_copy(s->data[0]);
323
0
    else {
324
0
  for(i=0; i < n; i++ )
325
0
      d->data[i] = my_mpi_copy( s->data[i] );
326
0
    }
327
0
    d->hashed = cp_subpktarea (s->hashed);
328
0
    d->unhashed = cp_subpktarea (s->unhashed);
329
0
    if (s->signers_uid)
330
0
      d->signers_uid = xstrdup (s->signers_uid);
331
0
    else
332
0
      d->signers_uid = NULL;
333
334
0
    if(s->numrevkeys)
335
0
      {
336
0
  d->revkey=NULL;
337
0
  d->numrevkeys=0;
338
0
  parse_revkeys(d);
339
0
      }
340
341
0
    if (s->rev_subject_info)
342
0
      {
343
0
        d->rev_subject_info = xmalloc (sizeof *d->rev_subject_info);
344
0
        memcpy (d->rev_subject_info, s->rev_subject_info, sizeof *d->rev_subject_info);
345
0
      }
346
0
    else
347
0
      d->rev_subject_info = NULL;
348
349
0
    return d;
350
0
}
351
352
353
/*
354
 * shallow copy of the user ID
355
 */
356
PKT_user_id *
357
scopy_user_id (PKT_user_id *s)
358
0
{
359
0
    if (s)
360
0
        s->ref++;
361
0
    return s;
362
0
}
363
364
365
366
void
367
free_comment( PKT_comment *rem )
368
619
{
369
619
    xfree(rem);
370
619
}
371
372
void
373
free_attributes(PKT_user_id *uid)
374
4.31k
{
375
4.31k
  if (!uid)
376
0
    return;
377
378
4.31k
  xfree(uid->attribs);
379
4.31k
  xfree(uid->attrib_data);
380
381
4.31k
  uid->attribs=NULL;
382
4.31k
  uid->attrib_data=NULL;
383
4.31k
  uid->attrib_len=0;
384
4.31k
}
385
386
void
387
free_user_id (PKT_user_id *uid)
388
96.4k
{
389
96.4k
  if (!uid)
390
92.1k
    return;
391
392
96.4k
  log_assert (uid->ref > 0);
393
4.31k
  if (--uid->ref)
394
0
    return;
395
396
4.31k
  free_attributes(uid);
397
4.31k
  xfree (uid->prefs);
398
4.31k
  xfree (uid->namehash);
399
4.31k
  xfree (uid->updateurl);
400
4.31k
  xfree (uid->mbox);
401
4.31k
  xfree (uid);
402
4.31k
}
403
404
void
405
free_compressed( PKT_compressed *zd )
406
470k
{
407
470k
  if (!zd)
408
0
    return;
409
410
470k
  if (zd->buf)
411
789
    {
412
      /* We need to skip some bytes.  Because don't have any
413
       * information about the length, so we assume this is the last
414
       * packet */
415
1.30k
      while (iobuf_read( zd->buf, NULL, 1<<30 ) != -1)
416
519
        ;
417
789
    }
418
470k
  xfree(zd);
419
470k
}
420
421
void
422
free_encrypted( PKT_encrypted *ed )
423
7.23k
{
424
7.23k
  if (!ed)
425
0
    return;
426
427
7.23k
  if (ed->buf)
428
660
    {
429
      /* We need to skip some bytes. */
430
660
      if (ed->is_partial)
431
141
        {
432
241
          while (iobuf_read( ed->buf, NULL, 1<<30 ) != -1)
433
100
            ;
434
141
  }
435
519
      else
436
519
        {
437
951
          while (ed->len)
438
432
            {
439
              /* Skip the packet. */
440
432
              int n = iobuf_read( ed->buf, NULL, ed->len );
441
432
              if (n == -1)
442
253
                ed->len = 0;
443
179
              else
444
179
                ed->len -= n;
445
432
            }
446
519
  }
447
660
    }
448
7.23k
  xfree (ed);
449
7.23k
}
450
451
452
void
453
free_plaintext( PKT_plaintext *pt )
454
136k
{
455
136k
  if (!pt)
456
0
    return;
457
458
136k
  if (pt->buf)
459
134k
    { /* We need to skip some bytes.  */
460
134k
      if (pt->is_partial)
461
9.38k
        {
462
18.6k
          while (iobuf_read( pt->buf, NULL, 1<<30 ) != -1)
463
9.25k
            ;
464
9.38k
        }
465
125k
      else
466
125k
        {
467
190k
          while( pt->len )
468
65.3k
            { /* Skip the packet.  */
469
65.3k
              int n = iobuf_read( pt->buf, NULL, pt->len );
470
65.3k
              if (n == -1)
471
16.7k
                pt->len = 0;
472
48.6k
              else
473
48.6k
                pt->len -= n;
474
65.3k
            }
475
125k
  }
476
134k
    }
477
136k
  xfree (pt);
478
136k
}
479
480
481
/****************
482
 * Free the packet in PKT.
483
 */
484
void
485
free_packet (PACKET *pkt, parse_packet_ctx_t parsectx)
486
3.84M
{
487
3.84M
  if (!pkt || !pkt->pkt.generic)
488
2.34M
    {
489
2.34M
      if (parsectx && parsectx->last_pkt.pkt.generic)
490
531k
        {
491
531k
          if (parsectx->free_last_pkt)
492
4.43k
            {
493
4.43k
              free_packet (&parsectx->last_pkt, NULL);
494
4.43k
              parsectx->free_last_pkt = 0;
495
4.43k
            }
496
531k
          parsectx->last_pkt.pkttype = 0;
497
531k
          parsectx->last_pkt.pkt.generic = NULL;
498
531k
        }
499
2.34M
      return;
500
2.34M
    }
501
502
1.50M
  if (DBG_MEMORY)
503
1.50M
    log_debug ("free_packet() type=%d\n", pkt->pkttype);
504
505
  /* If we have a parser context holding PKT then do not free the
506
   * packet but set a flag that the packet in the parser context is
507
   * now a deep copy.  */
508
1.50M
  if (parsectx && !parsectx->free_last_pkt
509
184k
      && parsectx->last_pkt.pkttype == pkt->pkttype
510
4.43k
      && parsectx->last_pkt.pkt.generic == pkt->pkt.generic)
511
4.43k
    {
512
4.43k
      parsectx->last_pkt = *pkt;
513
4.43k
      parsectx->free_last_pkt = 1;
514
4.43k
      pkt->pkt.generic = NULL;
515
4.43k
      return;
516
4.43k
    }
517
518
1.49M
  switch (pkt->pkttype)
519
1.49M
    {
520
548k
    case PKT_SIGNATURE:
521
548k
      free_seckey_enc (pkt->pkt.signature);
522
548k
      break;
523
35.5k
    case PKT_PUBKEY_ENC:
524
35.5k
      free_pubkey_enc (pkt->pkt.pubkey_enc);
525
35.5k
      break;
526
3.25k
    case PKT_SYMKEY_ENC:
527
3.25k
      free_symkey_enc (pkt->pkt.symkey_enc);
528
3.25k
      break;
529
1.39k
    case PKT_PUBLIC_KEY:
530
2.28k
    case PKT_PUBLIC_SUBKEY:
531
17.9k
    case PKT_SECRET_KEY:
532
84.3k
    case PKT_SECRET_SUBKEY:
533
84.3k
      free_public_key (pkt->pkt.public_key);
534
84.3k
      break;
535
619
    case PKT_COMMENT:
536
619
      free_comment (pkt->pkt.comment);
537
619
      break;
538
4.31k
    case PKT_USER_ID:
539
4.31k
      free_user_id (pkt->pkt.user_id);
540
4.31k
      break;
541
470k
    case PKT_COMPRESSED:
542
470k
      free_compressed (pkt->pkt.compressed);
543
470k
      break;
544
660
    case PKT_ENCRYPTED:
545
4.61k
    case PKT_ENCRYPTED_MDC:
546
7.23k
    case PKT_ENCRYPTED_AEAD:
547
7.23k
      free_encrypted (pkt->pkt.encrypted);
548
7.23k
      break;
549
136k
    case PKT_PLAINTEXT:
550
136k
      free_plaintext (pkt->pkt.plaintext);
551
136k
      break;
552
207k
    default:
553
207k
      xfree (pkt->pkt.generic);
554
207k
      break;
555
1.49M
    }
556
557
1.49M
  pkt->pkt.generic = NULL;
558
1.49M
}
559
560
561
/* Free an entire list of public or symmetric key encrypted data.  */
562
void
563
free_seskey_enc_list (struct seskey_enc_list *sesenc_list)
564
494k
{
565
494k
  while (sesenc_list)
566
0
    {
567
0
      struct seskey_enc_list *tmp = sesenc_list->next;
568
569
0
      if (!sesenc_list->u_sym)
570
0
        release_pubkey_enc_parts (&sesenc_list->u.pub);
571
0
      xfree (sesenc_list);
572
0
      sesenc_list = tmp;
573
0
    }
574
494k
}
575
576
577
/****************
578
 * returns 0 if they match.
579
 */
580
int
581
cmp_public_keys( PKT_public_key *a, PKT_public_key *b )
582
0
{
583
0
    int n, i;
584
585
0
    if( a->timestamp != b->timestamp )
586
0
  return -1;
587
0
    if( a->version < 4 && a->expiredate != b->expiredate )
588
0
  return -1;
589
0
    if( a->pubkey_algo != b->pubkey_algo )
590
0
  return -1;
591
592
0
    n = pubkey_get_npkey( b->pubkey_algo );
593
0
    if( !n ) { /* unknown algorithm, rest is in opaque MPI */
594
0
  if( mpi_cmp( a->pkey[0], b->pkey[0] ) )
595
0
      return -1; /* can't compare due to unknown algorithm */
596
0
    } else {
597
0
  for(i=0; i < n; i++ ) {
598
0
      if( mpi_cmp( a->pkey[i], b->pkey[i] ) )
599
0
    return -1;
600
0
  }
601
0
    }
602
603
0
    return 0;
604
0
}
605
606
607
608
int
609
cmp_signatures( PKT_signature *a, PKT_signature *b )
610
0
{
611
0
    int n, i;
612
613
0
    if( a->keyid[0] != b->keyid[0] )
614
0
  return -1;
615
0
    if( a->keyid[1] != b->keyid[1] )
616
0
  return -1;
617
0
    if( a->pubkey_algo != b->pubkey_algo )
618
0
  return -1;
619
620
0
    n = pubkey_get_nsig( a->pubkey_algo );
621
0
    if( !n )
622
0
  return -1; /* can't compare due to unknown algorithm */
623
0
    for(i=0; i < n; i++ ) {
624
0
  if( mpi_cmp( a->data[i] , b->data[i] ) )
625
0
      return -1;
626
0
    }
627
0
    return 0;
628
0
}
629
630
631
/****************
632
 * Returns: true if the user ids do not match
633
 */
634
int
635
cmp_user_ids( PKT_user_id *a, PKT_user_id *b )
636
0
{
637
0
    int res=1;
638
639
0
    if( a == b )
640
0
        return 0;
641
642
0
    if( a->attrib_data && b->attrib_data )
643
0
      {
644
0
  res = a->attrib_len - b->attrib_len;
645
0
  if( !res )
646
0
    res = memcmp( a->attrib_data, b->attrib_data, a->attrib_len );
647
0
      }
648
0
    else if( !a->attrib_data && !b->attrib_data )
649
0
      {
650
0
  res = a->len - b->len;
651
0
  if( !res )
652
0
    res = memcmp( a->name, b->name, a->len );
653
0
      }
654
655
0
    return res;
656
0
}