From 0d6f276f61c583d776687029c715b1ee4280e4ed Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 30 Mar 2021 17:28:45 +0200 Subject: [PATCH] card: Print the key's label if available. * tools/gpg-card.h (struct key_info_s): Add field 'label'. * tools/card-call-scd.c (learn_status_cb): Parse KEY-LABEL. (scd_learn): Always request KEY-LABEL. * tools/gpg-card.c (nullnone): New. (list_one_kinfo, list_card): Use it. Print the label. -- PKCS#15 defines label which help to understand for what a key is intended. Print them. Signed-off-by: Werner Koch --- tools/card-call-scd.c | 35 +++++++++++++++++++++++++++++++++++ tools/gpg-card.c | 30 ++++++++++++++++++++++-------- tools/gpg-card.h | 3 +++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/tools/card-call-scd.c b/tools/card-call-scd.c index 95f28e564..5a2b3001d 100644 --- a/tools/card-call-scd.c +++ b/tools/card-call-scd.c @@ -157,6 +157,7 @@ release_card_info (card_info_t info) while (info->kinfo) { key_info_t kinfo = info->kinfo->next; + xfree (kinfo->label); xfree (info->kinfo); info->kinfo = kinfo; } @@ -915,6 +916,36 @@ learn_status_cb (void *opaque, const char *line) xfree (parm->chvlabels); parm->chvlabels = xstrdup (line); } + else if (!memcmp (keyword, "KEY-LABEL", keywordlen)) + { + /* The format of such a line is: + * KEY-LABEL [label|"-"] */ + const char *fields[2]; + int nfields; + char *label; + + line_buffer = pline = xstrdup (line); + + if ((nfields = split_fields (line_buffer, fields, DIM (fields))) < 2) + goto leave; /* not enough args - ignore. */ + + keyref = fields[0]; + /* We don't remove the percent escaping because that is only + * used in case of strange characters in the label; we + * should not print them. Note that this info is only for + * human consumption, anyway. */ + label = xtrystrdup (fields[1]); + if (!label) + goto leave; /* We ignore malloc failures here. */ + + /* Check whether we already have an item for the keyref. */ + kinfo = find_kinfo (parm, keyref); + if (!kinfo) /* New entry. */ + kinfo = create_kinfo (parm, keyref); + + xfree (kinfo->label); + kinfo->label = label; + } break; case 10: @@ -1153,6 +1184,10 @@ scd_learn (card_info_t info) if (gpg_err_code (err) == GPG_ERR_INV_NAME || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_OPERATION) err = 0; /* Not implemented or GETATTR not supported. */ + err = scd_getattr ("KEY-LABEL", info); + if (gpg_err_code (err) == GPG_ERR_INV_NAME + || gpg_err_code (err) == GPG_ERR_UNSUPPORTED_OPERATION) + err = 0; /* Not implemented or GETATTR not supported. */ } if (info == &dummyinfo) diff --git a/tools/gpg-card.c b/tools/gpg-card.c index 813a7b48b..fa6d62325 100644 --- a/tools/gpg-card.c +++ b/tools/gpg-card.c @@ -373,6 +373,14 @@ main (int argc, char **argv) } +/* Return S or the string "[none]" if S is NULL. */ +static GPGRT_INLINE const char * +nullnone (const char *s) +{ + return s? s: "[none]"; +} + + /* Read data from file FNAME up to MAX_GET_DATA_FROM_FILE characters. * On error return an error code and stores NULL at R_BUFFER; on * success returns 0 and stores the number of bytes read at R_BUFLEN @@ -668,7 +676,10 @@ list_one_kinfo (card_info_t info, key_info_t kinfo, { tty_fprintf (fp, "[none]\n"); tty_fprintf (fp, " keyref .....: %s\n", kinfo->keyref); - tty_fprintf (fp, " algorithm ..: %s\n", kinfo->keyalgo); + if (kinfo->label) + tty_fprintf (fp, " label ......: %s\n", kinfo->label); + tty_fprintf (fp, " algorithm ..: %s\n", + nullnone (kinfo->keyalgo)); goto leave; } @@ -690,10 +701,13 @@ list_one_kinfo (card_info_t info, key_info_t kinfo, } tty_fprintf (fp, "\n"); + if (kinfo->label) + tty_fprintf (fp, " label ......: %s\n", kinfo->label); + if (!(err = scd_readkey (kinfo->keyref, &s_pkey))) { char *tmp = pubkey_algo_string (s_pkey, NULL); - tty_fprintf (fp, " algorithm ..: %s\n", tmp); + tty_fprintf (fp, " algorithm ..: %s\n", nullnone (tmp)); xfree (tmp); gcry_sexp_release (s_pkey); s_pkey = NULL; @@ -701,7 +715,8 @@ list_one_kinfo (card_info_t info, key_info_t kinfo, else { maybe_set_card_removed (info, err); - tty_fprintf (fp, " algorithm ..: %s\n", kinfo->keyalgo); + tty_fprintf (fp, " algorithm ..: %s\n", + nullnone (kinfo->keyalgo)); } if (kinfo->fprlen && kinfo->created) @@ -785,7 +800,8 @@ list_one_kinfo (card_info_t info, key_info_t kinfo, if (label_keyref) tty_fprintf (fp, " keyref .....: %s\n", label_keyref); if (kinfo) - tty_fprintf (fp, " algorithm ..: %s\n", kinfo->keyalgo); + tty_fprintf (fp, " algorithm ..: %s\n", + nullnone (kinfo->keyalgo)); } leave: @@ -1039,14 +1055,12 @@ list_card (card_info_t info, int no_key_lookup) { estream_t fp = opt.interactive? NULL : es_stdout; - tty_fprintf (fp, "Reader ...........: %s\n", - info->reader? info->reader : "[none]"); + tty_fprintf (fp, "Reader ...........: %s\n", nullnone (info->reader)); if (info->cardtype) tty_fprintf (fp, "Card type ........: %s\n", info->cardtype); if (info->cardversion) print_a_version (fp, "Card firmware ....:", info->cardversion); - tty_fprintf (fp, "Serial number ....: %s\n", - info->serialno? info->serialno : "[none]"); + tty_fprintf (fp, "Serial number ....: %s\n", nullnone (info->serialno)); tty_fprintf (fp, "Application type .: %s%s%s%s\n", app_type_string (info->apptype), info->apptype == APP_TYPE_UNKNOWN && info->apptypestr? "(":"", diff --git a/tools/gpg-card.h b/tools/gpg-card.h index bd4b923e7..b79d12d0d 100644 --- a/tools/gpg-card.h +++ b/tools/gpg-card.h @@ -125,6 +125,9 @@ struct key_info_s const char *keyalgo; enum gcry_pk_algos keyalgo_id; + /* An optional malloced label for the key. */ + char *label; + /* The three next items are mostly useful for OpenPGP cards. */ unsigned char fprlen; /* Use length of the next item. */ unsigned char fpr[32]; /* The binary fingerprint of length FPRLEN. */