Index: tclpkcs11.c ================================================================== --- tclpkcs11.c +++ tclpkcs11.c @@ -57,10 +57,11 @@ /* PKCS11 Module Pointers */ void *base; CK_FUNCTION_LIST_PTR pkcs11; /* Session Management */ + int session_active; CK_SLOT_ID session_slot; CK_SESSION_HANDLE session; }; /* @@ -365,46 +366,43 @@ /* Convience function to start a session if one is not already active */ MODULE_SCOPE int tclpkcs11_start_session(struct tclpkcs11_handle *handle, CK_SLOT_ID slot) { CK_SESSION_HANDLE tmp_session; CK_RV chk_rv; - if (handle->session != -1) { + if (handle->session_active) { if (handle->session_slot == slot) { return(CKR_OK); } /* Close the existing session and create a new one */ + handle->session_active = 0; chk_rv = handle->pkcs11->C_CloseSession(handle->session); - handle->session = -1; - handle->session_slot = -1; if (chk_rv != CKR_OK) { return(chk_rv); } } chk_rv = handle->pkcs11->C_OpenSession(slot, CKF_SERIAL_SESSION, NULL, NULL, &tmp_session); if (chk_rv != CKR_OK) { handle->pkcs11->C_CloseSession(handle->session); - handle->session = -1; - handle->session_slot = -1; return(chk_rv); } handle->session = tmp_session; handle->session_slot = slot; + handle->session_active = 1; return(CKR_OK); } MODULE_SCOPE int tclpkcs11_close_session(struct tclpkcs11_handle *handle) { CK_RV chk_rv; - if (handle->session != -1) { + if (handle->session_active) { + handle->session_active = 0; chk_rv = handle->pkcs11->C_CloseSession(handle->session); - handle->session = -1; - handle->session_slot = -1; if (chk_rv != CKR_OK) { return(chk_rv); } } @@ -584,12 +582,11 @@ new_handle = (struct tclpkcs11_handle *) ckalloc(sizeof(*new_handle)); /* Initialize the per-handle structure */ new_handle->base = handle; new_handle->pkcs11 = pkcs11_function_list; - new_handle->session = -1; - new_handle->session_slot = -1; + new_handle->session_active = 0; Tcl_SetHashValue(tcl_handle_entry, (ClientData) new_handle); Tcl_SetObjResult(interp, tcl_handle); @@ -1221,13 +1218,13 @@ } chk_rv = handle->pkcs11->C_Logout(handle->session); if (chk_rv != CKR_OK) { if (chk_rv == CKR_DEVICE_REMOVED) { + handle->session_active = 0; + handle->pkcs11->C_CloseSession(handle->session); - handle->session = -1; - handle->session_slot = -1; } else { Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv)); return(TCL_ERROR); }