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
|
|
|
|
}
|
|
|
|
|
|
2005-12-17 19:53:12 +00:00
|
|
|
|
static int dump_unusedspace()
|
|
|
|
|
{
|
|
|
|
|
u8 *buf = NULL;
|
|
|
|
|
size_t buf_len;
|
|
|
|
|
sc_path_t path;
|
|
|
|
|
sc_pkcs15_unusedspace_t *us;
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
if (p15card->file_unusedspace != NULL)
|
|
|
|
|
path = p15card->file_unusedspace->path;
|
|
|
|
|
else {
|
|
|
|
|
path = p15card->file_app->path;
|
|
|
|
|
sc_append_path_id(&path, (const u8 *) "\x50\x33", 2);
|
|
|
|
|
}
|
|
|
|
|
path.count = -1;
|
|
|
|
|
|
2005-12-21 21:19:37 +00:00
|
|
|
|
sc_ctx_suppress_errors_on(p15card->card->ctx);
|
2005-12-17 19:53:12 +00:00
|
|
|
|
r = sc_pkcs15_read_file(p15card, &path, &buf, &buf_len, NULL);
|
2005-12-21 21:19:37 +00:00
|
|
|
|
sc_ctx_suppress_errors_off(p15card->card->ctx);
|
2005-12-17 19:53:12 +00:00
|
|
|
|
if (r < 0) {
|
|
|
|
|
if (r == SC_ERROR_FILE_NOT_FOUND) {
|
|
|
|
|
printf("\nNo EF(UnusedSpace) file\n");
|
|
|
|
|
r = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
printf("\nError reading file \"%s\": %s\n",
|
|
|
|
|
sc_print_path(&path), sc_strerror(r));
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = sc_pkcs15_parse_unusedspace(buf, buf_len, p15card);
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
printf("\nError parsing EF(UnusedSpace): %s\n", sc_strerror(r));
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p15card->unusedspace_list == NULL)
|
|
|
|
|
printf("\nEF(UnusedSpace) file is empty\n");
|
|
|
|
|
else {
|
|
|
|
|
printf("\nContents of EF(UnusedSpace):\n");
|
|
|
|
|
for (us = p15card->unusedspace_list; us != NULL; us = us->next)
|
|
|
|
|
printf(" - path=%s, index=%d, length=%d -- auth_id = %s\n",
|
|
|
|
|
sc_print_path(&us->path), us->path.index, us->path.count,
|
|
|
|
|
us->auth_id.len == 0 ? "<empty>" : sc_pkcs15_print_id(&us->auth_id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err:
|
|
|
|
|
if (buf != NULL)
|
|
|
|
|
free(buf);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2005-12-17 19:53:12 +00:00
|
|
|
|
dump_unusedspace(p15card);
|
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;
|
|
|
|
|
}
|