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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
31 #include "../common/iobuf.h"
38 free_symkey_enc( PKT_symkey_enc
*enc
)
44 free_pubkey_enc( PKT_pubkey_enc
*enc
)
47 n
= pubkey_get_nenc( enc
->pubkey_algo
);
49 mpi_release(enc
->data
[0]);
51 mpi_release( enc
->data
[i
] );
56 free_seckey_enc( PKT_signature
*sig
)
60 n
= pubkey_get_nsig( sig
->pubkey_algo
);
62 mpi_release(sig
->data
[0]);
64 mpi_release( sig
->data
[i
] );
72 xfree (sig
->pka_info
->uri
);
73 xfree (sig
->pka_info
);
81 release_public_key_parts( PKT_public_key
*pk
)
84 n
= pubkey_get_npkey( pk
->pubkey_algo
);
86 mpi_release(pk
->pkey
[0]);
87 for(i
=0; i
< n
; i
++ ) {
88 mpi_release( pk
->pkey
[i
] );
96 free_user_id (pk
->user_id
);
108 free_public_key( PKT_public_key
*pk
)
110 release_public_key_parts( pk
);
115 static subpktarea_t
*
116 cp_subpktarea (subpktarea_t
*s
)
122 d
= xmalloc (sizeof (*d
) + s
->size
- 1 );
125 memcpy (d
->data
, s
->data
, s
->len
);
130 * Return a copy of the preferences
133 copy_prefs (const prefitem_t
*prefs
)
141 for (n
=0; prefs
[n
].type
; n
++)
143 new = xmalloc ( sizeof (*new) * (n
+1));
144 for (n
=0; prefs
[n
].type
; n
++) {
145 new[n
].type
= prefs
[n
].type
;
146 new[n
].value
= prefs
[n
].value
;
148 new[n
].type
= PREFTYPE_NONE
;
156 copy_public_key ( PKT_public_key
*d
, PKT_public_key
*s
)
161 d
= xmalloc(sizeof *d
);
162 memcpy( d
, s
, sizeof *d
);
163 d
->user_id
= scopy_user_id (s
->user_id
);
164 d
->prefs
= copy_prefs (s
->prefs
);
165 n
= pubkey_get_npkey( s
->pubkey_algo
);
167 d
->pkey
[0] = mpi_copy(s
->pkey
[0]);
169 for(i
=0; i
< n
; i
++ )
170 d
->pkey
[i
] = mpi_copy( s
->pkey
[i
] );
172 if( !s
->revkey
&& s
->numrevkeys
)
174 if( s
->numrevkeys
) {
175 d
->revkey
= xmalloc(sizeof(struct revocation_key
)*s
->numrevkeys
);
176 memcpy(d
->revkey
,s
->revkey
,sizeof(struct revocation_key
)*s
->numrevkeys
);
184 * Replace all common parts of a sk by the one from the public key.
185 * This is a hack and a better solution will be to just store the real secret
186 * parts somewhere and don't duplicate all the other stuff.
189 copy_public_parts_to_secret_key( PKT_public_key
*pk
, PKT_secret_key
*sk
)
191 sk
->expiredate
= pk
->expiredate
;
192 sk
->pubkey_algo
= pk
->pubkey_algo
;
193 sk
->pubkey_usage
= pk
->pubkey_usage
;
194 sk
->req_usage
= pk
->req_usage
;
195 sk
->req_algo
= pk
->req_algo
;
196 sk
->has_expired
= pk
->has_expired
;
197 sk
->is_revoked
= pk
->is_revoked
;
198 sk
->is_valid
= pk
->is_valid
;
199 sk
->main_keyid
[0]= pk
->main_keyid
[0];
200 sk
->main_keyid
[1]= pk
->main_keyid
[1];
201 sk
->keyid
[0] = pk
->keyid
[0];
202 sk
->keyid
[1] = pk
->keyid
[1];
207 cp_pka_info (const pka_info_t
*s
)
209 pka_info_t
*d
= xmalloc (sizeof *s
+ strlen (s
->email
));
212 d
->checked
= s
->checked
;
213 d
->uri
= s
->uri
? xstrdup (s
->uri
):NULL
;
214 memcpy (d
->fpr
, s
->fpr
, sizeof s
->fpr
);
215 strcpy (d
->email
, s
->email
);
221 copy_signature( PKT_signature
*d
, PKT_signature
*s
)
226 d
= xmalloc(sizeof *d
);
227 memcpy( d
, s
, sizeof *d
);
228 n
= pubkey_get_nsig( s
->pubkey_algo
);
230 d
->data
[0] = mpi_copy(s
->data
[0]);
232 for(i
=0; i
< n
; i
++ )
233 d
->data
[i
] = mpi_copy( s
->data
[i
] );
235 d
->pka_info
= s
->pka_info
? cp_pka_info (s
->pka_info
) : NULL
;
236 d
->hashed
= cp_subpktarea (s
->hashed
);
237 d
->unhashed
= cp_subpktarea (s
->unhashed
);
249 * shallow copy of the user ID
252 scopy_user_id (PKT_user_id
*s
)
262 release_secret_key_parts( PKT_secret_key
*sk
)
266 n
= pubkey_get_nskey( sk
->pubkey_algo
);
268 mpi_release(sk
->skey
[0]);
269 for(i
=0; i
< n
; i
++ ) {
270 mpi_release( sk
->skey
[i
] );
276 free_secret_key( PKT_secret_key
*sk
)
278 release_secret_key_parts( sk
);
283 copy_secret_key( PKT_secret_key
*d
, PKT_secret_key
*s
)
288 d
= xmalloc_secure(sizeof *d
);
290 release_secret_key_parts (d
);
291 memcpy( d
, s
, sizeof *d
);
292 n
= pubkey_get_nskey( s
->pubkey_algo
);
294 d
->skey
[0] = mpi_copy(s
->skey
[0]);
296 for(i
=0; i
< n
; i
++ )
297 d
->skey
[i
] = mpi_copy( s
->skey
[i
] );
304 free_comment( PKT_comment
*rem
)
310 free_attributes(PKT_user_id
*uid
)
313 xfree(uid
->attrib_data
);
316 uid
->attrib_data
=NULL
;
321 free_user_id (PKT_user_id
*uid
)
323 assert (uid
->ref
> 0);
327 free_attributes(uid
);
329 xfree (uid
->namehash
);
334 free_compressed( PKT_compressed
*zd
)
336 if( zd
->buf
) { /* have to skip some bytes */
337 /* don't have any information about the length, so
338 * we assume this is the last packet */
339 while( iobuf_read( zd
->buf
, NULL
, 1<<30 ) != -1 )
346 free_encrypted( PKT_encrypted
*ed
)
348 if( ed
->buf
) { /* have to skip some bytes */
349 if( ed
->is_partial
) {
350 while( iobuf_read( ed
->buf
, NULL
, 1<<30 ) != -1 )
354 while( ed
->len
) { /* skip the packet */
355 int n
= iobuf_read( ed
->buf
, NULL
, ed
->len
);
368 free_plaintext( PKT_plaintext
*pt
)
370 if( pt
->buf
) { /* have to skip some bytes */
371 if( pt
->is_partial
) {
372 while( iobuf_read( pt
->buf
, NULL
, 1<<30 ) != -1 )
376 while( pt
->len
) { /* skip the packet */
377 int n
= iobuf_read( pt
->buf
, NULL
, pt
->len
);
389 * Free the packet in pkt.
392 free_packet( PACKET
*pkt
)
394 if( !pkt
|| !pkt
->pkt
.generic
)
398 log_debug("free_packet() type=%d\n", pkt
->pkttype
);
400 switch( pkt
->pkttype
) {
402 free_seckey_enc( pkt
->pkt
.signature
);
405 free_pubkey_enc( pkt
->pkt
.pubkey_enc
);
408 free_symkey_enc( pkt
->pkt
.symkey_enc
);
411 case PKT_PUBLIC_SUBKEY
:
412 free_public_key( pkt
->pkt
.public_key
);
415 case PKT_SECRET_SUBKEY
:
416 free_secret_key( pkt
->pkt
.secret_key
);
419 free_comment( pkt
->pkt
.comment
);
422 free_user_id( pkt
->pkt
.user_id
);
425 free_compressed( pkt
->pkt
.compressed
);
428 case PKT_ENCRYPTED_MDC
:
429 free_encrypted( pkt
->pkt
.encrypted
);
432 free_plaintext( pkt
->pkt
.plaintext
);
435 xfree( pkt
->pkt
.generic
);
438 pkt
->pkt
.generic
= NULL
;
442 * returns 0 if they match.
445 cmp_public_keys( PKT_public_key
*a
, PKT_public_key
*b
)
449 if( a
->timestamp
!= b
->timestamp
)
451 if( a
->version
< 4 && a
->expiredate
!= b
->expiredate
)
453 if( a
->pubkey_algo
!= b
->pubkey_algo
)
456 n
= pubkey_get_npkey( b
->pubkey_algo
);
458 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
] ) )
468 * Returns 0 if they match.
469 * We only compare the public parts.
472 cmp_secret_keys( PKT_secret_key
*a
, PKT_secret_key
*b
)
476 if( a
->timestamp
!= b
->timestamp
)
478 if( a
->version
< 4 && a
->expiredate
!= b
->expiredate
)
480 if( a
->pubkey_algo
!= b
->pubkey_algo
)
483 n
= pubkey_get_npkey( b
->pubkey_algo
);
485 return -1; /* can't compare due to unknown algorithm */
486 for(i
=0; i
< n
; i
++ ) {
487 if( mpi_cmp( a
->skey
[i
], b
->skey
[i
] ) )
495 * Returns 0 if they match.
498 cmp_public_secret_key( PKT_public_key
*pk
, PKT_secret_key
*sk
)
502 if( pk
->timestamp
!= sk
->timestamp
)
504 if( pk
->version
< 4 && pk
->expiredate
!= sk
->expiredate
)
506 if( pk
->pubkey_algo
!= sk
->pubkey_algo
)
509 n
= pubkey_get_npkey( pk
->pubkey_algo
);
511 return -1; /* can't compare due to unknown algorithm */
512 for(i
=0; i
< n
; i
++ ) {
513 if( mpi_cmp( pk
->pkey
[i
] , sk
->skey
[i
] ) )
522 cmp_signatures( PKT_signature
*a
, PKT_signature
*b
)
526 if( a
->keyid
[0] != b
->keyid
[0] )
528 if( a
->keyid
[1] != b
->keyid
[1] )
530 if( a
->pubkey_algo
!= b
->pubkey_algo
)
533 n
= pubkey_get_nsig( a
->pubkey_algo
);
535 return -1; /* can't compare due to unknown algorithm */
536 for(i
=0; i
< n
; i
++ ) {
537 if( mpi_cmp( a
->data
[i
] , b
->data
[i
] ) )
545 * Returns: true if the user ids do not match
548 cmp_user_ids( PKT_user_id
*a
, PKT_user_id
*b
)
555 if( a
->attrib_data
&& b
->attrib_data
)
557 res
= a
->attrib_len
- b
->attrib_len
;
559 res
= memcmp( a
->attrib_data
, b
->attrib_data
, a
->attrib_len
);
561 else if( !a
->attrib_data
&& !b
->attrib_data
)
563 res
= a
->len
- b
->len
;
565 res
= memcmp( a
->name
, b
->name
, a
->len
);