Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: nss/lib/ckfw/instance.c

Issue 2078763002: Delete bundled copy of NSS and replace with README. (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/nss@master
Patch Set: Delete bundled copy of NSS and replace with README. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « nss/lib/ckfw/hash.c ('k') | nss/lib/ckfw/mechanism.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /*
6 * instance.c
7 *
8 * This file implements the NSSCKFWInstance type and methods.
9 */
10
11 #ifndef CK_T
12 #include "ck.h"
13 #endif /* CK_T */
14
15 /*
16 * NSSCKFWInstance
17 *
18 * -- create/destroy --
19 * nssCKFWInstance_Create
20 * nssCKFWInstance_Destroy
21 *
22 * -- public accessors --
23 * NSSCKFWInstance_GetMDInstance
24 * NSSCKFWInstance_GetArena
25 * NSSCKFWInstance_MayCreatePthreads
26 * NSSCKFWInstance_CreateMutex
27 * NSSCKFWInstance_GetConfigurationData
28 * NSSCKFWInstance_GetInitArgs
29 *
30 * -- implement public accessors --
31 * nssCKFWInstance_GetMDInstance
32 * nssCKFWInstance_GetArena
33 * nssCKFWInstance_MayCreatePthreads
34 * nssCKFWInstance_CreateMutex
35 * nssCKFWInstance_GetConfigurationData
36 * nssCKFWInstance_GetInitArgs
37 *
38 * -- private accessors --
39 * nssCKFWInstance_CreateSessionHandle
40 * nssCKFWInstance_ResolveSessionHandle
41 * nssCKFWInstance_DestroySessionHandle
42 * nssCKFWInstance_FindSessionHandle
43 * nssCKFWInstance_CreateObjectHandle
44 * nssCKFWInstance_ResolveObjectHandle
45 * nssCKFWInstance_DestroyObjectHandle
46 *
47 * -- module fronts --
48 * nssCKFWInstance_GetNSlots
49 * nssCKFWInstance_GetCryptokiVersion
50 * nssCKFWInstance_GetManufacturerID
51 * nssCKFWInstance_GetFlags
52 * nssCKFWInstance_GetLibraryDescription
53 * nssCKFWInstance_GetLibraryVersion
54 * nssCKFWInstance_GetModuleHandlesSessionObjects
55 * nssCKFWInstance_GetSlots
56 * nssCKFWInstance_WaitForSlotEvent
57 *
58 * -- debugging versions only --
59 * nssCKFWInstance_verifyPointer
60 */
61
62 struct NSSCKFWInstanceStr {
63 NSSCKFWMutex *mutex;
64 NSSArena *arena;
65 NSSCKMDInstance *mdInstance;
66 CK_C_INITIALIZE_ARGS_PTR pInitArgs;
67 CK_C_INITIALIZE_ARGS initArgs;
68 CryptokiLockingState LockingState;
69 CK_BBOOL mayCreatePthreads;
70 NSSUTF8 *configurationData;
71 CK_ULONG nSlots;
72 NSSCKFWSlot **fwSlotList;
73 NSSCKMDSlot **mdSlotList;
74 CK_BBOOL moduleHandlesSessionObjects;
75
76 /*
77 * Everything above is set at creation time, and then not modified.
78 * The invariants the mutex protects are:
79 *
80 * 1) Each of the cached descriptions (versions, etc.) are in an
81 * internally consistant state.
82 *
83 * 2) The session handle hashes and count are consistant
84 *
85 * 3) The object handle hashes and count are consistant.
86 *
87 * I could use multiple locks, but let's wait to see if that's
88 * really necessary.
89 *
90 * Note that the calls accessing the cached descriptions will
91 * call the NSSCKMDInstance methods with the mutex locked. Those
92 * methods may then call the public NSSCKFWInstance routines.
93 * Those public routines only access the constant data above, so
94 * there's no problem. But be careful if you add to this object;
95 * mutexes are in general not reentrant, so don't create deadlock
96 * situations.
97 */
98
99 CK_VERSION cryptokiVersion;
100 NSSUTF8 *manufacturerID;
101 NSSUTF8 *libraryDescription;
102 CK_VERSION libraryVersion;
103
104 CK_ULONG lastSessionHandle;
105 nssCKFWHash *sessionHandleHash;
106
107 CK_ULONG lastObjectHandle;
108 nssCKFWHash *objectHandleHash;
109 };
110
111 #ifdef DEBUG
112 /*
113 * But first, the pointer-tracking stuff.
114 *
115 * NOTE: the pointer-tracking support in NSS/base currently relies
116 * upon NSPR's CallOnce support. That, however, relies upon NSPR's
117 * locking, which is tied into the runtime. We need a pointer-tracker
118 * implementation that uses the locks supplied through C_Initialize.
119 * That support, however, can be filled in later. So for now, I'll
120 * just do this routines as no-ops.
121 */
122
123 static CK_RV
124 instance_add_pointer(
125 const NSSCKFWInstance *fwInstance)
126 {
127 return CKR_OK;
128 }
129
130 static CK_RV
131 instance_remove_pointer(
132 const NSSCKFWInstance *fwInstance)
133 {
134 return CKR_OK;
135 }
136
137 NSS_IMPLEMENT CK_RV
138 nssCKFWInstance_verifyPointer(
139 const NSSCKFWInstance *fwInstance)
140 {
141 return CKR_OK;
142 }
143
144 #endif /* DEBUG */
145
146 /*
147 * nssCKFWInstance_Create
148 *
149 */
150 NSS_IMPLEMENT NSSCKFWInstance *
151 nssCKFWInstance_Create(
152 CK_C_INITIALIZE_ARGS_PTR pInitArgs,
153 CryptokiLockingState LockingState,
154 NSSCKMDInstance *mdInstance,
155 CK_RV *pError)
156 {
157 NSSCKFWInstance *fwInstance;
158 NSSArena *arena = (NSSArena *)NULL;
159 CK_ULONG i;
160 CK_BBOOL called_Initialize = CK_FALSE;
161
162 #ifdef NSSDEBUG
163 if ((CK_RV)NULL == pError) {
164 return (NSSCKFWInstance *)NULL;
165 }
166
167 if (!mdInstance) {
168 *pError = CKR_ARGUMENTS_BAD;
169 return (NSSCKFWInstance *)NULL;
170 }
171 #endif /* NSSDEBUG */
172
173 arena = NSSArena_Create();
174 if (!arena) {
175 *pError = CKR_HOST_MEMORY;
176 return (NSSCKFWInstance *)NULL;
177 }
178
179 fwInstance = nss_ZNEW(arena, NSSCKFWInstance);
180 if (!fwInstance) {
181 goto nomem;
182 }
183
184 fwInstance->arena = arena;
185 fwInstance->mdInstance = mdInstance;
186
187 fwInstance->LockingState = LockingState;
188 if ((CK_C_INITIALIZE_ARGS_PTR)NULL != pInitArgs) {
189 fwInstance->initArgs = *pInitArgs;
190 fwInstance->pInitArgs = &fwInstance->initArgs;
191 if (pInitArgs->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS) {
192 fwInstance->mayCreatePthreads = CK_FALSE;
193 } else {
194 fwInstance->mayCreatePthreads = CK_TRUE;
195 }
196 fwInstance->configurationData = (NSSUTF8 *)(pInitArgs->pReserved);
197 } else {
198 fwInstance->mayCreatePthreads = CK_TRUE;
199 }
200
201 fwInstance->mutex = nssCKFWMutex_Create(pInitArgs, LockingState, arena,
202 pError);
203 if (!fwInstance->mutex) {
204 if (CKR_OK == *pError) {
205 *pError = CKR_GENERAL_ERROR;
206 }
207 goto loser;
208 }
209
210 if (mdInstance->Initialize) {
211 *pError = mdInstance->Initialize(mdInstance, fwInstance, fwInstance->con figurationData);
212 if (CKR_OK != *pError) {
213 goto loser;
214 }
215
216 called_Initialize = CK_TRUE;
217 }
218
219 if (mdInstance->ModuleHandlesSessionObjects) {
220 fwInstance->moduleHandlesSessionObjects =
221 mdInstance->ModuleHandlesSessionObjects(mdInstance, fwInstance);
222 } else {
223 fwInstance->moduleHandlesSessionObjects = CK_FALSE;
224 }
225
226 if (!mdInstance->GetNSlots) {
227 /* That routine is required */
228 *pError = CKR_GENERAL_ERROR;
229 goto loser;
230 }
231
232 fwInstance->nSlots = mdInstance->GetNSlots(mdInstance, fwInstance, pError);
233 if ((CK_ULONG)0 == fwInstance->nSlots) {
234 if (CKR_OK == *pError) {
235 /* Zero is not a legitimate answer */
236 *pError = CKR_GENERAL_ERROR;
237 }
238 goto loser;
239 }
240
241 fwInstance->fwSlotList = nss_ZNEWARRAY(arena, NSSCKFWSlot *, fwInstance->nSl ots);
242 if ((NSSCKFWSlot **)NULL == fwInstance->fwSlotList) {
243 goto nomem;
244 }
245
246 fwInstance->mdSlotList = nss_ZNEWARRAY(arena, NSSCKMDSlot *, fwInstance->nSl ots);
247 if ((NSSCKMDSlot **)NULL == fwInstance->mdSlotList) {
248 goto nomem;
249 }
250
251 fwInstance->sessionHandleHash = nssCKFWHash_Create(fwInstance,
252 fwInstance->arena, pError );
253 if (!fwInstance->sessionHandleHash) {
254 goto loser;
255 }
256
257 fwInstance->objectHandleHash = nssCKFWHash_Create(fwInstance,
258 fwInstance->arena, pError) ;
259 if (!fwInstance->objectHandleHash) {
260 goto loser;
261 }
262
263 if (!mdInstance->GetSlots) {
264 /* That routine is required */
265 *pError = CKR_GENERAL_ERROR;
266 goto loser;
267 }
268
269 *pError = mdInstance->GetSlots(mdInstance, fwInstance, fwInstance->mdSlotLis t);
270 if (CKR_OK != *pError) {
271 goto loser;
272 }
273
274 for (i = 0; i < fwInstance->nSlots; i++) {
275 NSSCKMDSlot *mdSlot = fwInstance->mdSlotList[i];
276
277 if (!mdSlot) {
278 *pError = CKR_GENERAL_ERROR;
279 goto loser;
280 }
281
282 fwInstance->fwSlotList[i] = nssCKFWSlot_Create(fwInstance, mdSlot, i, pE rror);
283 if (CKR_OK != *pError) {
284 CK_ULONG j;
285
286 for (j = 0; j < i; j++) {
287 (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[j]);
288 }
289
290 for (j = i; j < fwInstance->nSlots; j++) {
291 NSSCKMDSlot *mds = fwInstance->mdSlotList[j];
292 if (mds->Destroy) {
293 mds->Destroy(mds, (NSSCKFWSlot *)NULL, mdInstance, fwInstanc e);
294 }
295 }
296
297 goto loser;
298 }
299 }
300
301 #ifdef DEBUG
302 *pError = instance_add_pointer(fwInstance);
303 if (CKR_OK != *pError) {
304 for (i = 0; i < fwInstance->nSlots; i++) {
305 (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
306 }
307
308 goto loser;
309 }
310 #endif /* DEBUG */
311
312 *pError = CKR_OK;
313 return fwInstance;
314
315 nomem:
316 *pError = CKR_HOST_MEMORY;
317 /*FALLTHROUGH*/
318 loser:
319
320 if (CK_TRUE == called_Initialize) {
321 if (mdInstance->Finalize) {
322 mdInstance->Finalize(mdInstance, fwInstance);
323 }
324 }
325
326 if (fwInstance && fwInstance->mutex) {
327 nssCKFWMutex_Destroy(fwInstance->mutex);
328 }
329
330 if (arena) {
331 (void)NSSArena_Destroy(arena);
332 }
333 return (NSSCKFWInstance *)NULL;
334 }
335
336 /*
337 * nssCKFWInstance_Destroy
338 *
339 */
340 NSS_IMPLEMENT CK_RV
341 nssCKFWInstance_Destroy(
342 NSSCKFWInstance *fwInstance)
343 {
344 #ifdef NSSDEBUG
345 CK_RV error = CKR_OK;
346 #endif /* NSSDEBUG */
347 CK_ULONG i;
348
349 #ifdef NSSDEBUG
350 error = nssCKFWInstance_verifyPointer(fwInstance);
351 if (CKR_OK != error) {
352 return error;
353 }
354 #endif /* NSSDEBUG */
355
356 nssCKFWMutex_Destroy(fwInstance->mutex);
357
358 for (i = 0; i < fwInstance->nSlots; i++) {
359 (void)nssCKFWSlot_Destroy(fwInstance->fwSlotList[i]);
360 }
361
362 if (fwInstance->mdInstance->Finalize) {
363 fwInstance->mdInstance->Finalize(fwInstance->mdInstance, fwInstance);
364 }
365
366 if (fwInstance->sessionHandleHash) {
367 nssCKFWHash_Destroy(fwInstance->sessionHandleHash);
368 }
369
370 if (fwInstance->objectHandleHash) {
371 nssCKFWHash_Destroy(fwInstance->objectHandleHash);
372 }
373
374 #ifdef DEBUG
375 (void)instance_remove_pointer(fwInstance);
376 #endif /* DEBUG */
377
378 (void)NSSArena_Destroy(fwInstance->arena);
379 return CKR_OK;
380 }
381
382 /*
383 * nssCKFWInstance_GetMDInstance
384 *
385 */
386 NSS_IMPLEMENT NSSCKMDInstance *
387 nssCKFWInstance_GetMDInstance(
388 NSSCKFWInstance *fwInstance)
389 {
390 #ifdef NSSDEBUG
391 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
392 return (NSSCKMDInstance *)NULL;
393 }
394 #endif /* NSSDEBUG */
395
396 return fwInstance->mdInstance;
397 }
398
399 /*
400 * nssCKFWInstance_GetArena
401 *
402 */
403 NSS_IMPLEMENT NSSArena *
404 nssCKFWInstance_GetArena(
405 NSSCKFWInstance *fwInstance,
406 CK_RV *pError)
407 {
408 #ifdef NSSDEBUG
409 if (!pError) {
410 return (NSSArena *)NULL;
411 }
412
413 *pError = nssCKFWInstance_verifyPointer(fwInstance);
414 if (CKR_OK != *pError) {
415 return (NSSArena *)NULL;
416 }
417 #endif /* NSSDEBUG */
418
419 *pError = CKR_OK;
420 return fwInstance->arena;
421 }
422
423 /*
424 * nssCKFWInstance_MayCreatePthreads
425 *
426 */
427 NSS_IMPLEMENT CK_BBOOL
428 nssCKFWInstance_MayCreatePthreads(
429 NSSCKFWInstance *fwInstance)
430 {
431 #ifdef NSSDEBUG
432 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
433 return CK_FALSE;
434 }
435 #endif /* NSSDEBUG */
436
437 return fwInstance->mayCreatePthreads;
438 }
439
440 /*
441 * nssCKFWInstance_CreateMutex
442 *
443 */
444 NSS_IMPLEMENT NSSCKFWMutex *
445 nssCKFWInstance_CreateMutex(
446 NSSCKFWInstance *fwInstance,
447 NSSArena *arena,
448 CK_RV *pError)
449 {
450 NSSCKFWMutex *mutex;
451
452 #ifdef NSSDEBUG
453 if (!pError) {
454 return (NSSCKFWMutex *)NULL;
455 }
456
457 *pError = nssCKFWInstance_verifyPointer(fwInstance);
458 if (CKR_OK != *pError) {
459 return (NSSCKFWMutex *)NULL;
460 }
461 #endif /* NSSDEBUG */
462
463 mutex = nssCKFWMutex_Create(fwInstance->pInitArgs, fwInstance->LockingState,
464 arena, pError);
465 if (!mutex) {
466 if (CKR_OK == *pError) {
467 *pError = CKR_GENERAL_ERROR;
468 }
469
470 return (NSSCKFWMutex *)NULL;
471 }
472
473 return mutex;
474 }
475
476 /*
477 * nssCKFWInstance_GetConfigurationData
478 *
479 */
480 NSS_IMPLEMENT NSSUTF8 *
481 nssCKFWInstance_GetConfigurationData(
482 NSSCKFWInstance *fwInstance)
483 {
484 #ifdef NSSDEBUG
485 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
486 return (NSSUTF8 *)NULL;
487 }
488 #endif /* NSSDEBUG */
489
490 return fwInstance->configurationData;
491 }
492
493 /*
494 * nssCKFWInstance_GetInitArgs
495 *
496 */
497 CK_C_INITIALIZE_ARGS_PTR
498 nssCKFWInstance_GetInitArgs(
499 NSSCKFWInstance *fwInstance)
500 {
501 #ifdef NSSDEBUG
502 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
503 return (CK_C_INITIALIZE_ARGS_PTR)NULL;
504 }
505 #endif /* NSSDEBUG */
506
507 return fwInstance->pInitArgs;
508 }
509
510 /*
511 * nssCKFWInstance_CreateSessionHandle
512 *
513 */
514 NSS_IMPLEMENT CK_SESSION_HANDLE
515 nssCKFWInstance_CreateSessionHandle(
516 NSSCKFWInstance *fwInstance,
517 NSSCKFWSession *fwSession,
518 CK_RV *pError)
519 {
520 CK_SESSION_HANDLE hSession;
521
522 #ifdef NSSDEBUG
523 if (!pError) {
524 return (CK_SESSION_HANDLE)0;
525 }
526
527 *pError = nssCKFWInstance_verifyPointer(fwInstance);
528 if (CKR_OK != *pError) {
529 return (CK_SESSION_HANDLE)0;
530 }
531 #endif /* NSSDEBUG */
532
533 *pError = nssCKFWMutex_Lock(fwInstance->mutex);
534 if (CKR_OK != *pError) {
535 return (CK_SESSION_HANDLE)0;
536 }
537
538 hSession = ++(fwInstance->lastSessionHandle);
539
540 /* Alan would say I should unlock for this call. */
541
542 *pError = nssCKFWSession_SetHandle(fwSession, hSession);
543 if (CKR_OK != *pError) {
544 goto done;
545 }
546
547 *pError = nssCKFWHash_Add(fwInstance->sessionHandleHash,
548 (const void *)hSession, (const void *)fwSession);
549 if (CKR_OK != *pError) {
550 hSession = (CK_SESSION_HANDLE)0;
551 goto done;
552 }
553
554 done:
555 nssCKFWMutex_Unlock(fwInstance->mutex);
556 return hSession;
557 }
558
559 /*
560 * nssCKFWInstance_ResolveSessionHandle
561 *
562 */
563 NSS_IMPLEMENT NSSCKFWSession *
564 nssCKFWInstance_ResolveSessionHandle(
565 NSSCKFWInstance *fwInstance,
566 CK_SESSION_HANDLE hSession)
567 {
568 NSSCKFWSession *fwSession;
569
570 #ifdef NSSDEBUG
571 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
572 return (NSSCKFWSession *)NULL;
573 }
574 #endif /* NSSDEBUG */
575
576 if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
577 return (NSSCKFWSession *)NULL;
578 }
579
580 fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
581 fwInstance->sessionHandleHash, (const void *)hSession);
582
583 /* Assert(hSession == nssCKFWSession_GetHandle(fwSession)) */
584
585 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
586
587 return fwSession;
588 }
589
590 /*
591 * nssCKFWInstance_DestroySessionHandle
592 *
593 */
594 NSS_IMPLEMENT void
595 nssCKFWInstance_DestroySessionHandle(
596 NSSCKFWInstance *fwInstance,
597 CK_SESSION_HANDLE hSession)
598 {
599 NSSCKFWSession *fwSession;
600
601 #ifdef NSSDEBUG
602 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
603 return;
604 }
605 #endif /* NSSDEBUG */
606
607 if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
608 return;
609 }
610
611 fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
612 fwInstance->sessionHandleHash, (const void *)hSession);
613 if (fwSession) {
614 nssCKFWHash_Remove(fwInstance->sessionHandleHash, (const void *)hSession );
615 nssCKFWSession_SetHandle(fwSession, (CK_SESSION_HANDLE)0);
616 }
617
618 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
619
620 return;
621 }
622
623 /*
624 * nssCKFWInstance_FindSessionHandle
625 *
626 */
627 NSS_IMPLEMENT CK_SESSION_HANDLE
628 nssCKFWInstance_FindSessionHandle(
629 NSSCKFWInstance *fwInstance,
630 NSSCKFWSession *fwSession)
631 {
632 #ifdef NSSDEBUG
633 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
634 return (CK_SESSION_HANDLE)0;
635 }
636
637 if (CKR_OK != nssCKFWSession_verifyPointer(fwSession)) {
638 return (CK_SESSION_HANDLE)0;
639 }
640 #endif /* NSSDEBUG */
641
642 return nssCKFWSession_GetHandle(fwSession);
643 /* look it up and assert? */
644 }
645
646 /*
647 * nssCKFWInstance_CreateObjectHandle
648 *
649 */
650 NSS_IMPLEMENT CK_OBJECT_HANDLE
651 nssCKFWInstance_CreateObjectHandle(
652 NSSCKFWInstance *fwInstance,
653 NSSCKFWObject *fwObject,
654 CK_RV *pError)
655 {
656 CK_OBJECT_HANDLE hObject;
657
658 #ifdef NSSDEBUG
659 if (!pError) {
660 return (CK_OBJECT_HANDLE)0;
661 }
662
663 *pError = nssCKFWInstance_verifyPointer(fwInstance);
664 if (CKR_OK != *pError) {
665 return (CK_OBJECT_HANDLE)0;
666 }
667 #endif /* NSSDEBUG */
668
669 *pError = nssCKFWMutex_Lock(fwInstance->mutex);
670 if (CKR_OK != *pError) {
671 return (CK_OBJECT_HANDLE)0;
672 }
673
674 hObject = ++(fwInstance->lastObjectHandle);
675
676 *pError = nssCKFWObject_SetHandle(fwObject, hObject);
677 if (CKR_OK != *pError) {
678 hObject = (CK_OBJECT_HANDLE)0;
679 goto done;
680 }
681
682 *pError = nssCKFWHash_Add(fwInstance->objectHandleHash,
683 (const void *)hObject, (const void *)fwObject);
684 if (CKR_OK != *pError) {
685 hObject = (CK_OBJECT_HANDLE)0;
686 goto done;
687 }
688
689 done:
690 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
691 return hObject;
692 }
693
694 /*
695 * nssCKFWInstance_ResolveObjectHandle
696 *
697 */
698 NSS_IMPLEMENT NSSCKFWObject *
699 nssCKFWInstance_ResolveObjectHandle(
700 NSSCKFWInstance *fwInstance,
701 CK_OBJECT_HANDLE hObject)
702 {
703 NSSCKFWObject *fwObject;
704
705 #ifdef NSSDEBUG
706 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
707 return (NSSCKFWObject *)NULL;
708 }
709 #endif /* NSSDEBUG */
710
711 if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
712 return (NSSCKFWObject *)NULL;
713 }
714
715 fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
716 fwInstance->objectHandleHash, (const void *)hObject);
717
718 /* Assert(hObject == nssCKFWObject_GetHandle(fwObject)) */
719
720 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
721 return fwObject;
722 }
723
724 /*
725 * nssCKFWInstance_ReassignObjectHandle
726 *
727 */
728 NSS_IMPLEMENT CK_RV
729 nssCKFWInstance_ReassignObjectHandle(
730 NSSCKFWInstance *fwInstance,
731 CK_OBJECT_HANDLE hObject,
732 NSSCKFWObject *fwObject)
733 {
734 CK_RV error = CKR_OK;
735 NSSCKFWObject *oldObject;
736
737 #ifdef NSSDEBUG
738 error = nssCKFWInstance_verifyPointer(fwInstance);
739 if (CKR_OK != error) {
740 return error;
741 }
742 #endif /* NSSDEBUG */
743
744 error = nssCKFWMutex_Lock(fwInstance->mutex);
745 if (CKR_OK != error) {
746 return error;
747 }
748
749 oldObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
750 fwInstance->objectHandleHash, (const void *)hObject);
751 if (oldObject) {
752 /* Assert(hObject == nssCKFWObject_GetHandle(oldObject) */
753 (void)nssCKFWObject_SetHandle(oldObject, (CK_SESSION_HANDLE)0);
754 nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
755 }
756
757 error = nssCKFWObject_SetHandle(fwObject, hObject);
758 if (CKR_OK != error) {
759 goto done;
760 }
761 error = nssCKFWHash_Add(fwInstance->objectHandleHash,
762 (const void *)hObject, (const void *)fwObject);
763
764 done:
765 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
766 return error;
767 }
768
769 /*
770 * nssCKFWInstance_DestroyObjectHandle
771 *
772 */
773 NSS_IMPLEMENT void
774 nssCKFWInstance_DestroyObjectHandle(
775 NSSCKFWInstance *fwInstance,
776 CK_OBJECT_HANDLE hObject)
777 {
778 NSSCKFWObject *fwObject;
779
780 #ifdef NSSDEBUG
781 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
782 return;
783 }
784 #endif /* NSSDEBUG */
785
786 if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
787 return;
788 }
789
790 fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
791 fwInstance->objectHandleHash, (const void *)hObject);
792 if (fwObject) {
793 /* Assert(hObject = nssCKFWObject_GetHandle(fwObject)) */
794 nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)hObject);
795 (void)nssCKFWObject_SetHandle(fwObject, (CK_SESSION_HANDLE)0);
796 }
797
798 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
799 return;
800 }
801
802 /*
803 * nssCKFWInstance_FindObjectHandle
804 *
805 */
806 NSS_IMPLEMENT CK_OBJECT_HANDLE
807 nssCKFWInstance_FindObjectHandle(
808 NSSCKFWInstance *fwInstance,
809 NSSCKFWObject *fwObject)
810 {
811 #ifdef NSSDEBUG
812 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
813 return (CK_OBJECT_HANDLE)0;
814 }
815
816 if (CKR_OK != nssCKFWObject_verifyPointer(fwObject)) {
817 return (CK_OBJECT_HANDLE)0;
818 }
819 #endif /* NSSDEBUG */
820
821 return nssCKFWObject_GetHandle(fwObject);
822 }
823
824 /*
825 * nssCKFWInstance_GetNSlots
826 *
827 */
828 NSS_IMPLEMENT CK_ULONG
829 nssCKFWInstance_GetNSlots(
830 NSSCKFWInstance *fwInstance,
831 CK_RV *pError)
832 {
833 #ifdef NSSDEBUG
834 if (!pError) {
835 return (CK_ULONG)0;
836 }
837
838 *pError = nssCKFWInstance_verifyPointer(fwInstance);
839 if (CKR_OK != *pError) {
840 return (CK_ULONG)0;
841 }
842 #endif /* NSSDEBUG */
843
844 *pError = CKR_OK;
845 return fwInstance->nSlots;
846 }
847
848 /*
849 * nssCKFWInstance_GetCryptokiVersion
850 *
851 */
852 NSS_IMPLEMENT CK_VERSION
853 nssCKFWInstance_GetCryptokiVersion(
854 NSSCKFWInstance *fwInstance)
855 {
856 CK_VERSION rv;
857
858 #ifdef NSSDEBUG
859 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
860 rv.major = rv.minor = 0;
861 return rv;
862 }
863 #endif /* NSSDEBUG */
864
865 if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
866 rv.major = rv.minor = 0;
867 return rv;
868 }
869
870 if ((0 != fwInstance->cryptokiVersion.major) ||
871 (0 != fwInstance->cryptokiVersion.minor)) {
872 rv = fwInstance->cryptokiVersion;
873 goto done;
874 }
875
876 if (fwInstance->mdInstance->GetCryptokiVersion) {
877 fwInstance->cryptokiVersion = fwInstance->mdInstance->GetCryptokiVersion (
878 fwInstance->mdInstance, fwInstance);
879 } else {
880 fwInstance->cryptokiVersion.major = 2;
881 fwInstance->cryptokiVersion.minor = 1;
882 }
883
884 rv = fwInstance->cryptokiVersion;
885
886 done:
887 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
888 return rv;
889 }
890
891 /*
892 * nssCKFWInstance_GetManufacturerID
893 *
894 */
895 NSS_IMPLEMENT CK_RV
896 nssCKFWInstance_GetManufacturerID(
897 NSSCKFWInstance *fwInstance,
898 CK_CHAR manufacturerID[32])
899 {
900 CK_RV error = CKR_OK;
901
902 #ifdef NSSDEBUG
903 if ((CK_CHAR_PTR)NULL == manufacturerID) {
904 return CKR_ARGUMENTS_BAD;
905 }
906
907 error = nssCKFWInstance_verifyPointer(fwInstance);
908 if (CKR_OK != error) {
909 return error;
910 }
911 #endif /* NSSDEBUG */
912
913 error = nssCKFWMutex_Lock(fwInstance->mutex);
914 if (CKR_OK != error) {
915 return error;
916 }
917
918 if (!fwInstance->manufacturerID) {
919 if (fwInstance->mdInstance->GetManufacturerID) {
920 fwInstance->manufacturerID = fwInstance->mdInstance->GetManufacturer ID(
921 fwInstance->mdInstance, fwInstance, &error);
922 if ((!fwInstance->manufacturerID) && (CKR_OK != error)) {
923 goto done;
924 }
925 } else {
926 fwInstance->manufacturerID = (NSSUTF8 *)"";
927 }
928 }
929
930 (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->manufacturerID, (char *)manufa cturerID, 32, ' ');
931 error = CKR_OK;
932
933 done:
934 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
935 return error;
936 }
937
938 /*
939 * nssCKFWInstance_GetFlags
940 *
941 */
942 NSS_IMPLEMENT CK_ULONG
943 nssCKFWInstance_GetFlags(
944 NSSCKFWInstance *fwInstance)
945 {
946 #ifdef NSSDEBUG
947 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
948 return (CK_ULONG)0;
949 }
950 #endif /* NSSDEBUG */
951
952 /* No "instance flags" are yet defined by Cryptoki. */
953 return (CK_ULONG)0;
954 }
955
956 /*
957 * nssCKFWInstance_GetLibraryDescription
958 *
959 */
960 NSS_IMPLEMENT CK_RV
961 nssCKFWInstance_GetLibraryDescription(
962 NSSCKFWInstance *fwInstance,
963 CK_CHAR libraryDescription[32])
964 {
965 CK_RV error = CKR_OK;
966
967 #ifdef NSSDEBUG
968 if ((CK_CHAR_PTR)NULL == libraryDescription) {
969 return CKR_ARGUMENTS_BAD;
970 }
971
972 error = nssCKFWInstance_verifyPointer(fwInstance);
973 if (CKR_OK != error) {
974 return error;
975 }
976 #endif /* NSSDEBUG */
977
978 error = nssCKFWMutex_Lock(fwInstance->mutex);
979 if (CKR_OK != error) {
980 return error;
981 }
982
983 if (!fwInstance->libraryDescription) {
984 if (fwInstance->mdInstance->GetLibraryDescription) {
985 fwInstance->libraryDescription = fwInstance->mdInstance->GetLibraryD escription(
986 fwInstance->mdInstance, fwInstance, &error);
987 if ((!fwInstance->libraryDescription) && (CKR_OK != error)) {
988 goto done;
989 }
990 } else {
991 fwInstance->libraryDescription = (NSSUTF8 *)"";
992 }
993 }
994
995 (void)nssUTF8_CopyIntoFixedBuffer(fwInstance->libraryDescription, (char *)li braryDescription, 32, ' ');
996 error = CKR_OK;
997
998 done:
999 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
1000 return error;
1001 }
1002
1003 /*
1004 * nssCKFWInstance_GetLibraryVersion
1005 *
1006 */
1007 NSS_IMPLEMENT CK_VERSION
1008 nssCKFWInstance_GetLibraryVersion(
1009 NSSCKFWInstance *fwInstance)
1010 {
1011 CK_VERSION rv;
1012
1013 #ifdef NSSDEBUG
1014 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
1015 rv.major = rv.minor = 0;
1016 return rv;
1017 }
1018 #endif /* NSSDEBUG */
1019
1020 if (CKR_OK != nssCKFWMutex_Lock(fwInstance->mutex)) {
1021 rv.major = rv.minor = 0;
1022 return rv;
1023 }
1024
1025 if ((0 != fwInstance->libraryVersion.major) ||
1026 (0 != fwInstance->libraryVersion.minor)) {
1027 rv = fwInstance->libraryVersion;
1028 goto done;
1029 }
1030
1031 if (fwInstance->mdInstance->GetLibraryVersion) {
1032 fwInstance->libraryVersion = fwInstance->mdInstance->GetLibraryVersion(
1033 fwInstance->mdInstance, fwInstance);
1034 } else {
1035 fwInstance->libraryVersion.major = 0;
1036 fwInstance->libraryVersion.minor = 3;
1037 }
1038
1039 rv = fwInstance->libraryVersion;
1040 done:
1041 (void)nssCKFWMutex_Unlock(fwInstance->mutex);
1042 return rv;
1043 }
1044
1045 /*
1046 * nssCKFWInstance_GetModuleHandlesSessionObjects
1047 *
1048 */
1049 NSS_IMPLEMENT CK_BBOOL
1050 nssCKFWInstance_GetModuleHandlesSessionObjects(
1051 NSSCKFWInstance *fwInstance)
1052 {
1053 #ifdef NSSDEBUG
1054 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
1055 return CK_FALSE;
1056 }
1057 #endif /* NSSDEBUG */
1058
1059 return fwInstance->moduleHandlesSessionObjects;
1060 }
1061
1062 /*
1063 * nssCKFWInstance_GetSlots
1064 *
1065 */
1066 NSS_IMPLEMENT NSSCKFWSlot **
1067 nssCKFWInstance_GetSlots(
1068 NSSCKFWInstance *fwInstance,
1069 CK_RV *pError)
1070 {
1071 #ifdef NSSDEBUG
1072 if (!pError) {
1073 return (NSSCKFWSlot **)NULL;
1074 }
1075
1076 *pError = nssCKFWInstance_verifyPointer(fwInstance);
1077 if (CKR_OK != *pError) {
1078 return (NSSCKFWSlot **)NULL;
1079 }
1080 #endif /* NSSDEBUG */
1081
1082 return fwInstance->fwSlotList;
1083 }
1084
1085 /*
1086 * nssCKFWInstance_WaitForSlotEvent
1087 *
1088 */
1089 NSS_IMPLEMENT NSSCKFWSlot *
1090 nssCKFWInstance_WaitForSlotEvent(
1091 NSSCKFWInstance *fwInstance,
1092 CK_BBOOL block,
1093 CK_RV *pError)
1094 {
1095 NSSCKFWSlot *fwSlot = (NSSCKFWSlot *)NULL;
1096 NSSCKMDSlot *mdSlot;
1097 CK_ULONG i, n;
1098
1099 #ifdef NSSDEBUG
1100 if (!pError) {
1101 return (NSSCKFWSlot *)NULL;
1102 }
1103
1104 *pError = nssCKFWInstance_verifyPointer(fwInstance);
1105 if (CKR_OK != *pError) {
1106 return (NSSCKFWSlot *)NULL;
1107 }
1108
1109 switch (block) {
1110 case CK_TRUE:
1111 case CK_FALSE:
1112 break;
1113 default:
1114 *pError = CKR_ARGUMENTS_BAD;
1115 return (NSSCKFWSlot *)NULL;
1116 }
1117 #endif /* NSSDEBUG */
1118
1119 if (!fwInstance->mdInstance->WaitForSlotEvent) {
1120 *pError = CKR_NO_EVENT;
1121 return (NSSCKFWSlot *)NULL;
1122 }
1123
1124 mdSlot = fwInstance->mdInstance->WaitForSlotEvent(
1125 fwInstance->mdInstance,
1126 fwInstance,
1127 block,
1128 pError);
1129
1130 if (!mdSlot) {
1131 return (NSSCKFWSlot *)NULL;
1132 }
1133
1134 n = nssCKFWInstance_GetNSlots(fwInstance, pError);
1135 if (((CK_ULONG)0 == n) && (CKR_OK != *pError)) {
1136 return (NSSCKFWSlot *)NULL;
1137 }
1138
1139 for (i = 0; i < n; i++) {
1140 if (fwInstance->mdSlotList[i] == mdSlot) {
1141 fwSlot = fwInstance->fwSlotList[i];
1142 break;
1143 }
1144 }
1145
1146 if (!fwSlot) {
1147 /* Internal error */
1148 *pError = CKR_GENERAL_ERROR;
1149 return (NSSCKFWSlot *)NULL;
1150 }
1151
1152 return fwSlot;
1153 }
1154
1155 /*
1156 * NSSCKFWInstance_GetMDInstance
1157 *
1158 */
1159 NSS_IMPLEMENT NSSCKMDInstance *
1160 NSSCKFWInstance_GetMDInstance(
1161 NSSCKFWInstance *fwInstance)
1162 {
1163 #ifdef DEBUG
1164 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
1165 return (NSSCKMDInstance *)NULL;
1166 }
1167 #endif /* DEBUG */
1168
1169 return nssCKFWInstance_GetMDInstance(fwInstance);
1170 }
1171
1172 /*
1173 * NSSCKFWInstance_GetArena
1174 *
1175 */
1176 NSS_IMPLEMENT NSSArena *
1177 NSSCKFWInstance_GetArena(
1178 NSSCKFWInstance *fwInstance,
1179 CK_RV *pError)
1180 {
1181 #ifdef DEBUG
1182 if (!pError) {
1183 return (NSSArena *)NULL;
1184 }
1185
1186 *pError = nssCKFWInstance_verifyPointer(fwInstance);
1187 if (CKR_OK != *pError) {
1188 return (NSSArena *)NULL;
1189 }
1190 #endif /* DEBUG */
1191
1192 return nssCKFWInstance_GetArena(fwInstance, pError);
1193 }
1194
1195 /*
1196 * NSSCKFWInstance_MayCreatePthreads
1197 *
1198 */
1199 NSS_IMPLEMENT CK_BBOOL
1200 NSSCKFWInstance_MayCreatePthreads(
1201 NSSCKFWInstance *fwInstance)
1202 {
1203 #ifdef DEBUG
1204 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
1205 return CK_FALSE;
1206 }
1207 #endif /* DEBUG */
1208
1209 return nssCKFWInstance_MayCreatePthreads(fwInstance);
1210 }
1211
1212 /*
1213 * NSSCKFWInstance_CreateMutex
1214 *
1215 */
1216 NSS_IMPLEMENT NSSCKFWMutex *
1217 NSSCKFWInstance_CreateMutex(
1218 NSSCKFWInstance *fwInstance,
1219 NSSArena *arena,
1220 CK_RV *pError)
1221 {
1222 #ifdef DEBUG
1223 if (!pError) {
1224 return (NSSCKFWMutex *)NULL;
1225 }
1226
1227 *pError = nssCKFWInstance_verifyPointer(fwInstance);
1228 if (CKR_OK != *pError) {
1229 return (NSSCKFWMutex *)NULL;
1230 }
1231 #endif /* DEBUG */
1232
1233 return nssCKFWInstance_CreateMutex(fwInstance, arena, pError);
1234 }
1235
1236 /*
1237 * NSSCKFWInstance_GetConfigurationData
1238 *
1239 */
1240 NSS_IMPLEMENT NSSUTF8 *
1241 NSSCKFWInstance_GetConfigurationData(
1242 NSSCKFWInstance *fwInstance)
1243 {
1244 #ifdef DEBUG
1245 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
1246 return (NSSUTF8 *)NULL;
1247 }
1248 #endif /* DEBUG */
1249
1250 return nssCKFWInstance_GetConfigurationData(fwInstance);
1251 }
1252
1253 /*
1254 * NSSCKFWInstance_GetInitArgs
1255 *
1256 */
1257 NSS_IMPLEMENT CK_C_INITIALIZE_ARGS_PTR
1258 NSSCKFWInstance_GetInitArgs(
1259 NSSCKFWInstance *fwInstance)
1260 {
1261 #ifdef DEBUG
1262 if (CKR_OK != nssCKFWInstance_verifyPointer(fwInstance)) {
1263 return (CK_C_INITIALIZE_ARGS_PTR)NULL;
1264 }
1265 #endif /* DEBUG */
1266
1267 return nssCKFWInstance_GetInitArgs(fwInstance);
1268 }
OLDNEW
« no previous file with comments | « nss/lib/ckfw/hash.c ('k') | nss/lib/ckfw/mechanism.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698