1 /* mainproc.c - handle packets
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007 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 /* Name of the file with the complete signature or the file with the
70 detached signature. This is currently only used to deduce the
71 file name of the data file if that has not been given. */
72 const char *sigfilename
;
74 /* A structure to describe the signed data in case of a detached
78 /* A file descriptor of the the signed data. Only used if not -1. */
80 /* A list of filenames with the data files or NULL. This is only
81 used if DATA_FD is -1. */
83 /* Flag to indicated that either one of the next previous fieldss
84 is used. This is only needed for better readability. */
89 int last_was_session_key
;
90 KBNODE list
; /* The current list of packets. */
92 IOBUF iobuf
; /* Used to get the filename etc. */
93 int trustletter
; /* Temporary usage in list_node. */
95 struct kidlist_item
*pkenc_list
; /* List of encryption packets. */
96 int any_sig_seen
; /* Set to true if a signature packet has been seen. */
100 static int do_proc_packets( CTX c
, IOBUF a
);
101 static void list_node( CTX c
, KBNODE node
);
102 static void proc_tree( CTX c
, KBNODE node
);
103 static int literals_seen
;
106 reset_literals_seen(void)
112 release_list( CTX c
)
116 proc_tree(c
, c
->list
);
117 release_kbnode( c
->list
);
118 while( c
->pkenc_list
) {
119 struct kidlist_item
*tmp
= c
->pkenc_list
->next
;
120 xfree( c
->pkenc_list
);
123 c
->pkenc_list
= NULL
;
126 c
->last_was_session_key
= 0;
127 xfree(c
->dek
); c
->dek
= NULL
;
132 add_onepass_sig( CTX c
, PACKET
*pkt
)
136 if ( c
->list
) /* add another packet */
137 add_kbnode( c
->list
, new_kbnode( pkt
));
138 else /* insert the first one */
139 c
->list
= node
= new_kbnode( pkt
);
146 add_gpg_control( CTX c
, PACKET
*pkt
)
148 if ( pkt
->pkt
.gpg_control
->control
== CTRLPKT_CLEARSIGN_START
) {
149 /* New clear text signature.
150 * Process the last one and reset everything */
154 if( c
->list
) /* add another packet */
155 add_kbnode( c
->list
, new_kbnode( pkt
));
156 else /* insert the first one */
157 c
->list
= new_kbnode( pkt
);
165 add_user_id( CTX c
, PACKET
*pkt
)
168 log_error("orphaned user ID\n" );
171 add_kbnode( c
->list
, new_kbnode( pkt
) );
176 add_subkey( CTX c
, PACKET
*pkt
)
179 log_error("subkey w/o mainkey\n" );
182 add_kbnode( c
->list
, new_kbnode( pkt
) );
187 add_ring_trust( CTX c
, PACKET
*pkt
)
190 log_error("ring trust w/o key\n" );
193 add_kbnode( c
->list
, new_kbnode( pkt
) );
199 add_signature( CTX c
, PACKET
*pkt
)
204 if( pkt
->pkttype
== PKT_SIGNATURE
&& !c
->list
) {
205 /* This is the first signature for the following datafile.
206 * GPG does not write such packets; instead it always uses
207 * onepass-sig packets. The drawback of PGP's method
208 * of prepending the signature to the data is
209 * that it is not possible to make a signature from data read
210 * from stdin. (GPG is able to read PGP stuff anyway.) */
211 node
= new_kbnode( pkt
);
216 return 0; /* oops (invalid packet sequence)*/
217 else if( !c
->list
->pkt
)
218 BUG(); /* so nicht */
220 /* add a new signature node id at the end */
221 node
= new_kbnode( pkt
);
222 add_kbnode( c
->list
, node
);
227 symkey_decrypt_seskey( DEK
*dek
, byte
*seskey
, size_t slen
)
231 if(slen
< 17 || slen
> 33)
233 log_error ( _("weird size for an encrypted session key (%d)\n"),
235 return G10ERR_BAD_KEY
;
238 if (gcry_cipher_open (&hd
, dek
->algo
, GCRY_CIPHER_MODE_CFB
, 1))
240 if (gcry_cipher_setkey ( hd
, dek
->key
, dek
->keylen
))
242 gcry_cipher_setiv ( hd
, NULL
, 0 );
243 gcry_cipher_decrypt ( hd
, seskey
, slen
, NULL
, 0 );
244 gcry_cipher_close ( hd
);
246 /* Now we replace the dek components with the real session key to
247 decrypt the contents of the sequencing packet. */
252 if(dek
->keylen
> DIM(dek
->key
))
255 /* This is not completely accurate, since a bad passphrase may have
256 resulted in a garbage algorithm byte, but it's close enough since
257 a bogus byte here will fail later. */
258 if(dek
->algo
==CIPHER_ALGO_IDEA
)
261 memcpy(dek
->key
, seskey
+ 1, dek
->keylen
);
263 /*log_hexdump( "thekey", dek->key, dek->keylen );*/
269 proc_symkey_enc( CTX c
, PACKET
*pkt
)
273 enc
= pkt
->pkt
.symkey_enc
;
275 log_error ("invalid symkey encrypted packet\n");
278 int algo
= enc
->cipher_algo
;
279 const char *s
= gcry_cipher_algo_name (algo
);
281 if (!gcry_cipher_test_algo (algo
))
286 log_info(_("%s encrypted session key\n"), s
);
288 log_info(_("%s encrypted data\n"), s
);
292 log_error(_("encrypted with unknown algorithm %d\n"), algo
);
294 if(openpgp_md_test_algo (enc
->s2k
.hash_algo
))
296 log_error(_("passphrase generated with unknown digest"
297 " algorithm %d\n"),enc
->s2k
.hash_algo
);
301 c
->last_was_session_key
= 2;
302 if(!s
|| opt
.list_only
)
305 if(opt
.override_session_key
)
307 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
308 if(get_override_session_key(c
->dek
, opt
.override_session_key
))
316 c
->dek
= passphrase_to_dek (NULL
, 0, algo
, &enc
->s2k
, 0,
322 /* FIXME: This doesn't work perfectly if a symmetric
323 key comes before a public key in the message - if
324 the user doesn't know the passphrase, then there is
325 a chance that the "decrypted" algorithm will happen
326 to be a valid one, which will make the returned dek
327 appear valid, so we won't try any public keys that
331 if(symkey_decrypt_seskey(c
->dek
, enc
->seskey
,
339 c
->dek
->algo_info_printed
= 1;
350 proc_pubkey_enc( CTX c
, PACKET
*pkt
)
355 /* check whether the secret key is available and store in this case */
356 c
->last_was_session_key
= 1;
357 enc
= pkt
->pkt
.pubkey_enc
;
358 /*printf("enc: encrypted by a pubkey with keyid %08lX\n", enc->keyid[1] );*/
359 /* Hmmm: why do I have this algo check here - anyway there is
360 * function to check it. */
362 log_info(_("public key is %s\n"), keystr(enc
->keyid
) );
364 if( is_status_enabled() ) {
366 sprintf(buf
, "%08lX%08lX %d 0",
367 (ulong
)enc
->keyid
[0], (ulong
)enc
->keyid
[1], enc
->pubkey_algo
);
368 write_status_text( STATUS_ENC_TO
, buf
);
371 if( !opt
.list_only
&& opt
.override_session_key
) {
372 /* It does not make much sense to store the session key in
373 * secure memory because it has already been passed on the
374 * command line and the GCHQ knows about it. */
375 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
376 result
= get_override_session_key ( c
->dek
, opt
.override_session_key
);
378 xfree(c
->dek
); c
->dek
= NULL
;
381 else if( is_ELGAMAL(enc
->pubkey_algo
)
382 || enc
->pubkey_algo
== PUBKEY_ALGO_DSA
383 || is_RSA(enc
->pubkey_algo
) ) {
384 /* FIXME: strore this all in a list and process it later */
386 if ( !c
->dek
&& ((!enc
->keyid
[0] && !enc
->keyid
[1])
387 || opt
.try_all_secrets
388 || !seckey_available( enc
->keyid
)) ) {
392 c
->dek
= xmalloc_secure_clear( sizeof *c
->dek
);
393 if( (result
= get_session_key( enc
, c
->dek
)) ) {
394 /* error: delete the DEK */
395 xfree(c
->dek
); c
->dek
= NULL
;
400 result
= G10ERR_NO_SECKEY
;
403 result
= G10ERR_PUBKEY_ALGO
;
409 /* store it for later display */
410 struct kidlist_item
*x
= xmalloc( sizeof *x
);
411 x
->kid
[0] = enc
->keyid
[0];
412 x
->kid
[1] = enc
->keyid
[1];
413 x
->pubkey_algo
= enc
->pubkey_algo
;
415 x
->next
= c
->pkenc_list
;
418 if( !result
&& opt
.verbose
> 1 )
419 log_info( _("public key encrypted data: good DEK\n") );
428 * Print the list of public key encrypted packets which we could
432 print_pkenc_list( struct kidlist_item
*list
, int failed
)
434 for( ; list
; list
= list
->next
) {
438 if ( failed
&& !list
->reason
)
440 if ( !failed
&& list
->reason
)
443 algstr
= gcry_pk_algo_name ( list
->pubkey_algo
);
444 pk
= xmalloc_clear( sizeof *pk
);
448 pk
->pubkey_algo
= list
->pubkey_algo
;
449 if( !get_pubkey( pk
, list
->kid
) )
452 log_info( _("encrypted with %u-bit %s key, ID %s, created %s\n"),
453 nbits_from_pk( pk
), algstr
, keystr_from_pk(pk
),
454 strtimestamp(pk
->timestamp
) );
455 p
=get_user_id_native(list
->kid
);
456 log_printf (_(" \"%s\"\n"),p
);
460 log_info(_("encrypted with %s key, ID %s\n"),
461 algstr
,keystr(list
->kid
));
463 free_public_key( pk
);
465 if( list
->reason
== G10ERR_NO_SECKEY
) {
466 if( is_status_enabled() ) {
468 sprintf(buf
,"%08lX%08lX", (ulong
)list
->kid
[0],
469 (ulong
)list
->kid
[1] );
470 write_status_text( STATUS_NO_SECKEY
, buf
);
473 else if (list
->reason
)
474 log_info(_("public key decryption failed: %s\n"),
475 g10_errstr(list
->reason
));
481 proc_encrypted( CTX c
, PACKET
*pkt
)
488 log_info(_("encrypted with %lu passphrases\n"),c
->symkeys
);
489 else if(c
->symkeys
==1)
490 log_info(_("encrypted with 1 passphrase\n"));
491 print_pkenc_list ( c
->pkenc_list
, 1 );
492 print_pkenc_list ( c
->pkenc_list
, 0 );
495 /* FIXME: Figure out the session key by looking at all pkenc packets. */
498 write_status( STATUS_BEGIN_DECRYPTION
);
500 /*log_debug("dat: %sencrypted data\n", c->dek?"":"conventional ");*/
503 else if( !c
->dek
&& !c
->last_was_session_key
) {
505 STRING2KEY s2kbuf
, *s2k
= NULL
;
507 if(opt
.override_session_key
)
509 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
510 result
=get_override_session_key(c
->dek
, opt
.override_session_key
);
519 /* Assume this is old style conventional encrypted data. */
520 algo
= opt
.def_cipher_algo
;
522 log_info (_("assuming %s encrypted data\n"),
523 gcry_cipher_algo_name (algo
));
524 else if ( gcry_cipher_test_algo (CIPHER_ALGO_IDEA
) )
526 algo
= opt
.def_cipher_algo
;
528 algo
= opt
.s2k_cipher_algo
;
530 log_info (_("IDEA cipher unavailable, "
531 "optimistically attempting to use %s instead\n"),
532 gcry_cipher_algo_name (algo
));
536 algo
= CIPHER_ALGO_IDEA
;
537 if (!opt
.s2k_digest_algo
)
539 /* If no digest is given we assume MD5 */
541 s2kbuf
.hash_algo
= DIGEST_ALGO_MD5
;
544 log_info (_("assuming %s encrypted data\n"), "IDEA");
547 c
->dek
= passphrase_to_dek ( NULL
, 0, algo
, s2k
, 0, NULL
, NULL
);
549 c
->dek
->algo_info_printed
= 1;
553 result
= G10ERR_NO_SECKEY
;
555 result
= decrypt_data( c
, pkt
->pkt
.encrypted
, c
->dek
);
559 else if( !result
|| (gpg_err_code (result
) == GPG_ERR_BAD_SIGNATURE
560 && opt
.ignore_mdc_error
)) {
561 write_status( STATUS_DECRYPTION_OKAY
);
562 if( opt
.verbose
> 1 )
563 log_info(_("decryption okay\n"));
564 if( pkt
->pkt
.encrypted
->mdc_method
&& !result
)
565 write_status( STATUS_GOODMDC
);
566 else if(!opt
.no_mdc_warn
)
567 log_info (_("WARNING: message was not integrity protected\n"));
568 if(opt
.show_session_key
)
571 char *buf
= xmalloc ( c
->dek
->keylen
*2 + 20 );
572 sprintf ( buf
, "%d:", c
->dek
->algo
);
573 for(i
=0; i
< c
->dek
->keylen
; i
++ )
574 sprintf(buf
+strlen(buf
), "%02X", c
->dek
->key
[i
] );
575 log_info( "session key: `%s'\n", buf
);
576 write_status_text ( STATUS_SESSION_KEY
, buf
);
579 else if( result
== G10ERR_BAD_SIGN
) {
580 log_error(_("WARNING: encrypted message has been manipulated!\n"));
581 write_status( STATUS_BADMDC
);
582 write_status( STATUS_DECRYPTION_FAILED
);
585 write_status( STATUS_DECRYPTION_FAILED
);
586 log_error(_("decryption failed: %s\n"), g10_errstr(result
));
587 /* Hmmm: does this work when we have encrypted using multiple
588 * ways to specify the session key (symmmetric and PK)*/
590 xfree(c
->dek
); c
->dek
= NULL
;
592 c
->last_was_session_key
= 0;
593 write_status( STATUS_END_DECRYPTION
);
598 proc_plaintext( CTX c
, PACKET
*pkt
)
600 PKT_plaintext
*pt
= pkt
->pkt
.plaintext
;
601 int any
, clearsig
, only_md5
, rc
;
606 if( pt
->namelen
== 8 && !memcmp( pt
->name
, "_CONSOLE", 8 ) )
607 log_info(_("NOTE: sender requested \"for-your-eyes-only\"\n"));
608 else if( opt
.verbose
)
609 log_info(_("original file name='%.*s'\n"), pt
->namelen
, pt
->name
);
610 free_md_filter_context( &c
->mfx
);
611 if (gcry_md_open (&c
->mfx
.md
, 0, 0))
613 /* fixme: we may need to push the textfilter if we have sigclass 1
614 * and no armoring - Not yet tested
615 * Hmmm, why don't we need it at all if we have sigclass 1
616 * Should we assume that plaintext in mode 't' has always sigclass 1??
617 * See: Russ Allbery's mail 1999-02-09
619 any
= clearsig
= only_md5
= 0;
620 for(n
=c
->list
; n
; n
= n
->next
)
622 if( n
->pkt
->pkttype
== PKT_ONEPASS_SIG
)
624 /* For the onepass signature case */
625 if( n
->pkt
->pkt
.onepass_sig
->digest_algo
)
627 gcry_md_enable (c
->mfx
.md
,
628 n
->pkt
->pkt
.onepass_sig
->digest_algo
);
629 if( !any
&& n
->pkt
->pkt
.onepass_sig
->digest_algo
636 if( n
->pkt
->pkt
.onepass_sig
->sig_class
!= 0x01 )
639 else if( n
->pkt
->pkttype
== PKT_GPG_CONTROL
640 && n
->pkt
->pkt
.gpg_control
->control
641 == CTRLPKT_CLEARSIGN_START
)
643 /* For the clearsigned message case */
644 size_t datalen
= n
->pkt
->pkt
.gpg_control
->datalen
;
645 const byte
*data
= n
->pkt
->pkt
.gpg_control
->data
;
647 /* check that we have at least the sigclass and one hash */
649 log_fatal("invalid control packet CTRLPKT_CLEARSIGN_START\n");
650 /* Note that we don't set the clearsig flag for not-dash-escaped
652 clearsig
= (*data
== 0x01);
653 for( data
++, datalen
--; datalen
; datalen
--, data
++ )
654 gcry_md_enable (c
->mfx
.md
, *data
);
656 break; /* Stop here as one-pass signature packets are not
659 else if(n
->pkt
->pkttype
==PKT_SIGNATURE
)
661 /* For the SIG+LITERAL case that PGP used to use. */
662 gcry_md_enable ( c
->mfx
.md
, n
->pkt
->pkt
.signature
->digest_algo
);
667 if( !any
&& !opt
.skip_verify
)
669 /* This is for the old GPG LITERAL+SIG case. It's not legal
670 according to 2440, so hopefully it won't come up that
671 often. There is no good way to specify what algorithms to
672 use in that case, so these three are the historical
674 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_RMD160
);
675 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_SHA1
);
676 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_MD5
);
678 if( opt
.pgp2_workarounds
&& only_md5
&& !opt
.skip_verify
) {
679 /* This is a kludge to work around a bug in pgp2. It does only
680 * catch those mails which are armored. To catch the non-armored
681 * pgp mails we could see whether there is the signature packet
682 * in front of the plaintext. If someone needs this, send me a patch.
684 if ( gcry_md_open (&c
->mfx
.md2
, DIGEST_ALGO_MD5
, 0) )
688 gcry_md_start_debug ( c
->mfx
.md
, "verify" );
690 gcry_md_start_debug ( c
->mfx
.md2
, "verify2" );
697 log_info (_("WARNING: multiple plaintexts seen\n"));
699 if (!opt
.flags
.allow_multiple_messages
)
701 write_status_text (STATUS_ERROR
, "proc_pkt.plaintext 89_BAD_DATA");
702 log_inc_errorcount ();
703 rc
= gpg_error (GPG_ERR_UNEXPECTED
);
709 rc
= handle_plaintext( pt
, &c
->mfx
, c
->sigs_only
, clearsig
);
710 if ( gpg_err_code (rc
) == GPG_ERR_EACCES
&& !c
->sigs_only
)
712 /* Can't write output but we hash it anyway to check the
714 rc
= handle_plaintext( pt
, &c
->mfx
, 1, clearsig
);
719 log_error( "handle plaintext failed: %s\n", g10_errstr(rc
));
721 c
->last_was_session_key
= 0;
723 /* We add a marker control packet instead of the plaintext packet.
724 * This is so that we can later detect invalid packet sequences.
726 n
= new_kbnode (create_gpg_control (CTRLPKT_PLAINTEXT_MARK
, NULL
, 0));
728 add_kbnode (c
->list
, n
);
735 proc_compressed_cb( IOBUF a
, void *info
)
737 if ( ((CTX
)info
)->signed_data
.used
738 && ((CTX
)info
)->signed_data
.data_fd
!= -1)
739 return proc_signature_packets_by_fd (info
, a
,
740 ((CTX
)info
)->signed_data
.data_fd
);
742 return proc_signature_packets (info
, a
,
743 ((CTX
)info
)->signed_data
.data_names
,
744 ((CTX
)info
)->sigfilename
);
748 proc_encrypt_cb( IOBUF a
, void *info
)
750 return proc_encryption_packets( info
, a
);
754 proc_compressed( CTX c
, PACKET
*pkt
)
756 PKT_compressed
*zd
= pkt
->pkt
.compressed
;
759 /*printf("zip: compressed data packet\n");*/
761 rc
=G10ERR_COMPR_ALGO
;
762 else if( c
->sigs_only
)
763 rc
= handle_compressed( c
, zd
, proc_compressed_cb
, c
);
764 else if( c
->encrypt_only
)
765 rc
= handle_compressed( c
, zd
, proc_encrypt_cb
, c
);
767 rc
= handle_compressed( c
, zd
, NULL
, NULL
);
769 log_error("uncompressing failed: %s\n", g10_errstr(rc
));
771 c
->last_was_session_key
= 0;
775 * check the signature
776 * Returns: 0 = valid signature or an error code
779 do_check_sig( CTX c
, KBNODE node
, int *is_selfsig
,
780 int *is_expkey
, int *is_revkey
)
783 gcry_md_hd_t md
= NULL
, md2
= NULL
;
786 assert( node
->pkt
->pkttype
== PKT_SIGNATURE
);
789 sig
= node
->pkt
->pkt
.signature
;
791 algo
= sig
->digest_algo
;
792 rc
= openpgp_md_test_algo(algo
);
796 if( sig
->sig_class
== 0x00 ) {
799 if (gcry_md_copy (&md
, c
->mfx
.md
))
802 else /* detached signature */
804 /* signature_check() will enable the md*/
805 if (gcry_md_open (&md
, 0, 0 ))
809 else if( sig
->sig_class
== 0x01 ) {
810 /* how do we know that we have to hash the (already hashed) text
811 * in canonical mode ??? (calculating both modes???) */
813 if (gcry_md_copy (&md
, c
->mfx
.md
))
815 if( c
->mfx
.md2
&& gcry_md_copy (&md2
, c
->mfx
.md2
))
818 else { /* detached signature */
819 log_debug("Do we really need this here?");
820 /* signature_check() will enable the md*/
821 if (gcry_md_open (&md
, 0, 0 ))
823 if (gcry_md_open (&md2
, 0, 0 ))
827 else if( (sig
->sig_class
&~3) == 0x10
828 || sig
->sig_class
== 0x18
829 || sig
->sig_class
== 0x1f
830 || sig
->sig_class
== 0x20
831 || sig
->sig_class
== 0x28
832 || sig
->sig_class
== 0x30 ) {
833 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
834 || c
->list
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
835 return check_key_signature( c
->list
, node
, is_selfsig
);
837 else if( sig
->sig_class
== 0x20 ) {
838 log_error (_("standalone revocation - "
839 "use \"gpg --import\" to apply\n"));
840 return G10ERR_NOT_PROCESSED
;
843 log_error("invalid root packet for sigclass %02x\n",
845 return G10ERR_SIG_CLASS
;
849 return G10ERR_SIG_CLASS
;
850 rc
= signature_check2( sig
, md
, NULL
, is_expkey
, is_revkey
, NULL
);
851 if( gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
&& md2
)
852 rc
= signature_check2( sig
, md2
, NULL
, is_expkey
, is_revkey
, NULL
);
861 print_userid( PACKET
*pkt
)
865 if( pkt
->pkttype
!= PKT_USER_ID
) {
866 printf("ERROR: unexpected packet type %d", pkt
->pkttype
);
869 if( opt
.with_colons
)
871 if(pkt
->pkt
.user_id
->attrib_data
)
873 pkt
->pkt
.user_id
->numattribs
,
874 pkt
->pkt
.user_id
->attrib_len
);
876 print_string( stdout
, pkt
->pkt
.user_id
->name
,
877 pkt
->pkt
.user_id
->len
, ':');
880 print_utf8_string( stdout
, pkt
->pkt
.user_id
->name
,
881 pkt
->pkt
.user_id
->len
);
886 * List the certificate in a user friendly way
890 list_node( CTX c
, KBNODE node
)
897 else if( (mainkey
= (node
->pkt
->pkttype
== PKT_PUBLIC_KEY
) )
898 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
899 PKT_public_key
*pk
= node
->pkt
->pkt
.public_key
;
901 if( opt
.with_colons
)
904 keyid_from_pk( pk
, keyid
);
906 c
->trustletter
= opt
.fast_list_mode
?
907 0 : get_validity_info( pk
, NULL
);
908 printf("%s:", mainkey
? "pub":"sub" );
910 putchar( c
->trustletter
);
911 printf(":%u:%d:%08lX%08lX:%s:%s::",
914 (ulong
)keyid
[0],(ulong
)keyid
[1],
915 colon_datestr_from_pk( pk
),
916 colon_strtime (pk
->expiredate
) );
917 if( mainkey
&& !opt
.fast_list_mode
)
918 putchar( get_ownertrust_info (pk
) );
920 if( node
->next
&& node
->next
->pkt
->pkttype
== PKT_RING_TRUST
) {
921 putchar('\n'); any
=1;
922 if( opt
.fingerprint
)
923 print_fingerprint( pk
, NULL
, 0 );
924 printf("rtv:1:%u:\n",
925 node
->next
->pkt
->pkt
.ring_trust
->trustval
);
929 printf("%s %4u%c/%s %s%s",
930 mainkey
? "pub":"sub", nbits_from_pk( pk
),
931 pubkey_letter( pk
->pubkey_algo
), keystr_from_pk( pk
),
932 datestr_from_pk( pk
), mainkey
?" ":"");
935 /* and now list all userids with their signatures */
936 for( node
= node
->next
; node
; node
= node
->next
) {
937 if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
939 if( node
->pkt
->pkt
.signature
->sig_class
== 0x20 )
947 else if( node
->pkt
->pkttype
== PKT_USER_ID
) {
949 if( opt
.with_colons
)
950 printf("%s:::::::::",
951 node
->pkt
->pkt
.user_id
->attrib_data
?"uat":"uid");
953 printf( "uid%*s", 28, "" );
955 print_userid( node
->pkt
);
956 if( opt
.with_colons
)
959 if( opt
.fingerprint
&& !any
)
960 print_fingerprint( pk
, NULL
, 0 );
963 && node
->next
->pkt
->pkttype
== PKT_RING_TRUST
) {
964 printf("rtv:2:%u:\n",
965 node
->next
->pkt
->pkt
.ring_trust
?
966 node
->next
->pkt
->pkt
.ring_trust
->trustval
: 0);
970 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
985 printf(_("revoked: %s"),revokestr_from_pk(pk
));
988 else if( pk
->expiredate
)
991 printf(_("expires: %s"),expirestr_from_pk(pk
));
998 if( !mainkey
&& opt
.fingerprint
> 1 )
999 print_fingerprint( pk
, NULL
, 0 );
1001 else if( (mainkey
= (node
->pkt
->pkttype
== PKT_SECRET_KEY
) )
1002 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1003 PKT_secret_key
*sk
= node
->pkt
->pkt
.secret_key
;
1005 if( opt
.with_colons
)
1008 keyid_from_sk( sk
, keyid
);
1009 printf("%s::%u:%d:%08lX%08lX:%s:%s:::",
1010 mainkey
? "sec":"ssb",
1011 nbits_from_sk( sk
),
1013 (ulong
)keyid
[0],(ulong
)keyid
[1],
1014 colon_datestr_from_sk( sk
),
1015 colon_strtime (sk
->expiredate
)
1016 /* fixme: add LID */ );
1019 printf("%s %4u%c/%s %s ", mainkey
? "sec":"ssb",
1020 nbits_from_sk( sk
), pubkey_letter( sk
->pubkey_algo
),
1021 keystr_from_sk( sk
), datestr_from_sk( sk
));
1023 /* and now list all userids with their signatures */
1024 for( node
= node
->next
; node
; node
= node
->next
) {
1025 if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1027 if( node
->pkt
->pkt
.signature
->sig_class
== 0x20 )
1033 list_node(c
, node
);
1035 else if( node
->pkt
->pkttype
== PKT_USER_ID
) {
1037 if( opt
.with_colons
)
1038 printf("%s:::::::::",
1039 node
->pkt
->pkt
.user_id
->attrib_data
?"uat":"uid");
1041 printf( "uid%*s", 28, "" );
1043 print_userid( node
->pkt
);
1044 if( opt
.with_colons
)
1047 if( opt
.fingerprint
&& !any
)
1048 print_fingerprint( NULL
, sk
, 0 );
1051 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1056 list_node(c
, node
);
1062 if( !mainkey
&& opt
.fingerprint
> 1 )
1063 print_fingerprint( NULL
, sk
, 0 );
1065 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1066 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1076 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x30 )
1077 fputs("rev", stdout
);
1079 fputs("sig", stdout
);
1080 if( opt
.check_sigs
) {
1082 rc2
=do_check_sig( c
, node
, &is_selfsig
, NULL
, NULL
);
1083 switch (gpg_err_code (rc2
)) {
1084 case 0: sigrc
= '!'; break;
1085 case GPG_ERR_BAD_SIGNATURE
: sigrc
= '-'; break;
1086 case GPG_ERR_NO_PUBKEY
:
1087 case GPG_ERR_UNUSABLE_PUBKEY
: sigrc
= '?'; break;
1088 default: sigrc
= '%'; break;
1091 else { /* check whether this is a self signature */
1094 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
1095 || c
->list
->pkt
->pkttype
== PKT_SECRET_KEY
) {
1096 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
1097 keyid_from_pk( c
->list
->pkt
->pkt
.public_key
, keyid
);
1099 keyid_from_sk( c
->list
->pkt
->pkt
.secret_key
, keyid
);
1101 if( keyid
[0] == sig
->keyid
[0] && keyid
[1] == sig
->keyid
[1] )
1105 if( opt
.with_colons
) {
1109 printf("::%d:%08lX%08lX:%s:%s:", sig
->pubkey_algo
,
1110 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1111 colon_datestr_from_sig(sig
),
1112 colon_expirestr_from_sig(sig
));
1114 if(sig
->trust_depth
|| sig
->trust_value
)
1115 printf("%d %d",sig
->trust_depth
,sig
->trust_value
);
1118 if(sig
->trust_regexp
)
1119 print_string(stdout
,sig
->trust_regexp
,
1120 strlen(sig
->trust_regexp
),':');
1125 sigrc
, keystr(sig
->keyid
), datestr_from_sig(sig
));
1127 printf("[%s] ", g10_errstr(rc2
) );
1128 else if( sigrc
== '?' )
1130 else if( is_selfsig
) {
1131 if( opt
.with_colons
)
1133 fputs( sig
->sig_class
== 0x18? "[keybind]":"[selfsig]", stdout
);
1134 if( opt
.with_colons
)
1137 else if( !opt
.fast_list_mode
) {
1138 p
= get_user_id( sig
->keyid
, &n
);
1139 print_string( stdout
, p
, n
, opt
.with_colons
);
1142 if( opt
.with_colons
)
1143 printf(":%02x%c:", sig
->sig_class
, sig
->flags
.exportable
?'x':'l');
1147 log_error("invalid node with packet of type %d\n", node
->pkt
->pkttype
);
1153 proc_packets( void *anchor
, IOBUF a
)
1156 CTX c
= xmalloc_clear( sizeof *c
);
1159 rc
= do_proc_packets( c
, a
);
1167 proc_signature_packets( void *anchor
, IOBUF a
,
1168 strlist_t signedfiles
, const char *sigfilename
)
1170 CTX c
= xmalloc_clear( sizeof *c
);
1176 c
->signed_data
.data_fd
= -1;
1177 c
->signed_data
.data_names
= signedfiles
;
1178 c
->signed_data
.used
= !!signedfiles
;
1180 c
->sigfilename
= sigfilename
;
1181 rc
= do_proc_packets( c
, a
);
1183 /* If we have not encountered any signature we print an error
1184 messages, send a NODATA status back and return an error code.
1185 Using log_error is required because verify_files does not check
1186 error codes for each file but we want to terminate the process
1188 if (!rc
&& !c
->any_sig_seen
)
1190 write_status_text (STATUS_NODATA
, "4");
1191 log_error (_("no signature found\n"));
1192 rc
= G10ERR_NO_DATA
;
1195 /* Propagate the signature seen flag upward. Do this only on
1196 success so that we won't issue the nodata status several
1198 if (!rc
&& c
->anchor
&& c
->any_sig_seen
)
1199 c
->anchor
->any_sig_seen
= 1;
1206 proc_signature_packets_by_fd (void *anchor
, IOBUF a
, int signed_data_fd
)
1209 CTX c
= xcalloc (1, sizeof *c
);
1214 c
->signed_data
.data_fd
= signed_data_fd
;
1215 c
->signed_data
.data_names
= NULL
;
1216 c
->signed_data
.used
= (signed_data_fd
!= -1);
1218 rc
= do_proc_packets ( c
, a
);
1220 /* If we have not encountered any signature we print an error
1221 messages, send a NODATA status back and return an error code.
1222 Using log_error is required because verify_files does not check
1223 error codes for each file but we want to terminate the process
1225 if (!rc
&& !c
->any_sig_seen
)
1227 write_status_text (STATUS_NODATA
, "4");
1228 log_error (_("no signature found\n"));
1229 rc
= gpg_error (GPG_ERR_NO_DATA
);
1232 /* Propagate the signature seen flag upward. Do this only on success
1233 so that we won't issue the nodata status several times. */
1234 if (!rc
&& c
->anchor
&& c
->any_sig_seen
)
1235 c
->anchor
->any_sig_seen
= 1;
1243 proc_encryption_packets( void *anchor
, IOBUF a
)
1245 CTX c
= xmalloc_clear( sizeof *c
);
1249 c
->encrypt_only
= 1;
1250 rc
= do_proc_packets( c
, a
);
1257 do_proc_packets( CTX c
, IOBUF a
)
1259 PACKET
*pkt
= xmalloc( sizeof *pkt
);
1266 while( (rc
=parse_packet(a
, pkt
)) != -1 ) {
1270 /* stop processing when an invalid packet has been encountered
1271 * but don't do so when we are doing a --list-packets. */
1272 if (gpg_err_code (rc
) == GPG_ERR_INV_PACKET
1273 && opt
.list_packets
!= 2 )
1278 if( opt
.list_packets
) {
1279 switch( pkt
->pkttype
) {
1280 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1281 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1283 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1284 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1285 default: newpkt
= 0; break;
1288 else if( c
->sigs_only
) {
1289 switch( pkt
->pkttype
) {
1290 case PKT_PUBLIC_KEY
:
1291 case PKT_SECRET_KEY
:
1293 case PKT_SYMKEY_ENC
:
1294 case PKT_PUBKEY_ENC
:
1296 case PKT_ENCRYPTED_MDC
:
1297 write_status_text( STATUS_UNEXPECTED
, "0" );
1298 rc
= G10ERR_UNEXPECTED
;
1300 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1301 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1302 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1303 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1304 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1305 default: newpkt
= 0; break;
1308 else if( c
->encrypt_only
) {
1309 switch( pkt
->pkttype
) {
1310 case PKT_PUBLIC_KEY
:
1311 case PKT_SECRET_KEY
:
1313 write_status_text( STATUS_UNEXPECTED
, "0" );
1314 rc
= G10ERR_UNEXPECTED
;
1316 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1317 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1318 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1320 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1321 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1322 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1323 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1324 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1325 default: newpkt
= 0; break;
1329 switch( pkt
->pkttype
) {
1330 case PKT_PUBLIC_KEY
:
1331 case PKT_SECRET_KEY
:
1333 c
->list
= new_kbnode( pkt
);
1336 case PKT_PUBLIC_SUBKEY
:
1337 case PKT_SECRET_SUBKEY
:
1338 newpkt
= add_subkey( c
, pkt
);
1340 case PKT_USER_ID
: newpkt
= add_user_id( c
, pkt
); break;
1341 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1342 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1343 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1345 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1346 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1347 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1348 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1349 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1350 case PKT_RING_TRUST
: newpkt
= add_ring_trust( c
, pkt
); break;
1351 default: newpkt
= 0; break;
1354 /* This is a very ugly construct and frankly, I don't remember why
1355 * I used it. Adding the MDC check here is a hack.
1356 * The right solution is to initiate another context for encrypted
1357 * packet and not to reuse the current one ... It works right
1358 * when there is a compression packet inbetween which adds just
1360 * Hmmm: Rewrite this whole module here??
1362 if( pkt
->pkttype
!= PKT_SIGNATURE
&& pkt
->pkttype
!= PKT_MDC
)
1363 c
->have_data
= pkt
->pkttype
== PKT_PLAINTEXT
;
1368 pkt
= xmalloc( sizeof *pkt
);
1374 if( rc
== G10ERR_INVALID_PACKET
)
1375 write_status_text( STATUS_NODATA
, "3" );
1379 write_status_text( STATUS_NODATA
, "2" );
1387 free_md_filter_context( &c
->mfx
);
1392 /* Helper for pka_uri_from_sig to parse the to-be-verified address out
1393 of the notation data. */
1395 get_pka_address (PKT_signature
*sig
)
1397 pka_info_t
*pka
= NULL
;
1398 struct notation
*nd
,*notation
;
1400 notation
=sig_to_notation(sig
);
1402 for(nd
=notation
;nd
;nd
=nd
->next
)
1404 if(strcmp(nd
->name
,"pka-address@gnupg.org")!=0)
1405 continue; /* Not the notation we want. */
1407 /* For now we only use the first valid PKA notation. In future
1408 we might want to keep additional PKA notations in a linked
1410 if (is_valid_mailbox (nd
->value
))
1412 pka
= xmalloc (sizeof *pka
+ strlen(nd
->value
));
1416 strcpy (pka
->email
, nd
->value
);
1421 free_notation(notation
);
1427 /* Return the URI from a DNS PKA record. If this record has already
1428 be retrieved for the signature we merely return it; if not we go
1429 out and try to get that DNS record. */
1431 pka_uri_from_sig (PKT_signature
*sig
)
1433 if (!sig
->flags
.pka_tried
)
1435 assert (!sig
->pka_info
);
1436 sig
->flags
.pka_tried
= 1;
1437 sig
->pka_info
= get_pka_address (sig
);
1442 uri
= get_pka_info (sig
->pka_info
->email
, sig
->pka_info
->fpr
);
1445 sig
->pka_info
->valid
= 1;
1449 sig
->pka_info
->uri
= uri
;
1453 return sig
->pka_info
? sig
->pka_info
->uri
: NULL
;
1458 check_sig_and_print( CTX c
, KBNODE node
)
1460 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1462 int rc
, is_expkey
=0, is_revkey
=0;
1464 if (opt
.skip_verify
)
1466 log_info(_("signature verification suppressed\n"));
1470 /* Check that the message composition is valid.
1472 Per RFC-2440bis (-15) allowed:
1474 S{1,n} -- detached signature.
1475 S{1,n} P -- old style PGP2 signature
1476 O{1,n} P S{1,n} -- standard OpenPGP signature.
1477 C P S{1,n} -- cleartext signature.
1480 O = One-Pass Signature packet.
1481 S = Signature packet.
1482 P = OpenPGP Message packet (Encrypted | Compressed | Literal)
1483 (Note that the current rfc2440bis draft also allows
1484 for a signed message but that does not work as it
1485 introduces ambiguities.)
1486 We keep track of these packages using the marker packet
1487 CTRLPKT_PLAINTEXT_MARK.
1488 C = Marker packet for cleartext signatures.
1490 We reject all other messages.
1492 Actually we are calling this too often, i.e. for verification of
1493 each message but better have some duplicate work than to silently
1494 introduce a bug here.
1498 int n_onepass
, n_sig
;
1500 /* log_debug ("checking signature packet composition\n"); */
1501 /* dump_kbnode (c->list); */
1505 if ( n
->pkt
->pkttype
== PKT_SIGNATURE
)
1507 /* This is either "S{1,n}" case (detached signature) or
1508 "S{1,n} P" (old style PGP2 signature). */
1509 for (n
= n
->next
; n
; n
= n
->next
)
1510 if (n
->pkt
->pkttype
!= PKT_SIGNATURE
)
1513 ; /* Okay, this is a detached signature. */
1514 else if (n
->pkt
->pkttype
== PKT_GPG_CONTROL
1515 && (n
->pkt
->pkt
.gpg_control
->control
1516 == CTRLPKT_PLAINTEXT_MARK
) )
1519 goto ambiguous
; /* We only allow one P packet. */
1524 else if (n
->pkt
->pkttype
== PKT_ONEPASS_SIG
)
1526 /* This is the "O{1,n} P S{1,n}" case (standard signature). */
1527 for (n_onepass
=1, n
= n
->next
;
1528 n
&& n
->pkt
->pkttype
== PKT_ONEPASS_SIG
; n
= n
->next
)
1530 if (!n
|| !(n
->pkt
->pkttype
== PKT_GPG_CONTROL
1531 && (n
->pkt
->pkt
.gpg_control
->control
1532 == CTRLPKT_PLAINTEXT_MARK
)))
1534 for (n_sig
=0, n
= n
->next
;
1535 n
&& n
->pkt
->pkttype
== PKT_SIGNATURE
; n
= n
->next
)
1540 /* If we wanted to disallow multiple sig verification, we'd do
1541 something like this:
1543 if (n && !opt.allow_multisig_verification)
1546 However, now that we have --allow-multiple-messages, this
1547 can stay allowable as we can't get here unless multiple
1548 messages (i.e. multiple literals) are allowed. */
1550 if (n_onepass
!= n_sig
)
1552 log_info ("number of one-pass packets does not match "
1553 "number of signature packets\n");
1557 else if (n
->pkt
->pkttype
== PKT_GPG_CONTROL
1558 && n
->pkt
->pkt
.gpg_control
->control
== CTRLPKT_CLEARSIGN_START
)
1560 /* This is the "C P S{1,n}" case (clear text signature). */
1562 if (!n
|| !(n
->pkt
->pkttype
== PKT_GPG_CONTROL
1563 && (n
->pkt
->pkt
.gpg_control
->control
1564 == CTRLPKT_PLAINTEXT_MARK
)))
1566 for (n_sig
=0, n
= n
->next
;
1567 n
&& n
->pkt
->pkttype
== PKT_SIGNATURE
; n
= n
->next
)
1575 log_error(_("can't handle this ambiguous signature data\n"));
1581 /* (Indendation below not yet changed to GNU style.) */
1583 astr
= gcry_pk_algo_name ( sig
->pubkey_algo
);
1586 log_info(_("Signature made %s\n"),asctimestamp(sig
->timestamp
));
1587 log_info(_(" using %s key %s\n"),
1588 astr
? astr
: "?",keystr(sig
->keyid
));
1591 log_info(_("Signature made %s using %s key ID %s\n"),
1592 asctimestamp(sig
->timestamp
), astr
? astr
: "?",
1593 keystr(sig
->keyid
));
1595 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1597 /* If the key isn't found, check for a preferred keyserver */
1599 if(rc
==G10ERR_NO_PUBKEY
&& sig
->flags
.pref_ks
)
1605 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&n
,&seq
,NULL
)))
1607 /* According to my favorite copy editor, in English
1608 grammar, you say "at" if the key is located on a web
1609 page, but "from" if it is located on a keyserver. I'm
1610 not going to even try to make two strings here :) */
1611 log_info(_("Key available at: ") );
1612 print_utf8_string( log_get_stream(), p
, n
);
1615 if(opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
1616 && opt
.keyserver_options
.options
&KEYSERVER_HONOR_KEYSERVER_URL
)
1618 struct keyserver_spec
*spec
;
1620 spec
=parse_preferred_keyserver(sig
);
1625 glo_ctrl
.in_auto_key_retrieve
++;
1626 res
=keyserver_import_keyid(sig
->keyid
,spec
);
1627 glo_ctrl
.in_auto_key_retrieve
--;
1629 rc
=do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1630 free_keyserver_spec(spec
);
1639 /* If the preferred keyserver thing above didn't work, our second
1640 try is to use the URI from a DNS PKA record. */
1641 if ( rc
== G10ERR_NO_PUBKEY
1642 && opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
1643 && opt
.keyserver_options
.options
&KEYSERVER_HONOR_PKA_RECORD
)
1645 const char *uri
= pka_uri_from_sig (sig
);
1649 /* FIXME: We might want to locate the key using the
1650 fingerprint instead of the keyid. */
1652 struct keyserver_spec
*spec
;
1654 spec
= parse_keyserver_uri (uri
, 1, NULL
, 0);
1657 glo_ctrl
.in_auto_key_retrieve
++;
1658 res
= keyserver_import_keyid (sig
->keyid
, spec
);
1659 glo_ctrl
.in_auto_key_retrieve
--;
1660 free_keyserver_spec (spec
);
1662 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1667 /* If the preferred keyserver thing above didn't work and we got
1668 no information from the DNS PKA, this is a third try. */
1670 if( rc
== G10ERR_NO_PUBKEY
&& opt
.keyserver
1671 && opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
)
1675 glo_ctrl
.in_auto_key_retrieve
++;
1676 res
=keyserver_import_keyid ( sig
->keyid
, opt
.keyserver
);
1677 glo_ctrl
.in_auto_key_retrieve
--;
1679 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1682 if( !rc
|| gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
) {
1683 KBNODE un
, keyblock
;
1684 int count
=0, statno
;
1686 PKT_public_key
*pk
=NULL
;
1689 statno
=STATUS_BADSIG
;
1690 else if(sig
->flags
.expired
)
1691 statno
=STATUS_EXPSIG
;
1693 statno
=STATUS_EXPKEYSIG
;
1695 statno
=STATUS_REVKEYSIG
;
1697 statno
=STATUS_GOODSIG
;
1699 keyblock
= get_pubkeyblock( sig
->keyid
);
1701 sprintf (keyid_str
, "%08lX%08lX [uncertain] ",
1702 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1]);
1704 /* find and print the primary user ID */
1705 for( un
=keyblock
; un
; un
= un
->next
) {
1708 if(un
->pkt
->pkttype
==PKT_PUBLIC_KEY
)
1710 pk
=un
->pkt
->pkt
.public_key
;
1713 if( un
->pkt
->pkttype
!= PKT_USER_ID
)
1715 if ( !un
->pkt
->pkt
.user_id
->created
)
1717 if ( un
->pkt
->pkt
.user_id
->is_revoked
)
1719 if ( un
->pkt
->pkt
.user_id
->is_expired
)
1721 if ( !un
->pkt
->pkt
.user_id
->is_primary
)
1723 /* We want the textual primary user ID here */
1724 if ( un
->pkt
->pkt
.user_id
->attrib_data
)
1729 /* Get it before we print anything to avoid interrupting
1730 the output with the "please do a --check-trustdb"
1732 valid
=get_validity(pk
,un
->pkt
->pkt
.user_id
);
1734 keyid_str
[17] = 0; /* cut off the "[uncertain]" part */
1735 write_status_text_and_buffer (statno
, keyid_str
,
1736 un
->pkt
->pkt
.user_id
->name
,
1737 un
->pkt
->pkt
.user_id
->len
,
1740 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1741 un
->pkt
->pkt
.user_id
->len
,0);
1744 log_info(_("BAD signature from \"%s\""),p
);
1745 else if(sig
->flags
.expired
)
1746 log_info(_("Expired signature from \"%s\""),p
);
1748 log_info(_("Good signature from \"%s\""),p
);
1752 if(opt
.verify_options
&VERIFY_SHOW_UID_VALIDITY
)
1753 log_printf (" [%s]\n",trust_value_to_string(valid
));
1758 if( !count
) { /* just in case that we have no valid textual
1762 /* Try for an invalid textual userid */
1763 for( un
=keyblock
; un
; un
= un
->next
) {
1764 if( un
->pkt
->pkttype
== PKT_USER_ID
&&
1765 !un
->pkt
->pkt
.user_id
->attrib_data
)
1769 /* Try for any userid at all */
1771 for( un
=keyblock
; un
; un
= un
->next
) {
1772 if( un
->pkt
->pkttype
== PKT_USER_ID
)
1777 if (opt
.trust_model
==TM_ALWAYS
|| !un
)
1778 keyid_str
[17] = 0; /* cut off the "[uncertain]" part */
1780 write_status_text_and_buffer (statno
, keyid_str
,
1781 un
? un
->pkt
->pkt
.user_id
->name
:"[?]",
1782 un
? un
->pkt
->pkt
.user_id
->len
:3,
1786 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1787 un
->pkt
->pkt
.user_id
->len
,0);
1792 log_info(_("BAD signature from \"%s\""),p
);
1793 else if(sig
->flags
.expired
)
1794 log_info(_("Expired signature from \"%s\""),p
);
1796 log_info(_("Good signature from \"%s\""),p
);
1797 if (opt
.trust_model
!=TM_ALWAYS
&& un
)
1798 log_printf (" %s",_("[uncertain]") );
1802 /* If we have a good signature and already printed
1803 * the primary user ID, print all the other user IDs */
1805 && !(opt
.verify_options
&VERIFY_SHOW_PRIMARY_UID_ONLY
)) {
1807 for( un
=keyblock
; un
; un
= un
->next
) {
1808 if( un
->pkt
->pkttype
!= PKT_USER_ID
)
1810 if((un
->pkt
->pkt
.user_id
->is_revoked
1811 || un
->pkt
->pkt
.user_id
->is_expired
)
1812 && !(opt
.verify_options
&VERIFY_SHOW_UNUSABLE_UIDS
))
1814 /* Only skip textual primaries */
1815 if ( un
->pkt
->pkt
.user_id
->is_primary
&&
1816 !un
->pkt
->pkt
.user_id
->attrib_data
)
1819 if(un
->pkt
->pkt
.user_id
->attrib_data
)
1821 dump_attribs(un
->pkt
->pkt
.user_id
,pk
,NULL
);
1823 if(opt
.verify_options
&VERIFY_SHOW_PHOTOS
)
1824 show_photos(un
->pkt
->pkt
.user_id
->attribs
,
1825 un
->pkt
->pkt
.user_id
->numattribs
,pk
,NULL
);
1828 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1829 un
->pkt
->pkt
.user_id
->len
,0);
1830 log_info(_(" aka \"%s\""),p
);
1833 if(opt
.verify_options
&VERIFY_SHOW_UID_VALIDITY
)
1836 if(un
->pkt
->pkt
.user_id
->is_revoked
)
1838 else if(un
->pkt
->pkt
.user_id
->is_expired
)
1841 valid
=trust_value_to_string(get_validity(pk
,
1844 log_printf (" [%s]\n",valid
);
1850 release_kbnode( keyblock
);
1854 if(opt
.verify_options
&VERIFY_SHOW_POLICY_URLS
)
1855 show_policy_url(sig
,0,1);
1857 show_policy_url(sig
,0,2);
1859 if(opt
.verify_options
&VERIFY_SHOW_KEYSERVER_URLS
)
1860 show_keyserver_url(sig
,0,1);
1862 show_keyserver_url(sig
,0,2);
1864 if(opt
.verify_options
&VERIFY_SHOW_NOTATIONS
)
1865 show_notation(sig
,0,1,
1866 ((opt
.verify_options
&VERIFY_SHOW_STD_NOTATIONS
)?1:0)+
1867 ((opt
.verify_options
&VERIFY_SHOW_USER_NOTATIONS
)?2:0));
1869 show_notation(sig
,0,2,0);
1872 if( !rc
&& is_status_enabled() ) {
1873 /* print a status response with the fingerprint */
1874 PKT_public_key
*vpk
= xmalloc_clear( sizeof *vpk
);
1876 if( !get_pubkey( vpk
, sig
->keyid
) ) {
1877 byte array
[MAX_FINGERPRINT_LEN
], *p
;
1878 char buf
[MAX_FINGERPRINT_LEN
*4+90], *bufp
;
1882 fingerprint_from_pk( vpk
, array
, &n
);
1884 for(i
=0; i
< n
; i
++, p
++, bufp
+= 2)
1885 sprintf(bufp
, "%02X", *p
);
1886 /* TODO: Replace the reserved '0' in the field below
1887 with bits for status flags (policy url, notation,
1888 etc.). Remember to make the buffer larger to
1890 sprintf(bufp
, " %s %lu %lu %d 0 %d %d %02X ",
1891 strtimestamp( sig
->timestamp
),
1892 (ulong
)sig
->timestamp
,(ulong
)sig
->expiredate
,
1893 sig
->version
,sig
->pubkey_algo
,sig
->digest_algo
,
1895 bufp
= bufp
+ strlen (bufp
);
1896 if (!vpk
->is_primary
) {
1899 akid
[0] = vpk
->main_keyid
[0];
1900 akid
[1] = vpk
->main_keyid
[1];
1901 free_public_key (vpk
);
1902 vpk
= xmalloc_clear( sizeof *vpk
);
1903 if (get_pubkey (vpk
, akid
)) {
1904 /* impossible error, we simply return a zeroed out fpr */
1905 n
= MAX_FINGERPRINT_LEN
< 20? MAX_FINGERPRINT_LEN
: 20;
1906 memset (array
, 0, n
);
1909 fingerprint_from_pk( vpk
, array
, &n
);
1912 for(i
=0; i
< n
; i
++, p
++, bufp
+= 2)
1913 sprintf(bufp
, "%02X", *p
);
1914 write_status_text( STATUS_VALIDSIG
, buf
);
1916 free_public_key( vpk
);
1921 if(opt
.verify_options
&VERIFY_PKA_LOOKUPS
)
1922 pka_uri_from_sig (sig
); /* Make sure PKA info is available. */
1923 rc
= check_signatures_trust( sig
);
1926 if(sig
->flags
.expired
)
1928 log_info(_("Signature expired %s\n"),
1929 asctimestamp(sig
->expiredate
));
1930 rc
=G10ERR_GENERAL
; /* need a better error here? */
1932 else if(sig
->expiredate
)
1933 log_info(_("Signature expires %s\n"),asctimestamp(sig
->expiredate
));
1936 log_info(_("%s signature, digest algorithm %s\n"),
1937 sig
->sig_class
==0x00?_("binary"):
1938 sig
->sig_class
==0x01?_("textmode"):_("unknown"),
1939 gcry_md_algo_name (sig
->digest_algo
));
1942 g10_errors_seen
= 1;
1943 if( opt
.batch
&& rc
)
1948 sprintf(buf
, "%08lX%08lX %d %d %02x %lu %d",
1949 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1950 sig
->pubkey_algo
, sig
->digest_algo
,
1951 sig
->sig_class
, (ulong
)sig
->timestamp
, rc
);
1952 write_status_text( STATUS_ERRSIG
, buf
);
1953 if( rc
== G10ERR_NO_PUBKEY
) {
1955 write_status_text( STATUS_NO_PUBKEY
, buf
);
1957 if( rc
!= G10ERR_NOT_PROCESSED
)
1958 log_error(_("Can't check signature: %s\n"), g10_errstr(rc
) );
1965 * Process the tree which starts at node
1968 proc_tree( CTX c
, KBNODE node
)
1973 if( opt
.list_packets
|| opt
.list_only
)
1976 /* we must skip our special plaintext marker packets here becuase
1977 they may be the root packet. These packets are only used in
1978 addionla checks and skipping them here doesn't matter */
1980 && node
->pkt
->pkttype
== PKT_GPG_CONTROL
1981 && node
->pkt
->pkt
.gpg_control
->control
1982 == CTRLPKT_PLAINTEXT_MARK
) {
1988 c
->trustletter
= ' ';
1989 if( node
->pkt
->pkttype
== PKT_PUBLIC_KEY
1990 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1991 merge_keys_and_selfsig( node
);
1992 list_node( c
, node
);
1994 else if( node
->pkt
->pkttype
== PKT_SECRET_KEY
) {
1995 merge_keys_and_selfsig( node
);
1996 list_node( c
, node
);
1998 else if( node
->pkt
->pkttype
== PKT_ONEPASS_SIG
) {
1999 /* check all signatures */
2000 if( !c
->have_data
) {
2001 int use_textmode
= 0;
2003 free_md_filter_context( &c
->mfx
);
2004 /* prepare to create all requested message digests */
2005 if (gcry_md_open (&c
->mfx
.md
, 0, 0))
2008 /* fixme: why looking for the signature packet and not the
2010 for ( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
2012 gcry_md_enable (c
->mfx
.md
,
2013 n1
->pkt
->pkt
.signature
->digest_algo
);
2016 if (n1
&& n1
->pkt
->pkt
.onepass_sig
->sig_class
== 0x01)
2019 /* Ask for file and hash it. */
2020 if( c
->sigs_only
) {
2021 if (c
->signed_data
.used
&& c
->signed_data
.data_fd
!= -1)
2022 rc
= hash_datafile_by_fd (c
->mfx
.md
, NULL
,
2023 c
->signed_data
.data_fd
,
2026 rc
= hash_datafiles (c
->mfx
.md
, NULL
,
2027 c
->signed_data
.data_names
,
2032 rc
= ask_for_detached_datafile (c
->mfx
.md
, c
->mfx
.md2
,
2033 iobuf_get_real_fname(c
->iobuf
),
2037 log_error("can't hash datafile: %s\n", g10_errstr(rc
));
2041 else if ( c
->signed_data
.used
) {
2042 log_error (_("not a detached signature\n") );
2046 for( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
2047 check_sig_and_print( c
, n1
);
2049 else if( node
->pkt
->pkttype
== PKT_GPG_CONTROL
2050 && node
->pkt
->pkt
.gpg_control
->control
2051 == CTRLPKT_CLEARSIGN_START
) {
2052 /* clear text signed message */
2053 if( !c
->have_data
) {
2054 log_error("cleartext signature without data\n" );
2057 else if ( c
->signed_data
.used
) {
2058 log_error (_("not a detached signature\n") );
2062 for( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
2063 check_sig_and_print( c
, n1
);
2065 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
2066 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
2069 n1
=find_next_kbnode(node
, PKT_SIGNATURE
);
2072 byte
class=sig
->sig_class
;
2073 byte hash
=sig
->digest_algo
;
2075 for(; n1
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)))
2077 /* We can't currently handle multiple signatures of
2078 different classes or digests (we'd pretty much have
2079 to run a different hash context for each), but if
2080 they are all the same, make an exception. */
2081 if(n1
->pkt
->pkt
.signature
->sig_class
!=class
2082 || n1
->pkt
->pkt
.signature
->digest_algo
!=hash
)
2085 log_info(_("WARNING: multiple signatures detected. "
2086 "Only the first will be checked.\n"));
2092 if( sig
->sig_class
!= 0x00 && sig
->sig_class
!= 0x01 )
2093 log_info(_("standalone signature of class 0x%02x\n"),
2095 else if( !c
->have_data
) {
2096 /* detached signature */
2097 free_md_filter_context( &c
->mfx
);
2098 if (gcry_md_open (&c
->mfx
.md
, sig
->digest_algo
, 0))
2101 if( !opt
.pgp2_workarounds
)
2103 else if( sig
->digest_algo
== DIGEST_ALGO_MD5
2104 && is_RSA( sig
->pubkey_algo
) ) {
2105 /* enable a workaround for a pgp2 bug */
2106 if (gcry_md_open (&c
->mfx
.md2
, DIGEST_ALGO_MD5
, 0))
2109 else if( sig
->digest_algo
== DIGEST_ALGO_SHA1
2110 && sig
->pubkey_algo
== PUBKEY_ALGO_DSA
2111 && sig
->sig_class
== 0x01 ) {
2112 /* enable the workaround also for pgp5 when the detached
2113 * signature has been created in textmode */
2114 if (gcry_md_open (&c
->mfx
.md2
, sig
->digest_algo
, 0 ))
2117 #if 0 /* workaround disabled */
2118 /* Here we have another hack to work around a pgp 2 bug
2119 * It works by not using the textmode for detached signatures;
2120 * this will let the first signature check (on md) fail
2121 * but the second one (on md2) which adds an extra CR should
2122 * then produce the "correct" hash. This is very, very ugly
2123 * hack but it may help in some cases (and break others)
2125 /* c->mfx.md2? 0 :(sig->sig_class == 0x01) */
2127 if ( DBG_HASHING
) {
2128 gcry_md_start_debug( c
->mfx
.md
, "verify" );
2130 gcry_md_start_debug( c
->mfx
.md2
, "verify2" );
2132 if( c
->sigs_only
) {
2133 if (c
->signed_data
.used
&& c
->signed_data
.data_fd
!= -1)
2134 rc
= hash_datafile_by_fd (c
->mfx
.md
, c
->mfx
.md2
,
2135 c
->signed_data
.data_fd
,
2136 (sig
->sig_class
== 0x01));
2138 rc
= hash_datafiles (c
->mfx
.md
, c
->mfx
.md2
,
2139 c
->signed_data
.data_names
,
2141 (sig
->sig_class
== 0x01));
2144 rc
= ask_for_detached_datafile( c
->mfx
.md
, c
->mfx
.md2
,
2145 iobuf_get_real_fname(c
->iobuf
),
2146 (sig
->sig_class
== 0x01) );
2149 log_error("can't hash datafile: %s\n", g10_errstr(rc
));
2153 else if ( c
->signed_data
.used
) {
2154 log_error (_("not a detached signature\n") );
2157 else if (!opt
.quiet
)
2158 log_info(_("old style (PGP 2.x) signature\n"));
2161 for( n1
= node
; n1
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)) )
2162 check_sig_and_print( c
, n1
);
2164 check_sig_and_print( c
, node
);
2167 dump_kbnode (c
->list
);
2168 log_error(_("invalid root packet detected in proc_tree()\n"));