Imported Upstream version 2.1.3
[platform/upstream/gpg2.git] / g10 / keylist.c
1 /* keylist.c - Print information about OpenPGP keys
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3  *               2008, 2010, 2012 Free Software Foundation, Inc.
4  * Copyright (C) 2013, 2014  Werner Koch
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuPG is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28 #ifdef HAVE_DOSISH_SYSTEM
29 #include <fcntl.h>              /* for setmode() */
30 #endif
31
32 #include "gpg.h"
33 #include "options.h"
34 #include "packet.h"
35 #include "status.h"
36 #include "keydb.h"
37 #include "photoid.h"
38 #include "util.h"
39 #include "ttyio.h"
40 #include "trustdb.h"
41 #include "main.h"
42 #include "i18n.h"
43 #include "status.h"
44 #include "call-agent.h"
45 #include "mbox-util.h"
46
47
48 static void list_all (int, int);
49 static void list_one (strlist_t names, int secret, int mark_secret);
50 static void locate_one (ctrl_t ctrl, strlist_t names);
51 static void print_card_serialno (const char *serialno);
52
53 struct keylist_context
54 {
55   int check_sigs;  /* If set signatures shall be verified.  */
56   int inv_sigs;    /* Counter used if CHECK_SIGS is set.  */
57   int no_key;      /* Counter used if CHECK_SIGS is set.  */
58   int oth_err;     /* Counter used if CHECK_SIGS is set.  */
59 };
60
61
62 static void list_keyblock (kbnode_t keyblock, int secret, int has_secret,
63                            int fpr, struct keylist_context *listctx);
64
65
66 /* The stream used to write attribute packets to.  */
67 static estream_t attrib_fp;
68
69
70 /* Release resources from a keylist context.  */
71 static void
72 keylist_context_release (struct keylist_context *listctx)
73 {
74   (void)listctx; /* Nothing to release.  */
75 }
76
77
78 /* List the keys.  If list is NULL, all available keys are listed.
79    With LOCATE_MODE set the locate algorithm is used to find a
80    key.  */
81 void
82 public_key_list (ctrl_t ctrl, strlist_t list, int locate_mode)
83 {
84 #ifndef NO_TRUST_MODELS
85   if (opt.with_colons)
86     {
87       byte trust_model, marginals, completes, cert_depth, min_cert_level;
88       ulong created, nextcheck;
89
90       read_trust_options (&trust_model, &created, &nextcheck,
91                           &marginals, &completes, &cert_depth, &min_cert_level);
92
93       es_fprintf (es_stdout, "tru:");
94
95       if (nextcheck && nextcheck <= make_timestamp ())
96         es_fprintf (es_stdout, "o");
97       if (trust_model != opt.trust_model)
98         es_fprintf (es_stdout, "t");
99       if (opt.trust_model == TM_PGP || opt.trust_model == TM_CLASSIC)
100         {
101           if (marginals != opt.marginals_needed)
102             es_fprintf (es_stdout, "m");
103           if (completes != opt.completes_needed)
104             es_fprintf (es_stdout, "c");
105           if (cert_depth != opt.max_cert_depth)
106             es_fprintf (es_stdout, "d");
107           if (min_cert_level != opt.min_cert_level)
108             es_fprintf (es_stdout, "l");
109         }
110
111       es_fprintf (es_stdout, ":%d:%lu:%lu", trust_model, created, nextcheck);
112
113       /* Only show marginals, completes, and cert_depth in the classic
114          or PGP trust models since they are not meaningful
115          otherwise. */
116
117       if (trust_model == TM_PGP || trust_model == TM_CLASSIC)
118         es_fprintf (es_stdout, ":%d:%d:%d", marginals, completes, cert_depth);
119       es_fprintf (es_stdout, "\n");
120     }
121 #endif /*!NO_TRUST_MODELS*/
122
123   /* We need to do the stale check right here because it might need to
124      update the keyring while we already have the keyring open.  This
125      is very bad for W32 because of a sharing violation. For real OSes
126      it might lead to false results if we are later listing a keyring
127      which is associated with the inode of a deleted file.  */
128   check_trustdb_stale ();
129
130   if (locate_mode)
131     locate_one (ctrl, list);
132   else if (!list)
133     list_all (0, opt.with_secret);
134   else
135     list_one (list, 0, opt.with_secret);
136 }
137
138
139 void
140 secret_key_list (ctrl_t ctrl, strlist_t list)
141 {
142   (void)ctrl;
143
144   check_trustdb_stale ();
145
146   if (!list)
147     list_all (1, 0);
148   else                          /* List by user id */
149     list_one (list, 1, 0);
150 }
151
152 void
153 print_seckey_info (PKT_public_key *pk)
154 {
155   u32 keyid[2];
156   char *p;
157   char pkstrbuf[PUBKEY_STRING_SIZE];
158
159   keyid_from_pk (pk, keyid);
160   p = get_user_id_native (keyid);
161
162   tty_printf ("\nsec  %s/%s %s %s\n",
163               pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
164               keystr (keyid), datestr_from_pk (pk), p);
165
166   xfree (p);
167 }
168
169 /* Print information about the public key.  With FP passed as NULL,
170    the tty output interface is used, otherwise output is directted to
171    the given stream.  */
172 void
173 print_pubkey_info (estream_t fp, PKT_public_key * pk)
174 {
175   u32 keyid[2];
176   char *p;
177   char pkstrbuf[PUBKEY_STRING_SIZE];
178
179   keyid_from_pk (pk, keyid);
180
181   /* If the pk was chosen by a particular user ID, that is the one to
182      print.  */
183   if (pk->user_id)
184     p = utf8_to_native (pk->user_id->name, pk->user_id->len, 0);
185   else
186     p = get_user_id_native (keyid);
187
188   if (fp)
189     tty_printf ("\n");
190   tty_fprintf (fp, "pub  %s/%s %s %s\n",
191                pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
192                keystr (keyid), datestr_from_pk (pk), p);
193   xfree (p);
194 }
195
196
197 /* Print basic information of a secret key including the card serial
198    number information.  */
199 #ifdef ENABLE_CARD_SUPPORT
200 void
201 print_card_key_info (estream_t fp, kbnode_t keyblock)
202 {
203   kbnode_t node;
204   char *hexgrip;
205   char *serialno;
206   int s2k_char;
207   char pkstrbuf[PUBKEY_STRING_SIZE];
208
209   for (node = keyblock; node; node = node->next)
210     {
211       if (node->pkt->pkttype == PKT_PUBLIC_KEY
212           || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
213         {
214           int rc;
215           PKT_public_key *pk = node->pkt->pkt.public_key;
216
217           serialno = NULL;
218           rc = hexkeygrip_from_pk (pk, &hexgrip);
219           if (rc)
220             {
221               log_error ("error computing a keygrip: %s\n", gpg_strerror (rc));
222               s2k_char = '?';
223             }
224           else if (!agent_get_keyinfo (NULL, hexgrip, &serialno))
225             s2k_char = serialno? '>':' ';
226           else
227             s2k_char = '#';  /* Key not found.  */
228
229           tty_fprintf (fp, "%s%c  %s/%s  ",
230                        node->pkt->pkttype == PKT_PUBLIC_KEY ? "sec" : "ssb",
231                        s2k_char,
232                        pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
233                        keystr_from_pk (pk));
234           tty_fprintf (fp, _("created: %s"), datestr_from_pk (pk));
235           tty_fprintf (fp, "  ");
236           tty_fprintf (fp, _("expires: %s"), expirestr_from_pk (pk));
237           if (serialno)
238             {
239               tty_fprintf (fp, "\n                      ");
240               tty_fprintf (fp, _("card-no: "));
241               if (strlen (serialno) == 32
242                   && !strncmp (serialno, "D27600012401", 12))
243                 {
244                   /* This is an OpenPGP card.  Print the relevant part.  */
245                   /* Example: D2760001240101010001000003470000 */
246                   /*                          xxxxyyyyyyyy     */
247                   tty_fprintf (fp, "%.*s %.*s", 4, serialno+16, 8, serialno+20);
248                 }
249               else
250                 tty_fprintf (fp, "%s", serialno);
251             }
252           tty_fprintf (fp, "\n");
253           xfree (hexgrip);
254           xfree (serialno);
255         }
256     }
257 }
258 #endif /*ENABLE_CARD_SUPPORT*/
259
260
261 /* Flags = 0x01 hashed 0x02 critical.  */
262 static void
263 status_one_subpacket (sigsubpkttype_t type, size_t len, int flags,
264                       const byte * buf)
265 {
266   char status[40];
267
268   /* Don't print these. */
269   if (len > 256)
270     return;
271
272   snprintf (status, sizeof status,
273             "%d %u %u ", type, flags, (unsigned int) len);
274
275   write_status_text_and_buffer (STATUS_SIG_SUBPACKET, status, buf, len, 0);
276 }
277
278
279 /* Print a policy URL.  Allowed values for MODE are:
280  *   0 - print to stdout.
281  *   1 - use log_info and emit status messages.
282  *   2 - emit only status messages.
283  */
284 void
285 show_policy_url (PKT_signature * sig, int indent, int mode)
286 {
287   const byte *p;
288   size_t len;
289   int seq = 0, crit;
290   estream_t fp = mode ? log_get_stream () : es_stdout;
291
292   while ((p =
293           enum_sig_subpkt (sig->hashed, SIGSUBPKT_POLICY, &len, &seq, &crit)))
294     {
295       if (mode != 2)
296         {
297           int i;
298           const char *str;
299
300           for (i = 0; i < indent; i++)
301             es_putc (' ', fp);
302
303           if (crit)
304             str = _("Critical signature policy: ");
305           else
306             str = _("Signature policy: ");
307           if (mode)
308             log_info ("%s", str);
309           else
310             es_fprintf (fp, "%s", str);
311           print_utf8_buffer (fp, p, len);
312           es_fprintf (fp, "\n");
313         }
314
315       if (mode)
316         write_status_buffer (STATUS_POLICY_URL, p, len, 0);
317     }
318 }
319
320
321 /*
322   mode=0 for stdout.
323   mode=1 for log_info + status messages
324   mode=2 for status messages only
325 */
326 /* TODO: use this */
327 void
328 show_keyserver_url (PKT_signature * sig, int indent, int mode)
329 {
330   const byte *p;
331   size_t len;
332   int seq = 0, crit;
333   estream_t fp = mode ? log_get_stream () : es_stdout;
334
335   while ((p =
336           enum_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_KS, &len, &seq,
337                            &crit)))
338     {
339       if (mode != 2)
340         {
341           int i;
342           const char *str;
343
344           for (i = 0; i < indent; i++)
345             es_putc (' ', es_stdout);
346
347           if (crit)
348             str = _("Critical preferred keyserver: ");
349           else
350             str = _("Preferred keyserver: ");
351           if (mode)
352             log_info ("%s", str);
353           else
354             es_fprintf (es_stdout, "%s", str);
355           print_utf8_buffer (fp, p, len);
356           es_fprintf (fp, "\n");
357         }
358
359       if (mode)
360         status_one_subpacket (SIGSUBPKT_PREF_KS, len,
361                               (crit ? 0x02 : 0) | 0x01, p);
362     }
363 }
364
365 /*
366   mode=0 for stdout.
367   mode=1 for log_info + status messages
368   mode=2 for status messages only
369
370   Defined bits in WHICH:
371     1 == standard notations
372     2 == user notations
373 */
374 void
375 show_notation (PKT_signature * sig, int indent, int mode, int which)
376 {
377   estream_t fp = mode ? log_get_stream () : es_stdout;
378   struct notation *nd, *notations;
379
380   if (which == 0)
381     which = 3;
382
383   notations = sig_to_notation (sig);
384
385   /* There may be multiple notations in the same sig. */
386   for (nd = notations; nd; nd = nd->next)
387     {
388       if (mode != 2)
389         {
390           int has_at = !!strchr (nd->name, '@');
391
392           if ((which & 1 && !has_at) || (which & 2 && has_at))
393             {
394               int i;
395               const char *str;
396
397               for (i = 0; i < indent; i++)
398                 es_putc (' ', es_stdout);
399
400               if (nd->flags.critical)
401                 str = _("Critical signature notation: ");
402               else
403                 str = _("Signature notation: ");
404               if (mode)
405                 log_info ("%s", str);
406               else
407                 es_fprintf (es_stdout, "%s", str);
408               /* This is all UTF8 */
409               print_utf8_buffer (fp, nd->name, strlen (nd->name));
410               es_fprintf (fp, "=");
411               print_utf8_buffer (fp, nd->value, strlen (nd->value));
412               /* (We need to use log_printf so that the next call to a
413                   log function does not insert an extra LF.)  */
414               if (mode)
415                 log_printf ("\n");
416               else
417                 es_putc ('\n', fp);
418             }
419         }
420
421       if (mode)
422         {
423           write_status_buffer (STATUS_NOTATION_NAME,
424                                nd->name, strlen (nd->name), 0);
425           write_status_buffer (STATUS_NOTATION_DATA,
426                                nd->value, strlen (nd->value), 50);
427         }
428     }
429
430   free_notation (notations);
431 }
432
433
434 static void
435 print_signature_stats (struct keylist_context *s)
436 {
437   if (!s->check_sigs)
438     return;  /* Signature checking was not requested.  */
439
440   if (s->inv_sigs == 1)
441     tty_printf (_("1 bad signature\n"));
442   else if (s->inv_sigs)
443     tty_printf (_("%d bad signatures\n"), s->inv_sigs);
444   if (s->no_key == 1)
445     tty_printf (_("1 signature not checked due to a missing key\n"));
446   else if (s->no_key)
447     tty_printf (_("%d signatures not checked due to missing keys\n"),
448                 s->no_key);
449   if (s->oth_err == 1)
450     tty_printf (_("1 signature not checked due to an error\n"));
451   else if (s->oth_err)
452     tty_printf (_("%d signatures not checked due to errors\n"), s->oth_err);
453 }
454
455
456 /* List all keys.  If SECRET is true only secret keys are listed.  If
457    MARK_SECRET is true secret keys are indicated in a public key
458    listing.  */
459 static void
460 list_all (int secret, int mark_secret)
461 {
462   KEYDB_HANDLE hd;
463   KBNODE keyblock = NULL;
464   int rc = 0;
465   int any_secret;
466   const char *lastresname, *resname;
467   struct keylist_context listctx;
468
469   memset (&listctx, 0, sizeof (listctx));
470   if (opt.check_sigs)
471     listctx.check_sigs = 1;
472
473   hd = keydb_new ();
474   if (!hd)
475     rc = gpg_error (GPG_ERR_GENERAL);
476   else
477     rc = keydb_search_first (hd);
478   if (rc)
479     {
480       if (gpg_err_code (rc) != GPG_ERR_NOT_FOUND)
481         log_error ("keydb_search_first failed: %s\n", gpg_strerror (rc));
482       goto leave;
483     }
484
485   lastresname = NULL;
486   do
487     {
488       rc = keydb_get_keyblock (hd, &keyblock);
489       if (rc)
490         {
491           if (gpg_err_code (rc) == GPG_ERR_LEGACY_KEY)
492             continue;  /* Skip legacy keys.  */
493           log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
494           goto leave;
495         }
496
497       if (secret || mark_secret)
498         any_secret = !agent_probe_any_secret_key (NULL, keyblock);
499       else
500         any_secret = 0;
501
502       if (secret && !any_secret)
503         ; /* Secret key listing requested but this isn't one.  */
504       else
505         {
506           if (!opt.with_colons)
507             {
508               resname = keydb_get_resource_name (hd);
509               if (lastresname != resname)
510                 {
511                   int i;
512
513                   es_fprintf (es_stdout, "%s\n", resname);
514                   for (i = strlen (resname); i; i--)
515                     es_putc ('-', es_stdout);
516                   es_putc ('\n', es_stdout);
517                   lastresname = resname;
518                 }
519             }
520           merge_keys_and_selfsig (keyblock);
521           list_keyblock (keyblock, secret, any_secret, opt.fingerprint,
522                          &listctx);
523         }
524       release_kbnode (keyblock);
525       keyblock = NULL;
526     }
527   while (!(rc = keydb_search_next (hd)));
528   es_fflush (es_stdout);
529   if (rc && gpg_err_code (rc) != GPG_ERR_NOT_FOUND)
530     log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
531   if (keydb_get_skipped_counter (hd))
532     log_info (_("Warning: %lu key(s) skipped due to their large size\n"),
533               keydb_get_skipped_counter (hd));
534
535   if (opt.check_sigs && !opt.with_colons)
536     print_signature_stats (&listctx);
537
538  leave:
539   keylist_context_release (&listctx);
540   release_kbnode (keyblock);
541   keydb_release (hd);
542 }
543
544
545 static void
546 list_one (strlist_t names, int secret, int mark_secret)
547 {
548   int rc = 0;
549   KBNODE keyblock = NULL;
550   GETKEY_CTX ctx;
551   const char *resname;
552   const char *keyring_str = _("Keyring");
553   int i;
554   struct keylist_context listctx;
555
556   memset (&listctx, 0, sizeof (listctx));
557   if (!secret && opt.check_sigs)
558     listctx.check_sigs = 1;
559
560   /* fixme: using the bynames function has the disadvantage that we
561    * don't know wether one of the names given was not found.  OTOH,
562    * this function has the advantage to list the names in the
563    * sequence as defined by the keyDB and does not duplicate
564    * outputs.  A solution could be do test whether all given have
565    * been listed (this needs a way to use the keyDB search
566    * functions) or to have the search function return indicators for
567    * found names.  Yet another way is to use the keydb search
568    * facilities directly. */
569   rc = getkey_bynames (&ctx, NULL, names, secret, &keyblock);
570   if (rc)
571     {
572       log_error ("error reading key: %s\n", gpg_strerror (rc));
573       get_pubkey_end (ctx);
574       return;
575     }
576
577   do
578     {
579       if ((opt.list_options & LIST_SHOW_KEYRING) && !opt.with_colons)
580         {
581           resname = keydb_get_resource_name (get_ctx_handle (ctx));
582           es_fprintf (es_stdout, "%s: %s\n", keyring_str, resname);
583           for (i = strlen (resname) + strlen (keyring_str) + 2; i; i--)
584             es_putc ('-', es_stdout);
585           es_putc ('\n', es_stdout);
586         }
587       list_keyblock (keyblock, secret, mark_secret, opt.fingerprint, &listctx);
588       release_kbnode (keyblock);
589     }
590   while (!getkey_next (ctx, NULL, &keyblock));
591   getkey_end (ctx);
592
593   if (opt.check_sigs && !opt.with_colons)
594     print_signature_stats (&listctx);
595
596   keylist_context_release (&listctx);
597 }
598
599
600 static void
601 locate_one (ctrl_t ctrl, strlist_t names)
602 {
603   int rc = 0;
604   strlist_t sl;
605   GETKEY_CTX ctx = NULL;
606   KBNODE keyblock = NULL;
607   struct keylist_context listctx;
608
609   memset (&listctx, 0, sizeof (listctx));
610   if (opt.check_sigs)
611     listctx.check_sigs = 1;
612
613   for (sl = names; sl; sl = sl->next)
614     {
615       rc = get_pubkey_byname (ctrl, &ctx, NULL, sl->d, &keyblock, NULL, 1, 0);
616       if (rc)
617         {
618           if (gpg_err_code (rc) != GPG_ERR_NO_PUBKEY)
619             log_error ("error reading key: %s\n", gpg_strerror (rc));
620         }
621       else
622         {
623           do
624             {
625               list_keyblock (keyblock, 0, 0, opt.fingerprint, &listctx);
626               release_kbnode (keyblock);
627             }
628           while (ctx && !get_pubkey_next (ctx, NULL, &keyblock));
629           get_pubkey_end (ctx);
630           ctx = NULL;
631         }
632     }
633
634   if (opt.check_sigs && !opt.with_colons)
635     print_signature_stats (&listctx);
636
637   keylist_context_release (&listctx);
638 }
639
640
641 static void
642 print_key_data (PKT_public_key * pk)
643 {
644   int n = pk ? pubkey_get_npkey (pk->pubkey_algo) : 0;
645   int i;
646
647   for (i = 0; i < n; i++)
648     {
649       es_fprintf (es_stdout, "pkd:%d:%u:", i, mpi_get_nbits (pk->pkey[i]));
650       mpi_print (es_stdout, pk->pkey[i], 1);
651       es_putc (':', es_stdout);
652       es_putc ('\n', es_stdout);
653     }
654 }
655
656 static void
657 print_capabilities (PKT_public_key *pk, KBNODE keyblock)
658 {
659   unsigned int use = pk->pubkey_usage;
660   int c_printed = 0;
661
662   if (use & PUBKEY_USAGE_ENC)
663     es_putc ('e', es_stdout);
664
665   if (use & PUBKEY_USAGE_SIG)
666     {
667       es_putc ('s', es_stdout);
668       if (pk->flags.primary)
669         {
670           es_putc ('c', es_stdout);
671           /* The PUBKEY_USAGE_CERT flag was introduced later and we
672              used to always print 'c' for a primary key.  To avoid any
673              regression here we better track whether we printed 'c'
674              already.  */
675           c_printed = 1;
676         }
677     }
678
679   if ((use & PUBKEY_USAGE_CERT) && !c_printed)
680     es_putc ('c', es_stdout);
681
682   if ((use & PUBKEY_USAGE_AUTH))
683     es_putc ('a', es_stdout);
684
685   if ((use & PUBKEY_USAGE_UNKNOWN))
686     es_putc ('?', es_stdout);
687
688   if (keyblock)
689     {
690       /* Figure out the usable capabilities.  */
691       KBNODE k;
692       int enc = 0, sign = 0, cert = 0, auth = 0, disabled = 0;
693
694       for (k = keyblock; k; k = k->next)
695         {
696           if (k->pkt->pkttype == PKT_PUBLIC_KEY
697               || k->pkt->pkttype == PKT_PUBLIC_SUBKEY)
698             {
699               pk = k->pkt->pkt.public_key;
700
701               if (pk->flags.primary)
702                 disabled = pk_is_disabled (pk);
703
704               if (pk->flags.valid && !pk->flags.revoked && !pk->has_expired)
705                 {
706                   if (pk->pubkey_usage & PUBKEY_USAGE_ENC)
707                     enc = 1;
708                   if (pk->pubkey_usage & PUBKEY_USAGE_SIG)
709                     {
710                       sign = 1;
711                       if (pk->flags.primary)
712                         cert = 1;
713                     }
714                   if (pk->pubkey_usage & PUBKEY_USAGE_CERT)
715                     cert = 1;
716                   if ((pk->pubkey_usage & PUBKEY_USAGE_AUTH))
717                     auth = 1;
718                 }
719             }
720         }
721       if (enc)
722         es_putc ('E', es_stdout);
723       if (sign)
724         es_putc ('S', es_stdout);
725       if (cert)
726         es_putc ('C', es_stdout);
727       if (auth)
728         es_putc ('A', es_stdout);
729       if (disabled)
730         es_putc ('D', es_stdout);
731     }
732
733   es_putc (':', es_stdout);
734 }
735
736
737 /* FLAGS: 0x01 hashed
738           0x02 critical  */
739 static void
740 print_one_subpacket (sigsubpkttype_t type, size_t len, int flags,
741                      const byte * buf)
742 {
743   size_t i;
744
745   es_fprintf (es_stdout, "spk:%d:%u:%u:", type, flags, (unsigned int) len);
746
747   for (i = 0; i < len; i++)
748     {
749       /* printable ascii other than : and % */
750       if (buf[i] >= 32 && buf[i] <= 126 && buf[i] != ':' && buf[i] != '%')
751         es_fprintf (es_stdout, "%c", buf[i]);
752       else
753         es_fprintf (es_stdout, "%%%02X", buf[i]);
754     }
755
756   es_fprintf (es_stdout, "\n");
757 }
758
759
760 void
761 print_subpackets_colon (PKT_signature * sig)
762 {
763   byte *i;
764
765   assert (opt.show_subpackets);
766
767   for (i = opt.show_subpackets; *i; i++)
768     {
769       const byte *p;
770       size_t len;
771       int seq, crit;
772
773       seq = 0;
774
775       while ((p = enum_sig_subpkt (sig->hashed, *i, &len, &seq, &crit)))
776         print_one_subpacket (*i, len, 0x01 | (crit ? 0x02 : 0), p);
777
778       seq = 0;
779
780       while ((p = enum_sig_subpkt (sig->unhashed, *i, &len, &seq, &crit)))
781         print_one_subpacket (*i, len, 0x00 | (crit ? 0x02 : 0), p);
782     }
783 }
784
785
786 void
787 dump_attribs (const PKT_user_id *uid, PKT_public_key *pk)
788 {
789   int i;
790
791   if (!attrib_fp)
792     return;
793
794   for (i = 0; i < uid->numattribs; i++)
795     {
796       if (is_status_enabled ())
797         {
798           byte array[MAX_FINGERPRINT_LEN], *p;
799           char buf[(MAX_FINGERPRINT_LEN * 2) + 90];
800           size_t j, n;
801
802           if (!pk)
803             BUG ();
804           fingerprint_from_pk (pk, array, &n);
805
806           p = array;
807           for (j = 0; j < n; j++, p++)
808             sprintf (buf + 2 * j, "%02X", *p);
809
810           sprintf (buf + strlen (buf), " %lu %u %u %u %lu %lu %u",
811                    (ulong) uid->attribs[i].len, uid->attribs[i].type, i + 1,
812                    uid->numattribs, (ulong) uid->created,
813                    (ulong) uid->expiredate,
814                    ((uid->is_primary ? 0x01 : 0) | (uid->
815                                                     is_revoked ? 0x02 : 0) |
816                     (uid->is_expired ? 0x04 : 0)));
817           write_status_text (STATUS_ATTRIBUTE, buf);
818         }
819
820       es_fwrite (uid->attribs[i].data, uid->attribs[i].len, 1, attrib_fp);
821       es_fflush (attrib_fp);
822     }
823 }
824
825
826 /* Print IPGP cert records instead of a standard key listing.  */
827 static void
828 list_keyblock_pka (kbnode_t keyblock)
829 {
830   kbnode_t kbctx;
831   kbnode_t node;
832   PKT_public_key *pk;
833   char pkstrbuf[PUBKEY_STRING_SIZE];
834   char *hexfpr;
835
836   /* Get the keyid from the keyblock.  */
837   node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
838   if (!node)
839     {
840       log_error ("Oops; key lost!\n");
841       dump_kbnode (keyblock);
842       return;
843     }
844
845   pk = node->pkt->pkt.public_key;
846
847   es_fprintf (es_stdout, ";; pub  %s/%s %s\n;; ",
848               pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
849               keystr_from_pk (pk), datestr_from_pk (pk));
850   print_fingerprint (NULL, pk, 10);
851   hexfpr = hexfingerprint (pk);
852
853   for (kbctx = NULL; (node = walk_kbnode (keyblock, &kbctx, 0));)
854     {
855       if (node->pkt->pkttype == PKT_USER_ID)
856         {
857           PKT_user_id *uid = node->pkt->pkt.user_id;
858           char *mbox;
859           char *p;
860
861           if (pk && (uid->is_expired || uid->is_revoked)
862               && !(opt.list_options & LIST_SHOW_UNUSABLE_UIDS))
863             continue;
864
865           es_fputs (";; uid  ", es_stdout);
866           print_utf8_buffer (es_stdout, uid->name, uid->len);
867           es_putc ('\n', es_stdout);
868           mbox = mailbox_from_userid (uid->name);
869           if (mbox && (p = strchr (mbox, '@')))
870             {
871               char hashbuf[20];
872               char *hash;
873               unsigned int len;
874
875               *p++ = 0;
876               es_fprintf (es_stdout, "$ORIGIN _pka.%s.\n", p);
877               gcry_md_hash_buffer (GCRY_MD_SHA1, hashbuf, mbox, strlen (mbox));
878               hash = zb32_encode (hashbuf, 8*20);
879               if (hash)
880                 {
881                   len = strlen (hexfpr)/2;
882                   es_fprintf (es_stdout,
883                               "%s TYPE37 \\# %u 0006 0000 00 %02X %s\n",
884                               hash, 6 + len, len, hexfpr);
885                   xfree (hash);
886                 }
887             }
888           xfree (mbox);
889         }
890
891     }
892   es_putc ('\n', es_stdout);
893
894   xfree (hexfpr);
895 }
896
897
898 static void
899 list_keyblock_print (KBNODE keyblock, int secret, int fpr,
900                      struct keylist_context *listctx)
901 {
902   int rc;
903   KBNODE kbctx;
904   KBNODE node;
905   PKT_public_key *pk;
906   int skip_sigs = 0;
907   int s2k_char;
908   char *hexgrip = NULL;
909   char *serialno = NULL;
910   char pkstrbuf[PUBKEY_STRING_SIZE];
911
912   /* Get the keyid from the keyblock.  */
913   node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
914   if (!node)
915     {
916       log_error ("Oops; key lost!\n");
917       dump_kbnode (keyblock);
918       return;
919     }
920
921   pk = node->pkt->pkt.public_key;
922
923   if (secret || opt.with_keygrip)
924     {
925       rc = hexkeygrip_from_pk (pk, &hexgrip);
926       if (rc)
927         log_error ("error computing a keygrip: %s\n", gpg_strerror (rc));
928     }
929
930   if (secret)
931     {
932       if (!agent_get_keyinfo (NULL, hexgrip, &serialno))
933         s2k_char = serialno? '>':' ';
934       else
935         s2k_char = '#';  /* Key not found.  */
936     }
937   else
938     s2k_char = ' ';
939
940   check_trustdb_stale ();
941
942
943   es_fprintf (es_stdout, "%s%c  %s/%s %s",
944               secret? "sec":"pub",
945               s2k_char,
946               pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
947               keystr_from_pk (pk), datestr_from_pk (pk));
948
949   if ((opt.list_options & LIST_SHOW_USAGE))
950     {
951       es_fprintf (es_stdout, " [%s]", usagestr_from_pk (pk, 0));
952     }
953   if (pk->flags.revoked)
954     {
955       es_fprintf (es_stdout, " [");
956       es_fprintf (es_stdout, _("revoked: %s"), revokestr_from_pk (pk));
957       es_fprintf (es_stdout, "]");
958     }
959   else if (pk->has_expired)
960     {
961       es_fprintf (es_stdout, " [");
962       es_fprintf (es_stdout, _("expired: %s"), expirestr_from_pk (pk));
963       es_fprintf (es_stdout, "]");
964     }
965   else if (pk->expiredate)
966     {
967       es_fprintf (es_stdout, " [");
968       es_fprintf (es_stdout, _("expires: %s"), expirestr_from_pk (pk));
969       es_fprintf (es_stdout, "]");
970     }
971
972 #if 0
973   /* I need to think about this some more.  It's easy enough to
974      include, but it looks sort of confusing in the listing... */
975   if (opt.list_options & LIST_SHOW_VALIDITY)
976     {
977       int validity = get_validity (pk, NULL);
978       es_fprintf (es_stdout, " [%s]", trust_value_to_string (validity));
979     }
980 #endif
981
982   if (pk->pubkey_algo >= 100)
983     es_fprintf (es_stdout, " [experimental algorithm %d]", pk->pubkey_algo);
984
985   es_fprintf (es_stdout, "\n");
986
987   if (fpr)
988     print_fingerprint (NULL, pk, 0);
989
990   if (opt.with_keygrip && hexgrip)
991     es_fprintf (es_stdout, "      Keygrip = %s\n", hexgrip);
992
993   if (serialno)
994     print_card_serialno (serialno);
995
996   if (opt.with_key_data)
997     print_key_data (pk);
998
999   for (kbctx = NULL; (node = walk_kbnode (keyblock, &kbctx, 0));)
1000     {
1001       if (node->pkt->pkttype == PKT_USER_ID)
1002         {
1003           PKT_user_id *uid = node->pkt->pkt.user_id;
1004
1005           if ((uid->is_expired || uid->is_revoked)
1006               && !(opt.list_options & LIST_SHOW_UNUSABLE_UIDS))
1007             {
1008               skip_sigs = 1;
1009               continue;
1010             }
1011           else
1012             skip_sigs = 0;
1013
1014           if (attrib_fp && uid->attrib_data != NULL)
1015             dump_attribs (uid, pk);
1016
1017           if ((uid->is_revoked || uid->is_expired)
1018               || (opt.list_options & LIST_SHOW_UID_VALIDITY))
1019             {
1020               const char *validity;
1021               int indent;
1022
1023               validity = uid_trust_string_fixed (pk, uid);
1024               indent =
1025                 (keystrlen () + 9) -
1026                 atoi (uid_trust_string_fixed (NULL, NULL));
1027
1028               if (indent < 0 || indent > 40)
1029                 indent = 0;
1030
1031               es_fprintf (es_stdout, "uid%*s%s ", indent, "", validity);
1032             }
1033           else
1034             es_fprintf (es_stdout, "uid%*s", (int) keystrlen () + 10, "");
1035
1036           print_utf8_buffer (es_stdout, uid->name, uid->len);
1037           es_putc ('\n', es_stdout);
1038
1039           if ((opt.list_options & LIST_SHOW_PHOTOS) && uid->attribs != NULL)
1040             show_photos (uid->attribs, uid->numattribs, pk, uid);
1041         }
1042       else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1043         {
1044           PKT_public_key *pk2 = node->pkt->pkt.public_key;
1045
1046           if ((pk2->flags.revoked || pk2->has_expired)
1047               && !(opt.list_options & LIST_SHOW_UNUSABLE_SUBKEYS))
1048             {
1049               skip_sigs = 1;
1050               continue;
1051             }
1052           else
1053             skip_sigs = 0;
1054
1055           xfree (serialno); serialno = NULL;
1056           xfree (hexgrip); hexgrip = NULL;
1057           if (secret || opt.with_keygrip)
1058             {
1059               rc = hexkeygrip_from_pk (pk2, &hexgrip);
1060               if (rc)
1061                 log_error ("error computing a keygrip: %s\n",
1062                            gpg_strerror (rc));
1063             }
1064           if (secret)
1065             {
1066               if (!agent_get_keyinfo (NULL, hexgrip, &serialno))
1067                 s2k_char = serialno? '>':' ';
1068               else
1069                 s2k_char = '#';  /* Key not found.  */
1070             }
1071           else
1072             s2k_char = ' ';
1073
1074           es_fprintf (es_stdout, "%s%c  %s/%s %s",
1075                   secret? "ssb":"sub",
1076                   s2k_char,
1077                   pubkey_string (pk2, pkstrbuf, sizeof pkstrbuf),
1078                   keystr_from_pk (pk2), datestr_from_pk (pk2));
1079
1080           if (pk2->pubkey_algo == PUBKEY_ALGO_ECDSA
1081               || pk2->pubkey_algo == PUBKEY_ALGO_EDDSA
1082               || pk2->pubkey_algo == PUBKEY_ALGO_ECDH)
1083             {
1084               char *curve = openpgp_oid_to_str (pk2->pkey[0]);
1085               const char *name = openpgp_oid_to_curve (curve);
1086               if (!*name || *name == '?')
1087                 name = curve;
1088               es_fprintf (es_stdout, " %s", name);
1089               xfree (curve);
1090             }
1091
1092           if ((opt.list_options & LIST_SHOW_USAGE))
1093             {
1094               es_fprintf (es_stdout, " [%s]", usagestr_from_pk (pk2, 0));
1095             }
1096           if (pk2->flags.revoked)
1097             {
1098               es_fprintf (es_stdout, " [");
1099               es_fprintf (es_stdout, _("revoked: %s"), revokestr_from_pk (pk2));
1100               es_fprintf (es_stdout, "]");
1101             }
1102           else if (pk2->has_expired)
1103             {
1104               es_fprintf (es_stdout, " [");
1105               es_fprintf (es_stdout, _("expired: %s"), expirestr_from_pk (pk2));
1106               es_fprintf (es_stdout, "]");
1107             }
1108           else if (pk2->expiredate)
1109             {
1110               es_fprintf (es_stdout, " [");
1111               es_fprintf (es_stdout, _("expires: %s"), expirestr_from_pk (pk2));
1112               es_fprintf (es_stdout, "]");
1113             }
1114           es_putc ('\n', es_stdout);
1115           if (fpr > 1)
1116             {
1117               print_fingerprint (NULL, pk2, 0);
1118               if (serialno)
1119                 print_card_serialno (serialno);
1120             }
1121           if (opt.with_keygrip && hexgrip)
1122             es_fprintf (es_stdout, "      Keygrip = %s\n", hexgrip);
1123           if (opt.with_key_data)
1124             print_key_data (pk2);
1125         }
1126       else if (opt.list_sigs
1127                && node->pkt->pkttype == PKT_SIGNATURE && !skip_sigs)
1128         {
1129           PKT_signature *sig = node->pkt->pkt.signature;
1130           int sigrc;
1131           char *sigstr;
1132
1133           if (listctx->check_sigs)
1134             {
1135               rc = check_key_signature (keyblock, node, NULL);
1136               switch (gpg_err_code (rc))
1137                 {
1138                 case 0:
1139                   sigrc = '!';
1140                   break;
1141                 case GPG_ERR_BAD_SIGNATURE:
1142                   listctx->inv_sigs++;
1143                   sigrc = '-';
1144                   break;
1145                 case GPG_ERR_NO_PUBKEY:
1146                 case GPG_ERR_UNUSABLE_PUBKEY:
1147                   listctx->no_key++;
1148                   continue;
1149                 default:
1150                   listctx->oth_err++;
1151                   sigrc = '%';
1152                   break;
1153                 }
1154
1155               /* TODO: Make sure a cached sig record here still has
1156                  the pk that issued it.  See also
1157                  keyedit.c:print_and_check_one_sig */
1158             }
1159           else
1160             {
1161               rc = 0;
1162               sigrc = ' ';
1163             }
1164
1165           if (sig->sig_class == 0x20 || sig->sig_class == 0x28
1166               || sig->sig_class == 0x30)
1167             sigstr = "rev";
1168           else if ((sig->sig_class & ~3) == 0x10)
1169             sigstr = "sig";
1170           else if (sig->sig_class == 0x18)
1171             sigstr = "sig";
1172           else if (sig->sig_class == 0x1F)
1173             sigstr = "sig";
1174           else
1175             {
1176               es_fprintf (es_stdout, "sig                             "
1177                       "[unexpected signature class 0x%02x]\n",
1178                       sig->sig_class);
1179               continue;
1180             }
1181
1182           es_fputs (sigstr, es_stdout);
1183           es_fprintf (es_stdout, "%c%c %c%c%c%c%c%c %s %s",
1184                   sigrc, (sig->sig_class - 0x10 > 0 &&
1185                           sig->sig_class - 0x10 <
1186                           4) ? '0' + sig->sig_class - 0x10 : ' ',
1187                   sig->flags.exportable ? ' ' : 'L',
1188                   sig->flags.revocable ? ' ' : 'R',
1189                   sig->flags.policy_url ? 'P' : ' ',
1190                   sig->flags.notation ? 'N' : ' ',
1191                   sig->flags.expired ? 'X' : ' ',
1192                   (sig->trust_depth > 9) ? 'T' : (sig->trust_depth >
1193                                                   0) ? '0' +
1194                   sig->trust_depth : ' ', keystr (sig->keyid),
1195                   datestr_from_sig (sig));
1196           if (opt.list_options & LIST_SHOW_SIG_EXPIRE)
1197             es_fprintf (es_stdout, " %s", expirestr_from_sig (sig));
1198           es_fprintf (es_stdout, "  ");
1199           if (sigrc == '%')
1200             es_fprintf (es_stdout, "[%s] ", gpg_strerror (rc));
1201           else if (sigrc == '?')
1202             ;
1203           else if (!opt.fast_list_mode)
1204             {
1205               size_t n;
1206               char *p = get_user_id (sig->keyid, &n);
1207               print_utf8_buffer (es_stdout, p, n);
1208               xfree (p);
1209             }
1210           es_putc ('\n', es_stdout);
1211
1212           if (sig->flags.policy_url
1213               && (opt.list_options & LIST_SHOW_POLICY_URLS))
1214             show_policy_url (sig, 3, 0);
1215
1216           if (sig->flags.notation && (opt.list_options & LIST_SHOW_NOTATIONS))
1217             show_notation (sig, 3, 0,
1218                            ((opt.
1219                              list_options & LIST_SHOW_STD_NOTATIONS) ? 1 : 0)
1220                            +
1221                            ((opt.
1222                              list_options & LIST_SHOW_USER_NOTATIONS) ? 2 :
1223                             0));
1224
1225           if (sig->flags.pref_ks
1226               && (opt.list_options & LIST_SHOW_KEYSERVER_URLS))
1227             show_keyserver_url (sig, 3, 0);
1228
1229           /* fixme: check or list other sigs here */
1230         }
1231     }
1232   es_putc ('\n', es_stdout);
1233   xfree (serialno);
1234   xfree (hexgrip);
1235 }
1236
1237 void
1238 print_revokers (estream_t fp, PKT_public_key * pk)
1239 {
1240   /* print the revoker record */
1241   if (!pk->revkey && pk->numrevkeys)
1242     BUG ();
1243   else
1244     {
1245       int i, j;
1246
1247       for (i = 0; i < pk->numrevkeys; i++)
1248         {
1249           byte *p;
1250
1251           es_fprintf (fp, "rvk:::%d::::::", pk->revkey[i].algid);
1252           p = pk->revkey[i].fpr;
1253           for (j = 0; j < 20; j++, p++)
1254             es_fprintf (fp, "%02X", *p);
1255           es_fprintf (fp, ":%02x%s:\n",
1256                       pk->revkey[i].class,
1257                       (pk->revkey[i].class & 0x40) ? "s" : "");
1258         }
1259     }
1260 }
1261
1262
1263 /* List a key in colon mode.  If SECRET is true this is a secret key
1264    record (i.e. requested via --list-secret-key).  If HAS_SECRET a
1265    secret key is available even if SECRET is not set.  */
1266 static void
1267 list_keyblock_colon (KBNODE keyblock, int secret, int has_secret, int fpr)
1268 {
1269   int rc;
1270   KBNODE kbctx;
1271   KBNODE node;
1272   PKT_public_key *pk;
1273   u32 keyid[2];
1274   int trustletter = 0;
1275   int ulti_hack = 0;
1276   int i;
1277   char *p;
1278   char *hexgrip = NULL;
1279   char *serialno = NULL;
1280   int stubkey;
1281
1282   /* Get the keyid from the keyblock.  */
1283   node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
1284   if (!node)
1285     {
1286       log_error ("Oops; key lost!\n");
1287       dump_kbnode (keyblock);
1288       return;
1289     }
1290
1291   pk = node->pkt->pkt.public_key;
1292   if (secret || has_secret || opt.with_keygrip || opt.with_key_data)
1293     {
1294       rc = hexkeygrip_from_pk (pk, &hexgrip);
1295       if (rc)
1296         log_error ("error computing a keygrip: %s\n", gpg_strerror (rc));
1297     }
1298   stubkey = 0;
1299   if ((secret||has_secret) && agent_get_keyinfo (NULL, hexgrip, &serialno))
1300     stubkey = 1;  /* Key not found.  */
1301
1302   keyid_from_pk (pk, keyid);
1303   es_fputs (secret? "sec:":"pub:", es_stdout);
1304   if (!pk->flags.valid)
1305     es_putc ('i', es_stdout);
1306   else if (pk->flags.revoked)
1307     es_putc ('r', es_stdout);
1308   else if (pk->has_expired)
1309     es_putc ('e', es_stdout);
1310   else if (opt.fast_list_mode || opt.no_expensive_trust_checks)
1311     ;
1312   else
1313     {
1314       trustletter = get_validity_info (pk, NULL);
1315       if (trustletter == 'u')
1316         ulti_hack = 1;
1317       es_putc (trustletter, es_stdout);
1318     }
1319
1320   es_fprintf (es_stdout, ":%u:%d:%08lX%08lX:%s:%s::",
1321           nbits_from_pk (pk),
1322           pk->pubkey_algo,
1323           (ulong) keyid[0], (ulong) keyid[1],
1324           colon_datestr_from_pk (pk), colon_strtime (pk->expiredate));
1325
1326   if (!opt.fast_list_mode && !opt.no_expensive_trust_checks)
1327     es_putc (get_ownertrust_info (pk), es_stdout);
1328   es_putc (':', es_stdout);
1329
1330   es_putc (':', es_stdout);
1331   es_putc (':', es_stdout);
1332   print_capabilities (pk, keyblock);
1333   es_putc (':', es_stdout);             /* End of field 13. */
1334   es_putc (':', es_stdout);             /* End of field 14. */
1335   if (secret || has_secret)
1336     {
1337       if (stubkey)
1338         es_putc ('#', es_stdout);
1339       else if (serialno)
1340         es_fputs (serialno, es_stdout);
1341       else if (has_secret)
1342         es_putc ('+', es_stdout);
1343     }
1344   es_putc (':', es_stdout);             /* End of field 15. */
1345   es_putc (':', es_stdout);             /* End of field 16. */
1346   if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA
1347       || pk->pubkey_algo == PUBKEY_ALGO_EDDSA
1348       || pk->pubkey_algo == PUBKEY_ALGO_ECDH)
1349     {
1350       char *curve = openpgp_oid_to_str (pk->pkey[0]);
1351       const char *name = openpgp_oid_to_curve (curve);
1352       if (!*name || *name == '?')
1353         name = curve;
1354       es_fputs (name, es_stdout);
1355       xfree (curve);
1356     }
1357   es_putc (':', es_stdout);             /* End of field 17. */
1358   es_putc ('\n', es_stdout);
1359
1360   print_revokers (es_stdout, pk);
1361   if (fpr)
1362     print_fingerprint (NULL, pk, 0);
1363   if (opt.with_key_data || opt.with_keygrip)
1364     {
1365       if (hexgrip)
1366         es_fprintf (es_stdout, "grp:::::::::%s:\n", hexgrip);
1367       if (opt.with_key_data)
1368         print_key_data (pk);
1369     }
1370
1371   for (kbctx = NULL; (node = walk_kbnode (keyblock, &kbctx, 0));)
1372     {
1373       if (node->pkt->pkttype == PKT_USER_ID)
1374         {
1375           char *str;
1376           PKT_user_id *uid = node->pkt->pkt.user_id;
1377
1378           if (attrib_fp && node->pkt->pkt.user_id->attrib_data != NULL)
1379             dump_attribs (node->pkt->pkt.user_id, pk);
1380           /*
1381            * Fixme: We need a valid flag here too
1382            */
1383           str = uid->attrib_data ? "uat" : "uid";
1384           if (uid->is_revoked)
1385             es_fprintf (es_stdout, "%s:r::::", str);
1386           else if (uid->is_expired)
1387             es_fprintf (es_stdout, "%s:e::::", str);
1388           else if (opt.no_expensive_trust_checks)
1389             es_fprintf (es_stdout, "%s:::::", str);
1390           else
1391             {
1392               int uid_validity;
1393
1394               if (!ulti_hack)
1395                 uid_validity = get_validity_info (pk, uid);
1396               else
1397                 uid_validity = 'u';
1398               es_fprintf (es_stdout, "%s:%c::::", str, uid_validity);
1399             }
1400
1401           es_fprintf (es_stdout, "%s:", colon_strtime (uid->created));
1402           es_fprintf (es_stdout, "%s:", colon_strtime (uid->expiredate));
1403
1404           namehash_from_uid (uid);
1405
1406           for (i = 0; i < 20; i++)
1407             es_fprintf (es_stdout, "%02X", uid->namehash[i]);
1408
1409           es_fprintf (es_stdout, "::");
1410
1411           if (uid->attrib_data)
1412             es_fprintf (es_stdout, "%u %lu", uid->numattribs, uid->attrib_len);
1413           else
1414             es_write_sanitized (es_stdout, uid->name, uid->len, ":", NULL);
1415           es_putc (':', es_stdout);
1416           es_putc ('\n', es_stdout);
1417         }
1418       else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1419         {
1420           u32 keyid2[2];
1421           PKT_public_key *pk2;
1422
1423           pk2 = node->pkt->pkt.public_key;
1424           xfree (hexgrip); hexgrip = NULL;
1425           xfree (serialno); serialno = NULL;
1426           if (secret || has_secret || opt.with_keygrip || opt.with_key_data)
1427             {
1428               rc = hexkeygrip_from_pk (pk2, &hexgrip);
1429               if (rc)
1430                 log_error ("error computing a keygrip: %s\n",
1431                            gpg_strerror (rc));
1432             }
1433           stubkey = 0;
1434           if ((secret||has_secret)
1435               && agent_get_keyinfo (NULL, hexgrip, &serialno))
1436             stubkey = 1;  /* Key not found.  */
1437
1438           keyid_from_pk (pk2, keyid2);
1439           es_fputs (secret? "ssb:":"sub:", es_stdout);
1440           if (!pk2->flags.valid)
1441             es_putc ('i', es_stdout);
1442           else if (pk2->flags.revoked)
1443             es_putc ('r', es_stdout);
1444           else if (pk2->has_expired)
1445             es_putc ('e', es_stdout);
1446           else if (opt.fast_list_mode || opt.no_expensive_trust_checks)
1447             ;
1448           else
1449             {
1450               /* TRUSTLETTER should always be defined here. */
1451               if (trustletter)
1452                 es_fprintf (es_stdout, "%c", trustletter);
1453             }
1454           es_fprintf (es_stdout, ":%u:%d:%08lX%08lX:%s:%s:::::",
1455                   nbits_from_pk (pk2),
1456                   pk2->pubkey_algo,
1457                   (ulong) keyid2[0], (ulong) keyid2[1],
1458                   colon_datestr_from_pk (pk2), colon_strtime (pk2->expiredate)
1459                   /* fixme: add LID and ownertrust here */
1460             );
1461           print_capabilities (pk2, NULL);
1462           es_putc (':', es_stdout);     /* End of field 13. */
1463           es_putc (':', es_stdout);     /* End of field 14. */
1464           if (secret || has_secret)
1465             {
1466               if (stubkey)
1467                 es_putc ('#', es_stdout);
1468               else if (serialno)
1469                 es_fputs (serialno, es_stdout);
1470               else if (has_secret)
1471                 es_putc ('+', es_stdout);
1472             }
1473           es_putc (':', es_stdout);     /* End of field 15. */
1474           es_putc (':', es_stdout);     /* End of field 16. */
1475           if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA
1476               || pk->pubkey_algo == PUBKEY_ALGO_EDDSA
1477               || pk->pubkey_algo == PUBKEY_ALGO_ECDH)
1478             {
1479               char *curve = openpgp_oid_to_str (pk->pkey[0]);
1480               const char *name = openpgp_oid_to_curve (curve);
1481               if (!*name || *name == '?')
1482                 name = curve;
1483               es_fputs (name, es_stdout);
1484               xfree (curve);
1485             }
1486           es_putc (':', es_stdout);     /* End of field 17. */
1487           es_putc ('\n', es_stdout);
1488           if (fpr > 1)
1489             print_fingerprint (NULL, pk2, 0);
1490           if (opt.with_key_data || opt.with_keygrip)
1491             {
1492               if (hexgrip)
1493                 es_fprintf (es_stdout, "grp:::::::::%s:\n", hexgrip);
1494               if (opt.with_key_data)
1495                 print_key_data (pk2);
1496             }
1497         }
1498       else if (opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE)
1499         {
1500           PKT_signature *sig = node->pkt->pkt.signature;
1501           int sigrc, fprokay = 0;
1502           char *sigstr;
1503           size_t fplen;
1504           byte fparray[MAX_FINGERPRINT_LEN];
1505
1506           if (sig->sig_class == 0x20 || sig->sig_class == 0x28
1507               || sig->sig_class == 0x30)
1508             sigstr = "rev";
1509           else if ((sig->sig_class & ~3) == 0x10)
1510             sigstr = "sig";
1511           else if (sig->sig_class == 0x18)
1512             sigstr = "sig";
1513           else if (sig->sig_class == 0x1F)
1514             sigstr = "sig";
1515           else
1516             {
1517               es_fprintf (es_stdout, "sig::::::::::%02x%c:\n",
1518                       sig->sig_class, sig->flags.exportable ? 'x' : 'l');
1519               continue;
1520             }
1521
1522           if (opt.check_sigs)
1523             {
1524               PKT_public_key *signer_pk = NULL;
1525
1526               fflush (stdout);
1527               if (opt.no_sig_cache)
1528                 signer_pk = xmalloc_clear (sizeof (PKT_public_key));
1529
1530               rc = check_key_signature2 (keyblock, node, NULL, signer_pk,
1531                                          NULL, NULL, NULL);
1532               switch (gpg_err_code (rc))
1533                 {
1534                 case 0:
1535                   sigrc = '!';
1536                   break;
1537                 case GPG_ERR_BAD_SIGNATURE:
1538                   sigrc = '-';
1539                   break;
1540                 case GPG_ERR_NO_PUBKEY:
1541                 case GPG_ERR_UNUSABLE_PUBKEY:
1542                   sigrc = '?';
1543                   break;
1544                 default:
1545                   sigrc = '%';
1546                   break;
1547                 }
1548
1549               if (opt.no_sig_cache)
1550                 {
1551                   if (!rc)
1552                     {
1553                       fingerprint_from_pk (signer_pk, fparray, &fplen);
1554                       fprokay = 1;
1555                     }
1556                   free_public_key (signer_pk);
1557                 }
1558             }
1559           else
1560             {
1561               rc = 0;
1562               sigrc = ' ';
1563             }
1564           es_fputs (sigstr, es_stdout);
1565           es_putc (':', es_stdout);
1566           if (sigrc != ' ')
1567             es_putc (sigrc, es_stdout);
1568           es_fprintf (es_stdout, "::%d:%08lX%08lX:%s:%s:", sig->pubkey_algo,
1569                   (ulong) sig->keyid[0], (ulong) sig->keyid[1],
1570                   colon_datestr_from_sig (sig),
1571                   colon_expirestr_from_sig (sig));
1572
1573           if (sig->trust_depth || sig->trust_value)
1574             es_fprintf (es_stdout, "%d %d", sig->trust_depth, sig->trust_value);
1575           es_fprintf (es_stdout, ":");
1576
1577           if (sig->trust_regexp)
1578             es_write_sanitized (es_stdout, sig->trust_regexp,
1579                                 strlen (sig->trust_regexp), ":", NULL);
1580           es_fprintf (es_stdout, ":");
1581
1582           if (sigrc == '%')
1583             es_fprintf (es_stdout, "[%s] ", gpg_strerror (rc));
1584           else if (sigrc == '?')
1585             ;
1586           else if (!opt.fast_list_mode)
1587             {
1588               size_t n;
1589               p = get_user_id (sig->keyid, &n);
1590               es_write_sanitized (es_stdout, p, n, ":", NULL);
1591               xfree (p);
1592             }
1593           es_fprintf (es_stdout, ":%02x%c::", sig->sig_class,
1594                   sig->flags.exportable ? 'x' : 'l');
1595
1596           if (opt.no_sig_cache && opt.check_sigs && fprokay)
1597             {
1598               for (i = 0; i < fplen; i++)
1599                 es_fprintf (es_stdout, "%02X", fparray[i]);
1600             }
1601
1602           es_fprintf (es_stdout, ":::%d:\n", sig->digest_algo);
1603
1604           if (opt.show_subpackets)
1605             print_subpackets_colon (sig);
1606
1607           /* fixme: check or list other sigs here */
1608         }
1609     }
1610
1611   xfree (hexgrip);
1612   xfree (serialno);
1613 }
1614
1615 /*
1616  * Reorder the keyblock so that the primary user ID (and not attribute
1617  * packet) comes first.  Fixme: Replace this by a generic sort
1618  * function.  */
1619 static void
1620 do_reorder_keyblock (KBNODE keyblock, int attr)
1621 {
1622   KBNODE primary = NULL, primary0 = NULL, primary2 = NULL;
1623   KBNODE last, node;
1624
1625   for (node = keyblock; node; primary0 = node, node = node->next)
1626     {
1627       if (node->pkt->pkttype == PKT_USER_ID &&
1628           ((attr && node->pkt->pkt.user_id->attrib_data) ||
1629            (!attr && !node->pkt->pkt.user_id->attrib_data)) &&
1630           node->pkt->pkt.user_id->is_primary)
1631         {
1632           primary = primary2 = node;
1633           for (node = node->next; node; primary2 = node, node = node->next)
1634             {
1635               if (node->pkt->pkttype == PKT_USER_ID
1636                   || node->pkt->pkttype == PKT_PUBLIC_SUBKEY
1637                   || node->pkt->pkttype == PKT_SECRET_SUBKEY)
1638                 {
1639                   break;
1640                 }
1641             }
1642           break;
1643         }
1644     }
1645   if (!primary)
1646     return; /* No primary key flag found (should not happen).  */
1647
1648   for (last = NULL, node = keyblock; node; last = node, node = node->next)
1649     {
1650       if (node->pkt->pkttype == PKT_USER_ID)
1651         break;
1652     }
1653   assert (node);
1654   assert (last);         /* The user ID is never the first packet.  */
1655   assert (primary0);     /* Ditto (this is the node before primary).  */
1656   if (node == primary)
1657     return; /* Already the first one.  */
1658
1659   last->next = primary;
1660   primary0->next = primary2->next;
1661   primary2->next = node;
1662 }
1663
1664 void
1665 reorder_keyblock (KBNODE keyblock)
1666 {
1667   do_reorder_keyblock (keyblock, 1);
1668   do_reorder_keyblock (keyblock, 0);
1669 }
1670
1671 static void
1672 list_keyblock (KBNODE keyblock, int secret, int has_secret, int fpr,
1673                struct keylist_context *listctx)
1674 {
1675   reorder_keyblock (keyblock);
1676   if (opt.print_pka_records)
1677     list_keyblock_pka (keyblock);
1678   else if (opt.with_colons)
1679     list_keyblock_colon (keyblock, secret, has_secret, fpr);
1680   else
1681     list_keyblock_print (keyblock, secret, fpr, listctx);
1682   if (secret)
1683     es_fflush (es_stdout);
1684 }
1685
1686
1687 /* Public function used by keygen to list a keyblock.  */
1688 void
1689 list_keyblock_direct (kbnode_t keyblock, int secret, int has_secret, int fpr)
1690 {
1691   struct keylist_context listctx;
1692
1693   memset (&listctx, 0, sizeof (listctx));
1694   list_keyblock (keyblock, secret, has_secret, fpr, &listctx);
1695   keylist_context_release (&listctx);
1696 }
1697
1698
1699 /* Print an hex digit in ICAO spelling.  */
1700 static void
1701 print_icao_hexdigit (estream_t fp, int c)
1702 {
1703   static const char *list[16] = {
1704     "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
1705     "Eight", "Niner", "Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot"
1706   };
1707
1708   tty_fprintf (fp, "%s", list[c&15]);
1709 }
1710
1711
1712 /*
1713  * Function to print the finperprint.
1714  * mode 0: as used in key listings, opt.with_colons is honored
1715  *      1: print using log_info ()
1716  *      2: direct use of tty
1717  *      3: direct use of tty but only primary key.
1718  *     10: Same as 0 but with_colons etc is ignored.
1719  *
1720  * Modes 1 and 2 will try and print both subkey and primary key
1721  * fingerprints.  A MODE with bit 7 set is used internally.  If
1722  * OVERRIDE_FP is not NULL that stream will be used in  0 instead
1723  * of es_stdout or instead of the TTY in modes 2 and 3.
1724  */
1725 void
1726 print_fingerprint (estream_t override_fp, PKT_public_key *pk, int mode)
1727 {
1728   byte array[MAX_FINGERPRINT_LEN], *p;
1729   size_t i, n;
1730   estream_t fp;
1731   const char *text;
1732   int primary = 0;
1733   int with_colons = opt.with_colons;
1734   int with_icao   = opt.with_icao_spelling;
1735
1736   if (mode == 10)
1737     {
1738       mode = 0;
1739       with_colons = 0;
1740       with_icao = 0;
1741     }
1742
1743   if (pk->main_keyid[0] == pk->keyid[0]
1744       && pk->main_keyid[1] == pk->keyid[1])
1745     primary = 1;
1746
1747   /* Just to be safe */
1748   if ((mode & 0x80) && !primary)
1749     {
1750       log_error ("primary key is not really primary!\n");
1751       return;
1752     }
1753
1754   mode &= ~0x80;
1755
1756   if (!primary && (mode == 1 || mode == 2))
1757     {
1758       PKT_public_key *primary_pk = xmalloc_clear (sizeof (*primary_pk));
1759       get_pubkey (primary_pk, pk->main_keyid);
1760       print_fingerprint (override_fp, primary_pk, (mode | 0x80));
1761       free_public_key (primary_pk);
1762     }
1763
1764   if (mode == 1)
1765     {
1766       fp = log_get_stream ();
1767       if (primary)
1768         text = _("Primary key fingerprint:");
1769       else
1770         text = _("     Subkey fingerprint:");
1771     }
1772   else if (mode == 2)
1773     {
1774       fp = override_fp; /* Use tty or given stream.  */
1775       if (primary)
1776         /* TRANSLATORS: this should fit into 24 bytes to that the
1777          * fingerprint data is properly aligned with the user ID */
1778         text = _(" Primary key fingerprint:");
1779       else
1780         text = _("      Subkey fingerprint:");
1781     }
1782   else if (mode == 3)
1783     {
1784       fp = override_fp; /* Use tty or given stream.  */
1785       text = _("      Key fingerprint =");
1786     }
1787   else
1788     {
1789       fp = override_fp? override_fp : es_stdout;
1790       text = _("      Key fingerprint =");
1791     }
1792
1793   fingerprint_from_pk (pk, array, &n);
1794   p = array;
1795   if (with_colons && !mode)
1796     {
1797       es_fprintf (fp, "fpr:::::::::");
1798       for (i = 0; i < n; i++, p++)
1799         es_fprintf (fp, "%02X", *p);
1800       es_putc (':', fp);
1801     }
1802   else
1803     {
1804       tty_fprintf (fp, "%s", text);
1805       if (n == 20)
1806         {
1807           for (i = 0; i < n; i++, i++, p += 2)
1808             tty_fprintf (fp, "%s %02X%02X", i==10? " ":"", *p, p[1]);
1809         }
1810       else
1811         {
1812           for (i = 0; i < n; i++, p++)
1813             tty_fprintf (fp, "%s %02X", (i && !(i % 8))? " ":"", *p);
1814         }
1815     }
1816   tty_fprintf (fp, "\n");
1817   if (!with_colons && with_icao)
1818     {
1819       p = array;
1820       tty_fprintf (fp, "%*s\"", (int)strlen(text)+1, "");
1821       for (i = 0; i < n; i++, p++)
1822         {
1823           if (!i)
1824             ;
1825           else if (!(i%4))
1826             tty_fprintf (fp, "\n%*s ", (int)strlen(text)+1, "");
1827           else if (!(i%2))
1828             tty_fprintf (fp, "  ");
1829           else
1830             tty_fprintf (fp, " ");
1831           print_icao_hexdigit (fp, *p >> 4);
1832           tty_fprintf (fp, " ");
1833           print_icao_hexdigit (fp, *p & 15);
1834         }
1835       tty_fprintf (fp, "\"\n");
1836     }
1837 }
1838
1839 /* Print the serial number of an OpenPGP card if available.  */
1840 static void
1841 print_card_serialno (const char *serialno)
1842 {
1843   if (!serialno)
1844     return;
1845   if (opt.with_colons)
1846     return; /* Handled elsewhere. */
1847
1848   es_fputs (_("      Card serial no. ="), es_stdout);
1849   es_putc (' ', es_stdout);
1850   if (strlen (serialno) == 32 && !strncmp (serialno, "D27600012401", 12))
1851     {
1852       /* This is an OpenPGP card.  Print the relevant part.  */
1853       /* Example: D2760001240101010001000003470000 */
1854       /*                          xxxxyyyyyyyy     */
1855       es_fprintf (es_stdout, "%.*s %.*s", 4, serialno+16, 8, serialno+20);
1856     }
1857  else
1858    es_fputs (serialno, es_stdout);
1859   es_putc ('\n', es_stdout);
1860 }
1861
1862
1863
1864 void
1865 set_attrib_fd (int fd)
1866 {
1867   static int last_fd = -1;
1868
1869   if (fd != -1 && last_fd == fd)
1870     return;
1871
1872   /* Fixme: Do we need to check for the log stream here?  */
1873   if (attrib_fp && attrib_fp != log_get_stream ())
1874     es_fclose (attrib_fp);
1875   attrib_fp = NULL;
1876   if (fd == -1)
1877     return;
1878
1879 #ifdef HAVE_DOSISH_SYSTEM
1880   setmode (fd, O_BINARY);
1881 #endif
1882   if (fd == 1)
1883     attrib_fp = es_stdout;
1884   else if (fd == 2)
1885     attrib_fp = es_stderr;
1886   else
1887     attrib_fp = es_fdopen (fd, "wb");
1888   if (!attrib_fp)
1889     {
1890       log_fatal ("can't open fd %d for attribute output: %s\n",
1891                  fd, strerror (errno));
1892     }
1893
1894   last_fd = fd;
1895 }