Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* |
michael@0 | 6 | * wrap.c |
michael@0 | 7 | * |
michael@0 | 8 | * This file contains the routines that actually implement the cryptoki |
michael@0 | 9 | * API, using the internal APIs of the NSS Cryptoki Framework. There is |
michael@0 | 10 | * one routine here for every cryptoki routine. For linking reasons |
michael@0 | 11 | * the actual entry points passed back with C_GetFunctionList have to |
michael@0 | 12 | * exist in one of the Module's source files; however, those are merely |
michael@0 | 13 | * simple wrappers that call these routines. The intelligence of the |
michael@0 | 14 | * implementations is here. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #ifndef CK_T |
michael@0 | 18 | #include "ck.h" |
michael@0 | 19 | #endif /* CK_T */ |
michael@0 | 20 | |
michael@0 | 21 | /* |
michael@0 | 22 | * NSSCKFWC_Initialize |
michael@0 | 23 | * NSSCKFWC_Finalize |
michael@0 | 24 | * NSSCKFWC_GetInfo |
michael@0 | 25 | * -- NSSCKFWC_GetFunctionList -- see the API insert file |
michael@0 | 26 | * NSSCKFWC_GetSlotList |
michael@0 | 27 | * NSSCKFWC_GetSlotInfo |
michael@0 | 28 | * NSSCKFWC_GetTokenInfo |
michael@0 | 29 | * NSSCKFWC_WaitForSlotEvent |
michael@0 | 30 | * NSSCKFWC_GetMechanismList |
michael@0 | 31 | * NSSCKFWC_GetMechanismInfo |
michael@0 | 32 | * NSSCKFWC_InitToken |
michael@0 | 33 | * NSSCKFWC_InitPIN |
michael@0 | 34 | * NSSCKFWC_SetPIN |
michael@0 | 35 | * NSSCKFWC_OpenSession |
michael@0 | 36 | * NSSCKFWC_CloseSession |
michael@0 | 37 | * NSSCKFWC_CloseAllSessions |
michael@0 | 38 | * NSSCKFWC_GetSessionInfo |
michael@0 | 39 | * NSSCKFWC_GetOperationState |
michael@0 | 40 | * NSSCKFWC_SetOperationState |
michael@0 | 41 | * NSSCKFWC_Login |
michael@0 | 42 | * NSSCKFWC_Logout |
michael@0 | 43 | * NSSCKFWC_CreateObject |
michael@0 | 44 | * NSSCKFWC_CopyObject |
michael@0 | 45 | * NSSCKFWC_DestroyObject |
michael@0 | 46 | * NSSCKFWC_GetObjectSize |
michael@0 | 47 | * NSSCKFWC_GetAttributeValue |
michael@0 | 48 | * NSSCKFWC_SetAttributeValue |
michael@0 | 49 | * NSSCKFWC_FindObjectsInit |
michael@0 | 50 | * NSSCKFWC_FindObjects |
michael@0 | 51 | * NSSCKFWC_FindObjectsFinal |
michael@0 | 52 | * NSSCKFWC_EncryptInit |
michael@0 | 53 | * NSSCKFWC_Encrypt |
michael@0 | 54 | * NSSCKFWC_EncryptUpdate |
michael@0 | 55 | * NSSCKFWC_EncryptFinal |
michael@0 | 56 | * NSSCKFWC_DecryptInit |
michael@0 | 57 | * NSSCKFWC_Decrypt |
michael@0 | 58 | * NSSCKFWC_DecryptUpdate |
michael@0 | 59 | * NSSCKFWC_DecryptFinal |
michael@0 | 60 | * NSSCKFWC_DigestInit |
michael@0 | 61 | * NSSCKFWC_Digest |
michael@0 | 62 | * NSSCKFWC_DigestUpdate |
michael@0 | 63 | * NSSCKFWC_DigestKey |
michael@0 | 64 | * NSSCKFWC_DigestFinal |
michael@0 | 65 | * NSSCKFWC_SignInit |
michael@0 | 66 | * NSSCKFWC_Sign |
michael@0 | 67 | * NSSCKFWC_SignUpdate |
michael@0 | 68 | * NSSCKFWC_SignFinal |
michael@0 | 69 | * NSSCKFWC_SignRecoverInit |
michael@0 | 70 | * NSSCKFWC_SignRecover |
michael@0 | 71 | * NSSCKFWC_VerifyInit |
michael@0 | 72 | * NSSCKFWC_Verify |
michael@0 | 73 | * NSSCKFWC_VerifyUpdate |
michael@0 | 74 | * NSSCKFWC_VerifyFinal |
michael@0 | 75 | * NSSCKFWC_VerifyRecoverInit |
michael@0 | 76 | * NSSCKFWC_VerifyRecover |
michael@0 | 77 | * NSSCKFWC_DigestEncryptUpdate |
michael@0 | 78 | * NSSCKFWC_DecryptDigestUpdate |
michael@0 | 79 | * NSSCKFWC_SignEncryptUpdate |
michael@0 | 80 | * NSSCKFWC_DecryptVerifyUpdate |
michael@0 | 81 | * NSSCKFWC_GenerateKey |
michael@0 | 82 | * NSSCKFWC_GenerateKeyPair |
michael@0 | 83 | * NSSCKFWC_WrapKey |
michael@0 | 84 | * NSSCKFWC_UnwrapKey |
michael@0 | 85 | * NSSCKFWC_DeriveKey |
michael@0 | 86 | * NSSCKFWC_SeedRandom |
michael@0 | 87 | * NSSCKFWC_GenerateRandom |
michael@0 | 88 | * NSSCKFWC_GetFunctionStatus |
michael@0 | 89 | * NSSCKFWC_CancelFunction |
michael@0 | 90 | */ |
michael@0 | 91 | |
michael@0 | 92 | /* figure out out locking semantics */ |
michael@0 | 93 | static CK_RV |
michael@0 | 94 | nssCKFW_GetThreadSafeState(CK_C_INITIALIZE_ARGS_PTR pInitArgs, |
michael@0 | 95 | CryptokiLockingState *pLocking_state) { |
michael@0 | 96 | int functionCount = 0; |
michael@0 | 97 | |
michael@0 | 98 | /* parsed according to (PKCS #11 Section 11.4) */ |
michael@0 | 99 | /* no args, the degenerate version of case 1 */ |
michael@0 | 100 | if (!pInitArgs) { |
michael@0 | 101 | *pLocking_state = SingleThreaded; |
michael@0 | 102 | return CKR_OK; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | /* CKF_OS_LOCKING_OK set, Cases 2 and 4 */ |
michael@0 | 106 | if (pInitArgs->flags & CKF_OS_LOCKING_OK) { |
michael@0 | 107 | *pLocking_state = MultiThreaded; |
michael@0 | 108 | return CKR_OK; |
michael@0 | 109 | } |
michael@0 | 110 | if ((CK_CREATEMUTEX) NULL != pInitArgs->CreateMutex) functionCount++; |
michael@0 | 111 | if ((CK_DESTROYMUTEX) NULL != pInitArgs->DestroyMutex) functionCount++; |
michael@0 | 112 | if ((CK_LOCKMUTEX) NULL != pInitArgs->LockMutex) functionCount++; |
michael@0 | 113 | if ((CK_UNLOCKMUTEX) NULL != pInitArgs->UnlockMutex) functionCount++; |
michael@0 | 114 | |
michael@0 | 115 | /* CKF_OS_LOCKING_OK is not set, and not functions supplied, |
michael@0 | 116 | * explicit case 1 */ |
michael@0 | 117 | if (0 == functionCount) { |
michael@0 | 118 | *pLocking_state = SingleThreaded; |
michael@0 | 119 | return CKR_OK; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | /* OS_LOCKING_OK is not set and functions have been supplied. Since |
michael@0 | 123 | * ckfw uses nssbase library which explicitly calls NSPR, and since |
michael@0 | 124 | * there is no way to reliably override these explicit calls to NSPR, |
michael@0 | 125 | * therefore we can't support applications which have their own threading |
michael@0 | 126 | * module. Return CKR_CANT_LOCK if they supplied the correct number of |
michael@0 | 127 | * arguments, or CKR_ARGUMENTS_BAD if they did not in either case we will |
michael@0 | 128 | * fail the initialize */ |
michael@0 | 129 | return (4 == functionCount) ? CKR_CANT_LOCK : CKR_ARGUMENTS_BAD; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | static PRInt32 liveInstances; |
michael@0 | 133 | |
michael@0 | 134 | /* |
michael@0 | 135 | * NSSCKFWC_Initialize |
michael@0 | 136 | * |
michael@0 | 137 | */ |
michael@0 | 138 | NSS_IMPLEMENT CK_RV |
michael@0 | 139 | NSSCKFWC_Initialize |
michael@0 | 140 | ( |
michael@0 | 141 | NSSCKFWInstance **pFwInstance, |
michael@0 | 142 | NSSCKMDInstance *mdInstance, |
michael@0 | 143 | CK_VOID_PTR pInitArgs |
michael@0 | 144 | ) |
michael@0 | 145 | { |
michael@0 | 146 | CK_RV error = CKR_OK; |
michael@0 | 147 | CryptokiLockingState locking_state; |
michael@0 | 148 | |
michael@0 | 149 | if( (NSSCKFWInstance **)NULL == pFwInstance ) { |
michael@0 | 150 | error = CKR_GENERAL_ERROR; |
michael@0 | 151 | goto loser; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | if (*pFwInstance) { |
michael@0 | 155 | error = CKR_CRYPTOKI_ALREADY_INITIALIZED; |
michael@0 | 156 | goto loser; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | if (!mdInstance) { |
michael@0 | 160 | error = CKR_GENERAL_ERROR; |
michael@0 | 161 | goto loser; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | error = nssCKFW_GetThreadSafeState(pInitArgs,&locking_state); |
michael@0 | 165 | if( CKR_OK != error ) { |
michael@0 | 166 | goto loser; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | *pFwInstance = nssCKFWInstance_Create(pInitArgs, locking_state, mdInstance, &error); |
michael@0 | 170 | if (!*pFwInstance) { |
michael@0 | 171 | goto loser; |
michael@0 | 172 | } |
michael@0 | 173 | PR_ATOMIC_INCREMENT(&liveInstances); |
michael@0 | 174 | return CKR_OK; |
michael@0 | 175 | |
michael@0 | 176 | loser: |
michael@0 | 177 | switch( error ) { |
michael@0 | 178 | case CKR_ARGUMENTS_BAD: |
michael@0 | 179 | case CKR_CANT_LOCK: |
michael@0 | 180 | case CKR_CRYPTOKI_ALREADY_INITIALIZED: |
michael@0 | 181 | case CKR_FUNCTION_FAILED: |
michael@0 | 182 | case CKR_GENERAL_ERROR: |
michael@0 | 183 | case CKR_HOST_MEMORY: |
michael@0 | 184 | case CKR_NEED_TO_CREATE_THREADS: |
michael@0 | 185 | break; |
michael@0 | 186 | default: |
michael@0 | 187 | case CKR_OK: |
michael@0 | 188 | error = CKR_GENERAL_ERROR; |
michael@0 | 189 | break; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | return error; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | /* |
michael@0 | 196 | * NSSCKFWC_Finalize |
michael@0 | 197 | * |
michael@0 | 198 | */ |
michael@0 | 199 | NSS_IMPLEMENT CK_RV |
michael@0 | 200 | NSSCKFWC_Finalize |
michael@0 | 201 | ( |
michael@0 | 202 | NSSCKFWInstance **pFwInstance |
michael@0 | 203 | ) |
michael@0 | 204 | { |
michael@0 | 205 | CK_RV error = CKR_OK; |
michael@0 | 206 | |
michael@0 | 207 | if( (NSSCKFWInstance **)NULL == pFwInstance ) { |
michael@0 | 208 | error = CKR_GENERAL_ERROR; |
michael@0 | 209 | goto loser; |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | if (!*pFwInstance) { |
michael@0 | 213 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 214 | goto loser; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | error = nssCKFWInstance_Destroy(*pFwInstance); |
michael@0 | 218 | |
michael@0 | 219 | /* In any case */ |
michael@0 | 220 | *pFwInstance = (NSSCKFWInstance *)NULL; |
michael@0 | 221 | |
michael@0 | 222 | loser: |
michael@0 | 223 | switch( error ) { |
michael@0 | 224 | PRInt32 remainingInstances; |
michael@0 | 225 | case CKR_OK: |
michael@0 | 226 | remainingInstances = PR_ATOMIC_DECREMENT(&liveInstances); |
michael@0 | 227 | if (!remainingInstances) { |
michael@0 | 228 | nssArena_Shutdown(); |
michael@0 | 229 | } |
michael@0 | 230 | break; |
michael@0 | 231 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 232 | case CKR_FUNCTION_FAILED: |
michael@0 | 233 | case CKR_GENERAL_ERROR: |
michael@0 | 234 | case CKR_HOST_MEMORY: |
michael@0 | 235 | break; |
michael@0 | 236 | default: |
michael@0 | 237 | error = CKR_GENERAL_ERROR; |
michael@0 | 238 | break; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | /* |
michael@0 | 242 | * A thread's error stack is automatically destroyed when the thread |
michael@0 | 243 | * terminates or, for the primordial thread, by PR_Cleanup. On |
michael@0 | 244 | * Windows with MinGW, the thread private data destructor PR_Free |
michael@0 | 245 | * registered by this module is actually a thunk for PR_Free defined |
michael@0 | 246 | * in this module. When the thread that unloads this module terminates |
michael@0 | 247 | * or calls PR_Cleanup, the thunk for PR_Free is already gone with the |
michael@0 | 248 | * module. Therefore we need to destroy the error stack before the |
michael@0 | 249 | * module is unloaded. |
michael@0 | 250 | */ |
michael@0 | 251 | nss_DestroyErrorStack(); |
michael@0 | 252 | return error; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | /* |
michael@0 | 256 | * NSSCKFWC_GetInfo |
michael@0 | 257 | * |
michael@0 | 258 | */ |
michael@0 | 259 | NSS_IMPLEMENT CK_RV |
michael@0 | 260 | NSSCKFWC_GetInfo |
michael@0 | 261 | ( |
michael@0 | 262 | NSSCKFWInstance *fwInstance, |
michael@0 | 263 | CK_INFO_PTR pInfo |
michael@0 | 264 | ) |
michael@0 | 265 | { |
michael@0 | 266 | CK_RV error = CKR_OK; |
michael@0 | 267 | |
michael@0 | 268 | if( (CK_INFO_PTR)CK_NULL_PTR == pInfo ) { |
michael@0 | 269 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 270 | goto loser; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | /* |
michael@0 | 274 | * A purify error here means a caller error |
michael@0 | 275 | */ |
michael@0 | 276 | (void)nsslibc_memset(pInfo, 0, sizeof(CK_INFO)); |
michael@0 | 277 | |
michael@0 | 278 | pInfo->cryptokiVersion = nssCKFWInstance_GetCryptokiVersion(fwInstance); |
michael@0 | 279 | |
michael@0 | 280 | error = nssCKFWInstance_GetManufacturerID(fwInstance, pInfo->manufacturerID); |
michael@0 | 281 | if( CKR_OK != error ) { |
michael@0 | 282 | goto loser; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | pInfo->flags = nssCKFWInstance_GetFlags(fwInstance); |
michael@0 | 286 | |
michael@0 | 287 | error = nssCKFWInstance_GetLibraryDescription(fwInstance, pInfo->libraryDescription); |
michael@0 | 288 | if( CKR_OK != error ) { |
michael@0 | 289 | goto loser; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | pInfo->libraryVersion = nssCKFWInstance_GetLibraryVersion(fwInstance); |
michael@0 | 293 | |
michael@0 | 294 | return CKR_OK; |
michael@0 | 295 | |
michael@0 | 296 | loser: |
michael@0 | 297 | switch( error ) { |
michael@0 | 298 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 299 | case CKR_FUNCTION_FAILED: |
michael@0 | 300 | case CKR_GENERAL_ERROR: |
michael@0 | 301 | case CKR_HOST_MEMORY: |
michael@0 | 302 | break; |
michael@0 | 303 | default: |
michael@0 | 304 | error = CKR_GENERAL_ERROR; |
michael@0 | 305 | break; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | return error; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | /* |
michael@0 | 312 | * C_GetFunctionList is implemented entirely in the Module's file which |
michael@0 | 313 | * includes the Framework API insert file. It requires no "actual" |
michael@0 | 314 | * NSSCKFW routine. |
michael@0 | 315 | */ |
michael@0 | 316 | |
michael@0 | 317 | /* |
michael@0 | 318 | * NSSCKFWC_GetSlotList |
michael@0 | 319 | * |
michael@0 | 320 | */ |
michael@0 | 321 | NSS_IMPLEMENT CK_RV |
michael@0 | 322 | NSSCKFWC_GetSlotList |
michael@0 | 323 | ( |
michael@0 | 324 | NSSCKFWInstance *fwInstance, |
michael@0 | 325 | CK_BBOOL tokenPresent, |
michael@0 | 326 | CK_SLOT_ID_PTR pSlotList, |
michael@0 | 327 | CK_ULONG_PTR pulCount |
michael@0 | 328 | ) |
michael@0 | 329 | { |
michael@0 | 330 | CK_RV error = CKR_OK; |
michael@0 | 331 | CK_ULONG nSlots; |
michael@0 | 332 | |
michael@0 | 333 | if (!fwInstance) { |
michael@0 | 334 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 335 | goto loser; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | switch( tokenPresent ) { |
michael@0 | 339 | case CK_TRUE: |
michael@0 | 340 | case CK_FALSE: |
michael@0 | 341 | break; |
michael@0 | 342 | default: |
michael@0 | 343 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 344 | goto loser; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | if( (CK_ULONG_PTR)CK_NULL_PTR == pulCount ) { |
michael@0 | 348 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 349 | goto loser; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 353 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 354 | goto loser; |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | if( (CK_SLOT_ID_PTR)CK_NULL_PTR == pSlotList ) { |
michael@0 | 358 | *pulCount = nSlots; |
michael@0 | 359 | return CKR_OK; |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | /* |
michael@0 | 363 | * A purify error here indicates caller error. |
michael@0 | 364 | */ |
michael@0 | 365 | (void)nsslibc_memset(pSlotList, 0, *pulCount * sizeof(CK_SLOT_ID)); |
michael@0 | 366 | |
michael@0 | 367 | if( *pulCount < nSlots ) { |
michael@0 | 368 | *pulCount = nSlots; |
michael@0 | 369 | error = CKR_BUFFER_TOO_SMALL; |
michael@0 | 370 | goto loser; |
michael@0 | 371 | } else { |
michael@0 | 372 | CK_ULONG i; |
michael@0 | 373 | *pulCount = nSlots; |
michael@0 | 374 | |
michael@0 | 375 | /* |
michael@0 | 376 | * Our secret "mapping": CK_SLOT_IDs are integers [1,N], and we |
michael@0 | 377 | * just index one when we need it. |
michael@0 | 378 | */ |
michael@0 | 379 | |
michael@0 | 380 | for( i = 0; i < nSlots; i++ ) { |
michael@0 | 381 | pSlotList[i] = i+1; |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | return CKR_OK; |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | loser: |
michael@0 | 388 | switch( error ) { |
michael@0 | 389 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 390 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 391 | case CKR_FUNCTION_FAILED: |
michael@0 | 392 | case CKR_GENERAL_ERROR: |
michael@0 | 393 | case CKR_HOST_MEMORY: |
michael@0 | 394 | break; |
michael@0 | 395 | default: |
michael@0 | 396 | case CKR_OK: |
michael@0 | 397 | error = CKR_GENERAL_ERROR; |
michael@0 | 398 | break; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | return error; |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | /* |
michael@0 | 405 | * NSSCKFWC_GetSlotInfo |
michael@0 | 406 | * |
michael@0 | 407 | */ |
michael@0 | 408 | NSS_IMPLEMENT CK_RV |
michael@0 | 409 | NSSCKFWC_GetSlotInfo |
michael@0 | 410 | ( |
michael@0 | 411 | NSSCKFWInstance *fwInstance, |
michael@0 | 412 | CK_SLOT_ID slotID, |
michael@0 | 413 | CK_SLOT_INFO_PTR pInfo |
michael@0 | 414 | ) |
michael@0 | 415 | { |
michael@0 | 416 | CK_RV error = CKR_OK; |
michael@0 | 417 | CK_ULONG nSlots; |
michael@0 | 418 | NSSCKFWSlot **slots; |
michael@0 | 419 | NSSCKFWSlot *fwSlot; |
michael@0 | 420 | |
michael@0 | 421 | if (!fwInstance) { |
michael@0 | 422 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 423 | goto loser; |
michael@0 | 424 | } |
michael@0 | 425 | |
michael@0 | 426 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 427 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 428 | goto loser; |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | if( (slotID < 1) || (slotID > nSlots) ) { |
michael@0 | 432 | error = CKR_SLOT_ID_INVALID; |
michael@0 | 433 | goto loser; |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | if( (CK_SLOT_INFO_PTR)CK_NULL_PTR == pInfo ) { |
michael@0 | 437 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 438 | goto loser; |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | /* |
michael@0 | 442 | * A purify error here indicates caller error. |
michael@0 | 443 | */ |
michael@0 | 444 | (void)nsslibc_memset(pInfo, 0, sizeof(CK_SLOT_INFO)); |
michael@0 | 445 | |
michael@0 | 446 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 447 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 448 | goto loser; |
michael@0 | 449 | } |
michael@0 | 450 | |
michael@0 | 451 | fwSlot = slots[ slotID-1 ]; |
michael@0 | 452 | |
michael@0 | 453 | error = nssCKFWSlot_GetSlotDescription(fwSlot, pInfo->slotDescription); |
michael@0 | 454 | if( CKR_OK != error ) { |
michael@0 | 455 | goto loser; |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | error = nssCKFWSlot_GetManufacturerID(fwSlot, pInfo->manufacturerID); |
michael@0 | 459 | if( CKR_OK != error ) { |
michael@0 | 460 | goto loser; |
michael@0 | 461 | } |
michael@0 | 462 | |
michael@0 | 463 | if( nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 464 | pInfo->flags |= CKF_TOKEN_PRESENT; |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | if( nssCKFWSlot_GetRemovableDevice(fwSlot) ) { |
michael@0 | 468 | pInfo->flags |= CKF_REMOVABLE_DEVICE; |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | if( nssCKFWSlot_GetHardwareSlot(fwSlot) ) { |
michael@0 | 472 | pInfo->flags |= CKF_HW_SLOT; |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | pInfo->hardwareVersion = nssCKFWSlot_GetHardwareVersion(fwSlot); |
michael@0 | 476 | pInfo->firmwareVersion = nssCKFWSlot_GetFirmwareVersion(fwSlot); |
michael@0 | 477 | |
michael@0 | 478 | return CKR_OK; |
michael@0 | 479 | |
michael@0 | 480 | loser: |
michael@0 | 481 | switch( error ) { |
michael@0 | 482 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 483 | case CKR_DEVICE_ERROR: |
michael@0 | 484 | case CKR_FUNCTION_FAILED: |
michael@0 | 485 | case CKR_GENERAL_ERROR: |
michael@0 | 486 | case CKR_HOST_MEMORY: |
michael@0 | 487 | case CKR_SLOT_ID_INVALID: |
michael@0 | 488 | break; |
michael@0 | 489 | default: |
michael@0 | 490 | case CKR_OK: |
michael@0 | 491 | error = CKR_GENERAL_ERROR; |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | return error; |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | /* |
michael@0 | 498 | * NSSCKFWC_GetTokenInfo |
michael@0 | 499 | * |
michael@0 | 500 | */ |
michael@0 | 501 | NSS_IMPLEMENT CK_RV |
michael@0 | 502 | NSSCKFWC_GetTokenInfo |
michael@0 | 503 | ( |
michael@0 | 504 | NSSCKFWInstance *fwInstance, |
michael@0 | 505 | CK_SLOT_ID slotID, |
michael@0 | 506 | CK_TOKEN_INFO_PTR pInfo |
michael@0 | 507 | ) |
michael@0 | 508 | { |
michael@0 | 509 | CK_RV error = CKR_OK; |
michael@0 | 510 | CK_ULONG nSlots; |
michael@0 | 511 | NSSCKFWSlot **slots; |
michael@0 | 512 | NSSCKFWSlot *fwSlot; |
michael@0 | 513 | NSSCKFWToken *fwToken = (NSSCKFWToken *)NULL; |
michael@0 | 514 | |
michael@0 | 515 | if (!fwInstance) { |
michael@0 | 516 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 517 | goto loser; |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 521 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 522 | goto loser; |
michael@0 | 523 | } |
michael@0 | 524 | |
michael@0 | 525 | if( (slotID < 1) || (slotID > nSlots) ) { |
michael@0 | 526 | error = CKR_SLOT_ID_INVALID; |
michael@0 | 527 | goto loser; |
michael@0 | 528 | } |
michael@0 | 529 | |
michael@0 | 530 | if( (CK_TOKEN_INFO_PTR)CK_NULL_PTR == pInfo ) { |
michael@0 | 531 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 532 | goto loser; |
michael@0 | 533 | } |
michael@0 | 534 | |
michael@0 | 535 | /* |
michael@0 | 536 | * A purify error here indicates caller error. |
michael@0 | 537 | */ |
michael@0 | 538 | (void)nsslibc_memset(pInfo, 0, sizeof(CK_TOKEN_INFO)); |
michael@0 | 539 | |
michael@0 | 540 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 541 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 542 | goto loser; |
michael@0 | 543 | } |
michael@0 | 544 | |
michael@0 | 545 | fwSlot = slots[ slotID-1 ]; |
michael@0 | 546 | |
michael@0 | 547 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 548 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 549 | goto loser; |
michael@0 | 550 | } |
michael@0 | 551 | |
michael@0 | 552 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 553 | if (!fwToken) { |
michael@0 | 554 | goto loser; |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | error = nssCKFWToken_GetLabel(fwToken, pInfo->label); |
michael@0 | 558 | if( CKR_OK != error ) { |
michael@0 | 559 | goto loser; |
michael@0 | 560 | } |
michael@0 | 561 | |
michael@0 | 562 | error = nssCKFWToken_GetManufacturerID(fwToken, pInfo->manufacturerID); |
michael@0 | 563 | if( CKR_OK != error ) { |
michael@0 | 564 | goto loser; |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | error = nssCKFWToken_GetModel(fwToken, pInfo->model); |
michael@0 | 568 | if( CKR_OK != error ) { |
michael@0 | 569 | goto loser; |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | error = nssCKFWToken_GetSerialNumber(fwToken, pInfo->serialNumber); |
michael@0 | 573 | if( CKR_OK != error ) { |
michael@0 | 574 | goto loser; |
michael@0 | 575 | } |
michael@0 | 576 | |
michael@0 | 577 | if( nssCKFWToken_GetHasRNG(fwToken) ) { |
michael@0 | 578 | pInfo->flags |= CKF_RNG; |
michael@0 | 579 | } |
michael@0 | 580 | |
michael@0 | 581 | if( nssCKFWToken_GetIsWriteProtected(fwToken) ) { |
michael@0 | 582 | pInfo->flags |= CKF_WRITE_PROTECTED; |
michael@0 | 583 | } |
michael@0 | 584 | |
michael@0 | 585 | if( nssCKFWToken_GetLoginRequired(fwToken) ) { |
michael@0 | 586 | pInfo->flags |= CKF_LOGIN_REQUIRED; |
michael@0 | 587 | } |
michael@0 | 588 | |
michael@0 | 589 | if( nssCKFWToken_GetUserPinInitialized(fwToken) ) { |
michael@0 | 590 | pInfo->flags |= CKF_USER_PIN_INITIALIZED; |
michael@0 | 591 | } |
michael@0 | 592 | |
michael@0 | 593 | if( nssCKFWToken_GetRestoreKeyNotNeeded(fwToken) ) { |
michael@0 | 594 | pInfo->flags |= CKF_RESTORE_KEY_NOT_NEEDED; |
michael@0 | 595 | } |
michael@0 | 596 | |
michael@0 | 597 | if( nssCKFWToken_GetHasClockOnToken(fwToken) ) { |
michael@0 | 598 | pInfo->flags |= CKF_CLOCK_ON_TOKEN; |
michael@0 | 599 | } |
michael@0 | 600 | |
michael@0 | 601 | if( nssCKFWToken_GetHasProtectedAuthenticationPath(fwToken) ) { |
michael@0 | 602 | pInfo->flags |= CKF_PROTECTED_AUTHENTICATION_PATH; |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | if( nssCKFWToken_GetSupportsDualCryptoOperations(fwToken) ) { |
michael@0 | 606 | pInfo->flags |= CKF_DUAL_CRYPTO_OPERATIONS; |
michael@0 | 607 | } |
michael@0 | 608 | |
michael@0 | 609 | pInfo->ulMaxSessionCount = nssCKFWToken_GetMaxSessionCount(fwToken); |
michael@0 | 610 | pInfo->ulSessionCount = nssCKFWToken_GetSessionCount(fwToken); |
michael@0 | 611 | pInfo->ulMaxRwSessionCount = nssCKFWToken_GetMaxRwSessionCount(fwToken); |
michael@0 | 612 | pInfo->ulRwSessionCount= nssCKFWToken_GetRwSessionCount(fwToken); |
michael@0 | 613 | pInfo->ulMaxPinLen = nssCKFWToken_GetMaxPinLen(fwToken); |
michael@0 | 614 | pInfo->ulMinPinLen = nssCKFWToken_GetMinPinLen(fwToken); |
michael@0 | 615 | pInfo->ulTotalPublicMemory = nssCKFWToken_GetTotalPublicMemory(fwToken); |
michael@0 | 616 | pInfo->ulFreePublicMemory = nssCKFWToken_GetFreePublicMemory(fwToken); |
michael@0 | 617 | pInfo->ulTotalPrivateMemory = nssCKFWToken_GetTotalPrivateMemory(fwToken); |
michael@0 | 618 | pInfo->ulFreePrivateMemory = nssCKFWToken_GetFreePrivateMemory(fwToken); |
michael@0 | 619 | pInfo->hardwareVersion = nssCKFWToken_GetHardwareVersion(fwToken); |
michael@0 | 620 | pInfo->firmwareVersion = nssCKFWToken_GetFirmwareVersion(fwToken); |
michael@0 | 621 | |
michael@0 | 622 | error = nssCKFWToken_GetUTCTime(fwToken, pInfo->utcTime); |
michael@0 | 623 | if( CKR_OK != error ) { |
michael@0 | 624 | goto loser; |
michael@0 | 625 | } |
michael@0 | 626 | |
michael@0 | 627 | return CKR_OK; |
michael@0 | 628 | |
michael@0 | 629 | loser: |
michael@0 | 630 | switch( error ) { |
michael@0 | 631 | case CKR_DEVICE_REMOVED: |
michael@0 | 632 | case CKR_TOKEN_NOT_PRESENT: |
michael@0 | 633 | if (fwToken) |
michael@0 | 634 | nssCKFWToken_Destroy(fwToken); |
michael@0 | 635 | break; |
michael@0 | 636 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 637 | case CKR_DEVICE_ERROR: |
michael@0 | 638 | case CKR_DEVICE_MEMORY: |
michael@0 | 639 | case CKR_FUNCTION_FAILED: |
michael@0 | 640 | case CKR_GENERAL_ERROR: |
michael@0 | 641 | case CKR_HOST_MEMORY: |
michael@0 | 642 | case CKR_SLOT_ID_INVALID: |
michael@0 | 643 | case CKR_TOKEN_NOT_RECOGNIZED: |
michael@0 | 644 | break; |
michael@0 | 645 | default: |
michael@0 | 646 | case CKR_OK: |
michael@0 | 647 | error = CKR_GENERAL_ERROR; |
michael@0 | 648 | break; |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | return error; |
michael@0 | 652 | } |
michael@0 | 653 | |
michael@0 | 654 | /* |
michael@0 | 655 | * NSSCKFWC_WaitForSlotEvent |
michael@0 | 656 | * |
michael@0 | 657 | */ |
michael@0 | 658 | NSS_IMPLEMENT CK_RV |
michael@0 | 659 | NSSCKFWC_WaitForSlotEvent |
michael@0 | 660 | ( |
michael@0 | 661 | NSSCKFWInstance *fwInstance, |
michael@0 | 662 | CK_FLAGS flags, |
michael@0 | 663 | CK_SLOT_ID_PTR pSlot, |
michael@0 | 664 | CK_VOID_PTR pReserved |
michael@0 | 665 | ) |
michael@0 | 666 | { |
michael@0 | 667 | CK_RV error = CKR_OK; |
michael@0 | 668 | CK_ULONG nSlots; |
michael@0 | 669 | CK_BBOOL block; |
michael@0 | 670 | NSSCKFWSlot **slots; |
michael@0 | 671 | NSSCKFWSlot *fwSlot; |
michael@0 | 672 | CK_ULONG i; |
michael@0 | 673 | |
michael@0 | 674 | if (!fwInstance) { |
michael@0 | 675 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 676 | goto loser; |
michael@0 | 677 | } |
michael@0 | 678 | |
michael@0 | 679 | if( flags & ~CKF_DONT_BLOCK ) { |
michael@0 | 680 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 681 | goto loser; |
michael@0 | 682 | } |
michael@0 | 683 | |
michael@0 | 684 | block = (flags & CKF_DONT_BLOCK) ? CK_TRUE : CK_FALSE; |
michael@0 | 685 | |
michael@0 | 686 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 687 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 688 | goto loser; |
michael@0 | 689 | } |
michael@0 | 690 | |
michael@0 | 691 | if( (CK_SLOT_ID_PTR)CK_NULL_PTR == pSlot ) { |
michael@0 | 692 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 693 | goto loser; |
michael@0 | 694 | } |
michael@0 | 695 | |
michael@0 | 696 | if( (CK_VOID_PTR)CK_NULL_PTR != pReserved ) { |
michael@0 | 697 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 698 | goto loser; |
michael@0 | 699 | } |
michael@0 | 700 | |
michael@0 | 701 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 702 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 703 | goto loser; |
michael@0 | 704 | } |
michael@0 | 705 | |
michael@0 | 706 | fwSlot = nssCKFWInstance_WaitForSlotEvent(fwInstance, block, &error); |
michael@0 | 707 | if (!fwSlot) { |
michael@0 | 708 | goto loser; |
michael@0 | 709 | } |
michael@0 | 710 | |
michael@0 | 711 | for( i = 0; i < nSlots; i++ ) { |
michael@0 | 712 | if( fwSlot == slots[i] ) { |
michael@0 | 713 | *pSlot = (CK_SLOT_ID)(CK_ULONG)(i+1); |
michael@0 | 714 | return CKR_OK; |
michael@0 | 715 | } |
michael@0 | 716 | } |
michael@0 | 717 | |
michael@0 | 718 | error = CKR_GENERAL_ERROR; /* returned something not in the slot list */ |
michael@0 | 719 | |
michael@0 | 720 | loser: |
michael@0 | 721 | switch( error ) { |
michael@0 | 722 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 723 | case CKR_FUNCTION_FAILED: |
michael@0 | 724 | case CKR_GENERAL_ERROR: |
michael@0 | 725 | case CKR_HOST_MEMORY: |
michael@0 | 726 | case CKR_NO_EVENT: |
michael@0 | 727 | break; |
michael@0 | 728 | default: |
michael@0 | 729 | case CKR_OK: |
michael@0 | 730 | error = CKR_GENERAL_ERROR; |
michael@0 | 731 | break; |
michael@0 | 732 | } |
michael@0 | 733 | |
michael@0 | 734 | return error; |
michael@0 | 735 | } |
michael@0 | 736 | |
michael@0 | 737 | /* |
michael@0 | 738 | * NSSCKFWC_GetMechanismList |
michael@0 | 739 | * |
michael@0 | 740 | */ |
michael@0 | 741 | NSS_IMPLEMENT CK_RV |
michael@0 | 742 | NSSCKFWC_GetMechanismList |
michael@0 | 743 | ( |
michael@0 | 744 | NSSCKFWInstance *fwInstance, |
michael@0 | 745 | CK_SLOT_ID slotID, |
michael@0 | 746 | CK_MECHANISM_TYPE_PTR pMechanismList, |
michael@0 | 747 | CK_ULONG_PTR pulCount |
michael@0 | 748 | ) |
michael@0 | 749 | { |
michael@0 | 750 | CK_RV error = CKR_OK; |
michael@0 | 751 | CK_ULONG nSlots; |
michael@0 | 752 | NSSCKFWSlot **slots; |
michael@0 | 753 | NSSCKFWSlot *fwSlot; |
michael@0 | 754 | NSSCKFWToken *fwToken = (NSSCKFWToken *)NULL; |
michael@0 | 755 | CK_ULONG count; |
michael@0 | 756 | |
michael@0 | 757 | if (!fwInstance) { |
michael@0 | 758 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 759 | goto loser; |
michael@0 | 760 | } |
michael@0 | 761 | |
michael@0 | 762 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 763 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 764 | goto loser; |
michael@0 | 765 | } |
michael@0 | 766 | |
michael@0 | 767 | if( (slotID < 1) || (slotID > nSlots) ) { |
michael@0 | 768 | error = CKR_SLOT_ID_INVALID; |
michael@0 | 769 | goto loser; |
michael@0 | 770 | } |
michael@0 | 771 | |
michael@0 | 772 | if( (CK_ULONG_PTR)CK_NULL_PTR == pulCount ) { |
michael@0 | 773 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 774 | goto loser; |
michael@0 | 775 | } |
michael@0 | 776 | |
michael@0 | 777 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 778 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 779 | goto loser; |
michael@0 | 780 | } |
michael@0 | 781 | |
michael@0 | 782 | fwSlot = slots[ slotID-1 ]; |
michael@0 | 783 | |
michael@0 | 784 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 785 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 786 | goto loser; |
michael@0 | 787 | } |
michael@0 | 788 | |
michael@0 | 789 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 790 | if (!fwToken) { |
michael@0 | 791 | goto loser; |
michael@0 | 792 | } |
michael@0 | 793 | |
michael@0 | 794 | count = nssCKFWToken_GetMechanismCount(fwToken); |
michael@0 | 795 | |
michael@0 | 796 | if( (CK_MECHANISM_TYPE_PTR)CK_NULL_PTR == pMechanismList ) { |
michael@0 | 797 | *pulCount = count; |
michael@0 | 798 | return CKR_OK; |
michael@0 | 799 | } |
michael@0 | 800 | |
michael@0 | 801 | if( *pulCount < count ) { |
michael@0 | 802 | *pulCount = count; |
michael@0 | 803 | error = CKR_BUFFER_TOO_SMALL; |
michael@0 | 804 | goto loser; |
michael@0 | 805 | } |
michael@0 | 806 | |
michael@0 | 807 | /* |
michael@0 | 808 | * A purify error here indicates caller error. |
michael@0 | 809 | */ |
michael@0 | 810 | (void)nsslibc_memset(pMechanismList, 0, *pulCount * sizeof(CK_MECHANISM_TYPE)); |
michael@0 | 811 | |
michael@0 | 812 | *pulCount = count; |
michael@0 | 813 | |
michael@0 | 814 | if( 0 != count ) { |
michael@0 | 815 | error = nssCKFWToken_GetMechanismTypes(fwToken, pMechanismList); |
michael@0 | 816 | } else { |
michael@0 | 817 | error = CKR_OK; |
michael@0 | 818 | } |
michael@0 | 819 | |
michael@0 | 820 | if( CKR_OK == error ) { |
michael@0 | 821 | return CKR_OK; |
michael@0 | 822 | } |
michael@0 | 823 | |
michael@0 | 824 | loser: |
michael@0 | 825 | switch( error ) { |
michael@0 | 826 | case CKR_DEVICE_REMOVED: |
michael@0 | 827 | case CKR_TOKEN_NOT_PRESENT: |
michael@0 | 828 | if (fwToken) |
michael@0 | 829 | nssCKFWToken_Destroy(fwToken); |
michael@0 | 830 | break; |
michael@0 | 831 | case CKR_ARGUMENTS_BAD: |
michael@0 | 832 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 833 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 834 | case CKR_DEVICE_ERROR: |
michael@0 | 835 | case CKR_DEVICE_MEMORY: |
michael@0 | 836 | case CKR_FUNCTION_FAILED: |
michael@0 | 837 | case CKR_GENERAL_ERROR: |
michael@0 | 838 | case CKR_HOST_MEMORY: |
michael@0 | 839 | case CKR_SLOT_ID_INVALID: |
michael@0 | 840 | case CKR_TOKEN_NOT_RECOGNIZED: |
michael@0 | 841 | break; |
michael@0 | 842 | default: |
michael@0 | 843 | case CKR_OK: |
michael@0 | 844 | error = CKR_GENERAL_ERROR; |
michael@0 | 845 | break; |
michael@0 | 846 | } |
michael@0 | 847 | |
michael@0 | 848 | return error; |
michael@0 | 849 | } |
michael@0 | 850 | |
michael@0 | 851 | /* |
michael@0 | 852 | * NSSCKFWC_GetMechanismInfo |
michael@0 | 853 | * |
michael@0 | 854 | */ |
michael@0 | 855 | NSS_IMPLEMENT CK_RV |
michael@0 | 856 | NSSCKFWC_GetMechanismInfo |
michael@0 | 857 | ( |
michael@0 | 858 | NSSCKFWInstance *fwInstance, |
michael@0 | 859 | CK_SLOT_ID slotID, |
michael@0 | 860 | CK_MECHANISM_TYPE type, |
michael@0 | 861 | CK_MECHANISM_INFO_PTR pInfo |
michael@0 | 862 | ) |
michael@0 | 863 | { |
michael@0 | 864 | CK_RV error = CKR_OK; |
michael@0 | 865 | CK_ULONG nSlots; |
michael@0 | 866 | NSSCKFWSlot **slots; |
michael@0 | 867 | NSSCKFWSlot *fwSlot; |
michael@0 | 868 | NSSCKFWToken *fwToken = (NSSCKFWToken *)NULL; |
michael@0 | 869 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 870 | |
michael@0 | 871 | if (!fwInstance) { |
michael@0 | 872 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 873 | goto loser; |
michael@0 | 874 | } |
michael@0 | 875 | |
michael@0 | 876 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 877 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 878 | goto loser; |
michael@0 | 879 | } |
michael@0 | 880 | |
michael@0 | 881 | if( (slotID < 1) || (slotID > nSlots) ) { |
michael@0 | 882 | error = CKR_SLOT_ID_INVALID; |
michael@0 | 883 | goto loser; |
michael@0 | 884 | } |
michael@0 | 885 | |
michael@0 | 886 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 887 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 888 | goto loser; |
michael@0 | 889 | } |
michael@0 | 890 | |
michael@0 | 891 | fwSlot = slots[ slotID-1 ]; |
michael@0 | 892 | |
michael@0 | 893 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 894 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 895 | goto loser; |
michael@0 | 896 | } |
michael@0 | 897 | |
michael@0 | 898 | if( (CK_MECHANISM_INFO_PTR)CK_NULL_PTR == pInfo ) { |
michael@0 | 899 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 900 | goto loser; |
michael@0 | 901 | } |
michael@0 | 902 | |
michael@0 | 903 | /* |
michael@0 | 904 | * A purify error here indicates caller error. |
michael@0 | 905 | */ |
michael@0 | 906 | (void)nsslibc_memset(pInfo, 0, sizeof(CK_MECHANISM_INFO)); |
michael@0 | 907 | |
michael@0 | 908 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 909 | if (!fwToken) { |
michael@0 | 910 | goto loser; |
michael@0 | 911 | } |
michael@0 | 912 | |
michael@0 | 913 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, type, &error); |
michael@0 | 914 | if (!fwMechanism) { |
michael@0 | 915 | goto loser; |
michael@0 | 916 | } |
michael@0 | 917 | |
michael@0 | 918 | pInfo->ulMinKeySize = nssCKFWMechanism_GetMinKeySize(fwMechanism, &error); |
michael@0 | 919 | pInfo->ulMaxKeySize = nssCKFWMechanism_GetMaxKeySize(fwMechanism, &error); |
michael@0 | 920 | |
michael@0 | 921 | if( nssCKFWMechanism_GetInHardware(fwMechanism, &error) ) { |
michael@0 | 922 | pInfo->flags |= CKF_HW; |
michael@0 | 923 | } |
michael@0 | 924 | if( nssCKFWMechanism_GetCanEncrypt(fwMechanism, &error) ) { |
michael@0 | 925 | pInfo->flags |= CKF_ENCRYPT; |
michael@0 | 926 | } |
michael@0 | 927 | if( nssCKFWMechanism_GetCanDecrypt(fwMechanism, &error) ) { |
michael@0 | 928 | pInfo->flags |= CKF_DECRYPT; |
michael@0 | 929 | } |
michael@0 | 930 | if( nssCKFWMechanism_GetCanDigest(fwMechanism, &error) ) { |
michael@0 | 931 | pInfo->flags |= CKF_DIGEST; |
michael@0 | 932 | } |
michael@0 | 933 | if( nssCKFWMechanism_GetCanSign(fwMechanism, &error) ) { |
michael@0 | 934 | pInfo->flags |= CKF_SIGN; |
michael@0 | 935 | } |
michael@0 | 936 | if( nssCKFWMechanism_GetCanSignRecover(fwMechanism, &error) ) { |
michael@0 | 937 | pInfo->flags |= CKF_SIGN_RECOVER; |
michael@0 | 938 | } |
michael@0 | 939 | if( nssCKFWMechanism_GetCanVerify(fwMechanism, &error) ) { |
michael@0 | 940 | pInfo->flags |= CKF_VERIFY; |
michael@0 | 941 | } |
michael@0 | 942 | if( nssCKFWMechanism_GetCanVerifyRecover(fwMechanism, &error) ) { |
michael@0 | 943 | pInfo->flags |= CKF_VERIFY_RECOVER; |
michael@0 | 944 | } |
michael@0 | 945 | if( nssCKFWMechanism_GetCanGenerate(fwMechanism, &error) ) { |
michael@0 | 946 | pInfo->flags |= CKF_GENERATE; |
michael@0 | 947 | } |
michael@0 | 948 | if( nssCKFWMechanism_GetCanGenerateKeyPair(fwMechanism, &error) ) { |
michael@0 | 949 | pInfo->flags |= CKF_GENERATE_KEY_PAIR; |
michael@0 | 950 | } |
michael@0 | 951 | if( nssCKFWMechanism_GetCanWrap(fwMechanism, &error) ) { |
michael@0 | 952 | pInfo->flags |= CKF_WRAP; |
michael@0 | 953 | } |
michael@0 | 954 | if( nssCKFWMechanism_GetCanUnwrap(fwMechanism, &error) ) { |
michael@0 | 955 | pInfo->flags |= CKF_UNWRAP; |
michael@0 | 956 | } |
michael@0 | 957 | if( nssCKFWMechanism_GetCanDerive(fwMechanism, &error) ) { |
michael@0 | 958 | pInfo->flags |= CKF_DERIVE; |
michael@0 | 959 | } |
michael@0 | 960 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 961 | |
michael@0 | 962 | return error; |
michael@0 | 963 | |
michael@0 | 964 | loser: |
michael@0 | 965 | switch( error ) { |
michael@0 | 966 | case CKR_DEVICE_REMOVED: |
michael@0 | 967 | case CKR_TOKEN_NOT_PRESENT: |
michael@0 | 968 | if (fwToken) |
michael@0 | 969 | nssCKFWToken_Destroy(fwToken); |
michael@0 | 970 | break; |
michael@0 | 971 | case CKR_ARGUMENTS_BAD: |
michael@0 | 972 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 973 | case CKR_DEVICE_ERROR: |
michael@0 | 974 | case CKR_DEVICE_MEMORY: |
michael@0 | 975 | case CKR_FUNCTION_FAILED: |
michael@0 | 976 | case CKR_GENERAL_ERROR: |
michael@0 | 977 | case CKR_HOST_MEMORY: |
michael@0 | 978 | case CKR_MECHANISM_INVALID: |
michael@0 | 979 | case CKR_SLOT_ID_INVALID: |
michael@0 | 980 | case CKR_TOKEN_NOT_RECOGNIZED: |
michael@0 | 981 | break; |
michael@0 | 982 | default: |
michael@0 | 983 | case CKR_OK: |
michael@0 | 984 | error = CKR_GENERAL_ERROR; |
michael@0 | 985 | break; |
michael@0 | 986 | } |
michael@0 | 987 | |
michael@0 | 988 | return error; |
michael@0 | 989 | } |
michael@0 | 990 | |
michael@0 | 991 | /* |
michael@0 | 992 | * NSSCKFWC_InitToken |
michael@0 | 993 | * |
michael@0 | 994 | */ |
michael@0 | 995 | NSS_IMPLEMENT CK_RV |
michael@0 | 996 | NSSCKFWC_InitToken |
michael@0 | 997 | ( |
michael@0 | 998 | NSSCKFWInstance *fwInstance, |
michael@0 | 999 | CK_SLOT_ID slotID, |
michael@0 | 1000 | CK_CHAR_PTR pPin, |
michael@0 | 1001 | CK_ULONG ulPinLen, |
michael@0 | 1002 | CK_CHAR_PTR pLabel |
michael@0 | 1003 | ) |
michael@0 | 1004 | { |
michael@0 | 1005 | CK_RV error = CKR_OK; |
michael@0 | 1006 | CK_ULONG nSlots; |
michael@0 | 1007 | NSSCKFWSlot **slots; |
michael@0 | 1008 | NSSCKFWSlot *fwSlot; |
michael@0 | 1009 | NSSCKFWToken *fwToken = (NSSCKFWToken *)NULL; |
michael@0 | 1010 | NSSItem pin; |
michael@0 | 1011 | NSSUTF8 *label; |
michael@0 | 1012 | |
michael@0 | 1013 | if (!fwInstance) { |
michael@0 | 1014 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1015 | goto loser; |
michael@0 | 1016 | } |
michael@0 | 1017 | |
michael@0 | 1018 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 1019 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 1020 | goto loser; |
michael@0 | 1021 | } |
michael@0 | 1022 | |
michael@0 | 1023 | if( (slotID < 1) || (slotID > nSlots) ) { |
michael@0 | 1024 | error = CKR_SLOT_ID_INVALID; |
michael@0 | 1025 | goto loser; |
michael@0 | 1026 | } |
michael@0 | 1027 | |
michael@0 | 1028 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 1029 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 1030 | goto loser; |
michael@0 | 1031 | } |
michael@0 | 1032 | |
michael@0 | 1033 | fwSlot = slots[ slotID-1 ]; |
michael@0 | 1034 | |
michael@0 | 1035 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 1036 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 1037 | goto loser; |
michael@0 | 1038 | } |
michael@0 | 1039 | |
michael@0 | 1040 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 1041 | if (!fwToken) { |
michael@0 | 1042 | goto loser; |
michael@0 | 1043 | } |
michael@0 | 1044 | |
michael@0 | 1045 | pin.size = (PRUint32)ulPinLen; |
michael@0 | 1046 | pin.data = (void *)pPin; |
michael@0 | 1047 | label = (NSSUTF8 *)pLabel; /* identity conversion */ |
michael@0 | 1048 | |
michael@0 | 1049 | error = nssCKFWToken_InitToken(fwToken, &pin, label); |
michael@0 | 1050 | if( CKR_OK != error ) { |
michael@0 | 1051 | goto loser; |
michael@0 | 1052 | } |
michael@0 | 1053 | |
michael@0 | 1054 | return CKR_OK; |
michael@0 | 1055 | |
michael@0 | 1056 | loser: |
michael@0 | 1057 | switch( error ) { |
michael@0 | 1058 | case CKR_DEVICE_REMOVED: |
michael@0 | 1059 | case CKR_TOKEN_NOT_PRESENT: |
michael@0 | 1060 | if (fwToken) |
michael@0 | 1061 | nssCKFWToken_Destroy(fwToken); |
michael@0 | 1062 | break; |
michael@0 | 1063 | case CKR_ARGUMENTS_BAD: |
michael@0 | 1064 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1065 | case CKR_DEVICE_ERROR: |
michael@0 | 1066 | case CKR_DEVICE_MEMORY: |
michael@0 | 1067 | case CKR_FUNCTION_FAILED: |
michael@0 | 1068 | case CKR_GENERAL_ERROR: |
michael@0 | 1069 | case CKR_HOST_MEMORY: |
michael@0 | 1070 | case CKR_PIN_INCORRECT: |
michael@0 | 1071 | case CKR_PIN_LOCKED: |
michael@0 | 1072 | case CKR_SESSION_EXISTS: |
michael@0 | 1073 | case CKR_SLOT_ID_INVALID: |
michael@0 | 1074 | case CKR_TOKEN_NOT_RECOGNIZED: |
michael@0 | 1075 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 1076 | break; |
michael@0 | 1077 | default: |
michael@0 | 1078 | case CKR_OK: |
michael@0 | 1079 | error = CKR_GENERAL_ERROR; |
michael@0 | 1080 | break; |
michael@0 | 1081 | } |
michael@0 | 1082 | |
michael@0 | 1083 | return error; |
michael@0 | 1084 | } |
michael@0 | 1085 | |
michael@0 | 1086 | /* |
michael@0 | 1087 | * NSSCKFWC_InitPIN |
michael@0 | 1088 | * |
michael@0 | 1089 | */ |
michael@0 | 1090 | NSS_IMPLEMENT CK_RV |
michael@0 | 1091 | NSSCKFWC_InitPIN |
michael@0 | 1092 | ( |
michael@0 | 1093 | NSSCKFWInstance *fwInstance, |
michael@0 | 1094 | CK_SESSION_HANDLE hSession, |
michael@0 | 1095 | CK_CHAR_PTR pPin, |
michael@0 | 1096 | CK_ULONG ulPinLen |
michael@0 | 1097 | ) |
michael@0 | 1098 | { |
michael@0 | 1099 | CK_RV error = CKR_OK; |
michael@0 | 1100 | NSSCKFWSession *fwSession; |
michael@0 | 1101 | NSSItem pin, *arg; |
michael@0 | 1102 | |
michael@0 | 1103 | if (!fwInstance) { |
michael@0 | 1104 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1105 | goto loser; |
michael@0 | 1106 | } |
michael@0 | 1107 | |
michael@0 | 1108 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1109 | if (!fwSession) { |
michael@0 | 1110 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1111 | goto loser; |
michael@0 | 1112 | } |
michael@0 | 1113 | |
michael@0 | 1114 | if( (CK_CHAR_PTR)CK_NULL_PTR == pPin ) { |
michael@0 | 1115 | arg = (NSSItem *)NULL; |
michael@0 | 1116 | } else { |
michael@0 | 1117 | arg = &pin; |
michael@0 | 1118 | pin.size = (PRUint32)ulPinLen; |
michael@0 | 1119 | pin.data = (void *)pPin; |
michael@0 | 1120 | } |
michael@0 | 1121 | |
michael@0 | 1122 | error = nssCKFWSession_InitPIN(fwSession, arg); |
michael@0 | 1123 | if( CKR_OK != error ) { |
michael@0 | 1124 | goto loser; |
michael@0 | 1125 | } |
michael@0 | 1126 | |
michael@0 | 1127 | return CKR_OK; |
michael@0 | 1128 | |
michael@0 | 1129 | loser: |
michael@0 | 1130 | switch( error ) { |
michael@0 | 1131 | case CKR_SESSION_CLOSED: |
michael@0 | 1132 | /* destroy session? */ |
michael@0 | 1133 | break; |
michael@0 | 1134 | case CKR_DEVICE_REMOVED: |
michael@0 | 1135 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1136 | break; |
michael@0 | 1137 | case CKR_ARGUMENTS_BAD: |
michael@0 | 1138 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1139 | case CKR_DEVICE_ERROR: |
michael@0 | 1140 | case CKR_DEVICE_MEMORY: |
michael@0 | 1141 | case CKR_FUNCTION_FAILED: |
michael@0 | 1142 | case CKR_GENERAL_ERROR: |
michael@0 | 1143 | case CKR_HOST_MEMORY: |
michael@0 | 1144 | case CKR_PIN_INVALID: |
michael@0 | 1145 | case CKR_PIN_LEN_RANGE: |
michael@0 | 1146 | case CKR_SESSION_READ_ONLY: |
michael@0 | 1147 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1148 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 1149 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 1150 | break; |
michael@0 | 1151 | default: |
michael@0 | 1152 | case CKR_OK: |
michael@0 | 1153 | error = CKR_GENERAL_ERROR; |
michael@0 | 1154 | break; |
michael@0 | 1155 | } |
michael@0 | 1156 | |
michael@0 | 1157 | return error; |
michael@0 | 1158 | } |
michael@0 | 1159 | |
michael@0 | 1160 | /* |
michael@0 | 1161 | * NSSCKFWC_SetPIN |
michael@0 | 1162 | * |
michael@0 | 1163 | */ |
michael@0 | 1164 | NSS_IMPLEMENT CK_RV |
michael@0 | 1165 | NSSCKFWC_SetPIN |
michael@0 | 1166 | ( |
michael@0 | 1167 | NSSCKFWInstance *fwInstance, |
michael@0 | 1168 | CK_SESSION_HANDLE hSession, |
michael@0 | 1169 | CK_CHAR_PTR pOldPin, |
michael@0 | 1170 | CK_ULONG ulOldLen, |
michael@0 | 1171 | CK_CHAR_PTR pNewPin, |
michael@0 | 1172 | CK_ULONG ulNewLen |
michael@0 | 1173 | ) |
michael@0 | 1174 | { |
michael@0 | 1175 | CK_RV error = CKR_OK; |
michael@0 | 1176 | NSSCKFWSession *fwSession; |
michael@0 | 1177 | NSSItem oldPin, newPin, *oldArg, *newArg; |
michael@0 | 1178 | |
michael@0 | 1179 | if (!fwInstance) { |
michael@0 | 1180 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1181 | goto loser; |
michael@0 | 1182 | } |
michael@0 | 1183 | |
michael@0 | 1184 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1185 | if (!fwSession) { |
michael@0 | 1186 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1187 | goto loser; |
michael@0 | 1188 | } |
michael@0 | 1189 | |
michael@0 | 1190 | if( (CK_CHAR_PTR)CK_NULL_PTR == pOldPin ) { |
michael@0 | 1191 | oldArg = (NSSItem *)NULL; |
michael@0 | 1192 | } else { |
michael@0 | 1193 | oldArg = &oldPin; |
michael@0 | 1194 | oldPin.size = (PRUint32)ulOldLen; |
michael@0 | 1195 | oldPin.data = (void *)pOldPin; |
michael@0 | 1196 | } |
michael@0 | 1197 | |
michael@0 | 1198 | if( (CK_CHAR_PTR)CK_NULL_PTR == pNewPin ) { |
michael@0 | 1199 | newArg = (NSSItem *)NULL; |
michael@0 | 1200 | } else { |
michael@0 | 1201 | newArg = &newPin; |
michael@0 | 1202 | newPin.size = (PRUint32)ulNewLen; |
michael@0 | 1203 | newPin.data = (void *)pNewPin; |
michael@0 | 1204 | } |
michael@0 | 1205 | |
michael@0 | 1206 | error = nssCKFWSession_SetPIN(fwSession, oldArg, newArg); |
michael@0 | 1207 | if( CKR_OK != error ) { |
michael@0 | 1208 | goto loser; |
michael@0 | 1209 | } |
michael@0 | 1210 | |
michael@0 | 1211 | return CKR_OK; |
michael@0 | 1212 | |
michael@0 | 1213 | loser: |
michael@0 | 1214 | switch( error ) { |
michael@0 | 1215 | case CKR_SESSION_CLOSED: |
michael@0 | 1216 | /* destroy session? */ |
michael@0 | 1217 | break; |
michael@0 | 1218 | case CKR_DEVICE_REMOVED: |
michael@0 | 1219 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1220 | break; |
michael@0 | 1221 | case CKR_ARGUMENTS_BAD: |
michael@0 | 1222 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1223 | case CKR_DEVICE_ERROR: |
michael@0 | 1224 | case CKR_DEVICE_MEMORY: |
michael@0 | 1225 | case CKR_FUNCTION_FAILED: |
michael@0 | 1226 | case CKR_GENERAL_ERROR: |
michael@0 | 1227 | case CKR_HOST_MEMORY: |
michael@0 | 1228 | case CKR_PIN_INCORRECT: |
michael@0 | 1229 | case CKR_PIN_INVALID: |
michael@0 | 1230 | case CKR_PIN_LEN_RANGE: |
michael@0 | 1231 | case CKR_PIN_LOCKED: |
michael@0 | 1232 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1233 | case CKR_SESSION_READ_ONLY: |
michael@0 | 1234 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 1235 | break; |
michael@0 | 1236 | default: |
michael@0 | 1237 | case CKR_OK: |
michael@0 | 1238 | error = CKR_GENERAL_ERROR; |
michael@0 | 1239 | break; |
michael@0 | 1240 | } |
michael@0 | 1241 | |
michael@0 | 1242 | return error; |
michael@0 | 1243 | } |
michael@0 | 1244 | |
michael@0 | 1245 | /* |
michael@0 | 1246 | * NSSCKFWC_OpenSession |
michael@0 | 1247 | * |
michael@0 | 1248 | */ |
michael@0 | 1249 | NSS_IMPLEMENT CK_RV |
michael@0 | 1250 | NSSCKFWC_OpenSession |
michael@0 | 1251 | ( |
michael@0 | 1252 | NSSCKFWInstance *fwInstance, |
michael@0 | 1253 | CK_SLOT_ID slotID, |
michael@0 | 1254 | CK_FLAGS flags, |
michael@0 | 1255 | CK_VOID_PTR pApplication, |
michael@0 | 1256 | CK_NOTIFY Notify, |
michael@0 | 1257 | CK_SESSION_HANDLE_PTR phSession |
michael@0 | 1258 | ) |
michael@0 | 1259 | { |
michael@0 | 1260 | CK_RV error = CKR_OK; |
michael@0 | 1261 | CK_ULONG nSlots; |
michael@0 | 1262 | NSSCKFWSlot **slots; |
michael@0 | 1263 | NSSCKFWSlot *fwSlot; |
michael@0 | 1264 | NSSCKFWToken *fwToken = (NSSCKFWToken *)NULL; |
michael@0 | 1265 | NSSCKFWSession *fwSession; |
michael@0 | 1266 | CK_BBOOL rw; |
michael@0 | 1267 | |
michael@0 | 1268 | if (!fwInstance) { |
michael@0 | 1269 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1270 | goto loser; |
michael@0 | 1271 | } |
michael@0 | 1272 | |
michael@0 | 1273 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 1274 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 1275 | goto loser; |
michael@0 | 1276 | } |
michael@0 | 1277 | |
michael@0 | 1278 | if( (slotID < 1) || (slotID > nSlots) ) { |
michael@0 | 1279 | error = CKR_SLOT_ID_INVALID; |
michael@0 | 1280 | goto loser; |
michael@0 | 1281 | } |
michael@0 | 1282 | |
michael@0 | 1283 | if( flags & CKF_RW_SESSION ) { |
michael@0 | 1284 | rw = CK_TRUE; |
michael@0 | 1285 | } else { |
michael@0 | 1286 | rw = CK_FALSE; |
michael@0 | 1287 | } |
michael@0 | 1288 | |
michael@0 | 1289 | if( flags & CKF_SERIAL_SESSION ) { |
michael@0 | 1290 | ; |
michael@0 | 1291 | } else { |
michael@0 | 1292 | error = CKR_SESSION_PARALLEL_NOT_SUPPORTED; |
michael@0 | 1293 | goto loser; |
michael@0 | 1294 | } |
michael@0 | 1295 | |
michael@0 | 1296 | if( flags & ~(CKF_RW_SESSION|CKF_SERIAL_SESSION) ) { |
michael@0 | 1297 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 1298 | goto loser; |
michael@0 | 1299 | } |
michael@0 | 1300 | |
michael@0 | 1301 | if( (CK_SESSION_HANDLE_PTR)CK_NULL_PTR == phSession ) { |
michael@0 | 1302 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 1303 | goto loser; |
michael@0 | 1304 | } |
michael@0 | 1305 | |
michael@0 | 1306 | /* |
michael@0 | 1307 | * A purify error here indicates caller error. |
michael@0 | 1308 | */ |
michael@0 | 1309 | *phSession = (CK_SESSION_HANDLE)0; |
michael@0 | 1310 | |
michael@0 | 1311 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 1312 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 1313 | goto loser; |
michael@0 | 1314 | } |
michael@0 | 1315 | |
michael@0 | 1316 | fwSlot = slots[ slotID-1 ]; |
michael@0 | 1317 | |
michael@0 | 1318 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 1319 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 1320 | goto loser; |
michael@0 | 1321 | } |
michael@0 | 1322 | |
michael@0 | 1323 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 1324 | if (!fwToken) { |
michael@0 | 1325 | goto loser; |
michael@0 | 1326 | } |
michael@0 | 1327 | |
michael@0 | 1328 | fwSession = nssCKFWToken_OpenSession(fwToken, rw, pApplication, |
michael@0 | 1329 | Notify, &error); |
michael@0 | 1330 | if (!fwSession) { |
michael@0 | 1331 | goto loser; |
michael@0 | 1332 | } |
michael@0 | 1333 | |
michael@0 | 1334 | *phSession = nssCKFWInstance_CreateSessionHandle(fwInstance, |
michael@0 | 1335 | fwSession, &error); |
michael@0 | 1336 | if( (CK_SESSION_HANDLE)0 == *phSession ) { |
michael@0 | 1337 | goto loser; |
michael@0 | 1338 | } |
michael@0 | 1339 | |
michael@0 | 1340 | return CKR_OK; |
michael@0 | 1341 | |
michael@0 | 1342 | loser: |
michael@0 | 1343 | switch( error ) { |
michael@0 | 1344 | case CKR_SESSION_CLOSED: |
michael@0 | 1345 | /* destroy session? */ |
michael@0 | 1346 | break; |
michael@0 | 1347 | case CKR_DEVICE_REMOVED: |
michael@0 | 1348 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1349 | break; |
michael@0 | 1350 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1351 | case CKR_DEVICE_ERROR: |
michael@0 | 1352 | case CKR_DEVICE_MEMORY: |
michael@0 | 1353 | case CKR_FUNCTION_FAILED: |
michael@0 | 1354 | case CKR_GENERAL_ERROR: |
michael@0 | 1355 | case CKR_HOST_MEMORY: |
michael@0 | 1356 | case CKR_SESSION_COUNT: |
michael@0 | 1357 | case CKR_SESSION_EXISTS: |
michael@0 | 1358 | case CKR_SESSION_PARALLEL_NOT_SUPPORTED: |
michael@0 | 1359 | case CKR_SESSION_READ_WRITE_SO_EXISTS: |
michael@0 | 1360 | case CKR_SLOT_ID_INVALID: |
michael@0 | 1361 | case CKR_TOKEN_NOT_PRESENT: |
michael@0 | 1362 | case CKR_TOKEN_NOT_RECOGNIZED: |
michael@0 | 1363 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 1364 | break; |
michael@0 | 1365 | default: |
michael@0 | 1366 | case CKR_OK: |
michael@0 | 1367 | error = CKR_GENERAL_ERROR; |
michael@0 | 1368 | break; |
michael@0 | 1369 | } |
michael@0 | 1370 | |
michael@0 | 1371 | return error; |
michael@0 | 1372 | } |
michael@0 | 1373 | |
michael@0 | 1374 | /* |
michael@0 | 1375 | * NSSCKFWC_CloseSession |
michael@0 | 1376 | * |
michael@0 | 1377 | */ |
michael@0 | 1378 | NSS_IMPLEMENT CK_RV |
michael@0 | 1379 | NSSCKFWC_CloseSession |
michael@0 | 1380 | ( |
michael@0 | 1381 | NSSCKFWInstance *fwInstance, |
michael@0 | 1382 | CK_SESSION_HANDLE hSession |
michael@0 | 1383 | ) |
michael@0 | 1384 | { |
michael@0 | 1385 | CK_RV error = CKR_OK; |
michael@0 | 1386 | NSSCKFWSession *fwSession; |
michael@0 | 1387 | |
michael@0 | 1388 | if (!fwInstance) { |
michael@0 | 1389 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1390 | goto loser; |
michael@0 | 1391 | } |
michael@0 | 1392 | |
michael@0 | 1393 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1394 | if (!fwSession) { |
michael@0 | 1395 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1396 | goto loser; |
michael@0 | 1397 | } |
michael@0 | 1398 | |
michael@0 | 1399 | nssCKFWInstance_DestroySessionHandle(fwInstance, hSession); |
michael@0 | 1400 | error = nssCKFWSession_Destroy(fwSession, CK_TRUE); |
michael@0 | 1401 | |
michael@0 | 1402 | if( CKR_OK != error ) { |
michael@0 | 1403 | goto loser; |
michael@0 | 1404 | } |
michael@0 | 1405 | |
michael@0 | 1406 | return CKR_OK; |
michael@0 | 1407 | |
michael@0 | 1408 | loser: |
michael@0 | 1409 | switch( error ) { |
michael@0 | 1410 | case CKR_SESSION_CLOSED: |
michael@0 | 1411 | /* destroy session? */ |
michael@0 | 1412 | break; |
michael@0 | 1413 | case CKR_DEVICE_REMOVED: |
michael@0 | 1414 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1415 | break; |
michael@0 | 1416 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1417 | case CKR_DEVICE_ERROR: |
michael@0 | 1418 | case CKR_DEVICE_MEMORY: |
michael@0 | 1419 | case CKR_FUNCTION_FAILED: |
michael@0 | 1420 | case CKR_GENERAL_ERROR: |
michael@0 | 1421 | case CKR_HOST_MEMORY: |
michael@0 | 1422 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1423 | break; |
michael@0 | 1424 | default: |
michael@0 | 1425 | case CKR_OK: |
michael@0 | 1426 | error = CKR_GENERAL_ERROR; |
michael@0 | 1427 | break; |
michael@0 | 1428 | } |
michael@0 | 1429 | |
michael@0 | 1430 | return error; |
michael@0 | 1431 | } |
michael@0 | 1432 | |
michael@0 | 1433 | /* |
michael@0 | 1434 | * NSSCKFWC_CloseAllSessions |
michael@0 | 1435 | * |
michael@0 | 1436 | */ |
michael@0 | 1437 | NSS_IMPLEMENT CK_RV |
michael@0 | 1438 | NSSCKFWC_CloseAllSessions |
michael@0 | 1439 | ( |
michael@0 | 1440 | NSSCKFWInstance *fwInstance, |
michael@0 | 1441 | CK_SLOT_ID slotID |
michael@0 | 1442 | ) |
michael@0 | 1443 | { |
michael@0 | 1444 | CK_RV error = CKR_OK; |
michael@0 | 1445 | CK_ULONG nSlots; |
michael@0 | 1446 | NSSCKFWSlot **slots; |
michael@0 | 1447 | NSSCKFWSlot *fwSlot; |
michael@0 | 1448 | NSSCKFWToken *fwToken = (NSSCKFWToken *)NULL; |
michael@0 | 1449 | |
michael@0 | 1450 | if (!fwInstance) { |
michael@0 | 1451 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1452 | goto loser; |
michael@0 | 1453 | } |
michael@0 | 1454 | |
michael@0 | 1455 | nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error); |
michael@0 | 1456 | if( (CK_ULONG)0 == nSlots ) { |
michael@0 | 1457 | goto loser; |
michael@0 | 1458 | } |
michael@0 | 1459 | |
michael@0 | 1460 | if( (slotID < 1) || (slotID > nSlots) ) { |
michael@0 | 1461 | error = CKR_SLOT_ID_INVALID; |
michael@0 | 1462 | goto loser; |
michael@0 | 1463 | } |
michael@0 | 1464 | |
michael@0 | 1465 | slots = nssCKFWInstance_GetSlots(fwInstance, &error); |
michael@0 | 1466 | if( (NSSCKFWSlot **)NULL == slots ) { |
michael@0 | 1467 | goto loser; |
michael@0 | 1468 | } |
michael@0 | 1469 | |
michael@0 | 1470 | fwSlot = slots[ slotID-1 ]; |
michael@0 | 1471 | |
michael@0 | 1472 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 1473 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 1474 | goto loser; |
michael@0 | 1475 | } |
michael@0 | 1476 | |
michael@0 | 1477 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 1478 | if (!fwToken) { |
michael@0 | 1479 | goto loser; |
michael@0 | 1480 | } |
michael@0 | 1481 | |
michael@0 | 1482 | error = nssCKFWToken_CloseAllSessions(fwToken); |
michael@0 | 1483 | if( CKR_OK != error ) { |
michael@0 | 1484 | goto loser; |
michael@0 | 1485 | } |
michael@0 | 1486 | |
michael@0 | 1487 | return CKR_OK; |
michael@0 | 1488 | |
michael@0 | 1489 | loser: |
michael@0 | 1490 | switch( error ) { |
michael@0 | 1491 | case CKR_DEVICE_REMOVED: |
michael@0 | 1492 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1493 | break; |
michael@0 | 1494 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1495 | case CKR_DEVICE_ERROR: |
michael@0 | 1496 | case CKR_DEVICE_MEMORY: |
michael@0 | 1497 | case CKR_FUNCTION_FAILED: |
michael@0 | 1498 | case CKR_GENERAL_ERROR: |
michael@0 | 1499 | case CKR_HOST_MEMORY: |
michael@0 | 1500 | case CKR_SLOT_ID_INVALID: |
michael@0 | 1501 | case CKR_TOKEN_NOT_PRESENT: |
michael@0 | 1502 | break; |
michael@0 | 1503 | default: |
michael@0 | 1504 | case CKR_OK: |
michael@0 | 1505 | error = CKR_GENERAL_ERROR; |
michael@0 | 1506 | break; |
michael@0 | 1507 | } |
michael@0 | 1508 | |
michael@0 | 1509 | return error; |
michael@0 | 1510 | } |
michael@0 | 1511 | |
michael@0 | 1512 | /* |
michael@0 | 1513 | * NSSCKFWC_GetSessionInfo |
michael@0 | 1514 | * |
michael@0 | 1515 | */ |
michael@0 | 1516 | NSS_IMPLEMENT CK_RV |
michael@0 | 1517 | NSSCKFWC_GetSessionInfo |
michael@0 | 1518 | ( |
michael@0 | 1519 | NSSCKFWInstance *fwInstance, |
michael@0 | 1520 | CK_SESSION_HANDLE hSession, |
michael@0 | 1521 | CK_SESSION_INFO_PTR pInfo |
michael@0 | 1522 | ) |
michael@0 | 1523 | { |
michael@0 | 1524 | CK_RV error = CKR_OK; |
michael@0 | 1525 | NSSCKFWSession *fwSession; |
michael@0 | 1526 | NSSCKFWSlot *fwSlot; |
michael@0 | 1527 | |
michael@0 | 1528 | if (!fwInstance) { |
michael@0 | 1529 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1530 | goto loser; |
michael@0 | 1531 | } |
michael@0 | 1532 | |
michael@0 | 1533 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1534 | if (!fwSession) { |
michael@0 | 1535 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1536 | goto loser; |
michael@0 | 1537 | } |
michael@0 | 1538 | |
michael@0 | 1539 | if( (CK_SESSION_INFO_PTR)CK_NULL_PTR == pInfo ) { |
michael@0 | 1540 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 1541 | goto loser; |
michael@0 | 1542 | } |
michael@0 | 1543 | |
michael@0 | 1544 | /* |
michael@0 | 1545 | * A purify error here indicates caller error. |
michael@0 | 1546 | */ |
michael@0 | 1547 | (void)nsslibc_memset(pInfo, 0, sizeof(CK_SESSION_INFO)); |
michael@0 | 1548 | |
michael@0 | 1549 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 1550 | if (!fwSlot) { |
michael@0 | 1551 | error = CKR_GENERAL_ERROR; |
michael@0 | 1552 | goto loser; |
michael@0 | 1553 | } |
michael@0 | 1554 | |
michael@0 | 1555 | pInfo->slotID = nssCKFWSlot_GetSlotID(fwSlot); |
michael@0 | 1556 | pInfo->state = nssCKFWSession_GetSessionState(fwSession); |
michael@0 | 1557 | |
michael@0 | 1558 | if( CK_TRUE == nssCKFWSession_IsRWSession(fwSession) ) { |
michael@0 | 1559 | pInfo->flags |= CKF_RW_SESSION; |
michael@0 | 1560 | } |
michael@0 | 1561 | |
michael@0 | 1562 | pInfo->flags |= CKF_SERIAL_SESSION; /* Always true */ |
michael@0 | 1563 | |
michael@0 | 1564 | pInfo->ulDeviceError = nssCKFWSession_GetDeviceError(fwSession); |
michael@0 | 1565 | |
michael@0 | 1566 | return CKR_OK; |
michael@0 | 1567 | |
michael@0 | 1568 | loser: |
michael@0 | 1569 | switch( error ) { |
michael@0 | 1570 | case CKR_SESSION_CLOSED: |
michael@0 | 1571 | /* destroy session? */ |
michael@0 | 1572 | break; |
michael@0 | 1573 | case CKR_DEVICE_REMOVED: |
michael@0 | 1574 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1575 | break; |
michael@0 | 1576 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1577 | case CKR_DEVICE_ERROR: |
michael@0 | 1578 | case CKR_DEVICE_MEMORY: |
michael@0 | 1579 | case CKR_FUNCTION_FAILED: |
michael@0 | 1580 | case CKR_GENERAL_ERROR: |
michael@0 | 1581 | case CKR_HOST_MEMORY: |
michael@0 | 1582 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1583 | break; |
michael@0 | 1584 | default: |
michael@0 | 1585 | case CKR_OK: |
michael@0 | 1586 | error = CKR_GENERAL_ERROR; |
michael@0 | 1587 | break; |
michael@0 | 1588 | } |
michael@0 | 1589 | |
michael@0 | 1590 | return error; |
michael@0 | 1591 | } |
michael@0 | 1592 | |
michael@0 | 1593 | /* |
michael@0 | 1594 | * NSSCKFWC_GetOperationState |
michael@0 | 1595 | * |
michael@0 | 1596 | */ |
michael@0 | 1597 | NSS_IMPLEMENT CK_RV |
michael@0 | 1598 | NSSCKFWC_GetOperationState |
michael@0 | 1599 | ( |
michael@0 | 1600 | NSSCKFWInstance *fwInstance, |
michael@0 | 1601 | CK_SESSION_HANDLE hSession, |
michael@0 | 1602 | CK_BYTE_PTR pOperationState, |
michael@0 | 1603 | CK_ULONG_PTR pulOperationStateLen |
michael@0 | 1604 | ) |
michael@0 | 1605 | { |
michael@0 | 1606 | CK_RV error = CKR_OK; |
michael@0 | 1607 | NSSCKFWSession *fwSession; |
michael@0 | 1608 | CK_ULONG len; |
michael@0 | 1609 | NSSItem buf; |
michael@0 | 1610 | |
michael@0 | 1611 | if (!fwInstance) { |
michael@0 | 1612 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1613 | goto loser; |
michael@0 | 1614 | } |
michael@0 | 1615 | |
michael@0 | 1616 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1617 | if (!fwSession) { |
michael@0 | 1618 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1619 | goto loser; |
michael@0 | 1620 | } |
michael@0 | 1621 | |
michael@0 | 1622 | if( (CK_ULONG_PTR)CK_NULL_PTR == pulOperationStateLen ) { |
michael@0 | 1623 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 1624 | goto loser; |
michael@0 | 1625 | } |
michael@0 | 1626 | |
michael@0 | 1627 | len = nssCKFWSession_GetOperationStateLen(fwSession, &error); |
michael@0 | 1628 | if( ((CK_ULONG)0 == len) && (CKR_OK != error) ) { |
michael@0 | 1629 | goto loser; |
michael@0 | 1630 | } |
michael@0 | 1631 | |
michael@0 | 1632 | if( (CK_BYTE_PTR)CK_NULL_PTR == pOperationState ) { |
michael@0 | 1633 | *pulOperationStateLen = len; |
michael@0 | 1634 | return CKR_OK; |
michael@0 | 1635 | } |
michael@0 | 1636 | |
michael@0 | 1637 | if( *pulOperationStateLen < len ) { |
michael@0 | 1638 | *pulOperationStateLen = len; |
michael@0 | 1639 | error = CKR_BUFFER_TOO_SMALL; |
michael@0 | 1640 | goto loser; |
michael@0 | 1641 | } |
michael@0 | 1642 | |
michael@0 | 1643 | buf.size = (PRUint32)*pulOperationStateLen; |
michael@0 | 1644 | buf.data = (void *)pOperationState; |
michael@0 | 1645 | *pulOperationStateLen = len; |
michael@0 | 1646 | error = nssCKFWSession_GetOperationState(fwSession, &buf); |
michael@0 | 1647 | |
michael@0 | 1648 | if( CKR_OK != error ) { |
michael@0 | 1649 | goto loser; |
michael@0 | 1650 | } |
michael@0 | 1651 | |
michael@0 | 1652 | return CKR_OK; |
michael@0 | 1653 | |
michael@0 | 1654 | loser: |
michael@0 | 1655 | switch( error ) { |
michael@0 | 1656 | case CKR_SESSION_CLOSED: |
michael@0 | 1657 | /* destroy session? */ |
michael@0 | 1658 | break; |
michael@0 | 1659 | case CKR_DEVICE_REMOVED: |
michael@0 | 1660 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1661 | break; |
michael@0 | 1662 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 1663 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1664 | case CKR_DEVICE_ERROR: |
michael@0 | 1665 | case CKR_DEVICE_MEMORY: |
michael@0 | 1666 | case CKR_FUNCTION_FAILED: |
michael@0 | 1667 | case CKR_GENERAL_ERROR: |
michael@0 | 1668 | case CKR_HOST_MEMORY: |
michael@0 | 1669 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 1670 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1671 | case CKR_STATE_UNSAVEABLE: |
michael@0 | 1672 | break; |
michael@0 | 1673 | default: |
michael@0 | 1674 | case CKR_OK: |
michael@0 | 1675 | error = CKR_GENERAL_ERROR; |
michael@0 | 1676 | break; |
michael@0 | 1677 | } |
michael@0 | 1678 | |
michael@0 | 1679 | return error; |
michael@0 | 1680 | } |
michael@0 | 1681 | |
michael@0 | 1682 | /* |
michael@0 | 1683 | * NSSCKFWC_SetOperationState |
michael@0 | 1684 | * |
michael@0 | 1685 | */ |
michael@0 | 1686 | NSS_IMPLEMENT CK_RV |
michael@0 | 1687 | NSSCKFWC_SetOperationState |
michael@0 | 1688 | ( |
michael@0 | 1689 | NSSCKFWInstance *fwInstance, |
michael@0 | 1690 | CK_SESSION_HANDLE hSession, |
michael@0 | 1691 | CK_BYTE_PTR pOperationState, |
michael@0 | 1692 | CK_ULONG ulOperationStateLen, |
michael@0 | 1693 | CK_OBJECT_HANDLE hEncryptionKey, |
michael@0 | 1694 | CK_OBJECT_HANDLE hAuthenticationKey |
michael@0 | 1695 | ) |
michael@0 | 1696 | { |
michael@0 | 1697 | CK_RV error = CKR_OK; |
michael@0 | 1698 | NSSCKFWSession *fwSession; |
michael@0 | 1699 | NSSCKFWObject *eKey; |
michael@0 | 1700 | NSSCKFWObject *aKey; |
michael@0 | 1701 | NSSItem state; |
michael@0 | 1702 | |
michael@0 | 1703 | if (!fwInstance) { |
michael@0 | 1704 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1705 | goto loser; |
michael@0 | 1706 | } |
michael@0 | 1707 | |
michael@0 | 1708 | if( (CK_BYTE_PTR)CK_NULL_PTR == pOperationState ) { |
michael@0 | 1709 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 1710 | goto loser; |
michael@0 | 1711 | } |
michael@0 | 1712 | |
michael@0 | 1713 | /* |
michael@0 | 1714 | * We could loop through the buffer, to catch any purify errors |
michael@0 | 1715 | * in a place with a "user error" note. |
michael@0 | 1716 | */ |
michael@0 | 1717 | |
michael@0 | 1718 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1719 | if (!fwSession) { |
michael@0 | 1720 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1721 | goto loser; |
michael@0 | 1722 | } |
michael@0 | 1723 | |
michael@0 | 1724 | if( (CK_OBJECT_HANDLE)0 == hEncryptionKey ) { |
michael@0 | 1725 | eKey = (NSSCKFWObject *)NULL; |
michael@0 | 1726 | } else { |
michael@0 | 1727 | eKey = nssCKFWInstance_ResolveObjectHandle(fwInstance, hEncryptionKey); |
michael@0 | 1728 | if (!eKey) { |
michael@0 | 1729 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 1730 | goto loser; |
michael@0 | 1731 | } |
michael@0 | 1732 | } |
michael@0 | 1733 | |
michael@0 | 1734 | if( (CK_OBJECT_HANDLE)0 == hAuthenticationKey ) { |
michael@0 | 1735 | aKey = (NSSCKFWObject *)NULL; |
michael@0 | 1736 | } else { |
michael@0 | 1737 | aKey = nssCKFWInstance_ResolveObjectHandle(fwInstance, hAuthenticationKey); |
michael@0 | 1738 | if (!aKey) { |
michael@0 | 1739 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 1740 | goto loser; |
michael@0 | 1741 | } |
michael@0 | 1742 | } |
michael@0 | 1743 | |
michael@0 | 1744 | state.data = pOperationState; |
michael@0 | 1745 | state.size = ulOperationStateLen; |
michael@0 | 1746 | |
michael@0 | 1747 | error = nssCKFWSession_SetOperationState(fwSession, &state, eKey, aKey); |
michael@0 | 1748 | if( CKR_OK != error ) { |
michael@0 | 1749 | goto loser; |
michael@0 | 1750 | } |
michael@0 | 1751 | |
michael@0 | 1752 | return CKR_OK; |
michael@0 | 1753 | |
michael@0 | 1754 | loser: |
michael@0 | 1755 | switch( error ) { |
michael@0 | 1756 | case CKR_SESSION_CLOSED: |
michael@0 | 1757 | /* destroy session? */ |
michael@0 | 1758 | break; |
michael@0 | 1759 | case CKR_DEVICE_REMOVED: |
michael@0 | 1760 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1761 | break; |
michael@0 | 1762 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1763 | case CKR_DEVICE_ERROR: |
michael@0 | 1764 | case CKR_DEVICE_MEMORY: |
michael@0 | 1765 | case CKR_FUNCTION_FAILED: |
michael@0 | 1766 | case CKR_GENERAL_ERROR: |
michael@0 | 1767 | case CKR_HOST_MEMORY: |
michael@0 | 1768 | case CKR_KEY_CHANGED: |
michael@0 | 1769 | case CKR_KEY_NEEDED: |
michael@0 | 1770 | case CKR_KEY_NOT_NEEDED: |
michael@0 | 1771 | case CKR_SAVED_STATE_INVALID: |
michael@0 | 1772 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1773 | break; |
michael@0 | 1774 | default: |
michael@0 | 1775 | case CKR_OK: |
michael@0 | 1776 | error = CKR_GENERAL_ERROR; |
michael@0 | 1777 | break; |
michael@0 | 1778 | } |
michael@0 | 1779 | |
michael@0 | 1780 | return error; |
michael@0 | 1781 | } |
michael@0 | 1782 | |
michael@0 | 1783 | /* |
michael@0 | 1784 | * NSSCKFWC_Login |
michael@0 | 1785 | * |
michael@0 | 1786 | */ |
michael@0 | 1787 | NSS_IMPLEMENT CK_RV |
michael@0 | 1788 | NSSCKFWC_Login |
michael@0 | 1789 | ( |
michael@0 | 1790 | NSSCKFWInstance *fwInstance, |
michael@0 | 1791 | CK_SESSION_HANDLE hSession, |
michael@0 | 1792 | CK_USER_TYPE userType, |
michael@0 | 1793 | CK_CHAR_PTR pPin, |
michael@0 | 1794 | CK_ULONG ulPinLen |
michael@0 | 1795 | ) |
michael@0 | 1796 | { |
michael@0 | 1797 | CK_RV error = CKR_OK; |
michael@0 | 1798 | NSSCKFWSession *fwSession; |
michael@0 | 1799 | NSSItem pin, *arg; |
michael@0 | 1800 | |
michael@0 | 1801 | if (!fwInstance) { |
michael@0 | 1802 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1803 | goto loser; |
michael@0 | 1804 | } |
michael@0 | 1805 | |
michael@0 | 1806 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1807 | if (!fwSession) { |
michael@0 | 1808 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1809 | goto loser; |
michael@0 | 1810 | } |
michael@0 | 1811 | |
michael@0 | 1812 | if( (CK_CHAR_PTR)CK_NULL_PTR == pPin ) { |
michael@0 | 1813 | arg = (NSSItem *)NULL; |
michael@0 | 1814 | } else { |
michael@0 | 1815 | arg = &pin; |
michael@0 | 1816 | pin.size = (PRUint32)ulPinLen; |
michael@0 | 1817 | pin.data = (void *)pPin; |
michael@0 | 1818 | } |
michael@0 | 1819 | |
michael@0 | 1820 | error = nssCKFWSession_Login(fwSession, userType, arg); |
michael@0 | 1821 | if( CKR_OK != error ) { |
michael@0 | 1822 | goto loser; |
michael@0 | 1823 | } |
michael@0 | 1824 | |
michael@0 | 1825 | return CKR_OK; |
michael@0 | 1826 | |
michael@0 | 1827 | loser: |
michael@0 | 1828 | switch( error ) { |
michael@0 | 1829 | case CKR_SESSION_CLOSED: |
michael@0 | 1830 | /* destroy session? */ |
michael@0 | 1831 | break; |
michael@0 | 1832 | case CKR_DEVICE_REMOVED: |
michael@0 | 1833 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1834 | break; |
michael@0 | 1835 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1836 | case CKR_DEVICE_ERROR: |
michael@0 | 1837 | case CKR_DEVICE_MEMORY: |
michael@0 | 1838 | case CKR_FUNCTION_FAILED: |
michael@0 | 1839 | case CKR_GENERAL_ERROR: |
michael@0 | 1840 | case CKR_HOST_MEMORY: |
michael@0 | 1841 | case CKR_PIN_EXPIRED: |
michael@0 | 1842 | case CKR_PIN_INCORRECT: |
michael@0 | 1843 | case CKR_PIN_LOCKED: |
michael@0 | 1844 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1845 | case CKR_SESSION_READ_ONLY_EXISTS: |
michael@0 | 1846 | case CKR_USER_ALREADY_LOGGED_IN: |
michael@0 | 1847 | case CKR_USER_ANOTHER_ALREADY_LOGGED_IN: |
michael@0 | 1848 | case CKR_USER_PIN_NOT_INITIALIZED: |
michael@0 | 1849 | case CKR_USER_TOO_MANY_TYPES: |
michael@0 | 1850 | case CKR_USER_TYPE_INVALID: |
michael@0 | 1851 | break; |
michael@0 | 1852 | default: |
michael@0 | 1853 | case CKR_OK: |
michael@0 | 1854 | error = CKR_GENERAL_ERROR; |
michael@0 | 1855 | break; |
michael@0 | 1856 | } |
michael@0 | 1857 | |
michael@0 | 1858 | return error; |
michael@0 | 1859 | } |
michael@0 | 1860 | |
michael@0 | 1861 | /* |
michael@0 | 1862 | * NSSCKFWC_Logout |
michael@0 | 1863 | * |
michael@0 | 1864 | */ |
michael@0 | 1865 | NSS_IMPLEMENT CK_RV |
michael@0 | 1866 | NSSCKFWC_Logout |
michael@0 | 1867 | ( |
michael@0 | 1868 | NSSCKFWInstance *fwInstance, |
michael@0 | 1869 | CK_SESSION_HANDLE hSession |
michael@0 | 1870 | ) |
michael@0 | 1871 | { |
michael@0 | 1872 | CK_RV error = CKR_OK; |
michael@0 | 1873 | NSSCKFWSession *fwSession; |
michael@0 | 1874 | |
michael@0 | 1875 | if (!fwInstance) { |
michael@0 | 1876 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1877 | goto loser; |
michael@0 | 1878 | } |
michael@0 | 1879 | |
michael@0 | 1880 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1881 | if (!fwSession) { |
michael@0 | 1882 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1883 | goto loser; |
michael@0 | 1884 | } |
michael@0 | 1885 | |
michael@0 | 1886 | error = nssCKFWSession_Logout(fwSession); |
michael@0 | 1887 | if( CKR_OK != error ) { |
michael@0 | 1888 | goto loser; |
michael@0 | 1889 | } |
michael@0 | 1890 | |
michael@0 | 1891 | return CKR_OK; |
michael@0 | 1892 | |
michael@0 | 1893 | loser: |
michael@0 | 1894 | switch( error ) { |
michael@0 | 1895 | case CKR_SESSION_CLOSED: |
michael@0 | 1896 | /* destroy session? */ |
michael@0 | 1897 | break; |
michael@0 | 1898 | case CKR_DEVICE_REMOVED: |
michael@0 | 1899 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1900 | break; |
michael@0 | 1901 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1902 | case CKR_DEVICE_ERROR: |
michael@0 | 1903 | case CKR_DEVICE_MEMORY: |
michael@0 | 1904 | case CKR_FUNCTION_FAILED: |
michael@0 | 1905 | case CKR_GENERAL_ERROR: |
michael@0 | 1906 | case CKR_HOST_MEMORY: |
michael@0 | 1907 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1908 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 1909 | break; |
michael@0 | 1910 | default: |
michael@0 | 1911 | case CKR_OK: |
michael@0 | 1912 | error = CKR_GENERAL_ERROR; |
michael@0 | 1913 | break; |
michael@0 | 1914 | } |
michael@0 | 1915 | |
michael@0 | 1916 | return error; |
michael@0 | 1917 | } |
michael@0 | 1918 | |
michael@0 | 1919 | /* |
michael@0 | 1920 | * NSSCKFWC_CreateObject |
michael@0 | 1921 | * |
michael@0 | 1922 | */ |
michael@0 | 1923 | NSS_IMPLEMENT CK_RV |
michael@0 | 1924 | NSSCKFWC_CreateObject |
michael@0 | 1925 | ( |
michael@0 | 1926 | NSSCKFWInstance *fwInstance, |
michael@0 | 1927 | CK_SESSION_HANDLE hSession, |
michael@0 | 1928 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 1929 | CK_ULONG ulCount, |
michael@0 | 1930 | CK_OBJECT_HANDLE_PTR phObject |
michael@0 | 1931 | ) |
michael@0 | 1932 | { |
michael@0 | 1933 | CK_RV error = CKR_OK; |
michael@0 | 1934 | NSSCKFWSession *fwSession; |
michael@0 | 1935 | NSSCKFWObject *fwObject; |
michael@0 | 1936 | |
michael@0 | 1937 | if (!fwInstance) { |
michael@0 | 1938 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1939 | goto loser; |
michael@0 | 1940 | } |
michael@0 | 1941 | |
michael@0 | 1942 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 1943 | if (!fwSession) { |
michael@0 | 1944 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 1945 | goto loser; |
michael@0 | 1946 | } |
michael@0 | 1947 | |
michael@0 | 1948 | if( (CK_OBJECT_HANDLE_PTR)CK_NULL_PTR == phObject ) { |
michael@0 | 1949 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 1950 | goto loser; |
michael@0 | 1951 | } |
michael@0 | 1952 | |
michael@0 | 1953 | /* |
michael@0 | 1954 | * A purify error here indicates caller error. |
michael@0 | 1955 | */ |
michael@0 | 1956 | *phObject = (CK_OBJECT_HANDLE)0; |
michael@0 | 1957 | |
michael@0 | 1958 | fwObject = nssCKFWSession_CreateObject(fwSession, pTemplate, |
michael@0 | 1959 | ulCount, &error); |
michael@0 | 1960 | if (!fwObject) { |
michael@0 | 1961 | goto loser; |
michael@0 | 1962 | } |
michael@0 | 1963 | |
michael@0 | 1964 | *phObject = nssCKFWInstance_CreateObjectHandle(fwInstance, fwObject, &error); |
michael@0 | 1965 | if( (CK_OBJECT_HANDLE)0 == *phObject ) { |
michael@0 | 1966 | nssCKFWObject_Destroy(fwObject); |
michael@0 | 1967 | goto loser; |
michael@0 | 1968 | } |
michael@0 | 1969 | |
michael@0 | 1970 | return CKR_OK; |
michael@0 | 1971 | |
michael@0 | 1972 | loser: |
michael@0 | 1973 | switch( error ) { |
michael@0 | 1974 | case CKR_SESSION_CLOSED: |
michael@0 | 1975 | /* destroy session? */ |
michael@0 | 1976 | break; |
michael@0 | 1977 | case CKR_DEVICE_REMOVED: |
michael@0 | 1978 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 1979 | break; |
michael@0 | 1980 | case CKR_ATTRIBUTE_READ_ONLY: |
michael@0 | 1981 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 1982 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 1983 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 1984 | case CKR_DEVICE_ERROR: |
michael@0 | 1985 | case CKR_DEVICE_MEMORY: |
michael@0 | 1986 | case CKR_FUNCTION_FAILED: |
michael@0 | 1987 | case CKR_GENERAL_ERROR: |
michael@0 | 1988 | case CKR_HOST_MEMORY: |
michael@0 | 1989 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 1990 | case CKR_SESSION_READ_ONLY: |
michael@0 | 1991 | case CKR_TEMPLATE_INCOMPLETE: |
michael@0 | 1992 | case CKR_TEMPLATE_INCONSISTENT: |
michael@0 | 1993 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 1994 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 1995 | break; |
michael@0 | 1996 | default: |
michael@0 | 1997 | case CKR_OK: |
michael@0 | 1998 | error = CKR_GENERAL_ERROR; |
michael@0 | 1999 | break; |
michael@0 | 2000 | } |
michael@0 | 2001 | |
michael@0 | 2002 | return error; |
michael@0 | 2003 | } |
michael@0 | 2004 | |
michael@0 | 2005 | /* |
michael@0 | 2006 | * NSSCKFWC_CopyObject |
michael@0 | 2007 | * |
michael@0 | 2008 | */ |
michael@0 | 2009 | NSS_IMPLEMENT CK_RV |
michael@0 | 2010 | NSSCKFWC_CopyObject |
michael@0 | 2011 | ( |
michael@0 | 2012 | NSSCKFWInstance *fwInstance, |
michael@0 | 2013 | CK_SESSION_HANDLE hSession, |
michael@0 | 2014 | CK_OBJECT_HANDLE hObject, |
michael@0 | 2015 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 2016 | CK_ULONG ulCount, |
michael@0 | 2017 | CK_OBJECT_HANDLE_PTR phNewObject |
michael@0 | 2018 | ) |
michael@0 | 2019 | { |
michael@0 | 2020 | CK_RV error = CKR_OK; |
michael@0 | 2021 | NSSCKFWSession *fwSession; |
michael@0 | 2022 | NSSCKFWObject *fwObject; |
michael@0 | 2023 | NSSCKFWObject *fwNewObject; |
michael@0 | 2024 | |
michael@0 | 2025 | if (!fwInstance) { |
michael@0 | 2026 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2027 | goto loser; |
michael@0 | 2028 | } |
michael@0 | 2029 | |
michael@0 | 2030 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2031 | if (!fwSession) { |
michael@0 | 2032 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2033 | goto loser; |
michael@0 | 2034 | } |
michael@0 | 2035 | |
michael@0 | 2036 | if( (CK_OBJECT_HANDLE_PTR)CK_NULL_PTR == phNewObject ) { |
michael@0 | 2037 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 2038 | goto loser; |
michael@0 | 2039 | } |
michael@0 | 2040 | |
michael@0 | 2041 | /* |
michael@0 | 2042 | * A purify error here indicates caller error. |
michael@0 | 2043 | */ |
michael@0 | 2044 | *phNewObject = (CK_OBJECT_HANDLE)0; |
michael@0 | 2045 | |
michael@0 | 2046 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hObject); |
michael@0 | 2047 | if (!fwObject) { |
michael@0 | 2048 | error = CKR_OBJECT_HANDLE_INVALID; |
michael@0 | 2049 | goto loser; |
michael@0 | 2050 | } |
michael@0 | 2051 | |
michael@0 | 2052 | fwNewObject = nssCKFWSession_CopyObject(fwSession, fwObject, |
michael@0 | 2053 | pTemplate, ulCount, &error); |
michael@0 | 2054 | if (!fwNewObject) { |
michael@0 | 2055 | goto loser; |
michael@0 | 2056 | } |
michael@0 | 2057 | |
michael@0 | 2058 | *phNewObject = nssCKFWInstance_CreateObjectHandle(fwInstance, |
michael@0 | 2059 | fwNewObject, &error); |
michael@0 | 2060 | if( (CK_OBJECT_HANDLE)0 == *phNewObject ) { |
michael@0 | 2061 | nssCKFWObject_Destroy(fwNewObject); |
michael@0 | 2062 | goto loser; |
michael@0 | 2063 | } |
michael@0 | 2064 | |
michael@0 | 2065 | return CKR_OK; |
michael@0 | 2066 | |
michael@0 | 2067 | loser: |
michael@0 | 2068 | switch( error ) { |
michael@0 | 2069 | case CKR_SESSION_CLOSED: |
michael@0 | 2070 | /* destroy session? */ |
michael@0 | 2071 | break; |
michael@0 | 2072 | case CKR_DEVICE_REMOVED: |
michael@0 | 2073 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2074 | break; |
michael@0 | 2075 | case CKR_ATTRIBUTE_READ_ONLY: |
michael@0 | 2076 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 2077 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 2078 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2079 | case CKR_DEVICE_ERROR: |
michael@0 | 2080 | case CKR_DEVICE_MEMORY: |
michael@0 | 2081 | case CKR_FUNCTION_FAILED: |
michael@0 | 2082 | case CKR_GENERAL_ERROR: |
michael@0 | 2083 | case CKR_HOST_MEMORY: |
michael@0 | 2084 | case CKR_OBJECT_HANDLE_INVALID: |
michael@0 | 2085 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2086 | case CKR_SESSION_READ_ONLY: |
michael@0 | 2087 | case CKR_TEMPLATE_INCONSISTENT: |
michael@0 | 2088 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 2089 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 2090 | break; |
michael@0 | 2091 | default: |
michael@0 | 2092 | case CKR_OK: |
michael@0 | 2093 | error = CKR_GENERAL_ERROR; |
michael@0 | 2094 | break; |
michael@0 | 2095 | } |
michael@0 | 2096 | |
michael@0 | 2097 | return error; |
michael@0 | 2098 | } |
michael@0 | 2099 | |
michael@0 | 2100 | /* |
michael@0 | 2101 | * NSSCKFWC_DestroyObject |
michael@0 | 2102 | * |
michael@0 | 2103 | */ |
michael@0 | 2104 | NSS_IMPLEMENT CK_RV |
michael@0 | 2105 | NSSCKFWC_DestroyObject |
michael@0 | 2106 | ( |
michael@0 | 2107 | NSSCKFWInstance *fwInstance, |
michael@0 | 2108 | CK_SESSION_HANDLE hSession, |
michael@0 | 2109 | CK_OBJECT_HANDLE hObject |
michael@0 | 2110 | ) |
michael@0 | 2111 | { |
michael@0 | 2112 | CK_RV error = CKR_OK; |
michael@0 | 2113 | NSSCKFWSession *fwSession; |
michael@0 | 2114 | NSSCKFWObject *fwObject; |
michael@0 | 2115 | |
michael@0 | 2116 | if (!fwInstance) { |
michael@0 | 2117 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2118 | goto loser; |
michael@0 | 2119 | } |
michael@0 | 2120 | |
michael@0 | 2121 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2122 | if (!fwSession) { |
michael@0 | 2123 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2124 | goto loser; |
michael@0 | 2125 | } |
michael@0 | 2126 | |
michael@0 | 2127 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hObject); |
michael@0 | 2128 | if (!fwObject) { |
michael@0 | 2129 | error = CKR_OBJECT_HANDLE_INVALID; |
michael@0 | 2130 | goto loser; |
michael@0 | 2131 | } |
michael@0 | 2132 | |
michael@0 | 2133 | nssCKFWInstance_DestroyObjectHandle(fwInstance, hObject); |
michael@0 | 2134 | nssCKFWObject_Destroy(fwObject); |
michael@0 | 2135 | |
michael@0 | 2136 | return CKR_OK; |
michael@0 | 2137 | |
michael@0 | 2138 | loser: |
michael@0 | 2139 | switch( error ) { |
michael@0 | 2140 | case CKR_SESSION_CLOSED: |
michael@0 | 2141 | /* destroy session? */ |
michael@0 | 2142 | break; |
michael@0 | 2143 | case CKR_DEVICE_REMOVED: |
michael@0 | 2144 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2145 | break; |
michael@0 | 2146 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2147 | case CKR_DEVICE_ERROR: |
michael@0 | 2148 | case CKR_DEVICE_MEMORY: |
michael@0 | 2149 | case CKR_FUNCTION_FAILED: |
michael@0 | 2150 | case CKR_GENERAL_ERROR: |
michael@0 | 2151 | case CKR_HOST_MEMORY: |
michael@0 | 2152 | case CKR_OBJECT_HANDLE_INVALID: |
michael@0 | 2153 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2154 | case CKR_SESSION_READ_ONLY: |
michael@0 | 2155 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 2156 | break; |
michael@0 | 2157 | default: |
michael@0 | 2158 | case CKR_OK: |
michael@0 | 2159 | error = CKR_GENERAL_ERROR; |
michael@0 | 2160 | break; |
michael@0 | 2161 | } |
michael@0 | 2162 | |
michael@0 | 2163 | return error; |
michael@0 | 2164 | } |
michael@0 | 2165 | |
michael@0 | 2166 | /* |
michael@0 | 2167 | * NSSCKFWC_GetObjectSize |
michael@0 | 2168 | * |
michael@0 | 2169 | */ |
michael@0 | 2170 | NSS_IMPLEMENT CK_RV |
michael@0 | 2171 | NSSCKFWC_GetObjectSize |
michael@0 | 2172 | ( |
michael@0 | 2173 | NSSCKFWInstance *fwInstance, |
michael@0 | 2174 | CK_SESSION_HANDLE hSession, |
michael@0 | 2175 | CK_OBJECT_HANDLE hObject, |
michael@0 | 2176 | CK_ULONG_PTR pulSize |
michael@0 | 2177 | ) |
michael@0 | 2178 | { |
michael@0 | 2179 | CK_RV error = CKR_OK; |
michael@0 | 2180 | NSSCKFWSession *fwSession; |
michael@0 | 2181 | NSSCKFWObject *fwObject; |
michael@0 | 2182 | |
michael@0 | 2183 | if (!fwInstance) { |
michael@0 | 2184 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2185 | goto loser; |
michael@0 | 2186 | } |
michael@0 | 2187 | |
michael@0 | 2188 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2189 | if (!fwSession) { |
michael@0 | 2190 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2191 | goto loser; |
michael@0 | 2192 | } |
michael@0 | 2193 | |
michael@0 | 2194 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hObject); |
michael@0 | 2195 | if (!fwObject) { |
michael@0 | 2196 | error = CKR_OBJECT_HANDLE_INVALID; |
michael@0 | 2197 | goto loser; |
michael@0 | 2198 | } |
michael@0 | 2199 | |
michael@0 | 2200 | if( (CK_ULONG_PTR)CK_NULL_PTR == pulSize ) { |
michael@0 | 2201 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 2202 | goto loser; |
michael@0 | 2203 | } |
michael@0 | 2204 | |
michael@0 | 2205 | /* |
michael@0 | 2206 | * A purify error here indicates caller error. |
michael@0 | 2207 | */ |
michael@0 | 2208 | *pulSize = (CK_ULONG)0; |
michael@0 | 2209 | |
michael@0 | 2210 | *pulSize = nssCKFWObject_GetObjectSize(fwObject, &error); |
michael@0 | 2211 | if( ((CK_ULONG)0 == *pulSize) && (CKR_OK != error) ) { |
michael@0 | 2212 | goto loser; |
michael@0 | 2213 | } |
michael@0 | 2214 | |
michael@0 | 2215 | return CKR_OK; |
michael@0 | 2216 | |
michael@0 | 2217 | loser: |
michael@0 | 2218 | switch( error ) { |
michael@0 | 2219 | case CKR_SESSION_CLOSED: |
michael@0 | 2220 | /* destroy session? */ |
michael@0 | 2221 | break; |
michael@0 | 2222 | case CKR_DEVICE_REMOVED: |
michael@0 | 2223 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2224 | break; |
michael@0 | 2225 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2226 | case CKR_DEVICE_ERROR: |
michael@0 | 2227 | case CKR_DEVICE_MEMORY: |
michael@0 | 2228 | case CKR_FUNCTION_FAILED: |
michael@0 | 2229 | case CKR_GENERAL_ERROR: |
michael@0 | 2230 | case CKR_HOST_MEMORY: |
michael@0 | 2231 | case CKR_INFORMATION_SENSITIVE: |
michael@0 | 2232 | case CKR_OBJECT_HANDLE_INVALID: |
michael@0 | 2233 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2234 | break; |
michael@0 | 2235 | default: |
michael@0 | 2236 | case CKR_OK: |
michael@0 | 2237 | error = CKR_GENERAL_ERROR; |
michael@0 | 2238 | break; |
michael@0 | 2239 | } |
michael@0 | 2240 | |
michael@0 | 2241 | return error; |
michael@0 | 2242 | } |
michael@0 | 2243 | |
michael@0 | 2244 | /* |
michael@0 | 2245 | * NSSCKFWC_GetAttributeValue |
michael@0 | 2246 | * |
michael@0 | 2247 | */ |
michael@0 | 2248 | NSS_IMPLEMENT CK_RV |
michael@0 | 2249 | NSSCKFWC_GetAttributeValue |
michael@0 | 2250 | ( |
michael@0 | 2251 | NSSCKFWInstance *fwInstance, |
michael@0 | 2252 | CK_SESSION_HANDLE hSession, |
michael@0 | 2253 | CK_OBJECT_HANDLE hObject, |
michael@0 | 2254 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 2255 | CK_ULONG ulCount |
michael@0 | 2256 | ) |
michael@0 | 2257 | { |
michael@0 | 2258 | CK_RV error = CKR_OK; |
michael@0 | 2259 | NSSCKFWSession *fwSession; |
michael@0 | 2260 | NSSCKFWObject *fwObject; |
michael@0 | 2261 | CK_BBOOL sensitive = CK_FALSE; |
michael@0 | 2262 | CK_BBOOL invalid = CK_FALSE; |
michael@0 | 2263 | CK_BBOOL tooSmall = CK_FALSE; |
michael@0 | 2264 | CK_ULONG i; |
michael@0 | 2265 | |
michael@0 | 2266 | if (!fwInstance) { |
michael@0 | 2267 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2268 | goto loser; |
michael@0 | 2269 | } |
michael@0 | 2270 | |
michael@0 | 2271 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2272 | if (!fwSession) { |
michael@0 | 2273 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2274 | goto loser; |
michael@0 | 2275 | } |
michael@0 | 2276 | |
michael@0 | 2277 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hObject); |
michael@0 | 2278 | if (!fwObject) { |
michael@0 | 2279 | error = CKR_OBJECT_HANDLE_INVALID; |
michael@0 | 2280 | goto loser; |
michael@0 | 2281 | } |
michael@0 | 2282 | |
michael@0 | 2283 | if( (CK_ATTRIBUTE_PTR)CK_NULL_PTR == pTemplate ) { |
michael@0 | 2284 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 2285 | goto loser; |
michael@0 | 2286 | } |
michael@0 | 2287 | |
michael@0 | 2288 | for( i = 0; i < ulCount; i++ ) { |
michael@0 | 2289 | CK_ULONG size = nssCKFWObject_GetAttributeSize(fwObject, |
michael@0 | 2290 | pTemplate[i].type, &error); |
michael@0 | 2291 | if( (CK_ULONG)0 == size ) { |
michael@0 | 2292 | switch( error ) { |
michael@0 | 2293 | case CKR_ATTRIBUTE_SENSITIVE: |
michael@0 | 2294 | case CKR_INFORMATION_SENSITIVE: |
michael@0 | 2295 | sensitive = CK_TRUE; |
michael@0 | 2296 | pTemplate[i].ulValueLen = (CK_ULONG)(-1); |
michael@0 | 2297 | continue; |
michael@0 | 2298 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 2299 | invalid = CK_TRUE; |
michael@0 | 2300 | pTemplate[i].ulValueLen = (CK_ULONG)(-1); |
michael@0 | 2301 | continue; |
michael@0 | 2302 | case CKR_OK: |
michael@0 | 2303 | break; |
michael@0 | 2304 | default: |
michael@0 | 2305 | goto loser; |
michael@0 | 2306 | } |
michael@0 | 2307 | } |
michael@0 | 2308 | |
michael@0 | 2309 | if( (CK_VOID_PTR)CK_NULL_PTR == pTemplate[i].pValue ) { |
michael@0 | 2310 | pTemplate[i].ulValueLen = size; |
michael@0 | 2311 | } else { |
michael@0 | 2312 | NSSItem it, *p; |
michael@0 | 2313 | |
michael@0 | 2314 | if( pTemplate[i].ulValueLen < size ) { |
michael@0 | 2315 | tooSmall = CK_TRUE; |
michael@0 | 2316 | continue; |
michael@0 | 2317 | } |
michael@0 | 2318 | |
michael@0 | 2319 | it.size = (PRUint32)pTemplate[i].ulValueLen; |
michael@0 | 2320 | it.data = (void *)pTemplate[i].pValue; |
michael@0 | 2321 | p = nssCKFWObject_GetAttribute(fwObject, pTemplate[i].type, &it, |
michael@0 | 2322 | (NSSArena *)NULL, &error); |
michael@0 | 2323 | if (!p) { |
michael@0 | 2324 | switch( error ) { |
michael@0 | 2325 | case CKR_ATTRIBUTE_SENSITIVE: |
michael@0 | 2326 | case CKR_INFORMATION_SENSITIVE: |
michael@0 | 2327 | sensitive = CK_TRUE; |
michael@0 | 2328 | pTemplate[i].ulValueLen = (CK_ULONG)(-1); |
michael@0 | 2329 | continue; |
michael@0 | 2330 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 2331 | invalid = CK_TRUE; |
michael@0 | 2332 | pTemplate[i].ulValueLen = (CK_ULONG)(-1); |
michael@0 | 2333 | continue; |
michael@0 | 2334 | default: |
michael@0 | 2335 | goto loser; |
michael@0 | 2336 | } |
michael@0 | 2337 | } |
michael@0 | 2338 | |
michael@0 | 2339 | pTemplate[i].ulValueLen = size; |
michael@0 | 2340 | } |
michael@0 | 2341 | } |
michael@0 | 2342 | |
michael@0 | 2343 | if( sensitive ) { |
michael@0 | 2344 | error = CKR_ATTRIBUTE_SENSITIVE; |
michael@0 | 2345 | goto loser; |
michael@0 | 2346 | } else if( invalid ) { |
michael@0 | 2347 | error = CKR_ATTRIBUTE_TYPE_INVALID; |
michael@0 | 2348 | goto loser; |
michael@0 | 2349 | } else if( tooSmall ) { |
michael@0 | 2350 | error = CKR_BUFFER_TOO_SMALL; |
michael@0 | 2351 | goto loser; |
michael@0 | 2352 | } |
michael@0 | 2353 | |
michael@0 | 2354 | return CKR_OK; |
michael@0 | 2355 | |
michael@0 | 2356 | loser: |
michael@0 | 2357 | switch( error ) { |
michael@0 | 2358 | case CKR_SESSION_CLOSED: |
michael@0 | 2359 | /* destroy session? */ |
michael@0 | 2360 | break; |
michael@0 | 2361 | case CKR_DEVICE_REMOVED: |
michael@0 | 2362 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2363 | break; |
michael@0 | 2364 | case CKR_ATTRIBUTE_SENSITIVE: |
michael@0 | 2365 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 2366 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 2367 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2368 | case CKR_DEVICE_ERROR: |
michael@0 | 2369 | case CKR_DEVICE_MEMORY: |
michael@0 | 2370 | case CKR_FUNCTION_FAILED: |
michael@0 | 2371 | case CKR_GENERAL_ERROR: |
michael@0 | 2372 | case CKR_HOST_MEMORY: |
michael@0 | 2373 | case CKR_OBJECT_HANDLE_INVALID: |
michael@0 | 2374 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2375 | break; |
michael@0 | 2376 | default: |
michael@0 | 2377 | case CKR_OK: |
michael@0 | 2378 | error = CKR_GENERAL_ERROR; |
michael@0 | 2379 | break; |
michael@0 | 2380 | } |
michael@0 | 2381 | |
michael@0 | 2382 | return error; |
michael@0 | 2383 | } |
michael@0 | 2384 | |
michael@0 | 2385 | /* |
michael@0 | 2386 | * NSSCKFWC_SetAttributeValue |
michael@0 | 2387 | * |
michael@0 | 2388 | */ |
michael@0 | 2389 | NSS_IMPLEMENT CK_RV |
michael@0 | 2390 | NSSCKFWC_SetAttributeValue |
michael@0 | 2391 | ( |
michael@0 | 2392 | NSSCKFWInstance *fwInstance, |
michael@0 | 2393 | CK_SESSION_HANDLE hSession, |
michael@0 | 2394 | CK_OBJECT_HANDLE hObject, |
michael@0 | 2395 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 2396 | CK_ULONG ulCount |
michael@0 | 2397 | ) |
michael@0 | 2398 | { |
michael@0 | 2399 | CK_RV error = CKR_OK; |
michael@0 | 2400 | NSSCKFWSession *fwSession; |
michael@0 | 2401 | NSSCKFWObject *fwObject; |
michael@0 | 2402 | CK_ULONG i; |
michael@0 | 2403 | |
michael@0 | 2404 | if (!fwInstance) { |
michael@0 | 2405 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2406 | goto loser; |
michael@0 | 2407 | } |
michael@0 | 2408 | |
michael@0 | 2409 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2410 | if (!fwSession) { |
michael@0 | 2411 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2412 | goto loser; |
michael@0 | 2413 | } |
michael@0 | 2414 | |
michael@0 | 2415 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hObject); |
michael@0 | 2416 | if (!fwObject) { |
michael@0 | 2417 | error = CKR_OBJECT_HANDLE_INVALID; |
michael@0 | 2418 | goto loser; |
michael@0 | 2419 | } |
michael@0 | 2420 | |
michael@0 | 2421 | if( (CK_ATTRIBUTE_PTR)CK_NULL_PTR == pTemplate ) { |
michael@0 | 2422 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 2423 | goto loser; |
michael@0 | 2424 | } |
michael@0 | 2425 | |
michael@0 | 2426 | for (i=0; i < ulCount; i++) { |
michael@0 | 2427 | NSSItem value; |
michael@0 | 2428 | |
michael@0 | 2429 | value.data = pTemplate[i].pValue; |
michael@0 | 2430 | value.size = pTemplate[i].ulValueLen; |
michael@0 | 2431 | |
michael@0 | 2432 | error = nssCKFWObject_SetAttribute(fwObject, fwSession, |
michael@0 | 2433 | pTemplate[i].type, &value); |
michael@0 | 2434 | |
michael@0 | 2435 | if( CKR_OK != error ) { |
michael@0 | 2436 | goto loser; |
michael@0 | 2437 | } |
michael@0 | 2438 | } |
michael@0 | 2439 | |
michael@0 | 2440 | return CKR_OK; |
michael@0 | 2441 | |
michael@0 | 2442 | loser: |
michael@0 | 2443 | switch( error ) { |
michael@0 | 2444 | case CKR_SESSION_CLOSED: |
michael@0 | 2445 | /* destroy session? */ |
michael@0 | 2446 | break; |
michael@0 | 2447 | case CKR_DEVICE_REMOVED: |
michael@0 | 2448 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2449 | break; |
michael@0 | 2450 | case CKR_ATTRIBUTE_READ_ONLY: |
michael@0 | 2451 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 2452 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 2453 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2454 | case CKR_DEVICE_ERROR: |
michael@0 | 2455 | case CKR_DEVICE_MEMORY: |
michael@0 | 2456 | case CKR_FUNCTION_FAILED: |
michael@0 | 2457 | case CKR_GENERAL_ERROR: |
michael@0 | 2458 | case CKR_HOST_MEMORY: |
michael@0 | 2459 | case CKR_OBJECT_HANDLE_INVALID: |
michael@0 | 2460 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2461 | case CKR_SESSION_READ_ONLY: |
michael@0 | 2462 | case CKR_TEMPLATE_INCONSISTENT: |
michael@0 | 2463 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 2464 | break; |
michael@0 | 2465 | default: |
michael@0 | 2466 | case CKR_OK: |
michael@0 | 2467 | error = CKR_GENERAL_ERROR; |
michael@0 | 2468 | break; |
michael@0 | 2469 | } |
michael@0 | 2470 | |
michael@0 | 2471 | return error; |
michael@0 | 2472 | } |
michael@0 | 2473 | |
michael@0 | 2474 | /* |
michael@0 | 2475 | * NSSCKFWC_FindObjectsInit |
michael@0 | 2476 | * |
michael@0 | 2477 | */ |
michael@0 | 2478 | NSS_IMPLEMENT CK_RV |
michael@0 | 2479 | NSSCKFWC_FindObjectsInit |
michael@0 | 2480 | ( |
michael@0 | 2481 | NSSCKFWInstance *fwInstance, |
michael@0 | 2482 | CK_SESSION_HANDLE hSession, |
michael@0 | 2483 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 2484 | CK_ULONG ulCount |
michael@0 | 2485 | ) |
michael@0 | 2486 | { |
michael@0 | 2487 | CK_RV error = CKR_OK; |
michael@0 | 2488 | NSSCKFWSession *fwSession; |
michael@0 | 2489 | NSSCKFWFindObjects *fwFindObjects; |
michael@0 | 2490 | |
michael@0 | 2491 | if (!fwInstance) { |
michael@0 | 2492 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2493 | goto loser; |
michael@0 | 2494 | } |
michael@0 | 2495 | |
michael@0 | 2496 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2497 | if (!fwSession) { |
michael@0 | 2498 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2499 | goto loser; |
michael@0 | 2500 | } |
michael@0 | 2501 | |
michael@0 | 2502 | if( ((CK_ATTRIBUTE_PTR)CK_NULL_PTR == pTemplate) && (ulCount != 0) ) { |
michael@0 | 2503 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 2504 | goto loser; |
michael@0 | 2505 | } |
michael@0 | 2506 | |
michael@0 | 2507 | fwFindObjects = nssCKFWSession_GetFWFindObjects(fwSession, &error); |
michael@0 | 2508 | if (fwFindObjects) { |
michael@0 | 2509 | error = CKR_OPERATION_ACTIVE; |
michael@0 | 2510 | goto loser; |
michael@0 | 2511 | } |
michael@0 | 2512 | |
michael@0 | 2513 | if( CKR_OPERATION_NOT_INITIALIZED != error ) { |
michael@0 | 2514 | goto loser; |
michael@0 | 2515 | } |
michael@0 | 2516 | |
michael@0 | 2517 | fwFindObjects = nssCKFWSession_FindObjectsInit(fwSession, |
michael@0 | 2518 | pTemplate, ulCount, &error); |
michael@0 | 2519 | if (!fwFindObjects) { |
michael@0 | 2520 | goto loser; |
michael@0 | 2521 | } |
michael@0 | 2522 | |
michael@0 | 2523 | error = nssCKFWSession_SetFWFindObjects(fwSession, fwFindObjects); |
michael@0 | 2524 | |
michael@0 | 2525 | if( CKR_OK != error ) { |
michael@0 | 2526 | nssCKFWFindObjects_Destroy(fwFindObjects); |
michael@0 | 2527 | goto loser; |
michael@0 | 2528 | } |
michael@0 | 2529 | |
michael@0 | 2530 | return CKR_OK; |
michael@0 | 2531 | |
michael@0 | 2532 | loser: |
michael@0 | 2533 | switch( error ) { |
michael@0 | 2534 | case CKR_SESSION_CLOSED: |
michael@0 | 2535 | /* destroy session? */ |
michael@0 | 2536 | break; |
michael@0 | 2537 | case CKR_DEVICE_REMOVED: |
michael@0 | 2538 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2539 | break; |
michael@0 | 2540 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 2541 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 2542 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2543 | case CKR_DEVICE_ERROR: |
michael@0 | 2544 | case CKR_DEVICE_MEMORY: |
michael@0 | 2545 | case CKR_FUNCTION_FAILED: |
michael@0 | 2546 | case CKR_GENERAL_ERROR: |
michael@0 | 2547 | case CKR_HOST_MEMORY: |
michael@0 | 2548 | case CKR_OPERATION_ACTIVE: |
michael@0 | 2549 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2550 | break; |
michael@0 | 2551 | default: |
michael@0 | 2552 | case CKR_OK: |
michael@0 | 2553 | error = CKR_GENERAL_ERROR; |
michael@0 | 2554 | break; |
michael@0 | 2555 | } |
michael@0 | 2556 | |
michael@0 | 2557 | return error; |
michael@0 | 2558 | } |
michael@0 | 2559 | |
michael@0 | 2560 | /* |
michael@0 | 2561 | * NSSCKFWC_FindObjects |
michael@0 | 2562 | * |
michael@0 | 2563 | */ |
michael@0 | 2564 | NSS_IMPLEMENT CK_RV |
michael@0 | 2565 | NSSCKFWC_FindObjects |
michael@0 | 2566 | ( |
michael@0 | 2567 | NSSCKFWInstance *fwInstance, |
michael@0 | 2568 | CK_SESSION_HANDLE hSession, |
michael@0 | 2569 | CK_OBJECT_HANDLE_PTR phObject, |
michael@0 | 2570 | CK_ULONG ulMaxObjectCount, |
michael@0 | 2571 | CK_ULONG_PTR pulObjectCount |
michael@0 | 2572 | ) |
michael@0 | 2573 | { |
michael@0 | 2574 | CK_RV error = CKR_OK; |
michael@0 | 2575 | NSSCKFWSession *fwSession; |
michael@0 | 2576 | NSSCKFWFindObjects *fwFindObjects; |
michael@0 | 2577 | CK_ULONG i; |
michael@0 | 2578 | |
michael@0 | 2579 | if (!fwInstance) { |
michael@0 | 2580 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2581 | goto loser; |
michael@0 | 2582 | } |
michael@0 | 2583 | |
michael@0 | 2584 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2585 | if (!fwSession) { |
michael@0 | 2586 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2587 | goto loser; |
michael@0 | 2588 | } |
michael@0 | 2589 | |
michael@0 | 2590 | if( (CK_OBJECT_HANDLE_PTR)CK_NULL_PTR == phObject ) { |
michael@0 | 2591 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 2592 | goto loser; |
michael@0 | 2593 | } |
michael@0 | 2594 | |
michael@0 | 2595 | /* |
michael@0 | 2596 | * A purify error here indicates caller error. |
michael@0 | 2597 | */ |
michael@0 | 2598 | (void)nsslibc_memset(phObject, 0, sizeof(CK_OBJECT_HANDLE) * ulMaxObjectCount); |
michael@0 | 2599 | *pulObjectCount = (CK_ULONG)0; |
michael@0 | 2600 | |
michael@0 | 2601 | fwFindObjects = nssCKFWSession_GetFWFindObjects(fwSession, &error); |
michael@0 | 2602 | if (!fwFindObjects) { |
michael@0 | 2603 | goto loser; |
michael@0 | 2604 | } |
michael@0 | 2605 | |
michael@0 | 2606 | for( i = 0; i < ulMaxObjectCount; i++ ) { |
michael@0 | 2607 | NSSCKFWObject *fwObject = nssCKFWFindObjects_Next(fwFindObjects, |
michael@0 | 2608 | NULL, &error); |
michael@0 | 2609 | if (!fwObject) { |
michael@0 | 2610 | break; |
michael@0 | 2611 | } |
michael@0 | 2612 | |
michael@0 | 2613 | phObject[i] = nssCKFWInstance_FindObjectHandle(fwInstance, fwObject); |
michael@0 | 2614 | if( (CK_OBJECT_HANDLE)0 == phObject[i] ) { |
michael@0 | 2615 | phObject[i] = nssCKFWInstance_CreateObjectHandle(fwInstance, fwObject, &error); |
michael@0 | 2616 | } |
michael@0 | 2617 | if( (CK_OBJECT_HANDLE)0 == phObject[i] ) { |
michael@0 | 2618 | /* This isn't right either, is it? */ |
michael@0 | 2619 | nssCKFWObject_Destroy(fwObject); |
michael@0 | 2620 | goto loser; |
michael@0 | 2621 | } |
michael@0 | 2622 | } |
michael@0 | 2623 | |
michael@0 | 2624 | *pulObjectCount = i; |
michael@0 | 2625 | |
michael@0 | 2626 | return CKR_OK; |
michael@0 | 2627 | |
michael@0 | 2628 | loser: |
michael@0 | 2629 | switch( error ) { |
michael@0 | 2630 | case CKR_SESSION_CLOSED: |
michael@0 | 2631 | /* destroy session? */ |
michael@0 | 2632 | break; |
michael@0 | 2633 | case CKR_DEVICE_REMOVED: |
michael@0 | 2634 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2635 | break; |
michael@0 | 2636 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2637 | case CKR_DEVICE_ERROR: |
michael@0 | 2638 | case CKR_DEVICE_MEMORY: |
michael@0 | 2639 | case CKR_FUNCTION_FAILED: |
michael@0 | 2640 | case CKR_GENERAL_ERROR: |
michael@0 | 2641 | case CKR_HOST_MEMORY: |
michael@0 | 2642 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 2643 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2644 | break; |
michael@0 | 2645 | default: |
michael@0 | 2646 | case CKR_OK: |
michael@0 | 2647 | error = CKR_GENERAL_ERROR; |
michael@0 | 2648 | break; |
michael@0 | 2649 | } |
michael@0 | 2650 | |
michael@0 | 2651 | return error; |
michael@0 | 2652 | } |
michael@0 | 2653 | |
michael@0 | 2654 | /* |
michael@0 | 2655 | * NSSCKFWC_FindObjectsFinal |
michael@0 | 2656 | * |
michael@0 | 2657 | */ |
michael@0 | 2658 | NSS_IMPLEMENT CK_RV |
michael@0 | 2659 | NSSCKFWC_FindObjectsFinal |
michael@0 | 2660 | ( |
michael@0 | 2661 | NSSCKFWInstance *fwInstance, |
michael@0 | 2662 | CK_SESSION_HANDLE hSession |
michael@0 | 2663 | ) |
michael@0 | 2664 | { |
michael@0 | 2665 | CK_RV error = CKR_OK; |
michael@0 | 2666 | NSSCKFWSession *fwSession; |
michael@0 | 2667 | NSSCKFWFindObjects *fwFindObjects; |
michael@0 | 2668 | |
michael@0 | 2669 | if (!fwInstance) { |
michael@0 | 2670 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2671 | goto loser; |
michael@0 | 2672 | } |
michael@0 | 2673 | |
michael@0 | 2674 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2675 | if (!fwSession) { |
michael@0 | 2676 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2677 | goto loser; |
michael@0 | 2678 | } |
michael@0 | 2679 | |
michael@0 | 2680 | fwFindObjects = nssCKFWSession_GetFWFindObjects(fwSession, &error); |
michael@0 | 2681 | if (!fwFindObjects) { |
michael@0 | 2682 | error = CKR_OPERATION_NOT_INITIALIZED; |
michael@0 | 2683 | goto loser; |
michael@0 | 2684 | } |
michael@0 | 2685 | |
michael@0 | 2686 | nssCKFWFindObjects_Destroy(fwFindObjects); |
michael@0 | 2687 | error = nssCKFWSession_SetFWFindObjects(fwSession, |
michael@0 | 2688 | (NSSCKFWFindObjects *)NULL); |
michael@0 | 2689 | |
michael@0 | 2690 | if( CKR_OK != error ) { |
michael@0 | 2691 | goto loser; |
michael@0 | 2692 | } |
michael@0 | 2693 | |
michael@0 | 2694 | return CKR_OK; |
michael@0 | 2695 | |
michael@0 | 2696 | loser: |
michael@0 | 2697 | switch( error ) { |
michael@0 | 2698 | case CKR_SESSION_CLOSED: |
michael@0 | 2699 | /* destroy session? */ |
michael@0 | 2700 | break; |
michael@0 | 2701 | case CKR_DEVICE_REMOVED: |
michael@0 | 2702 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 2703 | break; |
michael@0 | 2704 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2705 | case CKR_DEVICE_ERROR: |
michael@0 | 2706 | case CKR_DEVICE_MEMORY: |
michael@0 | 2707 | case CKR_FUNCTION_FAILED: |
michael@0 | 2708 | case CKR_GENERAL_ERROR: |
michael@0 | 2709 | case CKR_HOST_MEMORY: |
michael@0 | 2710 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 2711 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2712 | break; |
michael@0 | 2713 | default: |
michael@0 | 2714 | case CKR_OK: |
michael@0 | 2715 | error = CKR_GENERAL_ERROR; |
michael@0 | 2716 | break; |
michael@0 | 2717 | } |
michael@0 | 2718 | |
michael@0 | 2719 | return error; |
michael@0 | 2720 | } |
michael@0 | 2721 | |
michael@0 | 2722 | /* |
michael@0 | 2723 | * NSSCKFWC_EncryptInit |
michael@0 | 2724 | * |
michael@0 | 2725 | */ |
michael@0 | 2726 | NSS_IMPLEMENT CK_RV |
michael@0 | 2727 | NSSCKFWC_EncryptInit |
michael@0 | 2728 | ( |
michael@0 | 2729 | NSSCKFWInstance *fwInstance, |
michael@0 | 2730 | CK_SESSION_HANDLE hSession, |
michael@0 | 2731 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 2732 | CK_OBJECT_HANDLE hKey |
michael@0 | 2733 | ) |
michael@0 | 2734 | { |
michael@0 | 2735 | CK_RV error = CKR_OK; |
michael@0 | 2736 | NSSCKFWSession *fwSession; |
michael@0 | 2737 | NSSCKFWObject *fwObject; |
michael@0 | 2738 | NSSCKFWSlot *fwSlot; |
michael@0 | 2739 | NSSCKFWToken *fwToken; |
michael@0 | 2740 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 2741 | |
michael@0 | 2742 | if (!fwInstance) { |
michael@0 | 2743 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2744 | goto loser; |
michael@0 | 2745 | } |
michael@0 | 2746 | |
michael@0 | 2747 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2748 | if (!fwSession) { |
michael@0 | 2749 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2750 | goto loser; |
michael@0 | 2751 | } |
michael@0 | 2752 | |
michael@0 | 2753 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 2754 | if (!fwObject) { |
michael@0 | 2755 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 2756 | goto loser; |
michael@0 | 2757 | } |
michael@0 | 2758 | |
michael@0 | 2759 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 2760 | if (!fwSlot) { |
michael@0 | 2761 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 2762 | goto loser; |
michael@0 | 2763 | } |
michael@0 | 2764 | |
michael@0 | 2765 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 2766 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 2767 | goto loser; |
michael@0 | 2768 | } |
michael@0 | 2769 | |
michael@0 | 2770 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 2771 | if (!fwToken) { |
michael@0 | 2772 | goto loser; |
michael@0 | 2773 | } |
michael@0 | 2774 | |
michael@0 | 2775 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 2776 | if (!fwMechanism) { |
michael@0 | 2777 | goto loser; |
michael@0 | 2778 | } |
michael@0 | 2779 | |
michael@0 | 2780 | error = nssCKFWMechanism_EncryptInit(fwMechanism, pMechanism, |
michael@0 | 2781 | fwSession, fwObject); |
michael@0 | 2782 | |
michael@0 | 2783 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 2784 | |
michael@0 | 2785 | if (CKR_OK == error) { |
michael@0 | 2786 | return CKR_OK; |
michael@0 | 2787 | } |
michael@0 | 2788 | |
michael@0 | 2789 | loser: |
michael@0 | 2790 | /* verify error */ |
michael@0 | 2791 | switch( error ) { |
michael@0 | 2792 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2793 | case CKR_DEVICE_ERROR: |
michael@0 | 2794 | case CKR_DEVICE_MEMORY: |
michael@0 | 2795 | case CKR_DEVICE_REMOVED: |
michael@0 | 2796 | case CKR_FUNCTION_CANCELED: |
michael@0 | 2797 | case CKR_FUNCTION_FAILED: |
michael@0 | 2798 | case CKR_GENERAL_ERROR: |
michael@0 | 2799 | case CKR_HOST_MEMORY: |
michael@0 | 2800 | case CKR_KEY_FUNCTION_NOT_PERMITTED: |
michael@0 | 2801 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 2802 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 2803 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 2804 | case CKR_MECHANISM_INVALID: |
michael@0 | 2805 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 2806 | case CKR_OPERATION_ACTIVE: |
michael@0 | 2807 | case CKR_PIN_EXPIRED: |
michael@0 | 2808 | case CKR_SESSION_CLOSED: |
michael@0 | 2809 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2810 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 2811 | break; |
michael@0 | 2812 | default: |
michael@0 | 2813 | case CKR_OK: |
michael@0 | 2814 | error = CKR_GENERAL_ERROR; |
michael@0 | 2815 | break; |
michael@0 | 2816 | } |
michael@0 | 2817 | return error; |
michael@0 | 2818 | } |
michael@0 | 2819 | |
michael@0 | 2820 | /* |
michael@0 | 2821 | * NSSCKFWC_Encrypt |
michael@0 | 2822 | * |
michael@0 | 2823 | */ |
michael@0 | 2824 | NSS_IMPLEMENT CK_RV |
michael@0 | 2825 | NSSCKFWC_Encrypt |
michael@0 | 2826 | ( |
michael@0 | 2827 | NSSCKFWInstance *fwInstance, |
michael@0 | 2828 | CK_SESSION_HANDLE hSession, |
michael@0 | 2829 | CK_BYTE_PTR pData, |
michael@0 | 2830 | CK_ULONG ulDataLen, |
michael@0 | 2831 | CK_BYTE_PTR pEncryptedData, |
michael@0 | 2832 | CK_ULONG_PTR pulEncryptedDataLen |
michael@0 | 2833 | ) |
michael@0 | 2834 | { |
michael@0 | 2835 | CK_RV error = CKR_OK; |
michael@0 | 2836 | NSSCKFWSession *fwSession; |
michael@0 | 2837 | |
michael@0 | 2838 | if (!fwInstance) { |
michael@0 | 2839 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2840 | goto loser; |
michael@0 | 2841 | } |
michael@0 | 2842 | |
michael@0 | 2843 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2844 | if (!fwSession) { |
michael@0 | 2845 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2846 | goto loser; |
michael@0 | 2847 | } |
michael@0 | 2848 | |
michael@0 | 2849 | error = nssCKFWSession_UpdateFinal(fwSession, |
michael@0 | 2850 | NSSCKFWCryptoOperationType_Encrypt, |
michael@0 | 2851 | NSSCKFWCryptoOperationState_EncryptDecrypt, |
michael@0 | 2852 | pData, ulDataLen, pEncryptedData, pulEncryptedDataLen); |
michael@0 | 2853 | |
michael@0 | 2854 | if (CKR_OK == error) { |
michael@0 | 2855 | return CKR_OK; |
michael@0 | 2856 | } |
michael@0 | 2857 | |
michael@0 | 2858 | loser: |
michael@0 | 2859 | /* verify error */ |
michael@0 | 2860 | switch( error ) { |
michael@0 | 2861 | case CKR_ARGUMENTS_BAD: |
michael@0 | 2862 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 2863 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2864 | case CKR_DATA_INVALID: |
michael@0 | 2865 | case CKR_DATA_LEN_RANGE: |
michael@0 | 2866 | case CKR_DEVICE_ERROR: |
michael@0 | 2867 | case CKR_DEVICE_MEMORY: |
michael@0 | 2868 | case CKR_DEVICE_REMOVED: |
michael@0 | 2869 | case CKR_FUNCTION_CANCELED: |
michael@0 | 2870 | case CKR_FUNCTION_FAILED: |
michael@0 | 2871 | case CKR_GENERAL_ERROR: |
michael@0 | 2872 | case CKR_HOST_MEMORY: |
michael@0 | 2873 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 2874 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2875 | case CKR_SESSION_CLOSED: |
michael@0 | 2876 | break; |
michael@0 | 2877 | default: |
michael@0 | 2878 | case CKR_OK: |
michael@0 | 2879 | error = CKR_GENERAL_ERROR; |
michael@0 | 2880 | break; |
michael@0 | 2881 | } |
michael@0 | 2882 | return error; |
michael@0 | 2883 | } |
michael@0 | 2884 | |
michael@0 | 2885 | /* |
michael@0 | 2886 | * NSSCKFWC_EncryptUpdate |
michael@0 | 2887 | * |
michael@0 | 2888 | */ |
michael@0 | 2889 | NSS_IMPLEMENT CK_RV |
michael@0 | 2890 | NSSCKFWC_EncryptUpdate |
michael@0 | 2891 | ( |
michael@0 | 2892 | NSSCKFWInstance *fwInstance, |
michael@0 | 2893 | CK_SESSION_HANDLE hSession, |
michael@0 | 2894 | CK_BYTE_PTR pPart, |
michael@0 | 2895 | CK_ULONG ulPartLen, |
michael@0 | 2896 | CK_BYTE_PTR pEncryptedPart, |
michael@0 | 2897 | CK_ULONG_PTR pulEncryptedPartLen |
michael@0 | 2898 | ) |
michael@0 | 2899 | { |
michael@0 | 2900 | CK_RV error = CKR_OK; |
michael@0 | 2901 | NSSCKFWSession *fwSession; |
michael@0 | 2902 | |
michael@0 | 2903 | if (!fwInstance) { |
michael@0 | 2904 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2905 | goto loser; |
michael@0 | 2906 | } |
michael@0 | 2907 | |
michael@0 | 2908 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2909 | if (!fwSession) { |
michael@0 | 2910 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2911 | goto loser; |
michael@0 | 2912 | } |
michael@0 | 2913 | |
michael@0 | 2914 | error = nssCKFWSession_Update(fwSession, |
michael@0 | 2915 | NSSCKFWCryptoOperationType_Encrypt, |
michael@0 | 2916 | NSSCKFWCryptoOperationState_EncryptDecrypt, |
michael@0 | 2917 | pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen); |
michael@0 | 2918 | |
michael@0 | 2919 | if (CKR_OK == error) { |
michael@0 | 2920 | return CKR_OK; |
michael@0 | 2921 | } |
michael@0 | 2922 | |
michael@0 | 2923 | loser: |
michael@0 | 2924 | /* verify error */ |
michael@0 | 2925 | switch( error ) { |
michael@0 | 2926 | case CKR_ARGUMENTS_BAD: |
michael@0 | 2927 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 2928 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2929 | case CKR_DATA_LEN_RANGE: |
michael@0 | 2930 | case CKR_DEVICE_ERROR: |
michael@0 | 2931 | case CKR_DEVICE_MEMORY: |
michael@0 | 2932 | case CKR_DEVICE_REMOVED: |
michael@0 | 2933 | case CKR_FUNCTION_CANCELED: |
michael@0 | 2934 | case CKR_FUNCTION_FAILED: |
michael@0 | 2935 | case CKR_GENERAL_ERROR: |
michael@0 | 2936 | case CKR_HOST_MEMORY: |
michael@0 | 2937 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 2938 | case CKR_SESSION_CLOSED: |
michael@0 | 2939 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 2940 | break; |
michael@0 | 2941 | default: |
michael@0 | 2942 | case CKR_OK: |
michael@0 | 2943 | error = CKR_GENERAL_ERROR; |
michael@0 | 2944 | break; |
michael@0 | 2945 | } |
michael@0 | 2946 | return error; |
michael@0 | 2947 | } |
michael@0 | 2948 | |
michael@0 | 2949 | /* |
michael@0 | 2950 | * NSSCKFWC_EncryptFinal |
michael@0 | 2951 | * |
michael@0 | 2952 | */ |
michael@0 | 2953 | NSS_IMPLEMENT CK_RV |
michael@0 | 2954 | NSSCKFWC_EncryptFinal |
michael@0 | 2955 | ( |
michael@0 | 2956 | NSSCKFWInstance *fwInstance, |
michael@0 | 2957 | CK_SESSION_HANDLE hSession, |
michael@0 | 2958 | CK_BYTE_PTR pLastEncryptedPart, |
michael@0 | 2959 | CK_ULONG_PTR pulLastEncryptedPartLen |
michael@0 | 2960 | ) |
michael@0 | 2961 | { |
michael@0 | 2962 | CK_RV error = CKR_OK; |
michael@0 | 2963 | NSSCKFWSession *fwSession; |
michael@0 | 2964 | |
michael@0 | 2965 | if (!fwInstance) { |
michael@0 | 2966 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 2967 | goto loser; |
michael@0 | 2968 | } |
michael@0 | 2969 | |
michael@0 | 2970 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 2971 | if (!fwSession) { |
michael@0 | 2972 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 2973 | goto loser; |
michael@0 | 2974 | } |
michael@0 | 2975 | |
michael@0 | 2976 | error = nssCKFWSession_Final(fwSession, |
michael@0 | 2977 | NSSCKFWCryptoOperationType_Encrypt, |
michael@0 | 2978 | NSSCKFWCryptoOperationState_EncryptDecrypt, |
michael@0 | 2979 | pLastEncryptedPart, pulLastEncryptedPartLen); |
michael@0 | 2980 | |
michael@0 | 2981 | if (CKR_OK == error) { |
michael@0 | 2982 | return CKR_OK; |
michael@0 | 2983 | } |
michael@0 | 2984 | |
michael@0 | 2985 | loser: |
michael@0 | 2986 | /* verify error */ |
michael@0 | 2987 | switch( error ) { |
michael@0 | 2988 | case CKR_ARGUMENTS_BAD: |
michael@0 | 2989 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 2990 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 2991 | case CKR_DATA_LEN_RANGE: |
michael@0 | 2992 | case CKR_DEVICE_ERROR: |
michael@0 | 2993 | case CKR_DEVICE_MEMORY: |
michael@0 | 2994 | case CKR_DEVICE_REMOVED: |
michael@0 | 2995 | case CKR_FUNCTION_CANCELED: |
michael@0 | 2996 | case CKR_FUNCTION_FAILED: |
michael@0 | 2997 | case CKR_GENERAL_ERROR: |
michael@0 | 2998 | case CKR_HOST_MEMORY: |
michael@0 | 2999 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3000 | case CKR_SESSION_CLOSED: |
michael@0 | 3001 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3002 | break; |
michael@0 | 3003 | default: |
michael@0 | 3004 | case CKR_OK: |
michael@0 | 3005 | error = CKR_GENERAL_ERROR; |
michael@0 | 3006 | break; |
michael@0 | 3007 | } |
michael@0 | 3008 | return error; |
michael@0 | 3009 | } |
michael@0 | 3010 | |
michael@0 | 3011 | /* |
michael@0 | 3012 | * NSSCKFWC_DecryptInit |
michael@0 | 3013 | * |
michael@0 | 3014 | */ |
michael@0 | 3015 | NSS_IMPLEMENT CK_RV |
michael@0 | 3016 | NSSCKFWC_DecryptInit |
michael@0 | 3017 | ( |
michael@0 | 3018 | NSSCKFWInstance *fwInstance, |
michael@0 | 3019 | CK_SESSION_HANDLE hSession, |
michael@0 | 3020 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 3021 | CK_OBJECT_HANDLE hKey |
michael@0 | 3022 | ) |
michael@0 | 3023 | { |
michael@0 | 3024 | CK_RV error = CKR_OK; |
michael@0 | 3025 | NSSCKFWSession *fwSession; |
michael@0 | 3026 | NSSCKFWObject *fwObject; |
michael@0 | 3027 | NSSCKFWSlot *fwSlot; |
michael@0 | 3028 | NSSCKFWToken *fwToken; |
michael@0 | 3029 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 3030 | |
michael@0 | 3031 | if (!fwInstance) { |
michael@0 | 3032 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3033 | goto loser; |
michael@0 | 3034 | } |
michael@0 | 3035 | |
michael@0 | 3036 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3037 | if (!fwSession) { |
michael@0 | 3038 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3039 | goto loser; |
michael@0 | 3040 | } |
michael@0 | 3041 | |
michael@0 | 3042 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 3043 | if (!fwObject) { |
michael@0 | 3044 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 3045 | goto loser; |
michael@0 | 3046 | } |
michael@0 | 3047 | |
michael@0 | 3048 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 3049 | if (!fwSlot) { |
michael@0 | 3050 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 3051 | goto loser; |
michael@0 | 3052 | } |
michael@0 | 3053 | |
michael@0 | 3054 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 3055 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 3056 | goto loser; |
michael@0 | 3057 | } |
michael@0 | 3058 | |
michael@0 | 3059 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 3060 | if (!fwToken) { |
michael@0 | 3061 | goto loser; |
michael@0 | 3062 | } |
michael@0 | 3063 | |
michael@0 | 3064 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 3065 | if (!fwMechanism) { |
michael@0 | 3066 | goto loser; |
michael@0 | 3067 | } |
michael@0 | 3068 | |
michael@0 | 3069 | error = nssCKFWMechanism_DecryptInit(fwMechanism, pMechanism, |
michael@0 | 3070 | fwSession, fwObject); |
michael@0 | 3071 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 3072 | |
michael@0 | 3073 | if (CKR_OK == error) { |
michael@0 | 3074 | return CKR_OK; |
michael@0 | 3075 | } |
michael@0 | 3076 | |
michael@0 | 3077 | loser: |
michael@0 | 3078 | /* verify error */ |
michael@0 | 3079 | switch( error ) { |
michael@0 | 3080 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3081 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3082 | case CKR_DEVICE_ERROR: |
michael@0 | 3083 | case CKR_DEVICE_MEMORY: |
michael@0 | 3084 | case CKR_DEVICE_REMOVED: |
michael@0 | 3085 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3086 | case CKR_FUNCTION_FAILED: |
michael@0 | 3087 | case CKR_GENERAL_ERROR: |
michael@0 | 3088 | case CKR_HOST_MEMORY: |
michael@0 | 3089 | case CKR_KEY_FUNCTION_NOT_PERMITTED: |
michael@0 | 3090 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 3091 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 3092 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 3093 | case CKR_MECHANISM_INVALID: |
michael@0 | 3094 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 3095 | case CKR_OPERATION_ACTIVE: |
michael@0 | 3096 | case CKR_PIN_EXPIRED: |
michael@0 | 3097 | case CKR_SESSION_CLOSED: |
michael@0 | 3098 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3099 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3100 | break; |
michael@0 | 3101 | default: |
michael@0 | 3102 | case CKR_OK: |
michael@0 | 3103 | error = CKR_GENERAL_ERROR; |
michael@0 | 3104 | break; |
michael@0 | 3105 | } |
michael@0 | 3106 | return error; |
michael@0 | 3107 | } |
michael@0 | 3108 | |
michael@0 | 3109 | /* |
michael@0 | 3110 | * NSSCKFWC_Decrypt |
michael@0 | 3111 | * |
michael@0 | 3112 | */ |
michael@0 | 3113 | NSS_IMPLEMENT CK_RV |
michael@0 | 3114 | NSSCKFWC_Decrypt |
michael@0 | 3115 | ( |
michael@0 | 3116 | NSSCKFWInstance *fwInstance, |
michael@0 | 3117 | CK_SESSION_HANDLE hSession, |
michael@0 | 3118 | CK_BYTE_PTR pEncryptedData, |
michael@0 | 3119 | CK_ULONG ulEncryptedDataLen, |
michael@0 | 3120 | CK_BYTE_PTR pData, |
michael@0 | 3121 | CK_ULONG_PTR pulDataLen |
michael@0 | 3122 | ) |
michael@0 | 3123 | { |
michael@0 | 3124 | CK_RV error = CKR_OK; |
michael@0 | 3125 | NSSCKFWSession *fwSession; |
michael@0 | 3126 | |
michael@0 | 3127 | if (!fwInstance) { |
michael@0 | 3128 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3129 | goto loser; |
michael@0 | 3130 | } |
michael@0 | 3131 | |
michael@0 | 3132 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3133 | if (!fwSession) { |
michael@0 | 3134 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3135 | goto loser; |
michael@0 | 3136 | } |
michael@0 | 3137 | |
michael@0 | 3138 | error = nssCKFWSession_UpdateFinal(fwSession, |
michael@0 | 3139 | NSSCKFWCryptoOperationType_Decrypt, |
michael@0 | 3140 | NSSCKFWCryptoOperationState_EncryptDecrypt, |
michael@0 | 3141 | pEncryptedData, ulEncryptedDataLen, pData, pulDataLen); |
michael@0 | 3142 | |
michael@0 | 3143 | if (CKR_OK == error) { |
michael@0 | 3144 | return CKR_OK; |
michael@0 | 3145 | } |
michael@0 | 3146 | |
michael@0 | 3147 | loser: |
michael@0 | 3148 | /* verify error */ |
michael@0 | 3149 | switch( error ) { |
michael@0 | 3150 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3151 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 3152 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3153 | case CKR_DEVICE_ERROR: |
michael@0 | 3154 | case CKR_DEVICE_MEMORY: |
michael@0 | 3155 | case CKR_DEVICE_REMOVED: |
michael@0 | 3156 | case CKR_ENCRYPTED_DATA_INVALID: |
michael@0 | 3157 | case CKR_ENCRYPTED_DATA_LEN_RANGE: |
michael@0 | 3158 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3159 | case CKR_FUNCTION_FAILED: |
michael@0 | 3160 | case CKR_GENERAL_ERROR: |
michael@0 | 3161 | case CKR_HOST_MEMORY: |
michael@0 | 3162 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3163 | case CKR_SESSION_CLOSED: |
michael@0 | 3164 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3165 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3166 | break; |
michael@0 | 3167 | case CKR_DATA_LEN_RANGE: |
michael@0 | 3168 | error = CKR_ENCRYPTED_DATA_LEN_RANGE; |
michael@0 | 3169 | break; |
michael@0 | 3170 | case CKR_DATA_INVALID: |
michael@0 | 3171 | error = CKR_ENCRYPTED_DATA_INVALID; |
michael@0 | 3172 | break; |
michael@0 | 3173 | default: |
michael@0 | 3174 | case CKR_OK: |
michael@0 | 3175 | error = CKR_GENERAL_ERROR; |
michael@0 | 3176 | break; |
michael@0 | 3177 | } |
michael@0 | 3178 | return error; |
michael@0 | 3179 | } |
michael@0 | 3180 | |
michael@0 | 3181 | /* |
michael@0 | 3182 | * NSSCKFWC_DecryptUpdate |
michael@0 | 3183 | * |
michael@0 | 3184 | */ |
michael@0 | 3185 | NSS_IMPLEMENT CK_RV |
michael@0 | 3186 | NSSCKFWC_DecryptUpdate |
michael@0 | 3187 | ( |
michael@0 | 3188 | NSSCKFWInstance *fwInstance, |
michael@0 | 3189 | CK_SESSION_HANDLE hSession, |
michael@0 | 3190 | CK_BYTE_PTR pEncryptedPart, |
michael@0 | 3191 | CK_ULONG ulEncryptedPartLen, |
michael@0 | 3192 | CK_BYTE_PTR pPart, |
michael@0 | 3193 | CK_ULONG_PTR pulPartLen |
michael@0 | 3194 | ) |
michael@0 | 3195 | { |
michael@0 | 3196 | CK_RV error = CKR_OK; |
michael@0 | 3197 | NSSCKFWSession *fwSession; |
michael@0 | 3198 | |
michael@0 | 3199 | if (!fwInstance) { |
michael@0 | 3200 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3201 | goto loser; |
michael@0 | 3202 | } |
michael@0 | 3203 | |
michael@0 | 3204 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3205 | if (!fwSession) { |
michael@0 | 3206 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3207 | goto loser; |
michael@0 | 3208 | } |
michael@0 | 3209 | |
michael@0 | 3210 | error = nssCKFWSession_Update(fwSession, |
michael@0 | 3211 | NSSCKFWCryptoOperationType_Decrypt, |
michael@0 | 3212 | NSSCKFWCryptoOperationState_EncryptDecrypt, |
michael@0 | 3213 | pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen); |
michael@0 | 3214 | |
michael@0 | 3215 | if (CKR_OK == error) { |
michael@0 | 3216 | return CKR_OK; |
michael@0 | 3217 | } |
michael@0 | 3218 | |
michael@0 | 3219 | loser: |
michael@0 | 3220 | /* verify error */ |
michael@0 | 3221 | switch( error ) { |
michael@0 | 3222 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3223 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 3224 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3225 | case CKR_DEVICE_ERROR: |
michael@0 | 3226 | case CKR_DEVICE_MEMORY: |
michael@0 | 3227 | case CKR_DEVICE_REMOVED: |
michael@0 | 3228 | case CKR_ENCRYPTED_DATA_INVALID: |
michael@0 | 3229 | case CKR_ENCRYPTED_DATA_LEN_RANGE: |
michael@0 | 3230 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3231 | case CKR_FUNCTION_FAILED: |
michael@0 | 3232 | case CKR_GENERAL_ERROR: |
michael@0 | 3233 | case CKR_HOST_MEMORY: |
michael@0 | 3234 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3235 | case CKR_SESSION_CLOSED: |
michael@0 | 3236 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3237 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3238 | break; |
michael@0 | 3239 | case CKR_DATA_LEN_RANGE: |
michael@0 | 3240 | error = CKR_ENCRYPTED_DATA_LEN_RANGE; |
michael@0 | 3241 | break; |
michael@0 | 3242 | case CKR_DATA_INVALID: |
michael@0 | 3243 | error = CKR_ENCRYPTED_DATA_INVALID; |
michael@0 | 3244 | break; |
michael@0 | 3245 | default: |
michael@0 | 3246 | case CKR_OK: |
michael@0 | 3247 | error = CKR_GENERAL_ERROR; |
michael@0 | 3248 | break; |
michael@0 | 3249 | } |
michael@0 | 3250 | return error; |
michael@0 | 3251 | } |
michael@0 | 3252 | |
michael@0 | 3253 | /* |
michael@0 | 3254 | * NSSCKFWC_DecryptFinal |
michael@0 | 3255 | * |
michael@0 | 3256 | */ |
michael@0 | 3257 | NSS_IMPLEMENT CK_RV |
michael@0 | 3258 | NSSCKFWC_DecryptFinal |
michael@0 | 3259 | ( |
michael@0 | 3260 | NSSCKFWInstance *fwInstance, |
michael@0 | 3261 | CK_SESSION_HANDLE hSession, |
michael@0 | 3262 | CK_BYTE_PTR pLastPart, |
michael@0 | 3263 | CK_ULONG_PTR pulLastPartLen |
michael@0 | 3264 | ) |
michael@0 | 3265 | { |
michael@0 | 3266 | CK_RV error = CKR_OK; |
michael@0 | 3267 | NSSCKFWSession *fwSession; |
michael@0 | 3268 | |
michael@0 | 3269 | if (!fwInstance) { |
michael@0 | 3270 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3271 | goto loser; |
michael@0 | 3272 | } |
michael@0 | 3273 | |
michael@0 | 3274 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3275 | if (!fwSession) { |
michael@0 | 3276 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3277 | goto loser; |
michael@0 | 3278 | } |
michael@0 | 3279 | |
michael@0 | 3280 | error = nssCKFWSession_Final(fwSession, |
michael@0 | 3281 | NSSCKFWCryptoOperationType_Decrypt, |
michael@0 | 3282 | NSSCKFWCryptoOperationState_EncryptDecrypt, |
michael@0 | 3283 | pLastPart, pulLastPartLen); |
michael@0 | 3284 | |
michael@0 | 3285 | if (CKR_OK == error) { |
michael@0 | 3286 | return CKR_OK; |
michael@0 | 3287 | } |
michael@0 | 3288 | |
michael@0 | 3289 | loser: |
michael@0 | 3290 | /* verify error */ |
michael@0 | 3291 | switch( error ) { |
michael@0 | 3292 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3293 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 3294 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3295 | case CKR_DEVICE_ERROR: |
michael@0 | 3296 | case CKR_DEVICE_MEMORY: |
michael@0 | 3297 | case CKR_DEVICE_REMOVED: |
michael@0 | 3298 | case CKR_FUNCTION_FAILED: |
michael@0 | 3299 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3300 | case CKR_ENCRYPTED_DATA_INVALID: |
michael@0 | 3301 | case CKR_ENCRYPTED_DATA_LEN_RANGE: |
michael@0 | 3302 | case CKR_GENERAL_ERROR: |
michael@0 | 3303 | case CKR_HOST_MEMORY: |
michael@0 | 3304 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3305 | case CKR_SESSION_CLOSED: |
michael@0 | 3306 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3307 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3308 | break; |
michael@0 | 3309 | case CKR_DATA_LEN_RANGE: |
michael@0 | 3310 | error = CKR_ENCRYPTED_DATA_LEN_RANGE; |
michael@0 | 3311 | break; |
michael@0 | 3312 | case CKR_DATA_INVALID: |
michael@0 | 3313 | error = CKR_ENCRYPTED_DATA_INVALID; |
michael@0 | 3314 | break; |
michael@0 | 3315 | default: |
michael@0 | 3316 | case CKR_OK: |
michael@0 | 3317 | error = CKR_GENERAL_ERROR; |
michael@0 | 3318 | break; |
michael@0 | 3319 | } |
michael@0 | 3320 | return error; |
michael@0 | 3321 | } |
michael@0 | 3322 | |
michael@0 | 3323 | /* |
michael@0 | 3324 | * NSSCKFWC_DigestInit |
michael@0 | 3325 | * |
michael@0 | 3326 | */ |
michael@0 | 3327 | NSS_IMPLEMENT CK_RV |
michael@0 | 3328 | NSSCKFWC_DigestInit |
michael@0 | 3329 | ( |
michael@0 | 3330 | NSSCKFWInstance *fwInstance, |
michael@0 | 3331 | CK_SESSION_HANDLE hSession, |
michael@0 | 3332 | CK_MECHANISM_PTR pMechanism |
michael@0 | 3333 | ) |
michael@0 | 3334 | { |
michael@0 | 3335 | CK_RV error = CKR_OK; |
michael@0 | 3336 | NSSCKFWSession *fwSession; |
michael@0 | 3337 | NSSCKFWSlot *fwSlot; |
michael@0 | 3338 | NSSCKFWToken *fwToken; |
michael@0 | 3339 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 3340 | |
michael@0 | 3341 | if (!fwInstance) { |
michael@0 | 3342 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3343 | goto loser; |
michael@0 | 3344 | } |
michael@0 | 3345 | |
michael@0 | 3346 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3347 | if (!fwSession) { |
michael@0 | 3348 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3349 | goto loser; |
michael@0 | 3350 | } |
michael@0 | 3351 | |
michael@0 | 3352 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 3353 | if (!fwSlot) { |
michael@0 | 3354 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 3355 | goto loser; |
michael@0 | 3356 | } |
michael@0 | 3357 | |
michael@0 | 3358 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 3359 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 3360 | goto loser; |
michael@0 | 3361 | } |
michael@0 | 3362 | |
michael@0 | 3363 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 3364 | if (!fwToken) { |
michael@0 | 3365 | goto loser; |
michael@0 | 3366 | } |
michael@0 | 3367 | |
michael@0 | 3368 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 3369 | if (!fwMechanism) { |
michael@0 | 3370 | goto loser; |
michael@0 | 3371 | } |
michael@0 | 3372 | |
michael@0 | 3373 | error = nssCKFWMechanism_DigestInit(fwMechanism, pMechanism, fwSession); |
michael@0 | 3374 | |
michael@0 | 3375 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 3376 | |
michael@0 | 3377 | if (CKR_OK == error) { |
michael@0 | 3378 | return CKR_OK; |
michael@0 | 3379 | } |
michael@0 | 3380 | |
michael@0 | 3381 | loser: |
michael@0 | 3382 | /* verify error */ |
michael@0 | 3383 | switch( error ) { |
michael@0 | 3384 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3385 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3386 | case CKR_DEVICE_ERROR: |
michael@0 | 3387 | case CKR_DEVICE_MEMORY: |
michael@0 | 3388 | case CKR_DEVICE_REMOVED: |
michael@0 | 3389 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3390 | case CKR_FUNCTION_FAILED: |
michael@0 | 3391 | case CKR_GENERAL_ERROR: |
michael@0 | 3392 | case CKR_HOST_MEMORY: |
michael@0 | 3393 | case CKR_MECHANISM_INVALID: |
michael@0 | 3394 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 3395 | case CKR_OPERATION_ACTIVE: |
michael@0 | 3396 | case CKR_PIN_EXPIRED: |
michael@0 | 3397 | case CKR_SESSION_CLOSED: |
michael@0 | 3398 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3399 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3400 | break; |
michael@0 | 3401 | default: |
michael@0 | 3402 | case CKR_OK: |
michael@0 | 3403 | error = CKR_GENERAL_ERROR; |
michael@0 | 3404 | break; |
michael@0 | 3405 | } |
michael@0 | 3406 | return error; |
michael@0 | 3407 | } |
michael@0 | 3408 | |
michael@0 | 3409 | /* |
michael@0 | 3410 | * NSSCKFWC_Digest |
michael@0 | 3411 | * |
michael@0 | 3412 | */ |
michael@0 | 3413 | NSS_IMPLEMENT CK_RV |
michael@0 | 3414 | NSSCKFWC_Digest |
michael@0 | 3415 | ( |
michael@0 | 3416 | NSSCKFWInstance *fwInstance, |
michael@0 | 3417 | CK_SESSION_HANDLE hSession, |
michael@0 | 3418 | CK_BYTE_PTR pData, |
michael@0 | 3419 | CK_ULONG ulDataLen, |
michael@0 | 3420 | CK_BYTE_PTR pDigest, |
michael@0 | 3421 | CK_ULONG_PTR pulDigestLen |
michael@0 | 3422 | ) |
michael@0 | 3423 | { |
michael@0 | 3424 | CK_RV error = CKR_OK; |
michael@0 | 3425 | NSSCKFWSession *fwSession; |
michael@0 | 3426 | |
michael@0 | 3427 | if (!fwInstance) { |
michael@0 | 3428 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3429 | goto loser; |
michael@0 | 3430 | } |
michael@0 | 3431 | |
michael@0 | 3432 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3433 | if (!fwSession) { |
michael@0 | 3434 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3435 | goto loser; |
michael@0 | 3436 | } |
michael@0 | 3437 | |
michael@0 | 3438 | error = nssCKFWSession_UpdateFinal(fwSession, |
michael@0 | 3439 | NSSCKFWCryptoOperationType_Digest, |
michael@0 | 3440 | NSSCKFWCryptoOperationState_Digest, |
michael@0 | 3441 | pData, ulDataLen, pDigest, pulDigestLen); |
michael@0 | 3442 | |
michael@0 | 3443 | if (CKR_OK == error) { |
michael@0 | 3444 | return CKR_OK; |
michael@0 | 3445 | } |
michael@0 | 3446 | |
michael@0 | 3447 | loser: |
michael@0 | 3448 | /* verify error */ |
michael@0 | 3449 | switch( error ) { |
michael@0 | 3450 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3451 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 3452 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3453 | case CKR_DEVICE_ERROR: |
michael@0 | 3454 | case CKR_DEVICE_MEMORY: |
michael@0 | 3455 | case CKR_DEVICE_REMOVED: |
michael@0 | 3456 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3457 | case CKR_FUNCTION_FAILED: |
michael@0 | 3458 | case CKR_GENERAL_ERROR: |
michael@0 | 3459 | case CKR_HOST_MEMORY: |
michael@0 | 3460 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3461 | case CKR_SESSION_CLOSED: |
michael@0 | 3462 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3463 | break; |
michael@0 | 3464 | default: |
michael@0 | 3465 | case CKR_OK: |
michael@0 | 3466 | error = CKR_GENERAL_ERROR; |
michael@0 | 3467 | break; |
michael@0 | 3468 | } |
michael@0 | 3469 | return error; |
michael@0 | 3470 | } |
michael@0 | 3471 | |
michael@0 | 3472 | /* |
michael@0 | 3473 | * NSSCKFWC_DigestUpdate |
michael@0 | 3474 | * |
michael@0 | 3475 | */ |
michael@0 | 3476 | NSS_IMPLEMENT CK_RV |
michael@0 | 3477 | NSSCKFWC_DigestUpdate |
michael@0 | 3478 | ( |
michael@0 | 3479 | NSSCKFWInstance *fwInstance, |
michael@0 | 3480 | CK_SESSION_HANDLE hSession, |
michael@0 | 3481 | CK_BYTE_PTR pData, |
michael@0 | 3482 | CK_ULONG ulDataLen |
michael@0 | 3483 | ) |
michael@0 | 3484 | { |
michael@0 | 3485 | CK_RV error = CKR_OK; |
michael@0 | 3486 | NSSCKFWSession *fwSession; |
michael@0 | 3487 | |
michael@0 | 3488 | if (!fwInstance) { |
michael@0 | 3489 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3490 | goto loser; |
michael@0 | 3491 | } |
michael@0 | 3492 | |
michael@0 | 3493 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3494 | if (!fwSession) { |
michael@0 | 3495 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3496 | goto loser; |
michael@0 | 3497 | } |
michael@0 | 3498 | |
michael@0 | 3499 | error = nssCKFWSession_DigestUpdate(fwSession, |
michael@0 | 3500 | NSSCKFWCryptoOperationType_Digest, |
michael@0 | 3501 | NSSCKFWCryptoOperationState_Digest, |
michael@0 | 3502 | pData, ulDataLen); |
michael@0 | 3503 | |
michael@0 | 3504 | if (CKR_OK == error) { |
michael@0 | 3505 | return CKR_OK; |
michael@0 | 3506 | } |
michael@0 | 3507 | |
michael@0 | 3508 | loser: |
michael@0 | 3509 | /* verify error */ |
michael@0 | 3510 | switch( error ) { |
michael@0 | 3511 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3512 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3513 | case CKR_DEVICE_ERROR: |
michael@0 | 3514 | case CKR_DEVICE_MEMORY: |
michael@0 | 3515 | case CKR_DEVICE_REMOVED: |
michael@0 | 3516 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3517 | case CKR_FUNCTION_FAILED: |
michael@0 | 3518 | case CKR_GENERAL_ERROR: |
michael@0 | 3519 | case CKR_HOST_MEMORY: |
michael@0 | 3520 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3521 | case CKR_SESSION_CLOSED: |
michael@0 | 3522 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3523 | break; |
michael@0 | 3524 | default: |
michael@0 | 3525 | case CKR_OK: |
michael@0 | 3526 | error = CKR_GENERAL_ERROR; |
michael@0 | 3527 | break; |
michael@0 | 3528 | } |
michael@0 | 3529 | return error; |
michael@0 | 3530 | } |
michael@0 | 3531 | |
michael@0 | 3532 | /* |
michael@0 | 3533 | * NSSCKFWC_DigestKey |
michael@0 | 3534 | * |
michael@0 | 3535 | */ |
michael@0 | 3536 | NSS_IMPLEMENT CK_RV |
michael@0 | 3537 | NSSCKFWC_DigestKey |
michael@0 | 3538 | ( |
michael@0 | 3539 | NSSCKFWInstance *fwInstance, |
michael@0 | 3540 | CK_SESSION_HANDLE hSession, |
michael@0 | 3541 | CK_OBJECT_HANDLE hKey |
michael@0 | 3542 | ) |
michael@0 | 3543 | { |
michael@0 | 3544 | CK_RV error = CKR_OK; |
michael@0 | 3545 | NSSCKFWSession *fwSession; |
michael@0 | 3546 | NSSCKFWObject *fwObject; |
michael@0 | 3547 | |
michael@0 | 3548 | if (!fwInstance) { |
michael@0 | 3549 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3550 | goto loser; |
michael@0 | 3551 | } |
michael@0 | 3552 | |
michael@0 | 3553 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3554 | if (!fwSession) { |
michael@0 | 3555 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3556 | goto loser; |
michael@0 | 3557 | } |
michael@0 | 3558 | |
michael@0 | 3559 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 3560 | if (!fwObject) { |
michael@0 | 3561 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 3562 | goto loser; |
michael@0 | 3563 | } |
michael@0 | 3564 | |
michael@0 | 3565 | error = nssCKFWSession_DigestKey(fwSession, fwObject); |
michael@0 | 3566 | |
michael@0 | 3567 | if (CKR_OK == error) { |
michael@0 | 3568 | return CKR_OK; |
michael@0 | 3569 | } |
michael@0 | 3570 | |
michael@0 | 3571 | loser: |
michael@0 | 3572 | /* verify error */ |
michael@0 | 3573 | switch( error ) { |
michael@0 | 3574 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3575 | case CKR_DEVICE_ERROR: |
michael@0 | 3576 | case CKR_DEVICE_MEMORY: |
michael@0 | 3577 | case CKR_DEVICE_REMOVED: |
michael@0 | 3578 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3579 | case CKR_FUNCTION_FAILED: |
michael@0 | 3580 | case CKR_GENERAL_ERROR: |
michael@0 | 3581 | case CKR_HOST_MEMORY: |
michael@0 | 3582 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 3583 | case CKR_KEY_INDIGESTIBLE: |
michael@0 | 3584 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 3585 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3586 | case CKR_SESSION_CLOSED: |
michael@0 | 3587 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3588 | break; |
michael@0 | 3589 | default: |
michael@0 | 3590 | case CKR_OK: |
michael@0 | 3591 | error = CKR_GENERAL_ERROR; |
michael@0 | 3592 | break; |
michael@0 | 3593 | } |
michael@0 | 3594 | return error; |
michael@0 | 3595 | } |
michael@0 | 3596 | |
michael@0 | 3597 | /* |
michael@0 | 3598 | * NSSCKFWC_DigestFinal |
michael@0 | 3599 | * |
michael@0 | 3600 | */ |
michael@0 | 3601 | NSS_IMPLEMENT CK_RV |
michael@0 | 3602 | NSSCKFWC_DigestFinal |
michael@0 | 3603 | ( |
michael@0 | 3604 | NSSCKFWInstance *fwInstance, |
michael@0 | 3605 | CK_SESSION_HANDLE hSession, |
michael@0 | 3606 | CK_BYTE_PTR pDigest, |
michael@0 | 3607 | CK_ULONG_PTR pulDigestLen |
michael@0 | 3608 | ) |
michael@0 | 3609 | { |
michael@0 | 3610 | CK_RV error = CKR_OK; |
michael@0 | 3611 | NSSCKFWSession *fwSession; |
michael@0 | 3612 | |
michael@0 | 3613 | if (!fwInstance) { |
michael@0 | 3614 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3615 | goto loser; |
michael@0 | 3616 | } |
michael@0 | 3617 | |
michael@0 | 3618 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3619 | if (!fwSession) { |
michael@0 | 3620 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3621 | goto loser; |
michael@0 | 3622 | } |
michael@0 | 3623 | |
michael@0 | 3624 | error = nssCKFWSession_Final(fwSession, |
michael@0 | 3625 | NSSCKFWCryptoOperationType_Digest, |
michael@0 | 3626 | NSSCKFWCryptoOperationState_Digest, |
michael@0 | 3627 | pDigest, pulDigestLen); |
michael@0 | 3628 | |
michael@0 | 3629 | if (CKR_OK == error) { |
michael@0 | 3630 | return CKR_OK; |
michael@0 | 3631 | } |
michael@0 | 3632 | |
michael@0 | 3633 | loser: |
michael@0 | 3634 | /* verify error */ |
michael@0 | 3635 | switch( error ) { |
michael@0 | 3636 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3637 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 3638 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3639 | case CKR_DEVICE_ERROR: |
michael@0 | 3640 | case CKR_DEVICE_MEMORY: |
michael@0 | 3641 | case CKR_DEVICE_REMOVED: |
michael@0 | 3642 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3643 | case CKR_FUNCTION_FAILED: |
michael@0 | 3644 | case CKR_GENERAL_ERROR: |
michael@0 | 3645 | case CKR_HOST_MEMORY: |
michael@0 | 3646 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3647 | case CKR_SESSION_CLOSED: |
michael@0 | 3648 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3649 | break; |
michael@0 | 3650 | default: |
michael@0 | 3651 | case CKR_OK: |
michael@0 | 3652 | error = CKR_GENERAL_ERROR; |
michael@0 | 3653 | break; |
michael@0 | 3654 | } |
michael@0 | 3655 | return error; |
michael@0 | 3656 | } |
michael@0 | 3657 | |
michael@0 | 3658 | /* |
michael@0 | 3659 | * NSSCKFWC_SignInit |
michael@0 | 3660 | * |
michael@0 | 3661 | */ |
michael@0 | 3662 | NSS_IMPLEMENT CK_RV |
michael@0 | 3663 | NSSCKFWC_SignInit |
michael@0 | 3664 | ( |
michael@0 | 3665 | NSSCKFWInstance *fwInstance, |
michael@0 | 3666 | CK_SESSION_HANDLE hSession, |
michael@0 | 3667 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 3668 | CK_OBJECT_HANDLE hKey |
michael@0 | 3669 | ) |
michael@0 | 3670 | { |
michael@0 | 3671 | CK_RV error = CKR_OK; |
michael@0 | 3672 | NSSCKFWSession *fwSession; |
michael@0 | 3673 | NSSCKFWObject *fwObject; |
michael@0 | 3674 | NSSCKFWSlot *fwSlot; |
michael@0 | 3675 | NSSCKFWToken *fwToken; |
michael@0 | 3676 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 3677 | |
michael@0 | 3678 | if (!fwInstance) { |
michael@0 | 3679 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3680 | goto loser; |
michael@0 | 3681 | } |
michael@0 | 3682 | |
michael@0 | 3683 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3684 | if (!fwSession) { |
michael@0 | 3685 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3686 | goto loser; |
michael@0 | 3687 | } |
michael@0 | 3688 | |
michael@0 | 3689 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 3690 | if (!fwObject) { |
michael@0 | 3691 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 3692 | goto loser; |
michael@0 | 3693 | } |
michael@0 | 3694 | |
michael@0 | 3695 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 3696 | if (!fwSlot) { |
michael@0 | 3697 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 3698 | goto loser; |
michael@0 | 3699 | } |
michael@0 | 3700 | |
michael@0 | 3701 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 3702 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 3703 | goto loser; |
michael@0 | 3704 | } |
michael@0 | 3705 | |
michael@0 | 3706 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 3707 | if (!fwToken) { |
michael@0 | 3708 | goto loser; |
michael@0 | 3709 | } |
michael@0 | 3710 | |
michael@0 | 3711 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 3712 | if (!fwMechanism) { |
michael@0 | 3713 | goto loser; |
michael@0 | 3714 | } |
michael@0 | 3715 | |
michael@0 | 3716 | error = nssCKFWMechanism_SignInit(fwMechanism, pMechanism, fwSession, |
michael@0 | 3717 | fwObject); |
michael@0 | 3718 | |
michael@0 | 3719 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 3720 | |
michael@0 | 3721 | if (CKR_OK == error) { |
michael@0 | 3722 | return CKR_OK; |
michael@0 | 3723 | } |
michael@0 | 3724 | |
michael@0 | 3725 | loser: |
michael@0 | 3726 | /* verify error */ |
michael@0 | 3727 | switch( error ) { |
michael@0 | 3728 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3729 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3730 | case CKR_DEVICE_ERROR: |
michael@0 | 3731 | case CKR_DEVICE_MEMORY: |
michael@0 | 3732 | case CKR_DEVICE_REMOVED: |
michael@0 | 3733 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3734 | case CKR_FUNCTION_FAILED: |
michael@0 | 3735 | case CKR_GENERAL_ERROR: |
michael@0 | 3736 | case CKR_HOST_MEMORY: |
michael@0 | 3737 | case CKR_KEY_FUNCTION_NOT_PERMITTED: |
michael@0 | 3738 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 3739 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 3740 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 3741 | case CKR_MECHANISM_INVALID: |
michael@0 | 3742 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 3743 | case CKR_OPERATION_ACTIVE: |
michael@0 | 3744 | case CKR_PIN_EXPIRED: |
michael@0 | 3745 | case CKR_SESSION_CLOSED: |
michael@0 | 3746 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3747 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3748 | break; |
michael@0 | 3749 | default: |
michael@0 | 3750 | case CKR_OK: |
michael@0 | 3751 | error = CKR_GENERAL_ERROR; |
michael@0 | 3752 | break; |
michael@0 | 3753 | } |
michael@0 | 3754 | return error; |
michael@0 | 3755 | } |
michael@0 | 3756 | |
michael@0 | 3757 | /* |
michael@0 | 3758 | * NSSCKFWC_Sign |
michael@0 | 3759 | * |
michael@0 | 3760 | */ |
michael@0 | 3761 | NSS_IMPLEMENT CK_RV |
michael@0 | 3762 | NSSCKFWC_Sign |
michael@0 | 3763 | ( |
michael@0 | 3764 | NSSCKFWInstance *fwInstance, |
michael@0 | 3765 | CK_SESSION_HANDLE hSession, |
michael@0 | 3766 | CK_BYTE_PTR pData, |
michael@0 | 3767 | CK_ULONG ulDataLen, |
michael@0 | 3768 | CK_BYTE_PTR pSignature, |
michael@0 | 3769 | CK_ULONG_PTR pulSignatureLen |
michael@0 | 3770 | ) |
michael@0 | 3771 | { |
michael@0 | 3772 | CK_RV error = CKR_OK; |
michael@0 | 3773 | NSSCKFWSession *fwSession; |
michael@0 | 3774 | |
michael@0 | 3775 | if (!fwInstance) { |
michael@0 | 3776 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3777 | goto loser; |
michael@0 | 3778 | } |
michael@0 | 3779 | |
michael@0 | 3780 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3781 | if (!fwSession) { |
michael@0 | 3782 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3783 | goto loser; |
michael@0 | 3784 | } |
michael@0 | 3785 | |
michael@0 | 3786 | error = nssCKFWSession_UpdateFinal(fwSession, |
michael@0 | 3787 | NSSCKFWCryptoOperationType_Sign, |
michael@0 | 3788 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 3789 | pData, ulDataLen, pSignature, pulSignatureLen); |
michael@0 | 3790 | |
michael@0 | 3791 | if (CKR_OK == error) { |
michael@0 | 3792 | return CKR_OK; |
michael@0 | 3793 | } |
michael@0 | 3794 | |
michael@0 | 3795 | loser: |
michael@0 | 3796 | /* verify error */ |
michael@0 | 3797 | switch( error ) { |
michael@0 | 3798 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3799 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 3800 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3801 | case CKR_DATA_INVALID: |
michael@0 | 3802 | case CKR_DATA_LEN_RANGE: |
michael@0 | 3803 | case CKR_DEVICE_ERROR: |
michael@0 | 3804 | case CKR_DEVICE_MEMORY: |
michael@0 | 3805 | case CKR_DEVICE_REMOVED: |
michael@0 | 3806 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3807 | case CKR_FUNCTION_FAILED: |
michael@0 | 3808 | case CKR_GENERAL_ERROR: |
michael@0 | 3809 | case CKR_HOST_MEMORY: |
michael@0 | 3810 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3811 | case CKR_SESSION_CLOSED: |
michael@0 | 3812 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3813 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3814 | case CKR_FUNCTION_REJECTED: |
michael@0 | 3815 | break; |
michael@0 | 3816 | default: |
michael@0 | 3817 | case CKR_OK: |
michael@0 | 3818 | error = CKR_GENERAL_ERROR; |
michael@0 | 3819 | break; |
michael@0 | 3820 | } |
michael@0 | 3821 | return error; |
michael@0 | 3822 | } |
michael@0 | 3823 | |
michael@0 | 3824 | /* |
michael@0 | 3825 | * NSSCKFWC_SignUpdate |
michael@0 | 3826 | * |
michael@0 | 3827 | */ |
michael@0 | 3828 | NSS_IMPLEMENT CK_RV |
michael@0 | 3829 | NSSCKFWC_SignUpdate |
michael@0 | 3830 | ( |
michael@0 | 3831 | NSSCKFWInstance *fwInstance, |
michael@0 | 3832 | CK_SESSION_HANDLE hSession, |
michael@0 | 3833 | CK_BYTE_PTR pPart, |
michael@0 | 3834 | CK_ULONG ulPartLen |
michael@0 | 3835 | ) |
michael@0 | 3836 | { |
michael@0 | 3837 | CK_RV error = CKR_OK; |
michael@0 | 3838 | NSSCKFWSession *fwSession; |
michael@0 | 3839 | |
michael@0 | 3840 | if (!fwInstance) { |
michael@0 | 3841 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3842 | goto loser; |
michael@0 | 3843 | } |
michael@0 | 3844 | |
michael@0 | 3845 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3846 | if (!fwSession) { |
michael@0 | 3847 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3848 | goto loser; |
michael@0 | 3849 | } |
michael@0 | 3850 | |
michael@0 | 3851 | error = nssCKFWSession_DigestUpdate(fwSession, |
michael@0 | 3852 | NSSCKFWCryptoOperationType_Sign, |
michael@0 | 3853 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 3854 | pPart, ulPartLen); |
michael@0 | 3855 | |
michael@0 | 3856 | if (CKR_OK == error) { |
michael@0 | 3857 | return CKR_OK; |
michael@0 | 3858 | } |
michael@0 | 3859 | |
michael@0 | 3860 | loser: |
michael@0 | 3861 | /* verify error */ |
michael@0 | 3862 | switch( error ) { |
michael@0 | 3863 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3864 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3865 | case CKR_DATA_LEN_RANGE: |
michael@0 | 3866 | case CKR_DEVICE_ERROR: |
michael@0 | 3867 | case CKR_DEVICE_MEMORY: |
michael@0 | 3868 | case CKR_DEVICE_REMOVED: |
michael@0 | 3869 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3870 | case CKR_FUNCTION_FAILED: |
michael@0 | 3871 | case CKR_GENERAL_ERROR: |
michael@0 | 3872 | case CKR_HOST_MEMORY: |
michael@0 | 3873 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3874 | case CKR_SESSION_CLOSED: |
michael@0 | 3875 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3876 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3877 | break; |
michael@0 | 3878 | default: |
michael@0 | 3879 | case CKR_OK: |
michael@0 | 3880 | error = CKR_GENERAL_ERROR; |
michael@0 | 3881 | break; |
michael@0 | 3882 | } |
michael@0 | 3883 | return error; |
michael@0 | 3884 | } |
michael@0 | 3885 | |
michael@0 | 3886 | /* |
michael@0 | 3887 | * NSSCKFWC_SignFinal |
michael@0 | 3888 | * |
michael@0 | 3889 | */ |
michael@0 | 3890 | NSS_IMPLEMENT CK_RV |
michael@0 | 3891 | NSSCKFWC_SignFinal |
michael@0 | 3892 | ( |
michael@0 | 3893 | NSSCKFWInstance *fwInstance, |
michael@0 | 3894 | CK_SESSION_HANDLE hSession, |
michael@0 | 3895 | CK_BYTE_PTR pSignature, |
michael@0 | 3896 | CK_ULONG_PTR pulSignatureLen |
michael@0 | 3897 | ) |
michael@0 | 3898 | { |
michael@0 | 3899 | CK_RV error = CKR_OK; |
michael@0 | 3900 | NSSCKFWSession *fwSession; |
michael@0 | 3901 | |
michael@0 | 3902 | if (!fwInstance) { |
michael@0 | 3903 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3904 | goto loser; |
michael@0 | 3905 | } |
michael@0 | 3906 | |
michael@0 | 3907 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3908 | if (!fwSession) { |
michael@0 | 3909 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3910 | goto loser; |
michael@0 | 3911 | } |
michael@0 | 3912 | |
michael@0 | 3913 | error = nssCKFWSession_Final(fwSession, |
michael@0 | 3914 | NSSCKFWCryptoOperationType_Sign, |
michael@0 | 3915 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 3916 | pSignature, pulSignatureLen); |
michael@0 | 3917 | |
michael@0 | 3918 | if (CKR_OK == error) { |
michael@0 | 3919 | return CKR_OK; |
michael@0 | 3920 | } |
michael@0 | 3921 | |
michael@0 | 3922 | loser: |
michael@0 | 3923 | /* verify error */ |
michael@0 | 3924 | switch( error ) { |
michael@0 | 3925 | case CKR_ARGUMENTS_BAD: |
michael@0 | 3926 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 3927 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 3928 | case CKR_DATA_LEN_RANGE: |
michael@0 | 3929 | case CKR_DEVICE_ERROR: |
michael@0 | 3930 | case CKR_DEVICE_MEMORY: |
michael@0 | 3931 | case CKR_DEVICE_REMOVED: |
michael@0 | 3932 | case CKR_FUNCTION_CANCELED: |
michael@0 | 3933 | case CKR_FUNCTION_FAILED: |
michael@0 | 3934 | case CKR_GENERAL_ERROR: |
michael@0 | 3935 | case CKR_HOST_MEMORY: |
michael@0 | 3936 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 3937 | case CKR_SESSION_CLOSED: |
michael@0 | 3938 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 3939 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 3940 | case CKR_FUNCTION_REJECTED: |
michael@0 | 3941 | break; |
michael@0 | 3942 | default: |
michael@0 | 3943 | case CKR_OK: |
michael@0 | 3944 | error = CKR_GENERAL_ERROR; |
michael@0 | 3945 | break; |
michael@0 | 3946 | } |
michael@0 | 3947 | return error; |
michael@0 | 3948 | } |
michael@0 | 3949 | |
michael@0 | 3950 | /* |
michael@0 | 3951 | * NSSCKFWC_SignRecoverInit |
michael@0 | 3952 | * |
michael@0 | 3953 | */ |
michael@0 | 3954 | NSS_IMPLEMENT CK_RV |
michael@0 | 3955 | NSSCKFWC_SignRecoverInit |
michael@0 | 3956 | ( |
michael@0 | 3957 | NSSCKFWInstance *fwInstance, |
michael@0 | 3958 | CK_SESSION_HANDLE hSession, |
michael@0 | 3959 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 3960 | CK_OBJECT_HANDLE hKey |
michael@0 | 3961 | ) |
michael@0 | 3962 | { |
michael@0 | 3963 | CK_RV error = CKR_OK; |
michael@0 | 3964 | NSSCKFWSession *fwSession; |
michael@0 | 3965 | NSSCKFWObject *fwObject; |
michael@0 | 3966 | NSSCKFWSlot *fwSlot; |
michael@0 | 3967 | NSSCKFWToken *fwToken; |
michael@0 | 3968 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 3969 | |
michael@0 | 3970 | if (!fwInstance) { |
michael@0 | 3971 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 3972 | goto loser; |
michael@0 | 3973 | } |
michael@0 | 3974 | |
michael@0 | 3975 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 3976 | if (!fwSession) { |
michael@0 | 3977 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 3978 | goto loser; |
michael@0 | 3979 | } |
michael@0 | 3980 | |
michael@0 | 3981 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 3982 | if (!fwObject) { |
michael@0 | 3983 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 3984 | goto loser; |
michael@0 | 3985 | } |
michael@0 | 3986 | |
michael@0 | 3987 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 3988 | if (!fwSlot) { |
michael@0 | 3989 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 3990 | goto loser; |
michael@0 | 3991 | } |
michael@0 | 3992 | |
michael@0 | 3993 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 3994 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 3995 | goto loser; |
michael@0 | 3996 | } |
michael@0 | 3997 | |
michael@0 | 3998 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 3999 | if (!fwToken) { |
michael@0 | 4000 | goto loser; |
michael@0 | 4001 | } |
michael@0 | 4002 | |
michael@0 | 4003 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 4004 | if (!fwMechanism) { |
michael@0 | 4005 | goto loser; |
michael@0 | 4006 | } |
michael@0 | 4007 | |
michael@0 | 4008 | error = nssCKFWMechanism_SignRecoverInit(fwMechanism, pMechanism, fwSession, |
michael@0 | 4009 | fwObject); |
michael@0 | 4010 | |
michael@0 | 4011 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 4012 | |
michael@0 | 4013 | if (CKR_OK == error) { |
michael@0 | 4014 | return CKR_OK; |
michael@0 | 4015 | } |
michael@0 | 4016 | |
michael@0 | 4017 | loser: |
michael@0 | 4018 | /* verify error */ |
michael@0 | 4019 | switch( error ) { |
michael@0 | 4020 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4021 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4022 | case CKR_DEVICE_ERROR: |
michael@0 | 4023 | case CKR_DEVICE_MEMORY: |
michael@0 | 4024 | case CKR_DEVICE_REMOVED: |
michael@0 | 4025 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4026 | case CKR_FUNCTION_FAILED: |
michael@0 | 4027 | case CKR_GENERAL_ERROR: |
michael@0 | 4028 | case CKR_HOST_MEMORY: |
michael@0 | 4029 | case CKR_KEY_FUNCTION_NOT_PERMITTED: |
michael@0 | 4030 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 4031 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 4032 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 4033 | case CKR_MECHANISM_INVALID: |
michael@0 | 4034 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 4035 | case CKR_OPERATION_ACTIVE: |
michael@0 | 4036 | case CKR_PIN_EXPIRED: |
michael@0 | 4037 | case CKR_SESSION_CLOSED: |
michael@0 | 4038 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4039 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 4040 | break; |
michael@0 | 4041 | default: |
michael@0 | 4042 | case CKR_OK: |
michael@0 | 4043 | error = CKR_GENERAL_ERROR; |
michael@0 | 4044 | break; |
michael@0 | 4045 | } |
michael@0 | 4046 | return error; |
michael@0 | 4047 | } |
michael@0 | 4048 | |
michael@0 | 4049 | /* |
michael@0 | 4050 | * NSSCKFWC_SignRecover |
michael@0 | 4051 | * |
michael@0 | 4052 | */ |
michael@0 | 4053 | NSS_IMPLEMENT CK_RV |
michael@0 | 4054 | NSSCKFWC_SignRecover |
michael@0 | 4055 | ( |
michael@0 | 4056 | NSSCKFWInstance *fwInstance, |
michael@0 | 4057 | CK_SESSION_HANDLE hSession, |
michael@0 | 4058 | CK_BYTE_PTR pData, |
michael@0 | 4059 | CK_ULONG ulDataLen, |
michael@0 | 4060 | CK_BYTE_PTR pSignature, |
michael@0 | 4061 | CK_ULONG_PTR pulSignatureLen |
michael@0 | 4062 | ) |
michael@0 | 4063 | { |
michael@0 | 4064 | CK_RV error = CKR_OK; |
michael@0 | 4065 | NSSCKFWSession *fwSession; |
michael@0 | 4066 | |
michael@0 | 4067 | if (!fwInstance) { |
michael@0 | 4068 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4069 | goto loser; |
michael@0 | 4070 | } |
michael@0 | 4071 | |
michael@0 | 4072 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4073 | if (!fwSession) { |
michael@0 | 4074 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4075 | goto loser; |
michael@0 | 4076 | } |
michael@0 | 4077 | |
michael@0 | 4078 | error = nssCKFWSession_UpdateFinal(fwSession, |
michael@0 | 4079 | NSSCKFWCryptoOperationType_SignRecover, |
michael@0 | 4080 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 4081 | pData, ulDataLen, pSignature, pulSignatureLen); |
michael@0 | 4082 | |
michael@0 | 4083 | if (CKR_OK == error) { |
michael@0 | 4084 | return CKR_OK; |
michael@0 | 4085 | } |
michael@0 | 4086 | |
michael@0 | 4087 | loser: |
michael@0 | 4088 | /* verify error */ |
michael@0 | 4089 | switch( error ) { |
michael@0 | 4090 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4091 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 4092 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4093 | case CKR_DATA_INVALID: |
michael@0 | 4094 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4095 | case CKR_DEVICE_ERROR: |
michael@0 | 4096 | case CKR_DEVICE_MEMORY: |
michael@0 | 4097 | case CKR_DEVICE_REMOVED: |
michael@0 | 4098 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4099 | case CKR_FUNCTION_FAILED: |
michael@0 | 4100 | case CKR_GENERAL_ERROR: |
michael@0 | 4101 | case CKR_HOST_MEMORY: |
michael@0 | 4102 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4103 | case CKR_SESSION_CLOSED: |
michael@0 | 4104 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4105 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 4106 | break; |
michael@0 | 4107 | default: |
michael@0 | 4108 | case CKR_OK: |
michael@0 | 4109 | error = CKR_GENERAL_ERROR; |
michael@0 | 4110 | break; |
michael@0 | 4111 | } |
michael@0 | 4112 | return error; |
michael@0 | 4113 | } |
michael@0 | 4114 | |
michael@0 | 4115 | /* |
michael@0 | 4116 | * NSSCKFWC_VerifyInit |
michael@0 | 4117 | * |
michael@0 | 4118 | */ |
michael@0 | 4119 | NSS_IMPLEMENT CK_RV |
michael@0 | 4120 | NSSCKFWC_VerifyInit |
michael@0 | 4121 | ( |
michael@0 | 4122 | NSSCKFWInstance *fwInstance, |
michael@0 | 4123 | CK_SESSION_HANDLE hSession, |
michael@0 | 4124 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 4125 | CK_OBJECT_HANDLE hKey |
michael@0 | 4126 | ) |
michael@0 | 4127 | { |
michael@0 | 4128 | CK_RV error = CKR_OK; |
michael@0 | 4129 | NSSCKFWSession *fwSession; |
michael@0 | 4130 | NSSCKFWObject *fwObject; |
michael@0 | 4131 | NSSCKFWSlot *fwSlot; |
michael@0 | 4132 | NSSCKFWToken *fwToken; |
michael@0 | 4133 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 4134 | |
michael@0 | 4135 | if (!fwInstance) { |
michael@0 | 4136 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4137 | goto loser; |
michael@0 | 4138 | } |
michael@0 | 4139 | |
michael@0 | 4140 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4141 | if (!fwSession) { |
michael@0 | 4142 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4143 | goto loser; |
michael@0 | 4144 | } |
michael@0 | 4145 | |
michael@0 | 4146 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 4147 | if (!fwObject) { |
michael@0 | 4148 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 4149 | goto loser; |
michael@0 | 4150 | } |
michael@0 | 4151 | |
michael@0 | 4152 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 4153 | if (!fwSlot) { |
michael@0 | 4154 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 4155 | goto loser; |
michael@0 | 4156 | } |
michael@0 | 4157 | |
michael@0 | 4158 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 4159 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 4160 | goto loser; |
michael@0 | 4161 | } |
michael@0 | 4162 | |
michael@0 | 4163 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 4164 | if (!fwToken) { |
michael@0 | 4165 | goto loser; |
michael@0 | 4166 | } |
michael@0 | 4167 | |
michael@0 | 4168 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 4169 | if (!fwMechanism) { |
michael@0 | 4170 | goto loser; |
michael@0 | 4171 | } |
michael@0 | 4172 | |
michael@0 | 4173 | error = nssCKFWMechanism_VerifyInit(fwMechanism, pMechanism, fwSession, |
michael@0 | 4174 | fwObject); |
michael@0 | 4175 | |
michael@0 | 4176 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 4177 | |
michael@0 | 4178 | if (CKR_OK == error) { |
michael@0 | 4179 | return CKR_OK; |
michael@0 | 4180 | } |
michael@0 | 4181 | |
michael@0 | 4182 | loser: |
michael@0 | 4183 | /* verify error */ |
michael@0 | 4184 | switch( error ) { |
michael@0 | 4185 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4186 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4187 | case CKR_DEVICE_ERROR: |
michael@0 | 4188 | case CKR_DEVICE_MEMORY: |
michael@0 | 4189 | case CKR_DEVICE_REMOVED: |
michael@0 | 4190 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4191 | case CKR_FUNCTION_FAILED: |
michael@0 | 4192 | case CKR_GENERAL_ERROR: |
michael@0 | 4193 | case CKR_HOST_MEMORY: |
michael@0 | 4194 | case CKR_KEY_FUNCTION_NOT_PERMITTED: |
michael@0 | 4195 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 4196 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 4197 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 4198 | case CKR_MECHANISM_INVALID: |
michael@0 | 4199 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 4200 | case CKR_OPERATION_ACTIVE: |
michael@0 | 4201 | case CKR_PIN_EXPIRED: |
michael@0 | 4202 | case CKR_SESSION_CLOSED: |
michael@0 | 4203 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4204 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 4205 | break; |
michael@0 | 4206 | default: |
michael@0 | 4207 | case CKR_OK: |
michael@0 | 4208 | error = CKR_GENERAL_ERROR; |
michael@0 | 4209 | break; |
michael@0 | 4210 | } |
michael@0 | 4211 | return error; |
michael@0 | 4212 | } |
michael@0 | 4213 | |
michael@0 | 4214 | /* |
michael@0 | 4215 | * NSSCKFWC_Verify |
michael@0 | 4216 | * |
michael@0 | 4217 | */ |
michael@0 | 4218 | NSS_IMPLEMENT CK_RV |
michael@0 | 4219 | NSSCKFWC_Verify |
michael@0 | 4220 | ( |
michael@0 | 4221 | NSSCKFWInstance *fwInstance, |
michael@0 | 4222 | CK_SESSION_HANDLE hSession, |
michael@0 | 4223 | CK_BYTE_PTR pData, |
michael@0 | 4224 | CK_ULONG ulDataLen, |
michael@0 | 4225 | CK_BYTE_PTR pSignature, |
michael@0 | 4226 | CK_ULONG ulSignatureLen |
michael@0 | 4227 | ) |
michael@0 | 4228 | { |
michael@0 | 4229 | CK_RV error = CKR_OK; |
michael@0 | 4230 | NSSCKFWSession *fwSession; |
michael@0 | 4231 | |
michael@0 | 4232 | if (!fwInstance) { |
michael@0 | 4233 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4234 | goto loser; |
michael@0 | 4235 | } |
michael@0 | 4236 | |
michael@0 | 4237 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4238 | if (!fwSession) { |
michael@0 | 4239 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4240 | goto loser; |
michael@0 | 4241 | } |
michael@0 | 4242 | |
michael@0 | 4243 | error = nssCKFWSession_UpdateFinal(fwSession, |
michael@0 | 4244 | NSSCKFWCryptoOperationType_Verify, |
michael@0 | 4245 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 4246 | pData, ulDataLen, pSignature, &ulSignatureLen); |
michael@0 | 4247 | |
michael@0 | 4248 | if (CKR_OK == error) { |
michael@0 | 4249 | return CKR_OK; |
michael@0 | 4250 | } |
michael@0 | 4251 | |
michael@0 | 4252 | loser: |
michael@0 | 4253 | /* verify error */ |
michael@0 | 4254 | switch( error ) { |
michael@0 | 4255 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4256 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4257 | case CKR_DATA_INVALID: |
michael@0 | 4258 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4259 | case CKR_DEVICE_ERROR: |
michael@0 | 4260 | case CKR_DEVICE_MEMORY: |
michael@0 | 4261 | case CKR_DEVICE_REMOVED: |
michael@0 | 4262 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4263 | case CKR_FUNCTION_FAILED: |
michael@0 | 4264 | case CKR_GENERAL_ERROR: |
michael@0 | 4265 | case CKR_HOST_MEMORY: |
michael@0 | 4266 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4267 | case CKR_SESSION_CLOSED: |
michael@0 | 4268 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4269 | case CKR_SIGNATURE_INVALID: |
michael@0 | 4270 | case CKR_SIGNATURE_LEN_RANGE: |
michael@0 | 4271 | break; |
michael@0 | 4272 | default: |
michael@0 | 4273 | case CKR_OK: |
michael@0 | 4274 | error = CKR_GENERAL_ERROR; |
michael@0 | 4275 | break; |
michael@0 | 4276 | } |
michael@0 | 4277 | return error; |
michael@0 | 4278 | } |
michael@0 | 4279 | |
michael@0 | 4280 | /* |
michael@0 | 4281 | * NSSCKFWC_VerifyUpdate |
michael@0 | 4282 | * |
michael@0 | 4283 | */ |
michael@0 | 4284 | NSS_IMPLEMENT CK_RV |
michael@0 | 4285 | NSSCKFWC_VerifyUpdate |
michael@0 | 4286 | ( |
michael@0 | 4287 | NSSCKFWInstance *fwInstance, |
michael@0 | 4288 | CK_SESSION_HANDLE hSession, |
michael@0 | 4289 | CK_BYTE_PTR pPart, |
michael@0 | 4290 | CK_ULONG ulPartLen |
michael@0 | 4291 | ) |
michael@0 | 4292 | { |
michael@0 | 4293 | CK_RV error = CKR_OK; |
michael@0 | 4294 | NSSCKFWSession *fwSession; |
michael@0 | 4295 | |
michael@0 | 4296 | if (!fwInstance) { |
michael@0 | 4297 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4298 | goto loser; |
michael@0 | 4299 | } |
michael@0 | 4300 | |
michael@0 | 4301 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4302 | if (!fwSession) { |
michael@0 | 4303 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4304 | goto loser; |
michael@0 | 4305 | } |
michael@0 | 4306 | |
michael@0 | 4307 | error = nssCKFWSession_DigestUpdate(fwSession, |
michael@0 | 4308 | NSSCKFWCryptoOperationType_Verify, |
michael@0 | 4309 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 4310 | pPart, ulPartLen); |
michael@0 | 4311 | |
michael@0 | 4312 | if (CKR_OK == error) { |
michael@0 | 4313 | return CKR_OK; |
michael@0 | 4314 | } |
michael@0 | 4315 | |
michael@0 | 4316 | loser: |
michael@0 | 4317 | /* verify error */ |
michael@0 | 4318 | switch( error ) { |
michael@0 | 4319 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4320 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4321 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4322 | case CKR_DEVICE_ERROR: |
michael@0 | 4323 | case CKR_DEVICE_MEMORY: |
michael@0 | 4324 | case CKR_DEVICE_REMOVED: |
michael@0 | 4325 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4326 | case CKR_FUNCTION_FAILED: |
michael@0 | 4327 | case CKR_GENERAL_ERROR: |
michael@0 | 4328 | case CKR_HOST_MEMORY: |
michael@0 | 4329 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4330 | case CKR_SESSION_CLOSED: |
michael@0 | 4331 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4332 | break; |
michael@0 | 4333 | default: |
michael@0 | 4334 | case CKR_OK: |
michael@0 | 4335 | error = CKR_GENERAL_ERROR; |
michael@0 | 4336 | break; |
michael@0 | 4337 | } |
michael@0 | 4338 | return error; |
michael@0 | 4339 | } |
michael@0 | 4340 | |
michael@0 | 4341 | /* |
michael@0 | 4342 | * NSSCKFWC_VerifyFinal |
michael@0 | 4343 | * |
michael@0 | 4344 | */ |
michael@0 | 4345 | NSS_IMPLEMENT CK_RV |
michael@0 | 4346 | NSSCKFWC_VerifyFinal |
michael@0 | 4347 | ( |
michael@0 | 4348 | NSSCKFWInstance *fwInstance, |
michael@0 | 4349 | CK_SESSION_HANDLE hSession, |
michael@0 | 4350 | CK_BYTE_PTR pSignature, |
michael@0 | 4351 | CK_ULONG ulSignatureLen |
michael@0 | 4352 | ) |
michael@0 | 4353 | { |
michael@0 | 4354 | CK_RV error = CKR_OK; |
michael@0 | 4355 | NSSCKFWSession *fwSession; |
michael@0 | 4356 | |
michael@0 | 4357 | if (!fwInstance) { |
michael@0 | 4358 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4359 | goto loser; |
michael@0 | 4360 | } |
michael@0 | 4361 | |
michael@0 | 4362 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4363 | if (!fwSession) { |
michael@0 | 4364 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4365 | goto loser; |
michael@0 | 4366 | } |
michael@0 | 4367 | |
michael@0 | 4368 | error = nssCKFWSession_Final(fwSession, |
michael@0 | 4369 | NSSCKFWCryptoOperationType_Verify, |
michael@0 | 4370 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 4371 | pSignature, &ulSignatureLen); |
michael@0 | 4372 | |
michael@0 | 4373 | if (CKR_OK == error) { |
michael@0 | 4374 | return CKR_OK; |
michael@0 | 4375 | } |
michael@0 | 4376 | |
michael@0 | 4377 | loser: |
michael@0 | 4378 | /* verify error */ |
michael@0 | 4379 | switch( error ) { |
michael@0 | 4380 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4381 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4382 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4383 | case CKR_DEVICE_ERROR: |
michael@0 | 4384 | case CKR_DEVICE_MEMORY: |
michael@0 | 4385 | case CKR_DEVICE_REMOVED: |
michael@0 | 4386 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4387 | case CKR_FUNCTION_FAILED: |
michael@0 | 4388 | case CKR_GENERAL_ERROR: |
michael@0 | 4389 | case CKR_HOST_MEMORY: |
michael@0 | 4390 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4391 | case CKR_SESSION_CLOSED: |
michael@0 | 4392 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4393 | case CKR_SIGNATURE_INVALID: |
michael@0 | 4394 | case CKR_SIGNATURE_LEN_RANGE: |
michael@0 | 4395 | break; |
michael@0 | 4396 | default: |
michael@0 | 4397 | case CKR_OK: |
michael@0 | 4398 | error = CKR_GENERAL_ERROR; |
michael@0 | 4399 | break; |
michael@0 | 4400 | } |
michael@0 | 4401 | return error; |
michael@0 | 4402 | } |
michael@0 | 4403 | |
michael@0 | 4404 | /* |
michael@0 | 4405 | * NSSCKFWC_VerifyRecoverInit |
michael@0 | 4406 | * |
michael@0 | 4407 | */ |
michael@0 | 4408 | NSS_IMPLEMENT CK_RV |
michael@0 | 4409 | NSSCKFWC_VerifyRecoverInit |
michael@0 | 4410 | ( |
michael@0 | 4411 | NSSCKFWInstance *fwInstance, |
michael@0 | 4412 | CK_SESSION_HANDLE hSession, |
michael@0 | 4413 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 4414 | CK_OBJECT_HANDLE hKey |
michael@0 | 4415 | ) |
michael@0 | 4416 | { |
michael@0 | 4417 | CK_RV error = CKR_OK; |
michael@0 | 4418 | NSSCKFWSession *fwSession; |
michael@0 | 4419 | NSSCKFWObject *fwObject; |
michael@0 | 4420 | NSSCKFWSlot *fwSlot; |
michael@0 | 4421 | NSSCKFWToken *fwToken; |
michael@0 | 4422 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 4423 | |
michael@0 | 4424 | if (!fwInstance) { |
michael@0 | 4425 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4426 | goto loser; |
michael@0 | 4427 | } |
michael@0 | 4428 | |
michael@0 | 4429 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4430 | if (!fwSession) { |
michael@0 | 4431 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4432 | goto loser; |
michael@0 | 4433 | } |
michael@0 | 4434 | |
michael@0 | 4435 | fwObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 4436 | if (!fwObject) { |
michael@0 | 4437 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 4438 | goto loser; |
michael@0 | 4439 | } |
michael@0 | 4440 | |
michael@0 | 4441 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 4442 | if (!fwSlot) { |
michael@0 | 4443 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 4444 | goto loser; |
michael@0 | 4445 | } |
michael@0 | 4446 | |
michael@0 | 4447 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 4448 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 4449 | goto loser; |
michael@0 | 4450 | } |
michael@0 | 4451 | |
michael@0 | 4452 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 4453 | if (!fwToken) { |
michael@0 | 4454 | goto loser; |
michael@0 | 4455 | } |
michael@0 | 4456 | |
michael@0 | 4457 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 4458 | if (!fwMechanism) { |
michael@0 | 4459 | goto loser; |
michael@0 | 4460 | } |
michael@0 | 4461 | |
michael@0 | 4462 | error = nssCKFWMechanism_VerifyRecoverInit(fwMechanism, pMechanism, |
michael@0 | 4463 | fwSession, fwObject); |
michael@0 | 4464 | |
michael@0 | 4465 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 4466 | |
michael@0 | 4467 | if (CKR_OK == error) { |
michael@0 | 4468 | return CKR_OK; |
michael@0 | 4469 | } |
michael@0 | 4470 | |
michael@0 | 4471 | loser: |
michael@0 | 4472 | /* verify error */ |
michael@0 | 4473 | switch( error ) { |
michael@0 | 4474 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4475 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4476 | case CKR_DEVICE_ERROR: |
michael@0 | 4477 | case CKR_DEVICE_MEMORY: |
michael@0 | 4478 | case CKR_DEVICE_REMOVED: |
michael@0 | 4479 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4480 | case CKR_FUNCTION_FAILED: |
michael@0 | 4481 | case CKR_GENERAL_ERROR: |
michael@0 | 4482 | case CKR_HOST_MEMORY: |
michael@0 | 4483 | case CKR_KEY_FUNCTION_NOT_PERMITTED: |
michael@0 | 4484 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 4485 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 4486 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 4487 | case CKR_MECHANISM_INVALID: |
michael@0 | 4488 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 4489 | case CKR_OPERATION_ACTIVE: |
michael@0 | 4490 | case CKR_PIN_EXPIRED: |
michael@0 | 4491 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4492 | case CKR_SESSION_CLOSED: |
michael@0 | 4493 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 4494 | break; |
michael@0 | 4495 | default: |
michael@0 | 4496 | case CKR_OK: |
michael@0 | 4497 | error = CKR_GENERAL_ERROR; |
michael@0 | 4498 | break; |
michael@0 | 4499 | } |
michael@0 | 4500 | return error; |
michael@0 | 4501 | } |
michael@0 | 4502 | |
michael@0 | 4503 | /* |
michael@0 | 4504 | * NSSCKFWC_VerifyRecover |
michael@0 | 4505 | * |
michael@0 | 4506 | */ |
michael@0 | 4507 | NSS_IMPLEMENT CK_RV |
michael@0 | 4508 | NSSCKFWC_VerifyRecover |
michael@0 | 4509 | ( |
michael@0 | 4510 | NSSCKFWInstance *fwInstance, |
michael@0 | 4511 | CK_SESSION_HANDLE hSession, |
michael@0 | 4512 | CK_BYTE_PTR pSignature, |
michael@0 | 4513 | CK_ULONG ulSignatureLen, |
michael@0 | 4514 | CK_BYTE_PTR pData, |
michael@0 | 4515 | CK_ULONG_PTR pulDataLen |
michael@0 | 4516 | ) |
michael@0 | 4517 | { |
michael@0 | 4518 | CK_RV error = CKR_OK; |
michael@0 | 4519 | NSSCKFWSession *fwSession; |
michael@0 | 4520 | |
michael@0 | 4521 | if (!fwInstance) { |
michael@0 | 4522 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4523 | goto loser; |
michael@0 | 4524 | } |
michael@0 | 4525 | |
michael@0 | 4526 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4527 | if (!fwSession) { |
michael@0 | 4528 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4529 | goto loser; |
michael@0 | 4530 | } |
michael@0 | 4531 | |
michael@0 | 4532 | error = nssCKFWSession_UpdateFinal(fwSession, |
michael@0 | 4533 | NSSCKFWCryptoOperationType_VerifyRecover, |
michael@0 | 4534 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 4535 | pSignature, ulSignatureLen, pData, pulDataLen); |
michael@0 | 4536 | if (CKR_OK == error) { |
michael@0 | 4537 | return CKR_OK; |
michael@0 | 4538 | } |
michael@0 | 4539 | loser: |
michael@0 | 4540 | /* verify error */ |
michael@0 | 4541 | switch( error ) { |
michael@0 | 4542 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4543 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 4544 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4545 | case CKR_DATA_INVALID: |
michael@0 | 4546 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4547 | case CKR_DEVICE_ERROR: |
michael@0 | 4548 | case CKR_DEVICE_MEMORY: |
michael@0 | 4549 | case CKR_DEVICE_REMOVED: |
michael@0 | 4550 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4551 | case CKR_FUNCTION_FAILED: |
michael@0 | 4552 | case CKR_GENERAL_ERROR: |
michael@0 | 4553 | case CKR_HOST_MEMORY: |
michael@0 | 4554 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4555 | case CKR_SESSION_CLOSED: |
michael@0 | 4556 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4557 | case CKR_SIGNATURE_INVALID: |
michael@0 | 4558 | case CKR_SIGNATURE_LEN_RANGE: |
michael@0 | 4559 | break; |
michael@0 | 4560 | default: |
michael@0 | 4561 | case CKR_OK: |
michael@0 | 4562 | error = CKR_GENERAL_ERROR; |
michael@0 | 4563 | break; |
michael@0 | 4564 | } |
michael@0 | 4565 | return error; |
michael@0 | 4566 | } |
michael@0 | 4567 | |
michael@0 | 4568 | /* |
michael@0 | 4569 | * NSSCKFWC_DigestEncryptUpdate |
michael@0 | 4570 | * |
michael@0 | 4571 | */ |
michael@0 | 4572 | NSS_IMPLEMENT CK_RV |
michael@0 | 4573 | NSSCKFWC_DigestEncryptUpdate |
michael@0 | 4574 | ( |
michael@0 | 4575 | NSSCKFWInstance *fwInstance, |
michael@0 | 4576 | CK_SESSION_HANDLE hSession, |
michael@0 | 4577 | CK_BYTE_PTR pPart, |
michael@0 | 4578 | CK_ULONG ulPartLen, |
michael@0 | 4579 | CK_BYTE_PTR pEncryptedPart, |
michael@0 | 4580 | CK_ULONG_PTR pulEncryptedPartLen |
michael@0 | 4581 | ) |
michael@0 | 4582 | { |
michael@0 | 4583 | CK_RV error = CKR_OK; |
michael@0 | 4584 | NSSCKFWSession *fwSession; |
michael@0 | 4585 | |
michael@0 | 4586 | if (!fwInstance) { |
michael@0 | 4587 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4588 | goto loser; |
michael@0 | 4589 | } |
michael@0 | 4590 | |
michael@0 | 4591 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4592 | if (!fwSession) { |
michael@0 | 4593 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4594 | goto loser; |
michael@0 | 4595 | } |
michael@0 | 4596 | |
michael@0 | 4597 | error = nssCKFWSession_UpdateCombo(fwSession, |
michael@0 | 4598 | NSSCKFWCryptoOperationType_Encrypt, |
michael@0 | 4599 | NSSCKFWCryptoOperationType_Digest, |
michael@0 | 4600 | NSSCKFWCryptoOperationState_Digest, |
michael@0 | 4601 | pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen); |
michael@0 | 4602 | |
michael@0 | 4603 | if (CKR_OK == error) { |
michael@0 | 4604 | return CKR_OK; |
michael@0 | 4605 | } |
michael@0 | 4606 | |
michael@0 | 4607 | loser: |
michael@0 | 4608 | /* verify error */ |
michael@0 | 4609 | switch( error ) { |
michael@0 | 4610 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4611 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 4612 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4613 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4614 | case CKR_DEVICE_ERROR: |
michael@0 | 4615 | case CKR_DEVICE_MEMORY: |
michael@0 | 4616 | case CKR_DEVICE_REMOVED: |
michael@0 | 4617 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4618 | case CKR_FUNCTION_FAILED: |
michael@0 | 4619 | case CKR_GENERAL_ERROR: |
michael@0 | 4620 | case CKR_HOST_MEMORY: |
michael@0 | 4621 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4622 | case CKR_SESSION_CLOSED: |
michael@0 | 4623 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4624 | break; |
michael@0 | 4625 | default: |
michael@0 | 4626 | case CKR_OK: |
michael@0 | 4627 | error = CKR_GENERAL_ERROR; |
michael@0 | 4628 | break; |
michael@0 | 4629 | } |
michael@0 | 4630 | return error; |
michael@0 | 4631 | } |
michael@0 | 4632 | |
michael@0 | 4633 | /* |
michael@0 | 4634 | * NSSCKFWC_DecryptDigestUpdate |
michael@0 | 4635 | * |
michael@0 | 4636 | */ |
michael@0 | 4637 | NSS_IMPLEMENT CK_RV |
michael@0 | 4638 | NSSCKFWC_DecryptDigestUpdate |
michael@0 | 4639 | ( |
michael@0 | 4640 | NSSCKFWInstance *fwInstance, |
michael@0 | 4641 | CK_SESSION_HANDLE hSession, |
michael@0 | 4642 | CK_BYTE_PTR pEncryptedPart, |
michael@0 | 4643 | CK_ULONG ulEncryptedPartLen, |
michael@0 | 4644 | CK_BYTE_PTR pPart, |
michael@0 | 4645 | CK_ULONG_PTR pulPartLen |
michael@0 | 4646 | ) |
michael@0 | 4647 | { |
michael@0 | 4648 | CK_RV error = CKR_OK; |
michael@0 | 4649 | NSSCKFWSession *fwSession; |
michael@0 | 4650 | |
michael@0 | 4651 | if (!fwInstance) { |
michael@0 | 4652 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4653 | goto loser; |
michael@0 | 4654 | } |
michael@0 | 4655 | |
michael@0 | 4656 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4657 | if (!fwSession) { |
michael@0 | 4658 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4659 | goto loser; |
michael@0 | 4660 | } |
michael@0 | 4661 | |
michael@0 | 4662 | error = nssCKFWSession_UpdateCombo(fwSession, |
michael@0 | 4663 | NSSCKFWCryptoOperationType_Decrypt, |
michael@0 | 4664 | NSSCKFWCryptoOperationType_Digest, |
michael@0 | 4665 | NSSCKFWCryptoOperationState_Digest, |
michael@0 | 4666 | pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen); |
michael@0 | 4667 | |
michael@0 | 4668 | if (CKR_OK == error) { |
michael@0 | 4669 | return CKR_OK; |
michael@0 | 4670 | } |
michael@0 | 4671 | |
michael@0 | 4672 | loser: |
michael@0 | 4673 | /* verify error */ |
michael@0 | 4674 | switch( error ) { |
michael@0 | 4675 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4676 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 4677 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4678 | case CKR_DEVICE_ERROR: |
michael@0 | 4679 | case CKR_DEVICE_MEMORY: |
michael@0 | 4680 | case CKR_DEVICE_REMOVED: |
michael@0 | 4681 | case CKR_ENCRYPTED_DATA_INVALID: |
michael@0 | 4682 | case CKR_ENCRYPTED_DATA_LEN_RANGE: |
michael@0 | 4683 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4684 | case CKR_FUNCTION_FAILED: |
michael@0 | 4685 | case CKR_GENERAL_ERROR: |
michael@0 | 4686 | case CKR_HOST_MEMORY: |
michael@0 | 4687 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4688 | case CKR_SESSION_CLOSED: |
michael@0 | 4689 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4690 | break; |
michael@0 | 4691 | case CKR_DATA_INVALID: |
michael@0 | 4692 | error = CKR_ENCRYPTED_DATA_INVALID; |
michael@0 | 4693 | break; |
michael@0 | 4694 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4695 | error = CKR_ENCRYPTED_DATA_LEN_RANGE; |
michael@0 | 4696 | break; |
michael@0 | 4697 | default: |
michael@0 | 4698 | case CKR_OK: |
michael@0 | 4699 | error = CKR_GENERAL_ERROR; |
michael@0 | 4700 | break; |
michael@0 | 4701 | } |
michael@0 | 4702 | return error; |
michael@0 | 4703 | } |
michael@0 | 4704 | |
michael@0 | 4705 | /* |
michael@0 | 4706 | * NSSCKFWC_SignEncryptUpdate |
michael@0 | 4707 | * |
michael@0 | 4708 | */ |
michael@0 | 4709 | NSS_IMPLEMENT CK_RV |
michael@0 | 4710 | NSSCKFWC_SignEncryptUpdate |
michael@0 | 4711 | ( |
michael@0 | 4712 | NSSCKFWInstance *fwInstance, |
michael@0 | 4713 | CK_SESSION_HANDLE hSession, |
michael@0 | 4714 | CK_BYTE_PTR pPart, |
michael@0 | 4715 | CK_ULONG ulPartLen, |
michael@0 | 4716 | CK_BYTE_PTR pEncryptedPart, |
michael@0 | 4717 | CK_ULONG_PTR pulEncryptedPartLen |
michael@0 | 4718 | ) |
michael@0 | 4719 | { |
michael@0 | 4720 | CK_RV error = CKR_OK; |
michael@0 | 4721 | NSSCKFWSession *fwSession; |
michael@0 | 4722 | |
michael@0 | 4723 | if (!fwInstance) { |
michael@0 | 4724 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4725 | goto loser; |
michael@0 | 4726 | } |
michael@0 | 4727 | |
michael@0 | 4728 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4729 | if (!fwSession) { |
michael@0 | 4730 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4731 | goto loser; |
michael@0 | 4732 | } |
michael@0 | 4733 | |
michael@0 | 4734 | error = nssCKFWSession_UpdateCombo(fwSession, |
michael@0 | 4735 | NSSCKFWCryptoOperationType_Encrypt, |
michael@0 | 4736 | NSSCKFWCryptoOperationType_Sign, |
michael@0 | 4737 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 4738 | pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen); |
michael@0 | 4739 | |
michael@0 | 4740 | if (CKR_OK == error) { |
michael@0 | 4741 | return CKR_OK; |
michael@0 | 4742 | } |
michael@0 | 4743 | |
michael@0 | 4744 | loser: |
michael@0 | 4745 | /* verify error */ |
michael@0 | 4746 | switch( error ) { |
michael@0 | 4747 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4748 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 4749 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4750 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4751 | case CKR_DEVICE_ERROR: |
michael@0 | 4752 | case CKR_DEVICE_MEMORY: |
michael@0 | 4753 | case CKR_DEVICE_REMOVED: |
michael@0 | 4754 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4755 | case CKR_FUNCTION_FAILED: |
michael@0 | 4756 | case CKR_GENERAL_ERROR: |
michael@0 | 4757 | case CKR_HOST_MEMORY: |
michael@0 | 4758 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4759 | case CKR_SESSION_CLOSED: |
michael@0 | 4760 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4761 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 4762 | break; |
michael@0 | 4763 | default: |
michael@0 | 4764 | case CKR_OK: |
michael@0 | 4765 | error = CKR_GENERAL_ERROR; |
michael@0 | 4766 | break; |
michael@0 | 4767 | } |
michael@0 | 4768 | return error; |
michael@0 | 4769 | } |
michael@0 | 4770 | |
michael@0 | 4771 | /* |
michael@0 | 4772 | * NSSCKFWC_DecryptVerifyUpdate |
michael@0 | 4773 | * |
michael@0 | 4774 | */ |
michael@0 | 4775 | NSS_IMPLEMENT CK_RV |
michael@0 | 4776 | NSSCKFWC_DecryptVerifyUpdate |
michael@0 | 4777 | ( |
michael@0 | 4778 | NSSCKFWInstance *fwInstance, |
michael@0 | 4779 | CK_SESSION_HANDLE hSession, |
michael@0 | 4780 | CK_BYTE_PTR pEncryptedPart, |
michael@0 | 4781 | CK_ULONG ulEncryptedPartLen, |
michael@0 | 4782 | CK_BYTE_PTR pPart, |
michael@0 | 4783 | CK_ULONG_PTR pulPartLen |
michael@0 | 4784 | ) |
michael@0 | 4785 | { |
michael@0 | 4786 | CK_RV error = CKR_OK; |
michael@0 | 4787 | NSSCKFWSession *fwSession; |
michael@0 | 4788 | |
michael@0 | 4789 | if (!fwInstance) { |
michael@0 | 4790 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4791 | goto loser; |
michael@0 | 4792 | } |
michael@0 | 4793 | |
michael@0 | 4794 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4795 | if (!fwSession) { |
michael@0 | 4796 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4797 | goto loser; |
michael@0 | 4798 | } |
michael@0 | 4799 | |
michael@0 | 4800 | error = nssCKFWSession_UpdateCombo(fwSession, |
michael@0 | 4801 | NSSCKFWCryptoOperationType_Decrypt, |
michael@0 | 4802 | NSSCKFWCryptoOperationType_Verify, |
michael@0 | 4803 | NSSCKFWCryptoOperationState_SignVerify, |
michael@0 | 4804 | pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen); |
michael@0 | 4805 | |
michael@0 | 4806 | if (CKR_OK == error) { |
michael@0 | 4807 | return CKR_OK; |
michael@0 | 4808 | } |
michael@0 | 4809 | |
michael@0 | 4810 | loser: |
michael@0 | 4811 | /* verify error */ |
michael@0 | 4812 | switch( error ) { |
michael@0 | 4813 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4814 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 4815 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4816 | case CKR_DATA_LEN_RANGE: |
michael@0 | 4817 | case CKR_DEVICE_ERROR: |
michael@0 | 4818 | case CKR_DEVICE_MEMORY: |
michael@0 | 4819 | case CKR_DEVICE_REMOVED: |
michael@0 | 4820 | case CKR_ENCRYPTED_DATA_INVALID: |
michael@0 | 4821 | case CKR_ENCRYPTED_DATA_LEN_RANGE: |
michael@0 | 4822 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4823 | case CKR_FUNCTION_FAILED: |
michael@0 | 4824 | case CKR_GENERAL_ERROR: |
michael@0 | 4825 | case CKR_HOST_MEMORY: |
michael@0 | 4826 | case CKR_OPERATION_NOT_INITIALIZED: |
michael@0 | 4827 | case CKR_SESSION_CLOSED: |
michael@0 | 4828 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4829 | break; |
michael@0 | 4830 | case CKR_DATA_INVALID: |
michael@0 | 4831 | error = CKR_ENCRYPTED_DATA_INVALID; |
michael@0 | 4832 | break; |
michael@0 | 4833 | default: |
michael@0 | 4834 | case CKR_OK: |
michael@0 | 4835 | error = CKR_GENERAL_ERROR; |
michael@0 | 4836 | break; |
michael@0 | 4837 | } |
michael@0 | 4838 | return error; |
michael@0 | 4839 | } |
michael@0 | 4840 | |
michael@0 | 4841 | /* |
michael@0 | 4842 | * NSSCKFWC_GenerateKey |
michael@0 | 4843 | * |
michael@0 | 4844 | */ |
michael@0 | 4845 | NSS_IMPLEMENT CK_RV |
michael@0 | 4846 | NSSCKFWC_GenerateKey |
michael@0 | 4847 | ( |
michael@0 | 4848 | NSSCKFWInstance *fwInstance, |
michael@0 | 4849 | CK_SESSION_HANDLE hSession, |
michael@0 | 4850 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 4851 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 4852 | CK_ULONG ulCount, |
michael@0 | 4853 | CK_OBJECT_HANDLE_PTR phKey |
michael@0 | 4854 | ) |
michael@0 | 4855 | { |
michael@0 | 4856 | CK_RV error = CKR_OK; |
michael@0 | 4857 | NSSCKFWSession *fwSession; |
michael@0 | 4858 | NSSCKFWObject *fwObject; |
michael@0 | 4859 | NSSCKFWSlot *fwSlot; |
michael@0 | 4860 | NSSCKFWToken *fwToken; |
michael@0 | 4861 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 4862 | |
michael@0 | 4863 | if (!fwInstance) { |
michael@0 | 4864 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4865 | goto loser; |
michael@0 | 4866 | } |
michael@0 | 4867 | |
michael@0 | 4868 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4869 | if (!fwSession) { |
michael@0 | 4870 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4871 | goto loser; |
michael@0 | 4872 | } |
michael@0 | 4873 | |
michael@0 | 4874 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 4875 | if (!fwSlot) { |
michael@0 | 4876 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 4877 | goto loser; |
michael@0 | 4878 | } |
michael@0 | 4879 | |
michael@0 | 4880 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 4881 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 4882 | goto loser; |
michael@0 | 4883 | } |
michael@0 | 4884 | |
michael@0 | 4885 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 4886 | if (!fwToken) { |
michael@0 | 4887 | goto loser; |
michael@0 | 4888 | } |
michael@0 | 4889 | |
michael@0 | 4890 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 4891 | if (!fwMechanism) { |
michael@0 | 4892 | goto loser; |
michael@0 | 4893 | } |
michael@0 | 4894 | |
michael@0 | 4895 | fwObject = nssCKFWMechanism_GenerateKey( |
michael@0 | 4896 | fwMechanism, |
michael@0 | 4897 | pMechanism, |
michael@0 | 4898 | fwSession, |
michael@0 | 4899 | pTemplate, |
michael@0 | 4900 | ulCount, |
michael@0 | 4901 | &error); |
michael@0 | 4902 | |
michael@0 | 4903 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 4904 | if (!fwObject) { |
michael@0 | 4905 | goto loser; |
michael@0 | 4906 | } |
michael@0 | 4907 | *phKey= nssCKFWInstance_CreateObjectHandle(fwInstance, fwObject, &error); |
michael@0 | 4908 | |
michael@0 | 4909 | if (CKR_OK == error) { |
michael@0 | 4910 | return CKR_OK; |
michael@0 | 4911 | } |
michael@0 | 4912 | |
michael@0 | 4913 | loser: |
michael@0 | 4914 | /* verify error */ |
michael@0 | 4915 | switch( error ) { |
michael@0 | 4916 | case CKR_ARGUMENTS_BAD: |
michael@0 | 4917 | case CKR_ATTRIBUTE_READ_ONLY: |
michael@0 | 4918 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 4919 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 4920 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 4921 | case CKR_DEVICE_ERROR: |
michael@0 | 4922 | case CKR_DEVICE_MEMORY: |
michael@0 | 4923 | case CKR_DEVICE_REMOVED: |
michael@0 | 4924 | case CKR_FUNCTION_CANCELED: |
michael@0 | 4925 | case CKR_FUNCTION_FAILED: |
michael@0 | 4926 | case CKR_GENERAL_ERROR: |
michael@0 | 4927 | case CKR_HOST_MEMORY: |
michael@0 | 4928 | case CKR_MECHANISM_INVALID: |
michael@0 | 4929 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 4930 | case CKR_OPERATION_ACTIVE: |
michael@0 | 4931 | case CKR_PIN_EXPIRED: |
michael@0 | 4932 | case CKR_SESSION_CLOSED: |
michael@0 | 4933 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 4934 | case CKR_SESSION_READ_ONLY: |
michael@0 | 4935 | case CKR_TEMPLATE_INCOMPLETE: |
michael@0 | 4936 | case CKR_TEMPLATE_INCONSISTENT: |
michael@0 | 4937 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 4938 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 4939 | break; |
michael@0 | 4940 | default: |
michael@0 | 4941 | case CKR_OK: |
michael@0 | 4942 | error = CKR_GENERAL_ERROR; |
michael@0 | 4943 | break; |
michael@0 | 4944 | } |
michael@0 | 4945 | return error; |
michael@0 | 4946 | } |
michael@0 | 4947 | |
michael@0 | 4948 | /* |
michael@0 | 4949 | * NSSCKFWC_GenerateKeyPair |
michael@0 | 4950 | * |
michael@0 | 4951 | */ |
michael@0 | 4952 | NSS_IMPLEMENT CK_RV |
michael@0 | 4953 | NSSCKFWC_GenerateKeyPair |
michael@0 | 4954 | ( |
michael@0 | 4955 | NSSCKFWInstance *fwInstance, |
michael@0 | 4956 | CK_SESSION_HANDLE hSession, |
michael@0 | 4957 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 4958 | CK_ATTRIBUTE_PTR pPublicKeyTemplate, |
michael@0 | 4959 | CK_ULONG ulPublicKeyAttributeCount, |
michael@0 | 4960 | CK_ATTRIBUTE_PTR pPrivateKeyTemplate, |
michael@0 | 4961 | CK_ULONG ulPrivateKeyAttributeCount, |
michael@0 | 4962 | CK_OBJECT_HANDLE_PTR phPublicKey, |
michael@0 | 4963 | CK_OBJECT_HANDLE_PTR phPrivateKey |
michael@0 | 4964 | ) |
michael@0 | 4965 | { |
michael@0 | 4966 | CK_RV error = CKR_OK; |
michael@0 | 4967 | NSSCKFWSession *fwSession; |
michael@0 | 4968 | NSSCKFWObject *fwPrivateKeyObject; |
michael@0 | 4969 | NSSCKFWObject *fwPublicKeyObject; |
michael@0 | 4970 | NSSCKFWSlot *fwSlot; |
michael@0 | 4971 | NSSCKFWToken *fwToken; |
michael@0 | 4972 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 4973 | |
michael@0 | 4974 | if (!fwInstance) { |
michael@0 | 4975 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 4976 | goto loser; |
michael@0 | 4977 | } |
michael@0 | 4978 | |
michael@0 | 4979 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 4980 | if (!fwSession) { |
michael@0 | 4981 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 4982 | goto loser; |
michael@0 | 4983 | } |
michael@0 | 4984 | |
michael@0 | 4985 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 4986 | if (!fwSlot) { |
michael@0 | 4987 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 4988 | goto loser; |
michael@0 | 4989 | } |
michael@0 | 4990 | |
michael@0 | 4991 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 4992 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 4993 | goto loser; |
michael@0 | 4994 | } |
michael@0 | 4995 | |
michael@0 | 4996 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 4997 | if (!fwToken) { |
michael@0 | 4998 | goto loser; |
michael@0 | 4999 | } |
michael@0 | 5000 | |
michael@0 | 5001 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 5002 | if (!fwMechanism) { |
michael@0 | 5003 | goto loser; |
michael@0 | 5004 | } |
michael@0 | 5005 | |
michael@0 | 5006 | error= nssCKFWMechanism_GenerateKeyPair( |
michael@0 | 5007 | fwMechanism, |
michael@0 | 5008 | pMechanism, |
michael@0 | 5009 | fwSession, |
michael@0 | 5010 | pPublicKeyTemplate, |
michael@0 | 5011 | ulPublicKeyAttributeCount, |
michael@0 | 5012 | pPublicKeyTemplate, |
michael@0 | 5013 | ulPublicKeyAttributeCount, |
michael@0 | 5014 | &fwPublicKeyObject, |
michael@0 | 5015 | &fwPrivateKeyObject); |
michael@0 | 5016 | |
michael@0 | 5017 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 5018 | if (CKR_OK != error) { |
michael@0 | 5019 | goto loser; |
michael@0 | 5020 | } |
michael@0 | 5021 | *phPublicKey = nssCKFWInstance_CreateObjectHandle(fwInstance, |
michael@0 | 5022 | fwPublicKeyObject, |
michael@0 | 5023 | &error); |
michael@0 | 5024 | if (CKR_OK != error) { |
michael@0 | 5025 | goto loser; |
michael@0 | 5026 | } |
michael@0 | 5027 | *phPrivateKey = nssCKFWInstance_CreateObjectHandle(fwInstance, |
michael@0 | 5028 | fwPrivateKeyObject, |
michael@0 | 5029 | &error); |
michael@0 | 5030 | if (CKR_OK == error) { |
michael@0 | 5031 | return CKR_OK; |
michael@0 | 5032 | } |
michael@0 | 5033 | |
michael@0 | 5034 | loser: |
michael@0 | 5035 | /* verify error */ |
michael@0 | 5036 | switch( error ) { |
michael@0 | 5037 | case CKR_ARGUMENTS_BAD: |
michael@0 | 5038 | case CKR_ATTRIBUTE_READ_ONLY: |
michael@0 | 5039 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 5040 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 5041 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 5042 | case CKR_DEVICE_ERROR: |
michael@0 | 5043 | case CKR_DEVICE_MEMORY: |
michael@0 | 5044 | case CKR_DEVICE_REMOVED: |
michael@0 | 5045 | case CKR_DOMAIN_PARAMS_INVALID: |
michael@0 | 5046 | case CKR_FUNCTION_CANCELED: |
michael@0 | 5047 | case CKR_FUNCTION_FAILED: |
michael@0 | 5048 | case CKR_GENERAL_ERROR: |
michael@0 | 5049 | case CKR_HOST_MEMORY: |
michael@0 | 5050 | case CKR_MECHANISM_INVALID: |
michael@0 | 5051 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 5052 | case CKR_OPERATION_ACTIVE: |
michael@0 | 5053 | case CKR_PIN_EXPIRED: |
michael@0 | 5054 | case CKR_SESSION_CLOSED: |
michael@0 | 5055 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 5056 | case CKR_SESSION_READ_ONLY: |
michael@0 | 5057 | case CKR_TEMPLATE_INCOMPLETE: |
michael@0 | 5058 | case CKR_TEMPLATE_INCONSISTENT: |
michael@0 | 5059 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 5060 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 5061 | break; |
michael@0 | 5062 | default: |
michael@0 | 5063 | case CKR_OK: |
michael@0 | 5064 | error = CKR_GENERAL_ERROR; |
michael@0 | 5065 | break; |
michael@0 | 5066 | } |
michael@0 | 5067 | return error; |
michael@0 | 5068 | } |
michael@0 | 5069 | |
michael@0 | 5070 | /* |
michael@0 | 5071 | * NSSCKFWC_WrapKey |
michael@0 | 5072 | * |
michael@0 | 5073 | */ |
michael@0 | 5074 | NSS_IMPLEMENT CK_RV |
michael@0 | 5075 | NSSCKFWC_WrapKey |
michael@0 | 5076 | ( |
michael@0 | 5077 | NSSCKFWInstance *fwInstance, |
michael@0 | 5078 | CK_SESSION_HANDLE hSession, |
michael@0 | 5079 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 5080 | CK_OBJECT_HANDLE hWrappingKey, |
michael@0 | 5081 | CK_OBJECT_HANDLE hKey, |
michael@0 | 5082 | CK_BYTE_PTR pWrappedKey, |
michael@0 | 5083 | CK_ULONG_PTR pulWrappedKeyLen |
michael@0 | 5084 | ) |
michael@0 | 5085 | { |
michael@0 | 5086 | CK_RV error = CKR_OK; |
michael@0 | 5087 | NSSCKFWSession *fwSession; |
michael@0 | 5088 | NSSCKFWObject *fwKeyObject; |
michael@0 | 5089 | NSSCKFWObject *fwWrappingKeyObject; |
michael@0 | 5090 | NSSCKFWSlot *fwSlot; |
michael@0 | 5091 | NSSCKFWToken *fwToken; |
michael@0 | 5092 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 5093 | NSSItem wrappedKey; |
michael@0 | 5094 | CK_ULONG wrappedKeyLength = 0; |
michael@0 | 5095 | |
michael@0 | 5096 | if (!fwInstance) { |
michael@0 | 5097 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 5098 | goto loser; |
michael@0 | 5099 | } |
michael@0 | 5100 | |
michael@0 | 5101 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 5102 | if (!fwSession) { |
michael@0 | 5103 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 5104 | goto loser; |
michael@0 | 5105 | } |
michael@0 | 5106 | |
michael@0 | 5107 | fwWrappingKeyObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, |
michael@0 | 5108 | hWrappingKey); |
michael@0 | 5109 | if (!fwWrappingKeyObject) { |
michael@0 | 5110 | error = CKR_WRAPPING_KEY_HANDLE_INVALID; |
michael@0 | 5111 | goto loser; |
michael@0 | 5112 | } |
michael@0 | 5113 | |
michael@0 | 5114 | fwKeyObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hKey); |
michael@0 | 5115 | if (!fwKeyObject) { |
michael@0 | 5116 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 5117 | goto loser; |
michael@0 | 5118 | } |
michael@0 | 5119 | |
michael@0 | 5120 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 5121 | if (!fwSlot) { |
michael@0 | 5122 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 5123 | goto loser; |
michael@0 | 5124 | } |
michael@0 | 5125 | |
michael@0 | 5126 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 5127 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 5128 | goto loser; |
michael@0 | 5129 | } |
michael@0 | 5130 | |
michael@0 | 5131 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 5132 | if (!fwToken) { |
michael@0 | 5133 | goto loser; |
michael@0 | 5134 | } |
michael@0 | 5135 | |
michael@0 | 5136 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 5137 | if (!fwMechanism) { |
michael@0 | 5138 | goto loser; |
michael@0 | 5139 | } |
michael@0 | 5140 | |
michael@0 | 5141 | /* |
michael@0 | 5142 | * first get the length... |
michael@0 | 5143 | */ |
michael@0 | 5144 | wrappedKeyLength = nssCKFWMechanism_GetWrapKeyLength( |
michael@0 | 5145 | fwMechanism, |
michael@0 | 5146 | pMechanism, |
michael@0 | 5147 | fwSession, |
michael@0 | 5148 | fwWrappingKeyObject, |
michael@0 | 5149 | fwKeyObject, |
michael@0 | 5150 | &error); |
michael@0 | 5151 | if ((CK_ULONG) 0 == wrappedKeyLength) { |
michael@0 | 5152 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 5153 | goto loser; |
michael@0 | 5154 | } |
michael@0 | 5155 | if ((CK_BYTE_PTR)NULL == pWrappedKey) { |
michael@0 | 5156 | *pulWrappedKeyLen = wrappedKeyLength; |
michael@0 | 5157 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 5158 | return CKR_OK; |
michael@0 | 5159 | } |
michael@0 | 5160 | if (wrappedKeyLength > *pulWrappedKeyLen) { |
michael@0 | 5161 | *pulWrappedKeyLen = wrappedKeyLength; |
michael@0 | 5162 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 5163 | error = CKR_BUFFER_TOO_SMALL; |
michael@0 | 5164 | goto loser; |
michael@0 | 5165 | } |
michael@0 | 5166 | |
michael@0 | 5167 | |
michael@0 | 5168 | wrappedKey.data = pWrappedKey; |
michael@0 | 5169 | wrappedKey.size = wrappedKeyLength; |
michael@0 | 5170 | |
michael@0 | 5171 | error = nssCKFWMechanism_WrapKey( |
michael@0 | 5172 | fwMechanism, |
michael@0 | 5173 | pMechanism, |
michael@0 | 5174 | fwSession, |
michael@0 | 5175 | fwWrappingKeyObject, |
michael@0 | 5176 | fwKeyObject, |
michael@0 | 5177 | &wrappedKey); |
michael@0 | 5178 | |
michael@0 | 5179 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 5180 | *pulWrappedKeyLen = wrappedKey.size; |
michael@0 | 5181 | |
michael@0 | 5182 | if (CKR_OK == error) { |
michael@0 | 5183 | return CKR_OK; |
michael@0 | 5184 | } |
michael@0 | 5185 | |
michael@0 | 5186 | loser: |
michael@0 | 5187 | /* verify error */ |
michael@0 | 5188 | switch( error ) { |
michael@0 | 5189 | case CKR_ARGUMENTS_BAD: |
michael@0 | 5190 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 5191 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 5192 | case CKR_DEVICE_ERROR: |
michael@0 | 5193 | case CKR_DEVICE_MEMORY: |
michael@0 | 5194 | case CKR_DEVICE_REMOVED: |
michael@0 | 5195 | case CKR_FUNCTION_CANCELED: |
michael@0 | 5196 | case CKR_FUNCTION_FAILED: |
michael@0 | 5197 | case CKR_GENERAL_ERROR: |
michael@0 | 5198 | case CKR_HOST_MEMORY: |
michael@0 | 5199 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 5200 | case CKR_KEY_NOT_WRAPPABLE: |
michael@0 | 5201 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 5202 | case CKR_KEY_UNEXTRACTABLE: |
michael@0 | 5203 | case CKR_MECHANISM_INVALID: |
michael@0 | 5204 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 5205 | case CKR_OPERATION_ACTIVE: |
michael@0 | 5206 | case CKR_PIN_EXPIRED: |
michael@0 | 5207 | case CKR_SESSION_CLOSED: |
michael@0 | 5208 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 5209 | case CKR_WRAPPING_KEY_HANDLE_INVALID: |
michael@0 | 5210 | case CKR_WRAPPING_KEY_SIZE_RANGE: |
michael@0 | 5211 | case CKR_WRAPPING_KEY_TYPE_INCONSISTENT: |
michael@0 | 5212 | break; |
michael@0 | 5213 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 5214 | error = CKR_WRAPPING_KEY_TYPE_INCONSISTENT; |
michael@0 | 5215 | break; |
michael@0 | 5216 | default: |
michael@0 | 5217 | case CKR_OK: |
michael@0 | 5218 | error = CKR_GENERAL_ERROR; |
michael@0 | 5219 | break; |
michael@0 | 5220 | } |
michael@0 | 5221 | return error; |
michael@0 | 5222 | } |
michael@0 | 5223 | |
michael@0 | 5224 | /* |
michael@0 | 5225 | * NSSCKFWC_UnwrapKey |
michael@0 | 5226 | * |
michael@0 | 5227 | */ |
michael@0 | 5228 | NSS_IMPLEMENT CK_RV |
michael@0 | 5229 | NSSCKFWC_UnwrapKey |
michael@0 | 5230 | ( |
michael@0 | 5231 | NSSCKFWInstance *fwInstance, |
michael@0 | 5232 | CK_SESSION_HANDLE hSession, |
michael@0 | 5233 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 5234 | CK_OBJECT_HANDLE hUnwrappingKey, |
michael@0 | 5235 | CK_BYTE_PTR pWrappedKey, |
michael@0 | 5236 | CK_ULONG ulWrappedKeyLen, |
michael@0 | 5237 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 5238 | CK_ULONG ulAttributeCount, |
michael@0 | 5239 | CK_OBJECT_HANDLE_PTR phKey |
michael@0 | 5240 | ) |
michael@0 | 5241 | { |
michael@0 | 5242 | CK_RV error = CKR_OK; |
michael@0 | 5243 | NSSCKFWSession *fwSession; |
michael@0 | 5244 | NSSCKFWObject *fwObject; |
michael@0 | 5245 | NSSCKFWObject *fwWrappingKeyObject; |
michael@0 | 5246 | NSSCKFWSlot *fwSlot; |
michael@0 | 5247 | NSSCKFWToken *fwToken; |
michael@0 | 5248 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 5249 | NSSItem wrappedKey; |
michael@0 | 5250 | |
michael@0 | 5251 | if (!fwInstance) { |
michael@0 | 5252 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 5253 | goto loser; |
michael@0 | 5254 | } |
michael@0 | 5255 | |
michael@0 | 5256 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 5257 | if (!fwSession) { |
michael@0 | 5258 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 5259 | goto loser; |
michael@0 | 5260 | } |
michael@0 | 5261 | |
michael@0 | 5262 | fwWrappingKeyObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, |
michael@0 | 5263 | hUnwrappingKey); |
michael@0 | 5264 | if (!fwWrappingKeyObject) { |
michael@0 | 5265 | error = CKR_WRAPPING_KEY_HANDLE_INVALID; |
michael@0 | 5266 | goto loser; |
michael@0 | 5267 | } |
michael@0 | 5268 | |
michael@0 | 5269 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 5270 | if (!fwSlot) { |
michael@0 | 5271 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 5272 | goto loser; |
michael@0 | 5273 | } |
michael@0 | 5274 | |
michael@0 | 5275 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 5276 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 5277 | goto loser; |
michael@0 | 5278 | } |
michael@0 | 5279 | |
michael@0 | 5280 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 5281 | if (!fwToken) { |
michael@0 | 5282 | goto loser; |
michael@0 | 5283 | } |
michael@0 | 5284 | |
michael@0 | 5285 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 5286 | if (!fwMechanism) { |
michael@0 | 5287 | goto loser; |
michael@0 | 5288 | } |
michael@0 | 5289 | |
michael@0 | 5290 | wrappedKey.data = pWrappedKey; |
michael@0 | 5291 | wrappedKey.size = ulWrappedKeyLen; |
michael@0 | 5292 | |
michael@0 | 5293 | fwObject = nssCKFWMechanism_UnwrapKey( |
michael@0 | 5294 | fwMechanism, |
michael@0 | 5295 | pMechanism, |
michael@0 | 5296 | fwSession, |
michael@0 | 5297 | fwWrappingKeyObject, |
michael@0 | 5298 | &wrappedKey, |
michael@0 | 5299 | pTemplate, |
michael@0 | 5300 | ulAttributeCount, |
michael@0 | 5301 | &error); |
michael@0 | 5302 | |
michael@0 | 5303 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 5304 | if (!fwObject) { |
michael@0 | 5305 | goto loser; |
michael@0 | 5306 | } |
michael@0 | 5307 | *phKey = nssCKFWInstance_CreateObjectHandle(fwInstance, fwObject, &error); |
michael@0 | 5308 | |
michael@0 | 5309 | if (CKR_OK == error) { |
michael@0 | 5310 | return CKR_OK; |
michael@0 | 5311 | } |
michael@0 | 5312 | |
michael@0 | 5313 | loser: |
michael@0 | 5314 | /* verify error */ |
michael@0 | 5315 | switch( error ) { |
michael@0 | 5316 | case CKR_ARGUMENTS_BAD: |
michael@0 | 5317 | case CKR_ATTRIBUTE_READ_ONLY: |
michael@0 | 5318 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 5319 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 5320 | case CKR_BUFFER_TOO_SMALL: |
michael@0 | 5321 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 5322 | case CKR_DEVICE_ERROR: |
michael@0 | 5323 | case CKR_DEVICE_MEMORY: |
michael@0 | 5324 | case CKR_DEVICE_REMOVED: |
michael@0 | 5325 | case CKR_DOMAIN_PARAMS_INVALID: |
michael@0 | 5326 | case CKR_FUNCTION_CANCELED: |
michael@0 | 5327 | case CKR_FUNCTION_FAILED: |
michael@0 | 5328 | case CKR_GENERAL_ERROR: |
michael@0 | 5329 | case CKR_HOST_MEMORY: |
michael@0 | 5330 | case CKR_MECHANISM_INVALID: |
michael@0 | 5331 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 5332 | case CKR_OPERATION_ACTIVE: |
michael@0 | 5333 | case CKR_PIN_EXPIRED: |
michael@0 | 5334 | case CKR_SESSION_CLOSED: |
michael@0 | 5335 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 5336 | case CKR_SESSION_READ_ONLY: |
michael@0 | 5337 | case CKR_TEMPLATE_INCOMPLETE: |
michael@0 | 5338 | case CKR_TEMPLATE_INCONSISTENT: |
michael@0 | 5339 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 5340 | case CKR_UNWRAPPING_KEY_HANDLE_INVALID: |
michael@0 | 5341 | case CKR_UNWRAPPING_KEY_SIZE_RANGE: |
michael@0 | 5342 | case CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT: |
michael@0 | 5343 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 5344 | case CKR_WRAPPED_KEY_INVALID: |
michael@0 | 5345 | case CKR_WRAPPED_KEY_LEN_RANGE: |
michael@0 | 5346 | break; |
michael@0 | 5347 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 5348 | error = CKR_UNWRAPPING_KEY_HANDLE_INVALID; |
michael@0 | 5349 | break; |
michael@0 | 5350 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 5351 | error = CKR_UNWRAPPING_KEY_SIZE_RANGE; |
michael@0 | 5352 | break; |
michael@0 | 5353 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 5354 | error = CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT; |
michael@0 | 5355 | break; |
michael@0 | 5356 | case CKR_ENCRYPTED_DATA_INVALID: |
michael@0 | 5357 | error = CKR_WRAPPED_KEY_INVALID; |
michael@0 | 5358 | break; |
michael@0 | 5359 | case CKR_ENCRYPTED_DATA_LEN_RANGE: |
michael@0 | 5360 | error = CKR_WRAPPED_KEY_LEN_RANGE; |
michael@0 | 5361 | break; |
michael@0 | 5362 | default: |
michael@0 | 5363 | case CKR_OK: |
michael@0 | 5364 | error = CKR_GENERAL_ERROR; |
michael@0 | 5365 | break; |
michael@0 | 5366 | } |
michael@0 | 5367 | return error; |
michael@0 | 5368 | } |
michael@0 | 5369 | |
michael@0 | 5370 | /* |
michael@0 | 5371 | * NSSCKFWC_DeriveKey |
michael@0 | 5372 | * |
michael@0 | 5373 | */ |
michael@0 | 5374 | NSS_IMPLEMENT CK_RV |
michael@0 | 5375 | NSSCKFWC_DeriveKey |
michael@0 | 5376 | ( |
michael@0 | 5377 | NSSCKFWInstance *fwInstance, |
michael@0 | 5378 | CK_SESSION_HANDLE hSession, |
michael@0 | 5379 | CK_MECHANISM_PTR pMechanism, |
michael@0 | 5380 | CK_OBJECT_HANDLE hBaseKey, |
michael@0 | 5381 | CK_ATTRIBUTE_PTR pTemplate, |
michael@0 | 5382 | CK_ULONG ulAttributeCount, |
michael@0 | 5383 | CK_OBJECT_HANDLE_PTR phKey |
michael@0 | 5384 | ) |
michael@0 | 5385 | { |
michael@0 | 5386 | CK_RV error = CKR_OK; |
michael@0 | 5387 | NSSCKFWSession *fwSession; |
michael@0 | 5388 | NSSCKFWObject *fwObject; |
michael@0 | 5389 | NSSCKFWObject *fwBaseKeyObject; |
michael@0 | 5390 | NSSCKFWSlot *fwSlot; |
michael@0 | 5391 | NSSCKFWToken *fwToken; |
michael@0 | 5392 | NSSCKFWMechanism *fwMechanism; |
michael@0 | 5393 | |
michael@0 | 5394 | if (!fwInstance) { |
michael@0 | 5395 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 5396 | goto loser; |
michael@0 | 5397 | } |
michael@0 | 5398 | |
michael@0 | 5399 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 5400 | if (!fwSession) { |
michael@0 | 5401 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 5402 | goto loser; |
michael@0 | 5403 | } |
michael@0 | 5404 | |
michael@0 | 5405 | fwBaseKeyObject = nssCKFWInstance_ResolveObjectHandle(fwInstance, hBaseKey); |
michael@0 | 5406 | if (!fwBaseKeyObject) { |
michael@0 | 5407 | error = CKR_KEY_HANDLE_INVALID; |
michael@0 | 5408 | goto loser; |
michael@0 | 5409 | } |
michael@0 | 5410 | |
michael@0 | 5411 | fwSlot = nssCKFWSession_GetFWSlot(fwSession); |
michael@0 | 5412 | if (!fwSlot) { |
michael@0 | 5413 | error = CKR_GENERAL_ERROR; /* should never happen! */ |
michael@0 | 5414 | goto loser; |
michael@0 | 5415 | } |
michael@0 | 5416 | |
michael@0 | 5417 | if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) { |
michael@0 | 5418 | error = CKR_TOKEN_NOT_PRESENT; |
michael@0 | 5419 | goto loser; |
michael@0 | 5420 | } |
michael@0 | 5421 | |
michael@0 | 5422 | fwToken = nssCKFWSlot_GetToken(fwSlot, &error); |
michael@0 | 5423 | if (!fwToken) { |
michael@0 | 5424 | goto loser; |
michael@0 | 5425 | } |
michael@0 | 5426 | |
michael@0 | 5427 | fwMechanism = nssCKFWToken_GetMechanism(fwToken, pMechanism->mechanism, &error); |
michael@0 | 5428 | if (!fwMechanism) { |
michael@0 | 5429 | goto loser; |
michael@0 | 5430 | } |
michael@0 | 5431 | |
michael@0 | 5432 | fwObject = nssCKFWMechanism_DeriveKey( |
michael@0 | 5433 | fwMechanism, |
michael@0 | 5434 | pMechanism, |
michael@0 | 5435 | fwSession, |
michael@0 | 5436 | fwBaseKeyObject, |
michael@0 | 5437 | pTemplate, |
michael@0 | 5438 | ulAttributeCount, |
michael@0 | 5439 | &error); |
michael@0 | 5440 | |
michael@0 | 5441 | nssCKFWMechanism_Destroy(fwMechanism); |
michael@0 | 5442 | if (!fwObject) { |
michael@0 | 5443 | goto loser; |
michael@0 | 5444 | } |
michael@0 | 5445 | *phKey = nssCKFWInstance_CreateObjectHandle(fwInstance, fwObject, &error); |
michael@0 | 5446 | |
michael@0 | 5447 | if (CKR_OK == error) { |
michael@0 | 5448 | return CKR_OK; |
michael@0 | 5449 | } |
michael@0 | 5450 | |
michael@0 | 5451 | loser: |
michael@0 | 5452 | /* verify error */ |
michael@0 | 5453 | switch( error ) { |
michael@0 | 5454 | case CKR_ARGUMENTS_BAD: |
michael@0 | 5455 | case CKR_ATTRIBUTE_READ_ONLY: |
michael@0 | 5456 | case CKR_ATTRIBUTE_TYPE_INVALID: |
michael@0 | 5457 | case CKR_ATTRIBUTE_VALUE_INVALID: |
michael@0 | 5458 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 5459 | case CKR_DEVICE_ERROR: |
michael@0 | 5460 | case CKR_DEVICE_MEMORY: |
michael@0 | 5461 | case CKR_DEVICE_REMOVED: |
michael@0 | 5462 | case CKR_DOMAIN_PARAMS_INVALID: |
michael@0 | 5463 | case CKR_FUNCTION_CANCELED: |
michael@0 | 5464 | case CKR_FUNCTION_FAILED: |
michael@0 | 5465 | case CKR_GENERAL_ERROR: |
michael@0 | 5466 | case CKR_HOST_MEMORY: |
michael@0 | 5467 | case CKR_KEY_HANDLE_INVALID: |
michael@0 | 5468 | case CKR_KEY_SIZE_RANGE: |
michael@0 | 5469 | case CKR_KEY_TYPE_INCONSISTENT: |
michael@0 | 5470 | case CKR_MECHANISM_INVALID: |
michael@0 | 5471 | case CKR_MECHANISM_PARAM_INVALID: |
michael@0 | 5472 | case CKR_OPERATION_ACTIVE: |
michael@0 | 5473 | case CKR_PIN_EXPIRED: |
michael@0 | 5474 | case CKR_SESSION_CLOSED: |
michael@0 | 5475 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 5476 | case CKR_SESSION_READ_ONLY: |
michael@0 | 5477 | case CKR_TEMPLATE_INCOMPLETE: |
michael@0 | 5478 | case CKR_TEMPLATE_INCONSISTENT: |
michael@0 | 5479 | case CKR_TOKEN_WRITE_PROTECTED: |
michael@0 | 5480 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 5481 | break; |
michael@0 | 5482 | default: |
michael@0 | 5483 | case CKR_OK: |
michael@0 | 5484 | error = CKR_GENERAL_ERROR; |
michael@0 | 5485 | break; |
michael@0 | 5486 | } |
michael@0 | 5487 | return error; |
michael@0 | 5488 | } |
michael@0 | 5489 | |
michael@0 | 5490 | /* |
michael@0 | 5491 | * NSSCKFWC_SeedRandom |
michael@0 | 5492 | * |
michael@0 | 5493 | */ |
michael@0 | 5494 | NSS_IMPLEMENT CK_RV |
michael@0 | 5495 | NSSCKFWC_SeedRandom |
michael@0 | 5496 | ( |
michael@0 | 5497 | NSSCKFWInstance *fwInstance, |
michael@0 | 5498 | CK_SESSION_HANDLE hSession, |
michael@0 | 5499 | CK_BYTE_PTR pSeed, |
michael@0 | 5500 | CK_ULONG ulSeedLen |
michael@0 | 5501 | ) |
michael@0 | 5502 | { |
michael@0 | 5503 | CK_RV error = CKR_OK; |
michael@0 | 5504 | NSSCKFWSession *fwSession; |
michael@0 | 5505 | NSSItem seed; |
michael@0 | 5506 | |
michael@0 | 5507 | if (!fwInstance) { |
michael@0 | 5508 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 5509 | goto loser; |
michael@0 | 5510 | } |
michael@0 | 5511 | |
michael@0 | 5512 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 5513 | if (!fwSession) { |
michael@0 | 5514 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 5515 | goto loser; |
michael@0 | 5516 | } |
michael@0 | 5517 | |
michael@0 | 5518 | if( (CK_BYTE_PTR)CK_NULL_PTR == pSeed ) { |
michael@0 | 5519 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 5520 | goto loser; |
michael@0 | 5521 | } |
michael@0 | 5522 | |
michael@0 | 5523 | /* We could read through the buffer in a Purify trap */ |
michael@0 | 5524 | |
michael@0 | 5525 | seed.size = (PRUint32)ulSeedLen; |
michael@0 | 5526 | seed.data = (void *)pSeed; |
michael@0 | 5527 | |
michael@0 | 5528 | error = nssCKFWSession_SeedRandom(fwSession, &seed); |
michael@0 | 5529 | |
michael@0 | 5530 | if( CKR_OK != error ) { |
michael@0 | 5531 | goto loser; |
michael@0 | 5532 | } |
michael@0 | 5533 | |
michael@0 | 5534 | return CKR_OK; |
michael@0 | 5535 | |
michael@0 | 5536 | loser: |
michael@0 | 5537 | switch( error ) { |
michael@0 | 5538 | case CKR_SESSION_CLOSED: |
michael@0 | 5539 | /* destroy session? */ |
michael@0 | 5540 | break; |
michael@0 | 5541 | case CKR_DEVICE_REMOVED: |
michael@0 | 5542 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 5543 | break; |
michael@0 | 5544 | case CKR_ARGUMENTS_BAD: |
michael@0 | 5545 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 5546 | case CKR_DEVICE_ERROR: |
michael@0 | 5547 | case CKR_DEVICE_MEMORY: |
michael@0 | 5548 | case CKR_FUNCTION_CANCELED: |
michael@0 | 5549 | case CKR_FUNCTION_FAILED: |
michael@0 | 5550 | case CKR_GENERAL_ERROR: |
michael@0 | 5551 | case CKR_HOST_MEMORY: |
michael@0 | 5552 | case CKR_OPERATION_ACTIVE: |
michael@0 | 5553 | case CKR_RANDOM_SEED_NOT_SUPPORTED: |
michael@0 | 5554 | case CKR_RANDOM_NO_RNG: |
michael@0 | 5555 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 5556 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 5557 | break; |
michael@0 | 5558 | default: |
michael@0 | 5559 | case CKR_OK: |
michael@0 | 5560 | error = CKR_GENERAL_ERROR; |
michael@0 | 5561 | break; |
michael@0 | 5562 | } |
michael@0 | 5563 | |
michael@0 | 5564 | return error; |
michael@0 | 5565 | } |
michael@0 | 5566 | |
michael@0 | 5567 | /* |
michael@0 | 5568 | * NSSCKFWC_GenerateRandom |
michael@0 | 5569 | * |
michael@0 | 5570 | */ |
michael@0 | 5571 | NSS_IMPLEMENT CK_RV |
michael@0 | 5572 | NSSCKFWC_GenerateRandom |
michael@0 | 5573 | ( |
michael@0 | 5574 | NSSCKFWInstance *fwInstance, |
michael@0 | 5575 | CK_SESSION_HANDLE hSession, |
michael@0 | 5576 | CK_BYTE_PTR pRandomData, |
michael@0 | 5577 | CK_ULONG ulRandomLen |
michael@0 | 5578 | ) |
michael@0 | 5579 | { |
michael@0 | 5580 | CK_RV error = CKR_OK; |
michael@0 | 5581 | NSSCKFWSession *fwSession; |
michael@0 | 5582 | NSSItem buffer; |
michael@0 | 5583 | |
michael@0 | 5584 | if (!fwInstance) { |
michael@0 | 5585 | error = CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 5586 | goto loser; |
michael@0 | 5587 | } |
michael@0 | 5588 | |
michael@0 | 5589 | fwSession = nssCKFWInstance_ResolveSessionHandle(fwInstance, hSession); |
michael@0 | 5590 | if (!fwSession) { |
michael@0 | 5591 | error = CKR_SESSION_HANDLE_INVALID; |
michael@0 | 5592 | goto loser; |
michael@0 | 5593 | } |
michael@0 | 5594 | |
michael@0 | 5595 | if( (CK_BYTE_PTR)CK_NULL_PTR == pRandomData ) { |
michael@0 | 5596 | error = CKR_ARGUMENTS_BAD; |
michael@0 | 5597 | goto loser; |
michael@0 | 5598 | } |
michael@0 | 5599 | |
michael@0 | 5600 | /* |
michael@0 | 5601 | * A purify error here indicates caller error. |
michael@0 | 5602 | */ |
michael@0 | 5603 | (void)nsslibc_memset(pRandomData, 0, ulRandomLen); |
michael@0 | 5604 | |
michael@0 | 5605 | buffer.size = (PRUint32)ulRandomLen; |
michael@0 | 5606 | buffer.data = (void *)pRandomData; |
michael@0 | 5607 | |
michael@0 | 5608 | error = nssCKFWSession_GetRandom(fwSession, &buffer); |
michael@0 | 5609 | |
michael@0 | 5610 | if( CKR_OK != error ) { |
michael@0 | 5611 | goto loser; |
michael@0 | 5612 | } |
michael@0 | 5613 | |
michael@0 | 5614 | return CKR_OK; |
michael@0 | 5615 | |
michael@0 | 5616 | loser: |
michael@0 | 5617 | switch( error ) { |
michael@0 | 5618 | case CKR_SESSION_CLOSED: |
michael@0 | 5619 | /* destroy session? */ |
michael@0 | 5620 | break; |
michael@0 | 5621 | case CKR_DEVICE_REMOVED: |
michael@0 | 5622 | /* (void)nssCKFWToken_Destroy(fwToken); */ |
michael@0 | 5623 | break; |
michael@0 | 5624 | case CKR_ARGUMENTS_BAD: |
michael@0 | 5625 | case CKR_CRYPTOKI_NOT_INITIALIZED: |
michael@0 | 5626 | case CKR_DEVICE_ERROR: |
michael@0 | 5627 | case CKR_DEVICE_MEMORY: |
michael@0 | 5628 | case CKR_FUNCTION_CANCELED: |
michael@0 | 5629 | case CKR_FUNCTION_FAILED: |
michael@0 | 5630 | case CKR_GENERAL_ERROR: |
michael@0 | 5631 | case CKR_HOST_MEMORY: |
michael@0 | 5632 | case CKR_OPERATION_ACTIVE: |
michael@0 | 5633 | case CKR_RANDOM_NO_RNG: |
michael@0 | 5634 | case CKR_SESSION_HANDLE_INVALID: |
michael@0 | 5635 | case CKR_USER_NOT_LOGGED_IN: |
michael@0 | 5636 | break; |
michael@0 | 5637 | default: |
michael@0 | 5638 | case CKR_OK: |
michael@0 | 5639 | error = CKR_GENERAL_ERROR; |
michael@0 | 5640 | break; |
michael@0 | 5641 | } |
michael@0 | 5642 | |
michael@0 | 5643 | return error; |
michael@0 | 5644 | } |
michael@0 | 5645 | |
michael@0 | 5646 | /* |
michael@0 | 5647 | * NSSCKFWC_GetFunctionStatus |
michael@0 | 5648 | * |
michael@0 | 5649 | */ |
michael@0 | 5650 | NSS_IMPLEMENT CK_RV |
michael@0 | 5651 | NSSCKFWC_GetFunctionStatus |
michael@0 | 5652 | ( |
michael@0 | 5653 | NSSCKFWInstance *fwInstance, |
michael@0 | 5654 | CK_SESSION_HANDLE hSession |
michael@0 | 5655 | ) |
michael@0 | 5656 | { |
michael@0 | 5657 | return CKR_FUNCTION_NOT_PARALLEL; |
michael@0 | 5658 | } |
michael@0 | 5659 | |
michael@0 | 5660 | /* |
michael@0 | 5661 | * NSSCKFWC_CancelFunction |
michael@0 | 5662 | * |
michael@0 | 5663 | */ |
michael@0 | 5664 | NSS_IMPLEMENT CK_RV |
michael@0 | 5665 | NSSCKFWC_CancelFunction |
michael@0 | 5666 | ( |
michael@0 | 5667 | NSSCKFWInstance *fwInstance, |
michael@0 | 5668 | CK_SESSION_HANDLE hSession |
michael@0 | 5669 | ) |
michael@0 | 5670 | { |
michael@0 | 5671 | return CKR_FUNCTION_NOT_PARALLEL; |
michael@0 | 5672 | } |