1
0
mirror of https://github.com/opencryptoki/opencryptoki.git synced 2026-10-28 18:37:25 +03:00

2157 Commits

Author SHA1 Message Date
Ingo Franzki
dc411e01cf COMMON: Avoid memory leak when strdup fails
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-29 15:01:04 +02:00
Joerg Schmidbauer
b8b5de56f5 EP11: make trace msg more informative
Routine check_required_versions not only checks required firmware
versions, but also crypto card levels. If this check fails, the
reason may also be that available cards are too old.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-27 14:24:24 +02:00
Ingo Franzki
17e26ea3a5 TRACE: Print the initial trace message for all trace levels
Print the initial trace message as ERROR instead of as INFO, although this
is really not an error. However, when a trace level lower than INFO is
selected, then the initial trace message would not be written.

The initial trace message contains information about the Opencryptoki
version as well as the trace level selected. This information is always
important, even for lower trace levels.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-27 14:21:59 +02:00
Ingo Franzki
e0b33ebeaa Fix code scanner findings
Fixes for findings found by clang's code checker tool.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-19 09:21:25 +02:00
Joerg Schmidbauer
249ae6d14f CCA: fix typo and formatting, no logic change
Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
556f8ec050 LGR support for PKEY: fix attribute handling in update_ep11_attrs_from_blob
Don't change protected key related attributes for public keys.
Otherwise CKA_IBM_PROTKEY_EXTRACTABLE might be turned off, which
would prevent public key operations to be performed via CPACF.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
4b5b096789 LGR support for PKEY: add memcpy length checks for EC/Ed
Add similar memcpy length checks for EC/Ed. Also clarify some length
calculations by introducing constants for p521 and ed448 CPACF
buffer sizes.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
c95dc21126 LGR support for PKEY: add memcpy length checks for ECB, CBC, and CMAC
Add similar memcpy length checks for ECB, CBC, and CMAC.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
2bc73a1901 LGR support for PKEY: add length checks when copying XTS protkeys
Add length checks where an XTS protected key is copied to a
CPACF param block.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
52630a19ab LGR support for PKEY: use convert_key routine for AES XTS
In case of XTS, the PCC instruction is used to create the XTS iv from given
tweak and XTS key 1. The KM instruction is used for encryption/decryption
using key 2. Both instructions may be interrupted by an LGR. But PCC either
succeeds or fails as a whole, while KM may process less blocks than expected.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
5d196de9de LGR support for PKEY: use convert_key routine for EC/ED
In case of EC/ED sign the whole signing operation with a given input message or
hash either succeeds or goes wrong. So in case of an LGR, we can simply retry
the whole operation. Verification is not a problem at all, because public keys
are not protected keys.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
158ca2ea54 LGR support for PKEY: use convert_key routine for AES ECB/CBC/CMAC
During live guest relocation (LGR) protected keys will become invalid and
their usage will result in a verification pattern mismatch error. Any code
that uses a protected key must therefore be able to handle this error by
re-establishing the protected key from the key material it was derived
from.

Live guest relocation may happen at any time with the exception that
processing of a single data block by a CPACF instruction can be viewed as
an atomic operation. This means that the AES algorithm always either
processes a given full block or fails entirely to process this block.

The application might perform an "in place" encryption/decryption, so that
input and output buffers are identical and data up to the point where
partial completion occurred, is already processed. As there are always
complete blocks processed, opencryptoki must be able to retry such an
operation on a block boundary with the actual CPACF parm block and running
iv or counter.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
84b221e666 LGR support for PKEY: let low-level routines return unsigned int
As the low-level functions return either zero or numbers of
processed bytes, the return value cannot be negative.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
b3777781f4 LGR support for PKEY: fix comments of low-level functions
The low-level functions never returned -1.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
26c092ec4d LGR support for PKEY: handle LGR in ep11tok_pkey_update
Make sure that the new pkey's verification pattern matches the one in
ep11_data. This should always be the case, except there was a live
guest relocation (LGR) in the middle of the process. In case of XTS
there could even be an LGR between creating the protected keys from
the two XTS keys. Also add the pkey lock usage at some places where
it was missing in old code.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
a04fcfe5c2 LGR support for PKEY: introduce convert_key routine for EP11
The new convert_key callback function for EP11 works very similar to the
one for CCA. Just some ep11-related details are different.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
47b0cc8d4b LGR support for PKEY: introduce PKEY lock for CCA
Introduce a read/write lock to protect the pkey_mk_vp field in cca_data
against concurrent access from multiple threads. This is already
implemented for EP11, but was still missing for CCA.

Since the most frequent use case is to just compare (i.e. read) the VP,
a read lock allows concurrent reading without blocking other threads.
With a mutex all other threads are potentially blocked as long as one
thread has the mutex, even as it just reads the VP.

Also add some retry logic to ccatok_pkey_update, because an LGR could
happen while updating a key.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
2a02363e92 LGR support for PKEY: introduce convert_key routine for CCA
The new convert_key callback function handles re-creating protected keys from
their related secure keys. It is called after an LGR when continuing to run
on the new system. This includes locking/unlocking of related key objects and
retrying the operation if another LGR happens in the middle of the process.
It also handles the case where creating the protected key from an XTS key1
happened on the old system, but creating the protected key from XTS key2
happens on the new system after an LGR. Lastly, the verification pattern of
the firmware wrapping key on the new system is saved in cca_data.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
c84b1d5621 LGR support for PKEY: add tokdata and session to AES ECB/CBC/XTS pkey routines
The parms will be needed later by the convert_key routine.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
c104658953 LGR support for PKEY: add tokdata and session to EC/ED pkey routines
The parms will be needed later by the convert_key routine.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
4526af51f2 LGR support for PKEY: add session to token_specific_aes_cmac interface
The session will be needed later by the convert_key routine.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
e8179e187e LGR support for PKEY: handle processed bytes internally in aes_xts_cipher
Routine aes_xts_cipher used *out_data_len als running offset to hold
the number of processed bytes. When retrying XTS ops, e.g. in case of
a wkvp mismatch, this corrupted the output buffer length as specified
by the calling function.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Joerg Schmidbauer
e8ef9abe12 LGR support for PKEY: fix return handling of s390_km
Function s390_km returns the number of processed bytes, not a
return code.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-08-15 16:39:54 +02:00
Ingo Franzki
3831c72964 ICSF: Unify shm name and support multiple token instances
Unify the shared memory segment naming of the ICSF tokens to match the
naming of the other tokens.

Also support to have multiple ICSF tokens, by automatically adding a 'tokname'
keyword pair to the slot configuration when adding a ICSF token via the
pkcsicsf tool. For multiple ICSF tokens it is important that each token
uses its own token directory, and thus a unique token name is required.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-12 14:46:21 +02:00
Ingo Franzki
6f18014618 PKCSHSM_MK_CHANGE: Add warning about no permissions to use a token
When using the pkcshsm_mk_change tool to initiate a concurrent master
key change, it is important that all affected tokens are available and
that the user using the pkcshsm_mk_change tool has permissions to use all
tokens, i.e. the user should be part of all token groups.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-12 14:46:21 +02:00
Ingo Franzki
fe511af72f PKCSSLOTD/API/COMMON: Allow to configure a token user group
A new optional key value pair named 'usergroup' can be specified within a
slot structure to specify the user group name that is set as the owner of
the token directory. Only users that are members of this group have access
to the token and its objects. Users that are not a member of this group
will not see the token as being available (e.g. via 'pkcsconf -t' or via
'C_GetTokenInfo').

If no token group is specified, then the token is owned by the 'pkcs11' group
like before this change.

If a token user group is specified, then the pkcsslotd checks if that group
exists, and if all members of that group are also members of the 'pkcs11'
group. This is required to be able to access the pkcsslotd socket at all.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-12 14:46:21 +02:00
Ingo Franzki
258022052d COMMON/EP11: Handle CKA_ALWAYS_AUTHENTICATE on C_Login
Add support for C_Login with user type CKU_CONTEXT_SPECIFIC for authenticating
due to key usage with CKA_ALWAYS_AUTHENTICATE=TRUE.

If no operation context has the flag set to indicate that authentication is
required, C_Login fails with CKR_OPERATION_NOT_INITIALIZED.

On successful login, reset the flag to indicate that authentication is required
in the operation context to indicate that authentication has been performed.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-07 10:38:30 +02:00
Ingo Franzki
c3db1ccb02 COMMON/EP11: Handle CKA_ALWAYS_AUTHENTICATE on operation execution
In operation execution of sign and decrypt (i.e. C_Sign, C_SignUpdate,
C_SignFinal, C_Decrypt, C_DecryptUpdate, C_DecryptFinal, etc.) check if the
flag indicating that authentication is required is still set in the operation
context.

If set, then no C_Login has been performed since the operation was
initialized, and the call fails with CKR_USER_NOT_LOGGED_IN. If the
flag is not set, then either no authentication has been requested, or it has
been requested, and a proper C_Login has been performed since the operation
was initialized, and thus we can continue with operation execution.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-07 10:38:30 +02:00
Ingo Franzki
69df71d9fc COMMON: Handle CKA_ALWAYS_AUTHENTICATE during operation init
Check attribute CKA_ALWAYS_AUTHENTICATE on operation init calls with private
keys (i.e. C_SignInit, C_DecryptInit) and set a flag in the operation context
that authentication is required before the operation can be continued.

Note that the ICSF token does not support the CKA_ALWAYS_AUTHENTICATE
attribute, because the underlying ICSF services do not support that
attribute. Specifying CKA_ALWAYS_AUTHENTICATE in a template will most
likely fail because ICSF does not know this attribute. The behavior is
as if CKA_ALWAYS_AUTHENTICATE would be FALSE.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-07 10:38:30 +02:00
Ingo Franzki
032e0bd508 COMMON: Allow CKA_ALWAYS_AUTHENTICATE to be set for private keys only
The PKCS#11 standard defines the CKA_ALWAYS_AUTHENTICATE attribute for private
keys (i.e. objects with class=CKO_PRIVATE_KEY) only. It may only be set to
CK_TRUE when CKA_PRIVATE is also CK_TRUE.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-08-07 10:38:30 +02:00
Ingo Franzki
b73d74187c ICSF: Temporarily disable SHA3-HMAC mechanisms
It turns out that ICSF supports the SHA3-HMAC mechanisms since z/OS 2.5.0,
but the RPLUGIN of the z/OS LDAP server does not support them.

Disable the mechanisms for now. Once the z/OS RPLUGIN has been enhanced, they
can be enabled quickly.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-30 09:03:58 +02:00
Ingo Franzki
4f65d91f47 ICSF: Complete the support for SHA3-HMAC mechanisms
A couple of places are missing SHA3-HMAC support, fix this.

Note that HMAC-SHA3 is only supported by z/OS 2.5.0 or later.
It will fail with CKR_FUNCTION_FAILED if used with a z/OS version
that does not support it.

Fixes: 259e9472e0

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-30 09:03:58 +02:00
Ingo Franzki
fb29320ca7 CCA: Add return code checking when adding an attribute
Fix a coverity finding on unchecked return value.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-29 09:54:45 +02:00
Ingo Franzki
ef66ae1c39 CCA: Fix length check on RSA public key import
Fix a coverity finding on a missing length check. The key value structure
contains not only the modulus, but also the public exponent. Make sure that
they fit into the size of the key value structure.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-29 09:54:45 +02:00
Ingo Franzki
34c38cbcef COMMON: Fix coverity finding for missing unlock
When object_put() is called with unlock = FALSE, then it is assumed that
the object is currently not locked. Thus object_put() obtains the read lock
to check if the object is a session or private object. After that the object
must always be unlocked, regardless of the 'unlock' parameter. Either
'unlock' was TRUE then we must unlock the object, or if 'unlock' was FALSE,
then we locked the object our-self, and thus must unlock it again.

Currently, object_put() may only be called with unlock = FALSE by
h_opaque_2_blob() from the EP11 token, when h_opaque_2_blob() was called
with lock_type=NO_LOCK. Currently there is no such case that does that.

Another case where object_put() may be called with unlock = FALSE is
object_mgr_find_in_map_nocache(), when it gets called with lock_type=NO_LOCK.
There, in error cases object_put() may be called with unlock = FALSE.
Currently there is no such case that calls object_mgr_find_in_map_nocache()
with lock_type=NO_LOCK.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-29 09:54:45 +02:00
Ingo Franzki
92135b1176 CCA/EP11: Fix coverity finding on double unlock
Don't go to out label again if an error occurs during unlock.
Instead return the lock error, if no other error is set.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-29 09:54:45 +02:00
Ingo Franzki
d285b768b8 ICSF: Lock session list mutex on session close as well
Fix a coverity finding on a missing mutex lock when closing a session.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-29 09:54:45 +02:00
Ingo Franzki
a221102434 CCA: Fix coverity scan finding
An attribute might have an NULL pValue, don't call OPENSSL_cleanse() in
such a case.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-29 09:54:45 +02:00
Ingo Franzki
dd89a6d149 POLICY/STATISTICS: Check mech parameter length and pointer before accessing it
Policy and statistics code has a need to check certain fields of the mechanism
parameter structures. Check for correct mechanism parameter length and pointer
before attempting to access the mechanism parameter fields. This avoids
seg-faults in case an application passes an invalid mechanism parameter.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-12 08:52:09 +02:00
Ingo Franzki
8ee411b075 COMMON: Fix sensitive IBM Kyber attribute
For the IBM Kyber key type, the CKA_IBM_KYBER_SK attribute contains the
private key and thus, this attribute is sensitive. Attribute CKA_IBM_KYBER_PK
contains the public key and is not sensitive.

Fixes: f8ad4f0395

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-07-10 08:44:51 +02:00
Ingo Franzki
6497508bcb CCA: Also treat RSA-OAEP error 8/2053 as CKR_ENCRYPTED_DATA_INVALID
CCA error 8/2053 means "No message found in the OAEP-decoded data" and is
an additional error (in addition to the already handled 8/2054 error: "There
is a non-valid RSA Enciphered Key cryptogram: OAEP optional encoding
parameters failed validation") that could be returned by CCA verb CSNDPKD in
case of RSA-OAEP on invalid encoded messages.

Translate this error also to CKR_ENCRYPTED_DATA_INVALID to not allow any
distinction between these two error situations. Until now 8/2053 was
translated to CKR_FUNCTION_FAILED, like any other CCA error codes.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-24 11:43:09 +02:00
Ingo Franzki
da7f76d09e SOFT: Check for Key type and size/variant when unwrapping a key
Only allow to unwrap key types and sizes/variants that the soft token
supports.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-24 10:51:12 +02:00
Ingo Franzki
d15cbf0090 SOFT: Add support for sign and verify with IBM Dilithium keys (experimental)
Add support for the soft token to sign and verify with IBM Dilithium keys.

This is an experimental feature, it may change in an incompatible way in
the future! It is not known how the 'oqsprovider' changes in the future,
and if it then still supports the Dilithium variants known by Opencryptoki.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-24 10:51:12 +02:00
Ingo Franzki
cd69f98898 SOFT: Add support for importing IBM Dilithium keys (experimental)
Add support for the soft token to import IBM Dilithium keys from clear
key values. The clear key values can either be supplied via CKA_VALUE,
or via the individual key component attributes.

For a public Dilithium key, the CKA_VALUE attribute may contain the BER
encoded SPKI of the public key. For a private Dilithium key, it may
contain the PKCS#8 encoded private key value.

This is an experimental feature, it may change in an incompatible way in
the future! It is not known how the 'oqsprovider' changes in the future,
and if it then still supports the Dilithium variants known by Opencryptoki.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-24 10:51:12 +02:00
Ingo Franzki
7cb70ddc23 SOFT: Add support for generating IBM Dilithium keys (experimental)
Add support for generating IBM Dilithium keys with mechanism
CKM_IBM_DILITHIUM for the soft token using the 'oqsprovider'.

The soft token can only support those Dilithium variants that the
'oqsprovider' supports. Currently, these are the 3 round 3 variants
with object IDs '1.3.6.1.4.1.2.267.7.4.4', '1.3.6.1.4.1.2.267.7.6.5',
and '1.3.6.1.4.1.2.267.7.8.7'. Those are called 'dilithium2',
'dilithium3' and 'dilithium5' by the 'oqsprovider'.

This is an experimental feature, it may change in an incompatible way in
the future! It is not known how the 'oqsprovider' changes in the future,
and if it then still supports the Dilithium variants known by Opencryptoki.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-24 10:51:12 +02:00
Ingo Franzki
dbaf9c1810 SOFT: Try to load the oqsprovider in token init to support IBM Dilithium
In preparation to support the IBM specific CKM_IBM_DILITHIUM mechanism,
try to load the oqsprovider. The oqsprovider is an optional provider for
OpenSSL 3.x and must be separately installed and configured.
It is available at https://github.com/open-quantum-safe/oqs-provider/ .

If loading of the oqsprovider fails, then mechanism CKM_IBM_DILITHIUM is
not supported by the soft token.

This is an experimental feature, it may change in an incompatible way in
the future! It is not known how the 'oqsprovider' changes in the future,
and if it then still supports the Dilithium variants known by Opencryptoki.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-24 10:51:12 +02:00
Ingo Franzki
6c38e0f747 COMMON: Allow a token to perform actions for a just unwrapped key
A token can either implement its own token_specific_key_unwrap() function
to perform the unwrap operations itself, or it can use common code to
perform the unwrap operation. In case the token performs the unwrap itself,
it can check if it supports the to be unwrapped key type and strength (e.g.
bit size, EC curve, etc), and act accordingly. However, if the token uses
common code for unwrapping, it has no chance to check the unwrapped key.
Thus it can happen that the common code unwraps a key with a key type or
strength that the token does not support.

Call the token_specific_set_attrs_for_new_object() function with mode
MODE_UNWRAPPED to give the token a chance to inspect the just unwrapped key
and take appropriate actions. This is only done when the common code is
used to perform the unwrap. If the token provides its own
token_specific_key_unwrap() function, then no additional call to the
token_specific_set_attrs_for_new_object() function is done, because the
token can check the unwrapped key in its token_specific_key_unwrap() function
already.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-24 10:51:12 +02:00
Joerg Schmidbauer
70d3c17755 COMMON: correct rc handling with s390_pcc function
Let s390_pcc return the condition code of the PCC instruction.
Also fix the error handling in the calling functions.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2024-06-20 11:39:45 +02:00
Ingo Franzki
ac4f322dcb ICSF: Fix covscan findings on potential integer overflows
Fix covscan warnings on cases like 'if (a - b > 0)' where both 'a' and 'b'
are unsigned types. In case 'b' is larger than 'a', then the subtraction
result may overflow because the result is also treated as unsigned type.
Fix this by using 'if (a > b)' instead.

Note that in the changed places 'a' is always larger than or equal to 'b',
so the overflow does not happen. Still, changing the code to be less
error-prone is a good thing.

Closes: https://github.com/opencryptoki/opencryptoki/issues/782

Suggested-by: Than Ngo <than@redhat.com>
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-18 14:08:54 +02:00
Ingo Franzki
72e3e2e8c7 COMMON: Update statistics for SHA based key derivation mechanisms
When adding common code support for the SHA based key derivation mechanisms
it was forgotten to also increase the statistics counters for those
mechanisms on successful completion.

Fixes: a34694e384

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-06-17 13:18:42 +02:00