Skip to content

WIP: Feature/pkcs11 token - #461

Open
MPeisl wants to merge 10 commits into
gyroidos:mainfrom
MPeisl:feature/pkcs11-token
Open

WIP: Feature/pkcs11 token#461
MPeisl wants to merge 10 commits into
gyroidos:mainfrom
MPeisl:feature/pkcs11-token

Conversation

@MPeisl

@MPeisl MPeisl commented Apr 18, 2024

Copy link
Copy Markdown
Member

Support for generic PKCS#11 tokens:

  • scd/pkcs11-lib/*: contains headers and helper libraries
  • scd/pkcs11-test/*: unit-tests for p11token module
  • scd/p11token: actual scd_token implementation for PKCS#11-API

TODO: integration in daemon
TODO: integration testing
TODO before merging: squash commits

@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from b99a779 to cb36751 Compare April 18, 2024 15:02
Comment thread daemon/c_smartcard.c Outdated
Comment thread daemon/c_smartcard.c Outdated
Comment thread daemon/container.h
Comment thread scd/p11token.h Outdated
Comment thread scd/p11token.h Outdated
Comment thread scd/p11token.c Outdated
Comment thread scd/scd.c Outdated
Comment thread scd/token.c
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 14e15bd to 20f9bb3 Compare April 25, 2024 06:34
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch 2 times, most recently from eb35e2b to 2c49d9f Compare June 21, 2024 12:04
Comment thread scd/pkcs11-lib/libpkcs11.c Outdated
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 2c49d9f to 28e753e Compare December 3, 2024 09:35
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch 2 times, most recently from 5e8f238 to f8c7d67 Compare February 24, 2025 16:59
@MPeisl
MPeisl changed the base branch from kirkstone to main February 24, 2025 17:01
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch 3 times, most recently from 3a80617 to da880e5 Compare February 25, 2025 10:26
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from da880e5 to b07efe0 Compare April 1, 2025 08:14
Comment thread scd/p11token.c Outdated
Comment thread scd/Makefile Outdated
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from fdc37d7 to 8352011 Compare April 29, 2025 07:50
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 8352011 to 691ffaf Compare August 12, 2025 07:41
Comment thread scd/scd.c Outdated
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 691ffaf to b8549f6 Compare September 2, 2025 11:12
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from b8549f6 to 1d8ae17 Compare December 3, 2025 13:04
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 6e19fc5 to b78b1f3 Compare January 9, 2026 12:52
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from b78b1f3 to 54b8cd2 Compare January 26, 2026 09:25
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 54b8cd2 to 7f2da99 Compare February 17, 2026 15:27
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 06bef70 to 997d86b Compare March 2, 2026 13:41
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 997d86b to ccffae4 Compare March 18, 2026 16:30
@MPeisl
MPeisl marked this pull request as ready for review March 18, 2026 16:39
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from ccffae4 to d1041ec Compare March 18, 2026 16:39
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from d1041ec to 555c1b4 Compare April 21, 2026 08:14
Comment thread daemon/device_config.h Outdated
Comment thread daemon/scd.c Outdated
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 555c1b4 to 41b165c Compare April 29, 2026 16:24
@MPeisl

MPeisl commented Apr 29, 2026

Copy link
Copy Markdown
Member Author

Refactored module loading, addressed change request. This PR currently depends on #586

@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 41b165c to 1c9cb1e Compare July 3, 2026 15:01
@MPeisl
MPeisl requested a review from quitschbo July 3, 2026 15:09
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch 5 times, most recently from 87cb2d9 to 5069b87 Compare July 7, 2026 16:11
Comment thread scd/p11token.c Outdated
// An open session handle is the ground truth for "already unlocked".
// Do not rely on the generic token->locked flag here: p11token_unlock may
// be invoked directly (e.g. from p11token_change_pin) without going through
// the token_unlock() wrapper that maintains that flag.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multi-line comments pleas use following style:

/*
 * line1
 * line2
 */

Comment thread scd/p11token.c Outdated
// flag: when called directly (e.g. from p11token_change_pin) that flag is
// not maintained, and keying off it would skip C_Logout/C_CloseSession/
// C_Finalize/dlclose, leaking the session and leaving the module
// C_Initialize'd (breaking the next unlock with CKR_CRYPTOKI_ALREADY_INITIALIZED).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multi-line comment

Comment thread scd/p11token.c
} p11_cipher_params_t;

int
int_pkcs11_wrap_to_pkcs7(const unsigned char *ciphertext, size_t ct_len,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare static, isn't exposed by header

Comment thread scd/p11token.c
}

int
int_pkcs7_unwrap_to_pkcs11(const unsigned char *der, int der_len, p11_cipher_params_t *params,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare static, isn't exposed by header

Comment thread scd/p11token.c Outdated

// TODO: use pairing secret?
token_err_t
p11token_unlock(void *int_token, const char *passwd, UNUSED const unsigned char *pairing_secret,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static

Comment thread scd/p11token.c
* lock token and cleanup data structure
*/
void
p11token_free(void *int_token)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static

Comment thread scd/p11token.c
error:
if (pt) {
mem_free0(pt);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (ct) mem_free0(ct); evtl. mem_memset0 beforehand (is encrypted anyway but wrapped key)

Comment thread scd/p11token.c
IF_TRUE_GOTO_ERROR(pt_len > INT_MAX, error);
*plain_key = pt;
*plain_key_len = (int)pt_len;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (ct) mem_free0(ct); evtl. mem_memset0 beforehand (is encrypted anyway but wrapped key)

Comment thread scd/p11token.c Outdated
P11_CHECK_RV_GOTO(ctx->C_CloseSession(sh), error);

// free slot id ptr
mem_free(slot);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible double free if next check failes and goes to error_init.
move mem_free(slot) after the Unload Module block? Double safe with mem_free0

Comment thread scd/p11token.c
token->ctx = NULL;
token->sh = NULL;
token->module = NULL;
token->initialized = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mem_free0(slot)

@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch 3 times, most recently from c7fef9e to 165f5c4 Compare July 16, 2026 14:59
MPeisl added 10 commits July 27, 2026 10:39
This commit unifies the usage of the 'const' qualifier in the token API.

Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
Add a new PKCS11-backed token (TOKEN_TYPE_PKCS11) implementation to scd.
Support is enabled at compile time using the PKCS11 build flag.
PKCS#11-modules are loaded dynamically at runtime based on a
configurable module path.

Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
Add support for new tokentype offered by scd. Add new 'pkcs11_module'
configuration option. Similar to usbtoken or softtoken one can now
configure a container to use a specific PKCS#11 module.

Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
Pass scd-specific environment variables defined in 'device.conf' to scd-unit.

Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
This commit contains several enhancements and security hardening
improvements to the PKCS#11 token type.
Parts are based on severall rounds of AI-based security audits
with corresponding code suggestions.
All proposed changes have been manually reviewed and applied on a
per case basis.

Assisted-by: Claude:claude-fable-5
Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
Guard PKCS#11 related changes to the protobuf files such that they are
excluded in CC_MODE builds.

Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
Add key derivation function which implements
PBKDF2-HMAC-SHA256.

Assisted-by: Claude:claude-fable-5
Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
Bind user-pin to pairing device secret using a KDF.

Assisted-by: Claude:claude-fable-5
Signed-off-by: Maximilian Peisl <maximilian.peisl@aisec.fraunhofer.de>
@MPeisl
MPeisl force-pushed the feature/pkcs11-token branch from 165f5c4 to 44937e8 Compare July 27, 2026 08:39
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

Successfully merging this pull request may close these issues.

2 participants