From b5d584e5a7febd8f613209115d4ce7b987a031bf Mon Sep 17 00:00:00 2001 From: jey Date: Sat, 20 Oct 2001 16:53:51 +0000 Subject: [PATCH] modified to use latest SC API git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@6 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/pkcs11/generic.c | 99 +++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 33 deletions(-) diff --git a/src/pkcs11/generic.c b/src/pkcs11/generic.c index ed611059..23ae918f 100644 --- a/src/pkcs11/generic.c +++ b/src/pkcs11/generic.c @@ -5,10 +5,10 @@ #include #include "sc-pkcs11.h" +#include "../sc.h" -SCARDCONTEXT sc_ctx; -int sc_num_readers; -char *sc_reader_name[16]; +struct sc_context *ctx = NULL; +struct sc_pkcs15_card *p15card = NULL; void LOG(char *format, ...) { @@ -31,25 +31,12 @@ CK_RV C_Initialize(CK_VOID_PTR pReserved) LOG("C_Initialize(0x%x)\n", pReserved); - rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &sc_ctx); - if (rv != SCARD_S_SUCCESS) { + ctx = NULL; + rv = sc_establish_context(&ctx); + if (rv != 0) { LOG("ERROR: Unable to connect to Resource Manager\n"); return CKR_DEVICE_ERROR; } - - // Fetch the list of readers - SCardListReaders(sc_ctx, NULL, NULL, (LPDWORD) &reader_buf_size); - reader_buf = (char*) malloc(reader_buf_size); - SCardListReaders(sc_ctx, NULL, reader_buf, (LPDWORD) &reader_buf_size); - p = reader_buf; - sc_num_readers = reader_count = 0; - do { - sc_reader_name[sc_num_readers++] = strdup(p); - while (*p++ != 0); - p++; - } while (p < reader_buf + reader_buf_size); - free(reader_buf); - return CKR_OK; } @@ -59,10 +46,11 @@ CK_RV C_Finalize(CK_VOID_PTR pReserved) LOG("C_Finalize(0x%x)\n", pReserved); - for (i = 0; i < sc_num_readers; i++) - free(sc_reader_name[i]); - - SCardReleaseContext(sc_ctx); + if (p15card != NULL) { + sc_disconnect_card(p15card->card); + sc_pkcs15_destroy(p15card); + } + sc_destroy_context(ctx); return CKR_OK; } @@ -74,7 +62,7 @@ CK_RV C_GetInfo(CK_INFO_PTR pInfo) pInfo->cryptokiVersion.major = 2; pInfo->cryptokiVersion.minor = 10; strcpy(pInfo->manufacturerID, "Timo Teras & Juha Yrjola"); - strcpy(pInfo->libraryDescription, "PCSC PKCS#15 SmartCard reader"); + strcpy(pInfo->libraryDescription, "PC/SC PKCS#15 SmartCard reader"); pInfo->libraryVersion.major = 0; pInfo->libraryVersion.minor = 1; return CKR_OK; @@ -96,11 +84,11 @@ CK_RV C_GetSlotList(CK_BBOOL tokenPresent, /* only slots with token prese LOG("C_GetSlotList(%d, 0x%x, 0x%x)\n", tokenPresent, pSlotList, pulCount); if (pSlotList == NULL_PTR) { - *pulCount = sc_num_readers; + *pulCount = ctx->reader_count; return CKR_OK; } - num = sc_num_readers > *pulCount ? *pulCount : sc_num_readers; + num = ctx->reader_count > *pulCount ? *pulCount : ctx->reader_count; for (i = 0; i < num; i++) pSlotList[i] = i; *pulCount = num; @@ -112,14 +100,23 @@ CK_RV C_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) { LOG("C_GetSlotInfo(%d, 0x%x)\n", slotID, pInfo); - if (slotID < 0 || slotID >= sc_num_readers) + if (slotID < 0 || slotID >= ctx->reader_count) return CKR_SLOT_ID_INVALID; memset(pInfo, 0, sizeof(CK_SLOT_INFO)); - strncpy(pInfo->slotDescription, sc_reader_name[slotID], + strncpy(pInfo->slotDescription, ctx->readers[slotID], sizeof(pInfo->slotDescription)); - strcpy(pInfo->manufacturerID, "PCSC interface"); + strcpy(pInfo->manufacturerID, "PC/SC interface"); pInfo->flags = CKF_REMOVABLE_DEVICE | CKF_HW_SLOT; + if (sc_detect_card(ctx, slotID) == 1) + pInfo->flags |= CKF_TOKEN_PRESENT; + else { + if (p15card != NULL) { + sc_disconnect_card(p15card->card); + sc_pkcs15_destroy(p15card); + p15card = NULL; + } + } pInfo->hardwareVersion.major = 1; pInfo->firmwareVersion.major = 1; @@ -128,8 +125,47 @@ CK_RV C_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) CK_RV C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) { + int r; + struct sc_card *card; + LOG("C_GetTokenInfo(%d, 0x%x)\n", slotID, pInfo); - return CKR_FUNCTION_NOT_SUPPORTED; + if (slotID < 0 || slotID >= ctx->reader_count) + return CKR_SLOT_ID_INVALID; + + memset(pInfo, 0, sizeof(CK_SLOT_INFO)); + + if (p15card == NULL) { + r = sc_connect_card(ctx, slotID, &card); + if (r) + return CKR_DEVICE_ERROR; + r = sc_pkcs15_init(card, &p15card); + if (r) { + /* PKCS#15 compatible SC probably not present */ + sc_disconnect_card(card); + return CKR_DEVICE_ERROR; + } + } + strcpy(pInfo->label, p15card->label); + strcpy(pInfo->manufacturerID, "unknown"); + strcpy(pInfo->model, "unknown"); + strcpy(pInfo->serialNumber, "unknown"); + pInfo->flags = CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED; + pInfo->ulMaxSessionCount = 1; /* opened in exclusive mode */ + pInfo->ulSessionCount = 0; + pInfo->ulMaxRwSessionCount = 1; + pInfo->ulRwSessionCount = 0; + pInfo->ulMaxPinLen = 8; /* FIXME: get these from PIN objects */ + pInfo->ulMinPinLen = 4; + pInfo->ulTotalPublicMemory = 0; + pInfo->ulFreePublicMemory = 0; + pInfo->ulTotalPrivateMemory = 0; + pInfo->ulFreePrivateMemory = 0; + pInfo->hardwareVersion.major = 1; + pInfo->hardwareVersion.minor = 0; + pInfo->firmwareVersion.major = 1; + pInfo->firmwareVersion.minor = 0; + + return CKR_OK; } CK_RV C_GetMechanismList(CK_SLOT_ID slotID, @@ -174,6 +210,3 @@ CK_RV C_SetPIN(CK_SESSION_HANDLE hSession, LOG("C_SetPIN(%d, '%s', %d, '%s', %d)\n", hSession, pOldPin, ulOldLen, pNewPin, ulNewLen); return CKR_FUNCTION_NOT_SUPPORTED; } - - -