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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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);
|
|
|
|
|
|
|
|
|
|
objs = calloc(count, sizeof(*objs));
|
|
|
|
|
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);
|
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");
|
|
|
|
|
sc_pkcs15_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);
|
|
|
|
|
dump_objects("RSA private keys", SC_PKCS15_TYPE_PRKEY_RSA);
|
2002-03-07 13:03:23 +00:00
|
|
|
|
dump_objects("RSA public keys", SC_PKCS15_TYPE_PUBKEY_RSA);
|
2002-03-04 08:35:48 +00:00
|
|
|
|
dump_objects("X.509 certificates", SC_PKCS15_TYPE_CERT_X509);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2002-03-22 00:13:25 +00:00
|
|
|
|
sc_pkcs15_unbind(p15card);
|
2001-11-17 15:48:10 +00:00
|
|
|
|
sc_test_cleanup();
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|