/
pk11slot.c
2572 lines (2303 loc) · 75.1 KB
/
pk11slot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Deal with PKCS #11 Slots.
*/
#include "seccomon.h"
#include "secmod.h"
#include "nssilock.h"
#include "secmodi.h"
#include "secmodti.h"
#include "pkcs11t.h"
#include "pk11func.h"
#include "secitem.h"
#include "secerr.h"
#include "dev.h"
#include "dev3hack.h"
#include "pkim.h"
#include "utilpars.h"
#include "pkcs11uri.h"
/*************************************************************
* local static and global data
*************************************************************/
/*
* This array helps parsing between names, mechanisms, and flags.
* to make the config files understand more entries, add them
* to this table.
*/
const PK11DefaultArrayEntry PK11_DefaultArray[] = {
{ "RSA", SECMOD_RSA_FLAG, CKM_RSA_PKCS },
{ "DSA", SECMOD_DSA_FLAG, CKM_DSA },
{ "ECC", SECMOD_ECC_FLAG, CKM_ECDSA },
{ "DH", SECMOD_DH_FLAG, CKM_DH_PKCS_DERIVE },
{ "RC2", SECMOD_RC2_FLAG, CKM_RC2_CBC },
{ "RC4", SECMOD_RC4_FLAG, CKM_RC4 },
{ "DES", SECMOD_DES_FLAG, CKM_DES_CBC },
{ "AES", SECMOD_AES_FLAG, CKM_AES_CBC },
{ "Camellia", SECMOD_CAMELLIA_FLAG, CKM_CAMELLIA_CBC },
{ "SEED", SECMOD_SEED_FLAG, CKM_SEED_CBC },
{ "RC5", SECMOD_RC5_FLAG, CKM_RC5_CBC },
{ "SHA-1", SECMOD_SHA1_FLAG, CKM_SHA_1 },
/* { "SHA224", SECMOD_SHA256_FLAG, CKM_SHA224 }, */
{ "SHA256", SECMOD_SHA256_FLAG, CKM_SHA256 },
/* { "SHA384", SECMOD_SHA512_FLAG, CKM_SHA384 }, */
{ "SHA512", SECMOD_SHA512_FLAG, CKM_SHA512 },
{ "MD5", SECMOD_MD5_FLAG, CKM_MD5 },
{ "MD2", SECMOD_MD2_FLAG, CKM_MD2 },
{ "SSL", SECMOD_SSL_FLAG, CKM_SSL3_PRE_MASTER_KEY_GEN },
{ "TLS", SECMOD_TLS_FLAG, CKM_TLS_MASTER_KEY_DERIVE },
{ "SKIPJACK", SECMOD_FORTEZZA_FLAG, CKM_SKIPJACK_CBC64 },
{ "Publicly-readable certs", SECMOD_FRIENDLY_FLAG, CKM_INVALID_MECHANISM },
{ "Random Num Generator", SECMOD_RANDOM_FLAG, CKM_FAKE_RANDOM },
};
const int num_pk11_default_mechanisms =
sizeof(PK11_DefaultArray) / sizeof(PK11_DefaultArray[0]);
const PK11DefaultArrayEntry *
PK11_GetDefaultArray(int *size)
{
if (size) {
*size = num_pk11_default_mechanisms;
}
return PK11_DefaultArray;
}
/*
* These slotlists are lists of modules which provide default support for
* a given algorithm or mechanism.
*/
static PK11SlotList
pk11_seedSlotList,
pk11_camelliaSlotList,
pk11_aesSlotList,
pk11_desSlotList,
pk11_rc4SlotList,
pk11_rc2SlotList,
pk11_rc5SlotList,
pk11_sha1SlotList,
pk11_md5SlotList,
pk11_md2SlotList,
pk11_rsaSlotList,
pk11_dsaSlotList,
pk11_dhSlotList,
pk11_ecSlotList,
pk11_ideaSlotList,
pk11_sslSlotList,
pk11_tlsSlotList,
pk11_randomSlotList,
pk11_sha256SlotList,
pk11_sha512SlotList; /* slots do SHA512 and SHA384 */
/************************************************************
* Generic Slot List and Slot List element manipulations
************************************************************/
/*
* allocate a new list
*/
PK11SlotList *
PK11_NewSlotList(void)
{
PK11SlotList *list;
list = (PK11SlotList *)PORT_Alloc(sizeof(PK11SlotList));
if (list == NULL)
return NULL;
list->head = NULL;
list->tail = NULL;
list->lock = PZ_NewLock(nssILockList);
if (list->lock == NULL) {
PORT_Free(list);
return NULL;
}
return list;
}
/*
* free a list element when all the references go away.
*/
SECStatus
PK11_FreeSlotListElement(PK11SlotList *list, PK11SlotListElement *le)
{
PRBool freeit = PR_FALSE;
if (list == NULL || le == NULL) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
PZ_Lock(list->lock);
if (le->refCount-- == 1) {
freeit = PR_TRUE;
}
PZ_Unlock(list->lock);
if (freeit) {
PK11_FreeSlot(le->slot);
PORT_Free(le);
}
return SECSuccess;
}
static void
pk11_FreeSlotListStatic(PK11SlotList *list)
{
PK11SlotListElement *le, *next;
if (list == NULL)
return;
for (le = list->head; le; le = next) {
next = le->next;
PK11_FreeSlotListElement(list, le);
}
if (list->lock) {
PZ_DestroyLock(list->lock);
}
list->lock = NULL;
list->head = NULL;
}
/*
* if we are freeing the list, we must be the only ones with a pointer
* to the list.
*/
void
PK11_FreeSlotList(PK11SlotList *list)
{
pk11_FreeSlotListStatic(list);
PORT_Free(list);
}
/*
* add a slot to a list
* "slot" is the slot to be added. Ownership is not transferred.
* "sorted" indicates whether or not the slot should be inserted according to
* cipherOrder of the associated module. PR_FALSE indicates that the slot
* should be inserted to the head of the list.
*/
SECStatus
PK11_AddSlotToList(PK11SlotList *list, PK11SlotInfo *slot, PRBool sorted)
{
PK11SlotListElement *le;
PK11SlotListElement *element;
le = (PK11SlotListElement *)PORT_Alloc(sizeof(PK11SlotListElement));
if (le == NULL)
return SECFailure;
le->slot = PK11_ReferenceSlot(slot);
le->prev = NULL;
le->refCount = 1;
PZ_Lock(list->lock);
element = list->head;
/* Insertion sort, with higher cipherOrders are sorted first in the list */
while (element && sorted && (element->slot->module->cipherOrder >
le->slot->module->cipherOrder)) {
element = element->next;
}
if (element) {
le->prev = element->prev;
element->prev = le;
le->next = element;
} else {
le->prev = list->tail;
le->next = NULL;
list->tail = le;
}
if (le->prev)
le->prev->next = le;
if (list->head == element)
list->head = le;
PZ_Unlock(list->lock);
return SECSuccess;
}
/*
* remove a slot entry from the list
*/
SECStatus
PK11_DeleteSlotFromList(PK11SlotList *list, PK11SlotListElement *le)
{
PZ_Lock(list->lock);
if (le->prev)
le->prev->next = le->next;
else
list->head = le->next;
if (le->next)
le->next->prev = le->prev;
else
list->tail = le->prev;
le->next = le->prev = NULL;
PZ_Unlock(list->lock);
PK11_FreeSlotListElement(list, le);
return SECSuccess;
}
/*
* Move a list to the end of the target list.
* NOTE: There is no locking here... This assumes BOTH lists are private copy
* lists. It also does not re-sort the target list.
*/
SECStatus
pk11_MoveListToList(PK11SlotList *target, PK11SlotList *src)
{
if (src->head == NULL)
return SECSuccess;
if (target->tail == NULL) {
target->head = src->head;
} else {
target->tail->next = src->head;
}
src->head->prev = target->tail;
target->tail = src->tail;
src->head = src->tail = NULL;
return SECSuccess;
}
/*
* get an element from the list with a reference. You must own the list.
*/
PK11SlotListElement *
PK11_GetFirstRef(PK11SlotList *list)
{
PK11SlotListElement *le;
le = list->head;
if (le != NULL)
(le)->refCount++;
return le;
}
/*
* get the next element from the list with a reference. You must own the list.
*/
PK11SlotListElement *
PK11_GetNextRef(PK11SlotList *list, PK11SlotListElement *le, PRBool restart)
{
PK11SlotListElement *new_le;
new_le = le->next;
if (new_le)
new_le->refCount++;
PK11_FreeSlotListElement(list, le);
return new_le;
}
/*
* get an element safely from the list. This just makes sure that if
* this element is not deleted while we deal with it.
*/
PK11SlotListElement *
PK11_GetFirstSafe(PK11SlotList *list)
{
PK11SlotListElement *le;
PZ_Lock(list->lock);
le = list->head;
if (le != NULL)
(le)->refCount++;
PZ_Unlock(list->lock);
return le;
}
/*
* NOTE: if this element gets deleted, we can no longer safely traverse using
* it's pointers. We can either terminate the loop, or restart from the
* beginning. This is controlled by the restart option.
*/
PK11SlotListElement *
PK11_GetNextSafe(PK11SlotList *list, PK11SlotListElement *le, PRBool restart)
{
PK11SlotListElement *new_le;
PZ_Lock(list->lock);
new_le = le->next;
if (le->next == NULL) {
/* if the prev and next fields are NULL then either this element
* has been removed and we need to walk the list again (if restart
* is true) or this was the only element on the list */
if ((le->prev == NULL) && restart && (list->head != le)) {
new_le = list->head;
}
}
if (new_le)
new_le->refCount++;
PZ_Unlock(list->lock);
PK11_FreeSlotListElement(list, le);
return new_le;
}
/*
* Find the element that holds this slot
*/
PK11SlotListElement *
PK11_FindSlotElement(PK11SlotList *list, PK11SlotInfo *slot)
{
PK11SlotListElement *le;
for (le = PK11_GetFirstSafe(list); le;
le = PK11_GetNextSafe(list, le, PR_TRUE)) {
if (le->slot == slot)
return le;
}
return NULL;
}
/************************************************************
* Generic Slot Utilities
************************************************************/
/*
* Create a new slot structure
*/
PK11SlotInfo *
PK11_NewSlotInfo(SECMODModule *mod)
{
PK11SlotInfo *slot;
slot = (PK11SlotInfo *)PORT_Alloc(sizeof(PK11SlotInfo));
if (slot == NULL)
return slot;
slot->sessionLock = mod->isThreadSafe ? PZ_NewLock(nssILockSession) : mod->refLock;
if (slot->sessionLock == NULL) {
PORT_Free(slot);
return NULL;
}
slot->freeListLock = PZ_NewLock(nssILockFreelist);
if (slot->freeListLock == NULL) {
if (mod->isThreadSafe) {
PZ_DestroyLock(slot->sessionLock);
}
PORT_Free(slot);
return NULL;
}
slot->freeSymKeysWithSessionHead = NULL;
slot->freeSymKeysHead = NULL;
slot->keyCount = 0;
slot->maxKeyCount = 0;
slot->functionList = NULL;
slot->needTest = PR_TRUE;
slot->isPerm = PR_FALSE;
slot->isHW = PR_FALSE;
slot->isInternal = PR_FALSE;
slot->isThreadSafe = PR_FALSE;
slot->disabled = PR_FALSE;
slot->series = 1;
slot->wrapKey = 0;
slot->wrapMechanism = CKM_INVALID_MECHANISM;
slot->refKeys[0] = CK_INVALID_HANDLE;
slot->reason = PK11_DIS_NONE;
slot->readOnly = PR_TRUE;
slot->needLogin = PR_FALSE;
slot->hasRandom = PR_FALSE;
slot->defRWSession = PR_FALSE;
slot->protectedAuthPath = PR_FALSE;
slot->flags = 0;
slot->session = CK_INVALID_SESSION;
slot->slotID = 0;
slot->defaultFlags = 0;
slot->refCount = 1;
slot->askpw = 0;
slot->timeout = 0;
slot->mechanismList = NULL;
slot->mechanismCount = 0;
slot->cert_array = NULL;
slot->cert_count = 0;
slot->slot_name[0] = 0;
slot->token_name[0] = 0;
PORT_Memset(slot->serial, ' ', sizeof(slot->serial));
PORT_Memset(&slot->tokenInfo, 0, sizeof(slot->tokenInfo));
slot->module = NULL;
slot->authTransact = 0;
slot->authTime = LL_ZERO;
slot->minPassword = 0;
slot->maxPassword = 0;
slot->hasRootCerts = PR_FALSE;
slot->hasRootTrust = PR_FALSE;
slot->nssToken = NULL;
return slot;
}
/* create a new reference to a slot so it doesn't go away */
PK11SlotInfo *
PK11_ReferenceSlot(PK11SlotInfo *slot)
{
PR_ATOMIC_INCREMENT(&slot->refCount);
return slot;
}
/* Destroy all info on a slot we have built up */
void
PK11_DestroySlot(PK11SlotInfo *slot)
{
/* free up the cached keys and sessions */
PK11_CleanKeyList(slot);
/* free up all the sessions on this slot */
if (slot->functionList) {
PK11_GETTAB(slot)
->C_CloseAllSessions(slot->slotID);
}
if (slot->mechanismList) {
PORT_Free(slot->mechanismList);
}
if (slot->isThreadSafe && slot->sessionLock) {
PZ_DestroyLock(slot->sessionLock);
}
slot->sessionLock = NULL;
if (slot->freeListLock) {
PZ_DestroyLock(slot->freeListLock);
slot->freeListLock = NULL;
}
/* finally Tell our parent module that we've gone away so it can unload */
if (slot->module) {
SECMOD_SlotDestroyModule(slot->module, PR_TRUE);
}
/* ok, well not quit finally... now we free the memory */
PORT_Free(slot);
}
/* We're all done with the slot, free it */
void
PK11_FreeSlot(PK11SlotInfo *slot)
{
if (PR_ATOMIC_DECREMENT(&slot->refCount) == 0) {
PK11_DestroySlot(slot);
}
}
void
PK11_EnterSlotMonitor(PK11SlotInfo *slot)
{
PZ_Lock(slot->sessionLock);
}
void
PK11_ExitSlotMonitor(PK11SlotInfo *slot)
{
PZ_Unlock(slot->sessionLock);
}
/***********************************************************
* Functions to find specific slots.
***********************************************************/
PRBool
SECMOD_HasRootCerts(void)
{
SECMODModuleList *mlp;
SECMODModuleList *modules;
SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock();
int i;
PRBool found = PR_FALSE;
if (!moduleLock) {
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
return found;
}
/* work through all the slots */
SECMOD_GetReadLock(moduleLock);
modules = SECMOD_GetDefaultModuleList();
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
for (i = 0; i < mlp->module->slotCount; i++) {
PK11SlotInfo *tmpSlot = mlp->module->slots[i];
if (PK11_IsPresent(tmpSlot)) {
if (tmpSlot->hasRootCerts) {
found = PR_TRUE;
break;
}
}
}
if (found)
break;
}
SECMOD_ReleaseReadLock(moduleLock);
return found;
}
/***********************************************************
* Functions to find specific slots.
***********************************************************/
PK11SlotList *
PK11_FindSlotsByNames(const char *dllName, const char *slotName,
const char *tokenName, PRBool presentOnly)
{
SECMODModuleList *mlp;
SECMODModuleList *modules;
SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock();
int i;
PK11SlotList *slotList = NULL;
PRUint32 slotcount = 0;
SECStatus rv = SECSuccess;
if (!moduleLock) {
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
return slotList;
}
slotList = PK11_NewSlotList();
if (!slotList) {
PORT_SetError(SEC_ERROR_NO_MEMORY);
return slotList;
}
if (((NULL == dllName) || (0 == *dllName)) &&
((NULL == slotName) || (0 == *slotName)) &&
((NULL == tokenName) || (0 == *tokenName))) {
/* default to softoken */
/* PK11_GetInternalKeySlot increments the refcount on the internal slot,
* but so does PK11_AddSlotToList. To avoid erroneously increasing the
* refcount twice, we get our own reference to the internal slot and
* decrement its refcount when we're done with it. */
PK11SlotInfo *internalKeySlot = PK11_GetInternalKeySlot();
PK11_AddSlotToList(slotList, internalKeySlot, PR_TRUE);
PK11_FreeSlot(internalKeySlot);
return slotList;
}
/* work through all the slots */
SECMOD_GetReadLock(moduleLock);
modules = SECMOD_GetDefaultModuleList();
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
PORT_Assert(mlp->module);
if (!mlp->module) {
rv = SECFailure;
break;
}
if ((!dllName) || (mlp->module->dllName &&
(0 == PORT_Strcmp(mlp->module->dllName, dllName)))) {
for (i = 0; i < mlp->module->slotCount; i++) {
PK11SlotInfo *tmpSlot = (mlp->module->slots ? mlp->module->slots[i] : NULL);
PORT_Assert(tmpSlot);
if (!tmpSlot) {
rv = SECFailure;
break;
}
if ((PR_FALSE == presentOnly || PK11_IsPresent(tmpSlot)) &&
((!tokenName) ||
(0 == PORT_Strcmp(tmpSlot->token_name, tokenName))) &&
((!slotName) ||
(0 == PORT_Strcmp(tmpSlot->slot_name, slotName)))) {
PK11_AddSlotToList(slotList, tmpSlot, PR_TRUE);
slotcount++;
}
}
}
}
SECMOD_ReleaseReadLock(moduleLock);
if ((0 == slotcount) || (SECFailure == rv)) {
PORT_SetError(SEC_ERROR_NO_TOKEN);
PK11_FreeSlotList(slotList);
slotList = NULL;
}
if (SECFailure == rv) {
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
}
return slotList;
}
PK11SlotInfo *
PK11_FindSlotByName(const char *name)
{
SECMODModuleList *mlp;
SECMODModuleList *modules;
SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock();
int i;
PK11SlotInfo *slot = NULL;
if (!moduleLock) {
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
return slot;
}
if ((name == NULL) || (*name == 0)) {
return PK11_GetInternalKeySlot();
}
/* work through all the slots */
SECMOD_GetReadLock(moduleLock);
modules = SECMOD_GetDefaultModuleList();
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
for (i = 0; i < mlp->module->slotCount; i++) {
PK11SlotInfo *tmpSlot = mlp->module->slots[i];
if (PK11_IsPresent(tmpSlot)) {
if (PORT_Strcmp(tmpSlot->token_name, name) == 0) {
slot = PK11_ReferenceSlot(tmpSlot);
break;
}
}
}
if (slot != NULL)
break;
}
SECMOD_ReleaseReadLock(moduleLock);
if (slot == NULL) {
PORT_SetError(SEC_ERROR_NO_TOKEN);
}
return slot;
}
PK11SlotInfo *
PK11_FindSlotBySerial(char *serial)
{
SECMODModuleList *mlp;
SECMODModuleList *modules;
SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock();
int i;
PK11SlotInfo *slot = NULL;
if (!moduleLock) {
PORT_SetError(SEC_ERROR_NOT_INITIALIZED);
return slot;
}
/* work through all the slots */
SECMOD_GetReadLock(moduleLock);
modules = SECMOD_GetDefaultModuleList();
for (mlp = modules; mlp != NULL; mlp = mlp->next) {
for (i = 0; i < mlp->module->slotCount; i++) {
PK11SlotInfo *tmpSlot = mlp->module->slots[i];
if (PK11_IsPresent(tmpSlot)) {
if (PORT_Memcmp(tmpSlot->serial, serial,
sizeof(tmpSlot->serial)) == 0) {
slot = PK11_ReferenceSlot(tmpSlot);
break;
}
}
}
if (slot != NULL)
break;
}
SECMOD_ReleaseReadLock(moduleLock);
if (slot == NULL) {
PORT_SetError(SEC_ERROR_NO_TOKEN);
}
return slot;
}
/*
* notification stub. If we ever get interested in any events that
* the pkcs11 functions may pass back to use, we can catch them here...
* currently pdata is a slotinfo structure.
*/
CK_RV
pk11_notify(CK_SESSION_HANDLE session, CK_NOTIFICATION event,
CK_VOID_PTR pdata)
{
return CKR_OK;
}
/*
* grab a new RW session
* !!! has a side effect of grabbing the Monitor if either the slot's default
* session is RW or the slot is not thread safe. Monitor is release in function
* below
*/
CK_SESSION_HANDLE
PK11_GetRWSession(PK11SlotInfo *slot)
{
CK_SESSION_HANDLE rwsession;
CK_RV crv;
PRBool haveMonitor = PR_FALSE;
if (!slot->isThreadSafe || slot->defRWSession) {
PK11_EnterSlotMonitor(slot);
haveMonitor = PR_TRUE;
}
if (slot->defRWSession) {
PORT_Assert(slot->session != CK_INVALID_SESSION);
if (slot->session != CK_INVALID_SESSION)
return slot->session;
}
crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID,
CKF_RW_SESSION | CKF_SERIAL_SESSION,
slot, pk11_notify, &rwsession);
PORT_Assert(rwsession != CK_INVALID_SESSION || crv != CKR_OK);
if (crv != CKR_OK || rwsession == CK_INVALID_SESSION) {
if (crv == CKR_OK)
crv = CKR_DEVICE_ERROR;
if (haveMonitor)
PK11_ExitSlotMonitor(slot);
PORT_SetError(PK11_MapError(crv));
return CK_INVALID_SESSION;
}
if (slot->defRWSession) { /* we have the monitor */
slot->session = rwsession;
}
return rwsession;
}
PRBool
PK11_RWSessionHasLock(PK11SlotInfo *slot, CK_SESSION_HANDLE session_handle)
{
PRBool hasLock;
hasLock = (PRBool)(!slot->isThreadSafe ||
(slot->defRWSession && slot->session != CK_INVALID_SESSION));
return hasLock;
}
static PRBool
pk11_RWSessionIsDefault(PK11SlotInfo *slot, CK_SESSION_HANDLE rwsession)
{
PRBool isDefault;
isDefault = (PRBool)(slot->session == rwsession &&
slot->defRWSession &&
slot->session != CK_INVALID_SESSION);
return isDefault;
}
/*
* close the rwsession and restore our readonly session
* !!! has a side effect of releasing the Monitor if either the slot's default
* session is RW or the slot is not thread safe.
*/
void
PK11_RestoreROSession(PK11SlotInfo *slot, CK_SESSION_HANDLE rwsession)
{
PORT_Assert(rwsession != CK_INVALID_SESSION);
if (rwsession != CK_INVALID_SESSION) {
PRBool doExit = PK11_RWSessionHasLock(slot, rwsession);
if (!pk11_RWSessionIsDefault(slot, rwsession))
PK11_GETTAB(slot)
->C_CloseSession(rwsession);
if (doExit)
PK11_ExitSlotMonitor(slot);
}
}
/************************************************************
* Manage the built-In Slot Lists
************************************************************/
/* Init the static built int slot list (should actually integrate
* with PK11_NewSlotList */
static void
pk11_InitSlotListStatic(PK11SlotList *list)
{
list->lock = PZ_NewLock(nssILockList);
list->head = NULL;
}
/* initialize the system slotlists */
SECStatus
PK11_InitSlotLists(void)
{
pk11_InitSlotListStatic(&pk11_seedSlotList);
pk11_InitSlotListStatic(&pk11_camelliaSlotList);
pk11_InitSlotListStatic(&pk11_aesSlotList);
pk11_InitSlotListStatic(&pk11_desSlotList);
pk11_InitSlotListStatic(&pk11_rc4SlotList);
pk11_InitSlotListStatic(&pk11_rc2SlotList);
pk11_InitSlotListStatic(&pk11_rc5SlotList);
pk11_InitSlotListStatic(&pk11_md5SlotList);
pk11_InitSlotListStatic(&pk11_md2SlotList);
pk11_InitSlotListStatic(&pk11_sha1SlotList);
pk11_InitSlotListStatic(&pk11_rsaSlotList);
pk11_InitSlotListStatic(&pk11_dsaSlotList);
pk11_InitSlotListStatic(&pk11_dhSlotList);
pk11_InitSlotListStatic(&pk11_ecSlotList);
pk11_InitSlotListStatic(&pk11_ideaSlotList);
pk11_InitSlotListStatic(&pk11_sslSlotList);
pk11_InitSlotListStatic(&pk11_tlsSlotList);
pk11_InitSlotListStatic(&pk11_randomSlotList);
pk11_InitSlotListStatic(&pk11_sha256SlotList);
pk11_InitSlotListStatic(&pk11_sha512SlotList);
return SECSuccess;
}
void
PK11_DestroySlotLists(void)
{
pk11_FreeSlotListStatic(&pk11_seedSlotList);
pk11_FreeSlotListStatic(&pk11_camelliaSlotList);
pk11_FreeSlotListStatic(&pk11_aesSlotList);
pk11_FreeSlotListStatic(&pk11_desSlotList);
pk11_FreeSlotListStatic(&pk11_rc4SlotList);
pk11_FreeSlotListStatic(&pk11_rc2SlotList);
pk11_FreeSlotListStatic(&pk11_rc5SlotList);
pk11_FreeSlotListStatic(&pk11_md5SlotList);
pk11_FreeSlotListStatic(&pk11_md2SlotList);
pk11_FreeSlotListStatic(&pk11_sha1SlotList);
pk11_FreeSlotListStatic(&pk11_rsaSlotList);
pk11_FreeSlotListStatic(&pk11_dsaSlotList);
pk11_FreeSlotListStatic(&pk11_dhSlotList);
pk11_FreeSlotListStatic(&pk11_ecSlotList);
pk11_FreeSlotListStatic(&pk11_ideaSlotList);
pk11_FreeSlotListStatic(&pk11_sslSlotList);
pk11_FreeSlotListStatic(&pk11_tlsSlotList);
pk11_FreeSlotListStatic(&pk11_randomSlotList);
pk11_FreeSlotListStatic(&pk11_sha256SlotList);
pk11_FreeSlotListStatic(&pk11_sha512SlotList);
return;
}
/* return a system slot list based on mechanism */
PK11SlotList *
PK11_GetSlotList(CK_MECHANISM_TYPE type)
{
/* XXX a workaround for Bugzilla bug #55267 */
#if defined(HPUX) && defined(__LP64__)
if (CKM_INVALID_MECHANISM == type)
return NULL;
#endif
switch (type) {
case CKM_SEED_CBC:
case CKM_SEED_ECB:
return &pk11_seedSlotList;
case CKM_CAMELLIA_CBC:
case CKM_CAMELLIA_ECB:
return &pk11_camelliaSlotList;
case CKM_AES_CBC:
case CKM_AES_CCM:
case CKM_AES_CTR:
case CKM_AES_CTS:
case CKM_AES_GCM:
case CKM_AES_ECB:
return &pk11_aesSlotList;
case CKM_DES_CBC:
case CKM_DES_ECB:
case CKM_DES3_ECB:
case CKM_DES3_CBC:
return &pk11_desSlotList;
case CKM_RC4:
return &pk11_rc4SlotList;
case CKM_RC5_CBC:
return &pk11_rc5SlotList;
case CKM_SHA_1:
return &pk11_sha1SlotList;
case CKM_SHA224:
case CKM_SHA256:
return &pk11_sha256SlotList;
case CKM_SHA384:
case CKM_SHA512:
return &pk11_sha512SlotList;
case CKM_MD5:
return &pk11_md5SlotList;
case CKM_MD2:
return &pk11_md2SlotList;
case CKM_RC2_ECB:
case CKM_RC2_CBC:
return &pk11_rc2SlotList;
case CKM_RSA_PKCS:
case CKM_RSA_PKCS_KEY_PAIR_GEN:
case CKM_RSA_X_509:
return &pk11_rsaSlotList;
case CKM_DSA:
return &pk11_dsaSlotList;
case CKM_DH_PKCS_KEY_PAIR_GEN:
case CKM_DH_PKCS_DERIVE:
return &pk11_dhSlotList;
case CKM_ECDSA:
case CKM_ECDSA_SHA1:
case CKM_EC_KEY_PAIR_GEN: /* aka CKM_ECDSA_KEY_PAIR_GEN */
case CKM_ECDH1_DERIVE:
return &pk11_ecSlotList;
case CKM_SSL3_PRE_MASTER_KEY_GEN:
case CKM_SSL3_MASTER_KEY_DERIVE:
case CKM_SSL3_SHA1_MAC:
case CKM_SSL3_MD5_MAC:
return &pk11_sslSlotList;
case CKM_TLS_MASTER_KEY_DERIVE:
case CKM_TLS_KEY_AND_MAC_DERIVE:
case CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256:
return &pk11_tlsSlotList;
case CKM_IDEA_CBC:
case CKM_IDEA_ECB:
return &pk11_ideaSlotList;
case CKM_FAKE_RANDOM:
return &pk11_randomSlotList;
}
return NULL;
}
/*
* load the static SlotInfo structures used to select a PKCS11 slot.
* preSlotInfo has a list of all the default flags for the slots on this
* module.
*/
void
PK11_LoadSlotList(PK11SlotInfo *slot, PK11PreSlotInfo *psi, int count)
{
int i;
for (i = 0; i < count; i++) {
if (psi[i].slotID == slot->slotID)
break;
}
if (i == count)
return;
slot->defaultFlags = psi[i].defaultFlags;
slot->askpw = psi[i].askpw;
slot->timeout = psi[i].timeout;
slot->hasRootCerts = psi[i].hasRootCerts;
/* if the slot is already disabled, don't load them into the
* default slot lists. We get here so we can save the default
* list value. */
if (slot->disabled)
return;
/* if the user has disabled us, don't load us in */
if (slot->defaultFlags & PK11_DISABLE_FLAG) {
slot->disabled = PR_TRUE;
slot->reason = PK11_DIS_USER_SELECTED;
/* free up sessions and things?? */
return;
}
for (i = 0; i < num_pk11_default_mechanisms; i++) {
if (slot->defaultFlags & PK11_DefaultArray[i].flag) {
CK_MECHANISM_TYPE mechanism = PK11_DefaultArray[i].mechanism;
PK11SlotList *slotList = PK11_GetSlotList(mechanism);
if (slotList)
PK11_AddSlotToList(slotList, slot, PR_FALSE);
}
}
return;
}
/*
* update a slot to its new attribute according to the slot list
* returns: SECSuccess if nothing to do or add/delete is successful
*/
SECStatus
PK11_UpdateSlotAttribute(PK11SlotInfo *slot,
const PK11DefaultArrayEntry *entry,
PRBool add)
/* add: PR_TRUE if want to turn on */
{
SECStatus result = SECSuccess;
PK11SlotList *slotList = PK11_GetSlotList(entry->mechanism);
if (add) { /* trying to turn on a mechanism */
/* turn on the default flag in the slot */
slot->defaultFlags |= entry->flag;
/* add this slot to the list */
if (slotList != NULL)
result = PK11_AddSlotToList(slotList, slot, PR_FALSE);
} else { /* trying to turn off */