1 /* keylist.c - List all or selected keys
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
42 static void list_all(int);
43 static void list_one( STRLIST names
, int secret
);
44 static void print_card_serialno (PKT_secret_key
*sk
);
53 static FILE *attrib_fp
=NULL
;
57 * If list is NULL, all available keys are listed
60 public_key_list( STRLIST list
)
64 byte trust_model
,marginals
,completes
,cert_depth
;
65 ulong created
,nextcheck
;
67 read_trust_options(&trust_model
,&created
,&nextcheck
,
68 &marginals
,&completes
,&cert_depth
);
72 if(nextcheck
&& nextcheck
<= make_timestamp())
74 if(trust_model
!=opt
.trust_model
)
76 if(opt
.trust_model
==TM_PGP
|| opt
.trust_model
==TM_CLASSIC
)
78 if(marginals
!=opt
.marginals_needed
)
80 if(completes
!=opt
.completes_needed
)
82 if(cert_depth
!=opt
.max_cert_depth
)
86 printf(":%d:%lu:%lu",trust_model
,created
,nextcheck
);
88 /* Only show marginals, completes, and cert_depth in the classic
89 or PGP trust models since they are not meaningful
92 if(trust_model
==TM_PGP
|| trust_model
==TM_CLASSIC
)
93 printf(":%d:%d:%d",marginals
,completes
,cert_depth
);
105 secret_key_list( STRLIST list
)
109 else /* List by user id */
114 print_seckey_info (PKT_secret_key
*sk
)
120 keyid_from_sk (sk
, sk_keyid
);
121 tty_printf ("\nsec %4u%c/%08lX %s ",
123 pubkey_letter (sk
->pubkey_algo
),
124 (ulong
)sk_keyid
[1], datestr_from_sk (sk
));
126 p
= get_user_id (sk_keyid
, &n
);
127 tty_print_utf8_string (p
, n
);
133 /* Print information about the public key. With FP passed as NULL,
134 the tty output interface is used, otherwise output is directted to
137 print_pubkey_info (FILE *fp
, PKT_public_key
*pk
)
143 keyid_from_pk (pk
, pk_keyid
);
145 fprintf (fp
, "pub %4u%c/%08lX %s ",
147 pubkey_letter (pk
->pubkey_algo
),
148 (ulong
)pk_keyid
[1], datestr_from_pk (pk
));
150 tty_printf ("\npub %4u%c/%08lX %s ",
152 pubkey_letter (pk
->pubkey_algo
),
153 (ulong
)pk_keyid
[1], datestr_from_pk (pk
));
155 p
= get_user_id (pk_keyid
, &n
);
157 print_utf8_string2 (fp
, p
, n
, '\n');
159 tty_print_utf8_string (p
, n
);
170 mode=1 for log_info + status messages
171 mode=2 for status messages only
175 show_policy_url(PKT_signature
*sig
,int indent
,int mode
)
180 FILE *fp
=mode
?log_get_stream():stdout
;
182 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_POLICY
,&len
,&seq
,&crit
)))
189 for(i
=0;i
<indent
;i
++)
193 str
=_("Critical signature policy: ");
195 str
=_("Signature policy: ");
200 print_utf8_string(fp
,p
,len
);
205 write_status_buffer ( STATUS_POLICY_URL
, p
, len
, 0 );
212 mode=1 for log_info + status messages
213 mode=2 for status messages only
217 show_keyserver_url(PKT_signature
*sig
,int indent
,int mode
)
222 FILE *fp
=mode
?log_get_stream():stdout
;
224 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&len
,&seq
,&crit
)))
231 for(i
=0;i
<indent
;i
++)
235 str
=_("Critical preferred keyserver: ");
237 str
=_("Preferred keyserver: ");
242 print_utf8_string(fp
,p
,len
);
246 /* TODO: put in a status-fd tag for preferred keyservers */
253 mode=1 for log_info + status messages
254 mode=2 for status messages only
258 show_notation(PKT_signature
*sig
,int indent
,int mode
)
263 FILE *fp
=mode
?log_get_stream():stdout
;
265 /* There may be multiple notations in the same sig. */
267 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_NOTATION
,&len
,&seq
,&crit
)))
277 log_info(_("WARNING: invalid notation data found\n"));
286 for(i
=0;i
<indent
;i
++)
291 str
=_("Critical signature notation: ");
293 str
=_("Signature notation: ");
298 print_utf8_string(fp
,p
+8,n1
);
302 print_utf8_string(fp
,p
+8+n1
,n2
);
304 fprintf(fp
,"[ %s ]",_("not human readable"));
311 write_status_buffer ( STATUS_NOTATION_NAME
, p
+8 , n1
, 0 );
312 write_status_buffer ( STATUS_NOTATION_DATA
, p
+8+n1
, n2
, 50 );
316 log_info(_("WARNING: invalid notation data found\n"));
320 print_signature_stats(struct sig_stats
*s
)
322 if( s
->inv_sigs
== 1 )
323 tty_printf(_("1 bad signature\n") );
324 else if( s
->inv_sigs
)
325 tty_printf(_("%d bad signatures\n"), s
->inv_sigs
);
327 tty_printf(_("1 signature not checked due to a missing key\n") );
329 tty_printf(_("%d signatures not checked due to missing keys\n"),s
->no_key
);
330 if( s
->oth_err
== 1 )
331 tty_printf(_("1 signature not checked due to an error\n") );
332 else if( s
->oth_err
)
333 tty_printf(_("%d signatures not checked due to errors\n"), s
->oth_err
);
337 list_all( int secret
)
340 KBNODE keyblock
= NULL
;
342 const char *lastresname
, *resname
;
343 struct sig_stats stats
;
345 memset(&stats
,0,sizeof(stats
));
347 hd
= keydb_new (secret
);
349 rc
= GPG_ERR_GENERAL
;
351 rc
= keydb_search_first (hd
);
354 log_error("keydb_search_first failed: %s\n", gpg_strerror (rc
) );
360 rc
= keydb_get_keyblock (hd
, &keyblock
);
362 log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc
));
367 resname
= keydb_get_resource_name (hd
);
368 if (lastresname
!= resname
)
372 printf("%s\n", resname
);
373 for(i
=strlen(resname
); i
; i
-- )
376 lastresname
= resname
;
379 merge_keys_and_selfsig( keyblock
);
380 list_keyblock( keyblock
, secret
, opt
.fingerprint
,
381 opt
.check_sigs
?&stats
:NULL
);
382 release_kbnode( keyblock
);
384 } while (!(rc
= keydb_search_next (hd
)));
386 log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc
));
388 if(opt
.check_sigs
&& !opt
.with_colons
)
389 print_signature_stats(&stats
);
392 release_kbnode (keyblock
);
398 list_one( STRLIST names
, int secret
)
401 KBNODE keyblock
= NULL
;
404 char *keyring_str
= _("Keyring");
406 struct sig_stats stats
;
408 memset(&stats
,0,sizeof(stats
));
410 /* fixme: using the bynames function has the disadvantage that we
411 * don't know wether one of the names given was not found. OTOH,
412 * this function has the advantage to list the names in the
413 * sequence as defined by the keyDB and does not duplicate
414 * outputs. A solution could be do test whether all given have
415 * been listed (this needs a way to use the keyDB search
416 * functions) or to have the search function return indicators for
417 * found names. Yet another way is to use the keydb search
418 * facilities directly. */
420 rc
= get_seckey_bynames( &ctx
, NULL
, names
, &keyblock
);
422 log_error("error reading key: %s\n", gpg_strerror (rc
) );
423 get_seckey_end( ctx
);
427 if ((opt
.list_options
&LIST_SHOW_KEYRING
) && !opt
.with_colons
) {
428 resname
= keydb_get_resource_name (get_ctx_handle(ctx
));
429 printf("%s: %s\n", keyring_str
, resname
);
430 for(i
= strlen(resname
) + strlen(keyring_str
) + 2; i
; i
-- )
434 list_keyblock( keyblock
, 1, opt
.fingerprint
, NULL
);
435 release_kbnode( keyblock
);
436 } while( !get_seckey_next( ctx
, NULL
, &keyblock
) );
437 get_seckey_end( ctx
);
440 rc
= get_pubkey_bynames( &ctx
, NULL
, names
, &keyblock
);
442 log_error("error reading key: %s\n", gpg_strerror (rc
) );
443 get_pubkey_end( ctx
);
447 if ((opt
.list_options
&LIST_SHOW_KEYRING
) && !opt
.with_colons
) {
448 resname
= keydb_get_resource_name (get_ctx_handle(ctx
));
449 printf("%s: %s\n", keyring_str
, resname
);
450 for(i
= strlen(resname
) + strlen(keyring_str
) + 2; i
; i
-- )
454 list_keyblock( keyblock
, 0, opt
.fingerprint
,
455 opt
.check_sigs
?&stats
:NULL
);
456 release_kbnode( keyblock
);
457 } while( !get_pubkey_next( ctx
, NULL
, &keyblock
) );
458 get_pubkey_end( ctx
);
461 if(opt
.check_sigs
&& !opt
.with_colons
)
462 print_signature_stats(&stats
);
466 print_key_data( PKT_public_key
*pk
, u32
*keyid
)
468 int n
= pk
? pubkey_get_npkey( pk
->pubkey_algo
) : 0;
471 for(i
=0; i
< n
; i
++ ) {
472 printf("pkd:%d:%u:", i
, mpi_get_nbits( pk
->pkey
[i
] ) );
473 mpi_print(stdout
, pk
->pkey
[i
], 1 );
480 print_capabilities (PKT_public_key
*pk
, PKT_secret_key
*sk
, KBNODE keyblock
)
482 if(pk
|| (sk
&& sk
->protect
.s2k
.mode
!=1001))
484 unsigned int use
= pk
? pk
->pubkey_usage
: sk
->pubkey_usage
;
486 if ( (use
& PUBKEY_USAGE_ENC
) )
489 if ( (use
& PUBKEY_USAGE_SIG
) )
492 if( pk
? pk
->is_primary
: sk
->is_primary
)
496 if ( (use
& PUBKEY_USAGE_AUTH
) )
500 if ( keyblock
) { /* figure out the usable capabilities */
502 int enc
=0, sign
=0, cert
=0, auth
=0, disabled
=0;
504 for (k
=keyblock
; k
; k
= k
->next
) {
505 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
506 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
507 pk
= k
->pkt
->pkt
.public_key
;
510 disabled
=pk_is_disabled(pk
);
512 if ( pk
->is_valid
&& !pk
->is_revoked
&& !pk
->has_expired
) {
513 if ( (pk
->pubkey_usage
& PUBKEY_USAGE_ENC
) )
515 if ( (pk
->pubkey_usage
& PUBKEY_USAGE_SIG
) )
521 if ( (pk
->pubkey_usage
& PUBKEY_USAGE_AUTH
) )
525 else if ( k
->pkt
->pkttype
== PKT_SECRET_KEY
526 || k
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
527 sk
= k
->pkt
->pkt
.secret_key
;
528 if ( sk
->is_valid
&& !sk
->is_revoked
&& !sk
->has_expired
529 && sk
->protect
.s2k
.mode
!=1001 ) {
530 if ( (sk
->pubkey_usage
& PUBKEY_USAGE_ENC
) )
532 if ( (sk
->pubkey_usage
& PUBKEY_USAGE_SIG
) )
538 if ( (sk
->pubkey_usage
& PUBKEY_USAGE_AUTH
) )
559 dump_attribs(const PKT_user_id
*uid
,PKT_public_key
*pk
,PKT_secret_key
*sk
)
566 for(i
=0;i
<uid
->numattribs
;i
++)
568 if(is_status_enabled())
570 byte array
[MAX_FINGERPRINT_LEN
], *p
;
571 char buf
[(MAX_FINGERPRINT_LEN
*2)+90];
575 fingerprint_from_pk( pk
, array
, &n
);
577 fingerprint_from_sk( sk
, array
, &n
);
582 for(j
=0; j
< n
; j
++, p
++ )
583 sprintf(buf
+2*j
, "%02X", *p
);
585 sprintf(buf
+strlen(buf
)," %lu %u %u %u %lu %lu %u",
586 (ulong
)uid
->attribs
[i
].len
,uid
->attribs
[i
].type
,i
+1,
587 uid
->numattribs
,(ulong
)uid
->created
,(ulong
)uid
->expiredate
,
588 ((uid
->is_primary
?0x01:0)|
589 (uid
->is_revoked
?0x02:0)|
590 (uid
->is_expired
?0x04:0)));
591 write_status_text(STATUS_ATTRIBUTE
,buf
);
594 fwrite(uid
->attribs
[i
].data
,uid
->attribs
[i
].len
,1,attrib_fp
);
599 list_keyblock_print ( KBNODE keyblock
, int secret
, int fpr
, void *opaque
)
608 struct sig_stats
*stats
=opaque
;
609 int newformat
=((opt
.list_options
&LIST_SHOW_VALIDITY
) && !secret
)
610 || (opt
.list_options
&LIST_SHOW_LONG_KEYID
);
612 /* get the keyid from the keyblock */
613 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
: PKT_PUBLIC_KEY
);
615 log_error("Oops; key lost!\n");
616 dump_kbnode( keyblock
);
623 sk
= node
->pkt
->pkt
.secret_key
;
624 keyid_from_sk( sk
, keyid
);
626 printf("sec%c %4u%c/",(sk
->protect
.s2k
.mode
==1001)?'#':' ',
627 nbits_from_sk( sk
),pubkey_letter( sk
->pubkey_algo
));
629 if(opt
.list_options
&LIST_SHOW_LONG_KEYID
)
630 printf("%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
632 printf("%08lX",(ulong
)keyid
[1]);
634 printf(" %s%s",datestr_from_sk( sk
),newformat
?"":" " );
636 if(newformat
&& sk
->expiredate
)
637 printf(_(" [expires: %s]"), expirestr_from_sk( sk
) );
642 pk
= node
->pkt
->pkt
.public_key
;
644 keyid_from_pk( pk
, keyid
);
646 validity
=get_validity(pk
,NULL
);
649 nbits_from_pk(pk
),pubkey_letter(pk
->pubkey_algo
));
651 if(opt
.list_options
&LIST_SHOW_LONG_KEYID
)
652 printf("%08lX%08lX",(ulong
)keyid
[0],(ulong
)keyid
[1]);
654 printf("%08lX",(ulong
)keyid
[1]);
656 printf(" %s%s",datestr_from_pk( pk
),newformat
?"":" " );
658 /* We didn't include this before in the key listing, but there
659 is room in the new format, so why not? */
660 if(newformat
&& pk
->expiredate
)
661 printf(_(" [expires: %s]"), expirestr_from_pk( pk
) );
663 if(opt
.list_options
&LIST_SHOW_VALIDITY
)
664 printf(" [%s]",trust_value_to_string(validity
));
667 for( kbctx
=NULL
; (node
=walk_kbnode( keyblock
, &kbctx
, 0)) ; ) {
668 if( node
->pkt
->pkttype
== PKT_USER_ID
&& !opt
.fast_list_mode
) {
670 /* don't list revoked or expired UIDS unless we are in
671 * verbose mode and signature listing has not been
673 if ( !opt
.verbose
&& !opt
.list_sigs
&&
674 (node
->pkt
->pkt
.user_id
->is_revoked
||
675 node
->pkt
->pkt
.user_id
->is_expired
))
678 if(attrib_fp
&& node
->pkt
->pkt
.user_id
->attrib_data
!=NULL
)
679 dump_attribs(node
->pkt
->pkt
.user_id
,pk
,sk
);
681 if(!any
&& newformat
)
684 if((opt
.list_options
&LIST_SHOW_VALIDITY
) && pk
)
686 const char *validity
=
687 trust_value_to_string(get_validity(pk
,node
->pkt
->pkt
.user_id
));
689 /* Includes the 3 spaces for [, ], and " ". */
690 indent
=((opt
.list_options
&LIST_SHOW_LONG_KEYID
)?23:15)
696 printf("uid%*s[%s] ",indent
,"",validity
);
699 printf("uid%*s",26,"");
701 printf("uid%*s",29,"");
703 if ( node
->pkt
->pkt
.user_id
->is_revoked
)
704 fputs ("[revoked] ", stdout
);
705 if ( node
->pkt
->pkt
.user_id
->is_expired
)
706 fputs ("[expired] ", stdout
);
708 print_utf8_string( stdout
, node
->pkt
->pkt
.user_id
->name
,
709 node
->pkt
->pkt
.user_id
->len
);
713 print_fingerprint( pk
, sk
, 0 );
714 print_card_serialno (sk
);
715 if( opt
.with_key_data
)
716 print_key_data( pk
, keyid
);
720 if((opt
.list_options
&LIST_SHOW_PHOTOS
)
721 && node
->pkt
->pkt
.user_id
->attribs
!=NULL
)
722 show_photos(node
->pkt
->pkt
.user_id
->attribs
,
723 node
->pkt
->pkt
.user_id
->numattribs
,pk
,sk
);
725 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
727 PKT_public_key
*pk2
= node
->pkt
->pkt
.public_key
;
732 print_fingerprint( pk
, sk
, 0 ); /* of the main key */
736 keyid_from_pk( pk2
, keyid2
);
738 nbits_from_pk( pk2
),pubkey_letter( pk2
->pubkey_algo
));
739 if(opt
.list_options
&LIST_SHOW_LONG_KEYID
)
740 printf("%08lX%08lX",(ulong
)keyid2
[0],(ulong
)keyid2
[1]);
742 printf("%08lX",(ulong
)keyid2
[1]);
743 printf(" %s",datestr_from_pk(pk2
));
744 if( pk2
->expiredate
)
745 printf(_(" [expires: %s]"), expirestr_from_pk( pk2
) );
748 print_fingerprint( pk2
, NULL
, 0 );
749 if( opt
.with_key_data
)
750 print_key_data( pk2
, keyid2
);
752 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
754 PKT_secret_key
*sk2
= node
->pkt
->pkt
.secret_key
;
759 print_fingerprint( pk
, sk
, 0 ); /* of the main key */
760 print_card_serialno (sk
);
764 keyid_from_sk( sk2
, keyid2
);
766 nbits_from_sk( sk2
),pubkey_letter( sk2
->pubkey_algo
));
767 if(opt
.list_options
&LIST_SHOW_LONG_KEYID
)
768 printf("%08lX%08lX",(ulong
)keyid2
[0],(ulong
)keyid2
[1]);
770 printf("%08lX",(ulong
)keyid2
[1]);
771 printf(" %s",datestr_from_sk( sk2
) );
772 if( sk2
->expiredate
)
773 printf(_(" [expires: %s]"), expirestr_from_sk( sk2
) );
777 print_fingerprint( NULL
, sk2
, 0 );
778 print_card_serialno (sk
);
781 else if( opt
.list_sigs
&& node
->pkt
->pkttype
== PKT_SIGNATURE
) {
782 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
788 rc
= check_key_signature( keyblock
, node
, NULL
);
789 switch( gpg_err_code (rc
) ) {
790 case 0: sigrc
= '!'; break;
791 case GPG_ERR_BAD_SIGNATURE
: stats
->inv_sigs
++; sigrc
= '-'; break;
792 case GPG_ERR_NO_PUBKEY
:
793 case GPG_ERR_UNUSABLE_PUBKEY
: stats
->no_key
++; continue;
794 default: stats
->oth_err
++; sigrc
= '%'; break;
797 /* TODO: Make sure a cached sig record here still has
798 the pk that issued it. See also
799 keyedit.c:print_and_check_one_sig */
807 if( !any
) { /* no user id, (maybe a revocation follows)*/
808 /* Check if the pk is really revoked - there could be a
809 0x20 sig packet there even if we are not revoked
810 (say, if a revocation key issued the packet, but the
811 revocation key isn't present to verify it.) */
812 if( sig
->sig_class
== 0x20 && pk
->is_revoked
)
814 else if( sig
->sig_class
== 0x18 )
815 puts("[key binding]");
816 else if( sig
->sig_class
== 0x28 )
817 puts("[subkey revoked]");
821 print_fingerprint( pk
, sk
, 0 );
822 print_card_serialno (sk
);
826 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x28
827 || sig
->sig_class
== 0x30 )
829 else if( (sig
->sig_class
&~3) == 0x10 )
831 else if( sig
->sig_class
== 0x18 )
833 else if( sig
->sig_class
== 0x1F )
837 "[unexpected signature class 0x%02x]\n",sig
->sig_class
);
841 fputs( sigstr
, stdout
);
842 printf("%c%c %c%c%c%c%c%c ",
843 sigrc
,(sig
->sig_class
-0x10>0 &&
844 sig
->sig_class
-0x10<4)?'0'+sig
->sig_class
-0x10:' ',
845 sig
->flags
.exportable
?' ':'L',
846 sig
->flags
.revocable
?' ':'R',
847 sig
->flags
.policy_url
?'P':' ',
848 sig
->flags
.notation
?'N':' ',
849 sig
->flags
.expired
?'X':' ',
850 (sig
->trust_depth
>9)?'T':
851 (sig
->trust_depth
>0)?'0'+sig
->trust_depth
:' ');
852 if(opt
.list_options
&LIST_SHOW_LONG_KEYID
)
853 printf("%08lX%08lX",(ulong
)sig
->keyid
[0],(ulong
)sig
->keyid
[1]);
855 printf("%08lX",(ulong
)sig
->keyid
[1]);
856 printf(" %s ", datestr_from_sig(sig
));
858 printf("[%s] ", gpg_strerror (rc
) );
859 else if( sigrc
== '?' )
861 else if ( !opt
.fast_list_mode
) {
863 char *p
= get_user_id( sig
->keyid
, &n
);
864 print_utf8_string( stdout
, p
, n
);
869 if(sig
->flags
.policy_url
&& (opt
.list_options
&LIST_SHOW_POLICY
))
870 show_policy_url(sig
,3,0);
872 if(sig
->flags
.notation
&& (opt
.list_options
&LIST_SHOW_NOTATION
))
873 show_notation(sig
,3,0);
875 if(sig
->flags
.pref_ks
&& (opt
.list_options
&LIST_SHOW_KEYSERVER
))
876 show_keyserver_url(sig
,3,0);
878 /* fixme: check or list other sigs here */
886 list_keyblock_colon( KBNODE keyblock
, int secret
, int fpr
)
898 /* get the keyid from the keyblock */
899 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
: PKT_PUBLIC_KEY
);
901 log_error("Oops; key lost!\n");
902 dump_kbnode( keyblock
);
908 sk
= node
->pkt
->pkt
.secret_key
;
909 keyid_from_sk( sk
, keyid
);
910 printf("sec::%u:%d:%08lX%08lX:%s:%s:::",
913 (ulong
)keyid
[0],(ulong
)keyid
[1],
914 colon_datestr_from_sk( sk
),
915 colon_strtime (sk
->expiredate
)
916 /* fixme: add LID here */ );
919 pk
= node
->pkt
->pkt
.public_key
;
921 keyid_from_pk( pk
, keyid
);
922 fputs( "pub:", stdout
);
925 else if ( pk
->is_revoked
)
927 else if ( pk
->has_expired
)
929 else if ( opt
.fast_list_mode
|| opt
.no_expensive_trust_checks
)
932 trustletter
= get_validity_info ( pk
, NULL
);
933 if( trustletter
== 'u' )
935 putchar(trustletter
);
937 printf(":%u:%d:%08lX%08lX:%s:%s:",
940 (ulong
)keyid
[0],(ulong
)keyid
[1],
941 colon_datestr_from_pk( pk
),
942 colon_strtime (pk
->expiredate
) );
944 printf("%lu", pk
->local_id
);
946 if( !opt
.fast_list_mode
&& !opt
.no_expensive_trust_checks
)
947 putchar( get_ownertrust_info(pk
) );
951 if (opt
.fixed_list_mode
) {
952 /* do not merge the first uid with the primary key */
955 print_capabilities (pk
, sk
, keyblock
);
958 print_fingerprint( pk
, sk
, 0 );
959 if( opt
.with_key_data
)
960 print_key_data( pk
, keyid
);
965 for( kbctx
=NULL
; (node
=walk_kbnode( keyblock
, &kbctx
, 0)) ; ) {
966 if( node
->pkt
->pkttype
== PKT_USER_ID
&& !opt
.fast_list_mode
) {
967 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
968 if(attrib_fp
&& node
->pkt
->pkt
.user_id
->attrib_data
!=NULL
)
969 dump_attribs(node
->pkt
->pkt
.user_id
,pk
,sk
);
971 * Fixme: We need a is_valid flag here too
975 char *str
=uid
->attrib_data
?"uat":"uid";
976 /* If we're listing a secret key, leave out the
977 validity values for now. FIXME: This should be
978 handled better in 1.9. */
980 printf("%s:::::",str
);
981 else if ( uid
->is_revoked
)
982 printf("%s:r::::",str
);
983 else if ( uid
->is_expired
)
984 printf("%s:e::::",str
);
985 else if ( opt
.no_expensive_trust_checks
)
986 printf("%s:::::",str
);
990 if( pk
&& !ulti_hack
)
991 uid_validity
=get_validity_info (pk
, uid
);
994 printf("%s:%c::::",str
,uid_validity
);
997 printf("%s:",colon_strtime(uid
->created
));
998 printf("%s:",colon_strtime(uid
->expiredate
));
1000 namehash_from_uid(uid
);
1002 for(i
=0; i
< 20; i
++ )
1003 printf("%02X",uid
->namehash
[i
]);
1007 if(uid
->attrib_data
)
1008 printf("%u %lu",uid
->numattribs
,uid
->attrib_len
);
1010 print_string(stdout
,uid
->name
,uid
->len
, ':' );
1016 print_capabilities (pk
, sk
, keyblock
);
1019 print_fingerprint( pk
, sk
, 0 );
1020 if( opt
.with_key_data
)
1021 print_key_data( pk
, keyid
);
1025 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1027 PKT_public_key
*pk2
= node
->pkt
->pkt
.public_key
;
1032 print_capabilities (pk
, sk
, keyblock
);
1035 print_fingerprint( pk
, sk
, 0 ); /* of the main key */
1039 keyid_from_pk( pk2
, keyid2
);
1040 fputs ("sub:", stdout
);
1041 if ( !pk2
->is_valid
)
1043 else if ( pk2
->is_revoked
)
1045 else if ( pk2
->has_expired
)
1047 else if ( opt
.fast_list_mode
|| opt
.no_expensive_trust_checks
)
1050 /* trustletter should always be defined here */
1052 printf("%c", trustletter
);
1054 printf(":%u:%d:%08lX%08lX:%s:%s:",
1055 nbits_from_pk( pk2
),
1057 (ulong
)keyid2
[0],(ulong
)keyid2
[1],
1058 colon_datestr_from_pk( pk2
),
1059 colon_strtime (pk2
->expiredate
)
1060 /* fixme: add LID and ownertrust here */
1062 if( pk
->local_id
) /* use the local_id of the main key??? */
1063 printf("%lu", pk
->local_id
);
1068 print_capabilities (pk2
, NULL
, NULL
);
1071 print_fingerprint( pk2
, NULL
, 0 );
1072 if( opt
.with_key_data
)
1073 print_key_data( pk2
, keyid2
);
1075 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1077 PKT_secret_key
*sk2
= node
->pkt
->pkt
.secret_key
;
1082 print_capabilities (pk
, sk
, keyblock
);
1085 print_fingerprint( pk
, sk
, 0 ); /* of the main key */
1089 keyid_from_sk( sk2
, keyid2
);
1090 printf("ssb::%u:%d:%08lX%08lX:%s:%s:::::",
1091 nbits_from_sk( sk2
),
1093 (ulong
)keyid2
[0],(ulong
)keyid2
[1],
1094 colon_datestr_from_sk( sk2
),
1095 colon_strtime (sk2
->expiredate
)
1096 /* fixme: add LID */ );
1097 print_capabilities (NULL
, sk2
, NULL
);
1100 print_fingerprint( NULL
, sk2
, 0 );
1102 else if( opt
.list_sigs
&& node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1103 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1104 int sigrc
, fprokay
=0;
1107 byte fparray
[MAX_FINGERPRINT_LEN
];
1109 if( !any
) { /* no user id, (maybe a revocation follows)*/
1110 if( sig
->sig_class
== 0x20 )
1111 fputs("[revoked]:", stdout
);
1112 else if( sig
->sig_class
== 0x18 )
1113 fputs("[key binding]:", stdout
);
1114 else if( sig
->sig_class
== 0x28 )
1115 fputs("[subkey revoked]:", stdout
);
1119 print_capabilities (pk
, sk
, keyblock
);
1122 print_fingerprint( pk
, sk
, 0 );
1126 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x28
1127 || sig
->sig_class
== 0x30 )
1129 else if( (sig
->sig_class
&~3) == 0x10 )
1131 else if( sig
->sig_class
== 0x18 )
1133 else if( sig
->sig_class
== 0x1F )
1136 printf ("sig::::::::::%02x%c:\n",
1137 sig
->sig_class
, sig
->flags
.exportable
?'x':'l');
1140 if( opt
.check_sigs
) {
1141 PKT_public_key
*signer_pk
=NULL
;
1144 if(opt
.no_sig_cache
)
1145 signer_pk
= xcalloc (1, sizeof(PKT_public_key
));
1147 rc
= check_key_signature2( keyblock
, node
, NULL
, signer_pk
,
1149 switch( gpg_err_code (rc
) ) {
1150 case 0: sigrc
= '!'; break;
1151 case GPG_ERR_BAD_SIGNATURE
: sigrc
= '-'; break;
1152 case GPG_ERR_NO_PUBKEY
:
1153 case GPG_ERR_UNUSABLE_PUBKEY
: sigrc
= '?'; break;
1154 default: sigrc
= '%'; break;
1157 if(opt
.no_sig_cache
)
1161 fingerprint_from_pk (signer_pk
, fparray
, &fplen
);
1164 free_public_key(signer_pk
);
1171 fputs( sigstr
, stdout
);
1175 printf("::%d:%08lX%08lX:%s:%s:", sig
->pubkey_algo
,
1176 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1177 colon_datestr_from_sig(sig
),
1178 colon_expirestr_from_sig(sig
));
1180 if(sig
->trust_depth
|| sig
->trust_value
)
1181 printf("%d %d",sig
->trust_depth
,sig
->trust_value
);
1184 if(sig
->trust_regexp
)
1185 print_string(stdout
,sig
->trust_regexp
,
1186 strlen(sig
->trust_regexp
),':');
1190 printf("[%s] ", gpg_strerror (rc
) );
1191 else if( sigrc
== '?' )
1193 else if ( !opt
.fast_list_mode
) {
1195 char *p
= get_user_id( sig
->keyid
, &n
);
1196 print_string( stdout
, p
, n
, ':' );
1199 printf(":%02x%c:", sig
->sig_class
,sig
->flags
.exportable
?'x':'l');
1200 if(opt
.no_sig_cache
&& opt
.check_sigs
&& fprokay
)
1206 for (i
=0; i
< fplen
; i
++ )
1207 printf ("%02X", fparray
[i
] );
1213 /* fixme: check or list other sigs here */
1216 if( !any
) {/* oops, no user id */
1219 print_capabilities (pk
, sk
, keyblock
);
1225 * Reorder the keyblock so that the primary user ID (and not attribute
1226 * packet) comes first. Fixme: Replace this by a generic sort
1229 reorder_keyblock (KBNODE keyblock
)
1231 KBNODE primary
= NULL
, primary0
= NULL
, primary2
= NULL
;
1234 for (node
=keyblock
; node
; primary0
=node
, node
= node
->next
) {
1235 if( node
->pkt
->pkttype
== PKT_USER_ID
&&
1236 !node
->pkt
->pkt
.user_id
->attrib_data
&&
1237 node
->pkt
->pkt
.user_id
->is_primary
) {
1238 primary
= primary2
= node
;
1239 for (node
=node
->next
; node
; primary2
=node
, node
= node
->next
) {
1240 if( node
->pkt
->pkttype
== PKT_USER_ID
1241 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1242 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1250 return; /* no primary key flag found (should not happen) */
1252 for (last
=NULL
, node
=keyblock
; node
; last
= node
, node
= node
->next
) {
1253 if( node
->pkt
->pkttype
== PKT_USER_ID
)
1257 assert (last
); /* the user ID is never the first packet */
1258 assert (primary0
); /* ditto (this is the node before primary) */
1259 if ( node
== primary
)
1260 return; /* already the first one */
1262 last
->next
= primary
;
1263 primary0
->next
= primary2
->next
;
1264 primary2
->next
= node
;
1268 list_keyblock( KBNODE keyblock
, int secret
, int fpr
, void *opaque
)
1270 reorder_keyblock (keyblock
);
1271 if (opt
.with_colons
)
1272 list_keyblock_colon (keyblock
, secret
, fpr
);
1274 list_keyblock_print (keyblock
, secret
, fpr
, opaque
);
1278 * standard function to print the finperprint.
1279 * mode 0: as used in key listings, opt.with_colons is honored
1280 * 1: print using log_info ()
1281 * 2: direct use of tty
1282 * 3: direct use of tty but only primary key.
1283 * modes 1 and 2 will try and print both subkey and primary key fingerprints
1286 print_fingerprint (PKT_public_key
*pk
, PKT_secret_key
*sk
, int mode
)
1288 byte array
[MAX_FINGERPRINT_LEN
], *p
;
1296 if(sk
->main_keyid
[0]==sk
->keyid
[0] && sk
->main_keyid
[1]==sk
->keyid
[1])
1301 if(pk
->main_keyid
[0]==pk
->keyid
[0] && pk
->main_keyid
[1]==pk
->keyid
[1])
1305 /* Just to be safe */
1306 if(mode
&0x80 && !primary
)
1308 log_error("primary key is not really primary!\n");
1314 if(!primary
&& (mode
==1 || mode
==2))
1318 PKT_secret_key
*primary_sk
=xcalloc (1,sizeof(*primary_sk
));
1319 get_seckey(primary_sk
,sk
->main_keyid
);
1320 print_fingerprint(NULL
,primary_sk
,mode
|0x80);
1321 free_secret_key(primary_sk
);
1325 PKT_public_key
*primary_pk
=xcalloc (1,sizeof(*primary_pk
));
1326 get_pubkey(primary_pk
,pk
->main_keyid
);
1327 print_fingerprint(primary_pk
,NULL
,mode
|0x80);
1328 free_public_key(primary_pk
);
1333 fp
= log_get_stream ();
1335 text
= _("Primary key fingerprint:");
1337 text
= _(" Subkey fingerprint:");
1339 else if (mode
== 2) {
1340 fp
= NULL
; /* use tty */
1341 /* Translators: this should fit into 24 bytes to that the fingerprint
1342 * data is properly aligned with the user ID */
1344 text
= _(" Primary key fingerprint:");
1346 text
= _(" Subkey fingerprint:");
1348 else if (mode
== 3) {
1349 fp
= NULL
; /* use tty */
1350 text
= _(" Key fingerprint =");
1354 text
= _(" Key fingerprint =");
1358 fingerprint_from_sk (sk
, array
, &n
);
1360 fingerprint_from_pk (pk
, array
, &n
);
1362 if (opt
.with_colons
&& !mode
) {
1363 fprintf (fp
, "fpr:::::::::");
1364 for (i
=0; i
< n
; i
++, p
++ )
1365 fprintf (fp
, "%02X", *p
);
1372 tty_printf ("%s", text
);
1374 for (i
=0; i
< n
; i
++, i
++, p
+= 2 ) {
1378 fprintf (fp
, " %02X%02X", *p
, p
[1] );
1383 tty_printf (" %02X%02X", *p
, p
[1]);
1388 for (i
=0; i
< n
; i
++, p
++ ) {
1392 fprintf (fp
, " %02X", *p
);
1397 tty_printf (" %02X", *p
);
1409 /* Print the serial number of an OpenPGP card if available. */
1411 print_card_serialno (PKT_secret_key
*sk
)
1417 if (!sk
->is_protected
|| sk
->protect
.s2k
.mode
!= 1002)
1418 return; /* Not a card. */
1419 if (opt
.with_colons
)
1420 return; /* Format not yet defined. */
1422 fputs (_(" Card serial no. ="), stdout
);
1424 if (sk
->protect
.ivlen
== 16
1425 && !memcmp (sk
->protect
.iv
, "\xD2\x76\x00\x01\x24\x01", 6) )
1426 { /* This is an OpenPGP card. Just print the relevant part. */
1427 for (i
=8; i
< 14; i
++)
1431 printf ("%02X", sk
->protect
.iv
[i
]);
1435 { /* Something is wrong: Print all. */
1436 for (i
=0; i
< sk
->protect
.ivlen
; i
++)
1437 printf ("%02X", sk
->protect
.iv
[i
]);
1442 void set_attrib_fd(int fd
)
1444 static int last_fd
=-1;
1446 if ( fd
!= -1 && last_fd
== fd
)
1449 if ( attrib_fp
&& attrib_fp
!= stdout
&& attrib_fp
!= stderr
)
1460 attrib_fp
= fdopen( fd
, "w" );
1462 log_fatal("can't open fd %d for attribute output: %s\n",
1463 fd
, strerror(errno
));