diff options
Diffstat (limited to 'g10/mainproc.c')
-rw-r--r-- | g10/mainproc.c | 833 |
1 files changed, 580 insertions, 253 deletions
diff --git a/g10/mainproc.c b/g10/mainproc.c index 7b04b3e6f..bcd1c1c01 100644 --- a/g10/mainproc.c +++ b/g10/mainproc.c @@ -1,5 +1,5 @@ /* mainproc.c - handle packets - * Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -25,18 +25,19 @@ #include <assert.h> #include <time.h> -#include <gcrypt.h> #include "packet.h" #include "iobuf.h" +#include "memory.h" #include "options.h" #include "util.h" +#include "cipher.h" #include "keydb.h" #include "filter.h" #include "main.h" #include "status.h" #include "i18n.h" #include "trustdb.h" -#include "hkp.h" +#include "keyserver-internal.h" struct kidlist_item { @@ -69,8 +70,11 @@ struct mainproc_context { IOBUF iobuf; /* used to get the filename etc. */ int trustletter; /* temp usage in list_node */ ulong local_id; /* ditto */ - struct kidlist_item *failed_pkenc; /* list of packets for which - we do not have a secret key */ + struct kidlist_item *pkenc_list; /* list of encryption packets */ + struct { + int op; + int stop_now; + } pipemode; }; @@ -87,13 +91,18 @@ release_list( CTX c ) return; proc_tree(c, c->list ); release_kbnode( c->list ); - while( c->failed_pkenc ) { - struct kidlist_item *tmp = c->failed_pkenc->next; - gcry_free( c->failed_pkenc ); - c->failed_pkenc = tmp; + while( c->pkenc_list ) { + struct kidlist_item *tmp = c->pkenc_list->next; + m_free( c->pkenc_list ); + c->pkenc_list = tmp; } - c->failed_pkenc = NULL; + c->pkenc_list = NULL; c->list = NULL; + c->have_data = 0; + c->last_was_session_key = 0; + c->pipemode.op = 0; + c->pipemode.stop_now = 0; + m_free(c->dek); c->dek = NULL; } @@ -103,8 +112,14 @@ add_onepass_sig( CTX c, PACKET *pkt ) KBNODE node; if( c->list ) { /* add another packet */ - if( c->list->pkt->pkttype != PKT_ONEPASS_SIG ) { - log_error("add_onepass_sig: another packet is in the way\n"); + /* We can only append another onepass packet if the list + * does contain only onepass packets */ + for( node=c->list; node && node->pkt->pkttype == PKT_ONEPASS_SIG; + node = node->next ) + ; + if( node ) { + /* this is not the case, so we flush the current thing and + * allow this packet to start a new verification thing */ release_list( c ); c->list = new_kbnode( pkt ); } @@ -118,6 +133,48 @@ add_onepass_sig( CTX c, PACKET *pkt ) } +static int +add_gpg_control( CTX c, PACKET *pkt ) +{ + if ( pkt->pkt.gpg_control->control == CTRLPKT_CLEARSIGN_START ) { + /* New clear text signature. + * Process the last one and reset everything */ + release_list(c); + } + else if ( pkt->pkt.gpg_control->control == CTRLPKT_PIPEMODE ) { + /* Pipemode control packet */ + if ( pkt->pkt.gpg_control->datalen < 2 ) + log_fatal ("invalid pipemode control packet length\n"); + if (pkt->pkt.gpg_control->data[0] == 1) { + /* start the whole thing */ + assert ( !c->list ); /* we should be in a pretty virgin state */ + assert ( !c->pipemode.op ); + c->pipemode.op = pkt->pkt.gpg_control->data[1]; + } + else if (pkt->pkt.gpg_control->data[0] == 2) { + /* the signed material follows in a plaintext packet */ + assert ( c->pipemode.op == 'B' ); + } + else if (pkt->pkt.gpg_control->data[0] == 3) { + assert ( c->pipemode.op == 'B' ); + release_list (c); + /* and tell the outer loop to terminate */ + c->pipemode.stop_now = 1; + } + else + log_fatal ("invalid pipemode control packet code\n"); + return 0; /* no need to store the packet */ + } + + if( c->list ) /* add another packet */ + add_kbnode( c->list, new_kbnode( pkt )); + else /* insert the first one */ + c->list = new_kbnode( pkt ); + + return 1; +} + + static int add_user_id( CTX c, PACKET *pkt ) @@ -180,6 +237,32 @@ add_signature( CTX c, PACKET *pkt ) return 1; } +static void +symkey_decrypt_sesskey( DEK *dek, byte *sesskey, size_t slen ) +{ + CIPHER_HANDLE hd; + + if ( slen < 17 || slen > 33 ) { + log_error( "weird size for an encrypted session key (%d)\n", slen ); + return; + } + hd = cipher_open( dek->algo, CIPHER_MODE_CFB, 1 ); + cipher_setkey( hd, dek->key, dek->keylen ); + cipher_setiv( hd, NULL, 0 ); + cipher_decrypt( hd, sesskey, sesskey, slen ); + cipher_close( hd ); + /* check first byte (the cipher algo) */ + if ( sesskey[0] > 10 ) { + log_error( "invalid symkey algorithm detected (%d)\n", sesskey[0] ); + return; + } + /* now we replace the dek components with the real session key + to decrypt the contents of the sequencing packet. */ + dek->keylen = cipher_get_keylen( sesskey[0] ) / 8; + dek->algo = sesskey[0]; + memcpy( dek->key, sesskey + 1, dek->keylen ); + /*log_hexdump( "thekey", dek->key, dek->keylen );*/ +} static void proc_symkey_enc( CTX c, PACKET *pkt ) @@ -187,12 +270,28 @@ proc_symkey_enc( CTX c, PACKET *pkt ) PKT_symkey_enc *enc; enc = pkt->pkt.symkey_enc; - if( enc->seskeylen ) - log_error( "symkey_enc packet with session keys are not supported!\n"); + if (!enc) + log_error ("invalid symkey encrypted packet\n"); else { + int algo = enc->cipher_algo; + const char *s; + + s = cipher_algo_to_string (algo); + if( s ) + log_info(_("%s encrypted data\n"), s ); + else + log_info(_("encrypted with unknown algorithm %d\n"), algo ); + c->last_was_session_key = 2; - c->dek = passphrase_to_dek( NULL, 0, enc->cipher_algo, &enc->s2k, 0 ); + if ( opt.list_only ) + goto leave; + c->dek = passphrase_to_dek( NULL, 0, algo, &enc->s2k, 0, NULL ); + if (c->dek) + c->dek->algo_info_printed = 1; + if ( c->dek && enc->seskeylen ) + symkey_decrypt_sesskey( c->dek, enc->seskey, enc->seskeylen ); } +leave: free_packet(pkt); } @@ -218,42 +317,64 @@ proc_pubkey_enc( CTX c, PACKET *pkt ) write_status_text( STATUS_ENC_TO, buf ); } - - if( is_ELGAMAL(enc->pubkey_algo) - || enc->pubkey_algo == GCRY_PK_DSA + if( !opt.list_only && opt.override_session_key ) { + /* It does not make much sense to store the session key in + * secure memory because it has already been passed on the + * command line and the GCHQ knows about it */ + c->dek = m_alloc_clear( sizeof *c->dek ); + result = get_override_session_key ( c->dek, opt.override_session_key ); + if ( result ) { + m_free(c->dek); c->dek = NULL; + } + } + else if( is_ELGAMAL(enc->pubkey_algo) + || enc->pubkey_algo == PUBKEY_ALGO_DSA || is_RSA(enc->pubkey_algo) ) { if ( !c->dek && ((!enc->keyid[0] && !enc->keyid[1]) + || opt.try_all_secrets || !seckey_available( enc->keyid )) ) { if( opt.list_only ) result = -1; else { - c->dek = gcry_xmalloc_secure( sizeof *c->dek ); + c->dek = m_alloc_secure_clear( sizeof *c->dek ); if( (result = get_session_key( enc, c->dek )) ) { /* error: delete the DEK */ - gcry_free(c->dek); c->dek = NULL; + m_free(c->dek); c->dek = NULL; } } } else - result = GPGERR_NO_SECKEY; + result = G10ERR_NO_SECKEY; } else - result = GPGERR_PUBKEY_ALGO; + result = G10ERR_PUBKEY_ALGO; if( result == -1 ) ; - else if( !result ) { - if( opt.verbose > 1 ) - log_info( _("public key encrypted data: good DEK\n") ); - } - else { /* store it for later display */ - struct kidlist_item *x = gcry_xmalloc( sizeof *x ); - x->kid[0] = enc->keyid[0]; - x->kid[1] = enc->keyid[1]; - x->pubkey_algo = enc->pubkey_algo; - x->reason = result; - x->next = c->failed_pkenc; - c->failed_pkenc = x; + else { + if( !result ) { + if( opt.verbose > 1 ) + log_info( _("public key encrypted data: good DEK\n") ); + if ( opt.show_session_key ) { + int i; + char *buf = m_alloc ( c->dek->keylen*2 + 20 ); + sprintf ( buf, "%d:", c->dek->algo ); + for(i=0; i < c->dek->keylen; i++ ) + sprintf(buf+strlen(buf), "%02X", c->dek->key[i] ); + log_info( "session key: \"%s\"\n", buf ); + write_status_text ( STATUS_SESSION_KEY, buf ); + } + } + /* store it for later display */ + { + struct kidlist_item *x = m_alloc( sizeof *x ); + x->kid[0] = enc->keyid[0]; + x->kid[1] = enc->keyid[1]; + x->pubkey_algo = enc->pubkey_algo; + x->reason = result; + x->next = c->pkenc_list; + c->pkenc_list = x; + } } free_packet(pkt); } @@ -265,11 +386,19 @@ proc_pubkey_enc( CTX c, PACKET *pkt ) * not decrypt. */ static void -print_failed_pkenc( struct kidlist_item *list ) +print_pkenc_list( struct kidlist_item *list, int failed ) { for( ; list; list = list->next ) { - PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); - const char *algstr = gcry_pk_algo_name( list->pubkey_algo ); + PKT_public_key *pk; + const char *algstr; + + if ( failed && !list->reason ) + continue; + if ( !failed && list->reason ) + continue; + + algstr = pubkey_algo_to_string( list->pubkey_algo ); + pk = m_alloc_clear( sizeof *pk ); if( !algstr ) algstr = "[?]"; @@ -282,8 +411,8 @@ print_failed_pkenc( struct kidlist_item *list ) strtimestamp(pk->timestamp) ); fputs(" \"", log_stream() ); p = get_user_id( list->kid, &n ); - print_string( log_stream(), p, n, '"' ); - gcry_free(p); + print_utf8_string2 ( log_stream(), p, n, '"' ); + m_free(p); fputs("\"\n", log_stream() ); } else { @@ -292,8 +421,7 @@ print_failed_pkenc( struct kidlist_item *list ) } free_public_key( pk ); - if( list->reason == GPGERR_NO_SECKEY ) { - log_info(_("no secret key for decryption available\n")); + if( list->reason == G10ERR_NO_SECKEY ) { if( is_status_enabled() ) { char buf[20]; sprintf(buf,"%08lX%08lX", (ulong)list->kid[0], @@ -301,9 +429,9 @@ print_failed_pkenc( struct kidlist_item *list ) write_status_text( STATUS_NO_SECKEY, buf ); } } - else + else if (list->reason) log_error(_("public key decryption failed: %s\n"), - gpg_errstr(list->reason)); + g10_errstr(list->reason)); } } @@ -313,7 +441,10 @@ proc_encrypted( CTX c, PACKET *pkt ) { int result = 0; - print_failed_pkenc( c->failed_pkenc ); + if (!opt.quiet) { + print_pkenc_list ( c->pkenc_list, 1 ); + print_pkenc_list ( c->pkenc_list, 0 ); + } write_status( STATUS_BEGIN_DECRYPTION ); @@ -321,19 +452,43 @@ proc_encrypted( CTX c, PACKET *pkt ) if( opt.list_only ) result = -1; else if( !c->dek && !c->last_was_session_key ) { - /* assume this is old conventional encrypted data - * Actually we should use IDEA and MD5 in this case, but because - * IDEA is patented we can't do so */ - c->dek = passphrase_to_dek( NULL, 0, - opt.def_cipher_algo ? opt.def_cipher_algo - : DEFAULT_CIPHER_ALGO, NULL, 0 ); + int algo; + STRING2KEY s2kbuf, *s2k = NULL; + + /* assume this is old style conventional encrypted data */ + if ( (algo = opt.def_cipher_algo)) + log_info (_("assuming %s encrypted data\n"), + cipher_algo_to_string(algo)); + else if ( check_cipher_algo(CIPHER_ALGO_IDEA) ) { + algo = opt.def_cipher_algo; + if (!algo) + algo = opt.s2k_cipher_algo; + idea_cipher_warn(1); + log_info (_("IDEA cipher unavailable, " + "optimistically attempting to use %s instead\n"), + cipher_algo_to_string(algo)); + } + else { + algo = CIPHER_ALGO_IDEA; + if (!opt.def_digest_algo) { + /* If no digest is given we assume MD5 */ + s2kbuf.mode = 0; + s2kbuf.hash_algo = DIGEST_ALGO_MD5; + s2k = &s2kbuf; + } + log_info (_("assuming %s encrypted data\n"), "IDEA"); + } + + c->dek = passphrase_to_dek ( NULL, 0, algo, s2k, 0, NULL ); + if (c->dek) + c->dek->algo_info_printed = 1; } else if( !c->dek ) - result = GPGERR_NO_SECKEY; + result = G10ERR_NO_SECKEY; if( !result ) result = decrypt_data( c, pkt->pkt.encrypted, c->dek ); - gcry_free(c->dek); c->dek = NULL; + m_free(c->dek); c->dek = NULL; if( result == -1 ) ; else if( !result ) { @@ -343,13 +498,13 @@ proc_encrypted( CTX c, PACKET *pkt ) if( pkt->pkt.encrypted->mdc_method ) write_status( STATUS_GOODMDC ); } - else if( result == GPGERR_BAD_SIGN ) { + else if( result == G10ERR_BAD_SIGN ) { log_error(_("WARNING: encrypted message has been manipulated!\n")); write_status( STATUS_BADMDC ); } else { write_status( STATUS_DECRYPTION_FAILED ); - log_error(_("decryption failed: %s\n"), gpg_errstr(result)); + log_error(_("decryption failed: %s\n"), g10_errstr(result)); /* Hmmm: does this work when we have encrypted using multiple * ways to specify the session key (symmmetric and PK)*/ } @@ -372,9 +527,7 @@ proc_plaintext( CTX c, PACKET *pkt ) else if( opt.verbose ) log_info(_("original file name='%.*s'\n"), pt->namelen, pt->name); free_md_filter_context( &c->mfx ); - c->mfx.md = gcry_md_open( 0, 0); - if( !c->mfx.md ) - BUG(); + c->mfx.md = md_open( 0, 0); /* fixme: we may need to push the textfilter if we have sigclass 1 * and no armoring - Not yet tested * Hmmm, why don't we need it at all if we have sigclass 1 @@ -385,9 +538,9 @@ proc_plaintext( CTX c, PACKET *pkt ) for(n=c->list; n; n = n->next ) { if( n->pkt->pkttype == PKT_ONEPASS_SIG ) { if( n->pkt->pkt.onepass_sig->digest_algo ) { - gcry_md_enable( c->mfx.md, n->pkt->pkt.onepass_sig->digest_algo ); + md_enable( c->mfx.md, n->pkt->pkt.onepass_sig->digest_algo ); if( !any && n->pkt->pkt.onepass_sig->digest_algo - == GCRY_MD_MD5 ) + == DIGEST_ALGO_MD5 ) only_md5 = 1; else only_md5 = 0; @@ -395,27 +548,31 @@ proc_plaintext( CTX c, PACKET *pkt ) } if( n->pkt->pkt.onepass_sig->sig_class != 0x01 ) only_md5 = 0; - - /* Check whether this is a cleartext signature. We assume that - * we have one if the sig_class is 1 and the keyid is 0, that - * are the faked packets produced by armor.c. There is a - * possibility that this fails, but there is no other easy way - * to do it. (We could use a special packet type to indicate - * this, but this may also be faked - it simply can't be verified - * and is _no_ security issue) - */ - if( n->pkt->pkt.onepass_sig->sig_class == 0x01 - && !n->pkt->pkt.onepass_sig->keyid[0] - && !n->pkt->pkt.onepass_sig->keyid[1] ) - clearsig = 1; } + else if( n->pkt->pkttype == PKT_GPG_CONTROL + && n->pkt->pkt.gpg_control->control + == CTRLPKT_CLEARSIGN_START ) { + size_t datalen = n->pkt->pkt.gpg_control->datalen; + const byte *data = n->pkt->pkt.gpg_control->data; + + /* check that we have at least the sigclass and one hash */ + if ( datalen < 2 ) + log_fatal("invalid control packet CTRLPKT_CLEARSIGN_START\n"); + /* Note that we don't set the clearsig flag for not-dash-escaped + * documents */ + clearsig = (*data == 0x01); + for( data++, datalen--; datalen; datalen--, data++ ) + md_enable( c->mfx.md, *data ); + any = 1; + break; /* no pass signature pakets are expected */ + } } if( !any && !opt.skip_verify ) { /* no onepass sig packet: enable all standard algos */ - gcry_md_enable( c->mfx.md, GCRY_MD_RMD160 ); - gcry_md_enable( c->mfx.md, GCRY_MD_SHA1 ); - gcry_md_enable( c->mfx.md, GCRY_MD_MD5 ); + md_enable( c->mfx.md, DIGEST_ALGO_RMD160 ); + md_enable( c->mfx.md, DIGEST_ALGO_SHA1 ); + md_enable( c->mfx.md, DIGEST_ALGO_MD5 ); } if( opt.pgp2_workarounds && only_md5 && !opt.skip_verify ) { /* This is a kludge to work around a bug in pgp2. It does only @@ -423,25 +580,36 @@ proc_plaintext( CTX c, PACKET *pkt ) * pgp mails we could see whether there is the signature packet * in front of the plaintext. If someone needs this, send me a patch. */ - c->mfx.md2 = gcry_md_open( GCRY_MD_MD5, 0); - if( !c->mfx.md2 ) - BUG(); + c->mfx.md2 = md_open( DIGEST_ALGO_MD5, 0); } if ( DBG_HASHING ) { - gcry_md_start_debug( c->mfx.md, "verify" ); + md_start_debug( c->mfx.md, "verify" ); if ( c->mfx.md2 ) - gcry_md_start_debug( c->mfx.md2, "verify2" ); + md_start_debug( c->mfx.md2, "verify2" ); } - rc = handle_plaintext( pt, &c->mfx, c->sigs_only, clearsig ); - if( rc == GPGERR_CREATE_FILE && !c->sigs_only) { - /* can't write output but we hash it anyway to - * check the signature */ - rc = handle_plaintext( pt, &c->mfx, 1, clearsig ); + if ( c->pipemode.op == 'B' ) + rc = handle_plaintext( pt, &c->mfx, 1, 0 ); + else { + rc = handle_plaintext( pt, &c->mfx, c->sigs_only, clearsig ); + if( rc == G10ERR_CREATE_FILE && !c->sigs_only) { + /* can't write output but we hash it anyway to + * check the signature */ + rc = handle_plaintext( pt, &c->mfx, 1, clearsig ); + } } if( rc ) - log_error( "handle plaintext failed: %s\n", gpg_errstr(rc)); + log_error( "handle plaintext failed: %s\n", g10_errstr(rc)); free_packet(pkt); c->last_was_session_key = 0; + + /* We add a marker control packet instead of the plaintext packet. + * This is so that we can later detect invalid packet sequences. + */ + n = new_kbnode (create_gpg_control (CTRLPKT_PLAINTEXT_MARK, NULL, 0)); + if (c->list) + add_kbnode (c->list, n); + else + c->list = n; } @@ -472,7 +640,7 @@ proc_compressed( CTX c, PACKET *pkt ) else rc = handle_compressed( c, zd, NULL, NULL ); if( rc ) - log_error("uncompressing failed: %s\n", gpg_errstr(rc)); + log_error("uncompressing failed: %s\n", g10_errstr(rc)); free_packet(pkt); c->last_was_session_key = 0; } @@ -482,11 +650,15 @@ proc_compressed( CTX c, PACKET *pkt ) * Returns: 0 = valid signature or an error code */ static int -do_check_sig( CTX c, KBNODE node, int *is_selfsig ) +do_check_sig( CTX c, KBNODE node, int *is_selfsig, int *is_expkey ) { PKT_signature *sig; - GCRY_MD_HD md = NULL, md2 = NULL; - int algo, rc; + MD_HANDLE md = NULL, md2 = NULL; + int algo, rc, dum2; + u32 dummy; + + if(!is_expkey) + is_expkey=&dum2; assert( node->pkt->pkttype == PKT_SIGNATURE ); if( is_selfsig ) @@ -494,42 +666,35 @@ do_check_sig( CTX c, KBNODE node, int *is_selfsig ) sig = node->pkt->pkt.signature; algo = sig->digest_algo; - if( (rc=openpgp_md_test_algo(algo)) ) + if( (rc=check_digest_algo(algo)) ) return rc; if( sig->sig_class == 0x00 ) { if( c->mfx.md ) - md = gcry_md_copy( c->mfx.md ); + md = md_copy( c->mfx.md ); else /* detached signature */ - md = gcry_md_open( 0, 0 ); /* signature_check() will enable the md*/ - if( !md ) - BUG(); + md = md_open( 0, 0 ); /* signature_check() will enable the md*/ } 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???) */ if( c->mfx.md ) { - md = gcry_md_copy( c->mfx.md ); - if( !md ) - BUG(); - if( c->mfx.md2 ) { - md2 = gcry_md_copy( c->mfx.md2 ); - if( !md2 ) - BUG(); - } + md = md_copy( c->mfx.md ); + if( c->mfx.md2 ) + md2 = md_copy( c->mfx.md2 ); } else { /* detached signature */ - log_debug("Do we really need this here?"); - md = gcry_md_open( 0, 0 ); /* signature_check() will enable the md*/ - md2 = gcry_md_open( 0, 0 ); - if( !md || !md2 ) - BUG(); + log_debug("Do we really need this here?"); + md = md_open( 0, 0 ); /* signature_check() will enable the md*/ + md2 = md_open( 0, 0 ); } } else if( (sig->sig_class&~3) == 0x10 || sig->sig_class == 0x18 + || sig->sig_class == 0x1f || sig->sig_class == 0x20 - || sig->sig_class == 0x30 ) { /* classes 0x10..0x17,0x20,0x30 */ + || sig->sig_class == 0x28 + || sig->sig_class == 0x30 ) { if( c->list->pkt->pkttype == PKT_PUBLIC_KEY || c->list->pkt->pkttype == PKT_PUBLIC_SUBKEY ) { return check_key_signature( c->list, node, is_selfsig ); @@ -537,21 +702,21 @@ do_check_sig( CTX c, KBNODE node, int *is_selfsig ) else if( sig->sig_class == 0x20 ) { log_info(_("standalone revocation - " "use \"gpg --import\" to apply\n")); - return GPGERR_NOT_PROCESSED; + return G10ERR_NOT_PROCESSED; } else { log_error("invalid root packet for sigclass %02x\n", sig->sig_class); - return GPGERR_SIG_CLASS; + return G10ERR_SIG_CLASS; } } else - return GPGERR_SIG_CLASS; - rc = signature_check( sig, md ); - if( rc == GPGERR_BAD_SIGN && md2 ) - rc = signature_check( sig, md2 ); - gcry_md_close(md); - gcry_md_close(md2); + return G10ERR_SIG_CLASS; + rc = signature_check2( sig, md, &dummy, is_expkey ); + if( rc == G10ERR_BAD_SIGN && md2 ) + rc = signature_check2( sig, md2, &dummy, is_expkey ); + md_close(md); + md_close(md2); return rc; } @@ -567,8 +732,15 @@ print_userid( PACKET *pkt ) return; } if( opt.with_colons ) - print_string( stdout, pkt->pkt.user_id->name, - pkt->pkt.user_id->len, ':'); + { + if(pkt->pkt.user_id->attrib_data) + printf("%u %lu", + pkt->pkt.user_id->numattribs, + pkt->pkt.user_id->attrib_len); + else + print_string( stdout, pkt->pkt.user_id->name, + pkt->pkt.user_id->len, ':'); + } else print_utf8_string( stdout, pkt->pkt.user_id->name, pkt->pkt.user_id->len ); @@ -576,51 +748,13 @@ print_userid( PACKET *pkt ) static void -print_fingerprint( PKT_public_key *pk, PKT_secret_key *sk ) -{ - byte array[MAX_FINGERPRINT_LEN], *p; - size_t i, n; - - if( sk ) - fingerprint_from_sk( sk, array, &n ); - else - fingerprint_from_pk( pk, array, &n ); - p = array; - if( opt.with_colons ) { - printf("fpr:::::::::"); - for(i=0; i < n ; i++, p++ ) - printf("%02X", *p ); - putchar(':'); - } - else { - 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'); -} - -static void print_notation_data( PKT_signature *sig ) { size_t n, n1, n2; const byte *p; int seq = 0; - while( (p = enum_sig_subpkt( sig->hashed_data, SIGSUBPKT_NOTATION, - &n, &seq )) ) { + while((p=enum_sig_subpkt(sig->hashed,SIGSUBPKT_NOTATION,&n,&seq,NULL))) { if( n < 8 ) { log_info(_("WARNING: invalid notation data found\n")); return; @@ -639,14 +773,20 @@ print_notation_data( PKT_signature *sig ) putc( '=', log_stream() ); print_string( log_stream(), p+n1, n2, 0 ); putc( '\n', log_stream() ); + write_status_buffer ( STATUS_NOTATION_NAME, p , n1, 0 ); + write_status_buffer ( STATUS_NOTATION_DATA, p+n1, n2, 50 ); } - if( (p = parse_sig_subpkt( sig->hashed_data, SIGSUBPKT_POLICY, &n ) )) { + + seq=0; + + while((p=enum_sig_subpkt(sig->hashed,SIGSUBPKT_POLICY,&n,&seq,NULL))) { log_info(_("Policy: ") ); print_string( log_stream(), p, n, 0 ); putc( '\n', log_stream() ); + write_status_buffer ( STATUS_POLICY_URL, p, n, 0 ); } - /* Now check wheter the key of this signature has some + /* Now check whether the key of this signature has some * notation data */ /* TODO */ @@ -675,7 +815,7 @@ list_node( CTX c, KBNODE node ) if( mainkey ) { c->local_id = pk->local_id; c->trustletter = opt.fast_list_mode? - 0 : query_trust_info( pk, NULL ); + 0 : get_validity_info( pk, NULL ); } printf("%s:", mainkey? "pub":"sub" ); if( c->trustletter ) @@ -684,18 +824,18 @@ list_node( CTX c, KBNODE node ) nbits_from_pk( pk ), pk->pubkey_algo, (ulong)keyid[0],(ulong)keyid[1], - datestr_from_pk( pk ), - pk->expiredate? strtimestamp(pk->expiredate):"" ); + colon_datestr_from_pk( pk ), + colon_strtime (pk->expiredate) ); if( c->local_id ) printf("%lu", c->local_id ); putchar(':'); - if( c->local_id && !opt.fast_list_mode ) - putchar( get_ownertrust_info( c->local_id ) ); + if( mainkey && !opt.fast_list_mode ) + putchar( get_ownertrust_info (pk) ); putchar(':'); if( node->next && node->next->pkt->pkttype == PKT_RING_TRUST) { putchar('\n'); any=1; if( opt.fingerprint ) - print_fingerprint( pk, NULL ); + print_fingerprint( pk, NULL, 0 ); printf("rtv:1:%u:\n", node->next->pkt->pkt.ring_trust->trustval ); } @@ -724,7 +864,8 @@ list_node( CTX c, KBNODE node ) else if( node->pkt->pkttype == PKT_USER_ID ) { if( any ) { if( opt.with_colons ) - printf("uid:::::::::"); + printf("%s:::::::::", + node->pkt->pkt.user_id->attrib_data?"uat":"uid"); else printf( "uid%*s", 28, "" ); } @@ -733,7 +874,7 @@ list_node( CTX c, KBNODE node ) putchar(':'); putchar('\n'); if( opt.fingerprint && !any ) - print_fingerprint( pk, NULL ); + print_fingerprint( pk, NULL, 0 ); if( node->next && node->next->pkt->pkttype == PKT_RING_TRUST ) { printf("rtv:2:%u:\n", @@ -757,7 +898,7 @@ list_node( CTX c, KBNODE node ) if( !any ) putchar('\n'); if( !mainkey && opt.fingerprint > 1 ) - print_fingerprint( pk, NULL ); + print_fingerprint( pk, NULL, 0 ); } else if( (mainkey = (node->pkt->pkttype == PKT_SECRET_KEY) ) || node->pkt->pkttype == PKT_SECRET_SUBKEY ) { @@ -771,8 +912,8 @@ list_node( CTX c, KBNODE node ) nbits_from_sk( sk ), sk->pubkey_algo, (ulong)keyid[0],(ulong)keyid[1], - datestr_from_sk( sk ), - sk->expiredate? strtimestamp(sk->expiredate):"" + colon_datestr_from_sk( sk ), + colon_strtime (sk->expiredate) /* fixme: add LID */ ); } else @@ -798,7 +939,8 @@ list_node( CTX c, KBNODE node ) else if( node->pkt->pkttype == PKT_USER_ID ) { if( any ) { if( opt.with_colons ) - printf("uid:::::::::"); + printf("%s:::::::::", + node->pkt->pkt.user_id->attrib_data?"uat":"uid"); else printf( "uid%*s", 28, "" ); } @@ -807,7 +949,7 @@ list_node( CTX c, KBNODE node ) putchar(':'); putchar('\n'); if( opt.fingerprint && !any ) - print_fingerprint( NULL, sk ); + print_fingerprint( NULL, sk, 0 ); any=1; } else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) { @@ -822,7 +964,7 @@ list_node( CTX c, KBNODE node ) if( !any ) putchar('\n'); if( !mainkey && opt.fingerprint > 1 ) - print_fingerprint( NULL, sk ); + print_fingerprint( NULL, sk, 0 ); } else if( node->pkt->pkttype == PKT_SIGNATURE ) { PKT_signature *sig = node->pkt->pkt.signature; @@ -841,10 +983,11 @@ list_node( CTX c, KBNODE node ) fputs("sig", stdout); if( opt.check_sigs ) { fflush(stdout); - switch( (rc2=do_check_sig( c, node, &is_selfsig )) ) { + switch( (rc2=do_check_sig( c, node, &is_selfsig, NULL )) ) { case 0: sigrc = '!'; break; - case GPGERR_BAD_SIGN: sigrc = '-'; break; - case GPGERR_NO_PUBKEY: sigrc = '?'; break; + case G10ERR_BAD_SIGN: sigrc = '-'; break; + case G10ERR_NO_PUBKEY: + case G10ERR_UNU_PUBKEY: sigrc = '?'; break; default: sigrc = '%'; break; } } @@ -868,13 +1011,13 @@ list_node( CTX c, KBNODE node ) putchar(sigrc); printf("::%d:%08lX%08lX:%s::::", sig->pubkey_algo, (ulong)sig->keyid[0], - (ulong)sig->keyid[1], datestr_from_sig(sig)); + (ulong)sig->keyid[1], colon_datestr_from_sig(sig)); } else printf("%c %08lX %s ", sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig)); if( sigrc == '%' ) - printf("[%s] ", gpg_errstr(rc2) ); + printf("[%s] ", g10_errstr(rc2) ); else if( sigrc == '?' ) ; else if( is_selfsig ) { @@ -887,10 +1030,10 @@ list_node( CTX c, KBNODE node ) else if( !opt.fast_list_mode ) { p = get_user_id( sig->keyid, &n ); print_string( stdout, p, n, opt.with_colons ); - gcry_free(p); + m_free(p); } if( opt.with_colons ) - printf(":%02x:", sig->sig_class ); + printf(":%02x%c:", sig->sig_class, sig->flags.exportable?'x':'l'); putchar('\n'); } else @@ -903,11 +1046,11 @@ int proc_packets( void *anchor, IOBUF a ) { int rc; - CTX c = gcry_xcalloc( 1, sizeof *c ); + CTX c = m_alloc_clear( sizeof *c ); c->anchor = anchor; rc = do_proc_packets( c, a ); - gcry_free( c ); + m_free( c ); return rc; } @@ -917,7 +1060,7 @@ int proc_signature_packets( void *anchor, IOBUF a, STRLIST signedfiles, const char *sigfilename ) { - CTX c = gcry_xcalloc( 1, sizeof *c ); + CTX c = m_alloc_clear( sizeof *c ); int rc; c->anchor = anchor; @@ -925,20 +1068,20 @@ proc_signature_packets( void *anchor, IOBUF a, c->signed_data = signedfiles; c->sigfilename = sigfilename; rc = do_proc_packets( c, a ); - gcry_free( c ); + m_free( c ); return rc; } int proc_encryption_packets( void *anchor, IOBUF a ) { - CTX c = gcry_xcalloc( 1, sizeof *c ); + CTX c = m_alloc_clear( sizeof *c ); int rc; c->anchor = anchor; c->encrypt_only = 1; rc = do_proc_packets( c, a ); - gcry_free( c ); + m_free( c ); return rc; } @@ -946,18 +1089,20 @@ proc_encryption_packets( void *anchor, IOBUF a ) int do_proc_packets( CTX c, IOBUF a ) { - PACKET *pkt = gcry_xmalloc( sizeof *pkt ); + PACKET *pkt = m_alloc( sizeof *pkt ); int rc=0; int any_data=0; int newpkt; c->iobuf = a; init_packet(pkt); - while( (rc=parse_packet(a, pkt, NULL)) != -1 ) { + while( (rc=parse_packet(a, pkt)) != -1 ) { any_data = 1; if( rc ) { free_packet(pkt); - if( rc == GPGERR_INVALID_PACKET ) + /* stop processing hwne an invalid packet has been encountered + * but don't do so when we are doing a --list-packet. */ + if( rc == G10ERR_INVALID_PACKET && opt.list_packets != 2 ) break; continue; } @@ -981,12 +1126,14 @@ do_proc_packets( CTX c, IOBUF a ) case PKT_PUBKEY_ENC: case PKT_ENCRYPTED: case PKT_ENCRYPTED_MDC: - rc = GPGERR_UNEXPECTED; + write_status_text( STATUS_UNEXPECTED, "0" ); + rc = G10ERR_UNEXPECTED; goto leave; case PKT_SIGNATURE: newpkt = add_signature( 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; + case PKT_GPG_CONTROL: newpkt = add_gpg_control(c, pkt); break; default: newpkt = 0; break; } } @@ -995,7 +1142,8 @@ do_proc_packets( CTX c, IOBUF a ) case PKT_PUBLIC_KEY: case PKT_SECRET_KEY: case PKT_USER_ID: - rc = GPGERR_UNEXPECTED; + write_status_text( STATUS_UNEXPECTED, "0" ); + rc = G10ERR_UNEXPECTED; goto leave; case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break; case PKT_SYMKEY_ENC: proc_symkey_enc( c, pkt ); break; @@ -1005,6 +1153,7 @@ do_proc_packets( CTX c, IOBUF a ) 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; + case PKT_GPG_CONTROL: newpkt = add_gpg_control(c, pkt); break; default: newpkt = 0; break; } } @@ -1029,23 +1178,38 @@ do_proc_packets( CTX c, IOBUF a ) 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; + case PKT_GPG_CONTROL: newpkt = add_gpg_control(c, pkt); break; case PKT_RING_TRUST: newpkt = add_ring_trust( c, pkt ); break; default: newpkt = 0; break; } } - if( pkt->pkttype != PKT_SIGNATURE ) + /* This is a very ugly construct and frankly, I don't remember why + * I used it. Adding the MDC check here is a hack. + * The right solution is to initiate another context for encrypted + * packet and not to reuse the current one ... It works right + * when there is a compression packet inbetween which adds just + * an extra layer. + * Hmmm: Rewrite this whole module here?? + */ + if( pkt->pkttype != PKT_SIGNATURE && pkt->pkttype != PKT_MDC ) c->have_data = pkt->pkttype == PKT_PLAINTEXT; if( newpkt == -1 ) ; else if( newpkt ) { - pkt = gcry_xmalloc( sizeof *pkt ); + pkt = m_alloc( sizeof *pkt ); init_packet(pkt); } else free_packet(pkt); + if ( c->pipemode.stop_now ) { + /* we won't get an EOF in pipemode, so we have to + * break the loop here */ + rc = -1; + break; + } } - if( rc == GPGERR_INVALID_PACKET ) + if( rc == G10ERR_INVALID_PACKET ) write_status_text( STATUS_NODATA, "3" ); if( any_data ) rc = 0; @@ -1055,9 +1219,9 @@ do_proc_packets( CTX c, IOBUF a ) leave: release_list( c ); - gcry_free(c->dek); + m_free(c->dek); free_packet( pkt ); - gcry_free( pkt ); + m_free( pkt ); free_md_filter_context( &c->mfx ); return rc; } @@ -1068,76 +1232,190 @@ check_sig_and_print( CTX c, KBNODE node ) { PKT_signature *sig = node->pkt->pkt.signature; const char *astr, *tstr; - int rc; + int rc, is_expkey=0; if( opt.skip_verify ) { log_info(_("signature verification suppressed\n")); return 0; } + /* It is not in all cases possible to check multiple signatures: + * PGP 2 (which is also allowed by OpenPGP), does use the packet + * sequence: sig+data, OpenPGP does use onepas+data=sig and GnuPG + * sometimes uses (because I did'nt read the specs right) data+sig. + * Because it is possible to create multiple signatures with + * different packet sequence (e.g. data+sig and sig+data) it might + * not be possible to get it right: let's say we have: + * data+sig, sig+data,sig+data and we have not yet encountered the last + * data, we could also see this a one data with 2 signatures and then + * data+sig. + * To protect against this we check that all signatures follow + * without any intermediate packets. Note, that we won't get this + * error when we use onepass packets or cleartext signatures because + * we reset the list every time + * + * FIXME: Now that we have these marker packets, we should create a + * real grammar and check against this. + */ + { + KBNODE n; + int n_sig=0; + + for (n=c->list; n; n=n->next ) { + if ( n->pkt->pkttype == PKT_SIGNATURE ) + n_sig++; + } + if (n_sig > 1) { /* more than one signature - check sequence */ + int tmp, onepass; + + for (tmp=onepass=0,n=c->list; n; n=n->next ) { + if (n->pkt->pkttype == PKT_ONEPASS_SIG) + onepass++; + else if (n->pkt->pkttype == PKT_GPG_CONTROL + && n->pkt->pkt.gpg_control->control + == CTRLPKT_CLEARSIGN_START ) { + onepass++; /* handle the same way as a onepass */ + } + else if ( (tmp && n->pkt->pkttype != PKT_SIGNATURE) ) { + log_error(_("can't handle these multiple signatures\n")); + return 0; + } + else if ( n->pkt->pkttype == PKT_SIGNATURE ) + tmp = 1; + else if (!tmp && !onepass + && n->pkt->pkttype == PKT_GPG_CONTROL + && n->pkt->pkt.gpg_control->control + == CTRLPKT_PLAINTEXT_MARK ) { + /* plaintext before signatures but no one-pass packets*/ + log_error(_("can't handle these multiple signatures\n")); + return 0; + } + } + } + } + + + tstr = asctimestamp(sig->timestamp); - astr = gcry_pk_algo_name( sig->pubkey_algo ); + astr = pubkey_algo_to_string( sig->pubkey_algo ); log_info(_("Signature made %.*s using %s key ID %08lX\n"), (int)strlen(tstr), tstr, astr? astr: "?", (ulong)sig->keyid[1] ); - rc = do_check_sig(c, node, NULL ); - if( rc == GPGERR_NO_PUBKEY && opt.keyserver_name && opt.auto_key_retrieve) { - if( !hkp_ask_import( sig->keyid ) ) - rc = do_check_sig(c, node, NULL ); + rc = do_check_sig(c, node, NULL, &is_expkey ); + if( rc == G10ERR_NO_PUBKEY && opt.keyserver_scheme && opt.keyserver_options.auto_key_retrieve) { + if( keyserver_import_keyid ( sig->keyid )==0 ) + rc = do_check_sig(c, node, NULL, &is_expkey ); } - if( !rc || rc == GPGERR_BAD_SIGN ) { + if( !rc || rc == G10ERR_BAD_SIGN ) { KBNODE un, keyblock; - char *us; - int count=0; + int count=0, statno; + char keyid_str[50]; + + if(rc) + statno=STATUS_BADSIG; + else if(sig->flags.expired) + statno=STATUS_EXPSIG; + else if(is_expkey) + statno=STATUS_EXPKEYSIG; + else + statno=STATUS_GOODSIG; keyblock = get_pubkeyblock( sig->keyid ); - us = get_long_user_id_string( sig->keyid ); - write_status_text( rc? STATUS_BADSIG : STATUS_GOODSIG, us ); - gcry_free(us); + sprintf (keyid_str, "%08lX%08lX [uncertain] ", + (ulong)sig->keyid[0], (ulong)sig->keyid[1]); - /* fixme: list only user ids which are valid and add information - * about the trustworthiness of each user id, sort them. - * Integrate this with check_signatures_trust(). */ + /* find and print the primary user ID */ for( un=keyblock; un; un = un->next ) { if( un->pkt->pkttype != PKT_USER_ID ) continue; - if( !count++ ) - log_info(rc? _("BAD signature from \"") - : _("Good signature from \"")); - else - log_info( _(" aka \"")); + if ( !un->pkt->pkt.user_id->created ) + continue; + if ( un->pkt->pkt.user_id->is_revoked ) + continue; + if ( !un->pkt->pkt.user_id->is_primary ) + continue; + + keyid_str[17] = 0; /* cut off the "[uncertain]" part */ + write_status_text_and_buffer (statno, keyid_str, + un->pkt->pkt.user_id->name, + un->pkt->pkt.user_id->len, + -1 ); + + log_info(rc? _("BAD signature from \"") + : sig->flags.expired ? _("Expired signature from \"") + : _("Good signature from \"")); print_utf8_string( log_stream(), un->pkt->pkt.user_id->name, un->pkt->pkt.user_id->len ); fputs("\"\n", log_stream() ); - if( rc ) - break; /* print only one id in this case */ + count++; } if( !count ) { /* just in case that we have no userid */ - log_info(rc? _("BAD signature from \"") + for( un=keyblock; un; un = un->next ) { + if( un->pkt->pkttype == PKT_USER_ID ) + break; + } + + if (opt.always_trust || !un) + keyid_str[17] = 0; /* cut off the "[uncertain]" part */ + + write_status_text_and_buffer (statno, keyid_str, + un? un->pkt->pkt.user_id->name:"[?]", + un? un->pkt->pkt.user_id->len:3, + -1 ); + + log_info(rc? _("BAD signature from \"") + : sig->flags.expired ? _("Expired signature from \"") : _("Good signature from \"")); - fputs("[?]\"\n", log_stream() ); + if (!opt.always_trust && un) { + fputs(_("[uncertain]"), log_stream() ); + putc(' ', log_stream() ); + } + print_utf8_string( log_stream(), + un? un->pkt->pkt.user_id->name:"[?]", + un? un->pkt->pkt.user_id->len:3 ); + fputs("\"\n", log_stream() ); + } + + /* If we have a good signature and already printed + * the primary user ID, print all the other user IDs */ + if ( count && !rc ) { + for( un=keyblock; un; un = un->next ) { + if( un->pkt->pkttype != PKT_USER_ID ) + continue; + if ( un->pkt->pkt.user_id->is_revoked ) + continue; + if ( un->pkt->pkt.user_id->is_primary ) + continue; + + log_info( _(" aka \"")); + print_utf8_string( log_stream(), un->pkt->pkt.user_id->name, + un->pkt->pkt.user_id->len ); + fputs("\"\n", log_stream() ); + } } release_kbnode( keyblock ); + if( !rc ) print_notation_data( sig ); if( !rc && is_status_enabled() ) { /* print a status response with the fingerprint */ - PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); + PKT_public_key *pk = m_alloc_clear( sizeof *pk ); if( !get_pubkey( pk, sig->keyid ) ) { byte array[MAX_FINGERPRINT_LEN], *p; - char buf[MAX_FINGERPRINT_LEN*2+61]; + char buf[MAX_FINGERPRINT_LEN*2+72]; size_t i, n; fingerprint_from_pk( pk, array, &n ); p = array; for(i=0; i < n ; i++, p++ ) sprintf(buf+2*i, "%02X", *p ); - sprintf(buf+strlen(buf), " %s %lu", + sprintf(buf+strlen(buf), " %s %lu %lu", strtimestamp( sig->timestamp ), - (ulong)sig->timestamp ); + (ulong)sig->timestamp, + (ulong)sig->expiredate ); write_status_text( STATUS_VALIDSIG, buf ); } free_public_key( pk ); @@ -1145,10 +1423,19 @@ check_sig_and_print( CTX c, KBNODE node ) if( !rc ) rc = check_signatures_trust( sig ); + + if(sig->flags.expired) + { + log_info("Signature expired %s\n",asctimestamp(sig->expiredate)); + rc=G10ERR_GENERAL; /* need a better error here? */ + } + else if(sig->expiredate) + log_info("Signature expires %s\n",asctimestamp(sig->expiredate)); + if( rc ) - gpg_errors_seen = 1; + g10_errors_seen = 1; if( opt.batch && rc ) - gpg_exit(1); + g10_exit(1); } else { char buf[50]; @@ -1157,12 +1444,12 @@ check_sig_and_print( CTX c, KBNODE node ) sig->pubkey_algo, sig->digest_algo, sig->sig_class, (ulong)sig->timestamp, rc ); write_status_text( STATUS_ERRSIG, buf ); - if( rc == GPGERR_NO_PUBKEY ) { + if( rc == G10ERR_NO_PUBKEY ) { buf[16] = 0; write_status_text( STATUS_NO_PUBKEY, buf ); } - if( rc != GPGERR_NOT_PROCESSED ) - log_error(_("Can't check signature: %s\n"), gpg_errstr(rc) ); + if( rc != G10ERR_NOT_PROCESSED ) + log_error(_("Can't check signature: %s\n"), g10_errstr(rc) ); } return rc; } @@ -1180,6 +1467,18 @@ proc_tree( CTX c, KBNODE node ) if( opt.list_packets || opt.list_only ) return; + /* we must skip our special plaintext marker packets here becuase + they may be the root packet. These packets are only used in + addionla checks and skipping them here doesn't matter */ + while ( node + && node->pkt->pkttype == PKT_GPG_CONTROL + && node->pkt->pkt.gpg_control->control + == CTRLPKT_PLAINTEXT_MARK ) { + node = node->next; + } + if (!node) + return; + c->local_id = 0; c->trustletter = ' '; if( node->pkt->pkttype == PKT_PUBLIC_KEY @@ -1196,12 +1495,11 @@ proc_tree( CTX c, KBNODE node ) if( !c->have_data ) { free_md_filter_context( &c->mfx ); /* prepare to create all requested message digests */ - if ( !(c->mfx.md = gcry_md_open(0, 0)) ) - BUG(); + c->mfx.md = md_open(0, 0); /* fixme: why looking for the signature packet and not 1passpacket*/ for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) { - gcry_md_enable( c->mfx.md, n1->pkt->pkt.signature->digest_algo); + md_enable( c->mfx.md, n1->pkt->pkt.signature->digest_algo); } /* ask for file and hash it */ if( c->sigs_only ) { @@ -1211,18 +1509,38 @@ proc_tree( CTX c, KBNODE node ) } else { rc = ask_for_detached_datafile( c->mfx.md, c->mfx.md2, - iobuf_get_fname(c->iobuf), + iobuf_get_real_fname(c->iobuf), n1? (n1->pkt->pkt.onepass_sig->sig_class == 0x01):0 ); } if( rc ) { - log_error("can't hash datafile: %s\n", gpg_errstr(rc)); + log_error("can't hash datafile: %s\n", g10_errstr(rc)); return; } } + else if ( c->signed_data ) { + log_error (_("not a detached signature\n") ); + return; + } for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) check_sig_and_print( c, n1 ); } + else if( node->pkt->pkttype == PKT_GPG_CONTROL + && node->pkt->pkt.gpg_control->control + == CTRLPKT_CLEARSIGN_START ) { + /* clear text signed message */ + if( !c->have_data ) { + log_error("cleartext signature without data\n" ); + return; + } + else if ( c->signed_data ) { + log_error (_("not a detached signature\n") ); + return; + } + + for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) + check_sig_and_print( c, n1 ); + } else if( node->pkt->pkttype == PKT_SIGNATURE ) { PKT_signature *sig = node->pkt->pkt.signature; @@ -1232,26 +1550,20 @@ proc_tree( CTX c, KBNODE node ) else if( !c->have_data ) { /* detached signature */ free_md_filter_context( &c->mfx ); - c->mfx.md = gcry_md_open(sig->digest_algo, 0); - if ( !c->mfx.md ) - BUG(); + c->mfx.md = md_open(sig->digest_algo, 0); if( !opt.pgp2_workarounds ) ; - else if( sig->digest_algo == GCRY_MD_MD5 + else if( sig->digest_algo == DIGEST_ALGO_MD5 && is_RSA( sig->pubkey_algo ) ) { /* enable a workaround for a pgp2 bug */ - c->mfx.md2 = gcry_md_open( GCRY_MD_MD5, 0 ); - if ( !c->mfx.md2 ) - BUG(); + c->mfx.md2 = md_open( DIGEST_ALGO_MD5, 0 ); } - else if( sig->digest_algo == GCRY_MD_SHA1 - && sig->pubkey_algo == GCRY_PK_DSA + else if( sig->digest_algo == DIGEST_ALGO_SHA1 + && sig->pubkey_algo == PUBKEY_ALGO_DSA && sig->sig_class == 0x01 ) { /* enable the workaround also for pgp5 when the detached * signature has been created in textmode */ - c->mfx.md2 = gcry_md_open( sig->digest_algo, 0 ); - if ( !c->mfx.md2 ) - BUG(); + c->mfx.md2 = md_open( sig->digest_algo, 0 ); } #if 0 /* workaround disabled */ /* Here we have another hack to work around a pgp 2 bug @@ -1263,6 +1575,11 @@ proc_tree( CTX c, KBNODE node ) */ /* c->mfx.md2? 0 :(sig->sig_class == 0x01) */ #endif + if ( DBG_HASHING ) { + md_start_debug( c->mfx.md, "verify" ); + if ( c->mfx.md2 ) + md_start_debug( c->mfx.md2, "verify2" ); + } if( c->sigs_only ) { rc = hash_datafiles( c->mfx.md, c->mfx.md2, c->signed_data, c->sigfilename, @@ -1270,23 +1587,33 @@ proc_tree( CTX c, KBNODE node ) } else { rc = ask_for_detached_datafile( c->mfx.md, c->mfx.md2, - iobuf_get_fname(c->iobuf), + iobuf_get_real_fname(c->iobuf), (sig->sig_class == 0x01) ); } if( rc ) { - log_error("can't hash datafile: %s\n", gpg_errstr(rc)); + log_error("can't hash datafile: %s\n", g10_errstr(rc)); return; } } - else + else if ( c->signed_data ) { + log_error (_("not a detached signature\n") ); + return; + } + else if ( c->pipemode.op == 'B' ) + ; /* this is a detached signature trough the pipemode handler */ + else if (!opt.quiet) log_info(_("old style (PGP 2.x) signature\n")); - check_sig_and_print( c, node ); + for( n1 = node; n1; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )) ) + check_sig_and_print( c, n1 ); } - else + else { + dump_kbnode (c->list); log_error(_("invalid root packet detected in proc_tree()\n")); - + dump_kbnode (node); + } } + |