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