Updated the german translation
[gnupg.git] / g10 / free-packet.c
blob85f23ce3faeee48f1fa0469d5f3a05f3757f9476
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/>.
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "gpg.h"
28 #include "packet.h"
29 #include "../common/iobuf.h"
30 #include "util.h"
31 #include "cipher.h"
32 #include "options.h"
35 void
36 free_symkey_enc( PKT_symkey_enc *enc )
38 xfree(enc);
41 void
42 free_pubkey_enc( PKT_pubkey_enc *enc )
44 int n, i;
45 n = pubkey_get_nenc( enc->pubkey_algo );
46 if( !n )
47 mpi_release(enc->data[0]);
48 for(i=0; i < n; i++ )
49 mpi_release( enc->data[i] );
50 xfree(enc);
53 void
54 free_seckey_enc( PKT_signature *sig )
56 int n, i;
58 n = pubkey_get_nsig( sig->pubkey_algo );
59 if( !n )
60 mpi_release(sig->data[0]);
61 for(i=0; i < n; i++ )
62 mpi_release( sig->data[i] );
64 xfree(sig->revkey);
65 xfree(sig->hashed);
66 xfree(sig->unhashed);
68 if (sig->pka_info)
70 xfree (sig->pka_info->uri);
71 xfree (sig->pka_info);
74 xfree(sig);
78 void
79 release_public_key_parts( PKT_public_key *pk )
81 int n, i;
82 n = pubkey_get_npkey( pk->pubkey_algo );
83 if( !n )
84 mpi_release(pk->pkey[0]);
85 for(i=0; i < n; i++ ) {
86 mpi_release( pk->pkey[i] );
87 pk->pkey[i] = NULL;
89 if (pk->prefs) {
90 xfree (pk->prefs);
91 pk->prefs = NULL;
93 if (pk->user_id) {
94 free_user_id (pk->user_id);
95 pk->user_id = NULL;
97 if (pk->revkey) {
98 xfree(pk->revkey);
99 pk->revkey=NULL;
100 pk->numrevkeys=0;
105 void
106 free_public_key( PKT_public_key *pk )
108 release_public_key_parts( pk );
109 xfree(pk);
113 static subpktarea_t *
114 cp_subpktarea (subpktarea_t *s )
116 subpktarea_t *d;
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;
128 * Return a copy of the preferences
130 prefitem_t *
131 copy_prefs (const prefitem_t *prefs)
133 size_t n;
134 prefitem_t *new;
136 if (!prefs)
137 return NULL;
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;
147 new[n].value = 0;
149 return new;
153 PKT_public_key *
154 copy_public_key ( PKT_public_key *d, PKT_public_key *s)
156 int n, i;
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] );
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);
176 else
177 d->revkey = NULL;
178 return d;
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.
186 void
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];
204 static pka_info_t *
205 cp_pka_info (const pka_info_t *s)
207 pka_info_t *d = xmalloc (sizeof *s + strlen (s->email));
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;
218 PKT_signature *
219 copy_signature( PKT_signature *d, PKT_signature *s )
221 int n, i;
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] );
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)
238 d->revkey=NULL;
239 d->numrevkeys=0;
240 parse_revkeys(d);
242 return d;
247 * shallow copy of the user ID
249 PKT_user_id *
250 scopy_user_id (PKT_user_id *s)
252 if (s)
253 s->ref++;
254 return s;
259 void
260 release_secret_key_parts( PKT_secret_key *sk )
262 int n, i;
264 n = pubkey_get_nskey( sk->pubkey_algo );
265 if( !n )
266 mpi_release(sk->skey[0]);
267 for(i=0; i < n; i++ ) {
268 mpi_release( sk->skey[i] );
269 sk->skey[i] = NULL;
273 void
274 free_secret_key( PKT_secret_key *sk )
276 release_secret_key_parts( sk );
277 xfree(sk);
280 PKT_secret_key *
281 copy_secret_key( PKT_secret_key *d, PKT_secret_key *s )
283 int n, i;
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] );
298 return d;
301 void
302 free_comment( PKT_comment *rem )
304 xfree(rem);
307 void
308 free_attributes(PKT_user_id *uid)
310 xfree(uid->attribs);
311 xfree(uid->attrib_data);
313 uid->attribs=NULL;
314 uid->attrib_data=NULL;
315 uid->attrib_len=0;
318 void
319 free_user_id (PKT_user_id *uid)
321 assert (uid->ref > 0);
322 if (--uid->ref)
323 return;
325 free_attributes(uid);
326 xfree (uid->prefs);
327 xfree (uid->namehash);
328 xfree (uid);
331 void
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 )
340 xfree(zd);
343 void
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 )
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;
361 xfree(ed);
365 void
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 )
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;
383 xfree(pt);
386 /****************
387 * Free the packet in pkt.
389 void
390 free_packet( PACKET *pkt )
392 if( !pkt || !pkt->pkt.generic )
393 return;
395 if( DBG_MEMORY )
396 log_debug("free_packet() type=%d\n", pkt->pkttype );
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;
436 pkt->pkt.generic = NULL;
439 /****************
440 * returns 0 if they match.
443 cmp_public_keys( PKT_public_key *a, PKT_public_key *b )
445 int n, i;
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;
454 n = pubkey_get_npkey( b->pubkey_algo );
455 if( !n )
456 return -1; /* can't compare due to unknown algorithm */
457 for(i=0; i < n; i++ ) {
458 if( mpi_cmp( a->pkey[i], b->pkey[i] ) )
459 return -1;
462 return 0;
465 /****************
466 * Returns 0 if they match.
467 * We only compare the public parts.
470 cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b )
472 int n, i;
474 if( a->timestamp != b->timestamp )
475 return -1;
476 if( a->version < 4 && a->expiredate != b->expiredate )
477 return -1;
478 if( a->pubkey_algo != b->pubkey_algo )
479 return -1;
481 n = pubkey_get_npkey( b->pubkey_algo );
482 if( !n )
483 return -1; /* can't compare due to unknown algorithm */
484 for(i=0; i < n; i++ ) {
485 if( mpi_cmp( a->skey[i], b->skey[i] ) )
486 return -1;
489 return 0;
492 /****************
493 * Returns 0 if they match.
496 cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
498 int n, i;
500 if( pk->timestamp != sk->timestamp )
501 return -1;
502 if( pk->version < 4 && pk->expiredate != sk->expiredate )
503 return -1;
504 if( pk->pubkey_algo != sk->pubkey_algo )
505 return -1;
507 n = pubkey_get_npkey( pk->pubkey_algo );
508 if( !n )
509 return -1; /* can't compare due to unknown algorithm */
510 for(i=0; i < n; i++ ) {
511 if( mpi_cmp( pk->pkey[i] , sk->skey[i] ) )
512 return -1;
514 return 0;
520 cmp_signatures( PKT_signature *a, PKT_signature *b )
522 int n, i;
524 if( a->keyid[0] != b->keyid[0] )
525 return -1;
526 if( a->keyid[1] != b->keyid[1] )
527 return -1;
528 if( a->pubkey_algo != b->pubkey_algo )
529 return -1;
531 n = pubkey_get_nsig( a->pubkey_algo );
532 if( !n )
533 return -1; /* can't compare due to unknown algorithm */
534 for(i=0; i < n; i++ ) {
535 if( mpi_cmp( a->data[i] , b->data[i] ) )
536 return -1;
538 return 0;
542 /****************
543 * Returns: true if the user ids do not match
546 cmp_user_ids( PKT_user_id *a, PKT_user_id *b )
548 int res=1;
550 if( a == b )
551 return 0;
553 if( a->attrib_data && b->attrib_data )
555 res = a->attrib_len - b->attrib_len;
556 if( !res )
557 res = memcmp( a->attrib_data, b->attrib_data, a->attrib_len );
559 else if( !a->attrib_data && !b->attrib_data )
561 res = a->len - b->len;
562 if( !res )
563 res = memcmp( a->name, b->name, a->len );
566 return res;