See ChangeLog: Sun Apr 18 20:48:15 CEST 1999 Werner Koch
[gnupg.git] / g10 / keylist.c
1 /* keylist.c
2  *      Copyright (C) 1998 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
27
28 #include "options.h"
29 #include "packet.h"
30 #include "errors.h"
31 #include "keydb.h"
32 #include "memory.h"
33 #include "util.h"
34 #include "trustdb.h"
35 #include "main.h"
36 #include "i18n.h"
37
38 static void list_all(int);
39 static void list_one( STRLIST names, int secret);
40 static void list_keyblock( KBNODE keyblock, int secret );
41 static void fingerprint( PKT_public_key *pk, PKT_secret_key *sk );
42
43
44 /****************
45  * List the keys
46  * If NNAMES is 0; all available keys are listed
47  */
48 void
49 public_key_list( int nnames, char **names )
50 {
51     if( !nnames )
52         list_all(0);
53     else { /* List by user id */
54         STRLIST list = NULL;
55         for( ; nnames ; nnames--, names++ )
56             add_to_strlist( &list, *names );
57         list_one( list, 0 );
58         free_strlist( list );
59     }
60 }
61
62 void
63 secret_key_list( int nnames, char **names )
64 {
65     if( !nnames )
66         list_all(1);
67     else { /* List by user id */
68         STRLIST list = NULL;
69         for( ; nnames ; nnames--, names++ )
70             add_to_strlist( &list, *names );
71         list_one( list, 1 );
72         free_strlist( list );
73     }
74 }
75
76
77 static void
78 list_all( int secret )
79 {
80     KBPOS kbpos;
81     KBNODE keyblock = NULL;
82     int rc=0;
83     int lastresno;
84
85     rc = enum_keyblocks( secret? 5:0, &kbpos, &keyblock );
86     if( rc ) {
87         if( rc != -1 )
88             log_error("enum_keyblocks(open) failed: %s\n", g10_errstr(rc) );
89         goto leave;
90     }
91
92     lastresno = -1;
93     while( !(rc = enum_keyblocks( 1, &kbpos, &keyblock )) ) {
94         if( lastresno != kbpos.resno ) {
95             const char *s = keyblock_resource_name( &kbpos );
96             int i;
97
98             lastresno = kbpos.resno;
99             printf("%s\n", s );
100             for(i=strlen(s); i; i-- )
101                 putchar('-');
102             putchar('\n');
103         }
104         merge_keys_and_selfsig( keyblock );
105         list_keyblock( keyblock, secret );
106         release_kbnode( keyblock ); keyblock = NULL;
107     }
108
109     if( rc && rc != -1 )
110         log_error("enum_keyblocks(read) failed: %s\n", g10_errstr(rc));
111
112   leave:
113     enum_keyblocks( 2, &kbpos, &keyblock ); /* close */
114     release_kbnode( keyblock );
115 }
116
117
118
119 static void
120 list_one( STRLIST names, int secret )
121 {
122     int rc = 0;
123     KBNODE keyblock = NULL;
124     GETKEY_CTX ctx;
125
126     if( secret ) {
127         rc = get_seckey_bynames( &ctx, NULL, names, &keyblock );
128         if( rc ) {
129             log_error("error reading key: %s\n",  g10_errstr(rc) );
130             get_seckey_end( ctx );
131             return;
132         }
133         do {
134             merge_keys_and_selfsig( keyblock );
135             list_keyblock( keyblock, 1 );
136             release_kbnode( keyblock );
137         } while( !get_seckey_next( ctx, NULL, &keyblock ) );
138         get_seckey_end( ctx );
139     }
140     else {
141         rc = get_pubkey_bynames( &ctx, NULL, names, &keyblock );
142         if( rc ) {
143             log_error("error reading key: %s\n", g10_errstr(rc) );
144             get_pubkey_end( ctx );
145             return;
146         }
147         do {
148             list_keyblock( keyblock, 0 );
149             release_kbnode( keyblock );
150         } while( !get_pubkey_next( ctx, NULL, &keyblock ) );
151         get_pubkey_end( ctx );
152     }
153 }
154
155
156 static void
157 list_keyblock( KBNODE keyblock, int secret )
158 {
159     int rc = 0;
160     KBNODE kbctx;
161     KBNODE node;
162     PKT_public_key *pk;
163     PKT_secret_key *sk;
164     u32 keyid[2];
165     int any=0;
166     int trustletter = 0;
167     int ulti_hack = 0;
168
169     /* get the keyid from the keyblock */
170     node = find_kbnode( keyblock, secret? PKT_SECRET_KEY : PKT_PUBLIC_KEY );
171     if( !node ) {
172         log_error("Oops; key lost!\n");
173         dump_kbnode( keyblock );
174         return;
175     }
176
177     if( secret ) {
178         pk = NULL;
179         sk = node->pkt->pkt.secret_key;
180         keyid_from_sk( sk, keyid );
181         if( opt.with_colons )
182             printf("sec:u:%u:%d:%08lX%08lX:%s:%s:::",
183                     nbits_from_sk( sk ),
184                     sk->pubkey_algo,
185                     (ulong)keyid[0],(ulong)keyid[1],
186                     datestr_from_sk( sk ),
187                     sk->expiredate? strtimestamp(sk->expiredate):""
188                     /* fixme: add LID here */ );
189         else
190             printf("sec  %4u%c/%08lX %s ", nbits_from_sk( sk ),
191                                        pubkey_letter( sk->pubkey_algo ),
192                                        (ulong)keyid[1],
193                                        datestr_from_sk( sk ) );
194     }
195     else {
196         pk = node->pkt->pkt.public_key;
197         sk = NULL;
198         keyid_from_pk( pk, keyid );
199         if( opt.with_colons ) {
200             trustletter = query_trust_info( pk, NULL );
201             if( trustletter == 'u' )
202                 ulti_hack = 1;
203             printf("pub:%c:%u:%d:%08lX%08lX:%s:%s:",
204                     trustletter,
205                     nbits_from_pk( pk ),
206                     pk->pubkey_algo,
207                     (ulong)keyid[0],(ulong)keyid[1],
208                     datestr_from_pk( pk ),
209                     pk->expiredate? strtimestamp(pk->expiredate):""
210                                              );
211             if( pk->local_id )
212                 printf("%lu", pk->local_id );
213             putchar(':');
214             if( pk->local_id )
215                 putchar( get_ownertrust_info( pk->local_id ) );
216             putchar(':');
217         }
218         else
219             printf("pub  %4u%c/%08lX %s ", nbits_from_pk( pk ),
220                                        pubkey_letter( pk->pubkey_algo ),
221                                        (ulong)keyid[1],
222                                        datestr_from_pk( pk ) );
223     }
224
225     for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
226         if( node->pkt->pkttype == PKT_USER_ID ) {
227             if( any ) {
228                 if( opt.with_colons ) {
229                     byte namehash[20];
230
231                     if( pk && !ulti_hack ) {
232                         rmd160_hash_buffer( namehash,
233                                         node->pkt->pkt.user_id->name,
234                                         node->pkt->pkt.user_id->len  );
235                         trustletter = query_trust_info( pk, namehash );
236                     }
237                     else
238                         trustletter = 'u';
239                     printf("uid:%c::::::::", trustletter);
240                 }
241                 else
242                     printf("uid%*s", 28, "");
243             }
244             print_string( stdout,  node->pkt->pkt.user_id->name,
245                           node->pkt->pkt.user_id->len, opt.with_colons );
246             if( opt.with_colons )
247                 putchar(':');
248             putchar('\n');
249             if( !any ) {
250                 if( opt.fingerprint )
251                     fingerprint( pk, sk );
252                 any = 1;
253             }
254         }
255         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
256             u32 keyid2[2];
257             PKT_public_key *pk2 = node->pkt->pkt.public_key;
258
259             if( !any ) {
260                 putchar('\n');
261                 if( opt.fingerprint )
262                     fingerprint( pk, sk ); /* of the main key */
263                 any = 1;
264             }
265
266
267
268             keyid_from_pk( pk2, keyid2 );
269             if( opt.with_colons ) {
270                 printf("sub:%c:%u:%d:%08lX%08lX:%s:%s:",
271                         trustletter,
272                         nbits_from_pk( pk2 ),
273                         pk2->pubkey_algo,
274                         (ulong)keyid2[0],(ulong)keyid2[1],
275                         datestr_from_pk( pk2 ),
276                         pk2->expiredate? strtimestamp(pk2->expiredate):""
277                         /* fixme: add LID and ownertrust here */
278                                                 );
279                 if( pk->local_id ) /* use the local_id of the main key??? */
280                     printf("%lu", pk->local_id );
281                 putchar(':');
282                 putchar(':');
283                 putchar('\n');
284             }
285             else
286                 printf("sub  %4u%c/%08lX %s\n", nbits_from_pk( pk2 ),
287                                            pubkey_letter( pk2->pubkey_algo ),
288                                            (ulong)keyid2[1],
289                                            datestr_from_pk( pk2 ) );
290             if( opt.fingerprint > 1 )
291                 fingerprint( pk2, NULL );
292         }
293         else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
294             u32 keyid2[2];
295             PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
296
297             if( !any ) {
298                 putchar('\n');
299                 if( opt.fingerprint )
300                     fingerprint( pk, sk ); /* of the main key */
301                 any = 1;
302             }
303
304             keyid_from_sk( sk2, keyid2 );
305             if( opt.with_colons )
306                 printf("ssb::%u:%d:%08lX%08lX:%s:%s:::\n",
307                         nbits_from_sk( sk2 ),
308                         sk2->pubkey_algo,
309                         (ulong)keyid2[0],(ulong)keyid2[1],
310                         datestr_from_sk( sk2 ),
311                         sk2->expiredate? strtimestamp(sk2->expiredate):""
312                         /* fixme: add LID */
313                                                 );
314             else
315                 printf("ssb  %4u%c/%08lX %s\n", nbits_from_sk( sk2 ),
316                                            pubkey_letter( sk2->pubkey_algo ),
317                                            (ulong)keyid2[1],
318                                            datestr_from_sk( sk2 ) );
319             if( opt.fingerprint > 1 )
320                 fingerprint( NULL, sk2 );
321
322         }
323         else if( opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE ) {
324             PKT_signature *sig = node->pkt->pkt.signature;
325             int sigrc;
326
327             if( !any ) { /* no user id, (maybe a revocation follows)*/
328                 if( sig->sig_class == 0x20 )
329                     puts("[revoked]");
330                 else if( sig->sig_class == 0x18 )
331                     puts("[key binding]");
332                 else
333                     putchar('\n');
334                 if( opt.fingerprint )
335                     fingerprint( pk, sk );
336                 any=1;
337             }
338
339             if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
340                 fputs("rev", stdout);
341             else if( (sig->sig_class&~3) == 0x10 )
342                 fputs("sig", stdout);
343             else if( sig->sig_class == 0x18 )
344                 fputs("sig", stdout);
345             else {
346                 if( opt.with_colons )
347                     printf("sig::::::::::%02x:\n",sig->sig_class );
348                 else
349                     printf("sig                             "
350                        "[unexpected signature class 0x%02x]\n",sig->sig_class );
351                 continue;
352             }
353             if( opt.check_sigs ) {
354                 fflush(stdout);
355                 rc = check_key_signature( keyblock, node, NULL );
356                 switch( rc ) {
357                   case 0:                  sigrc = '!'; break;
358                   case G10ERR_BAD_SIGN:    sigrc = '-'; break;
359                   case G10ERR_NO_PUBKEY:   sigrc = '?'; break;
360                   default:                 sigrc = '%'; break;
361                 }
362             }
363             else {
364                 rc = 0;
365                 sigrc = ' ';
366             }
367             if( opt.with_colons ) {
368                 putchar(':');
369                 if( sigrc != ' ' )
370                     putchar(sigrc);
371                 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
372                            (ulong)sig->keyid[1], datestr_from_sig(sig));
373             }
374             else
375                 printf("%c       %08lX %s  ",
376                     sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
377             if( sigrc == '%' )
378                 printf("[%s] ", g10_errstr(rc) );
379             else if( sigrc == '?' )
380                 ;
381             else {
382                 size_t n;
383                 char *p = get_user_id( sig->keyid, &n );
384                 print_string( stdout, p, n, opt.with_colons );
385                 m_free(p);
386             }
387             if( opt.with_colons )
388                 printf(":%02x:", sig->sig_class );
389             putchar('\n');
390             /* FIXME: check or list other sigs here (subpkt PRIV_ADD_SIG)*/
391         }
392     }
393     if( !any ) {/* oops, no user id */
394         if( opt.with_colons )
395             putchar(':');
396         putchar('\n');
397     }
398     else if( !opt.with_colons )
399         putchar('\n');  /* separator line */
400 }
401
402
403
404 static void
405 fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
406 {
407     byte *array, *p;
408     size_t i, n;
409
410     p = array = pk? fingerprint_from_pk( pk, NULL, &n )
411                    : fingerprint_from_sk( sk, NULL, &n );
412     if( opt.with_colons ) {
413         printf("fpr:::::::::");
414         for(i=0; i < n ; i++, p++ )
415             printf("%02X", *p );
416         putchar(':');
417     }
418     else {
419         printf("     Key fingerprint =");
420         if( n == 20 ) {
421             for(i=0; i < n ; i++, i++, p += 2 ) {
422                 if( i == 10 )
423                     putchar(' ');
424                 printf(" %02X%02X", *p, p[1] );
425             }
426         }
427         else {
428             for(i=0; i < n ; i++, p++ ) {
429                 if( i && !(i%8) )
430                     putchar(' ');
431                 printf(" %02X", *p );
432             }
433         }
434     }
435     putchar('\n');
436     m_free(array);
437 }
438