bug fix release
[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 fingerprint( PKT_public_key *pk, PKT_secret_key *sk );
41
42
43 /****************
44  * List the keys
45  * If NNAMES is 0; all available keys are listed
46  */
47 void
48 public_key_list( int nnames, char **names )
49 {
50     if( !nnames )
51         list_all(0);
52     else { /* List by user id */
53         for( ; nnames ; nnames--, names++ )
54             list_one( *names, 0 );
55     }
56 }
57
58 void
59 secret_key_list( int nnames, char **names )
60 {
61     if( !nnames )
62         list_all(1);
63     else { /* List by user id */
64         for( ; nnames ; nnames--, names++ )
65             list_one( *names, 1 );
66     }
67 }
68
69
70 static void
71 list_all( int secret )
72 {
73     int i, seq=0;
74     const char *s;
75     IOBUF a;
76
77     while( (s=secret? get_secret_keyring(seq++):get_keyring(seq++)) ) {
78         if( !(a = iobuf_open(s)) ) {
79             log_error(_("can't open %s: %s\n"), s, strerror(errno));
80             continue;
81         }
82         if( seq > 1 )
83             putchar('\n');
84         printf("%s\n", s );
85         for(i=strlen(s); i; i-- )
86             putchar('-');
87         putchar('\n');
88
89         proc_packets( a );
90         iobuf_close(a);
91     }
92 }
93
94 static void
95 list_one( const char *name, int secret )
96 {
97     int rc = 0;
98     KBNODE keyblock = NULL;
99     KBNODE kbctx;
100     KBNODE node;
101     KBPOS kbpos;
102     PKT_public_key *pk;
103     PKT_secret_key *sk;
104     u32 keyid[2];
105     int any=0;
106     int trustletter = 0;
107
108     /* search the userid */
109     rc = secret? find_secret_keyblock_byname( &kbpos, name )
110                : find_keyblock_byname( &kbpos, name );
111     if( rc ) {
112         log_error("%s: user not found\n", name );
113         goto leave;
114     }
115
116     /* read the keyblock */
117     rc = read_keyblock( &kbpos, &keyblock );
118     if( rc ) {
119         log_error("%s: keyblock read problem: %s\n", name, g10_errstr(rc) );
120         goto leave;
121     }
122
123
124     /* get the keyid from the keyblock */
125     node = find_kbnode( keyblock, secret? PKT_SECRET_KEY : PKT_PUBLIC_KEY );
126     if( !node ) {
127         log_error("Oops; key lost!\n");
128         goto leave;
129     }
130
131     if( secret ) {
132         pk = NULL;
133         sk = node->pkt->pkt.secret_key;
134         keyid_from_sk( sk, keyid );
135         if( opt.with_colons )
136             printf("sec::%u:%d:%08lX%08lX:%s:%u:::",
137                     nbits_from_sk( sk ),
138                     sk->pubkey_algo,
139                     (ulong)keyid[0],(ulong)keyid[1],
140                     datestr_from_sk( sk ),
141                     (unsigned)sk->valid_days
142                     /* fixme: add LID here */ );
143         else
144             printf("sec  %4u%c/%08lX %s ", nbits_from_sk( sk ),
145                                        pubkey_letter( sk->pubkey_algo ),
146                                        (ulong)keyid[1],
147                                        datestr_from_sk( sk ) );
148     }
149     else {
150         pk = node->pkt->pkt.public_key;
151         sk = NULL;
152         keyid_from_pk( pk, keyid );
153         if( opt.with_colons ) {
154             trustletter = query_trust_info( pk );
155             printf("pub:%c:%u:%d:%08lX%08lX:%s:%u:",
156                     trustletter,
157                     nbits_from_pk( pk ),
158                     pk->pubkey_algo,
159                     (ulong)keyid[0],(ulong)keyid[1],
160                     datestr_from_pk( pk ),
161                     (unsigned)pk->valid_days );
162             if( pk->local_id )
163                 printf("%lu", pk->local_id );
164             putchar(':');
165             /* fixme: add ownertrust here */
166             putchar(':');
167         }
168         else
169             printf("pub  %4u%c/%08lX %s ", nbits_from_pk( pk ),
170                                        pubkey_letter( pk->pubkey_algo ),
171                                        (ulong)keyid[1],
172                                        datestr_from_pk( pk ) );
173     }
174
175     for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
176         if( node->pkt->pkttype == PKT_USER_ID ) {
177             if( any ) {
178                 if( opt.with_colons )
179                     printf("uid:::::::::");
180                 else
181                     printf("uid%*s", 28, "");
182             }
183             print_string( stdout,  node->pkt->pkt.user_id->name,
184                           node->pkt->pkt.user_id->len, opt.with_colons );
185             if( opt.with_colons )
186                 putchar(':');
187             putchar('\n');
188             if( !any ) {
189                 if( opt.fingerprint )
190                     fingerprint( pk, sk );
191                 any = 1;
192             }
193         }
194         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
195             u32 keyid2[2];
196             PKT_public_key *pk2 = node->pkt->pkt.public_key;
197
198             if( !any ) {
199                 putchar('\n');
200                 if( opt.fingerprint )
201                     fingerprint( pk, sk ); /* of the main key */
202                 any = 1;
203             }
204
205
206
207             keyid_from_pk( pk2, keyid2 );
208             if( opt.with_colons ) {
209                 printf("sub:%c:%u:%d:%08lX%08lX:%s:%u:",
210                         trustletter,
211                         nbits_from_pk( pk2 ),
212                         pk2->pubkey_algo,
213                         (ulong)keyid2[0],(ulong)keyid2[1],
214                         datestr_from_pk( pk2 ),
215                         (unsigned)pk2->valid_days
216                         /* fixme: add LID and ownertrust here */
217                                                 );
218                 if( pk->local_id ) /* use the local_id of the main key??? */
219                     printf("%lu", pk->local_id );
220                 putchar(':');
221                 putchar(':');
222                 putchar('\n');
223             }
224             else
225                 printf("sub  %4u%c/%08lX %s\n", nbits_from_pk( pk2 ),
226                                            pubkey_letter( pk2->pubkey_algo ),
227                                            (ulong)keyid2[1],
228                                            datestr_from_pk( pk2 ) );
229             if( opt.fingerprint > 1 )
230                 fingerprint( pk2, NULL );
231         }
232         else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
233             u32 keyid2[2];
234             PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
235
236             if( !any ) {
237                 putchar('\n');
238                 if( opt.fingerprint )
239                     fingerprint( pk, sk ); /* of the main key */
240                 any = 1;
241             }
242
243             keyid_from_sk( sk2, keyid2 );
244             if( opt.with_colons )
245                 printf("ssb::%u:%d:%08lX%08lX:%s:%u:::\n",
246                         nbits_from_sk( sk2 ),
247                         sk2->pubkey_algo,
248                         (ulong)keyid2[0],(ulong)keyid2[1],
249                         datestr_from_sk( sk2 ),
250                         (unsigned)sk2->valid_days
251                         /* fixme: add LID */
252                                                 );
253             else
254                 printf("ssb  %4u%c/%08lX %s\n", nbits_from_sk( sk2 ),
255                                            pubkey_letter( sk2->pubkey_algo ),
256                                            (ulong)keyid2[1],
257                                            datestr_from_sk( sk2 ) );
258             if( opt.fingerprint > 1 )
259                 fingerprint( NULL, sk2 );
260
261         }
262         else if( opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE ) {
263             PKT_signature *sig = node->pkt->pkt.signature;
264             int sigrc;
265
266             if( !any ) { /* no user id, (maybe a revocation follows)*/
267                 if( sig->sig_class == 0x20 )
268                     puts("[revoked]");
269                 else if( sig->sig_class == 0x18 )
270                     puts("[key binding]");
271                 else
272                     putchar('\n');
273                 if( opt.fingerprint )
274                     fingerprint( pk, sk );
275                 any=1;
276             }
277
278             if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
279                 fputs("rev", stdout);
280             else if( (sig->sig_class&~3) == 0x10 )
281                 fputs("sig", stdout);
282             else if( sig->sig_class == 0x18 )
283                 fputs("sig", stdout);
284             else {
285                 if( opt.with_colons )
286                     printf("sig::::::::::%02x:\n",sig->sig_class );
287                 else
288                     printf("sig                             "
289                        "[unexpected signature class 0x%02x]\n",sig->sig_class );
290                 continue;
291             }
292             if( opt.check_sigs ) {
293                 fflush(stdout);
294                 rc = check_key_signature( keyblock, node, NULL );
295                 switch( rc ) {
296                   case 0:                  sigrc = '!'; break;
297                   case G10ERR_BAD_SIGN:    sigrc = '-'; break;
298                   case G10ERR_NO_PUBKEY:   sigrc = '?'; break;
299                   default:                 sigrc = '%'; break;
300                 }
301             }
302             else {
303                 rc = 0;
304                 sigrc = ' ';
305             }
306             if( opt.with_colons ) {
307                 putchar(':');
308                 if( sigrc != ' ' )
309                     putchar(sigrc);
310                 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
311                            (ulong)sig->keyid[1], datestr_from_sig(sig));
312             }
313             else
314                 printf("%c       %08lX %s  ",
315                     sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
316             if( sigrc == '%' )
317                 printf("[%s] ", g10_errstr(rc) );
318             else if( sigrc == '?' )
319                 ;
320             else {
321                 size_t n;
322                 char *p = get_user_id( sig->keyid, &n );
323                 print_string( stdout, p, n, opt.with_colons );
324                 m_free(p);
325             }
326             if( opt.with_colons )
327                 printf(":%02x:", sig->sig_class );
328             putchar('\n');
329             /* FIXME: check or list other sigs here (subpkt PRIV_ADD_SIG)*/
330         }
331     }
332     if( !any ) {/* oops, no user id */
333         if( opt.with_colons )
334             putchar(':');
335         putchar('\n');
336     }
337
338
339   leave:
340     release_kbnode( keyblock );
341 }
342
343 static void
344 fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
345 {
346     byte *array, *p;
347     size_t i, n;
348
349     p = array = pk? fingerprint_from_pk( pk, NULL, &n )
350                    : fingerprint_from_sk( sk, NULL, &n );
351     if( opt.with_colons ) {
352         printf("fpr:::::::::");
353         for(i=0; i < n ; i++, p++ )
354             printf("%02X", *p );
355         putchar(':');
356     }
357     else {
358         printf("     Key fingerprint =");
359         if( n == 20 ) {
360             for(i=0; i < n ; i++, i++, p += 2 ) {
361                 if( i == 10 )
362                     putchar(' ');
363                 printf(" %02X%02X", *p, p[1] );
364             }
365         }
366         else {
367             for(i=0; i < n ; i++, p++ ) {
368                 if( i && !(i%8) )
369                     putchar(' ');
370                 printf(" %02X", *p );
371             }
372         }
373     }
374     putchar('\n');
375     m_free(array);
376 }
377