Skip to content

Commit

Permalink
p11-kit tool: Factor out common logic
Browse files Browse the repository at this point in the history
This moves the common module loading and iteration logic to
p11-kit/tool.c to avoid code duplication.

Signed-off-by: Daiki Ueno <ueno@gnu.org>
  • Loading branch information
ueno committed Dec 28, 2023
1 parent 6450722 commit 45f4ea2
Show file tree
Hide file tree
Showing 15 changed files with 451 additions and 484 deletions.
4 changes: 2 additions & 2 deletions common/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ if get_option('test')
endif

libp11_tool_sources = [
'options.c'
'print.c',
'options.c',
'print.c'
]

if host_system != 'windows'
Expand Down
2 changes: 2 additions & 0 deletions p11-kit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ p11_kit_p11_kit_SOURCES = \
p11-kit/lists.c \
p11-kit/p11-kit.c \
p11-kit/print-config.c \
p11-kit/tool.c \
p11-kit/tool.h \
$(NULL)

if !OS_WIN32
Expand Down
72 changes: 21 additions & 51 deletions p11-kit/add-profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
#include "iter.h"
#include "message.h"
#include "options.h"

#ifdef OS_UNIX
#include "tty.h"
#endif
#include "tool.h"

#include <assert.h>
#include <stdlib.h>
Expand All @@ -63,20 +60,16 @@ p11_kit_add_profile (int argc,
char *argv[]);

static int
add_profile (const char *token_str,
CK_PROFILE_ID profile,
bool login)
add_profile (p11_tool *tool,
CK_PROFILE_ID profile)
{
int ret = 1;
CK_RV rv;
CK_ULONG count = 0;
CK_OBJECT_HANDLE object = 0;
CK_SESSION_HANDLE session = 0;
CK_FUNCTION_LIST *module = NULL;
CK_FUNCTION_LIST **modules = NULL;
P11KitUri *uri = NULL;
P11KitIter *iter = NULL;
P11KitIterBehavior behavior;
CK_BBOOL token = CK_TRUE;
CK_OBJECT_CLASS klass = CKO_PROFILE;
CK_ATTRIBUTE template[] = {
Expand All @@ -86,37 +79,12 @@ add_profile (const char *token_str,
};
CK_ULONG template_len = sizeof (template) / sizeof (template[0]);

uri = p11_kit_uri_new ();
if (uri == NULL) {
p11_message (_("failed to allocate memory"));
goto cleanup;
}

if (p11_kit_uri_parse (token_str, P11_KIT_URI_FOR_TOKEN, uri) != P11_KIT_URI_OK) {
p11_message (_("failed to parse URI"));
goto cleanup;
}

modules = p11_kit_modules_load_and_initialize (0);
if (modules == NULL) {
p11_message (_("failed to load and initialize modules"));
goto cleanup;
}

behavior = P11_KIT_ITER_WANT_WRITABLE | P11_KIT_ITER_WITH_SESSIONS | P11_KIT_ITER_WITHOUT_OBJECTS;
if (login) {
behavior |= P11_KIT_ITER_WITH_LOGIN;
#ifdef OS_UNIX
p11_kit_uri_set_pin_source (uri, "tty");
#endif
}
iter = p11_kit_iter_new (uri, behavior);
iter = p11_tool_begin_iter (tool, P11_KIT_ITER_WANT_WRITABLE | P11_KIT_ITER_WITH_SESSIONS | P11_KIT_ITER_WITHOUT_OBJECTS);
if (iter == NULL) {
p11_message (_("failed to initialize iterator"));
goto cleanup;
return 1;
}

p11_kit_iter_begin (iter, modules);
rv = p11_kit_iter_next (iter);
if (rv != CKR_OK) {
if (rv == CKR_CANCEL)
Expand Down Expand Up @@ -165,10 +133,7 @@ add_profile (const char *token_str,
ret = 0;

cleanup:
p11_kit_iter_free (iter);
p11_kit_uri_free (uri);
if (modules != NULL)
p11_kit_modules_finalize_and_release (modules);
p11_tool_end_iter (tool, iter);

return ret;
}
Expand All @@ -181,6 +146,7 @@ p11_kit_add_profile (int argc,
CK_ULONG profile = CKA_INVALID;
p11_dict *profile_nicks = NULL;
bool login = false;
p11_tool *tool = NULL;

enum {
opt_verbose = 'v',
Expand Down Expand Up @@ -262,19 +228,23 @@ p11_kit_add_profile (int argc,
goto cleanup;
}

#ifdef OS_UNIX
/* Register a fallback PIN callback that reads from terminal.
* We don't care whether the registration succeeds as it is a fallback.
*/
(void)p11_kit_pin_register_callback ("tty", p11_pin_tty_callback, NULL, NULL);
#endif
tool = p11_tool_new ();
if (!tool) {
p11_message (_("failed to allocate memory"));
goto cleanup;
}

ret = add_profile (*argv, profile, login);
if (p11_tool_set_uri (tool, *argv, P11_KIT_URI_FOR_TOKEN) != P11_KIT_URI_OK) {
p11_message (_("failed to parse URI"));
goto cleanup;
}

p11_tool_set_login (tool, login);

ret = add_profile (tool, profile);

cleanup:
#ifdef OS_UNIX
p11_kit_pin_unregister_callback ("tty", p11_pin_tty_callback, NULL);
#endif
p11_tool_free (tool);
p11_dict_free (profile_nicks);

return ret;
Expand Down
73 changes: 22 additions & 51 deletions p11-kit/delete-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
#include "iter.h"
#include "message.h"
#include "options.h"

#ifdef OS_UNIX
#include "tty.h"
#endif
#include "tool.h"

#include <assert.h>
#include <stdlib.h>
Expand All @@ -60,47 +57,18 @@ p11_kit_delete_object (int argc,
char *argv[]);

static int
delete_object (const char *token_str,
bool login)
delete_object (p11_tool *tool)
{
int ret = 1;
CK_RV rv;
P11KitIterBehavior behavior;
CK_FUNCTION_LIST **modules = NULL;
P11KitUri *uri = NULL;
P11KitIter *iter = NULL;

uri = p11_kit_uri_new ();
if (uri == NULL) {
p11_message (_("failed to allocate memory"));
goto cleanup;
}

if (p11_kit_uri_parse (token_str, P11_KIT_URI_FOR_OBJECT_ON_TOKEN, uri) != P11_KIT_URI_OK) {
p11_message (_("failed to parse URI"));
goto cleanup;
}

modules = p11_kit_modules_load_and_initialize (0);
if (modules == NULL) {
p11_message (_("failed to load and initialize modules"));
goto cleanup;
}

behavior = P11_KIT_ITER_WANT_WRITABLE;
if (login) {
behavior |= P11_KIT_ITER_WITH_LOGIN;
#ifdef OS_UNIX
p11_kit_uri_set_pin_source (uri, "tty");
#endif
}
iter = p11_kit_iter_new (uri, behavior);
iter = p11_tool_begin_iter (tool, P11_KIT_ITER_WANT_WRITABLE);
if (iter == NULL) {
p11_message (_("failed to initialize iterator"));
goto cleanup;
return 1;
}

p11_kit_iter_begin (iter, modules);
rv = p11_kit_iter_next (iter);
if (rv != CKR_OK) {
if (rv == CKR_CANCEL)
Expand All @@ -119,10 +87,7 @@ delete_object (const char *token_str,
ret = 0;

cleanup:
p11_kit_iter_free (iter);
p11_kit_uri_free (uri);
if (modules != NULL)
p11_kit_modules_finalize_and_release (modules);
p11_tool_end_iter (tool, iter);

return ret;
}
Expand All @@ -131,8 +96,9 @@ int
p11_kit_delete_object (int argc,
char *argv[])
{
int opt, ret;
int opt, ret = 2;
bool login = false;
p11_tool *tool = NULL;

enum {
opt_verbose = 'v',
Expand Down Expand Up @@ -185,18 +151,23 @@ p11_kit_delete_object (int argc,
return 2;
}

#ifdef OS_UNIX
/* Register a fallback PIN callback that reads from terminal.
* We don't care whether the registration succeeds as it is a fallback.
*/
(void)p11_kit_pin_register_callback ("tty", p11_pin_tty_callback, NULL, NULL);
#endif
tool = p11_tool_new ();
if (!tool) {
p11_message (_("failed to allocate memory"));
goto cleanup;
}

ret = delete_object (*argv, login);
if (p11_tool_set_uri (tool, *argv, P11_KIT_URI_FOR_OBJECT_ON_TOKEN) != P11_KIT_URI_OK) {
p11_message (_("failed to parse URI"));
goto cleanup;
}

#ifdef OS_UNIX
p11_kit_pin_unregister_callback ("tty", p11_pin_tty_callback, NULL);
#endif
p11_tool_set_login (tool, login);

ret = delete_object (tool);

cleanup:
p11_tool_free (tool);

return ret;
}
72 changes: 21 additions & 51 deletions p11-kit/delete-profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
#include "iter.h"
#include "message.h"
#include "options.h"

#ifdef OS_UNIX
#include "tty.h"
#endif
#include "tool.h"

#include <assert.h>
#include <stdlib.h>
Expand All @@ -65,58 +62,29 @@ p11_kit_delete_profile (int argc,
char *argv[]);

static int
delete_profile (const char *token_str,
CK_PROFILE_ID profile,
bool login)
delete_profile (p11_tool *tool,
CK_PROFILE_ID profile)
{
int ret = 1;
CK_RV rv;
CK_OBJECT_HANDLE objects[MAX_OBJECTS];
CK_ULONG i, count = 0;
CK_SESSION_HANDLE session = 0;
CK_FUNCTION_LIST *module = NULL;
CK_FUNCTION_LIST **modules = NULL;
P11KitUri *uri = NULL;
P11KitIter *iter = NULL;
P11KitIterBehavior behavior;
CK_OBJECT_CLASS klass = CKO_PROFILE;
CK_ATTRIBUTE template[] = {
{ CKA_CLASS, &klass, sizeof (klass) },
{ CKA_PROFILE_ID, &profile, sizeof (profile) }
};
CK_ULONG template_len = sizeof (template) / sizeof (template[0]);

uri = p11_kit_uri_new ();
if (uri == NULL) {
p11_message (_("failed to allocate memory"));
goto cleanup;
}

if (p11_kit_uri_parse (token_str, P11_KIT_URI_FOR_TOKEN, uri) != P11_KIT_URI_OK) {
p11_message (_("failed to parse URI"));
goto cleanup;
}

modules = p11_kit_modules_load_and_initialize (0);
if (modules == NULL) {
p11_message (_("failed to load and initialize modules"));
goto cleanup;
}

behavior = P11_KIT_ITER_WANT_WRITABLE | P11_KIT_ITER_WITH_SESSIONS | P11_KIT_ITER_WITHOUT_OBJECTS;
if (login) {
behavior |= P11_KIT_ITER_WITH_LOGIN;
#ifdef OS_UNIX
p11_kit_uri_set_pin_source (uri, "tty");
#endif
}
iter = p11_kit_iter_new (uri, behavior);
iter = p11_tool_begin_iter (tool, P11_KIT_ITER_WANT_WRITABLE | P11_KIT_ITER_WITH_SESSIONS | P11_KIT_ITER_WITHOUT_OBJECTS);
if (iter == NULL) {
p11_message (_("failed to initialize iterator"));
goto cleanup;
return 1;
}

p11_kit_iter_begin (iter, modules);
rv = p11_kit_iter_next (iter);
if (rv != CKR_OK) {
if (rv == CKR_CANCEL)
Expand Down Expand Up @@ -165,10 +133,7 @@ delete_profile (const char *token_str,
ret = 0;

cleanup:
p11_kit_iter_free (iter);
p11_kit_uri_free (uri);
if (modules != NULL)
p11_kit_modules_finalize_and_release (modules);
p11_tool_end_iter (tool, iter);

return ret;
}
Expand All @@ -181,6 +146,7 @@ p11_kit_delete_profile (int argc,
CK_ULONG profile = CKA_INVALID;
p11_dict *profile_nicks = NULL;
bool login = false;
p11_tool *tool = NULL;

enum {
opt_verbose = 'v',
Expand Down Expand Up @@ -262,19 +228,23 @@ p11_kit_delete_profile (int argc,
goto cleanup;
}

#ifdef OS_UNIX
/* Register a fallback PIN callback that reads from terminal.
* We don't care whether the registration succeeds as it is a fallback.
*/
(void)p11_kit_pin_register_callback ("tty", p11_pin_tty_callback, NULL, NULL);
#endif
tool = p11_tool_new ();
if (!tool) {
p11_message (_("failed to allocate memory"));
goto cleanup;
}

ret = delete_profile (*argv, profile, login);
if (p11_tool_set_uri (tool, *argv, P11_KIT_URI_FOR_TOKEN) != P11_KIT_URI_OK) {
p11_message (_("failed to parse URI"));
goto cleanup;
}

p11_tool_set_login (tool, login);

ret = delete_profile (tool, profile);

cleanup:
#ifdef OS_UNIX
p11_kit_pin_unregister_callback ("tty", p11_pin_tty_callback, NULL);
#endif
p11_tool_free (tool);
p11_dict_free (profile_nicks);

return ret;
Expand Down
Loading

0 comments on commit 45f4ea2

Please sign in to comment.