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
|
|
|
|
|
*/
|
|
|
|
|
|
2002-10-19 16:51:37 +00:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2002-03-21 09:36:38 +00:00
|
|
|
|
#include <string.h>
|
2002-10-19 16:51:37 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
2002-04-05 13:48:00 +00:00
|
|
|
|
#include <opensc/opensc.h>
|
|
|
|
|
#include <opensc/pkcs15.h>
|
2002-03-22 00:13:25 +00:00
|
|
|
|
#include "sc-test.h"
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
struct sc_pkcs15_card *p15card;
|
|
|
|
|
|
2002-03-07 13:02:30 +00:00
|
|
|
|
int enum_pins(struct sc_pkcs15_object ***ret)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2002-03-07 13:02:30 +00:00
|
|
|
|
struct sc_pkcs15_object **objs;
|
|
|
|
|
int i, n;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2002-03-07 13:02:30 +00:00
|
|
|
|
n = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, NULL, 0);
|
|
|
|
|
if (n < 0) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
fprintf(stderr, "Error enumerating PIN codes: %s\n",
|
2002-03-07 13:02:30 +00:00
|
|
|
|
sc_strerror(n));
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2002-03-07 13:02:30 +00:00
|
|
|
|
if (n == 0) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
fprintf(stderr, "No PIN codes found!\n");
|
2002-03-07 13:02:30 +00:00
|
|
|
|
return 0;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2002-03-07 13:02:30 +00:00
|
|
|
|
objs = (struct sc_pkcs15_object **) calloc(n, sizeof(*objs));
|
|
|
|
|
sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, objs, n);
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
|
sc_test_print_object(objs[i]);
|
|
|
|
|
}
|
|
|
|
|
*ret = objs;
|
|
|
|
|
return n;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-07 13:02:30 +00:00
|
|
|
|
int ask_and_verify_pin(struct sc_pkcs15_object *obj)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2002-03-07 13:02:30 +00:00
|
|
|
|
struct sc_pkcs15_pin_info *pin;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int i = 0;
|
2002-03-22 00:13:25 +00:00
|
|
|
|
char prompt[80];
|
|
|
|
|
u8 *pass;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2002-03-07 13:02:30 +00:00
|
|
|
|
pin = (struct sc_pkcs15_pin_info *) obj->data;
|
2003-11-07 11:15:27 +00:00
|
|
|
|
if (pin->flags & SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN) {
|
|
|
|
|
printf("Skipping unblocking pin [%s]\n", obj->label);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
while (1) {
|
2002-03-07 13:02:30 +00:00
|
|
|
|
sprintf(prompt, "Please enter PIN code [%s]: ", obj->label);
|
2002-03-22 00:13:25 +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;
|
2003-02-20 23:19:01 +00:00
|
|
|
|
if (strlen((char *) pass) > pin->max_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
|
|
|
|
|
2002-03-22 00:13:25 +00:00
|
|
|
|
sc_lock(card);
|
|
|
|
|
i = sc_pkcs15_verify_pin(p15card, pin, pass, strlen((char *) pass));
|
|
|
|
|
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
|
2001-11-20 22:21:58 +00:00
|
|
|
|
printf("PIN code correct.\n");
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
2002-03-22 00:13:25 +00:00
|
|
|
|
struct sc_pkcs15_object **objs;
|
|
|
|
|
int i, count;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
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));
|
2002-03-22 00:13:25 +00:00
|
|
|
|
sc_test_cleanup();
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
printf("found.\n");
|
|
|
|
|
printf("Enumerating PIN codes...\n");
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_lock(card);
|
2002-03-07 13:02:30 +00:00
|
|
|
|
count = enum_pins(&objs);
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_unlock(card);
|
2002-03-22 00:13:25 +00:00
|
|
|
|
if (count < 0) {
|
|
|
|
|
sc_pkcs15_unbind(p15card);
|
|
|
|
|
sc_test_cleanup();
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 1;
|
2002-03-22 00:13:25 +00:00
|
|
|
|
}
|
2002-03-07 13:02:30 +00:00
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
ask_and_verify_pin(objs[i]);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2002-03-22 00:13:25 +00:00
|
|
|
|
sc_pkcs15_unbind(p15card);
|
2001-11-04 14:08:38 +00:00
|
|
|
|
sc_test_cleanup();
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|