2a471b86ca1d76079a1f153c4940872f15cc57a6
[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         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     else if( !opt.with_colons )
384         putchar('\n');  /* separator line */
385 }
386
387
388
389 static void
390 fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
391 {
392     byte *array, *p;
393     size_t i, n;
394
395     p = array = pk? fingerprint_from_pk( pk, NULL, &n )
396                    : fingerprint_from_sk( sk, NULL, &n );
397     if( opt.with_colons ) {
398         printf("fpr:::::::::");
399         for(i=0; i < n ; i++, p++ )
400             printf("%02X", *p );
401         putchar(':');
402     }
403     else {
404         printf("     Key fingerprint =");
405         if( n == 20 ) {
406             for(i=0; i < n ; i++, i++, p += 2 ) {
407                 if( i == 10 )
408                     putchar(' ');
409                 printf(" %02X%02X", *p, p[1] );
410             }
411         }
412         else {
413             for(i=0; i < n ; i++, p++ ) {
414                 if( i && !(i%8) )
415                     putchar(' ');
416                 printf(" %02X", *p );
417             }
418         }
419     }
420     putchar('\n');
421     m_free(array);
422 }
423