lots and lots of changes.

git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@7 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
jey 2001-10-20 16:54:54 +00:00
parent b5d584e5a7
commit 9476addfa0
1 changed files with 58 additions and 16 deletions

View File

@ -2,7 +2,7 @@
/* Copyright (C) 2001 Juha Yrjölä <juha.yrjola@iki.fi>
* All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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;
}