assorted small fixes

git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@34 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
jey 2001-11-04 14:08:38 +00:00
parent 58fd72f22f
commit 16c6db4b2a
6 changed files with 60 additions and 12 deletions

View File

@ -103,6 +103,7 @@ struct sc_card {
int class; int class;
struct sc_context *context; struct sc_context *context;
SCARDHANDLE pcsc_card; SCARDHANDLE pcsc_card;
const char *reader;
}; };
struct sc_context { struct sc_context {
@ -130,7 +131,9 @@ struct sc_security_env {
int key_ref; int key_ref;
}; };
/* ASN.1 parsing functions */ /* ASN.1 functions */
/* DER tag and length parsing */
const u8 *sc_asn1_find_tag(const u8 * buf, int buflen, int tag, int *taglen); const u8 *sc_asn1_find_tag(const u8 * buf, int buflen, int tag, int *taglen);
const u8 *sc_asn1_verify_tag(const u8 * buf, int buflen, int tag, int *taglen); const u8 *sc_asn1_verify_tag(const u8 * buf, int buflen, int tag, int *taglen);
const u8 *sc_asn1_skip_tag(const u8 ** buf, int *buflen, int tag, int *taglen); const u8 *sc_asn1_skip_tag(const u8 ** buf, int *buflen, int tag, int *taglen);
@ -145,7 +148,7 @@ int sc_asn1_decode_bit_string(const u8 * inbuf,
int inlen, void *outbuf, int outlen); int inlen, void *outbuf, int outlen);
/* non-inverting version */ /* non-inverting version */
int sc_asn1_decode_bit_string_ni(const u8 * inbuf, int sc_asn1_decode_bit_string_ni(const u8 * inbuf,
int inlen, void *outbuf, int outlen); int inlen, void *outbuf, int outlen);
int sc_asn1_decode_integer(const u8 * inbuf, int inlen, int *out); int sc_asn1_decode_integer(const u8 * inbuf, int inlen, int *out);
int sc_asn1_decode_object_id(const u8 * inbuf, int inlen, int sc_asn1_decode_object_id(const u8 * inbuf, int inlen,
struct sc_object_id *id); struct sc_object_id *id);
@ -172,11 +175,14 @@ int sc_wait_for_card(struct sc_context *ctx, int reader, int timeout);
int sc_lock(struct sc_card *card); int sc_lock(struct sc_card *card);
int sc_unlock(struct sc_card *card); int sc_unlock(struct sc_card *card);
/* ISO 7816-4 related functions */
int sc_select_file(struct sc_card *card, int sc_select_file(struct sc_card *card,
struct sc_file *file, struct sc_file *file,
const struct sc_path *path, int pathtype); const struct sc_path *path, int pathtype);
int sc_read_binary(struct sc_card *card, int idx, u8 * buf, int count); int sc_read_binary(struct sc_card *card, int idx, u8 * buf, int count);
int sc_get_random(struct sc_card *card, u8 * rndout, int len);
/* ISO 7816-9 (?) related functions */
int sc_restore_security_env(struct sc_card *card, int se_num); int sc_restore_security_env(struct sc_card *card, int se_num);
int sc_set_security_env(struct sc_card *card, int sc_set_security_env(struct sc_card *card,
const struct sc_security_env *env); const struct sc_security_env *env);
@ -185,10 +191,11 @@ int sc_decipher(struct sc_card *card,
int sc_compute_signature(struct sc_card *card, int sc_compute_signature(struct sc_card *card,
const u8 * data, const u8 * data,
int data_len, u8 * out, int outlen); int data_len, u8 * out, int outlen);
int sc_get_random(struct sc_card *card, u8 * rndout, int len);
const char *sc_strerror(int error); const char *sc_strerror(int error);
int sc_debug; int sc_debug;
const char *sc_version;
#endif #endif

View File

@ -23,6 +23,8 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
const char *sc_version = LIBSC_VERSION;
int sc_debug = 0; int sc_debug = 0;
static int convert_sw_to_errorcode(u8 * sw) static int convert_sw_to_errorcode(u8 * sw)
@ -503,7 +505,7 @@ int sc_establish_context(struct sc_context **ctx_out)
DWORD reader_buf_size; DWORD reader_buf_size;
char *reader_buf, *p; char *reader_buf, *p;
LPCSTR mszGroups; LPCSTR mszGroups;
int i, reader_count; int reader_count;
assert(ctx_out != NULL); assert(ctx_out != NULL);
ctx = malloc(sizeof(struct sc_context)); ctx = malloc(sizeof(struct sc_context));
@ -536,8 +538,6 @@ int sc_establish_context(struct sc_context **ctx_out)
break; break;
} while (p < (reader_buf + reader_buf_size - 1)); } while (p < (reader_buf + reader_buf_size - 1));
free(reader_buf); free(reader_buf);
for (i = 0; i < ctx->reader_count; i++)
printf("Found reader #%d - %s\n", i + 1, ctx->readers[i]);
*ctx_out = ctx; *ctx_out = ctx;
return 0; return 0;
@ -580,6 +580,7 @@ int sc_connect_card(struct sc_context *ctx,
card->pcsc_card = card_handle; card->pcsc_card = card_handle;
*card_out = card; *card_out = card;
card->class = 0; /* FIXME */ card->class = 0; /* FIXME */
card->reader = ctx->readers[reader];
return 0; return 0;
} }
@ -587,7 +588,7 @@ int sc_connect_card(struct sc_context *ctx,
int sc_disconnect_card(struct sc_card *card) int sc_disconnect_card(struct sc_card *card)
{ {
assert(card != NULL); assert(card != NULL);
SCardDisconnect(card->pcsc_card, SCARD_UNPOWER_CARD); SCardDisconnect(card->pcsc_card, SCARD_LEAVE_CARD);
return 0; return 0;
} }

View File

@ -12,7 +12,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, c; int i, c, r;
int freq[39]; int freq[39];
struct timeval tv1, tv2; struct timeval tv1, tv2;
u8 buf[14]; u8 buf[14];
@ -30,7 +30,10 @@ int main(int argc, char *argv[])
} }
if (c == 0) if (c == 0)
gettimeofday(&tv1, NULL); gettimeofday(&tv1, NULL);
if (sc_get_random(card, buf, 14) == 0) { sc_lock(card);
r = sc_get_random(card, buf, 14);
sc_unlock(card);
if (r == 0) {
int i, jaljella = 39; int i, jaljella = 39;
printf("Lottorivi: "); printf("Lottorivi: ");
@ -47,6 +50,10 @@ int main(int argc, char *argv[])
printf("%3d ", num); printf("%3d ", num);
} }
printf("\n"); printf("\n");
} else {
fprintf(stderr, "get_random() failed: %s\n", sc_strerror(r));
sc_test_cleanup();
return 1;
} }
c++; c++;
if (c == 50) { if (c == 50) {
@ -63,5 +70,6 @@ int main(int argc, char *argv[])
c = 0; c = 0;
} }
} }
sc_test_cleanup();
return 0;
} }

View File

@ -17,7 +17,9 @@ int enum_pins()
{ {
int i, c; int i, c;
sc_lock(card);
c = sc_pkcs15_enum_pins(p15card); c = sc_pkcs15_enum_pins(p15card);
sc_unlock(card);
if (c < 0) { if (c < 0) {
fprintf(stderr, "Error enumerating PIN codes: %s\n", fprintf(stderr, "Error enumerating PIN codes: %s\n",
sc_strerror(i)); sc_strerror(i));
@ -40,22 +42,28 @@ int main(int argc, char *argv[])
return 1; return 1;
printf("Looking for a PKCS#15 compatible Smart Card... "); printf("Looking for a PKCS#15 compatible Smart Card... ");
fflush(stdout); fflush(stdout);
sc_lock(card);
i = sc_pkcs15_init(card, &p15card); i = sc_pkcs15_init(card, &p15card);
sc_unlock(card);
if (i) { if (i) {
fprintf(stderr, "failed: %s\n", sc_strerror(i)); fprintf(stderr, "failed: %s\n", sc_strerror(i));
return 1; return 1;
} }
printf("found.\n"); printf("found.\n");
sc_pkcs15_print_card(p15card); sc_pkcs15_print_card(p15card);
printf("Enumerating PIN codes...\n"); printf("Enumerating PIN codes...\n");
sc_lock(card);
i = enum_pins(); i = enum_pins();
sc_unlock(card);
if (i) if (i)
return 1; return 1;
printf("Enumerating private keys... "); printf("Enumerating private keys... ");
fflush(stdout); fflush(stdout);
sc_lock(card);
i = sc_pkcs15_enum_private_keys(p15card); i = sc_pkcs15_enum_private_keys(p15card);
sc_unlock(card);
if (i < 0) { if (i < 0) {
fprintf(stderr, "failed: %s\n", sc_strerror(i)); fprintf(stderr, "failed: %s\n", sc_strerror(i));
return 1; return 1;
@ -67,7 +75,9 @@ int main(int argc, char *argv[])
printf("Enumerating certificates... "); printf("Enumerating certificates... ");
fflush(stdout); fflush(stdout);
sc_lock(card);
i = sc_pkcs15_enum_certificates(p15card); i = sc_pkcs15_enum_certificates(p15card);
sc_unlock(card);
if (i < 0) { if (i < 0) {
fprintf(stderr, "failed: %s\n", sc_strerror(i)); fprintf(stderr, "failed: %s\n", sc_strerror(i));
return 1; return 1;
@ -77,5 +87,18 @@ int main(int argc, char *argv[])
sc_pkcs15_print_cert_info(&p15card->cert_info[c]); sc_pkcs15_print_cert_info(&p15card->cert_info[c]);
} }
for (c = 0; c < p15card->cert_count; c++) {
struct sc_pkcs15_cert *cert;
printf("Reading %s... ", p15card->cert_info[c].com_attr.label);
fflush(stdout);
i = sc_pkcs15_read_certificate(p15card, &p15card->cert_info[c], &cert);
if (i) {
fprintf(stderr, "failed: %s\n", sc_strerror(i));
return 1;
}
printf("\n");
sc_asn1_print_tags(cert->data, cert->data_len);
}
return 0; return 0;
} }

View File

@ -40,7 +40,9 @@ int ask_and_verify_pin(struct sc_pkcs15_pin_info *pin)
i = sc_sec_ask_pin_code(pin, buf, sizeof(buf), i = sc_sec_ask_pin_code(pin, buf, sizeof(buf),
"Please enter PIN code"); "Please enter PIN code");
if (i == 0) { if (i == 0) {
sc_lock(card);
i = sc_pkcs15_verify_pin(p15card, pin, buf, strlen(buf)); i = sc_pkcs15_verify_pin(p15card, pin, buf, strlen(buf));
sc_unlock(card);
if (i) { if (i) {
if (i == SC_ERROR_PIN_CODE_INCORRECT) if (i == SC_ERROR_PIN_CODE_INCORRECT)
fprintf(stderr, fprintf(stderr,
@ -68,18 +70,23 @@ int main(int argc, char *argv[])
return 1; return 1;
printf("Looking for a PKCS#15 compatible Smart Card... "); printf("Looking for a PKCS#15 compatible Smart Card... ");
fflush(stdout); fflush(stdout);
sc_lock(card);
i = sc_pkcs15_init(card, &p15card); i = sc_pkcs15_init(card, &p15card);
sc_unlock(card);
if (i) { if (i) {
fprintf(stderr, "failed: %s\n", sc_strerror(i)); fprintf(stderr, "failed: %s\n", sc_strerror(i));
return 1; return 1;
} }
printf("found.\n"); printf("found.\n");
printf("Enumerating PIN codes...\n"); printf("Enumerating PIN codes...\n");
sc_lock(card);
i = enum_pins(); i = enum_pins();
sc_unlock(card);
if (i) if (i)
return 1; return 1;
for (c = 0; c < p15card->pin_count; c++) { for (c = 0; c < p15card->pin_count; c++) {
ask_and_verify_pin(&p15card->pin_info[c]); ask_and_verify_pin(&p15card->pin_info[c]);
} }
sc_test_cleanup();
return 0; return 0;
} }

View File

@ -16,6 +16,7 @@ int sc_test_init(int *argc, char *argv[])
{ {
int i, c; int i, c;
printf("Using libsc version %s.\n", sc_version);
i = sc_establish_context(&ctx); i = sc_establish_context(&ctx);
if (i < 0) { if (i < 0) {
printf("sc_establish_context() failed (%d)\n", i); printf("sc_establish_context() failed (%d)\n", i);
@ -30,6 +31,7 @@ int sc_test_init(int *argc, char *argv[])
printf("Please insert a smart card."); printf("Please insert a smart card.");
fflush(stdout); fflush(stdout);
i = sc_wait_for_card(ctx, -1, -1); i = sc_wait_for_card(ctx, -1, -1);
printf("\n");
if (i < 0) if (i < 0)
return i; return i;
if (i != 1) if (i != 1)
@ -37,11 +39,11 @@ int sc_test_init(int *argc, char *argv[])
c = -1; c = -1;
for (i = 0; i < ctx->reader_count; i++) { for (i = 0; i < ctx->reader_count; i++) {
if (sc_detect_card(ctx, i) == 1) { if (sc_detect_card(ctx, i) == 1) {
printf("Card detected in reader '%s'\n", ctx->readers[i]);
c = i; c = i;
break; break;
} }
} }
printf("\n");
} else } else
c = 0; c = 0;
printf("Connecting... "); printf("Connecting... ");