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 3 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, see <http://www.gnu.org/licenses/>.
40 #include "keyserver-internal.h"
46 struct kidlist_item
*next
;
54 * Structure to hold the context
56 typedef struct mainproc_context
*CTX
;
57 struct mainproc_context
59 struct mainproc_context
*anchor
; /* May be useful in the future. */
60 PKT_public_key
*last_pubkey
;
61 PKT_secret_key
*last_seckey
;
62 PKT_user_id
*last_user_id
;
63 md_filter_context_t mfx
;
64 int sigs_only
; /* Process only signatures and reject all other stuff. */
65 int encrypt_only
; /* Process only encryption messages. */
67 /* Name of the file with the complete signature or the file with the
68 detached signature. This is currently only used to deduce the
69 file name of the data file if that has not been given. */
70 const char *sigfilename
;
72 /* A structure to describe the signed data in case of a detached
76 /* A file descriptor of the the signed data. Only used if not -1. */
78 /* A list of filenames with the data files or NULL. This is only
79 used if DATA_FD is -1. */
81 /* Flag to indicated that either one of the next previous fieldss
82 is used. This is only needed for better readability. */
87 int last_was_session_key
;
88 KBNODE list
; /* The current list of packets. */
90 IOBUF iobuf
; /* Used to get the filename etc. */
91 int trustletter
; /* Temporary usage in list_node. */
93 struct kidlist_item
*pkenc_list
; /* List of encryption packets. */
94 int any_sig_seen
; /* Set to true if a signature packet has been seen. */
98 static int do_proc_packets( CTX c
, IOBUF a
);
99 static void list_node( CTX c
, KBNODE node
);
100 static void proc_tree( CTX c
, KBNODE node
);
101 static int literals_seen
;
104 reset_literals_seen(void)
110 release_list( CTX c
)
114 proc_tree(c
, c
->list
);
115 release_kbnode( c
->list
);
116 while( c
->pkenc_list
) {
117 struct kidlist_item
*tmp
= c
->pkenc_list
->next
;
118 xfree( c
->pkenc_list
);
121 c
->pkenc_list
= NULL
;
124 c
->last_was_session_key
= 0;
125 xfree(c
->dek
); c
->dek
= NULL
;
130 add_onepass_sig( CTX c
, PACKET
*pkt
)
134 if ( c
->list
) /* add another packet */
135 add_kbnode( c
->list
, new_kbnode( pkt
));
136 else /* insert the first one */
137 c
->list
= node
= new_kbnode( pkt
);
144 add_gpg_control( CTX c
, PACKET
*pkt
)
146 if ( pkt
->pkt
.gpg_control
->control
== CTRLPKT_CLEARSIGN_START
) {
147 /* New clear text signature.
148 * Process the last one and reset everything */
152 if( c
->list
) /* add another packet */
153 add_kbnode( c
->list
, new_kbnode( pkt
));
154 else /* insert the first one */
155 c
->list
= new_kbnode( pkt
);
163 add_user_id( CTX c
, PACKET
*pkt
)
166 log_error("orphaned user ID\n" );
169 add_kbnode( c
->list
, new_kbnode( pkt
) );
174 add_subkey( CTX c
, PACKET
*pkt
)
177 log_error("subkey w/o mainkey\n" );
180 add_kbnode( c
->list
, new_kbnode( pkt
) );
185 add_ring_trust( CTX c
, PACKET
*pkt
)
188 log_error("ring trust w/o key\n" );
191 add_kbnode( c
->list
, new_kbnode( pkt
) );
197 add_signature( CTX c
, PACKET
*pkt
)
202 if( pkt
->pkttype
== PKT_SIGNATURE
&& !c
->list
) {
203 /* This is the first signature for the following datafile.
204 * GPG does not write such packets; instead it always uses
205 * onepass-sig packets. The drawback of PGP's method
206 * of prepending the signature to the data is
207 * that it is not possible to make a signature from data read
208 * from stdin. (GPG is able to read PGP stuff anyway.) */
209 node
= new_kbnode( pkt
);
214 return 0; /* oops (invalid packet sequence)*/
215 else if( !c
->list
->pkt
)
216 BUG(); /* so nicht */
218 /* add a new signature node id at the end */
219 node
= new_kbnode( pkt
);
220 add_kbnode( c
->list
, node
);
225 symkey_decrypt_seskey( DEK
*dek
, byte
*seskey
, size_t slen
)
229 if(slen
< 17 || slen
> 33)
231 log_error ( _("weird size for an encrypted session key (%d)\n"),
233 return G10ERR_BAD_KEY
;
236 if (gcry_cipher_open (&hd
, dek
->algo
, GCRY_CIPHER_MODE_CFB
, 1))
238 if (gcry_cipher_setkey ( hd
, dek
->key
, dek
->keylen
))
240 gcry_cipher_setiv ( hd
, NULL
, 0 );
241 gcry_cipher_decrypt ( hd
, seskey
, slen
, NULL
, 0 );
242 gcry_cipher_close ( hd
);
244 /* Now we replace the dek components with the real session key to
245 decrypt the contents of the sequencing packet. */
250 if(dek
->keylen
> DIM(dek
->key
))
253 /* This is not completely accurate, since a bad passphrase may have
254 resulted in a garbage algorithm byte, but it's close enough since
255 a bogus byte here will fail later. */
256 if(dek
->algo
==CIPHER_ALGO_IDEA
)
259 memcpy(dek
->key
, seskey
+ 1, dek
->keylen
);
261 /*log_hexdump( "thekey", dek->key, dek->keylen );*/
267 proc_symkey_enc( CTX c
, PACKET
*pkt
)
271 enc
= pkt
->pkt
.symkey_enc
;
273 log_error ("invalid symkey encrypted packet\n");
276 int algo
= enc
->cipher_algo
;
277 const char *s
= openpgp_cipher_algo_name (algo
);
279 if (!openpgp_cipher_test_algo (algo
))
284 log_info(_("%s encrypted session key\n"), s
);
286 log_info(_("%s encrypted data\n"), s
);
290 log_error(_("encrypted with unknown algorithm %d\n"), algo
);
292 if(openpgp_md_test_algo (enc
->s2k
.hash_algo
))
294 log_error(_("passphrase generated with unknown digest"
295 " algorithm %d\n"),enc
->s2k
.hash_algo
);
299 c
->last_was_session_key
= 2;
300 if(!s
|| opt
.list_only
)
303 if(opt
.override_session_key
)
305 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
306 if(get_override_session_key(c
->dek
, opt
.override_session_key
))
314 c
->dek
= passphrase_to_dek (NULL
, 0, algo
, &enc
->s2k
, 0,
320 /* FIXME: This doesn't work perfectly if a symmetric
321 key comes before a public key in the message - if
322 the user doesn't know the passphrase, then there is
323 a chance that the "decrypted" algorithm will happen
324 to be a valid one, which will make the returned dek
325 appear valid, so we won't try any public keys that
329 if(symkey_decrypt_seskey(c
->dek
, enc
->seskey
,
337 c
->dek
->algo_info_printed
= 1;
348 proc_pubkey_enc( CTX c
, PACKET
*pkt
)
353 /* check whether the secret key is available and store in this case */
354 c
->last_was_session_key
= 1;
355 enc
= pkt
->pkt
.pubkey_enc
;
356 /*printf("enc: encrypted by a pubkey with keyid %08lX\n", enc->keyid[1] );*/
357 /* Hmmm: why do I have this algo check here - anyway there is
358 * function to check it. */
360 log_info(_("public key is %s\n"), keystr(enc
->keyid
) );
362 if( is_status_enabled() ) {
364 sprintf(buf
, "%08lX%08lX %d 0",
365 (ulong
)enc
->keyid
[0], (ulong
)enc
->keyid
[1], enc
->pubkey_algo
);
366 write_status_text( STATUS_ENC_TO
, buf
);
369 if( !opt
.list_only
&& opt
.override_session_key
) {
370 /* It does not make much sense to store the session key in
371 * secure memory because it has already been passed on the
372 * command line and the GCHQ knows about it. */
373 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
374 result
= get_override_session_key ( c
->dek
, opt
.override_session_key
);
376 xfree(c
->dek
); c
->dek
= NULL
;
379 else if( is_ELGAMAL(enc
->pubkey_algo
)
380 || enc
->pubkey_algo
== PUBKEY_ALGO_DSA
381 || is_RSA(enc
->pubkey_algo
)
382 || enc
->pubkey_algo
== PUBKEY_ALGO_ELGAMAL
) {
383 /* Note that we also allow type 20 Elgamal keys for decryption.
384 There are still a couple of those keys in active use as a
387 /* FIXME: Store this all in a list and process it later so that
388 we can prioritize what key to use. This gives a better user
389 experience if wildcard keyids are used. */
390 if ( !c
->dek
&& ((!enc
->keyid
[0] && !enc
->keyid
[1])
391 || opt
.try_all_secrets
392 || !seckey_available( enc
->keyid
)) ) {
396 c
->dek
= xmalloc_secure_clear( sizeof *c
->dek
);
397 if( (result
= get_session_key( enc
, c
->dek
)) ) {
398 /* error: delete the DEK */
399 xfree(c
->dek
); c
->dek
= NULL
;
404 result
= G10ERR_NO_SECKEY
;
407 result
= G10ERR_PUBKEY_ALGO
;
413 /* store it for later display */
414 struct kidlist_item
*x
= xmalloc( sizeof *x
);
415 x
->kid
[0] = enc
->keyid
[0];
416 x
->kid
[1] = enc
->keyid
[1];
417 x
->pubkey_algo
= enc
->pubkey_algo
;
419 x
->next
= c
->pkenc_list
;
422 if( !result
&& opt
.verbose
> 1 )
423 log_info( _("public key encrypted data: good DEK\n") );
432 * Print the list of public key encrypted packets which we could
436 print_pkenc_list( struct kidlist_item
*list
, int failed
)
438 for( ; list
; list
= list
->next
) {
442 if ( failed
&& !list
->reason
)
444 if ( !failed
&& list
->reason
)
447 algstr
= gcry_pk_algo_name ( list
->pubkey_algo
);
448 pk
= xmalloc_clear( sizeof *pk
);
452 pk
->pubkey_algo
= list
->pubkey_algo
;
453 if( !get_pubkey( pk
, list
->kid
) )
456 log_info( _("encrypted with %u-bit %s key, ID %s, created %s\n"),
457 nbits_from_pk( pk
), algstr
, keystr_from_pk(pk
),
458 strtimestamp(pk
->timestamp
) );
459 p
=get_user_id_native(list
->kid
);
460 log_printf (_(" \"%s\"\n"),p
);
464 log_info(_("encrypted with %s key, ID %s\n"),
465 algstr
,keystr(list
->kid
));
467 free_public_key( pk
);
469 if( list
->reason
== G10ERR_NO_SECKEY
) {
470 if( is_status_enabled() ) {
472 sprintf(buf
,"%08lX%08lX", (ulong
)list
->kid
[0],
473 (ulong
)list
->kid
[1] );
474 write_status_text( STATUS_NO_SECKEY
, buf
);
477 else if (list
->reason
)
478 log_info(_("public key decryption failed: %s\n"),
479 g10_errstr(list
->reason
));
485 proc_encrypted( CTX c
, PACKET
*pkt
)
492 log_info(_("encrypted with %lu passphrases\n"),c
->symkeys
);
493 else if(c
->symkeys
==1)
494 log_info(_("encrypted with 1 passphrase\n"));
495 print_pkenc_list ( c
->pkenc_list
, 1 );
496 print_pkenc_list ( c
->pkenc_list
, 0 );
499 /* FIXME: Figure out the session key by looking at all pkenc packets. */
502 write_status( STATUS_BEGIN_DECRYPTION
);
504 /*log_debug("dat: %sencrypted data\n", c->dek?"":"conventional ");*/
507 else if( !c
->dek
&& !c
->last_was_session_key
) {
509 STRING2KEY s2kbuf
, *s2k
= NULL
;
511 if(opt
.override_session_key
)
513 c
->dek
= xmalloc_clear( sizeof *c
->dek
);
514 result
=get_override_session_key(c
->dek
, opt
.override_session_key
);
523 /* Assume this is old style conventional encrypted data. */
524 algo
= opt
.def_cipher_algo
;
526 log_info (_("assuming %s encrypted data\n"),
527 openpgp_cipher_algo_name (algo
));
528 else if ( openpgp_cipher_test_algo (CIPHER_ALGO_IDEA
) )
530 algo
= opt
.def_cipher_algo
;
532 algo
= opt
.s2k_cipher_algo
;
534 log_info (_("IDEA cipher unavailable, "
535 "optimistically attempting to use %s instead\n"),
536 openpgp_cipher_algo_name (algo
));
540 algo
= CIPHER_ALGO_IDEA
;
541 if (!opt
.s2k_digest_algo
)
543 /* If no digest is given we assume MD5 */
545 s2kbuf
.hash_algo
= DIGEST_ALGO_MD5
;
548 log_info (_("assuming %s encrypted data\n"), "IDEA");
551 c
->dek
= passphrase_to_dek ( NULL
, 0, algo
, s2k
, 0, NULL
, NULL
);
553 c
->dek
->algo_info_printed
= 1;
557 result
= G10ERR_NO_SECKEY
;
559 result
= decrypt_data( c
, pkt
->pkt
.encrypted
, c
->dek
);
563 else if( !result
|| (gpg_err_code (result
) == GPG_ERR_BAD_SIGNATURE
564 && opt
.ignore_mdc_error
)) {
565 write_status( STATUS_DECRYPTION_OKAY
);
566 if( opt
.verbose
> 1 )
567 log_info(_("decryption okay\n"));
568 if( pkt
->pkt
.encrypted
->mdc_method
&& !result
)
569 write_status( STATUS_GOODMDC
);
570 else if(!opt
.no_mdc_warn
)
571 log_info (_("WARNING: message was not integrity protected\n"));
572 if(opt
.show_session_key
)
575 char *buf
= xmalloc ( c
->dek
->keylen
*2 + 20 );
576 sprintf ( buf
, "%d:", c
->dek
->algo
);
577 for(i
=0; i
< c
->dek
->keylen
; i
++ )
578 sprintf(buf
+strlen(buf
), "%02X", c
->dek
->key
[i
] );
579 log_info( "session key: `%s'\n", buf
);
580 write_status_text ( STATUS_SESSION_KEY
, buf
);
583 else if( result
== G10ERR_BAD_SIGN
) {
584 log_error(_("WARNING: encrypted message has been manipulated!\n"));
585 write_status( STATUS_BADMDC
);
586 write_status( STATUS_DECRYPTION_FAILED
);
589 write_status( STATUS_DECRYPTION_FAILED
);
590 log_error(_("decryption failed: %s\n"), g10_errstr(result
));
591 /* Hmmm: does this work when we have encrypted using multiple
592 * ways to specify the session key (symmmetric and PK)*/
594 xfree(c
->dek
); c
->dek
= NULL
;
596 c
->last_was_session_key
= 0;
597 write_status( STATUS_END_DECRYPTION
);
602 proc_plaintext( CTX c
, PACKET
*pkt
)
604 PKT_plaintext
*pt
= pkt
->pkt
.plaintext
;
605 int any
, clearsig
, only_md5
, rc
;
610 if( pt
->namelen
== 8 && !memcmp( pt
->name
, "_CONSOLE", 8 ) )
611 log_info(_("NOTE: sender requested \"for-your-eyes-only\"\n"));
612 else if( opt
.verbose
)
613 log_info(_("original file name='%.*s'\n"), pt
->namelen
, pt
->name
);
614 free_md_filter_context( &c
->mfx
);
615 if (gcry_md_open (&c
->mfx
.md
, 0, 0))
617 /* fixme: we may need to push the textfilter if we have sigclass 1
618 * and no armoring - Not yet tested
619 * Hmmm, why don't we need it at all if we have sigclass 1
620 * Should we assume that plaintext in mode 't' has always sigclass 1??
621 * See: Russ Allbery's mail 1999-02-09
623 any
= clearsig
= only_md5
= 0;
624 for(n
=c
->list
; n
; n
= n
->next
)
626 if( n
->pkt
->pkttype
== PKT_ONEPASS_SIG
)
628 /* For the onepass signature case */
629 if( n
->pkt
->pkt
.onepass_sig
->digest_algo
)
631 gcry_md_enable (c
->mfx
.md
,
632 n
->pkt
->pkt
.onepass_sig
->digest_algo
);
633 if( !any
&& n
->pkt
->pkt
.onepass_sig
->digest_algo
640 if( n
->pkt
->pkt
.onepass_sig
->sig_class
!= 0x01 )
643 else if( n
->pkt
->pkttype
== PKT_GPG_CONTROL
644 && n
->pkt
->pkt
.gpg_control
->control
645 == CTRLPKT_CLEARSIGN_START
)
647 /* For the clearsigned message case */
648 size_t datalen
= n
->pkt
->pkt
.gpg_control
->datalen
;
649 const byte
*data
= n
->pkt
->pkt
.gpg_control
->data
;
651 /* check that we have at least the sigclass and one hash */
653 log_fatal("invalid control packet CTRLPKT_CLEARSIGN_START\n");
654 /* Note that we don't set the clearsig flag for not-dash-escaped
656 clearsig
= (*data
== 0x01);
657 for( data
++, datalen
--; datalen
; datalen
--, data
++ )
658 gcry_md_enable (c
->mfx
.md
, *data
);
660 break; /* Stop here as one-pass signature packets are not
663 else if(n
->pkt
->pkttype
==PKT_SIGNATURE
)
665 /* For the SIG+LITERAL case that PGP used to use. */
666 gcry_md_enable ( c
->mfx
.md
, n
->pkt
->pkt
.signature
->digest_algo
);
671 if( !any
&& !opt
.skip_verify
)
673 /* This is for the old GPG LITERAL+SIG case. It's not legal
674 according to 2440, so hopefully it won't come up that
675 often. There is no good way to specify what algorithms to
676 use in that case, so these three are the historical
678 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_RMD160
);
679 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_SHA1
);
680 gcry_md_enable( c
->mfx
.md
, DIGEST_ALGO_MD5
);
682 if( opt
.pgp2_workarounds
&& only_md5
&& !opt
.skip_verify
) {
683 /* This is a kludge to work around a bug in pgp2. It does only
684 * catch those mails which are armored. To catch the non-armored
685 * pgp mails we could see whether there is the signature packet
686 * in front of the plaintext. If someone needs this, send me a patch.
688 if ( gcry_md_open (&c
->mfx
.md2
, DIGEST_ALGO_MD5
, 0) )
692 gcry_md_start_debug ( c
->mfx
.md
, "verify" );
694 gcry_md_start_debug ( c
->mfx
.md2
, "verify2" );
701 log_info (_("WARNING: multiple plaintexts seen\n"));
703 if (!opt
.flags
.allow_multiple_messages
)
705 write_status_text (STATUS_ERROR
, "proc_pkt.plaintext 89_BAD_DATA");
706 log_inc_errorcount ();
707 rc
= gpg_error (GPG_ERR_UNEXPECTED
);
713 rc
= handle_plaintext( pt
, &c
->mfx
, c
->sigs_only
, clearsig
);
714 if ( gpg_err_code (rc
) == GPG_ERR_EACCES
&& !c
->sigs_only
)
716 /* Can't write output but we hash it anyway to check the
718 rc
= handle_plaintext( pt
, &c
->mfx
, 1, clearsig
);
723 log_error( "handle plaintext failed: %s\n", g10_errstr(rc
));
725 c
->last_was_session_key
= 0;
727 /* We add a marker control packet instead of the plaintext packet.
728 * This is so that we can later detect invalid packet sequences.
730 n
= new_kbnode (create_gpg_control (CTRLPKT_PLAINTEXT_MARK
, NULL
, 0));
732 add_kbnode (c
->list
, n
);
739 proc_compressed_cb( IOBUF a
, void *info
)
741 if ( ((CTX
)info
)->signed_data
.used
742 && ((CTX
)info
)->signed_data
.data_fd
!= -1)
743 return proc_signature_packets_by_fd (info
, a
,
744 ((CTX
)info
)->signed_data
.data_fd
);
746 return proc_signature_packets (info
, a
,
747 ((CTX
)info
)->signed_data
.data_names
,
748 ((CTX
)info
)->sigfilename
);
752 proc_encrypt_cb( IOBUF a
, void *info
)
754 return proc_encryption_packets( info
, a
);
758 proc_compressed( CTX c
, PACKET
*pkt
)
760 PKT_compressed
*zd
= pkt
->pkt
.compressed
;
763 /*printf("zip: compressed data packet\n");*/
765 rc
=G10ERR_COMPR_ALGO
;
766 else if( c
->sigs_only
)
767 rc
= handle_compressed( c
, zd
, proc_compressed_cb
, c
);
768 else if( c
->encrypt_only
)
769 rc
= handle_compressed( c
, zd
, proc_encrypt_cb
, c
);
771 rc
= handle_compressed( c
, zd
, NULL
, NULL
);
773 log_error("uncompressing failed: %s\n", g10_errstr(rc
));
775 c
->last_was_session_key
= 0;
779 * check the signature
780 * Returns: 0 = valid signature or an error code
783 do_check_sig( CTX c
, KBNODE node
, int *is_selfsig
,
784 int *is_expkey
, int *is_revkey
)
787 gcry_md_hd_t md
= NULL
, md2
= NULL
;
790 assert( node
->pkt
->pkttype
== PKT_SIGNATURE
);
793 sig
= node
->pkt
->pkt
.signature
;
795 algo
= sig
->digest_algo
;
796 rc
= openpgp_md_test_algo(algo
);
800 if( sig
->sig_class
== 0x00 ) {
803 if (gcry_md_copy (&md
, c
->mfx
.md
))
806 else /* detached signature */
808 /* signature_check() will enable the md*/
809 if (gcry_md_open (&md
, 0, 0 ))
813 else if( sig
->sig_class
== 0x01 ) {
814 /* how do we know that we have to hash the (already hashed) text
815 * in canonical mode ??? (calculating both modes???) */
817 if (gcry_md_copy (&md
, c
->mfx
.md
))
819 if( c
->mfx
.md2
&& gcry_md_copy (&md2
, c
->mfx
.md2
))
822 else { /* detached signature */
823 log_debug("Do we really need this here?");
824 /* signature_check() will enable the md*/
825 if (gcry_md_open (&md
, 0, 0 ))
827 if (gcry_md_open (&md2
, 0, 0 ))
831 else if( (sig
->sig_class
&~3) == 0x10
832 || sig
->sig_class
== 0x18
833 || sig
->sig_class
== 0x1f
834 || sig
->sig_class
== 0x20
835 || sig
->sig_class
== 0x28
836 || sig
->sig_class
== 0x30 ) {
837 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
838 || c
->list
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
839 return check_key_signature( c
->list
, node
, is_selfsig
);
841 else if( sig
->sig_class
== 0x20 ) {
842 log_error (_("standalone revocation - "
843 "use \"gpg --import\" to apply\n"));
844 return G10ERR_NOT_PROCESSED
;
847 log_error("invalid root packet for sigclass %02x\n",
849 return G10ERR_SIG_CLASS
;
853 return G10ERR_SIG_CLASS
;
854 rc
= signature_check2( sig
, md
, NULL
, is_expkey
, is_revkey
, NULL
);
855 if( gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
&& md2
)
856 rc
= signature_check2( sig
, md2
, NULL
, is_expkey
, is_revkey
, NULL
);
865 print_userid( PACKET
*pkt
)
869 if( pkt
->pkttype
!= PKT_USER_ID
) {
870 printf("ERROR: unexpected packet type %d", pkt
->pkttype
);
873 if( opt
.with_colons
)
875 if(pkt
->pkt
.user_id
->attrib_data
)
877 pkt
->pkt
.user_id
->numattribs
,
878 pkt
->pkt
.user_id
->attrib_len
);
880 print_string( stdout
, pkt
->pkt
.user_id
->name
,
881 pkt
->pkt
.user_id
->len
, ':');
884 print_utf8_string( stdout
, pkt
->pkt
.user_id
->name
,
885 pkt
->pkt
.user_id
->len
);
890 * List the certificate in a user friendly way
894 list_node( CTX c
, KBNODE node
)
901 else if( (mainkey
= (node
->pkt
->pkttype
== PKT_PUBLIC_KEY
) )
902 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
903 PKT_public_key
*pk
= node
->pkt
->pkt
.public_key
;
905 if( opt
.with_colons
)
908 keyid_from_pk( pk
, keyid
);
910 c
->trustletter
= opt
.fast_list_mode
?
911 0 : get_validity_info( pk
, NULL
);
912 printf("%s:", mainkey
? "pub":"sub" );
914 putchar( c
->trustletter
);
915 printf(":%u:%d:%08lX%08lX:%s:%s::",
918 (ulong
)keyid
[0],(ulong
)keyid
[1],
919 colon_datestr_from_pk( pk
),
920 colon_strtime (pk
->expiredate
) );
921 if( mainkey
&& !opt
.fast_list_mode
)
922 putchar( get_ownertrust_info (pk
) );
924 if( node
->next
&& node
->next
->pkt
->pkttype
== PKT_RING_TRUST
) {
925 putchar('\n'); any
=1;
926 if( opt
.fingerprint
)
927 print_fingerprint( pk
, NULL
, 0 );
928 printf("rtv:1:%u:\n",
929 node
->next
->pkt
->pkt
.ring_trust
->trustval
);
933 printf("%s %4u%c/%s %s%s",
934 mainkey
? "pub":"sub", nbits_from_pk( pk
),
935 pubkey_letter( pk
->pubkey_algo
), keystr_from_pk( pk
),
936 datestr_from_pk( pk
), mainkey
?" ":"");
939 /* and now list all userids with their signatures */
940 for( node
= node
->next
; node
; node
= node
->next
) {
941 if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
943 if( node
->pkt
->pkt
.signature
->sig_class
== 0x20 )
951 else if( node
->pkt
->pkttype
== PKT_USER_ID
) {
953 if( opt
.with_colons
)
954 printf("%s:::::::::",
955 node
->pkt
->pkt
.user_id
->attrib_data
?"uat":"uid");
957 printf( "uid%*s", 28, "" );
959 print_userid( node
->pkt
);
960 if( opt
.with_colons
)
963 if( opt
.fingerprint
&& !any
)
964 print_fingerprint( pk
, NULL
, 0 );
967 && node
->next
->pkt
->pkttype
== PKT_RING_TRUST
) {
968 printf("rtv:2:%u:\n",
969 node
->next
->pkt
->pkt
.ring_trust
?
970 node
->next
->pkt
->pkt
.ring_trust
->trustval
: 0);
974 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
989 printf(_("revoked: %s"),revokestr_from_pk(pk
));
992 else if( pk
->expiredate
)
995 printf(_("expires: %s"),expirestr_from_pk(pk
));
1002 if( !mainkey
&& opt
.fingerprint
> 1 )
1003 print_fingerprint( pk
, NULL
, 0 );
1005 else if( (mainkey
= (node
->pkt
->pkttype
== PKT_SECRET_KEY
) )
1006 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1007 PKT_secret_key
*sk
= node
->pkt
->pkt
.secret_key
;
1009 if( opt
.with_colons
)
1012 keyid_from_sk( sk
, keyid
);
1013 printf("%s::%u:%d:%08lX%08lX:%s:%s:::",
1014 mainkey
? "sec":"ssb",
1015 nbits_from_sk( sk
),
1017 (ulong
)keyid
[0],(ulong
)keyid
[1],
1018 colon_datestr_from_sk( sk
),
1019 colon_strtime (sk
->expiredate
)
1020 /* fixme: add LID */ );
1023 printf("%s %4u%c/%s %s ", mainkey
? "sec":"ssb",
1024 nbits_from_sk( sk
), pubkey_letter( sk
->pubkey_algo
),
1025 keystr_from_sk( sk
), datestr_from_sk( sk
));
1027 /* and now list all userids with their signatures */
1028 for( node
= node
->next
; node
; node
= node
->next
) {
1029 if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1031 if( node
->pkt
->pkt
.signature
->sig_class
== 0x20 )
1037 list_node(c
, node
);
1039 else if( node
->pkt
->pkttype
== PKT_USER_ID
) {
1041 if( opt
.with_colons
)
1042 printf("%s:::::::::",
1043 node
->pkt
->pkt
.user_id
->attrib_data
?"uat":"uid");
1045 printf( "uid%*s", 28, "" );
1047 print_userid( node
->pkt
);
1048 if( opt
.with_colons
)
1051 if( opt
.fingerprint
&& !any
)
1052 print_fingerprint( NULL
, sk
, 0 );
1055 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1060 list_node(c
, node
);
1066 if( !mainkey
&& opt
.fingerprint
> 1 )
1067 print_fingerprint( NULL
, sk
, 0 );
1069 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1070 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1080 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x30 )
1081 fputs("rev", stdout
);
1083 fputs("sig", stdout
);
1084 if( opt
.check_sigs
) {
1086 rc2
=do_check_sig( c
, node
, &is_selfsig
, NULL
, NULL
);
1087 switch (gpg_err_code (rc2
)) {
1088 case 0: sigrc
= '!'; break;
1089 case GPG_ERR_BAD_SIGNATURE
: sigrc
= '-'; break;
1090 case GPG_ERR_NO_PUBKEY
:
1091 case GPG_ERR_UNUSABLE_PUBKEY
: sigrc
= '?'; break;
1092 default: sigrc
= '%'; break;
1095 else { /* check whether this is a self signature */
1098 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
1099 || c
->list
->pkt
->pkttype
== PKT_SECRET_KEY
) {
1100 if( c
->list
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
1101 keyid_from_pk( c
->list
->pkt
->pkt
.public_key
, keyid
);
1103 keyid_from_sk( c
->list
->pkt
->pkt
.secret_key
, keyid
);
1105 if( keyid
[0] == sig
->keyid
[0] && keyid
[1] == sig
->keyid
[1] )
1109 if( opt
.with_colons
) {
1113 printf("::%d:%08lX%08lX:%s:%s:", sig
->pubkey_algo
,
1114 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1115 colon_datestr_from_sig(sig
),
1116 colon_expirestr_from_sig(sig
));
1118 if(sig
->trust_depth
|| sig
->trust_value
)
1119 printf("%d %d",sig
->trust_depth
,sig
->trust_value
);
1122 if(sig
->trust_regexp
)
1123 print_string(stdout
,sig
->trust_regexp
,
1124 strlen(sig
->trust_regexp
),':');
1129 sigrc
, keystr(sig
->keyid
), datestr_from_sig(sig
));
1131 printf("[%s] ", g10_errstr(rc2
) );
1132 else if( sigrc
== '?' )
1134 else if( is_selfsig
) {
1135 if( opt
.with_colons
)
1137 fputs( sig
->sig_class
== 0x18? "[keybind]":"[selfsig]", stdout
);
1138 if( opt
.with_colons
)
1141 else if( !opt
.fast_list_mode
) {
1142 p
= get_user_id( sig
->keyid
, &n
);
1143 print_string( stdout
, p
, n
, opt
.with_colons
);
1146 if( opt
.with_colons
)
1147 printf(":%02x%c:", sig
->sig_class
, sig
->flags
.exportable
?'x':'l');
1151 log_error("invalid node with packet of type %d\n", node
->pkt
->pkttype
);
1157 proc_packets( void *anchor
, IOBUF a
)
1160 CTX c
= xmalloc_clear( sizeof *c
);
1163 rc
= do_proc_packets( c
, a
);
1171 proc_signature_packets( void *anchor
, IOBUF a
,
1172 strlist_t signedfiles
, const char *sigfilename
)
1174 CTX c
= xmalloc_clear( sizeof *c
);
1180 c
->signed_data
.data_fd
= -1;
1181 c
->signed_data
.data_names
= signedfiles
;
1182 c
->signed_data
.used
= !!signedfiles
;
1184 c
->sigfilename
= sigfilename
;
1185 rc
= do_proc_packets( c
, a
);
1187 /* If we have not encountered any signature we print an error
1188 messages, send a NODATA status back and return an error code.
1189 Using log_error is required because verify_files does not check
1190 error codes for each file but we want to terminate the process
1192 if (!rc
&& !c
->any_sig_seen
)
1194 write_status_text (STATUS_NODATA
, "4");
1195 log_error (_("no signature found\n"));
1196 rc
= G10ERR_NO_DATA
;
1199 /* Propagate the signature seen flag upward. Do this only on
1200 success so that we won't issue the nodata status several
1202 if (!rc
&& c
->anchor
&& c
->any_sig_seen
)
1203 c
->anchor
->any_sig_seen
= 1;
1210 proc_signature_packets_by_fd (void *anchor
, IOBUF a
, int signed_data_fd
)
1213 CTX c
= xcalloc (1, sizeof *c
);
1218 c
->signed_data
.data_fd
= signed_data_fd
;
1219 c
->signed_data
.data_names
= NULL
;
1220 c
->signed_data
.used
= (signed_data_fd
!= -1);
1222 rc
= do_proc_packets ( c
, a
);
1224 /* If we have not encountered any signature we print an error
1225 messages, send a NODATA status back and return an error code.
1226 Using log_error is required because verify_files does not check
1227 error codes for each file but we want to terminate the process
1229 if (!rc
&& !c
->any_sig_seen
)
1231 write_status_text (STATUS_NODATA
, "4");
1232 log_error (_("no signature found\n"));
1233 rc
= gpg_error (GPG_ERR_NO_DATA
);
1236 /* Propagate the signature seen flag upward. Do this only on success
1237 so that we won't issue the nodata status several times. */
1238 if (!rc
&& c
->anchor
&& c
->any_sig_seen
)
1239 c
->anchor
->any_sig_seen
= 1;
1247 proc_encryption_packets( void *anchor
, IOBUF a
)
1249 CTX c
= xmalloc_clear( sizeof *c
);
1253 c
->encrypt_only
= 1;
1254 rc
= do_proc_packets( c
, a
);
1261 do_proc_packets( CTX c
, IOBUF a
)
1263 PACKET
*pkt
= xmalloc( sizeof *pkt
);
1270 while( (rc
=parse_packet(a
, pkt
)) != -1 ) {
1274 /* stop processing when an invalid packet has been encountered
1275 * but don't do so when we are doing a --list-packets. */
1276 if (gpg_err_code (rc
) == GPG_ERR_INV_PACKET
1277 && opt
.list_packets
!= 2 )
1282 if( opt
.list_packets
) {
1283 switch( pkt
->pkttype
) {
1284 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1285 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1287 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1288 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1289 default: newpkt
= 0; break;
1292 else if( c
->sigs_only
) {
1293 switch( pkt
->pkttype
) {
1294 case PKT_PUBLIC_KEY
:
1295 case PKT_SECRET_KEY
:
1297 case PKT_SYMKEY_ENC
:
1298 case PKT_PUBKEY_ENC
:
1300 case PKT_ENCRYPTED_MDC
:
1301 write_status_text( STATUS_UNEXPECTED
, "0" );
1302 rc
= G10ERR_UNEXPECTED
;
1304 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1305 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1306 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1307 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1308 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1309 default: newpkt
= 0; break;
1312 else if( c
->encrypt_only
) {
1313 switch( pkt
->pkttype
) {
1314 case PKT_PUBLIC_KEY
:
1315 case PKT_SECRET_KEY
:
1317 write_status_text( STATUS_UNEXPECTED
, "0" );
1318 rc
= G10ERR_UNEXPECTED
;
1320 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1321 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1322 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1324 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1325 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1326 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1327 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1328 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1329 default: newpkt
= 0; break;
1333 switch( pkt
->pkttype
) {
1334 case PKT_PUBLIC_KEY
:
1335 case PKT_SECRET_KEY
:
1337 c
->list
= new_kbnode( pkt
);
1340 case PKT_PUBLIC_SUBKEY
:
1341 case PKT_SECRET_SUBKEY
:
1342 newpkt
= add_subkey( c
, pkt
);
1344 case PKT_USER_ID
: newpkt
= add_user_id( c
, pkt
); break;
1345 case PKT_SIGNATURE
: newpkt
= add_signature( c
, pkt
); break;
1346 case PKT_PUBKEY_ENC
: proc_pubkey_enc( c
, pkt
); break;
1347 case PKT_SYMKEY_ENC
: proc_symkey_enc( c
, pkt
); break;
1349 case PKT_ENCRYPTED_MDC
: proc_encrypted( c
, pkt
); break;
1350 case PKT_PLAINTEXT
: proc_plaintext( c
, pkt
); break;
1351 case PKT_COMPRESSED
: proc_compressed( c
, pkt
); break;
1352 case PKT_ONEPASS_SIG
: newpkt
= add_onepass_sig( c
, pkt
); break;
1353 case PKT_GPG_CONTROL
: newpkt
= add_gpg_control(c
, pkt
); break;
1354 case PKT_RING_TRUST
: newpkt
= add_ring_trust( c
, pkt
); break;
1355 default: newpkt
= 0; break;
1358 /* This is a very ugly construct and frankly, I don't remember why
1359 * I used it. Adding the MDC check here is a hack.
1360 * The right solution is to initiate another context for encrypted
1361 * packet and not to reuse the current one ... It works right
1362 * when there is a compression packet inbetween which adds just
1364 * Hmmm: Rewrite this whole module here??
1366 if( pkt
->pkttype
!= PKT_SIGNATURE
&& pkt
->pkttype
!= PKT_MDC
)
1367 c
->have_data
= pkt
->pkttype
== PKT_PLAINTEXT
;
1372 pkt
= xmalloc( sizeof *pkt
);
1378 if( rc
== G10ERR_INVALID_PACKET
)
1379 write_status_text( STATUS_NODATA
, "3" );
1383 write_status_text( STATUS_NODATA
, "2" );
1391 free_md_filter_context( &c
->mfx
);
1396 /* Helper for pka_uri_from_sig to parse the to-be-verified address out
1397 of the notation data. */
1399 get_pka_address (PKT_signature
*sig
)
1401 pka_info_t
*pka
= NULL
;
1402 struct notation
*nd
,*notation
;
1404 notation
=sig_to_notation(sig
);
1406 for(nd
=notation
;nd
;nd
=nd
->next
)
1408 if(strcmp(nd
->name
,"pka-address@gnupg.org")!=0)
1409 continue; /* Not the notation we want. */
1411 /* For now we only use the first valid PKA notation. In future
1412 we might want to keep additional PKA notations in a linked
1414 if (is_valid_mailbox (nd
->value
))
1416 pka
= xmalloc (sizeof *pka
+ strlen(nd
->value
));
1420 strcpy (pka
->email
, nd
->value
);
1425 free_notation(notation
);
1431 /* Return the URI from a DNS PKA record. If this record has already
1432 be retrieved for the signature we merely return it; if not we go
1433 out and try to get that DNS record. */
1435 pka_uri_from_sig (PKT_signature
*sig
)
1437 if (!sig
->flags
.pka_tried
)
1439 assert (!sig
->pka_info
);
1440 sig
->flags
.pka_tried
= 1;
1441 sig
->pka_info
= get_pka_address (sig
);
1446 uri
= get_pka_info (sig
->pka_info
->email
, sig
->pka_info
->fpr
);
1449 sig
->pka_info
->valid
= 1;
1453 sig
->pka_info
->uri
= uri
;
1457 return sig
->pka_info
? sig
->pka_info
->uri
: NULL
;
1462 check_sig_and_print( CTX c
, KBNODE node
)
1464 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1466 int rc
, is_expkey
=0, is_revkey
=0;
1468 if (opt
.skip_verify
)
1470 log_info(_("signature verification suppressed\n"));
1474 /* Check that the message composition is valid.
1476 Per RFC-2440bis (-15) allowed:
1478 S{1,n} -- detached signature.
1479 S{1,n} P -- old style PGP2 signature
1480 O{1,n} P S{1,n} -- standard OpenPGP signature.
1481 C P S{1,n} -- cleartext signature.
1484 O = One-Pass Signature packet.
1485 S = Signature packet.
1486 P = OpenPGP Message packet (Encrypted | Compressed | Literal)
1487 (Note that the current rfc2440bis draft also allows
1488 for a signed message but that does not work as it
1489 introduces ambiguities.)
1490 We keep track of these packages using the marker packet
1491 CTRLPKT_PLAINTEXT_MARK.
1492 C = Marker packet for cleartext signatures.
1494 We reject all other messages.
1496 Actually we are calling this too often, i.e. for verification of
1497 each message but better have some duplicate work than to silently
1498 introduce a bug here.
1502 int n_onepass
, n_sig
;
1504 /* log_debug ("checking signature packet composition\n"); */
1505 /* dump_kbnode (c->list); */
1509 if ( n
->pkt
->pkttype
== PKT_SIGNATURE
)
1511 /* This is either "S{1,n}" case (detached signature) or
1512 "S{1,n} P" (old style PGP2 signature). */
1513 for (n
= n
->next
; n
; n
= n
->next
)
1514 if (n
->pkt
->pkttype
!= PKT_SIGNATURE
)
1517 ; /* Okay, this is a detached signature. */
1518 else if (n
->pkt
->pkttype
== PKT_GPG_CONTROL
1519 && (n
->pkt
->pkt
.gpg_control
->control
1520 == CTRLPKT_PLAINTEXT_MARK
) )
1523 goto ambiguous
; /* We only allow one P packet. */
1528 else if (n
->pkt
->pkttype
== PKT_ONEPASS_SIG
)
1530 /* This is the "O{1,n} P S{1,n}" case (standard signature). */
1531 for (n_onepass
=1, n
= n
->next
;
1532 n
&& n
->pkt
->pkttype
== PKT_ONEPASS_SIG
; n
= n
->next
)
1534 if (!n
|| !(n
->pkt
->pkttype
== PKT_GPG_CONTROL
1535 && (n
->pkt
->pkt
.gpg_control
->control
1536 == CTRLPKT_PLAINTEXT_MARK
)))
1538 for (n_sig
=0, n
= n
->next
;
1539 n
&& n
->pkt
->pkttype
== PKT_SIGNATURE
; n
= n
->next
)
1544 /* If we wanted to disallow multiple sig verification, we'd do
1545 something like this:
1547 if (n && !opt.allow_multisig_verification)
1550 However, now that we have --allow-multiple-messages, this
1551 can stay allowable as we can't get here unless multiple
1552 messages (i.e. multiple literals) are allowed. */
1554 if (n_onepass
!= n_sig
)
1556 log_info ("number of one-pass packets does not match "
1557 "number of signature packets\n");
1561 else if (n
->pkt
->pkttype
== PKT_GPG_CONTROL
1562 && n
->pkt
->pkt
.gpg_control
->control
== CTRLPKT_CLEARSIGN_START
)
1564 /* This is the "C P S{1,n}" case (clear text signature). */
1566 if (!n
|| !(n
->pkt
->pkttype
== PKT_GPG_CONTROL
1567 && (n
->pkt
->pkt
.gpg_control
->control
1568 == CTRLPKT_PLAINTEXT_MARK
)))
1570 for (n_sig
=0, n
= n
->next
;
1571 n
&& n
->pkt
->pkttype
== PKT_SIGNATURE
; n
= n
->next
)
1579 log_error(_("can't handle this ambiguous signature data\n"));
1585 /* (Indendation below not yet changed to GNU style.) */
1587 astr
= gcry_pk_algo_name ( sig
->pubkey_algo
);
1590 log_info(_("Signature made %s\n"),asctimestamp(sig
->timestamp
));
1591 log_info(_(" using %s key %s\n"),
1592 astr
? astr
: "?",keystr(sig
->keyid
));
1595 log_info(_("Signature made %s using %s key ID %s\n"),
1596 asctimestamp(sig
->timestamp
), astr
? astr
: "?",
1597 keystr(sig
->keyid
));
1599 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1601 /* If the key isn't found, check for a preferred keyserver */
1603 if(rc
==G10ERR_NO_PUBKEY
&& sig
->flags
.pref_ks
)
1609 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&n
,&seq
,NULL
)))
1611 /* According to my favorite copy editor, in English
1612 grammar, you say "at" if the key is located on a web
1613 page, but "from" if it is located on a keyserver. I'm
1614 not going to even try to make two strings here :) */
1615 log_info(_("Key available at: ") );
1616 print_utf8_string( log_get_stream(), p
, n
);
1619 if(opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
1620 && opt
.keyserver_options
.options
&KEYSERVER_HONOR_KEYSERVER_URL
)
1622 struct keyserver_spec
*spec
;
1624 spec
=parse_preferred_keyserver(sig
);
1629 glo_ctrl
.in_auto_key_retrieve
++;
1630 res
=keyserver_import_keyid(sig
->keyid
,spec
);
1631 glo_ctrl
.in_auto_key_retrieve
--;
1633 rc
=do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1634 free_keyserver_spec(spec
);
1643 /* If the preferred keyserver thing above didn't work, our second
1644 try is to use the URI from a DNS PKA record. */
1645 if ( rc
== G10ERR_NO_PUBKEY
1646 && opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
1647 && opt
.keyserver_options
.options
&KEYSERVER_HONOR_PKA_RECORD
)
1649 const char *uri
= pka_uri_from_sig (sig
);
1653 /* FIXME: We might want to locate the key using the
1654 fingerprint instead of the keyid. */
1656 struct keyserver_spec
*spec
;
1658 spec
= parse_keyserver_uri (uri
, 1, NULL
, 0);
1661 glo_ctrl
.in_auto_key_retrieve
++;
1662 res
= keyserver_import_keyid (sig
->keyid
, spec
);
1663 glo_ctrl
.in_auto_key_retrieve
--;
1664 free_keyserver_spec (spec
);
1666 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1671 /* If the preferred keyserver thing above didn't work and we got
1672 no information from the DNS PKA, this is a third try. */
1674 if( rc
== G10ERR_NO_PUBKEY
&& opt
.keyserver
1675 && opt
.keyserver_options
.options
&KEYSERVER_AUTO_KEY_RETRIEVE
)
1679 glo_ctrl
.in_auto_key_retrieve
++;
1680 res
=keyserver_import_keyid ( sig
->keyid
, opt
.keyserver
);
1681 glo_ctrl
.in_auto_key_retrieve
--;
1683 rc
= do_check_sig(c
, node
, NULL
, &is_expkey
, &is_revkey
);
1686 if( !rc
|| gpg_err_code (rc
) == GPG_ERR_BAD_SIGNATURE
) {
1687 KBNODE un
, keyblock
;
1688 int count
=0, statno
;
1690 PKT_public_key
*pk
=NULL
;
1693 statno
=STATUS_BADSIG
;
1694 else if(sig
->flags
.expired
)
1695 statno
=STATUS_EXPSIG
;
1697 statno
=STATUS_EXPKEYSIG
;
1699 statno
=STATUS_REVKEYSIG
;
1701 statno
=STATUS_GOODSIG
;
1703 keyblock
= get_pubkeyblock( sig
->keyid
);
1705 sprintf (keyid_str
, "%08lX%08lX [uncertain] ",
1706 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1]);
1708 /* find and print the primary user ID */
1709 for( un
=keyblock
; un
; un
= un
->next
) {
1712 if(un
->pkt
->pkttype
==PKT_PUBLIC_KEY
)
1714 pk
=un
->pkt
->pkt
.public_key
;
1717 if( un
->pkt
->pkttype
!= PKT_USER_ID
)
1719 if ( !un
->pkt
->pkt
.user_id
->created
)
1721 if ( un
->pkt
->pkt
.user_id
->is_revoked
)
1723 if ( un
->pkt
->pkt
.user_id
->is_expired
)
1725 if ( !un
->pkt
->pkt
.user_id
->is_primary
)
1727 /* We want the textual primary user ID here */
1728 if ( un
->pkt
->pkt
.user_id
->attrib_data
)
1733 /* Get it before we print anything to avoid interrupting
1734 the output with the "please do a --check-trustdb"
1736 valid
=get_validity(pk
,un
->pkt
->pkt
.user_id
);
1738 keyid_str
[17] = 0; /* cut off the "[uncertain]" part */
1739 write_status_text_and_buffer (statno
, keyid_str
,
1740 un
->pkt
->pkt
.user_id
->name
,
1741 un
->pkt
->pkt
.user_id
->len
,
1744 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1745 un
->pkt
->pkt
.user_id
->len
,0);
1748 log_info(_("BAD signature from \"%s\""),p
);
1749 else if(sig
->flags
.expired
)
1750 log_info(_("Expired signature from \"%s\""),p
);
1752 log_info(_("Good signature from \"%s\""),p
);
1756 if(opt
.verify_options
&VERIFY_SHOW_UID_VALIDITY
)
1757 log_printf (" [%s]\n",trust_value_to_string(valid
));
1762 if( !count
) { /* just in case that we have no valid textual
1766 /* Try for an invalid textual userid */
1767 for( un
=keyblock
; un
; un
= un
->next
) {
1768 if( un
->pkt
->pkttype
== PKT_USER_ID
&&
1769 !un
->pkt
->pkt
.user_id
->attrib_data
)
1773 /* Try for any userid at all */
1775 for( un
=keyblock
; un
; un
= un
->next
) {
1776 if( un
->pkt
->pkttype
== PKT_USER_ID
)
1781 if (opt
.trust_model
==TM_ALWAYS
|| !un
)
1782 keyid_str
[17] = 0; /* cut off the "[uncertain]" part */
1784 write_status_text_and_buffer (statno
, keyid_str
,
1785 un
? un
->pkt
->pkt
.user_id
->name
:"[?]",
1786 un
? un
->pkt
->pkt
.user_id
->len
:3,
1790 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1791 un
->pkt
->pkt
.user_id
->len
,0);
1796 log_info(_("BAD signature from \"%s\""),p
);
1797 else if(sig
->flags
.expired
)
1798 log_info(_("Expired signature from \"%s\""),p
);
1800 log_info(_("Good signature from \"%s\""),p
);
1801 if (opt
.trust_model
!=TM_ALWAYS
&& un
)
1802 log_printf (" %s",_("[uncertain]") );
1806 /* If we have a good signature and already printed
1807 * the primary user ID, print all the other user IDs */
1809 && !(opt
.verify_options
&VERIFY_SHOW_PRIMARY_UID_ONLY
)) {
1811 for( un
=keyblock
; un
; un
= un
->next
) {
1812 if( un
->pkt
->pkttype
!= PKT_USER_ID
)
1814 if((un
->pkt
->pkt
.user_id
->is_revoked
1815 || un
->pkt
->pkt
.user_id
->is_expired
)
1816 && !(opt
.verify_options
&VERIFY_SHOW_UNUSABLE_UIDS
))
1818 /* Only skip textual primaries */
1819 if ( un
->pkt
->pkt
.user_id
->is_primary
&&
1820 !un
->pkt
->pkt
.user_id
->attrib_data
)
1823 if(un
->pkt
->pkt
.user_id
->attrib_data
)
1825 dump_attribs(un
->pkt
->pkt
.user_id
,pk
,NULL
);
1827 if(opt
.verify_options
&VERIFY_SHOW_PHOTOS
)
1828 show_photos(un
->pkt
->pkt
.user_id
->attribs
,
1829 un
->pkt
->pkt
.user_id
->numattribs
,pk
,NULL
);
1832 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
1833 un
->pkt
->pkt
.user_id
->len
,0);
1834 log_info(_(" aka \"%s\""),p
);
1837 if(opt
.verify_options
&VERIFY_SHOW_UID_VALIDITY
)
1840 if(un
->pkt
->pkt
.user_id
->is_revoked
)
1842 else if(un
->pkt
->pkt
.user_id
->is_expired
)
1845 valid
=trust_value_to_string(get_validity(pk
,
1848 log_printf (" [%s]\n",valid
);
1854 release_kbnode( keyblock
);
1858 if(opt
.verify_options
&VERIFY_SHOW_POLICY_URLS
)
1859 show_policy_url(sig
,0,1);
1861 show_policy_url(sig
,0,2);
1863 if(opt
.verify_options
&VERIFY_SHOW_KEYSERVER_URLS
)
1864 show_keyserver_url(sig
,0,1);
1866 show_keyserver_url(sig
,0,2);
1868 if(opt
.verify_options
&VERIFY_SHOW_NOTATIONS
)
1869 show_notation(sig
,0,1,
1870 ((opt
.verify_options
&VERIFY_SHOW_STD_NOTATIONS
)?1:0)+
1871 ((opt
.verify_options
&VERIFY_SHOW_USER_NOTATIONS
)?2:0));
1873 show_notation(sig
,0,2,0);
1876 if( !rc
&& is_status_enabled() ) {
1877 /* print a status response with the fingerprint */
1878 PKT_public_key
*vpk
= xmalloc_clear( sizeof *vpk
);
1880 if( !get_pubkey( vpk
, sig
->keyid
) ) {
1881 byte array
[MAX_FINGERPRINT_LEN
], *p
;
1882 char buf
[MAX_FINGERPRINT_LEN
*4+90], *bufp
;
1886 fingerprint_from_pk( vpk
, array
, &n
);
1888 for(i
=0; i
< n
; i
++, p
++, bufp
+= 2)
1889 sprintf(bufp
, "%02X", *p
);
1890 /* TODO: Replace the reserved '0' in the field below
1891 with bits for status flags (policy url, notation,
1892 etc.). Remember to make the buffer larger to
1894 sprintf(bufp
, " %s %lu %lu %d 0 %d %d %02X ",
1895 strtimestamp( sig
->timestamp
),
1896 (ulong
)sig
->timestamp
,(ulong
)sig
->expiredate
,
1897 sig
->version
,sig
->pubkey_algo
,sig
->digest_algo
,
1899 bufp
= bufp
+ strlen (bufp
);
1900 if (!vpk
->is_primary
) {
1903 akid
[0] = vpk
->main_keyid
[0];
1904 akid
[1] = vpk
->main_keyid
[1];
1905 free_public_key (vpk
);
1906 vpk
= xmalloc_clear( sizeof *vpk
);
1907 if (get_pubkey (vpk
, akid
)) {
1908 /* impossible error, we simply return a zeroed out fpr */
1909 n
= MAX_FINGERPRINT_LEN
< 20? MAX_FINGERPRINT_LEN
: 20;
1910 memset (array
, 0, n
);
1913 fingerprint_from_pk( vpk
, array
, &n
);
1916 for(i
=0; i
< n
; i
++, p
++, bufp
+= 2)
1917 sprintf(bufp
, "%02X", *p
);
1918 write_status_text( STATUS_VALIDSIG
, buf
);
1920 free_public_key( vpk
);
1925 if(opt
.verify_options
&VERIFY_PKA_LOOKUPS
)
1926 pka_uri_from_sig (sig
); /* Make sure PKA info is available. */
1927 rc
= check_signatures_trust( sig
);
1930 if(sig
->flags
.expired
)
1932 log_info(_("Signature expired %s\n"),
1933 asctimestamp(sig
->expiredate
));
1934 rc
=G10ERR_GENERAL
; /* need a better error here? */
1936 else if(sig
->expiredate
)
1937 log_info(_("Signature expires %s\n"),asctimestamp(sig
->expiredate
));
1940 log_info(_("%s signature, digest algorithm %s\n"),
1941 sig
->sig_class
==0x00?_("binary"):
1942 sig
->sig_class
==0x01?_("textmode"):_("unknown"),
1943 gcry_md_algo_name (sig
->digest_algo
));
1946 g10_errors_seen
= 1;
1947 if( opt
.batch
&& rc
)
1952 sprintf(buf
, "%08lX%08lX %d %d %02x %lu %d",
1953 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1954 sig
->pubkey_algo
, sig
->digest_algo
,
1955 sig
->sig_class
, (ulong
)sig
->timestamp
, rc
);
1956 write_status_text( STATUS_ERRSIG
, buf
);
1957 if( rc
== G10ERR_NO_PUBKEY
) {
1959 write_status_text( STATUS_NO_PUBKEY
, buf
);
1961 if( rc
!= G10ERR_NOT_PROCESSED
)
1962 log_error(_("Can't check signature: %s\n"), g10_errstr(rc
) );
1969 * Process the tree which starts at node
1972 proc_tree( CTX c
, KBNODE node
)
1977 if( opt
.list_packets
|| opt
.list_only
)
1980 /* we must skip our special plaintext marker packets here becuase
1981 they may be the root packet. These packets are only used in
1982 addionla checks and skipping them here doesn't matter */
1984 && node
->pkt
->pkttype
== PKT_GPG_CONTROL
1985 && node
->pkt
->pkt
.gpg_control
->control
1986 == CTRLPKT_PLAINTEXT_MARK
) {
1992 c
->trustletter
= ' ';
1993 if( node
->pkt
->pkttype
== PKT_PUBLIC_KEY
1994 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1995 merge_keys_and_selfsig( node
);
1996 list_node( c
, node
);
1998 else if( node
->pkt
->pkttype
== PKT_SECRET_KEY
) {
1999 merge_keys_and_selfsig( node
);
2000 list_node( c
, node
);
2002 else if( node
->pkt
->pkttype
== PKT_ONEPASS_SIG
) {
2003 /* check all signatures */
2004 if( !c
->have_data
) {
2005 int use_textmode
= 0;
2007 free_md_filter_context( &c
->mfx
);
2008 /* prepare to create all requested message digests */
2009 if (gcry_md_open (&c
->mfx
.md
, 0, 0))
2012 /* fixme: why looking for the signature packet and not the
2014 for ( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
2016 gcry_md_enable (c
->mfx
.md
,
2017 n1
->pkt
->pkt
.signature
->digest_algo
);
2020 if (n1
&& n1
->pkt
->pkt
.onepass_sig
->sig_class
== 0x01)
2023 /* Ask for file and hash it. */
2024 if( c
->sigs_only
) {
2025 if (c
->signed_data
.used
&& c
->signed_data
.data_fd
!= -1)
2026 rc
= hash_datafile_by_fd (c
->mfx
.md
, NULL
,
2027 c
->signed_data
.data_fd
,
2030 rc
= hash_datafiles (c
->mfx
.md
, NULL
,
2031 c
->signed_data
.data_names
,
2036 rc
= ask_for_detached_datafile (c
->mfx
.md
, c
->mfx
.md2
,
2037 iobuf_get_real_fname(c
->iobuf
),
2041 log_error("can't hash datafile: %s\n", g10_errstr(rc
));
2045 else if ( c
->signed_data
.used
) {
2046 log_error (_("not a detached signature\n") );
2050 for( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
2051 check_sig_and_print( c
, n1
);
2053 else if( node
->pkt
->pkttype
== PKT_GPG_CONTROL
2054 && node
->pkt
->pkt
.gpg_control
->control
2055 == CTRLPKT_CLEARSIGN_START
) {
2056 /* clear text signed message */
2057 if( !c
->have_data
) {
2058 log_error("cleartext signature without data\n" );
2061 else if ( c
->signed_data
.used
) {
2062 log_error (_("not a detached signature\n") );
2066 for( n1
= node
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)); )
2067 check_sig_and_print( c
, n1
);
2069 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
) {
2070 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
2073 n1
=find_next_kbnode(node
, PKT_SIGNATURE
);
2076 byte
class=sig
->sig_class
;
2077 byte hash
=sig
->digest_algo
;
2079 for(; n1
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)))
2081 /* We can't currently handle multiple signatures of
2082 different classes or digests (we'd pretty much have
2083 to run a different hash context for each), but if
2084 they are all the same, make an exception. */
2085 if(n1
->pkt
->pkt
.signature
->sig_class
!=class
2086 || n1
->pkt
->pkt
.signature
->digest_algo
!=hash
)
2089 log_info(_("WARNING: multiple signatures detected. "
2090 "Only the first will be checked.\n"));
2096 if( sig
->sig_class
!= 0x00 && sig
->sig_class
!= 0x01 )
2097 log_info(_("standalone signature of class 0x%02x\n"),
2099 else if( !c
->have_data
) {
2100 /* detached signature */
2101 free_md_filter_context( &c
->mfx
);
2102 if (gcry_md_open (&c
->mfx
.md
, sig
->digest_algo
, 0))
2105 if( !opt
.pgp2_workarounds
)
2107 else if( sig
->digest_algo
== DIGEST_ALGO_MD5
2108 && is_RSA( sig
->pubkey_algo
) ) {
2109 /* enable a workaround for a pgp2 bug */
2110 if (gcry_md_open (&c
->mfx
.md2
, DIGEST_ALGO_MD5
, 0))
2113 else if( sig
->digest_algo
== DIGEST_ALGO_SHA1
2114 && sig
->pubkey_algo
== PUBKEY_ALGO_DSA
2115 && sig
->sig_class
== 0x01 ) {
2116 /* enable the workaround also for pgp5 when the detached
2117 * signature has been created in textmode */
2118 if (gcry_md_open (&c
->mfx
.md2
, sig
->digest_algo
, 0 ))
2121 #if 0 /* workaround disabled */
2122 /* Here we have another hack to work around a pgp 2 bug
2123 * It works by not using the textmode for detached signatures;
2124 * this will let the first signature check (on md) fail
2125 * but the second one (on md2) which adds an extra CR should
2126 * then produce the "correct" hash. This is very, very ugly
2127 * hack but it may help in some cases (and break others)
2129 /* c->mfx.md2? 0 :(sig->sig_class == 0x01) */
2131 if ( DBG_HASHING
) {
2132 gcry_md_start_debug( c
->mfx
.md
, "verify" );
2134 gcry_md_start_debug( c
->mfx
.md2
, "verify2" );
2136 if( c
->sigs_only
) {
2137 if (c
->signed_data
.used
&& c
->signed_data
.data_fd
!= -1)
2138 rc
= hash_datafile_by_fd (c
->mfx
.md
, c
->mfx
.md2
,
2139 c
->signed_data
.data_fd
,
2140 (sig
->sig_class
== 0x01));
2142 rc
= hash_datafiles (c
->mfx
.md
, c
->mfx
.md2
,
2143 c
->signed_data
.data_names
,
2145 (sig
->sig_class
== 0x01));
2148 rc
= ask_for_detached_datafile( c
->mfx
.md
, c
->mfx
.md2
,
2149 iobuf_get_real_fname(c
->iobuf
),
2150 (sig
->sig_class
== 0x01) );
2153 log_error("can't hash datafile: %s\n", g10_errstr(rc
));
2157 else if ( c
->signed_data
.used
) {
2158 log_error (_("not a detached signature\n") );
2161 else if (!opt
.quiet
)
2162 log_info(_("old style (PGP 2.x) signature\n"));
2165 for( n1
= node
; n1
; (n1
= find_next_kbnode(n1
, PKT_SIGNATURE
)) )
2166 check_sig_and_print( c
, n1
);
2168 check_sig_and_print( c
, node
);
2171 dump_kbnode (c
->list
);
2172 log_error(_("invalid root packet detected in proc_tree()\n"));