1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-10 21:38:50 +01:00
gnupg/g10/free-packet.c

389 lines
9.2 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* free-packet.c - cleanup stuff for packets
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* This file is part of GNUPG.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +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.
*
1998-02-24 19:50:46 +01:00
* 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"
#include "mpi.h"
#include "util.h"
#include "cipher.h"
#include "memory.h"
1998-05-03 17:42:08 +02:00
void
free_symkey_enc( PKT_symkey_enc *enc )
{
m_free(enc);
}
1997-11-18 15:06:00 +01:00
void
free_pubkey_enc( PKT_pubkey_enc *enc )
{
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(enc->pubkey_algo) ) {
1997-11-24 23:24:04 +01:00
mpi_free( enc->d.elg.a );
mpi_free( enc->d.elg.b );
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(enc->pubkey_algo) )
1997-11-24 23:24:04 +01:00
mpi_free( enc->d.rsa.rsa_integer );
1997-11-18 15:06:00 +01:00
m_free(enc);
}
void
1998-03-09 22:44:06 +01:00
free_seckey_enc( PKT_signature *sig )
1997-11-18 15:06:00 +01:00
{
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(sig->pubkey_algo) ) {
1998-03-09 22:44:06 +01:00
mpi_free( sig->d.elg.a );
mpi_free( sig->d.elg.b );
1997-11-24 23:24:04 +01:00
}
1998-03-09 22:44:06 +01:00
else if( sig->pubkey_algo == PUBKEY_ALGO_DSA ) {
mpi_free( sig->d.dsa.r );
mpi_free( sig->d.dsa.s );
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(sig->pubkey_algo) )
1998-03-09 22:44:06 +01:00
mpi_free( sig->d.rsa.rsa_integer );
m_free(sig->hashed_data);
m_free(sig->unhashed_data);
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-01-13 20:04:23 +01:00
release_public_cert_parts( PKT_public_cert *cert )
1997-11-18 15:06:00 +01:00
{
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(cert->pubkey_algo) ) {
1998-01-13 20:04:23 +01:00
mpi_free( cert->d.elg.p ); cert->d.elg.p = NULL;
mpi_free( cert->d.elg.g ); cert->d.elg.g = NULL;
mpi_free( cert->d.elg.y ); cert->d.elg.y = NULL;
1997-11-24 23:24:04 +01:00
}
1998-03-09 22:44:06 +01:00
else if( cert->pubkey_algo == PUBKEY_ALGO_DSA ) {
mpi_free( cert->d.dsa.p ); cert->d.dsa.p = NULL;
mpi_free( cert->d.dsa.q ); cert->d.dsa.q = NULL;
mpi_free( cert->d.dsa.g ); cert->d.dsa.g = NULL;
mpi_free( cert->d.dsa.y ); cert->d.dsa.y = NULL;
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(cert->pubkey_algo) ) {
1998-04-08 21:49:02 +02:00
mpi_free( cert->d.rsa.n ); cert->d.rsa.n = NULL;
mpi_free( cert->d.rsa.e ); cert->d.rsa.e = NULL;
1997-11-24 23:24:04 +01:00
}
1998-01-13 20:04:23 +01:00
}
void
free_public_cert( PKT_public_cert *cert )
{
release_public_cert_parts( cert );
1997-11-18 15:06:00 +01:00
m_free(cert);
}
1997-12-01 11:33:23 +01:00
PKT_public_cert *
copy_public_cert( PKT_public_cert *d, PKT_public_cert *s )
1997-11-18 15:06:00 +01:00
{
if( !d )
d = m_alloc(sizeof *d);
memcpy( d, s, sizeof *d );
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(s->pubkey_algo) ) {
1997-11-24 23:24:04 +01:00
d->d.elg.p = mpi_copy( s->d.elg.p );
d->d.elg.g = mpi_copy( s->d.elg.g );
d->d.elg.y = mpi_copy( s->d.elg.y );
}
1998-03-09 22:44:06 +01:00
else if( s->pubkey_algo == PUBKEY_ALGO_DSA ) {
d->d.dsa.p = mpi_copy( s->d.dsa.p );
d->d.dsa.q = mpi_copy( s->d.dsa.q );
d->d.dsa.g = mpi_copy( s->d.dsa.g );
d->d.dsa.y = mpi_copy( s->d.dsa.y );
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(s->pubkey_algo) ) {
1998-04-08 21:49:02 +02:00
d->d.rsa.n = mpi_copy( s->d.rsa.n );
d->d.rsa.e = mpi_copy( s->d.rsa.e );
1997-11-24 23:24:04 +01:00
}
1997-11-18 15:06:00 +01:00
return d;
}
void
1998-01-13 20:04:23 +01:00
release_secret_cert_parts( PKT_secret_cert *cert )
1997-11-18 15:06:00 +01:00
{
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(cert->pubkey_algo) ) {
1998-01-13 20:04:23 +01:00
mpi_free( cert->d.elg.p ); cert->d.elg.p = NULL;
mpi_free( cert->d.elg.g ); cert->d.elg.g = NULL;
mpi_free( cert->d.elg.y ); cert->d.elg.y = NULL;
mpi_free( cert->d.elg.x ); cert->d.elg.x = NULL;
1997-11-18 15:06:00 +01:00
}
1998-03-09 22:44:06 +01:00
else if( cert->pubkey_algo == PUBKEY_ALGO_DSA ) {
mpi_free( cert->d.dsa.p ); cert->d.dsa.p = NULL;
mpi_free( cert->d.dsa.q ); cert->d.dsa.q = NULL;
mpi_free( cert->d.dsa.g ); cert->d.dsa.g = NULL;
mpi_free( cert->d.dsa.y ); cert->d.dsa.y = NULL;
mpi_free( cert->d.dsa.x ); cert->d.dsa.x = NULL;
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(cert->pubkey_algo) ) {
1998-04-08 21:49:02 +02:00
mpi_free( cert->d.rsa.n ); cert->d.rsa.n = NULL;
mpi_free( cert->d.rsa.e ); cert->d.rsa.e = NULL;
mpi_free( cert->d.rsa.d ); cert->d.rsa.d = NULL;
mpi_free( cert->d.rsa.p ); cert->d.rsa.p = NULL;
mpi_free( cert->d.rsa.q ); cert->d.rsa.q = NULL;
mpi_free( cert->d.rsa.u ); cert->d.rsa.u = NULL;
1997-11-18 15:06:00 +01:00
}
1998-01-13 20:04:23 +01:00
}
void
free_secret_cert( PKT_secret_cert *cert )
{
release_secret_cert_parts( cert );
1997-11-18 15:06:00 +01:00
m_free(cert);
}
1997-12-01 11:33:23 +01:00
PKT_secret_cert *
copy_secret_cert( PKT_secret_cert *d, PKT_secret_cert *s )
1997-11-24 12:04:11 +01:00
{
if( !d )
d = m_alloc(sizeof *d);
memcpy( d, s, sizeof *d );
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(s->pubkey_algo) ) {
1997-11-24 23:24:04 +01:00
d->d.elg.p = mpi_copy( s->d.elg.p );
d->d.elg.g = mpi_copy( s->d.elg.g );
d->d.elg.y = mpi_copy( s->d.elg.y );
d->d.elg.x = mpi_copy( s->d.elg.x );
}
1998-03-09 22:44:06 +01:00
else if( s->pubkey_algo == PUBKEY_ALGO_DSA ) {
d->d.dsa.p = mpi_copy( s->d.dsa.p );
d->d.dsa.q = mpi_copy( s->d.dsa.q );
d->d.dsa.g = mpi_copy( s->d.dsa.g );
d->d.dsa.y = mpi_copy( s->d.dsa.y );
d->d.dsa.x = mpi_copy( s->d.dsa.x );
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(s->pubkey_algo) ) {
1998-04-08 21:49:02 +02:00
d->d.rsa.n = mpi_copy( s->d.rsa.n );
d->d.rsa.e = mpi_copy( s->d.rsa.e );
d->d.rsa.d = mpi_copy( s->d.rsa.d );
d->d.rsa.p = mpi_copy( s->d.rsa.p );
d->d.rsa.q = mpi_copy( s->d.rsa.q );
d->d.rsa.u = mpi_copy( s->d.rsa.u );
1997-11-24 23:24:04 +01:00
}
1997-11-24 12:04:11 +01:00
return d;
}
1997-11-18 15:06:00 +01:00
void
free_comment( PKT_comment *rem )
{
m_free(rem);
}
void
free_user_id( PKT_user_id *uid )
{
m_free(uid);
}
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_get(zd->buf) != -1 )
;
}
m_free(zd);
}
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_get(ed->buf) != -1 )
;
iobuf_set_block_mode(ed->buf, 0);
}
else {
for( ; ed->len; ed->len-- ) /* skip the packet */
iobuf_get(ed->buf);
}
}
m_free(ed);
}
void
free_plaintext( PKT_plaintext *pt )
{
if( pt->buf ) { /* have to skip some bytes */
if( iobuf_in_block_mode(pt->buf) ) {
while( iobuf_get(pt->buf) != -1 )
;
iobuf_set_block_mode(pt->buf, 0);
}
else {
for( ; pt->len; pt->len-- ) /* skip the packet */
iobuf_get(pt->buf);
}
}
m_free(pt);
}
/****************
* 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;
1997-12-01 11:33:23 +01:00
case PKT_PUBLIC_CERT:
free_public_cert( pkt->pkt.public_cert );
1997-11-18 15:06:00 +01:00
break;
1997-12-01 11:33:23 +01:00
case PKT_SECRET_CERT:
free_secret_cert( pkt->pkt.secret_cert );
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:
free_encrypted( pkt->pkt.encrypted );
1997-11-18 15:06:00 +01:00
break;
case PKT_PLAINTEXT:
free_plaintext( pkt->pkt.plaintext );
break;
default:
m_free( pkt->pkt.generic );
break;
}
pkt->pkt.generic = NULL;
}
1998-02-17 21:48:52 +01:00
/****************
* Returns 0 if they match.
*/
int
cmp_public_certs( PKT_public_cert *a, PKT_public_cert *b )
{
if( a->timestamp != b->timestamp )
return -1;
if( a->valid_days != b->valid_days )
return -1;
if( a->pubkey_algo != b->pubkey_algo )
return -1;
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(a->pubkey_algo) ) {
1998-02-17 21:48:52 +01:00
if( mpi_cmp( a->d.elg.p , b->d.elg.p ) )
return -1;
if( mpi_cmp( a->d.elg.g , b->d.elg.g ) )
return -1;
if( mpi_cmp( a->d.elg.y , b->d.elg.y ) )
return -1;
}
1998-03-09 22:44:06 +01:00
else if( a->pubkey_algo == PUBKEY_ALGO_DSA ) {
if( mpi_cmp( a->d.dsa.p , b->d.dsa.p ) )
return -1;
if( mpi_cmp( a->d.dsa.q , b->d.dsa.q ) )
return -1;
if( mpi_cmp( a->d.dsa.g , b->d.dsa.g ) )
return -1;
if( mpi_cmp( a->d.dsa.y , b->d.dsa.y ) )
return -1;
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(a->pubkey_algo) ) {
1998-04-08 21:49:02 +02:00
if( mpi_cmp( a->d.rsa.n , b->d.rsa.n ) )
1998-02-17 21:48:52 +01:00
return -1;
1998-04-08 21:49:02 +02:00
if( mpi_cmp( a->d.rsa.e , b->d.rsa.e ) )
1998-02-17 21:48:52 +01:00
return -1;
}
return 0;
}
1998-01-13 20:04:23 +01:00
/****************
* Returns 0 if they match.
*/
int
cmp_public_secret_cert( PKT_public_cert *pkc, PKT_secret_cert *skc )
{
if( pkc->timestamp != skc->timestamp )
return -1;
if( pkc->valid_days != skc->valid_days )
return -1;
if( pkc->pubkey_algo != skc->pubkey_algo )
return -1;
1998-05-04 20:49:26 +02:00
if( is_ELGAMAL(pkc->pubkey_algo) ) {
1998-01-13 20:04:23 +01:00
if( mpi_cmp( pkc->d.elg.p , skc->d.elg.p ) )
return -1;
if( mpi_cmp( pkc->d.elg.g , skc->d.elg.g ) )
return -1;
if( mpi_cmp( pkc->d.elg.y , skc->d.elg.y ) )
return -1;
}
1998-03-09 22:44:06 +01:00
else if( pkc->pubkey_algo == PUBKEY_ALGO_DSA ) {
if( mpi_cmp( pkc->d.dsa.p , skc->d.dsa.p ) )
return -1;
if( mpi_cmp( pkc->d.dsa.q , skc->d.dsa.q ) )
return -1;
if( mpi_cmp( pkc->d.dsa.g , skc->d.dsa.g ) )
return -1;
if( mpi_cmp( pkc->d.dsa.y , skc->d.dsa.y ) )
return -1;
}
1998-05-04 20:49:26 +02:00
else if( is_RSA(pkc->pubkey_algo) ) {
1998-04-08 21:49:02 +02:00
if( mpi_cmp( pkc->d.rsa.n , skc->d.rsa.n ) )
1998-01-13 20:04:23 +01:00
return -1;
1998-04-08 21:49:02 +02:00
if( mpi_cmp( pkc->d.rsa.e , skc->d.rsa.e ) )
1998-01-13 20:04:23 +01:00
return -1;
}
return 0;
}
1997-11-18 15:06:00 +01:00
1998-02-17 21:48:52 +01:00
int
cmp_user_ids( PKT_user_id *a, PKT_user_id *b )
{
int res;
res = a->len - b->len;
if( !res )
res = memcmp( a->name, b->name, a->len );
return res;
}