2001-10-19 17:26:04 +00:00
|
|
|
|
|
|
|
|
|
/* Copyright (C) 2001 Juha Yrj<72>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*/
|
2001-10-20 16:54:54 +00:00
|
|
|
|
|
2001-10-19 17:26:04 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "sc.h"
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2001-10-19 23:23:16 +00:00
|
|
|
|
struct sc_context *ctx = NULL;
|
|
|
|
|
struct sc_card *card = NULL;
|
2001-10-20 16:54:54 +00:00
|
|
|
|
struct sc_pkcs15_card *p15_card = NULL;
|
|
|
|
|
struct sc_pkcs15_pin_object pin;
|
|
|
|
|
char buf[16], buf2[16];
|
|
|
|
|
|
|
|
|
|
int i,c ;
|
2001-10-19 17:26:04 +00:00
|
|
|
|
|
2001-10-19 23:23:16 +00:00
|
|
|
|
i = sc_establish_context(&ctx);
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
printf("sc_establish_context() failed (%d)\n", i);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
i = sc_detect_card(ctx, 0);
|
2001-10-20 16:54:54 +00:00
|
|
|
|
printf("Card %s.\n", i == 1 ? "present" : "absent");
|
|
|
|
|
if (i < 0) {
|
2001-10-19 17:26:04 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2001-10-20 16:54:54 +00:00
|
|
|
|
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");
|
|
|
|
|
}
|
2001-10-19 17:26:04 +00:00
|
|
|
|
printf("Connecting... ");
|
|
|
|
|
fflush(stdout);
|
2001-10-19 23:23:16 +00:00
|
|
|
|
i = sc_connect_card(ctx, 0, &card);
|
|
|
|
|
if (i != 0) {
|
|
|
|
|
printf("Connecting to card failed\n");
|
2001-10-19 17:26:04 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
printf("done.\n");
|
2001-10-19 23:23:16 +00:00
|
|
|
|
fflush(stdout);
|
2001-10-19 17:26:04 +00:00
|
|
|
|
|
2001-10-19 23:23:16 +00:00
|
|
|
|
i = sc_pkcs15_init(card, &p15_card);
|
2001-10-19 17:26:04 +00:00
|
|
|
|
if (i != 0) {
|
2001-10-20 16:54:54 +00:00
|
|
|
|
fprintf(stderr, "PKCS#15 card init failed: %s\n", sc_strerror(i));
|
2001-10-19 17:26:04 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2001-10-20 16:54:54 +00:00
|
|
|
|
|
|
|
|
|
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");
|
2001-10-19 17:26:04 +00:00
|
|
|
|
if (i) {
|
2001-10-20 16:54:54 +00:00
|
|
|
|
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");
|
2001-10-19 17:26:04 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2001-10-20 16:54:54 +00:00
|
|
|
|
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));
|
2001-10-19 17:26:04 +00:00
|
|
|
|
if (i) {
|
2001-10-20 16:54:54 +00:00
|
|
|
|
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));
|
2001-10-19 17:26:04 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2001-10-20 16:54:54 +00:00
|
|
|
|
printf("PIN code correct.\n");
|
|
|
|
|
|
2001-10-19 23:23:16 +00:00
|
|
|
|
printf("Cleaning up...\n");
|
2001-10-20 16:54:54 +00:00
|
|
|
|
i = sc_pkcs15_destroy(p15_card);
|
2001-10-19 23:23:16 +00:00
|
|
|
|
sc_disconnect_card(card);
|
|
|
|
|
sc_destroy_context(ctx);
|
2001-10-20 16:54:54 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
2001-10-19 17:26:04 +00:00
|
|
|
|
|
2001-10-20 16:54:54 +00:00
|
|
|
|
i = sc_pkcs15_read_certificate(p15_card, 0);
|
|
|
|
|
if (i) {
|
|
|
|
|
fprintf(stderr, "Certificate read failed with %d\n", i);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2001-10-19 17:26:04 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|