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
);
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
] ) )
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
)
474 if( a
->timestamp
!= b
->timestamp
)
476 if( a
->version
< 4 && a
->expiredate
!= b
->expiredate
)
478 if( a
->pubkey_algo
!= b
->pubkey_algo
)
481 n
= pubkey_get_npkey( b
->pubkey_algo
);
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
] ) )
493 * Returns 0 if they match.
496 cmp_public_secret_key( PKT_public_key
*pk
, PKT_secret_key
*sk
)
500 if( pk
->timestamp
!= sk
->timestamp
)
502 if( pk
->version
< 4 && pk
->expiredate
!= sk
->expiredate
)
504 if( pk
->pubkey_algo
!= sk
->pubkey_algo
)
507 n
= pubkey_get_npkey( pk
->pubkey_algo
);
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
] ) )
520 cmp_signatures( PKT_signature
*a
, PKT_signature
*b
)
524 if( a
->keyid
[0] != b
->keyid
[0] )
526 if( a
->keyid
[1] != b
->keyid
[1] )
528 if( a
->pubkey_algo
!= b
->pubkey_algo
)
531 n
= pubkey_get_nsig( a
->pubkey_algo
);
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
] ) )
543 * Returns: true if the user ids do not match
546 cmp_user_ids( PKT_user_id
*a
, PKT_user_id
*b
)
553 if( a
->attrib_data
&& b
->attrib_data
)
555 res
= a
->attrib_len
- b
->attrib_len
;
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
;
563 res
= memcmp( a
->name
, b
->name
, a
->len
);