2001-11-22 15:40:51 +00:00
|
|
|
|
/*
|
2001-12-30 21:30:39 +00:00
|
|
|
|
* opensc-tool.c: Tool for accessing SmartCards with libopensc
|
2001-11-22 15:40:51 +00:00
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2001 Juha Yrj<EFBFBD>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*/
|
2001-11-18 01:52:32 +00:00
|
|
|
|
|
2002-02-25 16:30:38 +00:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
2001-12-30 21:17:34 +00:00
|
|
|
|
#include <stdio.h>
|
2001-11-18 01:52:32 +00:00
|
|
|
|
#include <stdlib.h>
|
2002-06-14 12:52:56 +00:00
|
|
|
|
#ifndef _WIN32
|
2001-12-30 21:17:34 +00:00
|
|
|
|
#include <unistd.h>
|
2002-06-14 12:52:56 +00:00
|
|
|
|
#endif
|
2001-11-18 01:52:32 +00:00
|
|
|
|
#include <string.h>
|
2002-04-05 13:48:00 +00:00
|
|
|
|
#include <errno.h>
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#include <ctype.h>
|
2002-04-05 13:48:00 +00:00
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <opensc/opensc.h>
|
|
|
|
|
#include "util.h"
|
2001-11-18 01:52:32 +00:00
|
|
|
|
|
|
|
|
|
#define OPT_CHANGE_PIN 0x100
|
|
|
|
|
#define OPT_LIST_PINS 0x101
|
2001-11-18 20:36:15 +00:00
|
|
|
|
#define OPT_READER 0x102
|
2001-11-30 11:57:21 +00:00
|
|
|
|
#define OPT_PIN_ID 0x103
|
2001-12-20 12:16:05 +00:00
|
|
|
|
#define OPT_NO_CACHE 0x104
|
2001-11-18 01:52:32 +00:00
|
|
|
|
|
2002-03-24 14:12:38 +00:00
|
|
|
|
const char *app_name = "opensc-tool";
|
|
|
|
|
|
2001-12-20 12:16:05 +00:00
|
|
|
|
int opt_reader = 0, opt_no_cache = 0, opt_debug = 0;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
char * opt_apdus[8];
|
|
|
|
|
int opt_apdu_count = 0;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
int quiet = 0;
|
|
|
|
|
|
|
|
|
|
const struct option options[] = {
|
2002-01-20 21:20:09 +00:00
|
|
|
|
{ "atr", 0, 0, 'a' },
|
2001-11-18 01:52:32 +00:00
|
|
|
|
{ "list-readers", 0, 0, 'l' },
|
2001-12-25 20:45:48 +00:00
|
|
|
|
{ "list-drivers", 0, 0, 'D' },
|
2002-03-01 11:52:55 +00:00
|
|
|
|
{ "list-rdrivers", 0, 0, 'R' },
|
2001-11-18 01:52:32 +00:00
|
|
|
|
{ "list-files", 0, 0, 'f' },
|
2001-11-20 22:21:58 +00:00
|
|
|
|
{ "send-apdu", 1, 0, 's' },
|
2002-01-08 13:56:50 +00:00
|
|
|
|
{ "reader", 1, 0, 'r' },
|
|
|
|
|
{ "card-driver", 1, 0, 'c' },
|
2001-11-18 01:52:32 +00:00
|
|
|
|
{ "quiet", 0, 0, 'q' },
|
2001-11-20 22:21:58 +00:00
|
|
|
|
{ "debug", 0, 0, 'd' },
|
2001-11-18 01:52:32 +00:00
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char *option_help[] = {
|
2002-01-20 21:20:09 +00:00
|
|
|
|
"Prints the ATR bytes of the card",
|
2001-11-18 01:52:32 +00:00
|
|
|
|
"Lists all configured readers",
|
2002-03-01 11:52:55 +00:00
|
|
|
|
"Lists all installed reader drivers",
|
2001-12-25 20:45:48 +00:00
|
|
|
|
"Lists all installed card drivers",
|
2001-11-18 01:52:32 +00:00
|
|
|
|
"Recursively lists files stored on card",
|
2001-11-20 22:21:58 +00:00
|
|
|
|
"Sends an APDU in format AA:BB:CC:DD:EE:FF...",
|
2002-01-08 13:56:50 +00:00
|
|
|
|
"Uses reader number <arg> [0]",
|
|
|
|
|
"Forces the use of driver <arg> [auto-detect]",
|
2001-11-18 01:52:32 +00:00
|
|
|
|
"Quiet operation",
|
2001-11-21 22:40:27 +00:00
|
|
|
|
"Debug output -- may be supplied several times",
|
2001-11-18 01:52:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct sc_context *ctx = NULL;
|
|
|
|
|
struct sc_card *card = NULL;
|
|
|
|
|
|
2001-12-30 21:17:34 +00:00
|
|
|
|
int list_readers(void)
|
2001-11-18 01:52:32 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (ctx->reader_count == 0) {
|
|
|
|
|
printf("No readers configured!\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2002-03-01 11:52:55 +00:00
|
|
|
|
printf("Readers known about:\n");
|
2002-02-24 19:32:14 +00:00
|
|
|
|
printf("Nr. Driver Name\n");
|
2001-11-18 01:52:32 +00:00
|
|
|
|
for (i = 0; i < ctx->reader_count; i++) {
|
2002-02-24 19:32:14 +00:00
|
|
|
|
printf("%-7d%-11s%s\n", i, ctx->reader[i]->driver->short_name,
|
|
|
|
|
ctx->reader[i]->name);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-01 11:52:55 +00:00
|
|
|
|
int list_reader_drivers(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (ctx->reader_drivers[0] == NULL) {
|
|
|
|
|
printf("No reader drivers installed!\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
printf("Configured reader drivers:\n");
|
|
|
|
|
for (i = 0; ctx->reader_drivers[i] != NULL; i++) {
|
|
|
|
|
printf(" %-16s %s\n", ctx->reader_drivers[i]->short_name,
|
|
|
|
|
ctx->reader_drivers[i]->name);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-30 21:17:34 +00:00
|
|
|
|
int list_drivers(void)
|
2001-12-25 20:45:48 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (ctx->card_drivers[0] == NULL) {
|
|
|
|
|
printf("No card drivers installed!\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
printf("Configured card drivers:\n");
|
|
|
|
|
for (i = 0; ctx->card_drivers[i] != NULL; i++) {
|
2002-01-08 13:56:50 +00:00
|
|
|
|
printf(" %-16s %s\n", ctx->card_drivers[i]->short_name,
|
|
|
|
|
ctx->card_drivers[i]->name);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
int print_file(struct sc_card *card, const struct sc_file *file, const struct sc_path *path, int depth)
|
2001-11-18 01:52:32 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
const char *tmps;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
const char *ac_ops_df[] = {
|
2002-01-08 13:56:50 +00:00
|
|
|
|
"select", "lock", "delete", "create", "rehab", "inval",
|
|
|
|
|
"list"
|
2001-12-29 02:07:32 +00:00
|
|
|
|
};
|
|
|
|
|
const char *ac_ops_ef[] = {
|
|
|
|
|
"read", "update", "write", "erase", "rehab", "inval"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (r = 0; r < depth; r++)
|
2001-11-18 01:52:32 +00:00
|
|
|
|
printf(" ");
|
2001-12-29 02:07:32 +00:00
|
|
|
|
for (r = 0; r < path->len; r++) {
|
|
|
|
|
printf("%02X", path->value[r]);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
if (r && (r & 1) == 1)
|
|
|
|
|
printf(" ");
|
|
|
|
|
}
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (file->namelen) {
|
|
|
|
|
printf("[");
|
|
|
|
|
print_binary(stdout, file->name, file->namelen);
|
|
|
|
|
printf("] ");
|
|
|
|
|
}
|
|
|
|
|
switch (file->type) {
|
|
|
|
|
case SC_FILE_TYPE_WORKING_EF:
|
|
|
|
|
tmps = "wEF";
|
|
|
|
|
break;
|
|
|
|
|
case SC_FILE_TYPE_INTERNAL_EF:
|
|
|
|
|
tmps = "iEF";
|
|
|
|
|
break;
|
|
|
|
|
case SC_FILE_TYPE_DF:
|
|
|
|
|
tmps = " DF";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
tmps = "unknown";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
printf("type: %-3s, ", tmps);
|
|
|
|
|
if (file->type != SC_FILE_TYPE_DF) {
|
|
|
|
|
const char *structs[] = {
|
|
|
|
|
"unknown", "transpnt", "linrfix", "linrfix(TLV)",
|
|
|
|
|
"linvar", "linvar(TLV)", "lincyc", "lincyc(TLV)"
|
|
|
|
|
};
|
|
|
|
|
printf("ef structure: %s, ", structs[file->ef_structure]);
|
|
|
|
|
}
|
|
|
|
|
printf("size: %d\n", file->size);
|
|
|
|
|
for (r = 0; r < depth; r++)
|
|
|
|
|
printf(" ");
|
|
|
|
|
if (file->type == SC_FILE_TYPE_DF)
|
|
|
|
|
for (r = 0; r < sizeof(ac_ops_df)/sizeof(ac_ops_df[0]); r++)
|
2002-02-24 19:32:14 +00:00
|
|
|
|
printf("%s[%s] ", ac_ops_df[r], acl_to_str(sc_file_get_acl_entry(file, r)));
|
2001-12-29 02:07:32 +00:00
|
|
|
|
else
|
|
|
|
|
for (r = 0; r < sizeof(ac_ops_ef)/sizeof(ac_ops_ef[0]); r++)
|
2002-02-24 19:32:14 +00:00
|
|
|
|
printf("%s[%s] ", ac_ops_ef[r], acl_to_str(sc_file_get_acl_entry(file, r)));
|
2001-12-29 02:07:32 +00:00
|
|
|
|
|
|
|
|
|
if (file->sec_attr_len) {
|
|
|
|
|
printf("sec: ");
|
|
|
|
|
/* Octets are as follows:
|
|
|
|
|
* DF: select, lock, delete, create, rehab, inval
|
|
|
|
|
* EF: read, update, write, erase, rehab, inval
|
|
|
|
|
* 4 MSB's of the octet mean:
|
|
|
|
|
* 0 = ALW, 1 = PIN1, 2 = PIN2, 4 = SYS,
|
|
|
|
|
* 15 = NEV */
|
2002-01-24 16:02:54 +00:00
|
|
|
|
hex_dump(stdout, file->sec_attr, file->sec_attr_len, ":");
|
2001-12-29 02:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
if (file->prop_attr_len) {
|
2001-12-20 12:16:05 +00:00
|
|
|
|
printf("\n");
|
2001-12-29 02:07:32 +00:00
|
|
|
|
for (r = 0; r < depth; r++)
|
|
|
|
|
printf(" ");
|
|
|
|
|
printf("prop: ");
|
2002-01-24 16:02:54 +00:00
|
|
|
|
hex_dump(stdout, file->prop_attr, file->prop_attr_len, ":");
|
2001-12-29 02:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
printf("\n\n");
|
2002-01-09 01:03:10 +00:00
|
|
|
|
#if 1
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (file->type != SC_FILE_TYPE_DF) {
|
|
|
|
|
u8 buf[2048];
|
|
|
|
|
if (file->ef_structure == SC_FILE_EF_TRANSPARENT) {
|
|
|
|
|
r = sc_read_binary(card, 0, buf, file->size, 0);
|
|
|
|
|
if (r > 0)
|
2002-01-09 01:03:10 +00:00
|
|
|
|
hex_dump_asc(stdout, buf, r, 0);
|
2001-12-29 02:07:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
r = sc_read_record(card, 0, buf, file->size, 0);
|
2001-12-27 17:25:10 +00:00
|
|
|
|
if (r > 0)
|
2002-01-09 01:03:10 +00:00
|
|
|
|
hex_dump_asc(stdout, buf, r, 0);
|
2001-12-27 17:25:10 +00:00
|
|
|
|
}
|
2001-11-18 01:52:32 +00:00
|
|
|
|
}
|
2001-12-29 02:07:32 +00:00
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int enum_dir(struct sc_path path, int depth)
|
|
|
|
|
{
|
2002-02-20 09:56:47 +00:00
|
|
|
|
struct sc_file *file;
|
|
|
|
|
int r, file_type;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
u8 files[SC_MAX_APDU_BUFFER_SIZE];
|
2001-12-29 02:07:32 +00:00
|
|
|
|
|
|
|
|
|
r = sc_select_file(card, &path, &file);
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "SELECT FILE failed: %s\n", sc_strerror(r));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2002-02-20 09:56:47 +00:00
|
|
|
|
print_file(card, file, &path, depth);
|
|
|
|
|
file_type = file->type;
|
|
|
|
|
sc_file_free(file);
|
|
|
|
|
if (file->type == SC_FILE_TYPE_DF) {
|
2001-11-18 01:52:32 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
r = sc_list_files(card, files, sizeof(files));
|
|
|
|
|
if (r <= 0) {
|
|
|
|
|
fprintf(stderr, "sc_list_files() failed: %s\n", sc_strerror(r));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < r/2; i++) {
|
|
|
|
|
struct sc_path tmppath;
|
|
|
|
|
|
|
|
|
|
memcpy(&tmppath, &path, sizeof(path));
|
|
|
|
|
memcpy(tmppath.value + tmppath.len, files + 2*i, 2);
|
|
|
|
|
tmppath.len += 2;
|
|
|
|
|
enum_dir(tmppath, depth + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-30 21:17:34 +00:00
|
|
|
|
int list_files(void)
|
2001-11-18 01:52:32 +00:00
|
|
|
|
{
|
|
|
|
|
struct sc_path path;
|
|
|
|
|
int r;
|
|
|
|
|
|
2001-12-22 20:52:57 +00:00
|
|
|
|
sc_format_path("3F00", &path);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
r = enum_dir(path, 0);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-30 21:17:34 +00:00
|
|
|
|
int send_apdu(void)
|
2001-11-20 22:21:58 +00:00
|
|
|
|
{
|
|
|
|
|
struct sc_apdu apdu;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
u8 buf[SC_MAX_APDU_BUFFER_SIZE], sbuf[SC_MAX_APDU_BUFFER_SIZE],
|
|
|
|
|
rbuf[SC_MAX_APDU_BUFFER_SIZE], *p;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
size_t len, len0, r;
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
for (c = 0; c < opt_apdu_count; c++) {
|
|
|
|
|
len0 = sizeof(buf);
|
|
|
|
|
sc_hex_to_bin(opt_apdus[c], buf, &len0);
|
|
|
|
|
if (len0 < 4) {
|
|
|
|
|
fprintf(stderr, "APDU too short (must be at least 4 bytes).\n");
|
2001-12-22 20:52:57 +00:00
|
|
|
|
return 2;
|
|
|
|
|
}
|
2002-01-08 13:56:50 +00:00
|
|
|
|
len = len0;
|
|
|
|
|
p = buf;
|
|
|
|
|
apdu.cla = *p++;
|
|
|
|
|
apdu.ins = *p++;
|
|
|
|
|
apdu.p1 = *p++;
|
|
|
|
|
apdu.p2 = *p++;
|
|
|
|
|
apdu.resp = rbuf;
|
|
|
|
|
apdu.resplen = sizeof(rbuf);
|
|
|
|
|
apdu.data = NULL;
|
|
|
|
|
apdu.datalen = 0;
|
2002-02-11 15:55:34 +00:00
|
|
|
|
apdu.lc = apdu.le = 0;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
len -= 4;
|
|
|
|
|
if (len > 1) {
|
|
|
|
|
apdu.lc = *p++;
|
|
|
|
|
len--;
|
|
|
|
|
memcpy(sbuf, p, apdu.lc);
|
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
apdu.datalen = apdu.lc;
|
|
|
|
|
if (len < apdu.lc) {
|
|
|
|
|
fprintf(stderr, "APDU too short (need %d bytes).\n",
|
|
|
|
|
apdu.lc-len);
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
len -= apdu.lc;
|
|
|
|
|
if (len) {
|
|
|
|
|
apdu.le = *p++;
|
2002-02-11 15:55:34 +00:00
|
|
|
|
if (apdu.le == 0)
|
|
|
|
|
apdu.le = 256;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
len--;
|
|
|
|
|
apdu.cse = SC_APDU_CASE_4_SHORT;
|
|
|
|
|
} else
|
|
|
|
|
apdu.cse = SC_APDU_CASE_3_SHORT;
|
|
|
|
|
if (len) {
|
|
|
|
|
fprintf(stderr, "APDU too long (%d bytes extra).\n", len);
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
} else if (len == 1) {
|
2001-11-20 22:21:58 +00:00
|
|
|
|
apdu.le = *p++;
|
2002-02-11 15:55:34 +00:00
|
|
|
|
if (apdu.le == 0)
|
|
|
|
|
apdu.le = 256;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
len--;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
apdu.cse = SC_APDU_CASE_2_SHORT;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
} else
|
2002-01-08 13:56:50 +00:00
|
|
|
|
apdu.cse = SC_APDU_CASE_1;
|
|
|
|
|
printf("Sending: ");
|
|
|
|
|
for (r = 0; r < len0; r++)
|
|
|
|
|
printf("%02X ", buf[r]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
#if 0
|
|
|
|
|
ctx->debug = 5;
|
|
|
|
|
#endif
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
#if 0
|
|
|
|
|
ctx->debug = opt_debug;
|
|
|
|
|
#endif
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "APDU transmit failed: %s\n", sc_strerror(r));
|
|
|
|
|
return 1;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
}
|
2002-01-08 13:56:50 +00:00
|
|
|
|
printf("Received (SW1=0x%02X, SW2=0x%02X)%s\n", apdu.sw1, apdu.sw2,
|
|
|
|
|
apdu.resplen ? ":" : "");
|
|
|
|
|
if (apdu.resplen)
|
2002-01-09 01:03:10 +00:00
|
|
|
|
hex_dump_asc(stdout, apdu.resp, apdu.resplen, -1);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-18 01:52:32 +00:00
|
|
|
|
int main(int argc, char * const argv[])
|
|
|
|
|
{
|
|
|
|
|
int err = 0, r, c, long_optind = 0;
|
|
|
|
|
int do_list_readers = 0;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
int do_list_drivers = 0;
|
2002-03-01 11:52:55 +00:00
|
|
|
|
int do_list_rdrivers = 0;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
int do_list_files = 0;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int do_send_apdu = 0;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
int do_print_atr = 0;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
int action_count = 0;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
const char *opt_driver = NULL;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
|
|
|
|
|
while (1) {
|
2002-03-01 11:52:55 +00:00
|
|
|
|
c = getopt_long(argc, argv, "lfr:qds:DRc:a", options, &long_optind);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
if (c == -1)
|
|
|
|
|
break;
|
|
|
|
|
if (c == '?')
|
2002-03-24 14:12:38 +00:00
|
|
|
|
print_usage_and_die();
|
2001-11-18 01:52:32 +00:00
|
|
|
|
switch (c) {
|
|
|
|
|
case 'l':
|
|
|
|
|
do_list_readers = 1;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
case 'D':
|
|
|
|
|
do_list_drivers = 1;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
2002-03-01 11:52:55 +00:00
|
|
|
|
case 'R':
|
|
|
|
|
do_list_rdrivers = 1;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
case 'f':
|
|
|
|
|
do_list_files = 1;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
case 's':
|
2002-01-08 13:56:50 +00:00
|
|
|
|
opt_apdus[opt_apdu_count] = optarg;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
do_send_apdu++;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (opt_apdu_count == 0)
|
|
|
|
|
action_count++;
|
|
|
|
|
opt_apdu_count++;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
break;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
case 'a':
|
|
|
|
|
do_print_atr = 1;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
case 'r':
|
2001-11-18 20:36:15 +00:00
|
|
|
|
opt_reader = atoi(optarg);
|
|
|
|
|
break;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
case 'q':
|
|
|
|
|
quiet++;
|
|
|
|
|
break;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
case 'd':
|
2001-12-20 12:16:05 +00:00
|
|
|
|
opt_debug++;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
break;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
case 'c':
|
|
|
|
|
opt_driver = optarg;
|
2001-12-20 12:16:05 +00:00
|
|
|
|
break;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (action_count == 0)
|
2002-03-24 14:12:38 +00:00
|
|
|
|
print_usage_and_die();
|
|
|
|
|
r = sc_establish_context(&ctx, app_name);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2002-03-24 21:56:13 +00:00
|
|
|
|
if (opt_debug)
|
|
|
|
|
ctx->debug = opt_debug;
|
2002-03-01 11:52:55 +00:00
|
|
|
|
if (do_list_rdrivers) {
|
|
|
|
|
if ((err = list_reader_drivers()))
|
|
|
|
|
goto end;
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
2001-11-18 01:52:32 +00:00
|
|
|
|
if (do_list_readers) {
|
|
|
|
|
if ((err = list_readers()))
|
|
|
|
|
goto end;
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (do_list_drivers) {
|
|
|
|
|
if ((err = list_drivers()))
|
|
|
|
|
goto end;
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
2001-11-18 01:52:32 +00:00
|
|
|
|
if (action_count <= 0)
|
|
|
|
|
goto end;
|
|
|
|
|
if (opt_reader >= ctx->reader_count || opt_reader < 0) {
|
|
|
|
|
fprintf(stderr, "Illegal reader number. Only %d reader(s) configured.\n", ctx->reader_count);
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2002-02-24 19:32:14 +00:00
|
|
|
|
if (sc_detect_card_presence(ctx->reader[opt_reader], 0) != 1) {
|
2001-11-18 01:52:32 +00:00
|
|
|
|
fprintf(stderr, "Card not present.\n");
|
2002-01-08 13:56:50 +00:00
|
|
|
|
err = 3;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
if (opt_driver != NULL) {
|
2002-01-10 12:33:56 +00:00
|
|
|
|
err = sc_set_card_driver(ctx, opt_driver);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (err) {
|
|
|
|
|
fprintf(stderr, "Driver '%s' not found!\n", opt_driver);
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2001-11-18 01:52:32 +00:00
|
|
|
|
}
|
|
|
|
|
if (!quiet)
|
2002-02-24 19:32:14 +00:00
|
|
|
|
fprintf(stderr, "Connecting to card in reader %s...\n", ctx->reader[opt_reader]->name);
|
|
|
|
|
r = sc_connect_card(ctx->reader[opt_reader], 0, &card);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r));
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2001-12-29 02:07:32 +00:00
|
|
|
|
printf("Using card driver: %s\n", card->driver->name);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
r = sc_lock(card);
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "Unable to lock card: %s\n", sc_strerror(r));
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2002-01-20 21:20:09 +00:00
|
|
|
|
if (do_print_atr) {
|
|
|
|
|
printf("Card ATR:\n");
|
|
|
|
|
hex_dump_asc(stdout, card->atr, card->atr_len, -1);
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
2001-11-20 22:21:58 +00:00
|
|
|
|
if (do_send_apdu) {
|
|
|
|
|
if ((err = send_apdu()))
|
|
|
|
|
goto end;
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-18 01:52:32 +00:00
|
|
|
|
if (do_list_files) {
|
|
|
|
|
if ((err = list_files()))
|
|
|
|
|
goto end;
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
|
|
|
|
end:
|
|
|
|
|
if (card) {
|
|
|
|
|
sc_unlock(card);
|
2002-02-24 19:32:14 +00:00
|
|
|
|
sc_disconnect_card(card, 0);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
}
|
|
|
|
|
if (ctx)
|
2002-03-24 14:12:38 +00:00
|
|
|
|
sc_release_context(ctx);
|
2001-11-18 01:52:32 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|