1 /* keylist.c - print keys
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2008 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/>.
27 #ifdef HAVE_DOSISH_SYSTEM
28 #include <fcntl.h> /* for setmode() */
44 static void list_all(int);
45 static void list_one( strlist_t names
, int secret
);
46 static void locate_one (strlist_t names
);
47 static void print_card_serialno (PKT_secret_key
*sk
);
56 /* The stream used to write attribute packets to. */
57 static FILE *attrib_fp
= NULL
;
61 * If list is NULL, all available keys are listed
64 public_key_list( strlist_t list
, int locate_mode
)
68 byte trust_model
,marginals
,completes
,cert_depth
;
69 ulong created
,nextcheck
;
71 read_trust_options(&trust_model
,&created
,&nextcheck
,
72 &marginals
,&completes
,&cert_depth
);
76 if(nextcheck
&& nextcheck
<= make_timestamp())
78 if(trust_model
!=opt
.trust_model
)
80 if(opt
.trust_model
==TM_PGP
|| opt
.trust_model
==TM_CLASSIC
)
82 if(marginals
!=opt
.marginals_needed
)
84 if(completes
!=opt
.completes_needed
)
86 if(cert_depth
!=opt
.max_cert_depth
)
90 printf(":%d:%lu:%lu",trust_model
,created
,nextcheck
);
92 /* Only show marginals, completes, and cert_depth in the classic
93 or PGP trust models since they are not meaningful
96 if(trust_model
==TM_PGP
|| trust_model
==TM_CLASSIC
)
97 printf(":%d:%d:%d",marginals
,completes
,cert_depth
);
102 /* We need to do the stale check right here because it might need to
103 update the keyring while we already have the keyring open. This
104 is very bad for W32 because of a sharing violation. For real OSes
105 it might lead to false results if we are later listing a keyring
106 which is associated with the inode of a deleted file. */
107 check_trustdb_stale ();
119 secret_key_list( strlist_t list
)
121 check_trustdb_stale ();
125 else /* List by user id */
130 print_seckey_info (PKT_secret_key
*sk
)
135 keyid_from_sk (sk
, keyid
);
136 p
=get_user_id_native(keyid
);
138 tty_printf ("\nsec %4u%c/%s %s %s\n",
140 pubkey_letter (sk
->pubkey_algo
),
141 keystr(keyid
), datestr_from_sk (sk
), p
);
146 /* Print information about the public key. With FP passed as NULL,
147 the tty output interface is used, otherwise output is directted to
150 print_pubkey_info (FILE *fp
, PKT_public_key
*pk
)
155 keyid_from_pk (pk
, keyid
);
157 /* If the pk was chosen by a particular user ID, that is the one to
160 p
=utf8_to_native(pk
->user_id
->name
,pk
->user_id
->len
,0);
162 p
=get_user_id_native(keyid
);
165 fprintf (fp
, "pub %4u%c/%s %s %s\n",
167 pubkey_letter (pk
->pubkey_algo
),
168 keystr(keyid
), datestr_from_pk (pk
), p
);
170 tty_printf ("\npub %4u%c/%s %s %s\n",
171 nbits_from_pk (pk
), pubkey_letter (pk
->pubkey_algo
),
172 keystr(keyid
), datestr_from_pk (pk
), p
);
178 /* Print basic information of a secret key including the card serial
179 number information. */
181 print_card_key_info (FILE *fp
, KBNODE keyblock
)
186 for (node
= keyblock
; node
; node
= node
->next
)
188 if (node
->pkt
->pkttype
== PKT_SECRET_KEY
189 || (node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) )
191 PKT_secret_key
*sk
= node
->pkt
->pkt
.secret_key
;
193 tty_fprintf (fp
, "%s%c %4u%c/%s ",
194 node
->pkt
->pkttype
== PKT_SECRET_KEY
? "sec":"ssb",
195 (sk
->protect
.s2k
.mode
==1001)?'#':
196 (sk
->protect
.s2k
.mode
==1002)?'>':' ',
198 pubkey_letter (sk
->pubkey_algo
),
200 tty_fprintf (fp
, _("created: %s"), datestr_from_sk (sk
));
201 tty_fprintf (fp
, " ");
202 tty_fprintf (fp
, _("expires: %s"), expirestr_from_sk (sk
));
203 if (sk
->is_protected
&& sk
->protect
.s2k
.mode
== 1002)
205 tty_fprintf (fp
, "\n ");
206 tty_fprintf (fp
, _("card-no: "));
207 if (sk
->protect
.ivlen
== 16
208 && !memcmp (sk
->protect
.iv
, "\xD2\x76\x00\x01\x24\x01", 6))
210 /* This is an OpenPGP card. */
211 for (i
=8; i
< 14; i
++)
214 tty_fprintf (fp
, " ");
215 tty_fprintf (fp
, "%02X", sk
->protect
.iv
[i
]);
219 { /* Something is wrong: Print all. */
220 for (i
=0; i
< sk
->protect
.ivlen
; i
++)
221 tty_fprintf (fp
, "%02X", sk
->protect
.iv
[i
]);
224 tty_fprintf (fp
, "\n");
231 /* Flags = 0x01 hashed 0x02 critical */
233 status_one_subpacket(sigsubpkttype_t type
,size_t len
,int flags
,const byte
*buf
)
237 /* Don't print these. */
241 sprintf(status
,"%d %u %u ",type
,flags
,(unsigned int)len
);
243 write_status_text_and_buffer(STATUS_SIG_SUBPACKET
,status
,buf
,len
,0);
248 mode=1 for log_info + status messages
249 mode=2 for status messages only
253 show_policy_url(PKT_signature
*sig
,int indent
,int mode
)
258 FILE *fp
=mode
?log_get_stream():stdout
;
260 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_POLICY
,&len
,&seq
,&crit
)))
267 for(i
=0;i
<indent
;i
++)
271 str
=_("Critical signature policy: ");
273 str
=_("Signature policy: ");
278 print_utf8_string(fp
,p
,len
);
283 write_status_buffer ( STATUS_POLICY_URL
, p
, len
, 0 );
289 mode=1 for log_info + status messages
290 mode=2 for status messages only
294 show_keyserver_url(PKT_signature
*sig
,int indent
,int mode
)
299 FILE *fp
=mode
?log_get_stream():stdout
;
301 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&len
,&seq
,&crit
)))
308 for(i
=0;i
<indent
;i
++)
312 str
=_("Critical preferred keyserver: ");
314 str
=_("Preferred keyserver: ");
319 print_utf8_string(fp
,p
,len
);
324 status_one_subpacket(SIGSUBPKT_PREF_KS
,len
,(crit
?0x02:0)|0x01,p
);
330 mode=1 for log_info + status messages
331 mode=2 for status messages only
334 1 == standard notations
339 show_notation(PKT_signature
*sig
,int indent
,int mode
,int which
)
341 FILE *fp
=mode
?log_get_stream():stdout
;
342 struct notation
*nd
,*notations
;
347 notations
=sig_to_notation(sig
);
349 /* There may be multiple notations in the same sig. */
350 for(nd
=notations
;nd
;nd
=nd
->next
)
354 int has_at
=!!strchr(nd
->name
,'@');
356 if((which
&1 && !has_at
) || (which
&2 && has_at
))
361 for(i
=0;i
<indent
;i
++)
364 if(nd
->flags
.critical
)
365 str
=_("Critical signature notation: ");
367 str
=_("Signature notation: ");
372 /* This is all UTF8 */
373 print_utf8_string(fp
,nd
->name
,strlen(nd
->name
));
375 print_utf8_string(fp
,nd
->value
,strlen(nd
->value
));
382 write_status_buffer(STATUS_NOTATION_NAME
,
383 nd
->name
,strlen(nd
->name
),0);
384 write_status_buffer(STATUS_NOTATION_DATA
,
385 nd
->value
,strlen(nd
->value
),50);
389 free_notation(notations
);
393 print_signature_stats(struct sig_stats
*s
)
395 if( s
->inv_sigs
== 1 )
396 tty_printf(_("1 bad signature\n") );
397 else if( s
->inv_sigs
)
398 tty_printf(_("%d bad signatures\n"), s
->inv_sigs
);
400 tty_printf(_("1 signature not checked due to a missing key\n") );
402 tty_printf(_("%d signatures not checked due to missing keys\n"),s
->no_key
);
403 if( s
->oth_err
== 1 )
404 tty_printf(_("1 signature not checked due to an error\n") );
405 else if( s
->oth_err
)
406 tty_printf(_("%d signatures not checked due to errors\n"), s
->oth_err
);
410 list_all( int secret
)
413 KBNODE keyblock
= NULL
;
415 const char *lastresname
, *resname
;
416 struct sig_stats stats
;
418 memset(&stats
,0,sizeof(stats
));
420 hd
= keydb_new (secret
);
424 rc
= keydb_search_first (hd
);
427 log_error("keydb_search_first failed: %s\n", g10_errstr(rc
) );
433 rc
= keydb_get_keyblock (hd
, &keyblock
);
435 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
440 resname
= keydb_get_resource_name (hd
);
441 if (lastresname
!= resname
)
445 printf("%s\n", resname
);
446 for(i
=strlen(resname
); i
; i
-- )
449 lastresname
= resname
;
452 merge_keys_and_selfsig( keyblock
);
453 list_keyblock( keyblock
, secret
, opt
.fingerprint
,
454 opt
.check_sigs
?&stats
:NULL
);
455 release_kbnode( keyblock
);
457 } while (!(rc
= keydb_search_next (hd
)));
459 log_error ("keydb_search_next failed: %s\n", g10_errstr(rc
));
461 if(opt
.check_sigs
&& !opt
.with_colons
)
462 print_signature_stats(&stats
);
465 release_kbnode (keyblock
);
471 list_one( strlist_t names
, int secret
)
474 KBNODE keyblock
= NULL
;
477 const char *keyring_str
= _("Keyring");
479 struct sig_stats stats
;
481 memset(&stats
,0,sizeof(stats
));
483 /* fixme: using the bynames function has the disadvantage that we
484 * don't know wether one of the names given was not found. OTOH,
485 * this function has the advantage to list the names in the
486 * sequence as defined by the keyDB and does not duplicate
487 * outputs. A solution could be do test whether all given have
488 * been listed (this needs a way to use the keyDB search
489 * functions) or to have the search function return indicators for
490 * found names. Yet another way is to use the keydb search
491 * facilities directly. */
493 rc
= get_seckey_bynames( &ctx
, NULL
, names
, &keyblock
);
495 log_error("error reading key: %s\n", g10_errstr(rc
) );
496 get_seckey_end( ctx
);
500 if ((opt
.list_options
&LIST_SHOW_KEYRING
) && !opt
.with_colons
) {
501 resname
= keydb_get_resource_name (get_ctx_handle(ctx
));
502 printf("%s: %s\n", keyring_str
, resname
);
503 for(i
= strlen(resname
) + strlen(keyring_str
) + 2; i
; i
-- )
507 list_keyblock( keyblock
, 1, opt
.fingerprint
, NULL
);
508 release_kbnode( keyblock
);
509 } while( !get_seckey_next( ctx
, NULL
, &keyblock
) );
510 get_seckey_end( ctx
);
513 rc
= get_pubkey_bynames( &ctx
, NULL
, names
, &keyblock
);
515 log_error("error reading key: %s\n", g10_errstr(rc
) );
516 get_pubkey_end( ctx
);
520 if ((opt
.list_options
&LIST_SHOW_KEYRING
) && !opt
.with_colons
) {
521 resname
= keydb_get_resource_name (get_ctx_handle(ctx
));
522 printf("%s: %s\n", keyring_str
, resname
);
523 for(i
= strlen(resname
) + strlen(keyring_str
) + 2; i
; i
-- )
527 list_keyblock( keyblock
, 0, opt
.fingerprint
,
528 opt
.check_sigs
?&stats
:NULL
);
529 release_kbnode( keyblock
);
530 } while( !get_pubkey_next( ctx
, NULL
, &keyblock
) );
531 get_pubkey_end( ctx
);
534 if(opt
.check_sigs
&& !opt
.with_colons
)
535 print_signature_stats(&stats
);
540 locate_one (strlist_t names
)
544 GETKEY_CTX ctx
= NULL
;
545 KBNODE keyblock
= NULL
;
546 struct sig_stats stats
;
548 memset (&stats
,0,sizeof(stats
));
550 for (sl
=names
; sl
; sl
= sl
->next
)
552 rc
= get_pubkey_byname (&ctx
, NULL
, sl
->d
, &keyblock
, NULL
, 1, 0);
555 if (gpg_err_code (rc
) != GPG_ERR_NO_PUBKEY
)
556 log_error ("error reading key: %s\n", g10_errstr(rc
) );
562 list_keyblock (keyblock
, 0, opt
.fingerprint
,
563 opt
.check_sigs
? &stats
: NULL
);
564 release_kbnode (keyblock
);
566 while ( ctx
&& !get_pubkey_next (ctx
, NULL
, &keyblock
));
567 get_pubkey_end (ctx
);
572 if (opt
.check_sigs
&& !opt
.with_colons
)
573 print_signature_stats (&stats
);
578 print_key_data( PKT_public_key
*pk
)
580 int n
= pk
? pubkey_get_npkey( pk
->pubkey_algo
) : 0;
583 for(i
=0; i
< n
; i
++ ) {
584 printf("pkd:%d:%u:", i
, mpi_get_nbits( pk
->pkey
[i
] ) );
585 mpi_print(stdout
, pk
->pkey
[i
], 1 );
592 print_capabilities (PKT_public_key
*pk
, PKT_secret_key
*sk
, KBNODE keyblock
)
594 if(pk
|| (sk
&& sk
->protect
.s2k
.mode
!=1001))
596 unsigned int use
= pk
? pk
->pubkey_usage
: sk
->pubkey_usage
;
598 if ( use
& PUBKEY_USAGE_ENC
)
601 if ( use
& PUBKEY_USAGE_SIG
)
604 if( pk
? pk
->is_primary
: sk
->is_primary
)
608 if ( (use
& PUBKEY_USAGE_AUTH
) )
612 if ( keyblock
) { /* figure out the usable capabilities */
614 int enc
=0, sign
=0, cert
=0, auth
=0, disabled
=0;
616 for (k
=keyblock
; k
; k
= k
->next
) {
617 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
618 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
619 pk
= k
->pkt
->pkt
.public_key
;
622 disabled
=pk_is_disabled(pk
);
624 if ( pk
->is_valid
&& !pk
->is_revoked
&& !pk
->has_expired
) {
625 if ( pk
->pubkey_usage
& PUBKEY_USAGE_ENC
)
627 if ( pk
->pubkey_usage
& PUBKEY_USAGE_SIG
)
633 if ( (pk
->pubkey_usage
& PUBKEY_USAGE_AUTH
) )
637 else if ( k
->pkt
->pkttype
== PKT_SECRET_KEY
638 || k
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
639 sk
= k
->pkt
->pkt
.secret_key
;
640 if ( sk
->is_valid
&& !sk
->is_revoked
&& !sk
->has_expired
641 && sk
->protect
.s2k
.mode
!=1001 ) {
642 if ( sk
->pubkey_usage
& PUBKEY_USAGE_ENC
)
644 if ( sk
->pubkey_usage
& PUBKEY_USAGE_SIG
)
650 if ( (sk
->pubkey_usage
& PUBKEY_USAGE_AUTH
) )
670 /* Flags = 0x01 hashed 0x02 critical */
672 print_one_subpacket(sigsubpkttype_t type
,size_t len
,int flags
,const byte
*buf
)
676 printf("spk:%d:%u:%u:",type
,flags
,(unsigned int)len
);
680 /* printable ascii other than : and % */
681 if(buf
[i
]>=32 && buf
[i
]<=126 && buf
[i
]!=':' && buf
[i
]!='%')
684 printf("%%%02X",buf
[i
]);
691 print_subpackets_colon(PKT_signature
*sig
)
695 assert(opt
.show_subpackets
);
697 for(i
=opt
.show_subpackets
;*i
;i
++)
705 while((p
=enum_sig_subpkt(sig
->hashed
,*i
,&len
,&seq
,&crit
)))
706 print_one_subpacket(*i
,len
,0x01|(crit
?0x02:0),p
);
710 while((p
=enum_sig_subpkt(sig
->unhashed
,*i
,&len
,&seq
,&crit
)))
711 print_one_subpacket(*i
,len
,0x00|(crit
?0x02:0),p
);
716 dump_attribs(const PKT_user_id
*uid
,PKT_public_key
*pk
,PKT_secret_key
*sk
)
723 for(i
=0;i
<uid
->numattribs
;i
++)
725 if(is_status_enabled())
727 byte array
[MAX_FINGERPRINT_LEN
], *p
;
728 char buf
[(MAX_FINGERPRINT_LEN
*2)+90];
732 fingerprint_from_pk( pk
, array
, &n
);
734 fingerprint_from_sk( sk
, array
, &n
);
739 for(j
=0; j
< n
; j
++, p
++ )
740 sprintf(buf
+2*j
, "%02X", *p
);
742 sprintf(buf
+strlen(buf
)," %lu %u %u %u %lu %lu %u",
743 (ulong
)uid
->attribs
[i
].len
,uid
->attribs
[i
].type
,i
+1,
744 uid
->numattribs
,(ulong
)uid
->created
,(ulong
)uid
->expiredate
,
745 ((uid
->is_primary
?0x01:0)|
746 (uid
->is_revoked
?0x02:0)|
747 (uid
->is_expired
?0x04:0)));
748 write_status_text(STATUS_ATTRIBUTE
,buf
);
751 fwrite(uid
->attribs
[i
].data
,uid
->attribs
[i
].len
,1,attrib_fp
);
757 list_keyblock_print ( KBNODE keyblock
, int secret
, int fpr
, void *opaque
)
764 struct sig_stats
*stats
=opaque
;
767 /* get the keyid from the keyblock */
768 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
: PKT_PUBLIC_KEY
);
770 log_error("Oops; key lost!\n");
771 dump_kbnode( keyblock
);
778 sk
= node
->pkt
->pkt
.secret_key
;
780 printf("sec%c %4u%c/%s %s",(sk
->protect
.s2k
.mode
==1001)?'#':
781 (sk
->protect
.s2k
.mode
==1002)?'>':' ',
782 nbits_from_sk( sk
),pubkey_letter( sk
->pubkey_algo
),
783 keystr_from_sk(sk
),datestr_from_sk( sk
));
788 printf(_("expired: %s"),expirestr_from_sk(sk
));
791 else if(sk
->expiredate
)
794 printf(_("expires: %s"),expirestr_from_sk(sk
));
802 pk
= node
->pkt
->pkt
.public_key
;
805 check_trustdb_stale();
807 printf("pub %4u%c/%s %s",
808 nbits_from_pk(pk
),pubkey_letter(pk
->pubkey_algo
),
809 keystr_from_pk(pk
),datestr_from_pk( pk
));
811 /* We didn't include this before in the key listing, but there
812 is room in the new format, so why not? */
817 printf(_("revoked: %s"),revokestr_from_pk(pk
));
820 else if(pk
->has_expired
)
823 printf(_("expired: %s"),expirestr_from_pk(pk
));
826 else if(pk
->expiredate
)
829 printf(_("expires: %s"),expirestr_from_pk(pk
));
834 /* I need to think about this some more. It's easy enough to
835 include, but it looks sort of confusing in the
837 if(opt
.list_options
&LIST_SHOW_VALIDITY
)
839 int validity
=get_validity(pk
,NULL
);
840 printf(" [%s]",trust_value_to_string(validity
));
848 print_fingerprint( pk
, sk
, 0 );
849 print_card_serialno (sk
);
850 if( opt
.with_key_data
)
851 print_key_data( pk
);
853 for( kbctx
=NULL
; (node
=walk_kbnode( keyblock
, &kbctx
, 0)) ; ) {
854 if( node
->pkt
->pkttype
== PKT_USER_ID
&& !opt
.fast_list_mode
) {
855 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
857 if(pk
&& (uid
->is_expired
|| uid
->is_revoked
)
858 && !(opt
.list_options
&LIST_SHOW_UNUSABLE_UIDS
))
866 if(attrib_fp
&& uid
->attrib_data
!=NULL
)
867 dump_attribs(uid
,pk
,sk
);
869 if((uid
->is_revoked
|| uid
->is_expired
)
870 || ((opt
.list_options
&LIST_SHOW_UID_VALIDITY
) && pk
))
872 const char *validity
;
875 validity
=uid_trust_string_fixed(pk
,uid
);
876 indent
=(keystrlen()+9)-atoi(uid_trust_string_fixed(NULL
,NULL
));
878 if(indent
<0 || indent
>40)
881 printf("uid%*s%s ",indent
,"",validity
);
884 printf("uid%*s", (int)keystrlen()+10,"");
886 print_utf8_string( stdout
, uid
->name
, uid
->len
);
889 if((opt
.list_options
&LIST_SHOW_PHOTOS
) && uid
->attribs
!=NULL
)
890 show_photos(uid
->attribs
,uid
->numattribs
,pk
,sk
,uid
);
892 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
)
894 PKT_public_key
*pk2
= node
->pkt
->pkt
.public_key
;
896 if((pk2
->is_revoked
|| pk2
->has_expired
)
897 && !(opt
.list_options
&LIST_SHOW_UNUSABLE_SUBKEYS
))
905 printf("sub %4u%c/%s %s",
906 nbits_from_pk( pk2
),pubkey_letter( pk2
->pubkey_algo
),
907 keystr_from_pk(pk2
),datestr_from_pk(pk2
));
908 if( pk2
->is_revoked
)
911 printf(_("revoked: %s"),revokestr_from_pk(pk2
));
914 else if( pk2
->has_expired
)
917 printf(_("expired: %s"),expirestr_from_pk(pk2
));
920 else if( pk2
->expiredate
)
923 printf(_("expires: %s"),expirestr_from_pk(pk2
));
928 print_fingerprint( pk2
, NULL
, 0 );
929 if( opt
.with_key_data
)
930 print_key_data( pk2
);
932 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
)
934 PKT_secret_key
*sk2
= node
->pkt
->pkt
.secret_key
;
936 printf("ssb%c %4u%c/%s %s",
937 (sk2
->protect
.s2k
.mode
==1001)?'#':
938 (sk2
->protect
.s2k
.mode
==1002)?'>':' ',
939 nbits_from_sk( sk2
),pubkey_letter( sk2
->pubkey_algo
),
940 keystr_from_sk(sk2
),datestr_from_sk( sk2
) );
941 if( sk2
->expiredate
)
944 printf(_("expires: %s"),expirestr_from_sk(sk2
));
950 print_fingerprint( NULL
, sk2
, 0 );
951 print_card_serialno (sk2
);
954 else if( opt
.list_sigs
955 && node
->pkt
->pkttype
== PKT_SIGNATURE
957 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
963 rc
= check_key_signature( keyblock
, node
, NULL
);
964 switch( gpg_err_code (rc
) ) {
965 case 0: sigrc
= '!'; break;
966 case GPG_ERR_BAD_SIGNATURE
:
967 stats
->inv_sigs
++; sigrc
= '-'; break;
968 case GPG_ERR_NO_PUBKEY
:
969 case GPG_ERR_UNUSABLE_PUBKEY
: stats
->no_key
++; continue;
970 default: stats
->oth_err
++; sigrc
= '%'; break;
973 /* TODO: Make sure a cached sig record here still has
974 the pk that issued it. See also
975 keyedit.c:print_and_check_one_sig */
982 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x28
983 || sig
->sig_class
== 0x30 )
985 else if( (sig
->sig_class
&~3) == 0x10 )
987 else if( sig
->sig_class
== 0x18 )
989 else if( sig
->sig_class
== 0x1F )
993 "[unexpected signature class 0x%02x]\n",sig
->sig_class
);
997 fputs( sigstr
, stdout
);
998 printf("%c%c %c%c%c%c%c%c %s %s",
999 sigrc
,(sig
->sig_class
-0x10>0 &&
1000 sig
->sig_class
-0x10<4)?'0'+sig
->sig_class
-0x10:' ',
1001 sig
->flags
.exportable
?' ':'L',
1002 sig
->flags
.revocable
?' ':'R',
1003 sig
->flags
.policy_url
?'P':' ',
1004 sig
->flags
.notation
?'N':' ',
1005 sig
->flags
.expired
?'X':' ',
1006 (sig
->trust_depth
>9)?'T':
1007 (sig
->trust_depth
>0)?'0'+sig
->trust_depth
:' ',
1008 keystr(sig
->keyid
),datestr_from_sig(sig
));
1009 if(opt
.list_options
&LIST_SHOW_SIG_EXPIRE
)
1010 printf(" %s", expirestr_from_sig(sig
));
1013 printf("[%s] ", g10_errstr(rc
) );
1014 else if( sigrc
== '?' )
1016 else if ( !opt
.fast_list_mode
) {
1018 char *p
= get_user_id( sig
->keyid
, &n
);
1019 print_utf8_string( stdout
, p
, n
);
1024 if(sig
->flags
.policy_url
1025 && (opt
.list_options
&LIST_SHOW_POLICY_URLS
))
1026 show_policy_url(sig
,3,0);
1028 if(sig
->flags
.notation
&& (opt
.list_options
&LIST_SHOW_NOTATIONS
))
1029 show_notation(sig
,3,0,
1030 ((opt
.list_options
&LIST_SHOW_STD_NOTATIONS
)?1:0)+
1031 ((opt
.list_options
&LIST_SHOW_USER_NOTATIONS
)?2:0));
1033 if(sig
->flags
.pref_ks
1034 && (opt
.list_options
&LIST_SHOW_KEYSERVER_URLS
))
1035 show_keyserver_url(sig
,3,0);
1037 /* fixme: check or list other sigs here */
1044 print_revokers(PKT_public_key
*pk
)
1046 /* print the revoker record */
1047 if( !pk
->revkey
&& pk
->numrevkeys
)
1053 for (i
=0; i
< pk
->numrevkeys
; i
++)
1057 printf ("rvk:::%d::::::", pk
->revkey
[i
].algid
);
1058 p
= pk
->revkey
[i
].fpr
;
1059 for (j
=0; j
< 20; j
++, p
++ )
1060 printf ("%02X", *p
);
1061 printf (":%02x%s:\n", pk
->revkey
[i
].class,
1062 (pk
->revkey
[i
].class&0x40)?"s":"");
1068 list_keyblock_colon( KBNODE keyblock
, int secret
, int fpr
)
1076 int trustletter
= 0;
1080 /* get the keyid from the keyblock */
1081 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
: PKT_PUBLIC_KEY
);
1084 log_error("Oops; key lost!\n");
1085 dump_kbnode( keyblock
);
1092 sk
= node
->pkt
->pkt
.secret_key
;
1093 keyid_from_sk ( sk
, keyid
);
1094 printf ("sec::%u:%d:%08lX%08lX:%s:%s:::",
1095 nbits_from_sk( sk
),
1097 (ulong
)keyid
[0],(ulong
)keyid
[1],
1098 colon_datestr_from_sk( sk
),
1099 colon_strtime (sk
->expiredate
)
1100 /* fixme: add LID here */ );
1104 pk
= node
->pkt
->pkt
.public_key
;
1106 keyid_from_pk( pk
, keyid
);
1107 fputs( "pub:", stdout
);
1108 if ( !pk
->is_valid
)
1110 else if ( pk
->is_revoked
)
1112 else if ( pk
->has_expired
)
1114 else if ( opt
.fast_list_mode
|| opt
.no_expensive_trust_checks
)
1118 trustletter
= get_validity_info ( pk
, NULL
);
1119 if ( trustletter
== 'u' )
1121 putchar(trustletter
);
1123 printf (":%u:%d:%08lX%08lX:%s:%s::",
1124 nbits_from_pk( pk
),
1126 (ulong
)keyid
[0],(ulong
)keyid
[1],
1127 colon_datestr_from_pk( pk
),
1128 colon_strtime (pk
->expiredate
) );
1129 if ( !opt
.fast_list_mode
&& !opt
.no_expensive_trust_checks
)
1130 putchar( get_ownertrust_info(pk
) );
1136 print_capabilities (pk
, sk
, keyblock
);
1139 putchar (':'); /* End of field 13. */
1140 putchar (':'); /* End of field 14. */
1141 if (sk
->protect
.s2k
.mode
== 1001)
1142 putchar ('#'); /* Key is just a stub. */
1143 else if (sk
->protect
.s2k
.mode
== 1002)
1145 /* Key is stored on an external token (card) or handled by
1146 the gpg-agent. Print the serial number of that token
1148 for (i
=0; i
< sk
->protect
.ivlen
; i
++)
1149 printf ("%02X", sk
->protect
.iv
[i
]);
1151 putchar (':'); /* End of field 15. */
1155 print_revokers (pk
);
1157 print_fingerprint (pk
, sk
, 0);
1158 if (opt
.with_key_data
)
1159 print_key_data (pk
);
1162 for ( kbctx
=NULL
; (node
=walk_kbnode( keyblock
, &kbctx
, 0)) ; )
1164 if ( node
->pkt
->pkttype
== PKT_USER_ID
&& !opt
.fast_list_mode
)
1167 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
1169 if (attrib_fp
&& node
->pkt
->pkt
.user_id
->attrib_data
!= NULL
)
1170 dump_attribs (node
->pkt
->pkt
.user_id
,pk
,sk
);
1172 * Fixme: We need a is_valid flag here too
1174 str
= uid
->attrib_data
? "uat":"uid";
1175 /* If we're listing a secret key, leave out the validity
1176 values for now. This is handled better in 1.9. */
1178 printf ("%s:::::",str
);
1179 else if ( uid
->is_revoked
)
1180 printf ("%s:r::::",str
);
1181 else if ( uid
->is_expired
)
1182 printf ("%s:e::::",str
);
1183 else if ( opt
.no_expensive_trust_checks
)
1184 printf ("%s:::::",str
);
1189 if ( pk
&& !ulti_hack
)
1190 uid_validity
=get_validity_info (pk
, uid
);
1193 printf ("%s:%c::::",str
,uid_validity
);
1196 printf ("%s:", colon_strtime (uid
->created
));
1197 printf ("%s:", colon_strtime (uid
->expiredate
));
1199 namehash_from_uid (uid
);
1201 for (i
=0; i
< 20; i
++ )
1202 printf ("%02X",uid
->namehash
[i
]);
1206 if (uid
->attrib_data
)
1207 printf ("%u %lu",uid
->numattribs
,uid
->attrib_len
);
1209 print_string (stdout
,uid
->name
,uid
->len
, ':' );
1213 else if ( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
)
1216 PKT_public_key
*pk2
= node
->pkt
->pkt
.public_key
;
1218 keyid_from_pk ( pk2
, keyid2
);
1219 fputs ("sub:", stdout
);
1220 if ( !pk2
->is_valid
)
1222 else if ( pk2
->is_revoked
)
1224 else if ( pk2
->has_expired
)
1226 else if ( opt
.fast_list_mode
|| opt
.no_expensive_trust_checks
)
1230 /* TRUSTLETTER should always be defined here. */
1232 printf ("%c", trustletter
);
1234 printf(":%u:%d:%08lX%08lX:%s:%s:::::",
1235 nbits_from_pk( pk2
),
1237 (ulong
)keyid2
[0],(ulong
)keyid2
[1],
1238 colon_datestr_from_pk( pk2
),
1239 colon_strtime (pk2
->expiredate
)
1240 /* fixme: add LID and ownertrust here */
1242 print_capabilities (pk2
, NULL
, NULL
);
1245 print_fingerprint ( pk2
, NULL
, 0 );
1246 if ( opt
.with_key_data
)
1247 print_key_data( pk2
);
1249 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
)
1252 PKT_secret_key
*sk2
= node
->pkt
->pkt
.secret_key
;
1254 keyid_from_sk ( sk2
, keyid2
);
1255 printf ("ssb::%u:%d:%08lX%08lX:%s:%s:::::",
1256 nbits_from_sk( sk2
),
1258 (ulong
)keyid2
[0],(ulong
)keyid2
[1],
1259 colon_datestr_from_sk( sk2
),
1260 colon_strtime (sk2
->expiredate
)
1261 /* fixme: add LID */ );
1262 print_capabilities (NULL
, sk2
, NULL
);
1263 putchar(':'); /* End of field 13. */
1264 putchar(':'); /* End of field 14. */
1265 if (sk2
->protect
.s2k
.mode
== 1001)
1266 putchar ('#'); /* Key is just a stub. */
1267 else if (sk2
->protect
.s2k
.mode
== 1002)
1269 /* Key is stored on an external token (card) or handled by
1270 the gpg-agent. Print the serial number of that token
1272 for (i
=0; i
< sk2
->protect
.ivlen
; i
++)
1273 printf ("%02X", sk2
->protect
.iv
[i
]);
1275 putchar(':'); /* End of field 15. */
1279 print_fingerprint ( NULL
, sk2
, 0 );
1281 else if ( opt
.list_sigs
&& node
->pkt
->pkttype
== PKT_SIGNATURE
)
1283 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1284 int sigrc
,fprokay
=0;
1287 byte fparray
[MAX_FINGERPRINT_LEN
];
1289 if ( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x28
1290 || sig
->sig_class
== 0x30 )
1292 else if ( (sig
->sig_class
&~3) == 0x10 )
1294 else if ( sig
->sig_class
== 0x18 )
1296 else if ( sig
->sig_class
== 0x1F )
1300 printf ("sig::::::::::%02x%c:\n",
1301 sig
->sig_class
, sig
->flags
.exportable
?'x':'l');
1305 if ( opt
.check_sigs
)
1307 PKT_public_key
*signer_pk
=NULL
;
1310 if (opt
.no_sig_cache
)
1311 signer_pk
= xmalloc_clear (sizeof(PKT_public_key
));
1313 rc
= check_key_signature2 ( keyblock
, node
, NULL
, signer_pk
,
1315 switch ( gpg_err_code (rc
) )
1317 case 0: sigrc
= '!'; break;
1318 case GPG_ERR_BAD_SIGNATURE
: sigrc
= '-'; break;
1319 case GPG_ERR_NO_PUBKEY
:
1320 case GPG_ERR_UNUSABLE_PUBKEY
: sigrc
= '?'; break;
1321 default: sigrc
= '%'; break;
1324 if (opt
.no_sig_cache
)
1328 fingerprint_from_pk (signer_pk
, fparray
, &fplen
);
1331 free_public_key(signer_pk
);
1339 fputs ( sigstr
, stdout
);
1343 printf ("::%d:%08lX%08lX:%s:%s:", sig
->pubkey_algo
,
1344 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1345 colon_datestr_from_sig(sig
),
1346 colon_expirestr_from_sig(sig
));
1348 if (sig
->trust_depth
|| sig
->trust_value
)
1349 printf("%d %d",sig
->trust_depth
,sig
->trust_value
);
1352 if (sig
->trust_regexp
)
1353 print_string (stdout
,sig
->trust_regexp
,
1354 strlen(sig
->trust_regexp
),':');
1358 printf("[%s] ", g10_errstr(rc
) );
1359 else if ( sigrc
== '?' )
1361 else if ( !opt
.fast_list_mode
)
1364 char *p
= get_user_id( sig
->keyid
, &n
);
1365 print_string( stdout
, p
, n
, ':' );
1368 printf (":%02x%c:", sig
->sig_class
,sig
->flags
.exportable
?'x':'l');
1370 if (opt
.no_sig_cache
&& opt
.check_sigs
&& fprokay
)
1374 for (i
=0; i
< fplen
; i
++ )
1375 printf ("%02X", fparray
[i
] );
1382 if (opt
.show_subpackets
)
1383 print_subpackets_colon (sig
);
1385 /* fixme: check or list other sigs here */
1391 * Reorder the keyblock so that the primary user ID (and not attribute
1392 * packet) comes first. Fixme: Replace this by a generic sort
1395 do_reorder_keyblock (KBNODE keyblock
,int attr
)
1397 KBNODE primary
= NULL
, primary0
= NULL
, primary2
= NULL
;
1400 for (node
=keyblock
; node
; primary0
=node
, node
= node
->next
) {
1401 if( node
->pkt
->pkttype
== PKT_USER_ID
&&
1402 ((attr
&& node
->pkt
->pkt
.user_id
->attrib_data
) ||
1403 (!attr
&& !node
->pkt
->pkt
.user_id
->attrib_data
)) &&
1404 node
->pkt
->pkt
.user_id
->is_primary
) {
1405 primary
= primary2
= node
;
1406 for (node
=node
->next
; node
; primary2
=node
, node
= node
->next
) {
1407 if( node
->pkt
->pkttype
== PKT_USER_ID
1408 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1409 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1417 return; /* no primary key flag found (should not happen) */
1419 for (last
=NULL
, node
=keyblock
; node
; last
= node
, node
= node
->next
) {
1420 if( node
->pkt
->pkttype
== PKT_USER_ID
)
1424 assert (last
); /* the user ID is never the first packet */
1425 assert (primary0
); /* ditto (this is the node before primary) */
1426 if ( node
== primary
)
1427 return; /* already the first one */
1429 last
->next
= primary
;
1430 primary0
->next
= primary2
->next
;
1431 primary2
->next
= node
;
1435 reorder_keyblock (KBNODE keyblock
)
1437 do_reorder_keyblock(keyblock
,1);
1438 do_reorder_keyblock(keyblock
,0);
1442 list_keyblock( KBNODE keyblock
, int secret
, int fpr
, void *opaque
)
1444 reorder_keyblock (keyblock
);
1445 if (opt
.with_colons
)
1446 list_keyblock_colon (keyblock
, secret
, fpr
);
1448 list_keyblock_print (keyblock
, secret
, fpr
, opaque
);
1452 * standard function to print the finperprint.
1453 * mode 0: as used in key listings, opt.with_colons is honored
1454 * 1: print using log_info ()
1455 * 2: direct use of tty
1456 * 3: direct use of tty but only primary key.
1457 * modes 1 and 2 will try and print both subkey and primary key fingerprints
1460 print_fingerprint (PKT_public_key
*pk
, PKT_secret_key
*sk
, int mode
)
1462 byte array
[MAX_FINGERPRINT_LEN
], *p
;
1470 if(sk
->main_keyid
[0]==sk
->keyid
[0] && sk
->main_keyid
[1]==sk
->keyid
[1])
1475 if(pk
->main_keyid
[0]==pk
->keyid
[0] && pk
->main_keyid
[1]==pk
->keyid
[1])
1479 /* Just to be safe */
1480 if(mode
&0x80 && !primary
)
1482 log_error("primary key is not really primary!\n");
1488 if(!primary
&& (mode
==1 || mode
==2))
1492 PKT_secret_key
*primary_sk
=xmalloc_clear(sizeof(*primary_sk
));
1493 get_seckey(primary_sk
,sk
->main_keyid
);
1494 print_fingerprint(NULL
,primary_sk
,mode
|0x80);
1495 free_secret_key(primary_sk
);
1499 PKT_public_key
*primary_pk
=xmalloc_clear(sizeof(*primary_pk
));
1500 get_pubkey(primary_pk
,pk
->main_keyid
);
1501 print_fingerprint(primary_pk
,NULL
,mode
|0x80);
1502 free_public_key(primary_pk
);
1507 fp
= log_get_stream ();
1509 text
= _("Primary key fingerprint:");
1511 text
= _(" Subkey fingerprint:");
1513 else if (mode
== 2) {
1514 fp
= NULL
; /* use tty */
1516 /* TRANSLATORS: this should fit into 24 bytes to that the
1517 * fingerprint data is properly aligned with the user ID */
1518 text
= _(" Primary key fingerprint:");
1520 text
= _(" Subkey fingerprint:");
1522 else if (mode
== 3) {
1523 fp
= NULL
; /* use tty */
1524 text
= _(" Key fingerprint =");
1528 text
= _(" Key fingerprint =");
1532 fingerprint_from_sk (sk
, array
, &n
);
1534 fingerprint_from_pk (pk
, array
, &n
);
1536 if (opt
.with_colons
&& !mode
) {
1537 fprintf (fp
, "fpr:::::::::");
1538 for (i
=0; i
< n
; i
++, p
++ )
1539 fprintf (fp
, "%02X", *p
);
1546 tty_printf ("%s", text
);
1548 for (i
=0; i
< n
; i
++, i
++, p
+= 2 ) {
1552 fprintf (fp
, " %02X%02X", *p
, p
[1] );
1557 tty_printf (" %02X%02X", *p
, p
[1]);
1562 for (i
=0; i
< n
; i
++, p
++ ) {
1566 fprintf (fp
, " %02X", *p
);
1571 tty_printf (" %02X", *p
);
1582 /* Print the serial number of an OpenPGP card if available. */
1584 print_card_serialno (PKT_secret_key
*sk
)
1590 if (!sk
->is_protected
|| sk
->protect
.s2k
.mode
!= 1002)
1591 return; /* Not a card. */
1592 if (opt
.with_colons
)
1593 return; /* Handled elsewhere. */
1595 fputs (_(" Card serial no. ="), stdout
);
1597 if (sk
->protect
.ivlen
== 16
1598 && !memcmp (sk
->protect
.iv
, "\xD2\x76\x00\x01\x24\x01", 6) )
1599 { /* This is an OpenPGP card. Just print the relevant part. */
1600 for (i
=8; i
< 14; i
++)
1604 printf ("%02X", sk
->protect
.iv
[i
]);
1608 { /* Something is wrong: Print all. */
1609 for (i
=0; i
< sk
->protect
.ivlen
; i
++)
1610 printf ("%02X", sk
->protect
.iv
[i
]);
1618 set_attrib_fd (int fd
)
1620 static int last_fd
=-1;
1622 if ( fd
!= -1 && last_fd
== fd
)
1625 if ( attrib_fp
&& attrib_fp
!= stdout
&& attrib_fp
!= stderr
1626 && attrib_fp
!= log_get_stream () )
1632 #ifdef HAVE_DOSISH_SYSTEM
1633 setmode (fd
, O_BINARY
);
1640 attrib_fp
= fdopen (fd
, "wb");
1643 log_fatal("can't open fd %d for attribute output: %s\n",
1644 fd
, strerror(errno
));