1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-10-31 20:08:43 +01:00
gnupg/g10/free-packet.c

548 lines
11 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* free-packet.c - cleanup stuff for packets
2002-06-29 15:46:34 +02:00
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
* This file is part of GnuPG.
1997-11-18 15:06:00 +01:00
*
* GnuPG is free software; you can redistribute it and/or modify
1997-11-18 15:06:00 +01:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
1997-11-18 15:06:00 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "packet.h"
#include "iobuf.h"
2002-06-29 15:46:34 +02:00
#include "mpi.h"
1997-11-18 15:06:00 +01:00
#include "util.h"
2002-06-29 15:46:34 +02:00
#include "cipher.h"
#include "memory.h"
1998-08-05 18:51:59 +02:00
#include "options.h"
1997-11-18 15:06:00 +01:00
1998-05-03 17:42:08 +02:00
void
free_symkey_enc( PKT_symkey_enc *enc )
{
2002-06-29 15:46:34 +02:00
m_free(enc);
1998-05-03 17:42:08 +02:00
}
1997-11-18 15:06:00 +01:00
void
free_pubkey_enc( PKT_pubkey_enc *enc )
{
1998-06-13 08:59:14 +02:00
int n, i;
n = pubkey_get_nenc( enc->pubkey_algo );
if( !n )
2002-06-29 15:46:34 +02:00
mpi_free(enc->data[0]);
1998-06-13 08:59:14 +02:00
for(i=0; i < n; i++ )
2002-06-29 15:46:34 +02:00
mpi_free( enc->data[i] );
m_free(enc);
1997-11-18 15:06:00 +01:00
}
void
1998-03-09 22:44:06 +01:00
free_seckey_enc( PKT_signature *sig )
1997-11-18 15:06:00 +01:00
{
2002-06-29 15:46:34 +02:00
int n, i;
n = pubkey_get_nsig( sig->pubkey_algo );
if( !n )
mpi_free(sig->data[0]);
for(i=0; i < n; i++ )
mpi_free( sig->data[i] );
m_free(sig->revkey);
m_free(sig->hashed);
m_free(sig->unhashed);
m_free(sig);
1997-11-18 15:06:00 +01:00
}
1998-01-30 17:23:16 +01:00
1997-11-18 15:06:00 +01:00
void
1998-06-29 14:30:57 +02:00
release_public_key_parts( PKT_public_key *pk )
1997-11-18 15:06:00 +01:00
{
1998-06-13 08:59:14 +02:00
int n, i;
1998-06-29 14:30:57 +02:00
n = pubkey_get_npkey( pk->pubkey_algo );
if( !n )
2002-06-29 15:46:34 +02:00
mpi_free(pk->pkey[0]);
1998-06-13 08:59:14 +02:00
for(i=0; i < n; i++ ) {
2002-06-29 15:46:34 +02:00
mpi_free( pk->pkey[i] );
1998-06-29 14:30:57 +02:00
pk->pkey[i] = NULL;
1997-11-24 23:24:04 +01:00
}
2002-06-29 15:46:34 +02:00
if (pk->prefs) {
m_free (pk->prefs);
pk->prefs = NULL;
}
1998-08-05 18:51:59 +02:00
if( pk->namehash ) {
2002-06-29 15:46:34 +02:00
m_free(pk->namehash);
1998-08-05 18:51:59 +02:00
pk->namehash = NULL;
}
2002-06-29 15:46:34 +02:00
if (pk->user_id) {
free_user_id (pk->user_id);
pk->user_id = NULL;
}
if (pk->revkey) {
m_free(pk->revkey);
pk->revkey=NULL;
pk->numrevkeys=0;
}
1998-01-13 20:04:23 +01:00
}
1998-06-13 08:59:14 +02:00
1998-01-13 20:04:23 +01:00
void
1998-06-29 14:30:57 +02:00
free_public_key( PKT_public_key *pk )
1998-01-13 20:04:23 +01:00
{
1998-06-29 14:30:57 +02:00
release_public_key_parts( pk );
2002-06-29 15:46:34 +02:00
m_free(pk);
1997-11-18 15:06:00 +01:00
}
2002-06-29 15:46:34 +02:00
static subpktarea_t *
cp_subpktarea (subpktarea_t *s )
1998-07-29 21:35:05 +02:00
{
2002-06-29 15:46:34 +02:00
subpktarea_t *d;
1998-07-29 21:35:05 +02:00
if( !s )
return NULL;
2002-06-29 15:46:34 +02:00
d = m_alloc (sizeof (*d) + s->size - 1 );
d->size = s->size;
d->len = s->len;
memcpy (d->data, s->data, s->len);
1998-07-29 21:35:05 +02:00
return d;
}
2002-06-29 15:46:34 +02:00
/*
* Return a copy of the preferences
*/
prefitem_t *
copy_prefs (const prefitem_t *prefs)
{
size_t n;
prefitem_t *new;
if (!prefs)
return NULL;
for (n=0; prefs[n].type; n++)
;
new = m_alloc ( sizeof (*new) * (n+1));
for (n=0; prefs[n].type; n++) {
new[n].type = prefs[n].type;
new[n].value = prefs[n].value;
}
new[n].type = PREFTYPE_NONE;
new[n].value = 0;
return new;
}
1998-06-29 14:30:57 +02:00
PKT_public_key *
2002-06-29 15:46:34 +02:00
copy_public_key ( PKT_public_key *d, PKT_public_key *s)
1997-11-18 15:06:00 +01:00
{
1998-06-13 08:59:14 +02:00
int n, i;
1997-11-18 15:06:00 +01:00
if( !d )
2002-06-29 15:46:34 +02:00
d = m_alloc(sizeof *d);
1997-11-18 15:06:00 +01:00
memcpy( d, s, sizeof *d );
2002-06-29 15:46:34 +02:00
d->user_id = scopy_user_id (s->user_id);
d->prefs = copy_prefs (s->prefs);
1998-06-13 08:59:14 +02:00
n = pubkey_get_npkey( s->pubkey_algo );
if( !n )
d->pkey[0] = mpi_copy(s->pkey[0]);
else {
for(i=0; i < n; i++ )
d->pkey[i] = mpi_copy( s->pkey[i] );
}
2002-06-29 15:46:34 +02:00
if( !s->revkey && s->numrevkeys )
BUG();
if( s->numrevkeys ) {
d->revkey = m_alloc(sizeof(struct revocation_key)*s->numrevkeys);
memcpy(d->revkey,s->revkey,sizeof(struct revocation_key)*s->numrevkeys);
}
else
d->revkey = NULL;
1997-11-18 15:06:00 +01:00
return d;
}
/****************
* Replace all common parts of a sk by the one from the public key.
* This is a hack and a better solution will be to just store the real secret
* parts somewhere and don't duplicate all the other stuff.
*/
void
copy_public_parts_to_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
{
sk->expiredate = pk->expiredate;
sk->pubkey_algo = pk->pubkey_algo;
sk->pubkey_usage= pk->pubkey_usage;
sk->req_usage = pk->req_usage;
sk->req_algo = pk->req_algo;
sk->has_expired = pk->has_expired;
sk->is_revoked = pk->is_revoked;
sk->is_valid = pk->is_valid;
sk->main_keyid[0]= pk->main_keyid[0];
sk->main_keyid[1]= pk->main_keyid[1];
sk->keyid[0] = pk->keyid[0];
sk->keyid[1] = pk->keyid[1];
}
1998-07-29 21:35:05 +02:00
PKT_signature *
copy_signature( PKT_signature *d, PKT_signature *s )
{
int n, i;
if( !d )
2002-06-29 15:46:34 +02:00
d = m_alloc(sizeof *d);
1998-07-29 21:35:05 +02:00
memcpy( d, s, sizeof *d );
n = pubkey_get_nsig( s->pubkey_algo );
1998-07-29 21:35:05 +02:00
if( !n )
d->data[0] = mpi_copy(s->data[0]);
else {
for(i=0; i < n; i++ )
d->data[i] = mpi_copy( s->data[i] );
}
2002-06-29 15:46:34 +02:00
d->hashed = cp_subpktarea (s->hashed);
d->unhashed = cp_subpktarea (s->unhashed);
if(s->numrevkeys)
{
d->revkey=NULL;
d->numrevkeys=0;
parse_revkeys(d);
}
1998-07-29 21:35:05 +02:00
return d;
}
2002-06-29 15:46:34 +02:00
/*
* shallow copy of the user ID
*/
1998-07-29 21:35:05 +02:00
PKT_user_id *
2002-06-29 15:46:34 +02:00
scopy_user_id (PKT_user_id *s)
1998-07-29 21:35:05 +02:00
{
2002-06-29 15:46:34 +02:00
if (s)
s->ref++;
return s;
1998-07-29 21:35:05 +02:00
}
1997-11-18 15:06:00 +01:00
void
1998-06-29 14:30:57 +02:00
release_secret_key_parts( PKT_secret_key *sk )
1997-11-18 15:06:00 +01:00
{
1998-06-13 08:59:14 +02:00
int n, i;
1998-06-29 14:30:57 +02:00
n = pubkey_get_nskey( sk->pubkey_algo );
if( !n )
2002-06-29 15:46:34 +02:00
mpi_free(sk->skey[0]);
1998-06-13 08:59:14 +02:00
for(i=0; i < n; i++ ) {
2002-06-29 15:46:34 +02:00
mpi_free( sk->skey[i] );
1998-06-29 14:30:57 +02:00
sk->skey[i] = NULL;
1997-11-18 15:06:00 +01:00
}
1998-01-13 20:04:23 +01:00
}
void
1998-06-29 14:30:57 +02:00
free_secret_key( PKT_secret_key *sk )
1998-01-13 20:04:23 +01:00
{
1998-06-29 14:30:57 +02:00
release_secret_key_parts( sk );
2002-06-29 15:46:34 +02:00
m_free(sk);
1997-11-18 15:06:00 +01:00
}
1998-06-29 14:30:57 +02:00
PKT_secret_key *
copy_secret_key( PKT_secret_key *d, PKT_secret_key *s )
1997-11-24 12:04:11 +01:00
{
1998-06-13 08:59:14 +02:00
int n, i;
1997-11-24 12:04:11 +01:00
if( !d )
2002-06-29 15:46:34 +02:00
d = m_alloc(sizeof *d);
1997-11-24 12:04:11 +01:00
memcpy( d, s, sizeof *d );
1998-06-13 08:59:14 +02:00
n = pubkey_get_nskey( s->pubkey_algo );
if( !n )
d->skey[0] = mpi_copy(s->skey[0]);
else {
for(i=0; i < n; i++ )
d->skey[i] = mpi_copy( s->skey[i] );
}
1997-11-24 12:04:11 +01:00
return d;
}
1997-11-18 15:06:00 +01:00
void
free_comment( PKT_comment *rem )
{
2002-06-29 15:46:34 +02:00
m_free(rem);
1997-11-18 15:06:00 +01:00
}
void
2002-06-29 15:46:34 +02:00
free_attributes(PKT_user_id *uid)
1997-11-18 15:06:00 +01:00
{
2002-06-29 15:46:34 +02:00
m_free(uid->attribs);
m_free(uid->attrib_data);
uid->attribs=NULL;
uid->attrib_data=NULL;
uid->attrib_len=0;
}
void
free_user_id (PKT_user_id *uid)
{
assert (uid->ref > 0);
if (--uid->ref)
return;
free_attributes(uid);
if (uid->prefs)
m_free (uid->prefs);
m_free (uid);
1997-11-18 15:06:00 +01:00
}
void
free_compressed( PKT_compressed *zd )
{
if( zd->buf ) { /* have to skip some bytes */
1998-04-14 19:51:16 +02:00
/* don't have any information about the length, so
1997-11-18 15:06:00 +01:00
* we assume this is the last packet */
while( iobuf_read( zd->buf, NULL, 1<<30 ) != -1 )
1997-11-18 15:06:00 +01:00
;
}
2002-06-29 15:46:34 +02:00
m_free(zd);
1997-11-18 15:06:00 +01:00
}
void
1997-12-01 11:33:23 +01:00
free_encrypted( PKT_encrypted *ed )
1997-11-18 15:06:00 +01:00
{
if( ed->buf ) { /* have to skip some bytes */
if( iobuf_in_block_mode(ed->buf) ) {
while( iobuf_read( ed->buf, NULL, 1<<30 ) != -1 )
1997-11-18 15:06:00 +01:00
;
}
else {
while( ed->len ) { /* skip the packet */
int n = iobuf_read( ed->buf, NULL, ed->len );
if( n == -1 )
ed->len = 0;
else
ed->len -= n;
}
1997-11-18 15:06:00 +01:00
}
}
2002-06-29 15:46:34 +02:00
m_free(ed);
1997-11-18 15:06:00 +01:00
}
void
free_plaintext( PKT_plaintext *pt )
{
if( pt->buf ) { /* have to skip some bytes */
if( iobuf_in_block_mode(pt->buf) ) {
while( iobuf_read( pt->buf, NULL, 1<<30 ) != -1 )
1997-11-18 15:06:00 +01:00
;
}
else {
while( pt->len ) { /* skip the packet */
int n = iobuf_read( pt->buf, NULL, pt->len );
if( n == -1 )
pt->len = 0;
else
pt->len -= n;
}
1997-11-18 15:06:00 +01:00
}
}
2002-06-29 15:46:34 +02:00
m_free(pt);
1997-11-18 15:06:00 +01:00
}
/****************
* Free the packet in pkt.
*/
void
free_packet( PACKET *pkt )
{
if( !pkt || !pkt->pkt.generic )
return;
if( DBG_MEMORY )
log_debug("free_packet() type=%d\n", pkt->pkttype );
switch( pkt->pkttype ) {
case PKT_SIGNATURE:
free_seckey_enc( pkt->pkt.signature );
break;
case PKT_PUBKEY_ENC:
free_pubkey_enc( pkt->pkt.pubkey_enc );
break;
1998-05-03 17:42:08 +02:00
case PKT_SYMKEY_ENC:
free_symkey_enc( pkt->pkt.symkey_enc );
break;
1998-06-29 14:30:57 +02:00
case PKT_PUBLIC_KEY:
case PKT_PUBLIC_SUBKEY:
free_public_key( pkt->pkt.public_key );
1997-11-18 15:06:00 +01:00
break;
1998-06-29 14:30:57 +02:00
case PKT_SECRET_KEY:
case PKT_SECRET_SUBKEY:
free_secret_key( pkt->pkt.secret_key );
1997-11-18 15:06:00 +01:00
break;
case PKT_COMMENT:
free_comment( pkt->pkt.comment );
break;
case PKT_USER_ID:
free_user_id( pkt->pkt.user_id );
break;
1997-12-01 11:33:23 +01:00
case PKT_COMPRESSED:
1997-11-18 15:06:00 +01:00
free_compressed( pkt->pkt.compressed);
break;
1997-12-01 11:33:23 +01:00
case PKT_ENCRYPTED:
2002-06-29 15:46:34 +02:00
case PKT_ENCRYPTED_MDC:
1997-12-01 11:33:23 +01:00
free_encrypted( pkt->pkt.encrypted );
1997-11-18 15:06:00 +01:00
break;
case PKT_PLAINTEXT:
free_plaintext( pkt->pkt.plaintext );
break;
default:
2002-06-29 15:46:34 +02:00
m_free( pkt->pkt.generic );
1997-11-18 15:06:00 +01:00
break;
}
pkt->pkt.generic = NULL;
}
1998-02-17 21:48:52 +01:00
/****************
* returns 0 if they match.
1998-02-17 21:48:52 +01:00
*/
int
1998-06-29 14:30:57 +02:00
cmp_public_keys( PKT_public_key *a, PKT_public_key *b )
1998-02-17 21:48:52 +01:00
{
1998-06-13 08:59:14 +02:00
int n, i;
1998-02-17 21:48:52 +01:00
if( a->timestamp != b->timestamp )
return -1;
if( a->version < 4 && a->expiredate != b->expiredate )
1998-02-17 21:48:52 +01:00
return -1;
if( a->pubkey_algo != b->pubkey_algo )
return -1;
1998-06-13 08:59:14 +02:00
n = pubkey_get_npkey( b->pubkey_algo );
if( !n )
return -1; /* can't compare due to unknown algorithm */
1998-06-13 08:59:14 +02:00
for(i=0; i < n; i++ ) {
if( mpi_cmp( a->pkey[i], b->pkey[i] ) )
1998-02-17 21:48:52 +01:00
return -1;
}
return 0;
}
/****************
* Returns 0 if they match.
* We only compare the public parts.
*/
int
cmp_secret_keys( PKT_secret_key *a, PKT_secret_key *b )
{
int n, i;
if( a->timestamp != b->timestamp )
return -1;
if( a->version < 4 && a->expiredate != b->expiredate )
return -1;
if( a->pubkey_algo != b->pubkey_algo )
return -1;
n = pubkey_get_npkey( b->pubkey_algo );
if( !n )
return -1; /* can't compare due to unknown algorithm */
for(i=0; i < n; i++ ) {
if( mpi_cmp( a->skey[i], b->skey[i] ) )
return -1;
}
return 0;
}
1998-01-13 20:04:23 +01:00
/****************
* Returns 0 if they match.
*/
int
1998-06-29 14:30:57 +02:00
cmp_public_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
1998-01-13 20:04:23 +01:00
{
1998-06-13 08:59:14 +02:00
int n, i;
1998-06-29 14:30:57 +02:00
if( pk->timestamp != sk->timestamp )
1998-01-13 20:04:23 +01:00
return -1;
if( pk->version < 4 && pk->expiredate != sk->expiredate )
1998-01-13 20:04:23 +01:00
return -1;
1998-06-29 14:30:57 +02:00
if( pk->pubkey_algo != sk->pubkey_algo )
1998-01-13 20:04:23 +01:00
return -1;
1998-06-29 14:30:57 +02:00
n = pubkey_get_npkey( pk->pubkey_algo );
if( !n )
return -1; /* can't compare due to unknown algorithm */
1998-06-13 08:59:14 +02:00
for(i=0; i < n; i++ ) {
1998-06-29 14:30:57 +02:00
if( mpi_cmp( pk->pkey[i] , sk->skey[i] ) )
1998-03-09 22:44:06 +01:00
return -1;
}
1998-01-13 20:04:23 +01:00
return 0;
}
1997-11-18 15:06:00 +01:00
1998-10-18 17:21:22 +02:00
int
cmp_signatures( PKT_signature *a, PKT_signature *b )
{
int n, i;
if( a->keyid[0] != b->keyid[0] )
return -1;
if( a->keyid[1] != b->keyid[1] )
return -1;
if( a->pubkey_algo != b->pubkey_algo )
return -1;
n = pubkey_get_nsig( a->pubkey_algo );
1998-10-18 17:21:22 +02:00
if( !n )
return -1; /* can't compare due to unknown algorithm */
for(i=0; i < n; i++ ) {
if( mpi_cmp( a->data[i] , b->data[i] ) )
return -1;
}
return 0;
}
/****************
* Returns: true if the user ids do not match
*/
1998-02-17 21:48:52 +01:00
int
cmp_user_ids( PKT_user_id *a, PKT_user_id *b )
{
2002-06-29 15:46:34 +02:00
int res=1;
if( a == b )
return 0;
if( a->attrib_data && b->attrib_data )
{
res = a->attrib_len - b->attrib_len;
if( !res )
res = memcmp( a->attrib_data, b->attrib_data, a->attrib_len );
}
else if( !a->attrib_data && !b->attrib_data )
{
res = a->len - b->len;
if( !res )
res = memcmp( a->name, b->name, a->len );
}
1998-02-17 21:48:52 +01:00
return res;
}