288
* find the function pointer.
291
secmod_FindModuleByFuncPtr(void *funcPtr)
293
SECMODModuleList *mlp;
294
SECMODModule *module = NULL;
296
SECMOD_GetReadLock(moduleLock);
297
for(mlp = modules; mlp != NULL; mlp = mlp->next) {
298
/* paranoia, shouldn't ever happen */
302
if (funcPtr == mlp->module->functionList) {
303
module = mlp->module;
304
SECMOD_ReferenceModule(module);
308
SECMOD_ReleaseReadLock(moduleLock);
309
if (module == NULL) {
310
PORT_SetError(SEC_ERROR_NO_MODULE);
277
316
* Find the Slot based on ID and the module.
1224
1300
PK11_ExitSlotMonitor(slot);
1226
1302
if (crv != CKR_OK) {
1227
PK11_FreeSlot(slot);
1228
1303
PORT_SetError(PK11_MapError(crv));
1229
1304
return SECFailure;
1231
rv = SECMOD_UpdateSlotList(slot->module);
1306
return SECMOD_UpdateSlotList(slot->module);
1310
* return true if the selected slot ID is not present or doesn't exist
1313
secmod_SlotIsEmpty(SECMODModule *mod, CK_SLOT_ID slotID)
1315
PK11SlotInfo *slot = SECMOD_LookupSlot(mod->moduleID, slotID);
1317
PRBool present = PK11_IsPresent(slot);
1318
PK11_FreeSlot(slot);
1323
/* it doesn't exist or isn't present, it's available */
1328
* Find an unused slot id in module.
1331
secmod_FindFreeSlot(SECMODModule *mod)
1333
CK_SLOT_ID i, minSlotID, maxSlotID;
1335
/* look for a free slot id on the internal module */
1336
if (mod->internal && mod->isFIPS) {
1337
minSlotID = SFTK_MIN_FIPS_USER_SLOT_ID;
1338
maxSlotID = SFTK_MAX_FIPS_USER_SLOT_ID;
1340
minSlotID = SFTK_MIN_USER_SLOT_ID;
1341
maxSlotID = SFTK_MAX_USER_SLOT_ID;
1343
for (i=minSlotID; i < maxSlotID; i++) {
1344
if (secmod_SlotIsEmpty(mod,i)) {
1348
PORT_SetError(SEC_ERROR_NO_SLOT_SELECTED);
1349
return (CK_SLOT_ID) -1;
1353
* Attempt to open a new slot.
1355
* This works the same os OpenUserDB except it can be called against
1356
* any module that understands the softoken protocol for opening new
1357
* slots, not just the softoken itself. If the selected module does not
1358
* understand the protocol, C_CreateObject will fail with
1359
* CKR_INVALID_ATTRIBUTE, and SECMOD_OpenNewSlot will return NULL and set
1360
* SEC_ERROR_BAD_DATA.
1362
* NewSlots can be closed with SECMOD_CloseUserDB();
1364
* Modulespec is module dependent.
1367
SECMOD_OpenNewSlot(SECMODModule *mod, const char *moduleSpec)
1369
CK_SLOT_ID slotID = 0;
1375
slotID = secmod_FindFreeSlot(mod);
1376
if (slotID == (CK_SLOT_ID) -1) {
1380
if (mod->slotCount == 0) {
1384
/* just grab the first slot in the module, any present slot should work */
1385
slot = PK11_ReferenceSlot(mod->slots[0]);
1390
/* we've found the slot, now build the moduleSpec */
1391
escSpec = secmod_DoubleEscape(moduleSpec, '>', ']');
1392
if (escSpec == NULL) {
1393
PK11_FreeSlot(slot);
1396
sendSpec = PR_smprintf("tokens=[0x%x=<%s>]", slotID, escSpec);
1399
if (sendSpec == NULL) {
1400
/* PR_smprintf does not set SEC_ERROR_NO_MEMORY on failure. */
1401
PK11_FreeSlot(slot);
1402
PORT_SetError(SEC_ERROR_NO_MEMORY);
1405
rv = secmod_UserDBOp(slot, CKO_NETSCAPE_NEWSLOT, sendSpec);
1406
PR_smprintf_free(sendSpec);
1232
1407
PK11_FreeSlot(slot);
1237
* add escapes to protect quote characters...
1240
nss_addEscape(const char *string, char quote)
1242
char *newString = 0;
1243
int escapes = 0, size = 0;
1247
for (src=string; *src ; src++) {
1248
if ((*src == quote) || (*src == '\\')) escapes++;
1252
newString = PORT_ZAlloc(escapes+size+1);
1253
if (newString == NULL) {
1257
for (src=string, dest=newString; *src; src++,dest++) {
1258
if ((*src == '\\') || (*src == quote)) {
1268
nss_doubleEscape(const char *string)
1270
char *round1 = NULL;
1271
char *retValue = NULL;
1272
if (string == NULL) {
1275
round1 = nss_addEscape(string,'>');
1277
retValue = nss_addEscape(round1,']');
1282
if (retValue == NULL) {
1283
retValue = PORT_Strdup("");
1408
if (rv != SECSuccess) {
1412
return SECMOD_FindSlotByID(mod, slotID);
1357
1478
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1361
/* look for a free slot id on the internal module */
1363
minSlotID = SFTK_MIN_FIPS_USER_SLOT_ID;
1364
maxSlotID = SFTK_MAX_FIPS_USER_SLOT_ID;
1366
minSlotID = SFTK_MIN_USER_SLOT_ID;
1367
maxSlotID = SFTK_MAX_USER_SLOT_ID;
1369
for (i=minSlotID; i < maxSlotID; i++) {
1370
PK11SlotInfo *slot = SECMOD_LookupSlot(mod->moduleID, i);
1372
PRBool present = PK11_IsPresent(slot);
1373
PK11_FreeSlot(slot);
1377
/* not present means it's available */
1379
/* it doesn't exist or isn't present, it's available */
1386
/* this could happen if we try to open too many slots */
1387
PORT_SetError(SEC_ERROR_NO_SLOT_SELECTED);
1391
/* we've found the slot, now build the moduleSpec */
1393
escSpec = nss_doubleEscape(moduleSpec);
1394
if (escSpec == NULL) {
1397
sendSpec = PR_smprintf("tokens=[0x%x=<%s>]", slotID, escSpec);
1400
if (sendSpec == NULL) {
1401
/* PR_smprintf does not set no memory error */
1402
PORT_SetError(SEC_ERROR_NO_MEMORY);
1405
rv = secmod_UserDBOp(CKO_NETSCAPE_NEWSLOT, sendSpec);
1406
PR_smprintf_free(sendSpec);
1407
if (rv != SECSuccess) {
1411
return SECMOD_FindSlotByID(mod, slotID);
1481
return SECMOD_OpenNewSlot(mod, moduleSpec);
1415
1486
* close an already opened user database. NOTE: the database must be
1416
1487
* in the internal token, and must be one created with SECMOD_OpenUserDB().
1417
1488
* Once the database is closed, the slot will remain as an empty slot
1418
* until it's used again with SECMOD_OpenUserDB().
1489
* until it's used again with SECMOD_OpenUserDB() or SECMOD_OpenNewSlot().
1421
1492
SECMOD_CloseUserDB(PK11SlotInfo *slot)
1424
1495
char *sendSpec;
1426
if (!slot->isInternal) {
1427
PORT_SetError(SEC_ERROR_INVALID_ARGS);
1431
1497
sendSpec = PR_smprintf("tokens=[0x%x=<>]", slot->slotID);
1432
1498
if (sendSpec == NULL) {