1 /* free-packet.c - cleanup stuff for packets
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2005 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 <http://www.gnu.org/licenses/>.
29 #include "../common/iobuf.h"
36 free_symkey_enc( PKT_symkey_enc *enc )
42 free_pubkey_enc( PKT_pubkey_enc *enc )
45 n = pubkey_get_nenc( enc->pubkey_algo );
47 mpi_release(enc->data[0]);
49 mpi_release( enc->data[i] );
54 free_seckey_enc( PKT_signature *sig )
58 n = pubkey_get_nsig( sig->pubkey_algo );
60 mpi_release(sig->data[0]);
62 mpi_release( sig->data[i] );
70 xfree (sig->pka_info->uri);
71 xfree (sig->pka_info);
79 release_public_key_parts( PKT_public_key *pk )
82 n = pubkey_get_npkey( pk->pubkey_algo );
84 mpi_release(pk->pkey[0]);
85 for(i=0; i < n; i++ ) {
86 mpi_release( pk->pkey[i] );
94 free_user_id (pk->user_id);
106 free_public_key( PKT_public_key *pk )
108 release_public_key_parts( pk );
113 static subpktarea_t *
114 cp_subpktarea (subpktarea_t *s )
120 d = xmalloc (sizeof (*d) + s->size - 1 );
123 memcpy (d->data, s->data, s->len);
128 * Return a copy of the preferences
131 copy_prefs (const prefitem_t *prefs)
139 for (n=0; prefs[n].type; n++)
141 new = xmalloc ( sizeof (*new) * (n+1));
142 for (n=0; prefs[n].type; n++) {
143 new[n].type = prefs[n].type;
144 new[n].value = prefs[n].value;
146 new[n].type = PREFTYPE_NONE;
154 copy_public_key ( PKT_public_key *d, PKT_public_key *s)
159 d = xmalloc(sizeof *d);
160 memcpy( d, s, sizeof *d );
161 d->user_id = scopy_user_id (s->user_id);
162 d->prefs = copy_prefs (s->prefs);
163 n = pubkey_get_npkey( s->pubkey_algo );
165 d->pkey[0] = mpi_copy(s->pkey[0]);
167 for(i=0; i < n; i++ )
168 d->pkey[i] = mpi_copy( s->pkey[i] );
170 if( !s->revkey && s->numrevkeys )
172 if( s->numrevkeys ) {
173 d->revkey = xmalloc(sizeof(struct revocation_key)*s->numrevkeys);
174 memcpy(d->revkey,s->revkey,sizeof(struct revocation_key)*s->numrevkeys);
182 * Replace all common parts of a sk by the one from the public key.
183 * This is a hack and a better solution will be to just store the real secret
184 * parts somewhere and don't duplicate all the other stuff.
187 copy_public_parts_to_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
189 sk->expiredate = pk->expiredate;
190 sk->pubkey_algo = pk->pubkey_algo;
191 sk->pubkey_usage= pk->pubkey_usage;
192 sk->req_usage = pk->req_usage;
193 sk->req_algo = pk->req_algo;
194 sk->has_expired = pk->has_expired;
195 sk->is_revoked = pk->is_revoked;
196 sk->is_valid = pk->is_valid;
197 sk->main_keyid[0]= pk->main_keyid[0];
198 sk->main_keyid[1]= pk->main_keyid[1];
199 sk->keyid[0] = pk->keyid[0];
200 sk->keyid[1] = pk->keyid[1];
205 cp_pka_info (const pka_info_t *s)
207 pka_info_t *d = xmalloc (sizeof *s + strlen (s->email));
210 d->checked = s->checked;
211 d->uri = s->uri? xstrdup (s->uri):NULL;
212 memcpy (d->fpr, s->fpr, sizeof s->fpr);
213 strcpy (d->email, s->email);
219 copy_signature( PKT_signature *d, PKT_signature *s )
224 d = xmalloc(sizeof *d);
225 memcpy( d, s, sizeof *d );
226 n = pubkey_get_nsig( s->pubkey_algo );
228 d->data[0] = mpi_copy(s->data[0]);
230 for(i=0; i < n; i++ )
231 d->data[i] = mpi_copy( s->data[i] );
233 d->pka_info = s->pka_info? cp_pka_info (s->pka_info) : NULL;
234 d->hashed = cp_subpktarea (s->hashed);
235 d->unhashed = cp_subpktarea (s->unhashed);
247 * shallow copy of the user ID
250 scopy_user_id (PKT_user_id *s)
260 release_secret_key_parts( PKT_secret_key *sk )
264 n = pubkey_get_nskey( sk->pubkey_algo );
266 mpi_release(sk->skey[0]);
267 for(i=0; i < n; i++ ) {
268 mpi_release( sk->skey[i] );
274 free_secret_key( PKT_secret_key *sk )
276 release_secret_key_parts( sk );
281 copy_secret_key( PKT_secret_key *d, PKT_secret_key *s )
286 d = xmalloc_secure(sizeof *d);
288 release_secret_key_parts (d);
289 memcpy( d, s, sizeof *d );
290 n = pubkey_get_nskey( s->pubkey_algo );
292 d->skey[0] = mpi_copy(s->skey[0]);
294 for(i=0; i < n; i++ )
295 d->skey[i] = mpi_copy( s->skey[i] );
302 free_comment( PKT_comment *rem )
308 free_attributes(PKT_user_id *uid)
311 xfree(uid->attrib_data);
314 uid->attrib_data=NULL;
319 free_user_id (PKT_user_id *uid)
321 assert (uid->ref > 0);
325 free_attributes(uid);
327 xfree (uid->namehash);
332 free_compressed( PKT_compressed *zd )
334 if( zd->buf ) { /* have to skip some bytes */
335 /* don't have any information about the length, so
336 * we assume this is the last packet */
337 while( iobuf_read( zd->buf, NULL, 1<<30 ) != -1 )
344 free_encrypted( PKT_encrypted *ed )
346 if( ed->buf ) { /* have to skip some bytes */
347 if( ed->is_partial ) {
348 while( iobuf_read( ed->buf, NULL, 1<<30 ) != -1 )
352 while( ed->len ) { /* skip the packet */
353 int n = iobuf_read( ed->buf, NULL, ed->len );
366 free_plaintext( PKT_plaintext *pt )
368 if( pt->buf ) { /* have to skip some bytes */
369 if( pt->is_partial ) {
370 while( iobuf_read( pt->buf, NULL, 1<<30 ) != -1 )
374 while( pt->len ) { /* skip the packet */
375 int n = iobuf_read( pt->buf, NULL, pt->len );
387 * Free the packet in pkt.
390 free_packet( PACKET *pkt )
392 if( !pkt || !pkt->pkt.generic )
396 log_debug("free_packet() type=%d\n", pkt->pkttype );
398 switch( pkt->pkttype ) {
400 free_seckey_enc( pkt->pkt.signature );
403 free_pubkey_enc( pkt->pkt.pubkey_enc );
406 free_symkey_enc( pkt->pkt.symkey_enc );
409 case PKT_PUBLIC_SUBKEY:
410 free_public_key( pkt->pkt.public_key );
413 case PKT_SECRET_SUBKEY:
414 free_secret_key( pkt->pkt.secret_key );
417 free_comment( pkt->pkt.comment );
420 free_user_id( pkt->pkt.user_id );
423 free_compressed( pkt->pkt.compressed);
426 case PKT_ENCRYPTED_MDC:
427 free_encrypted( pkt->pkt.encrypted );
430 free_plaintext( pkt->pkt.plaintext );
433 xfree( pkt->pkt.generic );
436 pkt->pkt.generic = NULL;
440 * returns 0 if they match.
443 cmp_public_keys( PKT_public_key *a, PKT_public_key *b )
447 if( a->timestamp != b->timestamp )
449 if( a->version < 4 && a->expiredate != b->expiredate )
451 if( a->pubkey_algo != b->pubkey_algo )
454 n = pubkey_get_npkey( b->pubkey_algo );
455 if( !n ) { /* unknown algorithm, rest is in opaque MPI */
456 if( mpi_cmp( a->pkey[0], b->pkey[0] ) )
457 return -1; /* can't compare due to unknown algorithm */
459 for(i=0; i < n; i++ ) {
460 if( mpi_cmp( a->pkey[i], b->pkey[i] ) )
469 * Returns 0 if they match.
470 * We only compare the public parts.
473 cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b )
477 if( a->timestamp != b->timestamp )
479 if( a->version < 4 && a->expiredate != b->expiredate )
481 if( a->pubkey_algo != b->pubkey_algo )
484 n = pubkey_get_npkey( b->pubkey_algo );
485 if( !n ) { /* unknown algorithm, rest is in opaque MPI */
486 if( mpi_cmp( a->skey[0], b->skey[0] ) )
489 for(i=0; i < n; i++ ) {
490 if( mpi_cmp( a->skey[i], b->skey[i] ) )
499 * Returns 0 if they match.
502 cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
506 if( pk->timestamp != sk->timestamp )
508 if( pk->version < 4 && pk->expiredate != sk->expiredate )
510 if( pk->pubkey_algo != sk->pubkey_algo )
513 n = pubkey_get_npkey( pk->pubkey_algo );
515 return -1; /* can't compare due to unknown algorithm */
516 for(i=0; i < n; i++ ) {
517 if( mpi_cmp( pk->pkey[i] , sk->skey[i] ) )
526 cmp_signatures( PKT_signature *a, PKT_signature *b )
530 if( a->keyid[0] != b->keyid[0] )
532 if( a->keyid[1] != b->keyid[1] )
534 if( a->pubkey_algo != b->pubkey_algo )
537 n = pubkey_get_nsig( a->pubkey_algo );
539 return -1; /* can't compare due to unknown algorithm */
540 for(i=0; i < n; i++ ) {
541 if( mpi_cmp( a->data[i] , b->data[i] ) )
549 * Returns: true if the user ids do not match
552 cmp_user_ids( PKT_user_id *a, PKT_user_id *b )
559 if( a->attrib_data && b->attrib_data )
561 res = a->attrib_len - b->attrib_len;
563 res = memcmp( a->attrib_data, b->attrib_data, a->attrib_len );
565 else if( !a->attrib_data && !b->attrib_data )
567 res = a->len - b->len;
569 res = memcmp( a->name, b->name, a->len );