1 /* free-packet.c - cleanup stuff for packets
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3  *               2005  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 <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 
27 #include "packet.h"
28 #include "iobuf.h"
29 #include "mpi.h"
30 #include "util.h"
31 #include "cipher.h"
32 #include "memory.h"
33 #include "options.h"
34 
35 void
free_symkey_enc(PKT_symkey_enc * enc)36 free_symkey_enc( PKT_symkey_enc *enc )
37 {
38     xfree(enc);
39 }
40 
41 void
free_pubkey_enc(PKT_pubkey_enc * enc)42 free_pubkey_enc( PKT_pubkey_enc *enc )
43 {
44     int n, i;
45     n = pubkey_get_nenc( enc->pubkey_algo );
46     if( !n )
47 	mpi_free(enc->data[0]);
48     for(i=0; i < n; i++ )
49 	mpi_free( enc->data[i] );
50     xfree(enc);
51 }
52 
53 void
free_seckey_enc(PKT_signature * sig)54 free_seckey_enc( PKT_signature *sig )
55 {
56   int n, i;
57 
58   n = pubkey_get_nsig( sig->pubkey_algo );
59   if( !n )
60     mpi_free(sig->data[0]);
61   for(i=0; i < n; i++ )
62     mpi_free( sig->data[i] );
63 
64   xfree(sig->revkey);
65   xfree(sig->hashed);
66   xfree(sig->unhashed);
67 
68   if (sig->pka_info)
69     {
70       xfree (sig->pka_info->uri);
71       xfree (sig->pka_info);
72     }
73 
74   xfree(sig);
75 }
76 
77 
78 void
release_public_key_parts(PKT_public_key * pk)79 release_public_key_parts( PKT_public_key *pk )
80 {
81     int n, i;
82     n = pubkey_get_npkey( pk->pubkey_algo );
83     if( !n )
84 	mpi_free(pk->pkey[0]);
85     for(i=0; i < n; i++ ) {
86 	mpi_free( pk->pkey[i] );
87 	pk->pkey[i] = NULL;
88     }
89     if (pk->prefs) {
90         xfree (pk->prefs);
91         pk->prefs = NULL;
92     }
93     if (pk->user_id) {
94         free_user_id (pk->user_id);
95         pk->user_id = NULL;
96     }
97     if (pk->revkey) {
98         xfree(pk->revkey);
99 	pk->revkey=NULL;
100 	pk->numrevkeys=0;
101     }
102 }
103 
104 
105 void
free_public_key(PKT_public_key * pk)106 free_public_key( PKT_public_key *pk )
107 {
108     release_public_key_parts( pk );
109     xfree(pk);
110 }
111 
112 
113 static subpktarea_t *
cp_subpktarea(subpktarea_t * s)114 cp_subpktarea (subpktarea_t *s )
115 {
116     subpktarea_t *d;
117 
118     if( !s )
119 	return NULL;
120     d = xmalloc (sizeof (*d) + s->size - 1 );
121     d->size = s->size;
122     d->len = s->len;
123     memcpy (d->data, s->data, s->len);
124     return d;
125 }
126 
127 /*
128  * Return a copy of the preferences
129  */
130 prefitem_t *
copy_prefs(const prefitem_t * prefs)131 copy_prefs (const prefitem_t *prefs)
132 {
133     size_t n;
134     prefitem_t *new;
135 
136     if (!prefs)
137         return NULL;
138 
139     for (n=0; prefs[n].type; n++)
140         ;
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;
145     }
146     new[n].type = PREFTYPE_NONE;
147     new[n].value = 0;
148 
149     return new;
150 }
151 
152 
153 PKT_public_key *
copy_public_key(PKT_public_key * d,PKT_public_key * s)154 copy_public_key ( PKT_public_key *d, PKT_public_key *s)
155 {
156     int n, i;
157 
158     if( !d )
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 );
164     if( !n )
165 	d->pkey[0] = mpi_copy(s->pkey[0]);
166     else {
167 	for(i=0; i < n; i++ )
168 	    d->pkey[i] = mpi_copy( s->pkey[i] );
169     }
170     if( !s->revkey && s->numrevkeys )
171         BUG();
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);
175     }
176     else
177         d->revkey = NULL;
178     return d;
179 }
180 
181 /****************
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.
185  */
186 void
copy_public_parts_to_secret_key(PKT_public_key * pk,PKT_secret_key * sk)187 copy_public_parts_to_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
188 {
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];
201 }
202 
203 
204 static pka_info_t *
cp_pka_info(const pka_info_t * s)205 cp_pka_info (const pka_info_t *s)
206 {
207   pka_info_t *d = xmalloc (sizeof *s + strlen (s->email));
208 
209   d->valid = s->valid;
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);
214   return d;
215 }
216 
217 
218 PKT_signature *
copy_signature(PKT_signature * d,PKT_signature * s)219 copy_signature( PKT_signature *d, PKT_signature *s )
220 {
221     int n, i;
222 
223     if( !d )
224 	d = xmalloc(sizeof *d);
225     memcpy( d, s, sizeof *d );
226     n = pubkey_get_nsig( s->pubkey_algo );
227     if( !n )
228 	d->data[0] = mpi_copy(s->data[0]);
229     else {
230 	for(i=0; i < n; i++ )
231 	    d->data[i] = mpi_copy( s->data[i] );
232     }
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);
236     if(s->numrevkeys)
237       {
238 	d->revkey=NULL;
239 	d->numrevkeys=0;
240 	parse_revkeys(d);
241       }
242     return d;
243 }
244 
245 
246 /*
247  * shallow copy of the user ID
248  */
249 PKT_user_id *
scopy_user_id(PKT_user_id * s)250 scopy_user_id (PKT_user_id *s)
251 {
252     if (s)
253         s->ref++;
254     return s;
255 }
256 
257 
258 
259 void
release_secret_key_parts(PKT_secret_key * sk)260 release_secret_key_parts( PKT_secret_key *sk )
261 {
262     int n, i;
263 
264     n = pubkey_get_nskey( sk->pubkey_algo );
265     if( !n )
266 	mpi_free(sk->skey[0]);
267     for(i=0; i < n; i++ ) {
268 	mpi_free( sk->skey[i] );
269 	sk->skey[i] = NULL;
270     }
271 }
272 
273 void
free_secret_key(PKT_secret_key * sk)274 free_secret_key( PKT_secret_key *sk )
275 {
276     release_secret_key_parts( sk );
277     xfree(sk);
278 }
279 
280 PKT_secret_key *
copy_secret_key(PKT_secret_key * d,PKT_secret_key * s)281 copy_secret_key( PKT_secret_key *d, PKT_secret_key *s )
282 {
283     int n, i;
284 
285     if( !d )
286 	d = xmalloc_secure(sizeof *d);
287     else
288         release_secret_key_parts (d);
289     memcpy( d, s, sizeof *d );
290     n = pubkey_get_nskey( s->pubkey_algo );
291     if( !n )
292   	d->skey[0] = mpi_copy(s->skey[0]);
293     else {
294 	for(i=0; i < n; i++ )
295   	    d->skey[i] = mpi_copy( s->skey[i] );
296     }
297 
298     return d;
299 }
300 
301 void
free_comment(PKT_comment * rem)302 free_comment( PKT_comment *rem )
303 {
304     xfree(rem);
305 }
306 
307 void
free_attributes(PKT_user_id * uid)308 free_attributes(PKT_user_id *uid)
309 {
310   xfree(uid->attribs);
311   xfree(uid->attrib_data);
312 
313   uid->attribs=NULL;
314   uid->attrib_data=NULL;
315   uid->attrib_len=0;
316 }
317 
318 void
free_user_id(PKT_user_id * uid)319 free_user_id (PKT_user_id *uid)
320 {
321     assert (uid->ref > 0);
322     if (--uid->ref)
323         return;
324 
325     free_attributes(uid);
326     xfree (uid->prefs);
327     xfree (uid->namehash);
328     xfree (uid);
329 }
330 
331 void
free_compressed(PKT_compressed * zd)332 free_compressed( PKT_compressed *zd )
333 {
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 )
338 	    ;
339     }
340     xfree(zd);
341 }
342 
343 void
free_encrypted(PKT_encrypted * ed)344 free_encrypted( PKT_encrypted *ed )
345 {
346     if( ed->buf ) { /* have to skip some bytes */
347 	if( ed->is_partial ) {
348 	    while( iobuf_read( ed->buf, NULL, 1<<30 ) != -1 )
349 		;
350 	}
351 	else {
352 	   while( ed->len ) { /* skip the packet */
353 	       int n = iobuf_read( ed->buf, NULL, ed->len );
354 	       if( n == -1 )
355 		   ed->len = 0;
356 	       else
357 		   ed->len -= n;
358 	   }
359 	}
360     }
361     xfree(ed);
362 }
363 
364 
365 void
free_plaintext(PKT_plaintext * pt)366 free_plaintext( PKT_plaintext *pt )
367 {
368     if( pt->buf ) { /* have to skip some bytes */
369 	if( pt->is_partial ) {
370 	    while( iobuf_read( pt->buf, NULL, 1<<30 ) != -1 )
371 		;
372 	}
373 	else {
374 	   while( pt->len ) { /* skip the packet */
375 	       int n = iobuf_read( pt->buf, NULL, pt->len );
376 	       if( n == -1 )
377 		   pt->len = 0;
378 	       else
379 		   pt->len -= n;
380 	   }
381 	}
382     }
383     xfree(pt);
384 }
385 
386 /****************
387  * Free the packet in pkt.
388  */
389 void
free_packet(PACKET * pkt)390 free_packet( PACKET *pkt )
391 {
392     if( !pkt || !pkt->pkt.generic )
393 	return;
394 
395     if( DBG_MEMORY )
396 	log_debug("free_packet() type=%d\n", pkt->pkttype );
397 
398     switch( pkt->pkttype ) {
399       case PKT_SIGNATURE:
400 	free_seckey_enc( pkt->pkt.signature );
401 	break;
402       case PKT_PUBKEY_ENC:
403 	free_pubkey_enc( pkt->pkt.pubkey_enc );
404 	break;
405       case PKT_SYMKEY_ENC:
406 	free_symkey_enc( pkt->pkt.symkey_enc );
407 	break;
408       case PKT_PUBLIC_KEY:
409       case PKT_PUBLIC_SUBKEY:
410 	free_public_key( pkt->pkt.public_key );
411 	break;
412       case PKT_SECRET_KEY:
413       case PKT_SECRET_SUBKEY:
414 	free_secret_key( pkt->pkt.secret_key );
415 	break;
416       case PKT_COMMENT:
417 	free_comment( pkt->pkt.comment );
418 	break;
419       case PKT_USER_ID:
420 	free_user_id( pkt->pkt.user_id );
421 	break;
422       case PKT_COMPRESSED:
423 	free_compressed( pkt->pkt.compressed);
424 	break;
425       case PKT_ENCRYPTED:
426       case PKT_ENCRYPTED_MDC:
427 	free_encrypted( pkt->pkt.encrypted );
428 	break;
429       case PKT_PLAINTEXT:
430 	free_plaintext( pkt->pkt.plaintext );
431 	break;
432       default:
433 	xfree( pkt->pkt.generic );
434 	break;
435     }
436     pkt->pkt.generic = NULL;
437 }
438 
439 /****************
440  * returns 0 if they match.
441  */
442 int
cmp_public_keys(PKT_public_key * a,PKT_public_key * b)443 cmp_public_keys( PKT_public_key *a, PKT_public_key *b )
444 {
445     int n, i;
446 
447     if( a->timestamp != b->timestamp )
448 	return -1;
449     if( a->version < 4 && a->expiredate != b->expiredate )
450 	return -1;
451     if( a->pubkey_algo != b->pubkey_algo )
452 	return -1;
453 
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;
458     } else {
459 	for(i=0; i < n; i++ ) {
460 	    if( mpi_cmp( a->pkey[i], b->pkey[i] ) )
461 		return -1;
462 	}
463     }
464 
465     return 0;
466 }
467 
468 /****************
469  * Returns 0 if they match.
470  * We only compare the public parts.
471  */
472 int
cmp_secret_keys(PKT_secret_key * a,PKT_secret_key * b)473 cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b )
474 {
475     int n, i;
476 
477     if( a->timestamp != b->timestamp )
478 	return -1;
479     if( a->version < 4 && a->expiredate != b->expiredate )
480 	return -1;
481     if( a->pubkey_algo != b->pubkey_algo )
482 	return -1;
483 
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] ) )
487 	    return -1;
488     } else {
489 	for(i=0; i < n; i++ ) {
490 	    if( mpi_cmp( a->skey[i], b->skey[i] ) )
491 		return -1;
492 	}
493     }
494 
495     return 0;
496 }
497 
498 /****************
499  * Returns 0 if they match.
500  */
501 int
cmp_public_secret_key(PKT_public_key * pk,PKT_secret_key * sk)502 cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
503 {
504     int n, i;
505 
506     if( pk->timestamp != sk->timestamp )
507 	return -1;
508     if( pk->version < 4 && pk->expiredate != sk->expiredate )
509 	return -1;
510     if( pk->pubkey_algo != sk->pubkey_algo )
511 	return -1;
512 
513     n = pubkey_get_npkey( pk->pubkey_algo );
514     if( !n )
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] ) )
518 	    return -1;
519     }
520     return 0;
521 }
522 
523 
524 
525 int
cmp_signatures(PKT_signature * a,PKT_signature * b)526 cmp_signatures( PKT_signature *a, PKT_signature *b )
527 {
528     int n, i;
529 
530     if( a->keyid[0] != b->keyid[0] )
531 	return -1;
532     if( a->keyid[1] != b->keyid[1] )
533 	return -1;
534     if( a->pubkey_algo != b->pubkey_algo )
535 	return -1;
536 
537     n = pubkey_get_nsig( a->pubkey_algo );
538     if( !n )
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] ) )
542 	    return -1;
543     }
544     return 0;
545 }
546 
547 
548 /****************
549  * Returns: true if the user ids do not match
550  */
551 int
cmp_user_ids(PKT_user_id * a,PKT_user_id * b)552 cmp_user_ids( PKT_user_id *a, PKT_user_id *b )
553 {
554     int res=1;
555 
556     if( a == b )
557         return 0;
558 
559     if( a->attrib_data && b->attrib_data )
560       {
561 	res = a->attrib_len - b->attrib_len;
562 	if( !res )
563 	  res = memcmp( a->attrib_data, b->attrib_data, a->attrib_len );
564       }
565     else if( !a->attrib_data && !b->attrib_data )
566       {
567 	res = a->len - b->len;
568 	if( !res )
569 	  res = memcmp( a->name, b->name, a->len );
570       }
571 
572     return res;
573 }
574