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

23 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
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
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
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
73faec8193 COMMON: Fix code scan finding: Possible memory leak
If object_init_lock() or object_init_ex_data_lock() fails after the
template is assigned to 'o', then the template is not freed at the
'done' label.

Free the object's template explicitly if it is not NULL.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2025-11-03 14:58:27 +01:00
Ingo Franzki
f7463ce9d0 common: Allow to attach external data to an object
Some tokens may have a need to attach arbitrary data to an object in memory.
This could for example be an OpenSSL PKEY that was created from the object's
attributes, or any other processing or state data that a token needs to keep
with the object.

This data only lives in memory, and is not saved to the disk when the object
is stored (i.e. for token objects).

Each object contains a R/W lock that must be used to protect the attached
data from being accessed concurrently by several threads. Any attempt to set
or update the attached data must be protected by a WRITE lock, while any
attempt to use or read the attached data must be protected by a READ lock.
This lock is independent from the object's template R/W lock. Thus, a token
can update the attached data, although it only has a READ lock on the object
itself (as long as the objects attributes are not updated).

A token may supply a free and a reload routine when attaching such data to an
object. Whenever the object is freed, the free routine is called to allow the
token to also free the attached data. Similarly, when the object is being
reloaded from disk, e.g. because another process has changed the object, the
reload routine is called to allow the token to re-create or update the attached
data. When the reload routine is called, the WRITE lock is being held.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2023-05-04 16:26:48 +02:00
Ingo Franzki
4a3baa9963 EP11: Handle m_Get/m_SetAttributeValue with session bound blobs
Like other EP11 host library functions that operate on key blobs,
m_GetAttributeValue and m_SetAttributeValue may also return
CKR_SESSION_CLOSED when the session that the key blob is bound to
is not logged in into the adapter that is currently used for the
request.

For that the session must be passed through to the functions.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-09-26 13:20:41 +02:00
Ingo Franzki
46baf13ddb COMMON: Validate object name when loading token objects
The object name within a token object payload must match the file name
it was loaded from. For private token objects (CKA_PRIVATE=TRUE) the
object name is part of the encrypted payload.

A mismatch may indicate that the token object file has been tampered,
for example due to a file rename or replacement. Don't load such
objects, since this is most likely not the expected object that is being
loaded.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2022-02-25 13:27:52 +01:00
Juergen Christ
8df74e7c8d Implement and enforce policies
Implement strength and policy parsing and enforce policies in all tokens.  The
main object of the policy is the struct objstrength which carries the strength
of a key along with a flag specifying if the EC is allowed, and a (maximal)
signature length if this key is used to sign or verify and the length of the
signature depends on the key.

A valid strength configuration has to be found.  If no policy configuration is
found, policies are simply deactivated and keys are only classified according
to the strength definition, but can be used without restrictions.  If both, a
strength and a policy configuration, are available, both have to be valid for
OpenCryptoki to start.  If either of these two configurations fails to parse
or validate, OpenCryptoki will fail with a general error since the system is
not in a valid state.

The configuration files have to be owned by root:pkcs11 and have to have modes
0640.  This is done for security reasons to prevent anybody except root from
altering the policy or strength definitions.

Signed-off-by: Juergen Christ <jchrist@linux.ibm.com>
2021-11-08 16:29:24 +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
44e50fa96f Add token specific set_attribute_values function
When attributes are changed via C_SetAttributeValues, give the token
a chance to react on an attribute modification. The token can, for
example, reject certain modifications, or it can perform additional
steps on an modification of certain attributes. Secure key tokens may
for example update the CKA_IBM_OPAQUE key blob to reflect the attribute
modifications.

The token specific set_attribute_values is also called when an object
is copied via C_CopyObject. While copying an object, attributes can be
modified or added, so this is basically the same as first copying the
object as-is, and then modify its attributes.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-12-09 13:54:46 +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
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
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
03610bea17 Return code fixes for C_CreateObject with improper template
Return CKR_TEMPLATE_INCONSISTENT when the template at C_CreateObject
does not contain a sub-class attribute, because this could also be
caused by an unknown object type.

Return CKR_ARGUMENTS_BAD if template is NULL.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-11-24 09:43:57 +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
cf766bd5d5 Dont pass NULL source arg to memcpy even if lenght arg is 0
Signed-off-by: Patrick Steuer <patrick.steuer@de.ibm.com>
2020-10-08 13:04:02 +02:00
Ingo Franzki
d6a19754a5 Protect object attributes from concurrent modification
Add a r/w-lock to each object to protect the object's attributes
from concurrent updates.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-04-03 13:22:06 +02:00
Ingo Franzki
b963c8d1a1 Add reference counts for all objects stored in btrees
This ensures that an object can not be freed while it is still used
by another thread. The object might still be removed from a btree while another
thread is using it, but the object is not freed until its ref count is zero.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-04-03 13:22:06 +02:00
Ingo Franzki
a9ff3ad19e Simplify btree function bt_node_free
There exist 2 almost equal functions named 'bt_node_free()' and
'bt_node_free_()'. The only difference is the signature of the callback
routine used. For one the callback just gets the node value, and for the
other it gets the node value and the STDLL_TokData_t of the token.

Simplify the code by removing the duplicate function and always use
the callback function with just the value.
Use the same callback function signature for bt_destroy().

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2020-04-03 13:22:06 +02:00
Ingo Franzki
a0315a4637 Fix uninitialized pointer read
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2018-10-29 17:28:32 +01:00
Patrick Steuer
e6d7071e89 Autotools rework: get rid of recursive make
Signed-off-by: Patrick Steuer <patrick.steuer@de.ibm.com>
2018-10-08 10:48:42 +02:00