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