1/* (C) COPYRIGHT International Business Machines Corp. 2001, 2002, 2005 */ 2/* 3 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 4 * Use is subject to license terms. 5 */ 6 7#include "tpmtok_int.h" 8 9CK_BBOOL initialized = FALSE; 10 11pthread_mutex_t native_mutex; 12pthread_mutex_t pkcs_mutex, obj_list_mutex, 13 sess_list_mutex, login_mutex; 14 15void *xproclock; 16 17DL_NODE *sess_list = NULL; 18DL_NODE *sess_obj_list = NULL; 19DL_NODE *publ_token_obj_list = NULL; 20DL_NODE *priv_token_obj_list = NULL; 21DL_NODE *object_map = NULL; 22 23CK_STATE global_login_state = 0; 24 25LW_SHM_TYPE *global_shm; 26 27CK_ULONG next_session_handle = 1; 28CK_ULONG next_object_handle = 1; 29 30TOKEN_DATA *nv_token_data = NULL; 31 32extern CK_RV LW_Initialize(); 33extern CK_RV SC_GetFunctionList(); 34extern CK_RV SC_GetTokenInfo(); 35extern CK_RV SC_GetMechanismList(); 36extern CK_RV SC_GetMechanismInfo(); 37extern CK_RV SC_InitToken(); 38extern CK_RV SC_InitPIN(); 39extern CK_RV SC_SetPIN(); 40extern CK_RV SC_OpenSession(); 41extern CK_RV SC_CloseSession(); 42extern CK_RV SC_CloseAllSessions(); 43extern CK_RV SC_GetSessionInfo(); 44extern CK_RV SC_GetOperationState(); 45extern CK_RV SC_SetOperationState(); 46extern CK_RV SC_Login(); 47extern CK_RV SC_Logout(); 48extern CK_RV SC_CreateObject(); 49extern CK_RV SC_CopyObject(); 50extern CK_RV SC_DestroyObject(); 51extern CK_RV SC_GetObjectSize(); 52extern CK_RV SC_GetAttributeValue(); 53extern CK_RV SC_SetAttributeValue(); 54extern CK_RV SC_FindObjectsInit(); 55extern CK_RV SC_FindObjects(); 56extern CK_RV SC_FindObjectsFinal(); 57extern CK_RV SC_EncryptInit(); 58extern CK_RV SC_Encrypt(); 59extern CK_RV SC_EncryptUpdate(); 60extern CK_RV SC_EncryptFinal(); 61extern CK_RV SC_DecryptInit(); 62extern CK_RV SC_Decrypt(); 63extern CK_RV SC_DecryptUpdate(); 64extern CK_RV SC_DecryptFinal(); 65extern CK_RV SC_DigestInit(); 66extern CK_RV SC_Digest(); 67extern CK_RV SC_DigestUpdate(); 68extern CK_RV SC_DigestKey(); 69extern CK_RV SC_DigestFinal(); 70extern CK_RV SC_SignInit(); 71extern CK_RV SC_Sign(); 72extern CK_RV SC_SignUpdate(); 73extern CK_RV SC_SignFinal(); 74extern CK_RV SC_SignRecoverInit(); 75extern CK_RV SC_SignRecover(); 76extern CK_RV SC_VerifyInit(); 77extern CK_RV SC_Verify(); 78extern CK_RV SC_VerifyUpdate(); 79extern CK_RV SC_VerifyFinal(); 80extern CK_RV SC_VerifyRecoverInit(); 81extern CK_RV SC_VerifyRecover(); 82extern CK_RV SC_DigestEncryptUpdate(); 83extern CK_RV SC_DecryptDigestUpdate(); 84extern CK_RV SC_SignEncryptUpdate(); 85extern CK_RV SC_DecryptVerifyUpdate(); 86extern CK_RV SC_GenerateKey(); 87extern CK_RV SC_GenerateKeyPair(); 88extern CK_RV SC_WrapKey(); 89extern CK_RV SC_UnwrapKey(); 90extern CK_RV SC_DeriveKey(); 91extern CK_RV SC_SeedRandom(); 92extern CK_RV SC_GenerateRandom(); 93extern CK_RV SC_GetFunctionStatus(); 94extern CK_RV SC_CancelFunction(); 95extern CK_RV SC_WaitForSlotEvent(); 96 97CK_BYTE ber_rsaEncryption[] = { 98 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 99}; 100CK_BYTE ber_md5WithRSAEncryption[] = { 101 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x04 102}; 103CK_BYTE ber_sha1WithRSAEncryption[] = { 104 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05 105}; 106 107CK_BYTE ber_AlgMd5[] = { 108 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 109 0x02, 0x05, 0x05, 0x00 110}; 111CK_BYTE ber_AlgSha1[] = { 112 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 113 0x00 114}; 115CK_BYTE ber_AlgIdRSAEncryption[] = { 116 0x30, 0x0D, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 117 0x01, 0x01, 0x01, 0x05, 0x00 118}; 119 120CK_ULONG ber_rsaEncryptionLen = sizeof (ber_rsaEncryption); 121CK_ULONG ber_md5WithRSAEncryptionLen = sizeof (ber_md5WithRSAEncryption); 122CK_ULONG ber_sha1WithRSAEncryptionLen = sizeof (ber_sha1WithRSAEncryption); 123 124CK_ULONG ber_AlgMd5Len = sizeof (ber_AlgMd5); 125CK_ULONG ber_AlgSha1Len = sizeof (ber_AlgSha1); 126CK_ULONG ber_AlgIdRSAEncryptionLen = sizeof (ber_AlgIdRSAEncryption); 127 128MECH_LIST_ELEMENT mech_list[] = { 129 { CKM_RSA_PKCS_KEY_PAIR_GEN, {512, 2048, CKF_HW | 130 CKF_GENERATE_KEY_PAIR } }, 131 { CKM_RSA_PKCS, {512, 2048, CKF_HW | CKF_ENCRYPT | CKF_DECRYPT | 132 CKF_WRAP | CKF_UNWRAP | CKF_SIGN | CKF_VERIFY | 133 CKF_SIGN_RECOVER | CKF_VERIFY_RECOVER } }, 134 135 { CKM_RSA_PKCS_OAEP, {512, 2048, CKF_HW | CKF_ENCRYPT | 136 CKF_DECRYPT | CKF_WRAP | CKF_UNWRAP | 137 CKF_SIGN | CKF_VERIFY | CKF_SIGN_RECOVER | CKF_VERIFY_RECOVER }}, 138 139#if 0 140 /* No RSA_X_509 support for now... */ 141 { CKM_RSA_X_509, {512, 2048, CKF_HW | 142 CKF_ENCRYPT | CKF_DECRYPT | 143 CKF_WRAP | CKF_UNWRAP | 144 CKF_SIGN | CKF_VERIFY | 145 CKF_SIGN_RECOVER | CKF_VERIFY_RECOVER } }, 146#endif 147 148 {CKM_MD5_RSA_PKCS, 149 {512, 2048, CKF_HW | CKF_SIGN | CKF_VERIFY } }, 150 151 { CKM_SHA1_RSA_PKCS, 152 {512, 2048, CKF_HW | CKF_SIGN | CKF_VERIFY } }, 153 154 {CKM_SHA_1, 155 {0, 0, CKF_DIGEST } }, 156 157 {CKM_SHA_1_HMAC, 158 {0, 0, CKF_SIGN | CKF_VERIFY } }, 159 160 {CKM_SHA_1_HMAC_GENERAL, 161 {0, 0, CKF_SIGN | CKF_VERIFY } }, 162 163 {CKM_MD5, 164 {0, 0, CKF_DIGEST } }, 165 166 {CKM_MD5_HMAC, 167 {0, 0, CKF_SIGN | CKF_VERIFY } }, 168 169 {CKM_MD5_HMAC_GENERAL, 170 {0, 0, CKF_SIGN | CKF_VERIFY } }, 171}; 172 173CK_ULONG mech_list_len = (sizeof (mech_list) / sizeof (MECH_LIST_ELEMENT)); 174 175/* 176 * default SO pin hash values 177 * 178 * default SO pin = "87654321" 179 */ 180CK_BYTE default_so_pin_md5[MD5_DIGEST_LENGTH] = { 181 0x5E, 0x86, 0x67, 0xA4, 0x39, 0xC6, 0x8F, 0x51, 182 0x45, 0xDD, 0x2F, 0xCB, 0xEC, 0xF0, 0x22, 0x09 183}; 184 185CK_BYTE default_so_pin_sha[SHA1_DIGEST_LENGTH] = { 186 0xA7, 0xD5, 0x79, 0xBA, 0x76, 0x39, 0x80, 0x70, 187 0xEA, 0xE6, 0x54, 0xC3, 0x0F, 0xF1, 0x53, 0xA4, 188 0xC2, 0x73, 0x27, 0x2A 189}; 190 191/* SH - 1 of "12345678" */ 192CK_BYTE default_user_pin_sha[SHA1_DIGEST_LENGTH] = { 193 0x7c, 0x22, 0x2f, 0xb2, 0x92, 0x7d, 0x82, 0x8a, 194 0xf2, 0x2f, 0x59, 0x21, 0x34, 0xe8, 0x93, 0x24, 195 0x80, 0x63, 0x7c, 0x0d 196}; 197 198CK_BYTE user_pin_md5[MD5_DIGEST_LENGTH]; 199CK_BYTE so_pin_md5[MD5_DIGEST_LENGTH]; 200