From soap at gentoo.org Tue Nov 3 13:17:24 2026 From: soap at gentoo.org (David Seifert) Date: Tue, 3 Nov 2026 13:17:24 +0100 Subject: [PATCH gnupg] scd:piv: Let the card decide whether signing needs a PIN Message-ID: <20261103121726.2212590-1-soap@gentoo.org> * scd/app-piv.c (YKPIV_METADATA_POLICY_TAG): Remove. (get_cached_data): Remove support for special tag 0xF7xx. (check_pin_policy): Remove. (do_sign): Do not verify the PIN before signing. If GENERAL AUTHENTICATE fails with GPG_ERR_BAD_PIN, force a verify and retry once. -- Deciding up front whether to ask for the PIN requires knowing the card's per-key PIN policy. The YubiKey GET METADATA command (0xF7) only exists on YubiKey 5.3 and later and was only used for cards detected as YubiKeys. Older YubiKeys and other cards implementing the same per-key policies (e.g. TOKEN2 PIV) thus fall back to forcing a verify for 9C on every signature, ignoring a configured ONCE or NEVER policy. Instead, just try the operation first. If the card refuses it with 6982 (mapped to GPG_ERR_BAD_PIN), force a fresh PIN entry and retry once. This works for any card that enforces its own policy, needs no vendor detection, and still prompts for every signature with a "PIN Always" key, because a forced verify bypasses the PIN cache. This changes signing for all keys handled by do_sign (and thus do_auth), not only 9C: e.g. 9E now signs without a PIN, as intended by SP 800-73. I have tested the change against various smartcards with PIV functionality. A fake pinentry counted the prompts - each scenario starts with a fresh P-256 key in 9C and a card reset. TOKEN2 PIV card (PIN+ R3.3, policy read back via GET METADATA): 9C policy Operations Prompts Result NEVER sign, sign 0, 0 both signed ONCE sign, sign 1, 0 both signed ALWAYS sign, sign 1, 1 both signed ALWAYS cancel 1 canceled, retries kept (6) ALWAYS wrong, good PIN 1, 1 Bad PIN (6 -> 5), then signed, retries back to 6 YubiKey 4 (firmware 4.3.7, policy confirmed via attestation): 9C policy Operations Prompts Result NEVER sign, sign 0, 0 both signed ONCE sign, sign 1, 0 both signed ALWAYS sign, sign 1, 1 both signed ALWAYS cancel 1 canceled, retries kept (3) ALWAYS wrong, good PIN 1, 1 Bad PIN (3 -> 2), then signed, retries back to 3 YubiKey 5 NFC (firmware 5.1.2, policy confirmed via attestation): 9C policy Operations Prompts Result NEVER sign, sign 0, 0 both signed ONCE sign, sign 1, 0 both signed ALWAYS sign, sign 1, 1 both signed ALWAYS cancel 1 canceled, retries kept (3) ALWAYS wrong, good PIN 1, 1 Bad PIN (3 -> 2), then signed, retries back to 3 Nitrokey 3 (accepts but ignores the policy at key generation and enforces "PIN Always" for 9C): 9C policy Operations Prompts Result NEVER sign, sign 1, 1 both signed ONCE sign, sign 1, 1 both signed ALWAYS sign, sign 1, 1 both signed ALWAYS cancel 1 canceled, retries kept (3) ALWAYS wrong, good PIN 1, 1 Bad PIN (3 -> 2), then signed, retries back to 3 Signed-off-by: David Seifert --- scd/app-piv.c | 109 ++++++++------------------------------------------ 1 file changed, 16 insertions(+), 93 deletions(-) diff --git a/scd/app-piv.c b/scd/app-piv.c index 9652ff68e..d9ff8be06 100644 --- a/scd/app-piv.c +++ b/scd/app-piv.c @@ -266,14 +266,9 @@ do_deinit (app_t app) } -#define YKPIV_METADATA_POLICY_TAG 0x02 - /* Wrapper around iso7816_get_data which first tries to get the data * from the cache. With GET_IMMEDIATE passed as true, the cache is * bypassed. The tag-53 container is also removed. - * - * A special tag F7xx is also supported to get PIN Policy and Touch - * Policy. */ static gpg_error_t get_cached_data (app_t app, int tag, @@ -311,43 +306,7 @@ get_cached_data (app_t app, int tag, } } - if (app->app_local->flags.yubikey && (tag >> 8) == 0xF7) - /* Get policies from metadata for a key slot. */ - { - unsigned char apdu[4]; - - /* We use a proprietary Yubikey command to get metadata for KEYREF. */ - apdu[0] = 0; - apdu[1] = 0xf7; /* Yubikey: Get metadata. */ - apdu[2] = 0; - apdu[3] = (tag & 0xff); - err = iso7816_apdu_direct (app_get_slot (app), apdu, 4, 1, - NULL, &p, &len); - if (!err) - { - unsigned char *policies = NULL; - - s = find_tlv (p, len, YKPIV_METADATA_POLICY_TAG, &len); - if (s && len == 2) - { - policies = xtrymalloc (len); - if (!policies) - err = gpg_error_from_syserror (); - else - memcpy (policies, s, len); - } - xfree (p); - if (policies) - p = policies; - else - { - p = NULL; - len = 0; - } - } - } - else - err = iso7816_get_data_odd (app_get_slot (app), 0, tag, &p, &len); + err = iso7816_get_data_odd (app_get_slot (app), 0, tag, &p, &len); if (err) return err; @@ -356,8 +315,6 @@ get_cached_data (app_t app, int tag, * (SP 800-73-5 Part 2, section 3.1.2) */ if (tag == 0x7E || tag == 0x7F61) ; - else if (app->app_local->flags.yubikey && (tag >> 8) == 0xF7) - ; else if (len && *p == 0x53 && (s = find_tlv (p, len, 0x53, &n))) { memmove (p, s, n); @@ -2191,48 +2148,6 @@ do_check_chv (app_t app, ctrl_t ctrl, const char *pwidstr) } -/* Examining the per slot PIN policy for KEYREF, return 1 if the - * verification of PIN is always required. Return 0, otherwise. */ -static int -check_pin_policy (app_t app, int keyref) -{ - unsigned char *buffer; - size_t buflen; - gpg_error_t err; - int value; - int force; - - if (!app->app_local->flags.yubikey) - { - no_f7xx: - switch (keyref) - { - case 0x9c: force = 1; break; - default: force = 0; break; - } - return force; - } - - /* Use special tag F7xx to get PIN Policy and Touch Policy. */ - err = get_cached_data (app, ((0xf7 << 8) | keyref), &buffer, &buflen, 0); - if (err) - goto no_f7xx; - - if (buflen == 2) - value = buffer[0]; /* 0: Default, 1: Never, 2: Once, 3: Always */ - else - goto no_f7xx; - - switch (keyref) - { - case 0x9c: force = (value == 0 || value == 3); break; - default: force = (value == 3); break; - } - - xfree (buffer); - return force; -} - /* Compute a digital signature using the GENERAL AUTHENTICATE command * on INDATA which is expected to be the raw message digest. The * KEYIDSTR has the key reference or its OID (e.g. "PIV.9A"). The @@ -2261,7 +2176,6 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo, unsigned char *indata_buffer = NULL; /* Malloced helper. */ unsigned char *apdudata = NULL; size_t apdudatalen; - int force_verify; (void)ctrl; @@ -2284,7 +2198,9 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo, * for each key. It's user's control. * https://developers.yubico.com/PIV/Introduction/Yubico_extensions.html */ - force_verify = check_pin_policy (app, keyref); + /* Don't hardcode per-key PIN rules: let the card enforce its own + * policy (SP 800-73 "PIN Always", Yubico per-key policies, ...). + * We try the operation first and only VERIFY if the card refuses. */ err = get_key_algorithm_by_dobj (app, dobj, &mechanism); if (err) @@ -2446,11 +2362,6 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo, goto leave; } - /* Now verify the Application PIN. */ - err = verify_chv (app, ctrl, 0x80, force_verify); - if (err) - goto leave; - /* Build the Dynamic Authentication Template. */ err = concat_tlv_list (0, &apdudata, &apdudatalen, (int)0x7c, (size_t)0, NULL, /* Constructed. */ @@ -2465,6 +2376,18 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo, mechanism, keyref, apdudata, (int)apdudatalen, 0, &outdata, &outdatalen); + /* GPG_ERR_BAD_PIN (SW 6982 or 63Cx): the card wants a VERIFY first. + * Force it even if the PIN is already verified, because a "PIN + * Always" key needs a fresh one. Retry only once. */ + if (gpg_err_code (err) == GPG_ERR_BAD_PIN) + { + err = verify_chv (app, ctrl, 0x80, 1); + if (!err) + err = iso7816_general_authenticate (app_get_slot (app), -1, + mechanism, keyref, + apdudata, (int)apdudatalen, 0, + &outdata, &outdatalen); + } if (err) goto leave; -- 2.56.0 From reishoku at reishoku.net Wed Nov 4 01:00:38 2026 From: reishoku at reishoku.net (KOSHIKAWA Kenichi) Date: Wed, 4 Nov 2026 09:00:38 +0900 Subject: [gnupg] gpgv --output FILE leaves FILE.part (regression in 2.5.21) Message-ID: Hello, I could not open a task on dev.gnupg.org, so I am reporting this here. Since 2.5.21, "gpgv --output FILE" exits with 0 and reports a good signature, but FILE is never created. The data is left in FILE.part. "gpg --verify --output FILE" is not affected. Affected: 2.5.21 to 2.5.24, and master as of 5b001e5ccd62 Good: 2.5.20, 2.4.x (checked 2.4.4 and 2.4.7) Systems: Linux x86_64 (Ubuntu 24.04, built from git) macOS 27.0.1 arm64 (2.5.24 from Homebrew) Recipe (any gpg 2.x can create the key and the signature): mkdir -m 700 home gpg --homedir home --batch --pinentry-mode loopback --passphrase '' \ --quick-gen-key 'Test ' ed25519 sign never printf 'Hello\n' > data.txt gpg --homedir home --batch --clearsign -o signed.asc data.txt gpg --homedir home --batch --export > trusted.gpg gpgv --homedir home --keyring ./trusted.gpg --output out signed.asc ls out* Expected: out Actual: out.part Output of the gpgv steps with 2.5.24, built from the gnupg-2.5.24 tag on Linux x86_64 (the key and signature were made with gpg 2.4.4): $ gpgv --version | head -n 1 gpgv (GnuPG) 2.5.24 $ gpgv --homedir home --keyring ./trusted.gpg --output out signed.asc gpgv: Signature made Tue Nov 3 16:12:01 2026 UTC gpgv: using EDDSA key 200E09EBB8AD23B976698A94C71F7B4433288887 gpgv: Good signature from "Test " $ echo $? 0 $ ls out* out.part With master (2.5.25-beta34) the result is the same. With 2.5.20, "ls out*" shows "out", and its content is the signed text. git bisect between gnupg-2.5.20 and gnupg-2.5.24 gives ab2e64e4b490 gpg: Defer renaming at exit, so that it can remove on failure. (T7873) as the first bad commit. With it, pfg_close_file only registers the rename, and gnupg_commit_partial_file is called from g10_exit in g10/gpg.c. gpgv has its own g10_exit in g10/gpgv.c, which does not call it, so the .part file is never renamed. The later changes for T8399 and 31339d07 only touch the gpg side. Exit code when FILE cannot be created (FILE is an existing directory): 2.4.4, 2.5.20 2 "error creating 'out'" a9c9435c (2.5.21-beta15) 2 "renaming 'out.part' ... failed" 2.5.24, master 0 (FILE.part is left, as above) In g10/gpg.c, gnupg_commit_partial_file is called after the exit code is computed, so the log_error from a failed rename is not counted. "gpg --verify --output FILE" on master prints "renaming 'out.part' to 'out' failed" and exits with 0 in the same case. This looks like the same problem on the gpg side. When FILE already exists, gpgv 2.5.21 and later keep its old content and still exit with 0 and GOODSIG. A caller that passes a file created with mktemp, or that reuses an output path, may then read old data as verified. I leave it to you whether this needs more than a normal bug fix. The test suite did not catch this, because tests/openpgp/gpgv.scm does not use --output. I can test a fix on Linux and macOS. I used an AI assistant (Anthropic's Claude) for the bisect and the analysis, and to help write this report. I have checked the results above myself. Thanks, Kenichi -- KOSHIKAWA Kenichi reishoku at reishoku.net -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 265 bytes Desc: not available URL: From peijiankang at kylinos.cn Thu Nov 5 04:39:13 2026 From: peijiankang at kylinos.cn (=?UTF-8?B?6KO05bu65bq3?=) Date: Thu, 05 Nov 2026 11:39:13 +0800 Subject: =?UTF-8?B?W1BBVENIXSBwaW5lbnRyeS1lbWFjczogZml4IHRpbWVvdXQgcmFjZSBpbiByZWFkX2Zyb21fZW1hY3MgKENXRS0zNjIp?= Message-ID: <15sf6pup1j2j-15sk5dd3kmvg@nsmail8.2--kylin--1> An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: 0001-fix-timeout-race-1.3.3.patch Type: application/octet-stream Size: 1808 bytes Desc: not available URL: From gniibe at fsij.org Thu Nov 5 06:16:52 2026 From: gniibe at fsij.org (NIIBE Yutaka) Date: Thu, 05 Nov 2026 14:16:52 +0900 Subject: [gnupg] gpgv --output FILE leaves FILE.part (regression in 2.5.21) In-Reply-To: References: Message-ID: <87cxsjamd7.fsf@haruna.fsij.org> Hello, Thank you for your report with repro. I didn't notice until today (sorry, I rarely use gpgv). KOSHIKAWA Kenichi wrote: > ab2e64e4b490 gpg: Defer renaming at exit, so that it can remove > on failure. (T7873) Ah, indeed. One liner could fix the issue: ========================== diff --git a/g10/gpgv.c b/g10/gpgv.c index 382720ef8..9790cae3b 100644 --- a/g10/gpgv.c +++ b/g10/gpgv.c @@ -322,6 +322,7 @@ g10_exit (int rc) else if (opt.assert_pubkey_algos && assert_pubkey_algo_false) rc = 1; + gnupg_commit_partial_file (); exit (rc); } ========================== I created a ticket for this: https://static.dev.gnupg.org/T8540.html -- From gniibe at fsij.org Thu Nov 5 06:26:01 2026 From: gniibe at fsij.org (NIIBE Yutaka) Date: Thu, 05 Nov 2026 14:26:01 +0900 Subject: [PATCH] pinentry-emacs: fix timeout race in read_from_emacs (CWE-362) In-Reply-To: <15sf6pup1j2j-15sk5dd3kmvg@nsmail8.2--kylin--1> References: <15sf6pup1j2j-15sk5dd3kmvg@nsmail8.2--kylin--1> Message-ID: <87a4nnalxy.fsf@haruna.fsij.org> Hello, Thank you for your report. pinentry-emacs is deprecated, it only existed in the pinentry source code repository until recently. It has been removed already from the repository. 裴建康 wrote: > Tested on openEuler 20.03 LTS-SP4 with its /usr/bin/pinentry-emacs > (pinentry 1.1.0). The patch is against pinentry-1.3.3 and also applies > to 1.1.0, 1.1.1 and 1.2.x. pinentry-emacs is for GnuPG 2.0 or older (with Emacs 25 or older). I wonder the reason why openEuler 20.03 includes pinentry-emacs. Which version of Emacs it has? Are you (or any users) using it? And in what situation? If any users with openEuler, I would suggest to build your pinentry-emacs with the patch. -- From gniibe at fsij.org Fri Nov 6 06:14:04 2026 From: gniibe at fsij.org (NIIBE Yutaka) Date: Fri, 06 Nov 2026 14:14:04 +0900 Subject: [PATCH gnupg] scd:piv: Let the card decide whether signing needs a PIN In-Reply-To: <20261103121726.2212590-1-soap@gentoo.org> References: <20261103121726.2212590-1-soap@gentoo.org> Message-ID: <87zevmsfs3.fsf@haruna.fsij.org> Hello, Thank you for your contribution. David Seifert wrote: > * scd/app-piv.c (YKPIV_METADATA_POLICY_TAG): Remove. > (get_cached_data): Remove support for special tag 0xF7xx. > (check_pin_policy): Remove. > (do_sign): Do not verify the PIN before signing. If GENERAL > AUTHENTICATE fails with GPG_ERR_BAD_PIN, force a verify and retry > once. Looks good. Please have a look at gnupg/doc/HACKING, specifically, the "License policy" section. And please consider sending your signed DCO. -- From soap at gentoo.org Fri Nov 6 10:03:14 2026 From: soap at gentoo.org (David Seifert) Date: Fri, 06 Nov 2026 10:03:14 +0100 Subject: DCO for David Seifert Message-ID: <6dec6090812be78fb6971ece854849c0e391fda2.camel@gentoo.org> GnuPG Developer's Certificate of Origin. Version 1.0 ===================================================== By making a contribution to the GnuPG project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the free software license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate free software license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same free software license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the free software license(s) involved. Signed-off-by: David Seifert -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 1042 bytes Desc: This is a digitally signed message part URL: From soap at gentoo.org Fri Nov 6 10:05:26 2026 From: soap at gentoo.org (David Seifert) Date: Fri, 06 Nov 2026 10:05:26 +0100 Subject: [PATCH gnupg] scd:piv: Let the card decide whether signing needs a PIN In-Reply-To: <87zevmsfs3.fsf@haruna.fsij.org> References: <20261103121726.2212590-1-soap@gentoo.org> <87zevmsfs3.fsf@haruna.fsij.org> Message-ID: On Fri, 2026-11-06 at 14:14 +0900, NIIBE Yutaka wrote: > Hello, > > Thank you for your contribution. > > David Seifert wrote: > > * scd/app-piv.c (YKPIV_METADATA_POLICY_TAG): Remove. > > (get_cached_data): Remove support for special tag 0xF7xx. > > (check_pin_policy): Remove. > > (do_sign): Do not verify the PIN before signing. If GENERAL > > AUTHENTICATE fails with GPG_ERR_BAD_PIN, force a verify and retry > > once. > > Looks good. > > Please have a look at gnupg/doc/HACKING, specifically, the "License > policy" section.  And please consider sending your signed DCO. Hi Niibe, I just sent in the DCO, it likely got lost before I registered for gnupg-devel. Regards David From reishoku at reishoku.net Sat Nov 7 05:01:27 2026 From: reishoku at reishoku.net (KOSHIKAWA Kenichi) Date: Sat, 7 Nov 2026 13:01:27 +0900 Subject: [gnupg] gpgv --output FILE leaves FILE.part (regression in 2.5.21) In-Reply-To: <87cxsjamd7.fsf@haruna.fsij.org> References: <87cxsjamd7.fsf@haruna.fsij.org> Message-ID: Hello NIIBE-san, NIIBE Yutaka wrote: > One liner could fix the issue: [...] > I created a ticket for this: > > https://static.dev.gnupg.org/T8540.html Thank you for the fix and for pushing it (e10ce533). I tested master (a6aa02c0) on Linux x86_64: - The recipe from my report now creates "out" with the signed text, and no out.part is left. - A bad signature gives exit code 1, as in 2.4. - An existing "out" is now replaced with the verified data. - "make -C tests/openpgp check": 64 succeeded, 0 failed, 3 skipped. Outputs of the recipe described previously, as of a6aa02c0: $ git log -1 --format='%h %s' a6aa02c0 gpgsm: Use a separate source file also for KEM based encryption. $ git merge-base --is-ancestor e10ce533 a6aa02c0 && echo "e10ce533 is in a6aa02c0" e10ce533 is in a6aa02c0 $ gpgv --version | head -n 1 gpgv (GnuPG) 2.5.25-beta38 $ gpgv --homedir home --keyring ./trusted.gpg --output out signed.asc gpgv: Signature made Sat Nov 7 03:47:15 2026 UTC gpgv: using EDDSA key 81724F3A3B331D3DEB423ADFB70F62D0A395009F gpgv: Good signature from "Test " $ echo $? 0 $ ls out* out $ cat out Hello Expected: out Actual: out (previously: out.part) Thanks, Kenichi -- KOSHIKAWA Kenichi reishoku at reishoku.net On Thu, Nov 05, 2026 at 02:16:52PM +0900, NIIBE Yutaka wrote: > Hello, > > Thank you for your report with repro. I didn't notice until today > (sorry, I rarely use gpgv). > > KOSHIKAWA Kenichi wrote: > > ab2e64e4b490 gpg: Defer renaming at exit, so that it can remove > > on failure. (T7873) > > Ah, indeed. > > One liner could fix the issue: > ========================== > diff --git a/g10/gpgv.c b/g10/gpgv.c > index 382720ef8..9790cae3b 100644 > --- a/g10/gpgv.c > +++ b/g10/gpgv.c > @@ -322,6 +322,7 @@ g10_exit (int rc) > else if (opt.assert_pubkey_algos && assert_pubkey_algo_false) > rc = 1; > > + gnupg_commit_partial_file (); > exit (rc); > } > > ========================== > > I created a ticket for this: > > https://static.dev.gnupg.org/T8540.html > -- -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 265 bytes Desc: not available URL: From gniibe at fsij.org Tue Nov 10 02:35:07 2026 From: gniibe at fsij.org (NIIBE Yutaka) Date: Tue, 10 Nov 2026 10:35:07 +0900 Subject: [PATCH gnupg] scd:piv: Let the card decide whether signing needs a PIN In-Reply-To: References: <20261103121726.2212590-1-soap@gentoo.org> <87zevmsfs3.fsf@haruna.fsij.org> Message-ID: <87a4nho4dw.fsf@haruna.fsij.org> Hello, Thank you for your sending DCO. I put the record in AUTHORS and applied your change to master. Also, updated the ticket: https://static.dev.gnupg.org/T8442.html -- From soap at gentoo.org Tue Nov 10 13:40:17 2026 From: soap at gentoo.org (David Seifert) Date: Tue, 10 Nov 2026 13:40:17 +0100 Subject: [PATCH GnuPG 1/2] scd:piv: Support AES administration keys Message-ID: <20261110124018.140795-1-soap@gentoo.org> * scd/app-piv.c (adm_key_algos): New. (auth_adm_key): Support AES-128, AES-192 and AES-256 keys. Try AES-192 before 3DES for 24 byte keys. (set_adm_key_algo): New. Code taken from set_adm_key. Return the status word instead of logging it. (set_adm_key): Support AES keys. Set 24 byte keys as AES-192 key and fall back to 3DES. Log only the last failure. -- YubiKeys with firmware 5.7 come with an AES-192 administration key; its value is the same as that of the former 3DES default key. scdaemon only implemented 3DES and thus authentication failed, which made it impossible to generate or import keys or to write certificates. 3DES and AES-192 keys are both 24 bytes long. To tell them apart, we request a witness using AES-192 first. The card refuses this with 6A80 if its key is a 3DES key and we try again with 3DES. For 3DES we keep on using the algorithm identifier 0x00 as before. Tested on a YubiKey 5C NFC (firmware 5.7.1): - Authentication with 3DES, AES-128, AES-192 and AES-256 keys, followed by key generation; wrong keys are rejected. - Setting 16, 24 and 32 byte keys, checked with ykman. A 24 byte key replacing a 3DES key is set as AES-192 key. Tested on a YubiKey 5 NFC (firmware 5.1.2), which has no AES support: - Authentication with a 3DES key; the card refuses AES-192 with 6A80. Wrong keys are rejected. - Setting a 24 byte key falls back to 3DES without logging an error, checked with ykman. 16 and 32 byte keys are rejected and the current key is kept. Tested on a TOKEN2 PIN+ R3.3 card, which comes with an AES-192 administration key: - The card refuses witness requests for AES-128, AES-256 and 3DES with 6A80. Authentication with AES-192 works; wrong keys are rejected. GnuPG-bug-id: 5837 Signed-off-by: David Seifert --- scd/app-piv.c | 164 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 112 insertions(+), 52 deletions(-) diff --git a/scd/app-piv.c b/scd/app-piv.c index d9ff8be06..a316fd41a 100644 --- a/scd/app-piv.c +++ b/scd/app-piv.c @@ -875,70 +875,109 @@ do_getattr (app_t app, ctrl_t ctrl, const char *name) } +/* The algorithms supported for the Card Application Administration + * Key. AES-192 and 3DES both use 24 byte keys; we try AES-192 first + * because it is the default since Yubikey firmware 5.7 and fall back + * to 3DES if the card rejects it. */ +static const struct adm_key_algo_s +{ + int algo; /* The PIV algorithm identifier. */ + int cipher; /* The Libgcrypt cipher. */ + size_t keylen; + size_t blklen; +} adm_key_algos[] = + { + { PIV_ALGORITHM_AES128_ECB, GCRY_CIPHER_AES128, 16, 16 }, + { PIV_ALGORITHM_AES192_ECB, GCRY_CIPHER_AES192, 24, 16 }, + { PIV_ALGORITHM_3DES_ECB, GCRY_CIPHER_3DES, 24, 8 }, + { PIV_ALGORITHM_AES256_ECB, GCRY_CIPHER_AES256, 32, 16 } + }; + + /* Authenticate the card using the Card Application Administration - * Key. (VALUE,VALUELEN) has that 24 byte key. */ + * Key. (VALUE,VALUELEN) has that 3DES or AES key. */ static gpg_error_t auth_adm_key (app_t app, const unsigned char *value, size_t valuelen) { - gpg_error_t err; - unsigned char tmpl[4+24]; + gpg_error_t err = gpg_error (GPG_ERR_INV_LENGTH); + const struct adm_key_algo_s *ka; + int i; + int p1 = 0; + size_t bl; + unsigned char tmpl[2+3*(2+16)]; size_t tmpllen; unsigned char *outdata = NULL; size_t outdatalen; const unsigned char *s; - char witness[8]; + unsigned char witness[16]; size_t n; gcry_cipher_hd_t cipher = NULL; - /* Prepare decryption. */ - err = gcry_cipher_open (&cipher, GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_ECB, 0); - if (err) - goto leave; - err = gcry_cipher_setkey (cipher, value, valuelen); - if (err) - goto leave; - - /* Request a witness. */ + /* 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; - err = iso7816_general_authenticate (app_get_slot (app), 0, - PIV_ALGORITHM_3DES_ECB_0, 0x9B, - tmpl, tmpllen, 0, - &outdata, &outdatalen); + 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); + err = iso7816_general_authenticate (app_get_slot (app), 0, + p1, 0x9B, + tmpl, tmpllen, 0, + &outdata, &outdatalen); + if (!err) + break; + } if (gpg_err_code (err) == GPG_ERR_BAD_PIN) err = gpg_error (GPG_ERR_BAD_AUTH); + if (err) + goto leave; + bl = ka->blklen; + + /* Prepare decryption. */ + err = gcry_cipher_open (&cipher, ka->cipher, GCRY_CIPHER_MODE_ECB, 0); + if (err) + goto leave; + err = gcry_cipher_setkey (cipher, value, valuelen); if (err) goto leave; if (!(outdatalen && *outdata == 0x7c && (s = find_tlv (outdata, outdatalen, 0x80, &n)) - && n == 8)) + && n == bl)) { err = gpg_error (GPG_ERR_CARD); log_error ("piv: improper witness received\n"); goto leave; } - err = gcry_cipher_decrypt (cipher, witness, 8, s, 8); + err = gcry_cipher_decrypt (cipher, witness, bl, s, bl); if (err) goto leave; /* Return decrypted witness and send our challenge. */ tmpl[0] = 0x7c; - tmpl[1] = 22; + tmpl[1] = 2 + bl + 2 + bl + 2; tmpl[2] = 0x80; - tmpl[3] = 8; - memcpy (tmpl+4, witness, 8); - tmpl[12] = 0x81; - tmpl[13] = 8; - gcry_create_nonce (tmpl+14, 8); - tmpl[22] = 0x82; - tmpl[23] = 0; - tmpllen = 24; + 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; xfree (outdata); err = iso7816_general_authenticate (app_get_slot (app), 0, - PIV_ALGORITHM_3DES_ECB_0, 0x9B, + p1, 0x9B, tmpl, tmpllen, 0, &outdata, &outdatalen); if (gpg_err_code (err) == GPG_ERR_BAD_PIN) @@ -947,17 +986,17 @@ auth_adm_key (app_t app, const unsigned char *value, size_t valuelen) goto leave; if (!(outdatalen && *outdata == 0x7c && (s = find_tlv (outdata, outdatalen, 0x82, &n)) - && n == 8)) + && n == bl)) { err = gpg_error (GPG_ERR_CARD); log_error ("piv: improper challenge received\n"); goto leave; } /* (We reuse the witness buffer.) */ - err = gcry_cipher_decrypt (cipher, witness, 8, s, 8); + err = gcry_cipher_decrypt (cipher, witness, bl, s, bl); if (err) goto leave; - if (memcmp (witness, tmpl+14, 8)) + if (memcmp (witness, tmpl+6+bl, bl)) { err = gpg_error (GPG_ERR_BAD_AUTH); goto leave; @@ -970,19 +1009,23 @@ auth_adm_key (app_t app, const unsigned char *value, size_t valuelen) } -/* Set a new admin key. */ +/* Set a new admin key using the algorithm KA. The status word of the + * command is stored at R_SW, or 0 if no command was sent. */ static gpg_error_t -set_adm_key (app_t app, const unsigned char *value, size_t valuelen) +set_adm_key_algo (app_t app, const struct adm_key_algo_s *ka, + const unsigned char *value, size_t valuelen, + unsigned int *r_sw) { gpg_error_t err; - unsigned char apdu[8+24]; - unsigned int sw; + unsigned char apdu[8+32]; + + *r_sw = 0; /* Check whether it is a weak key and that it is of proper length. */ { gcry_cipher_hd_t cipher; - err = gcry_cipher_open (&cipher, GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_ECB, 0); + err = gcry_cipher_open (&cipher, ka->cipher, GCRY_CIPHER_MODE_ECB, 0); if (!err) { err = gcry_cipher_setkey (cipher, value, valuelen); @@ -995,27 +1038,19 @@ set_adm_key (app_t app, const unsigned char *value, size_t valuelen) if (app->app_local->flags.yubikey) { /* This is a Yubikey. */ - if (valuelen != 24) - { - err = gpg_error (GPG_ERR_INV_LENGTH); - goto leave; - } - /* We use a proprietary Yubikey command. */ apdu[0] = 0; apdu[1] = 0xff; apdu[2] = 0xff; apdu[3] = 0xff; /* touch policy: 0xff=never, 0xfe = always. */ - apdu[4] = 3 + 24; - apdu[5] = PIV_ALGORITHM_3DES_ECB; + apdu[4] = 3 + valuelen; + apdu[5] = ka->algo; apdu[6] = 0x9b; - apdu[7] = 24; - memcpy (apdu+8, value, 24); - err = iso7816_apdu_direct (app_get_slot (app), apdu, 8+24, 0, - &sw, NULL, NULL); - wipememory (apdu+8, 24); - if (err) - log_error ("piv: setting admin key failed; sw=%04x\n", sw); + apdu[7] = valuelen; + memcpy (apdu+8, value, valuelen); + err = iso7816_apdu_direct (app_get_slot (app), apdu, 8+valuelen, 0, + r_sw, NULL, NULL); + wipememory (apdu+8, valuelen); /* A PIN is not required, thus use a better error code. */ if (gpg_err_code (err) == GPG_ERR_BAD_PIN) err = gpg_error (GPG_ERR_NO_AUTH); @@ -1028,6 +1063,31 @@ set_adm_key (app_t app, const unsigned char *value, size_t valuelen) return err; } + +/* Set a new admin key. The algorithm is taken from the length of the + * key; a 24 byte key is set as AES-192 key, or as 3DES key if the card + * does not support AES. */ +static gpg_error_t +set_adm_key (app_t app, const unsigned char *value, size_t valuelen) +{ + gpg_error_t err = gpg_error (GPG_ERR_INV_LENGTH); + unsigned int sw = 0; + int i; + + for (i=0; i < DIM (adm_key_algos); i++) + if (adm_key_algos[i].keylen == valuelen) + { + err = set_adm_key_algo (app, adm_key_algos + i, value, valuelen, &sw); + if (!err) + break; + } + /* Only report the last failure; an earlier one is expected for an + * AES-192 key on a card without AES support. */ + if (err && sw) + log_error ("piv: setting admin key failed; sw=%04x\n", sw); + return err; +} + /* Handle the SETATTR operation. All arguments are already basically * checked. */ static gpg_error_t -- 2.56.0 From soap at gentoo.org Tue Nov 10 13:40:18 2026 From: soap at gentoo.org (David Seifert) Date: Tue, 10 Nov 2026 13:40:18 +0100 Subject: [PATCH GnuPG 2/2] scd:piv: Support 4096 bit RSA keys In-Reply-To: <20261110124018.140795-1-soap@gentoo.org> References: <20261110124018.140795-1-soap@gentoo.org> Message-ID: <20261110124018.140795-2-soap@gentoo.org> * scd/app-piv.c (PIV_ALGORITHM_RSA_4096): New. (get_key_algorithm_by_dobj): Map RSA keys larger than 3072 bit to PIV_ALGORITHM_RSA_4096. (do_sign, do_decipher): Support PIV_ALGORITHM_RSA_4096. (get_keygrip_by_tag, do_readkey): Handle 3072 and 4096 bit RSA keys stored without a certificate. (writekey_rsa): Use PIV_ALGORITHM_RSA_4096 for keys larger than 3072 bit. (do_genkey): Support "rsa4096". -- YubiKeys with firmware 5.7 and later support 4096 bit RSA keys in PIV using the algorithm identifier 0x16. scdaemon took any RSA key larger than 3000 bit for a 3072 bit key, so signing and decryption with such a key failed with "input of 512 octets too large for mechanism 5", and generating or importing such a key was not possible. After generating or importing a key, scdaemon stores the public key in the certificate object until a certificate is written. Reading such a key only worked for 2048 bit RSA keys; for 3072 bit keys it failed with "Invalid public key algorithm". This is fixed as well. Tested on a YubiKey 5C NFC (firmware 5.7.1) and a TOKEN2 PIN+ R3.3 card (which also implements 0x16) with on-card generated 4096 bit keys in 9C and 9D: - gpgsm signing and decryption, checked with OpenSSL. - gpg with both keys added as OpenPGP subkeys ([S] and [E]) to a software primary key: the backsig made by 9C, signing and verifying, and encrypting and decrypting. - RSA-PSS (SHA-256, 32 byte salt): PKSIGN --hash=none with a 512 byte PSS encoded block, checked with OpenSSL. Without this change all of these fail. Key generation and import through scdaemon on the YubiKey 5C NFC: - Generating 2048, 3072 and 4096 bit RSA keys and a nistp256 key, reading the RSA keys back and signing with them, checked with OpenSSL. Decryption with a 4096 bit key in 9D. - Importing a 4096 bit RSA key into 9D; the keygrip matches the software key, signing and decryption work. Key generation through scdaemon on the TOKEN2 card: - Generating 4096 bit RSA keys in 9A, 9C and 9D, reading them back and signing with them (PKCS#1 v1.5 and PSS), checked with OpenSSL. Decryption with the key in 9D. Signed-off-by: David Seifert --- scd/app-piv.c | 52 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/scd/app-piv.c b/scd/app-piv.c index a316fd41a..74fa14844 100644 --- a/scd/app-piv.c +++ b/scd/app-piv.c @@ -81,6 +81,7 @@ #define PIV_ALGORITHM_AES256_CBC 0x0D #define PIV_ALGORITHM_ECC_P256 0x11 #define PIV_ALGORITHM_ECC_P384 0x14 +#define PIV_ALGORITHM_RSA_4096 0x16 /* Yubico extension (fw 5.7). */ /* The AID for PIV. */ @@ -1332,7 +1333,9 @@ get_keygrip_by_tag (app_t app, unsigned int tag, goto leave; if (mechanism) /* Compute keygrip from public key. */ { - if (mechanism == PIV_ALGORITHM_RSA_2048) + if (mechanism == PIV_ALGORITHM_RSA_2048 + || mechanism == PIV_ALGORITHM_RSA_3072 + || mechanism == PIV_ALGORITHM_RSA_4096) err = genkey_parse_rsa (certbuf, certbuflen, &s_pkey); else if (mechanism == PIV_ALGORITHM_ECC_P256 || mechanism == PIV_ALGORITHM_ECC_P384) @@ -1547,7 +1550,9 @@ do_readkey (app_t app, ctrl_t ctrl, const char *keyrefstr, unsigned int flags, else { /* Convert the public key into the expected s-expression. */ - if (mechanism == PIV_ALGORITHM_RSA_2048) + if (mechanism == PIV_ALGORITHM_RSA_2048 + || mechanism == PIV_ALGORITHM_RSA_3072 + || mechanism == PIV_ALGORITHM_RSA_4096) err = genkey_parse_rsa (cert, certlen, &s_pkey); else if (mechanism == PIV_ALGORITHM_ECC_P256 || mechanism == PIV_ALGORITHM_ECC_P384) @@ -1636,6 +1641,7 @@ get_key_algorithm_by_dobj (app_t app, data_object_t dobj, int *r_mechanism) { case PIV_ALGORITHM_RSA_2048: case PIV_ALGORITHM_RSA_3072: + case PIV_ALGORITHM_RSA_4096: case PIV_ALGORITHM_ECC_P256: case PIV_ALGORITHM_ECC_P384: *r_mechanism = mechanism; @@ -1698,7 +1704,9 @@ get_key_algorithm_by_dobj (app_t app, data_object_t dobj, int *r_mechanism) switch (algo) { case GCRY_PK_RSA: - if (gcry_pk_get_nbits (s_pkey) > 3000) + if (gcry_pk_get_nbits (s_pkey) > 3072) + algo = PIV_ALGORITHM_RSA_4096; + else if (gcry_pk_get_nbits (s_pkey) > 3000) algo = PIV_ALGORITHM_RSA_3072; else algo = PIV_ALGORITHM_RSA_2048; @@ -2330,18 +2338,30 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo, * this is PSS formatted data and we use it verbatim; PIV cards * accept PSS as well as PKCS#1. */ } + else if (mechanism == PIV_ALGORITHM_RSA_4096 + && indatalen == 4096/8 && indata[indatalen-1] == 0xBC) + { + /* If the provided data length matches the supported RSA + * framelen and the last octet of the data is 0xBC, we assume + * this is PSS formatted data and we use it verbatim; PIV cards + * accept PSS as well as PKCS#1. */ + } else if (mechanism == PIV_ALGORITHM_RSA_2048 - || mechanism == PIV_ALGORITHM_RSA_3072) + || mechanism == PIV_ALGORITHM_RSA_3072 + || mechanism == PIV_ALGORITHM_RSA_4096) { - /* PIV requires 2048 bit or 3072 bit RSA. */ + /* PIV requires 2048 bit or 3072 bit RSA; Yubikeys with + * firmware 5.7 also support 4096 bit RSA. */ unsigned int framelen; unsigned char *frame; int i; if (mechanism == PIV_ALGORITHM_RSA_2048) framelen = 2048 / 8; - else + else if (mechanism == PIV_ALGORITHM_RSA_3072) framelen = 3072 / 8; + else + framelen = 4096 / 8; oidbuflen = sizeof oidbuf; if (!hashalgo) @@ -2460,7 +2480,8 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo, memmove (outdata, outdata + (s - outdata), n); outdatalen = n; } - else if (mechanism == PIV_ALGORITHM_RSA_3072) + else if (mechanism == PIV_ALGORITHM_RSA_3072 + || mechanism == PIV_ALGORITHM_RSA_4096) { memmove (outdata, outdata + (s - outdata), n); outdatalen = n; @@ -2608,6 +2629,9 @@ do_decipher (app_t app, ctrl_t ctrl, const char *keyidstr, case PIV_ALGORITHM_RSA_3072: framelen = 3072 / 8; break; + case PIV_ALGORITHM_RSA_4096: + framelen = 4096 / 8; + break; default: err = gpg_error (GPG_ERR_INTERNAL); log_debug ("piv: unknown PIV mechanism %d while decrypting\n", mechanism); @@ -2625,7 +2649,8 @@ do_decipher (app_t app, ctrl_t ctrl, const char *keyidstr, if (indatalen > framelen) { if ((mechanism == PIV_ALGORITHM_RSA_2048 - || mechanism == PIV_ALGORITHM_RSA_3072) + || mechanism == PIV_ALGORITHM_RSA_3072 + || mechanism == PIV_ALGORITHM_RSA_4096) && indatalen == framelen + 1 && !*indata) { indata_buffer = xtrycalloc (1, framelen); @@ -2669,7 +2694,8 @@ do_decipher (app_t app, ctrl_t ctrl, const char *keyidstr, (int)0x7c, (size_t)0, NULL, /* Constructed. */ (int)0x82, (size_t)0, "", (mechanism == PIV_ALGORITHM_RSA_2048 - || mechanism == PIV_ALGORITHM_RSA_3072)? + || mechanism == PIV_ALGORITHM_RSA_3072 + || mechanism == PIV_ALGORITHM_RSA_4096)? (int)0x81 : (int)0x85, (size_t)indatalen, indata, (int)0, (size_t)0, NULL); if (err) @@ -2878,7 +2904,9 @@ writekey_rsa (app_t app, data_object_t dobj, int keyref, if (err) goto leave; - if (rsa_n_len > 3000/8) + if (rsa_n_len > 3072/8) + mechanism = PIV_ALGORITHM_RSA_4096; + else if (rsa_n_len > 3000/8) mechanism = PIV_ALGORITHM_RSA_3072; else mechanism = PIV_ALGORITHM_RSA_2048; @@ -3290,7 +3318,7 @@ genkey_parse_ecc (const unsigned char *data, size_t datalen, int mechanism, /* Create a new keypair for KEYREF. If KEYTYPE is NULL a default * keytype is selected, else it may be one of the strings: - * "rsa2048", "rsa3072", "nistp256, or "nistp384". + * "rsa2048", "rsa3072", "rsa4096", "nistp256, or "nistp384". * * Supported FLAGS are: * APP_GENKEY_FLAG_FORCE Overwrite existing key. @@ -3330,6 +3358,8 @@ do_genkey (app_t app, ctrl_t ctrl, const char *keyrefstr, const char *keytype, mechanism = PIV_ALGORITHM_RSA_2048; else if (!strcmp (keytype, "rsa3072")) mechanism = PIV_ALGORITHM_RSA_3072; + else if (!strcmp (keytype, "rsa4096")) + mechanism = PIV_ALGORITHM_RSA_4096; else if (!strcmp (keytype, "nistp256")) mechanism = PIV_ALGORITHM_ECC_P256; else if (!strcmp (keytype, "nistp384")) -- 2.56.0 From soap at gentoo.org Wed Nov 11 12:39:17 2026 From: soap at gentoo.org (David Seifert) Date: Wed, 11 Nov 2026 12:39:17 +0100 Subject: [PATCH GnuPG] scd:piv: Support the administration key of the Nitrokey 3 Message-ID: <20261111113917.97698-1-soap@gentoo.org> * 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 --- 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 From johnpace95 at mail.de Wed Nov 11 23:20:49 2026 From: johnpace95 at mail.de (John Pace) Date: Wed, 11 Nov 2026 23:20:49 +0100 Subject: [NSIS Installer] Incomplete cleanup during upgrade leaves processes running and old files behind Message-ID: <9b052a1d-1883-470e-b796-d50fb4e51544@mail.de> Hi, here is a bugreport for you. Also available here: https://bugs.kde.org/show_bug.cgi?id=528718 Original bug: https://bugs.kde.org/show_bug.cgi?id=523721 Description The problem occurs when updating an existing Gpg4win installation. During the update process, the existing version is uninstalled before the new version is installed. However, some processes from the previous installation may still be running after the uninstallation. In my case, the following processes remained running: kleopatra.exe dirmngr.exe gpg-agent.exe keyboxd.exe The uninstaller reports that Gpg4win has been successfully removed, even though these processes are still running. As a consequence, files from the previous installation may remain in place and potentially mix with files from the new version. This can result in an inconsistent installation and may cause the issue described in Bug 523721. Expected behavior During an upgrade, the installer should ensure that processes belonging to the previous installation are no longer running before proceeding with the installation of the new version. If processes cannot be terminated or files cannot be removed, the user should be informed accordingly, rather than being presented with an apparently successful uninstallation. Actual behavior The update proceeds even though processes from the previous installation are still running. This can leave files from the old version behind, potentially resulting in a mixture of files from different versions. Workaround Before starting the update, the following batch script can be used to terminate the relevant processes manually: taskkill /F /IM kleopatra.exe taskkill /F /IM dirmngr.exe taskkill /F /IM gpg-agent.exe taskkill /F /IM keyboxd.exe After terminating these processes, the update can proceed without this particular problem. From gniibe at fsij.org Thu Nov 12 01:34:35 2026 From: gniibe at fsij.org (NIIBE Yutaka) Date: Thu, 12 Nov 2026 09:34:35 +0900 Subject: [PATCH GnuPG 1/2] scd:piv: Support AES administration keys In-Reply-To: <20261110124018.140795-1-soap@gentoo.org> References: <20261110124018.140795-1-soap@gentoo.org> Message-ID: <87pkwaubtw.fsf@haruna.fsij.org> Hello, Thank you for your contributions. David Seifert wrote: > * scd/app-piv.c (adm_key_algos): New. > (auth_adm_key): Support AES-128, AES-192 and AES-256 keys. Try > AES-192 before 3DES for 24 byte keys. > (set_adm_key_algo): New. Code taken from set_adm_key. Return the > status word instead of logging it. > (set_adm_key): Support AES keys. Set 24 byte keys as AES-192 key and > fall back to 3DES. Log only the last failure. [...] > * scd/app-piv.c (PIV_ALGORITHM_RSA_4096): New. > (get_key_algorithm_by_dobj): Map RSA keys larger than 3072 bit to > PIV_ALGORITHM_RSA_4096. > (do_sign, do_decipher): Support PIV_ALGORITHM_RSA_4096. > (get_keygrip_by_tag, do_readkey): Handle 3072 and 4096 bit RSA keys > stored without a certificate. > (writekey_rsa): Use PIV_ALGORITHM_RSA_4096 for keys larger than 3072 > bit. > (do_genkey): Support "rsa4096". Applied both to master and pushed. -- From gniibe at fsij.org Thu Nov 12 07:11:22 2026 From: gniibe at fsij.org (NIIBE Yutaka) Date: Thu, 12 Nov 2026 15:11:22 +0900 Subject: [PATCH] agent: Use CONFIRM --focus=ok for SSH, card and other confirmation. Message-ID: <7f65dbac1d737eacc7e6fef5e689442413c3d9a2.1794463825.git.gniibe@fsij.org> * agent/agent.h (CONFIRM_FLAG_WITHCANCEL, CONFIRM_FLAG_FOCUSOK): New. (agent_get_confirmation): Change the last argument to FLAGS. * agent/call-pinentry.c (agent_get_confirmation): Handle FLAGS. * agent/command-ssh.c (data_sign): Use CONFIRM_FLAG_FOCUSOK. * agent/findkey.c (prompt_for_card): Likewise. (agent_key_from_file): Likewise. * agent/trustlist.c (agent_marktrusted): Use CONFIRM_FLAG_WITHCANCEL. -- It can work well only when the pinentry supports focus on the "ok" button (curses and GTK for pinentry 1.3.3). GnuPG-bug-id: 2265, 8182 Signed-off-by: NIIBE Yutaka --- agent/agent.h | 5 ++++- agent/call-pinentry.c | 19 ++++++++++++------- agent/command-ssh.c | 3 ++- agent/findkey.c | 6 ++++-- agent/trustlist.c | 6 ++++-- 5 files changed, 26 insertions(+), 13 deletions(-) -------------- next part -------------- A non-text attachment was scrubbed... Name: 0001-agent-Use-CONFIRM-focus-ok-for-SSH-card-and-other-co.patch Type: text/x-patch Size: 5942 bytes Desc: not available URL: From soap at gentoo.org Thu Nov 12 10:22:05 2026 From: soap at gentoo.org (David Seifert) Date: Thu, 12 Nov 2026 10:22:05 +0100 Subject: [PATCH GnuPG 1/2] scd:piv: Keep the public key if a key import fails Message-ID: <20261112092206.14919-1-soap@gentoo.org> * scd/app-piv.c (do_writekey): Do not clear the cert DO before importing the key. -- do_writekey cleared the cert DO before handing the key to writekey_rsa or writekey_ecc. If the import then failed, the card still had the old private key, but scdaemon had lost its public key and READKEY returned "No public key" for the slot. For a key generated on the card there is no copy to import again. The import can fail in scdaemon, for example for an Ed25519 key, which is rejected with "Unknown elliptic curve" before any APDU is sent. It can also fail on the card, for example for a 4096 bit RSA key on a YubiKey before firmware 5.7. Clearing is not needed: writekey_rsa and writekey_ecc overwrite the cert DO with the new public key after the card accepted the key. do_genkey does the same. Tested with --force imports into a slot holding an RSA-2048 key: Card Slot Failed import Slot afterwards ---------------------- ---- --------------------- --------------- YubiKey 5C NFC (5.7.1) 9E Ed25519 (scdaemon) key kept YubiKey 5 NFC (5.1.2) 95 Ed25519 (scdaemon) key kept RSA-4096 (card, 6A80) key kept, signs YubiKey 4 (4.3.7) 95 Ed25519 (scdaemon) key kept RSA-4096 (card, 6A80) key kept Without this patch READKEY returned "No public key" after each of these failed imports. On the YubiKey 5C NFC, importing RSA-2048, RSA-4096 and P-256 keys with --force still replaces the key, and READKEY returns the keygrip of the new key. Signed-off-by: David Seifert --- scd/app-piv.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/scd/app-piv.c b/scd/app-piv.c index baa9b187a..f26203e67 100644 --- a/scd/app-piv.c +++ b/scd/app-piv.c @@ -3186,20 +3186,11 @@ do_writekey (app_t app, ctrl_t ctrl, if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) goto leave; - /* First clear an existing key. We do this by writing an empty 7f49 - * tag. This will return GPG_ERR_NO_PUBKEY on a later read. */ + /* Divert to the algo specific implementation. The cert DO is not + * cleared beforehand: the implementations overwrite it only after + * the card accepted the key, so a failed import keeps the public + * key of the existing key. */ flush_cached_data (app, dobj->tag); - err = put_data (app_get_slot (app), dobj->tag, - (int)0x7f49, (size_t)0, "", - (int)0, (size_t)0, NULL); - if (err) - { - log_error ("piv: failed to clear the cert DO %s: %s\n", - dobj->keyref, gpg_strerror (err)); - goto leave; - } - - /* Divert to the algo specific implementation. */ if (tok && toklen == 3 && memcmp ("rsa", tok, toklen) == 0) err = writekey_rsa (app, dobj, keyref, buf, buflen, depth); else if (tok && toklen == 3 && memcmp ("ecc", tok, toklen) == 0) -- 2.56.0 From soap at gentoo.org Thu Nov 12 10:22:06 2026 From: soap at gentoo.org (David Seifert) Date: Thu, 12 Nov 2026 10:22:06 +0100 Subject: [PATCH GnuPG 2/2] scd:piv: Allow key import on all cards In-Reply-To: <20261112092206.14919-1-soap@gentoo.org> References: <20261112092206.14919-1-soap@gentoo.org> Message-ID: <20261112092206.14919-2-soap@gentoo.org> * scd/app-piv.c (do_writekey): Do not restrict to YubiKeys. -- Key import uses the Yubico import command (INS 0xFE), which is not part of the PIV specification. Other vendors implement it too, for example Token2, so do not check for a YubiKey before sending it. A card without the command rejects it, and since the previous commit the slot then keeps its existing key and public key. On such cards the error changes from "Not supported" to the error for the status word returned by the card. Tested on a TOKEN2 PIN+ R3.3 card in a Cherry ST-2xxx reader (T=0), slot 9E, and on a Nitrokey 3, slot 95, which holds a P-256 key generated on the card: Card Import Result ---------- --------------------- ---------------------------- TOKEN2 RSA-2048, RSA-4096 imported; RSA-4096 key signs TOKEN2 Ed25519 (scdaemon) rejected; key kept Nitrokey 3 RSA-2048 (card, 6A81) "Not supported"; key kept "Key kept" means READKEY still returns the keygrip of the slot's key. The Nitrokey 3 does not implement the import command. Signed-off-by: David Seifert --- scd/app-piv.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/scd/app-piv.c b/scd/app-piv.c index f26203e67..3961cd860 100644 --- a/scd/app-piv.c +++ b/scd/app-piv.c @@ -3120,11 +3120,12 @@ writekey_ecc (app_t app, data_object_t dobj, int keyref, } -/* Write a key to a slot. This command requires proprietary - * extensions of the PIV specification and is thus only implemented for - * supported card types. The input is a canonical encoded - * S-expression with the secret key in KEYDATA and its length (for - * assertion) in KEYDATALEN. KEYREFSTR needs to be the usual 2 +/* Write a key to a slot. This command uses the import command of + * Yubico, which is not part of the PIV specification. It is sent to + * all cards because other vendors, for example Token2, implement it + * too; cards without it reject the command. The input is a canonical + * encoded S-expression with the secret key in KEYDATA and its length + * (for assertion) in KEYDATALEN. KEYREFSTR needs to be the usual 2 * hexdigit slot number prefixed with "PIV." * * Supported FLAGS are: @@ -3145,12 +3146,6 @@ do_writekey (app_t app, ctrl_t ctrl, (void)ctrl; - if (!app->app_local->flags.yubikey) - { - err = gpg_error (GPG_ERR_NOT_SUPPORTED); - goto leave; - } - /* Check keyref and test whether a key already exists. */ dobj = find_dobj_by_keyref (app, keyrefstr); if ((keyref = keyref_from_dobj (dobj)) == -1) -- 2.56.0 From kloecker at kde.org Thu Nov 12 16:29:01 2026 From: kloecker at kde.org (Ingo =?UTF-8?B?S2zDtmNrZXI=?=) Date: Thu, 12 Nov 2026 16:29:01 +0100 Subject: [PATCH GnuPG 1/2] scd:piv: Keep the public key if a key import fails In-Reply-To: <20261112092206.14919-1-soap@gentoo.org> References: <20261112092206.14919-1-soap@gentoo.org> Message-ID: <25_cwnX2RfK7qUiIjuDBMg@kde.org> On Donnerstag, 12. November 2026 10:22:05 Mitteleuropäische Normalzeit David Seifert via Gnupg-devel wrote: > Clearing is not needed: writekey_rsa and writekey_ecc overwrite the > cert DO with the new public key after the card accepted the key. > do_genkey does the same. What happens when overwriting the cert DO fails? Do you then have a public key on the card which doesn't match the private key on the card? Isn't that worse than losing the public key on a failed import. You should have made a backup of the public key anyway. (How to get the backup of the public key back on the card is another story.) Regards Ingo -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 265 bytes Desc: This is a digitally signed message part. URL: