NK3 frequent hangs / lost connection

Hey!
I’m currently working on an OpenPGP implementation for SmartCards using javax.smartcardio and I’m testing against both a Yubikey 4, as well as a Nitrokey 3 A Mini (firmware v1.9.1)

On the Nitrokey I’m experiencing frequent freezes of the device, especially when trying to PUT_DATA_B (e.g. during key upload). The NK3 just never responds and I need to unplug it.

Factory-resetting using nitropy nk3 factory-reset also fails:

Critical error:
An unhandled exception occurred
Exception encountered: ConnectionError(‘Lost the connection to the device while executing a command’)

Here is an APDU dump of a test:

==> APDU(00a40400 d27600012401) // Select OpenPGP
<== OKAY: 9000
==> APDU(00ca006e) // Get Application Related Data
<== OKAY: 4f10d276000124010304000f2654270200005f520a0031f573c001600590007f660802021db902021db97f74038101207381cfc00a3f001000100010000001c10a132a8648ce3d030107ffc206010800002000c306010800002000c407007f7f7f030003c53c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c63c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd0c000000000000000000000000de06010002000300d6020020d7020020d80200209000
==> APDU(00ca00fa) // Get Algorithm Attributes
<== OKAY: c10b162b06010401da470f01ffc10a132a8648ce3d030107ffc106010800002000c106010c00002000c106011000002000c107132b81040022ffc107132b81040023ffc10b132b2403030208010107ffc10b132b240303020801010bffc10b132b240303020801010dffc107132b8104000affc20c122b060104019755010501ffc20a122a8648ce3d030107ffc206010800002000c206010c00002000c206011000002000c207122b81040022ffc207122b81040023ffc20b122b2403030208010107ffc20b122b240303020801010bffc20b122b240303020801010dffc207122b8104000affc30b162b06010401da470f01ffc30a132a8648ce3d030107ffc306010800002000c306010c00002000c306011000002000c307132b81040022ffc307132b81040023ffc30b132b2403030208010107ffc30b132b240303020801010bffc30b132b240303020801010dffc307132b8104000aff9000
==> APDU(00200083 3132333435363738) // Verify Admin PIN
<== OKAY: 9000
==> APDU(00da00c1 132a8648ce3d030107ff) // Set CardholderPrivateKeyTemplate
<== OKAY: 9000
==> APDU(00db3fff 4d6db6007f4804922099415f486154b42f11c31d1e1a0c9c5c0fe539a8f273ada2c2fd3bb9e8465e1ed67b726f3e04f847572a15ff28eac75076a16b202f9bd9321f2d0373569f5525c9a76257dba1d643d3874d0f7f3425789e48609e7b54fb2bf8cfd26716b9de9cb24c39410afc) // Upload key
<<<No Reponse>>>

Any ideas? Is my NK3 broken?

What is the status of the LED when the device freezes? Does the factory reset not work at all or specifically after the freeze? Can you try to run nitropy nk3 test and share the output?

The LED turns blinks while I can still see APDUs being transferred, but turns off once the device freezes.

Factory reset works if I perform a manual reset using APDU sequences

==> APDU(00200081 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c2): 63c2
==> APDU(00200081 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c1): 63c1
==> APDU(00200081 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c0): 63c0
==> APDU(00200081 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c0): 63c0
==> APDU(00200083 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c2): 63c2
==> APDU(00200083 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c1): 63c1
==> APDU(00200083 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c0): 63c0
==> APDU(00200083 4040404040404040404040404040404040404040404040404040404040404040)
<== UNKNOWN(63c0): 63c0
==> APDU(00e60000)
<== OKAY: 9000
==> APDU(00440000)
<== OKAY: 9000
==> APDU(00a40400 d27600012401)
<== OKAY: 9000

Nitropy test seems to be okay:

~ nitropy nk3 test
Command line tool to interact with Nitrokey devices 0.14.0
Found 1 Nitrokey 3 device(s):
- Nitrokey 3 at /dev/hidraw8

Running tests for Nitrokey 3 at /dev/hidraw8

[1/5]	uuid     	UUID query              	SUCCESS  	26542702BD53A0820000000000000000
[2/5]	version  	Firmware version query  	SUCCESS  	v1.9.1
[3/5]	status   	Device status           	SUCCESS  	Status(init_status=<InitStatus: 0>, ifs_blocks=238, efs_blocks=462, variant=<Variant.NRF52: 2>, model=<Model.NK3: 'Nitrokey 3'>, revision=1)
Running SE050 test: |                                                                                                                                                                                              
[4/5]	se050    	SE050                   	SUCCESS  	SE050 firmware version: 3.1.1 - 1.11, (persistent: (32767,), transient_deselect: (607,), transient_reset: (592,))
Please press the touch button on the device ...
Please press the touch button on the device ...
[5/5]	fido2    	FIDO2                   	SUCCESS  	

5 tests, 5 successful, 0 skipped, 0 failed

Summary: 1 device(s) tested, 1 successful, 0 failed

Thanks for the information. What happens if you run nitropy nk3 status after the device freezes (without unplugging)?

Does the factory reset using APDU work directly after the freeze or only after replugging? Does the factory reset using nitropy work after replugging the device?

I just ran a test. The NK3 froze when I sent APDU(00da00c1 010800002000) (set signing key template to RSA2048).

With my test still running, waiting for the device to respond, executing nitropy nk3 status resulted in nitropy also hanging. Only once I unplugged the key, I got

Critical error:
An unhandled exception occurred
	Exception encountered: ConnectionError('Lost the connection to the device while executing a command')

Once the device freezes, I cannot send it anymore commands, as cardTerminal.connect("*") doesn’t return. Only unplugging it brings it back into a usable state.

Note: It appears that uploading key material becomes more reliable, if I manually factory-reset the device before every test. I have a test where I loop over all algorithms advertised by the nk, create a keypair using KeyFactory and upload it. Tests succeed if I .reset() before each key upload.

Maybe this is a locking issue? Though I don’t use any locks (e.g. `Card.beginExclusive()`) in my tests.

Yes, it could be related to concurrent access to the card. On the pcscd level, you can have exclusive or shared connections. For shared connections, you can use transactions to obtain exclusive access. At first glance, it looks like the javax.smartcardio API only supports shared transactions. Can you please check if using an exclusive transaction for your complete program changes the behavior (or even an exclusive connection, if you find a way to do that)?

With `card.beginExclusive()`, the nk3 also hangs at APDU(00da00c1 010800002000).

I threw together a minimal reproducer with just javax.smartcardio:

package org.pgpainless

import javax.smartcardio.CardException
import javax.smartcardio.CommandAPDU
import javax.smartcardio.ResponseAPDU
import javax.smartcardio.TerminalFactory

fun main() {
    val terminalFactory: TerminalFactory = TerminalFactory.getInstance("PC/SC", null)
    val terminal = terminalFactory.terminals().list().firstOrNull { it.isCardPresent } ?: throw CardException("No cards present")

    println("connect to ${terminal.name}")
    val card = terminal.connect("*")
    println("begin exclusive")
    card.beginExclusive()
    val basicChannel = card.basicChannel

    println("SELECT OPENPGP")
    val selectOpenPGP = basicChannel.transmit(CommandAPDU(0x00, 0xa4, 0x04, 0x00,
        "d27600012401".hex()))
    assertOkay(selectOpenPGP)

    println("GET APP_RELATED_DATA")
    val appRelData = basicChannel.transmit(CommandAPDU(0x00, 0xca, 0x00, 0x6e))
    assertOkay(appRelData)

    println("GET ALGORITHM_ATTRS")
    val algoAttrs = basicChannel.transmit(CommandAPDU(0x00, 0xca, 0x00, 0xfa))
    assertOkay(algoAttrs)

    println("VERIFY ADMIN_PIN")
    val verAdmPin = basicChannel.transmit(CommandAPDU(0x00, 0x20, 0x00, 0x83,
        "3132333435363738".hex()))
    assertOkay(verAdmPin)

    println("PUT_DATA CARDHOLDER_PRIVATE_KEY_TEMPLATE")
    val setKeyTempl = basicChannel.transmit(CommandAPDU(0x00, 0xda, 0x00, 0xc1,
        "132a8648ce3d030107ff".hex()))
    assertOkay(setKeyTempl)

    println("PUT_DATA_B EXTENDED_HEADER_LIST (hang here)")
    val uploadKey = basicChannel.transmit(CommandAPDU(0x00, 0xdb, 0x3f, 0xff,
        "4d6db6007f4804922099415f486154b42f11c31d1e1a0c9c5c0fe539a8f273ada2c2fd3bb9e8465e1ed67b726f3e04f847572a15ff28eac75076a16b202f9bd9321f2d0373569f5525c9a76257dba1d643d3874d0f7f3425789e48609e7b54fb2bf8cfd26716b9de9cb24c39410afc".hex()))
    assertOkay(uploadKey) // never reached

    println("end exclusive and disconnect")
    card.endExclusive()
    card.disconnect(true)
    println("Done")
}

fun assertOkay(response: ResponseAPDU) {
    require(response.sw == 0x9000) {
        "Response not okay"
    }
}

fun String.hex(): ByteArray {
    return hexToByteArray()
}

Maybe you can reproduce the issue on your end by running this test a few times?