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

opensc-explorer: make '--card-driver ?' list all available drivers #1896

Merged
merged 10 commits into from
Jan 22, 2020
7 changes: 0 additions & 7 deletions doc/tools/cardos-tool.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ smart cards and similar security tokens based on Siemens Card/OS M4.
<title>Options</title>
<para>
<variablelist>
<varlistentry>
<term>
<option>--card-driver</option> <replaceable>name</replaceable>,
<option>-c</option> <replaceable>name</replaceable></term>
<listitem><para>Use the card driver specified by <replaceable>name</replaceable>.
The default is to auto-detect the correct card driver.</para></listitem>
</varlistentry>
<varlistentry>
<term>
<option>--format</option>,
Expand Down
8 changes: 0 additions & 8 deletions doc/tools/dnie-tool.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--driver</option> <replaceable>driver</replaceable>,
<option>-c</option> <replaceable>driver</replaceable>
</term>
<listitem><para>Specify the card driver <replaceable>driver</replaceable> to use.
Default is use driver from configuration file, or auto-detect if absent</para></listitem>
</varlistentry>
<varlistentry>
<term>
<option>--wait</option>,
Expand Down
7 changes: 5 additions & 2 deletions doc/tools/opensc-explorer.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@
<option>-c</option> <replaceable>driver</replaceable>
</term>
<listitem><para>
Use the given card driver. The default is
auto-detected.
Use the given card driver.
The default is to auto-detect the correct card driver.
The literal value <literal>?</literal> lists
all available card drivers and terminates
<command>opensc-explorer</command>.
</para></listitem>
</varlistentry>
<varlistentry>
Expand Down
8 changes: 6 additions & 2 deletions doc/tools/opensc-tool.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@
<option>--card-driver</option> <replaceable>driver</replaceable>,
<option>-c</option> <replaceable>driver</replaceable>
</term>
<listitem><para>Use the given card driver.
The default is auto-detected.</para></listitem>
<listitem><para>
Use the given card driver.
The default is to auto-detect the correct card driver.
The literal value <literal>?</literal> lists
all available card drivers.
</para></listitem>
</varlistentry>
<varlistentry>
<term>
Expand Down
8 changes: 0 additions & 8 deletions doc/tools/piv-tool.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,6 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--card-driver</option> <replaceable>driver</replaceable>,
<option>-c</option> <replaceable>driver</replaceable>
</term>
<listitem><para>Use the given card driver.
The default is auto-detected.</para></listitem>
</varlistentry>
<varlistentry>
<term>
<option>--wait</option>,
Expand Down
38 changes: 18 additions & 20 deletions src/tools/cardos-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#endif

#include "libopensc/opensc.h"
#include "libopensc/cards.h"
#include "util.h"

static const char *app_name = "cardos-tool";
Expand All @@ -54,7 +55,6 @@ static const struct option options[] = {
{"startkey", 1, NULL, 's'},
{"change-startkey", 1, NULL, 'S'},
{"reader", 1, NULL, 'r'},
{"card-driver", 1, NULL, 'c'},
{"wait", 0, NULL, 'w'},
{"verbose", 0, NULL, 'v'},
{NULL, 0, NULL, 0}
Expand All @@ -67,7 +67,6 @@ static const char *option_help[] = {
"Specify startkey for format",
"Change Startkey with given APDU command",
"Uses reader number <arg> [0]",
"Forces the use of driver <arg> [auto-detect]",
"Wait for a card to be inserted",
"Verbose operation. Use several times to enable debug output.",
};
Expand Down Expand Up @@ -1034,21 +1033,16 @@ static int cardos_change_startkey(const char *change_startkey_apdu) {

int main(int argc, char *argv[])
{
int err = 0, r, c, long_optind = 0;
int err = 0, r, c;
int do_info = 0;
int do_format = 0;
int do_change_startkey = 0;
int action_count = 0;
const char *opt_driver = NULL;
const char *opt_startkey = NULL;
const char *opt_change_startkey = NULL;
sc_context_param_t ctx_param;

while (1) {
c = getopt_long(argc, argv, "hifs:r:vdc:wS:", options,
&long_optind);
if (c == -1)
break;
while ((c = getopt_long(argc, argv, "hifs:r:vdwS:", options, (int *) 0)) != -1) {
switch (c) {
case 'h':
printf("NB! This tool is only for Siemens CardOS based cards!\n\n");
Expand All @@ -1075,12 +1069,11 @@ int main(int argc, char *argv[])
case 'v':
verbose++;
break;
case 'c':
opt_driver = optarg;
break;
case 'w':
opt_wait = 1;
break;
default:
util_print_usage_and_die(app_name, options, option_help, NULL);
}
}

Expand All @@ -1095,20 +1088,25 @@ int main(int argc, char *argv[])
return 1;
}

if (opt_driver != NULL) {
err = sc_set_card_driver(ctx, opt_driver);
if (err) {
fprintf(stderr, "Driver '%s' not found!\n",
opt_driver);
err = 1;
goto end;
}
/* force CardOS card driver */
err = sc_set_card_driver(ctx, "cardos");
if (err) {
fprintf(stderr, "CardOS card driver not found!\n");
err = 1;
goto end;
}

err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
if (err)
goto end;

/* fail if card is not a CardOS card */
if (card->type < SC_CARD_TYPE_CARDOS_BASE || card->type >= SC_CARD_TYPE_CARDOS_BASE+1000) {
fprintf(stderr, "Card type %X: not a CardOS card\n", card->type);
err = 1;
goto end;
}

if (do_info) {
if ((err = cardos_info())) {
goto end;
Expand Down
105 changes: 49 additions & 56 deletions src/tools/dnie-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "libopensc/opensc.h"
#include "libopensc/errors.h"
#include "libopensc/cardctl.h"
#include "libopensc/cards.h"
#include "libopensc/pkcs15.h"
#include "util.h"

Expand All @@ -54,7 +55,6 @@ static const char *app_name = "dnie-tool";

static const struct option options[] = {
{"reader", 1, NULL, 'r'},
{"driver", 1, NULL, 'c'},
{"wait", 0, NULL, 'w'},
{"pin", 1, NULL, 'p'},
{"idesp", 0, NULL, 'i'},
Expand All @@ -68,7 +68,6 @@ static const struct option options[] = {

static const char *option_help[] = {
"Uses reader number <arg> [0]",
"Uses card driver <arg> [auto-detect]",
"Wait for a card to be inserted",
"Specify PIN",
"Retrieve IDESP",
Expand All @@ -86,33 +85,23 @@ int main(int argc, char* argv[])
int opt_wait = 0;
const char *opt_pin = NULL;
const char *opt_reader = NULL;
const char *opt_driver = NULL;
int opt_operation = OP_NONE;
int verbose = 0;

int err = 0;
sc_context_t *ctx = NULL;
sc_context_param_t ctx_param;
sc_card_t *card = NULL;
int c, long_optind, r, tries_left;
int c, r;

char *data[] = { NULL, NULL, NULL, NULL, NULL };
sc_serial_number_t serial;

while (1) {
c = getopt_long(argc, argv, "r:c:wp:iVdsav",
options, &long_optind);
if (c == -1)
break;
while ((c = getopt_long(argc, argv, "r:wp:iVdsav", options, (int *) 0)) != -1) {
switch (c) {
case '?':
util_print_usage_and_die(app_name, options, option_help, NULL);
case 'r':
opt_reader = optarg;
break;
case 'c':
opt_driver = optarg;
break;
case 'w':
opt_wait = 1;
break;
Expand All @@ -137,6 +126,8 @@ int main(int argc, char* argv[])
case 'v':
verbose++;
break;
default:
util_print_usage_and_die(app_name, options, option_help, NULL);
}
}

Expand All @@ -146,17 +137,16 @@ int main(int argc, char* argv[])
if (r) {
fprintf(stderr, "Error: Failed to establish context: %s\n",
sc_strerror(r));
err = -1;
goto dnie_tool_end;
}

if (opt_driver != NULL) {
err = sc_set_card_driver(ctx, opt_driver);
if (err) {
fprintf(stderr, "Driver '%s' not found!\n",
opt_driver);
err = -1;
goto dnie_tool_end;
}
/* force DNIe card driver */
err = sc_set_card_driver(ctx, "dnie");
if (err) {
fprintf(stderr, "DNIe card driver not found!\n");
err = -1;
goto dnie_tool_end;
}

if (util_connect_card(ctx, &card, opt_reader, opt_wait, verbose) ) {
Expand All @@ -165,13 +155,16 @@ int main(int argc, char* argv[])
goto dnie_tool_end;
}

if ( strcmp(card->name,"dnie") ) {
fprintf(stderr, "Error: Card seems not to be a DNIe\n");
err=-1;
/* fail if card is not a DNIe card */
if (card->type < SC_CARD_TYPE_DNIE_BASE || card->type >= SC_CARD_TYPE_DNIE_BASE+1000) {
fprintf(stderr, "Card type %X: not a DNIe card\n", card->type);
err = -1;
goto dnie_tool_end;
}

if ( opt_pin ) {
int tries_left;

/* verify */
r = sc_verify(card, SC_AC_CHV, 0,
(u8*)opt_pin, strlen(opt_pin), &tries_left);
Expand All @@ -186,44 +179,44 @@ int main(int argc, char* argv[])
}
}

if (opt_operation==0) {
fprintf(stderr,"Error: No operation specified");
err = -1;
goto dnie_tool_end;
}
if (opt_operation & 0x0f) {
if (opt_operation & (OP_GET_DATA | OP_GET_IDESP | OP_GET_VERSION | OP_GET_SERIALNR)) {
r = sc_card_ctl(card, SC_CARDCTL_DNIE_GET_INFO, data);
if ( r != SC_SUCCESS ) {
fprintf(stderr, "Error: Get info failed: %s\n", sc_strerror(r));
err = -1;
goto dnie_tool_end;
}
}
if (opt_operation & OP_GET_DATA) {
printf("DNIe Number: %s\n",data[0]);
printf("SurName: %s\n",data[1]);
printf("Name: %s\n",data[2]);
}
if (opt_operation & OP_GET_IDESP) {
if (data[3]==NULL)
printf("IDESP: (Not available)\n");
else printf("IDESP: %s\n",data[3]);
}
if (opt_operation & OP_GET_VERSION) {
if (data[4]==NULL)
printf("DNIe Version: (Not available)\n");
else printf("DNIe Version: %s\n",data[4]);
}
if (opt_operation & OP_GET_SERIALNR) {
r = sc_card_ctl(card, SC_CARDCTL_GET_SERIALNR, &serial);
if ( r != SC_SUCCESS ) {
fprintf(stderr,"Error: Get serial failed: %s\n",sc_strerror(r));
err = -1;
goto dnie_tool_end;

if (opt_operation & OP_GET_DATA) {
printf("DNIe Number: %s\n",data[0]);
printf("Surname: %s\n",data[1]);
printf("Name: %s\n",data[2]);
}
if (opt_operation & OP_GET_IDESP) {
if (data[3]==NULL)
printf("IDESP: (Not available)\n");
else printf("IDESP: %s\n",data[3]);
}
printf("Serial number: ");
util_hex_dump(stdout, serial.value, serial.len, NULL);
putchar('\n');
if (opt_operation & OP_GET_VERSION) {
if (data[4]==NULL)
printf("DNIe Version: (Not available)\n");
else printf("DNIe Version: %s\n",data[4]);
}
if (opt_operation & OP_GET_SERIALNR) {
r = sc_card_ctl(card, SC_CARDCTL_GET_SERIALNR, &serial);
if ( r != SC_SUCCESS ) {
fprintf(stderr,"Error: Get serial failed: %s\n",sc_strerror(r));
err = -1;
goto dnie_tool_end;
}
printf("Serial number: ");
util_hex_dump(stdout, serial.value, serial.len, NULL);
putchar('\n');
}
}
else {
fprintf(stderr,"Error: No operation specified");
err = -1;
}

dnie_tool_end:
Expand Down
2 changes: 1 addition & 1 deletion src/tools/eidenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static void decode_options(int argc, char **argv)
{
int c;

while ((c = getopt_long(argc, argv,"pwtr:x:hV", options, (int *) 0)) != EOF) {
while ((c = getopt_long(argc, argv,"pwtr:x:hV", options, (int *) 0)) != -1) {

switch (c) {
case 'r':
Expand Down
4 changes: 3 additions & 1 deletion src/tools/netkey-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ int main(
int r, oerr=0, reader=0, debug=0, pin_nr=-1, cert_nr=-1;
size_t i, newlen=0;

while((r=getopt_long(argc,argv,"hvr:p:u:0:1:",options,NULL))!=EOF) switch(r){
while ((r = getopt_long(argc, argv, "hvr:p:u:0:1:", options, NULL)) != -1) {
switch (r) {
case 'h': ++do_help; break;
case 'v': ++debug; break;
case 'r': reader=atoi(optarg); break;
Expand All @@ -455,6 +456,7 @@ int main(
case '0': set_pin(pinlist[2].value, &pinlist[2].len, optarg); break;
case '1': set_pin(pinlist[3].value, &pinlist[3].len, optarg); break;
default: ++oerr;
}
}
if(do_help){
fprintf(stderr,"This is netkey-tool V1.0, May 15 2005, Copyright Peter Koch <[email protected]>\n");
Expand Down
2 changes: 1 addition & 1 deletion src/tools/openpgp-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ static int decode_options(int argc, char **argv)
char *endptr;
unsigned long val;

while ((c = getopt_long(argc, argv,"r:x:CUG:KEht:wvVd:", options, (int *) 0)) != EOF) {
while ((c = getopt_long(argc, argv,"r:x:CUG:KEht:wvVd:", options, (int *) 0)) != -1) {
switch (c) {
case 'r':
opt_reader = optarg;
Expand Down
Loading