/*********************************************************** Function: sc_init_one Description: get the function list pointer first. initialize and find the slot with the card ***********************************************************/ int sc_init_one( CK_SLOT_ID_PTR pslot) { CK_RV status; CK_SLOT_ID list[20]; CK_SLOT_ID slot; CK_SLOT_ID_PTR slotList = &list[0]; CK_TOKEN_INFO tokeninfo; CK_ULONG count = 0; CK_C_Initialize pC_Initialize; if (!sc_get_function_list()) { return SCERR_R_INITIALIZE; } pC_Initialize = pFunctionList->C_Initialize; status = (*pC_Initialize)(0); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_INITIALIZE); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_INITIALIZE; } /* status = (*(pFunctionList->C_GetSlotList))(FALSE, NULL, &count); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_GETSLOTLIST); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_GETSLOTLIST; } fprintf(stderr,"Slotlist count = %d\n",count); */ count = 20; status = (*(pFunctionList->C_GetSlotList))(FALSE, slotList, &count); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_GETSLOTLIST); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_GETSLOTLIST; } if (count == 0) { SCerr(SCERR_F_SCINIT,SCERR_R_OPENSESSION); ERR_add_error_data(1,"\n No SmartCard readers found"); return SCERR_R_OPENSESSION; } /* * need to look at all the slots. * Maybe provide the card label then look for it */ slot = list[0]; if (pslot) { *pslot = slot; } return 0; }
int sc_init_open_login( CK_SESSION_HANDLE_PTR PsessionHandle, CK_SLOT_ID_PTR pslot, char * ppin, CK_USER_TYPE userType) { CK_RV status; char * pin; char rpin[256]; /* could also add CKF_EXCLUSIVE_SESSION */ int flags = CKF_RW_SESSION | CKF_SERIAL_SESSION ; status = (*(pFunctionList->C_OpenSession))(*pslot, flags, 0, NULL, PsessionHandle); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_OPENSESSION); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_OPENSESSION; } if (ppin) /* did user provide the pin? */ { pin = ppin; } else { pin = rpin; memset(rpin,0,sizeof(rpin)); #ifdef WIN32 read_passphrase_win32_prompt( (userType == CKU_USER) ? "Smart Card User PIN:" : "Smart Card SO PIN:",0); read_passphrase_win32(rpin,sizeof(rpin),0); #else des_read_pw_string(rpin,sizeof(rpin), (userType == CKU_USER) ? "Smart Card User PIN:" : "Smart Card SO PIN:",0); #endif /*DEE should test this too */ } status = (*(pFunctionList->C_Login))(*PsessionHandle, userType, (CK_CHAR_PTR)pin, strlen(pin)); memset(rpin,0,sizeof(rpin)); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_LOGIN); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_LOGIN; } return 0; }
int sc_init_info( CK_SLOT_ID_PTR pslot, CK_TOKEN_INFO_PTR ptokenInfo) { CK_RV status; status = (*(pFunctionList->C_GetTokenInfo))(*pslot, ptokenInfo); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_LOGIN); ERR_add_error_data(2, "While reading Smart Card Info", sc_ERR_code(status)); return SCERR_R_LOGIN; } return 0; }
/********************************************************************** Function: sc_get_function_list() Description: Get the name of the PKCS11 dll to use from the registry, load it, get the entry for the C_GetFunctionList call it to set the pFunctionList. Parameters: Returns: the pFunctionList or NULL **********************************************************************/ CK_FUNCTION_LIST_PTR sc_get_function_list() { CK_RV status; #if defined(USE_PKCS11_DL) || defined(WIN32) CK_RV (*gfl)(CK_FUNCTION_LIST_PTR_PTR); #endif if (pFunctionList) { return pFunctionList; } #if defined(USE_PKCS11_DL) || defined(WIN32) if (!h_m_pkcs11) { char * dllname = NULL; #ifdef WIN32 HKEY hkDir = NULL; char val_dllname[512] = {"NONE"}; LONG lval; DWORD type; if (!h_m_pkcs11) { RegOpenKey(HKEY_CURRENT_USER,GSI_REGISTRY_DIR,&hkDir); lval = sizeof(val_dllname) -1; if (hkDir && (RegQueryValueEx(hkDir, "PKCS11.DLL", 0, &type, val_dllname,&lval) == ERROR_SUCCESS)) { h_m_pkcs11 = LoadLibrary(val_dllname); } if (hkDir) { RegCloseKey(hkDir); } if (!h_m_pkcs11) { SCerr(SCERR_F_SCINIT,SCERR_R_NO_PKCS11_DLL); ERR_add_error_data(2,"Name of DLL=", dllname? dllname:"NONE"); return NULL; } } gfl = (CK_RV (*)(CK_FUNCTION_LIST_PTR *)) GetProcAddress(h_m_pkcs11,"C_GetFunctionList"); #else if (!h_m_pkcs11) { dllname = getenv("PKCS11_LIB"); if (!dllname) { dllname = "libDSPKCS.so"; } h_m_pkcs11 = dlopen("libDSPKCS.so",RTLD_LAZY); } if (!h_m_pkcs11) { SCerr(SCERR_F_SCINIT,SCERR_R_NO_PKCS11_DLL); ERR_add_error_data(2,"Name of shared library=", dllname); return NULL; } gfl = (CK_RV(*)(CK_FUNCTION_LIST_PTR_PTR)) dlsym(h_m_pkcs11,"C_GetFunctionList"); #endif if (!gfl) { SCerr(SCERR_F_LOAD_DLL,SCERR_R_NO_PKCS11_DLL); ERR_add_error_data(1,"Cant find C_GetFunctionList"); return NULL; } } status = (*gfl)(&pFunctionList); #else status = C_GetFunctionList(&pFunctionList); #endif /* PKCS11_DYNLOAD */ if (status != CKR_OK) { SCerr(SCERR_F_LOAD_DLL,SCERR_R_UNSUPPORTED); ERR_add_error_data(1,sc_ERR_code(status)); return NULL; } return pFunctionList; }
/*********************************************************** Function: sc_init_one Description: get the function list pointer first. initialize and find the slot with the card ***********************************************************/ int sc_init_one( CK_SLOT_ID_PTR pslot) { CK_RV status; CK_SLOT_ID list[20]; CK_SLOT_ID slot; CK_SLOT_ID_PTR slotList = &list[0]; CK_ULONG count = 0; CK_C_Initialize pC_Initialize; CK_C_INITIALIZE_ARGS initArgs; CK_C_INITIALIZE_ARGS_PTR args = NULL; const char * nss_library_params = NULL; if (!sc_get_function_list()) { return SCERR_R_INITIALIZE; } nss_library_params = getenv("PKCS11_INIT_ARGS"); if (nss_library_params) { /* hack to initialize the NSS soft token */ memset(&initArgs, 0, sizeof(initArgs)); initArgs.flags = CKF_OS_LOCKING_OK; initArgs.pReserved = (void *) nss_library_params; args = &initArgs; } pC_Initialize = pFunctionList->C_Initialize; status = (*pC_Initialize)(args); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_INITIALIZE); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_INITIALIZE; } /* status = (*(pFunctionList->C_GetSlotList))(FALSE, NULL, &count); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_GETSLOTLIST); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_GETSLOTLIST; } fprintf(stderr,"Slotlist count = %d\n",count); */ count = 20; status = (*(pFunctionList->C_GetSlotList))(FALSE, slotList, &count); if (status != CKR_OK) { SCerr(SCERR_F_SCINIT,SCERR_R_GETSLOTLIST); ERR_add_error_data(1,sc_ERR_code(status)); return SCERR_R_GETSLOTLIST; } if (count == 0) { SCerr(SCERR_F_SCINIT,SCERR_R_OPENSESSION); ERR_add_error_data(1,"\n No SmartCard readers found"); return SCERR_R_OPENSESSION; } /* * need to look at all the slots. * Maybe provide the card label then look for it */ // slot = list[0]; slot = list[1]; if (pslot) { *pslot = slot; } return 0; }