Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P11_CHILD: Make p11_child iterate over all slots #7817

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 46 additions & 23 deletions src/p11_child/p11_child_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,15 @@
token_name, slot_name, (int) slot_id, (int) module_id,
module_file_name);

if (mode == OP_AUTH && strcmp(token_name, token_name_in) != 0) {
DEBUG(SSSDBG_TRACE_ALL, "Token name [%s] does not match "
"token_name_in [%s]. "
"Skipping this token...\n",
token_name, token_name_in);
ret = EOK;
goto done;
}

rv = module->C_OpenSession(slot_id, CKF_SERIAL_SESSION, NULL, NULL,
&session);
if (rv != CKR_OK) {
Expand Down Expand Up @@ -1878,7 +1887,6 @@

if (cert_list == NULL) {
DEBUG(SSSDBG_TRACE_ALL, "No certificate found.\n");
*_multi = NULL;
ret = EOK;
goto done;
}
Expand All @@ -1903,20 +1911,19 @@
"Certificate verified and validated.\n");
}

*_multi = talloc_strdup(mem_ctx, "");
if (*_multi == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Failed to create output string.\n");
ret = ENOMEM;
goto done;
}

DLIST_FOR_EACH(item, cert_list) {
DEBUG(SSSDBG_TRACE_ALL, "Found certificate has key id [%s].\n",
item->id);

*_multi = talloc_asprintf_append(*_multi, "%s\n%s\n%s\n%s\n%s\n",
token_name, module_file_name, item->id,
item->label, item->cert_b64);
if (*_multi == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Failed to append certiticate to the output string.\n");
ret = ENOMEM;
goto done;
}
}

ret = EOK;
Expand All @@ -1943,7 +1950,7 @@
return ret;
}

errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,

Check warning on line 1953 in src/p11_child/p11_child_openssl.c

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'do_card' is never used.
enum op_mode mode, const char *pin,
const char *module_name_in, const char *token_name_in,
const char *key_id_in, const char *label_in,
Expand All @@ -1965,9 +1972,14 @@
CK_INFO module_info;
CK_RV rv;
size_t module_id;
char *multi = NULL;
P11KitUri *uri = NULL;

*_multi = talloc_strdup(mem_ctx, "");
if (*_multi == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Failed to create output string.\n");
return ENOMEM;
}

if (uri_str != NULL) {
uri = p11_kit_uri_new();
if (uri == NULL) {
Expand Down Expand Up @@ -2018,9 +2030,17 @@
}

DEBUG(SSSDBG_TRACE_ALL, "common name: [%s].\n", mod_name);
DEBUG(SSSDBG_TRACE_ALL, "dll name: [%s].\n", mod_file_name);

free(mod_name);

DEBUG(SSSDBG_TRACE_ALL, "dll name: [%s].\n", mod_file_name);
if (mode == OP_AUTH && strcmp(mod_file_name, module_name_in) != 0) {
DEBUG(SSSDBG_TRACE_ALL, "Module name [%s] does not match "
"module_name_in [%s]. "
"Skipping this module...\n",
mod_file_name, module_name_in);
free(mod_file_name);
continue;
}
free(mod_file_name);

rv = modules[c]->C_GetInfo(&module_info);
Expand Down Expand Up @@ -2136,10 +2156,13 @@
}

slot_id = slots[s];
break;
}
if (slot_id != (CK_SLOT_ID)-1) {
break;
module_id = c;
ret = do_slot(module, module_id, slot_id, info, token_info, module_info,
mem_ctx, p11_ctx, mode, pin, module_name_in, token_name_in,
key_id_in, label_in, uri_str, _multi);
if (ret != EOK) {
Copy link
Contributor

@sumit-bose sumit-bose Feb 28, 2025

Choose a reason for hiding this comment

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

Hi,

thank you very much for the patch, it works well in my test with three different tokens.

About the erro handling here. What do you think would be better? Fail on error, SSSD's PAM responder will then forward the error as "no certificate present", as it is now or try best effort and continue with the next slot until all are checked and return the certificate which are found and return no error?

Currently it is a mix. If there are already some certificates found and then an error occurs the error and the already found certificates are return and then ignored. I tend to return the error. If you agree I would like to ask to free _multi at the end of the function if ret is not EOK.

bye,
Sumit

Copy link
Contributor

@lo1ol lo1ol Feb 28, 2025

Choose a reason for hiding this comment

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

I'm not sure, but I think that it could be better ignore failed slots. Fail collection on all tokens also looks good for me

goto done;
}
}
}

Expand All @@ -2158,7 +2181,7 @@
goto done;
}

if (slot_id == (CK_SLOT_ID)-1) {
if (slot_id == (CK_SLOT_ID)-1 || (mode == OP_AUTH && *_multi[0] == '\0')) {
DEBUG(SSSDBG_TRACE_ALL, "Token not present.\n");
if (!p11_ctx->wait_for_card) {
ret = EIO;
Expand All @@ -2179,15 +2202,15 @@
DEBUG(SSSDBG_OP_FAILURE, "wait_for_card failed.\n");
goto done;
}
}

module_id = c;
ret = do_slot(module, module_id, slot_id, info, token_info, module_info,
mem_ctx, p11_ctx, mode, pin, module_name_in, token_name_in,
key_id_in, label_in, uri_str, &multi);
*_multi = multi;
ret = do_slot(module, module_id, slot_id, info, token_info, module_info,
mem_ctx, p11_ctx, mode, pin, module_name_in, token_name_in,
key_id_in, label_in, uri_str, _multi);
if (mode == OP_AUTH && *_multi[0] == '\0') {
ret = EIO;
}
}

ret = EOK;
done:
p11_kit_modules_finalize_and_release(modules);
p11_kit_uri_free(uri);
Expand Down