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

53 Commits

Author SHA1 Message Date
Ingo Franzki
dbd41d4a6c TESTS: Add unit and fuzzing tests for object flatten and unflatten in object.c
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
Assisted-by: IBM Bob: 1.0.1
2026-05-28 10:41:26 +02:00
Ingo Franzki
f79c7f7c05 COMMON: Fix possible out-of-bounds read in attribute_array_unflatten()
For unflattening array attributes there was no length check for the attribute
elements within the array attribute to check if the length information within
the array elements is within the array attribute buffer.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-04-24 13:33:12 +02:00
Ingo Franzki
ecbbf6701f COMMON: Fix possible out-of-bounds read in template_unflatten_withSize()
For unflattening ULONG attributes that are stored as 32 bit ULONG a length
check was missing to check if the 32 bit value still is within the buffer
boundaries.

Also check the length before allocating an attribute buffer. Otherwise
a garbage length may cause a very large memory allocation that can cause
memory pressure for other processes.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-04-24 13:33:12 +02:00
Ingo Franzki
76bd743eba COMMON: Add support for CKM_PUB_KEY_FROM_PRIV_KEY in common code
Add support for CKM_PUB_KEY_FROM_PRIV_KEY in common code. This can be
supported transparently for the tokens, by using the SPKI contained in
attribute CKA_PUBLIC_KEY_INFO to extract the public key from.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-04-22 13:07:43 +02:00
Ingo Franzki
3a4719bbac COMMON: Add support for ML-DSA and ML-KEM key types
Add support for the new for ML-DSA and ML-KEM key types.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-04-13 14:39:43 +02:00
Ingo Franzki
f08c175a36 COMMON: Add base support for PKCS#11 v3.2
Add support for the new PKCS#11 v3.2 function list and the new functions
(all of them return CKR_FUNCTION_NOT_SUPPORTED as of now), as well as
for the new encapsulate/decvapsulate attributes.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-04-13 14:39:43 +02:00
Ingo Franzki
fb627b57ad COMMON: Load objects with length checking
Always call object_mgr_restore_obj_withSize() directly and with a proper size.
This ensures that tampered object data does not cause out-of-bounds access.

With that, any check for the special size of -1 can be removed, because now
the size must always be the correct size. This also applies to function
template_unflatten_withSize().

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-04-09 17:24:17 +02:00
Ingo Franzki
3fd5120a41 COMMON: Add support for Edwards and Montgomery key types and mechanisms
Make the Edwards and Montgomery mechanisms and their mechanism parameter known
to common code, policy, statistics, etc. Also support for BER-en-/decoding of
public and private Edwards and Montgomery keys.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-01-27 08:14:16 +01:00
Ingo Franzki
9eb7951a9c COMMON: Set CKA_PRIVATE=TRUE by default for secret and private keys
For keys of class CKO_SECRET_KEY and CKO_PRIVATE_KEY set the CKA_PRIVATE
attribute to TRUE by default. For other objects the default remains
CKA_PRIVATE=FALSE.

In case USER is not logged in, the default for CKA_PRIVATE also remains at
FALSE, because a public or SO session does not have access to private
objects, and thus private keys can not be created in a public or SO session.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-01-15 10:49:18 +01:00
Ingo Franzki
5479612396 COMMON: Make ML-DSA and ML-KEM mechs, attrs, keytype known to common code
Change all Dilithium and Kyber related functions to be named as ML-DSA
and ML-KEM respectively, but ensure that they can be used for Dilithium
and ML-DSA, as well as Kyber and ML-KEM respectively.

While at it, refactor some code places to unify Dilithium/ML-DSA and
Kyber/ML-KEM functions.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2025-10-31 14:15:14 +01:00
Ingo Franzki
d1b0286e12 COMMON: Add support for SHA-HMAC key types and key gen mechanisms
Add support for key types like CKK_SHAxxx_HMAC and the corresponding key
generation mechanisms CKM_SHAxxx_KEY_GEN. These key types are similar to
the generic secret key type (CKK_GENERIC_SECRET). Keys of these types can
be used for the corresponding HMAC-SHA mechanisms (CKM_SHAxxx_HMAC and
CKM_SHAxxx_HMAC_GENERAL), along with generic secret keys.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2025-06-27 10:23:41 +02:00
Ingo Franzki
87d026561e COMMON: Add support for CKM_RSA_AES_KEY_WRAP to key/encrypt/decrypt manager
Implement the CKM_RSA_AES_KEY_WRAP mechanism by internally performing mechanism
CKM_AES_KEY_GEN for generation of the temporary wrapping key, mechanism
CKM_AES_KEY_WRAP_KWP for wrapping the to-be wrapped key, and mechanism
CKM_RSA_PKCS_OAEP for wrapping the temporary AES wrapping key.
Afterwards, destroy the temporary AES key.

The temporary AES wrapping key object is generated as a session object with
CKA_HIDDEN=TRUE, so that it can not be found by means of C_FindObject.
It exists only for a very short time, but it should never be able to be found
by a PKCS#11 application whatsoever.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-12-11 14:37:58 +01: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
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
f8ad4f0395 COMMON: Make private key components of Dilithium and Kyber keys sensitive
The private key components of Dilithium and Kyber keys should be sensitive,
and thus should not be retrievable if CKA_SENSITIVE is true or CKA_EXTRACTABLE
is false.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-04-19 13:42:23 +02:00
Ingo Franzki
bae58c2f30 Fixes for protecting buffers containing sensitive data
For OpenSSL BIGNUMs containing sensitive private key material, use
BN_secure_new() to allocate the BIGNUM on OpenSSLs secure heap.

Also clean any buffers that might contain sensitive data before freeing it.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2024-01-17 10:49:05 +01:00
Ingo Franzki
3ed6d7e1e7 COMMON: Fix detection of subclass for HW feature objects
Similar to keys and certificates, a HW feature object also has a 'subclass'
attribute, i.e. CKA_HW_FEATURE_TYPE. This subclass is used when validating
attributes during C_SetAttributeValue() and C_CopyObject().

Without this fix, setting attributes of a HW feature object, or copying a
HW feature object will most likely fail with CKR_ATTRIBUTE_VALUE_INVALID,
because the subclass is not known and thus the attribute validation fails.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2023-09-11 14:22:58 +02:00
Ingo Franzki
dfac814f0f common: Add template_remove_attribute() function
Allow to remove an attribute from a template.

Also refactor template_update_attribute() to use the new
template_remove_attribute() function to remove the attribute
if it is already existing.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2023-03-14 10:19:10 +01:00
Ingo Franzki
20f3cafe5c common: Add attribute for re-encrypted and old secure key blob
Secure key tokens store the secure key blob in attribute CKA_IBM_OPAQUE
in their key object. During an HSM master key change, the secure key blob
is being re-enciphered with the new master key. This re-enciphered secure
key blob is stored in attribute CKA_IBM_OPAQUE_REENC while the HSM master
key change operation is active.

When a HSM master key change operation is finalized, the secure key blob
enciphered with the old master key is backed up into attribute CKA_IBM_OPAQUE_OLD,
and the re-enciphered secure key blob from CKA_IBM_OPAQUE_REENC becomes
the current one in CKA_IBM_OPAQUE.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2023-03-14 10:19:10 +01:00
Ingo Franzki
c14d85b2b5 COMMON/EP11: Add Kyber key type and mechanism
Kyber requires an EP11 host library of version 4.0 or later,
and a CEX8P crypto card.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2023-01-24 14:52:11 +01:00
Vishnudatha Kanjur
dce327363f common: Add support for AES-XTS in common code
Add mechanisms CKM_AES_XTS and CKM_AES_XTS_KEY_GEN as well as
key type CKK_AES_XTS according to PKCS#11 version 3.0.

Add common code support for generating AES-XTS keys, perform
encryption/decryption as well as wrap/unwrap with AES-XTS keys.

Signed-off-by: Vishnudatha Kanjur <kanjur@ibm.com>
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-10-17 09:54:42 +02:00
Ingo Franzki
ec5f50dda3 common: Remove support for JUNIPER key type
No token is supporting JUNIPER key type anyway, so remove all
common code related to JUNIPER key type as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
d574c3514c common: Remove support for BATON key type
No token is supporting BATON key type anyway, so remove all
common code related to BATON key type as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
4d32537336 common Remove support for SKIPJACK key type
No token is supporting SKIPJACK key type anyway, so remove all
common code related to SKIPJACK key type as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
9c4670f864 common: Remove support for IDEA key type
No token is supporting IDEA key type anyway, so remove all
common code related to IDEA key type as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
4214b4843f common: Remove support for CAST key types
No token is supporting CAST key types anyway, so remove all
common code related to CAST key types as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
e48fdca901 common: Remove support for RC2, RC4, and RC5 key types
No token is supporting RC2, RC4, and RC5 key types anyway, so remove all
common code related to RC2, RC4, and RC5 key types as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
ae53050d6c common: Remove support for KEA key type
No token is supporting KEA key type anyway, so remove all
common code related to KEA key type as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
7d87cd35f7 Remove support for CDMF mechanisms and key type
No token is supporting CDMF mechanisms anyway, so remove all
common code related to CDMF mechanisms as well.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-19 13:30:30 +02:00
Ingo Franzki
8beaea13c0 Attribute CKA_ALWAYS_AUTHENTICATE can not be modified
Return CKR_ATTRIBUTE_READ_ONLY if this attribute is attempted to
be modified via C_SetAttributeValues or C_CopyObject. When keys are
created by means of  C_GenerateKey, C_GenerateKeyPair, C_DeriveKey,
C_UnwrapKey, or C_CreateObject the attribute value must be FALSE,
otherwise CKR_ATTRIBUTE_VALUE_INVALID is returned.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2021-03-16 17:37:41 +01:00
Ingo Franzki
ff74d0d93d Don't allow CKA_COPYABLE to be set to TRUE during C_CopyObject
PKCS#11: "The Cryptoki attributes which can be modified during
the course of a C_CopyObject operation are the same as the Cryptoki
attributes which are described as being modifiable, plus the three
special attributes CKA_TOKEN, CKA_PRIVATE, CKA_MODIFIABLE and
CKA_DESTROYABLE."

CKA_COPYABLE can not be set to TRUE once it was set to FALSE, so it
also can not be set to TRUE during C_CopyObject.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2021-03-16 17:37:41 +01:00
Joerg Schmidbauer
39cedb79ca EP11 pkey option: common code changes
Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2021-02-26 12:44:49 +01:00
Ingo Franzki
d19c461364 Fix attribute-array flatten and unflatten
Some ULONG attributes are always stored as 32 bit ULONG, while others are stored
in the length that CK_ULONG is on the systems architecture. I.e. on a 64 bit
system a CK_ULONG is 64 bit in size, while on a 32 bit system it is 32 bit in size.

The same logic must also be used when unflattening attribute-array elements,
when an element is such an attribute that is always stored as 32 bit ULONG.

Also fix an error when flattening an attribute-array element.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2021-02-01 14:05:50 +01:00
Ingo Franzki
41bb270afa Fix -Wmaybe-uninitialized compile warning
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2021-01-21 19:51:46 +01:00
Ingo Franzki
6c833045dd Some attributes can not be modified after object creation
The following attributes can not be modified after object creation:
CKA_ALLOWED_MECHANISMS, CKA_WRAP_TEMPLATE, CKA_UNWRAP_TEMPLATE, and
CKA_ALWAYS_AUTHENTICATE. Return CKR_ATTRIBUTE_READ_ONLY if those
attributes are being modified.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2021-01-19 10:04:46 +01:00
Harald Freudenberger
dc851deba4 CCA: support import of raw cca tokens with C_ObjectCreate via CKA_IBM_OPAQUE
The secure key blob for all the keys used with the CCA token is
stored in an attribute CKA_IBM_OPAQUE. The content is a so called
secure CCA key token and a description can be found in the
  "Secure Key Solution with the Common Cryptographic
   Architecture Application Programmer's Guide".

There is a requirement to export and import these raw secure key token
objects. Export is simple: Just fetch the value the CKA_IBM_OPAQUE
attribute for a key object and dump it for example into a file.
Importing such a value was until now not supported. This patch
enables the possibility to import such a raw CCA key token object with
the C_CreateObject() pkcs#11 function.

To import an CCA DES key token, use a template with these mandatory
attributes:
  CKA_CLASS = CKO_SECRET_KEY
  CK_KEY_TYPE = CKK_DES
  CKA_IBM_OPAQUE - with the CCA DES key token

to import an CCA DES3 key token, use a template with these mandatory
attributes:
  CKA_CLASS = CKO_SECRET_KEY
  CK_KEY_TYPE = CKK_DES3
  CKA_IBM_OPAQUE - with the CCA DES3 key token

to import an CCA AES data key token, use a template with these mandatory
attributes:
  CKA_CLASS = CKO_SECRET_KEY
  CK_KEY_TYPE = CKK_AES
  CKA_IBM_OPAQUE - with the CCA AES data key token

CCA AES cipher key token import is not supported yet.

to import an CCA internal RSA private key token, use a template with
these mandatory attributes:
  CKA_CLASS = CKO_PRIVATE_KEY
  CKA_KEY_TYPE = CKK_RSA
  CKA_IBM_OPAQUE - with the CCA RSA private key token

to import an CCA RSA public key token, use a template with these
mandatory attributes:
  CKA_CLASS = CKO_PUBLIC_KEY
  CKA_KEY_TYPE = CKK_RSA
  CKA_IBM_OPAQUE - with the CCA RSA public key token

to import an CCA HMAC key token, use a template with these mandatory
attributes:
  CKA_CLASS = CKO_SECRET_KEY
  CKA_KEY_TYPE = CKK_GENERIC_SECRET
  CKA_VALUE - with a dummy value the size in bytes of the clear key
	      byte size.
  CKA_IBM_OPAQUE with the CCA RSA private key token.

to import an CCA internal EC private key token, use a template with
these mandatory attributes:
  CKA_CLASS = CKO_PRIVATE_KEY
  CKA_KEY_TYPE = CKK_EC
  CKA_IBM_OPAQUE - with the CCA EC private key token

to import an CCA EC public key token, use a template with these
mandatory attributes:
  CKA_CLASS = CKO_PUBLIC_KEY
  CKA_KEY_TYPE = CKK_EC
  CKA_IBM_OPAQUE - with the CCA EC public key token

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
2020-12-09 13:54:19 +01:00
Harald Freudenberger
ce13c5ce0a DEBUG: new debug functions
new debug function to hexdump mem region TRACE_DEBUG_DUMP
new debug function to trace one attriubte TRACE_DEBUG_DUMPATTR
new debug function to trace a template TRACE_DEBUG_DUMPTEMPL

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
2020-12-09 13:54:19 +01:00
Joerg Schmidbauer
68f85a6e75 Fix comment and return code
Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
2020-12-01 22:25:08 +01:00
Ingo Franzki
c4f46cbfc9 Add support for attribute-array attributes
Certain attributes, such as CKA_WRAP_TEMPLATE and CKA_UNWRAP_TEMPLATE
contain an array of attributes. These kind of attributes need special
handling at various places, e.g. when storing and reading token objects
from the token object directory, but also when templates are copied or
populated.

Note: Although parts of the token object load and store code has been
previously duplicated to source file usr/sbin/pkcscca/pkcscca.c, the
current changes do not need to be synchronized over to pkcscca.c.
pkcscca.c does not care about any array-attributes, it only looks at some
standard attributes, such as CKA_CLASS, CKA_KEY_TYPE, CKA_VALUE, and
CKA_IBM_OPAQUE. The way array-attributes are stored on disk allows that
such attributes are treated as regular attributes with an arbitrary payload.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
7261d95776 Don't allow to set attribute CKA_ALWAYS_AUTHENTICATE to TRUE
Currently re-authentication for each use of a key is not supported
by Opencryptoki, so do not allow to set CKA_ALWAYS_AUTHENTICATE to
TRUE.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
a7fd22034c Add template_update_attribute error checking
Function template_update_attribute now rturnd CKR_HOST_MEMORY  if no
more memory is available. Fix all callers to check for the return code
of template_update_attribute and cleanup appropriately.

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

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
18b9d14ca4 Fix return codes in attribute validation
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
3e29bcd065 Check access to attributes more precisely
When accessing attributes, check that the length is matching to the
attribute type, e.g. before reading a CK_ULONG or CK_BBOOL attribute
value, make sure that the attribute value size matches the type.

Also add checks for accessing empty attributes. An empty attribute
has a length of 0, and a pointer of NULL. Ensure to not use an NULL
pointer when dealing with an empty attribute.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
682d98ebbb Remove superfluous code
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
a0379df0d4 Support CKA_DESTROYABLE and CKA_COPYABLE
If CKA_DESTROYABLE is FALSE, then the object can not be destroyed.
If CKA_COPYABLE is FALSE, then the object can not be copied.
The default for both is TRUE.

CKA_DESTROYABLE can be updated via C_SetAttributeValue at any time,
unless CKA_MODIFIABLE is FALSE. CKA_COPYABLE can only be update to
FALSE (if CKA_MODIFIABLE=TRUE). Once it is FALSE, it can not be
updated to TRUE anymore. The default for both attributes is TRUE.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
f1e85c0e07 Support CKA_TRUSTED and CKA_WRAP_WITH_TRUSTED attributes
The CKA_TRUSTED attribute can be set for certificates, public and secret key
objects. This attribute can be set to TRUE only be SO after object creation.

Once attribute CKA_WRAP_WITH_TRUSTED is set to TRUE, it becomes read only.
If a key has CKA_WRAP_WITH_TRUSTED = TRUE, then it can only be wrapped
with a key that has CKA_TRUSTED = TRUE.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +01:00
Ingo Franzki
1190f0ac2d Fix buffer overrun in C_CopyObject
When copying attribute CKA_UNIQUE_ID a buffer overrun by 1 byte
occurs when generating a new unique id for the copied object.

Fixes: 07ee26eeef

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-10-26 10:22:30 +01:00
Ingo Franzki
61988c197d Trace more information for attribute type and value errors
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-10-16 13:23:05 +02:00
Patrick Steuer
3e6b3d431c Conform to PKCS 11 3.0 Baseline Provider profile - part 3
Support profile objects.

Signed-off-by: Patrick Steuer <patrick.steuer@de.ibm.com>
2020-10-14 10:59:54 +02:00
Patrick Steuer
07ee26eeef Conform to PKCS 11 v3.0 Baseline Provider profile - part 2
Support unique id attribute.

Signed-off-by: Patrick Steuer <patrick.steuer@de.ibm.com>
2020-10-14 10:59:54 +02:00