opensc/src/pkcs11/slot.c

110 lines
5.3 KiB
C

#include <stdio.h>
#include <malloc.h>
#include "sc-pkcs11.h"
static CK_OBJECT_CLASS cert_class = CKO_CERTIFICATE;
static CK_BYTE cert_value[] =
"\x30\x82\x03\x8f\x30\x82\x02\x77\xa0\x03\x02\x01\x02\x02\x02\x68\xf6\x30\x0d\x06"
"\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x30\x4c\x31\x0b\x30\x09\x06\x03"
"\x55\x04\x06\x13\x02\x46\x49\x31\x1c\x30\x1a\x06\x03\x55\x04\x0a\x14\x13\x56\x52"
"\x4b\x2d\x46\x49\x4e\x53\x49\x47\x4e\x20\x47\x6f\x76\x2e\x20\x43\x41\x31\x1f\x30"
"\x1d\x06\x03\x55\x04\x03\x14\x16\x46\x49\x4e\x53\x49\x47\x4e\x20\x43\x41\x20\x66"
"\x6f\x72\x20\x43\x69\x74\x69\x7a\x65\x6e\x30\x1e\x17\x0d\x30\x31\x30\x39\x31\x32"
"\x32\x33\x35\x39\x35\x39\x5a\x17\x0d\x30\x34\x30\x39\x30\x35\x32\x33\x35\x39\x35"
"\x39\x5a\x30\x5f\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x46\x49\x31\x0e\x30"
"\x0c\x06\x03\x55\x04\x04\x14\x05\x54\x45\x52\xc4\x53\x31\x0d\x30\x0b\x06\x03\x55"
"\x04\x2a\x14\x04\x54\x49\x4d\x4f\x31\x1d\x30\x1b\x06\x03\x55\x04\x03\x14\x14\x54"
"\x45\x52\xc4\x53\x20\x54\x49\x4d\x4f\x20\x31\x30\x30\x31\x30\x33\x30\x33\x30\x31"
"\x12\x30\x10\x06\x03\x55\x04\x05\x13\x09\x31\x30\x30\x31\x30\x33\x30\x33\x30\x30"
"\x81\x9f\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03\x81\x8d"
"\x00\x30\x81\x89\x02\x81\x81\x00\xba\xb3\xc3\x65\xfb\xab\xd3\x4f\xf1\xe8\x72\xb8"
"\xaa\x48\x6a\x82\x31\x43\xc9\x3e\xe6\xff\x6b\xb6\x0e\xa3\x82\xb4\xda\x3f\xed\xa6"
"\x0b\xbc\xf2\xd3\xad\x53\x88\x88\x14\x14\x3f\x2b\x24\x8d\xd7\x3f\x4b\xb3\xe6\xc1"
"\xb9\xb1\x4d\x3a\x10\xc4\x65\xdc\xe2\xa1\x27\xd2\x8f\xb2\x67\x54\x34\x73\x53\xeb"
"\xec\x84\xab\xdd\xc1\x76\xc9\x73\x49\x4c\x7c\x18\x98\xd3\x40\xc4\x1c\xfd\x0d\x6b"
"\xae\xb7\x9f\x44\xc6\x0a\x5a\x89\x91\xb8\x6e\x20\x38\x2b\xff\x42\xf7\xfe\x95\xc0"
"\x1f\xa5\xca\x07\x2e\x4a\xb0\x9c\x07\x60\x02\x61\xe1\x8b\x25\x01\x02\x03\x01\x00"
"\x01\xa3\x81\xeb\x30\x81\xe8\x30\x11\x06\x03\x55\x1d\x0e\x04\x0a\x04\x08\x43\x9b"
"\x88\x1f\x86\xef\x7c\x8c\x30\x14\x06\x03\x55\x1d\x20\x04\x0d\x30\x0b\x30\x09\x06"
"\x07\x2a\x81\x76\x84\x05\x01\x01\x30\x13\x06\x03\x55\x1d\x23\x04\x0c\x30\x0a\x80"
"\x08\x46\x49\x4e\x43\x41\x4b\x30\x31\x30\x0e\x06\x03\x55\x1d\x0f\x01\x01\xff\x04"
"\x04\x03\x02\x04\xb0\x30\x81\x97\x06\x03\x55\x1d\x1f\x04\x81\x8f\x30\x81\x8c\x30"
"\x81\x89\xa0\x81\x86\xa0\x81\x83\x86\x81\x80\x6c\x64\x61\x70\x3a\x2f\x2f\x31\x39"
"\x33\x2e\x32\x32\x39\x2e\x30\x2e\x32\x31\x30\x3a\x33\x38\x39\x2f\x63\x6e\x3d\x66"
"\x69\x6e\x73\x69\x67\x6e\x25\x32\x30\x63\x61\x25\x32\x30\x66\x6f\x72\x25\x32\x30"
"\x63\x69\x74\x69\x7a\x65\x6e\x2c\x6f\x3d\x76\x72\x6b\x2d\x66\x69\x6e\x73\x69\x67"
"\x6e\x25\x32\x30\x67\x6f\x76\x2e\x25\x32\x30\x63\x61\x2c\x64\x6d\x64\x6e\x61\x6d"
"\x65\x3d\x66\x69\x6e\x65\x69\x64\x2c\x63\x3d\x46\x49\x3f\x63\x65\x72\x74\x69\x66"
"\x69\x63\x61\x74\x65\x72\x65\x76\x6f\x63\x61\x74\x69\x6f\x6e\x6c\x69\x73\x74\x30"
"\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x03\x82\x01\x01\x00\x7d"
"\x4d\xc2\x22\xcb\x63\x82\x9f\xee\x09\x73\xda\x2d\xef\x2f\xb5\x82\x30\x61\x32\x42"
"\x1a\xee\x1c\x41\xac\x24\x36\xd3\x70\xdc\xd9\x02\x84\x58\x47\x11\xb3\x93\xc2\x7b"
"\xa3\x12\x82\x64\xa5\xf2\x8b\x33\x63\x38\x2d\x19\xe7\xfd\xe0\xb0\xf8\x70\xa2\xde"
"\xa3\xd2\x11\xdf\x8c\x41\xcb\x9b\x9a\xa0\xf8\x2c\xdb\xf9\x75\x97\x36\x92\x2d\x90"
"\xd9\x32\x10\xb4\x66\x9c\x21\xd9\xfe\xe5\x46\x59\xbf\x9b\x08\x19\x21\x6a\x52\x30"
"\x19\xac\x35\xc2\x98\x15\x49\x90\x5b\x22\x28\x84\xbd\x61\x3e\xd5\xf6\xa4\xba\x90"
"\x24\xa1\x58\x6f\x8c\xe4\x52\x96\xf7\x31\xb8\x82\xf2\xaa\x9d\x4d\xb0\xf7\xfc\x6a"
"\x1d\x7f\xec\xc4\x39\x50\x3a\x98\x87\xe2\x7b\x59\x16\x42\x3e\xe9\x32\x05\x07\xf0"
"\x5d\x35\xc1\x49\x7f\x91\xde\x75\x58\x30\xd3\xfd\x5f\xb9\x24\x90\xe7\xd9\xd4\x11"
"\x60\x93\x5c\x73\x97\x16\x20\x52\x8f\x04\x9d\x3b\x77\x0f\x12\xb9\x84\x02\x68\x47"
"\x92\x15\xa2\xbb\x79\x10\x04\x28\x24\xa4\xed\x60\x2c\x9f\xd0\xfd\x7b\xa6\xb3\xf7"
"\x80\xe0\x2d\x4d\xdf\x00\x52\x0a\x3b\xbe\x26\x3a\xb5\xed\xf7\x87\x35\x39\x3a\xe8"
"\x0a\x80\x06\x67\x60\x24\x38\xc4\x79\x0b\xa4\x17\x65\x62\x14";
static CK_BYTE cert_id[] = "\x01";
static CK_BYTE cert_label[] = "Timo Teras / Verification certificate";
static CK_ATTRIBUTE certificate[] = {
{ CKA_CLASS, &cert_class, sizeof(cert_class) },
{ CKA_VALUE, cert_value, sizeof(cert_value)-1 },
{ CKA_ID, cert_id, sizeof(cert_id)-1 },
{ CKA_LABEL, cert_label, sizeof(cert_label) }
};
int slot_connect(int id)
{
struct sc_card *card;
struct sc_pkcs15_pin_object pin;
int r, c = 0;
r = sc_connect_card(ctx, id, &card);
if (r) {
LOG("Failed to connect in slot %d (r=%d)\n", id, r);
return CKR_TOKEN_NOT_PRESENT;
}
r = sc_pkcs15_init(card, &slot[id].p15card);
if (r) {
LOG("sc_pkcs15_init failed for slot %d (r=%d)\n", id, r);
/* PKCS#15 compatible SC probably not present */
sc_disconnect_card(card);
return CKR_TOKEN_NOT_RECOGNIZED;
}
while (sc_pkcs15_read_pin_object(slot[id].p15card, ++c, &pin) == 0);
LOG("Found total of %d PIN codes.\n", c);
slot[id].flags = SLOT_CONNECTED;
// KLUDGE:
slot[id].num_objects = 1;
slot[id].object[1] = (struct pkcs11_object*) malloc(sizeof(struct pkcs11_object));
slot[id].object[1]->num_attributes = sizeof(certificate) / sizeof(certificate[0]);
slot[id].object[1]->attribute = certificate;
return CKR_OK;
}
int slot_disconnect(int id)
{
LOG("Disconnecting from slot %d\n", id);
slot[id].flags = 0;
if (slot[id].p15card != NULL) {
sc_disconnect_card(slot[id].p15card->card);
sc_pkcs15_destroy(slot[id].p15card);
slot[id].p15card = NULL;
}
return CKR_OK;
}