📄 obj_mgr.c
字号:
obj->session = sess; memset( obj->name, 0x0, sizeof(CK_BYTE) * 8 ); sess_obj_list = dlist_add_as_first( sess_obj_list, obj ); } else { CK_BYTE current[8]; CK_BYTE next[8]; // we'll be modifying nv_token_data so we should protect this part // with 'pkcs_mutex' // rc = XProcLock( xproclock ); if (rc != CKR_OK){ st_err_log(150, __FILE__, __LINE__); goto done; } else { // Determine if we have already reached our Max Token Objects // if (priv_obj) { if (global_shm->num_priv_tok_obj >= MAX_TOK_OBJS) { XProcUnLock(xproclock); st_err_log(1, __FILE__, __LINE__); rc = CKR_HOST_MEMORY; goto done; } } else { if (global_shm->num_publ_tok_obj >= MAX_TOK_OBJS) { XProcUnLock(xproclock); st_err_log(1, __FILE__, __LINE__); rc = CKR_HOST_MEMORY; goto done; } } memcpy( current, &nv_token_data->next_token_object_name, 8 ); obj->session = NULL; memcpy( &obj->name, current, 8 ); compute_next_token_obj_name( current, next ); memcpy( &nv_token_data->next_token_object_name, next, 8 ); save_token_object( obj ); // add the object identifier to the shared memory segment // object_mgr_add_to_shm( obj ); XProcUnLock( xproclock ); save_token_data(); } // now, store the object in the token object list in RAM for speed // if (priv_obj) priv_token_obj_list = dlist_add_as_last( priv_token_obj_list, obj ); else publ_token_obj_list = dlist_add_as_last( publ_token_obj_list, obj ); } rc = object_mgr_add_to_map( sess, obj, handle ); if (rc != CKR_OK) { DL_NODE *node = NULL; st_err_log(157, __FILE__, __LINE__); // this is messy but we need to remove the object from whatever // list we just added it to // if (sess_obj) { node = dlist_find( sess_obj_list, obj ); if (node) sess_obj_list = dlist_remove_node( sess_obj_list, node ); } else { // FIXME - need to destroy the token object file too // delete_token_object( obj ); if (priv_obj) { node = dlist_find( priv_token_obj_list, obj ); if (node) priv_token_obj_list = dlist_remove_node( priv_token_obj_list, node ); } else { node = dlist_find( publ_token_obj_list, obj ); if (node) publ_token_obj_list = dlist_remove_node( publ_token_obj_list, node ); } rc = XProcLock( xproclock ); if (rc != CKR_OK){ st_err_log(150, __FILE__, __LINE__); goto done; } object_mgr_del_from_shm( obj ); XProcUnLock( xproclock ); } }done: if (locked) MY_UnlockMutex( &obj_list_mutex ); return rc;}////CK_RVobject_mgr_destroy_object( SESSION * sess, CK_OBJECT_HANDLE handle ){ OBJECT * obj = NULL; CK_BBOOL sess_obj; CK_BBOOL priv_obj; CK_BBOOL locked = FALSE; CK_RV rc; if (!sess){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } rc = MY_LockMutex( &obj_list_mutex ); if (rc != CKR_OK){ st_err_log(146, __FILE__, __LINE__); goto done; } locked = TRUE; rc = object_mgr_find_in_map1( handle, &obj ); if (rc != CKR_OK){ st_err_log(110, __FILE__, __LINE__); goto done; } sess_obj = object_is_session_object( obj ); priv_obj = object_is_private( obj ); if (sess_obj) { DL_NODE *node; node = dlist_find( sess_obj_list, obj ); if (node) { object_mgr_remove_from_map( handle ); object_free( obj ); sess_obj_list = dlist_remove_node( sess_obj_list, node ); rc = CKR_OK; goto done; } } else { DL_NODE *node = NULL; delete_token_object( obj ); if (priv_obj) node = dlist_find( priv_token_obj_list, obj ); else node = dlist_find( publ_token_obj_list, obj ); if (node) { rc = XProcLock( xproclock ); if (rc != CKR_OK){ st_err_log(150, __FILE__, __LINE__); goto done; } object_mgr_del_from_shm( obj ); XProcUnLock( xproclock ); object_mgr_remove_from_map( handle ); object_free( obj ); if (priv_obj) priv_token_obj_list = dlist_remove_node( priv_token_obj_list, node ); else publ_token_obj_list = dlist_remove_node( publ_token_obj_list, node ); rc = CKR_OK; goto done; } } st_err_log(4, __FILE__, __LINE__, __FUNCTION__); rc = CKR_FUNCTION_FAILED;done: if (locked) MY_UnlockMutex( &obj_list_mutex ); return rc;}// this routine will destroy all token objects in the system//CK_RVobject_mgr_destroy_token_objects( void ){ CK_BBOOL locked1 = FALSE, locked2 = FALSE; CK_RV rc; rc = MY_LockMutex( &obj_list_mutex ); if (rc != CKR_OK){ st_err_log(146, __FILE__, __LINE__); goto done; } else locked1 = TRUE; while (publ_token_obj_list) { OBJECT *obj = (OBJECT *)publ_token_obj_list->data; CK_OBJECT_HANDLE handle; rc = object_mgr_find_in_map2( obj, &handle ); if (rc == CKR_OK) { // only if it's found in the object map. it might not be there // object_mgr_remove_from_map( handle ); } else{ st_err_log(110, __FILE__, __LINE__); } delete_token_object( obj ); object_free( obj ); publ_token_obj_list = dlist_remove_node( publ_token_obj_list, publ_token_obj_list ); } while (priv_token_obj_list) { OBJECT *obj = (OBJECT *)priv_token_obj_list->data; CK_OBJECT_HANDLE handle; rc = object_mgr_find_in_map2( obj, &handle ); if (rc == CKR_OK) { // only if it's found in the object map. it might not be there // object_mgr_remove_from_map( handle ); } else{ st_err_log(110, __FILE__, __LINE__); } delete_token_object( obj ); object_free( obj ); priv_token_obj_list = dlist_remove_node( priv_token_obj_list, priv_token_obj_list ); } // now we want to purge the token object list in shared memory // rc = XProcLock( xproclock ); if (rc == CKR_OK) { locked2 = TRUE; global_shm->num_priv_tok_obj = 0; global_shm->num_publ_tok_obj = 0; memset( &global_shm->publ_tok_objs, 0x0, MAX_TOK_OBJS * sizeof(TOK_OBJ_ENTRY) ); memset( &global_shm->priv_tok_objs, 0x0, MAX_TOK_OBJS * sizeof(TOK_OBJ_ENTRY) ); } else st_err_log(150, __FILE__, __LINE__); done: if (locked1 == TRUE) MY_UnlockMutex( &obj_list_mutex ); if (locked2 == TRUE) XProcUnLock( xproclock ); return rc;}// object_mgr_find_in_map1()//// Locates the specified object in the map//CK_RVobject_mgr_find_in_map1( CK_OBJECT_HANDLE handle, OBJECT ** ptr ){ DL_NODE * node = NULL; OBJECT * obj = NULL; if (!ptr){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } // // no mutex here. the calling function should have locked the mutex // node = object_map; while (node) { OBJECT_MAP *map = (OBJECT_MAP *)node->data; if (map->handle == handle) { obj = map->ptr; break; } node = node->next; } if (obj == NULL || node == NULL) { st_err_log(30, __FILE__, __LINE__); return CKR_OBJECT_HANDLE_INVALID; } // // if this is a token object, we need to check the shared memory segment // to see if any other processes have updated the object // if (object_is_session_object(obj) == TRUE) { *ptr = obj; return CKR_OK; } object_mgr_check_shm( obj ); *ptr = obj; return CKR_OK;}// object_mgr_find_in_map2()//CK_RVobject_mgr_find_in_map2( OBJECT * obj, CK_OBJECT_HANDLE * handle ){ DL_NODE * node = NULL; CK_OBJECT_HANDLE h = (CK_OBJECT_HANDLE)NULL; if (!obj || !handle){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } // // no mutex here. the calling function should have locked the mutex // node = object_map; while (node) { OBJECT_MAP *map = (OBJECT_MAP *)node->data; if (map->ptr == obj) { h = map->handle; break; } node = node->next; } if (node == NULL) {// st_err_log(30, __FILE__, __LINE__); return CKR_OBJECT_HANDLE_INVALID; } // // if this is a token object, we need to check the shared memory segment // to see if any other processes have updated the object // if (object_is_session_object(obj) == TRUE) { *handle = h; return CKR_OK; } object_mgr_check_shm( obj ); *handle = h; return CKR_OK;}CK_RVobject_mgr_find_init( SESSION * sess, CK_ATTRIBUTE * pTemplate, CK_ULONG ulCount ){ // it is possible the pTemplate == NULL // if (!sess){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } if (sess->find_active != FALSE){ return CKR_OPERATION_ACTIVE; st_err_log(31, __FILE__, __LINE__); } // initialize the found object list. if it doesn't exist, allocate // a list big enough for 10 handles. we'll reallocate if we need more // if (sess->find_list != NULL) { memset( sess->find_list, 0x0, sess->find_len * sizeof(CK_OBJECT_HANDLE) ); } else { sess->find_list = (CK_OBJECT_HANDLE *)malloc(10 * sizeof(CK_OBJECT_HANDLE)); if (!sess->find_list){ st_err_log(0, __FILE__, __LINE__); return CKR_HOST_MEMORY; } else { memset( sess->find_list, 0x0, 10 * sizeof(CK_OBJECT_HANDLE) ); sess->find_len = 10; } } sess->find_count = 0; sess->find_idx = 0;// --- need to grab the object lock here MY_LockMutex(&obj_list_mutex); object_mgr_update_from_shm(); MY_UnlockMutex(&obj_list_mutex); // which objects can be returned: // // Public Session: public session objects, public token objects // User Session: all session objects, all token objects // SO session: public session objects, public token objects // switch (sess->session_info.state) { case CKS_RO_PUBLIC_SESSION: case CKS_RW_PUBLIC_SESSION: case CKS_RW_SO_FUNCTIONS: object_mgr_find_build_list( sess, pTemplate, ulCount, publ_token_obj_list, TRUE ); object_mgr_find_build_list( sess, pTemplate, ulCount, sess_obj_list, TRUE ); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -