pk11util.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 585 行 · 第 1/2 页

C
585
字号
	}    }    SECMOD_ReleaseWriteLock(moduleLock);    if (rv == SECSuccess) { 	SECMOD_DeletePermDB(mlp->module);	SECMOD_DestroyModuleListElement(mlp);    }    return rv;}/* * find a module by name and delete it of the module list */SECStatusSECMOD_DeleteInternalModule(char *name) {    SECMODModuleList *mlp;    SECMODModuleList **mlpp;    SECStatus rv = SECFailure;    SECMOD_GetWriteLock(moduleLock);    for(mlpp = &modules,mlp = modules; 				mlp != NULL; mlpp = &mlp->next, mlp = *mlpp) {	if (PORT_Strcmp(name,mlp->module->commonName) == 0) {	    /* don't delete the internal module */	    if (mlp->module->internal) {		rv = SECSuccess;		SECMOD_RemoveList(mlpp,mlp);	    } 	    break;	}    }    SECMOD_ReleaseWriteLock(moduleLock);    if (rv == SECSuccess) {	SECMODModule *newModule,*oldModule;	if (mlp->module->isFIPS) {	    newModule = SECMOD_NewInternal();	} else {	    newModule = SECMOD_GetFIPSInternal();	}	if (newModule == NULL) {	    SECMODModuleList *last,*mlp2;	   /* we're in pretty deep trouble if this happens...Security	    * not going to work well... try to put the old module back on	    * the list */	   SECMOD_GetWriteLock(moduleLock);	   for(mlp2 = modules; mlp2 != NULL; mlp2 = mlp->next) {		last = mlp2;	   }	   if (last == NULL) {		modules = mlp;	   } else {		SECMOD_AddList(last,mlp,NULL);	   }	   SECMOD_ReleaseWriteLock(moduleLock);	   return SECFailure; 	}	oldModule = internalModule;	internalModule = SECMOD_ReferenceModule(newModule);	SECMOD_AddModule(internalModule);	SECMOD_DestroyModule(oldModule); 	SECMOD_DeletePermDB(mlp->module);	SECMOD_DestroyModuleListElement(mlp);    }    return rv;}SECStatusSECMOD_AddModule(SECMODModule *newModule) {    SECStatus rv;    SECMODModuleList *mlp, *newListElement, *last = NULL;    /* Test if a module w/ the same name already exists */    /* and return SECWouldBlock if so. */    /* We should probably add a new return value such as */    /* SECDublicateModule, but to minimize ripples, I'll */    /* give SECWouldBlock a new meaning */    if (SECMOD_FindModule(newModule->commonName)) {        return SECWouldBlock;        /* module already exists. */    }    rv = SECMOD_LoadModule(newModule);    if (rv != SECSuccess) {	return rv;    }    newListElement = SECMOD_NewModuleListElement();    if (newListElement == NULL) {	return SECFailure;    }    SECMOD_AddPermDB(newModule);    newListElement->module = newModule;    SECMOD_GetWriteLock(moduleLock);    /* Added it to the end (This is very inefficient, but Adding a module     * on the fly should happen maybe 2-3 times through the life this program     * on a given computer, and this list should be *SHORT*. */    for(mlp = modules; mlp != NULL; mlp = mlp->next) {	last = mlp;    }    if (last == NULL) {	modules = newListElement;    } else {	SECMOD_AddList(last,newListElement,NULL);    }    SECMOD_ReleaseWriteLock(moduleLock);    return SECSuccess;}PK11SlotInfo *SECMOD_FindSlot(SECMODModule *module,char *name) {    int i;    char *string;    for (i=0; i < module->slotCount; i++) {	PK11SlotInfo *slot = module->slots[i];	if (PK11_IsPresent(slot)) {	    string = PK11_GetTokenName(slot);	} else {	    string = PK11_GetSlotName(slot);	}	if (PORT_Strcmp(name,string) == 0) {	    return PK11_ReferenceSlot(slot);	}    }    return NULL;}SECStatusPK11_GetModInfo(SECMODModule *mod,CK_INFO *info) {    CK_RV crv;    if (mod->functionList == NULL) return SECFailure;    crv = PK11_GETTAB(mod)->C_GetInfo(info);    return (crv == CKR_OK) ? SECSuccess : SECFailure;}/* Determine if we have the FIP's module loaded as the default * module to trigger other bogus FIPS requirements in PKCS #12 and * SSL */PRBoolPK11_IsFIPS(void){    SECMODModule *mod = SECMOD_GetInternalModule();    if (mod && mod->internal) {	return mod->isFIPS;    }    return PR_FALSE;}/* combines NewModule() & AddModule *//* give a string for the module name & the full-path for the dll, *//* installs the PKCS11 module & update registry */SECStatus SECMOD_AddNewModule(char* moduleName, char* dllPath,                              unsigned long defaultMechanismFlags,                              unsigned long cipherEnableFlags) {    SECMODModule *module;    SECStatus result;    int s,i;    PK11SlotInfo* slot;    module = SECMOD_NewModule();        if (moduleName) {	        module->commonName=PORT_ArenaStrdup(module->arena,moduleName);    } else {        module->commonName=NULL;    }    if (dllPath) {        module->dllName=PORT_ArenaStrdup(module->arena,dllPath);    } else {        module->dllName=NULL;    }    if (module->dllName != NULL) {        if (module->dllName[0] != 0) {           SECStatus rv = SECMOD_AddModule(module);            if (rv != SECSuccess) {                /* SECFailure: failed to add module, corrupt or missing module etc. */                /* SECBlock: a module with the same name already exists */                return rv;            } else { /* successfully added module */                /* turn on SSL cipher enable flags */                module->ssl[0] = cipherEnableFlags;                /* check each slot to turn on appropriate mechanisms */                for (s = 0; s < module->slotCount; s++) {                    slot = (module->slots)[s];                    /* for each possible mechanism */                    for (i=0; i < num_pk11_default_mechanisms; i++) {                        /* we are told to turn it on by default ? */                        if (PK11_DefaultArray[i].flag & defaultMechanismFlags) {                                                        /* it ignores if slot attribute update failes */                            result = PK11_UpdateSlotAttribute(slot, &(PK11_DefaultArray[i]), PR_TRUE);                        } else { /* turn this mechanism of the slot off by default */                            result = PK11_UpdateSlotAttribute(slot, &(PK11_DefaultArray[i]), PR_FALSE);                        }                    } /* for each mechanism */                    /* disable each slot if the defaultFlags say so */                    if (defaultMechanismFlags & PK11_DISABLE_FLAG) {                        PK11_UserDisableSlot(slot);                    }                } /* for each slot of this module */                /* delete and re-add module in order to save changes to the module */                result = SECMOD_DeletePermDB(module);                                if (result == SECSuccess) {                              result = SECMOD_AddPermDB(module);                    if (result == SECSuccess) {                        return SECSuccess;                    }                                    }                            }        }    }    SECMOD_DestroyModule(module);    return SECFailure;}/* Public & Internal(Security Library)  representation of * encryption mechanism flags conversion *//* Currently, the only difference is that internal representation  * puts RANDOM_FLAG at bit 31 (Most-significant bit), but * public representation puts this bit at bit 28 */unsigned long SECMOD_PubMechFlagstoInternal(unsigned long publicFlags) {    unsigned long internalFlags = publicFlags;    if (publicFlags & PUBLIC_MECH_RANDOM_FLAG) {        internalFlags &= ~PUBLIC_MECH_RANDOM_FLAG;        internalFlags |= SECMOD_RANDOM_FLAG;    }    return internalFlags;}unsigned long SECMOD_InternaltoPubMechFlags(unsigned long internalFlags) {    unsigned long publicFlags = internalFlags;    if (internalFlags & SECMOD_RANDOM_FLAG) {        publicFlags &= ~SECMOD_RANDOM_FLAG;        publicFlags |= PUBLIC_MECH_RANDOM_FLAG;    }    return publicFlags;}/* Public & Internal(Security Library)  representation of *//* cipher flags conversion *//* Note: currently they are just stubs */unsigned long SECMOD_PubCipherFlagstoInternal(unsigned long publicFlags) {    return publicFlags;}unsigned long SECMOD_InternaltoPubCipherFlags(unsigned long internalFlags) {    return internalFlags;}/* Funtion reports true if module of modType is installed/configured */PRBool SECMOD_IsModulePresent( unsigned long int pubCipherEnableFlags ){    PRBool result = PR_FALSE;    SECMODModuleList *mods = SECMOD_GetDefaultModuleList();    SECMODListLock *modsLock = SECMOD_GetDefaultModuleListLock();    SECMOD_GetReadLock(moduleLock);    for ( ; mods != NULL; mods = mods->next) {        if (mods->module->ssl[0] & SECMOD_PubCipherFlagstoInternal(pubCipherEnableFlags)) {            result = PR_TRUE;        }    }    SECMOD_ReleaseReadLock(moduleLock);    return result;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?