From 9476addfa07d4a35fdfca7fb538279961e490d4f Mon Sep 17 00:00:00 2001 From: jey Date: Sat, 20 Oct 2001 16:54:54 +0000 Subject: [PATCH] lots and lots of changes. git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@7 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/tests/hst-test.c | 74 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/src/tests/hst-test.c b/src/tests/hst-test.c index bdce65b6..b6b3c5c7 100644 --- a/src/tests/hst-test.c +++ b/src/tests/hst-test.c @@ -2,7 +2,7 @@ /* Copyright (C) 2001 Juha Yrjölä * All rights reserved. */ - + #include #include #include @@ -11,8 +11,11 @@ int main(int argc, char **argv) { struct sc_context *ctx = NULL; struct sc_card *card = NULL; - struct sc_pkcs15_card p15_card; - int i; + struct sc_pkcs15_card *p15_card = NULL; + struct sc_pkcs15_pin_object pin; + char buf[16], buf2[16]; + + int i,c ; i = sc_establish_context(&ctx); if (i < 0) { @@ -20,10 +23,18 @@ int main(int argc, char **argv) { return 1; } i = sc_detect_card(ctx, 0); - fprintf(stderr, "Card %s.\n", i == 1 ? "present" : "absent"); - if (i != 1) { + printf("Card %s.\n", i == 1 ? "present" : "absent"); + if (i < 0) { return 1; } + if (i == 0) { + printf("Please insert a smart card."); + fflush(stdout); + i = sc_wait_for_card(ctx, 0, -1); + if (i != 1) + return 1; + printf("\n"); + } printf("Connecting... "); fflush(stdout); i = sc_connect_card(ctx, 0, &card); @@ -36,23 +47,54 @@ int main(int argc, char **argv) { i = sc_pkcs15_init(card, &p15_card); if (i != 0) { - fprintf(stderr, "PKCS#15 card init failed with %d\n", i); + fprintf(stderr, "PKCS#15 card init failed: %s\n", sc_strerror(i)); return 1; } + + printf("Searching for PIN codes...\n"); + + c = 0; + while (sc_pkcs15_read_pin_object(p15_card, ++c, &pin) == 0) { + sc_pkcs15_print_pin_object(&pin); + } + c--; + if (c == 0) { + printf("No PIN codes found!\n"); + return 1; + } + + i = sc_sec_ask_pin_code(&p15_card->pins[0], buf, sizeof(buf), "Please enter PIN code"); + if (i) { + fprintf(stderr, "\nFailed to ask PIN code from user\n"); + return 1; + } + i = sc_sec_ask_pin_code(&p15_card->pins[0], buf2, sizeof(buf2), "Please enter _new_ PIN code"); + if (i) { + fprintf(stderr, "\nFailed to ask PIN code from user\n"); + return 1; + } + i = sc_pkcs15_change_pin(p15_card, &p15_card->pins[0], buf, strlen(buf), buf2, strlen(buf2)); +// i = sc_pkcs15_verify_pin(p15_card, &p15_card->pins[0], buf, strlen(buf)); + if (i) { + if (i == SC_ERROR_PIN_CODE_INCORRECT) + fprintf(stderr, "Incorrect PIN code (%d tries left)\n", p15_card->pins[0].tries_left); + else + fprintf(stderr, "PIN verifying failed: %s\n", sc_strerror(i)); + return 1; + } + printf("PIN code correct.\n"); - i = sc_pkcs15_read_certificate(&p15_card, 0); + printf("Cleaning up...\n"); + i = sc_pkcs15_destroy(p15_card); + sc_disconnect_card(card); + sc_destroy_context(ctx); + + return 0; + + i = sc_pkcs15_read_certificate(p15_card, 0); if (i) { fprintf(stderr, "Certificate read failed with %d\n", i); return 1; } - i = sc_pkcs15_read_pin_object(&p15_card, 1, NULL); - if (i) { - fprintf(stderr, "PIN object read failed with %d\n", i); - return 1; - } - printf("Cleaning up...\n"); - sc_disconnect_card(card); - sc_destroy_context(ctx); - return 0; }