1997-11-18 15:06:00 +01:00
|
|
|
/* mainproc.c - handle 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>
|
1997-12-01 11:33:23 +01:00
|
|
|
#include <assert.h>
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
#include "packet.h"
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
#include "keydb.h"
|
1997-11-24 12:04:11 +01:00
|
|
|
#include "filter.h"
|
1997-12-01 11:33:23 +01:00
|
|
|
#include "cipher.h"
|
1997-11-24 23:24:04 +01:00
|
|
|
#include "main.h"
|
1998-01-30 17:23:16 +01:00
|
|
|
#include "status.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1997-11-27 12:44:13 +01:00
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
/****************
|
|
|
|
* Structure to hold the context
|
|
|
|
*/
|
|
|
|
|
1997-11-27 12:44:13 +01:00
|
|
|
typedef struct {
|
1997-12-01 11:33:23 +01:00
|
|
|
PKT_public_cert *last_pubkey;
|
|
|
|
PKT_secret_cert *last_seckey;
|
1997-11-27 12:44:13 +01:00
|
|
|
PKT_user_id *last_user_id;
|
|
|
|
md_filter_context_t mfx;
|
|
|
|
DEK *dek;
|
|
|
|
int last_was_pubkey_enc;
|
1998-02-17 21:48:52 +01:00
|
|
|
KBNODE list; /* the current list of packets */
|
1997-12-03 11:20:03 +01:00
|
|
|
int have_data;
|
1997-12-23 18:30:18 +01:00
|
|
|
IOBUF iobuf; /* used to get the filename etc. */
|
1997-11-27 12:44:13 +01:00
|
|
|
} *CTX;
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-12-12 13:03:58 +01:00
|
|
|
static void list_node( CTX c, KBNODE node );
|
|
|
|
static void proc_tree( CTX c, KBNODE node );
|
1997-12-01 11:33:23 +01:00
|
|
|
|
|
|
|
|
1997-11-26 23:02:28 +01:00
|
|
|
static void
|
1998-02-17 21:48:52 +01:00
|
|
|
release_list( CTX c )
|
1997-11-26 23:02:28 +01:00
|
|
|
{
|
1998-02-17 21:48:52 +01:00
|
|
|
if( !c->list )
|
1997-12-01 11:33:23 +01:00
|
|
|
return;
|
1998-02-17 21:48:52 +01:00
|
|
|
proc_tree(c, c->list );
|
|
|
|
release_kbnode( c->list );
|
|
|
|
c->list = NULL;
|
1997-11-26 23:02:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-02 20:36:53 +01:00
|
|
|
static int
|
|
|
|
add_onepass_sig( CTX c, PACKET *pkt )
|
|
|
|
{
|
1997-12-12 13:03:58 +01:00
|
|
|
KBNODE node;
|
1997-12-03 11:20:03 +01:00
|
|
|
|
1998-02-17 21:48:52 +01:00
|
|
|
if( c->list ) { /* add another packet */
|
|
|
|
if( c->list->pkt->pkttype != PKT_ONEPASS_SIG ) {
|
1997-12-02 20:36:53 +01:00
|
|
|
log_error("add_onepass_sig: another packet is in the way\n");
|
1998-02-17 21:48:52 +01:00
|
|
|
release_list( c );
|
1997-12-02 20:36:53 +01:00
|
|
|
}
|
1998-02-17 21:48:52 +01:00
|
|
|
add_kbnode( c->list, new_kbnode( pkt ));
|
1997-12-02 20:36:53 +01:00
|
|
|
}
|
|
|
|
else /* insert the first one */
|
1998-02-17 21:48:52 +01:00
|
|
|
c->list = node = new_kbnode( pkt );
|
1997-12-03 11:20:03 +01:00
|
|
|
|
1997-12-02 20:36:53 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
add_public_cert( CTX c, PACKET *pkt )
|
|
|
|
{
|
1998-02-17 21:48:52 +01:00
|
|
|
release_list( c );
|
|
|
|
c->list = new_kbnode( pkt );
|
1997-12-01 11:33:23 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_secret_cert( CTX c, PACKET *pkt )
|
|
|
|
{
|
1998-02-17 21:48:52 +01:00
|
|
|
release_list( c );
|
|
|
|
c->list = new_kbnode( pkt );
|
1997-12-01 11:33:23 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_user_id( CTX c, PACKET *pkt )
|
1997-11-27 12:44:13 +01:00
|
|
|
{
|
1998-02-17 21:48:52 +01:00
|
|
|
if( !c->list ) {
|
1997-12-01 11:33:23 +01:00
|
|
|
log_error("orphaned user id\n" );
|
|
|
|
return 0;
|
1997-11-27 12:44:13 +01:00
|
|
|
}
|
1998-02-17 21:48:52 +01:00
|
|
|
add_kbnode( c->list, new_kbnode( pkt ) );
|
1997-12-01 11:33:23 +01:00
|
|
|
return 1;
|
1997-11-27 12:44:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
static int
|
|
|
|
add_signature( CTX c, PACKET *pkt )
|
1997-11-27 12:44:13 +01:00
|
|
|
{
|
1998-02-12 00:22:09 +01:00
|
|
|
KBNODE node;
|
1997-11-27 12:44:13 +01:00
|
|
|
|
1998-02-17 21:48:52 +01:00
|
|
|
if( pkt->pkttype == PKT_SIGNATURE && !c->list ) {
|
1998-01-30 17:23:16 +01:00
|
|
|
/* This is the first signature for a following datafile.
|
|
|
|
* G10 does not write such packets, instead it always uses
|
|
|
|
* onepass-sig packets. The drawback of PGP's method
|
1998-02-11 04:25:44 +01:00
|
|
|
* of prepending the signtaure to the data is,
|
|
|
|
* that it is not possible to make a signature from data read
|
1998-02-12 00:22:09 +01:00
|
|
|
* from stdin. (Anyway, G10 is able to read these stuff) */
|
1998-01-30 17:23:16 +01:00
|
|
|
node = new_kbnode( pkt );
|
1998-02-17 21:48:52 +01:00
|
|
|
c->list = node;
|
1998-01-30 17:23:16 +01:00
|
|
|
return 1;
|
1997-11-27 12:44:13 +01:00
|
|
|
}
|
1998-02-17 21:48:52 +01:00
|
|
|
else if( !c->list )
|
1998-02-12 00:22:09 +01:00
|
|
|
return 0; /* oops (invalid packet sequence)*/
|
1998-02-17 21:48:52 +01:00
|
|
|
else if( !c->list->pkt )
|
1998-02-12 00:22:09 +01:00
|
|
|
BUG(); /* so nicht */
|
1998-01-30 17:23:16 +01:00
|
|
|
|
1998-02-12 00:22:09 +01:00
|
|
|
/* add a new signature node id at the end */
|
1997-12-12 13:03:58 +01:00
|
|
|
node = new_kbnode( pkt );
|
1998-02-17 21:48:52 +01:00
|
|
|
add_kbnode( c->list, node );
|
1997-12-01 11:33:23 +01:00
|
|
|
return 1;
|
1997-11-27 12:44:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
proc_pubkey_enc( CTX c, PACKET *pkt )
|
|
|
|
{
|
|
|
|
PKT_pubkey_enc *enc;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
c->last_was_pubkey_enc = 1;
|
|
|
|
enc = pkt->pkt.pubkey_enc;
|
1998-01-02 21:40:10 +01:00
|
|
|
/*printf("enc: encrypted by a pubkey with keyid %08lX\n", enc->keyid[1] );*/
|
1997-11-27 12:44:13 +01:00
|
|
|
if( enc->pubkey_algo == PUBKEY_ALGO_ELGAMAL
|
|
|
|
|| enc->pubkey_algo == PUBKEY_ALGO_RSA ) {
|
|
|
|
m_free(c->dek ); /* paranoid: delete a pending DEK */
|
|
|
|
c->dek = m_alloc_secure( sizeof *c->dek );
|
|
|
|
if( (result = get_session_key( enc, c->dek )) ) {
|
|
|
|
/* error: delete the DEK */
|
|
|
|
m_free(c->dek); c->dek = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = G10ERR_PUBKEY_ALGO;
|
|
|
|
|
|
|
|
if( result == -1 )
|
|
|
|
;
|
1998-01-02 21:40:10 +01:00
|
|
|
else if( !result ) {
|
|
|
|
if( opt.verbose > 1 )
|
|
|
|
log_info( "pubkey_enc packet: Good DEK\n" );
|
|
|
|
}
|
1997-11-27 12:44:13 +01:00
|
|
|
else
|
1998-01-02 21:40:10 +01:00
|
|
|
log_error( "pubkey_enc packet: %s\n", g10_errstr(result));
|
1997-11-27 12:44:13 +01:00
|
|
|
free_packet(pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1997-12-01 11:33:23 +01:00
|
|
|
proc_encrypted( CTX c, PACKET *pkt )
|
1997-11-27 12:44:13 +01:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
1998-01-02 21:40:10 +01:00
|
|
|
/*printf("dat: %sencrypted data\n", c->dek?"":"conventional ");*/
|
1997-11-27 12:44:13 +01:00
|
|
|
if( !c->dek && !c->last_was_pubkey_enc ) {
|
|
|
|
/* assume this is conventional encrypted data */
|
|
|
|
c->dek = m_alloc_secure( sizeof *c->dek );
|
1998-01-07 21:47:46 +01:00
|
|
|
c->dek->algo = opt.def_cipher_algo;
|
1998-02-24 19:50:46 +01:00
|
|
|
result = make_dek_from_passphrase( c->dek, 0, NULL );
|
1997-11-27 12:44:13 +01:00
|
|
|
}
|
|
|
|
else if( !c->dek )
|
|
|
|
result = G10ERR_NO_SECKEY;
|
|
|
|
if( !result )
|
1997-12-01 11:33:23 +01:00
|
|
|
result = decrypt_data( pkt->pkt.encrypted, c->dek );
|
1997-11-27 12:44:13 +01:00
|
|
|
m_free(c->dek); c->dek = NULL;
|
|
|
|
if( result == -1 )
|
|
|
|
;
|
1998-01-02 21:40:10 +01:00
|
|
|
else if( !result ) {
|
|
|
|
if( opt.verbose > 1 )
|
|
|
|
log_info("encryption okay\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("encryption failed: %s\n", g10_errstr(result));
|
|
|
|
}
|
1997-11-27 12:44:13 +01:00
|
|
|
free_packet(pkt);
|
|
|
|
c->last_was_pubkey_enc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
proc_plaintext( CTX c, PACKET *pkt )
|
|
|
|
{
|
|
|
|
PKT_plaintext *pt = pkt->pkt.plaintext;
|
1998-01-02 21:40:10 +01:00
|
|
|
int rc;
|
1997-11-27 12:44:13 +01:00
|
|
|
|
1998-01-02 21:40:10 +01:00
|
|
|
if( opt.verbose )
|
|
|
|
log_info("original file name='%.*s'\n", pt->namelen, pt->name);
|
1997-11-27 12:44:13 +01:00
|
|
|
free_md_filter_context( &c->mfx );
|
1997-12-23 18:30:18 +01:00
|
|
|
/* fixme: take the digest algo(s) to use from the
|
|
|
|
* onepass_sig packet (if we have these)
|
|
|
|
* And look at the sigclass to check wether we should use the
|
|
|
|
* textmode filter (sigclass 0x01)
|
|
|
|
*/
|
1998-01-12 11:18:17 +01:00
|
|
|
c->mfx.md = md_open( DIGEST_ALGO_RMD160, 0);
|
1998-02-04 19:54:31 +01:00
|
|
|
md_enable( c->mfx.md, DIGEST_ALGO_MD5 );
|
1998-01-02 21:40:10 +01:00
|
|
|
rc = handle_plaintext( pt, &c->mfx );
|
|
|
|
if( rc )
|
|
|
|
log_error( "handle plaintext failed: %s\n", g10_errstr(rc));
|
1997-11-27 12:44:13 +01:00
|
|
|
free_packet(pkt);
|
|
|
|
c->last_was_pubkey_enc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1997-12-01 11:33:23 +01:00
|
|
|
proc_compressed( CTX c, PACKET *pkt )
|
1997-11-27 12:44:13 +01:00
|
|
|
{
|
|
|
|
PKT_compressed *zd = pkt->pkt.compressed;
|
1998-01-02 21:40:10 +01:00
|
|
|
int rc;
|
1997-11-27 12:44:13 +01:00
|
|
|
|
1998-01-02 21:40:10 +01:00
|
|
|
/*printf("zip: compressed data packet\n");*/
|
|
|
|
rc = handle_compressed( zd );
|
|
|
|
if( rc )
|
|
|
|
log_error("uncompressing failed: %s\n", g10_errstr(rc));
|
1997-11-27 12:44:13 +01:00
|
|
|
free_packet(pkt);
|
|
|
|
c->last_was_pubkey_enc = 0;
|
|
|
|
}
|
1997-11-26 23:02:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* check the signature
|
|
|
|
* Returns: 0 = valid signature or an error code
|
|
|
|
*/
|
|
|
|
static int
|
1997-12-12 13:03:58 +01:00
|
|
|
do_check_sig( CTX c, KBNODE node )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
|
|
|
PKT_signature *sig;
|
1998-01-12 11:18:17 +01:00
|
|
|
MD_HANDLE md;
|
1997-12-01 11:33:23 +01:00
|
|
|
int algo, rc;
|
|
|
|
|
|
|
|
assert( node->pkt->pkttype == PKT_SIGNATURE );
|
|
|
|
sig = node->pkt->pkt.signature;
|
|
|
|
|
1998-02-18 14:58:46 +01:00
|
|
|
algo = digest_algo_from_sig( sig );
|
|
|
|
if( !algo )
|
1997-12-01 11:33:23 +01:00
|
|
|
return G10ERR_PUBKEY_ALGO;
|
1998-01-12 11:18:17 +01:00
|
|
|
if( (rc=check_digest_algo(algo)) )
|
1997-12-01 11:33:23 +01:00
|
|
|
return rc;
|
|
|
|
|
1997-12-03 11:20:03 +01:00
|
|
|
if( sig->sig_class == 0x00 ) {
|
1998-02-27 18:51:28 +01:00
|
|
|
if( c->mfx.md )
|
|
|
|
md = md_copy( c->mfx.md );
|
|
|
|
else /* detached signature */
|
|
|
|
md = md_open( 0, 0 ); /* signature_check() will enable the md*/
|
1997-12-03 11:20:03 +01:00
|
|
|
}
|
1998-02-04 19:54:31 +01:00
|
|
|
else if( sig->sig_class == 0x01 ) {
|
|
|
|
/* how do we know that we have to hash the (already hashed) text
|
|
|
|
* in canonical mode ??? (calculating both modes???) */
|
1998-02-27 18:51:28 +01:00
|
|
|
if( c->mfx.md )
|
|
|
|
md = md_copy( c->mfx.md );
|
|
|
|
else /* detached signature */
|
|
|
|
md = md_open( 0, 0 ); /* signature_check() will enable the md*/
|
1998-02-04 19:54:31 +01:00
|
|
|
}
|
1998-02-18 14:58:46 +01:00
|
|
|
else if( (sig->sig_class&~3) == 0x10
|
|
|
|
|| sig->sig_class == 0x20
|
|
|
|
|| sig->sig_class == 0x30 ) { /* classes 0x10..0x13,0x20,0x30 */
|
1998-02-17 21:48:52 +01:00
|
|
|
if( c->list->pkt->pkttype == PKT_PUBLIC_CERT ) {
|
1998-02-18 14:58:46 +01:00
|
|
|
#if 0
|
|
|
|
KBNODE n1;
|
|
|
|
|
|
|
|
if( sig->sig_class == 0x20 ) {
|
|
|
|
md = md_open( algo, 0 );
|
|
|
|
hash_public_cert( md, c->list->pkt->pkt.public_cert );
|
|
|
|
}
|
|
|
|
else if( (n1=find_prev_kbnode( c->list, node, PKT_USER_ID )) ) {
|
|
|
|
md = md_open( algo, 0 );
|
|
|
|
hash_public_cert( md, c->list->pkt->pkt.public_cert );
|
|
|
|
if( sig->sig_class != 0x20 )
|
|
|
|
md_write( md, n1->pkt->pkt.user_id->name,
|
|
|
|
n1->pkt->pkt.user_id->len);
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
else {
|
1998-02-18 14:58:46 +01:00
|
|
|
log_error("invalid parent packet for sigclass %02x\n",
|
|
|
|
sig->sig_class);
|
1997-12-01 11:33:23 +01:00
|
|
|
return G10ERR_SIG_CLASS;
|
|
|
|
}
|
1998-02-18 14:58:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return check_key_signature( c->list, node, NULL );
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
else {
|
1998-02-18 14:58:46 +01:00
|
|
|
log_error("invalid root packet for sigclass %02x\n",
|
|
|
|
sig->sig_class);
|
1997-12-01 11:33:23 +01:00
|
|
|
return G10ERR_SIG_CLASS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return G10ERR_SIG_CLASS;
|
|
|
|
rc = signature_check( sig, md );
|
|
|
|
md_close(md);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_userid( PACKET *pkt )
|
|
|
|
{
|
|
|
|
if( !pkt )
|
1998-01-16 22:15:24 +01:00
|
|
|
BUG();
|
1997-12-01 11:33:23 +01:00
|
|
|
if( pkt->pkttype != PKT_USER_ID ) {
|
|
|
|
printf("ERROR: unexpected packet type %d", pkt->pkttype );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
print_string( stdout, pkt->pkt.user_id->name, pkt->pkt.user_id->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-09 13:46:23 +01:00
|
|
|
static void
|
|
|
|
print_fingerprint( PKT_public_cert *pkc, PKT_secret_cert *skc )
|
|
|
|
{
|
|
|
|
byte *array, *p;
|
|
|
|
size_t i, n;
|
|
|
|
|
|
|
|
p = array = skc? fingerprint_from_skc( skc, &n )
|
|
|
|
: fingerprint_from_pkc( pkc, &n );
|
|
|
|
printf(" Key fingerprint =");
|
|
|
|
if( n == 20 ) {
|
|
|
|
for(i=0; i < n ; i++, i++, p += 2 ) {
|
|
|
|
if( i == 10 )
|
|
|
|
putchar(' ');
|
|
|
|
printf(" %02X%02X", *p, p[1] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(i=0; i < n ; i++, p++ ) {
|
|
|
|
if( i && !(i%8) )
|
|
|
|
putchar(' ');
|
|
|
|
printf(" %02X", *p );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
m_free(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
/****************
|
|
|
|
* List the certificate in a user friendly way
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
1997-12-12 13:03:58 +01:00
|
|
|
list_node( CTX c, KBNODE node )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-02-11 04:25:44 +01:00
|
|
|
int any=0;
|
1997-12-01 11:33:23 +01:00
|
|
|
|
|
|
|
if( !node )
|
|
|
|
;
|
|
|
|
else if( node->pkt->pkttype == PKT_PUBLIC_CERT ) {
|
|
|
|
PKT_public_cert *pkc = node->pkt->pkt.public_cert;
|
|
|
|
|
|
|
|
printf("pub %4u%c/%08lX %s ", nbits_from_pkc( pkc ),
|
|
|
|
pubkey_letter( pkc->pubkey_algo ),
|
|
|
|
(ulong)keyid_from_pkc( pkc, NULL ),
|
|
|
|
datestr_from_pkc( pkc ) );
|
1998-02-11 04:25:44 +01:00
|
|
|
/* and now list all userids with their signatures */
|
1998-02-17 21:48:52 +01:00
|
|
|
for( node = node->next; node; node = node->next ) {
|
1998-02-18 14:58:46 +01:00
|
|
|
if( any != 2 && node->pkt->pkttype == PKT_SIGNATURE ) {
|
|
|
|
if( !any )
|
|
|
|
putchar('\n');
|
|
|
|
list_node(c, node );
|
|
|
|
any = 1;
|
|
|
|
}
|
|
|
|
else if( node->pkt->pkttype == PKT_USER_ID ) {
|
1998-02-17 21:48:52 +01:00
|
|
|
KBNODE n;
|
|
|
|
|
|
|
|
if( any )
|
|
|
|
printf( "%*s", 31, "" );
|
|
|
|
print_userid( node->pkt );
|
|
|
|
putchar('\n');
|
|
|
|
if( opt.fingerprint && !any )
|
|
|
|
print_fingerprint( pkc, NULL );
|
|
|
|
for( n=node->next; n; n = n->next ) {
|
|
|
|
if( n->pkt->pkttype == PKT_USER_ID )
|
|
|
|
break;
|
|
|
|
if( n->pkt->pkttype == PKT_SIGNATURE )
|
|
|
|
list_node(c, n );
|
|
|
|
}
|
1998-02-18 14:58:46 +01:00
|
|
|
any=2;
|
1998-02-17 21:48:52 +01:00
|
|
|
}
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
1998-02-18 14:58:46 +01:00
|
|
|
if( any != 2 )
|
1998-02-11 04:25:44 +01:00
|
|
|
printf("ERROR: no user id!\n");
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
else if( node->pkt->pkttype == PKT_SECRET_CERT ) {
|
|
|
|
PKT_secret_cert *skc = node->pkt->pkt.secret_cert;
|
|
|
|
|
|
|
|
printf("sec %4u%c/%08lX %s ", nbits_from_skc( skc ),
|
|
|
|
pubkey_letter( skc->pubkey_algo ),
|
|
|
|
(ulong)keyid_from_skc( skc, NULL ),
|
|
|
|
datestr_from_skc( skc ) );
|
1998-02-11 04:25:44 +01:00
|
|
|
/* and now list all userids */
|
|
|
|
while( (node = find_next_kbnode(node, PKT_USER_ID)) ) {
|
|
|
|
print_userid( node->pkt );
|
1997-12-09 13:46:23 +01:00
|
|
|
putchar('\n');
|
1998-02-11 04:25:44 +01:00
|
|
|
if( opt.fingerprint && !any )
|
1997-12-09 13:46:23 +01:00
|
|
|
print_fingerprint( NULL, skc );
|
1998-02-11 04:25:44 +01:00
|
|
|
any=1;
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
1998-02-11 04:25:44 +01:00
|
|
|
if( !any )
|
|
|
|
printf("ERROR: no user id!\n");
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
1997-12-09 13:46:23 +01:00
|
|
|
else if( node->pkt->pkttype == PKT_SIGNATURE ) {
|
1997-12-01 11:33:23 +01:00
|
|
|
PKT_signature *sig = node->pkt->pkt.signature;
|
1998-02-17 21:48:52 +01:00
|
|
|
int rc2=0;
|
1997-12-01 11:33:23 +01:00
|
|
|
size_t n;
|
|
|
|
char *p;
|
|
|
|
int sigrc = ' ';
|
|
|
|
|
1997-12-09 13:46:23 +01:00
|
|
|
if( !opt.list_sigs )
|
|
|
|
return;
|
|
|
|
|
1998-02-18 14:58:46 +01:00
|
|
|
if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
|
|
|
|
fputs("rev", stdout);
|
|
|
|
else
|
|
|
|
fputs("sig", stdout);
|
1997-12-01 11:33:23 +01:00
|
|
|
if( opt.check_sigs ) {
|
1997-12-23 18:30:18 +01:00
|
|
|
fflush(stdout);
|
1997-12-01 11:33:23 +01:00
|
|
|
switch( (rc2=do_check_sig( c, node )) ) {
|
|
|
|
case 0: sigrc = '!'; break;
|
|
|
|
case G10ERR_BAD_SIGN: sigrc = '-'; break;
|
|
|
|
case G10ERR_NO_PUBKEY: sigrc = '?'; break;
|
|
|
|
default: sigrc = '%'; break;
|
|
|
|
}
|
|
|
|
}
|
1997-12-23 18:30:18 +01:00
|
|
|
printf("%c %08lX %s ",
|
1998-02-03 13:09:20 +01:00
|
|
|
sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
|
1997-12-01 11:33:23 +01:00
|
|
|
if( sigrc == '%' )
|
|
|
|
printf("[%s] ", g10_errstr(rc2) );
|
|
|
|
else if( sigrc == '?' )
|
|
|
|
;
|
|
|
|
else {
|
|
|
|
p = get_user_id( sig->keyid, &n );
|
|
|
|
print_string( stdout, p, n );
|
|
|
|
m_free(p);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log_error("invalid node with packet of type %d\n", node->pkt->pkttype);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-26 23:02:28 +01:00
|
|
|
int
|
|
|
|
proc_packets( IOBUF a )
|
|
|
|
{
|
1997-11-27 12:44:13 +01:00
|
|
|
CTX c = m_alloc_clear( sizeof *c );
|
|
|
|
PACKET *pkt = m_alloc( sizeof *pkt );
|
1998-01-16 22:15:24 +01:00
|
|
|
int rc;
|
1997-12-01 11:33:23 +01:00
|
|
|
int newpkt;
|
1997-11-26 23:02:28 +01:00
|
|
|
|
1997-12-23 18:30:18 +01:00
|
|
|
c->iobuf = a;
|
1997-11-26 23:02:28 +01:00
|
|
|
init_packet(pkt);
|
|
|
|
while( (rc=parse_packet(a, pkt)) != -1 ) {
|
|
|
|
/* cleanup if we have an illegal data structure */
|
1997-12-01 11:33:23 +01:00
|
|
|
if( c->dek && pkt->pkttype != PKT_ENCRYPTED ) {
|
1997-11-26 23:02:28 +01:00
|
|
|
log_error("oops: valid pubkey enc packet not followed by data\n");
|
1997-11-27 12:44:13 +01:00
|
|
|
m_free(c->dek); c->dek = NULL; /* burn it */
|
1997-11-26 23:02:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( rc ) {
|
|
|
|
free_packet(pkt);
|
1997-12-23 18:30:18 +01:00
|
|
|
if( rc == G10ERR_INVALID_PACKET )
|
|
|
|
break;
|
1997-11-26 23:02:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
1997-12-01 11:33:23 +01:00
|
|
|
newpkt = -1;
|
1998-01-02 21:40:10 +01:00
|
|
|
if( opt.list_packets ) {
|
|
|
|
switch( pkt->pkttype ) {
|
|
|
|
case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
|
|
|
|
case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
|
|
|
|
case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
|
|
|
|
default: newpkt = 0; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch( pkt->pkttype ) {
|
|
|
|
case PKT_PUBLIC_CERT: newpkt = add_public_cert( c, pkt ); break;
|
|
|
|
case PKT_SECRET_CERT: newpkt = add_secret_cert( c, pkt ); break;
|
|
|
|
case PKT_USER_ID: newpkt = add_user_id( c, pkt ); break;
|
|
|
|
case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
|
|
|
|
case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
|
|
|
|
case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
|
|
|
|
case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
|
|
|
|
case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
|
|
|
|
case PKT_ONEPASS_SIG: newpkt = add_onepass_sig( c, pkt ); break;
|
|
|
|
default: newpkt = 0; break;
|
|
|
|
}
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
1997-12-03 11:20:03 +01:00
|
|
|
if( pkt->pkttype != PKT_SIGNATURE )
|
|
|
|
c->have_data = pkt->pkttype == PKT_PLAINTEXT;
|
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
if( newpkt == -1 )
|
|
|
|
;
|
|
|
|
else if( newpkt ) {
|
|
|
|
pkt = m_alloc( sizeof *pkt );
|
|
|
|
init_packet(pkt);
|
1997-11-26 23:02:28 +01:00
|
|
|
}
|
1997-12-01 11:33:23 +01:00
|
|
|
else
|
|
|
|
free_packet(pkt);
|
1997-11-26 23:02:28 +01:00
|
|
|
}
|
|
|
|
|
1998-02-17 21:48:52 +01:00
|
|
|
release_list( c );
|
1997-11-27 12:44:13 +01:00
|
|
|
m_free(c->dek);
|
1997-11-26 23:02:28 +01:00
|
|
|
free_packet( pkt );
|
|
|
|
m_free( pkt );
|
1997-11-27 12:44:13 +01:00
|
|
|
free_md_filter_context( &c->mfx );
|
|
|
|
m_free( c );
|
1997-11-26 23:02:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
|
1997-12-02 20:36:53 +01:00
|
|
|
static void
|
|
|
|
print_keyid( FILE *fp, u32 *keyid )
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
char *p = get_user_id( keyid, &n );
|
|
|
|
print_string( fp, p, n );
|
|
|
|
m_free(p);
|
|
|
|
}
|
|
|
|
|
1998-01-30 17:23:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_sig_and_print( CTX c, KBNODE node )
|
|
|
|
{
|
|
|
|
PKT_signature *sig = node->pkt->pkt.signature;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = do_check_sig(c, node );
|
|
|
|
if( !rc ) {
|
|
|
|
write_status( STATUS_GOODSIG );
|
|
|
|
log_info("Good signature from ");
|
|
|
|
print_keyid( stderr, sig->keyid );
|
|
|
|
putc('\n', stderr);
|
|
|
|
}
|
|
|
|
else if( rc == G10ERR_BAD_SIGN ) {
|
|
|
|
write_status( STATUS_BADSIG );
|
|
|
|
log_error("BAD signature from ");
|
|
|
|
print_keyid( stderr, sig->keyid );
|
|
|
|
putc('\n', stderr);
|
|
|
|
if( opt.batch )
|
|
|
|
g10_exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
write_status( STATUS_ERRSIG );
|
|
|
|
log_error("Can't check signature made by %08lX: %s\n",
|
1998-02-03 13:09:20 +01:00
|
|
|
(ulong)sig->keyid[1], g10_errstr(rc) );
|
1998-01-30 17:23:16 +01:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-02 20:36:53 +01:00
|
|
|
/****************
|
1997-12-23 18:30:18 +01:00
|
|
|
* Process the tree which starts at node
|
1997-12-02 20:36:53 +01:00
|
|
|
*/
|
|
|
|
static void
|
1997-12-12 13:03:58 +01:00
|
|
|
proc_tree( CTX c, KBNODE node )
|
1997-12-02 20:36:53 +01:00
|
|
|
{
|
1997-12-12 13:03:58 +01:00
|
|
|
KBNODE n1;
|
1997-12-02 20:36:53 +01:00
|
|
|
int rc;
|
|
|
|
|
1998-01-02 21:40:10 +01:00
|
|
|
if( opt.list_packets )
|
|
|
|
return;
|
|
|
|
|
1997-12-02 20:36:53 +01:00
|
|
|
if( node->pkt->pkttype == PKT_PUBLIC_CERT )
|
|
|
|
list_node( c, node );
|
|
|
|
else if( node->pkt->pkttype == PKT_SECRET_CERT )
|
|
|
|
list_node( c, node );
|
|
|
|
else if( node->pkt->pkttype == PKT_ONEPASS_SIG ) {
|
1998-02-11 04:25:44 +01:00
|
|
|
/* check all signatures */
|
|
|
|
if( !c->have_data ) {
|
|
|
|
free_md_filter_context( &c->mfx );
|
|
|
|
/* prepare to create all requested message digests */
|
|
|
|
c->mfx.md = md_open(0, 0);
|
|
|
|
for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) {
|
|
|
|
md_enable( c->mfx.md,
|
|
|
|
digest_algo_from_sig(n1->pkt->pkt.signature));
|
|
|
|
}
|
|
|
|
/* ask for file and hash it */
|
|
|
|
rc = ask_for_detached_datafile( &c->mfx,
|
|
|
|
iobuf_get_fname(c->iobuf));
|
|
|
|
if( rc ) {
|
|
|
|
log_error("can't hash datafile: %s\n", g10_errstr(rc));
|
|
|
|
return;
|
1997-12-03 11:20:03 +01:00
|
|
|
}
|
1997-12-02 20:36:53 +01:00
|
|
|
}
|
1998-02-11 04:25:44 +01:00
|
|
|
|
|
|
|
for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); )
|
|
|
|
check_sig_and_print( c, n1 );
|
1997-12-02 20:36:53 +01:00
|
|
|
}
|
|
|
|
else if( node->pkt->pkttype == PKT_SIGNATURE ) {
|
1998-01-30 17:23:16 +01:00
|
|
|
PKT_signature *sig = node->pkt->pkt.signature;
|
|
|
|
|
1998-02-27 18:51:28 +01:00
|
|
|
if( !c->have_data ) {
|
|
|
|
free_md_filter_context( &c->mfx );
|
|
|
|
c->mfx.md = md_open(digest_algo_from_sig(sig), 0);
|
|
|
|
rc = ask_for_detached_datafile( &c->mfx,
|
|
|
|
iobuf_get_fname(c->iobuf));
|
|
|
|
if( rc ) {
|
|
|
|
log_error("can't hash datafile: %s\n", g10_errstr(rc));
|
|
|
|
return;
|
1998-01-30 17:23:16 +01:00
|
|
|
}
|
|
|
|
}
|
1998-02-27 18:51:28 +01:00
|
|
|
else
|
|
|
|
log_info("old style signature\n");
|
1998-01-30 17:23:16 +01:00
|
|
|
|
|
|
|
check_sig_and_print( c, node );
|
1997-12-02 20:36:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
log_error("proc_tree: invalid root packet\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|