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