diff --git a/doc/tools/cardos-tool.1.xml b/doc/tools/cardos-tool.1.xml index 5f461414..c41e78a0 100644 --- a/doc/tools/cardos-tool.1.xml +++ b/doc/tools/cardos-tool.1.xml @@ -63,14 +63,14 @@ smart cards and similar security tokens based on Siemens Card/OS M4. - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/cryptoflex-tool.1.xml b/doc/tools/cryptoflex-tool.1.xml index f34a3c00..35d5c048 100644 --- a/doc/tools/cryptoflex-tool.1.xml +++ b/doc/tools/cryptoflex-tool.1.xml @@ -131,14 +131,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/dnie-tool.1.xml b/doc/tools/dnie-tool.1.xml index c7bc2051..0fbbb47e 100644 --- a/doc/tools/dnie-tool.1.xml +++ b/doc/tools/dnie-tool.1.xml @@ -83,14 +83,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/egk-tool.1.xml b/doc/tools/egk-tool.1.xml index df6a9cb6..42754235 100644 --- a/doc/tools/egk-tool.1.xml +++ b/doc/tools/egk-tool.1.xml @@ -50,10 +50,10 @@ arg - Specify the reader to use. - Use -1 as arg - to automatically detect the reader to use. - By default, the first reader with a present card is used. + Number of the reader to use. By default, the first + reader with a present card is used. If + arg is an ATR, the + reader with a matching card will be chosen. diff --git a/doc/tools/eidenv.1.xml b/doc/tools/eidenv.1.xml index 75f2d0a7..9ecae75c 100644 --- a/doc/tools/eidenv.1.xml +++ b/doc/tools/eidenv.1.xml @@ -66,14 +66,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/gids-tool.1.xml b/doc/tools/gids-tool.1.xml index ee1a5cac..a7d4f8be 100644 --- a/doc/tools/gids-tool.1.xml +++ b/doc/tools/gids-tool.1.xml @@ -84,9 +84,9 @@ - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + argument is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/iasecc-tool.1.xml b/doc/tools/iasecc-tool.1.xml index 01f0a746..7b175571 100644 --- a/doc/tools/iasecc-tool.1.xml +++ b/doc/tools/iasecc-tool.1.xml @@ -34,13 +34,13 @@ - num, + arg, - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/netkey-tool.1.xml b/doc/tools/netkey-tool.1.xml index 24badd90..89516c03 100644 --- a/doc/tools/netkey-tool.1.xml +++ b/doc/tools/netkey-tool.1.xml @@ -71,14 +71,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/npa-tool.1.xml b/doc/tools/npa-tool.1.xml index a3b419b8..8dcf07ff 100644 --- a/doc/tools/npa-tool.1.xml +++ b/doc/tools/npa-tool.1.xml @@ -52,10 +52,10 @@ arg - Specify the reader to use. - Use -1 as arg - to automatically detect the reader to use. - By default, the first reader with a present card is used. + Number of the reader to use. By default, the first + reader with a present card is used. If + arg is an ATR, the + reader with a matching card will be chosen. diff --git a/doc/tools/openpgp-tool.1.xml b/doc/tools/openpgp-tool.1.xml index bdcaf21b..5f46be29 100644 --- a/doc/tools/openpgp-tool.1.xml +++ b/doc/tools/openpgp-tool.1.xml @@ -179,14 +179,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first - reader with a present card is used. If - num is an ATR, the - reader with a matching card will be chosen. + Number of the reader to use. By default, the first + reader with a present card is used. If + arg is an ATR, the + reader with a matching card will be chosen. diff --git a/doc/tools/opensc-explorer.1.xml b/doc/tools/opensc-explorer.1.xml index 128b486a..7fdd93ce 100644 --- a/doc/tools/opensc-explorer.1.xml +++ b/doc/tools/opensc-explorer.1.xml @@ -76,14 +76,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/opensc-tool.1.xml b/doc/tools/opensc-tool.1.xml index d92c022e..9252d0ce 100644 --- a/doc/tools/opensc-tool.1.xml +++ b/doc/tools/opensc-tool.1.xml @@ -112,14 +112,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/piv-tool.1.xml b/doc/tools/piv-tool.1.xml index 234c3c82..27a052b2 100644 --- a/doc/tools/piv-tool.1.xml +++ b/doc/tools/piv-tool.1.xml @@ -151,14 +151,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/pkcs15-crypt.1.xml b/doc/tools/pkcs15-crypt.1.xml index 6bc6b90e..6c634af4 100644 --- a/doc/tools/pkcs15-crypt.1.xml +++ b/doc/tools/pkcs15-crypt.1.xml @@ -129,14 +129,14 @@ - N, - N + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/pkcs15-init.1.xml b/doc/tools/pkcs15-init.1.xml index 88e73c7a..0983dbca 100644 --- a/doc/tools/pkcs15-init.1.xml +++ b/doc/tools/pkcs15-init.1.xml @@ -644,14 +644,14 @@ puk 87654321 - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/pkcs15-tool.1.xml b/doc/tools/pkcs15-tool.1.xml index bb224c37..c995a3fa 100644 --- a/doc/tools/pkcs15-tool.1.xml +++ b/doc/tools/pkcs15-tool.1.xml @@ -278,13 +278,13 @@ - num + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/sc-hsm-tool.1.xml b/doc/tools/sc-hsm-tool.1.xml index 9f130e1e..deb53e91 100644 --- a/doc/tools/sc-hsm-tool.1.xml +++ b/doc/tools/sc-hsm-tool.1.xml @@ -201,14 +201,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/doc/tools/westcos-tool.1.xml b/doc/tools/westcos-tool.1.xml index 3ddf3827..3034f2e8 100644 --- a/doc/tools/westcos-tool.1.xml +++ b/doc/tools/westcos-tool.1.xml @@ -148,14 +148,14 @@ - num, - num + arg, + arg - Specify the reader to use. By default, the first + Number of the reader to use. By default, the first reader with a present card is used. If - num is an ATR, the + arg is an ATR, the reader with a matching card will be chosen. diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am index 9579c766..a01570f6 100644 --- a/src/tools/Makefile.am +++ b/src/tools/Makefile.am @@ -84,10 +84,14 @@ dnie_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS) gids_tool_SOURCES = gids-tool.c util.c gids_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS) -npa_tool_SOURCES = npa-tool.c fread_to_eof.c $(NPA_TOOL_BUILT_SOURCES) +npa_tool_SOURCES = npa-tool.c fread_to_eof.c util.c $(NPA_TOOL_BUILT_SOURCES) npa_tool_LDADD = $(top_builddir)/src/libopensc/libopensc.la \ $(OPENPACE_LIBS) npa_tool_CFLAGS = -I$(top_srcdir)/src $(OPENPACE_CFLAGS) $(OPENSSL_CFLAGS) +npa_tool_CFLAGS += -Wno-unused-but-set-variable +if HAVE_UNKNOWN_WARNING_OPTION +npa_tool_CFLAGS += -Wno-unknown-warning-option +endif npa-tool.c: $(abs_builddir)/npa-tool.ggo $(NPA_TOOL_BUILT_SOURCES) @@ -117,7 +121,7 @@ $(OPENSC_NOTIFY_BUILT_SOURCES): $(abs_builddir)/opensc-notify.ggo: opensc-notify.ggo.in $(do_subst) < $(abs_srcdir)/opensc-notify.ggo.in > $@ -egk_tool_SOURCES = egk-tool.c $(EGK_TOOL_BUILT_SOURCES) +egk_tool_SOURCES = egk-tool.c util.c $(EGK_TOOL_BUILT_SOURCES) egk_tool_LDADD = $(top_builddir)/src/libopensc/libopensc.la $(OPTIONAL_ZLIB_LIBS) egk_tool_CFLAGS = -I$(top_srcdir)/src $(OPTIONAL_ZLIB_CFLAGS) egk_tool_CFLAGS += -Wno-unused-but-set-variable diff --git a/src/tools/Makefile.mak b/src/tools/Makefile.mak index 20be4b04..0c10a084 100644 --- a/src/tools/Makefile.mak +++ b/src/tools/Makefile.mak @@ -27,14 +27,14 @@ opensc-notify.exe: opensc-notify-cmdline.obj versioninfo-opensc-notify.res $(LIB link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj opensc-notify-cmdline.obj versioninfo-opensc-notify.res $(LIBS) gdi32.lib shell32.lib User32.lib ws2_32.lib mt -manifest exe.manifest -outputresource:$@;1 -npa-tool.exe: npa-tool-cmdline.obj fread_to_eof.obj $(LIBS) +npa-tool.exe: npa-tool-cmdline.obj fread_to_eof.obj util.obj $(LIBS) cl $(COPTS) /c $*.c - link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj npa-tool-cmdline.obj fread_to_eof.obj $(LIBS) $(OPENPACE_LIB) $(OPENSSL_LIB) gdi32.lib shell32.lib User32.lib ws2_32.lib + link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj npa-tool-cmdline.obj fread_to_eof.obj util.obj $(LIBS) $(OPENPACE_LIB) $(OPENSSL_LIB) gdi32.lib shell32.lib User32.lib ws2_32.lib mt -manifest exe.manifest -outputresource:$@;1 -egk-tool.exe: egk-tool-cmdline.obj $(LIBS) +egk-tool.exe: egk-tool-cmdline.obj util.obj $(LIBS) cl $(COPTS) /c $*.c - link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj egk-tool-cmdline.obj $(LIBS) $(ZLIB_LIB) gdi32.lib shell32.lib User32.lib ws2_32.lib + link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj egk-tool-cmdline.obj util.obj $(LIBS) $(ZLIB_LIB) gdi32.lib shell32.lib User32.lib ws2_32.lib mt -manifest exe.manifest -outputresource:$@;1 opensc-asn1.exe: opensc-asn1-cmdline.obj fread_to_eof.obj $(LIBS) diff --git a/src/tools/egk-tool-cmdline.c b/src/tools/egk-tool-cmdline.c index 1ae79678..39f991e7 100644 --- a/src/tools/egk-tool-cmdline.c +++ b/src/tools/egk-tool-cmdline.c @@ -33,22 +33,22 @@ const char *gengetopt_args_info_versiontext = ""; const char *gengetopt_args_info_description = ""; const char *gengetopt_args_info_help[] = { - " -h, --help Print help and exit", - " -V, --version Print version and exit", - " -r, --reader=INT Number of the PC/SC reader to use (-1 for autodetect)\n (default=`-1')", - " -v, --verbose Use (several times) to be more verbose", + " -h, --help Print help and exit", + " -V, --version Print version and exit", + " -r, --reader=STRING Number of the reader to use. By default, the first\n reader with a present card is used. If the arguement\n is an ATR, the reader with a matching card will be\n chosen.", + " -v, --verbose Use (several times) to be more verbose", "\nHealth Care Application (HCA):", - " --pd Show 'Persönliche Versicherungsdaten' (XML) (default=off)", - " --vd Show 'Allgemeine Versicherungsdaten' (XML) (default=off)", - " --gvd Show 'Geschützte Versicherungsdaten' (XML) (default=off)", - " --vsd-status Show 'Versichertenstammdaten-Status' (default=off)", + " --pd Show 'Persönliche Versicherungsdaten' (XML)\n (default=off)", + " --vd Show 'Allgemeine Versicherungsdaten' (XML)\n (default=off)", + " --gvd Show 'Geschützte Versicherungsdaten' (XML)\n (default=off)", + " --vsd-status Show 'Versichertenstammdaten-Status' (default=off)", "\nReport bugs to https://github.com/OpenSC/OpenSC/issues\n\nWritten by Frank Morgner ", 0 }; typedef enum {ARG_NO , ARG_FLAG - , ARG_INT + , ARG_STRING } cmdline_parser_arg_type; static @@ -83,7 +83,7 @@ static void clear_args (struct gengetopt_args_info *args_info) { FIX_UNUSED (args_info); - args_info->reader_arg = -1; + args_info->reader_arg = NULL; args_info->reader_orig = NULL; args_info->pd_flag = 0; args_info->vd_flag = 0; @@ -190,6 +190,7 @@ static void cmdline_parser_release (struct gengetopt_args_info *args_info) { + free_string_field (&(args_info->reader_arg)); free_string_field (&(args_info->reader_orig)); @@ -1051,6 +1052,7 @@ int update_arg(void *field, char **orig_field, char *stop_char = 0; const char *val = value; int found; + char **string_field; FIX_UNUSED (field); stop_char = 0; @@ -1084,24 +1086,18 @@ int update_arg(void *field, char **orig_field, case ARG_FLAG: *((int *)field) = !*((int *)field); break; - case ARG_INT: - if (val) *((int *)field) = strtol (val, &stop_char, 0); + case ARG_STRING: + if (val) { + string_field = (char **)field; + if (!no_free && *string_field) + free (*string_field); /* free previous string */ + *string_field = gengetopt_strdup (val); + } break; default: break; }; - /* check numeric conversion */ - switch(arg_type) { - case ARG_INT: - if (val && !(stop_char && *stop_char == '\0')) { - fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); - return 1; /* failure */ - } - break; - default: - ; - }; /* store the original value */ switch(arg_type) { @@ -1203,12 +1199,12 @@ cmdline_parser_internal ( cmdline_parser_free (&local_args_info); exit (EXIT_SUCCESS); - case 'r': /* Number of the PC/SC reader to use (-1 for autodetect). */ + case 'r': /* Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen.. */ if (update_arg( (void *)&(args_info->reader_arg), &(args_info->reader_orig), &(args_info->reader_given), - &(local_args_info.reader_given), optarg, 0, "-1", ARG_INT, + &(local_args_info.reader_given), optarg, 0, 0, ARG_STRING, check_ambiguity, override, 0, 0, "reader", 'r', additional_error)) diff --git a/src/tools/egk-tool-cmdline.h b/src/tools/egk-tool-cmdline.h index 4b891264..3fb36efa 100644 --- a/src/tools/egk-tool-cmdline.h +++ b/src/tools/egk-tool-cmdline.h @@ -39,9 +39,9 @@ struct gengetopt_args_info { const char *help_help; /**< @brief Print help and exit help description. */ const char *version_help; /**< @brief Print version and exit help description. */ - int reader_arg; /**< @brief Number of the PC/SC reader to use (-1 for autodetect) (default='-1'). */ - char * reader_orig; /**< @brief Number of the PC/SC reader to use (-1 for autodetect) original value given at command line. */ - const char *reader_help; /**< @brief Number of the PC/SC reader to use (-1 for autodetect) help description. */ + char * reader_arg; /**< @brief Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen.. */ + char * reader_orig; /**< @brief Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen. original value given at command line. */ + const char *reader_help; /**< @brief Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen. help description. */ unsigned int verbose_min; /**< @brief Use (several times) to be more verbose's minimum occurreces */ unsigned int verbose_max; /**< @brief Use (several times) to be more verbose's maximum occurreces */ const char *verbose_help; /**< @brief Use (several times) to be more verbose help description. */ diff --git a/src/tools/egk-tool.c b/src/tools/egk-tool.c index af787ed8..f2915ea2 100644 --- a/src/tools/egk-tool.c +++ b/src/tools/egk-tool.c @@ -24,6 +24,7 @@ #include "egk-tool-cmdline.h" #include "libopensc/log.h" #include "libopensc/opensc.h" +#include "util.h" #include #include #include @@ -81,59 +82,7 @@ void dump_binary(void *buf, size_t buf_len) } const unsigned char aid_hca[] = {0xD2, 0x76, 0x00, 0x00, 0x01, 0x02}; - -static int initialize(int reader_id, int verbose, - sc_context_t **ctx, sc_reader_t **reader) -{ - unsigned int i, reader_count; - int r; - - if (!ctx || !reader) - return SC_ERROR_INVALID_ARGUMENTS; - - r = sc_establish_context(ctx, ""); - if (r < 0 || !*ctx) { - fprintf(stderr, "Failed to create initial context: %s", sc_strerror(r)); - return r; - } - - (*ctx)->debug = verbose; - (*ctx)->flags |= SC_CTX_FLAG_ENABLE_DEFAULT_DRIVER; - - reader_count = sc_ctx_get_reader_count(*ctx); - - if (reader_count == 0) { - sc_log(*ctx, "No reader not found.\n"); - return SC_ERROR_NO_READERS_FOUND; - } - - if (reader_id < 0) { - /* Automatically try to skip to a reader with a card if reader not specified */ - for (i = 0; i < reader_count; i++) { - *reader = sc_ctx_get_reader(*ctx, i); - if (sc_detect_card_presence(*reader) & SC_READER_CARD_PRESENT) { - reader_id = i; - sc_log(*ctx, "Using the first reader" - " with a card: %s", (*reader)->name); - break; - } - } - if ((unsigned int) reader_id >= reader_count) { - sc_log(*ctx, "No card found, using the first reader."); - reader_id = 0; - } - } - - if ((unsigned int) reader_id >= reader_count) { - sc_log(*ctx, "Invalid reader number " - "(%d), only %d available.\n", reader_id, reader_count); - return SC_ERROR_NO_READERS_FOUND; - } - - *reader = sc_ctx_get_reader(*ctx, reader_id); - - return SC_SUCCESS; -} +static const char *app_name = "egk-tool"; int read_file(struct sc_card *card, char *str_path, unsigned char **data, size_t *data_len) { @@ -196,26 +145,29 @@ main (int argc, char **argv) struct gengetopt_args_info cmdline; struct sc_path path; struct sc_context *ctx; - struct sc_reader *reader = NULL; struct sc_card *card; unsigned char *data = NULL; size_t data_len = 0; int r; + sc_context_param_t ctx_param; if (cmdline_parser(argc, argv, &cmdline) != 0) exit(1); - r = initialize(cmdline.reader_arg, cmdline.verbose_given, &ctx, &reader); - if (r < 0) { - fprintf(stderr, "Can't initialize reader\n"); + memset(&ctx_param, 0, sizeof(ctx_param)); + ctx_param.ver = 0; + ctx_param.app_name = app_name; + + r = sc_context_create(&ctx, &ctx_param); + if (r) { + fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); exit(1); } - if (sc_connect_card(reader, &card) < 0) { - fprintf(stderr, "Could not connect to card\n"); - sc_release_context(ctx); - exit(1); - } + r = util_connect_card_ex(ctx, &card, cmdline.reader_arg, 0, 0, cmdline.verbose_given); + if (r) + goto err; + sc_path_set(&path, SC_PATH_TYPE_DF_NAME, aid_hca, sizeof aid_hca, 0, 0); if (SC_SUCCESS != sc_select_file(card, &path, NULL)) diff --git a/src/tools/egk-tool.ggo.in b/src/tools/egk-tool.ggo.in index 9142c3e8..6b709461 100644 --- a/src/tools/egk-tool.ggo.in +++ b/src/tools/egk-tool.ggo.in @@ -2,9 +2,8 @@ package "egk-tool" purpose "@PACKAGE_SUMMARY@" option "reader" r - "Number of the PC/SC reader to use (-1 for autodetect)" - int - default="-1" + "Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen." + string optional option "verbose" v "Use (several times) to be more verbose" diff --git a/src/tools/npa-tool-cmdline.c b/src/tools/npa-tool-cmdline.c index 117c6cb1..1b88a20c 100644 --- a/src/tools/npa-tool-cmdline.c +++ b/src/tools/npa-tool-cmdline.c @@ -35,7 +35,7 @@ const char *gengetopt_args_info_description = ""; const char *gengetopt_args_info_help[] = { " -h, --help Print help and exit", " -V, --version Print version and exit", - " -r, --reader=INT Number of the PC/SC reader to use (-1 for\n autodetect) (default=`-1')", + " -r, --reader=STRING Number of the reader to use. By default, the\n first reader with a present card is used. If\n the arguement is an ATR, the reader with a\n matching card will be chosen.", " -v, --verbose Use (several times) to be more verbose", "\nPassword Authenticated Connection Establishment (PACE):", " -p, --pin[=STRING] Run PACE with (transport) eID-PIN", @@ -100,7 +100,6 @@ const char *gengetopt_args_info_help[] = { typedef enum {ARG_NO , ARG_FLAG , ARG_STRING - , ARG_INT } cmdline_parser_arg_type; static @@ -181,7 +180,7 @@ static void clear_args (struct gengetopt_args_info *args_info) { FIX_UNUSED (args_info); - args_info->reader_arg = -1; + args_info->reader_arg = NULL; args_info->reader_orig = NULL; args_info->pin_arg = NULL; args_info->pin_orig = NULL; @@ -400,7 +399,6 @@ free_string_field (char **s) /** @brief generic value variable */ union generic_value { - int int_arg; char *string_arg; const char *default_string_arg; }; @@ -448,6 +446,7 @@ static void cmdline_parser_release (struct gengetopt_args_info *args_info) { + free_string_field (&(args_info->reader_arg)); free_string_field (&(args_info->reader_orig)); free_string_field (&(args_info->pin_arg)); free_string_field (&(args_info->pin_orig)); @@ -1558,9 +1557,6 @@ int update_arg(void *field, char **orig_field, case ARG_FLAG: *((int *)field) = !*((int *)field); break; - case ARG_INT: - if (val) *((int *)field) = strtol (val, &stop_char, 0); - break; case ARG_STRING: if (val) { string_field = (char **)field; @@ -1573,17 +1569,6 @@ int update_arg(void *field, char **orig_field, break; }; - /* check numeric conversion */ - switch(arg_type) { - case ARG_INT: - if (val && !(stop_char && *stop_char == '\0')) { - fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); - return 1; /* failure */ - } - break; - default: - ; - }; /* store the original value */ switch(arg_type) { @@ -1688,8 +1673,6 @@ void update_multiple_arg(void *field, char ***orig_field, *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *)); switch(arg_type) { - case ARG_INT: - *((int **)field) = (int *)realloc (*((int **)field), (field_given + prev_given) * sizeof (int)); break; case ARG_STRING: *((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break; default: @@ -1701,8 +1684,6 @@ void update_multiple_arg(void *field, char ***orig_field, tmp = list; switch(arg_type) { - case ARG_INT: - (*((int **)field))[i + field_given] = tmp->arg.int_arg; break; case ARG_STRING: (*((char ***)field))[i + field_given] = tmp->arg.string_arg; break; default: @@ -1715,12 +1696,6 @@ void update_multiple_arg(void *field, char ***orig_field, } else { /* set the default value */ if (default_value && ! field_given) { switch(arg_type) { - case ARG_INT: - if (! *((int **)field)) { - *((int **)field) = (int *)malloc (sizeof (int)); - (*((int **)field))[0] = default_value->int_arg; - } - break; case ARG_STRING: if (! *((char ***)field)) { *((char ***)field) = (char **)malloc (sizeof (char *)); @@ -1863,12 +1838,12 @@ cmdline_parser_internal ( cmdline_parser_free (&local_args_info); exit (EXIT_SUCCESS); - case 'r': /* Number of the PC/SC reader to use (-1 for autodetect). */ + case 'r': /* Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen.. */ if (update_arg( (void *)&(args_info->reader_arg), &(args_info->reader_orig), &(args_info->reader_given), - &(local_args_info.reader_given), optarg, 0, "-1", ARG_INT, + &(local_args_info.reader_given), optarg, 0, 0, ARG_STRING, check_ambiguity, override, 0, 0, "reader", 'r', additional_error)) diff --git a/src/tools/npa-tool-cmdline.h b/src/tools/npa-tool-cmdline.h index 8c78362a..ad42cc0b 100644 --- a/src/tools/npa-tool-cmdline.h +++ b/src/tools/npa-tool-cmdline.h @@ -39,9 +39,9 @@ struct gengetopt_args_info { const char *help_help; /**< @brief Print help and exit help description. */ const char *version_help; /**< @brief Print version and exit help description. */ - int reader_arg; /**< @brief Number of the PC/SC reader to use (-1 for autodetect) (default='-1'). */ - char * reader_orig; /**< @brief Number of the PC/SC reader to use (-1 for autodetect) original value given at command line. */ - const char *reader_help; /**< @brief Number of the PC/SC reader to use (-1 for autodetect) help description. */ + char * reader_arg; /**< @brief Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen.. */ + char * reader_orig; /**< @brief Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen. original value given at command line. */ + const char *reader_help; /**< @brief Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen. help description. */ unsigned int verbose_min; /**< @brief Use (several times) to be more verbose's minimum occurreces */ unsigned int verbose_max; /**< @brief Use (several times) to be more verbose's maximum occurreces */ const char *verbose_help; /**< @brief Use (several times) to be more verbose help description. */ diff --git a/src/tools/npa-tool.c b/src/tools/npa-tool.c index 92b52808..91bf6489 100644 --- a/src/tools/npa-tool.c +++ b/src/tools/npa-tool.c @@ -22,15 +22,16 @@ #endif #ifdef ENABLE_OPENPACE -#include "npa-tool-cmdline.h" #include "fread_to_eof.h" -#include "sm/sslutil.h" +#include "npa-tool-cmdline.h" #include "sm/sm-eac.h" +#include "sm/sslutil.h" +#include "util.h" #include +#include #include #include #include -#include #include #include #include @@ -111,59 +112,7 @@ IMPLEMENT_ASN1_FUNCTIONS(ASN1_AUXILIARY_DATA_NPA_TOOL) label, (unsigned int) len, len==1?"":"s", len==0?"":":\n", sc_dump_hex(data, len)); \ } -static int initialize(int reader_id, int verbose, - sc_context_t **ctx, sc_reader_t **reader) -{ - unsigned int i, reader_count; - int r; - - if (!ctx || !reader) - return SC_ERROR_INVALID_ARGUMENTS; - - r = sc_establish_context(ctx, ""); - if (r < 0 || !*ctx) { - fprintf(stderr, "Failed to create initial context: %s", sc_strerror(r)); - return r; - } - - (*ctx)->debug = verbose; - (*ctx)->flags |= SC_CTX_FLAG_ENABLE_DEFAULT_DRIVER; - - reader_count = sc_ctx_get_reader_count(*ctx); - - if (reader_count == 0) { - sc_log(*ctx, "No reader not found.\n"); - return SC_ERROR_NO_READERS_FOUND; - } - - if (reader_id < 0) { - /* Automatically try to skip to a reader with a card if reader not specified */ - for (i = 0; i < reader_count; i++) { - *reader = sc_ctx_get_reader(*ctx, i); - if (sc_detect_card_presence(*reader) & SC_READER_CARD_PRESENT) { - reader_id = i; - sc_log(*ctx, "Using the first reader" - " with a card: %s", (*reader)->name); - break; - } - } - if ((unsigned int) reader_id >= reader_count) { - sc_log(*ctx, "No card found, using the first reader."); - reader_id = 0; - } - } - - if ((unsigned int) reader_id >= reader_count) { - sc_log(*ctx, "Invalid reader number " - "(%d), only %d available.\n", reader_id, reader_count); - return SC_ERROR_NO_READERS_FOUND; - } - - *reader = sc_ctx_get_reader(*ctx, reader_id); - - return SC_SUCCESS; -} - +static const char *app_name = "npa-tool"; static void read_dg(sc_card_t *card, unsigned char sfid, const char *dg_str, unsigned char **dg, size_t *dg_len) @@ -361,7 +310,7 @@ main (int argc, char **argv) sc_context_t *ctx = NULL; sc_card_t *card = NULL; - sc_reader_t *reader = NULL; + sc_context_param_t ctx_param; int r, tr_version = EAC_TR_VERSION_2_02; struct establish_pace_channel_input pace_input; @@ -428,17 +377,19 @@ main (int argc, char **argv) eac_default_flags |= EAC_FLAG_DISABLE_CHECK_CA; - r = initialize(cmdline.reader_arg, cmdline.verbose_given, &ctx, &reader); - if (r < 0) { - fprintf(stderr, "Can't initialize reader\n"); + memset(&ctx_param, 0, sizeof(ctx_param)); + ctx_param.ver = 0; + ctx_param.app_name = app_name; + + r = sc_context_create(&ctx, &ctx_param); + if (r) { + fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); exit(1); } - if (sc_connect_card(reader, &card) < 0) { - fprintf(stderr, "Could not connect to card\n"); - sc_release_context(ctx); - exit(1); - } + r = util_connect_card_ex(ctx, &card, cmdline.reader_arg, 0, 0, cmdline.verbose_given); + if (r) + goto err; EAC_init(); if (cmdline.cvc_dir_given) diff --git a/src/tools/npa-tool.ggo.in b/src/tools/npa-tool.ggo.in index 11b0199e..e27889ca 100644 --- a/src/tools/npa-tool.ggo.in +++ b/src/tools/npa-tool.ggo.in @@ -2,9 +2,8 @@ package "npa-tool" purpose "@PACKAGE_SUMMARY@" option "reader" r - "Number of the PC/SC reader to use (-1 for autodetect)" - int - default="-1" + "Number of the reader to use. By default, the first reader with a present card is used. If the arguement is an ATR, the reader with a matching card will be chosen." + string optional option "verbose" v "Use (several times) to be more verbose" diff --git a/src/tools/opensc-notify-cmdline.c b/src/tools/opensc-notify-cmdline.c index 96f2b373..4870aa28 100644 --- a/src/tools/opensc-notify-cmdline.c +++ b/src/tools/opensc-notify-cmdline.c @@ -1176,7 +1176,7 @@ cmdline_parser_internal ( goto failure; break; - case 'R': /* See notify_card_inserted in opensc.conf. */ + case 'R': /* See notify_card_removed in opensc.conf. */ args_info->standard_mode_counter += 1; diff --git a/src/tools/opensc-notify-cmdline.h b/src/tools/opensc-notify-cmdline.h index f0783d62..9bd77963 100644 --- a/src/tools/opensc-notify-cmdline.h +++ b/src/tools/opensc-notify-cmdline.h @@ -47,8 +47,8 @@ struct gengetopt_args_info const char *message_help; /**< @brief Main text of the notification help description. */ int notify_card_inserted_flag; /**< @brief See notify_card_inserted in opensc.conf (default=off). */ const char *notify_card_inserted_help; /**< @brief See notify_card_inserted in opensc.conf help description. */ - int notify_card_removed_flag; /**< @brief See notify_card_inserted in opensc.conf (default=off). */ - const char *notify_card_removed_help; /**< @brief See notify_card_inserted in opensc.conf help description. */ + int notify_card_removed_flag; /**< @brief See notify_card_removed in opensc.conf (default=off). */ + const char *notify_card_removed_help; /**< @brief See notify_card_removed in opensc.conf help description. */ int notify_pin_good_flag; /**< @brief See notify_pin_good in opensc.conf (default=off). */ const char *notify_pin_good_help; /**< @brief See notify_pin_good in opensc.conf help description. */ int notify_pin_bad_flag; /**< @brief See notify_pin_bad in opensc.conf (default=off). */