See ChangeLog: Wed Mar 10 11:26:18 CET 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
168     /* get the keyid from the keyblock */
169     node = find_kbnode( keyblock, secret? PKT_SECRET_KEY : PKT_PUBLIC_KEY );
170     if( !node ) {
171         log_error("Oops; key lost!\n");
172         dump_kbnode( keyblock );
173         return;
174     }
175
176     if( secret ) {
177         pk = NULL;
178         sk = node->pkt->pkt.secret_key;
179         keyid_from_sk( sk, keyid );
180         if( opt.with_colons )
181             printf("sec:u:%u:%d:%08lX%08lX:%s:%s:::",
182                     nbits_from_sk( sk ),
183                     sk->pubkey_algo,
184                     (ulong)keyid[0],(ulong)keyid[1],
185                     datestr_from_sk( sk ),
186                     sk->expiredate? strtimestamp(sk->expiredate):""
187                     /* fixme: add LID here */ );
188         else
189             printf("sec  %4u%c/%08lX %s ", nbits_from_sk( sk ),
190                                        pubkey_letter( sk->pubkey_algo ),
191                                        (ulong)keyid[1],
192                                        datestr_from_sk( sk ) );
193     }
194     else {
195         pk = node->pkt->pkt.public_key;
196         sk = NULL;
197         keyid_from_pk( pk, keyid );
198         if( opt.with_colons ) {
199             trustletter = query_trust_info( pk, NULL );
200             printf("pub:%c:%u:%d:%08lX%08lX:%s:%s:",
201                     trustletter,
202                     nbits_from_pk( pk ),
203                     pk->pubkey_algo,
204                     (ulong)keyid[0],(ulong)keyid[1],
205                     datestr_from_pk( pk ),
206                     pk->expiredate? strtimestamp(pk->expiredate):""
207                                              );
208             if( pk->local_id )
209                 printf("%lu", pk->local_id );
210             putchar(':');
211             if( pk->local_id )
212                 putchar( get_ownertrust_info( pk->local_id ) );
213             putchar(':');
214         }
215         else
216             printf("pub  %4u%c/%08lX %s ", nbits_from_pk( pk ),
217                                        pubkey_letter( pk->pubkey_algo ),
218                                        (ulong)keyid[1],
219                                        datestr_from_pk( pk ) );
220     }
221
222     for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
223         if( node->pkt->pkttype == PKT_USER_ID ) {
224             if( any ) {
225                 if( opt.with_colons ) {
226                     byte namehash[20];
227
228                     if( pk ) {
229                         rmd160_hash_buffer( namehash,
230                                         node->pkt->pkt.user_id->name,
231                                         node->pkt->pkt.user_id->len  );
232                         trustletter = query_trust_info( pk, namehash );
233                     }
234                     else
235                         trustletter = 'u';
236                     printf("uid:%c::::::::", trustletter);
237                 }
238                 else
239                     printf("uid%*s", 28, "");
240             }
241             print_string( stdout,  node->pkt->pkt.user_id->name,
242                           node->pkt->pkt.user_id->len, opt.with_colons );
243             if( opt.with_colons )
244                 putchar(':');
245             putchar('\n');
246             if( !any ) {
247                 if( opt.fingerprint )
248                     fingerprint( pk, sk );
249                 any = 1;
250             }
251         }
252         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
253             u32 keyid2[2];
254             PKT_public_key *pk2 = node->pkt->pkt.public_key;
255
256             if( !any ) {
257                 putchar('\n');
258                 if( opt.fingerprint )
259                     fingerprint( pk, sk ); /* of the main key */
260                 any = 1;
261             }
262
263
264
265             keyid_from_pk( pk2, keyid2 );
266             if( opt.with_colons ) {
267                 printf("sub:%c:%u:%d:%08lX%08lX:%s:%s:",
268                         trustletter,
269                         nbits_from_pk( pk2 ),
270                         pk2->pubkey_algo,
271                         (ulong)keyid2[0],(ulong)keyid2[1],
272                         datestr_from_pk( pk2 ),
273                         pk2->expiredate? strtimestamp(pk2->expiredate):""
274                         /* fixme: add LID and ownertrust here */
275                                                 );
276                 if( pk->local_id ) /* use the local_id of the main key??? */
277                     printf("%lu", pk->local_id );
278                 putchar(':');
279                 putchar(':');
280                 putchar('\n');
281             }
282             else
283                 printf("sub  %4u%c/%08lX %s\n", nbits_from_pk( pk2 ),
284                                            pubkey_letter( pk2->pubkey_algo ),
285                                            (ulong)keyid2[1],
286                                            datestr_from_pk( pk2 ) );
287             if( opt.fingerprint > 1 )
288                 fingerprint( pk2, NULL );
289         }
290         else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
291             u32 keyid2[2];
292             PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
293
294             if( !any ) {
295                 putchar('\n');
296                 if( opt.fingerprint )
297                     fingerprint( pk, sk ); /* of the main key */
298                 any = 1;
299             }
300
301             keyid_from_sk( sk2, keyid2 );
302             if( opt.with_colons )
303                 printf("ssb::%u:%d:%08lX%08lX:%s:%s:::\n",
304                         nbits_from_sk( sk2 ),
305                         sk2->pubkey_algo,
306                         (ulong)keyid2[0],(ulong)keyid2[1],
307                         datestr_from_sk( sk2 ),
308                         sk2->expiredate? strtimestamp(sk2->expiredate):""
309                         /* fixme: add LID */
310                                                 );
311             else
312                 printf("ssb  %4u%c/%08lX %s\n", nbits_from_sk( sk2 ),
313                                            pubkey_letter( sk2->pubkey_algo ),
314                                            (ulong)keyid2[1],
315                                            datestr_from_sk( sk2 ) );
316             if( opt.fingerprint > 1 )
317                 fingerprint( NULL, sk2 );
318
319         }
320         else if( opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE ) {
321             PKT_signature *sig = node->pkt->pkt.signature;
322             int sigrc;
323
324             if( !any ) { /* no user id, (maybe a revocation follows)*/
325                 if( sig->sig_class == 0x20 )
326                     puts("[revoked]");
327                 else if( sig->sig_class == 0x18 )
328                     puts("[key binding]");
329                 else
330                     putchar('\n');
331                 if( opt.fingerprint )
332                     fingerprint( pk, sk );
333                 any=1;
334             }
335
336             if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
337                 fputs("rev", stdout);
338             else if( (sig->sig_class&~3) == 0x10 )
339                 fputs("sig", stdout);
340             else if( sig->sig_class == 0x18 )
341                 fputs("sig", stdout);
342             else {
343                 if( opt.with_colons )
344                     printf("sig::::::::::%02x:\n",sig->sig_class );
345                 else
346                     printf("sig                             "
347                        "[unexpected signature class 0x%02x]\n",sig->sig_class );
348                 continue;
349             }
350             if( opt.check_sigs ) {
351                 fflush(stdout);
352                 rc = check_key_signature( keyblock, node, NULL );
353                 switch( rc ) {
354                   case 0:                  sigrc = '!'; break;
355                   case G10ERR_BAD_SIGN:    sigrc = '-'; break;
356                   case G10ERR_NO_PUBKEY:   sigrc = '?'; break;
357                   default:                 sigrc = '%'; break;
358                 }
359             }
360             else {
361                 rc = 0;
362                 sigrc = ' ';
363             }
364             if( opt.with_colons ) {
365                 putchar(':');
366                 if( sigrc != ' ' )
367                     putchar(sigrc);
368                 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
369                            (ulong)sig->keyid[1], datestr_from_sig(sig));
370             }
371             else
372                 printf("%c       %08lX %s  ",
373                     sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
374             if( sigrc == '%' )
375                 printf("[%s] ", g10_errstr(rc) );
376             else if( sigrc == '?' )
377                 ;
378             else {
379                 size_t n;
380                 char *p = get_user_id( sig->keyid, &n );
381                 print_string( stdout, p, n, opt.with_colons );
382                 m_free(p);
383             }
384             if( opt.with_colons )
385                 printf(":%02x:", sig->sig_class );
386             putchar('\n');
387             /* FIXME: check or list other sigs here (subpkt PRIV_ADD_SIG)*/
388         }
389     }
390     if( !any ) {/* oops, no user id */
391         if( opt.with_colons )
392             putchar(':');
393         putchar('\n');
394     }
395     else if( !opt.with_colons )
396         putchar('\n');  /* separator line */
397 }
398
399
400
401 static void
402 fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
403 {
404     byte *array, *p;
405     size_t i, n;
406
407     p = array = pk? fingerprint_from_pk( pk, NULL, &n )
408                    : fingerprint_from_sk( sk, NULL, &n );
409     if( opt.with_colons ) {
410         printf("fpr:::::::::");
411         for(i=0; i < n ; i++, p++ )
412             printf("%02X", *p );
413         putchar(':');
414     }
415     else {
416         printf("     Key fingerprint =");
417         if( n == 20 ) {
418             for(i=0; i < n ; i++, i++, p += 2 ) {
419                 if( i == 10 )
420                     putchar(' ');
421                 printf(" %02X%02X", *p, p[1] );
422             }
423         }
424         else {
425             for(i=0; i < n ; i++, p++ ) {
426                 if( i && !(i%8) )
427                     putchar(' ');
428                 printf(" %02X", *p );
429             }
430         }
431     }
432     putchar('\n');
433     m_free(array);
434 }
435