1 /* free-packet.c - cleanup stuff for packets
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2005, 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
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.
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.
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/>.
29 #include "../common/iobuf.h"
33 /* This is mpi_copy with a fix for opaque MPIs which store a NULL
34 pointer. This will also be fixed in Libggcrypt 1.7.0. */
36 my_mpi_copy (gcry_mpi_t a)
39 && gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)
40 && !gcry_mpi_get_opaque (a, NULL))
43 return gcry_mpi_copy (a);
48 free_symkey_enc( PKT_symkey_enc *enc )
54 free_pubkey_enc( PKT_pubkey_enc *enc )
57 n = pubkey_get_nenc( enc->pubkey_algo );
59 mpi_release(enc->data[0]);
61 mpi_release( enc->data[i] );
66 free_seckey_enc( PKT_signature *sig )
70 n = pubkey_get_nsig( sig->pubkey_algo );
72 mpi_release(sig->data[0]);
74 mpi_release( sig->data[i] );
82 xfree (sig->pka_info->uri);
83 xfree (sig->pka_info);
85 xfree (sig->signers_uid);
92 release_public_key_parts (PKT_public_key *pk)
97 n = pubkey_get_nskey (pk->pubkey_algo);
99 n = pubkey_get_npkey (pk->pubkey_algo);
101 mpi_release (pk->pkey[0]);
102 for (i=0; i < n; i++ )
104 mpi_release (pk->pkey[i]);
109 xfree (pk->seckey_info);
110 pk->seckey_info = NULL;
117 free_user_id (pk->user_id);
127 xfree (pk->serialno);
133 /* Free an allocated public key structure including all parts.
134 Passing NULL is allowed. */
136 free_public_key (PKT_public_key *pk)
140 release_public_key_parts (pk);
146 static subpktarea_t *
147 cp_subpktarea (subpktarea_t *s )
153 d = xmalloc (sizeof (*d) + s->size - 1 );
156 memcpy (d->data, s->data, s->len);
161 * Return a copy of the preferences
164 copy_prefs (const prefitem_t *prefs)
172 for (n=0; prefs[n].type; n++)
174 new = xmalloc ( sizeof (*new) * (n+1));
175 for (n=0; prefs[n].type; n++) {
176 new[n].type = prefs[n].type;
177 new[n].value = prefs[n].value;
179 new[n].type = PREFTYPE_NONE;
186 /* Copy the public key S to D. If D is NULL allocate a new public key
187 structure. If S has seckret key infos, only the public stuff is
190 copy_public_key (PKT_public_key *d, PKT_public_key *s)
195 d = xmalloc (sizeof *d);
196 memcpy (d, s, sizeof *d);
197 d->seckey_info = NULL;
198 d->user_id = scopy_user_id (s->user_id);
199 d->prefs = copy_prefs (s->prefs);
201 n = pubkey_get_npkey (s->pubkey_algo);
204 d->pkey[i++] = my_mpi_copy (s->pkey[0]);
208 d->pkey[i] = my_mpi_copy (s->pkey[i]);
210 for (; i < PUBKEY_MAX_NSKEY; i++)
213 if (!s->revkey && s->numrevkeys)
217 d->revkey = xmalloc(sizeof(struct revocation_key)*s->numrevkeys);
218 memcpy(d->revkey,s->revkey,sizeof(struct revocation_key)*s->numrevkeys);
228 cp_pka_info (const pka_info_t *s)
230 pka_info_t *d = xmalloc (sizeof *s + strlen (s->email));
233 d->checked = s->checked;
234 d->uri = s->uri? xstrdup (s->uri):NULL;
235 memcpy (d->fpr, s->fpr, sizeof s->fpr);
236 strcpy (d->email, s->email);
242 copy_signature( PKT_signature *d, PKT_signature *s )
247 d = xmalloc(sizeof *d);
248 memcpy( d, s, sizeof *d );
249 n = pubkey_get_nsig( s->pubkey_algo );
251 d->data[0] = my_mpi_copy(s->data[0]);
253 for(i=0; i < n; i++ )
254 d->data[i] = my_mpi_copy( s->data[i] );
256 d->pka_info = s->pka_info? cp_pka_info (s->pka_info) : NULL;
257 d->hashed = cp_subpktarea (s->hashed);
258 d->unhashed = cp_subpktarea (s->unhashed);
260 d->signers_uid = xstrdup (s->signers_uid);
272 * shallow copy of the user ID
275 scopy_user_id (PKT_user_id *s)
285 free_comment( PKT_comment *rem )
291 free_attributes(PKT_user_id *uid)
297 xfree(uid->attrib_data);
300 uid->attrib_data=NULL;
305 free_user_id (PKT_user_id *uid)
310 log_assert (uid->ref > 0);
314 free_attributes(uid);
316 xfree (uid->namehash);
322 free_compressed( PKT_compressed *zd )
329 /* We need to skip some bytes. Because don't have any
330 * information about the length, so we assume this is the last
332 while (iobuf_read( zd->buf, NULL, 1<<30 ) != -1)
339 free_encrypted( PKT_encrypted *ed )
346 /* We need to skip some bytes. */
349 while (iobuf_read( ed->buf, NULL, 1<<30 ) != -1)
356 /* Skip the packet. */
357 int n = iobuf_read( ed->buf, NULL, ed->len );
370 free_plaintext( PKT_plaintext *pt )
376 { /* We need to skip some bytes. */
379 while (iobuf_read( pt->buf, NULL, 1<<30 ) != -1)
385 { /* Skip the packet. */
386 int n = iobuf_read( pt->buf, NULL, pt->len );
398 * Free the packet in pkt.
401 free_packet( PACKET *pkt )
403 if( !pkt || !pkt->pkt.generic )
407 log_debug("free_packet() type=%d\n", pkt->pkttype );
409 switch( pkt->pkttype ) {
411 free_seckey_enc( pkt->pkt.signature );
414 free_pubkey_enc( pkt->pkt.pubkey_enc );
417 free_symkey_enc( pkt->pkt.symkey_enc );
420 case PKT_PUBLIC_SUBKEY:
422 case PKT_SECRET_SUBKEY:
423 free_public_key (pkt->pkt.public_key);
426 free_comment( pkt->pkt.comment );
429 free_user_id( pkt->pkt.user_id );
432 free_compressed( pkt->pkt.compressed);
435 case PKT_ENCRYPTED_MDC:
436 free_encrypted( pkt->pkt.encrypted );
439 free_plaintext( pkt->pkt.plaintext );
442 xfree( pkt->pkt.generic );
445 pkt->pkt.generic = NULL;
449 * returns 0 if they match.
452 cmp_public_keys( PKT_public_key *a, PKT_public_key *b )
456 if( a->timestamp != b->timestamp )
458 if( a->version < 4 && a->expiredate != b->expiredate )
460 if( a->pubkey_algo != b->pubkey_algo )
463 n = pubkey_get_npkey( b->pubkey_algo );
464 if( !n ) { /* unknown algorithm, rest is in opaque MPI */
465 if( mpi_cmp( a->pkey[0], b->pkey[0] ) )
466 return -1; /* can't compare due to unknown algorithm */
468 for(i=0; i < n; i++ ) {
469 if( mpi_cmp( a->pkey[i], b->pkey[i] ) )
480 cmp_signatures( PKT_signature *a, PKT_signature *b )
484 if( a->keyid[0] != b->keyid[0] )
486 if( a->keyid[1] != b->keyid[1] )
488 if( a->pubkey_algo != b->pubkey_algo )
491 n = pubkey_get_nsig( a->pubkey_algo );
493 return -1; /* can't compare due to unknown algorithm */
494 for(i=0; i < n; i++ ) {
495 if( mpi_cmp( a->data[i] , b->data[i] ) )
503 * Returns: true if the user ids do not match
506 cmp_user_ids( PKT_user_id *a, PKT_user_id *b )
513 if( a->attrib_data && b->attrib_data )
515 res = a->attrib_len - b->attrib_len;
517 res = memcmp( a->attrib_data, b->attrib_data, a->attrib_len );
519 else if( !a->attrib_data && !b->attrib_data )
521 res = a->len - b->len;
523 res = memcmp( a->name, b->name, a->len );