2001-11-01 15:43:20 +00:00
|
|
|
|
/* Copyright (C) 2001 Juha Yrj<72>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* PKCS#15 PIN code test
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "sc-test.h"
|
2001-11-24 13:32:52 +00:00
|
|
|
|
#include "opensc.h"
|
|
|
|
|
#include "opensc-pkcs15.h"
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2001-11-20 22:21:58 +00:00
|
|
|
|
#include <unistd.h>
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
struct sc_pkcs15_card *p15card;
|
|
|
|
|
|
2002-02-25 12:04:39 +00:00
|
|
|
|
int enum_pins(void)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
|
|
|
|
int i, c;
|
|
|
|
|
|
|
|
|
|
c = sc_pkcs15_enum_pins(p15card);
|
|
|
|
|
if (c < 0) {
|
|
|
|
|
fprintf(stderr, "Error enumerating PIN codes: %s\n",
|
2001-12-28 14:19:41 +00:00
|
|
|
|
sc_strerror(c));
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (c == 0)
|
|
|
|
|
fprintf(stderr, "No PIN codes found!\n");
|
|
|
|
|
for (i = 0; i < c; i++) {
|
|
|
|
|
sc_pkcs15_print_pin_info(&p15card->pin_info[i]);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ask_and_verify_pin(struct sc_pkcs15_pin_info *pin)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
char prompt[80];
|
2001-12-30 21:17:34 +00:00
|
|
|
|
u8 *pass;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
while (1) {
|
|
|
|
|
sprintf(prompt, "Please enter PIN code [%s]: ", pin->com_attr.label);
|
2001-12-30 21:17:34 +00:00
|
|
|
|
pass = (u8 *) getpass(prompt);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
|
2001-12-30 21:17:34 +00:00
|
|
|
|
if (strlen((char *) pass) == 0) {
|
2001-11-20 22:21:58 +00:00
|
|
|
|
printf("Not verifying PIN code.\n");
|
|
|
|
|
return -1;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-12-30 21:17:34 +00:00
|
|
|
|
if (strlen((char *) pass) < pin->min_length)
|
2001-11-20 22:21:58 +00:00
|
|
|
|
break;
|
2001-12-30 21:17:34 +00:00
|
|
|
|
if (strlen((char *) pass) > pin->stored_length)
|
2001-11-20 22:21:58 +00:00
|
|
|
|
break;
|
|
|
|
|
break;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-11-20 22:21:58 +00:00
|
|
|
|
|
|
|
|
|
sc_lock(card);
|
2001-12-30 21:17:34 +00:00
|
|
|
|
i = sc_pkcs15_verify_pin(p15card, pin, pass, strlen((char *) pass));
|
2001-11-20 22:21:58 +00:00
|
|
|
|
sc_unlock(card);
|
|
|
|
|
if (i) {
|
|
|
|
|
if (i == SC_ERROR_PIN_CODE_INCORRECT)
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"Incorrect PIN code (%d tries left)\n",
|
|
|
|
|
pin->tries_left);
|
|
|
|
|
else
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"PIN verifying failed: %s\n",
|
|
|
|
|
sc_strerror(i));
|
|
|
|
|
return 1;
|
|
|
|
|
} else
|
|
|
|
|
printf("PIN code correct.\n");
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int i, c;
|
|
|
|
|
|
|
|
|
|
i = sc_test_init(&argc, argv);
|
|
|
|
|
if (i < 0)
|
|
|
|
|
return 1;
|
|
|
|
|
printf("Looking for a PKCS#15 compatible Smart Card... ");
|
|
|
|
|
fflush(stdout);
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_lock(card);
|
2001-12-22 20:52:57 +00:00
|
|
|
|
i = sc_pkcs15_bind(card, &p15card);
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_unlock(card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (i) {
|
|
|
|
|
fprintf(stderr, "failed: %s\n", sc_strerror(i));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
printf("found.\n");
|
|
|
|
|
printf("Enumerating PIN codes...\n");
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_lock(card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
i = enum_pins();
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_unlock(card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (i)
|
|
|
|
|
return 1;
|
|
|
|
|
for (c = 0; c < p15card->pin_count; c++) {
|
|
|
|
|
ask_and_verify_pin(&p15card->pin_info[c]);
|
|
|
|
|
}
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_test_cleanup();
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|