17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 58047c9fbSmcpowers * Common Development and Distribution License (the "License"). 68047c9fbSmcpowers * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*19193bb6SDina K Nimeh * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * General-Purpose Functions 287c478bd9Sstevel@tonic-gate * (as defined in PKCS#11 spec section 11.4) 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <unistd.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include "metaGlobal.h" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate extern meta_session_t *meta_sessionlist_head; 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate struct CK_FUNCTION_LIST metaslot_functionList = { 39f66d273dSizick { 2, 20 }, /* version */ 407c478bd9Sstevel@tonic-gate meta_Initialize, 417c478bd9Sstevel@tonic-gate meta_Finalize, 427c478bd9Sstevel@tonic-gate meta_GetInfo, 437c478bd9Sstevel@tonic-gate meta_GetFunctionList, 447c478bd9Sstevel@tonic-gate meta_GetSlotList, 457c478bd9Sstevel@tonic-gate meta_GetSlotInfo, 467c478bd9Sstevel@tonic-gate meta_GetTokenInfo, 477c478bd9Sstevel@tonic-gate meta_GetMechanismList, 487c478bd9Sstevel@tonic-gate meta_GetMechanismInfo, 497c478bd9Sstevel@tonic-gate meta_InitToken, 507c478bd9Sstevel@tonic-gate meta_InitPIN, 517c478bd9Sstevel@tonic-gate meta_SetPIN, 527c478bd9Sstevel@tonic-gate meta_OpenSession, 537c478bd9Sstevel@tonic-gate meta_CloseSession, 547c478bd9Sstevel@tonic-gate meta_CloseAllSessions, 557c478bd9Sstevel@tonic-gate meta_GetSessionInfo, 567c478bd9Sstevel@tonic-gate meta_GetOperationState, 577c478bd9Sstevel@tonic-gate meta_SetOperationState, 587c478bd9Sstevel@tonic-gate meta_Login, 597c478bd9Sstevel@tonic-gate meta_Logout, 607c478bd9Sstevel@tonic-gate meta_CreateObject, 617c478bd9Sstevel@tonic-gate meta_CopyObject, 627c478bd9Sstevel@tonic-gate meta_DestroyObject, 637c478bd9Sstevel@tonic-gate meta_GetObjectSize, 647c478bd9Sstevel@tonic-gate meta_GetAttributeValue, 657c478bd9Sstevel@tonic-gate meta_SetAttributeValue, 667c478bd9Sstevel@tonic-gate meta_FindObjectsInit, 677c478bd9Sstevel@tonic-gate meta_FindObjects, 687c478bd9Sstevel@tonic-gate meta_FindObjectsFinal, 697c478bd9Sstevel@tonic-gate meta_EncryptInit, 707c478bd9Sstevel@tonic-gate meta_Encrypt, 717c478bd9Sstevel@tonic-gate meta_EncryptUpdate, 727c478bd9Sstevel@tonic-gate meta_EncryptFinal, 737c478bd9Sstevel@tonic-gate meta_DecryptInit, 747c478bd9Sstevel@tonic-gate meta_Decrypt, 757c478bd9Sstevel@tonic-gate meta_DecryptUpdate, 767c478bd9Sstevel@tonic-gate meta_DecryptFinal, 777c478bd9Sstevel@tonic-gate meta_DigestInit, 787c478bd9Sstevel@tonic-gate meta_Digest, 797c478bd9Sstevel@tonic-gate meta_DigestUpdate, 807c478bd9Sstevel@tonic-gate meta_DigestKey, 817c478bd9Sstevel@tonic-gate meta_DigestFinal, 827c478bd9Sstevel@tonic-gate meta_SignInit, 837c478bd9Sstevel@tonic-gate meta_Sign, 847c478bd9Sstevel@tonic-gate meta_SignUpdate, 857c478bd9Sstevel@tonic-gate meta_SignFinal, 867c478bd9Sstevel@tonic-gate meta_SignRecoverInit, 877c478bd9Sstevel@tonic-gate meta_SignRecover, 887c478bd9Sstevel@tonic-gate meta_VerifyInit, 897c478bd9Sstevel@tonic-gate meta_Verify, 907c478bd9Sstevel@tonic-gate meta_VerifyUpdate, 917c478bd9Sstevel@tonic-gate meta_VerifyFinal, 927c478bd9Sstevel@tonic-gate meta_VerifyRecoverInit, 937c478bd9Sstevel@tonic-gate meta_VerifyRecover, 947c478bd9Sstevel@tonic-gate meta_DigestEncryptUpdate, 957c478bd9Sstevel@tonic-gate meta_DecryptDigestUpdate, 967c478bd9Sstevel@tonic-gate meta_SignEncryptUpdate, 977c478bd9Sstevel@tonic-gate meta_DecryptVerifyUpdate, 987c478bd9Sstevel@tonic-gate meta_GenerateKey, 997c478bd9Sstevel@tonic-gate meta_GenerateKeyPair, 1007c478bd9Sstevel@tonic-gate meta_WrapKey, 1017c478bd9Sstevel@tonic-gate meta_UnwrapKey, 1027c478bd9Sstevel@tonic-gate meta_DeriveKey, 1037c478bd9Sstevel@tonic-gate meta_SeedRandom, 1047c478bd9Sstevel@tonic-gate meta_GenerateRandom, 1057c478bd9Sstevel@tonic-gate meta_GetFunctionStatus, 1067c478bd9Sstevel@tonic-gate meta_CancelFunction, 1077c478bd9Sstevel@tonic-gate meta_WaitForSlotEvent 1087c478bd9Sstevel@tonic-gate }; 1097c478bd9Sstevel@tonic-gate 1108047c9fbSmcpowers pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER; 1118047c9fbSmcpowers 1128047c9fbSmcpowers int meta_urandom_seed_fd = -1; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate ses_to_be_freed_list_t ses_delay_freed; 1157c478bd9Sstevel@tonic-gate object_to_be_freed_list_t obj_delay_freed; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * meta_Initialize 1197c478bd9Sstevel@tonic-gate * 1207c478bd9Sstevel@tonic-gate * This function is never called by the application. It is only 1217c478bd9Sstevel@tonic-gate * called by uCF to initialize metaslot. The pInitArgs argument is ignored. 1227c478bd9Sstevel@tonic-gate * 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1257c478bd9Sstevel@tonic-gate CK_RV 1267c478bd9Sstevel@tonic-gate meta_Initialize(CK_VOID_PTR pInitArgs) 1277c478bd9Sstevel@tonic-gate { 1287c478bd9Sstevel@tonic-gate CK_RV rv; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* Make sure function hasn't been called twice */ 1317c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&initmutex); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate rv = meta_slotManager_initialize(); 1347c478bd9Sstevel@tonic-gate if (rv != CKR_OK) { 1357c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&initmutex); 1367c478bd9Sstevel@tonic-gate return (rv); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate rv = meta_mechManager_initialize(); 1407c478bd9Sstevel@tonic-gate if (rv != CKR_OK) { 1417c478bd9Sstevel@tonic-gate (void) meta_slotManager_finalize(); 1427c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&initmutex); 1437c478bd9Sstevel@tonic-gate return (rv); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate rv = meta_objectManager_initialize(); 1477c478bd9Sstevel@tonic-gate if (rv != CKR_OK) { 1487c478bd9Sstevel@tonic-gate (void) meta_slotManager_finalize(); 1497c478bd9Sstevel@tonic-gate (void) meta_mechManager_finalize(); 1507c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&initmutex); 1517c478bd9Sstevel@tonic-gate return (rv); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate rv = meta_sessionManager_initialize(); 1557c478bd9Sstevel@tonic-gate if (rv != CKR_OK) { 1567c478bd9Sstevel@tonic-gate (void) meta_slotManager_finalize(); 1577c478bd9Sstevel@tonic-gate (void) meta_mechManager_finalize(); 1587c478bd9Sstevel@tonic-gate (void) meta_objectManager_finalize(); 1597c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&initmutex); 1607c478bd9Sstevel@tonic-gate return (rv); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate meta_slotManager_find_object_token(); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* Initialize the object_to_be_freed list */ 1667c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL); 1677c478bd9Sstevel@tonic-gate obj_delay_freed.count = 0; 1687c478bd9Sstevel@tonic-gate obj_delay_freed.first = NULL; 1697c478bd9Sstevel@tonic-gate obj_delay_freed.last = NULL; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* Initialize the session_to_be_freed list */ 1727c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL); 1737c478bd9Sstevel@tonic-gate ses_delay_freed.count = 0; 1747c478bd9Sstevel@tonic-gate ses_delay_freed.first = NULL; 1757c478bd9Sstevel@tonic-gate ses_delay_freed.last = NULL; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&initmutex); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate return (CKR_OK); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * meta_Finalize 1857c478bd9Sstevel@tonic-gate * 1867c478bd9Sstevel@tonic-gate * Called by uCF only, "pReserved" argument is ignored. 1877c478bd9Sstevel@tonic-gate */ 1887c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1897c478bd9Sstevel@tonic-gate CK_RV 1907c478bd9Sstevel@tonic-gate meta_Finalize(CK_VOID_PTR pReserved) 1917c478bd9Sstevel@tonic-gate { 1927c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK; 1937c478bd9Sstevel@tonic-gate meta_object_t *delay_free_obj, *tmpo; 1947c478bd9Sstevel@tonic-gate meta_session_t *delay_free_ses, *tmps; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate if (pReserved != NULL) 1977c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD); 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&initmutex); 2007c478bd9Sstevel@tonic-gate 2018047c9fbSmcpowers if (meta_urandom_seed_fd > 0) { 2028047c9fbSmcpowers (void) close(meta_urandom_seed_fd); 2038047c9fbSmcpowers meta_urandom_seed_fd = -1; 2048047c9fbSmcpowers } 2058047c9fbSmcpowers 2067c478bd9Sstevel@tonic-gate meta_objectManager_finalize(); 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate meta_sessionManager_finalize(); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate meta_mechManager_finalize(); 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate meta_slotManager_finalize(); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * free all entries in the delay_freed list 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate delay_free_obj = obj_delay_freed.first; 2187c478bd9Sstevel@tonic-gate while (delay_free_obj != NULL) { 2197c478bd9Sstevel@tonic-gate tmpo = delay_free_obj->next; 2207c478bd9Sstevel@tonic-gate free(delay_free_obj); 2217c478bd9Sstevel@tonic-gate delay_free_obj = tmpo; 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex); 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate delay_free_ses = ses_delay_freed.first; 2267c478bd9Sstevel@tonic-gate while (delay_free_ses != NULL) { 2277c478bd9Sstevel@tonic-gate tmps = delay_free_ses->next; 2287c478bd9Sstevel@tonic-gate free(delay_free_ses); 2297c478bd9Sstevel@tonic-gate delay_free_ses = tmps; 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex); 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&initmutex); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate return (rv); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* 2397c478bd9Sstevel@tonic-gate * meta_GetInfo 2407c478bd9Sstevel@tonic-gate * 2417c478bd9Sstevel@tonic-gate * NOTE: This function will never be called by applications because it's 2427c478bd9Sstevel@tonic-gate * hidden behind the uCF C_GetInfo. So, it is not implemented. 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2457c478bd9Sstevel@tonic-gate CK_RV 2467c478bd9Sstevel@tonic-gate meta_GetInfo(CK_INFO_PTR pInfo) 2477c478bd9Sstevel@tonic-gate { 2487c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_SUPPORTED); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * meta_GetFunctionList 2547c478bd9Sstevel@tonic-gate * 2557c478bd9Sstevel@tonic-gate * This function is not implemented because metaslot is part of the framework, 2567c478bd9Sstevel@tonic-gate * so, the framework can just do a static assignment to metaslot's 2577c478bd9Sstevel@tonic-gate * function list instead of calling this function. 2587c478bd9Sstevel@tonic-gate */ 2597c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2607c478bd9Sstevel@tonic-gate CK_RV 2617c478bd9Sstevel@tonic-gate meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList) 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_SUPPORTED); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * Parallel Function Management Function 2697c478bd9Sstevel@tonic-gate * (as defined in PKCS#11 spec section 11.16) 2707c478bd9Sstevel@tonic-gate */ 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate /* 2737c478bd9Sstevel@tonic-gate * This function is no longer supported in this revision of the PKCS#11 2747c478bd9Sstevel@tonic-gate * standard. It is maintained for backwards compatibility only. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2777c478bd9Sstevel@tonic-gate CK_RV 2787c478bd9Sstevel@tonic-gate meta_GetFunctionStatus(CK_SESSION_HANDLE hSession) 2797c478bd9Sstevel@tonic-gate { 2807c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_PARALLEL); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* 2857c478bd9Sstevel@tonic-gate * This function is no longer supported in this revision of the PKCS#11 2867c478bd9Sstevel@tonic-gate * standard. It is maintained for backwards compatibility only. 2877c478bd9Sstevel@tonic-gate */ 2887c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2897c478bd9Sstevel@tonic-gate CK_RV 2907c478bd9Sstevel@tonic-gate meta_CancelFunction(CK_SESSION_HANDLE hSession) 2917c478bd9Sstevel@tonic-gate { 2927c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_PARALLEL); 2937c478bd9Sstevel@tonic-gate } 294