CK_RV C_CloseSession(CK_SESSION_HANDLE hSession) { P11_SESSION *pSession = NULL; P11_SLOT *pSlot = NULL; CK_RV ret; log_trace(WHERE, "I: enter"); ret = p11_lock(); if (ret != CKR_OK) { log_trace(WHERE, "I: leave, p11_lock failed with %i",ret); return ret; } log_trace(WHERE, "S: C_CloseSession (session %d)", hSession); //get session, of pSession is found, regardless the ret value, we can clean it up ret = p11_get_session(hSession, &pSession); if (pSession == NULL) { log_trace(WHERE, "E: Invalid session handle (%d)", hSession); goto cleanup; } //get slot, if not exist, we allow to close session anyway pSlot = p11_get_slot(pSession->hslot); if (pSlot == NULL) { log_trace(WHERE, "W: Invalid slot (%d) for session (%d)", pSession->hslot, hSession); //ret = CKR_OK; } else { // if (pSlot->nsessions > 0) pSlot->nsessions--; if ((pSlot->nsessions < 1) && (pSlot->login_type >= 0) ) { //TODO what to do if no session longer exists? // cal_logout(pSlot); pSlot->login_type = -1; } //disconnect this session to device ret = cal_disconnect(pSession->hslot); } //clear data so it can be reused pSession->state = 0; pSession->inuse = 0; pSession->flags = 0; pSession->hslot = 0; pSession->pdNotify = NULL; pSession->pfNotify = NULL; cleanup: p11_unlock(); log_trace(WHERE, "I: leave, ret = %i",ret); return ret; }
CK_RV p11_close_session(P11_SLOT* pSlot, P11_SESSION* pSession) { CK_RV ret = CKR_OK; if (pSlot == NULL) return ret; if (pSlot->nsessions > 0) pSlot->nsessions--; if ((pSlot->nsessions < 1) && (pSlot->login_type >= 0) ) { cal_logout(pSession->hslot); pSlot->login_type = -1; } //disconnect this session to device ret = cal_disconnect(pSession->hslot); //clear data so it can be reused if(pSession->Operation[P11_OPERATION_FIND].active) { p11_clean_finddata(pSession->Operation[P11_OPERATION_FIND].pData); free(pSession->Operation[P11_OPERATION_FIND].pData); pSession->Operation[P11_OPERATION_FIND].pData = NULL; pSession->Operation[P11_OPERATION_FIND].active = 0; } if(pSession->Operation[P11_OPERATION_DIGEST].active) { free(pSession->Operation[P11_OPERATION_DIGEST].pData); pSession->Operation[P11_OPERATION_DIGEST].pData = NULL; pSession->Operation[P11_OPERATION_DIGEST].active = 0; } if(pSession->Operation[P11_OPERATION_SIGN].active) { free(pSession->Operation[P11_OPERATION_SIGN].pData); pSession->Operation[P11_OPERATION_SIGN].pData = NULL; pSession->Operation[P11_OPERATION_SIGN].active = 0; } pSession->state = 0; pSession->inuse = 0; pSession->flags = 0; pSession->hslot = 0; pSession->pdNotify = NULL; pSession->pfNotify = NULL; return ret; }