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) ) {
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;
192 result = G10ERR_PUBKEY_ALGO;
197 if( opt.verbose > 1 )
198 log_info( _("public key encrypted data: Good DEK\n") );
201 log_error(_("public key decryption failed: %s\n"), g10_errstr(result));
208 proc_encrypted( CTX c, PACKET *pkt )
212 /*printf("dat: %sencrypted data\n", c->dek?"":"conventional ");*/
213 if( !c->dek && !c->last_was_session_key ) {
214 /* assume this is old conventional encrypted data */
215 c->dek = passphrase_to_dek( NULL,
216 opt.def_cipher_algo ? opt.def_cipher_algo
217 : DEFAULT_CIPHER_ALGO, NULL, 0 );
220 result = G10ERR_NO_SECKEY;
222 result = decrypt_data( pkt->pkt.encrypted, c->dek );
223 m_free(c->dek); c->dek = NULL;
227 if( opt.verbose > 1 )
228 log_info(_("decryption okay\n"));
231 log_error(_("decryption failed: %s\n"), g10_errstr(result));
232 /* FIXME: if this is secret key not available, try with
236 c->last_was_session_key = 0;
241 proc_plaintext( CTX c, PACKET *pkt )
243 PKT_plaintext *pt = pkt->pkt.plaintext;
244 int any, clearsig, rc;
247 if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) )
248 log_info(_("NOTE: sender requested \"for-your-eyes-only\"\n"));
249 else if( opt.verbose )
250 log_info(_("original file name='%.*s'\n"), pt->namelen, pt->name);
251 free_md_filter_context( &c->mfx );
252 c->mfx.md = md_open( 0, 0);
253 /* fixme: we may need to push the textfilter if we have sigclass 1
254 * and no armoring - Not yet tested */
256 for(n=c->list; n; n = n->next ) {
257 if( n->pkt->pkttype == PKT_ONEPASS_SIG ) {
258 if( n->pkt->pkt.onepass_sig->digest_algo ) {
259 md_enable( c->mfx.md, n->pkt->pkt.onepass_sig->digest_algo );
262 /* Check whether this is a cleartext signature. We assume that
263 * we have one if the sig_class is 1 and the keyid is 0, that
264 * are the faked packets produced by armor.c. There is a
265 * possibility that this fails, but there is no other easy way
266 * to do it. (We could use a special packet type to indicate
267 * this, but this may also be faked - it simply can't be verified
268 * and is _no_ security issue)
270 if( n->pkt->pkt.onepass_sig->sig_class == 0x01
271 && !n->pkt->pkt.onepass_sig->keyid[0]
272 && !n->pkt->pkt.onepass_sig->keyid[1] )
276 if( !any ) { /* no onepass sig packet: enable all standard algos */
277 md_enable( c->mfx.md, DIGEST_ALGO_RMD160 );
278 md_enable( c->mfx.md, DIGEST_ALGO_SHA1 );
279 md_enable( c->mfx.md, DIGEST_ALGO_MD5 );
283 if( c->mfx.md->list )
284 m_check( c->mfx.md->list );
286 rc = handle_plaintext( pt, &c->mfx, c->sigs_only, clearsig );
287 if( rc == G10ERR_CREATE_FILE && !c->sigs_only) {
288 /* can't write output but we hash it anyway to
289 * check the signature */
290 rc = handle_plaintext( pt, &c->mfx, 1, clearsig );
293 log_error( "handle plaintext failed: %s\n", g10_errstr(rc));
295 c->last_was_session_key = 0;
300 proc_compressed_cb( IOBUF a, void *info )
302 return proc_signature_packets( a, ((CTX)info)->signed_data,
303 ((CTX)info)->sigfilename );
307 proc_encrypt_cb( IOBUF a, void *info )
309 return proc_encryption_packets( a );
313 proc_compressed( CTX c, PACKET *pkt )
315 PKT_compressed *zd = pkt->pkt.compressed;
318 /*printf("zip: compressed data packet\n");*/
320 rc = handle_compressed( zd, proc_compressed_cb, c );
321 else if( c->encrypt_only )
322 rc = handle_compressed( zd, proc_encrypt_cb, c );
324 rc = handle_compressed( zd, NULL, NULL );
326 log_error("uncompressing failed: %s\n", g10_errstr(rc));
328 c->last_was_session_key = 0;
335 * check the signature
336 * Returns: 0 = valid signature or an error code
339 do_check_sig( CTX c, KBNODE node, int *is_selfsig )
345 assert( node->pkt->pkttype == PKT_SIGNATURE );
348 sig = node->pkt->pkt.signature;
350 algo = sig->digest_algo;
352 return G10ERR_PUBKEY_ALGO;
353 if( (rc=check_digest_algo(algo)) )
358 if( c->mfx.md->list )
359 m_check( c->mfx.md->list );
362 if( sig->sig_class == 0x00 ) {
364 md = md_copy( c->mfx.md );
365 else /* detached signature */
366 md = md_open( 0, 0 ); /* signature_check() will enable the md*/
368 else if( sig->sig_class == 0x01 ) {
369 /* how do we know that we have to hash the (already hashed) text
370 * in canonical mode ??? (calculating both modes???) */
372 md = md_copy( c->mfx.md );
373 else /* detached signature */
374 md = md_open( 0, 0 ); /* signature_check() will enable the md*/
376 else if( (sig->sig_class&~3) == 0x10
377 || sig->sig_class == 0x18
378 || sig->sig_class == 0x20
379 || sig->sig_class == 0x30 ) { /* classes 0x10..0x17,0x20,0x30 */
380 if( c->list->pkt->pkttype == PKT_PUBLIC_KEY
381 || c->list->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
382 return check_key_signature( c->list, node, is_selfsig );
385 log_error("invalid root packet for sigclass %02x\n",
387 return G10ERR_SIG_CLASS;
391 return G10ERR_SIG_CLASS;
392 rc = signature_check( sig, md );
401 print_userid( PACKET *pkt )
405 if( pkt->pkttype != PKT_USER_ID ) {
406 printf("ERROR: unexpected packet type %d", pkt->pkttype );
409 print_string( stdout, pkt->pkt.user_id->name, pkt->pkt.user_id->len,
415 print_fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
417 byte array[MAX_FINGERPRINT_LEN], *p;
421 fingerprint_from_sk( sk, array, &n );
423 fingerprint_from_pk( pk, array, &n );
425 if( opt.with_colons ) {
426 printf("fpr:::::::::");
427 for(i=0; i < n ; i++, p++ )
432 printf(" Key fingerprint =");
434 for(i=0; i < n ; i++, i++, p += 2 ) {
437 printf(" %02X%02X", *p, p[1] );
441 for(i=0; i < n ; i++, p++ ) {
444 printf(" %02X", *p );
453 * List the certificate in a user friendly way
457 list_node( CTX c, KBNODE node )
464 else if( (mainkey = (node->pkt->pkttype == PKT_PUBLIC_KEY) )
465 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
466 PKT_public_key *pk = node->pkt->pkt.public_key;
468 if( opt.with_colons ) {
470 keyid_from_pk( pk, keyid );
472 c->local_id = pk->local_id;
473 c->trustletter = query_trust_info( pk );
475 printf("%s:%c:%u:%d:%08lX%08lX:%s:%s:",
476 mainkey? "pub":"sub",
480 (ulong)keyid[0],(ulong)keyid[1],
481 datestr_from_pk( pk ),
482 pk->expiredate? strtimestamp(pk->expiredate):"" );
484 printf("%lu", c->local_id );
486 /* fixme: add ownertrust here */
490 printf("%s %4u%c/%08lX %s ",
491 mainkey? "pub":"sub",
493 pubkey_letter( pk->pubkey_algo ),
494 (ulong)keyid_from_pk( pk, NULL ),
495 datestr_from_pk( pk ) );
497 /* and now list all userids with their signatures */
498 for( node = node->next; node; node = node->next ) {
499 if( node->pkt->pkttype == PKT_SIGNATURE ) {
501 if( node->pkt->pkt.signature->sig_class == 0x20 )
509 else if( node->pkt->pkttype == PKT_USER_ID ) {
511 if( opt.with_colons )
512 printf("uid:::::::::");
514 printf( "uid%*s", 28, "" );
516 print_userid( node->pkt );
517 if( opt.with_colons )
520 if( opt.fingerprint && !any )
521 print_fingerprint( pk, NULL );
524 else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
535 if( !mainkey && opt.fingerprint > 1 )
536 print_fingerprint( pk, NULL );
538 else if( (mainkey = (node->pkt->pkttype == PKT_SECRET_KEY) )
539 || node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
540 PKT_secret_key *sk = node->pkt->pkt.secret_key;
542 if( opt.with_colons ) {
544 keyid_from_sk( sk, keyid );
545 printf("%s::%u:%d:%08lX%08lX:%s:%s:::",
546 mainkey? "sec":"ssb",
549 (ulong)keyid[0],(ulong)keyid[1],
550 datestr_from_sk( sk ),
551 sk->expiredate? strtimestamp(sk->expiredate):""
552 /* fixme: add LID */ );
555 printf("%s %4u%c/%08lX %s ",
556 mainkey? "sec":"ssb",
558 pubkey_letter( sk->pubkey_algo ),
559 (ulong)keyid_from_sk( sk, NULL ),
560 datestr_from_sk( sk ) );
562 /* and now list all userids with their signatures */
563 for( node = node->next; node; node = node->next ) {
564 if( node->pkt->pkttype == PKT_SIGNATURE ) {
566 if( node->pkt->pkt.signature->sig_class == 0x20 )
574 else if( node->pkt->pkttype == PKT_USER_ID ) {
576 if( opt.with_colons )
577 printf("uid:::::::::");
579 printf( "uid%*s", 28, "" );
581 print_userid( node->pkt );
582 if( opt.with_colons )
585 if( opt.fingerprint && !any )
586 print_fingerprint( NULL, sk );
589 else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
600 if( !mainkey && opt.fingerprint > 1 )
601 print_fingerprint( NULL, sk );
603 else if( node->pkt->pkttype == PKT_SIGNATURE ) {
604 PKT_signature *sig = node->pkt->pkt.signature;
614 if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
615 fputs("rev", stdout);
617 fputs("sig", stdout);
618 if( opt.check_sigs ) {
620 switch( (rc2=do_check_sig( c, node, &is_selfsig )) ) {
621 case 0: sigrc = '!'; break;
622 case G10ERR_BAD_SIGN: sigrc = '-'; break;
623 case G10ERR_NO_PUBKEY: sigrc = '?'; break;
624 default: sigrc = '%'; break;
627 else { /* check whether this is a self signature */
630 if( c->list->pkt->pkttype == PKT_PUBLIC_KEY
631 || c->list->pkt->pkttype == PKT_SECRET_KEY ) {
632 if( c->list->pkt->pkttype == PKT_PUBLIC_KEY )
633 keyid_from_pk( c->list->pkt->pkt.public_key, keyid );
635 keyid_from_sk( c->list->pkt->pkt.secret_key, keyid );
637 if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] )
641 if( opt.with_colons ) {
645 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
646 (ulong)sig->keyid[1], datestr_from_sig(sig));
649 printf("%c %08lX %s ",
650 sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
652 printf("[%s] ", g10_errstr(rc2) );
653 else if( sigrc == '?' )
655 else if( is_selfsig ) {
656 if( opt.with_colons )
658 fputs( sig->sig_class == 0x18? "[keybind]":"[selfsig]", stdout);
659 if( opt.with_colons )
663 p = get_user_id( sig->keyid, &n );
664 print_string( stdout, p, n, opt.with_colons );
667 if( opt.with_colons )
668 printf(":%02x:", sig->sig_class );
672 log_error("invalid node with packet of type %d\n", node->pkt->pkttype);
677 proc_packets( IOBUF a )
679 CTX c = m_alloc_clear( sizeof *c );
680 int rc = do_proc_packets( c, a );
686 proc_signature_packets( IOBUF a, STRLIST signedfiles, const char *sigfilename )
688 CTX c = m_alloc_clear( sizeof *c );
691 c->signed_data = signedfiles;
692 c->sigfilename = sigfilename;
693 rc = do_proc_packets( c, a );
699 proc_encryption_packets( IOBUF a )
701 CTX c = m_alloc_clear( sizeof *c );
704 rc = do_proc_packets( c, a );
711 do_proc_packets( CTX c, IOBUF a )
713 PACKET *pkt = m_alloc( sizeof *pkt );
719 while( (rc=parse_packet(a, pkt)) != -1 ) {
723 if( rc == G10ERR_INVALID_PACKET )
728 if( opt.list_packets ) {
729 switch( pkt->pkttype ) {
730 case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
731 case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
732 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
733 default: newpkt = 0; break;
736 else if( c->sigs_only ) {
737 switch( pkt->pkttype ) {
744 rc = G10ERR_UNEXPECTED;
746 case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
747 case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
748 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
749 case PKT_ONEPASS_SIG: newpkt = add_onepass_sig( c, pkt ); break;
750 default: newpkt = 0; break;
753 else if( c->encrypt_only ) {
754 switch( pkt->pkttype ) {
758 rc = G10ERR_UNEXPECTED;
760 case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
761 case PKT_SYMKEY_ENC: proc_symkey_enc( c, pkt ); break;
762 case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
763 case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
764 case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
765 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
766 case PKT_ONEPASS_SIG: newpkt = add_onepass_sig( c, pkt ); break;
767 default: newpkt = 0; break;
771 switch( pkt->pkttype ) {
775 c->list = new_kbnode( pkt );
778 case PKT_PUBLIC_SUBKEY:
779 case PKT_SECRET_SUBKEY:
780 newpkt = add_subkey( c, pkt );
782 case PKT_USER_ID: newpkt = add_user_id( c, pkt ); break;
783 case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
784 case PKT_PUBKEY_ENC: proc_pubkey_enc( c, pkt ); break;
785 case PKT_SYMKEY_ENC: proc_symkey_enc( c, pkt ); break;
786 case PKT_ENCRYPTED: proc_encrypted( c, pkt ); break;
787 case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
788 case PKT_COMPRESSED: proc_compressed( c, pkt ); break;
789 case PKT_ONEPASS_SIG: newpkt = add_onepass_sig( c, pkt ); break;
790 default: newpkt = 0; break;
793 if( pkt->pkttype != PKT_SIGNATURE )
794 c->have_data = pkt->pkttype == PKT_PLAINTEXT;
799 pkt = m_alloc( sizeof *pkt );
812 free_md_filter_context( &c->mfx );
818 print_keyid( FILE *fp, u32 *keyid )
821 char *p = get_user_id( keyid, &n );
822 print_string( fp, p, n, opt.with_colons );
829 check_sig_and_print( CTX c, KBNODE node )
831 PKT_signature *sig = node->pkt->pkt.signature;
832 const char *astr, *tstr;
835 if( opt.skip_verify ) {
836 log_info(_("signature verification suppressed\n"));
840 tstr = asctimestamp(sig->timestamp);
841 astr = pubkey_algo_to_string( sig->pubkey_algo );
842 log_info(_("Signature made %.*s using %s key ID %08lX\n"),
843 (int)strlen(tstr), tstr, astr? astr: "?", (ulong)sig->keyid[1] );
845 rc = do_check_sig(c, node, NULL );
846 if( !rc || rc == G10ERR_BAD_SIGN ) {
847 char *us = get_user_id_string( sig->keyid );
848 write_status_text( rc? STATUS_BADSIG : STATUS_GOODSIG, us );
850 log_info(rc? _("BAD signature from \"")
851 : _("Good signature from \""));
852 print_keyid( stderr, sig->keyid );
856 rc = check_signatures_trust( sig );
859 if( opt.batch && rc )
863 write_status( STATUS_ERRSIG );
864 log_error(_("Can't check signature: %s\n"), g10_errstr(rc) );
871 * Process the tree which starts at node
874 proc_tree( CTX c, KBNODE node )
879 if( opt.list_packets )
883 c->trustletter = ' ';
884 if( node->pkt->pkttype == PKT_PUBLIC_KEY
885 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
886 merge_keys_and_selfsig( node );
887 list_node( c, node );
889 else if( node->pkt->pkttype == PKT_SECRET_KEY ) {
890 merge_keys_and_selfsig( node );
891 list_node( c, node );
893 else if( node->pkt->pkttype == PKT_ONEPASS_SIG ) {
894 /* check all signatures */
895 if( !c->have_data ) {
896 free_md_filter_context( &c->mfx );
897 /* prepare to create all requested message digests */
898 c->mfx.md = md_open(0, 0);
899 /* fixme: why looking for the signature packet and not 1passpacket*/
900 for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) {
901 md_enable( c->mfx.md, n1->pkt->pkt.signature->digest_algo);
903 /* ask for file and hash it */
905 rc = hash_datafiles( c->mfx.md, c->signed_data, c->sigfilename,
906 n1? (n1->pkt->pkt.onepass_sig->sig_class == 0x01):0 );
908 rc = ask_for_detached_datafile( &c->mfx,
909 iobuf_get_fname(c->iobuf));
911 log_error("can't hash datafile: %s\n", g10_errstr(rc));
916 for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); )
917 check_sig_and_print( c, n1 );
919 else if( node->pkt->pkttype == PKT_SIGNATURE ) {
920 PKT_signature *sig = node->pkt->pkt.signature;
922 if( !c->have_data ) {
923 free_md_filter_context( &c->mfx );
924 c->mfx.md = md_open(sig->digest_algo, 0);
926 rc = hash_datafiles( c->mfx.md, c->signed_data, c->sigfilename,
927 sig->sig_class == 0x01 );
929 rc = ask_for_detached_datafile( &c->mfx,
930 iobuf_get_fname(c->iobuf));
932 log_error("can't hash datafile: %s\n", g10_errstr(rc));
937 log_info(_("old style (PGP 2.x) signature\n"));
939 check_sig_and_print( c, node );
942 log_error(_("invalid root packet detected in proc_tree()\n"));