1 /* mainproc.c - handle packets
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 * 2005, 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 #include "keyserver-internal.h"
48 struct kidlist_item
*next
;
56 * Structure to hold the context
58 typedef struct mainproc_context
*CTX
;
59 struct mainproc_context
61 struct mainproc_context
*anchor
; /* May be useful in the future. */
62 PKT_public_key
*last_pubkey
;
63 PKT_secret_key
*last_seckey
;
64 PKT_user_id
*last_user_id
;
65 md_filter_context_t mfx
;
66 int sigs_only
; /* Process only signatures and reject all other stuff. */
67 int encrypt_only
; /* Process only encryption messages. */
69 const char *sigfilename
;
71 int last_was_session_key
;
72 KBNODE list
; /* The current list of packets. */
74 IOBUF iobuf
; /* Used to get the filename etc. */
75 int trustletter
; /* Temporary usage in list_node. */
77 struct kidlist_item
*pkenc_list
; /* List of encryption packets. */
78 int any_sig_seen
; /* Set to true if a signature packet has been seen. */
82 static int do_proc_packets( CTX c
, IOBUF a
);
84 static void list_node( CTX c
, KBNODE node
);
85 static void proc_tree( CTX c
, KBNODE node
);
93 proc_tree(c
, c
->list
);
94 release_kbnode( c
->list
);
95 while( c
->pkenc_list
) {
96 struct kidlist_item
*tmp
= c
->pkenc_list
->next
;
97 xfree( c
->pkenc_list
);
100 c
->pkenc_list
= NULL
;
103 c
->last_was_session_key
= 0;
104 xfree(c
->dek
); c
->dek
= NULL
;
109 add_onepass_sig( CTX c
, PACKET
*pkt
)
113 if ( c
->list
) /* add another packet */
114 add_kbnode( c
->list
, new_kbnode( pkt
));
115 else /* insert the first one */
116 c
->list
= node
= new_kbnode( pkt
);
123 add_gpg_control( CTX c
, PACKET
*pkt
)
125 if ( pkt
->pkt
.gpg_control
->control
== CTRLPKT_CLEARSIGN_START
) {
126 /* New clear text signature.
127 * Process the last one and reset everything */
131 if( c
->list
) /* add another packet */
132 add_kbnode( c
->list
, new_kbnode( pkt
));
133 else /* insert the first one */
134 c
->list
= new_kbnode( pkt
);
142 add_user_id( CTX c
, PACKET
*pkt
)
145 log_error("orphaned user ID\n" );
148 add_kbnode( c
->list
, new_kbnode( pkt
) );
153 add_subkey( CTX c
, PACKET
*pkt
)
156 log_error("subkey w/o mainkey\n" );
159 add_kbnode( c
->list
, new_kbnode( pkt
) );
164 add_ring_trust( CTX c
, PACKET
*pkt
)
167 log_error("ring trust w/o key\n" );
170 add_kbnode( c
->list
, new_kbnode( pkt
) );
176 add_signature( CTX c
, PACKET
*pkt
)
181 if( pkt
->pkttype
== PKT_SIGNATURE
&& !c
->list
) {
182 /* This is the first signature for the following datafile.
183 * GPG does not write such packets; instead it always uses
184 * onepass-sig packets. The drawback of PGP's method
185 * of prepending the signature to the data is
186 * that it is not possible to make a signature from data read
187 * from stdin. (GPG is able to read PGP stuff anyway.) */
188 node
= new_kbnode( pkt
);
193 return 0; /* oops (invalid packet sequence)*/
194 else if( !c
->list
->pkt
)
195 BUG(); /* so nicht */
197 /* add a new signature node id at the end */
198 node
= new_kbnode( pkt
);
199 add_kbnode( c
->list
, node
);
204 symkey_decrypt_seskey( DEK
*dek
, byte
*seskey
, size_t slen
)
208 if(slen
< 17 || slen
> 33)
210 log_error ( _("weird size for an encrypted session key (%d)\n"),
212 return G10ERR_BAD_KEY
;
215 if (gcry_cipher_open (&hd
, dek
->algo
, GCRY_CIPHER_MODE_CFB
, 1))
217 if (gcry_cipher_setkey ( hd
, dek
->key
, dek
->keylen
))
219 gcry_cipher_setiv ( hd
, NULL
, 0 );
220 gcry_cipher_decrypt ( hd
, seskey
, slen
, NULL
, 0 );
221 gcry_cipher_close ( hd
);
223 /* Now we replace the dek components with the real session key to
224 decrypt the contents of the sequencing packet. */
229 if(dek
->keylen
> DIM(dek
->key
))
232 /* This is not completely accurate, since a bad passphrase may have
233 resulted in a garbage algorithm byte, but it's close enough since
234 a bogus byte here will fail later. */
235 if(dek
->algo
==CIPHER_ALGO_IDEA
)
238 memcpy(dek
->key
, seskey
+ 1, dek
->keylen
);
240 /*log_hexdump( "thekey", dek->key, dek->keylen );*/
246 proc_symkey_enc( CTX c
, PACKET
*pkt
)
250 enc
= pkt
->pkt
.symkey_enc
;
252 log_error ("invalid symkey encrypted packet\n");
255 int algo
= enc
->cipher_algo
;
256 const char *s
= gcry_cipher_algo_name (algo
);
263 log_info(_("%s encrypted session key\n"), s
);
265 log_info(_("%s encrypted data\n"), s
);
269 log_error(_("encrypted with unknown algorithm %d\n"), algo
);
271 if(openpgp_md_test_algo (enc
->s2k
.hash_algo
))
273 log_error(_("passphrase generated with unknown digest"
274 " algorithm %d\n"),enc
->s2k
.hash_algo
);
278 c
->last_was_session_key
= 2;
279 if(!s
|| opt
.list_only
)
282 if(opt
.override_session_key
)
284 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
285 if(get_override_session_key(c
->dek
, opt
.override_session_key
))
295 c
->dek
= passphrase_to_dek (NULL
, 0, algo
, &enc
->s2k
, 0,
299 /* For unknown reasons passphrase_to_dek does only
300 return NULL if a new passphrase has been requested
301 and has not been repeated correctly. Thus even
302 with a cancel requested (by means of the gpg-agent)
303 it won't return NULL but an empty passphrase. We
304 take the most conservative approach for now and
305 work around it right here. */
314 /* FIXME: This doesn't work perfectly if a symmetric
315 key comes before a public key in the message - if
316 the user doesn't know the passphrase, then there is
317 a chance that the "decrypted" algorithm will happen
318 to be a valid one, which will make the returned dek
319 appear valid, so we won't try any public keys that
323 if(symkey_decrypt_seskey(c
->dek
, enc
->seskey
,
331 c
->dek
->algo_info_printed
= 1;
342 proc_pubkey_enc( CTX c
, PACKET
*pkt
)
347 /* check whether the secret key is available and store in this case */
348 c
->last_was_session_key
= 1;
349 enc
= pkt
->pkt
.pubkey_enc
;
350 /*printf("enc: encrypted by a pubkey with keyid %08lX\n", enc->keyid[1] );*/
351 /* Hmmm: why do I have this algo check here - anyway there is
352 * function to check it. */
354 log_info(_("public key is %s\n"), keystr(enc
->keyid
) );
356 if( is_status_enabled() ) {
358 sprintf(buf
, "%08lX%08lX %d 0",
359 (ulong
)enc
->keyid
[0], (ulong
)enc
->keyid
[1], enc
->pubkey_algo
);
360 write_status_text( STATUS_ENC_TO
, buf
);
363 if( !opt
.list_only
&& opt
.override_session_key
) {
364 /* It does not make much sense to store the session key in
365 * secure memory because it has already been passed on the
366 * command line and the GCHQ knows about it. */
367 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
368 result
= get_override_session_key ( c
->dek
, opt
.override_session_key
);
370 xfree(c
->dek
); c
->dek
= NULL
;
373 else if( is_ELGAMAL(enc
->pubkey_algo
)
374 || enc
->pubkey_algo
== PUBKEY_ALGO_DSA
375 || is_RSA(enc
->pubkey_algo
) ) {
376 /* FIXME: strore this all in a list and process it later */
378 if ( !c
->dek
&& ((!enc
->keyid
[0] && !enc
->keyid
[1])
379 || opt
.try_all_secrets
380 || !seckey_available( enc
->keyid
)) ) {
384 c
->dek
= xmalloc_secure_clear( sizeof *c
->dek
);
385 if( (result
= get_session_key( enc
, c
->dek
)) ) {
386 /* error: delete the DEK */
387 xfree(c
->dek
); c
->dek
= NULL
;
392 result
= G10ERR_NO_SECKEY
;
395 result
= G10ERR_PUBKEY_ALGO
;
401 /* store it for later display */
402 struct kidlist_item
*x
= xmalloc( sizeof *x
);
403 x
->kid
[0] = enc
->keyid
[0];
404 x
->kid
[1] = enc
->keyid
[1];
405 x
->pubkey_algo
= enc
->pubkey_algo
;
407 x
->next
= c
->pkenc_list
;
410 if( !result
&& opt
.verbose
> 1 )
411 log_info( _("public key encrypted data: good DEK\n") );
420 * Print the list of public key encrypted packets which we could
424 print_pkenc_list( struct kidlist_item
*list
, int failed
)
426 for( ; list
; list
= list
->next
) {
430 if ( failed
&& !list
->reason
)
432 if ( !failed
&& list
->reason
)
435 algstr
= gcry_pk_algo_name ( list
->pubkey_algo
);
436 pk
= xmalloc_clear( sizeof *pk
);
440 pk
->pubkey_algo
= list
->pubkey_algo
;
441 if( !get_pubkey( pk
, list
->kid
) )
444 log_info( _("encrypted with %u-bit %s key, ID %s, created %s\n"),
445 nbits_from_pk( pk
), algstr
, keystr_from_pk(pk
),
446 strtimestamp(pk
->timestamp
) );
447 p
=get_user_id_native(list
->kid
);
448 log_printf (_(" \"%s\"\n"),p
);
452 log_info(_("encrypted with %s key, ID %s\n"),
453 algstr
,keystr(list
->kid
));
455 free_public_key( pk
);
457 if( list
->reason
== G10ERR_NO_SECKEY
) {
458 if( is_status_enabled() ) {
460 sprintf(buf
,"%08lX%08lX", (ulong
)list
->kid
[0],
461 (ulong
)list
->kid
[1] );
462 write_status_text( STATUS_NO_SECKEY
, buf
);
465 else if (list
->reason
)
466 log_info(_("public key decryption failed: %s\n"),
467 g10_errstr(list
->reason
));
473 proc_encrypted( CTX c
, PACKET
*pkt
)
480 log_info(_("encrypted with %lu passphrases\n"),c
->symkeys
);
481 else if(c
->symkeys
==1)
482 log_info(_("encrypted with 1 passphrase\n"));
483 print_pkenc_list ( c
->pkenc_list
, 1 );
484 print_pkenc_list ( c
->pkenc_list
, 0 );
487 /* FIXME: Figure out the session key by looking at all pkenc packets. */
490 write_status( STATUS_BEGIN_DECRYPTION
);
492 /*log_debug("dat: %sencrypted data\n", c->dek?"":"conventional ");*/
495 else if( !c
->dek
&& !c
->last_was_session_key
) {
497 STRING2KEY s2kbuf
, *s2k
= NULL
;
499 if(opt
.override_session_key
)
501 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
502 result
=get_override_session_key(c
->dek
, opt
.override_session_key
);
511 /* Assume this is old style conventional encrypted data. */
512 algo
= opt
.def_cipher_algo
;
514 log_info (_("assuming %s encrypted data\n"),
515 gcry_cipher_algo_name (algo
));
516 else if ( gcry_cipher_test_algo (CIPHER_ALGO_IDEA
) )
518 algo
= opt
.def_cipher_algo
;
520 algo
= opt
.s2k_cipher_algo
;
522 log_info (_("IDEA cipher unavailable, "
523 "optimistically attempting to use %s instead\n"),
524 gcry_cipher_algo_name (algo
));
528 algo
= CIPHER_ALGO_IDEA
;
529 if (!opt
.s2k_digest_algo
)
531 /* If no digest is given we assume MD5 */
533 s2kbuf
.hash_algo
= DIGEST_ALGO_MD5
;
536 log_info (_("assuming %s encrypted data\n"), "IDEA");
539 c
->dek
= passphrase_to_dek ( NULL
, 0, algo
, s2k
, 0, NULL
, NULL
);
541 c
->dek
->algo_info_printed
= 1;
545 result
= G10ERR_NO_SECKEY
;
547 result
= decrypt_data( c
, pkt
->pkt
.encrypted
, c
->dek
);
551 else if( !result
|| (gpg_err_code (result
) == GPG_ERR_BAD_SIGNATURE
552 && opt
.ignore_mdc_error
)) {
553 write_status( STATUS_DECRYPTION_OKAY
);
554 if( opt
.verbose
> 1 )
555 log_info(_("decryption okay\n"));
556 if( pkt
->pkt
.encrypted
->mdc_method
&& !result
)
557 write_status( STATUS_GOODMDC
);
558 else if(!opt
.no_mdc_warn
)
559 log_info (_("WARNING: message was not integrity protected\n"));
560 if(opt
.show_session_key
)
563 char *buf
= xmalloc ( c
->dek
->keylen
*2 + 20 );
564 sprintf ( buf
, "%d:", c
->dek
->algo
);
565 for(i
=0; i
< c
->dek
->keylen
; i
++ )
566 sprintf(buf
+strlen(buf
), "%02X", c
->dek
->key
[i
] );
567 log_info( "session key: `%s'\n", buf
);
568 write_status_text ( STATUS_SESSION_KEY
, buf
);
571 else if( result
== G10ERR_BAD_SIGN
) {
572 log_error(_("WARNING: encrypted message has been manipulated!\n"));
573 write_status( STATUS_BADMDC
);
574 write_status( STATUS_DECRYPTION_FAILED
);
577 write_status( STATUS_DECRYPTION_FAILED
);
578 log_error(_("decryption failed: %s\n"), g10_errstr(result
));
579 /* Hmmm: does this work when we have encrypted using multiple
580 * ways to specify the session key (symmmetric and PK)*/
582 xfree(c
->dek
); c
->dek
= NULL
;
584 c
->last_was_session_key
= 0;
585 write_status( STATUS_END_DECRYPTION
);
590 proc_plaintext( CTX c
, PACKET
*pkt
)
592 PKT_plaintext
*pt
= pkt
->pkt
.plaintext
;
593 int any
, clearsig
, only_md5
, rc
;
596 if( pt
->namelen
== 8 && !memcmp( pt
->name
, "_CONSOLE", 8 ) )
597 log_info(_("NOTE: sender requested \"for-your-eyes-only\"\n"));
598 else if( opt
.verbose
)
599 log_info(_("original file name='%.*s'\n"), pt
->namelen
, pt
->name
);
600 free_md_filter_context( &c
->mfx
);
601 if (gcry_md_open (&c
->mfx
.md
, 0, 0))
603 /* fixme: we may need to push the textfilter if we have sigclass 1
604 * and no armoring - Not yet tested
605 * Hmmm, why don't we need it at all if we have sigclass 1
606 * Should we assume that plaintext in mode 't' has always sigclass 1??
607 * See: Russ Allbery's mail 1999-02-09
609 any
= clearsig
= only_md5
= 0;
610 for(n
=c
->list
; n
; n
= n
->next
)
612 if( n
->pkt
->pkttype
== PKT_ONEPASS_SIG
)
614 /* For the onepass signature case */
615 if( n
->pkt
->pkt
.onepass_sig
->digest_algo
)
617 gcry_md_enable (c
->mfx
.md
,
618 n
->pkt
->pkt
.onepass_sig
->digest_algo
);
619 if( !any
&& n
->pkt
->pkt
.onepass_sig
->digest_algo
626 if( n
->pkt
->pkt
.onepass_sig
->sig_class
!= 0x01 )
629 else if( n
->pkt
->pkttype
== PKT_GPG_CONTROL
630 && n
->pkt
->pkt
.gpg_control
->control
631 == CTRLPKT_CLEARSIGN_START
)
633 /* For the clearsigned message case */
634 size_t datalen
= n
->pkt
->pkt
.gpg_control
->datalen
;
635 const byte
*data
= n
->pkt
->pkt
.gpg_control
->data
;
637 /* check that we have at least the sigclass and one hash */
639 log_fatal("invalid control packet CTRLPKT_CLEARSIGN_START\n");
640 /* Note that we don't set the clearsig flag for not-dash-escaped
642 clearsig
= (*data
== 0x01);
643 for( data
++, datalen
--; datalen
; datalen
--, data
++ )
644 gcry_md_enable (c
->mfx
.md
, *data
);
646 break; /* Stop here as one-pass signature packets are not
649 else if(n
->pkt
->pkttype
==PKT_SIGNATURE
)
651 /* For the SIG+LITERAL case that PGP used to use. */
652 gcry_md_enable ( c
->mfx
.md
, n
->pkt
->pkt
.signature
->digest_algo
);
657 if( !any
&& !opt
.skip_verify
)
659 /* This is for the old GPG LITERAL+SIG case. It's not legal
660 according to 2440, so hopefully it won't come up that
661 often. There is no good way to specify what algorithms to
662 use in that case, so these three are the historical
664 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_RMD160
);
665 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_SHA1
);
666 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_MD5
);
668 if( opt
.pgp2_workarounds
&& only_md5
&& !opt
.skip_verify
) {
669 /* This is a kludge to work around a bug in pgp2. It does only
670 * catch those mails which are armored. To catch the non-armored
671 * pgp mails we could see whether there is the signature packet
672 * in front of the plaintext. If someone needs this, send me a patch.
674 if ( gcry_md_open (&c
->mfx
.md2
, DIGEST_ALGO_MD5
, 0) )
678 gcry_md_start_debug ( c
->mfx
.md
, "verify" );
680 gcry_md_start_debug ( c
->mfx
.md2
, "verify2" );
683 rc
= handle_plaintext( pt
, &c
->mfx
, c
->sigs_only
, clearsig
);
684 if ( gpg_err_code (rc
) == GPG_ERR_EACCES
&& !c
->sigs_only
)
686 /* Can't write output but we hash it anyway to check the
688 rc
= handle_plaintext( pt
, &c
->mfx
, 1, clearsig
);
692 log_error( "handle plaintext failed: %s\n", g10_errstr(rc
));
694 c
->last_was_session_key
= 0;
696 /* We add a marker control packet instead of the plaintext packet.
697 * This is so that we can later detect invalid packet sequences.
699 n
= new_kbnode (create_gpg_control (CTRLPKT_PLAINTEXT_MARK
, NULL
, 0));
701 add_kbnode (c
->list
, n
);
708 proc_compressed_cb( IOBUF a
, void *info
)
710 return proc_signature_packets( info
, a
, ((CTX
)info
)->signed_data
,
711 ((CTX
)info
)->sigfilename
);
715 proc_encrypt_cb( IOBUF a
, void *info
)
717 return proc_encryption_packets( info
, a
);
721 proc_compressed( CTX c
, PACKET
*pkt
)
723 PKT_compressed
*zd
= pkt
->pkt
.compressed
;
726 /*printf("zip: compressed data packet\n");*/
728 rc
=G10ERR_COMPR_ALGO
;
729 else if( c
->sigs_only
)
730 rc
= handle_compressed( c
, zd
, proc_compressed_cb
, c
);
731 else if( c
->encrypt_only
)
732 rc
= handle_compressed( c
, zd
, proc_encrypt_cb
, c
);
734 rc
= handle_compressed( c
, zd
, NULL
, NULL
);
736 log_error("uncompressing failed: %s\n", g10_errstr(rc
));
738 c
->last_was_session_key
= 0;
742 * check the signature
743 * Returns: 0 = valid signature or an error code
746 do_check_sig( CTX c
, KBNODE node
, int *is_selfsig
,
747 int *is_expkey
, int *is_revkey
)
750 gcry_md_hd_t md
= NULL
, md2
= NULL
;
753 assert( node
->pkt
->pkttype
== PKT_SIGNATURE
);
756 sig
= node
->pkt
->pkt
.signature
;
758 algo
= sig
->digest_algo
;
759 rc
= openpgp_md_test_algo(algo
);
763 if( sig
->sig_class
== 0x00 ) {
766 if (gcry_md_copy (&md
, c
->mfx
.md
))
769 else /* detached signature */
771 /* signature_check() will enable the md*/
772 if (gcry_md_open (&md
, 0, 0 ))
776 else if( sig
->sig_class
== 0x01 ) {
777 /* how do we know that we have to hash the (already hashed) text
778 * in canonical mode ??? (calculating both modes???) */
780 if (gcry_md_copy (&md
, c
->mfx
.md
))
782 if( c
->mfx
.md2
&& gcry_md_copy (&md2
, c
->mfx
.md2
))
785 else { /* detached signature */
786 log_debug("Do we really need this here?");
787 /* signature_check() will enable the md*/
788 if (gcry_md_open (&md
, 0, 0 ))
790 if (gcry_md_open (&md2
, 0, 0 ))
794 else if( (sig
->sig_class
&~3) == 0x10
795 || sig
->sig_class
== 0x18
796 || sig
->sig_class
== 0x1f
797 || sig
->sig_class
== 0x20
798 || sig
->sig_class
== 0x28
799 || sig
->sig_class
== 0x30 ) {
800 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
801 || c
->list
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
802 return check_key_signature( c
->list
, node
, is_selfsig
);
804 else if( sig
->sig_class
== 0x20 ) {
805 log_error (_("standalone revocation - "
806 "use \"gpg --import\" to apply\n"));
807 return G10ERR_NOT_PROCESSED
;
810 log_error("invalid root packet for sigclass %02x\n",
812 return G10ERR_SIG_CLASS
;
816 return G10ERR_SIG_CLASS
;
817 rc
= signature_check2( sig
, md
, NULL
, is_expkey
, is_revkey
, NULL
);
818 if( gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
&& md2
)
819 rc
= signature_check2( sig
, md2
, NULL
, is_expkey
, is_revkey
, NULL
);
828 print_userid( PACKET
*pkt
)
832 if( pkt
->pkttype
!= PKT_USER_ID
) {
833 printf("ERROR: unexpected packet type %d", pkt
->pkttype
);
836 if( opt
.with_colons
)
838 if(pkt
->pkt
.user_id
->attrib_data
)
840 pkt
->pkt
.user_id
->numattribs
,
841 pkt
->pkt
.user_id
->attrib_len
);
843 print_string( stdout
, pkt
->pkt
.user_id
->name
,
844 pkt
->pkt
.user_id
->len
, ':');
847 print_utf8_string( stdout
, pkt
->pkt
.user_id
->name
,
848 pkt
->pkt
.user_id
->len
);
853 * List the certificate in a user friendly way
857 list_node( CTX c
, KBNODE node
)
864 else if( (mainkey
= (node
->pkt
->pkttype
== PKT_PUBLIC_KEY
) )
865 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
866 PKT_public_key
*pk
= node
->pkt
->pkt
.public_key
;
868 if( opt
.with_colons
)
871 keyid_from_pk( pk
, keyid
);
873 c
->trustletter
= opt
.fast_list_mode
?
874 0 : get_validity_info( pk
, NULL
);
875 printf("%s:", mainkey
? "pub":"sub" );
877 putchar( c
->trustletter
);
878 printf(":%u:%d:%08lX%08lX:%s:%s::",
881 (ulong
)keyid
[0],(ulong
)keyid
[1],
882 colon_datestr_from_pk( pk
),
883 colon_strtime (pk
->expiredate
) );
884 if( mainkey
&& !opt
.fast_list_mode
)
885 putchar( get_ownertrust_info (pk
) );
887 if( node
->next
&& node
->next
->pkt
->pkttype
== PKT_RING_TRUST
) {
888 putchar('\n'); any
=1;
889 if( opt
.fingerprint
)
890 print_fingerprint( pk
, NULL
, 0 );
891 printf("rtv:1:%u:\n",
892 node
->next
->pkt
->pkt
.ring_trust
->trustval
);
896 printf("%s %4u%c/%s %s%s",
897 mainkey
? "pub":"sub", nbits_from_pk( pk
),
898 pubkey_letter( pk
->pubkey_algo
), keystr_from_pk( pk
),
899 datestr_from_pk( pk
), mainkey
?" ":"");
902 /* and now list all userids with their signatures */
903 for( node
= node
->next
; node
; node
= node
->next
) {
904 if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
906 if( node
->pkt
->pkt
.signature
->sig_class
== 0x20 )
914 else if( node
->pkt
->pkttype
== PKT_USER_ID
) {
916 if( opt
.with_colons
)
917 printf("%s:::::::::",
918 node
->pkt
->pkt
.user_id
->attrib_data
?"uat":"uid");
920 printf( "uid%*s", 28, "" );
922 print_userid( node
->pkt
);
923 if( opt
.with_colons
)
926 if( opt
.fingerprint
&& !any
)
927 print_fingerprint( pk
, NULL
, 0 );
930 && node
->next
->pkt
->pkttype
== PKT_RING_TRUST
) {
931 printf("rtv:2:%u:\n",
932 node
->next
->pkt
->pkt
.ring_trust
?
933 node
->next
->pkt
->pkt
.ring_trust
->trustval
: 0);
937 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
952 printf(_("revoked: %s"),revokestr_from_pk(pk
));
955 else if( pk
->expiredate
)
958 printf(_("expires: %s"),expirestr_from_pk(pk
));
965 if( !mainkey
&& opt
.fingerprint
> 1 )
966 print_fingerprint( pk
, NULL
, 0 );
968 else if( (mainkey
= (node
->pkt
->pkttype
== PKT_SECRET_KEY
) )
969 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
970 PKT_secret_key
*sk
= node
->pkt
->pkt
.secret_key
;
972 if( opt
.with_colons
)
975 keyid_from_sk( sk
, keyid
);
976 printf("%s::%u:%d:%08lX%08lX:%s:%s:::",
977 mainkey
? "sec":"ssb",
980 (ulong
)keyid
[0],(ulong
)keyid
[1],
981 colon_datestr_from_sk( sk
),
982 colon_strtime (sk
->expiredate
)
983 /* fixme: add LID */ );
986 printf("%s %4u%c/%s %s ", mainkey
? "sec":"ssb",
987 nbits_from_sk( sk
), pubkey_letter( sk
->pubkey_algo
),
988 keystr_from_sk( sk
), datestr_from_sk( sk
));
990 /* and now list all userids with their signatures */
991 for( node
= node
->next
; node
; node
= node
->next
) {
992 if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
994 if( node
->pkt
->pkt
.signature
->sig_class
== 0x20 )
1000 list_node(c
, node
);
1002 else if( node
->pkt
->pkttype
== PKT_USER_ID
) {
1004 if( opt
.with_colons
)
1005 printf("%s:::::::::",
1006 node
->pkt
->pkt
.user_id
->attrib_data
?"uat":"uid");
1008 printf( "uid%*s", 28, "" );
1010 print_userid( node
->pkt
);
1011 if( opt
.with_colons
)
1014 if( opt
.fingerprint
&& !any
)
1015 print_fingerprint( NULL
, sk
, 0 );
1018 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1023 list_node(c
, node
);
1029 if( !mainkey
&& opt
.fingerprint
> 1 )
1030 print_fingerprint( NULL
, sk
, 0 );
1032 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1033 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1043 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x30 )
1044 fputs("rev", stdout
);
1046 fputs("sig", stdout
);
1047 if( opt
.check_sigs
) {
1049 rc2
=do_check_sig( c
, node
, &is_selfsig
, NULL
, NULL
);
1050 switch (gpg_err_code (rc2
)) {
1051 case 0: sigrc
= '!'; break;
1052 case GPG_ERR_BAD_SIGNATURE
: sigrc
= '-'; break;
1053 case GPG_ERR_NO_PUBKEY
:
1054 case GPG_ERR_UNUSABLE_PUBKEY
: sigrc
= '?'; break;
1055 default: sigrc
= '%'; break;
1058 else { /* check whether this is a self signature */
1061 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
1062 || c
->list
->pkt
->pkttype
== PKT_SECRET_KEY
) {
1063 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
1064 keyid_from_pk( c
->list
->pkt
->pkt
.public_key
, keyid
);
1066 keyid_from_sk( c
->list
->pkt
->pkt
.secret_key
, keyid
);
1068 if( keyid
[0] == sig
->keyid
[0] && keyid
[1] == sig
->keyid
[1] )
1072 if( opt
.with_colons
) {
1076 printf("::%d:%08lX%08lX:%s:%s:", sig
->pubkey_algo
,
1077 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1078 colon_datestr_from_sig(sig
),
1079 colon_expirestr_from_sig(sig
));
1081 if(sig
->trust_depth
|| sig
->trust_value
)
1082 printf("%d %d",sig
->trust_depth
,sig
->trust_value
);
1085 if(sig
->trust_regexp
)
1086 print_string(stdout
,sig
->trust_regexp
,
1087 strlen(sig
->trust_regexp
),':');
1092 sigrc
, keystr(sig
->keyid
), datestr_from_sig(sig
));
1094 printf("[%s] ", g10_errstr(rc2
) );
1095 else if( sigrc
== '?' )
1097 else if( is_selfsig
) {
1098 if( opt
.with_colons
)
1100 fputs( sig
->sig_class
== 0x18? "[keybind]":"[selfsig]", stdout
);
1101 if( opt
.with_colons
)
1104 else if( !opt
.fast_list_mode
) {
1105 p
= get_user_id( sig
->keyid
, &n
);
1106 print_string( stdout
, p
, n
, opt
.with_colons
);
1109 if( opt
.with_colons
)
1110 printf(":%02x%c:", sig
->sig_class
, sig
->flags
.exportable
?'x':'l');
1114 log_error("invalid node with packet of type %d\n", node
->pkt
->pkttype
);
1120 proc_packets( void *anchor
, IOBUF a
)
1123 CTX c
= xmalloc_clear( sizeof *c
);
1126 rc
= do_proc_packets( c
, a
);
1134 proc_signature_packets( void *anchor
, IOBUF a
,
1135 STRLIST signedfiles
, const char *sigfilename
)
1137 CTX c
= xmalloc_clear( sizeof *c
);
1142 c
->signed_data
= signedfiles
;
1143 c
->sigfilename
= sigfilename
;
1144 rc
= do_proc_packets( c
, a
);
1146 /* If we have not encountered any signature we print an error
1147 messages, send a NODATA status back and return an error code.
1148 Using log_error is required because verify_files does not check
1149 error codes for each file but we want to terminate the process
1151 if (!rc
&& !c
->any_sig_seen
)
1153 write_status_text (STATUS_NODATA
, "4");
1154 log_error (_("no signature found\n"));
1155 rc
= G10ERR_NO_DATA
;
1158 /* Propagate the signature seen flag upward. Do this only on
1159 success so that we won't issue the nodata status several
1161 if (!rc
&& c
->anchor
&& c
->any_sig_seen
)
1162 c
->anchor
->any_sig_seen
= 1;
1169 proc_encryption_packets( void *anchor
, IOBUF a
)
1171 CTX c
= xmalloc_clear( sizeof *c
);
1175 c
->encrypt_only
= 1;
1176 rc
= do_proc_packets( c
, a
);
1183 do_proc_packets( CTX c
, IOBUF a
)
1185 PACKET
*pkt
= xmalloc( sizeof *pkt
);
1192 while( (rc
=parse_packet(a
, pkt
)) != -1 ) {
1196 /* stop processing when an invalid packet has been encountered
1197 * but don't do so when we are doing a --list-packets. */
1198 if (gpg_err_code (rc
) == GPG_ERR_INV_PACKET
1199 && opt
.list_packets
!= 2 )
1204 if( opt
.list_packets
) {
1205 switch( pkt
->pkttype
) {
1206 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1207 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1209 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1210 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1211 default: newpkt
= 0; break;
1214 else if( c
->sigs_only
) {
1215 switch( pkt
->pkttype
) {
1216 case PKT_PUBLIC_KEY
:
1217 case PKT_SECRET_KEY
:
1219 case PKT_SYMKEY_ENC
:
1220 case PKT_PUBKEY_ENC
:
1222 case PKT_ENCRYPTED_MDC
:
1223 write_status_text( STATUS_UNEXPECTED
, "0" );
1224 rc
= G10ERR_UNEXPECTED
;
1226 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1227 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1228 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1229 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1230 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1231 default: newpkt
= 0; break;
1234 else if( c
->encrypt_only
) {
1235 switch( pkt
->pkttype
) {
1236 case PKT_PUBLIC_KEY
:
1237 case PKT_SECRET_KEY
:
1239 write_status_text( STATUS_UNEXPECTED
, "0" );
1240 rc
= G10ERR_UNEXPECTED
;
1242 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1243 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1244 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1246 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1247 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1248 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1249 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1250 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1251 default: newpkt
= 0; break;
1255 switch( pkt
->pkttype
) {
1256 case PKT_PUBLIC_KEY
:
1257 case PKT_SECRET_KEY
:
1259 c
->list
= new_kbnode( pkt
);
1262 case PKT_PUBLIC_SUBKEY
:
1263 case PKT_SECRET_SUBKEY
:
1264 newpkt
= add_subkey( c
, pkt
);
1266 case PKT_USER_ID
: newpkt
= add_user_id( c
, pkt
); break;
1267 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1268 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1269 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1271 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1272 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1273 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1274 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1275 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1276 case PKT_RING_TRUST
: newpkt
= add_ring_trust( c
, pkt
); break;
1277 default: newpkt
= 0; break;
1280 /* This is a very ugly construct and frankly, I don't remember why
1281 * I used it. Adding the MDC check here is a hack.
1282 * The right solution is to initiate another context for encrypted
1283 * packet and not to reuse the current one ... It works right
1284 * when there is a compression packet inbetween which adds just
1286 * Hmmm: Rewrite this whole module here??
1288 if( pkt
->pkttype
!= PKT_SIGNATURE
&& pkt
->pkttype
!= PKT_MDC
)
1289 c
->have_data
= pkt
->pkttype
== PKT_PLAINTEXT
;
1294 pkt
= xmalloc( sizeof *pkt
);
1300 if( rc
== G10ERR_INVALID_PACKET
)
1301 write_status_text( STATUS_NODATA
, "3" );
1305 write_status_text( STATUS_NODATA
, "2" );
1313 free_md_filter_context( &c
->mfx
);
1318 /* Helper for pka_uri_from_sig to parse the to-be-verified address out
1319 of the notation data. */
1321 get_pka_address (PKT_signature
*sig
)
1323 pka_info_t
*pka
= NULL
;
1324 struct notation
*nd
,*notation
;
1326 notation
=sig_to_notation(sig
);
1328 for(nd
=notation
;nd
;nd
=nd
->next
)
1330 if(strcmp(nd
->name
,"pka-address@gnupg.org")!=0)
1331 continue; /* Not the notation we want. */
1333 /* For now we only use the first valid PKA notation. In future
1334 we might want to keep additional PKA notations in a linked
1336 if (is_valid_mailbox (nd
->value
))
1338 pka
= xmalloc (sizeof *pka
+ strlen(nd
->value
));
1342 strcpy (pka
->email
, nd
->value
);
1347 free_notation(notation
);
1353 /* Return the URI from a DNS PKA record. If this record has already
1354 be retrieved for the signature we merely return it; if not we go
1355 out and try to get that DNS record. */
1357 pka_uri_from_sig (PKT_signature
*sig
)
1359 if (!sig
->flags
.pka_tried
)
1361 assert (!sig
->pka_info
);
1362 sig
->flags
.pka_tried
= 1;
1363 sig
->pka_info
= get_pka_address (sig
);
1368 uri
= get_pka_info (sig
->pka_info
->email
, sig
->pka_info
->fpr
);
1371 sig
->pka_info
->valid
= 1;
1375 sig
->pka_info
->uri
= uri
;
1379 return sig
->pka_info
? sig
->pka_info
->uri
: NULL
;
1384 check_sig_and_print( CTX c
, KBNODE node
)
1386 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1388 int rc
, is_expkey
=0, is_revkey
=0;
1390 if (opt
.skip_verify
)
1392 log_info(_("signature verification suppressed\n"));
1396 /* Check that the message composition is valid.
1398 Per RFC-2440bis (-15) allowed:
1400 S{1,n} -- detached signature.
1401 S{1,n} P -- old style PGP2 signature
1402 O{1,n} P S{1,n} -- standard OpenPGP signature.
1403 C P S{1,n} -- cleartext signature.
1406 O = One-Pass Signature packet.
1407 S = Signature packet.
1408 P = OpenPGP Message packet (Encrypted | Compressed | Literal)
1409 (Note that the current rfc2440bis draft also allows
1410 for a signed message but that does not work as it
1411 introduces ambiguities.)
1412 We keep track of these packages using the marker packet
1413 CTRLPKT_PLAINTEXT_MARK.
1414 C = Marker packet for cleartext signatures.
1416 We reject all other messages.
1418 Actually we are calling this too often, i.e. for verification of
1419 each message but better have some duplicate work than to silently
1420 introduce a bug here.
1424 int n_onepass
, n_sig
;
1426 /* log_debug ("checking signature packet composition\n"); */
1427 /* dump_kbnode (c->list); */
1431 if ( n
->pkt
->pkttype
== PKT_SIGNATURE
)
1433 /* This is either "S{1,n}" case (detached signature) or
1434 "S{1,n} P" (old style PGP2 signature). */
1435 for (n
= n
->next
; n
; n
= n
->next
)
1436 if (n
->pkt
->pkttype
!= PKT_SIGNATURE
)
1439 ; /* Okay, this is a detached signature. */
1440 else if (n
->pkt
->pkttype
== PKT_GPG_CONTROL
1441 && (n
->pkt
->pkt
.gpg_control
->control
1442 == CTRLPKT_PLAINTEXT_MARK
) )
1445 goto ambiguous
; /* We only allow one P packet. */
1450 else if (n
->pkt
->pkttype
== PKT_ONEPASS_SIG
)
1452 /* This is the "O{1,n} P S{1,n}" case (standard signature). */
1453 for (n_onepass
=1, n
= n
->next
;
1454 n
&& n
->pkt
->pkttype
== PKT_ONEPASS_SIG
; n
= n
->next
)
1456 if (!n
|| !(n
->pkt
->pkttype
== PKT_GPG_CONTROL
1457 && (n
->pkt
->pkt
.gpg_control
->control
1458 == CTRLPKT_PLAINTEXT_MARK
)))
1460 for (n_sig
=0, n
= n
->next
;
1461 n
&& n
->pkt
->pkttype
== PKT_SIGNATURE
; n
= n
->next
)
1465 if (n
&& !opt
.allow_multisig_verification
)
1467 if (n_onepass
!= n_sig
)
1469 log_info ("number of one-pass packets does not match "
1470 "number of signature packets\n");
1474 else if (n
->pkt
->pkttype
== PKT_GPG_CONTROL
1475 && n
->pkt
->pkt
.gpg_control
->control
== CTRLPKT_CLEARSIGN_START
)
1477 /* This is the "C P S{1,n}" case (clear text signature). */
1479 if (!n
|| !(n
->pkt
->pkttype
== PKT_GPG_CONTROL
1480 && (n
->pkt
->pkt
.gpg_control
->control
1481 == CTRLPKT_PLAINTEXT_MARK
)))
1483 for (n_sig
=0, n
= n
->next
;
1484 n
&& n
->pkt
->pkttype
== PKT_SIGNATURE
; n
= n
->next
)
1492 log_error(_("can't handle this ambiguous signature data\n"));
1498 /* (Indendation below not yet changed to GNU style.) */
1500 astr
= gcry_pk_algo_name ( sig
->pubkey_algo
);
1503 log_info(_("Signature made %s\n"),asctimestamp(sig
->timestamp
));
1504 log_info(_(" using %s key %s\n"),
1505 astr
? astr
: "?",keystr(sig
->keyid
));
1508 log_info(_("Signature made %s using %s key ID %s\n"),
1509 asctimestamp(sig
->timestamp
), astr
? astr
: "?",
1510 keystr(sig
->keyid
));
1512 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1514 /* If the key isn't found, check for a preferred keyserver */
1516 if(rc
==G10ERR_NO_PUBKEY
&& sig
->flags
.pref_ks
)
1522 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&n
,&seq
,NULL
)))
1524 /* According to my favorite copy editor, in English
1525 grammar, you say "at" if the key is located on a web
1526 page, but "from" if it is located on a keyserver. I'm
1527 not going to even try to make two strings here :) */
1528 log_info(_("Key available at: ") );
1529 print_utf8_string( log_get_stream(), p
, n
);
1532 if(opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
1533 && opt
.keyserver_options
.options
&KEYSERVER_HONOR_KEYSERVER_URL
)
1535 struct keyserver_spec
*spec
;
1537 spec
=parse_preferred_keyserver(sig
);
1542 glo_ctrl
.in_auto_key_retrieve
++;
1543 res
=keyserver_import_keyid(sig
->keyid
,spec
);
1544 glo_ctrl
.in_auto_key_retrieve
--;
1546 rc
=do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1547 free_keyserver_spec(spec
);
1556 /* If the preferred keyserver thing above didn't work, our second
1557 try is to use the URI from a DNS PKA record. */
1558 if ( rc
== G10ERR_NO_PUBKEY
1559 && opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
1560 && opt
.keyserver_options
.options
&KEYSERVER_HONOR_PKA_RECORD
)
1562 const char *uri
= pka_uri_from_sig (sig
);
1566 /* FIXME: We might want to locate the key using the
1567 fingerprint instead of the keyid. */
1569 struct keyserver_spec
*spec
;
1571 spec
= parse_keyserver_uri (uri
, 1, NULL
, 0);
1574 glo_ctrl
.in_auto_key_retrieve
++;
1575 res
= keyserver_import_keyid (sig
->keyid
, spec
);
1576 glo_ctrl
.in_auto_key_retrieve
--;
1577 free_keyserver_spec (spec
);
1579 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1584 /* If the preferred keyserver thing above didn't work and we got
1585 no information from the DNS PKA, this is a third try. */
1587 if( rc
== G10ERR_NO_PUBKEY
&& opt
.keyserver
1588 && opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
)
1592 glo_ctrl
.in_auto_key_retrieve
++;
1593 res
=keyserver_import_keyid ( sig
->keyid
, opt
.keyserver
);
1594 glo_ctrl
.in_auto_key_retrieve
--;
1596 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1599 if( !rc
|| gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
) {
1600 KBNODE un
, keyblock
;
1601 int count
=0, statno
;
1603 PKT_public_key
*pk
=NULL
;
1606 statno
=STATUS_BADSIG
;
1607 else if(sig
->flags
.expired
)
1608 statno
=STATUS_EXPSIG
;
1610 statno
=STATUS_EXPKEYSIG
;
1612 statno
=STATUS_REVKEYSIG
;
1614 statno
=STATUS_GOODSIG
;
1616 keyblock
= get_pubkeyblock( sig
->keyid
);
1618 sprintf (keyid_str
, "%08lX%08lX [uncertain] ",
1619 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1]);
1621 /* find and print the primary user ID */
1622 for( un
=keyblock
; un
; un
= un
->next
) {
1625 if(un
->pkt
->pkttype
==PKT_PUBLIC_KEY
)
1627 pk
=un
->pkt
->pkt
.public_key
;
1630 if( un
->pkt
->pkttype
!= PKT_USER_ID
)
1632 if ( !un
->pkt
->pkt
.user_id
->created
)
1634 if ( un
->pkt
->pkt
.user_id
->is_revoked
)
1636 if ( un
->pkt
->pkt
.user_id
->is_expired
)
1638 if ( !un
->pkt
->pkt
.user_id
->is_primary
)
1640 /* We want the textual primary user ID here */
1641 if ( un
->pkt
->pkt
.user_id
->attrib_data
)
1646 /* Get it before we print anything to avoid interrupting
1647 the output with the "please do a --check-trustdb"
1649 valid
=get_validity(pk
,un
->pkt
->pkt
.user_id
);
1651 keyid_str
[17] = 0; /* cut off the "[uncertain]" part */
1652 write_status_text_and_buffer (statno
, keyid_str
,
1653 un
->pkt
->pkt
.user_id
->name
,
1654 un
->pkt
->pkt
.user_id
->len
,
1657 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1658 un
->pkt
->pkt
.user_id
->len
,0);
1661 log_info(_("BAD signature from \"%s\""),p
);
1662 else if(sig
->flags
.expired
)
1663 log_info(_("Expired signature from \"%s\""),p
);
1665 log_info(_("Good signature from \"%s\""),p
);
1669 if(opt
.verify_options
&VERIFY_SHOW_UID_VALIDITY
)
1670 log_printf (" [%s]\n",trust_value_to_string(valid
));
1675 if( !count
) { /* just in case that we have no valid textual
1679 /* Try for an invalid textual userid */
1680 for( un
=keyblock
; un
; un
= un
->next
) {
1681 if( un
->pkt
->pkttype
== PKT_USER_ID
&&
1682 !un
->pkt
->pkt
.user_id
->attrib_data
)
1686 /* Try for any userid at all */
1688 for( un
=keyblock
; un
; un
= un
->next
) {
1689 if( un
->pkt
->pkttype
== PKT_USER_ID
)
1694 if (opt
.trust_model
==TM_ALWAYS
|| !un
)
1695 keyid_str
[17] = 0; /* cut off the "[uncertain]" part */
1697 write_status_text_and_buffer (statno
, keyid_str
,
1698 un
? un
->pkt
->pkt
.user_id
->name
:"[?]",
1699 un
? un
->pkt
->pkt
.user_id
->len
:3,
1703 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1704 un
->pkt
->pkt
.user_id
->len
,0);
1709 log_info(_("BAD signature from \"%s\""),p
);
1710 else if(sig
->flags
.expired
)
1711 log_info(_("Expired signature from \"%s\""),p
);
1713 log_info(_("Good signature from \"%s\""),p
);
1714 if (opt
.trust_model
!=TM_ALWAYS
&& un
)
1715 log_printf (" %s",_("[uncertain]") );
1719 /* If we have a good signature and already printed
1720 * the primary user ID, print all the other user IDs */
1721 if ( count
&& !rc
) {
1723 for( un
=keyblock
; un
; un
= un
->next
) {
1724 if( un
->pkt
->pkttype
!= PKT_USER_ID
)
1726 if((un
->pkt
->pkt
.user_id
->is_revoked
1727 || un
->pkt
->pkt
.user_id
->is_expired
)
1728 && !(opt
.verify_options
&VERIFY_SHOW_UNUSABLE_UIDS
))
1730 /* Only skip textual primaries */
1731 if ( un
->pkt
->pkt
.user_id
->is_primary
&&
1732 !un
->pkt
->pkt
.user_id
->attrib_data
)
1735 if(un
->pkt
->pkt
.user_id
->attrib_data
)
1737 dump_attribs(un
->pkt
->pkt
.user_id
,pk
,NULL
);
1739 if(opt
.verify_options
&VERIFY_SHOW_PHOTOS
)
1740 show_photos(un
->pkt
->pkt
.user_id
->attribs
,
1741 un
->pkt
->pkt
.user_id
->numattribs
,pk
,NULL
);
1744 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1745 un
->pkt
->pkt
.user_id
->len
,0);
1746 log_info(_(" aka \"%s\""),p
);
1749 if(opt
.verify_options
&VERIFY_SHOW_UID_VALIDITY
)
1752 if(un
->pkt
->pkt
.user_id
->is_revoked
)
1754 else if(un
->pkt
->pkt
.user_id
->is_expired
)
1757 valid
=trust_value_to_string(get_validity(pk
,
1760 log_printf (" [%s]\n",valid
);
1766 release_kbnode( keyblock
);
1770 if(opt
.verify_options
&VERIFY_SHOW_POLICY_URLS
)
1771 show_policy_url(sig
,0,1);
1773 show_policy_url(sig
,0,2);
1775 if(opt
.verify_options
&VERIFY_SHOW_KEYSERVER_URLS
)
1776 show_keyserver_url(sig
,0,1);
1778 show_keyserver_url(sig
,0,2);
1780 if(opt
.verify_options
&VERIFY_SHOW_NOTATIONS
)
1781 show_notation(sig
,0,1,
1782 ((opt
.verify_options
&VERIFY_SHOW_STD_NOTATIONS
)?1:0)+
1783 ((opt
.verify_options
&VERIFY_SHOW_USER_NOTATIONS
)?2:0));
1785 show_notation(sig
,0,2,0);
1788 if( !rc
&& is_status_enabled() ) {
1789 /* print a status response with the fingerprint */
1790 PKT_public_key
*vpk
= xmalloc_clear( sizeof *vpk
);
1792 if( !get_pubkey( vpk
, sig
->keyid
) ) {
1793 byte array
[MAX_FINGERPRINT_LEN
], *p
;
1794 char buf
[MAX_FINGERPRINT_LEN
*4+90], *bufp
;
1798 fingerprint_from_pk( vpk
, array
, &n
);
1800 for(i
=0; i
< n
; i
++, p
++, bufp
+= 2)
1801 sprintf(bufp
, "%02X", *p
);
1802 /* TODO: Replace the reserved '0' in the field below
1803 with bits for status flags (policy url, notation,
1804 etc.). Remember to make the buffer larger to
1806 sprintf(bufp
, " %s %lu %lu %d 0 %d %d %02X ",
1807 strtimestamp( sig
->timestamp
),
1808 (ulong
)sig
->timestamp
,(ulong
)sig
->expiredate
,
1809 sig
->version
,sig
->pubkey_algo
,sig
->digest_algo
,
1811 bufp
= bufp
+ strlen (bufp
);
1812 if (!vpk
->is_primary
) {
1815 akid
[0] = vpk
->main_keyid
[0];
1816 akid
[1] = vpk
->main_keyid
[1];
1817 free_public_key (vpk
);
1818 vpk
= xmalloc_clear( sizeof *vpk
);
1819 if (get_pubkey (vpk
, akid
)) {
1820 /* impossible error, we simply return a zeroed out fpr */
1821 n
= MAX_FINGERPRINT_LEN
< 20? MAX_FINGERPRINT_LEN
: 20;
1822 memset (array
, 0, n
);
1825 fingerprint_from_pk( vpk
, array
, &n
);
1828 for(i
=0; i
< n
; i
++, p
++, bufp
+= 2)
1829 sprintf(bufp
, "%02X", *p
);
1830 write_status_text( STATUS_VALIDSIG
, buf
);
1832 free_public_key( vpk
);
1837 if(opt
.verify_options
&VERIFY_PKA_LOOKUPS
)
1838 pka_uri_from_sig (sig
); /* Make sure PKA info is available. */
1839 rc
= check_signatures_trust( sig
);
1842 if(sig
->flags
.expired
)
1844 log_info(_("Signature expired %s\n"),
1845 asctimestamp(sig
->expiredate
));
1846 rc
=G10ERR_GENERAL
; /* need a better error here? */
1848 else if(sig
->expiredate
)
1849 log_info(_("Signature expires %s\n"),asctimestamp(sig
->expiredate
));
1852 log_info(_("%s signature, digest algorithm %s\n"),
1853 sig
->sig_class
==0x00?_("binary"):
1854 sig
->sig_class
==0x01?_("textmode"):_("unknown"),
1855 gcry_md_algo_name (sig
->digest_algo
));
1858 g10_errors_seen
= 1;
1859 if( opt
.batch
&& rc
)
1864 sprintf(buf
, "%08lX%08lX %d %d %02x %lu %d",
1865 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1866 sig
->pubkey_algo
, sig
->digest_algo
,
1867 sig
->sig_class
, (ulong
)sig
->timestamp
, rc
);
1868 write_status_text( STATUS_ERRSIG
, buf
);
1869 if( rc
== G10ERR_NO_PUBKEY
) {
1871 write_status_text( STATUS_NO_PUBKEY
, buf
);
1873 if( rc
!= G10ERR_NOT_PROCESSED
)
1874 log_error(_("Can't check signature: %s\n"), g10_errstr(rc
) );
1881 * Process the tree which starts at node
1884 proc_tree( CTX c
, KBNODE node
)
1889 if( opt
.list_packets
|| opt
.list_only
)
1892 /* we must skip our special plaintext marker packets here becuase
1893 they may be the root packet. These packets are only used in
1894 addionla checks and skipping them here doesn't matter */
1896 && node
->pkt
->pkttype
== PKT_GPG_CONTROL
1897 && node
->pkt
->pkt
.gpg_control
->control
1898 == CTRLPKT_PLAINTEXT_MARK
) {
1904 c
->trustletter
= ' ';
1905 if( node
->pkt
->pkttype
== PKT_PUBLIC_KEY
1906 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1907 merge_keys_and_selfsig( node
);
1908 list_node( c
, node
);
1910 else if( node
->pkt
->pkttype
== PKT_SECRET_KEY
) {
1911 merge_keys_and_selfsig( node
);
1912 list_node( c
, node
);
1914 else if( node
->pkt
->pkttype
== PKT_ONEPASS_SIG
) {
1915 /* check all signatures */
1916 if( !c
->have_data
) {
1917 free_md_filter_context( &c
->mfx
);
1918 /* prepare to create all requested message digests */
1919 if (gcry_md_open (&c
->mfx
.md
, 0, 0))
1922 /* fixme: why looking for the signature packet and not the
1924 for ( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
1926 gcry_md_enable (c
->mfx
.md
,
1927 n1
->pkt
->pkt
.signature
->digest_algo
);
1929 /* ask for file and hash it */
1930 if( c
->sigs_only
) {
1931 rc
= hash_datafiles( c
->mfx
.md
, NULL
,
1932 c
->signed_data
, c
->sigfilename
,
1933 n1
? (n1
->pkt
->pkt
.onepass_sig
->sig_class
== 0x01):0 );
1936 rc
= ask_for_detached_datafile( c
->mfx
.md
, c
->mfx
.md2
,
1937 iobuf_get_real_fname(c
->iobuf
),
1938 n1
? (n1
->pkt
->pkt
.onepass_sig
->sig_class
== 0x01):0 );
1941 log_error("can't hash datafile: %s\n", g10_errstr(rc
));
1945 else if ( c
->signed_data
) {
1946 log_error (_("not a detached signature\n") );
1950 for( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
1951 check_sig_and_print( c
, n1
);
1953 else if( node
->pkt
->pkttype
== PKT_GPG_CONTROL
1954 && node
->pkt
->pkt
.gpg_control
->control
1955 == CTRLPKT_CLEARSIGN_START
) {
1956 /* clear text signed message */
1957 if( !c
->have_data
) {
1958 log_error("cleartext signature without data\n" );
1961 else if ( c
->signed_data
) {
1962 log_error (_("not a detached signature\n") );
1966 for( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
1967 check_sig_and_print( c
, n1
);
1969 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1970 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1973 n1
=find_next_kbnode(node
, PKT_SIGNATURE
);
1976 byte
class=sig
->sig_class
;
1977 byte hash
=sig
->digest_algo
;
1979 for(; n1
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)))
1981 /* We can't currently handle multiple signatures of
1982 different classes or digests (we'd pretty much have
1983 to run a different hash context for each), but if
1984 they are all the same, make an exception. */
1985 if(n1
->pkt
->pkt
.signature
->sig_class
!=class
1986 || n1
->pkt
->pkt
.signature
->digest_algo
!=hash
)
1989 log_info(_("WARNING: multiple signatures detected. "
1990 "Only the first will be checked.\n"));
1996 if( sig
->sig_class
!= 0x00 && sig
->sig_class
!= 0x01 )
1997 log_info(_("standalone signature of class 0x%02x\n"),
1999 else if( !c
->have_data
) {
2000 /* detached signature */
2001 free_md_filter_context( &c
->mfx
);
2002 if (gcry_md_open (&c
->mfx
.md
, sig
->digest_algo
, 0))
2005 if( !opt
.pgp2_workarounds
)
2007 else if( sig
->digest_algo
== DIGEST_ALGO_MD5
2008 && is_RSA( sig
->pubkey_algo
) ) {
2009 /* enable a workaround for a pgp2 bug */
2010 if (gcry_md_open (&c
->mfx
.md2
, DIGEST_ALGO_MD5
, 0))
2013 else if( sig
->digest_algo
== DIGEST_ALGO_SHA1
2014 && sig
->pubkey_algo
== PUBKEY_ALGO_DSA
2015 && sig
->sig_class
== 0x01 ) {
2016 /* enable the workaround also for pgp5 when the detached
2017 * signature has been created in textmode */
2018 if (gcry_md_open (&c
->mfx
.md2
, sig
->digest_algo
, 0 ))
2021 #if 0 /* workaround disabled */
2022 /* Here we have another hack to work around a pgp 2 bug
2023 * It works by not using the textmode for detached signatures;
2024 * this will let the first signature check (on md) fail
2025 * but the second one (on md2) which adds an extra CR should
2026 * then produce the "correct" hash. This is very, very ugly
2027 * hack but it may help in some cases (and break others)
2029 /* c->mfx.md2? 0 :(sig->sig_class == 0x01) */
2031 if ( DBG_HASHING
) {
2032 gcry_md_start_debug( c
->mfx
.md
, "verify" );
2034 gcry_md_start_debug( c
->mfx
.md2
, "verify2" );
2036 if( c
->sigs_only
) {
2037 rc
= hash_datafiles( c
->mfx
.md
, c
->mfx
.md2
,
2038 c
->signed_data
, c
->sigfilename
,
2039 (sig
->sig_class
== 0x01) );
2042 rc
= ask_for_detached_datafile( c
->mfx
.md
, c
->mfx
.md2
,
2043 iobuf_get_real_fname(c
->iobuf
),
2044 (sig
->sig_class
== 0x01) );
2047 log_error("can't hash datafile: %s\n", g10_errstr(rc
));
2051 else if ( c
->signed_data
) {
2052 log_error (_("not a detached signature\n") );
2055 else if (!opt
.quiet
)
2056 log_info(_("old style (PGP 2.x) signature\n"));
2059 for( n1
= node
; n1
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)) )
2060 check_sig_and_print( c
, n1
);
2062 check_sig_and_print( c
, node
);
2065 dump_kbnode (c
->list
);
2066 log_error(_("invalid root packet detected in proc_tree()\n"));