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