Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nitrokey HSM: timeout on initialization after heavy use #78

Open
szszszsz opened this issue Jan 16, 2021 · 0 comments
Open

Nitrokey HSM: timeout on initialization after heavy use #78

szszszsz opened this issue Jan 16, 2021 · 0 comments

Comments

@szszszsz
Copy link
Member

szszszsz commented Jan 16, 2021

Summary

Initialization command for the Nitrokey HSM can time out after it was heavily populated.

Possible Cause

The smart card is engaged in a cleanup operations (like garbage collection), and cannot reply immediately. The timeout extension commands are not sent to the host due to waiting for the smart card reply in a busy loop, which results in a timeout error shown by the OpenSC to the user.

Workaround

While the timeout error is shown to the user, this does not necessarily means the operation failed. The smart card continues execution of the initialization command and should finish after around 30 seconds of the processing (usually 10 seconds later after the timeout error is reported).
The workaround is to ignore the error and wait until the LED on the Nitrokey HSM will stop flashing, which will signalize the reception of the smart card's reply, or add a hard delay in case of the batch execution, and restart the pcscd (OpenSC smart card service) after doing so.

Possible solution

Send timeout extension packets on the bulk endpoint (during the busy wait loop in USART_ByteReceive()) while the smart card is busy and is not doing it by itself. Perhaps by reusing CCID_CheckUsbCommunication().

static ErrorStatus USART_ByteReceive (u8 * Data, u32 TimeOut)
{
u32 Counter = 0;
while ((USART_GetFlagStatus (USART1, USART_FLAG_RXNE) == RESET) && (Counter != TimeOut))
{
Counter++;
}
if (Counter != TimeOut)
{
*Data = (u8) USART_ReceiveData (USART1);
return SUCCESS;
}
else
{
return ERROR;
}
}

See below for CCID protocol details:

Used library:

Details

Nitrokey HSM firmware: hsm-2
HSM smart card firmware: v3.4
Frequency: tester1: 1/6, tester2: 1/3

Scenario

  1. Fill HSM by generating RSA4096 keys until the device is full, e.g. with:
    pkcs11-tool -l --pin 648219 --keypairgen --key-type rsa:4096 --id $i
  2. Reset the device with: sc-hsm-tool --initialize --so-pin 3537363231383830 --pin 648219

Execution takes about 45-60 minutes.
Populating with binary data (like certificates) might be faster, but the occurrence frequency seems to drop. With generating ECC keys the issue was not reproduced.

Populating script
#!/bin/bash
set -xeo

for i in {1..1000}
do
   env OPENSC_DEBUG=2 pkcs11-tool -l --pin 648219 --keypairgen --key-type rsa:4096 --id $i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant