Coverage Report

Created: 2026-09-01 06:58

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
11.3k
{
40
11.3k
  if (a
41
1.43k
      && gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)
42
456
      && !gcry_mpi_get_opaque (a, NULL))
43
0
    return NULL;
44
45
11.3k
  return gcry_mpi_copy (a);
46
11.3k
}
47
48
49
void
50
free_symkey_enc( PKT_symkey_enc *enc )
51
20.5k
{
52
20.5k
  if (enc)
53
20.5k
    xfree (enc->seskey);
54
20.5k
  xfree(enc);
55
20.5k
}
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
21.7k
{
63
21.7k
  int n, i;
64
21.7k
  n = pubkey_get_nenc( enc->pubkey_algo );
65
21.7k
  if (!n)
66
21.7k
    mpi_release (enc->data[0]);
67
24.8k
  for (i=0; i < n; i++ )
68
21.7k
    mpi_release (enc->data[i]);
69
21.7k
}
70
71
void
72
free_pubkey_enc (PKT_pubkey_enc *enc)
73
11.0k
{
74
11.0k
  release_pubkey_enc_parts (enc);
75
11.0k
  xfree (enc);
76
11.0k
}
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
10.7k
{
84
10.7k
  int n, i;
85
86
10.7k
  dst->keyid[0] = src->keyid[0];
87
10.7k
  dst->keyid[1] = src->keyid[1];
88
10.7k
  dst->version  = src->version;
89
10.7k
  dst->pubkey_algo = src->pubkey_algo;
90
10.7k
  dst->seskey_algo = src->seskey_algo;
91
10.7k
  dst->throw_keyid = src->throw_keyid;
92
93
10.7k
  if (!(n = pubkey_get_nenc (dst->pubkey_algo)))
94
9.94k
    n = 1;  /* All data is in the first item as an opaque MPI. */
95
22.1k
  for (i=0; i < n; i++)
96
11.3k
    dst->data[i] = my_mpi_copy (src->data[i]);
97
42.4k
  for (; i < PUBKEY_MAX_NENC; i++)
98
31.6k
    dst->data[i] = NULL;
99
10.7k
}
100
101
102
void
103
free_seckey_enc( PKT_signature *sig )
104
64.2k
{
105
64.2k
  int n, i;
106
107
64.2k
  n = pubkey_get_nsig( sig->pubkey_algo );
108
64.2k
  if( !n )
109
64.2k
    mpi_release(sig->data[0]);
110
74.2k
  for(i=0; i < n; i++ )
111
64.2k
    mpi_release( sig->data[i] );
112
113
64.2k
  xfree(sig->revkey);
114
64.2k
  xfree(sig->hashed);
115
64.2k
  xfree(sig->unhashed);
116
117
  /* Do not forget to update copy_signature() too. */
118
64.2k
  xfree(sig->rev_subject_info);
119
64.2k
  xfree (sig->signers_uid);
120
121
64.2k
  xfree(sig);
122
64.2k
}
123
124
125
void
126
release_public_key_parts (PKT_public_key *pk)
127
946k
{
128
946k
  int n, i;
129
130
946k
  if (pk->seckey_info)
131
4.94k
    n = pubkey_get_nskey (pk->pubkey_algo);
132
941k
  else
133
941k
    n = pubkey_get_npkey (pk->pubkey_algo);
134
946k
  if (!n)
135
946k
    mpi_release (pk->pkey[0]);
136
3.42M
  for (i=0; i < n; i++ )
137
2.48M
    {
138
2.48M
      mpi_release (pk->pkey[i]);
139
2.48M
      pk->pkey[i] = NULL;
140
2.48M
    }
141
946k
  if (pk->seckey_info)
142
4.94k
    {
143
4.94k
      xfree (pk->seckey_info);
144
4.94k
      pk->seckey_info = NULL;
145
4.94k
    }
146
946k
  if (pk->prefs)
147
1.74k
    {
148
1.74k
      xfree (pk->prefs);
149
1.74k
      pk->prefs = NULL;
150
1.74k
    }
151
946k
  free_user_id (pk->user_id);
152
946k
  pk->user_id = NULL;
153
946k
  if (pk->revkey)
154
0
    {
155
0
      xfree(pk->revkey);
156
0
      pk->revkey=NULL;
157
0
      pk->numrevkeys=0;
158
0
    }
159
946k
  if (pk->serialno)
160
0
    {
161
0
      xfree (pk->serialno);
162
0
      pk->serialno = NULL;
163
0
    }
164
946k
  if (pk->updateurl)
165
1.04k
    {
166
1.04k
      xfree (pk->updateurl);
167
1.04k
      pk->updateurl = NULL;
168
1.04k
    }
169
946k
  if (pk->revoked.reason_comment)
170
0
    {
171
0
      xfree (pk->revoked.reason_comment);
172
0
      pk->revoked.reason_comment = NULL;
173
0
    }
174
946k
}
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
964k
{
182
964k
  if (pk)
183
946k
    {
184
946k
      release_public_key_parts (pk);
185
946k
      xfree(pk);
186
946k
    }
187
964k
}
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
2.19k
{
210
2.19k
    size_t n;
211
2.19k
    prefitem_t *new;
212
213
2.19k
    if (!prefs)
214
444
        return NULL;
215
216
17.2k
    for (n=0; prefs[n].type; n++)
217
15.5k
        ;
218
1.74k
    new = xmalloc ( sizeof (*new) * (n+1));
219
17.2k
    for (n=0; prefs[n].type; n++) {
220
15.5k
        new[n].type = prefs[n].type;
221
15.5k
        new[n].value = prefs[n].value;
222
15.5k
    }
223
1.74k
    new[n].type = PREFTYPE_NONE;
224
1.74k
    new[n].value = 0;
225
226
1.74k
    return new;
227
2.19k
}
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
383
{
369
383
    xfree(rem);
370
383
}
371
372
void
373
free_attributes(PKT_user_id *uid)
374
35.5k
{
375
35.5k
  if (!uid)
376
0
    return;
377
378
35.5k
  xfree(uid->attribs);
379
35.5k
  xfree(uid->attrib_data);
380
381
35.5k
  uid->attribs=NULL;
382
35.5k
  uid->attrib_data=NULL;
383
35.5k
  uid->attrib_len=0;
384
35.5k
}
385
386
void
387
free_user_id (PKT_user_id *uid)
388
981k
{
389
981k
  if (!uid)
390
946k
    return;
391
392
981k
  log_assert (uid->ref > 0);
393
35.5k
  if (--uid->ref)
394
0
    return;
395
396
35.5k
  free_attributes(uid);
397
35.5k
  xfree (uid->prefs);
398
35.5k
  xfree (uid->namehash);
399
35.5k
  xfree (uid->updateurl);
400
35.5k
  xfree (uid->mbox);
401
35.5k
  xfree (uid);
402
35.5k
}
403
404
void
405
free_compressed( PKT_compressed *zd )
406
49.9k
{
407
49.9k
  if (!zd)
408
0
    return;
409
410
49.9k
  if (zd->buf)
411
202
    {
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
842
      while (iobuf_read( zd->buf, NULL, 1<<30 ) != -1)
416
640
        ;
417
202
    }
418
49.9k
  xfree(zd);
419
49.9k
}
420
421
void
422
free_encrypted( PKT_encrypted *ed )
423
34.5k
{
424
34.5k
  if (!ed)
425
0
    return;
426
427
34.5k
  if (ed->buf)
428
33.7k
    {
429
      /* We need to skip some bytes. */
430
33.7k
      if (ed->is_partial)
431
1.57k
        {
432
2.00k
          while (iobuf_read( ed->buf, NULL, 1<<30 ) != -1)
433
427
            ;
434
1.57k
  }
435
32.1k
      else
436
32.1k
        {
437
35.6k
          while (ed->len)
438
3.48k
            {
439
              /* Skip the packet. */
440
3.48k
              int n = iobuf_read( ed->buf, NULL, ed->len );
441
3.48k
              if (n == -1)
442
349
                ed->len = 0;
443
3.13k
              else
444
3.13k
                ed->len -= n;
445
3.48k
            }
446
32.1k
  }
447
33.7k
    }
448
34.5k
  xfree (ed);
449
34.5k
}
450
451
452
void
453
free_plaintext( PKT_plaintext *pt )
454
14.0k
{
455
14.0k
  if (!pt)
456
0
    return;
457
458
14.0k
  if (pt->buf)
459
14.0k
    { /* We need to skip some bytes.  */
460
14.0k
      if (pt->is_partial)
461
3.91k
        {
462
7.65k
          while (iobuf_read( pt->buf, NULL, 1<<30 ) != -1)
463
3.74k
            ;
464
3.91k
        }
465
10.1k
      else
466
10.1k
        {
467
11.8k
          while( pt->len )
468
1.75k
            { /* Skip the packet.  */
469
1.75k
              int n = iobuf_read( pt->buf, NULL, pt->len );
470
1.75k
              if (n == -1)
471
170
                pt->len = 0;
472
1.58k
              else
473
1.58k
                pt->len -= n;
474
1.75k
            }
475
10.1k
  }
476
14.0k
    }
477
14.0k
  xfree (pt);
478
14.0k
}
479
480
481
/****************
482
 * Free the packet in PKT.
483
 */
484
void
485
free_packet (PACKET *pkt, parse_packet_ctx_t parsectx)
486
723k
{
487
723k
  if (!pkt || !pkt->pkt.generic)
488
419k
    {
489
419k
      if (parsectx && parsectx->last_pkt.pkt.generic)
490
112k
        {
491
112k
          if (parsectx->free_last_pkt)
492
12.7k
            {
493
12.7k
              free_packet (&parsectx->last_pkt, NULL);
494
12.7k
              parsectx->free_last_pkt = 0;
495
12.7k
            }
496
112k
          parsectx->last_pkt.pkttype = 0;
497
112k
          parsectx->last_pkt.pkt.generic = NULL;
498
112k
        }
499
419k
      return;
500
419k
    }
501
502
304k
  if (DBG_MEMORY)
503
304k
    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
304k
  if (parsectx && !parsectx->free_last_pkt
509
38.3k
      && parsectx->last_pkt.pkttype == pkt->pkttype
510
12.7k
      && parsectx->last_pkt.pkt.generic == pkt->pkt.generic)
511
12.7k
    {
512
12.7k
      parsectx->last_pkt = *pkt;
513
12.7k
      parsectx->free_last_pkt = 1;
514
12.7k
      pkt->pkt.generic = NULL;
515
12.7k
      return;
516
12.7k
    }
517
518
291k
  switch (pkt->pkttype)
519
291k
    {
520
64.0k
    case PKT_SIGNATURE:
521
64.0k
      free_seckey_enc (pkt->pkt.signature);
522
64.0k
      break;
523
11.0k
    case PKT_PUBKEY_ENC:
524
11.0k
      free_pubkey_enc (pkt->pkt.pubkey_enc);
525
11.0k
      break;
526
20.5k
    case PKT_SYMKEY_ENC:
527
20.5k
      free_symkey_enc (pkt->pkt.symkey_enc);
528
20.5k
      break;
529
23.9k
    case PKT_PUBLIC_KEY:
530
29.3k
    case PKT_PUBLIC_SUBKEY:
531
30.7k
    case PKT_SECRET_KEY:
532
35.4k
    case PKT_SECRET_SUBKEY:
533
35.4k
      free_public_key (pkt->pkt.public_key);
534
35.4k
      break;
535
383
    case PKT_COMMENT:
536
383
      free_comment (pkt->pkt.comment);
537
383
      break;
538
35.5k
    case PKT_USER_ID:
539
35.5k
      free_user_id (pkt->pkt.user_id);
540
35.5k
      break;
541
49.9k
    case PKT_COMPRESSED:
542
49.9k
      free_compressed (pkt->pkt.compressed);
543
49.9k
      break;
544
9.29k
    case PKT_ENCRYPTED:
545
11.1k
    case PKT_ENCRYPTED_MDC:
546
34.5k
    case PKT_ENCRYPTED_AEAD:
547
34.5k
      free_encrypted (pkt->pkt.encrypted);
548
34.5k
      break;
549
14.0k
    case PKT_PLAINTEXT:
550
14.0k
      free_plaintext (pkt->pkt.plaintext);
551
14.0k
      break;
552
26.0k
    default:
553
26.0k
      xfree (pkt->pkt.generic);
554
26.0k
      break;
555
291k
    }
556
557
291k
  pkt->pkt.generic = NULL;
558
291k
}
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
85.2k
{
565
96.0k
  while (sesenc_list)
566
10.7k
    {
567
10.7k
      struct seskey_enc_list *tmp = sesenc_list->next;
568
569
10.7k
      if (!sesenc_list->u_sym)
570
10.7k
        release_pubkey_enc_parts (&sesenc_list->u.pub);
571
10.7k
      xfree (sesenc_list);
572
10.7k
      sesenc_list = tmp;
573
10.7k
    }
574
85.2k
}
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
10.1k
{
637
10.1k
    int res=1;
638
639
10.1k
    if( a == b )
640
0
        return 0;
641
642
10.1k
    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
10.1k
    else if( !a->attrib_data && !b->attrib_data )
649
10.1k
      {
650
10.1k
  res = a->len - b->len;
651
10.1k
  if( !res )
652
5.58k
    res = memcmp( a->name, b->name, a->len );
653
10.1k
      }
654
655
10.1k
    return res;
656
10.1k
}