michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #if defined(WIN32) michael@0: #undef __STDC__ michael@0: #include "fcntl.h" michael@0: #include "io.h" michael@0: #include michael@0: #else michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include "secutil.h" michael@0: michael@0: michael@0: #include "nspr.h" michael@0: #include "prtypes.h" michael@0: #include "prtime.h" michael@0: #include "prlong.h" michael@0: #include "prinrval.h" michael@0: #include "prenv.h" michael@0: michael@0: #include "pkcs11.h" michael@0: michael@0: #include "pk11table.h" michael@0: michael@0: #ifndef O_BINARY michael@0: #define O_BINARY 0 michael@0: #endif michael@0: michael@0: CK_ULONG systemFlags; michael@0: #define FLAG_NEGATE 0x80000000 michael@0: #define FLAG_Verify 0x00000001 michael@0: #define FLAG_VerifyFile 0x00000002 michael@0: #define CKR_QUIT 0x80000000 michael@0: michael@0: int ArgSize(ArgType type); michael@0: const char *constLookup(const char *bp, CK_ULONG *value, ConstType *type); michael@0: michael@0: int michael@0: isNum(char c) michael@0: { michael@0: return (c >= '0' && c <= '9'); michael@0: } michael@0: michael@0: int michael@0: isConst(const char *c) michael@0: { michael@0: CK_ULONG value; michael@0: ConstType type; michael@0: michael@0: constLookup(c, &value, &type); michael@0: return type != ConstNone; michael@0: } michael@0: michael@0: /* michael@0: * see if the variable is really a 'size' function. This michael@0: * function may modify var if it is a size function. michael@0: */ michael@0: char * michael@0: isSize(char *var, int *isArray) michael@0: { michael@0: char *ptr = NULL; michael@0: char *end; michael@0: int array = 0; michael@0: michael@0: if (PL_strncasecmp(var,"sizeof(",/*)*/ 7) == 0) { michael@0: ptr = var + 7; michael@0: } else if (PL_strncasecmp(var,"size(",/*)*/ 5) == 0) { michael@0: ptr = var + 5; michael@0: } else if (PL_strncasecmp(var,"sizeofarray(",/*)*/ 12) == 0) { michael@0: ptr = var + 12; michael@0: array = 1; michael@0: } else if (PL_strncasecmp(var,"sizea(",/*)*/ 6) == 0) { michael@0: ptr = var + 6; michael@0: array = 1; michael@0: } else { michael@0: return NULL; michael@0: } michael@0: end = strchr(ptr,/*(*/ ')') ; michael@0: if (end == NULL) { michael@0: return NULL; michael@0: } michael@0: if (isArray) *isArray = array; michael@0: *end = 0; michael@0: return ptr; michael@0: } michael@0: michael@0: void michael@0: printConst(CK_ULONG value, ConstType type, int newLine) michael@0: { michael@0: int i; michael@0: michael@0: for (i=0; i < constCount; i++) { michael@0: if (consts[i].type == type && consts[i].value == value) { michael@0: printf("%s",consts[i].name); michael@0: break; michael@0: } michael@0: if (type == ConstNone && consts[i].value == value) { michael@0: printf("%s",consts[i].name); michael@0: break; michael@0: } michael@0: } michael@0: if (i == constCount) { michael@0: if ((type == ConstAvailableSizes) || (type == ConstCurrentSize)) { michael@0: printf("%lu",value); michael@0: } else { michael@0: printf("Unknown %s (%lu:0x%lx)",constTypeString[type],value,value); michael@0: } michael@0: } michael@0: if (newLine) { michael@0: printf("\n"); michael@0: } michael@0: } michael@0: michael@0: ConstType michael@0: getConstFromAttribute(CK_ATTRIBUTE_TYPE type) michael@0: { michael@0: int i; michael@0: michael@0: for (i=0; i < constCount; i++) { michael@0: if (consts[i].type == ConstAttribute && consts[i].value == type) { michael@0: return consts[i].attrType; michael@0: } michael@0: } michael@0: return ConstNone; michael@0: } michael@0: michael@0: void michael@0: printChars(const char *name, CK_ULONG size) michael@0: { michael@0: CK_ULONG i; michael@0: for (i=0; i < size; i++) { michael@0: if (name[i] == 0) { michael@0: break; michael@0: } michael@0: printf("%c",name[i]); michael@0: } michael@0: printf("\n"); michael@0: } michael@0: michael@0: #define DUMP_LEN 16 michael@0: void printDump(const unsigned char *buf, int size) michael@0: { michael@0: int i,j; michael@0: michael@0: for(i=0; i < size; i+= DUMP_LEN) { michael@0: printf(" "); michael@0: for (j=0; j< DUMP_LEN; j++) { michael@0: if (i+j < size) { michael@0: printf("%02x ",buf[i+j]); michael@0: } else { michael@0: printf(" "); michael@0: } michael@0: } michael@0: for (j=0; j< DUMP_LEN; j++) { michael@0: if (i+j < size) { michael@0: if (buf[i+j] < ' ' || buf[i+j] >= 0x7f) { michael@0: printf("."); michael@0: } else { michael@0: printf("%c",buf[i+j]); michael@0: } michael@0: } else { michael@0: printf(" "); michael@0: } michael@0: } michael@0: printf("\n"); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * free an argument structure michael@0: */ michael@0: void michael@0: argFreeData(Value *arg) michael@0: { michael@0: if (arg->data && ((arg->type & ArgStatic) == 0)) { michael@0: if ((arg->type & ArgMask) == ArgAttribute) { michael@0: int i; michael@0: CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)arg->data; michael@0: michael@0: for (i=0; i < arg->arraySize; i++) { michael@0: free(template[i].pValue); michael@0: } michael@0: } michael@0: if ((arg->type & ArgMask) == ArgInitializeArgs) { michael@0: CK_C_INITIALIZE_ARGS *init = (CK_C_INITIALIZE_ARGS *)arg->data; michael@0: if (init->LibraryParameters) { michael@0: free(init->LibraryParameters); michael@0: } michael@0: } michael@0: free(arg->data); michael@0: } michael@0: arg->type &= ~ArgStatic; michael@0: arg->data = NULL; michael@0: } michael@0: michael@0: void michael@0: argFree(Value *arg) michael@0: { michael@0: if (arg == NULL) return; michael@0: michael@0: arg->reference--; michael@0: if (arg->reference == 0) { michael@0: if (arg->type & ArgFile) { michael@0: free(arg->filename); michael@0: } michael@0: argFreeData(arg); michael@0: free (arg); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * free and argument list michael@0: */ michael@0: void michael@0: parseFree(Value **ap) michael@0: { michael@0: int i; michael@0: for (i=0 ; i < MAX_ARGS; i++) { michael@0: argFree(ap[i]); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * getEnd: how for to the end of this argmument list? michael@0: */ michael@0: int michael@0: getEnd(const char *bp) michael@0: { michael@0: int count = 0; michael@0: michael@0: while (*bp) { michael@0: if (*bp == ' ' || *bp == '\t' || *bp == '\n') return count; michael@0: count++; michael@0: bp++; michael@0: } michael@0: return (count); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * strip: return the first none white space character michael@0: */ michael@0: const char * michael@0: strip(const char *bp) michael@0: { michael@0: while (*bp && (*bp == ' ' || *bp == '\t' || *bp == '\n')) bp++; michael@0: return bp; michael@0: } michael@0: michael@0: /* michael@0: * read in the next argument into dp ... don't overflow michael@0: */ michael@0: const char * michael@0: readChars(const char *bp, char *dp, int max ) michael@0: { michael@0: int count = 1; michael@0: while (*bp) { michael@0: if (*bp == ' ' || *bp == '\t' || *bp == '\n' ) { michael@0: *dp = 0; michael@0: return bp; michael@0: } michael@0: *dp++ = *bp++; michael@0: if (++count == max) break; michael@0: } michael@0: while (*bp && (*bp != ' ' && *bp != '\t' && *bp != '\n')) bp++; michael@0: *dp = 0; michael@0: return (bp); michael@0: } michael@0: michael@0: Value * varLookup(const char *bp, char *vname, int max, int *error); michael@0: michael@0: CK_ULONG michael@0: getValue(const char *v, int *error) michael@0: { michael@0: Value * varVal = NULL; michael@0: CK_ULONG retVal = 0; michael@0: ConstType type; michael@0: char tvar[512]; michael@0: michael@0: *error = 0; michael@0: michael@0: varVal = varLookup( v, tvar, sizeof(tvar), error); michael@0: michael@0: if (varVal) { michael@0: if ((varVal->type & ArgMask) == ArgULong) { michael@0: retVal = *(CK_ULONG *)varVal->data; michael@0: } else { michael@0: fprintf(stderr,"%s: is not a ulong\n", v); michael@0: *error = 1; michael@0: } michael@0: argFree(varVal); michael@0: return retVal; michael@0: } michael@0: constLookup(v, &retVal, &type); michael@0: return retVal; michael@0: } michael@0: michael@0: Value * michael@0: NewValue(ArgType type, CK_ULONG arraySize) michael@0: { michael@0: Value *value; michael@0: michael@0: value = (Value *)malloc(sizeof(Value)); michael@0: if (!value) return NULL; michael@0: value->size = ArgSize(type)*arraySize; michael@0: value->type = type; michael@0: value->filename = NULL; michael@0: value->constType = ConstNone; michael@0: value->data = (void *)malloc(value->size); michael@0: if (!value->data) { michael@0: free(value); michael@0: return NULL; michael@0: } michael@0: value->reference = 1; michael@0: value->arraySize = (type == ArgChar) ? 1: arraySize; michael@0: michael@0: memset(value->data, 0, value->size); michael@0: return value; michael@0: } michael@0: michael@0: #define INVALID_INDEX 0xffffffff michael@0: michael@0: CK_ULONG michael@0: handleArray(char *vname, int *error) michael@0: { michael@0: char *bracket; michael@0: CK_ULONG index = INVALID_INDEX; michael@0: michael@0: if ((bracket = strchr(vname,'[')) != 0) { michael@0: char *tmpv = bracket+1; michael@0: *bracket = 0; michael@0: bracket = strchr(tmpv,']'); michael@0: michael@0: if (bracket == 0) { michael@0: fprintf(stderr,"%s: missing closing brace\n", vname); michael@0: return INVALID_INDEX; michael@0: } michael@0: *bracket = 0; michael@0: michael@0: index = getValue(tmpv, error); michael@0: if (*error == 1) { michael@0: return INVALID_INDEX; michael@0: } else if (index == INVALID_INDEX) { michael@0: fprintf(stderr, "%s: 0x%lx is an invalid index\n",vname,index); michael@0: *error = 1; michael@0: } michael@0: } michael@0: return index; michael@0: } michael@0: michael@0: void * michael@0: makeArrayTarget(const char *vname, const Value *value, CK_ULONG index) michael@0: { michael@0: char * target; michael@0: CK_ULONG elementSize; michael@0: michael@0: if (index >= (CK_ULONG)value->arraySize) { michael@0: fprintf(stderr, "%s[%lu]: index larger than array size (%d)\n", michael@0: vname, index, value->arraySize); michael@0: return NULL; michael@0: } michael@0: michael@0: target = (char *)value->data; michael@0: elementSize = value->size/value->arraySize; michael@0: target += index * elementSize; michael@0: return target; michael@0: } michael@0: michael@0: /* michael@0: * look up a variable from the variable chain michael@0: */ michael@0: static Variable *varHead = NULL; michael@0: Value * michael@0: varLookup(const char *bp, char *vname, int max, int *error) michael@0: { michael@0: Variable *current; michael@0: CK_ULONG index = INVALID_INDEX; michael@0: int isArray = 0; michael@0: char *ptr; michael@0: *error = 0; michael@0: michael@0: if (bp != NULL) { michael@0: readChars(bp, vname, max); michael@0: } michael@0: michael@0: /* don't make numbers into variables */ michael@0: if (isNum(vname[0])) { michael@0: return NULL; michael@0: } michael@0: /* nor consts */ michael@0: if (isConst(vname)) { michael@0: return NULL; michael@0: } michael@0: /* handle sizeof() */ michael@0: if ((ptr = isSize(vname, &isArray)) != NULL) { michael@0: CK_ULONG size; michael@0: Value *targetValue = NULL; michael@0: Value *sourceValue = varLookup(NULL, ptr, 0, error); michael@0: if (!sourceValue) { michael@0: if (*error == 0) { michael@0: /* just didn't find it */ michael@0: *error = 1; michael@0: fprintf(stderr,"Couldn't find variable %s to take size of\n", michael@0: ptr); michael@0: return NULL; michael@0: } michael@0: } michael@0: size = isArray ? sourceValue->arraySize : sourceValue->size; michael@0: targetValue = NewValue(ArgULong,1); michael@0: memcpy(targetValue->data, &size, sizeof(size)); michael@0: michael@0: return targetValue; michael@0: } michael@0: michael@0: /* modifies vname */ michael@0: index = handleArray(vname, error); michael@0: if (*error == 1) { michael@0: return NULL; michael@0: } michael@0: michael@0: for (current = varHead; current; current = current->next) { michael@0: if (PL_strcasecmp(current->vname, vname) == 0) { michael@0: char *target; michael@0: if (index == INVALID_INDEX) { michael@0: (current->value->reference)++; michael@0: return current->value; michael@0: } michael@0: target = makeArrayTarget(vname, current->value, index); michael@0: if (target) { michael@0: Value *element = NewValue(current->value->type, 1); michael@0: if (!element) { michael@0: fprintf(stderr, "MEMORY ERROR!\n"); michael@0: *error = 1; michael@0: } michael@0: argFreeData(element); michael@0: element->data = target; michael@0: element->type |= ArgStatic; michael@0: return element; michael@0: } michael@0: *error = 1; michael@0: return NULL; michael@0: } michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: static CK_RV michael@0: list(void) michael@0: { michael@0: Variable *current; michael@0: michael@0: if (varHead) { michael@0: printf(" %10s\t%16s\t%8s\tSize\tElements\n","Name","Type","Const"); michael@0: } else { michael@0: printf(" no variables set\n"); michael@0: } michael@0: michael@0: for (current = varHead; current; current = current->next) { michael@0: printf(" %10s\t%16s\t%8s\t%d\t%d\n", current->vname, michael@0: valueString[current->value->type&ArgMask], michael@0: constTypeString[current->value->constType], michael@0: current->value->size, current->value->arraySize); michael@0: } michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: printFlags(const char *s, CK_ULONG flags, ConstType type) michael@0: { michael@0: CK_ULONG i; michael@0: int needComma = 0; michael@0: michael@0: printf("%s",s); michael@0: for (i=1; i ; i=i << 1) { michael@0: if (flags & i) { michael@0: printf("%s",needComma?",":""); michael@0: printConst(i, type, 0); michael@0: needComma=1; michael@0: } michael@0: } michael@0: if (!needComma) { michael@0: printf("Empty"); michael@0: } michael@0: printf("\n"); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * add a new variable to the chain michael@0: */ michael@0: const char * michael@0: AddVariable(const char *bp, Value **ptr) michael@0: { michael@0: char vname[512]; michael@0: Variable *current; michael@0: int index = INVALID_INDEX; michael@0: int size; michael@0: int error = 0; michael@0: michael@0: bp = readChars(bp,vname,sizeof(vname)); michael@0: michael@0: /* don't make numbers into variables */ michael@0: if (isNum(vname[0])) { michael@0: return bp; michael@0: } michael@0: /* or consts */ michael@0: if (isConst(vname)) { michael@0: return bp; michael@0: } michael@0: /* or NULLs */ michael@0: if (vname[0] == 0) { michael@0: return bp; michael@0: } michael@0: /* or sizeof */ michael@0: if (isSize(vname, NULL)) { michael@0: return bp; michael@0: } michael@0: /* arrays values should be written back to the original */ michael@0: index = handleArray(vname, &error); michael@0: if (error == 1) { michael@0: return bp; michael@0: } michael@0: michael@0: michael@0: for (current = varHead; current; current = current->next) { michael@0: if (PL_strcasecmp(current->vname,vname) == 0) { michael@0: char *target; michael@0: /* found a complete object, return the found one */ michael@0: if (index == INVALID_INDEX) { michael@0: argFree(*ptr); michael@0: *ptr = current->value; michael@0: return bp; michael@0: } michael@0: /* found an array, update the array element */ michael@0: target = makeArrayTarget(vname, current->value, index); michael@0: if (target) { michael@0: memcpy(target, (*ptr)->data, (*ptr)->size); michael@0: argFreeData(*ptr); michael@0: (*ptr)->data = target; michael@0: (*ptr)->type |= ArgStatic; michael@0: } michael@0: return bp; michael@0: } michael@0: } michael@0: michael@0: /* we are looking for an array and didn't find one */ michael@0: if (index != INVALID_INDEX) { michael@0: return bp; michael@0: } michael@0: michael@0: michael@0: current = (Variable *)malloc(sizeof(Variable)); michael@0: size = strlen(vname); michael@0: current->vname = (char *)malloc(size+1); michael@0: strcpy(current->vname,vname); michael@0: current->value = *ptr; michael@0: (*ptr)->reference++; michael@0: michael@0: current->next = varHead; michael@0: varHead = current; michael@0: return bp; michael@0: } michael@0: michael@0: ArgType michael@0: FindTypeByName(const char *typeName) michael@0: { michael@0: int i; michael@0: michael@0: for (i=0; i < valueCount; i++) { michael@0: if (PL_strcasecmp(typeName,valueString[i]) == 0) { michael@0: return (ArgType) i; michael@0: } michael@0: if (valueString[i][0] == 'C' && valueString[i][1] == 'K' && michael@0: valueString[i][2] == '_' && michael@0: (PL_strcasecmp(typeName,&valueString[i][3]) == 0)) { michael@0: return (ArgType) i; michael@0: } michael@0: } michael@0: return ArgNone; michael@0: } michael@0: michael@0: CK_RV michael@0: ArrayVariable(const char *bp, const char *typeName, CK_ULONG count) michael@0: { michael@0: ArgType type; michael@0: Value *value; /* new Value */ michael@0: michael@0: type = FindTypeByName(typeName); michael@0: if (type == ArgNone) { michael@0: fprintf(stderr,"Invalid type (%s)\n", typeName); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: value = NewValue(type, count); michael@0: (void) AddVariable(bp, &value); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: #define MAX_TEMPLATE 25 michael@0: michael@0: CK_RV michael@0: ArrayTemplate(const char *bp, char *attributes) michael@0: { michael@0: char aname[512]; michael@0: CK_ULONG attributeTypes[MAX_TEMPLATE]; michael@0: CK_ATTRIBUTE *template; michael@0: Value *value; /* new Value */ michael@0: char *ap; michael@0: int i, count = 0; michael@0: michael@0: memcpy(aname,attributes,strlen(attributes)+1); michael@0: michael@0: for (ap = aname, count = 0; ap && *ap && count < MAX_TEMPLATE; count++) { michael@0: char *cur = ap; michael@0: ConstType type; michael@0: michael@0: ap = strchr(ap,','); michael@0: if (ap) { michael@0: *ap++ = 0; michael@0: } michael@0: michael@0: (void)constLookup(cur, &attributeTypes[count], &type); michael@0: if ((type != ConstAttribute) && (type != ConstNone)) { michael@0: fprintf(stderr, "Unknown Attribute %s\n", cur); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: } michael@0: michael@0: value = NewValue(ArgAttribute, count); michael@0: michael@0: template = (CK_ATTRIBUTE *)value->data; michael@0: for (i=0; i < count ; i++) { michael@0: template[i].type = attributeTypes[i]; michael@0: } michael@0: (void) AddVariable(bp, &value); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: BuildTemplate(Value *vp) michael@0: { michael@0: CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data; michael@0: int i; michael@0: michael@0: for (i=0; i < vp->arraySize; i++) { michael@0: if (((signed long)template[i].ulValueLen) > 0) { michael@0: if (template[i].pValue) free(template[i].pValue); michael@0: template[i].pValue = malloc(template[i].ulValueLen); michael@0: } michael@0: } michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: SetTemplate(Value *vp, CK_ULONG index, CK_ULONG value) michael@0: { michael@0: CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data; michael@0: int isbool = 0; michael@0: CK_ULONG len; michael@0: ConstType attrType; michael@0: michael@0: if (index >= (CK_ULONG) vp->arraySize) { michael@0: fprintf(stderr,"index (%lu) greater than array (%d)\n", michael@0: index, vp->arraySize); michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: attrType = getConstFromAttribute(template[index].type); michael@0: michael@0: if (attrType == ConstNone) { michael@0: fprintf(stderr,"can't set index (%lu) because ", index); michael@0: printConst(template[index].type,ConstAttribute, 0); michael@0: fprintf(stderr, " is not a CK_BBOOL or CK_ULONG\n"); michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: isbool = (attrType == ConstBool); michael@0: len = isbool ? sizeof (CK_BBOOL) : sizeof(CK_ULONG); michael@0: if ((template[index].ulValueLen != len) || (template[index].pValue)) { michael@0: free(template[index].pValue); michael@0: template[index].pValue = malloc(len); michael@0: template[index].ulValueLen = len; michael@0: } michael@0: if (isbool) { michael@0: *(CK_BBOOL *)template[index].pValue = (CK_BBOOL) value; michael@0: } else { michael@0: *(CK_ULONG *)template[index].pValue = (CK_ULONG) value; michael@0: } michael@0: return CKR_OK; michael@0: michael@0: } michael@0: michael@0: CK_RV michael@0: NewMechanism(const char *bp, CK_ULONG mechType) michael@0: { michael@0: Value *value; /* new Value */ michael@0: CK_MECHANISM *mechanism; michael@0: michael@0: value = NewValue(ArgMechanism, 1); michael@0: mechanism = (CK_MECHANISM *)value->data; michael@0: mechanism->mechanism = mechType; michael@0: mechanism->pParameter = NULL; michael@0: mechanism->ulParameterLen = 0; michael@0: (void) AddVariable(bp, &value); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: NewInitializeArgs(const char *bp, CK_ULONG flags, const char *param) michael@0: { michael@0: Value *value; /* new Value */ michael@0: CK_C_INITIALIZE_ARGS *init; michael@0: michael@0: value = NewValue(ArgInitializeArgs, 1); michael@0: init = (CK_C_INITIALIZE_ARGS *)value->data; michael@0: init->flags = flags; michael@0: if (strcmp(param, "null") != 0) { michael@0: init->LibraryParameters = (CK_CHAR_PTR *)strdup(param); michael@0: } michael@0: (void) AddVariable(bp, &value); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * add a new variable to the chain michael@0: */ michael@0: CK_RV michael@0: DeleteVariable(const char *bp) michael@0: { michael@0: char vname[512]; michael@0: Variable **current; michael@0: michael@0: bp = readChars(bp,vname,sizeof(vname)); michael@0: michael@0: for (current = &varHead; *current; current = &(*current)->next) { michael@0: if (PL_strcasecmp((*current)->vname,vname) == 0) { michael@0: argFree((*current)->value); michael@0: *current = (*current)->next; michael@0: break; michael@0: } michael@0: } michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * convert an octal value to integer michael@0: */ michael@0: CK_ULONG michael@0: otoi(const char *o) michael@0: { michael@0: CK_ULONG value = 0; michael@0: michael@0: while (*o) { michael@0: if ((*o >= '0') && (*o <= '7')) { michael@0: value = (value << 3) | (unsigned)(*o - '0'); michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: /* michael@0: * convert a hex value to integer michael@0: */ michael@0: CK_ULONG michael@0: htoi(const char *x) michael@0: { michael@0: CK_ULONG value = 0; michael@0: michael@0: while (*x) { michael@0: if ((*x >= '0') && (*x <= '9')) { michael@0: value = (value << 4) | (unsigned)(*x - '0'); michael@0: } else if ((*x >= 'a') && (*x <= 'f')) { michael@0: value = (value << 4) | (unsigned)(*x - 'a'); michael@0: } else if ((*x >= 'A') && (*x <= 'F')) { michael@0: value = (value << 4) | (unsigned)(*x - 'A'); michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * look up or decode a constant value michael@0: */ michael@0: const char * michael@0: constLookup(const char *bp, CK_ULONG *value, ConstType *type) michael@0: { michael@0: char vname[512]; michael@0: int i; michael@0: michael@0: bp = readChars(bp,vname,sizeof(vname)); michael@0: michael@0: for (i=0; i < constCount; i++) { michael@0: if ((PL_strcasecmp(consts[i].name,vname) == 0) || michael@0: PL_strcasecmp(consts[i].name+5,vname) == 0) { michael@0: *value = consts[i].value; michael@0: *type = consts[i].type; michael@0: return bp; michael@0: } michael@0: } michael@0: michael@0: *type = ConstNone; michael@0: if (vname[0] == '0' && vname[1] == 'X') { michael@0: *value = htoi(&vname[2]); michael@0: } else if (vname[0] == '0') { michael@0: *value = otoi(&vname[1]); michael@0: } else { michael@0: *value = atoi(vname); michael@0: } michael@0: return bp; michael@0: } michael@0: michael@0: int michael@0: ArgSize(ArgType type) michael@0: { michael@0: int size=0; michael@0: type &= ArgMask; michael@0: michael@0: switch (type) { michael@0: case ArgNone: michael@0: size = 0; michael@0: break; michael@0: case ArgULong: michael@0: size = sizeof(CK_ULONG); michael@0: break; michael@0: case ArgVar: michael@0: size = 1; /* get's changed later */ michael@0: break; michael@0: case ArgChar: michael@0: case ArgUTF8: michael@0: size = 1; michael@0: break; michael@0: case ArgInfo: michael@0: size = sizeof(CK_INFO); michael@0: break; michael@0: case ArgSlotInfo: michael@0: size = sizeof(CK_SLOT_INFO); michael@0: break; michael@0: case ArgTokenInfo: michael@0: size = sizeof(CK_TOKEN_INFO); michael@0: break; michael@0: case ArgSessionInfo: michael@0: size = sizeof(CK_SESSION_INFO); michael@0: break; michael@0: case ArgAttribute: michael@0: size = sizeof(CK_ATTRIBUTE); michael@0: break; michael@0: case ArgMechanism: michael@0: size = sizeof(CK_MECHANISM); michael@0: break; michael@0: case ArgMechanismInfo: michael@0: size = sizeof(CK_MECHANISM_INFO); michael@0: break; michael@0: case ArgInitializeArgs: michael@0: size = sizeof(CK_C_INITIALIZE_ARGS); michael@0: break; michael@0: case ArgFunctionList: michael@0: size = sizeof(CK_FUNCTION_LIST); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: return (size); michael@0: } michael@0: michael@0: CK_RV michael@0: restore(const char *filename,Value *ptr) michael@0: { michael@0: int fd,size; michael@0: michael@0: fd = open(filename,O_RDONLY|O_BINARY); michael@0: if (fd < 0) { michael@0: perror(filename); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: michael@0: size = read(fd,ptr->data,ptr->size); michael@0: if (systemFlags & FLAG_VerifyFile) { michael@0: printDump(ptr->data,ptr->size); michael@0: } michael@0: if (size < 0) { michael@0: perror(filename); michael@0: return CKR_FUNCTION_FAILED; michael@0: } else if (size != ptr->size) { michael@0: fprintf(stderr,"%s: only read %d bytes, needed to read %d bytes\n", michael@0: filename,size,ptr->size); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: close(fd); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: save(const char *filename,Value *ptr) michael@0: { michael@0: int fd,size; michael@0: michael@0: fd = open(filename,O_WRONLY|O_BINARY|O_CREAT,0666); michael@0: if (fd < 0) { michael@0: perror(filename); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: michael@0: size = write(fd,ptr->data,ptr->size); michael@0: if (size < 0) { michael@0: perror(filename); michael@0: return CKR_FUNCTION_FAILED; michael@0: } else if (size != ptr->size) { michael@0: fprintf(stderr,"%s: only wrote %d bytes, need to write %d bytes\n", michael@0: filename,size,ptr->size); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: close(fd); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: static CK_RV michael@0: increment(Value *ptr, CK_ULONG value) michael@0: { michael@0: if ((ptr->type & ArgMask) != ArgULong) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: *(CK_ULONG *)ptr->data += value; michael@0: return CKR_OK; michael@0: } michael@0: michael@0: static CK_RV michael@0: decrement(Value *ptr, CK_ULONG value) michael@0: { michael@0: if ((ptr->type & ArgMask) != ArgULong) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: *(CK_ULONG *)ptr->data -= value; michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: printArg(Value *ptr,int arg_number) michael@0: { michael@0: ArgType type = ptr->type & ArgMask; michael@0: CK_INFO *info; michael@0: CK_SLOT_INFO *slotInfo; michael@0: CK_TOKEN_INFO *tokenInfo; michael@0: CK_SESSION_INFO *sessionInfo; michael@0: CK_ATTRIBUTE *attribute; michael@0: CK_MECHANISM *mechanism; michael@0: CK_MECHANISM_INFO *mechanismInfo; michael@0: CK_C_INITIALIZE_ARGS *initArgs; michael@0: CK_FUNCTION_LIST *functionList; michael@0: CK_RV ckrv = CKR_OK; michael@0: ConstType constType; michael@0: michael@0: if (arg_number) { michael@0: printf("Arg %d: \n",arg_number); michael@0: } michael@0: if (ptr->arraySize > 1) { michael@0: Value element; michael@0: int i; michael@0: int elementSize = ptr->size/ptr->arraySize; michael@0: char *dp = (char *)ptr->data; michael@0: michael@0: /* build a temporary Value to hold a single element */ michael@0: element.type = type; michael@0: element.constType = ptr->constType; michael@0: element.size = elementSize; michael@0: element.filename = ptr->filename; michael@0: element.reference = 1; michael@0: element.arraySize = 1; michael@0: for (i=0; i < ptr->arraySize; i++) { michael@0: printf(" -----[ %d ] -----\n", i); michael@0: element.data = (void *) &dp[i*elementSize]; michael@0: (void) printArg(&element, 0); michael@0: } michael@0: return ckrv; michael@0: } michael@0: if (ptr->data == NULL) { michael@0: printf(" NULL ptr to a %s\n", valueString[type]); michael@0: return ckrv; michael@0: } michael@0: switch (type) { michael@0: case ArgNone: michael@0: printf(" None\n"); michael@0: break; michael@0: case ArgULong: michael@0: printf(" %lu (0x%lx)\n", *((CK_ULONG *)ptr->data), michael@0: *((CK_ULONG *)ptr->data)); michael@0: if (ptr->constType != ConstNone) { michael@0: printf(" "); michael@0: printConst(*(CK_ULONG *)ptr->data,ptr->constType,1); michael@0: } michael@0: break; michael@0: case ArgVar: michael@0: printf(" %s\n",(char *)ptr->data); michael@0: break; michael@0: case ArgUTF8: michael@0: printf(" %s\n",(char *)ptr->data); michael@0: break; michael@0: case ArgChar: michael@0: printDump(ptr->data,ptr->size); michael@0: break; michael@0: case ArgInfo: michael@0: #define VERSION(x) (x).major, (x).minor michael@0: info = (CK_INFO *)ptr->data; michael@0: printf(" Cryptoki Version: %d.%02d\n", michael@0: VERSION(info->cryptokiVersion)); michael@0: printf(" Manufacturer ID: "); michael@0: printChars((char *)info->manufacturerID, michael@0: sizeof(info->manufacturerID)); michael@0: printFlags(" Flags: ", info->flags, ConstInfoFlags); michael@0: printf(" Library Description: "); michael@0: printChars((char *)info->libraryDescription, michael@0: sizeof(info->libraryDescription)); michael@0: printf(" Library Version: %d.%02d\n", michael@0: VERSION(info->libraryVersion)); michael@0: break; michael@0: case ArgSlotInfo: michael@0: slotInfo = (CK_SLOT_INFO *)ptr->data; michael@0: printf(" Slot Description: "); michael@0: printChars((char *)slotInfo->slotDescription, michael@0: sizeof(slotInfo->slotDescription)); michael@0: printf(" Manufacturer ID: "); michael@0: printChars((char *)slotInfo->manufacturerID, michael@0: sizeof(slotInfo->manufacturerID)); michael@0: printFlags(" Flags: ", slotInfo->flags, ConstSlotFlags); michael@0: printf(" Hardware Version: %d.%02d\n", michael@0: VERSION(slotInfo->hardwareVersion)); michael@0: printf(" Firmware Version: %d.%02d\n", michael@0: VERSION(slotInfo->firmwareVersion)); michael@0: break; michael@0: case ArgTokenInfo: michael@0: tokenInfo = (CK_TOKEN_INFO *)ptr->data; michael@0: printf(" Label: "); michael@0: printChars((char *) tokenInfo->label,sizeof(tokenInfo->label)); michael@0: printf(" Manufacturer ID: "); michael@0: printChars((char *)tokenInfo->manufacturerID, michael@0: sizeof(tokenInfo->manufacturerID)); michael@0: printf(" Model: "); michael@0: printChars((char *)tokenInfo->model,sizeof(tokenInfo->model)); michael@0: printf(" Serial Number: "); michael@0: printChars((char *)tokenInfo->serialNumber, michael@0: sizeof(tokenInfo->serialNumber)); michael@0: printFlags(" Flags: ", tokenInfo->flags, ConstTokenFlags); michael@0: printf(" Max Session Count: "); michael@0: printConst(tokenInfo->ulMaxSessionCount, ConstAvailableSizes, 1); michael@0: printf(" Session Count: "); michael@0: printConst(tokenInfo->ulSessionCount, ConstCurrentSize, 1); michael@0: printf(" RW Session Count: "); michael@0: printConst(tokenInfo->ulMaxRwSessionCount, ConstAvailableSizes, 1); michael@0: printf(" Max Pin Length : "); michael@0: printConst(tokenInfo->ulMaxPinLen, ConstCurrentSize, 1); michael@0: printf(" Min Pin Length : "); michael@0: printConst(tokenInfo->ulMinPinLen, ConstCurrentSize, 1); michael@0: printf(" Total Public Memory: "); michael@0: printConst(tokenInfo->ulTotalPublicMemory, ConstAvailableSizes, 1); michael@0: printf(" Free Public Memory: "); michael@0: printConst(tokenInfo->ulFreePublicMemory, ConstCurrentSize, 1); michael@0: printf(" Total Private Memory: "); michael@0: printConst(tokenInfo->ulTotalPrivateMemory, ConstAvailableSizes, 1); michael@0: printf(" Free Private Memory: "); michael@0: printConst(tokenInfo->ulFreePrivateMemory, ConstCurrentSize, 1); michael@0: printf(" Hardware Version: %d.%02d\n", michael@0: VERSION(tokenInfo->hardwareVersion)); michael@0: printf(" Firmware Version: %d.%02d\n", michael@0: VERSION(tokenInfo->firmwareVersion)); michael@0: printf(" UTC Time: "); michael@0: printChars((char *)tokenInfo->utcTime,sizeof(tokenInfo->utcTime)); michael@0: break; michael@0: case ArgSessionInfo: michael@0: sessionInfo = (CK_SESSION_INFO *)ptr->data; michael@0: printf(" SlotID: 0x%08lx\n", sessionInfo->slotID); michael@0: printf(" State: "); michael@0: printConst(sessionInfo->state, ConstSessionState, 1); michael@0: printFlags(" Flags: ", sessionInfo->flags, ConstSessionFlags); michael@0: printf(" Device error: %lu 0x%08lx\n",sessionInfo->ulDeviceError, michael@0: sessionInfo->ulDeviceError); michael@0: break; michael@0: case ArgAttribute: michael@0: attribute = (CK_ATTRIBUTE *)ptr->data; michael@0: printf(" Attribute Type: "); michael@0: printConst(attribute->type, ConstAttribute, 1); michael@0: printf(" Attribute Data: "); michael@0: if (attribute->pValue == NULL) { michael@0: printf("NULL\n"); michael@0: printf("Attribute Len: %lu\n",attribute->ulValueLen); michael@0: } else { michael@0: constType = getConstFromAttribute(attribute->type); michael@0: if (constType != ConstNone) { michael@0: CK_ULONG value = (constType == ConstBool) ? michael@0: *(CK_BBOOL *)attribute->pValue : michael@0: *(CK_ULONG *)attribute->pValue; michael@0: printConst(value, constType, 1); michael@0: } else { michael@0: printf("\n"); michael@0: printDump(attribute->pValue, attribute->ulValueLen); michael@0: } michael@0: } michael@0: break; michael@0: case ArgMechanism: michael@0: mechanism = (CK_MECHANISM *)ptr->data; michael@0: printf(" Mechanism Type: "); michael@0: printConst(mechanism->mechanism, ConstMechanism, 1); michael@0: printf(" Mechanism Data:\n"); michael@0: printDump(mechanism->pParameter, mechanism->ulParameterLen); michael@0: break; michael@0: case ArgMechanismInfo: michael@0: mechanismInfo = (CK_MECHANISM_INFO *)ptr->data; michael@0: printf(" Minimum Key Size: %ld\n",mechanismInfo->ulMinKeySize); michael@0: printf(" Maximum Key Size: %ld\n",mechanismInfo->ulMaxKeySize); michael@0: printFlags(" Flags: ", mechanismInfo->flags, ConstMechanismFlags); michael@0: break; michael@0: case ArgInitializeArgs: michael@0: initArgs = (CK_C_INITIALIZE_ARGS *)ptr->data; michael@0: printFlags(" Flags: ", initArgs->flags, ConstInitializeFlags); michael@0: if (initArgs->LibraryParameters) { michael@0: printf("Params: %s\n",(char *)initArgs->LibraryParameters); michael@0: } michael@0: case ArgFunctionList: michael@0: functionList = (CK_FUNCTION_LIST *)ptr->data; michael@0: printf(" Version: %d.%02d\n", VERSION(functionList->version)); michael@0: #ifdef notdef michael@0: #undef CK_NEED_ARG_LIST michael@0: #define CK_PKCS11_FUNCTION_INFO(func) \ michael@0: printf(" %s: 0x%08lx\n", #func, (unsigned long) functionList->func ); michael@0: #include "pkcs11f.h" michael@0: #undef CK_NEED_ARG_LIST michael@0: #undef CK_PKCS11_FUNCTION_INFO michael@0: #endif michael@0: default: michael@0: ckrv = CKR_ARGUMENTS_BAD; michael@0: break; michael@0: } michael@0: michael@0: return ckrv; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Feeling ambitious? turn this whole thing into lexx yacc parser michael@0: * with full expressions. michael@0: */ michael@0: Value ** michael@0: parseArgs(int index, const char * bp) michael@0: { michael@0: const Commands *cp = &commands[index]; michael@0: int size = strlen(cp->fname); michael@0: int i; michael@0: CK_ULONG value; michael@0: char vname[512]; michael@0: Value **argList,*possible; michael@0: ConstType constType; michael@0: michael@0: /* michael@0: * skip pass the command michael@0: */ michael@0: if ((cp->fname[0] == 'C') && (cp->fname[1] == '_') && (bp[1] != '_')) { michael@0: size -= 2; michael@0: } michael@0: bp += size; michael@0: michael@0: /* michael@0: * Initialize our argument list michael@0: */ michael@0: argList = (Value **)malloc(sizeof(Value*)*MAX_ARGS); michael@0: for (i=0; i < MAX_ARGS; i++) { argList[i] = NULL; } michael@0: michael@0: /* michael@0: * Walk the argument list parsing it... michael@0: */ michael@0: for (i=0 ;i < MAX_ARGS; i++) { michael@0: ArgType type = cp->args[i] & ArgMask; michael@0: int error; michael@0: michael@0: /* strip blanks */ michael@0: bp = strip(bp); michael@0: michael@0: /* if we hit ArgNone, we've nabbed all the arguments we need */ michael@0: if (type == ArgNone) { michael@0: break; michael@0: } michael@0: michael@0: /* if we run out of space in the line, we weren't given enough michael@0: * arguments... */ michael@0: if (*bp == '\0') { michael@0: /* we're into optional arguments, ok to quit now */ michael@0: if (cp->args[i] & ArgOpt) { michael@0: break; michael@0: } michael@0: fprintf(stderr,"%s: only %d args found,\n",cp->fname,i); michael@0: parseFree(argList); michael@0: return NULL; michael@0: } michael@0: michael@0: /* collect all the rest of the command line and send michael@0: * it as a single argument */ michael@0: if (cp->args[i] & ArgFull) { michael@0: int size = strlen(bp)+1; michael@0: argList[i] = NewValue(type, size); michael@0: memcpy(argList[i]->data, bp, size); michael@0: break; michael@0: } michael@0: michael@0: /* michael@0: * look up the argument in our variable list first... only michael@0: * exception is the new argument type for set... michael@0: */ michael@0: error = 0; michael@0: if ((cp->args[i] != (ArgVar|ArgNew)) && michael@0: (possible = varLookup(bp,vname,sizeof(vname),&error))) { michael@0: /* ints are only compatible with other ints... all other types michael@0: * are interchangeable... */ michael@0: if (type != ArgVar) { /* ArgVar's match anyone */ michael@0: if ((type == ArgULong) ^ michael@0: ((possible->type & ArgMask) == ArgULong)) { michael@0: fprintf(stderr,"%s: Arg %d incompatible type with <%s>\n", michael@0: cp->fname,i+1,vname); michael@0: argFree(possible); michael@0: parseFree(argList); michael@0: return NULL; michael@0: } michael@0: /* michael@0: * ... that is as long as they are big enough... michael@0: */ michael@0: if (ArgSize(type) > possible->size) { michael@0: fprintf(stderr, michael@0: "%s: Arg %d %s is too small (%d bytes needs to be %d bytes)\n", michael@0: cp->fname,i+1,vname,possible->size,ArgSize(type)); michael@0: argFree(possible); michael@0: parseFree(argList); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: /* everything looks kosher here, use it */ michael@0: argList[i] = possible; michael@0: michael@0: bp = readChars(bp,vname,sizeof(vname)); michael@0: if (cp->args[i] & ArgOut) { michael@0: possible->type |= ArgOut; michael@0: } michael@0: continue; michael@0: } michael@0: michael@0: if (error == 1) { michael@0: parseFree(argList); michael@0: return NULL; michael@0: } michael@0: michael@0: /* create space for our argument */ michael@0: argList[i] = NewValue(type, 1); michael@0: michael@0: if ((PL_strncasecmp(bp, "null", 4) == 0) && ((bp[4] == 0) michael@0: || (bp[4] == ' ') || (bp[4] =='\t') || (bp[4] =='\n'))) { michael@0: if (cp->args[i] == ArgULong) { michael@0: fprintf(stderr, "%s: Arg %d CK_ULONG can't be NULL\n", michael@0: cp->fname,i+1); michael@0: parseFree(argList); michael@0: return NULL; michael@0: } michael@0: argFreeData(argList[i]); michael@0: argList[i]->data = NULL; michael@0: argList[i]->size = 0; michael@0: bp += 4; michael@0: if (*bp) bp++; michael@0: continue; michael@0: } michael@0: michael@0: /* if we're an output variable, we need to add it */ michael@0: if (cp->args[i] & ArgOut) { michael@0: if (PL_strncasecmp(bp,"file(",5) == 0 /* ) */ ) { michael@0: char filename[512]; michael@0: bp = readChars(bp+5,filename,sizeof(filename)); michael@0: size = PL_strlen(filename); michael@0: if ((size > 0) && (/* ( */filename[size-1] == ')')) { michael@0: filename[size-1] = 0; michael@0: } michael@0: filename[size] = 0; michael@0: argList[i]->filename = (char *)malloc(size+1); michael@0: michael@0: PL_strcpy(argList[i]->filename,filename); michael@0: michael@0: argList[i]->type |= ArgOut|ArgFile; michael@0: break; michael@0: } michael@0: bp = AddVariable(bp,&argList[i]); michael@0: argList[i]->type |= ArgOut; michael@0: continue; michael@0: } michael@0: michael@0: if (PL_strncasecmp(bp, "file(", 5) == 0 /* ) */ ) { michael@0: char filename[512]; michael@0: michael@0: bp = readChars(bp+5,filename,sizeof(filename)); michael@0: size = PL_strlen(filename); michael@0: if ((size > 0) && ( /* ( */ filename[size-1] == ')')) { michael@0: filename[size-1] = 0; michael@0: } michael@0: michael@0: if (restore(filename,argList[i]) != CKR_OK) { michael@0: parseFree(argList); michael@0: return NULL; michael@0: } michael@0: continue; michael@0: } michael@0: michael@0: switch (type) { michael@0: case ArgULong: michael@0: bp = constLookup(bp, &value, &constType); michael@0: *(int *)argList[i]->data = value; michael@0: argList[i]->constType = constType; michael@0: break; michael@0: case ArgVar: michael@0: argFreeData(argList[i]); michael@0: size = getEnd(bp)+1; michael@0: argList[i]->data = (void *)malloc(size); michael@0: argList[i]->size = size; michael@0: /* fall through */ michael@0: case ArgInfo: michael@0: case ArgSlotInfo: michael@0: case ArgTokenInfo: michael@0: case ArgSessionInfo: michael@0: case ArgAttribute: michael@0: case ArgMechanism: michael@0: case ArgMechanismInfo: michael@0: case ArgInitializeArgs: michael@0: case ArgUTF8: michael@0: case ArgChar: michael@0: bp = readChars(bp,(char *)argList[i]->data,argList[i]->size); michael@0: case ArgNone: michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: return argList; michael@0: } michael@0: michael@0: /* lookup the command in the array */ michael@0: int michael@0: lookup(const char *buf) michael@0: { michael@0: int size,i; michael@0: int buflen; michael@0: michael@0: buflen = PL_strlen(buf); michael@0: michael@0: for ( i = 0; i < commandCount; i++) { michael@0: size = PL_strlen(commands[i].fname); michael@0: michael@0: if (size <= buflen) { michael@0: if (PL_strncasecmp(buf,commands[i].fname,size) == 0) { michael@0: return i; michael@0: } michael@0: } michael@0: if (size-2 <= buflen) { michael@0: if (commands[i].fname[0] == 'C' && commands[i].fname[1] == '_' && michael@0: (PL_strncasecmp(buf,&commands[i].fname[2],size-2) == 0)) { michael@0: return i; michael@0: } michael@0: } michael@0: } michael@0: fprintf(stderr,"Can't find command %s\n",buf); michael@0: return -1; michael@0: } michael@0: michael@0: void michael@0: putOutput(Value **ptr) michael@0: { michael@0: int i; michael@0: michael@0: for (i=0; i < MAX_ARGS; i++) { michael@0: ArgType type; michael@0: michael@0: if (ptr[i] == NULL) break; michael@0: michael@0: type = ptr[i]->type; michael@0: michael@0: ptr[i]->type &= ~ArgOut; michael@0: if (type == ArgNone) { michael@0: break; michael@0: } michael@0: if (type & ArgOut) { michael@0: (void) printArg(ptr[i],i+1); michael@0: } michael@0: if (type & ArgFile) { michael@0: save(ptr[i]->filename,ptr[i]); michael@0: free(ptr[i]->filename); michael@0: ptr[i]->filename= NULL; /* paranoia */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: CK_RV michael@0: unloadModule(Module *module) michael@0: { michael@0: char *disableUnload = NULL; michael@0: michael@0: disableUnload = PR_GetEnv("NSS_DISABLE_UNLOAD"); michael@0: michael@0: if (module->library && !disableUnload) { michael@0: PR_UnloadLibrary(module->library); michael@0: } michael@0: michael@0: module->library = NULL; michael@0: module->functionList = NULL; michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: loadModule(Module *module, char *library) michael@0: { michael@0: PRLibrary *newLibrary; michael@0: CK_C_GetFunctionList getFunctionList; michael@0: CK_FUNCTION_LIST *functionList; michael@0: CK_RV ckrv; michael@0: michael@0: newLibrary = PR_LoadLibrary(library); michael@0: if (!newLibrary) { michael@0: fprintf(stderr,"Couldn't load library %s\n",library); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: getFunctionList = (CK_C_GetFunctionList) michael@0: PR_FindSymbol(newLibrary,"C_GetFunctionList"); michael@0: if (!getFunctionList) { michael@0: fprintf(stderr,"Couldn't find \"C_GetFunctionList\" in %s\n",library); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: michael@0: ckrv = (*getFunctionList)(&functionList); michael@0: if (ckrv != CKR_OK) { michael@0: return ckrv; michael@0: } michael@0: michael@0: if (module->library) { michael@0: PR_UnloadLibrary(module->library); michael@0: } michael@0: michael@0: module->library = newLibrary; michael@0: module->functionList = functionList; michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: static void michael@0: printHelp(int index, int full) michael@0: { michael@0: int j; michael@0: printf(" %s", commands[index].fname); michael@0: for (j=0; j < MAX_ARGS; j++) { michael@0: ArgType type = commands[index].args[j] & ArgMask; michael@0: if (type == ArgNone) { michael@0: break; michael@0: } michael@0: printf(" %s", valueString[type]); michael@0: } michael@0: printf("\n"); michael@0: printf(" %s\n",commands[index].helpString); michael@0: } michael@0: michael@0: /* add Topical help here ! */ michael@0: static CK_RV michael@0: printTopicHelp(char *topic) michael@0: { michael@0: int size,i; michael@0: int topicLen; michael@0: michael@0: topicLen = PL_strlen(topic); michael@0: michael@0: for ( i = 0; i < topicCount; i++) { michael@0: size = PL_strlen(topics[i].name); michael@0: michael@0: if (size <= topicLen) { michael@0: if (PL_strncasecmp(topic,topics[i].name,size) == 0) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (i == topicCount) { michael@0: fprintf(stderr,"Can't find topic '%s'\n", topic); michael@0: return CKR_DATA_INVALID; michael@0: } michael@0: michael@0: printf(" %s", topic); michael@0: printf("\n"); michael@0: printf(" %s\n",topics[i].helpString); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: static CK_RV michael@0: printGeneralHelp(void) michael@0: { michael@0: int i; michael@0: printf(" To get help on commands, select from the list below:"); michael@0: for ( i = 0; i < commandCount; i++) { michael@0: if (i % 5 == 0) printf("\n"); michael@0: printf("%s,", commands[i].fname); michael@0: } michael@0: printf("\n"); michael@0: /* print help topics */ michael@0: printf(" To get help on a topic, select from the list below:"); michael@0: for ( i = 0; i < topicCount; i++) { michael@0: if (i % 5 == 0) printf("\n"); michael@0: printf("%s,", topics[i].name); michael@0: } michael@0: printf("\n"); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: static CK_RV michael@0: quitIf(CK_ULONG a, const char *cmp, CK_ULONG b) michael@0: { michael@0: if (strcmp(cmp, "<") == 0) { michael@0: return (a < b) ? CKR_QUIT : CKR_OK; michael@0: } else if (strcmp(cmp, ">") == 0) { michael@0: return (a > b) ? CKR_QUIT : CKR_OK; michael@0: } else if (strcmp(cmp, "<=") == 0) { michael@0: return (a <= b) ? CKR_QUIT : CKR_OK; michael@0: } else if (strcmp(cmp, ">=") == 0) { michael@0: return (a >= b) ? CKR_QUIT : CKR_OK; michael@0: } else if (strcmp(cmp, "=") == 0) { michael@0: return (a == b) ? CKR_QUIT : CKR_OK; michael@0: } else if (strcmp(cmp, "!=") == 0) { michael@0: return (a != b) ? CKR_QUIT : CKR_OK; michael@0: } michael@0: printf("Unkown integer comparator: '%s'\n", cmp); michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: static CK_RV michael@0: quitIfString(const char *a, const char *cmp, const char *b) michael@0: { michael@0: michael@0: if (strcmp(cmp, "=") == 0) { michael@0: return (strcmp(a,b) == 0) ? CKR_QUIT : CKR_OK; michael@0: } else if (strcmp(cmp, "!=") == 0) { michael@0: return (strcmp(a,b) != 0) ? CKR_QUIT : CKR_OK; michael@0: } michael@0: printf("Unkown string comparator: '%s'\n", cmp); michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: CK_RV run(const char *); michael@0: CK_RV timeCommand(const char *); michael@0: CK_RV loop(const char *filename, const char *var, michael@0: CK_ULONG start, CK_ULONG end, CK_ULONG step) ; michael@0: michael@0: /* michael@0: * Actually dispatch the function... Bad things happen michael@0: * if these don't match the commands array. michael@0: */ michael@0: CK_RV michael@0: do_func(int index, Value **a) michael@0: { michael@0: int value, helpIndex; michael@0: static Module module = { NULL, NULL} ; michael@0: CK_FUNCTION_LIST *func = module.functionList; michael@0: michael@0: switch (commands[index].fType) { michael@0: case F_C_Initialize: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Initialize((void *)a[0]->data); michael@0: case F_C_Finalize: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Finalize((void *)a[0]->data); michael@0: case F_C_GetInfo: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetInfo((CK_INFO *)a[0]->data); michael@0: case F_C_GetFunctionList: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetFunctionList((CK_FUNCTION_LIST **)a[0]->data); michael@0: case F_C_GetSlotList: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetSlotList((CK_BBOOL)*(CK_ULONG *)a[0]->data, michael@0: (CK_SLOT_ID *)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_GetSlotInfo: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetSlotInfo(*(CK_ULONG *)a[0]->data, michael@0: (CK_SLOT_INFO *)a[1]->data); michael@0: case F_C_GetTokenInfo: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetTokenInfo(*(CK_ULONG *)a[0]->data, michael@0: (CK_TOKEN_INFO *)a[1]->data); michael@0: case F_C_GetMechanismList: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: if (a[1]->data) { michael@0: a[1]->constType = ConstMechanism; michael@0: } michael@0: return func->C_GetMechanismList(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM_TYPE*)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_GetMechanismInfo: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetMechanismInfo(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data, michael@0: (CK_MECHANISM_INFO *)a[2]->data); michael@0: case F_C_InitToken: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_InitToken(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data); michael@0: case F_C_InitPIN: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_InitPIN(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_SetPIN: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SetPIN(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: *(CK_ULONG *)a[4]->data); michael@0: case F_C_OpenSession: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_OpenSession(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data, michael@0: (void *)NULL, michael@0: (CK_NOTIFY) NULL, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_CloseSession: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_CloseSession(*(CK_ULONG *)a[0]->data); michael@0: case F_C_CloseAllSessions: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_CloseAllSessions(*(CK_ULONG *)a[0]->data); michael@0: case F_C_GetSessionInfo: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetSessionInfo(*(CK_ULONG *)a[0]->data, michael@0: (CK_SESSION_INFO *)a[1]->data); michael@0: case F_C_GetOperationState: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetOperationState(*(CK_ULONG *)a[0]->data, michael@0: (CK_BYTE *)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_SetOperationState: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SetOperationState(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data, michael@0: *(CK_ULONG *)a[4]->data); michael@0: case F_C_Login: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Login(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data, michael@0: (CK_CHAR *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data); michael@0: case F_C_Logout: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Logout(*(CK_ULONG *)a[0]->data); michael@0: case F_C_CreateObject: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_CreateObject(*(CK_ULONG *)a[0]->data, michael@0: (CK_ATTRIBUTE *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_ULONG *)a[3]->data); michael@0: case F_C_CopyObject: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_CopyObject(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[0]->data, michael@0: (CK_ATTRIBUTE *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_ULONG *)a[3]->data); michael@0: case F_C_DestroyObject: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DestroyObject(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data); michael@0: case F_C_GetObjectSize: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetObjectSize(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_GetAttributeValue: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetAttributeValue(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data, michael@0: (CK_ATTRIBUTE *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data); michael@0: case F_C_SetAttributeValue: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SetAttributeValue(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data, michael@0: (CK_ATTRIBUTE *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data); michael@0: case F_C_FindObjectsInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_FindObjectsInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_ATTRIBUTE *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_FindObjects: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_FindObjects(*(CK_ULONG *)a[0]->data, michael@0: (CK_ULONG *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_ULONG *)a[3]->data); michael@0: case F_C_FindObjectsFinal: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_FindObjectsFinal(*(CK_ULONG *)a[0]->data); michael@0: case F_C_EncryptInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_EncryptInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_Encrypt: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Encrypt(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_EncryptUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_EncryptUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_EncryptFinal: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_EncryptFinal(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_DecryptInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DecryptInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_Decrypt: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Decrypt(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_DecryptUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DecryptUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_DecryptFinal: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DecryptFinal(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_DigestInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DigestInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data); michael@0: case F_C_Digest: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Digest(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_DigestUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DigestUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_DigestKey: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DigestKey(*(CK_ULONG *)a[0]->data, michael@0: *(CK_ULONG *)a[1]->data); michael@0: case F_C_DigestFinal: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DigestFinal(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: case F_C_SignInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SignInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_Sign: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Sign(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_SignUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SignUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_SignFinal: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SignFinal(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: (CK_ULONG *)a[2]->data); michael@0: michael@0: case F_C_SignRecoverInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SignRecoverInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_SignRecover: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SignRecover(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_VerifyInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_VerifyInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_Verify: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_Verify(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: *(CK_ULONG *)a[4]->data); michael@0: case F_C_VerifyUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_VerifyUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_VerifyFinal: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_VerifyFinal(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: michael@0: case F_C_VerifyRecoverInit: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_VerifyRecoverInit(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_VerifyRecover: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_VerifyRecover(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_DigestEncryptUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DigestEncryptUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_DecryptDigestUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DecryptDigestUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_SignEncryptUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SignEncryptUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_DecryptVerifyUpdate: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DecryptVerifyUpdate(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_GenerateKey: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GenerateKey(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: (CK_ATTRIBUTE *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data, michael@0: (CK_ULONG *)a[4]->data); michael@0: case F_C_GenerateKeyPair: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GenerateKeyPair(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: (CK_ATTRIBUTE *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data, michael@0: (CK_ATTRIBUTE *)a[4]->data, michael@0: *(CK_ULONG *)a[5]->data, michael@0: (CK_ULONG *)a[6]->data, michael@0: (CK_ULONG *)a[7]->data); michael@0: case F_C_WrapKey: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_WrapKey(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data, michael@0: (CK_CHAR *)a[5]->data, michael@0: (CK_ULONG *)a[6]->data); michael@0: case F_C_UnwrapKey: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_UnwrapKey(*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_CHAR *)a[3]->data, michael@0: *(CK_ULONG *)a[4]->data, michael@0: (CK_ATTRIBUTE *)a[5]->data, michael@0: *(CK_ULONG *)a[6]->data, michael@0: (CK_ULONG *)a[7]->data); michael@0: case F_C_DeriveKey: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_DeriveKey (*(CK_ULONG *)a[0]->data, michael@0: (CK_MECHANISM *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data, michael@0: (CK_ATTRIBUTE *)a[3]->data, michael@0: *(CK_ULONG *)a[4]->data, michael@0: (CK_ULONG *)a[5]->data); michael@0: case F_C_SeedRandom: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_SeedRandom(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_GenerateRandom: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GenerateRandom(*(CK_ULONG *)a[0]->data, michael@0: (CK_CHAR *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_C_GetFunctionStatus: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_GetFunctionStatus(*(CK_ULONG *)a[0]->data); michael@0: case F_C_CancelFunction: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_CancelFunction(*(CK_ULONG *)a[0]->data); michael@0: case F_C_WaitForSlotEvent: michael@0: if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; michael@0: return func->C_WaitForSlotEvent(*(CK_ULONG *)a[0]->data, michael@0: (CK_ULONG *)a[1]->data, michael@0: (void *)a[2]->data); michael@0: /* set a variable */ michael@0: case F_SetVar: michael@0: case F_SetStringVar: michael@0: (void) DeleteVariable(a[0]->data); michael@0: (void) AddVariable(a[0]->data,&a[1]); michael@0: return CKR_OK; michael@0: /* print a value */ michael@0: case F_Print: michael@0: return printArg(a[0],0); michael@0: case F_SaveVar: michael@0: return save(a[0]->data,a[1]); michael@0: case F_RestoreVar: michael@0: return restore(a[0]->data,a[1]); michael@0: case F_Delete: michael@0: return DeleteVariable(a[0]->data); michael@0: case F_Increment: michael@0: return increment(a[0], *(CK_ULONG *)a[1]->data); michael@0: case F_Decrement: michael@0: return decrement(a[0], *(CK_ULONG *)a[1]->data); michael@0: case F_List: michael@0: return list(); michael@0: case F_Run: michael@0: return run(a[0]->data); michael@0: case F_Time: michael@0: return timeCommand(a[0]->data); michael@0: case F_Load: michael@0: return loadModule(&module,a[0]->data); michael@0: case F_Unload: michael@0: return unloadModule(&module); michael@0: case F_NewArray: michael@0: (void) DeleteVariable(a[0]->data); michael@0: return ArrayVariable(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data); michael@0: case F_NewTemplate: michael@0: (void) DeleteVariable(a[0]->data); michael@0: return ArrayTemplate(a[0]->data,a[1]->data); michael@0: case F_BuildTemplate: michael@0: return BuildTemplate(a[0]); michael@0: case F_SetTemplate: michael@0: return SetTemplate(a[0], michael@0: *(CK_ULONG *)a[1]->data, michael@0: *(CK_ULONG *)a[2]->data); michael@0: case F_NewMechanism: michael@0: (void) DeleteVariable(a[0]->data); michael@0: return NewMechanism(a[0]->data,*(CK_ULONG *)a[1]->data); michael@0: case F_NewInitializeArgs: michael@0: (void) DeleteVariable(a[0]->data); michael@0: return NewInitializeArgs(a[0]->data,*(CK_ULONG *)a[1]->data,a[2]->data); michael@0: case F_System: michael@0: value = *(int *)a[0]->data; michael@0: if (value & 0x80000000) { michael@0: systemFlags &= ~value; michael@0: } else { michael@0: systemFlags |= value; michael@0: } michael@0: return CKR_OK; michael@0: case F_Loop: michael@0: return loop(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data, michael@0: *(CK_ULONG *)a[3]->data,*(CK_ULONG *)a[4]->data); michael@0: case F_Help: michael@0: if (a[0]) { michael@0: helpIndex = lookup(a[0]->data); michael@0: if (helpIndex < 0) { michael@0: return printTopicHelp(a[0]->data); michael@0: } michael@0: printHelp(helpIndex, 1); michael@0: return CKR_OK; michael@0: } michael@0: return printGeneralHelp(); michael@0: case F_QuitIfString: michael@0: return quitIfString(a[0]->data,a[1]->data,a[2]->data); michael@0: case F_QuitIf: michael@0: return quitIf(*(CK_ULONG *)a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data); michael@0: case F_Quit: michael@0: return CKR_QUIT; michael@0: default: michael@0: fprintf(stderr, michael@0: "Function %s not yet supported\n",commands[index].fname ); michael@0: return CKR_OK; michael@0: } michael@0: /* Not Reached */ michael@0: return CKR_OK; michael@0: } michael@0: michael@0: CK_RV michael@0: processCommand(const char * buf) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: int index; michael@0: const char *bp; michael@0: Value **arglist; michael@0: michael@0: bp = strip(buf); michael@0: /* allow comments and blank lines in scripts */ michael@0: if ((*bp == '#') || (*bp == 0) || (*bp == '\n')){ michael@0: return CKR_OK; michael@0: } michael@0: michael@0: index = lookup(bp); michael@0: michael@0: if (index < 0) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: arglist = parseArgs(index,bp); michael@0: if (arglist == NULL) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: error = do_func(index,arglist); michael@0: if (error == CKR_OK) { michael@0: putOutput(arglist); michael@0: } else if (error != CKR_QUIT) { michael@0: printf(">> Error : "); michael@0: printConst(error, ConstResult, 1); michael@0: } michael@0: michael@0: parseFree(arglist); michael@0: return error; michael@0: } michael@0: michael@0: CK_RV michael@0: timeCommand(const char *command) michael@0: { michael@0: CK_RV ckrv; michael@0: PRIntervalTime startTime = PR_IntervalNow(); michael@0: PRIntervalTime endTime; michael@0: PRIntervalTime elapsedTime; michael@0: michael@0: ckrv = processCommand(command); michael@0: michael@0: endTime = PR_IntervalNow(); michael@0: elapsedTime = endTime - startTime; michael@0: printf("Time -- %d msec \n", michael@0: PR_IntervalToMilliseconds(elapsedTime)); michael@0: michael@0: return ckrv; michael@0: } michael@0: michael@0: michael@0: michael@0: CK_RV michael@0: process(FILE *inFile,int user) michael@0: { michael@0: char buf[2048]; michael@0: CK_RV error; michael@0: CK_RV ckrv = CKR_OK; michael@0: michael@0: if (user) { printf("pkcs11> "); fflush(stdout); } michael@0: michael@0: while (fgets(buf,2048,inFile) != NULL) { michael@0: michael@0: if (!user) printf("* %s",buf); michael@0: error = processCommand(buf); michael@0: if (error == CKR_QUIT) { michael@0: break; michael@0: } else if (error != CKR_OK) { michael@0: ckrv = error; michael@0: } michael@0: if (user) { michael@0: printf("pkcs11> "); fflush(stdout); michael@0: } michael@0: } michael@0: return ckrv; michael@0: } michael@0: michael@0: CK_RV michael@0: run(const char *filename) michael@0: { michael@0: FILE *infile; michael@0: CK_RV ckrv; michael@0: michael@0: infile = fopen(filename,"r"); michael@0: michael@0: if (infile == NULL) { michael@0: perror(filename); michael@0: return CKR_FUNCTION_FAILED; michael@0: } michael@0: michael@0: ckrv = process(infile, 0); michael@0: michael@0: fclose(infile); michael@0: return ckrv; michael@0: } michael@0: michael@0: CK_RV michael@0: loop(const char *filename, const char *var, michael@0: CK_ULONG start, CK_ULONG end, CK_ULONG step) michael@0: { michael@0: CK_ULONG i = 0; michael@0: Value *value = 0; michael@0: CK_RV ckrv; michael@0: michael@0: for (i=start; i < end; i += step) michael@0: { michael@0: value = NewValue(ArgULong, 1); michael@0: *(CK_ULONG *)value->data = i; michael@0: DeleteVariable(var); michael@0: AddVariable(var, &value); michael@0: ckrv = run(filename); michael@0: argFree(value); michael@0: if (ckrv == CKR_QUIT) { michael@0: break; michael@0: } michael@0: } michael@0: return ckrv; michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: /* I suppose that some day we could parse some arguments */ michael@0: (void) process(stdin, 1); michael@0: return 0; michael@0: }