/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* * The following code handles the storage of PKCS 11 modules used by the * NSS. This file is written to abstract away how the modules are * stored so we can decide that later.
*/ #include"secport.h" #include"prprf.h" #include"prenv.h" #include"utilpars.h" #include"utilmodt.h"
/* * return the expected matching quote value for the one specified
*/
PRBool
NSSUTIL_ArgGetPair(char c)
{ switch (c) { case'\'': return c; case'\"': return c; case'<': return'>'; case'{': return'}'; case'[': return']'; case'(': return')'; default: break;
} return' ';
}
PRBool
NSSUTIL_ArgIsBlank(char c)
{ return isspace((unsignedchar)c);
}
PRBool
NSSUTIL_ArgIsEscape(char c)
{ return c == '\\';
}
PRBool
NSSUTIL_ArgIsQuote(char c)
{ switch (c) { case'\'': case'\"': case'<': case'{': /* } end curly to keep vi bracket matching working */ case'(': /* ) */ case'[': /* ] */ return PR_TRUE; default: break;
} return PR_FALSE;
}
/* * find the end of the current tag/value pair. string should be pointing just * after the equal sign. Handles quoted characters.
*/ constchar *
NSSUTIL_ArgFindEnd(constchar *string)
{ char endChar = ' ';
PRBool lastEscape = PR_FALSE;
if (NSSUTIL_ArgIsQuote(*string)) {
endChar = NSSUTIL_ArgGetPair(*string);
string++;
}
for (; *string; string++) { if (lastEscape) {
lastEscape = PR_FALSE; continue;
} if (NSSUTIL_ArgIsEscape(*string) && !lastEscape) {
lastEscape = PR_TRUE; continue;
} if ((endChar == ' ') && NSSUTIL_ArgIsBlank(*string)) break; if (*string == endChar) { break;
}
}
return string;
}
/* * get the value pointed to by string. string should be pointing just beyond * the equal sign.
*/ char *
NSSUTIL_ArgFetchValue(constchar *string, int *pcount)
{ constchar *end = NSSUTIL_ArgFindEnd(string); char *retString, *copyString;
PRBool lastEscape = PR_FALSE; int len;
len = end - string; if (len == 0) {
*pcount = 0; return NULL;
}
/* * point to the next parameter in string
*/ constchar *
NSSUTIL_ArgSkipParameter(constchar *string)
{ constchar *end; /* look for the end of the <name>= */ for (; *string; string++) { if (*string == '=') {
string++; break;
} if (NSSUTIL_ArgIsBlank(*string)) return (string);
}
end = NSSUTIL_ArgFindEnd(string); if (*end)
end++; return end;
}
/* * get the value from that tag value pair.
*/ char *
NSSUTIL_ArgGetParamValue(constchar *paramName, constchar *parameters)
{ char searchValue[256];
size_t paramLen = strlen(paramName); char *returnValue = NULL; int next;
if ((parameters == NULL) || (*parameters == 0)) return NULL;
/* * find the next flag in the parameter list
*/ constchar *
NSSUTIL_ArgNextFlag(constchar *flags)
{ for (; *flags; flags++) { if (*flags == ',') {
flags++; break;
}
} return flags;
}
/* * return true if the flag is set in the label parameter.
*/
PRBool
NSSUTIL_ArgHasFlag(constchar *label, constchar *flag, constchar *parameters)
{ char *flags; constchar *index; int len = strlen(flag);
PRBool found = PR_FALSE;
flags = NSSUTIL_ArgGetParamValue(label, parameters); if (flags == NULL) return PR_FALSE;
for (index = flags; *index; index = NSSUTIL_ArgNextFlag(index)) { if (PORT_Strncasecmp(index, flag, len) == 0) {
found = PR_TRUE; break;
}
}
PORT_Free(flags); return found;
}
/* * decode a number. handle octal (leading '0'), hex (leading '0x') or decimal
*/ long
NSSUTIL_ArgDecodeNumber(constchar *num)
{ int radix = 10; unsignedlong value = 0; long retValue = 0; int sign = 1; int digit;
/* * parameters are tag value pairs. This function returns the tag or label (the * value before the equal size.
*/ char *
NSSUTIL_ArgGetLabel(constchar *inString, int *next)
{ char *name = NULL; constchar *string; int len;
/* look for the end of the <label>= */ for (string = inString; *string; string++) { if (*string == '=') { break;
} if (NSSUTIL_ArgIsBlank(*string)) break;
}
len = string - inString;
*next = len; if (*string == '=')
(*next) += 1; if (len > 0) {
name = PORT_Alloc(len + 1);
PORT_Strncpy(name, inString, len);
name[len] = 0;
} return name;
}
/* * read an argument at a Long integer
*/ long
NSSUTIL_ArgReadLong(constchar *label, constchar *params, long defValue, PRBool *isdefault)
{ char *value; long retValue; if (isdefault)
*isdefault = PR_FALSE;
value = NSSUTIL_ArgGetParamValue(label, params); if (value == NULL) { if (isdefault)
*isdefault = PR_TRUE; return defValue;
}
retValue = NSSUTIL_ArgDecodeNumber(value); if (value)
PORT_Free(value);
return retValue;
}
/* * prepare a string to be quoted with 'quote' marks. We do that by adding * appropriate escapes.
*/ staticint
nssutil_escapeQuotesSize(constchar *string, char quote, PRBool addquotes)
{ int escapes = 0, size = 0; constchar *src;
/************************************************************************ * These functions are used in contructing strings. * NOTE: they will always return a string, but sometimes it will return * a specific NULL string. These strings must be freed with util_freePair.
*/
/* string to return on error... */ staticchar *nssutil_nullString = "";
/* turn the slot flags into a bit mask */ unsignedlong
NSSUTIL_ArgParseSlotFlags(constchar *label, constchar *params)
{ char *flags; constchar *index; unsignedlong retValue = 0; int i;
PRBool all = PR_FALSE;
flags = NSSUTIL_ArgGetParamValue(label, params); if (flags == NULL) return 0;
if (PORT_Strcasecmp(flags, "all") == 0)
all = PR_TRUE;
for (index = flags; *index; index = NSSUTIL_ArgNextFlag(index)) { for (i = 0; i < nssutil_argSlotFlagTableSize; i++) { if (all ||
(PORT_Strncasecmp(index, nssutil_argSlotFlagTable[i].name,
nssutil_argSlotFlagTable[i].len) == 0)) {
retValue |= nssutil_argSlotFlagTable[i].value;
}
}
}
PORT_Free(flags); return retValue;
}
/* parse a single slot specific parameter */ staticvoid
nssutil_argDecodeSingleSlotInfo(char *name, char *params, struct NSSUTILPreSlotInfoStr *slotInfo)
{ char *askpw;
/* first count the number of slots */ for (slotIndex = NSSUTIL_ArgStrip(slotParams); *slotIndex;
slotIndex = NSSUTIL_ArgStrip(NSSUTIL_ArgSkipParameter(slotIndex))) {
count++;
}
/* get the data structures */ if (arena) {
slotInfo = PORT_ArenaZNewArray(arena, struct NSSUTILPreSlotInfoStr, count);
} else {
slotInfo = PORT_ZNewArray(struct NSSUTILPreSlotInfoStr, count);
} if (slotInfo == NULL) return NULL;
for (slotIndex = NSSUTIL_ArgStrip(slotParams), i = 0;
*slotIndex && i < count;) { char *name;
name = NSSUTIL_ArgGetLabel(slotIndex, &next);
slotIndex += next;
/************************************************************************ * make a new slot specific parameter
*/ /* first make the slot flags */ staticchar *
nssutil_mkSlotFlags(unsignedlong defaultFlags)
{ char *flags = NULL; unsignedint i; int j;
for (i = 0; i < sizeof(defaultFlags) * 8; i++) { if (defaultFlags & (1UL << i)) { char *string = NULL;
#define NSSUTIL_ARG_FORTEZZA_FLAG "FORTEZZA" /****************************************************************************** * Parse the cipher flags from the NSS parameter
*/ void
NSSUTIL_ArgParseCipherFlags(unsignedlong *newCiphers, constchar *cipherList)
{
newCiphers[0] = newCiphers[1] = 0; if ((cipherList == NULL) || (*cipherList == 0)) return;
for (; *cipherList; cipherList = NSSUTIL_ArgNextFlag(cipherList)) { if (PORT_Strncasecmp(cipherList, NSSUTIL_ARG_FORTEZZA_FLAG, sizeof(NSSUTIL_ARG_FORTEZZA_FLAG) - 1) == 0) {
newCiphers[0] |= SECMOD_FORTEZZA_FLAG;
}
/* add additional flags here as necessary */ /* direct bit mapping escape */ if (*cipherList == 0) { if (cipherList[1] == 'l') {
newCiphers[1] |= atoi(&cipherList[2]);
} else {
newCiphers[0] |= atoi(&cipherList[2]);
}
}
}
}
/********************************************************************* * make NSS parameter...
*/ /* First make NSS specific flags */ #define MAX_FLAG_SIZE sizeof("internal") + sizeof("FIPS") + sizeof("moduleDB") + \ sizeof("moduleDBOnly") + sizeof("critical") staticchar *
nssutil_mkNSSFlags(PRBool internal, PRBool isFIPS,
PRBool isModuleDB, PRBool isModuleDBOnly, PRBool isCritical)
{ char *flags = (char *)PORT_ZAlloc(MAX_FLAG_SIZE);
PRBool first = PR_TRUE;
PORT_Memset(flags, 0, MAX_FLAG_SIZE); if (internal) {
PORT_Strcat(flags, "internal");
first = PR_FALSE;
} if (isFIPS) { if (!first)
PORT_Strcat(flags, ",");
PORT_Strcat(flags, "FIPS");
first = PR_FALSE;
} if (isModuleDB) { if (!first)
PORT_Strcat(flags, ",");
PORT_Strcat(flags, "moduleDB");
first = PR_FALSE;
} if (isModuleDBOnly) { if (!first)
PORT_Strcat(flags, ",");
PORT_Strcat(flags, "moduleDBOnly");
first = PR_FALSE;
} if (isCritical) { if (!first)
PORT_Strcat(flags, ",");
PORT_Strcat(flags, "critical");
} return flags;
}
/* now let's build up the string * first the slot infos
*/
slotLen = 0; for (i = 0; i < (int)slotCount; i++) {
slotLen += PORT_Strlen(slotStrings[i]) + 1;
}
slotLen += 1; /* space for the final NULL */
slotParams = (char *)PORT_ZAlloc(slotLen);
PORT_Memset(slotParams, 0, slotLen); for (i = 0; i < (int)slotCount; i++) {
PORT_Strcat(slotParams, slotStrings[i]);
PORT_Strcat(slotParams, " ");
PR_smprintf_free(slotStrings[i]);
slotStrings[i] = NULL;
}
/* * now the NSS structure
*/
nssFlags = nssutil_mkNSSFlags(internal, isFIPS, isModuleDB, isModuleDBOnly,
isCritical); /* for now only the internal module is critical */
ciphers = nssutil_mkCipherFlags(ssl0, ssl1);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.