some bug fixes
[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(const char *name, 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         for( ; nnames ; nnames--, names++ )
55             list_one( *names, 0 );
56     }
57 }
58
59 void
60 secret_key_list( int nnames, char **names )
61 {
62     if( !nnames )
63         list_all(1);
64     else { /* List by user id */
65         for( ; nnames ; nnames--, names++ )
66             list_one( *names, 1 );
67     }
68 }
69
70
71 static void
72 list_all( int secret )
73 {
74     KBPOS kbpos;
75     KBNODE keyblock = NULL;
76     int rc=0;
77     int lastresno;
78
79     rc = enum_keyblocks( secret? 5:0, &kbpos, &keyblock );
80     if( rc ) {
81         if( rc != -1 )
82             log_error("enum_keyblocks(open) failed: %s\n", g10_errstr(rc) );
83         goto leave;
84     }
85
86     lastresno = -1;
87     while( !(rc = enum_keyblocks( 1, &kbpos, &keyblock )) ) {
88         if( lastresno != kbpos.resno ) {
89             const char *s = keyblock_resource_name( &kbpos );
90             int i;
91
92             lastresno = kbpos.resno;
93             printf("%s\n", s );
94             for(i=strlen(s); i; i-- )
95                 putchar('-');
96             putchar('\n');
97         }
98         merge_keys_and_selfsig( keyblock );
99         list_keyblock( keyblock, secret );
100         release_kbnode( keyblock ); keyblock = NULL;
101     }
102
103     if( rc && rc != -1 )
104         log_error("enum_keyblocks(read) failed: %s\n", g10_errstr(rc));
105
106   leave:
107     enum_keyblocks( 2, &kbpos, &keyblock ); /* close */
108     release_kbnode( keyblock );
109 }
110
111
112
113 static void
114 list_one( const char *name, int secret )
115 {
116     int rc = 0;
117     KBNODE keyblock = NULL;
118
119     if( secret ) {
120         KBPOS kbpos;
121
122         rc = secret? find_secret_keyblock_byname( &kbpos, name )
123                    : find_keyblock_byname( &kbpos, name );
124         if( rc ) {
125             log_error("%s: user not found\n", name );
126             return;
127         }
128
129         rc = read_keyblock( &kbpos, &keyblock );
130         if( rc ) {
131             log_error("%s: keyblock read problem: %s\n", name, g10_errstr(rc) );
132             return;
133         }
134         merge_keys_and_selfsig( keyblock );
135         list_keyblock( keyblock, secret );
136         release_kbnode( keyblock );
137     }
138     else {
139         GETKEY_CTX ctx;
140
141         rc = get_pubkey_byname( &ctx, NULL, name, &keyblock );
142         if( rc ) {
143             log_error("%s: %s\n", name, 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         return;
173     }
174
175     if( secret ) {
176         pk = NULL;
177         sk = node->pkt->pkt.secret_key;
178         keyid_from_sk( sk, keyid );
179         if( opt.with_colons )
180             printf("sec::%u:%d:%08lX%08lX:%s:%s:::",
181                     nbits_from_sk( sk ),
182                     sk->pubkey_algo,
183                     (ulong)keyid[0],(ulong)keyid[1],
184                     datestr_from_sk( sk ),
185                     sk->expiredate? strtimestamp(sk->expiredate):""
186                     /* fixme: add LID here */ );
187         else
188             printf("sec  %4u%c/%08lX %s ", nbits_from_sk( sk ),
189                                        pubkey_letter( sk->pubkey_algo ),
190                                        (ulong)keyid[1],
191                                        datestr_from_sk( sk ) );
192     }
193     else {
194         pk = node->pkt->pkt.public_key;
195         sk = NULL;
196         keyid_from_pk( pk, keyid );
197         if( opt.with_colons ) {
198             trustletter = query_trust_info( pk );
199             printf("pub:%c:%u:%d:%08lX%08lX:%s:%s:",
200                     trustletter,
201                     nbits_from_pk( pk ),
202                     pk->pubkey_algo,
203                     (ulong)keyid[0],(ulong)keyid[1],
204                     datestr_from_pk( pk ),
205                     pk->expiredate? strtimestamp(pk->expiredate):""
206                                              );
207             if( pk->local_id )
208                 printf("%lu", pk->local_id );
209             putchar(':');
210             if( pk->local_id )
211                 putchar( get_ownertrust_info( pk->local_id ) );
212             putchar(':');
213         }
214         else
215             printf("pub  %4u%c/%08lX %s ", nbits_from_pk( pk ),
216                                        pubkey_letter( pk->pubkey_algo ),
217                                        (ulong)keyid[1],
218                                        datestr_from_pk( pk ) );
219     }
220
221     for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
222         if( node->pkt->pkttype == PKT_USER_ID ) {
223             if( any ) {
224                 if( opt.with_colons )
225                     printf("uid:::::::::");
226                 else
227                     printf("uid%*s", 28, "");
228             }
229             print_string( stdout,  node->pkt->pkt.user_id->name,
230                           node->pkt->pkt.user_id->len, opt.with_colons );
231             if( opt.with_colons )
232                 putchar(':');
233             putchar('\n');
234             if( !any ) {
235                 if( opt.fingerprint )
236                     fingerprint( pk, sk );
237                 any = 1;
238             }
239         }
240         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
241             u32 keyid2[2];
242             PKT_public_key *pk2 = node->pkt->pkt.public_key;
243
244             if( !any ) {
245                 putchar('\n');
246                 if( opt.fingerprint )
247                     fingerprint( pk, sk ); /* of the main key */
248                 any = 1;
249             }
250
251
252
253             keyid_from_pk( pk2, keyid2 );
254             if( opt.with_colons ) {
255                 printf("sub:%c:%u:%d:%08lX%08lX:%s:%s:",
256                         trustletter,
257                         nbits_from_pk( pk2 ),
258                         pk2->pubkey_algo,
259                         (ulong)keyid2[0],(ulong)keyid2[1],
260                         datestr_from_pk( pk2 ),
261                         pk2->expiredate? strtimestamp(pk2->expiredate):""
262                         /* fixme: add LID and ownertrust here */
263                                                 );
264                 if( pk->local_id ) /* use the local_id of the main key??? */
265                     printf("%lu", pk->local_id );
266                 putchar(':');
267                 putchar(':');
268                 putchar('\n');
269             }
270             else
271                 printf("sub  %4u%c/%08lX %s\n", nbits_from_pk( pk2 ),
272                                            pubkey_letter( pk2->pubkey_algo ),
273                                            (ulong)keyid2[1],
274                                            datestr_from_pk( pk2 ) );
275             if( opt.fingerprint > 1 )
276                 fingerprint( pk2, NULL );
277         }
278         else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
279             u32 keyid2[2];
280             PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
281
282             if( !any ) {
283                 putchar('\n');
284                 if( opt.fingerprint )
285                     fingerprint( pk, sk ); /* of the main key */
286                 any = 1;
287             }
288
289             keyid_from_sk( sk2, keyid2 );
290             if( opt.with_colons )
291                 printf("ssb::%u:%d:%08lX%08lX:%s:%s:::\n",
292                         nbits_from_sk( sk2 ),
293                         sk2->pubkey_algo,
294                         (ulong)keyid2[0],(ulong)keyid2[1],
295                         datestr_from_sk( sk2 ),
296                         sk2->expiredate? strtimestamp(sk2->expiredate):""
297                         /* fixme: add LID */
298                                                 );
299             else
300                 printf("ssb  %4u%c/%08lX %s\n", nbits_from_sk( sk2 ),
301                                            pubkey_letter( sk2->pubkey_algo ),
302                                            (ulong)keyid2[1],
303                                            datestr_from_sk( sk2 ) );
304             if( opt.fingerprint > 1 )
305                 fingerprint( NULL, sk2 );
306
307         }
308         else if( opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE ) {
309             PKT_signature *sig = node->pkt->pkt.signature;
310             int sigrc;
311
312             if( !any ) { /* no user id, (maybe a revocation follows)*/
313                 if( sig->sig_class == 0x20 )
314                     puts("[revoked]");
315                 else if( sig->sig_class == 0x18 )
316                     puts("[key binding]");
317                 else
318                     putchar('\n');
319                 if( opt.fingerprint )
320                     fingerprint( pk, sk );
321                 any=1;
322             }
323
324             if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
325                 fputs("rev", stdout);
326             else if( (sig->sig_class&~3) == 0x10 )
327                 fputs("sig", stdout);
328             else if( sig->sig_class == 0x18 )
329                 fputs("sig", stdout);
330             else {
331                 if( opt.with_colons )
332                     printf("sig::::::::::%02x:\n",sig->sig_class );
333                 else
334                     printf("sig                             "
335                        "[unexpected signature class 0x%02x]\n",sig->sig_class );
336                 continue;
337             }
338             if( opt.check_sigs ) {
339                 fflush(stdout);
340                 rc = check_key_signature( keyblock, node, NULL );
341                 switch( rc ) {
342                   case 0:                  sigrc = '!'; break;
343                   case G10ERR_BAD_SIGN:    sigrc = '-'; break;
344                   case G10ERR_NO_PUBKEY:   sigrc = '?'; break;
345                   default:                 sigrc = '%'; break;
346                 }
347             }
348             else {
349                 rc = 0;
350                 sigrc = ' ';
351             }
352             if( opt.with_colons ) {
353                 putchar(':');
354                 if( sigrc != ' ' )
355                     putchar(sigrc);
356                 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
357                            (ulong)sig->keyid[1], datestr_from_sig(sig));
358             }
359             else
360                 printf("%c       %08lX %s  ",
361                     sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
362             if( sigrc == '%' )
363                 printf("[%s] ", g10_errstr(rc) );
364             else if( sigrc == '?' )
365                 ;
366             else {
367                 size_t n;
368                 char *p = get_user_id( sig->keyid, &n );
369                 print_string( stdout, p, n, opt.with_colons );
370                 m_free(p);
371             }
372             if( opt.with_colons )
373                 printf(":%02x:", sig->sig_class );
374             putchar('\n');
375             /* FIXME: check or list other sigs here (subpkt PRIV_ADD_SIG)*/
376         }
377     }
378     if( !any ) {/* oops, no user id */
379         if( opt.with_colons )
380             putchar(':');
381         putchar('\n');
382     }
383 }
384
385
386
387 static void
388 fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
389 {
390     byte *array, *p;
391     size_t i, n;
392
393     p = array = pk? fingerprint_from_pk( pk, NULL, &n )
394                    : fingerprint_from_sk( sk, NULL, &n );
395     if( opt.with_colons ) {
396         printf("fpr:::::::::");
397         for(i=0; i < n ; i++, p++ )
398             printf("%02X", *p );
399         putchar(':');
400     }
401     else {
402         printf("     Key fingerprint =");
403         if( n == 20 ) {
404             for(i=0; i < n ; i++, i++, p += 2 ) {
405                 if( i == 10 )
406                     putchar(' ');
407                 printf(" %02X%02X", *p, p[1] );
408             }
409         }
410         else {
411             for(i=0; i < n ; i++, p++ ) {
412                 if( i && !(i%8) )
413                     putchar(' ');
414                 printf(" %02X", *p );
415             }
416         }
417     }
418     putchar('\n');
419     m_free(array);
420 }
421