2001-11-01 15:43:20 +00:00
|
|
|
|
/* Copyright (C) 2001 Juha Yrj<72>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
2002-03-22 00:13:25 +00:00
|
|
|
|
* PKCS#15 objects test
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*/
|
|
|
|
|
|
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-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-22 00:13:25 +00:00
|
|
|
|
static int dump_objects(const char *what, int type)
|
2002-03-04 08:35:48 +00:00
|
|
|
|
{
|
2002-03-22 00:13:25 +00:00
|
|
|
|
struct sc_pkcs15_object **objs;
|
|
|
|
|
int count, i;
|
2002-03-04 08:35:48 +00:00
|
|
|
|
|
|
|
|
|
printf("\nEnumerating %s... ", what);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
sc_lock(card);
|
|
|
|
|
count = sc_pkcs15_get_objects(p15card, type, NULL, 0);
|
|
|
|
|
if (count < 0) {
|
|
|
|
|
printf("failed.\n");
|
|
|
|
|
fprintf(stderr, "Error enumerating %s: %s\n",
|
|
|
|
|
what, sc_strerror(count));
|
|
|
|
|
sc_unlock(card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2002-03-04 08:35:48 +00:00
|
|
|
|
if (count == 0) {
|
|
|
|
|
printf("none found.\n");
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
return 0;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2002-03-04 08:35:48 +00:00
|
|
|
|
printf("%u found.\n", count);
|
|
|
|
|
|
2002-04-19 14:23:31 +00:00
|
|
|
|
objs = (struct sc_pkcs15_object **) calloc(count, sizeof(*objs));
|
2002-03-04 08:35:48 +00:00
|
|
|
|
if ((count = sc_pkcs15_get_objects(p15card, type, objs, count)) < 0) {
|
|
|
|
|
fprintf(stderr, "Error enumerating %s: %s\n",
|
|
|
|
|
what, sc_strerror(count));
|
|
|
|
|
} else {
|
|
|
|
|
for (i = 0; i < count; i++)
|
2002-03-07 13:03:23 +00:00
|
|
|
|
sc_test_print_object(objs[i]);
|
2002-03-04 08:35:48 +00:00
|
|
|
|
}
|
|
|
|
|
free(objs);
|
|
|
|
|
sc_unlock(card);
|
2002-03-22 00:13:25 +00:00
|
|
|
|
return (count < 0) ? 1 : 0;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-22 00:13:25 +00:00
|
|
|
|
int main(int argc, char *argv[])
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2002-03-04 08:35:48 +00:00
|
|
|
|
int i;
|
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);
|
2003-06-10 06:32:31 +00:00
|
|
|
|
/* Keep card locked to prevent useless calls to sc_logout */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (i) {
|
|
|
|
|
fprintf(stderr, "failed: %s\n", sc_strerror(i));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
printf("found.\n");
|
2003-10-31 16:02:42 +00:00
|
|
|
|
sc_test_print_card(p15card);
|
2001-11-04 14:08:38 +00:00
|
|
|
|
|
2002-03-04 08:35:48 +00:00
|
|
|
|
dump_objects("PIN codes", SC_PKCS15_TYPE_AUTH_PIN);
|
2002-04-17 20:46:56 +00:00
|
|
|
|
dump_objects("Private keys", SC_PKCS15_TYPE_PRKEY);
|
|
|
|
|
dump_objects("Public keys", SC_PKCS15_TYPE_PUBKEY);
|
2002-03-04 08:35:48 +00:00
|
|
|
|
dump_objects("X.509 certificates", SC_PKCS15_TYPE_CERT_X509);
|
2002-12-18 10:17:01 +00:00
|
|
|
|
dump_objects("data objects", SC_PKCS15_TYPE_DATA_OBJECT);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2002-03-22 00:13:25 +00:00
|
|
|
|
sc_pkcs15_unbind(p15card);
|
2003-06-10 06:32:31 +00:00
|
|
|
|
sc_unlock(card);
|
2001-11-17 15:48:10 +00:00
|
|
|
|
sc_test_cleanup();
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|