1
0
mirror of https://github.com/opencryptoki/opencryptoki.git synced 2026-10-24 05:39:17 +03:00

724 Commits

Author SHA1 Message Date
Ingo Franzki
73b2f41d77 P11KMIP: Fix NULL-check, wrong dereference, and rc ordering
Certain error situations are not handled correctly. Fix them.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
e1b16f4111 P11KMIP: Fix memory leak in kmip_encode_json()
If adding an array element, the not-added element must be freed before
returning an error.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
d6d5194712 P11KMIP: Fix hex representation of values >= 2^52
When producing the hex representation of values >= 2^52 make sure that
the value is in big endian format, so that the hex representation is
correct.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
a59d96cf69 P11KMIP: Fix hostname verification with IPv6 addresses
IPv6 brackets must be removed for hostname verification, otherwise the
verification will fail.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
7d0c7f126f P11KMIP: Structures must be a multiple of the TTLV block length
Add a check to ensure that structures are a multiple of the TTLV block
length. If thats not the case return an error early, and do not only
rely on parsing the structure elements to fail.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
277e098409 P11KMIP: Fix attributes with wrong KMIP tag
Correct the KMIP tags used for date and application specific information
attributes.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
345eec8904 P11KMIP: Load the system's default CA certs if no CA is specified
if field 'tls_ca' is NULL in the connection config structure, the system's
default CA certificates are supposed to be loaded and used. This is
currently not done, leaving it with an empty certificate store and
any peer or hostname verification will fail.

Call SSL_CTX_set_default_verify_paths() to load the system's default CA
certificates.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
b71487d962 P11KMIP: On any I/O failure tear down the connection completely
Just resetting the OpenSSL BIO via BIO_reset() leaves the BIO in a state
where the next attempt to use it will reconnect to the server and perform
a TLS handshake freshly. However, this reconnect omits the checks done
by kmip_connection_tls_verify_server().

In case of any I/O failure tear down the connection completely to
avoid any reconnects.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
7758e6e595 P11KMIP: Fix NULL pointer dereference on corrupted KMIP response
When a corrupted KMIP response contains KMIP nodes that are expected to
be of type TEXT-STRING, but they are of a different type, then function
kmip_node_get_text_string() returns NULL. Fix all callers to check
for such NULL returns and return -EBADMSG if so.

Not checking for NULL might cause NULL pointer dereferences in the
callers, or the callers further down the stack.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
f58f72d548 PKCSSLOTD: Fix potentially unterminated string usage
Although the fiends in 'sinfo[slot_no]' in question are usually NUL-terminated
(do_str() pads the string with NUL values), there is one case where it is not
NUL-terminated: when the string length matches the target field length.

Use the 'str' variable, which is always NUL-terminated for the checks.
Variable 'str' and the struct field are identical in value, using 'str'
is semantically equivalent, but eliminates the unterminated string usage.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
b1776832b6 PKCSICSF: Fix uninitialized variable
When writing the initial NVTOK.dat file fails before the file name is
set, the attempt to unlink the file uses an uninitialized variable name.
Initialize fname with zeros, and attempt the unlink only if the file name
was set before.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
e93833f433 P11SAK: Fix assignment discards ‘const’ qualifier warning
Fix the following build warning:

usr/sbin/p11sak/p11sak.c:3979:8: warning: assignment discards ‘const’
        qualifier from pointer target type [-Wdiscarded-qualifiers]
 3979 |     ch = strchr(attrs, ':');
      |        ^

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-10-05 08:35:13 +02:00
Ingo Franzki
22d88c2ea3 PKCSICSF: Allow to specify the SO PIN and RACF password as option
For better scripting capabilities, the SO PIN and the RACF password can
optionally be supplied via options -R and -S. If not specified you are
prompted for them.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-23 13:25:13 +02:00
Ingo Franzki
7f5c86a549 PKCSICSF: Allow to specify the slot number to create with '-a <token>'
If -slot is specified then create the new ICSF token at the given slot number.
Otherwise the next free slot is used.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-23 13:25:13 +02:00
Ingo Franzki
0daecd52ed PKCSICSF: Store absolute paths in ICSF token config for SASL method
If relative file names are specified for the SASL key and certificates,
store the absolute path of them into the ICSF token config file.
This makes it location independent.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-23 13:25:13 +02:00
Ingo Franzki
063621047a PKCSICSF: Add support for changing the RACF password
Use 'pkcsicsf -p <token-name>' to change the RACF password of an existing
ICSF token.

The pkcsicsf tool updates the RACF file under the XProcLock, so the ICSF
token must read the RACF file under the XProcLock. This prevents concurrent
reads on a RACF file that is currently modified.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-23 13:25:13 +02:00
Ingo Franzki
bf40a5fe9f ICSF: Introduce a FIPS-compliant token data store format
Similar to the FIPS-compliant token data store format of the other tokens,
define a new token data store format for the ICSF token.

The new data format is used when the ICSF token has 'tokversion = 3.28'
in the slot definition in opencryptoki.conf. Existing ICSF tokens continue
to work unchanged. New ICSF tokens added via the pkcsicsf tool will always
have 'tokversion = 3.28' and thus use the new data format.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-23 13:25:13 +02:00
Ingo Franzki
90df45fe70 COMMON: Remove dependency on liblber in common code
The use of liblber has been eliminated for most of the cases already.
The only leftover cases are in RSA, DSA, and DH public key encoding
functions for encoding BIT STRINGs.

Update these leftover places and use the already existing
ber_encode_BIT_STRING() function instead.

With this, '-lber' can be removed from the make files for most of
the tokens and tools. The ICSF token still requires liblber for its
LDAP communication.

Also adjust the configure script and example RPM spec accordingly.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-07 11:11:06 +02:00
Ingo Franzki
e9c93d708f P11KIMIP: Fix NULL pointer dereference in kmip_v2_attr_from_v1_attr()
In kmip_get_attribute_v1(), KMIP_TAG_ATTRIBUTE_NAME is retrieved by tag,
and the function stores the result of kmip_node_get_text_string(nam)
without rejecting a non-text node. kmip_node_get_text_string() returns
NULL on a type mismatch, so a malformed v1.x 'Attribute Name' can leave
name == NULL while kmip_get_attribute_v1() still returns success.
kmip_v2_attr_from_v1_attr() then dereferences name through strncmp().

Reported by: Aisle Research
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-01 10:52:12 +02:00
Ingo Franzki
4bd4e4c9c1 P11SAK: Fix typos and copy&paste errors
Use correct NID for ffdhe8192 and fix a typo in help text.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-09-01 10:52:12 +02:00
Ingo Franzki
c87900ac8f CONFIGURE/MAKE: Fix configure script and make files
Fix several inconsistencies and errors in the configure script and make files.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
26c6ae629f PKCSTOK_ADMIN: Fix build error due to missing mechtable-gen.h
Include file mechtable-gen.h is generated, and thus needs statements
in the make file.

Fixes: 7174f72dc6

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
67200ebf42 PKCSSTATS: Fix typos in messages
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
0bdb7099fb PKCSICSF: Fix SASL login for the initial connection by the tool
The pkcsicsf tool should also use SASL logon for its initial connection.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
cdb49602e3 PKCSEP11_SESSION: Fix error message about PIN length
The PIN can be from 8 to 16 characters, the error message was misleading.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
7a847e186d PKCSEP11_MIGRATE: Allow hex and decimal for adapter and domain arguments
Allow that the adapter and domain values can be specified in decimal
(as before), but also in hex, like the man page states already.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
84342a9f97 PKCSCCA: Fix verbose option argument parsing
The verbose option requires an argument. Correct this for the long option
parsing.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
200c54f21d P11KMIP: Fix config file parsing and checking
Wrap key attributes can only come from the config file. Verify they were
set regardless of whether a config file was found.

Also remove the  unused define P11KMIP_CONFIG_KEYWORD_PORT. The port
number can be specified as part of the host setting.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
8db57cd33e P11SAK: Fix typo in error message
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-25 14:45:29 +02:00
Ingo Franzki
922ea4bf9e PKCSICSF: Make adding all ICSF tokens usable
Using pkcsicsf with '-a all' adds all available ICSF tokens. Mention this in
the man page.

For each added token, pkcsicsf prompts for the SO pin to set. Mention the
token name for which the SO pin is prompted.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:28:02 +02:00
Ingo Franzki
b66ca61651 PKCSICSF: Also create the token lock directory
Besides the token directory, also the corresponding lock directory must
be created. If this is not done, then the pkcsslotd might do so during
startup, but only if it is stared as root.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:28:01 +02:00
Ingo Franzki
655139c885 ICSF/PKCSICSF: Fix SO PIN handling for token creation and initialization
Currently the ICSF token is added using 'pkcsicsf -a' and the default
SO pin (87654321) must be used with it. Only then a subsequent setting
of the SO pin via 'pkcsconf -P' works (where the old pin is also the
default SO pin).

The reason for this is that 'pkcsicsf -a' does not create a NVTOK.dat,
and thus, when the token comes up  the very first time, the NVTOK.dat
is created, but with the SO default pin. This happens for example
during 'pkcsconf -P', before the new PIN is set.

Update pkcsicsf tool to also create an initial NVTOK.dat file which
contains the hash of the user supplied SO pin. Thus, any SO pin can
be used with 'pkcsicsf -a', and this already sets the SO pin.
The initial NVTOK.dat is a too short version, and will be written in
its full length on the very first use of the token.

With this change no subsequent 'pkcsconf -P' is required (but possible),
the desired SO pin is already set and the token is already initialized.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:28:01 +02:00
Ingo Franzki
144abab9d8 PKCSTOK_MIGRATE: Use file descriptor operations relative to the directory
Refactor the code to use file descriptor operations relative to the directory
(e.g. openat(), unlinkat(), fstatat(), renameat()) instead of path based
operations. This is safer against TOCTOU issues and symlink-following
attacks where the files or directories are replaced after the check, but
before the path based operation on it.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:59 +02:00
Ingo Franzki
134cf16c57 PKCSTOK_ADMIN: Use file descriptor operations relative to the directory
Refactor the code to use file descriptor operations relative to the directory
(e.g. openat(), unlinkat(), fstatat(), renameat()) instead of path based
operations. This is safer against TOCTOU issues and symlink-following
attacks where the files or directories are replaced after the check, but
before the path based operation on it.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:59 +02:00
Ingo Franzki
b7a6619456 P11KMIP: Fix memory leak on error path
Function p11kmip_unwrap_local_secret_key() may leak memory when an
unsupported wrapping method is used.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:57 +02:00
Ingo Franzki
83b55b175a P11KMIP: Fix return code confusion
Function build_kmip_config() returns a CK_RV directly, don't assign this to
rc, which is an int.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:57 +02:00
Ingo Franzki
4ea74bacb9 P11KMIP: Fix error handling in p11kmip_check_certificate()
Assign errno, not a CKR_xxx value.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:57 +02:00
Ingo Franzki
ad60ede409 P11KMIP: Don't use hard coded file names for temporary files
Use mkstemp() to get unique file names.

Also remove these temporary files when no longer needed.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:57 +02:00
Ingo Franzki
513647b3f4 P11KMIP: Check length value received from KMIP before using it
KMIP uses signed integers, so theoretically the length can be negative.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:56 +02:00
Ingo Franzki
3b97757967 P11KMIP: Use strtoul() instead of atoi() for slot number parsing
This allows better error handling, atoi() returns 0 on error, but 0 is a
valid slot number.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:56 +02:00
Ingo Franzki
91e698c220 P11KMIP: Fix p11kmip_retrieve_remote_public_key() with transparent public key
Retrieving an RSA public key with KMIP_KEY_FORMAT_TYPE_TRANSPARENT_RSA_PUBLIC_KEY
can never have worked. Implement it correctly.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:56 +02:00
Ingo Franzki
153300dd28 P11KMIP: Prevent unchecked memcpy for boolean attributes
Check the target size before copying the value.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:56 +02:00
Ingo Franzki
df37804a2d P11KMIP: Check for NULL pointer after malloc
Handle allocation failures properly.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:55 +02:00
Ingo Franzki
15bf1bbc3b P11SAK: Fix memory leak with importing or exporting ML-DSA/ML-KEM keys
For import, the private seed is obtained via p11tool_get_octet_string_param_from_pkey()
but is not freed at the end of the import functions.

For export the private seed is allocated but never freed.

Fixes: 5e27911d0a

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:55 +02:00
Ingo Franzki
5db10858aa P11SAK: Don't leak memory on error in p11tool_get_attribute()
If p11tool_get_attribute() allocated a value buffer, but it then fails
to obtain the attribute value, free the allocated buffer and set
pValue = NULL and ulValueLen = 0.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:55 +02:00
Ingo Franzki
8db20c8f42 P11SAK: Cleanse sensitive data on attribute values
Cleanse any attribute value before freeing it.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:55 +02:00
Ingo Franzki
51d6e19d3b P11SAK: Fix possible memory leak when C_WrapKey fails
Don't return early, go to the done label instead.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:54 +02:00
Ingo Franzki
38d5252fac P11SAK: Limit the size of a wrapped key read from disk
Limit the p11tool_bio_readall() to read only up to 64 MB. That is far
more than any realistic wrapped key or opaque blob.

While at it, fix the use of OPENSSL_free() instead of free().

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:54 +02:00
Ingo Franzki
96fd7989e6 P11SAK: Fix an out-of-bounds access on an empty OpenSSL error message
Check for empty message before attempting to read the character at len - 1.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:54 +02:00
Ingo Franzki
11ceab5334 P11SAK: Fix a TOCTOU issue when reading symmetric keys for importing
Use fstat() on the already opened file to avoid a TOCTOU issue.

While at it also cleanse the clear key data on error path.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:53 +02:00