1 /* mainproc.c - handle packets
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GNUPG.
6 * GNUPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNUPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
43 * Structure to hold the context
47 PKT_public_key *last_pubkey;
48 PKT_secret_key *last_seckey;
49 PKT_user_id *last_user_id;
50 md_filter_context_t mfx;
51 int sigs_only; /* process only signatures and reject all other stuff */
52 int encrypt_only; /* process only encrytion messages */
54 const char *sigfilename;
56 int last_was_session_key;
57 KBNODE list; /* the current list of packets */
59 IOBUF iobuf; /* used to get the filename etc. */
60 int trustletter; /* temp usage in list_node */
61 ulong local_id; /* ditto */
65 static int do_proc_packets( CTX c, IOBUF a );
67 static void list_node( CTX c, KBNODE node );
68 static void proc_tree( CTX c, KBNODE node );
76 proc_tree(c, c->list );
77 release_kbnode( c->list );
83 add_onepass_sig( CTX c, PACKET *pkt )
87 if( c->list ) { /* add another packet */
88 if( c->list->pkt->pkttype != PKT_ONEPASS_SIG ) {
89 log_error("add_onepass_sig: another packet is in the way\n");
91 c->list = new_kbnode( pkt );
94 add_kbnode( c->list, new_kbnode( pkt ));
96 else /* insert the first one */
97 c->list = node = new_kbnode( pkt );
105 add_user_id( CTX c, PACKET *pkt )
108 log_error("orphaned user id\n" );
111 add_kbnode( c->list, new_kbnode( pkt ) );
116 add_subkey( CTX c, PACKET *pkt )
119 log_error("subkey w/o mainkey\n" );
122 add_kbnode( c->list, new_kbnode( pkt ) );
128 add_signature( CTX c, PACKET *pkt )
132 if( pkt->pkttype == PKT_SIGNATURE && !c->list ) {
133 /* This is the first signature for the following datafile.
134 * G10 does not write such packets; instead it always uses
135 * onepass-sig packets. The drawback of PGP's method
136 * of prepending the signature to the data is
137 * that it is not possible to make a signature from data read
138 * from stdin. (G10 is able to read PGP stuff anyway.) */
139 node = new_kbnode( pkt );
144 return 0; /* oops (invalid packet sequence)*/
145 else if( !c->list->pkt )
146 BUG(); /* so nicht */
148 /* add a new signature node id at the end */
149 node = new_kbnode( pkt );
150 add_kbnode( c->list, node );
156 proc_symkey_enc( CTX c, PACKET *pkt )
160 enc = pkt->pkt.symkey_enc;
162 log_error( "symkey_enc packet with session keys are not supported!\n");
164 c->last_was_session_key = 2;
165 c->dek = passphrase_to_dek( NULL, enc->cipher_algo, &enc->s2k, 0 );
171 proc_pubkey_enc( CTX c, PACKET *pkt )
176 /* check whether the secret key is available and store in this case */
177 c->last_was_session_key = 1;
178 enc = pkt->pkt.pubkey_enc;
179 /*printf("enc: encrypted by a pubkey with keyid %08lX\n", enc->keyid[1] );*/
180 if( is_ELGAMAL(enc->pubkey_algo)
181 || enc->pubkey_algo == PUBKEY_ALGO_DSA
182 || is_RSA(enc->pubkey_algo) ) {
183 m_free(c->dek ); /* paranoid: delete a pending DEK */
184 c->dek = m_alloc_secure( sizeof *c->dek );
185 if( (result = get_session_key( enc, c->dek )) ) {
186 /* error: delete the DEK */
187 m_free(c->dek); c->dek = NULL;
191 result = G10ERR_PUBKEY_ALGO;
196 if( opt.verbose > 1 )
197 log_info( "pubkey_enc packet: Good DEK\n" );
200 log_error(_("public key decryption failed: %s\n"), g10_errstr(result));
207 proc_encrypted( CTX c, PACKET *pkt )
211 /*printf("dat: %sencrypted data\n", c->dek?"":"conventional ");*/
212 if( !c->dek && !c->last_was_session_key ) {
213 /* assume this is old conventional encrypted data */
214 c->dek = passphrase_to_dek( NULL,
215 opt.def_cipher_algo ? opt.def_cipher_algo
216 : DEFAULT_CIPHER_ALGO, NULL, 0 );
219 result = G10ERR_NO_SECKEY;
221 result = decrypt_data( pkt->pkt.encrypted, c->dek );
222 m_free(c->dek); c->dek = NULL;
226 if( opt.verbose > 1 )
227 log_info("decryption okay\n");
230 log_error(_("decryption failed: %s\n"), g10_errstr(result));
231 /* FIXME: if this is secret key not available, try with
235 c->last_was_session_key = 0;
240 proc_plaintext( CTX c, PACKET *pkt )
242 PKT_plaintext *pt = pkt->pkt.plaintext;
243 int any, clearsig, rc;
247 log_info("original file name='%.*s'\n", pt->namelen, pt->name);
248 free_md_filter_context( &c->mfx );
249 c->mfx.md = md_open( 0, 0);
250 /* fixme: we may need to push the textfilter if we have sigclass 1
251 * and no armoring - Not yet tested */
253 for(n=c->list; n; n = n->next ) {
254 if( n->pkt->pkttype == PKT_ONEPASS_SIG ) {
255 if( n->pkt->pkt.onepass_sig->digest_algo ) {
256 md_enable( c->mfx.md, n->pkt->pkt.onepass_sig->digest_algo );
259 /* Check whether this is a cleartext signature. We assume that
260 * we have one if the sig_class is 1 and the keyid is 0, that
261 * are the faked packets produced by armor.c. There is a
262 * possibility that this fails, but there is no other easy way
263 * to do it. (We could use a special packet type to indicate
264 * this, but this may also be faked - it simply can't be verified
265 * and is _no_ security issue)
267 if( n->pkt->pkt.onepass_sig->sig_class == 0x01
268 && !n->pkt->pkt.onepass_sig->keyid[0]
269 && !n->pkt->pkt.onepass_sig->keyid[1] )
273 if( !any ) { /* no onepass sig packet: enable all standard algos */
274 md_enable( c->mfx.md, DIGEST_ALGO_RMD160 );
275 md_enable( c->mfx.md, DIGEST_ALGO_SHA1 );
276 md_enable( c->mfx.md, DIGEST_ALGO_MD5 );
280 if( c->mfx.md->list )
281 m_check( c->mfx.md->list );
283 rc = handle_plaintext( pt, &c->mfx, c->sigs_only, clearsig );
284 if( rc == G10ERR_CREATE_FILE && !c->sigs_only) {
285 /* can't write output but we hash it anyway to
286 * check the signature */
287 rc = handle_plaintext( pt, &c->mfx, 1, clearsig );
290 log_error( "handle plaintext failed: %s\n", g10_errstr(rc));
292 c->last_was_session_key = 0;
297 proc_compressed_cb( IOBUF a, void *info )
299 return proc_signature_packets( a, ((CTX)info)->signed_data,
300 ((CTX)info)->sigfilename );
304 proc_encrypt_cb( IOBUF a, void *info )
306 return proc_encryption_packets( a );
310 proc_compressed( CTX c, PACKET *pkt )
312 PKT_compressed *zd = pkt->pkt.compressed;
315 /*printf("zip: compressed data packet\n");*/
317 rc = handle_compressed( zd, proc_compressed_cb, c );
318 else if( c->encrypt_only )
319 rc = handle_compressed( zd, proc_encrypt_cb, c );
321 rc = handle_compressed( zd, NULL, NULL );
323 log_error("uncompressing failed: %s\n", g10_errstr(rc));
325 c->last_was_session_key = 0;
332 * check the signature
333 * Returns: 0 = valid signature or an error code
336 do_check_sig( CTX c, KBNODE node, int *is_selfsig )
342 assert( node->pkt->pkttype == PKT_SIGNATURE );
345 sig = node->pkt->pkt.signature;
347 algo = sig->digest_algo;
349 return G10ERR_PUBKEY_ALGO;
350 if( (rc=check_digest_algo(algo)) )
355 if( c->mfx.md->list )
356 m_check( c->mfx.md->list );
359 if( sig->sig_class == 0x00 ) {
361 md = md_copy( c->mfx.md );
362 else /* detached signature */
363 md = md_open( 0, 0 ); /* signature_check() will enable the md*/
365 else if( sig->sig_class == 0x01 ) {
366 /* how do we know that we have to hash the (already hashed) text
367 * in canonical mode ??? (calculating both modes???) */
369 md = md_copy( c->mfx.md );
370 else /* detached signature */
371 md = md_open( 0, 0 ); /* signature_check() will enable the md*/
373 else if( (sig->sig_class&~3) == 0x10
374 || sig->sig_class == 0x18
375 || sig->sig_class == 0x20
376 || sig->sig_class == 0x30 ) { /* classes 0x10..0x17,0x20,0x30 */
377 if( c->list->pkt->pkttype == PKT_PUBLIC_KEY
378 || c->list->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
379 return check_key_signature( c->list, node, is_selfsig );
382 log_error("invalid root packet for sigclass %02x\n",
384 return G10ERR_SIG_CLASS;
388 return G10ERR_SIG_CLASS;
389 rc = signature_check( sig, md );
398 print_userid( PACKET *pkt )
402 if( pkt->pkttype != PKT_USER_ID ) {
403 printf("ERROR: unexpected packet type %d", pkt->pkttype );
406 print_string( stdout, pkt->pkt.user_id->name, pkt->pkt.user_id->len,
412 print_fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
417 p = array = sk? fingerprint_from_sk( sk, NULL, &n )
418 : fingerprint_from_pk( pk, NULL, &n );
419 if( opt.with_colons ) {
420 printf("fpr:::::::::");
421 for(i=0; i < n ; i++, p++ )
426 printf(" Key fingerprint =");
428 for(i=0; i < n ; i++, i++, p += 2 ) {
431 printf(" %02X%02X", *p, p[1] );
435 for(i=0; i < n ; i++, p++ ) {
438 printf(" %02X", *p );
448 * List the certificate in a user friendly way
452 list_node( CTX c, KBNODE node )
459 else if( (mainkey = (node->pkt->pkttype == PKT_PUBLIC_KEY) )
460 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
461 PKT_public_key *pk = node->pkt->pkt.public_key;
463 if( opt.with_colons ) {
465 keyid_from_pk( pk, keyid );
467 c->local_id = pk->local_id;
468 c->trustletter = query_trust_info( pk );
470 printf("%s:%c:%u:%d:%08lX%08lX:%s:%u:",
471 mainkey? "pub":"sub",
475 (ulong)keyid[0],(ulong)keyid[1],
476 datestr_from_pk( pk ),
477 (unsigned)pk->valid_days );
479 printf("%lu", c->local_id );
481 /* fixme: add ownertrust here */
485 printf("%s %4u%c/%08lX %s ",
486 mainkey? "pub":"sub",
488 pubkey_letter( pk->pubkey_algo ),
489 (ulong)keyid_from_pk( pk, NULL ),
490 datestr_from_pk( pk ) );
492 /* and now list all userids with their signatures */
493 for( node = node->next; node; node = node->next ) {
494 if( node->pkt->pkttype == PKT_SIGNATURE ) {
496 if( node->pkt->pkt.signature->sig_class == 0x20 )
504 else if( node->pkt->pkttype == PKT_USER_ID ) {
506 if( opt.with_colons )
507 printf("uid:::::::::");
509 printf( "uid%*s", 28, "" );
511 print_userid( node->pkt );
512 if( opt.with_colons )
515 if( opt.fingerprint && !any )
516 print_fingerprint( pk, NULL );
519 else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
530 if( !mainkey && opt.fingerprint > 1 )
531 print_fingerprint( pk, NULL );
533 else if( (mainkey = (node->pkt->pkttype == PKT_SECRET_KEY) )
534 || node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
535 PKT_secret_key *sk = node->pkt->pkt.secret_key;
537 if( opt.with_colons ) {
539 keyid_from_sk( sk, keyid );
540 printf("%s::%u:%d:%08lX%08lX:%s:%u:::",
541 mainkey? "sec":"ssb",
544 (ulong)keyid[0],(ulong)keyid[1],
545 datestr_from_sk( sk ),
546 (unsigned)sk->valid_days
547 /* fixme: add LID */ );
550 printf("%s %4u%c/%08lX %s ",
551 mainkey? "sec":"ssb",
553 pubkey_letter( sk->pubkey_algo ),
554 (ulong)keyid_from_sk( sk, NULL ),
555 datestr_from_sk( sk ) );
557 /* and now list all userids with their signatures */
558 for( node = node->next; node; node = node->next ) {
559 if( node->pkt->pkttype == PKT_SIGNATURE ) {
561 if( node->pkt->pkt.signature->sig_class == 0x20 )
569 else if( node->pkt->pkttype == PKT_USER_ID ) {
571 if( opt.with_colons )
572 printf("uid:::::::::");
574 printf( "uid%*s", 28, "" );
576 print_userid( node->pkt );
577 if( opt.with_colons )
580 if( opt.fingerprint && !any )
581 print_fingerprint( NULL, sk );
584 else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
595 if( !mainkey && opt.fingerprint > 1 )
596 print_fingerprint( NULL, sk );
598 else if( node->pkt->pkttype == PKT_SIGNATURE ) {
599 PKT_signature *sig = node->pkt->pkt.signature;
609 if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
610 fputs("rev", stdout);
612 fputs("sig", stdout);
613 if( opt.check_sigs ) {
615 switch( (rc2=do_check_sig( c, node, &is_selfsig )) ) {
616 case 0: sigrc = '!'; break;
617 case G10ERR_BAD_SIGN: sigrc = '-'; break;
618 case G10ERR_NO_PUBKEY: sigrc = '?'; break;
619 default: sigrc = '%'; break;
622 else { /* check whether this is a self signature */
625 if( c->list->pkt->pkttype == PKT_PUBLIC_KEY
626 || c->list->pkt->pkttype == PKT_SECRET_KEY ) {
627 if( c->list->pkt->pkttype == PKT_PUBLIC_KEY )
628 keyid_from_pk( c->list->pkt->pkt.public_key, keyid );
630 keyid_from_sk( c->list->pkt->pkt.secret_key, keyid );
632 if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] )
636 if( opt.with_colons ) {
640 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
641 (ulong)sig->keyid[1], datestr_from_sig(sig));
644 printf("%c %08lX %s ",
645 sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
647 printf("[%s] ", g10_errstr(rc2) );
648 else if( sigrc == '?' )
650 else if( is_selfsig ) {
651 if( opt.with_colons )
653 fputs( sig->sig_class == 0x18? "[keybind]":"[selfsig]", stdout);
654 if( opt.with_colons )
658 p = get_user_id( sig->keyid, &n );
659 print_string( stdout, p, n, opt.with_colons );
662 if( opt.with_colons )
663 printf(":%02x:", sig->sig_class );
667 log_error("invalid node with packet of type %d\n", node->pkt->pkttype);
672 proc_packets( IOBUF a )
674 CTX c = m_alloc_clear( sizeof *c );
675 int rc = do_proc_packets( c, a );
681 proc_signature_packets( IOBUF a, STRLIST signedfiles, const char *sigfilename )
683 CTX c = m_alloc_clear( sizeof *c );
686 c->signed_data = signedfiles;
687 c->sigfilename = sigfilename;
688 rc = do_proc_packets( c, a );
694 proc_encryption_packets( IOBUF a )
696 CTX c = m_alloc_clear( sizeof *c );
699 rc = do_proc_packets( c, a );
706 do_proc_packets( CTX c, IOBUF a )
708 PACKET *pkt = m_alloc( sizeof *pkt );
714 while( (rc=parse_packet(a, pkt)) != -1 ) {
715 /* cleanup if we have an illegal data structure */
716 if( c->dek && pkt->pkttype != PKT_ENCRYPTED ) {
717 /* FIXME: do we need to ave it in case we have no secret
718 * key for one of the next reciepents- we should check it
720 m_free(c->dek); c->dek = NULL; /* burn it */
725 if( rc == G10ERR_INVALID_PACKET )
730 if( opt.list_packets ) {
731 switch( pkt->pkttype ) {
732 case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
733 case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
734 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
735 default: newpkt = 0; break;
738 else if( c->sigs_only ) {
739 switch( pkt->pkttype ) {
746 rc = G10ERR_UNEXPECTED;
748 case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
749 case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
750 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
751 case PKT_ONEPASS_SIG: newpkt = add_onepass_sig( c, pkt ); break;
752 default: newpkt = 0; break;
755 else if( c->encrypt_only ) {
756 switch( pkt->pkttype ) {
760 rc = G10ERR_UNEXPECTED;
762 case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
763 case PKT_SYMKEY_ENC: proc_symkey_enc( c, pkt ); break;
764 case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
765 case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
766 case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
767 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
768 case PKT_ONEPASS_SIG: newpkt = add_onepass_sig( c, pkt ); break;
769 default: newpkt = 0; break;
773 switch( pkt->pkttype ) {
777 c->list = new_kbnode( pkt );
780 case PKT_PUBLIC_SUBKEY:
781 case PKT_SECRET_SUBKEY:
782 newpkt = add_subkey( c, pkt );
784 case PKT_USER_ID: newpkt = add_user_id( c, pkt ); break;
785 case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
786 case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
787 case PKT_SYMKEY_ENC: proc_symkey_enc( c, pkt ); break;
788 case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
789 case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
790 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
791 case PKT_ONEPASS_SIG: newpkt = add_onepass_sig( c, pkt ); break;
792 default: newpkt = 0; break;
795 if( pkt->pkttype != PKT_SIGNATURE )
796 c->have_data = pkt->pkttype == PKT_PLAINTEXT;
801 pkt = m_alloc( sizeof *pkt );
814 free_md_filter_context( &c->mfx );
820 print_keyid( FILE *fp, u32 *keyid )
823 char *p = get_user_id( keyid, &n );
824 print_string( fp, p, n, opt.with_colons );
831 check_sig_and_print( CTX c, KBNODE node )
833 PKT_signature *sig = node->pkt->pkt.signature;
834 const char *astr, *tstr;
837 if( opt.skip_verify ) {
838 log_info("signature verification suppressed\n");
842 tstr = asctimestamp(sig->timestamp);
843 astr = pubkey_algo_to_string( sig->pubkey_algo );
844 log_info(_("Signature made %.*s using %s key ID %08lX\n"),
845 (int)strlen(tstr)-1, tstr, astr? astr: "?", (ulong)sig->keyid[1] );
847 rc = do_check_sig(c, node, NULL );
848 if( !rc || rc == G10ERR_BAD_SIGN ) {
849 write_status( rc? STATUS_BADSIG : STATUS_GOODSIG );
850 log_info(rc? _("BAD signature from \"")
851 : _("Good signature from \""));
852 print_keyid( stderr, sig->keyid );
856 rc = check_signatures_trust( sig );
857 if( opt.batch && rc )
861 write_status( STATUS_ERRSIG );
862 log_error(_("Can't check signature: %s\n"), g10_errstr(rc) );
869 * Process the tree which starts at node
872 proc_tree( CTX c, KBNODE node )
877 if( opt.list_packets )
881 c->trustletter = ' ';
882 if( node->pkt->pkttype == PKT_PUBLIC_KEY
883 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
884 list_node( c, node );
885 else if( node->pkt->pkttype == PKT_SECRET_KEY )
886 list_node( c, node );
887 else if( node->pkt->pkttype == PKT_ONEPASS_SIG ) {
888 /* check all signatures */
889 if( !c->have_data ) {
890 free_md_filter_context( &c->mfx );
891 /* prepare to create all requested message digests */
892 c->mfx.md = md_open(0, 0);
893 /* fixme: why looking for the signature packet and not 1passpacket*/
894 for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) {
895 md_enable( c->mfx.md, n1->pkt->pkt.signature->digest_algo);
897 /* ask for file and hash it */
899 rc = hash_datafiles( c->mfx.md, c->signed_data, c->sigfilename,
900 n1? (n1->pkt->pkt.onepass_sig->sig_class == 0x01):0 );
902 rc = ask_for_detached_datafile( &c->mfx,
903 iobuf_get_fname(c->iobuf));
905 log_error("can't hash datafile: %s\n", g10_errstr(rc));
910 for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); )
911 check_sig_and_print( c, n1 );
913 else if( node->pkt->pkttype == PKT_SIGNATURE ) {
914 PKT_signature *sig = node->pkt->pkt.signature;
916 if( !c->have_data ) {
917 free_md_filter_context( &c->mfx );
918 c->mfx.md = md_open(sig->digest_algo, 0);
920 rc = hash_datafiles( c->mfx.md, c->signed_data, c->sigfilename,
921 sig->sig_class == 0x01 );
923 rc = ask_for_detached_datafile( &c->mfx,
924 iobuf_get_fname(c->iobuf));
926 log_error("can't hash datafile: %s\n", g10_errstr(rc));
931 log_info("old style signature\n");
933 check_sig_and_print( c, node );
936 log_error("proc_tree: invalid root packet\n");