2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2004, 2005 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 static void list_all(int);
44 static void list_one( STRLIST names
, int secret
);
45 static void print_card_serialno (PKT_secret_key
*sk
);
54 static FILE *attrib_fp
=NULL
;
58 * If list is NULL, all available keys are listed
61 public_key_list( STRLIST list
)
65 byte trust_model
,marginals
,completes
,cert_depth
;
66 ulong created
,nextcheck
;
68 read_trust_options(&trust_model
,&created
,&nextcheck
,
69 &marginals
,&completes
,&cert_depth
);
73 if(nextcheck
&& nextcheck
<= make_timestamp())
75 if(trust_model
!=opt
.trust_model
)
77 if(opt
.trust_model
==TM_PGP
|| opt
.trust_model
==TM_CLASSIC
)
79 if(marginals
!=opt
.marginals_needed
)
81 if(completes
!=opt
.completes_needed
)
83 if(cert_depth
!=opt
.max_cert_depth
)
87 printf(":%d:%lu:%lu",trust_model
,created
,nextcheck
);
89 /* Only show marginals, completes, and cert_depth in the classic
90 or PGP trust models since they are not meaningful
93 if(trust_model
==TM_PGP
|| trust_model
==TM_CLASSIC
)
94 printf(":%d:%d:%d",marginals
,completes
,cert_depth
);
99 /* We need to do the stale check right here because it might need to
100 update the keyring while we already have the keyring open. This
101 is very bad for W32 because of a sharing violation. For real OSes
102 it might lead to false results if we are later listing a keyring
103 which is associated with the inode of a deleted file. */
104 check_trustdb_stale ();
113 secret_key_list( STRLIST list
)
115 check_trustdb_stale ();
119 else /* List by user id */
124 print_seckey_info (PKT_secret_key
*sk
)
129 keyid_from_sk (sk
, keyid
);
130 p
=get_user_id_native(keyid
);
132 tty_printf ("\nsec %4u%c/%s %s %s\n",
134 pubkey_letter (sk
->pubkey_algo
),
135 keystr(keyid
), datestr_from_sk (sk
), p
);
140 /* Print information about the public key. With FP passed as NULL,
141 the tty output interface is used, otherwise output is directted to
144 print_pubkey_info (FILE *fp
, PKT_public_key
*pk
)
149 keyid_from_pk (pk
, keyid
);
151 /* If the pk was chosen by a particular user ID, that is the one to
154 p
=utf8_to_native(pk
->user_id
->name
,pk
->user_id
->len
,0);
156 p
=get_user_id_native(keyid
);
159 fprintf (fp
, "pub %4u%c/%s %s %s\n",
161 pubkey_letter (pk
->pubkey_algo
),
162 keystr(keyid
), datestr_from_pk (pk
), p
);
164 tty_printf ("\npub %4u%c/%s %s %s\n",
165 nbits_from_pk (pk
), pubkey_letter (pk
->pubkey_algo
),
166 keystr(keyid
), datestr_from_pk (pk
), p
);
172 /* Print basic information of a secret key including the card serial
173 number information. */
175 print_card_key_info (FILE *fp
, KBNODE keyblock
)
180 for (node
= keyblock
; node
; node
= node
->next
)
182 if (node
->pkt
->pkttype
== PKT_SECRET_KEY
183 || (node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) )
185 PKT_secret_key
*sk
= node
->pkt
->pkt
.secret_key
;
187 tty_fprintf (fp
, "%s%c %4u%c/%s ",
188 node
->pkt
->pkttype
== PKT_SECRET_KEY
? "sec":"ssb",
189 (sk
->protect
.s2k
.mode
==1001)?'#':
190 (sk
->protect
.s2k
.mode
==1002)?'>':' ',
192 pubkey_letter (sk
->pubkey_algo
),
194 tty_fprintf (fp
, _("created: %s"), datestr_from_sk (sk
));
195 tty_fprintf (fp
, " ");
196 tty_fprintf (fp
, _("expires: %s"), expirestr_from_sk (sk
));
197 if (sk
->is_protected
&& sk
->protect
.s2k
.mode
== 1002)
199 tty_fprintf (fp
, "\n ");
200 tty_fprintf (fp
, _("card-no: "));
201 if (sk
->protect
.ivlen
== 16
202 && !memcmp (sk
->protect
.iv
, "\xD2\x76\x00\x01\x24\x01", 6))
204 /* This is an OpenPGP card. */
205 for (i
=8; i
< 14; i
++)
208 tty_fprintf (fp
, " ");
209 tty_fprintf (fp
, "%02X", sk
->protect
.iv
[i
]);
213 { /* Something is wrong: Print all. */
214 for (i
=0; i
< sk
->protect
.ivlen
; i
++)
215 tty_fprintf (fp
, "%02X", sk
->protect
.iv
[i
]);
218 tty_fprintf (fp
, "\n");
225 /* Flags = 0x01 hashed 0x02 critical */
227 status_one_subpacket(sigsubpkttype_t type
,size_t len
,int flags
,const byte
*buf
)
231 /* Don't print these. */
235 sprintf(status
,"%d %u %u ",type
,flags
,(unsigned int)len
);
237 write_status_text_and_buffer(STATUS_SIG_SUBPACKET
,status
,buf
,len
,0);
242 mode=1 for log_info + status messages
243 mode=2 for status messages only
247 show_policy_url(PKT_signature
*sig
,int indent
,int mode
)
252 FILE *fp
=mode
?log_stream():stdout
;
254 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_POLICY
,&len
,&seq
,&crit
)))
261 for(i
=0;i
<indent
;i
++)
265 str
=_("Critical signature policy: ");
267 str
=_("Signature policy: ");
272 print_utf8_string(fp
,p
,len
);
277 write_status_buffer ( STATUS_POLICY_URL
, p
, len
, 0 );
283 mode=1 for log_info + status messages
284 mode=2 for status messages only
288 show_keyserver_url(PKT_signature
*sig
,int indent
,int mode
)
293 FILE *fp
=mode
?log_stream():stdout
;
295 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&len
,&seq
,&crit
)))
302 for(i
=0;i
<indent
;i
++)
306 str
=_("Critical preferred keyserver: ");
308 str
=_("Preferred keyserver: ");
313 print_utf8_string(fp
,p
,len
);
318 status_one_subpacket(SIGSUBPKT_PREF_KS
,len
,(crit
?0x02:0)|0x01,p
);
324 mode=1 for log_info + status messages
325 mode=2 for status messages only
328 1 == standard notations
333 show_notation(PKT_signature
*sig
,int indent
,int mode
,int which
)
335 FILE *fp
=mode
?log_stream():stdout
;
336 struct notation
*nd
,*notations
;
341 notations
=sig_to_notation(sig
);
343 /* There may be multiple notations in the same sig. */
344 for(nd
=notations
;nd
;nd
=nd
->next
)
348 int has_at
=!!strchr(nd
->name
,'@');
350 if((which
&1 && !has_at
) || (which
&2 && has_at
))
355 for(i
=0;i
<indent
;i
++)
358 if(nd
->flags
.critical
)
359 str
=_("Critical signature notation: ");
361 str
=_("Signature notation: ");
366 /* This is all UTF8 */
367 print_utf8_string(fp
,nd
->name
,strlen(nd
->name
));
369 print_utf8_string(fp
,nd
->value
,strlen(nd
->value
));
376 write_status_buffer(STATUS_NOTATION_NAME
,
377 nd
->name
,strlen(nd
->name
),0);
378 write_status_buffer(STATUS_NOTATION_DATA
,
379 nd
->value
,strlen(nd
->value
),50);
383 free_notation(notations
);
387 print_signature_stats(struct sig_stats
*s
)
389 if( s
->inv_sigs
== 1 )
390 tty_printf(_("1 bad signature\n") );
391 else if( s
->inv_sigs
)
392 tty_printf(_("%d bad signatures\n"), s
->inv_sigs
);
394 tty_printf(_("1 signature not checked due to a missing key\n") );
396 tty_printf(_("%d signatures not checked due to missing keys\n"),s
->no_key
);
397 if( s
->oth_err
== 1 )
398 tty_printf(_("1 signature not checked due to an error\n") );
399 else if( s
->oth_err
)
400 tty_printf(_("%d signatures not checked due to errors\n"), s
->oth_err
);
404 list_all( int secret
)
407 KBNODE keyblock
= NULL
;
409 const char *lastresname
, *resname
;
410 struct sig_stats stats
;
412 memset(&stats
,0,sizeof(stats
));
414 hd
= keydb_new (secret
);
418 rc
= keydb_search_first (hd
);
421 log_error("keydb_search_first failed: %s\n", g10_errstr(rc
) );
427 rc
= keydb_get_keyblock (hd
, &keyblock
);
429 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
434 resname
= keydb_get_resource_name (hd
);
435 if (lastresname
!= resname
)
439 printf("%s\n", resname
);
440 for(i
=strlen(resname
); i
; i
-- )
443 lastresname
= resname
;
446 merge_keys_and_selfsig( keyblock
);
447 list_keyblock( keyblock
, secret
, opt
.fingerprint
,
448 opt
.check_sigs
?&stats
:NULL
);
449 release_kbnode( keyblock
);
451 } while (!(rc
= keydb_search_next (hd
)));
453 log_error ("keydb_search_next failed: %s\n", g10_errstr(rc
));
455 if(opt
.check_sigs
&& !opt
.with_colons
)
456 print_signature_stats(&stats
);
459 release_kbnode (keyblock
);
465 list_one( STRLIST names
, int secret
)
468 KBNODE keyblock
= NULL
;
471 const char *keyring_str
= _("Keyring");
473 struct sig_stats stats
;
475 memset(&stats
,0,sizeof(stats
));
477 /* fixme: using the bynames function has the disadvantage that we
478 * don't know wether one of the names given was not found. OTOH,
479 * this function has the advantage to list the names in the
480 * sequence as defined by the keyDB and does not duplicate
481 * outputs. A solution could be do test whether all given have
482 * been listed (this needs a way to use the keyDB search
483 * functions) or to have the search function return indicators for
484 * found names. Yet another way is to use the keydb search
485 * facilities directly. */
487 rc
= get_seckey_bynames( &ctx
, NULL
, names
, &keyblock
);
489 log_error("error reading key: %s\n", g10_errstr(rc
) );
490 get_seckey_end( ctx
);
494 if ((opt
.list_options
&LIST_SHOW_KEYRING
) && !opt
.with_colons
) {
495 resname
= keydb_get_resource_name (get_ctx_handle(ctx
));
496 printf("%s: %s\n", keyring_str
, resname
);
497 for(i
= strlen(resname
) + strlen(keyring_str
) + 2; i
; i
-- )
501 list_keyblock( keyblock
, 1, opt
.fingerprint
, NULL
);
502 release_kbnode( keyblock
);
503 } while( !get_seckey_next( ctx
, NULL
, &keyblock
) );
504 get_seckey_end( ctx
);
507 rc
= get_pubkey_bynames( &ctx
, NULL
, names
, &keyblock
);
509 log_error("error reading key: %s\n", g10_errstr(rc
) );
510 get_pubkey_end( ctx
);
514 if ((opt
.list_options
&LIST_SHOW_KEYRING
) && !opt
.with_colons
) {
515 resname
= keydb_get_resource_name (get_ctx_handle(ctx
));
516 printf("%s: %s\n", keyring_str
, resname
);
517 for(i
= strlen(resname
) + strlen(keyring_str
) + 2; i
; i
-- )
521 list_keyblock( keyblock
, 0, opt
.fingerprint
,
522 opt
.check_sigs
?&stats
:NULL
);
523 release_kbnode( keyblock
);
524 } while( !get_pubkey_next( ctx
, NULL
, &keyblock
) );
525 get_pubkey_end( ctx
);
528 if(opt
.check_sigs
&& !opt
.with_colons
)
529 print_signature_stats(&stats
);
533 print_key_data( PKT_public_key
*pk
)
535 int n
= pk
? pubkey_get_npkey( pk
->pubkey_algo
) : 0;
538 for(i
=0; i
< n
; i
++ ) {
539 printf("pkd:%d:%u:", i
, mpi_get_nbits( pk
->pkey
[i
] ) );
540 mpi_print(stdout
, pk
->pkey
[i
], 1 );
547 print_capabilities (PKT_public_key
*pk
, PKT_secret_key
*sk
, KBNODE keyblock
)
549 if(pk
|| (sk
&& sk
->protect
.s2k
.mode
!=1001))
551 unsigned int use
= pk
? pk
->pubkey_usage
: sk
->pubkey_usage
;
553 if ( use
& PUBKEY_USAGE_ENC
)
556 if ( use
& PUBKEY_USAGE_SIG
)
559 if( pk
? pk
->is_primary
: sk
->is_primary
)
563 if ( (use
& PUBKEY_USAGE_AUTH
) )
567 if ( keyblock
) { /* figure out the usable capabilities */
569 int enc
=0, sign
=0, cert
=0, auth
=0, disabled
=0;
571 for (k
=keyblock
; k
; k
= k
->next
) {
572 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
573 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
574 pk
= k
->pkt
->pkt
.public_key
;
577 disabled
=pk_is_disabled(pk
);
579 if ( pk
->is_valid
&& !pk
->is_revoked
&& !pk
->has_expired
) {
580 if ( pk
->pubkey_usage
& PUBKEY_USAGE_ENC
)
582 if ( pk
->pubkey_usage
& PUBKEY_USAGE_SIG
)
588 if ( (pk
->pubkey_usage
& PUBKEY_USAGE_AUTH
) )
592 else if ( k
->pkt
->pkttype
== PKT_SECRET_KEY
593 || k
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
594 sk
= k
->pkt
->pkt
.secret_key
;
595 if ( sk
->is_valid
&& !sk
->is_revoked
&& !sk
->has_expired
596 && sk
->protect
.s2k
.mode
!=1001 ) {
597 if ( sk
->pubkey_usage
& PUBKEY_USAGE_ENC
)
599 if ( sk
->pubkey_usage
& PUBKEY_USAGE_SIG
)
605 if ( (sk
->pubkey_usage
& PUBKEY_USAGE_AUTH
) )
625 /* Flags = 0x01 hashed 0x02 critical */
627 print_one_subpacket(sigsubpkttype_t type
,size_t len
,int flags
,const byte
*buf
)
631 printf("spk:%d:%u:%u:",type
,flags
,(unsigned int)len
);
635 /* printable ascii other than : and % */
636 if(buf
[i
]>=32 && buf
[i
]<=126 && buf
[i
]!=':' && buf
[i
]!='%')
639 printf("%%%02X",buf
[i
]);
646 print_subpackets_colon(PKT_signature
*sig
)
650 assert(opt
.show_subpackets
);
652 for(i
=opt
.show_subpackets
;*i
;i
++)
660 while((p
=enum_sig_subpkt(sig
->hashed
,*i
,&len
,&seq
,&crit
)))
661 print_one_subpacket(*i
,len
,0x01|(crit
?0x02:0),p
);
665 while((p
=enum_sig_subpkt(sig
->unhashed
,*i
,&len
,&seq
,&crit
)))
666 print_one_subpacket(*i
,len
,0x00|(crit
?0x02:0),p
);
671 dump_attribs(const PKT_user_id
*uid
,PKT_public_key
*pk
,PKT_secret_key
*sk
)
678 for(i
=0;i
<uid
->numattribs
;i
++)
680 if(is_status_enabled())
682 byte array
[MAX_FINGERPRINT_LEN
], *p
;
683 char buf
[(MAX_FINGERPRINT_LEN
*2)+90];
687 fingerprint_from_pk( pk
, array
, &n
);
689 fingerprint_from_sk( sk
, array
, &n
);
694 for(j
=0; j
< n
; j
++, p
++ )
695 sprintf(buf
+2*j
, "%02X", *p
);
697 sprintf(buf
+strlen(buf
)," %lu %u %u %u %lu %lu %u",
698 (ulong
)uid
->attribs
[i
].len
,uid
->attribs
[i
].type
,i
+1,
699 uid
->numattribs
,(ulong
)uid
->created
,(ulong
)uid
->expiredate
,
700 ((uid
->is_primary
?0x01:0)|
701 (uid
->is_revoked
?0x02:0)|
702 (uid
->is_expired
?0x04:0)));
703 write_status_text(STATUS_ATTRIBUTE
,buf
);
706 fwrite(uid
->attribs
[i
].data
,uid
->attribs
[i
].len
,1,attrib_fp
);
711 list_keyblock_print ( KBNODE keyblock
, int secret
, int fpr
, void *opaque
)
718 struct sig_stats
*stats
=opaque
;
721 /* get the keyid from the keyblock */
722 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
: PKT_PUBLIC_KEY
);
724 log_error("Oops; key lost!\n");
725 dump_kbnode( keyblock
);
732 sk
= node
->pkt
->pkt
.secret_key
;
734 printf("sec%c %4u%c/%s %s",(sk
->protect
.s2k
.mode
==1001)?'#':
735 (sk
->protect
.s2k
.mode
==1002)?'>':' ',
736 nbits_from_sk( sk
),pubkey_letter( sk
->pubkey_algo
),
737 keystr_from_sk(sk
),datestr_from_sk( sk
));
742 printf(_("expired: %s"),expirestr_from_sk(sk
));
745 else if(sk
->expiredate
)
748 printf(_("expires: %s"),expirestr_from_sk(sk
));
756 pk
= node
->pkt
->pkt
.public_key
;
759 check_trustdb_stale();
761 printf("pub %4u%c/%s %s",
762 nbits_from_pk(pk
),pubkey_letter(pk
->pubkey_algo
),
763 keystr_from_pk(pk
),datestr_from_pk( pk
));
765 /* We didn't include this before in the key listing, but there
766 is room in the new format, so why not? */
771 printf(_("revoked: %s"),revokestr_from_pk(pk
));
774 else if(pk
->has_expired
)
777 printf(_("expired: %s"),expirestr_from_pk(pk
));
780 else if(pk
->expiredate
)
783 printf(_("expires: %s"),expirestr_from_pk(pk
));
788 /* I need to think about this some more. It's easy enough to
789 include, but it looks sort of confusing in the
791 if(opt
.list_options
&LIST_SHOW_VALIDITY
)
793 int validity
=get_validity(pk
,NULL
);
794 printf(" [%s]",trust_value_to_string(validity
));
802 print_fingerprint( pk
, sk
, 0 );
803 print_card_serialno (sk
);
804 if( opt
.with_key_data
)
805 print_key_data( pk
);
807 for( kbctx
=NULL
; (node
=walk_kbnode( keyblock
, &kbctx
, 0)) ; ) {
808 if( node
->pkt
->pkttype
== PKT_USER_ID
&& !opt
.fast_list_mode
) {
809 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
811 if(pk
&& (uid
->is_expired
|| uid
->is_revoked
)
812 && !(opt
.list_options
&LIST_SHOW_UNUSABLE_UIDS
))
820 if(attrib_fp
&& uid
->attrib_data
!=NULL
)
821 dump_attribs(uid
,pk
,sk
);
823 if((uid
->is_revoked
|| uid
->is_expired
)
824 || ((opt
.list_options
&LIST_SHOW_UID_VALIDITY
) && pk
))
826 const char *validity
;
829 validity
=uid_trust_string_fixed(pk
,uid
);
830 indent
=(keystrlen()+9)-atoi(uid_trust_string_fixed(NULL
,NULL
));
832 if(indent
<0 || indent
>40)
835 printf("uid%*s%s ",indent
,"",validity
);
838 printf("uid%*s", (int)keystrlen()+10,"");
840 print_utf8_string( stdout
, uid
->name
, uid
->len
);
843 if((opt
.list_options
&LIST_SHOW_PHOTOS
) && uid
->attribs
!=NULL
)
844 show_photos(uid
->attribs
,uid
->numattribs
,pk
,sk
);
846 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
)
848 PKT_public_key
*pk2
= node
->pkt
->pkt
.public_key
;
850 if((pk2
->is_revoked
|| pk2
->has_expired
)
851 && !(opt
.list_options
&LIST_SHOW_UNUSABLE_SUBKEYS
))
859 printf("sub %4u%c/%s %s",
860 nbits_from_pk( pk2
),pubkey_letter( pk2
->pubkey_algo
),
861 keystr_from_pk(pk2
),datestr_from_pk(pk2
));
862 if( pk2
->is_revoked
)
865 printf(_("revoked: %s"),revokestr_from_pk(pk2
));
868 else if( pk2
->has_expired
)
871 printf(_("expired: %s"),expirestr_from_pk(pk2
));
874 else if( pk2
->expiredate
)
877 printf(_("expires: %s"),expirestr_from_pk(pk2
));
882 print_fingerprint( pk2
, NULL
, 0 );
883 if( opt
.with_key_data
)
884 print_key_data( pk2
);
886 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
)
888 PKT_secret_key
*sk2
= node
->pkt
->pkt
.secret_key
;
890 printf("ssb%c %4u%c/%s %s",
891 (sk2
->protect
.s2k
.mode
==1001)?'#':
892 (sk2
->protect
.s2k
.mode
==1002)?'>':' ',
893 nbits_from_sk( sk2
),pubkey_letter( sk2
->pubkey_algo
),
894 keystr_from_sk(sk2
),datestr_from_sk( sk2
) );
895 if( sk2
->expiredate
)
898 printf(_("expires: %s"),expirestr_from_sk(sk2
));
904 print_fingerprint( NULL
, sk2
, 0 );
905 print_card_serialno (sk2
);
908 else if( opt
.list_sigs
909 && node
->pkt
->pkttype
== PKT_SIGNATURE
911 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
917 rc
= check_key_signature( keyblock
, node
, NULL
);
919 case 0: sigrc
= '!'; break;
920 case G10ERR_BAD_SIGN
: stats
->inv_sigs
++; sigrc
= '-'; break;
921 case G10ERR_NO_PUBKEY
:
922 case G10ERR_UNU_PUBKEY
: stats
->no_key
++; continue;
923 default: stats
->oth_err
++; sigrc
= '%'; break;
926 /* TODO: Make sure a cached sig record here still has
927 the pk that issued it. See also
928 keyedit.c:print_and_check_one_sig */
935 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x28
936 || sig
->sig_class
== 0x30 )
938 else if( (sig
->sig_class
&~3) == 0x10 )
940 else if( sig
->sig_class
== 0x18 )
942 else if( sig
->sig_class
== 0x1F )
946 "[unexpected signature class 0x%02x]\n",sig
->sig_class
);
950 fputs( sigstr
, stdout
);
951 printf("%c%c %c%c%c%c%c%c %s %s",
952 sigrc
,(sig
->sig_class
-0x10>0 &&
953 sig
->sig_class
-0x10<4)?'0'+sig
->sig_class
-0x10:' ',
954 sig
->flags
.exportable
?' ':'L',
955 sig
->flags
.revocable
?' ':'R',
956 sig
->flags
.policy_url
?'P':' ',
957 sig
->flags
.notation
?'N':' ',
958 sig
->flags
.expired
?'X':' ',
959 (sig
->trust_depth
>9)?'T':
960 (sig
->trust_depth
>0)?'0'+sig
->trust_depth
:' ',
961 keystr(sig
->keyid
),datestr_from_sig(sig
));
962 if(opt
.list_options
&LIST_SHOW_SIG_EXPIRE
)
963 printf(" %s", expirestr_from_sig(sig
));
966 printf("[%s] ", g10_errstr(rc
) );
967 else if( sigrc
== '?' )
969 else if ( !opt
.fast_list_mode
) {
971 char *p
= get_user_id( sig
->keyid
, &n
);
972 print_utf8_string( stdout
, p
, n
);
977 if(sig
->flags
.policy_url
978 && (opt
.list_options
&LIST_SHOW_POLICY_URLS
))
979 show_policy_url(sig
,3,0);
981 if(sig
->flags
.notation
&& (opt
.list_options
&LIST_SHOW_NOTATIONS
))
982 show_notation(sig
,3,0,
983 ((opt
.list_options
&LIST_SHOW_STD_NOTATIONS
)?1:0)+
984 ((opt
.list_options
&LIST_SHOW_USER_NOTATIONS
)?2:0));
986 if(sig
->flags
.pref_ks
987 && (opt
.list_options
&LIST_SHOW_KEYSERVER_URLS
))
988 show_keyserver_url(sig
,3,0);
990 /* fixme: check or list other sigs here */
997 print_revokers(PKT_public_key
*pk
)
999 /* print the revoker record */
1000 if( !pk
->revkey
&& pk
->numrevkeys
)
1006 for (i
=0; i
< pk
->numrevkeys
; i
++)
1010 printf ("rvk:::%d::::::", pk
->revkey
[i
].algid
);
1011 p
= pk
->revkey
[i
].fpr
;
1012 for (j
=0; j
< 20; j
++, p
++ )
1013 printf ("%02X", *p
);
1014 printf (":%02x%s:\n", pk
->revkey
[i
].class,
1015 (pk
->revkey
[i
].class&0x40)?"s":"");
1021 list_keyblock_colon( KBNODE keyblock
, int secret
, int fpr
)
1030 int trustletter
= 0;
1034 /* get the keyid from the keyblock */
1035 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
: PKT_PUBLIC_KEY
);
1037 log_error("Oops; key lost!\n");
1038 dump_kbnode( keyblock
);
1044 sk
= node
->pkt
->pkt
.secret_key
;
1045 keyid_from_sk( sk
, keyid
);
1046 printf("sec::%u:%d:%08lX%08lX:%s:%s:::",
1047 nbits_from_sk( sk
),
1049 (ulong
)keyid
[0],(ulong
)keyid
[1],
1050 colon_datestr_from_sk( sk
),
1051 colon_strtime (sk
->expiredate
)
1052 /* fixme: add LID here */ );
1055 pk
= node
->pkt
->pkt
.public_key
;
1057 keyid_from_pk( pk
, keyid
);
1058 fputs( "pub:", stdout
);
1059 if ( !pk
->is_valid
)
1061 else if ( pk
->is_revoked
)
1063 else if ( pk
->has_expired
)
1065 else if ( opt
.fast_list_mode
|| opt
.no_expensive_trust_checks
)
1068 trustletter
= get_validity_info ( pk
, NULL
);
1069 if( trustletter
== 'u' )
1071 putchar(trustletter
);
1073 printf(":%u:%d:%08lX%08lX:%s:%s::",
1074 nbits_from_pk( pk
),
1076 (ulong
)keyid
[0],(ulong
)keyid
[1],
1077 colon_datestr_from_pk( pk
),
1078 colon_strtime (pk
->expiredate
) );
1079 if( !opt
.fast_list_mode
&& !opt
.no_expensive_trust_checks
)
1080 putchar( get_ownertrust_info(pk
) );
1084 if (opt
.fixed_list_mode
) {
1085 /* do not merge the first uid with the primary key */
1088 print_capabilities (pk
, sk
, keyblock
);
1090 putchar(':'); /* End of field 13. */
1091 putchar(':'); /* End of field 14. */
1092 if (sk
->protect
.s2k
.mode
== 1001)
1093 putchar('#'); /* Key is just a stub. */
1094 else if (sk
->protect
.s2k
.mode
== 1002) {
1095 /* Key is stored on an external token (card) or handled by
1096 the gpg-agent. Print the serial number of that token
1098 for (i
=0; i
< sk
->protect
.ivlen
; i
++)
1099 printf ("%02X", sk
->protect
.iv
[i
]);
1101 putchar(':'); /* End of field 15. */
1107 print_fingerprint( pk
, sk
, 0 );
1108 if( opt
.with_key_data
)
1109 print_key_data( pk
);
1113 for( kbctx
=NULL
; (node
=walk_kbnode( keyblock
, &kbctx
, 0)) ; ) {
1114 if( node
->pkt
->pkttype
== PKT_USER_ID
&& !opt
.fast_list_mode
) {
1115 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
1116 if(attrib_fp
&& node
->pkt
->pkt
.user_id
->attrib_data
!=NULL
)
1117 dump_attribs(node
->pkt
->pkt
.user_id
,pk
,sk
);
1119 * Fixme: We need a is_valid flag here too
1122 char *str
=uid
->attrib_data
?"uat":"uid";
1123 /* If we're listing a secret key, leave out the
1124 validity values for now. This is handled better in
1127 printf("%s:::::",str
);
1128 else if ( uid
->is_revoked
)
1129 printf("%s:r::::",str
);
1130 else if ( uid
->is_expired
)
1131 printf("%s:e::::",str
);
1132 else if ( opt
.no_expensive_trust_checks
)
1133 printf("%s:::::",str
);
1137 if( pk
&& !ulti_hack
)
1138 uid_validity
=get_validity_info (pk
, uid
);
1141 printf("%s:%c::::",str
,uid_validity
);
1144 printf("%s:",colon_strtime(uid
->created
));
1145 printf("%s:",colon_strtime(uid
->expiredate
));
1147 namehash_from_uid(uid
);
1149 for(i
=0; i
< 20; i
++ )
1150 printf("%02X",uid
->namehash
[i
]);
1154 if(uid
->attrib_data
)
1155 printf("%u %lu",uid
->numattribs
,uid
->attrib_len
);
1157 print_string(stdout
,uid
->name
,uid
->len
, ':' );
1163 print_capabilities (pk
, sk
, keyblock
);
1166 print_fingerprint( pk
, sk
, 0 );
1167 if( opt
.with_key_data
)
1168 print_key_data( pk
);
1172 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1174 PKT_public_key
*pk2
= node
->pkt
->pkt
.public_key
;
1179 print_capabilities (pk
, sk
, keyblock
);
1182 print_fingerprint( pk
, sk
, 0 ); /* of the main key */
1186 keyid_from_pk( pk2
, keyid2
);
1187 fputs ("sub:", stdout
);
1188 if ( !pk2
->is_valid
)
1190 else if ( pk2
->is_revoked
)
1192 else if ( pk2
->has_expired
)
1194 else if ( opt
.fast_list_mode
|| opt
.no_expensive_trust_checks
)
1197 /* trustletter should always be defined here */
1199 printf("%c", trustletter
);
1201 printf(":%u:%d:%08lX%08lX:%s:%s:::::",
1202 nbits_from_pk( pk2
),
1204 (ulong
)keyid2
[0],(ulong
)keyid2
[1],
1205 colon_datestr_from_pk( pk2
),
1206 colon_strtime (pk2
->expiredate
)
1207 /* fixme: add LID and ownertrust here */
1209 print_capabilities (pk2
, NULL
, NULL
);
1212 print_fingerprint( pk2
, NULL
, 0 );
1213 if( opt
.with_key_data
)
1214 print_key_data( pk2
);
1216 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1218 PKT_secret_key
*sk2
= node
->pkt
->pkt
.secret_key
;
1223 print_capabilities (pk
, sk
, keyblock
);
1226 print_fingerprint( pk
, sk
, 0 ); /* of the main key */
1230 keyid_from_sk( sk2
, keyid2
);
1231 printf("ssb::%u:%d:%08lX%08lX:%s:%s:::::",
1232 nbits_from_sk( sk2
),
1234 (ulong
)keyid2
[0],(ulong
)keyid2
[1],
1235 colon_datestr_from_sk( sk2
),
1236 colon_strtime (sk2
->expiredate
)
1237 /* fixme: add LID */ );
1238 print_capabilities (NULL
, sk2
, NULL
);
1239 if (opt
.fixed_list_mode
) {
1240 /* We print the serial number only in fixed list mode
1241 for the primary key so, so avoid questions we print
1242 it for subkeys also only in this mode. There is no
1243 technical reason, though. */
1244 putchar(':'); /* End of field 13. */
1245 putchar(':'); /* End of field 14. */
1246 if (sk2
->protect
.s2k
.mode
== 1001)
1247 putchar('#'); /* Key is just a stub. */
1248 else if (sk2
->protect
.s2k
.mode
== 1002) {
1249 /* Key is stored on an external token (card) or handled by
1250 the gpg-agent. Print the serial number of that token
1252 for (i
=0; i
< sk2
->protect
.ivlen
; i
++)
1253 printf ("%02X", sk2
->protect
.iv
[i
]);
1255 putchar(':'); /* End of field 15. */
1259 print_fingerprint( NULL
, sk2
, 0 );
1261 else if( opt
.list_sigs
&& node
->pkt
->pkttype
== PKT_SIGNATURE
) {
1262 PKT_signature
*sig
= node
->pkt
->pkt
.signature
;
1263 int sigrc
,fprokay
=0;
1266 byte fparray
[MAX_FINGERPRINT_LEN
];
1268 if( !any
) { /* no user id, (maybe a revocation follows)*/
1269 if( sig
->sig_class
== 0x20 )
1270 fputs("[revoked]:", stdout
);
1271 else if( sig
->sig_class
== 0x18 )
1272 fputs("[key binding]:", stdout
);
1273 else if( sig
->sig_class
== 0x28 )
1274 fputs("[subkey revoked]:", stdout
);
1278 print_capabilities (pk
, sk
, keyblock
);
1281 print_fingerprint( pk
, sk
, 0 );
1285 if( sig
->sig_class
== 0x20 || sig
->sig_class
== 0x28
1286 || sig
->sig_class
== 0x30 )
1288 else if( (sig
->sig_class
&~3) == 0x10 )
1290 else if( sig
->sig_class
== 0x18 )
1292 else if( sig
->sig_class
== 0x1F )
1295 printf ("sig::::::::::%02x%c:\n",
1296 sig
->sig_class
, sig
->flags
.exportable
?'x':'l');
1299 if( opt
.check_sigs
) {
1300 PKT_public_key
*signer_pk
=NULL
;
1303 if(opt
.no_sig_cache
)
1304 signer_pk
=xmalloc_clear(sizeof(PKT_public_key
));
1306 rc
= check_key_signature2( keyblock
, node
, NULL
, signer_pk
,
1309 case 0: sigrc
= '!'; break;
1310 case G10ERR_BAD_SIGN
: sigrc
= '-'; break;
1311 case G10ERR_NO_PUBKEY
:
1312 case G10ERR_UNU_PUBKEY
: sigrc
= '?'; break;
1313 default: sigrc
= '%'; break;
1316 if(opt
.no_sig_cache
)
1320 fingerprint_from_pk (signer_pk
, fparray
, &fplen
);
1323 free_public_key(signer_pk
);
1330 fputs( sigstr
, stdout
);
1334 printf("::%d:%08lX%08lX:%s:%s:", sig
->pubkey_algo
,
1335 (ulong
)sig
->keyid
[0], (ulong
)sig
->keyid
[1],
1336 colon_datestr_from_sig(sig
),
1337 colon_expirestr_from_sig(sig
));
1339 if(sig
->trust_depth
|| sig
->trust_value
)
1340 printf("%d %d",sig
->trust_depth
,sig
->trust_value
);
1343 if(sig
->trust_regexp
)
1344 print_string(stdout
,sig
->trust_regexp
,
1345 strlen(sig
->trust_regexp
),':');
1349 printf("[%s] ", g10_errstr(rc
) );
1350 else if( sigrc
== '?' )
1352 else if ( !opt
.fast_list_mode
) {
1354 char *p
= get_user_id( sig
->keyid
, &n
);
1355 print_string( stdout
, p
, n
, ':' );
1358 printf(":%02x%c:", sig
->sig_class
,sig
->flags
.exportable
?'x':'l');
1360 if(opt
.no_sig_cache
&& opt
.check_sigs
&& fprokay
)
1364 for (i
=0; i
< fplen
; i
++ )
1365 printf ("%02X", fparray
[i
] );
1372 if(opt
.show_subpackets
)
1373 print_subpackets_colon(sig
);
1375 /* fixme: check or list other sigs here */
1378 if( !any
) {/* oops, no user id */
1381 print_capabilities (pk
, sk
, keyblock
);
1387 * Reorder the keyblock so that the primary user ID (and not attribute
1388 * packet) comes first. Fixme: Replace this by a generic sort
1391 do_reorder_keyblock (KBNODE keyblock
,int attr
)
1393 KBNODE primary
= NULL
, primary0
= NULL
, primary2
= NULL
;
1396 for (node
=keyblock
; node
; primary0
=node
, node
= node
->next
) {
1397 if( node
->pkt
->pkttype
== PKT_USER_ID
&&
1398 ((attr
&& node
->pkt
->pkt
.user_id
->attrib_data
) ||
1399 (!attr
&& !node
->pkt
->pkt
.user_id
->attrib_data
)) &&
1400 node
->pkt
->pkt
.user_id
->is_primary
) {
1401 primary
= primary2
= node
;
1402 for (node
=node
->next
; node
; primary2
=node
, node
= node
->next
) {
1403 if( node
->pkt
->pkttype
== PKT_USER_ID
1404 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1405 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1413 return; /* no primary key flag found (should not happen) */
1415 for (last
=NULL
, node
=keyblock
; node
; last
= node
, node
= node
->next
) {
1416 if( node
->pkt
->pkttype
== PKT_USER_ID
)
1420 assert (last
); /* the user ID is never the first packet */
1421 assert (primary0
); /* ditto (this is the node before primary) */
1422 if ( node
== primary
)
1423 return; /* already the first one */
1425 last
->next
= primary
;
1426 primary0
->next
= primary2
->next
;
1427 primary2
->next
= node
;
1431 reorder_keyblock (KBNODE keyblock
)
1433 do_reorder_keyblock(keyblock
,1);
1434 do_reorder_keyblock(keyblock
,0);
1438 list_keyblock( KBNODE keyblock
, int secret
, int fpr
, void *opaque
)
1440 reorder_keyblock (keyblock
);
1441 if (opt
.with_colons
)
1442 list_keyblock_colon (keyblock
, secret
, fpr
);
1444 list_keyblock_print (keyblock
, secret
, fpr
, opaque
);
1448 * standard function to print the finperprint.
1449 * mode 0: as used in key listings, opt.with_colons is honored
1450 * 1: print using log_info ()
1451 * 2: direct use of tty
1452 * 3: direct use of tty but only primary key.
1453 * modes 1 and 2 will try and print both subkey and primary key fingerprints
1456 print_fingerprint (PKT_public_key
*pk
, PKT_secret_key
*sk
, int mode
)
1458 byte array
[MAX_FINGERPRINT_LEN
], *p
;
1466 if(sk
->main_keyid
[0]==sk
->keyid
[0] && sk
->main_keyid
[1]==sk
->keyid
[1])
1471 if(pk
->main_keyid
[0]==pk
->keyid
[0] && pk
->main_keyid
[1]==pk
->keyid
[1])
1475 /* Just to be safe */
1476 if(mode
&0x80 && !primary
)
1478 log_error("primary key is not really primary!\n");
1484 if(!primary
&& (mode
==1 || mode
==2))
1488 PKT_secret_key
*primary_sk
=xmalloc_clear(sizeof(*primary_sk
));
1489 get_seckey(primary_sk
,sk
->main_keyid
);
1490 print_fingerprint(NULL
,primary_sk
,mode
|0x80);
1491 free_secret_key(primary_sk
);
1495 PKT_public_key
*primary_pk
=xmalloc_clear(sizeof(*primary_pk
));
1496 get_pubkey(primary_pk
,pk
->main_keyid
);
1497 print_fingerprint(primary_pk
,NULL
,mode
|0x80);
1498 free_public_key(primary_pk
);
1505 text
= _("Primary key fingerprint:");
1507 text
= _(" Subkey fingerprint:");
1509 else if (mode
== 2) {
1510 fp
= NULL
; /* use tty */
1512 /* TRANSLATORS: this should fit into 24 bytes to that the
1513 * fingerprint data is properly aligned with the user ID */
1514 text
= _(" Primary key fingerprint:");
1516 text
= _(" Subkey fingerprint:");
1518 else if (mode
== 3) {
1519 fp
= NULL
; /* use tty */
1520 text
= _(" Key fingerprint =");
1524 text
= _(" Key fingerprint =");
1528 fingerprint_from_sk (sk
, array
, &n
);
1530 fingerprint_from_pk (pk
, array
, &n
);
1532 if (opt
.with_colons
&& !mode
) {
1533 fprintf (fp
, "fpr:::::::::");
1534 for (i
=0; i
< n
; i
++, p
++ )
1535 fprintf (fp
, "%02X", *p
);
1542 tty_printf ("%s", text
);
1544 for (i
=0; i
< n
; i
++, i
++, p
+= 2 ) {
1548 fprintf (fp
, " %02X%02X", *p
, p
[1] );
1553 tty_printf (" %02X%02X", *p
, p
[1]);
1558 for (i
=0; i
< n
; i
++, p
++ ) {
1562 fprintf (fp
, " %02X", *p
);
1567 tty_printf (" %02X", *p
);
1578 /* Print the serial number of an OpenPGP card if available. */
1580 print_card_serialno (PKT_secret_key
*sk
)
1586 if (!sk
->is_protected
|| sk
->protect
.s2k
.mode
!= 1002)
1587 return; /* Not a card. */
1588 if (opt
.with_colons
)
1589 return; /* Handled elsewhere. */
1591 fputs (_(" Card serial no. ="), stdout
);
1593 if (sk
->protect
.ivlen
== 16
1594 && !memcmp (sk
->protect
.iv
, "\xD2\x76\x00\x01\x24\x01", 6) )
1595 { /* This is an OpenPGP card. Just print the relevant part. */
1596 for (i
=8; i
< 14; i
++)
1600 printf ("%02X", sk
->protect
.iv
[i
]);
1604 { /* Something is wrong: Print all. */
1605 for (i
=0; i
< sk
->protect
.ivlen
; i
++)
1606 printf ("%02X", sk
->protect
.iv
[i
]);
1613 void set_attrib_fd(int fd
)
1615 static int last_fd
=-1;
1617 if ( fd
!= -1 && last_fd
== fd
)
1620 if ( attrib_fp
&& attrib_fp
!= stdout
&& attrib_fp
!= stderr
)
1631 attrib_fp
= fdopen( fd
, "wb" );
1633 log_fatal("can't open fd %d for attribute output: %s\n",
1634 fd
, strerror(errno
));