|
|
71e593 |
From d0830a4445115ef4cdbc8b524794f276f7954f7a Mon Sep 17 00:00:00 2001
|
|
|
71e593 |
From: Sumit Bose <sbose@redhat.com>
|
|
|
71e593 |
Date: Mon, 8 Oct 2018 12:47:25 +0200
|
|
|
71e593 |
Subject: [PATCH 44/47] p11_child: add PKCS#11 uri to restrict selection
|
|
|
71e593 |
|
|
|
71e593 |
Related to https://pagure.io/SSSD/sssd/issue/3814
|
|
|
71e593 |
|
|
|
71e593 |
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
|
|
|
71e593 |
(cherry picked from commit f7b2152a4c3c816a5bc4226a0e01791313accef3)
|
|
|
71e593 |
---
|
|
|
71e593 |
src/p11_child/p11_child.h | 2 +-
|
|
|
71e593 |
src/p11_child/p11_child_common.c | 9 +++--
|
|
|
71e593 |
src/p11_child/p11_child_nss.c | 2 +-
|
|
|
71e593 |
src/p11_child/p11_child_openssl.c | 81 +++++++++++++++++++++++++++++++++++++--
|
|
|
71e593 |
4 files changed, 86 insertions(+), 8 deletions(-)
|
|
|
71e593 |
|
|
|
71e593 |
diff --git a/src/p11_child/p11_child.h b/src/p11_child/p11_child.h
|
|
|
71e593 |
index dd8fdeafbf947aad930e61ae694bc99df6d8212a..92ecf74a891e46f93e8dee69391bec6325fe2249 100644
|
|
|
71e593 |
--- a/src/p11_child/p11_child.h
|
|
|
71e593 |
+++ b/src/p11_child/p11_child.h
|
|
|
71e593 |
@@ -54,5 +54,5 @@ bool do_verification_b64(struct p11_ctx *p11_ctx, const char *cert_b64);
|
|
|
71e593 |
errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
enum op_mode mode, const char *pin,
|
|
|
71e593 |
const char *module_name_in, const char *token_name_in,
|
|
|
71e593 |
- const char *key_id_in, char **_multi);
|
|
|
71e593 |
+ const char *key_id_in, const char *uri, char **_multi);
|
|
|
71e593 |
#endif /* __P11_CHILD_H__ */
|
|
|
71e593 |
diff --git a/src/p11_child/p11_child_common.c b/src/p11_child/p11_child_common.c
|
|
|
71e593 |
index bc5f6b09b191f0ea853f45d8a78bc6e4a69c3da7..097e7fa07fb4d90e087250aec9f971b4a2afdb52 100644
|
|
|
71e593 |
--- a/src/p11_child/p11_child_common.c
|
|
|
71e593 |
+++ b/src/p11_child/p11_child_common.c
|
|
|
71e593 |
@@ -60,7 +60,7 @@ static int do_work(TALLOC_CTX *mem_ctx, enum op_mode mode, const char *ca_db,
|
|
|
71e593 |
bool wait_for_card,
|
|
|
71e593 |
const char *cert_b64, const char *pin,
|
|
|
71e593 |
const char *module_name, const char *token_name,
|
|
|
71e593 |
- const char *key_id, char **multi)
|
|
|
71e593 |
+ const char *key_id, const char *uri, char **multi)
|
|
|
71e593 |
{
|
|
|
71e593 |
int ret;
|
|
|
71e593 |
struct p11_ctx *p11_ctx;
|
|
|
71e593 |
@@ -90,7 +90,7 @@ static int do_work(TALLOC_CTX *mem_ctx, enum op_mode mode, const char *ca_db,
|
|
|
71e593 |
}
|
|
|
71e593 |
} else {
|
|
|
71e593 |
ret = do_card(mem_ctx, p11_ctx, mode, pin,
|
|
|
71e593 |
- module_name, token_name, key_id, multi);
|
|
|
71e593 |
+ module_name, token_name, key_id, uri, multi);
|
|
|
71e593 |
}
|
|
|
71e593 |
|
|
|
71e593 |
done:
|
|
|
71e593 |
@@ -159,6 +159,7 @@ int main(int argc, const char *argv[])
|
|
|
71e593 |
char *key_id = NULL;
|
|
|
71e593 |
char *cert_b64 = NULL;
|
|
|
71e593 |
bool wait_for_card = false;
|
|
|
71e593 |
+ char *uri = NULL;
|
|
|
71e593 |
|
|
|
71e593 |
struct poptOption long_options[] = {
|
|
|
71e593 |
POPT_AUTOHELP
|
|
|
71e593 |
@@ -194,6 +195,8 @@ int main(int argc, const char *argv[])
|
|
|
71e593 |
_("Key ID for authentication"), NULL},
|
|
|
71e593 |
{"certificate", 0, POPT_ARG_STRING, &cert_b64, 0,
|
|
|
71e593 |
_("certificate to verify, base64 encoded"), NULL},
|
|
|
71e593 |
+ {"uri", 0, POPT_ARG_STRING, &uri, 0,
|
|
|
71e593 |
+ _("PKCS#11 URI to restrict selection"), NULL},
|
|
|
71e593 |
POPT_TABLEEND
|
|
|
71e593 |
};
|
|
|
71e593 |
|
|
|
71e593 |
@@ -367,7 +370,7 @@ int main(int argc, const char *argv[])
|
|
|
71e593 |
}
|
|
|
71e593 |
|
|
|
71e593 |
ret = do_work(main_ctx, mode, nss_db, cert_verify_opts, wait_for_card,
|
|
|
71e593 |
- cert_b64, pin, module_name, token_name, key_id, &multi);
|
|
|
71e593 |
+ cert_b64, pin, module_name, token_name, key_id, uri, &multi);
|
|
|
71e593 |
if (ret != 0) {
|
|
|
71e593 |
DEBUG(SSSDBG_OP_FAILURE, "do_work failed.\n");
|
|
|
71e593 |
goto fail;
|
|
|
71e593 |
diff --git a/src/p11_child/p11_child_nss.c b/src/p11_child/p11_child_nss.c
|
|
|
71e593 |
index fff1f2525878b596e518b717476e892d1cf2ddae..f9cbf3f37a26c7fefe2106aa9db4b006faaf4e1a 100644
|
|
|
71e593 |
--- a/src/p11_child/p11_child_nss.c
|
|
|
71e593 |
+++ b/src/p11_child/p11_child_nss.c
|
|
|
71e593 |
@@ -480,7 +480,7 @@ bool do_verification_b64(struct p11_ctx *p11_ctx, const char *cert_b64)
|
|
|
71e593 |
errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
enum op_mode mode, const char *pin,
|
|
|
71e593 |
const char *module_name_in, const char *token_name_in,
|
|
|
71e593 |
- const char *key_id_in, char **_multi)
|
|
|
71e593 |
+ const char *key_id_in, const char *uri, char **_multi)
|
|
|
71e593 |
{
|
|
|
71e593 |
int ret;
|
|
|
71e593 |
SECStatus rv;
|
|
|
71e593 |
diff --git a/src/p11_child/p11_child_openssl.c b/src/p11_child/p11_child_openssl.c
|
|
|
71e593 |
index 09edeefbdf95e151af97cd4b4e5811569386caec..000e1c94f11edc32abceafb39e98b16ca0664c50 100644
|
|
|
71e593 |
--- a/src/p11_child/p11_child_openssl.c
|
|
|
71e593 |
+++ b/src/p11_child/p11_child_openssl.c
|
|
|
71e593 |
@@ -85,7 +85,7 @@ static char *get_pkcs11_uri(TALLOC_CTX *mem_ctx, CK_INFO *module_info,
|
|
|
71e593 |
memcpy(p11_kit_uri_get_token_info(uri), token_info, sizeof(CK_TOKEN_INFO));
|
|
|
71e593 |
|
|
|
71e593 |
memcpy(p11_kit_uri_get_slot_info(uri), slot_info, sizeof(CK_SLOT_INFO));
|
|
|
71e593 |
- ret = p11_kit_uri_set_slot_id(uri, slot_id);
|
|
|
71e593 |
+ p11_kit_uri_set_slot_id(uri, slot_id);
|
|
|
71e593 |
|
|
|
71e593 |
memcpy(p11_kit_uri_get_module_info(uri), module_info, sizeof(CK_INFO));
|
|
|
71e593 |
|
|
|
71e593 |
@@ -662,7 +662,7 @@ static errno_t wait_for_card(CK_FUNCTION_LIST *module, CK_SLOT_ID *slot_id)
|
|
|
71e593 |
errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
enum op_mode mode, const char *pin,
|
|
|
71e593 |
const char *module_name_in, const char *token_name_in,
|
|
|
71e593 |
- const char *key_id_in, char **_multi)
|
|
|
71e593 |
+ const char *key_id_in, const char *uri_str, char **_multi)
|
|
|
71e593 |
{
|
|
|
71e593 |
int ret;
|
|
|
71e593 |
size_t c;
|
|
|
71e593 |
@@ -674,6 +674,7 @@ errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
CK_ULONG num_slots;
|
|
|
71e593 |
CK_SLOT_ID slots[MAX_SLOTS];
|
|
|
71e593 |
CK_SLOT_ID slot_id;
|
|
|
71e593 |
+ CK_SLOT_ID uri_slot_id;
|
|
|
71e593 |
CK_SLOT_INFO info;
|
|
|
71e593 |
CK_TOKEN_INFO token_info;
|
|
|
71e593 |
CK_INFO module_info;
|
|
|
71e593 |
@@ -690,6 +691,19 @@ errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
char *multi = NULL;
|
|
|
71e593 |
bool pkcs11_session = false;
|
|
|
71e593 |
bool pkcs11_login = false;
|
|
|
71e593 |
+ P11KitUri *uri = NULL;
|
|
|
71e593 |
+
|
|
|
71e593 |
+ if (uri_str != NULL) {
|
|
|
71e593 |
+ uri = p11_kit_uri_new();
|
|
|
71e593 |
+ ret = p11_kit_uri_parse(uri_str, P11_KIT_URI_FOR_ANY, uri);
|
|
|
71e593 |
+ if (ret != P11_KIT_URI_OK) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_OP_FAILURE, "p11_kit_uri_parse failed [%d][%s].\n",
|
|
|
71e593 |
+ ret, p11_kit_uri_message(ret));
|
|
|
71e593 |
+ ret = EINVAL;
|
|
|
71e593 |
+ goto done;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+
|
|
|
71e593 |
|
|
|
71e593 |
/* Maybe use P11_KIT_MODULE_TRUSTED ? */
|
|
|
71e593 |
modules = p11_kit_modules_load_and_initialize(0);
|
|
|
71e593 |
@@ -709,6 +723,23 @@ errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
free(mod_name);
|
|
|
71e593 |
free(mod_file_name);
|
|
|
71e593 |
|
|
|
71e593 |
+ if (uri != NULL) {
|
|
|
71e593 |
+ memset(&module_info, 0, sizeof(CK_INFO));
|
|
|
71e593 |
+ rv = modules[c]->C_GetInfo(&module_info);
|
|
|
71e593 |
+ if (rv != CKR_OK) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_OP_FAILURE, "C_GetInfo failed.\n");
|
|
|
71e593 |
+ ret = EIO;
|
|
|
71e593 |
+ goto done;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+
|
|
|
71e593 |
+ /* Skip modules which do not match the PKCS#11 URI */
|
|
|
71e593 |
+ if (p11_kit_uri_match_module_info(uri, &module_info) != 1) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_TRACE_ALL,
|
|
|
71e593 |
+ "Not matching URI [%s], skipping.\n", uri_str);
|
|
|
71e593 |
+ continue;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+
|
|
|
71e593 |
num_slots = MAX_SLOTS;
|
|
|
71e593 |
rv = modules[c]->C_GetSlotList(CK_FALSE, slots, &num_slots);
|
|
|
71e593 |
if (rv != CKR_OK) {
|
|
|
71e593 |
@@ -730,6 +761,37 @@ errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
info.slotDescription, info.manufacturerID, info.flags,
|
|
|
71e593 |
(info.flags & CKF_REMOVABLE_DEVICE) ? "true": "false",
|
|
|
71e593 |
(info.flags & CKF_TOKEN_PRESENT) ? "true": "false");
|
|
|
71e593 |
+
|
|
|
71e593 |
+ /* Skip slots which do not match the PKCS#11 URI */
|
|
|
71e593 |
+ if (uri != NULL) {
|
|
|
71e593 |
+ uri_slot_id = p11_kit_uri_get_slot_id(uri);
|
|
|
71e593 |
+ if ((uri_slot_id != (CK_SLOT_ID)-1
|
|
|
71e593 |
+ && uri_slot_id != slots[s])
|
|
|
71e593 |
+ || p11_kit_uri_match_slot_info(uri, &info) != 1) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_TRACE_ALL,
|
|
|
71e593 |
+ "Not matching URI [%s], skipping.\n", uri_str);
|
|
|
71e593 |
+ continue;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+
|
|
|
71e593 |
+ if ((info.flags & CKF_TOKEN_PRESENT) && uri != NULL) {
|
|
|
71e593 |
+ rv = modules[c]->C_GetTokenInfo(slots[s], &token_info);
|
|
|
71e593 |
+ if (rv != CKR_OK) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_OP_FAILURE, "C_GetTokenInfo failed.\n");
|
|
|
71e593 |
+ ret = EIO;
|
|
|
71e593 |
+ goto done;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+ DEBUG(SSSDBG_TRACE_ALL, "Token label [%s].\n",
|
|
|
71e593 |
+ token_info.label);
|
|
|
71e593 |
+
|
|
|
71e593 |
+ if (p11_kit_uri_match_token_info(uri, &token_info) != 1) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_CONF_SETTINGS,
|
|
|
71e593 |
+ "No matching uri [%s], skipping.\n", uri_str);
|
|
|
71e593 |
+ continue;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+
|
|
|
71e593 |
if ((info.flags & CKF_REMOVABLE_DEVICE)) {
|
|
|
71e593 |
break;
|
|
|
71e593 |
}
|
|
|
71e593 |
@@ -788,6 +850,13 @@ errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
goto done;
|
|
|
71e593 |
}
|
|
|
71e593 |
|
|
|
71e593 |
+ if (uri != NULL && p11_kit_uri_match_token_info(uri, &token_info) != 1) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_CONF_SETTINGS, "No token matching uri [%s] found.",
|
|
|
71e593 |
+ uri_str);
|
|
|
71e593 |
+ ret = ENOENT;
|
|
|
71e593 |
+ goto done;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
+
|
|
|
71e593 |
module_id = c;
|
|
|
71e593 |
slot_name = p11_kit_space_strdup(info.slotDescription,
|
|
|
71e593 |
sizeof(info.slotDescription));
|
|
|
71e593 |
@@ -891,7 +960,12 @@ errno_t do_card(TALLOC_CTX *mem_ctx, struct p11_ctx *p11_ctx,
|
|
|
71e593 |
}
|
|
|
71e593 |
|
|
|
71e593 |
memset(&module_info, 0, sizeof(CK_INFO));
|
|
|
71e593 |
- module->C_GetInfo(&module_info);
|
|
|
71e593 |
+ rv = module->C_GetInfo(&module_info);
|
|
|
71e593 |
+ if (rv != CKR_OK) {
|
|
|
71e593 |
+ DEBUG(SSSDBG_OP_FAILURE, "C_GetInfo failed.\n");
|
|
|
71e593 |
+ ret = EIO;
|
|
|
71e593 |
+ goto done;
|
|
|
71e593 |
+ }
|
|
|
71e593 |
|
|
|
71e593 |
DLIST_FOR_EACH(item, cert_list) {
|
|
|
71e593 |
item->uri = get_pkcs11_uri(mem_ctx, &module_info, &info, slot_id,
|
|
|
71e593 |
@@ -970,6 +1044,7 @@ done:
|
|
|
71e593 |
free(token_name);
|
|
|
71e593 |
free(module_file_name);
|
|
|
71e593 |
p11_kit_modules_finalize_and_release(modules);
|
|
|
71e593 |
+ p11_kit_uri_free(uri);
|
|
|
71e593 |
|
|
|
71e593 |
return ret;
|
|
|
71e593 |
}
|
|
|
71e593 |
--
|
|
|
71e593 |
2.14.4
|
|
|
71e593 |
|