Skip to content

IMPORT ASYMMETRIC KEY (into PIV) leaves the imported key in volatile storage; FIDO2 PIN token requests then fail until power cycle #59

Open

Description

@mirko

Disclaimer:

This issue was initially triggered while experimenting with RSA keys > 2048, as the firmware supports up to RSA-4096 since recently, but the tooling does not yet (at least nitropy). In order to import RSA-4096 keys via nitropy, nitropy was locally modified.
However, the issue can also be triggered with RSA-2048 keys and unmodified nitropy. That needs more import rounds (~7 to provide a number), though - and I'm honestly not yet sure why one RSA-4096 is enough but 6 RSA-2048 are not to trigger the issue.
The issue in question was discovered by a real human (me!) in the real world.
I then tried to narrow it down with the help of LLMs and used Claude Opus to map the behaviour in question to code and trying to confirm it's an /actual/ bug. I passed it through several models and they all agree on my understanding of the situation:
Looking at the code and the resources being used, it's most likely not corruption, but rather resource exhaustion.
Hence, I don't want to be the person flagging this as security related, however what appears concerning to me is the fact that importing a key into the PIV applet can cause the fido2-applet to fail.. While tinkering, narrowing things down and debugging I also seemingly managed to get the NitroKey into a hung up state, which I suppose is also nothing which should just happen.

Following report is generated with help of AI/LLMs:

Summary

import_asymmetric_key injects the key with unsafe_inject_key into
Location::Volatile, seals it to flash, and returns without ever calling
clear or delete on the injected key id. generate_asymmetric_keypair
had the same omission; it was fixed in e168886 ("Fix memory leak",
2024-08-26, later changed to clear in 563fcaf). The import path, added
earlier in e6f6a63 (2024-06-12), never received the equivalent change.

On a Nitrokey 3 this is observable from the outside: after a number of
imports (7 × RSA-2048, or 1 × RSA-4096 on my device), every FIDO2 operation
that needs a PIN token fails with CTAP2_ERR_INVALID_PARAMETER until the
device is power-cycled. The FIDO application does not use the SE050; what
the two applications share is the 8 KB volatile littlefs store, where each
import leaves one key record behind.

Environment

  • Device: Nitrokey 3, nRF52 variant (lsusb: 20a0:42b2 Nitrokey 3A Mini/3A NFC/3C NFC), with SE050
  • Firmware: v1.9.1, with piv-authenticator 0.7.0, fido-authenticator 0.3.0,
    trussed 0.2.0, trussed-se050-backend 0.8.0
    (Cargo.lock)
  • Host: Debian forky; libfido2 / fido2-tools 1.17.0; pcsc-lite 2.5.2;
    pynitrokey 0.14.1, unmodified, for the steps below (it imports RSA-2048
    only; the RSA-4096 observations used a copy patched to send P1=0x16 with
    256-byte primes)
  • PIV state: default PIN and management key

Steps to reproduce

None of these steps requires a touch. A FIDO2 PIN must be set.

  1. Power-cycle the device (unplug and replug).
  2. Check that a FIDO2 PIN token can be obtained. Credential management needs
    a PIN token and no user presence:
    fido2-token -L -r /dev/hidrawN      # enter the FIDO2 PIN; succeeds
    
  3. Create an RSA-2048 key and certificate:
    openssl req -new -x509 -newkey rsa:2048 -nodes -keyout k.pem -out c.pem -days 1 -subj /CN=test
    openssl pkcs12 -export -inkey k.pem -in c.pem -passout pass: -out k.p12
    
  4. Import it into 9A, then repeat step 2. Repeat until step 2 fails:
    nitropy nk3 piv --experimental import-key --key 9a --path k.p12
    fido2-token -L -r /dev/hidrawN
    
    Observed (every import returned success):
    import #1 -> fido2-token -L -r: ok
    ...
    import #6 -> ok
    import #7 -> fido2-token: fido_credman_get_dev_rp: FIDO_ERR_INVALID_PARAMETER
    
  5. Power-cycle the device and repeat step 2. It succeeds again, and the
    imported key is still present in 9A.

Expected behaviour

FIDO2 keeps working after PIV key imports.

Actual behaviour

After the imports, every FIDO2 operation that needs a PIN token fails with
CTAP2_ERR_INVALID_PARAMETER (0x02) until power-off: fido2-cred -M,
fido2-assert -G and fido2-token -L -r. The libfido2 debug trace shows
the rejected request:

06 a6 01 02 02 09 03 a5 ... 06 58 20 ... 09 01 0a 6a "luks.local"
   clientPIN: pinUvAuthProtocol=2, subCommand=0x09
   (getPinUvAuthTokenUsingPinWithPermissions), keyAgreement,
   pinHashEnc(32), permissions=0x01 (mc), rpId
-> 02

The PIN retry counter stays at 8 throughout, so the PIN itself is not being
rejected.

Observations

All on the device above. Each block starts from a power cycle. "FIDO2
harness" creates a credential and runs hmac-secret assertions with PIN and
touch; "check" is the touch-free fido2-token -L -r from step 2.

Sequence FIDO2 afterwards
power cycle → FIDO2 harness works
PIV PIN verify + GENERAL AUTHENTICATE (RSA-4096 key in 9A) → FIDO2 harness works
IMPORT RSA-4096 into 9A (once) → FIDO2 harness fails
power cycle → check → IMPORT RSA-2048 into 9A, 1st…6th → check after each works
7th IMPORT RSA-2048 → check fails
power cycle → check → IMPORT RSA-4096 into 9A (once) → check fails

So using an imported key does not trigger the failure; importing does,
and the effect accumulates. In the first occurrence, one makeCredential
still succeeded and the following getAssertion failed, consistent with a
nearly exhausted store rather than a hard state flag.

Additionally, after the last row the device was left idle in that state
with nothing sent to it. About 25 minutes later it re-enumerated as
20a0:42e8 Nitrokey 3A Mini Bootloader and stayed there until replugged.
Given that the firmware's panic handler loops forever and the nRF52
watchdog is set to 15 minutes, this looks like a panic followed by a
watchdog reset, but I did not capture what panicked. Observed once; I cannot
say whether it is a consequence of the leak.

Analysis

piv-authenticator v0.7.0 (unchanged on main as of f907ae2)

  • import_asymmetric_key calls unsafe_inject_key(..., AsymmetricKeyReference::PivAuthentication.storage(...)),
    which is Location::Volatile for every slot except 9E
    (src/lib.rs#L1079-L1095,
    src/container.rs#L271-L276).
  • replace_asymmetric_key → set_asymetric_for_reference seals that key to
    flash with hpke_seal_key_to_file
    (src/state.rs#L197-L205),
    clears the temporary user public key, and deletes the previous key only
    if it was unencrypted (9E). The injected key id is never cleared or deleted.
  • generate_asymmetric_keypair does clean up for encrypted slots
    (src/lib.rs#L950-L953):
    syscall!(self.trussed.delete(public_key));
    if reference.is_encrypted() {
        syscall!(self.trussed.clear(secret_key));
    }

What is actually leaked on a Nitrokey 3 (firmware v1.9.1)

  • The PIV app's backends are [Se050, Staging, Core]; the FIDO app's are
    [Staging, Core] (components/apps/src/lib.rs). RSA import is therefore
    handled by trussed-se050-backend 0.8.0, unsafe_inject_volatile_rsa:
    it writes the RSA key and a 16-byte AES intermediary key into the SE050
    (persistent objects, policy-bound to each other) and stores a small
    VolatileRsaKey { key_id, intermediary_key } record in the volatile
    littlefs store via the client keystore.
  • For such a key, the backend's clear deletes only that record and leaves
    the SE050 objects in place (comment in source: "can be imported again if
    unwrapped" — the sealed copy re-references them). So clear after
    sealing is safe, and it is what the generation path already does.
  • The volatile store is 8 KB: 32 blocks of 256 bytes
    (components/boards/src/store.rs, const_ram_storage!(name = VolatileStorage, ...)),
    shared by all trussed clients, separated only by a client-id path prefix
    (trussed 0.2.0 src/store/keystore.rs). Key files are named by a 32-hex
    key id, so with 256-byte metadata blocks only a few entries fit per
    directory metadata pair before it splits.
  • On the FIDO side, PinProtocol::shared_secret in fido-authenticator 0.3.0
    (src/ctap2/pin.rs) returns Error::InvalidParameter when any of its
    volatile key allocations (peer key, ECDH result, KDF outputs) fails. That
    is the error observed, and shared_secret is the first volatile allocation
    in the getPinUvAuthTokenUsingPinWithPermissions handler.
  • On builds without the SE050 backend (software RSA via trussed-rsa-alloc),
    the same code path leaves the full PKCS#8 private key (1216 B for
    RSA-2048, 2374 B for RSA-4096) in the volatile store instead of a small
    record. I have not tested such a build.

Suggested fix

Clear the injected key after sealing, as generation does. In
import_asymmetric_key, after replace_asymmetric_key(...):

if key.is_encrypted() {
    syscall!(self.trussed.clear(id));
}

Related points:

  • The import path uses PivAuthentication.storage(...) regardless of the
    target slot. Harmless while import is limited to 9A; wrong for 9E if the
    slot restriction is lifted (see Support ECC key import in all asymmetric slots #56).
  • fido-authenticator reports an allocation failure as INVALID_PARAMETER,
    which points away from the real cause and makes this class of problem
    hard to diagnose from the host side.

Open questions

  • One RSA-4096 import exhausts what six RSA-2048 imports do not, although
    the record left in the volatile store should be the same size for both.
    I have not found what else the RSA-4096 path consumes. You will know
    better whether something in the SE050 import path or in littlefs explains
    it.
  • Whether the full device hang after ~35 minutes idle in the exhausted
    state is related. Observed once.
  • Whether a PIV factory reset (which runs delete_all on all locations)
    frees the store without a power cycle. Not yet tested.

Activity

  1. sosthene-nitrokey commented on Nov 6, 2026

    @sosthene-nitrokey
    Contributor

    I can confirm this issue. Thank you for the report, this will be fixed in the next release, we are planning to improve testing for all this by having automated testing for any memory leak in volatile memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions