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