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

softhsm2-util AES key import fails whenever a newline character is present in file #746

Open
ijsf opened this issue Apr 30, 2024 · 2 comments

Comments

@ijsf
Copy link
Contributor

ijsf commented Apr 30, 2024

I have just ran into an obscure problem where softhsm2-util --import --aes results in a (seemingly) succesful import of an AES key into SoftHSM v2, but actually has an incorrect AES key length. This results in a corrupted AES key that cannot be used.

The test case was a 32-byte (AES-256) secret key while the import listed by PKCS#11 (pkcs11-tool) resulted in an AES key with a length less than 32 bytes.

For example, the following key will result in an incorrect AES key length of 25 (notice the 0A newline character):

echo 2BC9931E0E50B4F6995CF194321D7DDDB91FA104925B6F8A0AFBAEF0207BB612 | xxd -r -p - > p.key

While looking at the relevant source code, I noticed that fgets and strlen are used to read out the binary key file:

This is incorrect behaviour as the C function fgets is intended for text strings, not binary strings, and stops whenever a newline character is encountered (even though the file is opened with rb attributes) leading to a silent corruption of the key. fread should probably be used instead to properly read the key in all conditions.

Furthermore, strlen is used in a similar manner to determine the AES key length, as if it were a text string while it is actually a binary byte array. As far as I know and have tested, the CKA_VALUE expects a byte array.

@ijsf
Copy link
Contributor Author

ijsf commented Apr 30, 2024

I've also created a PR to fix this issue: #747

@AlexanderBand
Copy link

Please note this statement on the status of SoftHSM v2 development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants