[PATCH GnuPG] scd:piv: Support the administration key of the Nitrokey 3
David Seifert
soap at gentoo.org
Wed Nov 11 12:39:17 CET 2026
* scd/app-piv.c (auth_adm_key): Use the current algorithm identifier
for 3DES and fall back to the legacy one. Retry without the empty
response tag if the card rejects it. Report a 6A80 of that retry as
bad authentication.
--
scdaemon could not authenticate with the administration key of a
Nitrokey 3 and thus could neither generate nor import keys on it.
The PIV application of the Nitrokey 3 differs in two ways:
- For 3DES it only accepts the identifier 0x03 and rejects the
legacy identifier 0x00, which scdaemon used, with 6A86.
- It rejects the empty response tag (82 00) in the second GENERAL
AUTHENTICATE command with 6A80. SP 800-73-5 Part 2 includes this
tag in its example in Table 23.
Request the witness for 3DES with 0x03 first, which is also what
Yubico's yubikit and set_adm_key use, and fall back to 0x00. Keep
sending the empty response tag, and only if the card rejects the
command retry without it, which is how yubikit sends the command;
this requires a new witness. The Nitrokey 3 also answers a wrong key
with 6A80, so report that as bad authentication.
Tested with the default administration keys:
Card Key Result
---------------------- ------- ------------------------------------
Nitrokey 3 3DES works with the retry without 82 00
YubiKey 4 (4.3.7) 3DES 0x03 and 82 00 work at first attempt
YubiKey 5 NFC (5.1.2) 3DES 0x03 and 82 00 work at first attempt
YubiKey 5C NFC (5.7.1) AES-192 commands unchanged
TOKEN2 PIN+ R3.3 AES-192 commands unchanged
On the Nitrokey 3 a P-256 key can now be generated in slot 95. On all
five cards a wrong key is reported as "Bad authentication" (6A80 on
the Nitrokey 3, 6982 on the others).
Signed-off-by: David Seifert <soap at gentoo.org>
---
scd/app-piv.c | 58 ++++++++++++++++++++++++++++++++++++---------------
1 file changed, 41 insertions(+), 17 deletions(-)
diff --git a/scd/app-piv.c b/scd/app-piv.c
index 74fa14844..baa9b187a 100644
--- a/scd/app-piv.c
+++ b/scd/app-piv.c
@@ -900,10 +900,13 @@ static const struct adm_key_algo_s
static gpg_error_t
auth_adm_key (app_t app, const unsigned char *value, size_t valuelen)
{
+ /* An empty witness requests a witness. */
+ static const unsigned char witness_req[] = { 0x7c, 0x02, 0x80, 0x00 };
gpg_error_t err = gpg_error (GPG_ERR_INV_LENGTH);
const struct adm_key_algo_s *ka;
int i;
int p1 = 0;
+ int resp_tag = 1;
size_t bl;
unsigned char tmpl[2+3*(2+16)];
size_t tmpllen;
@@ -917,25 +920,25 @@ auth_adm_key (app_t app, const unsigned char *value, size_t valuelen)
/* Request a witness. The card refuses that for an algorithm
* other than the one of its key, so the first algorithm with a
* matching key length for which we get a witness is the one. */
- tmpl[0] = 0x7c;
- tmpl[1] = 0x02;
- tmpl[2] = 0x80;
- tmpl[3] = 0; /* (Empty witness requests a witness.) */
- tmpllen = 4;
for (i=0; i < DIM (adm_key_algos); i++)
{
ka = adm_key_algos + i;
if (ka->keylen != valuelen)
continue;
- /* For 3DES we keep on using the legacy identifier because that
- * is what we always did and thus known to work with all
- * cards. */
- p1 = (ka->algo == PIV_ALGORITHM_3DES_ECB
- ? PIV_ALGORITHM_3DES_ECB_0 : ka->algo);
+ p1 = ka->algo;
err = iso7816_general_authenticate (app_get_slot (app), 0,
p1, 0x9B,
- tmpl, tmpllen, 0,
- &outdata, &outdatalen);
+ witness_req, sizeof witness_req,
+ 0, &outdata, &outdatalen);
+ if (err && p1 == PIV_ALGORITHM_3DES_ECB)
+ {
+ /* Some cards only know the legacy identifier for 3DES. */
+ p1 = PIV_ALGORITHM_3DES_ECB_0;
+ err = iso7816_general_authenticate (app_get_slot (app), 0,
+ p1, 0x9B,
+ witness_req, sizeof witness_req,
+ 0, &outdata, &outdatalen);
+ }
if (!err)
break;
}
@@ -952,6 +955,7 @@ auth_adm_key (app_t app, const unsigned char *value, size_t valuelen)
err = gcry_cipher_setkey (cipher, value, valuelen);
if (err)
goto leave;
+ again:
if (!(outdatalen && *outdata == 0x7c
&& (s = find_tlv (outdata, outdatalen, 0x80, &n))
&& n == bl))
@@ -966,22 +970,42 @@ auth_adm_key (app_t app, const unsigned char *value, size_t valuelen)
/* Return decrypted witness and send our challenge. */
tmpl[0] = 0x7c;
- tmpl[1] = 2 + bl + 2 + bl + 2;
+ tmpl[1] = 2 + bl + 2 + bl;
tmpl[2] = 0x80;
tmpl[3] = bl;
memcpy (tmpl+4, witness, bl);
tmpl[4+bl] = 0x81;
tmpl[5+bl] = bl;
gcry_create_nonce (tmpl+6+bl, bl);
- tmpl[6+2*bl] = 0x82;
- tmpl[7+2*bl] = 0;
- tmpllen = 8 + 2*bl;
+ tmpllen = 6 + 2*bl;
+ if (resp_tag)
+ {
+ /* Request the response with an empty tag as in the example of
+ * SP 800-73-5 Part 2, Table 23. */
+ tmpl[1] += 2;
+ tmpl[tmpllen++] = 0x82;
+ tmpl[tmpllen++] = 0;
+ }
xfree (outdata);
err = iso7816_general_authenticate (app_get_slot (app), 0,
p1, 0x9B,
tmpl, tmpllen, 0,
&outdata, &outdatalen);
- if (gpg_err_code (err) == GPG_ERR_BAD_PIN)
+ if (resp_tag && gpg_err_code (err) == GPG_ERR_INV_VALUE)
+ {
+ /* The Nitrokey 3 rejects the empty response tag. Retry
+ * without it; that requires a new witness. */
+ resp_tag = 0;
+ err = iso7816_general_authenticate (app_get_slot (app), 0,
+ p1, 0x9B,
+ witness_req, sizeof witness_req,
+ 0, &outdata, &outdatalen);
+ if (!err)
+ goto again;
+ }
+ /* The Nitrokey 3 also uses 6A80 for a wrong key. */
+ if (gpg_err_code (err) == GPG_ERR_BAD_PIN
+ || (!resp_tag && gpg_err_code (err) == GPG_ERR_INV_VALUE))
err = gpg_error (GPG_ERR_BAD_AUTH);
if (err)
goto leave;
--
2.56.0
More information about the Gnupg-devel
mailing list