agent: Show "Label:" field of private key when prompt the insertion.

* agent/findkey.c (prompt_for_card): Use "Label:" field.
(agent_key_from_file): Use KEYMETA.

--

GnuPG-bug-id: 5986
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-05-20 14:38:33 +09:00
parent 6a37240cf2
commit 5986310866
1 changed files with 27 additions and 24 deletions

View File

@ -965,26 +965,30 @@ remove_key_file (const unsigned char *grip)
* Prompt a user the card insertion, when it's not available yet. * Prompt a user the card insertion, when it's not available yet.
*/ */
static gpg_error_t static gpg_error_t
prompt_for_card (ctrl_t ctrl, const unsigned char *shadow_info, prompt_for_card (ctrl_t ctrl, const unsigned char *grip,
const unsigned char *grip) nvc_t keymeta, const unsigned char *shadow_info)
{ {
char *serialno; char *serialno;
char *desc; char *desc;
char *want_sn; char *want_sn = NULL;
int len; int len;
gpg_error_t err; gpg_error_t err;
char hexgrip[41]; char hexgrip[41];
char *comment_buffer = NULL;
const char *comment = NULL;
bin2hex (grip, 20, hexgrip); bin2hex (grip, 20, hexgrip);
if (shadow_info) if (keymeta && (comment = nvc_get_string (keymeta, "Label:")))
{ {
err = parse_shadow_info (shadow_info, &want_sn, NULL, NULL); if (strchr (comment, '\n')
if (err) && (comment_buffer = linefeed_to_percent0A (comment)))
return err; comment = comment_buffer;
} }
else
want_sn = NULL; err = parse_shadow_info (shadow_info, &want_sn, NULL, NULL);
if (err)
return err;
len = want_sn? strlen (want_sn) : 0; len = want_sn? strlen (want_sn) : 0;
if (len == 32 && !strncmp (want_sn, "D27600012401", 12)) if (len == 32 && !strncmp (want_sn, "D27600012401", 12))
@ -1031,23 +1035,20 @@ prompt_for_card (ctrl_t ctrl, const unsigned char *shadow_info,
err = agent_card_keyinfo (ctrl, hexgrip, 0, &keyinfo); err = agent_card_keyinfo (ctrl, hexgrip, 0, &keyinfo);
if (!err) if (!err)
{ {
/* Key for GRIP found, use it directly. */ /* Key for GRIP found, use it. */
agent_card_free_keyinfo (keyinfo); agent_card_free_keyinfo (keyinfo);
xfree (want_sn); break;
return 0;
} }
} }
if (!want_sn) /* Card is not available. Prompt the insertion. */
; /* No shadow info so we can't ask; ERR is already set. */ if (asprintf (&desc,
else if (asprintf (&desc,
"%s:%%0A%%0A" "%s:%%0A%%0A"
" %s%%0A"
" %s", " %s",
L_("Please insert the card with serial number"), L_("Please insert the card with serial number"),
want_sn) < 0) want_sn ? want_sn : "", comment) < 0)
{ err = out_of_core ();
err = out_of_core ();
}
else else
{ {
err = agent_get_confirmation (ctrl, desc, NULL, NULL, 0); err = agent_get_confirmation (ctrl, desc, NULL, NULL, 0);
@ -1059,11 +1060,12 @@ prompt_for_card (ctrl_t ctrl, const unsigned char *shadow_info,
} }
if (err) if (err)
{ break;
xfree (want_sn);
return err;
}
} }
xfree (want_sn);
gcry_free (comment_buffer);
return err;
} }
@ -1271,7 +1273,8 @@ agent_key_from_file (ctrl_t ctrl, const char *cache_nonce,
* it's available. * it's available.
*/ */
if (strcmp (shadow_type, "t1-v1") == 0 && !grip) if (strcmp (shadow_type, "t1-v1") == 0 && !grip)
err = prompt_for_card (ctrl, *shadow_info, ctrl->keygrip); err = prompt_for_card (ctrl, ctrl->keygrip,
keymeta, *shadow_info);
} }
} }
else else