diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h index 6a437887..04d68666 100644 --- a/src/libopensc/opensc.h +++ b/src/libopensc/opensc.h @@ -103,6 +103,7 @@ struct sc_card { int class; struct sc_context *context; SCARDHANDLE pcsc_card; + const char *reader; }; struct sc_context { @@ -130,7 +131,9 @@ struct sc_security_env { 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_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); @@ -145,7 +148,7 @@ int sc_asn1_decode_bit_string(const u8 * inbuf, int inlen, void *outbuf, int outlen); /* non-inverting version */ 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_object_id(const u8 * inbuf, int inlen, 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_unlock(struct sc_card *card); +/* ISO 7816-4 related functions */ int sc_select_file(struct sc_card *card, struct sc_file *file, const struct sc_path *path, int pathtype); 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_set_security_env(struct sc_card *card, 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, const u8 * data, 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); int sc_debug; +const char *sc_version; + #endif diff --git a/src/libopensc/sc.c b/src/libopensc/sc.c index 8dac74b2..f8baeaca 100644 --- a/src/libopensc/sc.c +++ b/src/libopensc/sc.c @@ -23,6 +23,8 @@ #include #include +const char *sc_version = LIBSC_VERSION; + int sc_debug = 0; 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; char *reader_buf, *p; LPCSTR mszGroups; - int i, reader_count; + int reader_count; assert(ctx_out != NULL); ctx = malloc(sizeof(struct sc_context)); @@ -536,8 +538,6 @@ int sc_establish_context(struct sc_context **ctx_out) break; } while (p < (reader_buf + reader_buf_size - 1)); 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; return 0; @@ -580,6 +580,7 @@ int sc_connect_card(struct sc_context *ctx, card->pcsc_card = card_handle; *card_out = card; card->class = 0; /* FIXME */ + card->reader = ctx->readers[reader]; return 0; } @@ -587,7 +588,7 @@ int sc_connect_card(struct sc_context *ctx, int sc_disconnect_card(struct sc_card *card) { assert(card != NULL); - SCardDisconnect(card->pcsc_card, SCARD_UNPOWER_CARD); + SCardDisconnect(card->pcsc_card, SCARD_LEAVE_CARD); return 0; } diff --git a/src/tests/lottery.c b/src/tests/lottery.c index 23b5d224..4b72788e 100644 --- a/src/tests/lottery.c +++ b/src/tests/lottery.c @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) { - int i, c; + int i, c, r; int freq[39]; struct timeval tv1, tv2; u8 buf[14]; @@ -30,7 +30,10 @@ int main(int argc, char *argv[]) } if (c == 0) 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; printf("Lottorivi: "); @@ -47,6 +50,10 @@ int main(int argc, char *argv[]) printf("%3d ", num); } printf("\n"); + } else { + fprintf(stderr, "get_random() failed: %s\n", sc_strerror(r)); + sc_test_cleanup(); + return 1; } c++; if (c == 50) { @@ -63,5 +70,6 @@ int main(int argc, char *argv[]) c = 0; } } - + sc_test_cleanup(); + return 0; } diff --git a/src/tests/p15dump.c b/src/tests/p15dump.c index f47fe3b6..4b4ce42c 100644 --- a/src/tests/p15dump.c +++ b/src/tests/p15dump.c @@ -17,7 +17,9 @@ int enum_pins() { int i, c; + sc_lock(card); c = sc_pkcs15_enum_pins(p15card); + sc_unlock(card); if (c < 0) { fprintf(stderr, "Error enumerating PIN codes: %s\n", sc_strerror(i)); @@ -40,22 +42,28 @@ int main(int argc, char *argv[]) return 1; printf("Looking for a PKCS#15 compatible Smart Card... "); fflush(stdout); + sc_lock(card); i = sc_pkcs15_init(card, &p15card); + sc_unlock(card); if (i) { fprintf(stderr, "failed: %s\n", sc_strerror(i)); return 1; } printf("found.\n"); sc_pkcs15_print_card(p15card); - + printf("Enumerating PIN codes...\n"); + sc_lock(card); i = enum_pins(); + sc_unlock(card); if (i) return 1; printf("Enumerating private keys... "); fflush(stdout); + sc_lock(card); i = sc_pkcs15_enum_private_keys(p15card); + sc_unlock(card); if (i < 0) { fprintf(stderr, "failed: %s\n", sc_strerror(i)); return 1; @@ -67,7 +75,9 @@ int main(int argc, char *argv[]) printf("Enumerating certificates... "); fflush(stdout); + sc_lock(card); i = sc_pkcs15_enum_certificates(p15card); + sc_unlock(card); if (i < 0) { fprintf(stderr, "failed: %s\n", sc_strerror(i)); return 1; @@ -77,5 +87,18 @@ int main(int argc, char *argv[]) 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; } diff --git a/src/tests/pintest.c b/src/tests/pintest.c index a0c5a99a..0c467405 100644 --- a/src/tests/pintest.c +++ b/src/tests/pintest.c @@ -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), "Please enter PIN code"); if (i == 0) { + sc_lock(card); i = sc_pkcs15_verify_pin(p15card, pin, buf, strlen(buf)); + sc_unlock(card); if (i) { if (i == SC_ERROR_PIN_CODE_INCORRECT) fprintf(stderr, @@ -68,18 +70,23 @@ int main(int argc, char *argv[]) return 1; printf("Looking for a PKCS#15 compatible Smart Card... "); fflush(stdout); + sc_lock(card); i = sc_pkcs15_init(card, &p15card); + sc_unlock(card); if (i) { fprintf(stderr, "failed: %s\n", sc_strerror(i)); return 1; } printf("found.\n"); printf("Enumerating PIN codes...\n"); + sc_lock(card); i = enum_pins(); + sc_unlock(card); if (i) return 1; for (c = 0; c < p15card->pin_count; c++) { ask_and_verify_pin(&p15card->pin_info[c]); } + sc_test_cleanup(); return 0; } diff --git a/src/tests/sc-test.c b/src/tests/sc-test.c index fd67a352..f19276f0 100644 --- a/src/tests/sc-test.c +++ b/src/tests/sc-test.c @@ -16,6 +16,7 @@ int sc_test_init(int *argc, char *argv[]) { int i, c; + printf("Using libsc version %s.\n", sc_version); i = sc_establish_context(&ctx); if (i < 0) { 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."); fflush(stdout); i = sc_wait_for_card(ctx, -1, -1); + printf("\n"); if (i < 0) return i; if (i != 1) @@ -37,11 +39,11 @@ int sc_test_init(int *argc, char *argv[]) c = -1; for (i = 0; i < ctx->reader_count; i++) { if (sc_detect_card(ctx, i) == 1) { + printf("Card detected in reader '%s'\n", ctx->readers[i]); c = i; break; } } - printf("\n"); } else c = 0; printf("Connecting... ");