/src/mozilla-central/security/nss/lib/pk11wrap/pk11slot.c
Line | Count | Source (jump to first uncovered line) |
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 | | * Deal with PKCS #11 Slots. |
6 | | */ |
7 | | #include "seccomon.h" |
8 | | #include "secmod.h" |
9 | | #include "nssilock.h" |
10 | | #include "secmodi.h" |
11 | | #include "secmodti.h" |
12 | | #include "pkcs11t.h" |
13 | | #include "pk11func.h" |
14 | | #include "secitem.h" |
15 | | #include "secerr.h" |
16 | | |
17 | | #include "dev.h" |
18 | | #include "dev3hack.h" |
19 | | #include "pkim.h" |
20 | | #include "utilpars.h" |
21 | | #include "pkcs11uri.h" |
22 | | |
23 | | /************************************************************* |
24 | | * local static and global data |
25 | | *************************************************************/ |
26 | | |
27 | | /* |
28 | | * This array helps parsing between names, mechanisms, and flags. |
29 | | * to make the config files understand more entries, add them |
30 | | * to this table. |
31 | | */ |
32 | | const PK11DefaultArrayEntry PK11_DefaultArray[] = { |
33 | | { "RSA", SECMOD_RSA_FLAG, CKM_RSA_PKCS }, |
34 | | { "DSA", SECMOD_DSA_FLAG, CKM_DSA }, |
35 | | { "ECC", SECMOD_ECC_FLAG, CKM_ECDSA }, |
36 | | { "DH", SECMOD_DH_FLAG, CKM_DH_PKCS_DERIVE }, |
37 | | { "RC2", SECMOD_RC2_FLAG, CKM_RC2_CBC }, |
38 | | { "RC4", SECMOD_RC4_FLAG, CKM_RC4 }, |
39 | | { "DES", SECMOD_DES_FLAG, CKM_DES_CBC }, |
40 | | { "AES", SECMOD_AES_FLAG, CKM_AES_CBC }, |
41 | | { "Camellia", SECMOD_CAMELLIA_FLAG, CKM_CAMELLIA_CBC }, |
42 | | { "SEED", SECMOD_SEED_FLAG, CKM_SEED_CBC }, |
43 | | { "RC5", SECMOD_RC5_FLAG, CKM_RC5_CBC }, |
44 | | { "SHA-1", SECMOD_SHA1_FLAG, CKM_SHA_1 }, |
45 | | /* { "SHA224", SECMOD_SHA256_FLAG, CKM_SHA224 }, */ |
46 | | { "SHA256", SECMOD_SHA256_FLAG, CKM_SHA256 }, |
47 | | /* { "SHA384", SECMOD_SHA512_FLAG, CKM_SHA384 }, */ |
48 | | { "SHA512", SECMOD_SHA512_FLAG, CKM_SHA512 }, |
49 | | { "MD5", SECMOD_MD5_FLAG, CKM_MD5 }, |
50 | | { "MD2", SECMOD_MD2_FLAG, CKM_MD2 }, |
51 | | { "SSL", SECMOD_SSL_FLAG, CKM_SSL3_PRE_MASTER_KEY_GEN }, |
52 | | { "TLS", SECMOD_TLS_FLAG, CKM_TLS_MASTER_KEY_DERIVE }, |
53 | | { "SKIPJACK", SECMOD_FORTEZZA_FLAG, CKM_SKIPJACK_CBC64 }, |
54 | | { "Publicly-readable certs", SECMOD_FRIENDLY_FLAG, CKM_INVALID_MECHANISM }, |
55 | | { "Random Num Generator", SECMOD_RANDOM_FLAG, CKM_FAKE_RANDOM }, |
56 | | }; |
57 | | const int num_pk11_default_mechanisms = |
58 | | sizeof(PK11_DefaultArray) / sizeof(PK11_DefaultArray[0]); |
59 | | |
60 | | const PK11DefaultArrayEntry * |
61 | | PK11_GetDefaultArray(int *size) |
62 | 0 | { |
63 | 0 | if (size) { |
64 | 0 | *size = num_pk11_default_mechanisms; |
65 | 0 | } |
66 | 0 | return PK11_DefaultArray; |
67 | 0 | } |
68 | | |
69 | | /* |
70 | | * These slotlists are lists of modules which provide default support for |
71 | | * a given algorithm or mechanism. |
72 | | */ |
73 | | static PK11SlotList |
74 | | pk11_seedSlotList, |
75 | | pk11_camelliaSlotList, |
76 | | pk11_aesSlotList, |
77 | | pk11_desSlotList, |
78 | | pk11_rc4SlotList, |
79 | | pk11_rc2SlotList, |
80 | | pk11_rc5SlotList, |
81 | | pk11_sha1SlotList, |
82 | | pk11_md5SlotList, |
83 | | pk11_md2SlotList, |
84 | | pk11_rsaSlotList, |
85 | | pk11_dsaSlotList, |
86 | | pk11_dhSlotList, |
87 | | pk11_ecSlotList, |
88 | | pk11_ideaSlotList, |
89 | | pk11_sslSlotList, |
90 | | pk11_tlsSlotList, |
91 | | pk11_randomSlotList, |
92 | | pk11_sha256SlotList, |
93 | | pk11_sha512SlotList; /* slots do SHA512 and SHA384 */ |
94 | | |
95 | | /************************************************************ |
96 | | * Generic Slot List and Slot List element manipulations |
97 | | ************************************************************/ |
98 | | |
99 | | /* |
100 | | * allocate a new list |
101 | | */ |
102 | | PK11SlotList * |
103 | | PK11_NewSlotList(void) |
104 | 0 | { |
105 | 0 | PK11SlotList *list; |
106 | 0 |
|
107 | 0 | list = (PK11SlotList *)PORT_Alloc(sizeof(PK11SlotList)); |
108 | 0 | if (list == NULL) |
109 | 0 | return NULL; |
110 | 0 | list->head = NULL; |
111 | 0 | list->tail = NULL; |
112 | 0 | list->lock = PZ_NewLock(nssILockList); |
113 | 0 | if (list->lock == NULL) { |
114 | 0 | PORT_Free(list); |
115 | 0 | return NULL; |
116 | 0 | } |
117 | 0 |
|
118 | 0 | return list; |
119 | 0 | } |
120 | | |
121 | | /* |
122 | | * free a list element when all the references go away. |
123 | | */ |
124 | | SECStatus |
125 | | PK11_FreeSlotListElement(PK11SlotList *list, PK11SlotListElement *le) |
126 | 0 | { |
127 | 0 | PRBool freeit = PR_FALSE; |
128 | 0 |
|
129 | 0 | if (list == NULL || le == NULL) { |
130 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
131 | 0 | return SECFailure; |
132 | 0 | } |
133 | 0 |
|
134 | 0 | PZ_Lock(list->lock); |
135 | 0 | if (le->refCount-- == 1) { |
136 | 0 | freeit = PR_TRUE; |
137 | 0 | } |
138 | 0 | PZ_Unlock(list->lock); |
139 | 0 | if (freeit) { |
140 | 0 | PK11_FreeSlot(le->slot); |
141 | 0 | PORT_Free(le); |
142 | 0 | } |
143 | 0 | return SECSuccess; |
144 | 0 | } |
145 | | |
146 | | static void |
147 | | pk11_FreeSlotListStatic(PK11SlotList *list) |
148 | 0 | { |
149 | 0 | PK11SlotListElement *le, *next; |
150 | 0 | if (list == NULL) |
151 | 0 | return; |
152 | 0 | |
153 | 0 | for (le = list->head; le; le = next) { |
154 | 0 | next = le->next; |
155 | 0 | PK11_FreeSlotListElement(list, le); |
156 | 0 | } |
157 | 0 | if (list->lock) { |
158 | 0 | PZ_DestroyLock(list->lock); |
159 | 0 | } |
160 | 0 | list->lock = NULL; |
161 | 0 | list->head = NULL; |
162 | 0 | } |
163 | | |
164 | | /* |
165 | | * if we are freeing the list, we must be the only ones with a pointer |
166 | | * to the list. |
167 | | */ |
168 | | void |
169 | | PK11_FreeSlotList(PK11SlotList *list) |
170 | 0 | { |
171 | 0 | pk11_FreeSlotListStatic(list); |
172 | 0 | PORT_Free(list); |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * add a slot to a list |
177 | | * "slot" is the slot to be added. Ownership is not transferred. |
178 | | * "sorted" indicates whether or not the slot should be inserted according to |
179 | | * cipherOrder of the associated module. PR_FALSE indicates that the slot |
180 | | * should be inserted to the head of the list. |
181 | | */ |
182 | | SECStatus |
183 | | PK11_AddSlotToList(PK11SlotList *list, PK11SlotInfo *slot, PRBool sorted) |
184 | 0 | { |
185 | 0 | PK11SlotListElement *le; |
186 | 0 | PK11SlotListElement *element; |
187 | 0 |
|
188 | 0 | le = (PK11SlotListElement *)PORT_Alloc(sizeof(PK11SlotListElement)); |
189 | 0 | if (le == NULL) |
190 | 0 | return SECFailure; |
191 | 0 | |
192 | 0 | le->slot = PK11_ReferenceSlot(slot); |
193 | 0 | le->prev = NULL; |
194 | 0 | le->refCount = 1; |
195 | 0 | PZ_Lock(list->lock); |
196 | 0 | element = list->head; |
197 | 0 | /* Insertion sort, with higher cipherOrders are sorted first in the list */ |
198 | 0 | while (element && sorted && (element->slot->module->cipherOrder > |
199 | 0 | le->slot->module->cipherOrder)) { |
200 | 0 | element = element->next; |
201 | 0 | } |
202 | 0 | if (element) { |
203 | 0 | le->prev = element->prev; |
204 | 0 | element->prev = le; |
205 | 0 | le->next = element; |
206 | 0 | } else { |
207 | 0 | le->prev = list->tail; |
208 | 0 | le->next = NULL; |
209 | 0 | list->tail = le; |
210 | 0 | } |
211 | 0 | if (le->prev) |
212 | 0 | le->prev->next = le; |
213 | 0 | if (list->head == element) |
214 | 0 | list->head = le; |
215 | 0 | PZ_Unlock(list->lock); |
216 | 0 |
|
217 | 0 | return SECSuccess; |
218 | 0 | } |
219 | | |
220 | | /* |
221 | | * remove a slot entry from the list |
222 | | */ |
223 | | SECStatus |
224 | | PK11_DeleteSlotFromList(PK11SlotList *list, PK11SlotListElement *le) |
225 | 0 | { |
226 | 0 | PZ_Lock(list->lock); |
227 | 0 | if (le->prev) |
228 | 0 | le->prev->next = le->next; |
229 | 0 | else |
230 | 0 | list->head = le->next; |
231 | 0 | if (le->next) |
232 | 0 | le->next->prev = le->prev; |
233 | 0 | else |
234 | 0 | list->tail = le->prev; |
235 | 0 | le->next = le->prev = NULL; |
236 | 0 | PZ_Unlock(list->lock); |
237 | 0 | PK11_FreeSlotListElement(list, le); |
238 | 0 | return SECSuccess; |
239 | 0 | } |
240 | | |
241 | | /* |
242 | | * Move a list to the end of the target list. |
243 | | * NOTE: There is no locking here... This assumes BOTH lists are private copy |
244 | | * lists. It also does not re-sort the target list. |
245 | | */ |
246 | | SECStatus |
247 | | pk11_MoveListToList(PK11SlotList *target, PK11SlotList *src) |
248 | 0 | { |
249 | 0 | if (src->head == NULL) |
250 | 0 | return SECSuccess; |
251 | 0 | |
252 | 0 | if (target->tail == NULL) { |
253 | 0 | target->head = src->head; |
254 | 0 | } else { |
255 | 0 | target->tail->next = src->head; |
256 | 0 | } |
257 | 0 | src->head->prev = target->tail; |
258 | 0 | target->tail = src->tail; |
259 | 0 | src->head = src->tail = NULL; |
260 | 0 | return SECSuccess; |
261 | 0 | } |
262 | | |
263 | | /* |
264 | | * get an element from the list with a reference. You must own the list. |
265 | | */ |
266 | | PK11SlotListElement * |
267 | | PK11_GetFirstRef(PK11SlotList *list) |
268 | 0 | { |
269 | 0 | PK11SlotListElement *le; |
270 | 0 |
|
271 | 0 | le = list->head; |
272 | 0 | if (le != NULL) |
273 | 0 | (le)->refCount++; |
274 | 0 | return le; |
275 | 0 | } |
276 | | |
277 | | /* |
278 | | * get the next element from the list with a reference. You must own the list. |
279 | | */ |
280 | | PK11SlotListElement * |
281 | | PK11_GetNextRef(PK11SlotList *list, PK11SlotListElement *le, PRBool restart) |
282 | 0 | { |
283 | 0 | PK11SlotListElement *new_le; |
284 | 0 | new_le = le->next; |
285 | 0 | if (new_le) |
286 | 0 | new_le->refCount++; |
287 | 0 | PK11_FreeSlotListElement(list, le); |
288 | 0 | return new_le; |
289 | 0 | } |
290 | | |
291 | | /* |
292 | | * get an element safely from the list. This just makes sure that if |
293 | | * this element is not deleted while we deal with it. |
294 | | */ |
295 | | PK11SlotListElement * |
296 | | PK11_GetFirstSafe(PK11SlotList *list) |
297 | 0 | { |
298 | 0 | PK11SlotListElement *le; |
299 | 0 |
|
300 | 0 | PZ_Lock(list->lock); |
301 | 0 | le = list->head; |
302 | 0 | if (le != NULL) |
303 | 0 | (le)->refCount++; |
304 | 0 | PZ_Unlock(list->lock); |
305 | 0 | return le; |
306 | 0 | } |
307 | | |
308 | | /* |
309 | | * NOTE: if this element gets deleted, we can no longer safely traverse using |
310 | | * it's pointers. We can either terminate the loop, or restart from the |
311 | | * beginning. This is controlled by the restart option. |
312 | | */ |
313 | | PK11SlotListElement * |
314 | | PK11_GetNextSafe(PK11SlotList *list, PK11SlotListElement *le, PRBool restart) |
315 | 0 | { |
316 | 0 | PK11SlotListElement *new_le; |
317 | 0 | PZ_Lock(list->lock); |
318 | 0 | new_le = le->next; |
319 | 0 | if (le->next == NULL) { |
320 | 0 | /* if the prev and next fields are NULL then either this element |
321 | 0 | * has been removed and we need to walk the list again (if restart |
322 | 0 | * is true) or this was the only element on the list */ |
323 | 0 | if ((le->prev == NULL) && restart && (list->head != le)) { |
324 | 0 | new_le = list->head; |
325 | 0 | } |
326 | 0 | } |
327 | 0 | if (new_le) |
328 | 0 | new_le->refCount++; |
329 | 0 | PZ_Unlock(list->lock); |
330 | 0 | PK11_FreeSlotListElement(list, le); |
331 | 0 | return new_le; |
332 | 0 | } |
333 | | |
334 | | /* |
335 | | * Find the element that holds this slot |
336 | | */ |
337 | | PK11SlotListElement * |
338 | | PK11_FindSlotElement(PK11SlotList *list, PK11SlotInfo *slot) |
339 | 0 | { |
340 | 0 | PK11SlotListElement *le; |
341 | 0 |
|
342 | 0 | for (le = PK11_GetFirstSafe(list); le; |
343 | 0 | le = PK11_GetNextSafe(list, le, PR_TRUE)) { |
344 | 0 | if (le->slot == slot) |
345 | 0 | return le; |
346 | 0 | } |
347 | 0 | return NULL; |
348 | 0 | } |
349 | | |
350 | | /************************************************************ |
351 | | * Generic Slot Utilities |
352 | | ************************************************************/ |
353 | | /* |
354 | | * Create a new slot structure |
355 | | */ |
356 | | PK11SlotInfo * |
357 | | PK11_NewSlotInfo(SECMODModule *mod) |
358 | 0 | { |
359 | 0 | PK11SlotInfo *slot; |
360 | 0 |
|
361 | 0 | slot = (PK11SlotInfo *)PORT_Alloc(sizeof(PK11SlotInfo)); |
362 | 0 | if (slot == NULL) |
363 | 0 | return slot; |
364 | 0 | |
365 | 0 | slot->sessionLock = mod->isThreadSafe ? PZ_NewLock(nssILockSession) : mod->refLock; |
366 | 0 | if (slot->sessionLock == NULL) { |
367 | 0 | PORT_Free(slot); |
368 | 0 | return NULL; |
369 | 0 | } |
370 | 0 | slot->freeListLock = PZ_NewLock(nssILockFreelist); |
371 | 0 | if (slot->freeListLock == NULL) { |
372 | 0 | if (mod->isThreadSafe) { |
373 | 0 | PZ_DestroyLock(slot->sessionLock); |
374 | 0 | } |
375 | 0 | PORT_Free(slot); |
376 | 0 | return NULL; |
377 | 0 | } |
378 | 0 | slot->freeSymKeysWithSessionHead = NULL; |
379 | 0 | slot->freeSymKeysHead = NULL; |
380 | 0 | slot->keyCount = 0; |
381 | 0 | slot->maxKeyCount = 0; |
382 | 0 | slot->functionList = NULL; |
383 | 0 | slot->needTest = PR_TRUE; |
384 | 0 | slot->isPerm = PR_FALSE; |
385 | 0 | slot->isHW = PR_FALSE; |
386 | 0 | slot->isInternal = PR_FALSE; |
387 | 0 | slot->isThreadSafe = PR_FALSE; |
388 | 0 | slot->disabled = PR_FALSE; |
389 | 0 | slot->series = 1; |
390 | 0 | slot->wrapKey = 0; |
391 | 0 | slot->wrapMechanism = CKM_INVALID_MECHANISM; |
392 | 0 | slot->refKeys[0] = CK_INVALID_HANDLE; |
393 | 0 | slot->reason = PK11_DIS_NONE; |
394 | 0 | slot->readOnly = PR_TRUE; |
395 | 0 | slot->needLogin = PR_FALSE; |
396 | 0 | slot->hasRandom = PR_FALSE; |
397 | 0 | slot->defRWSession = PR_FALSE; |
398 | 0 | slot->protectedAuthPath = PR_FALSE; |
399 | 0 | slot->flags = 0; |
400 | 0 | slot->session = CK_INVALID_SESSION; |
401 | 0 | slot->slotID = 0; |
402 | 0 | slot->defaultFlags = 0; |
403 | 0 | slot->refCount = 1; |
404 | 0 | slot->askpw = 0; |
405 | 0 | slot->timeout = 0; |
406 | 0 | slot->mechanismList = NULL; |
407 | 0 | slot->mechanismCount = 0; |
408 | 0 | slot->cert_array = NULL; |
409 | 0 | slot->cert_count = 0; |
410 | 0 | slot->slot_name[0] = 0; |
411 | 0 | slot->token_name[0] = 0; |
412 | 0 | PORT_Memset(slot->serial, ' ', sizeof(slot->serial)); |
413 | 0 | PORT_Memset(&slot->tokenInfo, 0, sizeof(slot->tokenInfo)); |
414 | 0 | slot->module = NULL; |
415 | 0 | slot->authTransact = 0; |
416 | 0 | slot->authTime = LL_ZERO; |
417 | 0 | slot->minPassword = 0; |
418 | 0 | slot->maxPassword = 0; |
419 | 0 | slot->hasRootCerts = PR_FALSE; |
420 | 0 | slot->hasRootTrust = PR_FALSE; |
421 | 0 | slot->nssToken = NULL; |
422 | 0 | return slot; |
423 | 0 | } |
424 | | |
425 | | /* create a new reference to a slot so it doesn't go away */ |
426 | | PK11SlotInfo * |
427 | | PK11_ReferenceSlot(PK11SlotInfo *slot) |
428 | 0 | { |
429 | 0 | PR_ATOMIC_INCREMENT(&slot->refCount); |
430 | 0 | return slot; |
431 | 0 | } |
432 | | |
433 | | /* Destroy all info on a slot we have built up */ |
434 | | void |
435 | | PK11_DestroySlot(PK11SlotInfo *slot) |
436 | 0 | { |
437 | 0 | /* free up the cached keys and sessions */ |
438 | 0 | PK11_CleanKeyList(slot); |
439 | 0 |
|
440 | 0 | /* free up all the sessions on this slot */ |
441 | 0 | if (slot->functionList) { |
442 | 0 | PK11_GETTAB(slot) |
443 | 0 | ->C_CloseAllSessions(slot->slotID); |
444 | 0 | } |
445 | 0 |
|
446 | 0 | if (slot->mechanismList) { |
447 | 0 | PORT_Free(slot->mechanismList); |
448 | 0 | } |
449 | 0 | if (slot->isThreadSafe && slot->sessionLock) { |
450 | 0 | PZ_DestroyLock(slot->sessionLock); |
451 | 0 | } |
452 | 0 | slot->sessionLock = NULL; |
453 | 0 | if (slot->freeListLock) { |
454 | 0 | PZ_DestroyLock(slot->freeListLock); |
455 | 0 | slot->freeListLock = NULL; |
456 | 0 | } |
457 | 0 |
|
458 | 0 | /* finally Tell our parent module that we've gone away so it can unload */ |
459 | 0 | if (slot->module) { |
460 | 0 | SECMOD_SlotDestroyModule(slot->module, PR_TRUE); |
461 | 0 | } |
462 | 0 |
|
463 | 0 | /* ok, well not quit finally... now we free the memory */ |
464 | 0 | PORT_Free(slot); |
465 | 0 | } |
466 | | |
467 | | /* We're all done with the slot, free it */ |
468 | | void |
469 | | PK11_FreeSlot(PK11SlotInfo *slot) |
470 | 0 | { |
471 | 0 | if (PR_ATOMIC_DECREMENT(&slot->refCount) == 0) { |
472 | 0 | PK11_DestroySlot(slot); |
473 | 0 | } |
474 | 0 | } |
475 | | |
476 | | void |
477 | | PK11_EnterSlotMonitor(PK11SlotInfo *slot) |
478 | 0 | { |
479 | 0 | PZ_Lock(slot->sessionLock); |
480 | 0 | } |
481 | | |
482 | | void |
483 | | PK11_ExitSlotMonitor(PK11SlotInfo *slot) |
484 | 0 | { |
485 | 0 | PZ_Unlock(slot->sessionLock); |
486 | 0 | } |
487 | | |
488 | | /*********************************************************** |
489 | | * Functions to find specific slots. |
490 | | ***********************************************************/ |
491 | | PRBool |
492 | | SECMOD_HasRootCerts(void) |
493 | 0 | { |
494 | 0 | SECMODModuleList *mlp; |
495 | 0 | SECMODModuleList *modules; |
496 | 0 | SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock(); |
497 | 0 | int i; |
498 | 0 | PRBool found = PR_FALSE; |
499 | 0 |
|
500 | 0 | if (!moduleLock) { |
501 | 0 | PORT_SetError(SEC_ERROR_NOT_INITIALIZED); |
502 | 0 | return found; |
503 | 0 | } |
504 | 0 |
|
505 | 0 | /* work through all the slots */ |
506 | 0 | SECMOD_GetReadLock(moduleLock); |
507 | 0 | modules = SECMOD_GetDefaultModuleList(); |
508 | 0 | for (mlp = modules; mlp != NULL; mlp = mlp->next) { |
509 | 0 | for (i = 0; i < mlp->module->slotCount; i++) { |
510 | 0 | PK11SlotInfo *tmpSlot = mlp->module->slots[i]; |
511 | 0 | if (PK11_IsPresent(tmpSlot)) { |
512 | 0 | if (tmpSlot->hasRootCerts) { |
513 | 0 | found = PR_TRUE; |
514 | 0 | break; |
515 | 0 | } |
516 | 0 | } |
517 | 0 | } |
518 | 0 | if (found) |
519 | 0 | break; |
520 | 0 | } |
521 | 0 | SECMOD_ReleaseReadLock(moduleLock); |
522 | 0 |
|
523 | 0 | return found; |
524 | 0 | } |
525 | | |
526 | | /*********************************************************** |
527 | | * Functions to find specific slots. |
528 | | ***********************************************************/ |
529 | | PK11SlotList * |
530 | | PK11_FindSlotsByNames(const char *dllName, const char *slotName, |
531 | | const char *tokenName, PRBool presentOnly) |
532 | | { |
533 | | SECMODModuleList *mlp; |
534 | | SECMODModuleList *modules; |
535 | | SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock(); |
536 | | int i; |
537 | | PK11SlotList *slotList = NULL; |
538 | | PRUint32 slotcount = 0; |
539 | | SECStatus rv = SECSuccess; |
540 | | |
541 | | if (!moduleLock) { |
542 | | PORT_SetError(SEC_ERROR_NOT_INITIALIZED); |
543 | | return slotList; |
544 | | } |
545 | | |
546 | | slotList = PK11_NewSlotList(); |
547 | | if (!slotList) { |
548 | | PORT_SetError(SEC_ERROR_NO_MEMORY); |
549 | | return slotList; |
550 | | } |
551 | | |
552 | | if (((NULL == dllName) || (0 == *dllName)) && |
553 | | ((NULL == slotName) || (0 == *slotName)) && |
554 | | ((NULL == tokenName) || (0 == *tokenName))) { |
555 | | /* default to softoken */ |
556 | | /* PK11_GetInternalKeySlot increments the refcount on the internal slot, |
557 | | * but so does PK11_AddSlotToList. To avoid erroneously increasing the |
558 | | * refcount twice, we get our own reference to the internal slot and |
559 | | * decrement its refcount when we're done with it. */ |
560 | | PK11SlotInfo *internalKeySlot = PK11_GetInternalKeySlot(); |
561 | | PK11_AddSlotToList(slotList, internalKeySlot, PR_TRUE); |
562 | | PK11_FreeSlot(internalKeySlot); |
563 | | return slotList; |
564 | | } |
565 | | |
566 | | /* work through all the slots */ |
567 | | SECMOD_GetReadLock(moduleLock); |
568 | | modules = SECMOD_GetDefaultModuleList(); |
569 | | for (mlp = modules; mlp != NULL; mlp = mlp->next) { |
570 | | PORT_Assert(mlp->module); |
571 | | if (!mlp->module) { |
572 | | rv = SECFailure; |
573 | | break; |
574 | | } |
575 | | if ((!dllName) || (mlp->module->dllName && |
576 | | (0 == PORT_Strcmp(mlp->module->dllName, dllName)))) { |
577 | | for (i = 0; i < mlp->module->slotCount; i++) { |
578 | | PK11SlotInfo *tmpSlot = (mlp->module->slots ? mlp->module->slots[i] : NULL); |
579 | | PORT_Assert(tmpSlot); |
580 | | if (!tmpSlot) { |
581 | | rv = SECFailure; |
582 | | break; |
583 | | } |
584 | | if ((PR_FALSE == presentOnly || PK11_IsPresent(tmpSlot)) && |
585 | | ((!tokenName) || |
586 | | (0 == PORT_Strcmp(tmpSlot->token_name, tokenName))) && |
587 | | ((!slotName) || |
588 | | (0 == PORT_Strcmp(tmpSlot->slot_name, slotName)))) { |
589 | | PK11_AddSlotToList(slotList, tmpSlot, PR_TRUE); |
590 | | slotcount++; |
591 | | } |
592 | | } |
593 | | } |
594 | | } |
595 | | SECMOD_ReleaseReadLock(moduleLock); |
596 | | |
597 | | if ((0 == slotcount) || (SECFailure == rv)) { |
598 | | PORT_SetError(SEC_ERROR_NO_TOKEN); |
599 | | PK11_FreeSlotList(slotList); |
600 | | slotList = NULL; |
601 | | } |
602 | | |
603 | | if (SECFailure == rv) { |
604 | | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
605 | | } |
606 | | |
607 | | return slotList; |
608 | | } |
609 | | |
610 | | typedef PRBool (*PK11SlotMatchFunc)(PK11SlotInfo *slot, const void *arg); |
611 | | |
612 | | static PRBool |
613 | | pk11_MatchSlotByTokenName(PK11SlotInfo *slot, const void *arg) |
614 | 0 | { |
615 | 0 | return PORT_Strcmp(slot->token_name, arg) == 0; |
616 | 0 | } |
617 | | |
618 | | static PRBool |
619 | | pk11_MatchSlotBySerial(PK11SlotInfo *slot, const void *arg) |
620 | 0 | { |
621 | 0 | return PORT_Memcmp(slot->serial, arg, sizeof(slot->serial)) == 0; |
622 | 0 | } |
623 | | |
624 | | static PRBool |
625 | | pk11_MatchSlotByTokenURI(PK11SlotInfo *slot, const void *arg) |
626 | 0 | { |
627 | 0 | return pk11_MatchUriTokenInfo(slot, (PK11URI *)arg); |
628 | 0 | } |
629 | | |
630 | | static PK11SlotInfo * |
631 | | pk11_FindSlot(const void *arg, PK11SlotMatchFunc func) |
632 | 0 | { |
633 | 0 | SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock(); |
634 | 0 | SECMODModuleList *mlp; |
635 | 0 | SECMODModuleList *modules; |
636 | 0 | int i; |
637 | 0 | PK11SlotInfo *slot = NULL; |
638 | 0 |
|
639 | 0 | if (!moduleLock) { |
640 | 0 | PORT_SetError(SEC_ERROR_NOT_INITIALIZED); |
641 | 0 | return slot; |
642 | 0 | } |
643 | 0 | /* work through all the slots */ |
644 | 0 | SECMOD_GetReadLock(moduleLock); |
645 | 0 | modules = SECMOD_GetDefaultModuleList(); |
646 | 0 | for (mlp = modules; mlp != NULL; mlp = mlp->next) { |
647 | 0 | for (i = 0; i < mlp->module->slotCount; i++) { |
648 | 0 | PK11SlotInfo *tmpSlot = mlp->module->slots[i]; |
649 | 0 | if (PK11_IsPresent(tmpSlot)) { |
650 | 0 | if (func(tmpSlot, arg)) { |
651 | 0 | slot = PK11_ReferenceSlot(tmpSlot); |
652 | 0 | break; |
653 | 0 | } |
654 | 0 | } |
655 | 0 | } |
656 | 0 | if (slot != NULL) |
657 | 0 | break; |
658 | 0 | } |
659 | 0 | SECMOD_ReleaseReadLock(moduleLock); |
660 | 0 |
|
661 | 0 | if (slot == NULL) { |
662 | 0 | PORT_SetError(SEC_ERROR_NO_TOKEN); |
663 | 0 | } |
664 | 0 |
|
665 | 0 | return slot; |
666 | 0 | } |
667 | | |
668 | | static PK11SlotInfo * |
669 | | pk11_FindSlotByTokenURI(const char *uriString) |
670 | 0 | { |
671 | 0 | PK11SlotInfo *slot = NULL; |
672 | 0 | PK11URI *uri; |
673 | 0 |
|
674 | 0 | uri = PK11URI_ParseURI(uriString); |
675 | 0 | if (!uri) { |
676 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
677 | 0 | return slot; |
678 | 0 | } |
679 | 0 |
|
680 | 0 | slot = pk11_FindSlot(uri, pk11_MatchSlotByTokenURI); |
681 | 0 | PK11URI_DestroyURI(uri); |
682 | 0 | return slot; |
683 | 0 | } |
684 | | |
685 | | PK11SlotInfo * |
686 | | PK11_FindSlotByName(const char *name) |
687 | 0 | { |
688 | 0 | if ((name == NULL) || (*name == 0)) { |
689 | 0 | return PK11_GetInternalKeySlot(); |
690 | 0 | } |
691 | 0 | |
692 | 0 | if (!PORT_Strncasecmp(name, "pkcs11:", strlen("pkcs11:"))) { |
693 | 0 | return pk11_FindSlotByTokenURI(name); |
694 | 0 | } |
695 | 0 | |
696 | 0 | return pk11_FindSlot(name, pk11_MatchSlotByTokenName); |
697 | 0 | } |
698 | | |
699 | | PK11SlotInfo * |
700 | | PK11_FindSlotBySerial(char *serial) |
701 | 0 | { |
702 | 0 | return pk11_FindSlot(serial, pk11_MatchSlotBySerial); |
703 | 0 | } |
704 | | |
705 | | /* |
706 | | * notification stub. If we ever get interested in any events that |
707 | | * the pkcs11 functions may pass back to use, we can catch them here... |
708 | | * currently pdata is a slotinfo structure. |
709 | | */ |
710 | | CK_RV |
711 | | pk11_notify(CK_SESSION_HANDLE session, CK_NOTIFICATION event, |
712 | | CK_VOID_PTR pdata) |
713 | 0 | { |
714 | 0 | return CKR_OK; |
715 | 0 | } |
716 | | |
717 | | /* |
718 | | * grab a new RW session |
719 | | * !!! has a side effect of grabbing the Monitor if either the slot's default |
720 | | * session is RW or the slot is not thread safe. Monitor is release in function |
721 | | * below |
722 | | */ |
723 | | CK_SESSION_HANDLE |
724 | | PK11_GetRWSession(PK11SlotInfo *slot) |
725 | 0 | { |
726 | 0 | CK_SESSION_HANDLE rwsession; |
727 | 0 | CK_RV crv; |
728 | 0 | PRBool haveMonitor = PR_FALSE; |
729 | 0 |
|
730 | 0 | if (!slot->isThreadSafe || slot->defRWSession) { |
731 | 0 | PK11_EnterSlotMonitor(slot); |
732 | 0 | haveMonitor = PR_TRUE; |
733 | 0 | } |
734 | 0 | if (slot->defRWSession) { |
735 | 0 | PORT_Assert(slot->session != CK_INVALID_SESSION); |
736 | 0 | if (slot->session != CK_INVALID_SESSION) |
737 | 0 | return slot->session; |
738 | 0 | } |
739 | 0 | |
740 | 0 | crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID, |
741 | 0 | CKF_RW_SESSION | CKF_SERIAL_SESSION, |
742 | 0 | slot, pk11_notify, &rwsession); |
743 | 0 | PORT_Assert(rwsession != CK_INVALID_SESSION || crv != CKR_OK); |
744 | 0 | if (crv != CKR_OK || rwsession == CK_INVALID_SESSION) { |
745 | 0 | if (crv == CKR_OK) |
746 | 0 | crv = CKR_DEVICE_ERROR; |
747 | 0 | if (haveMonitor) |
748 | 0 | PK11_ExitSlotMonitor(slot); |
749 | 0 | PORT_SetError(PK11_MapError(crv)); |
750 | 0 | return CK_INVALID_SESSION; |
751 | 0 | } |
752 | 0 | if (slot->defRWSession) { /* we have the monitor */ |
753 | 0 | slot->session = rwsession; |
754 | 0 | } |
755 | 0 | return rwsession; |
756 | 0 | } |
757 | | |
758 | | PRBool |
759 | | PK11_RWSessionHasLock(PK11SlotInfo *slot, CK_SESSION_HANDLE session_handle) |
760 | 0 | { |
761 | 0 | PRBool hasLock; |
762 | 0 | hasLock = (PRBool)(!slot->isThreadSafe || |
763 | 0 | (slot->defRWSession && slot->session != CK_INVALID_SESSION)); |
764 | 0 | return hasLock; |
765 | 0 | } |
766 | | |
767 | | static PRBool |
768 | | pk11_RWSessionIsDefault(PK11SlotInfo *slot, CK_SESSION_HANDLE rwsession) |
769 | 0 | { |
770 | 0 | PRBool isDefault; |
771 | 0 | isDefault = (PRBool)(slot->session == rwsession && |
772 | 0 | slot->defRWSession && |
773 | 0 | slot->session != CK_INVALID_SESSION); |
774 | 0 | return isDefault; |
775 | 0 | } |
776 | | |
777 | | /* |
778 | | * close the rwsession and restore our readonly session |
779 | | * !!! has a side effect of releasing the Monitor if either the slot's default |
780 | | * session is RW or the slot is not thread safe. |
781 | | */ |
782 | | void |
783 | | PK11_RestoreROSession(PK11SlotInfo *slot, CK_SESSION_HANDLE rwsession) |
784 | 0 | { |
785 | 0 | PORT_Assert(rwsession != CK_INVALID_SESSION); |
786 | 0 | if (rwsession != CK_INVALID_SESSION) { |
787 | 0 | PRBool doExit = PK11_RWSessionHasLock(slot, rwsession); |
788 | 0 | if (!pk11_RWSessionIsDefault(slot, rwsession)) |
789 | 0 | PK11_GETTAB(slot) |
790 | 0 | ->C_CloseSession(rwsession); |
791 | 0 | if (doExit) |
792 | 0 | PK11_ExitSlotMonitor(slot); |
793 | 0 | } |
794 | 0 | } |
795 | | |
796 | | /************************************************************ |
797 | | * Manage the built-In Slot Lists |
798 | | ************************************************************/ |
799 | | |
800 | | /* Init the static built int slot list (should actually integrate |
801 | | * with PK11_NewSlotList */ |
802 | | static void |
803 | | pk11_InitSlotListStatic(PK11SlotList *list) |
804 | 0 | { |
805 | 0 | list->lock = PZ_NewLock(nssILockList); |
806 | 0 | list->head = NULL; |
807 | 0 | } |
808 | | |
809 | | /* initialize the system slotlists */ |
810 | | SECStatus |
811 | | PK11_InitSlotLists(void) |
812 | 0 | { |
813 | 0 | pk11_InitSlotListStatic(&pk11_seedSlotList); |
814 | 0 | pk11_InitSlotListStatic(&pk11_camelliaSlotList); |
815 | 0 | pk11_InitSlotListStatic(&pk11_aesSlotList); |
816 | 0 | pk11_InitSlotListStatic(&pk11_desSlotList); |
817 | 0 | pk11_InitSlotListStatic(&pk11_rc4SlotList); |
818 | 0 | pk11_InitSlotListStatic(&pk11_rc2SlotList); |
819 | 0 | pk11_InitSlotListStatic(&pk11_rc5SlotList); |
820 | 0 | pk11_InitSlotListStatic(&pk11_md5SlotList); |
821 | 0 | pk11_InitSlotListStatic(&pk11_md2SlotList); |
822 | 0 | pk11_InitSlotListStatic(&pk11_sha1SlotList); |
823 | 0 | pk11_InitSlotListStatic(&pk11_rsaSlotList); |
824 | 0 | pk11_InitSlotListStatic(&pk11_dsaSlotList); |
825 | 0 | pk11_InitSlotListStatic(&pk11_dhSlotList); |
826 | 0 | pk11_InitSlotListStatic(&pk11_ecSlotList); |
827 | 0 | pk11_InitSlotListStatic(&pk11_ideaSlotList); |
828 | 0 | pk11_InitSlotListStatic(&pk11_sslSlotList); |
829 | 0 | pk11_InitSlotListStatic(&pk11_tlsSlotList); |
830 | 0 | pk11_InitSlotListStatic(&pk11_randomSlotList); |
831 | 0 | pk11_InitSlotListStatic(&pk11_sha256SlotList); |
832 | 0 | pk11_InitSlotListStatic(&pk11_sha512SlotList); |
833 | 0 | return SECSuccess; |
834 | 0 | } |
835 | | |
836 | | void |
837 | | PK11_DestroySlotLists(void) |
838 | 0 | { |
839 | 0 | pk11_FreeSlotListStatic(&pk11_seedSlotList); |
840 | 0 | pk11_FreeSlotListStatic(&pk11_camelliaSlotList); |
841 | 0 | pk11_FreeSlotListStatic(&pk11_aesSlotList); |
842 | 0 | pk11_FreeSlotListStatic(&pk11_desSlotList); |
843 | 0 | pk11_FreeSlotListStatic(&pk11_rc4SlotList); |
844 | 0 | pk11_FreeSlotListStatic(&pk11_rc2SlotList); |
845 | 0 | pk11_FreeSlotListStatic(&pk11_rc5SlotList); |
846 | 0 | pk11_FreeSlotListStatic(&pk11_md5SlotList); |
847 | 0 | pk11_FreeSlotListStatic(&pk11_md2SlotList); |
848 | 0 | pk11_FreeSlotListStatic(&pk11_sha1SlotList); |
849 | 0 | pk11_FreeSlotListStatic(&pk11_rsaSlotList); |
850 | 0 | pk11_FreeSlotListStatic(&pk11_dsaSlotList); |
851 | 0 | pk11_FreeSlotListStatic(&pk11_dhSlotList); |
852 | 0 | pk11_FreeSlotListStatic(&pk11_ecSlotList); |
853 | 0 | pk11_FreeSlotListStatic(&pk11_ideaSlotList); |
854 | 0 | pk11_FreeSlotListStatic(&pk11_sslSlotList); |
855 | 0 | pk11_FreeSlotListStatic(&pk11_tlsSlotList); |
856 | 0 | pk11_FreeSlotListStatic(&pk11_randomSlotList); |
857 | 0 | pk11_FreeSlotListStatic(&pk11_sha256SlotList); |
858 | 0 | pk11_FreeSlotListStatic(&pk11_sha512SlotList); |
859 | 0 | return; |
860 | 0 | } |
861 | | |
862 | | /* return a system slot list based on mechanism */ |
863 | | PK11SlotList * |
864 | | PK11_GetSlotList(CK_MECHANISM_TYPE type) |
865 | 0 | { |
866 | 0 | /* XXX a workaround for Bugzilla bug #55267 */ |
867 | | #if defined(HPUX) && defined(__LP64__) |
868 | | if (CKM_INVALID_MECHANISM == type) |
869 | | return NULL; |
870 | | #endif |
871 | | switch (type) { |
872 | 0 | case CKM_SEED_CBC: |
873 | 0 | case CKM_SEED_ECB: |
874 | 0 | return &pk11_seedSlotList; |
875 | 0 | case CKM_CAMELLIA_CBC: |
876 | 0 | case CKM_CAMELLIA_ECB: |
877 | 0 | return &pk11_camelliaSlotList; |
878 | 0 | case CKM_AES_CBC: |
879 | 0 | case CKM_AES_CCM: |
880 | 0 | case CKM_AES_CTR: |
881 | 0 | case CKM_AES_CTS: |
882 | 0 | case CKM_AES_GCM: |
883 | 0 | case CKM_AES_ECB: |
884 | 0 | return &pk11_aesSlotList; |
885 | 0 | case CKM_DES_CBC: |
886 | 0 | case CKM_DES_ECB: |
887 | 0 | case CKM_DES3_ECB: |
888 | 0 | case CKM_DES3_CBC: |
889 | 0 | return &pk11_desSlotList; |
890 | 0 | case CKM_RC4: |
891 | 0 | return &pk11_rc4SlotList; |
892 | 0 | case CKM_RC5_CBC: |
893 | 0 | return &pk11_rc5SlotList; |
894 | 0 | case CKM_SHA_1: |
895 | 0 | return &pk11_sha1SlotList; |
896 | 0 | case CKM_SHA224: |
897 | 0 | case CKM_SHA256: |
898 | 0 | return &pk11_sha256SlotList; |
899 | 0 | case CKM_SHA384: |
900 | 0 | case CKM_SHA512: |
901 | 0 | return &pk11_sha512SlotList; |
902 | 0 | case CKM_MD5: |
903 | 0 | return &pk11_md5SlotList; |
904 | 0 | case CKM_MD2: |
905 | 0 | return &pk11_md2SlotList; |
906 | 0 | case CKM_RC2_ECB: |
907 | 0 | case CKM_RC2_CBC: |
908 | 0 | return &pk11_rc2SlotList; |
909 | 0 | case CKM_RSA_PKCS: |
910 | 0 | case CKM_RSA_PKCS_KEY_PAIR_GEN: |
911 | 0 | case CKM_RSA_X_509: |
912 | 0 | return &pk11_rsaSlotList; |
913 | 0 | case CKM_DSA: |
914 | 0 | return &pk11_dsaSlotList; |
915 | 0 | case CKM_DH_PKCS_KEY_PAIR_GEN: |
916 | 0 | case CKM_DH_PKCS_DERIVE: |
917 | 0 | return &pk11_dhSlotList; |
918 | 0 | case CKM_ECDSA: |
919 | 0 | case CKM_ECDSA_SHA1: |
920 | 0 | case CKM_EC_KEY_PAIR_GEN: /* aka CKM_ECDSA_KEY_PAIR_GEN */ |
921 | 0 | case CKM_ECDH1_DERIVE: |
922 | 0 | return &pk11_ecSlotList; |
923 | 0 | case CKM_SSL3_PRE_MASTER_KEY_GEN: |
924 | 0 | case CKM_SSL3_MASTER_KEY_DERIVE: |
925 | 0 | case CKM_SSL3_SHA1_MAC: |
926 | 0 | case CKM_SSL3_MD5_MAC: |
927 | 0 | return &pk11_sslSlotList; |
928 | 0 | case CKM_TLS_MASTER_KEY_DERIVE: |
929 | 0 | case CKM_TLS_KEY_AND_MAC_DERIVE: |
930 | 0 | case CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256: |
931 | 0 | return &pk11_tlsSlotList; |
932 | 0 | case CKM_IDEA_CBC: |
933 | 0 | case CKM_IDEA_ECB: |
934 | 0 | return &pk11_ideaSlotList; |
935 | 0 | case CKM_FAKE_RANDOM: |
936 | 0 | return &pk11_randomSlotList; |
937 | 0 | } |
938 | 0 | return NULL; |
939 | 0 | } |
940 | | |
941 | | /* |
942 | | * load the static SlotInfo structures used to select a PKCS11 slot. |
943 | | * preSlotInfo has a list of all the default flags for the slots on this |
944 | | * module. |
945 | | */ |
946 | | void |
947 | | PK11_LoadSlotList(PK11SlotInfo *slot, PK11PreSlotInfo *psi, int count) |
948 | 0 | { |
949 | 0 | int i; |
950 | 0 |
|
951 | 0 | for (i = 0; i < count; i++) { |
952 | 0 | if (psi[i].slotID == slot->slotID) |
953 | 0 | break; |
954 | 0 | } |
955 | 0 |
|
956 | 0 | if (i == count) |
957 | 0 | return; |
958 | 0 | |
959 | 0 | slot->defaultFlags = psi[i].defaultFlags; |
960 | 0 | slot->askpw = psi[i].askpw; |
961 | 0 | slot->timeout = psi[i].timeout; |
962 | 0 | slot->hasRootCerts = psi[i].hasRootCerts; |
963 | 0 |
|
964 | 0 | /* if the slot is already disabled, don't load them into the |
965 | 0 | * default slot lists. We get here so we can save the default |
966 | 0 | * list value. */ |
967 | 0 | if (slot->disabled) |
968 | 0 | return; |
969 | 0 | |
970 | 0 | /* if the user has disabled us, don't load us in */ |
971 | 0 | if (slot->defaultFlags & PK11_DISABLE_FLAG) { |
972 | 0 | slot->disabled = PR_TRUE; |
973 | 0 | slot->reason = PK11_DIS_USER_SELECTED; |
974 | 0 | /* free up sessions and things?? */ |
975 | 0 | return; |
976 | 0 | } |
977 | 0 |
|
978 | 0 | for (i = 0; i < num_pk11_default_mechanisms; i++) { |
979 | 0 | if (slot->defaultFlags & PK11_DefaultArray[i].flag) { |
980 | 0 | CK_MECHANISM_TYPE mechanism = PK11_DefaultArray[i].mechanism; |
981 | 0 | PK11SlotList *slotList = PK11_GetSlotList(mechanism); |
982 | 0 |
|
983 | 0 | if (slotList) |
984 | 0 | PK11_AddSlotToList(slotList, slot, PR_FALSE); |
985 | 0 | } |
986 | 0 | } |
987 | 0 |
|
988 | 0 | return; |
989 | 0 | } |
990 | | |
991 | | /* |
992 | | * update a slot to its new attribute according to the slot list |
993 | | * returns: SECSuccess if nothing to do or add/delete is successful |
994 | | */ |
995 | | SECStatus |
996 | | PK11_UpdateSlotAttribute(PK11SlotInfo *slot, |
997 | | const PK11DefaultArrayEntry *entry, |
998 | | PRBool add) |
999 | | /* add: PR_TRUE if want to turn on */ |
1000 | 0 | { |
1001 | 0 | SECStatus result = SECSuccess; |
1002 | 0 | PK11SlotList *slotList = PK11_GetSlotList(entry->mechanism); |
1003 | 0 |
|
1004 | 0 | if (add) { /* trying to turn on a mechanism */ |
1005 | 0 |
|
1006 | 0 | /* turn on the default flag in the slot */ |
1007 | 0 | slot->defaultFlags |= entry->flag; |
1008 | 0 |
|
1009 | 0 | /* add this slot to the list */ |
1010 | 0 | if (slotList != NULL) |
1011 | 0 | result = PK11_AddSlotToList(slotList, slot, PR_FALSE); |
1012 | 0 |
|
1013 | 0 | } else { /* trying to turn off */ |
1014 | 0 |
|
1015 | 0 | /* turn OFF the flag in the slot */ |
1016 | 0 | slot->defaultFlags &= ~entry->flag; |
1017 | 0 |
|
1018 | 0 | if (slotList) { |
1019 | 0 | /* find the element in the list & delete it */ |
1020 | 0 | PK11SlotListElement *le = PK11_FindSlotElement(slotList, slot); |
1021 | 0 |
|
1022 | 0 | /* remove the slot from the list */ |
1023 | 0 | if (le) |
1024 | 0 | result = PK11_DeleteSlotFromList(slotList, le); |
1025 | 0 | } |
1026 | 0 | } |
1027 | 0 | return result; |
1028 | 0 | } |
1029 | | |
1030 | | /* |
1031 | | * clear a slot off of all of it's default list |
1032 | | */ |
1033 | | void |
1034 | | PK11_ClearSlotList(PK11SlotInfo *slot) |
1035 | 0 | { |
1036 | 0 | int i; |
1037 | 0 |
|
1038 | 0 | if (slot->disabled) |
1039 | 0 | return; |
1040 | 0 | if (slot->defaultFlags == 0) |
1041 | 0 | return; |
1042 | 0 | |
1043 | 0 | for (i = 0; i < num_pk11_default_mechanisms; i++) { |
1044 | 0 | if (slot->defaultFlags & PK11_DefaultArray[i].flag) { |
1045 | 0 | CK_MECHANISM_TYPE mechanism = PK11_DefaultArray[i].mechanism; |
1046 | 0 | PK11SlotList *slotList = PK11_GetSlotList(mechanism); |
1047 | 0 | PK11SlotListElement *le = NULL; |
1048 | 0 |
|
1049 | 0 | if (slotList) |
1050 | 0 | le = PK11_FindSlotElement(slotList, slot); |
1051 | 0 |
|
1052 | 0 | if (le) { |
1053 | 0 | PK11_DeleteSlotFromList(slotList, le); |
1054 | 0 | PK11_FreeSlotListElement(slotList, le); |
1055 | 0 | } |
1056 | 0 | } |
1057 | 0 | } |
1058 | 0 | } |
1059 | | |
1060 | | /****************************************************************** |
1061 | | * Slot initialization |
1062 | | ******************************************************************/ |
1063 | | /* |
1064 | | * turn a PKCS11 Static Label into a string |
1065 | | */ |
1066 | | char * |
1067 | | PK11_MakeString(PLArenaPool *arena, char *space, |
1068 | | char *staticString, int stringLen) |
1069 | 0 | { |
1070 | 0 | int i; |
1071 | 0 | char *newString; |
1072 | 0 | for (i = (stringLen - 1); i >= 0; i--) { |
1073 | 0 | if (staticString[i] != ' ') |
1074 | 0 | break; |
1075 | 0 | } |
1076 | 0 | /* move i to point to the last space */ |
1077 | 0 | i++; |
1078 | 0 | if (arena) { |
1079 | 0 | newString = (char *)PORT_ArenaAlloc(arena, i + 1 /* space for NULL */); |
1080 | 0 | } else if (space) { |
1081 | 0 | newString = space; |
1082 | 0 | } else { |
1083 | 0 | newString = (char *)PORT_Alloc(i + 1 /* space for NULL */); |
1084 | 0 | } |
1085 | 0 | if (newString == NULL) |
1086 | 0 | return NULL; |
1087 | 0 | |
1088 | 0 | if (i) |
1089 | 0 | PORT_Memcpy(newString, staticString, i); |
1090 | 0 | newString[i] = 0; |
1091 | 0 |
|
1092 | 0 | return newString; |
1093 | 0 | } |
1094 | | |
1095 | | /* |
1096 | | * check if a null-terminated string matches with a PKCS11 Static Label |
1097 | | */ |
1098 | | PRBool |
1099 | | pk11_MatchString(const char *string, |
1100 | | const char *staticString, int staticStringLen) |
1101 | 0 | { |
1102 | 0 | int i; |
1103 | 0 |
|
1104 | 0 | for (i = (staticStringLen - 1); i >= 0; i--) { |
1105 | 0 | if (staticString[i] != ' ') |
1106 | 0 | break; |
1107 | 0 | } |
1108 | 0 | /* move i to point to the last space */ |
1109 | 0 | i++; |
1110 | 0 |
|
1111 | 0 | if (strlen(string) == i && memcmp(string, staticString, i) == 0) { |
1112 | 0 | return PR_TRUE; |
1113 | 0 | } |
1114 | 0 |
|
1115 | 0 | return PR_FALSE; |
1116 | 0 | } |
1117 | | |
1118 | | /* |
1119 | | * Reads in the slots mechanism list for later use |
1120 | | */ |
1121 | | SECStatus |
1122 | | PK11_ReadMechanismList(PK11SlotInfo *slot) |
1123 | 0 | { |
1124 | 0 | CK_ULONG count; |
1125 | 0 | CK_RV crv; |
1126 | 0 | PRUint32 i; |
1127 | 0 |
|
1128 | 0 | if (slot->mechanismList) { |
1129 | 0 | PORT_Free(slot->mechanismList); |
1130 | 0 | slot->mechanismList = NULL; |
1131 | 0 | } |
1132 | 0 | slot->mechanismCount = 0; |
1133 | 0 |
|
1134 | 0 | if (!slot->isThreadSafe) |
1135 | 0 | PK11_EnterSlotMonitor(slot); |
1136 | 0 | crv = PK11_GETTAB(slot)->C_GetMechanismList(slot->slotID, NULL, &count); |
1137 | 0 | if (crv != CKR_OK) { |
1138 | 0 | if (!slot->isThreadSafe) |
1139 | 0 | PK11_ExitSlotMonitor(slot); |
1140 | 0 | PORT_SetError(PK11_MapError(crv)); |
1141 | 0 | return SECFailure; |
1142 | 0 | } |
1143 | 0 |
|
1144 | 0 | slot->mechanismList = (CK_MECHANISM_TYPE *) |
1145 | 0 | PORT_Alloc(count * sizeof(CK_MECHANISM_TYPE)); |
1146 | 0 | if (slot->mechanismList == NULL) { |
1147 | 0 | if (!slot->isThreadSafe) |
1148 | 0 | PK11_ExitSlotMonitor(slot); |
1149 | 0 | return SECFailure; |
1150 | 0 | } |
1151 | 0 | crv = PK11_GETTAB(slot)->C_GetMechanismList(slot->slotID, |
1152 | 0 | slot->mechanismList, &count); |
1153 | 0 | if (!slot->isThreadSafe) |
1154 | 0 | PK11_ExitSlotMonitor(slot); |
1155 | 0 | if (crv != CKR_OK) { |
1156 | 0 | PORT_Free(slot->mechanismList); |
1157 | 0 | slot->mechanismList = NULL; |
1158 | 0 | PORT_SetError(PK11_MapError(crv)); |
1159 | 0 | return SECSuccess; |
1160 | 0 | } |
1161 | 0 | slot->mechanismCount = count; |
1162 | 0 | PORT_Memset(slot->mechanismBits, 0, sizeof(slot->mechanismBits)); |
1163 | 0 |
|
1164 | 0 | for (i = 0; i < count; i++) { |
1165 | 0 | CK_MECHANISM_TYPE mech = slot->mechanismList[i]; |
1166 | 0 | if (mech < 0x7ff) { |
1167 | 0 | slot->mechanismBits[mech & 0xff] |= 1 << (mech >> 8); |
1168 | 0 | } |
1169 | 0 | } |
1170 | 0 | return SECSuccess; |
1171 | 0 | } |
1172 | | |
1173 | | /* |
1174 | | * initialize a new token |
1175 | | * unlike initialize slot, this can be called multiple times in the lifetime |
1176 | | * of NSS. It reads the information associated with a card or token, |
1177 | | * that is not going to change unless the card or token changes. |
1178 | | */ |
1179 | | SECStatus |
1180 | | PK11_InitToken(PK11SlotInfo *slot, PRBool loadCerts) |
1181 | 0 | { |
1182 | 0 | CK_RV crv; |
1183 | 0 | SECStatus rv; |
1184 | 0 | PRStatus status; |
1185 | 0 |
|
1186 | 0 | /* set the slot flags to the current token values */ |
1187 | 0 | if (!slot->isThreadSafe) |
1188 | 0 | PK11_EnterSlotMonitor(slot); |
1189 | 0 | crv = PK11_GETTAB(slot)->C_GetTokenInfo(slot->slotID, &slot->tokenInfo); |
1190 | 0 | if (!slot->isThreadSafe) |
1191 | 0 | PK11_ExitSlotMonitor(slot); |
1192 | 0 | if (crv != CKR_OK) { |
1193 | 0 | PORT_SetError(PK11_MapError(crv)); |
1194 | 0 | return SECFailure; |
1195 | 0 | } |
1196 | 0 |
|
1197 | 0 | /* set the slot flags to the current token values */ |
1198 | 0 | slot->series++; /* allow other objects to detect that the |
1199 | 0 | * slot is different */ |
1200 | 0 | slot->flags = slot->tokenInfo.flags; |
1201 | 0 | slot->needLogin = ((slot->tokenInfo.flags & CKF_LOGIN_REQUIRED) ? PR_TRUE : PR_FALSE); |
1202 | 0 | slot->readOnly = ((slot->tokenInfo.flags & CKF_WRITE_PROTECTED) ? PR_TRUE : PR_FALSE); |
1203 | 0 |
|
1204 | 0 | slot->hasRandom = ((slot->tokenInfo.flags & CKF_RNG) ? PR_TRUE : PR_FALSE); |
1205 | 0 | slot->protectedAuthPath = |
1206 | 0 | ((slot->tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) |
1207 | 0 | ? PR_TRUE |
1208 | 0 | : PR_FALSE); |
1209 | 0 | slot->lastLoginCheck = 0; |
1210 | 0 | slot->lastState = 0; |
1211 | 0 | /* on some platforms Active Card incorrectly sets the |
1212 | 0 | * CKF_PROTECTED_AUTHENTICATION_PATH bit when it doesn't mean to. */ |
1213 | 0 | if (slot->isActiveCard) { |
1214 | 0 | slot->protectedAuthPath = PR_FALSE; |
1215 | 0 | } |
1216 | 0 | (void)PK11_MakeString(NULL, slot->token_name, |
1217 | 0 | (char *)slot->tokenInfo.label, sizeof(slot->tokenInfo.label)); |
1218 | 0 | slot->minPassword = slot->tokenInfo.ulMinPinLen; |
1219 | 0 | slot->maxPassword = slot->tokenInfo.ulMaxPinLen; |
1220 | 0 | PORT_Memcpy(slot->serial, slot->tokenInfo.serialNumber, sizeof(slot->serial)); |
1221 | 0 |
|
1222 | 0 | nssToken_UpdateName(slot->nssToken); |
1223 | 0 |
|
1224 | 0 | slot->defRWSession = (PRBool)((!slot->readOnly) && |
1225 | 0 | (slot->tokenInfo.ulMaxSessionCount == 1)); |
1226 | 0 | rv = PK11_ReadMechanismList(slot); |
1227 | 0 | if (rv != SECSuccess) |
1228 | 0 | return rv; |
1229 | 0 | |
1230 | 0 | slot->hasRSAInfo = PR_FALSE; |
1231 | 0 | slot->RSAInfoFlags = 0; |
1232 | 0 |
|
1233 | 0 | /* initialize the maxKeyCount value */ |
1234 | 0 | if (slot->tokenInfo.ulMaxSessionCount == 0) { |
1235 | 0 | slot->maxKeyCount = 800; /* should be #define or a config param */ |
1236 | 0 | } else if (slot->tokenInfo.ulMaxSessionCount < 20) { |
1237 | 0 | /* don't have enough sessions to keep that many keys around */ |
1238 | 0 | slot->maxKeyCount = 0; |
1239 | 0 | } else { |
1240 | 0 | slot->maxKeyCount = slot->tokenInfo.ulMaxSessionCount / 2; |
1241 | 0 | } |
1242 | 0 |
|
1243 | 0 | /* Make sure our session handle is valid */ |
1244 | 0 | if (slot->session == CK_INVALID_SESSION) { |
1245 | 0 | /* we know we don't have a valid session, go get one */ |
1246 | 0 | CK_SESSION_HANDLE session; |
1247 | 0 |
|
1248 | 0 | /* session should be Readonly, serial */ |
1249 | 0 | if (!slot->isThreadSafe) |
1250 | 0 | PK11_EnterSlotMonitor(slot); |
1251 | 0 | crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID, |
1252 | 0 | (slot->defRWSession ? CKF_RW_SESSION : 0) | CKF_SERIAL_SESSION, |
1253 | 0 | slot, pk11_notify, &session); |
1254 | 0 | if (!slot->isThreadSafe) |
1255 | 0 | PK11_ExitSlotMonitor(slot); |
1256 | 0 | if (crv != CKR_OK) { |
1257 | 0 | PORT_SetError(PK11_MapError(crv)); |
1258 | 0 | return SECFailure; |
1259 | 0 | } |
1260 | 0 | slot->session = session; |
1261 | 0 | } else { |
1262 | 0 | /* The session we have may be defunct (the token associated with it) |
1263 | 0 | * has been removed */ |
1264 | 0 | CK_SESSION_INFO sessionInfo; |
1265 | 0 |
|
1266 | 0 | if (!slot->isThreadSafe) |
1267 | 0 | PK11_EnterSlotMonitor(slot); |
1268 | 0 | crv = PK11_GETTAB(slot)->C_GetSessionInfo(slot->session, &sessionInfo); |
1269 | 0 | if (crv == CKR_DEVICE_ERROR) { |
1270 | 0 | PK11_GETTAB(slot) |
1271 | 0 | ->C_CloseSession(slot->session); |
1272 | 0 | crv = CKR_SESSION_CLOSED; |
1273 | 0 | } |
1274 | 0 | if ((crv == CKR_SESSION_CLOSED) || (crv == CKR_SESSION_HANDLE_INVALID)) { |
1275 | 0 | crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID, |
1276 | 0 | (slot->defRWSession ? CKF_RW_SESSION : 0) | CKF_SERIAL_SESSION, |
1277 | 0 | slot, pk11_notify, &slot->session); |
1278 | 0 | if (crv != CKR_OK) { |
1279 | 0 | PORT_SetError(PK11_MapError(crv)); |
1280 | 0 | slot->session = CK_INVALID_SESSION; |
1281 | 0 | if (!slot->isThreadSafe) |
1282 | 0 | PK11_ExitSlotMonitor(slot); |
1283 | 0 | return SECFailure; |
1284 | 0 | } |
1285 | 0 | } |
1286 | 0 | if (!slot->isThreadSafe) |
1287 | 0 | PK11_ExitSlotMonitor(slot); |
1288 | 0 | } |
1289 | 0 |
|
1290 | 0 | status = nssToken_Refresh(slot->nssToken); |
1291 | 0 | if (status != PR_SUCCESS) |
1292 | 0 | return SECFailure; |
1293 | 0 | |
1294 | 0 | if (!(slot->isInternal) && (slot->hasRandom)) { |
1295 | 0 | /* if this slot has a random number generater, use it to add entropy |
1296 | 0 | * to the internal slot. */ |
1297 | 0 | PK11SlotInfo *int_slot = PK11_GetInternalSlot(); |
1298 | 0 |
|
1299 | 0 | if (int_slot) { |
1300 | 0 | unsigned char random_bytes[32]; |
1301 | 0 |
|
1302 | 0 | /* if this slot can issue random numbers, get some entropy from |
1303 | 0 | * that random number generater and give it to our internal token. |
1304 | 0 | */ |
1305 | 0 | PK11_EnterSlotMonitor(slot); |
1306 | 0 | crv = PK11_GETTAB(slot)->C_GenerateRandom(slot->session, random_bytes, sizeof(random_bytes)); |
1307 | 0 | PK11_ExitSlotMonitor(slot); |
1308 | 0 | if (crv == CKR_OK) { |
1309 | 0 | PK11_EnterSlotMonitor(int_slot); |
1310 | 0 | PK11_GETTAB(int_slot) |
1311 | 0 | ->C_SeedRandom(int_slot->session, |
1312 | 0 | random_bytes, sizeof(random_bytes)); |
1313 | 0 | PK11_ExitSlotMonitor(int_slot); |
1314 | 0 | } |
1315 | 0 |
|
1316 | 0 | /* Now return the favor and send entropy to the token's random |
1317 | 0 | * number generater */ |
1318 | 0 | PK11_EnterSlotMonitor(int_slot); |
1319 | 0 | crv = PK11_GETTAB(int_slot)->C_GenerateRandom(int_slot->session, |
1320 | 0 | random_bytes, sizeof(random_bytes)); |
1321 | 0 | PK11_ExitSlotMonitor(int_slot); |
1322 | 0 | if (crv == CKR_OK) { |
1323 | 0 | PK11_EnterSlotMonitor(slot); |
1324 | 0 | crv = PK11_GETTAB(slot)->C_SeedRandom(slot->session, |
1325 | 0 | random_bytes, sizeof(random_bytes)); |
1326 | 0 | PK11_ExitSlotMonitor(slot); |
1327 | 0 | } |
1328 | 0 | PK11_FreeSlot(int_slot); |
1329 | 0 | } |
1330 | 0 | } |
1331 | 0 | /* work around a problem in softoken where it incorrectly |
1332 | 0 | * reports databases opened read only as read/write. */ |
1333 | 0 | if (slot->isInternal && !slot->readOnly) { |
1334 | 0 | CK_SESSION_HANDLE session = CK_INVALID_SESSION; |
1335 | 0 |
|
1336 | 0 | /* try to open a R/W session */ |
1337 | 0 | crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID, |
1338 | 0 | CKF_RW_SESSION | CKF_SERIAL_SESSION, slot, pk11_notify, &session); |
1339 | 0 | /* what a well behaved token should return if you open |
1340 | 0 | * a RW session on a read only token */ |
1341 | 0 | if (crv == CKR_TOKEN_WRITE_PROTECTED) { |
1342 | 0 | slot->readOnly = PR_TRUE; |
1343 | 0 | } else if (crv == CKR_OK) { |
1344 | 0 | CK_SESSION_INFO sessionInfo; |
1345 | 0 |
|
1346 | 0 | /* Because of a second bug in softoken, which silently returns |
1347 | 0 | * a RO session, we need to check what type of session we got. */ |
1348 | 0 | crv = PK11_GETTAB(slot)->C_GetSessionInfo(session, &sessionInfo); |
1349 | 0 | if (crv == CKR_OK) { |
1350 | 0 | if ((sessionInfo.flags & CKF_RW_SESSION) == 0) { |
1351 | 0 | /* session was readonly, so this softoken slot must be readonly */ |
1352 | 0 | slot->readOnly = PR_TRUE; |
1353 | 0 | } |
1354 | 0 | } |
1355 | 0 | PK11_GETTAB(slot) |
1356 | 0 | ->C_CloseSession(session); |
1357 | 0 | } |
1358 | 0 | } |
1359 | 0 |
|
1360 | 0 | return SECSuccess; |
1361 | 0 | } |
1362 | | |
1363 | | /* |
1364 | | * initialize a new token |
1365 | | * unlike initialize slot, this can be called multiple times in the lifetime |
1366 | | * of NSS. It reads the information associated with a card or token, |
1367 | | * that is not going to change unless the card or token changes. |
1368 | | */ |
1369 | | SECStatus |
1370 | | PK11_TokenRefresh(PK11SlotInfo *slot) |
1371 | 0 | { |
1372 | 0 | CK_RV crv; |
1373 | 0 |
|
1374 | 0 | /* set the slot flags to the current token values */ |
1375 | 0 | if (!slot->isThreadSafe) |
1376 | 0 | PK11_EnterSlotMonitor(slot); |
1377 | 0 | crv = PK11_GETTAB(slot)->C_GetTokenInfo(slot->slotID, &slot->tokenInfo); |
1378 | 0 | if (!slot->isThreadSafe) |
1379 | 0 | PK11_ExitSlotMonitor(slot); |
1380 | 0 | if (crv != CKR_OK) { |
1381 | 0 | PORT_SetError(PK11_MapError(crv)); |
1382 | 0 | return SECFailure; |
1383 | 0 | } |
1384 | 0 |
|
1385 | 0 | slot->flags = slot->tokenInfo.flags; |
1386 | 0 | slot->needLogin = ((slot->tokenInfo.flags & CKF_LOGIN_REQUIRED) ? PR_TRUE : PR_FALSE); |
1387 | 0 | slot->readOnly = ((slot->tokenInfo.flags & CKF_WRITE_PROTECTED) ? PR_TRUE : PR_FALSE); |
1388 | 0 | slot->hasRandom = ((slot->tokenInfo.flags & CKF_RNG) ? PR_TRUE : PR_FALSE); |
1389 | 0 | slot->protectedAuthPath = |
1390 | 0 | ((slot->tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) |
1391 | 0 | ? PR_TRUE |
1392 | 0 | : PR_FALSE); |
1393 | 0 | /* on some platforms Active Card incorrectly sets the |
1394 | 0 | * CKF_PROTECTED_AUTHENTICATION_PATH bit when it doesn't mean to. */ |
1395 | 0 | if (slot->isActiveCard) { |
1396 | 0 | slot->protectedAuthPath = PR_FALSE; |
1397 | 0 | } |
1398 | 0 | return SECSuccess; |
1399 | 0 | } |
1400 | | |
1401 | | static PRBool |
1402 | | pk11_isRootSlot(PK11SlotInfo *slot) |
1403 | 0 | { |
1404 | 0 | CK_ATTRIBUTE findTemp[1]; |
1405 | 0 | CK_ATTRIBUTE *attrs; |
1406 | 0 | CK_OBJECT_CLASS oclass = CKO_NETSCAPE_BUILTIN_ROOT_LIST; |
1407 | 0 | int tsize; |
1408 | 0 | CK_OBJECT_HANDLE handle; |
1409 | 0 |
|
1410 | 0 | attrs = findTemp; |
1411 | 0 | PK11_SETATTRS(attrs, CKA_CLASS, &oclass, sizeof(oclass)); |
1412 | 0 | attrs++; |
1413 | 0 | tsize = attrs - findTemp; |
1414 | 0 | PORT_Assert(tsize <= sizeof(findTemp) / sizeof(CK_ATTRIBUTE)); |
1415 | 0 |
|
1416 | 0 | handle = pk11_FindObjectByTemplate(slot, findTemp, tsize); |
1417 | 0 | if (handle == CK_INVALID_HANDLE) { |
1418 | 0 | return PR_FALSE; |
1419 | 0 | } |
1420 | 0 | return PR_TRUE; |
1421 | 0 | } |
1422 | | |
1423 | | /* |
1424 | | * Initialize the slot : |
1425 | | * This initialization code is called on each slot a module supports when |
1426 | | * it is loaded. It does the bringup initialization. The difference between |
1427 | | * this and InitToken is Init slot does those one time initialization stuff, |
1428 | | * usually associated with the reader, while InitToken may get called multiple |
1429 | | * times as tokens are removed and re-inserted. |
1430 | | */ |
1431 | | void |
1432 | | PK11_InitSlot(SECMODModule *mod, CK_SLOT_ID slotID, PK11SlotInfo *slot) |
1433 | 0 | { |
1434 | 0 | SECStatus rv; |
1435 | 0 | CK_SLOT_INFO slotInfo; |
1436 | 0 |
|
1437 | 0 | slot->functionList = mod->functionList; |
1438 | 0 | slot->isInternal = mod->internal; |
1439 | 0 | slot->slotID = slotID; |
1440 | 0 | slot->isThreadSafe = mod->isThreadSafe; |
1441 | 0 | slot->hasRSAInfo = PR_FALSE; |
1442 | 0 |
|
1443 | 0 | if (PK11_GETTAB(slot)->C_GetSlotInfo(slotID, &slotInfo) != CKR_OK) { |
1444 | 0 | slot->disabled = PR_TRUE; |
1445 | 0 | slot->reason = PK11_DIS_COULD_NOT_INIT_TOKEN; |
1446 | 0 | return; |
1447 | 0 | } |
1448 | 0 |
|
1449 | 0 | /* test to make sure claimed mechanism work */ |
1450 | 0 | slot->needTest = mod->internal ? PR_FALSE : PR_TRUE; |
1451 | 0 | slot->module = mod; /* NOTE: we don't make a reference here because |
1452 | 0 | * modules have references to their slots. This |
1453 | 0 | * works because modules keep implicit references |
1454 | 0 | * from their slots, and won't unload and disappear |
1455 | 0 | * until all their slots have been freed */ |
1456 | 0 | (void)PK11_MakeString(NULL, slot->slot_name, |
1457 | 0 | (char *)slotInfo.slotDescription, sizeof(slotInfo.slotDescription)); |
1458 | 0 | slot->isHW = (PRBool)((slotInfo.flags & CKF_HW_SLOT) == CKF_HW_SLOT); |
1459 | 0 | #define ACTIVE_CARD "ActivCard SA" |
1460 | 0 | slot->isActiveCard = (PRBool)(PORT_Strncmp((char *)slotInfo.manufacturerID, |
1461 | 0 | ACTIVE_CARD, sizeof(ACTIVE_CARD) - 1) == 0); |
1462 | 0 | if ((slotInfo.flags & CKF_REMOVABLE_DEVICE) == 0) { |
1463 | 0 | slot->isPerm = PR_TRUE; |
1464 | 0 | /* permanment slots must have the token present always */ |
1465 | 0 | if ((slotInfo.flags & CKF_TOKEN_PRESENT) == 0) { |
1466 | 0 | slot->disabled = PR_TRUE; |
1467 | 0 | slot->reason = PK11_DIS_TOKEN_NOT_PRESENT; |
1468 | 0 | return; /* nothing else to do */ |
1469 | 0 | } |
1470 | 0 | } |
1471 | 0 | /* if the token is present, initialize it */ |
1472 | 0 | if ((slotInfo.flags & CKF_TOKEN_PRESENT) != 0) { |
1473 | 0 | rv = PK11_InitToken(slot, PR_TRUE); |
1474 | 0 | /* the only hard failures are on permanent devices, or function |
1475 | 0 | * verify failures... function verify failures are already handled |
1476 | 0 | * by tokenInit */ |
1477 | 0 | if ((rv != SECSuccess) && (slot->isPerm) && (!slot->disabled)) { |
1478 | 0 | slot->disabled = PR_TRUE; |
1479 | 0 | slot->reason = PK11_DIS_COULD_NOT_INIT_TOKEN; |
1480 | 0 | } |
1481 | 0 | if (rv == SECSuccess && pk11_isRootSlot(slot)) { |
1482 | 0 | if (!slot->hasRootCerts) { |
1483 | 0 | slot->module->trustOrder = 100; |
1484 | 0 | } |
1485 | 0 | slot->hasRootCerts = PR_TRUE; |
1486 | 0 | } |
1487 | 0 | } |
1488 | 0 | if ((slotInfo.flags & CKF_USER_PIN_INITIALIZED) != 0) { |
1489 | 0 | slot->flags |= CKF_USER_PIN_INITIALIZED; |
1490 | 0 | } |
1491 | 0 | } |
1492 | | |
1493 | | /********************************************************************* |
1494 | | * Slot mapping utility functions. |
1495 | | *********************************************************************/ |
1496 | | |
1497 | | /* |
1498 | | * determine if the token is present. If the token is present, make sure |
1499 | | * we have a valid session handle. Also set the value of needLogin |
1500 | | * appropriately. |
1501 | | */ |
1502 | | static PRBool |
1503 | | pk11_IsPresentCertLoad(PK11SlotInfo *slot, PRBool loadCerts) |
1504 | 0 | { |
1505 | 0 | CK_SLOT_INFO slotInfo; |
1506 | 0 | CK_SESSION_INFO sessionInfo; |
1507 | 0 | CK_RV crv; |
1508 | 0 |
|
1509 | 0 | /* disabled slots are never present */ |
1510 | 0 | if (slot->disabled) { |
1511 | 0 | return PR_FALSE; |
1512 | 0 | } |
1513 | 0 |
|
1514 | 0 | /* permanent slots are always present */ |
1515 | 0 | if (slot->isPerm && (slot->session != CK_INVALID_SESSION)) { |
1516 | 0 | return PR_TRUE; |
1517 | 0 | } |
1518 | 0 |
|
1519 | 0 | if (slot->nssToken) { |
1520 | 0 | return nssToken_IsPresent(slot->nssToken); |
1521 | 0 | } |
1522 | 0 | |
1523 | 0 | /* removable slots have a flag that says they are present */ |
1524 | 0 | if (!slot->isThreadSafe) |
1525 | 0 | PK11_EnterSlotMonitor(slot); |
1526 | 0 | if (PK11_GETTAB(slot)->C_GetSlotInfo(slot->slotID, &slotInfo) != CKR_OK) { |
1527 | 0 | if (!slot->isThreadSafe) |
1528 | 0 | PK11_ExitSlotMonitor(slot); |
1529 | 0 | return PR_FALSE; |
1530 | 0 | } |
1531 | 0 | if ((slotInfo.flags & CKF_TOKEN_PRESENT) == 0) { |
1532 | 0 | /* if the slot is no longer present, close the session */ |
1533 | 0 | if (slot->session != CK_INVALID_SESSION) { |
1534 | 0 | PK11_GETTAB(slot) |
1535 | 0 | ->C_CloseSession(slot->session); |
1536 | 0 | slot->session = CK_INVALID_SESSION; |
1537 | 0 | } |
1538 | 0 | if (!slot->isThreadSafe) |
1539 | 0 | PK11_ExitSlotMonitor(slot); |
1540 | 0 | return PR_FALSE; |
1541 | 0 | } |
1542 | 0 |
|
1543 | 0 | /* use the session Info to determine if the card has been removed and then |
1544 | 0 | * re-inserted */ |
1545 | 0 | if (slot->session != CK_INVALID_SESSION) { |
1546 | 0 | if (slot->isThreadSafe) |
1547 | 0 | PK11_EnterSlotMonitor(slot); |
1548 | 0 | crv = PK11_GETTAB(slot)->C_GetSessionInfo(slot->session, &sessionInfo); |
1549 | 0 | if (crv != CKR_OK) { |
1550 | 0 | PK11_GETTAB(slot) |
1551 | 0 | ->C_CloseSession(slot->session); |
1552 | 0 | slot->session = CK_INVALID_SESSION; |
1553 | 0 | } |
1554 | 0 | if (slot->isThreadSafe) |
1555 | 0 | PK11_ExitSlotMonitor(slot); |
1556 | 0 | } |
1557 | 0 | if (!slot->isThreadSafe) |
1558 | 0 | PK11_ExitSlotMonitor(slot); |
1559 | 0 |
|
1560 | 0 | /* card has not been removed, current token info is correct */ |
1561 | 0 | if (slot->session != CK_INVALID_SESSION) |
1562 | 0 | return PR_TRUE; |
1563 | 0 | |
1564 | 0 | /* initialize the token info state */ |
1565 | 0 | if (PK11_InitToken(slot, loadCerts) != SECSuccess) { |
1566 | 0 | return PR_FALSE; |
1567 | 0 | } |
1568 | 0 |
|
1569 | 0 | return PR_TRUE; |
1570 | 0 | } |
1571 | | |
1572 | | /* |
1573 | | * old version of the routine |
1574 | | */ |
1575 | | PRBool |
1576 | | PK11_IsPresent(PK11SlotInfo *slot) |
1577 | 0 | { |
1578 | 0 | return pk11_IsPresentCertLoad(slot, PR_TRUE); |
1579 | 0 | } |
1580 | | |
1581 | | /* is the slot disabled? */ |
1582 | | PRBool |
1583 | | PK11_IsDisabled(PK11SlotInfo *slot) |
1584 | 0 | { |
1585 | 0 | return slot->disabled; |
1586 | 0 | } |
1587 | | |
1588 | | /* and why? */ |
1589 | | PK11DisableReasons |
1590 | | PK11_GetDisabledReason(PK11SlotInfo *slot) |
1591 | 0 | { |
1592 | 0 | return slot->reason; |
1593 | 0 | } |
1594 | | |
1595 | | /* returns PR_TRUE if successfully disable the slot */ |
1596 | | /* returns PR_FALSE otherwise */ |
1597 | | PRBool |
1598 | | PK11_UserDisableSlot(PK11SlotInfo *slot) |
1599 | 0 | { |
1600 | 0 |
|
1601 | 0 | /* Prevent users from disabling the internal module. */ |
1602 | 0 | if (slot->isInternal) { |
1603 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1604 | 0 | return PR_FALSE; |
1605 | 0 | } |
1606 | 0 |
|
1607 | 0 | slot->defaultFlags |= PK11_DISABLE_FLAG; |
1608 | 0 | slot->disabled = PR_TRUE; |
1609 | 0 | slot->reason = PK11_DIS_USER_SELECTED; |
1610 | 0 |
|
1611 | 0 | return PR_TRUE; |
1612 | 0 | } |
1613 | | |
1614 | | PRBool |
1615 | | PK11_UserEnableSlot(PK11SlotInfo *slot) |
1616 | 0 | { |
1617 | 0 |
|
1618 | 0 | slot->defaultFlags &= ~PK11_DISABLE_FLAG; |
1619 | 0 | slot->disabled = PR_FALSE; |
1620 | 0 | slot->reason = PK11_DIS_NONE; |
1621 | 0 | return PR_TRUE; |
1622 | 0 | } |
1623 | | |
1624 | | PRBool |
1625 | | PK11_HasRootCerts(PK11SlotInfo *slot) |
1626 | 0 | { |
1627 | 0 | return slot->hasRootCerts; |
1628 | 0 | } |
1629 | | |
1630 | | /* Get the module this slot is attached to */ |
1631 | | SECMODModule * |
1632 | | PK11_GetModule(PK11SlotInfo *slot) |
1633 | 0 | { |
1634 | 0 | return slot->module; |
1635 | 0 | } |
1636 | | |
1637 | | /* return the default flags of a slot */ |
1638 | | unsigned long |
1639 | | PK11_GetDefaultFlags(PK11SlotInfo *slot) |
1640 | 0 | { |
1641 | 0 | return slot->defaultFlags; |
1642 | 0 | } |
1643 | | |
1644 | | /* |
1645 | | * The following wrapper functions allow us to export an opaque slot |
1646 | | * function to the rest of libsec and the world... */ |
1647 | | PRBool |
1648 | | PK11_IsReadOnly(PK11SlotInfo *slot) |
1649 | 0 | { |
1650 | 0 | return slot->readOnly; |
1651 | 0 | } |
1652 | | |
1653 | | PRBool |
1654 | | PK11_IsHW(PK11SlotInfo *slot) |
1655 | 0 | { |
1656 | 0 | return slot->isHW; |
1657 | 0 | } |
1658 | | |
1659 | | PRBool |
1660 | | PK11_IsRemovable(PK11SlotInfo *slot) |
1661 | 0 | { |
1662 | 0 | return !slot->isPerm; |
1663 | 0 | } |
1664 | | |
1665 | | PRBool |
1666 | | PK11_IsInternal(PK11SlotInfo *slot) |
1667 | 0 | { |
1668 | 0 | return slot->isInternal; |
1669 | 0 | } |
1670 | | |
1671 | | PRBool |
1672 | | PK11_IsInternalKeySlot(PK11SlotInfo *slot) |
1673 | 0 | { |
1674 | 0 | PK11SlotInfo *int_slot; |
1675 | 0 | PRBool result; |
1676 | 0 |
|
1677 | 0 | if (!slot->isInternal) { |
1678 | 0 | return PR_FALSE; |
1679 | 0 | } |
1680 | 0 |
|
1681 | 0 | int_slot = PK11_GetInternalKeySlot(); |
1682 | 0 | result = (int_slot == slot) ? PR_TRUE : PR_FALSE; |
1683 | 0 | PK11_FreeSlot(int_slot); |
1684 | 0 | return result; |
1685 | 0 | } |
1686 | | |
1687 | | PRBool |
1688 | | PK11_NeedLogin(PK11SlotInfo *slot) |
1689 | 0 | { |
1690 | 0 | return slot->needLogin; |
1691 | 0 | } |
1692 | | |
1693 | | PRBool |
1694 | | PK11_IsFriendly(PK11SlotInfo *slot) |
1695 | 0 | { |
1696 | 0 | /* internal slot always has public readable certs */ |
1697 | 0 | return (PRBool)(slot->isInternal || |
1698 | 0 | ((slot->defaultFlags & SECMOD_FRIENDLY_FLAG) == |
1699 | 0 | SECMOD_FRIENDLY_FLAG)); |
1700 | 0 | } |
1701 | | |
1702 | | char * |
1703 | | PK11_GetTokenName(PK11SlotInfo *slot) |
1704 | 0 | { |
1705 | 0 | return slot->token_name; |
1706 | 0 | } |
1707 | | |
1708 | | char * |
1709 | | PK11_GetTokenURI(PK11SlotInfo *slot) |
1710 | 0 | { |
1711 | 0 | PK11URI *uri; |
1712 | 0 | char *ret = NULL; |
1713 | 0 | char label[32 + 1], manufacturer[32 + 1], serial[16 + 1], model[16 + 1]; |
1714 | 0 | PK11URIAttribute attrs[4]; |
1715 | 0 | size_t nattrs = 0; |
1716 | 0 |
|
1717 | 0 | PK11_MakeString(NULL, label, (char *)slot->tokenInfo.label, |
1718 | 0 | sizeof(slot->tokenInfo.label)); |
1719 | 0 | if (*label != '\0') { |
1720 | 0 | attrs[nattrs].name = PK11URI_PATTR_TOKEN; |
1721 | 0 | attrs[nattrs].value = label; |
1722 | 0 | nattrs++; |
1723 | 0 | } |
1724 | 0 |
|
1725 | 0 | PK11_MakeString(NULL, manufacturer, (char *)slot->tokenInfo.manufacturerID, |
1726 | 0 | sizeof(slot->tokenInfo.manufacturerID)); |
1727 | 0 | if (*manufacturer != '\0') { |
1728 | 0 | attrs[nattrs].name = PK11URI_PATTR_MANUFACTURER; |
1729 | 0 | attrs[nattrs].value = manufacturer; |
1730 | 0 | nattrs++; |
1731 | 0 | } |
1732 | 0 |
|
1733 | 0 | PK11_MakeString(NULL, serial, (char *)slot->tokenInfo.serialNumber, |
1734 | 0 | sizeof(slot->tokenInfo.serialNumber)); |
1735 | 0 | if (*serial != '\0') { |
1736 | 0 | attrs[nattrs].name = PK11URI_PATTR_SERIAL; |
1737 | 0 | attrs[nattrs].value = serial; |
1738 | 0 | nattrs++; |
1739 | 0 | } |
1740 | 0 |
|
1741 | 0 | PK11_MakeString(NULL, model, (char *)slot->tokenInfo.model, |
1742 | 0 | sizeof(slot->tokenInfo.model)); |
1743 | 0 | if (*model != '\0') { |
1744 | 0 | attrs[nattrs].name = PK11URI_PATTR_MODEL; |
1745 | 0 | attrs[nattrs].value = model; |
1746 | 0 | nattrs++; |
1747 | 0 | } |
1748 | 0 |
|
1749 | 0 | uri = PK11URI_CreateURI(attrs, nattrs, NULL, 0); |
1750 | 0 | if (uri == NULL) { |
1751 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1752 | 0 | return NULL; |
1753 | 0 | } |
1754 | 0 |
|
1755 | 0 | ret = PK11URI_FormatURI(NULL, uri); |
1756 | 0 | PK11URI_DestroyURI(uri); |
1757 | 0 |
|
1758 | 0 | if (ret == NULL) { |
1759 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1760 | 0 | } |
1761 | 0 |
|
1762 | 0 | return ret; |
1763 | 0 | } |
1764 | | |
1765 | | char * |
1766 | | PK11_GetSlotName(PK11SlotInfo *slot) |
1767 | 0 | { |
1768 | 0 | return slot->slot_name; |
1769 | 0 | } |
1770 | | |
1771 | | int |
1772 | | PK11_GetSlotSeries(PK11SlotInfo *slot) |
1773 | 0 | { |
1774 | 0 | return slot->series; |
1775 | 0 | } |
1776 | | |
1777 | | int |
1778 | | PK11_GetCurrentWrapIndex(PK11SlotInfo *slot) |
1779 | 0 | { |
1780 | 0 | return slot->wrapKey; |
1781 | 0 | } |
1782 | | |
1783 | | CK_SLOT_ID |
1784 | | PK11_GetSlotID(PK11SlotInfo *slot) |
1785 | 0 | { |
1786 | 0 | return slot->slotID; |
1787 | 0 | } |
1788 | | |
1789 | | SECMODModuleID |
1790 | | PK11_GetModuleID(PK11SlotInfo *slot) |
1791 | 0 | { |
1792 | 0 | return slot->module->moduleID; |
1793 | 0 | } |
1794 | | |
1795 | | static void |
1796 | | pk11_zeroTerminatedToBlankPadded(CK_CHAR *buffer, size_t buffer_size) |
1797 | 0 | { |
1798 | 0 | CK_CHAR *walk = buffer; |
1799 | 0 | CK_CHAR *end = buffer + buffer_size; |
1800 | 0 |
|
1801 | 0 | /* find the NULL */ |
1802 | 0 | while (walk < end && *walk != '\0') { |
1803 | 0 | walk++; |
1804 | 0 | } |
1805 | 0 |
|
1806 | 0 | /* clear out the buffer */ |
1807 | 0 | while (walk < end) { |
1808 | 0 | *walk++ = ' '; |
1809 | 0 | } |
1810 | 0 | } |
1811 | | |
1812 | | /* return the slot info structure */ |
1813 | | SECStatus |
1814 | | PK11_GetSlotInfo(PK11SlotInfo *slot, CK_SLOT_INFO *info) |
1815 | 0 | { |
1816 | 0 | CK_RV crv; |
1817 | 0 |
|
1818 | 0 | if (!slot->isThreadSafe) |
1819 | 0 | PK11_EnterSlotMonitor(slot); |
1820 | 0 | /* |
1821 | 0 | * some buggy drivers do not fill the buffer completely, |
1822 | 0 | * erase the buffer first |
1823 | 0 | */ |
1824 | 0 | PORT_Memset(info->slotDescription, ' ', sizeof(info->slotDescription)); |
1825 | 0 | PORT_Memset(info->manufacturerID, ' ', sizeof(info->manufacturerID)); |
1826 | 0 | crv = PK11_GETTAB(slot)->C_GetSlotInfo(slot->slotID, info); |
1827 | 0 | pk11_zeroTerminatedToBlankPadded(info->slotDescription, |
1828 | 0 | sizeof(info->slotDescription)); |
1829 | 0 | pk11_zeroTerminatedToBlankPadded(info->manufacturerID, |
1830 | 0 | sizeof(info->manufacturerID)); |
1831 | 0 | if (!slot->isThreadSafe) |
1832 | 0 | PK11_ExitSlotMonitor(slot); |
1833 | 0 | if (crv != CKR_OK) { |
1834 | 0 | PORT_SetError(PK11_MapError(crv)); |
1835 | 0 | return SECFailure; |
1836 | 0 | } |
1837 | 0 | return SECSuccess; |
1838 | 0 | } |
1839 | | |
1840 | | /* return the token info structure */ |
1841 | | SECStatus |
1842 | | PK11_GetTokenInfo(PK11SlotInfo *slot, CK_TOKEN_INFO *info) |
1843 | 0 | { |
1844 | 0 | CK_RV crv; |
1845 | 0 | if (!slot->isThreadSafe) |
1846 | 0 | PK11_EnterSlotMonitor(slot); |
1847 | 0 | /* |
1848 | 0 | * some buggy drivers do not fill the buffer completely, |
1849 | 0 | * erase the buffer first |
1850 | 0 | */ |
1851 | 0 | PORT_Memset(info->label, ' ', sizeof(info->label)); |
1852 | 0 | PORT_Memset(info->manufacturerID, ' ', sizeof(info->manufacturerID)); |
1853 | 0 | PORT_Memset(info->model, ' ', sizeof(info->model)); |
1854 | 0 | PORT_Memset(info->serialNumber, ' ', sizeof(info->serialNumber)); |
1855 | 0 | crv = PK11_GETTAB(slot)->C_GetTokenInfo(slot->slotID, info); |
1856 | 0 | pk11_zeroTerminatedToBlankPadded(info->label, sizeof(info->label)); |
1857 | 0 | pk11_zeroTerminatedToBlankPadded(info->manufacturerID, |
1858 | 0 | sizeof(info->manufacturerID)); |
1859 | 0 | pk11_zeroTerminatedToBlankPadded(info->model, sizeof(info->model)); |
1860 | 0 | pk11_zeroTerminatedToBlankPadded(info->serialNumber, |
1861 | 0 | sizeof(info->serialNumber)); |
1862 | 0 | if (!slot->isThreadSafe) |
1863 | 0 | PK11_ExitSlotMonitor(slot); |
1864 | 0 | if (crv != CKR_OK) { |
1865 | 0 | PORT_SetError(PK11_MapError(crv)); |
1866 | 0 | return SECFailure; |
1867 | 0 | } |
1868 | 0 | return SECSuccess; |
1869 | 0 | } |
1870 | | |
1871 | | PRBool |
1872 | | pk11_MatchUriTokenInfo(PK11SlotInfo *slot, PK11URI *uri) |
1873 | 0 | { |
1874 | 0 | const char *value; |
1875 | 0 |
|
1876 | 0 | value = PK11URI_GetPathAttribute(uri, PK11URI_PATTR_TOKEN); |
1877 | 0 | if (value) { |
1878 | 0 | if (!pk11_MatchString(value, (char *)slot->tokenInfo.label, |
1879 | 0 | sizeof(slot->tokenInfo.label))) { |
1880 | 0 | return PR_FALSE; |
1881 | 0 | } |
1882 | 0 | } |
1883 | 0 |
|
1884 | 0 | value = PK11URI_GetPathAttribute(uri, PK11URI_PATTR_MANUFACTURER); |
1885 | 0 | if (value) { |
1886 | 0 | if (!pk11_MatchString(value, (char *)slot->tokenInfo.manufacturerID, |
1887 | 0 | sizeof(slot->tokenInfo.manufacturerID))) { |
1888 | 0 | return PR_FALSE; |
1889 | 0 | } |
1890 | 0 | } |
1891 | 0 |
|
1892 | 0 | value = PK11URI_GetPathAttribute(uri, PK11URI_PATTR_SERIAL); |
1893 | 0 | if (value) { |
1894 | 0 | if (!pk11_MatchString(value, (char *)slot->tokenInfo.serialNumber, |
1895 | 0 | sizeof(slot->tokenInfo.serialNumber))) { |
1896 | 0 | return PR_FALSE; |
1897 | 0 | } |
1898 | 0 | } |
1899 | 0 |
|
1900 | 0 | value = PK11URI_GetPathAttribute(uri, PK11URI_PATTR_MODEL); |
1901 | 0 | if (value) { |
1902 | 0 | if (!pk11_MatchString(value, (char *)slot->tokenInfo.model, |
1903 | 0 | sizeof(slot->tokenInfo.model))) { |
1904 | 0 | return PR_FALSE; |
1905 | 0 | } |
1906 | 0 | } |
1907 | 0 |
|
1908 | 0 | return PR_TRUE; |
1909 | 0 | } |
1910 | | |
1911 | | /* Find out if we need to initialize the user's pin */ |
1912 | | PRBool |
1913 | | PK11_NeedUserInit(PK11SlotInfo *slot) |
1914 | 0 | { |
1915 | 0 | PRBool needUserInit = (PRBool)((slot->flags & CKF_USER_PIN_INITIALIZED) == 0); |
1916 | 0 |
|
1917 | 0 | if (needUserInit) { |
1918 | 0 | CK_TOKEN_INFO info; |
1919 | 0 | SECStatus rv; |
1920 | 0 |
|
1921 | 0 | /* see if token has been initialized off line */ |
1922 | 0 | rv = PK11_GetTokenInfo(slot, &info); |
1923 | 0 | if (rv == SECSuccess) { |
1924 | 0 | slot->flags = info.flags; |
1925 | 0 | } |
1926 | 0 | } |
1927 | 0 | return (PRBool)((slot->flags & CKF_USER_PIN_INITIALIZED) == 0); |
1928 | 0 | } |
1929 | | |
1930 | | static PK11SlotInfo *pk11InternalKeySlot = NULL; |
1931 | | |
1932 | | /* |
1933 | | * Set a new default internal keyslot. If one has already been set, clear it. |
1934 | | * Passing NULL falls back to the NSS normally selected default internal key |
1935 | | * slot. |
1936 | | */ |
1937 | | void |
1938 | | pk11_SetInternalKeySlot(PK11SlotInfo *slot) |
1939 | 0 | { |
1940 | 0 | if (pk11InternalKeySlot) { |
1941 | 0 | PK11_FreeSlot(pk11InternalKeySlot); |
1942 | 0 | } |
1943 | 0 | pk11InternalKeySlot = slot ? PK11_ReferenceSlot(slot) : NULL; |
1944 | 0 | } |
1945 | | |
1946 | | /* |
1947 | | * Set a new default internal keyslot if the normal key slot has not already |
1948 | | * been overridden. Subsequent calls to this function will be ignored unless |
1949 | | * pk11_SetInternalKeySlot is used to clear the current default. |
1950 | | */ |
1951 | | void |
1952 | | pk11_SetInternalKeySlotIfFirst(PK11SlotInfo *slot) |
1953 | 0 | { |
1954 | 0 | if (pk11InternalKeySlot) { |
1955 | 0 | return; |
1956 | 0 | } |
1957 | 0 | pk11InternalKeySlot = slot ? PK11_ReferenceSlot(slot) : NULL; |
1958 | 0 | } |
1959 | | |
1960 | | /* |
1961 | | * Swap out a default internal keyslot. Caller owns the Slot Reference |
1962 | | */ |
1963 | | PK11SlotInfo * |
1964 | | pk11_SwapInternalKeySlot(PK11SlotInfo *slot) |
1965 | 0 | { |
1966 | 0 | PK11SlotInfo *swap = pk11InternalKeySlot; |
1967 | 0 |
|
1968 | 0 | pk11InternalKeySlot = slot ? PK11_ReferenceSlot(slot) : NULL; |
1969 | 0 | return swap; |
1970 | 0 | } |
1971 | | |
1972 | | /* get the internal key slot. FIPS has only one slot for both key slots and |
1973 | | * default slots */ |
1974 | | PK11SlotInfo * |
1975 | | PK11_GetInternalKeySlot(void) |
1976 | 0 | { |
1977 | 0 | SECMODModule *mod; |
1978 | 0 |
|
1979 | 0 | if (pk11InternalKeySlot) { |
1980 | 0 | return PK11_ReferenceSlot(pk11InternalKeySlot); |
1981 | 0 | } |
1982 | 0 | |
1983 | 0 | mod = SECMOD_GetInternalModule(); |
1984 | 0 | PORT_Assert(mod != NULL); |
1985 | 0 | if (!mod) { |
1986 | 0 | PORT_SetError(SEC_ERROR_NO_MODULE); |
1987 | 0 | return NULL; |
1988 | 0 | } |
1989 | 0 | return PK11_ReferenceSlot(mod->isFIPS ? mod->slots[0] : mod->slots[1]); |
1990 | 0 | } |
1991 | | |
1992 | | /* get the internal default slot */ |
1993 | | PK11SlotInfo * |
1994 | | PK11_GetInternalSlot(void) |
1995 | 0 | { |
1996 | 0 | SECMODModule *mod = SECMOD_GetInternalModule(); |
1997 | 0 | PORT_Assert(mod != NULL); |
1998 | 0 | if (!mod) { |
1999 | 0 | PORT_SetError(SEC_ERROR_NO_MODULE); |
2000 | 0 | return NULL; |
2001 | 0 | } |
2002 | 0 | if (mod->isFIPS) { |
2003 | 0 | return PK11_GetInternalKeySlot(); |
2004 | 0 | } |
2005 | 0 | return PK11_ReferenceSlot(mod->slots[0]); |
2006 | 0 | } |
2007 | | |
2008 | | /* |
2009 | | * check if a given slot supports the requested mechanism |
2010 | | */ |
2011 | | PRBool |
2012 | | PK11_DoesMechanism(PK11SlotInfo *slot, CK_MECHANISM_TYPE type) |
2013 | 0 | { |
2014 | 0 | int i; |
2015 | 0 |
|
2016 | 0 | /* CKM_FAKE_RANDOM is not a real PKCS mechanism. It's a marker to |
2017 | 0 | * tell us we're looking form someone that has implemented get |
2018 | 0 | * random bits */ |
2019 | 0 | if (type == CKM_FAKE_RANDOM) { |
2020 | 0 | return slot->hasRandom; |
2021 | 0 | } |
2022 | 0 | |
2023 | 0 | /* for most mechanism, bypass the linear lookup */ |
2024 | 0 | if (type < 0x7ff) { |
2025 | 0 | return (slot->mechanismBits[type & 0xff] & (1 << (type >> 8))) ? PR_TRUE : PR_FALSE; |
2026 | 0 | } |
2027 | 0 |
|
2028 | 0 | for (i = 0; i < (int)slot->mechanismCount; i++) { |
2029 | 0 | if (slot->mechanismList[i] == type) |
2030 | 0 | return PR_TRUE; |
2031 | 0 | } |
2032 | 0 | return PR_FALSE; |
2033 | 0 | } |
2034 | | |
2035 | | /* |
2036 | | * Return true if a token that can do the desired mechanism exists. |
2037 | | * This allows us to have hardware tokens that can do function XYZ magically |
2038 | | * allow SSL Ciphers to appear if they are plugged in. |
2039 | | */ |
2040 | | PRBool |
2041 | | PK11_TokenExists(CK_MECHANISM_TYPE type) |
2042 | 0 | { |
2043 | 0 | SECMODModuleList *mlp; |
2044 | 0 | SECMODModuleList *modules; |
2045 | 0 | SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock(); |
2046 | 0 | PK11SlotInfo *slot; |
2047 | 0 | PRBool found = PR_FALSE; |
2048 | 0 | int i; |
2049 | 0 |
|
2050 | 0 | if (!moduleLock) { |
2051 | 0 | PORT_SetError(SEC_ERROR_NOT_INITIALIZED); |
2052 | 0 | return found; |
2053 | 0 | } |
2054 | 0 | /* we only need to know if there is a token that does this mechanism. |
2055 | 0 | * check the internal module first because it's fast, and supports |
2056 | 0 | * almost everything. */ |
2057 | 0 | slot = PK11_GetInternalSlot(); |
2058 | 0 | if (slot) { |
2059 | 0 | found = PK11_DoesMechanism(slot, type); |
2060 | 0 | PK11_FreeSlot(slot); |
2061 | 0 | } |
2062 | 0 | if (found) |
2063 | 0 | return PR_TRUE; /* bypass getting module locks */ |
2064 | 0 | |
2065 | 0 | SECMOD_GetReadLock(moduleLock); |
2066 | 0 | modules = SECMOD_GetDefaultModuleList(); |
2067 | 0 | for (mlp = modules; mlp != NULL && (!found); mlp = mlp->next) { |
2068 | 0 | for (i = 0; i < mlp->module->slotCount; i++) { |
2069 | 0 | slot = mlp->module->slots[i]; |
2070 | 0 | if (PK11_IsPresent(slot)) { |
2071 | 0 | if (PK11_DoesMechanism(slot, type)) { |
2072 | 0 | found = PR_TRUE; |
2073 | 0 | break; |
2074 | 0 | } |
2075 | 0 | } |
2076 | 0 | } |
2077 | 0 | } |
2078 | 0 | SECMOD_ReleaseReadLock(moduleLock); |
2079 | 0 | return found; |
2080 | 0 | } |
2081 | | |
2082 | | /* |
2083 | | * get all the currently available tokens in a list. |
2084 | | * that can perform the given mechanism. If mechanism is CKM_INVALID_MECHANISM, |
2085 | | * get all the tokens. Make sure tokens that need authentication are put at |
2086 | | * the end of this list. |
2087 | | */ |
2088 | | PK11SlotList * |
2089 | | PK11_GetAllTokens(CK_MECHANISM_TYPE type, PRBool needRW, PRBool loadCerts, |
2090 | | void *wincx) |
2091 | 0 | { |
2092 | 0 | PK11SlotList *list; |
2093 | 0 | PK11SlotList *loginList; |
2094 | 0 | PK11SlotList *friendlyList; |
2095 | 0 | SECMODModuleList *mlp; |
2096 | 0 | SECMODModuleList *modules; |
2097 | 0 | SECMODListLock *moduleLock; |
2098 | 0 | int i; |
2099 | | #if defined(XP_WIN32) |
2100 | | int j = 0; |
2101 | | PRInt32 waste[16]; |
2102 | | #endif |
2103 | |
|
2104 | 0 | moduleLock = SECMOD_GetDefaultModuleListLock(); |
2105 | 0 | if (!moduleLock) { |
2106 | 0 | PORT_SetError(SEC_ERROR_NOT_INITIALIZED); |
2107 | 0 | return NULL; |
2108 | 0 | } |
2109 | 0 |
|
2110 | 0 | list = PK11_NewSlotList(); |
2111 | 0 | loginList = PK11_NewSlotList(); |
2112 | 0 | friendlyList = PK11_NewSlotList(); |
2113 | 0 | if ((list == NULL) || (loginList == NULL) || (friendlyList == NULL)) { |
2114 | 0 | if (list) |
2115 | 0 | PK11_FreeSlotList(list); |
2116 | 0 | if (loginList) |
2117 | 0 | PK11_FreeSlotList(loginList); |
2118 | 0 | if (friendlyList) |
2119 | 0 | PK11_FreeSlotList(friendlyList); |
2120 | 0 | return NULL; |
2121 | 0 | } |
2122 | 0 |
|
2123 | 0 | SECMOD_GetReadLock(moduleLock); |
2124 | 0 |
|
2125 | 0 | modules = SECMOD_GetDefaultModuleList(); |
2126 | 0 | for (mlp = modules; mlp != NULL; mlp = mlp->next) { |
2127 | 0 |
|
2128 | | #if defined(XP_WIN32) |
2129 | | /* This is works around some horrible cache/page thrashing problems |
2130 | | ** on Win32. Without this, this loop can take up to 6 seconds at |
2131 | | ** 100% CPU on a Pentium-Pro 200. The thing this changes is to |
2132 | | ** increase the size of the stack frame and modify it. |
2133 | | ** Moving the loop code itself seems to have no effect. |
2134 | | ** Dunno why this combination makes a difference, but it does. |
2135 | | */ |
2136 | | waste[j & 0xf] = j++; |
2137 | | #endif |
2138 | |
|
2139 | 0 | for (i = 0; i < mlp->module->slotCount; i++) { |
2140 | 0 | PK11SlotInfo *slot = mlp->module->slots[i]; |
2141 | 0 |
|
2142 | 0 | if (pk11_IsPresentCertLoad(slot, loadCerts)) { |
2143 | 0 | if (needRW && slot->readOnly) |
2144 | 0 | continue; |
2145 | 0 | if ((type == CKM_INVALID_MECHANISM) || PK11_DoesMechanism(slot, type)) { |
2146 | 0 | if (pk11_LoginStillRequired(slot, wincx)) { |
2147 | 0 | if (PK11_IsFriendly(slot)) { |
2148 | 0 | PK11_AddSlotToList(friendlyList, slot, PR_TRUE); |
2149 | 0 | } else { |
2150 | 0 | PK11_AddSlotToList(loginList, slot, PR_TRUE); |
2151 | 0 | } |
2152 | 0 | } else { |
2153 | 0 | PK11_AddSlotToList(list, slot, PR_TRUE); |
2154 | 0 | } |
2155 | 0 | } |
2156 | 0 | } |
2157 | 0 | } |
2158 | 0 | } |
2159 | 0 | SECMOD_ReleaseReadLock(moduleLock); |
2160 | 0 |
|
2161 | 0 | pk11_MoveListToList(list, friendlyList); |
2162 | 0 | PK11_FreeSlotList(friendlyList); |
2163 | 0 | pk11_MoveListToList(list, loginList); |
2164 | 0 | PK11_FreeSlotList(loginList); |
2165 | 0 |
|
2166 | 0 | return list; |
2167 | 0 | } |
2168 | | |
2169 | | /* |
2170 | | * NOTE: This routine is working from a private List generated by |
2171 | | * PK11_GetAllTokens. That is why it does not need to lock. |
2172 | | */ |
2173 | | PK11SlotList * |
2174 | | PK11_GetPrivateKeyTokens(CK_MECHANISM_TYPE type, PRBool needRW, void *wincx) |
2175 | 0 | { |
2176 | 0 | PK11SlotList *list = PK11_GetAllTokens(type, needRW, PR_TRUE, wincx); |
2177 | 0 | PK11SlotListElement *le, *next; |
2178 | 0 | SECStatus rv; |
2179 | 0 |
|
2180 | 0 | if (list == NULL) |
2181 | 0 | return list; |
2182 | 0 | |
2183 | 0 | for (le = list->head; le; le = next) { |
2184 | 0 | next = le->next; /* save the pointer here in case we have to |
2185 | 0 | * free the element later */ |
2186 | 0 | rv = PK11_Authenticate(le->slot, PR_TRUE, wincx); |
2187 | 0 | if (rv != SECSuccess) { |
2188 | 0 | PK11_DeleteSlotFromList(list, le); |
2189 | 0 | continue; |
2190 | 0 | } |
2191 | 0 | } |
2192 | 0 | return list; |
2193 | 0 | } |
2194 | | |
2195 | | /* |
2196 | | * returns true if the slot doesn't conform to the requested attributes |
2197 | | */ |
2198 | | PRBool |
2199 | | pk11_filterSlot(PK11SlotInfo *slot, CK_MECHANISM_TYPE mechanism, |
2200 | | CK_FLAGS mechanismInfoFlags, unsigned int keySize) |
2201 | 0 | { |
2202 | 0 | CK_MECHANISM_INFO mechanism_info; |
2203 | 0 | CK_RV crv = CKR_OK; |
2204 | 0 |
|
2205 | 0 | /* handle the only case where we don't actually fetch the mechanisms |
2206 | 0 | * on the fly */ |
2207 | 0 | if ((keySize == 0) && (mechanism == CKM_RSA_PKCS) && (slot->hasRSAInfo)) { |
2208 | 0 | mechanism_info.flags = slot->RSAInfoFlags; |
2209 | 0 | } else { |
2210 | 0 | if (!slot->isThreadSafe) |
2211 | 0 | PK11_EnterSlotMonitor(slot); |
2212 | 0 | crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, mechanism, |
2213 | 0 | &mechanism_info); |
2214 | 0 | if (!slot->isThreadSafe) |
2215 | 0 | PK11_ExitSlotMonitor(slot); |
2216 | 0 | /* if we were getting the RSA flags, save them */ |
2217 | 0 | if ((crv == CKR_OK) && (mechanism == CKM_RSA_PKCS) && (!slot->hasRSAInfo)) { |
2218 | 0 | slot->RSAInfoFlags = mechanism_info.flags; |
2219 | 0 | slot->hasRSAInfo = PR_TRUE; |
2220 | 0 | } |
2221 | 0 | } |
2222 | 0 | /* couldn't get the mechanism info */ |
2223 | 0 | if (crv != CKR_OK) { |
2224 | 0 | return PR_TRUE; |
2225 | 0 | } |
2226 | 0 | if (keySize && ((mechanism_info.ulMinKeySize > keySize) || (mechanism_info.ulMaxKeySize < keySize))) { |
2227 | 0 | /* Token can do mechanism, but not at the key size we |
2228 | 0 | * want */ |
2229 | 0 | return PR_TRUE; |
2230 | 0 | } |
2231 | 0 | if (mechanismInfoFlags && ((mechanism_info.flags & mechanismInfoFlags) != |
2232 | 0 | mechanismInfoFlags)) { |
2233 | 0 | return PR_TRUE; |
2234 | 0 | } |
2235 | 0 | return PR_FALSE; |
2236 | 0 | } |
2237 | | |
2238 | | /* |
2239 | | * Find the best slot which supports the given set of mechanisms and key sizes. |
2240 | | * In normal cases this should grab the first slot on the list with no fuss. |
2241 | | * The size array is presumed to match one for one with the mechanism type |
2242 | | * array, which allows you to specify the required key size for each |
2243 | | * mechanism in the list. Whether key size is in bits or bytes is mechanism |
2244 | | * dependent. Typically asymetric keys are in bits and symetric keys are in |
2245 | | * bytes. |
2246 | | */ |
2247 | | PK11SlotInfo * |
2248 | | PK11_GetBestSlotMultipleWithAttributes(CK_MECHANISM_TYPE *type, |
2249 | | CK_FLAGS *mechanismInfoFlags, unsigned int *keySize, |
2250 | | unsigned int mech_count, void *wincx) |
2251 | 0 | { |
2252 | 0 | PK11SlotList *list = NULL; |
2253 | 0 | PK11SlotListElement *le; |
2254 | 0 | PK11SlotInfo *slot = NULL; |
2255 | 0 | PRBool freeit = PR_FALSE; |
2256 | 0 | PRBool listNeedLogin = PR_FALSE; |
2257 | 0 | unsigned int i; |
2258 | 0 | SECStatus rv; |
2259 | 0 |
|
2260 | 0 | list = PK11_GetSlotList(type[0]); |
2261 | 0 |
|
2262 | 0 | if ((list == NULL) || (list->head == NULL)) { |
2263 | 0 | /* We need to look up all the tokens for the mechanism */ |
2264 | 0 | list = PK11_GetAllTokens(type[0], PR_FALSE, PR_TRUE, wincx); |
2265 | 0 | freeit = PR_TRUE; |
2266 | 0 | } |
2267 | 0 |
|
2268 | 0 | /* no one can do it! */ |
2269 | 0 | if (list == NULL) { |
2270 | 0 | PORT_SetError(SEC_ERROR_NO_TOKEN); |
2271 | 0 | return NULL; |
2272 | 0 | } |
2273 | 0 |
|
2274 | 0 | PORT_SetError(0); |
2275 | 0 |
|
2276 | 0 | listNeedLogin = PR_FALSE; |
2277 | 0 | for (i = 0; i < mech_count; i++) { |
2278 | 0 | if ((type[i] != CKM_FAKE_RANDOM) && |
2279 | 0 | (type[i] != CKM_SHA_1) && |
2280 | 0 | (type[i] != CKM_SHA224) && |
2281 | 0 | (type[i] != CKM_SHA256) && |
2282 | 0 | (type[i] != CKM_SHA384) && |
2283 | 0 | (type[i] != CKM_SHA512) && |
2284 | 0 | (type[i] != CKM_MD5) && |
2285 | 0 | (type[i] != CKM_MD2)) { |
2286 | 0 | listNeedLogin = PR_TRUE; |
2287 | 0 | break; |
2288 | 0 | } |
2289 | 0 | } |
2290 | 0 |
|
2291 | 0 | for (le = PK11_GetFirstSafe(list); le; |
2292 | 0 | le = PK11_GetNextSafe(list, le, PR_TRUE)) { |
2293 | 0 | if (PK11_IsPresent(le->slot)) { |
2294 | 0 | PRBool doExit = PR_FALSE; |
2295 | 0 | for (i = 0; i < mech_count; i++) { |
2296 | 0 | if (!PK11_DoesMechanism(le->slot, type[i])) { |
2297 | 0 | doExit = PR_TRUE; |
2298 | 0 | break; |
2299 | 0 | } |
2300 | 0 | if ((mechanismInfoFlags && mechanismInfoFlags[i]) || |
2301 | 0 | (keySize && keySize[i])) { |
2302 | 0 | if (pk11_filterSlot(le->slot, type[i], |
2303 | 0 | mechanismInfoFlags ? mechanismInfoFlags[i] : 0, |
2304 | 0 | keySize ? keySize[i] : 0)) { |
2305 | 0 | doExit = PR_TRUE; |
2306 | 0 | break; |
2307 | 0 | } |
2308 | 0 | } |
2309 | 0 | } |
2310 | 0 |
|
2311 | 0 | if (doExit) |
2312 | 0 | continue; |
2313 | 0 | |
2314 | 0 | if (listNeedLogin && le->slot->needLogin) { |
2315 | 0 | rv = PK11_Authenticate(le->slot, PR_TRUE, wincx); |
2316 | 0 | if (rv != SECSuccess) |
2317 | 0 | continue; |
2318 | 0 | } |
2319 | 0 | slot = le->slot; |
2320 | 0 | PK11_ReferenceSlot(slot); |
2321 | 0 | PK11_FreeSlotListElement(list, le); |
2322 | 0 | if (freeit) { |
2323 | 0 | PK11_FreeSlotList(list); |
2324 | 0 | } |
2325 | 0 | return slot; |
2326 | 0 | } |
2327 | 0 | } |
2328 | 0 | if (freeit) { |
2329 | 0 | PK11_FreeSlotList(list); |
2330 | 0 | } |
2331 | 0 | if (PORT_GetError() == 0) { |
2332 | 0 | PORT_SetError(SEC_ERROR_NO_TOKEN); |
2333 | 0 | } |
2334 | 0 | return NULL; |
2335 | 0 | } |
2336 | | |
2337 | | PK11SlotInfo * |
2338 | | PK11_GetBestSlotMultiple(CK_MECHANISM_TYPE *type, |
2339 | | unsigned int mech_count, void *wincx) |
2340 | 0 | { |
2341 | 0 | return PK11_GetBestSlotMultipleWithAttributes(type, NULL, NULL, |
2342 | 0 | mech_count, wincx); |
2343 | 0 | } |
2344 | | |
2345 | | /* original get best slot now calls the multiple version with only one type */ |
2346 | | PK11SlotInfo * |
2347 | | PK11_GetBestSlot(CK_MECHANISM_TYPE type, void *wincx) |
2348 | 0 | { |
2349 | 0 | return PK11_GetBestSlotMultipleWithAttributes(&type, NULL, NULL, 1, wincx); |
2350 | 0 | } |
2351 | | |
2352 | | PK11SlotInfo * |
2353 | | PK11_GetBestSlotWithAttributes(CK_MECHANISM_TYPE type, CK_FLAGS mechanismFlags, |
2354 | | unsigned int keySize, void *wincx) |
2355 | 0 | { |
2356 | 0 | return PK11_GetBestSlotMultipleWithAttributes(&type, &mechanismFlags, |
2357 | 0 | &keySize, 1, wincx); |
2358 | 0 | } |
2359 | | |
2360 | | int |
2361 | | PK11_GetBestKeyLength(PK11SlotInfo *slot, CK_MECHANISM_TYPE mechanism) |
2362 | 0 | { |
2363 | 0 | CK_MECHANISM_INFO mechanism_info; |
2364 | 0 | CK_RV crv; |
2365 | 0 |
|
2366 | 0 | if (!slot->isThreadSafe) |
2367 | 0 | PK11_EnterSlotMonitor(slot); |
2368 | 0 | crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, |
2369 | 0 | mechanism, &mechanism_info); |
2370 | 0 | if (!slot->isThreadSafe) |
2371 | 0 | PK11_ExitSlotMonitor(slot); |
2372 | 0 | if (crv != CKR_OK) |
2373 | 0 | return 0; |
2374 | 0 | |
2375 | 0 | if (mechanism_info.ulMinKeySize == mechanism_info.ulMaxKeySize) |
2376 | 0 | return 0; |
2377 | 0 | return mechanism_info.ulMaxKeySize; |
2378 | 0 | } |
2379 | | |
2380 | | /* |
2381 | | * This function uses the existing PKCS #11 module to find the |
2382 | | * longest supported key length in the preferred token for a mechanism. |
2383 | | * This varies from the above function in that 1) it returns the key length |
2384 | | * even for fixed key algorithms, and 2) it looks through the tokens |
2385 | | * generally rather than for a specific token. This is used in liu of |
2386 | | * a PK11_GetKeyLength function in pk11mech.c since we can actually read |
2387 | | * supported key lengths from PKCS #11. |
2388 | | * |
2389 | | * For symmetric key operations the length is returned in bytes. |
2390 | | */ |
2391 | | int |
2392 | | PK11_GetMaxKeyLength(CK_MECHANISM_TYPE mechanism) |
2393 | 0 | { |
2394 | 0 | CK_MECHANISM_INFO mechanism_info; |
2395 | 0 | PK11SlotList *list = NULL; |
2396 | 0 | PK11SlotListElement *le; |
2397 | 0 | PRBool freeit = PR_FALSE; |
2398 | 0 | int keyLength = 0; |
2399 | 0 |
|
2400 | 0 | list = PK11_GetSlotList(mechanism); |
2401 | 0 |
|
2402 | 0 | if ((list == NULL) || (list->head == NULL)) { |
2403 | 0 | /* We need to look up all the tokens for the mechanism */ |
2404 | 0 | list = PK11_GetAllTokens(mechanism, PR_FALSE, PR_FALSE, NULL); |
2405 | 0 | freeit = PR_TRUE; |
2406 | 0 | } |
2407 | 0 |
|
2408 | 0 | /* no tokens recognize this mechanism */ |
2409 | 0 | if (list == NULL) { |
2410 | 0 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
2411 | 0 | return 0; |
2412 | 0 | } |
2413 | 0 |
|
2414 | 0 | for (le = PK11_GetFirstSafe(list); le; |
2415 | 0 | le = PK11_GetNextSafe(list, le, PR_TRUE)) { |
2416 | 0 | PK11SlotInfo *slot = le->slot; |
2417 | 0 | CK_RV crv; |
2418 | 0 | if (PK11_IsPresent(slot)) { |
2419 | 0 | if (!slot->isThreadSafe) |
2420 | 0 | PK11_EnterSlotMonitor(slot); |
2421 | 0 | crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, |
2422 | 0 | mechanism, &mechanism_info); |
2423 | 0 | if (!slot->isThreadSafe) |
2424 | 0 | PK11_ExitSlotMonitor(slot); |
2425 | 0 | if ((crv == CKR_OK) && (mechanism_info.ulMaxKeySize != 0) && (mechanism_info.ulMaxKeySize != 0xffffffff)) { |
2426 | 0 | keyLength = mechanism_info.ulMaxKeySize; |
2427 | 0 | break; |
2428 | 0 | } |
2429 | 0 | } |
2430 | 0 | } |
2431 | 0 |
|
2432 | 0 | /* fallback to pk11_GetPredefinedKeyLength for fixed key size algorithms */ |
2433 | 0 | if (keyLength == 0) { |
2434 | 0 | CK_KEY_TYPE keyType; |
2435 | 0 | keyType = PK11_GetKeyType(mechanism, 0); |
2436 | 0 | keyLength = pk11_GetPredefinedKeyLength(keyType); |
2437 | 0 | } |
2438 | 0 |
|
2439 | 0 | if (le) |
2440 | 0 | PK11_FreeSlotListElement(list, le); |
2441 | 0 | if (freeit) |
2442 | 0 | PK11_FreeSlotList(list); |
2443 | 0 | return keyLength; |
2444 | 0 | } |
2445 | | |
2446 | | SECStatus |
2447 | | PK11_SeedRandom(PK11SlotInfo *slot, unsigned char *data, int len) |
2448 | 0 | { |
2449 | 0 | CK_RV crv; |
2450 | 0 |
|
2451 | 0 | PK11_EnterSlotMonitor(slot); |
2452 | 0 | crv = PK11_GETTAB(slot)->C_SeedRandom(slot->session, data, (CK_ULONG)len); |
2453 | 0 | PK11_ExitSlotMonitor(slot); |
2454 | 0 | if (crv != CKR_OK) { |
2455 | 0 | PORT_SetError(PK11_MapError(crv)); |
2456 | 0 | return SECFailure; |
2457 | 0 | } |
2458 | 0 | return SECSuccess; |
2459 | 0 | } |
2460 | | |
2461 | | SECStatus |
2462 | | PK11_GenerateRandomOnSlot(PK11SlotInfo *slot, unsigned char *data, int len) |
2463 | 0 | { |
2464 | 0 | CK_RV crv; |
2465 | 0 |
|
2466 | 0 | if (!slot->isInternal) |
2467 | 0 | PK11_EnterSlotMonitor(slot); |
2468 | 0 | crv = PK11_GETTAB(slot)->C_GenerateRandom(slot->session, data, |
2469 | 0 | (CK_ULONG)len); |
2470 | 0 | if (!slot->isInternal) |
2471 | 0 | PK11_ExitSlotMonitor(slot); |
2472 | 0 | if (crv != CKR_OK) { |
2473 | 0 | PORT_SetError(PK11_MapError(crv)); |
2474 | 0 | return SECFailure; |
2475 | 0 | } |
2476 | 0 | return SECSuccess; |
2477 | 0 | } |
2478 | | |
2479 | | /* Attempts to update the Best Slot for "FAKE RANDOM" generation. |
2480 | | ** If that's not the internal slot, then it also attempts to update the |
2481 | | ** internal slot. |
2482 | | ** The return value indicates if the INTERNAL slot was updated OK. |
2483 | | */ |
2484 | | SECStatus |
2485 | | PK11_RandomUpdate(void *data, size_t bytes) |
2486 | 0 | { |
2487 | 0 | PK11SlotInfo *slot; |
2488 | 0 | PRBool bestIsInternal; |
2489 | 0 | SECStatus status; |
2490 | 0 |
|
2491 | 0 | slot = PK11_GetBestSlot(CKM_FAKE_RANDOM, NULL); |
2492 | 0 | if (slot == NULL) { |
2493 | 0 | slot = PK11_GetInternalSlot(); |
2494 | 0 | if (!slot) |
2495 | 0 | return SECFailure; |
2496 | 0 | } |
2497 | 0 | |
2498 | 0 | bestIsInternal = PK11_IsInternal(slot); |
2499 | 0 | status = PK11_SeedRandom(slot, data, bytes); |
2500 | 0 | PK11_FreeSlot(slot); |
2501 | 0 |
|
2502 | 0 | if (!bestIsInternal) { |
2503 | 0 | /* do internal slot, too. */ |
2504 | 0 | slot = PK11_GetInternalSlot(); |
2505 | 0 | PORT_Assert(slot); |
2506 | 0 | if (!slot) { |
2507 | 0 | return SECFailure; |
2508 | 0 | } |
2509 | 0 | status = PK11_SeedRandom(slot, data, bytes); |
2510 | 0 | PK11_FreeSlot(slot); |
2511 | 0 | } |
2512 | 0 | return status; |
2513 | 0 | } |
2514 | | |
2515 | | SECStatus |
2516 | | PK11_GenerateRandom(unsigned char *data, int len) |
2517 | 0 | { |
2518 | 0 | PK11SlotInfo *slot; |
2519 | 0 | SECStatus rv; |
2520 | 0 |
|
2521 | 0 | slot = PK11_GetBestSlot(CKM_FAKE_RANDOM, NULL); |
2522 | 0 | if (slot == NULL) |
2523 | 0 | return SECFailure; |
2524 | 0 | |
2525 | 0 | rv = PK11_GenerateRandomOnSlot(slot, data, len); |
2526 | 0 | PK11_FreeSlot(slot); |
2527 | 0 | return rv; |
2528 | 0 | } |
2529 | | |
2530 | | /* |
2531 | | * Reset the token to it's initial state. For the internal module, this will |
2532 | | * Purge your keydb, and reset your cert db certs to USER_INIT. |
2533 | | */ |
2534 | | SECStatus |
2535 | | PK11_ResetToken(PK11SlotInfo *slot, char *sso_pwd) |
2536 | 0 | { |
2537 | 0 | unsigned char tokenName[32]; |
2538 | 0 | int tokenNameLen; |
2539 | 0 | CK_RV crv; |
2540 | 0 |
|
2541 | 0 | /* reconstruct the token name */ |
2542 | 0 | tokenNameLen = PORT_Strlen(slot->token_name); |
2543 | 0 | if (tokenNameLen > sizeof(tokenName)) { |
2544 | 0 | tokenNameLen = sizeof(tokenName); |
2545 | 0 | } |
2546 | 0 |
|
2547 | 0 | PORT_Memcpy(tokenName, slot->token_name, tokenNameLen); |
2548 | 0 | if (tokenNameLen < sizeof(tokenName)) { |
2549 | 0 | PORT_Memset(&tokenName[tokenNameLen], ' ', |
2550 | 0 | sizeof(tokenName) - tokenNameLen); |
2551 | 0 | } |
2552 | 0 |
|
2553 | 0 | /* initialize the token */ |
2554 | 0 | PK11_EnterSlotMonitor(slot); |
2555 | 0 |
|
2556 | 0 | /* first shutdown the token. Existing sessions will get closed here */ |
2557 | 0 | PK11_GETTAB(slot) |
2558 | 0 | ->C_CloseAllSessions(slot->slotID); |
2559 | 0 | slot->session = CK_INVALID_SESSION; |
2560 | 0 |
|
2561 | 0 | /* now re-init the token */ |
2562 | 0 | crv = PK11_GETTAB(slot)->C_InitToken(slot->slotID, |
2563 | 0 | (unsigned char *)sso_pwd, sso_pwd ? PORT_Strlen(sso_pwd) : 0, tokenName); |
2564 | 0 |
|
2565 | 0 | /* finally bring the token back up */ |
2566 | 0 | PK11_InitToken(slot, PR_TRUE); |
2567 | 0 | PK11_ExitSlotMonitor(slot); |
2568 | 0 | if (crv != CKR_OK) { |
2569 | 0 | PORT_SetError(PK11_MapError(crv)); |
2570 | 0 | return SECFailure; |
2571 | 0 | } |
2572 | 0 | nssTrustDomain_UpdateCachedTokenCerts(slot->nssToken->trustDomain, |
2573 | 0 | slot->nssToken); |
2574 | 0 | return SECSuccess; |
2575 | 0 | } |
2576 | | void |
2577 | | PK11Slot_SetNSSToken(PK11SlotInfo *sl, NSSToken *nsst) |
2578 | 0 | { |
2579 | 0 | sl->nssToken = nsst; |
2580 | 0 | } |
2581 | | |
2582 | | NSSToken * |
2583 | | PK11Slot_GetNSSToken(PK11SlotInfo *sl) |
2584 | 0 | { |
2585 | 0 | return sl->nssToken; |
2586 | 0 | } |
2587 | | |
2588 | | /* |
2589 | | * wait for a token to change it's state. The application passes in the expected |
2590 | | * new state in event. |
2591 | | */ |
2592 | | PK11TokenStatus |
2593 | | PK11_WaitForTokenEvent(PK11SlotInfo *slot, PK11TokenEvent event, |
2594 | | PRIntervalTime timeout, PRIntervalTime latency, int series) |
2595 | 0 | { |
2596 | 0 | PRIntervalTime first_time = 0; |
2597 | 0 | PRBool first_time_set = PR_FALSE; |
2598 | 0 | PRBool waitForRemoval; |
2599 | 0 |
|
2600 | 0 | if (slot->isPerm) { |
2601 | 0 | return PK11TokenNotRemovable; |
2602 | 0 | } |
2603 | 0 | if (latency == 0) { |
2604 | 0 | latency = PR_SecondsToInterval(5); |
2605 | 0 | } |
2606 | 0 | waitForRemoval = (PRBool)(event == PK11TokenRemovedOrChangedEvent); |
2607 | 0 |
|
2608 | 0 | if (series == 0) { |
2609 | 0 | series = PK11_GetSlotSeries(slot); |
2610 | 0 | } |
2611 | 0 | while (PK11_IsPresent(slot) == waitForRemoval) { |
2612 | 0 | PRIntervalTime interval; |
2613 | 0 |
|
2614 | 0 | if (waitForRemoval && series != PK11_GetSlotSeries(slot)) { |
2615 | 0 | return PK11TokenChanged; |
2616 | 0 | } |
2617 | 0 | if (timeout == PR_INTERVAL_NO_WAIT) { |
2618 | 0 | return waitForRemoval ? PK11TokenPresent : PK11TokenRemoved; |
2619 | 0 | } |
2620 | 0 | if (timeout != PR_INTERVAL_NO_TIMEOUT) { |
2621 | 0 | interval = PR_IntervalNow(); |
2622 | 0 | if (!first_time_set) { |
2623 | 0 | first_time = interval; |
2624 | 0 | first_time_set = PR_TRUE; |
2625 | 0 | } |
2626 | 0 | if ((interval - first_time) > timeout) { |
2627 | 0 | return waitForRemoval ? PK11TokenPresent : PK11TokenRemoved; |
2628 | 0 | } |
2629 | 0 | } |
2630 | 0 | PR_Sleep(latency); |
2631 | 0 | } |
2632 | 0 | return waitForRemoval ? PK11TokenRemoved : PK11TokenPresent; |
2633 | 0 | } |