📄 pkcs11-spy.c
字号:
/* * Copyright (C) 2003 Mathias Brossard <mathias.brossard@idealx.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, * USA */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdlib.h>#include <stdio.h>#include <opensc/opensc.h>#include "pkcs11-display.h"#define __PASTE(x,y) x##y/* Declare all spy_* Cryptoki function */#define CK_NEED_ARG_LIST 1#define CK_PKCS11_FUNCTION_INFO(name) CK_RV name#include "rsaref/pkcs11f.h"/* Spy Module Function List */CK_FUNCTION_LIST_PTR pkcs11_spy = NULL;/* Real Module Function List */CK_FUNCTION_LIST_PTR po = NULL;/* Dynamic Module Handle */static void *modhandle = NULL;/* Spy module output */FILE *spy_output = NULL;#undef CK_NEED_ARG_LIST#undef CK_PKCS11_FUNCTION_INFO#define CK_PKCS11_FUNCTION_INFO(name) \ pkcs11_spy->name = name;/* Inits the spy. If successfull, po != NULL */CK_RV init_spy(void){ const char *mspec = NULL, *file = NULL, *env = NULL; scconf_block *conf_block = NULL, **blocks; struct sc_context *ctx = NULL; int rv = CKR_OK, r, i; /* Allocates and initializes the pkcs11_spy structure */ pkcs11_spy = (CK_FUNCTION_LIST_PTR) malloc(sizeof(CK_FUNCTION_LIST)); if (pkcs11_spy) {#include "rsaref/pkcs11f.h" } else { return CKR_HOST_MEMORY; } r = sc_establish_context(&ctx, "pkcs11-spy"); if (r != 0) { free(pkcs11_spy); return CKR_HOST_MEMORY; } for (i = 0; ctx->conf_blocks[i] != NULL; i++) { blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i], "spy", NULL); conf_block = blocks[0]; free(blocks); if (conf_block != NULL) break; } /* If conf_block is NULL, just return the default value * * Don't use getenv() as the last parameter for scconf_get_str(), * as we want to be able to override configuration file via * environment variables */ env = getenv("PKCS11SPY_OUTPUT"); file = env ? env : scconf_get_str(conf_block, "output", NULL); if (file) { spy_output = fopen(file, "a"); } if (!spy_output) { spy_output = stderr; } fprintf(spy_output, "\n\n*************** OpenSC PKCS#11 spy *****************\n"); env = getenv("PKCS11SPY"); mspec = env ? env : scconf_get_str(conf_block, "module", NULL); modhandle = C_LoadModule(mspec, &po); if (modhandle && po) { fprintf(spy_output, "Loaded: \"%s\"\n", mspec == NULL ? "default module" : mspec); } else { po = NULL; free(pkcs11_spy); rv = CKR_GENERAL_ERROR; } sc_release_context(ctx); return rv;}void enter(char *function){ static int count = 0; fprintf(spy_output, "\n\n%d: %s\n", count++, function);}CK_RV retne(CK_RV rv){ fprintf(spy_output, "Returned: %ld %s\n", rv, lookup_enum ( RV_T, rv )); return rv;}void spy_dump_string_in(char *name, CK_VOID_PTR data, CK_ULONG size){ fprintf(spy_output, "[in] %s ", name); print_generic(spy_output, 0, data, size, NULL);}void spy_dump_string_out(char *name, CK_VOID_PTR data, CK_ULONG size){ fprintf(spy_output, "[out] %s ", name); print_generic(spy_output, 0, data, size, NULL);}void spy_dump_ulong_in(char *name, CK_ULONG value){ fprintf(spy_output, "[in] %s = 0x%lx\n", name, value);}void spy_dump_ulong_out(char *name, CK_ULONG value){ fprintf(spy_output, "[out] %s = 0x%lx\n", name, value);}void spy_dump_desc_out(char *name){ fprintf(spy_output, "[out] %s: \n", name);}void spy_dump_array_out(char *name, CK_ULONG size){ fprintf(spy_output, "[out] %s[%ld]: \n", name, size);}void spy_attribute_req_in(char *name, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount){ fprintf(spy_output, "[in] %s[%ld]: \n", name, ulCount); print_attribute_list_req(spy_output, pTemplate, ulCount);}void spy_attribute_list_in(char *name, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount){ fprintf(spy_output, "[in] %s[%ld]: \n", name, ulCount); print_attribute_list(spy_output, pTemplate, ulCount);}void spy_attribute_list_out(char *name, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount){ fprintf(spy_output, "[out] %s[%ld]: \n", name, ulCount); print_attribute_list(spy_output, pTemplate, ulCount);}CK_RV C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList){ if (po == NULL) { CK_RV rv = init_spy(); if (rv != CKR_OK) return rv; } enter("C_GetFunctionList"); *ppFunctionList = pkcs11_spy; return retne(CKR_OK);}CK_RV C_Initialize(CK_VOID_PTR pInitArgs){ CK_RV rv; if (po == NULL) { rv = init_spy(); if (rv != CKR_OK) return rv; } enter("C_Initialize"); rv = po->C_Initialize(pInitArgs); return retne(rv);}CK_RV C_Finalize(CK_VOID_PTR pReserved){ CK_RV rv; enter("C_Finalize"); rv = po->C_Finalize(pReserved); /* After Finalize do not use the module again */ C_UnloadModule(modhandle); po = NULL; return retne(rv);}CK_RV C_GetInfo(CK_INFO_PTR pInfo){ CK_RV rv; enter("C_GetInfo"); rv = po->C_GetInfo(pInfo); if(rv == CKR_OK) { print_ck_info(spy_output, pInfo); } return retne(rv);}CK_RV C_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount){ CK_RV rv; enter("C_GetSlotList"); spy_dump_ulong_in("tokenPresent", tokenPresent); rv = po->C_GetSlotList(tokenPresent, pSlotList, pulCount); if(rv == CKR_OK) { spy_dump_desc_out("pSlotList"); print_slot_list(spy_output, pSlotList, *pulCount); spy_dump_ulong_out("*pulCount", *pulCount); } return retne(rv);}CK_RV C_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo){ CK_RV rv; enter("C_GetSlotInfo"); spy_dump_ulong_in("slotID", slotID); rv = po->C_GetSlotInfo(slotID, pInfo); if(rv == CKR_OK) { spy_dump_desc_out("pInfo"); print_slot_info(spy_output, pInfo); } return retne(rv);}CK_RV C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo){ CK_RV rv; enter("C_GetTokenInfo"); spy_dump_ulong_in("slotID", slotID); rv = po->C_GetTokenInfo(slotID, pInfo); if(rv == CKR_OK) { spy_dump_desc_out("pInfo"); print_token_info(spy_output, pInfo); } return retne(rv);}CK_RV C_GetMechanismList(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pulCount){ CK_RV rv; enter("C_GetMechanismList"); spy_dump_ulong_in("slotID", slotID); rv = po->C_GetMechanismList(slotID, pMechanismList, pulCount); if(rv == CKR_OK) { spy_dump_array_out("pMechanismList", *pulCount); print_mech_list(spy_output, pMechanismList, *pulCount); } return retne(rv);}CK_RV C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_MECHANISM_INFO_PTR pInfo){ CK_RV rv; const char *name = lookup_enum(MEC_T, type); enter("C_GetMechanismInfo"); spy_dump_ulong_in("slotID", slotID); if (name) { fprintf(spy_output, "%30s \n", name); } else { fprintf(spy_output, " Unknown Mechanism (%08lx) \n", type); } rv = po->C_GetMechanismInfo(slotID, type, pInfo); if(rv == CKR_OK) { spy_dump_desc_out("pInfo"); print_mech_info(spy_output, type, pInfo); } return retne(rv);}CK_RV C_InitToken (CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen, CK_UTF8CHAR_PTR pLabel){ CK_RV rv; enter("C_InitToken"); spy_dump_ulong_in("slotID", slotID); spy_dump_string_in("pPin[ulPinLen]", pPin, ulPinLen); spy_dump_string_in("pLabel[32]", pLabel, 32); rv = po->C_InitToken (slotID, pPin, ulPinLen, pLabel); return retne(rv);}CK_RV C_InitPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen){ CK_RV rv; enter("C_InitPIN"); spy_dump_ulong_in("hSession", hSession); spy_dump_string_in("pPin[ulPinLen]", pPin, ulPinLen); rv = po->C_InitPIN(hSession, pPin, ulPinLen); return retne(rv);}CK_RV C_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin, CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen){ CK_RV rv; enter("C_SetPIN"); spy_dump_ulong_in("hSession", hSession); spy_dump_string_in("pOldPin[ulOldLen]", pOldPin, ulOldLen); spy_dump_string_in("pNewPin[ulNewLen]", pNewPin, ulNewLen); rv = po->C_SetPIN(hSession, pOldPin, ulOldLen, pNewPin, ulNewLen); return retne(rv);}CK_RV C_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession){ CK_RV rv; enter("C_OpenSession"); spy_dump_ulong_in("slotID", slotID); spy_dump_ulong_in("flags", flags); fprintf(spy_output, "pApplication=%p\n", pApplication); fprintf(spy_output, "Notify=%p\n", (void *)Notify); rv = po->C_OpenSession(slotID, flags, pApplication, Notify, phSession); spy_dump_ulong_out("*phSession", *phSession); return retne(rv);}CK_RV C_CloseSession(CK_SESSION_HANDLE hSession){ CK_RV rv; enter("C_CloseSession"); spy_dump_ulong_in("hSession", hSession); rv = po->C_CloseSession(hSession); return retne(rv);}CK_RV C_CloseAllSessions(CK_SLOT_ID slotID){ CK_RV rv; enter("C_CloseAllSessions"); spy_dump_ulong_in("slotID", slotID); rv = po->C_CloseAllSessions(slotID); return retne(rv);}CK_RV C_GetSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo){ CK_RV rv; enter("C_GetSessionInfo"); spy_dump_ulong_in("hSession", hSession); rv = po->C_GetSessionInfo(hSession, pInfo); if(rv == CKR_OK) { spy_dump_desc_out("pInfo"); print_session_info(spy_output, pInfo); } return retne(rv);}CK_RV C_GetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState, CK_ULONG_PTR pulOperationStateLen){ CK_RV rv; enter("C_GetOperationState"); spy_dump_ulong_in("hSession", hSession); rv = po->C_GetOperationState(hSession, pOperationState, pulOperationStateLen); if (rv == CKR_OK) { spy_dump_string_out("pOperationState[*pulOperationStateLen]", pOperationState, *pulOperationStateLen); } return retne(rv);}CK_RV C_SetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -