📄 api_interface.c
字号:
// Although why does modutil do a close all sessions. It is a single // application it can only close its sessions... // And all sessions should be closed anyhow. LOG("CloseAllSessions"); if (API_Initialized() == FALSE ){ st_err_log(72, __FILE__, __LINE__); return CKR_CRYPTOKI_NOT_INITIALIZED; } if (slotID >= NUMBER_SLOTS_MANAGED ) { st_err_log(2, __FILE__, __LINE__); return CKR_SLOT_ID_INVALID; } // Proc Mutex is locked when we remove from the seesion list in // Close SEssion. Therefore we don't need to do any locking // the atomic operations are controled when we use the linked list pCur = Anchor->SessListBeg; while (pCur){ //LOGIT(LOG_DEBUG,"Pcur Loop %x Slot %d Pslot %d ",pCur,slotID,pCur->SltId); // Session owned by the slot we are working on // There is a basic problem here. We are using th pCur to point to // the current one, however we delete it from the linked list and // can no longer go Forward. So we have to use the fact that this // is a doubly linked list and get the previous pointer. After // deletion, the next pointer of this block will point to the // next one in the list... // If the value is Null, then this was the first one in the list // and we just set pCur to the SessListBeg. if (pCur->SltId == slotID ){ hSession.sessionh = pCur->RealHandle; // use this since after close session hSession.slotID = pCur->SltId; pPrev = pCur->Previous; //LOGIT(LOG_DEBUG,"Prev %x PrevNext %x %d",pPrev,pPrev->Next,count++); //rv = fcn->ST_CloseSession(hSession); rv = C_CloseSession((CK_SESSION_HANDLE)pCur); // Call the local copy if (rv == CKR_OK || rv == CKR_SESSION_CLOSED || rv == CKR_SESSION_HANDLE_INVALID) { if (pPrev == NULL){ //LOGIT(LOG_DEBUG,"Re-wind since we removed the head"); pCur = Anchor->SessListBeg; } else { //LOGIT(LOG_DEBUG,"XXX Prev %x PrevNext %x",pPrev,pPrev->Next); pCur = pPrev->Next; } } else { // We have had a problem deleting a session and // need to abort this operation. This path should not occur // unless LOGIT(LOG_DEBUG,"CloseAllSessions STDLL Problem"); st_err_log(153, __FILE__, __LINE__); return rv; } } else { pCur = pCur->Next; } } sltp = &(Anchor->SltList[slotID]); if (sltp->pSTcloseall) { sltp->pSTcloseall(slotID); // call the terminate function.. } LOG("CloseAllSessions OK"); return CKR_OK; } // end of C_CloseAllSessions//------------------------------------------------------------------------// API function C_CloseSession//------------------------------------------------------------------------// Netscape Required////////------------------------------------------------------------------------CK_RV CK_ENTRY C_CloseSession ( CK_SESSION_HANDLE hSession ){ CK_RV rv; API_Slot_t *sltp; STDLL_FcnList_t *fcn; CK_SLOT_ID slotID; Session_Struct_t *sessp; ST_SESSION_T rSession; LOG("C_CloseSession"); if (API_Initialized() == FALSE ){ st_err_log(72, __FILE__, __LINE__); return CKR_CRYPTOKI_NOT_INITIALIZED; } // Validate Session if (!Valid_Session((Session_Struct_t *)hSession,&rSession)){ st_err_log(40, __FILE__, __LINE__); return CKR_SESSION_HANDLE_INVALID; } // Get local pointers to session slotID = rSession.slotID; sltp = &(Anchor->SltList[slotID]); if (sltp->DLLoaded == FALSE ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if ( (fcn=sltp->FcnList) == NULL ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if (fcn->ST_CloseSession){ // Map the Session to the slot session rv = fcn->ST_CloseSession(rSession); LOGIT(LOG_DEBUG,"Called STDLL rv = 0x%x",rv); // If the STDLL successfuly closed the session // we can free it.. Otherwise we will have to leave it // lying arround. if (rv == CKR_OK) { sessp = (Session_Struct_t *)hSession; RemoveFromSessionList(sessp); // Need to decrement the global slot session count as well // as the per process slot session count to allow for // proper tracking of the number of sessions on a slot. // This allows things like InitToken to properly work in case // other applications have the token active. decr_sess_counts(slotID); } } else { st_err_log(142, __FILE__, __LINE__, __FUNCTION__); rv = CKR_FUNCTION_NOT_SUPPORTED; } return rv;} // end of C_CloseSession//------------------------------------------------------------------------// API function C_CopyObject//------------------------------------------------------------------------// Netscape Required////////------------------------------------------------------------------------CK_RV CK_ENTRY C_CopyObject ( CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phNewObject ){ CK_RV rv; API_Slot_t *sltp; STDLL_FcnList_t *fcn; CK_SLOT_ID slotID; Session_Struct_t *sessp; ST_SESSION_T rSession; LOG("C_CopyObject"); if (API_Initialized() == FALSE ){ st_err_log(72, __FILE__, __LINE__); return CKR_CRYPTOKI_NOT_INITIALIZED; } // Validate Session if (!Valid_Session((Session_Struct_t *)hSession,&rSession)){ st_err_log(40, __FILE__, __LINE__); return CKR_SESSION_HANDLE_INVALID; } if ( !phNewObject ){ st_err_log(5, __FILE__, __LINE__, __FUNCTION__); return CKR_ARGUMENTS_BAD; } // null template with a count... will cause the lower layer // to have problems // Template with 0 count is not a problem. we can let // the STDLL handle that... if ( !pTemplate && ulCount ){ st_err_log(5, __FILE__, __LINE__, __FUNCTION__); return CKR_ARGUMENTS_BAD; } // Get local pointers to session slotID = rSession.slotID; sltp = &(Anchor->SltList[slotID]); if (sltp->DLLoaded == FALSE ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if ( (fcn=sltp->FcnList) == NULL ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if (fcn->ST_CopyObject){ // Map the Session to the slot session rv = fcn->ST_CopyObject(rSession,hObject,pTemplate,ulCount,phNewObject); LOGIT(LOG_DEBUG,"Called STDLL rv = 0x%x",rv); } else { st_err_log(142, __FILE__, __LINE__, __FUNCTION__); rv = CKR_FUNCTION_NOT_SUPPORTED; } return rv;} // end of C_CopyObject//------------------------------------------------------------------------// API function C_CreateObject//------------------------------------------------------------------------// Netscape Required////////------------------------------------------------------------------------CK_RV CK_ENTRY C_CreateObject ( CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject ){ CK_RV rv; API_Slot_t *sltp; STDLL_FcnList_t *fcn; CK_SLOT_ID slotID; Session_Struct_t *sessp; ST_SESSION_T rSession; LOG("C_CreateObject"); if (API_Initialized() == FALSE ){ st_err_log(72, __FILE__, __LINE__); return CKR_CRYPTOKI_NOT_INITIALIZED; } // Validate Session if (!Valid_Session((Session_Struct_t *)hSession,&rSession)){ st_err_log(40, __FILE__, __LINE__); return CKR_SESSION_HANDLE_INVALID; } // Null template is invalid... An object needs a minimal // template for creation. if ( !pTemplate ){ st_err_log(48, __FILE__, __LINE__); return CKR_TEMPLATE_INCOMPLETE; } // A 0 count for the template is bad if ( ulCount == 0 ){ st_err_log(48, __FILE__, __LINE__); return CKR_TEMPLATE_INCOMPLETE; } // A Null pointer to return the handle in is also bad // since we could de-reference incorrectly. if (! phObject ) { st_err_log(5, __FILE__, __LINE__, __FUNCTION__); return CKR_ARGUMENTS_BAD; } // Get local pointers to session slotID = rSession.slotID; sltp = &(Anchor->SltList[slotID]); if (sltp->DLLoaded == FALSE ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if ( (fcn=sltp->FcnList) == NULL ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if (fcn->ST_CreateObject){ // Map the Session to the slot session rv = fcn->ST_CreateObject(rSession,pTemplate,ulCount,phObject); LOGIT(LOG_DEBUG,"Called STDLL rv = 0x%x",rv); } else { st_err_log(142, __FILE__, __LINE__, __FUNCTION__); rv = CKR_FUNCTION_NOT_SUPPORTED; } return rv;} // end of C_CreateObject//------------------------------------------------------------------------// API function C_Decrypt//------------------------------------------------------------------------// Netscape Required////////------------------------------------------------------------------------CK_RV CK_ENTRY C_Decrypt ( CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen ){ CK_RV rv; API_Slot_t *sltp; STDLL_FcnList_t *fcn; CK_SLOT_ID slotID; Session_Struct_t *sessp; ST_SESSION_T rSession; LOG("C_Decrypt"); if (API_Initialized() == FALSE ){ st_err_log(72, __FILE__, __LINE__); return CKR_CRYPTOKI_NOT_INITIALIZED; } // Validate Session if (!Valid_Session((Session_Struct_t *)hSession,&rSession)){ st_err_log(40, __FILE__, __LINE__); return CKR_SESSION_HANDLE_INVALID; } // Null encrypted data is invalid, null pData buffer is invalid // as is null location to put the response into. if ( !pEncryptedData || !pulDataLen) { st_err_log(5, __FILE__, __LINE__, __FUNCTION__); return CKR_ARGUMENTS_BAD; } // Get local pointers to session slotID = rSession.slotID; sltp = &(Anchor->SltList[slotID]); if (sltp->DLLoaded == FALSE ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if ( (fcn=sltp->FcnList) == NULL ){ st_err_log(50, __FILE__, __LINE__); return CKR_TOKEN_NOT_PRESENT; } if (fcn->ST_Decrypt){ // Map the Session to the slot session rv = fcn->ST_Decrypt(rSession,pEncryptedData,ulEncryptedDataLen,pData,pulDataLen); LOGIT(LOG_DEBUG,"Called STDLL rv = 0x%x",rv); } else { st_err_log(142, __FILE__, __LINE__, __FUNCTION__); rv = CKR_FUNCTION_NOT_SUPPORTED; } return rv;} // end of C_Decrypt//------------------------------------------------------------------------// API function C_DecryptDigestUpdate //------------------------------------------------------------------------// Netscape RequiredCK_RV CK_ENTRY C_DecryptDigestUpdate ( CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen ){ CK_RV rv; API_Slot_t *sltp; STDLL_FcnList_t *fcn; CK_SLOT_ID slotID; Session_Struct_t *sessp; ST_SESSION_T rSession; LOG("C_DecryptDigestUpdate"); if (API_Initialized() == FALSE ){ st_err_log(72, __FILE__, __LINE__); return CKR_CRYPTOKI_NOT_INITIALIZED; } // Validate Session if (!Valid_Session((Session_Struct_t *)hSession,&rSession)){ st_err_log(40, __FILE__, __LINE__); return CKR_SESSION_HANDLE_INVALID; } // This may have to go to the STDLL for validation if ( !pEncryptedPart || !pulPartLen) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -