nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / cmd / pk11util / pk11util.c
blob4929ad1e3819d310c347848978d5cf47bd5de096
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 #include <stdio.h>
39 #include <string.h>
41 #if defined(WIN32)
42 #undef __STDC__
43 #include "fcntl.h"
44 #include "io.h"
45 #include <fcntl.h>
46 #else
47 #include <unistd.h>
48 #include <sys/fcntl.h>
49 #endif
51 #include "secutil.h"
54 #include "nspr.h"
55 #include "prtypes.h"
56 #include "prtime.h"
57 #include "prlong.h"
58 #include "prinrval.h"
59 #include "prenv.h"
61 #include "pkcs11.h"
63 #include "pk11util.h"
65 #ifndef O_BINARY
66 #define O_BINARY 0
67 #endif
69 CK_ULONG systemFlags;
70 #define FLAG_NEGATE 0x80000000
71 #define FLAG_Verify 0x00000001
72 #define FLAG_VerifyFile 0x00000002
73 #define CKR_QUIT 0x80000000
75 int ArgSize(ArgType type);
76 const char *constLookup(const char *bp, CK_ULONG *value, ConstType *type);
78 int
79 isNum(char c)
81 return (c >= '0' && c <= '9');
84 int
85 isConst(const char *c)
87 CK_ULONG value;
88 ConstType type;
90 constLookup(c, &value, &type);
91 return type != ConstNone;
95 * see if the variable is really a 'size' function. This
96 * function may modify var if it is a size function.
98 char *
99 isSize(char *var, int *isArray)
101 char *ptr = NULL;
102 char *end;
103 int array = 0;
105 if (PL_strncasecmp(var,"sizeof(",/*)*/ 7) == 0) {
106 ptr = var + 7;
107 } else if (PL_strncasecmp(var,"size(",/*)*/ 5) == 0) {
108 ptr = var + 5;
109 } else if (PL_strncasecmp(var,"sizeofarray(",/*)*/ 12) == 0) {
110 ptr = var + 12;
111 array = 1;
112 } else if (PL_strncasecmp(var,"sizea(",/*)*/ 6) == 0) {
113 ptr = var + 6;
114 array = 1;
115 } else {
116 return NULL;
118 end = strchr(ptr,/*(*/ ')') ;
119 if (end == NULL) {
120 return NULL;
122 if (isArray) *isArray = array;
123 *end = 0;
124 return ptr;
127 void
128 printConst(CK_ULONG value, ConstType type, int newLine)
130 int i;
132 for (i=0; i < constCount; i++) {
133 if (consts[i].type == type && consts[i].value == value) {
134 printf("%s",consts[i].name);
135 break;
137 if (type == ConstNone && consts[i].value == value) {
138 printf("%s",consts[i].name);
139 break;
142 if (i == constCount) {
143 if ((type == ConstAvailableSizes) || (type == ConstCurrentSize)) {
144 printf("%lu",value);
145 } else {
146 printf("Unknown %s (%lu:0x%lx)",constTypeString[type],value,value);
149 if (newLine) {
150 printf("\n");
154 ConstType
155 getConstFromAttribute(CK_ATTRIBUTE_TYPE type)
157 int i;
159 for (i=0; i < constCount; i++) {
160 if (consts[i].type == ConstAttribute && consts[i].value == type) {
161 return consts[i].attrType;
164 return ConstNone;
167 void
168 printChars(const char *name, CK_ULONG size)
170 CK_ULONG i;
171 for (i=0; i < size; i++) {
172 if (name[i] == 0) {
173 break;
175 printf("%c",name[i]);
177 printf("\n");
180 #define DUMP_LEN 16
181 void printDump(const unsigned char *buf, int size)
183 int i,j;
185 for(i=0; i < size; i+= DUMP_LEN) {
186 printf(" ");
187 for (j=0; j< DUMP_LEN; j++) {
188 if (i+j < size) {
189 printf("%02x ",buf[i+j]);
190 } else {
191 printf(" ");
194 for (j=0; j< DUMP_LEN; j++) {
195 if (i+j < size) {
196 if (buf[i+j] < ' ' || buf[i+j] >= 0x7f) {
197 printf(".");
198 } else {
199 printf("%c",buf[i+j]);
201 } else {
202 printf(" ");
205 printf("\n");
210 * free an argument structure
212 void
213 argFreeData(Value *arg)
215 if (arg->data && ((arg->type & ArgStatic) == 0)) {
216 if ((arg->type & ArgMask) == ArgAttribute) {
217 int i;
218 CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)arg->data;
220 for (i=0; i < arg->arraySize; i++) {
221 free(template[i].pValue);
224 if ((arg->type & ArgMask) == ArgInitializeArgs) {
225 CK_C_INITIALIZE_ARGS *init = (CK_C_INITIALIZE_ARGS *)arg->data;
226 if (init->LibraryParameters) {
227 free(init->LibraryParameters);
230 free(arg->data);
232 arg->type &= ~ArgStatic;
233 arg->data = NULL;
236 void
237 argFree(Value *arg)
239 if (arg == NULL) return;
241 arg->reference--;
242 if (arg->reference == 0) {
243 if (arg->type & ArgFile) {
244 free(arg->filename);
246 argFreeData(arg);
247 free (arg);
252 * free and argument list
254 void
255 parseFree(Value **ap)
257 int i;
258 for (i=0 ; i < MAX_ARGS; i++) {
259 argFree(ap[i]);
264 * getEnd: how for to the end of this argmument list?
267 getEnd(const char *bp)
269 int count = 0;
271 while (*bp) {
272 if (*bp == ' ' || *bp == '\t' || *bp == '\n') return count;
273 count++;
274 bp++;
276 return (count);
281 * strip: return the first none white space character
283 const char *
284 strip(const char *bp)
286 while (*bp && (*bp == ' ' || *bp == '\t' || *bp == '\n')) bp++;
287 return bp;
291 * read in the next argument into dp ... don't overflow
293 const char *
294 readChars(const char *bp, char *dp, int max )
296 int count = 1;
297 while (*bp) {
298 if (*bp == ' ' || *bp == '\t' || *bp == '\n' ) {
299 *dp = 0;
300 return bp;
302 *dp++ = *bp++;
303 if (++count == max) break;
305 while (*bp && (*bp != ' ' && *bp != '\t' && *bp != '\n')) bp++;
306 *dp = 0;
307 return (bp);
310 Value * varLookup(const char *bp, char *vname, int max, int *error);
312 CK_ULONG
313 getValue(const char *v, int *error)
315 Value * varVal = NULL;
316 CK_ULONG retVal = 0;
317 ConstType type;
318 char tvar[512];
320 *error = 0;
322 varVal = varLookup( v, tvar, sizeof(tvar), error);
324 if (varVal) {
325 if ((varVal->type & ArgMask) == ArgULong) {
326 retVal = *(CK_ULONG *)varVal->data;
327 } else {
328 fprintf(stderr,"%s: is not a ulong\n", v);
329 *error = 1;
331 argFree(varVal);
332 return retVal;
334 constLookup(v, &retVal, &type);
335 return retVal;
338 Value *
339 NewValue(ArgType type, CK_ULONG arraySize)
341 Value *value;
343 value = (Value *)malloc(sizeof(Value));
344 if (!value) return NULL;
345 value->size = ArgSize(type)*arraySize;
346 value->type = type;
347 value->filename = NULL;
348 value->constType = ConstNone;
349 value->data = (void *)malloc(value->size);
350 if (!value->data) {
351 free(value);
352 return NULL;
354 value->reference = 1;
355 value->arraySize = (type == ArgChar) ? 1: arraySize;
357 memset(value->data, 0, value->size);
358 return value;
361 #define INVALID_INDEX 0xffffffff
363 CK_ULONG
364 handleArray(char *vname, int *error)
366 char *bracket;
367 CK_ULONG index = INVALID_INDEX;
369 if ((bracket = strchr(vname,'[')) != 0) {
370 char *tmpv = bracket+1;
371 *bracket = 0;
372 bracket = strchr(tmpv,']');
374 if (bracket == 0) {
375 fprintf(stderr,"%s: missing closing brace\n", vname);
376 return INVALID_INDEX;
378 *bracket = 0;
380 index = getValue(tmpv, error);
381 if (*error == 1) {
382 return INVALID_INDEX;
383 } else if (index == INVALID_INDEX) {
384 fprintf(stderr, "%s: 0x%x is an invalid index\n",vname,index);
385 *error = 1;
388 return index;
391 void *
392 makeArrayTarget(const char *vname, const Value *value, CK_ULONG index)
394 char * target;
395 CK_ULONG elementSize;
397 if (index >= (CK_ULONG)value->arraySize) {
398 fprintf(stderr, "%s[%d]: index larger than array size (%d)\n",
399 vname, index, value->arraySize);
400 return NULL;
403 target = (char *)value->data;
404 elementSize = value->size/value->arraySize;
405 target += index * elementSize;
406 return target;
410 * look up a variable from the variable chain
412 static Variable *varHead = NULL;
413 Value *
414 varLookup(const char *bp, char *vname, int max, int *error)
416 Variable *current;
417 CK_ULONG index = INVALID_INDEX;
418 int isArray = 0;
419 char *ptr;
420 *error = 0;
422 if (bp != NULL) {
423 readChars(bp, vname, max);
426 /* don't make numbers into variables */
427 if (isNum(vname[0])) {
428 return NULL;
430 /* nor consts */
431 if (isConst(vname)) {
432 return NULL;
434 /* handle sizeof() */
435 if ((ptr = isSize(vname, &isArray)) != NULL) {
436 CK_ULONG size;
437 Value *targetValue = NULL;
438 Value *sourceValue = varLookup(NULL, ptr, 0, error);
439 if (!sourceValue) {
440 if (*error == 0) {
441 /* just didn't find it */
442 *error = 1;
443 fprintf(stderr,"Couldn't find variable %s to take size of\n",
444 ptr);
445 return NULL;
448 size = isArray ? sourceValue->arraySize : sourceValue->size;
449 targetValue = NewValue(ArgULong,1);
450 memcpy(targetValue->data, &size, sizeof(size));
452 return targetValue;
455 /* modifies vname */
456 index = handleArray(vname, error);
457 if (*error == 1) {
458 return NULL;
461 for (current = varHead; current; current = current->next) {
462 if (PL_strcasecmp(current->vname, vname) == 0) {
463 char *target;
464 if (index == INVALID_INDEX) {
465 (current->value->reference)++;
466 return current->value;
468 target = makeArrayTarget(vname, current->value, index);
469 if (target) {
470 Value *element = NewValue(current->value->type, 1);
471 if (!element) {
472 fprintf(stderr, "MEMORY ERROR!\n");
473 *error = 1;
475 argFreeData(element);
476 element->data = target;
477 element->type |= ArgStatic;
478 return element;
480 *error = 1;
481 return NULL;
484 return NULL;
487 static CK_RV
488 list(void)
490 Variable *current;
492 if (varHead) {
493 printf(" %10s\t%16s\t%8s\tSize\tElements\n","Name","Type","Const");
494 } else {
495 printf(" no variables set\n");
498 for (current = varHead; current; current = current->next) {
499 printf(" %10s\t%16s\t%8s\t%d\t%d\n", current->vname,
500 valueString[current->value->type&ArgMask],
501 constTypeString[current->value->constType],
502 current->value->size, current->value->arraySize);
504 return CKR_OK;
507 CK_RV
508 printFlags(const char *s, CK_ULONG flags, ConstType type)
510 CK_ULONG i;
511 int needComma = 0;
513 printf("%s",s);
514 for (i=1; i ; i=i << 1) {
515 if (flags & i) {
516 printf("%s",needComma?",":"");
517 printConst(i, type, 0);
518 needComma=1;
521 if (!needComma) {
522 printf("Empty");
524 printf("\n");
525 return CKR_OK;
529 * add a new variable to the chain
531 const char *
532 AddVariable(const char *bp, Value **ptr)
534 char vname[512];
535 Variable *current;
536 int index = INVALID_INDEX;
537 int size;
538 int error = 0;
540 bp = readChars(bp,vname,sizeof(vname));
542 /* don't make numbers into variables */
543 if (isNum(vname[0])) {
544 return bp;
546 /* or consts */
547 if (isConst(vname)) {
548 return bp;
550 /* or NULLs */
551 if (vname[0] == 0) {
552 return bp;
554 /* or sizeof */
555 if (isSize(vname, NULL)) {
556 return bp;
558 /* arrays values should be written back to the original */
559 index = handleArray(vname, &error);
560 if (error == 1) {
561 return bp;
565 for (current = varHead; current; current = current->next) {
566 if (PL_strcasecmp(current->vname,vname) == 0) {
567 char *target;
568 /* found a complete object, return the found one */
569 if (index == INVALID_INDEX) {
570 argFree(*ptr);
571 *ptr = current->value;
572 return bp;
574 /* found an array, update the array element */
575 target = makeArrayTarget(vname, current->value, index);
576 if (target) {
577 memcpy(target, (*ptr)->data, (*ptr)->size);
578 argFreeData(*ptr);
579 (*ptr)->data = target;
580 (*ptr)->type |= ArgStatic;
582 return bp;
586 /* we are looking for an array and didn't find one */
587 if (index != INVALID_INDEX) {
588 return bp;
592 current = (Variable *)malloc(sizeof(Variable));
593 size = strlen(vname);
594 current->vname = (char *)malloc(size+1);
595 strcpy(current->vname,vname);
596 current->value = *ptr;
597 (*ptr)->reference++;
599 current->next = varHead;
600 varHead = current;
601 return bp;
604 ArgType
605 FindTypeByName(const char *typeName)
607 int i;
609 for (i=0; i < valueCount; i++) {
610 if (PL_strcasecmp(typeName,valueString[i]) == 0) {
611 return (ArgType) i;
613 if (valueString[i][0] == 'C' && valueString[i][1] == 'K' &&
614 valueString[i][2] == '_' &&
615 (PL_strcasecmp(typeName,&valueString[i][3]) == 0)) {
616 return (ArgType) i;
619 return ArgNone;
622 CK_RV
623 ArrayVariable(const char *bp, const char *typeName, CK_ULONG count)
625 ArgType type;
626 Value *value; /* new Value */
628 type = FindTypeByName(typeName);
629 if (type == ArgNone) {
630 fprintf(stderr,"Invalid type (%s)\n", typeName);
631 return CKR_FUNCTION_FAILED;
633 value = NewValue(type, count);
634 (void) AddVariable(bp, &value);
635 return CKR_OK;
638 #define MAX_TEMPLATE 25
640 CK_RV
641 ArrayTemplate(const char *bp, char *attributes)
643 char aname[512];
644 CK_ULONG attributeTypes[MAX_TEMPLATE];
645 CK_ATTRIBUTE *template;
646 Value *value; /* new Value */
647 char *ap;
648 int i, count = 0;
650 memcpy(aname,attributes,strlen(attributes)+1);
652 for (ap = aname, count = 0; ap && *ap && count < MAX_TEMPLATE; count++) {
653 char *cur = ap;
654 ConstType type;
656 ap = strchr(ap,',');
657 if (ap) {
658 *ap++ = 0;
661 (void)constLookup(cur, &attributeTypes[count], &type);
662 if ((type != ConstAttribute) && (type != ConstNone)) {
663 fprintf(stderr, "Unknown Attribute %s\n", cur);
664 return CKR_FUNCTION_FAILED;
668 value = NewValue(ArgAttribute, count);
670 template = (CK_ATTRIBUTE *)value->data;
671 for (i=0; i < count ; i++) {
672 template[i].type = attributeTypes[i];
674 (void) AddVariable(bp, &value);
675 return CKR_OK;
678 CK_RV
679 BuildTemplate(Value *vp)
681 CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data;
682 int i;
684 for (i=0; i < vp->arraySize; i++) {
685 if (((signed long)template[i].ulValueLen) > 0) {
686 if (template[i].pValue) free(template[i].pValue);
687 template[i].pValue = malloc(template[i].ulValueLen);
690 return CKR_OK;
693 CK_RV
694 SetTemplate(Value *vp, CK_ULONG index, CK_ULONG value)
696 CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data;
697 int isbool = 0;
698 CK_ULONG len;
699 ConstType attrType;
701 if (index >= (CK_ULONG) vp->arraySize) {
702 fprintf(stderr,"index (%lu) greater than array (%d)\n",
703 index, vp->arraySize);
704 return CKR_ARGUMENTS_BAD;
706 attrType = getConstFromAttribute(template[index].type);
708 if (attrType == ConstNone) {
709 fprintf(stderr,"can't set index (%lu) because ", index);
710 printConst(template[index].type,ConstAttribute, 0);
711 fprintf(stderr, " is not a CK_BBOOL or CK_ULONG\n");
712 return CKR_ARGUMENTS_BAD;
714 isbool = (attrType == ConstBool);
715 len = isbool ? sizeof (CK_BBOOL) : sizeof(CK_ULONG);
716 if ((template[index].ulValueLen != len) || (template[index].pValue)) {
717 free(template[index].pValue);
718 template[index].pValue = malloc(len);
719 template[index].ulValueLen = len;
721 if (isbool) {
722 *(CK_BBOOL *)template[index].pValue = (CK_BBOOL) value;
723 } else {
724 *(CK_ULONG *)template[index].pValue = (CK_ULONG) value;
726 return CKR_OK;
730 CK_RV
731 NewMechanism(const char *bp, CK_ULONG mechType)
733 Value *value; /* new Value */
734 CK_MECHANISM *mechanism;
736 value = NewValue(ArgMechanism, 1);
737 mechanism = (CK_MECHANISM *)value->data;
738 mechanism->mechanism = mechType;
739 mechanism->pParameter = NULL;
740 mechanism->ulParameterLen = 0;
741 (void) AddVariable(bp, &value);
742 return CKR_OK;
745 CK_RV
746 NewInitializeArgs(const char *bp, CK_ULONG flags, const char *param)
748 Value *value; /* new Value */
749 CK_C_INITIALIZE_ARGS *init;
751 value = NewValue(ArgInitializeArgs, 1);
752 init = (CK_C_INITIALIZE_ARGS *)value->data;
753 init->flags = flags;
754 if (strcmp(param, "null") != 0) {
755 init->LibraryParameters = (CK_CHAR_PTR *)strdup(param);
757 (void) AddVariable(bp, &value);
758 return CKR_OK;
762 * add a new variable to the chain
764 CK_RV
765 DeleteVariable(const char *bp)
767 char vname[512];
768 Variable **current;
770 bp = readChars(bp,vname,sizeof(vname));
772 for (current = &varHead; *current; current = &(*current)->next) {
773 if (PL_strcasecmp((*current)->vname,vname) == 0) {
774 argFree((*current)->value);
775 *current = (*current)->next;
776 break;
779 return CKR_OK;
783 * convert an octal value to integer
785 CK_ULONG
786 otoi(const char *o)
788 CK_ULONG value = 0;
790 while (*o) {
791 if ((*o >= '0') && (*o <= '7')) {
792 value = (value << 3) | (unsigned)(*o - '0');
793 } else {
794 break;
797 return value;
801 * convert a hex value to integer
803 CK_ULONG
804 htoi(const char *x)
806 CK_ULONG value = 0;
808 while (*x) {
809 if ((*x >= '0') && (*x <= '9')) {
810 value = (value << 4) | (unsigned)(*x - '0');
811 } else if ((*x >= 'a') && (*x <= 'f')) {
812 value = (value << 4) | (unsigned)(*x - 'a');
813 } else if ((*x >= 'A') && (*x <= 'F')) {
814 value = (value << 4) | (unsigned)(*x - 'A');
815 } else {
816 break;
819 return value;
824 * look up or decode a constant value
826 const char *
827 constLookup(const char *bp, CK_ULONG *value, ConstType *type)
829 char vname[512];
830 int i;
832 bp = readChars(bp,vname,sizeof(vname));
834 for (i=0; i < constCount; i++) {
835 if ((PL_strcasecmp(consts[i].name,vname) == 0) ||
836 PL_strcasecmp(consts[i].name+5,vname) == 0) {
837 *value = consts[i].value;
838 *type = consts[i].type;
839 return bp;
843 *type = ConstNone;
844 if (vname[0] == '0' && vname[1] == 'X') {
845 *value = htoi(&vname[2]);
846 } else if (vname[0] == '0') {
847 *value = otoi(&vname[1]);
848 } else {
849 *value = atoi(vname);
851 return bp;
855 ArgSize(ArgType type)
857 int size=0;
858 type &= ArgMask;
860 switch (type) {
861 case ArgNone:
862 size = 0;
863 break;
864 case ArgULong:
865 size = sizeof(CK_ULONG);
866 break;
867 case ArgVar:
868 size = 1; /* get's changed later */
869 break;
870 case ArgChar:
871 case ArgUTF8:
872 size = 1;
873 break;
874 case ArgInfo:
875 size = sizeof(CK_INFO);
876 break;
877 case ArgSlotInfo:
878 size = sizeof(CK_SLOT_INFO);
879 break;
880 case ArgTokenInfo:
881 size = sizeof(CK_TOKEN_INFO);
882 break;
883 case ArgSessionInfo:
884 size = sizeof(CK_SESSION_INFO);
885 break;
886 case ArgAttribute:
887 size = sizeof(CK_ATTRIBUTE);
888 break;
889 case ArgMechanism:
890 size = sizeof(CK_MECHANISM);
891 break;
892 case ArgMechanismInfo:
893 size = sizeof(CK_MECHANISM_INFO);
894 break;
895 case ArgInitializeArgs:
896 size = sizeof(CK_C_INITIALIZE_ARGS);
897 break;
898 case ArgFunctionList:
899 size = sizeof(CK_FUNCTION_LIST);
900 break;
901 default:
902 break;
905 return (size);
908 CK_RV
909 restore(const char *filename,Value *ptr)
911 int fd,size;
913 fd = open(filename,O_RDONLY|O_BINARY);
914 if (fd < 0) {
915 perror(filename);
916 return CKR_FUNCTION_FAILED;
919 size = read(fd,ptr->data,ptr->size);
920 if (systemFlags & FLAG_VerifyFile) {
921 printDump(ptr->data,ptr->size);
923 if (size < 0) {
924 perror(filename);
925 return CKR_FUNCTION_FAILED;
926 } else if (size != ptr->size) {
927 fprintf(stderr,"%s: only read %d bytes, needed to read %d bytes\n",
928 filename,size,ptr->size);
929 return CKR_FUNCTION_FAILED;
931 close(fd);
932 return CKR_OK;
935 CK_RV
936 save(const char *filename,Value *ptr)
938 int fd,size;
940 fd = open(filename,O_WRONLY|O_BINARY|O_CREAT,0666);
941 if (fd < 0) {
942 perror(filename);
943 return CKR_FUNCTION_FAILED;
946 size = write(fd,ptr->data,ptr->size);
947 if (size < 0) {
948 perror(filename);
949 return CKR_FUNCTION_FAILED;
950 } else if (size != ptr->size) {
951 fprintf(stderr,"%s: only wrote %d bytes, need to write %d bytes\n",
952 filename,size,ptr->size);
953 return CKR_FUNCTION_FAILED;
955 close(fd);
956 return CKR_OK;
959 static CK_RV
960 increment(Value *ptr, CK_ULONG value)
962 if ((ptr->type & ArgMask) != ArgULong) {
963 return CKR_ARGUMENTS_BAD;
965 *(CK_ULONG *)ptr->data += value;
966 return CKR_OK;
969 static CK_RV
970 decrement(Value *ptr, CK_ULONG value)
972 if ((ptr->type & ArgMask) != ArgULong) {
973 return CKR_ARGUMENTS_BAD;
975 *(CK_ULONG *)ptr->data -= value;
976 return CKR_OK;
979 CK_RV
980 printArg(Value *ptr,int arg_number)
982 ArgType type = ptr->type & ArgMask;
983 CK_INFO *info;
984 CK_SLOT_INFO *slotInfo;
985 CK_TOKEN_INFO *tokenInfo;
986 CK_SESSION_INFO *sessionInfo;
987 CK_ATTRIBUTE *attribute;
988 CK_MECHANISM *mechanism;
989 CK_MECHANISM_INFO *mechanismInfo;
990 CK_C_INITIALIZE_ARGS *initArgs;
991 CK_FUNCTION_LIST *functionList;
992 CK_RV ckrv = CKR_OK;
993 ConstType constType;
995 if (arg_number) {
996 printf("Arg %d: \n",arg_number);
998 if (ptr->arraySize > 1) {
999 Value element;
1000 int i;
1001 int elementSize = ptr->size/ptr->arraySize;
1002 char *dp = (char *)ptr->data;
1004 /* build a temporary Value to hold a single element */
1005 element.type = type;
1006 element.constType = ptr->constType;
1007 element.size = elementSize;
1008 element.filename = ptr->filename;
1009 element.reference = 1;
1010 element.arraySize = 1;
1011 for (i=0; i < ptr->arraySize; i++) {
1012 printf(" -----[ %d ] -----\n", i);
1013 element.data = (void *) &dp[i*elementSize];
1014 (void) printArg(&element, 0);
1016 return ckrv;
1018 if (ptr->data == NULL) {
1019 printf(" NULL ptr to a %s\n", valueString[type]);
1020 return ckrv;
1022 switch (type) {
1023 case ArgNone:
1024 printf(" None\n");
1025 break;
1026 case ArgULong:
1027 printf(" %lu (0x%lx)\n", *((CK_ULONG *)ptr->data),
1028 *((CK_ULONG *)ptr->data));
1029 if (ptr->constType != ConstNone) {
1030 printf(" ");
1031 printConst(*(CK_ULONG *)ptr->data,ptr->constType,1);
1033 break;
1034 case ArgVar:
1035 printf(" %s\n",(char *)ptr->data);
1036 break;
1037 case ArgUTF8:
1038 printf(" %s\n",(char *)ptr->data);
1039 break;
1040 case ArgChar:
1041 printDump(ptr->data,ptr->size);
1042 break;
1043 case ArgInfo:
1044 #define VERSION(x) (x).major, (x).minor
1045 info = (CK_INFO *)ptr->data;
1046 printf(" Cryptoki Version: %d.%02d\n",
1047 VERSION(info->cryptokiVersion));
1048 printf(" Manufacturer ID: ");
1049 printChars(info->manufacturerID,sizeof(info->manufacturerID));
1050 printFlags(" Flags: ", info->flags, ConstInfoFlags);
1051 printf(" Library Description: ");
1052 printChars(info->libraryDescription,sizeof(info->libraryDescription));
1053 printf(" Library Version: %d.%02d\n",
1054 VERSION(info->libraryVersion));
1055 break;
1056 case ArgSlotInfo:
1057 slotInfo = (CK_SLOT_INFO *)ptr->data;
1058 printf(" Slot Description: ");
1059 printChars(slotInfo->slotDescription,sizeof(slotInfo->slotDescription));
1060 printf(" Manufacturer ID: ");
1061 printChars(slotInfo->manufacturerID,sizeof(slotInfo->manufacturerID));
1062 printFlags(" Flags: ", slotInfo->flags, ConstSlotFlags);
1063 printf(" Hardware Version: %d.%02d\n",
1064 VERSION(slotInfo->hardwareVersion));
1065 printf(" Firmware Version: %d.%02d\n",
1066 VERSION(slotInfo->firmwareVersion));
1067 break;
1068 case ArgTokenInfo:
1069 tokenInfo = (CK_TOKEN_INFO *)ptr->data;
1070 printf(" Label: ");
1071 printChars(tokenInfo->label,sizeof(tokenInfo->label));
1072 printf(" Manufacturer ID: ");
1073 printChars(tokenInfo->manufacturerID,sizeof(tokenInfo->manufacturerID));
1074 printf(" Model: ");
1075 printChars(tokenInfo->model,sizeof(tokenInfo->model));
1076 printf(" Serial Number: ");
1077 printChars(tokenInfo->serialNumber,sizeof(tokenInfo->serialNumber));
1078 printFlags(" Flags: ", tokenInfo->flags, ConstTokenFlags);
1079 printf(" Max Session Count: ");
1080 printConst(tokenInfo->ulMaxSessionCount, ConstAvailableSizes, 1);
1081 printf(" Session Count: ");
1082 printConst(tokenInfo->ulSessionCount, ConstCurrentSize, 1);
1083 printf(" RW Session Count: ");
1084 printConst(tokenInfo->ulMaxRwSessionCount, ConstAvailableSizes, 1);
1085 printf(" Max Pin Length : ");
1086 printConst(tokenInfo->ulMaxPinLen, ConstCurrentSize, 1);
1087 printf(" Min Pin Length : ");
1088 printConst(tokenInfo->ulMinPinLen, ConstCurrentSize, 1);
1089 printf(" Total Public Memory: ");
1090 printConst(tokenInfo->ulTotalPublicMemory, ConstAvailableSizes, 1);
1091 printf(" Free Public Memory: ");
1092 printConst(tokenInfo->ulFreePublicMemory, ConstCurrentSize, 1);
1093 printf(" Total Private Memory: ");
1094 printConst(tokenInfo->ulTotalPrivateMemory, ConstAvailableSizes, 1);
1095 printf(" Free Private Memory: ");
1096 printConst(tokenInfo->ulFreePrivateMemory, ConstCurrentSize, 1);
1097 printf(" Hardware Version: %d.%02d\n",
1098 VERSION(tokenInfo->hardwareVersion));
1099 printf(" Firmware Version: %d.%02d\n",
1100 VERSION(tokenInfo->firmwareVersion));
1101 printf(" UTC Time: ");
1102 printChars(tokenInfo->utcTime,sizeof(tokenInfo->utcTime));
1103 break;
1104 case ArgSessionInfo:
1105 sessionInfo = (CK_SESSION_INFO *)ptr->data;
1106 printf(" SlotID: 0x%08lx\n", sessionInfo->slotID);
1107 printf(" State: ");
1108 printConst(sessionInfo->state, ConstSessionState, 1);
1109 printFlags(" Flags: ", sessionInfo->flags, ConstSessionFlags);
1110 printf(" Device error: %lu 0x%08lx\n",sessionInfo->ulDeviceError,
1111 sessionInfo->ulDeviceError);
1112 break;
1113 case ArgAttribute:
1114 attribute = (CK_ATTRIBUTE *)ptr->data;
1115 printf(" Attribute Type: ");
1116 printConst(attribute->type, ConstAttribute, 1);
1117 printf(" Attribute Data: ");
1118 if (attribute->pValue == NULL) {
1119 printf("NULL\n");
1120 printf("Attribute Len: %lu\n",attribute->ulValueLen);
1121 } else {
1122 constType = getConstFromAttribute(attribute->type);
1123 if (constType != ConstNone) {
1124 CK_ULONG value = (constType == ConstBool) ?
1125 *(CK_BBOOL *)attribute->pValue :
1126 *(CK_ULONG *)attribute->pValue;
1127 printConst(value, constType, 1);
1128 } else {
1129 printf("\n");
1130 printDump(attribute->pValue, attribute->ulValueLen);
1133 break;
1134 case ArgMechanism:
1135 mechanism = (CK_MECHANISM *)ptr->data;
1136 printf(" Mechanism Type: ");
1137 printConst(mechanism->mechanism, ConstMechanism, 1);
1138 printf(" Mechanism Data:\n");
1139 printDump(mechanism->pParameter, mechanism->ulParameterLen);
1140 break;
1141 case ArgMechanismInfo:
1142 mechanismInfo = (CK_MECHANISM_INFO *)ptr->data;
1143 printf(" Minimum Key Size: %ld\n",mechanismInfo->ulMinKeySize);
1144 printf(" Maximum Key Size: %ld\n",mechanismInfo->ulMaxKeySize);
1145 printFlags(" Flags: ", mechanismInfo->flags, ConstMechanismFlags);
1146 break;
1147 case ArgInitializeArgs:
1148 initArgs = (CK_C_INITIALIZE_ARGS *)ptr->data;
1149 printFlags(" Flags: ", initArgs->flags, ConstInitializeFlags);
1150 if (initArgs->LibraryParameters) {
1151 printf("Params: %s\n",initArgs->LibraryParameters);
1153 case ArgFunctionList:
1154 functionList = (CK_FUNCTION_LIST *)ptr->data;
1155 printf(" Version: %d.%02d\n", VERSION(functionList->version));
1156 #ifdef notdef
1157 #undef CK_NEED_ARG_LIST
1158 #define CK_PKCS11_FUNCTION_INFO(func) \
1159 printf(" %s: 0x%08lx\n", #func, (unsigned long) functionList->func );
1160 #include "pkcs11f.h"
1161 #undef CK_NEED_ARG_LIST
1162 #undef CK_PKCS11_FUNCTION_INFO
1163 #endif
1164 default:
1165 ckrv = CKR_ARGUMENTS_BAD;
1166 break;
1169 return ckrv;
1174 * Feeling ambitious? turn this whole thing into lexx yacc parser
1175 * with full expressions.
1177 Value **
1178 parseArgs(int index, const char * bp)
1180 const Commands *cp = &commands[index];
1181 int size = strlen(cp->fname);
1182 int i;
1183 CK_ULONG value;
1184 char vname[512];
1185 Value **argList,*possible;
1186 ConstType constType;
1189 * skip pass the command
1191 if ((cp->fname[0] == 'C') && (cp->fname[1] == '_') && (bp[1] != '_')) {
1192 size -= 2;
1194 bp += size;
1197 * Initialize our argument list
1199 argList = (Value **)malloc(sizeof(Value*)*MAX_ARGS);
1200 for (i=0; i < MAX_ARGS; i++) { argList[i] = NULL; }
1203 * Walk the argument list parsing it...
1205 for (i=0 ;i < MAX_ARGS; i++) {
1206 ArgType type = cp->args[i] & ArgMask;
1207 int error;
1209 /* strip blanks */
1210 bp = strip(bp);
1212 /* if we hit ArgNone, we've nabbed all the arguments we need */
1213 if (type == ArgNone) {
1214 break;
1217 /* if we run out of space in the line, we weren't given enough
1218 * arguments... */
1219 if (*bp == '\0') {
1220 /* we're into optional arguments, ok to quit now */
1221 if (cp->args[i] & ArgOpt) {
1222 break;
1224 fprintf(stderr,"%s: only %d args found,\n",cp->fname,i);
1225 parseFree(argList);
1226 return NULL;
1229 /* collect all the rest of the command line and send
1230 * it as a single argument */
1231 if (cp->args[i] & ArgFull) {
1232 int size = strlen(bp)+1;
1233 argList[i] = NewValue(type, size);
1234 memcpy(argList[i]->data, bp, size);
1235 break;
1239 * look up the argument in our variable list first... only
1240 * exception is the new argument type for set...
1242 error = 0;
1243 if ((cp->args[i] != (ArgVar|ArgNew)) &&
1244 (possible = varLookup(bp,vname,sizeof(vname),&error))) {
1245 /* ints are only compatible with other ints... all other types
1246 * are interchangeable... */
1247 if (type != ArgVar) { /* ArgVar's match anyone */
1248 if ((type == ArgULong) ^
1249 ((possible->type & ArgMask) == ArgULong)) {
1250 fprintf(stderr,"%s: Arg %d incompatible type with <%s>\n",
1251 cp->fname,i+1,vname);
1252 argFree(possible);
1253 parseFree(argList);
1254 return NULL;
1257 * ... that is as long as they are big enough...
1259 if (ArgSize(type) > possible->size) {
1260 fprintf(stderr,
1261 "%s: Arg %d %s is too small (%d bytes needs to be %d bytes)\n",
1262 cp->fname,i+1,vname,possible->size,ArgSize(type));
1263 argFree(possible);
1264 parseFree(argList);
1265 return NULL;
1269 /* everything looks kosher here, use it */
1270 argList[i] = possible;
1272 bp = readChars(bp,vname,sizeof(vname));
1273 if (cp->args[i] & ArgOut) {
1274 possible->type |= ArgOut;
1276 continue;
1279 if (error == 1) {
1280 parseFree(argList);
1281 return NULL;
1284 /* create space for our argument */
1285 argList[i] = NewValue(type, 1);
1287 if ((PL_strncasecmp(bp, "null", 4) == 0) && ((bp[4] == 0)
1288 || (bp[4] == ' ') || (bp[4] =='\t') || (bp[4] =='\n'))) {
1289 if (cp->args[i] == ArgULong) {
1290 fprintf(stderr, "%s: Arg %d CK_ULONG can't be NULL\n",
1291 cp->fname,i+1);
1292 parseFree(argList);
1293 return NULL;
1295 argFreeData(argList[i]);
1296 argList[i]->data = NULL;
1297 argList[i]->size = 0;
1298 bp += 4;
1299 if (*bp) bp++;
1300 continue;
1303 /* if we're an output variable, we need to add it */
1304 if (cp->args[i] & ArgOut) {
1305 if (PL_strncasecmp(bp,"file(",5) == 0 /* ) */ ) {
1306 char filename[512];
1307 bp = readChars(bp+5,filename,sizeof(filename));
1308 size = PL_strlen(filename);
1309 if ((size > 0) && (/* ( */filename[size-1] == ')')) {
1310 filename[size-1] = 0;
1312 filename[size] = 0;
1313 argList[i]->filename = (char *)malloc(size+1);
1315 PL_strcpy(argList[i]->filename,filename);
1317 argList[i]->type |= ArgOut|ArgFile;
1318 break;
1320 bp = AddVariable(bp,&argList[i]);
1321 argList[i]->type |= ArgOut;
1322 continue;
1325 if (PL_strncasecmp(bp, "file(", 5) == 0 /* ) */ ) {
1326 char filename[512];
1328 bp = readChars(bp+5,filename,sizeof(filename));
1329 size = PL_strlen(filename);
1330 if ((size > 0) && ( /* ( */ filename[size-1] == ')')) {
1331 filename[size-1] = 0;
1334 if (restore(filename,argList[i]) != CKR_OK) {
1335 parseFree(argList);
1336 return NULL;
1338 continue;
1341 switch (type) {
1342 case ArgULong:
1343 bp = constLookup(bp, &value, &constType);
1344 *(int *)argList[i]->data = value;
1345 argList[i]->constType = constType;
1346 break;
1347 case ArgVar:
1348 argFreeData(argList[i]);
1349 size = getEnd(bp)+1;
1350 argList[i]->data = (void *)malloc(size);
1351 argList[i]->size = size;
1352 /* fall through */
1353 case ArgInfo:
1354 case ArgSlotInfo:
1355 case ArgTokenInfo:
1356 case ArgSessionInfo:
1357 case ArgAttribute:
1358 case ArgMechanism:
1359 case ArgMechanismInfo:
1360 case ArgInitializeArgs:
1361 case ArgUTF8:
1362 case ArgChar:
1363 bp = readChars(bp,(char *)argList[i]->data,argList[i]->size);
1364 case ArgNone:
1365 default:
1366 break;
1370 return argList;
1373 /* lookup the command in the array */
1375 lookup(const char *buf)
1377 int size,i;
1378 int buflen;
1380 buflen = PL_strlen(buf);
1382 for ( i = 0; i < commandCount; i++) {
1383 size = PL_strlen(commands[i].fname);
1385 if (size <= buflen) {
1386 if (PL_strncasecmp(buf,commands[i].fname,size) == 0) {
1387 return i;
1390 if (size-2 <= buflen) {
1391 if (commands[i].fname[0] == 'C' && commands[i].fname[1] == '_' &&
1392 (PL_strncasecmp(buf,&commands[i].fname[2],size-2) == 0)) {
1393 return i;
1397 fprintf(stderr,"Can't find command %s\n",buf);
1398 return -1;
1401 void
1402 putOutput(Value **ptr)
1404 int i;
1406 for (i=0; i < MAX_ARGS; i++) {
1407 ArgType type;
1409 if (ptr[i] == NULL) break;
1411 type = ptr[i]->type;
1413 ptr[i]->type &= ~ArgOut;
1414 if (type == ArgNone) {
1415 break;
1417 if (type & ArgOut) {
1418 (void) printArg(ptr[i],i+1);
1420 if (type & ArgFile) {
1421 save(ptr[i]->filename,ptr[i]);
1422 free(ptr[i]->filename);
1423 ptr[i]->filename= NULL; /* paranoia */
1428 CK_RV
1429 unloadModule(Module *module)
1431 char *disableUnload = NULL;
1433 disableUnload = PR_GetEnv("NSS_DISABLE_UNLOAD");
1435 if (module->library && !disableUnload) {
1436 PR_UnloadLibrary(module->library);
1439 module->library = NULL;
1440 module->functionList = NULL;
1442 return CKR_OK;
1445 CK_RV
1446 loadModule(Module *module, char *library)
1448 PRLibrary *newLibrary;
1449 CK_C_GetFunctionList getFunctionList;
1450 CK_FUNCTION_LIST *functionList;
1451 CK_RV ckrv;
1453 newLibrary = PR_LoadLibrary(library);
1454 if (!newLibrary) {
1455 fprintf(stderr,"Couldn't load library %s\n",library);
1456 return CKR_FUNCTION_FAILED;
1458 getFunctionList = (CK_C_GetFunctionList)
1459 PR_FindSymbol(newLibrary,"C_GetFunctionList");
1460 if (!getFunctionList) {
1461 fprintf(stderr,"Couldn't find \"C_GetFunctionList\" in %s\n",library);
1462 return CKR_FUNCTION_FAILED;
1465 ckrv = (*getFunctionList)(&functionList);
1466 if (ckrv != CKR_OK) {
1467 return ckrv;
1470 if (module->library) {
1471 PR_UnloadLibrary(module->library);
1474 module->library = newLibrary;
1475 module->functionList = functionList;
1477 return CKR_OK;
1480 static void
1481 printHelp(int index, int full)
1483 int j;
1484 printf(" %s", commands[index].fname);
1485 for (j=0; j < MAX_ARGS; j++) {
1486 ArgType type = commands[index].args[j] & ArgMask;
1487 if (type == ArgNone) {
1488 break;
1490 printf(" %s", valueString[type]);
1492 printf("\n");
1493 printf(" %s\n",commands[index].helpString);
1496 /* add Topical help here ! */
1497 static CK_RV
1498 printTopicHelp(char *topic)
1500 int size,i;
1501 int topicLen;
1503 topicLen = PL_strlen(topic);
1505 for ( i = 0; i < topicCount; i++) {
1506 size = PL_strlen(topics[i].name);
1508 if (size <= topicLen) {
1509 if (PL_strncasecmp(topic,topics[i].name,size) == 0) {
1510 break;
1515 if (i == topicCount) {
1516 fprintf(stderr,"Can't find topic '%s'\n", topic);
1517 return CKR_DATA_INVALID;
1520 printf(" %s", topic);
1521 printf("\n");
1522 printf(" %s\n",topics[i].helpString);
1523 return CKR_OK;
1526 static CK_RV
1527 printGeneralHelp(void)
1529 int i;
1530 printf(" To get help on commands, select from the list below:");
1531 for ( i = 0; i < commandCount; i++) {
1532 if (i % 5 == 0) printf("\n");
1533 printf("%s,", commands[i].fname);
1535 printf("\n");
1536 /* print help topics */
1537 printf(" To get help on a topic, select from the list below:");
1538 for ( i = 0; i < topicCount; i++) {
1539 if (i % 5 == 0) printf("\n");
1540 printf("%s,", topics[i].name);
1542 printf("\n");
1543 return CKR_OK;
1546 static CK_RV
1547 quitIf(CK_ULONG a, const char *cmp, CK_ULONG b)
1549 if (strcmp(cmp, "<") == 0) {
1550 return (a < b) ? CKR_QUIT : CKR_OK;
1551 } else if (strcmp(cmp, ">") == 0) {
1552 return (a > b) ? CKR_QUIT : CKR_OK;
1553 } else if (strcmp(cmp, "<=") == 0) {
1554 return (a <= b) ? CKR_QUIT : CKR_OK;
1555 } else if (strcmp(cmp, ">=") == 0) {
1556 return (a >= b) ? CKR_QUIT : CKR_OK;
1557 } else if (strcmp(cmp, "=") == 0) {
1558 return (a == b) ? CKR_QUIT : CKR_OK;
1559 } else if (strcmp(cmp, "!=") == 0) {
1560 return (a != b) ? CKR_QUIT : CKR_OK;
1562 printf("Unkown integer comparator: '%s'\n", cmp);
1563 return CKR_ARGUMENTS_BAD;
1566 static CK_RV
1567 quitIfString(const char *a, const char *cmp, const char *b)
1570 if (strcmp(cmp, "=") == 0) {
1571 return (strcmp(a,b) == 0) ? CKR_QUIT : CKR_OK;
1572 } else if (strcmp(cmp, "!=") == 0) {
1573 return (strcmp(a,b) != 0) ? CKR_QUIT : CKR_OK;
1575 printf("Unkown string comparator: '%s'\n", cmp);
1576 return CKR_ARGUMENTS_BAD;
1579 CK_RV run(const char *);
1580 CK_RV timeCommand(const char *);
1581 CK_RV loop(const char *filename, const char *var,
1582 CK_ULONG start, CK_ULONG end, CK_ULONG step) ;
1585 * Actually dispatch the function... Bad things happen
1586 * if these don't match the commands array.
1588 CK_RV
1589 do_func(int index, Value **a)
1591 int value, helpIndex;
1592 static Module module = { NULL, NULL} ;
1593 CK_FUNCTION_LIST *func = module.functionList;
1595 switch (commands[index].fType) {
1596 case F_C_Initialize:
1597 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1598 return func->C_Initialize((void *)a[0]->data);
1599 case F_C_Finalize:
1600 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1601 return func->C_Finalize((void *)a[0]->data);
1602 case F_C_GetInfo:
1603 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1604 return func->C_GetInfo((CK_INFO *)a[0]->data);
1605 case F_C_GetFunctionList:
1606 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1607 return func->C_GetFunctionList((CK_FUNCTION_LIST **)a[0]->data);
1608 case F_C_GetSlotList:
1609 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1610 return func->C_GetSlotList((CK_BBOOL)*(CK_ULONG *)a[0]->data,
1611 (CK_SLOT_ID *)a[1]->data,
1612 (CK_LONG *)a[2]->data);
1613 case F_C_GetSlotInfo:
1614 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1615 return func->C_GetSlotInfo(*(CK_ULONG *)a[0]->data,
1616 (CK_SLOT_INFO *)a[1]->data);
1617 case F_C_GetTokenInfo:
1618 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1619 return func->C_GetTokenInfo(*(CK_ULONG *)a[0]->data,
1620 (CK_TOKEN_INFO *)a[1]->data);
1621 case F_C_GetMechanismList:
1622 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1623 if (a[1]->data) {
1624 a[1]->constType = ConstMechanism;
1626 return func->C_GetMechanismList(*(CK_ULONG *)a[0]->data,
1627 (CK_MECHANISM_TYPE*)a[1]->data,
1628 (CK_ULONG *)a[2]->data);
1629 case F_C_GetMechanismInfo:
1630 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1631 return func->C_GetMechanismInfo(*(CK_ULONG *)a[0]->data,
1632 *(CK_ULONG *)a[1]->data,
1633 (CK_MECHANISM_INFO *)a[2]->data);
1634 case F_C_InitToken:
1635 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1636 return func->C_InitToken(*(CK_ULONG *)a[0]->data,
1637 (CK_CHAR *)a[1]->data,
1638 *(CK_ULONG *)a[2]->data,
1639 (CK_CHAR *)a[3]->data);
1640 case F_C_InitPIN:
1641 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1642 return func->C_InitPIN(*(CK_ULONG *)a[0]->data,
1643 (CK_CHAR *)a[1]->data,
1644 *(CK_ULONG *)a[2]->data);
1645 case F_C_SetPIN:
1646 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1647 return func->C_SetPIN(*(CK_ULONG *)a[0]->data,
1648 (CK_CHAR *)a[1]->data,
1649 *(CK_ULONG *)a[2]->data,
1650 (CK_CHAR *)a[3]->data,
1651 *(CK_ULONG *)a[4]->data);
1652 case F_C_OpenSession:
1653 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1654 return func->C_OpenSession(*(CK_ULONG *)a[0]->data,
1655 *(CK_ULONG *)a[1]->data,
1656 (void *)NULL,
1657 (CK_NOTIFY) NULL,
1658 (CK_ULONG *)a[2]->data);
1659 case F_C_CloseSession:
1660 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1661 return func->C_CloseSession(*(CK_ULONG *)a[0]->data);
1662 case F_C_CloseAllSessions:
1663 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1664 return func->C_CloseAllSessions(*(CK_ULONG *)a[0]->data);
1665 case F_C_GetSessionInfo:
1666 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1667 return func->C_GetSessionInfo(*(CK_ULONG *)a[0]->data,
1668 (CK_SESSION_INFO *)a[1]->data);
1669 case F_C_GetOperationState:
1670 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1671 return func->C_GetOperationState(*(CK_ULONG *)a[0]->data,
1672 (CK_BYTE *)a[1]->data,
1673 (CK_ULONG *)a[2]->data);
1674 case F_C_SetOperationState:
1675 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1676 return func->C_SetOperationState(*(CK_ULONG *)a[0]->data,
1677 (CK_CHAR *)a[1]->data,
1678 *(CK_ULONG *)a[2]->data,
1679 *(CK_ULONG *)a[3]->data,
1680 *(CK_ULONG *)a[4]->data);
1681 case F_C_Login:
1682 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1683 return func->C_Login(*(CK_ULONG *)a[0]->data,
1684 *(CK_ULONG *)a[1]->data,
1685 (CK_CHAR *)a[2]->data,
1686 *(CK_ULONG *)a[3]->data);
1687 case F_C_Logout:
1688 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1689 return func->C_Logout(*(CK_ULONG *)a[0]->data);
1690 case F_C_CreateObject:
1691 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1692 return func->C_CreateObject(*(CK_ULONG *)a[0]->data,
1693 (CK_ATTRIBUTE *)a[1]->data,
1694 *(CK_ULONG *)a[2]->data,
1695 (CK_ULONG *)a[3]->data);
1696 case F_C_CopyObject:
1697 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1698 return func->C_CopyObject(*(CK_ULONG *)a[0]->data,
1699 *(CK_ULONG *)a[0]->data,
1700 (CK_ATTRIBUTE *)a[1]->data,
1701 *(CK_ULONG *)a[2]->data,
1702 (CK_ULONG *)a[3]->data);
1703 case F_C_DestroyObject:
1704 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1705 return func->C_DestroyObject(*(CK_ULONG *)a[0]->data,
1706 *(CK_ULONG *)a[1]->data);
1707 case F_C_GetObjectSize:
1708 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1709 return func->C_GetObjectSize(*(CK_ULONG *)a[0]->data,
1710 *(CK_ULONG *)a[1]->data,
1711 (CK_ULONG *)a[2]->data);
1712 case F_C_GetAttributeValue:
1713 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1714 return func->C_GetAttributeValue(*(CK_ULONG *)a[0]->data,
1715 *(CK_ULONG *)a[1]->data,
1716 (CK_ATTRIBUTE *)a[2]->data,
1717 *(CK_ULONG *)a[3]->data);
1718 case F_C_SetAttributeValue:
1719 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1720 return func->C_SetAttributeValue(*(CK_ULONG *)a[0]->data,
1721 *(CK_ULONG *)a[1]->data,
1722 (CK_ATTRIBUTE *)a[2]->data,
1723 *(CK_ULONG *)a[3]->data);
1724 case F_C_FindObjectsInit:
1725 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1726 return func->C_FindObjectsInit(*(CK_ULONG *)a[0]->data,
1727 (CK_ATTRIBUTE *)a[1]->data,
1728 *(CK_ULONG *)a[2]->data);
1729 case F_C_FindObjects:
1730 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1731 return func->C_FindObjects(*(CK_ULONG *)a[0]->data,
1732 (CK_ULONG *)a[1]->data,
1733 *(CK_ULONG *)a[2]->data,
1734 (CK_ULONG *)a[3]->data);
1735 case F_C_FindObjectsFinal:
1736 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1737 return func->C_FindObjectsFinal(*(CK_ULONG *)a[0]->data);
1738 case F_C_EncryptInit:
1739 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1740 return func->C_EncryptInit(*(CK_ULONG *)a[0]->data,
1741 (CK_MECHANISM *)a[1]->data,
1742 *(CK_ULONG *)a[2]->data);
1743 case F_C_Encrypt:
1744 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1745 return func->C_Encrypt(*(CK_ULONG *)a[0]->data,
1746 (CK_CHAR *)a[1]->data,
1747 *(CK_ULONG *)a[2]->data,
1748 (CK_CHAR *)a[3]->data,
1749 (CK_ULONG *)a[4]->data);
1750 case F_C_EncryptUpdate:
1751 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1752 return func->C_EncryptUpdate(*(CK_ULONG *)a[0]->data,
1753 (CK_CHAR *)a[1]->data,
1754 *(CK_ULONG *)a[2]->data,
1755 (CK_CHAR *)a[3]->data,
1756 (CK_ULONG *)a[4]->data);
1757 case F_C_EncryptFinal:
1758 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1759 return func->C_EncryptFinal(*(CK_ULONG *)a[0]->data,
1760 (CK_CHAR *)a[1]->data,
1761 (CK_ULONG *)a[2]->data);
1762 case F_C_DecryptInit:
1763 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1764 return func->C_DecryptInit(*(CK_ULONG *)a[0]->data,
1765 (CK_MECHANISM *)a[1]->data,
1766 *(CK_ULONG *)a[2]->data);
1767 case F_C_Decrypt:
1768 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1769 return func->C_Decrypt(*(CK_ULONG *)a[0]->data,
1770 (CK_CHAR *)a[1]->data,
1771 *(CK_ULONG *)a[2]->data,
1772 (CK_CHAR *)a[3]->data,
1773 (CK_ULONG *)a[4]->data);
1774 case F_C_DecryptUpdate:
1775 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1776 return func->C_DecryptUpdate(*(CK_ULONG *)a[0]->data,
1777 (CK_CHAR *)a[1]->data,
1778 *(CK_ULONG *)a[2]->data,
1779 (CK_CHAR *)a[3]->data,
1780 (CK_ULONG *)a[4]->data);
1781 case F_C_DecryptFinal:
1782 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1783 return func->C_DecryptFinal(*(CK_ULONG *)a[0]->data,
1784 (CK_CHAR *)a[1]->data,
1785 (CK_ULONG *)a[2]->data);
1786 case F_C_DigestInit:
1787 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1788 return func->C_DigestInit(*(CK_ULONG *)a[0]->data,
1789 (CK_MECHANISM *)a[1]->data);
1790 case F_C_Digest:
1791 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1792 return func->C_Digest(*(CK_ULONG *)a[0]->data,
1793 (CK_CHAR *)a[1]->data,
1794 *(CK_ULONG *)a[2]->data,
1795 (CK_CHAR *)a[3]->data,
1796 (CK_ULONG *)a[4]->data);
1797 case F_C_DigestUpdate:
1798 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1799 return func->C_DigestUpdate(*(CK_ULONG *)a[0]->data,
1800 (CK_CHAR *)a[1]->data,
1801 *(CK_ULONG *)a[2]->data);
1802 case F_C_DigestKey:
1803 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1804 return func->C_DigestKey(*(CK_ULONG *)a[0]->data,
1805 *(CK_ULONG *)a[1]->data);
1806 case F_C_DigestFinal:
1807 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1808 return func->C_DigestFinal(*(CK_ULONG *)a[0]->data,
1809 (CK_CHAR *)a[1]->data,
1810 (CK_ULONG *)a[2]->data);
1811 case F_C_SignInit:
1812 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1813 return func->C_SignInit(*(CK_ULONG *)a[0]->data,
1814 (CK_MECHANISM *)a[1]->data,
1815 *(CK_ULONG *)a[2]->data);
1816 case F_C_Sign:
1817 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1818 return func->C_Sign(*(CK_ULONG *)a[0]->data,
1819 (CK_CHAR *)a[1]->data,
1820 *(CK_ULONG *)a[2]->data,
1821 (CK_CHAR *)a[3]->data,
1822 (CK_ULONG *)a[4]->data);
1823 case F_C_SignUpdate:
1824 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1825 return func->C_SignUpdate(*(CK_ULONG *)a[0]->data,
1826 (CK_CHAR *)a[1]->data,
1827 *(CK_ULONG *)a[2]->data);
1828 case F_C_SignFinal:
1829 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1830 return func->C_SignFinal(*(CK_ULONG *)a[0]->data,
1831 (CK_CHAR *)a[1]->data,
1832 (CK_ULONG *)a[2]->data);
1834 case F_C_SignRecoverInit:
1835 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1836 return func->C_SignRecoverInit(*(CK_ULONG *)a[0]->data,
1837 (CK_MECHANISM *)a[1]->data,
1838 *(CK_ULONG *)a[2]->data);
1839 case F_C_SignRecover:
1840 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1841 return func->C_SignRecover(*(CK_ULONG *)a[0]->data,
1842 (CK_CHAR *)a[1]->data,
1843 *(CK_ULONG *)a[2]->data,
1844 (CK_CHAR *)a[3]->data,
1845 (CK_ULONG *)a[4]->data);
1846 case F_C_VerifyInit:
1847 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1848 return func->C_VerifyInit(*(CK_ULONG *)a[0]->data,
1849 (CK_MECHANISM *)a[1]->data,
1850 *(CK_ULONG *)a[2]->data);
1851 case F_C_Verify:
1852 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1853 return func->C_Verify(*(CK_ULONG *)a[0]->data,
1854 (CK_CHAR *)a[1]->data,
1855 *(CK_ULONG *)a[2]->data,
1856 (CK_CHAR *)a[3]->data,
1857 *(CK_ULONG *)a[4]->data);
1858 case F_C_VerifyUpdate:
1859 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1860 return func->C_VerifyUpdate(*(CK_ULONG *)a[0]->data,
1861 (CK_CHAR *)a[1]->data,
1862 *(CK_ULONG *)a[2]->data);
1863 case F_C_VerifyFinal:
1864 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1865 return func->C_VerifyFinal(*(CK_ULONG *)a[0]->data,
1866 (CK_CHAR *)a[1]->data,
1867 *(CK_ULONG *)a[2]->data);
1869 case F_C_VerifyRecoverInit:
1870 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1871 return func->C_VerifyRecoverInit(*(CK_ULONG *)a[0]->data,
1872 (CK_MECHANISM *)a[1]->data,
1873 *(CK_ULONG *)a[2]->data);
1874 case F_C_VerifyRecover:
1875 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1876 return func->C_VerifyRecover(*(CK_ULONG *)a[0]->data,
1877 (CK_CHAR *)a[1]->data,
1878 *(CK_ULONG *)a[2]->data,
1879 (CK_CHAR *)a[3]->data,
1880 (CK_ULONG *)a[4]->data);
1881 case F_C_DigestEncryptUpdate:
1882 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1883 return func->C_DigestEncryptUpdate(*(CK_ULONG *)a[0]->data,
1884 (CK_CHAR *)a[1]->data,
1885 *(CK_ULONG *)a[2]->data,
1886 (CK_CHAR *)a[3]->data,
1887 (CK_ULONG *)a[4]->data);
1888 case F_C_DecryptDigestUpdate:
1889 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1890 return func->C_DecryptDigestUpdate(*(CK_ULONG *)a[0]->data,
1891 (CK_CHAR *)a[1]->data,
1892 *(CK_ULONG *)a[2]->data,
1893 (CK_CHAR *)a[3]->data,
1894 (CK_ULONG *)a[4]->data);
1895 case F_C_SignEncryptUpdate:
1896 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1897 return func->C_SignEncryptUpdate(*(CK_ULONG *)a[0]->data,
1898 (CK_CHAR *)a[1]->data,
1899 *(CK_ULONG *)a[2]->data,
1900 (CK_CHAR *)a[3]->data,
1901 (CK_ULONG *)a[4]->data);
1902 case F_C_DecryptVerifyUpdate:
1903 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1904 return func->C_DecryptVerifyUpdate(*(CK_ULONG *)a[0]->data,
1905 (CK_CHAR *)a[1]->data,
1906 *(CK_ULONG *)a[2]->data,
1907 (CK_CHAR *)a[3]->data,
1908 (CK_ULONG *)a[4]->data);
1909 case F_C_GenerateKey:
1910 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1911 return func->C_GenerateKey(*(CK_ULONG *)a[0]->data,
1912 (CK_MECHANISM *)a[1]->data,
1913 (CK_ATTRIBUTE *)a[2]->data,
1914 *(CK_ULONG *)a[3]->data,
1915 (CK_ULONG *)a[4]->data);
1916 case F_C_GenerateKeyPair:
1917 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1918 return func->C_GenerateKeyPair(*(CK_ULONG *)a[0]->data,
1919 (CK_MECHANISM *)a[1]->data,
1920 (CK_ATTRIBUTE *)a[2]->data,
1921 *(CK_ULONG *)a[3]->data,
1922 (CK_ATTRIBUTE *)a[4]->data,
1923 *(CK_ULONG *)a[5]->data,
1924 (CK_ULONG *)a[6]->data,
1925 (CK_ULONG *)a[7]->data);
1926 case F_C_WrapKey:
1927 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1928 return func->C_WrapKey(*(CK_ULONG *)a[0]->data,
1929 (CK_MECHANISM *)a[1]->data,
1930 *(CK_ULONG *)a[2]->data,
1931 *(CK_ULONG *)a[3]->data,
1932 (CK_CHAR *)a[5]->data,
1933 (CK_ULONG *)a[6]->data);
1934 case F_C_UnwrapKey:
1935 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1936 return func->C_UnwrapKey(*(CK_ULONG *)a[0]->data,
1937 (CK_MECHANISM *)a[1]->data,
1938 *(CK_ULONG *)a[2]->data,
1939 (CK_CHAR *)a[3]->data,
1940 *(CK_ULONG *)a[4]->data,
1941 (CK_ATTRIBUTE *)a[5]->data,
1942 *(CK_ULONG *)a[6]->data,
1943 (CK_ULONG *)a[7]->data);
1944 case F_C_DeriveKey:
1945 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1946 return func->C_DeriveKey (*(CK_ULONG *)a[0]->data,
1947 (CK_MECHANISM *)a[1]->data,
1948 *(CK_ULONG *)a[2]->data,
1949 (CK_ATTRIBUTE *)a[3]->data,
1950 *(CK_ULONG *)a[4]->data,
1951 (CK_ULONG *)a[5]->data);
1952 case F_C_SeedRandom:
1953 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1954 return func->C_SeedRandom(*(CK_ULONG *)a[0]->data,
1955 (CK_CHAR *)a[1]->data,
1956 *(CK_ULONG *)a[2]->data);
1957 case F_C_GenerateRandom:
1958 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1959 return func->C_GenerateRandom(*(CK_ULONG *)a[0]->data,
1960 (CK_CHAR *)a[1]->data,
1961 *(CK_ULONG *)a[2]->data);
1962 case F_C_GetFunctionStatus:
1963 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1964 return func->C_GetFunctionStatus(*(CK_ULONG *)a[0]->data);
1965 case F_C_CancelFunction:
1966 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1967 return func->C_CancelFunction(*(CK_ULONG *)a[0]->data);
1968 case F_C_WaitForSlotEvent:
1969 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1970 return func->C_WaitForSlotEvent(*(CK_ULONG *)a[0]->data,
1971 (CK_ULONG *)a[1]->data,
1972 (void *)a[2]->data);
1973 /* set a variable */
1974 case F_SetVar:
1975 case F_SetStringVar:
1976 (void) DeleteVariable(a[0]->data);
1977 (void) AddVariable(a[0]->data,&a[1]);
1978 return CKR_OK;
1979 /* print a value */
1980 case F_Print:
1981 return printArg(a[0],0);
1982 case F_SaveVar:
1983 return save(a[0]->data,a[1]);
1984 case F_RestoreVar:
1985 return restore(a[0]->data,a[1]);
1986 case F_Delete:
1987 return DeleteVariable(a[0]->data);
1988 case F_Increment:
1989 return increment(a[0], *(CK_ULONG *)a[1]->data);
1990 case F_Decrement:
1991 return decrement(a[0], *(CK_ULONG *)a[1]->data);
1992 case F_List:
1993 return list();
1994 case F_Run:
1995 return run(a[0]->data);
1996 case F_Time:
1997 return timeCommand(a[0]->data);
1998 case F_Load:
1999 return loadModule(&module,a[0]->data);
2000 case F_Unload:
2001 return unloadModule(&module);
2002 case F_NewArray:
2003 (void) DeleteVariable(a[0]->data);
2004 return ArrayVariable(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data);
2005 case F_NewTemplate:
2006 (void) DeleteVariable(a[0]->data);
2007 return ArrayTemplate(a[0]->data,a[1]->data);
2008 case F_BuildTemplate:
2009 return BuildTemplate(a[0]);
2010 case F_SetTemplate:
2011 return SetTemplate(a[0],
2012 *(CK_ULONG *)a[1]->data,
2013 *(CK_ULONG *)a[2]->data);
2014 case F_NewMechanism:
2015 (void) DeleteVariable(a[0]->data);
2016 return NewMechanism(a[0]->data,*(CK_ULONG *)a[1]->data);
2017 case F_NewInitializeArgs:
2018 (void) DeleteVariable(a[0]->data);
2019 return NewInitializeArgs(a[0]->data,*(CK_ULONG *)a[1]->data,a[2]->data);
2020 case F_System:
2021 value = *(int *)a[0]->data;
2022 if (value & 0x80000000) {
2023 systemFlags &= ~value;
2024 } else {
2025 systemFlags |= value;
2027 return CKR_OK;
2028 case F_Loop:
2029 return loop(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data,
2030 *(CK_ULONG *)a[3]->data,*(CK_ULONG *)a[4]->data);
2031 case F_Help:
2032 if (a[0]) {
2033 helpIndex = lookup(a[0]->data);
2034 if (helpIndex < 0) {
2035 return printTopicHelp(a[0]->data);
2037 printHelp(helpIndex, 1);
2038 return CKR_OK;
2040 return printGeneralHelp();
2041 case F_QuitIfString:
2042 return quitIfString(a[0]->data,a[1]->data,a[2]->data);
2043 case F_QuitIf:
2044 return quitIf(*(CK_ULONG *)a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data);
2045 case F_Quit:
2046 return CKR_QUIT;
2047 default:
2048 fprintf(stderr,
2049 "Function %s not yet supported\n",commands[index].fname );
2050 return CKR_OK;
2052 /* Not Reached */
2053 return CKR_OK;
2056 CK_RV
2057 processCommand(const char * buf)
2059 CK_RV error = CKR_OK;
2060 int index;
2061 const char *bp;
2062 Value **arglist;
2064 bp = strip(buf);
2065 /* allow comments and blank lines in scripts */
2066 if ((*bp == '#') || (*bp == 0) || (*bp == '\n')){
2067 return CKR_OK;
2070 index = lookup(bp);
2072 if (index < 0) {
2073 return CKR_OK;
2076 arglist = parseArgs(index,bp);
2077 if (arglist == NULL) {
2078 return CKR_OK;
2081 error = do_func(index,arglist);
2082 if (error == CKR_OK) {
2083 putOutput(arglist);
2084 } else if (error != CKR_QUIT) {
2085 printf(">> Error : ");
2086 printConst(error, ConstResult, 1);
2089 parseFree(arglist);
2090 return error;
2093 CK_RV
2094 timeCommand(const char *command)
2096 CK_RV ckrv;
2097 PRIntervalTime startTime = PR_IntervalNow();
2098 PRIntervalTime endTime;
2099 PRIntervalTime elapsedTime;
2101 ckrv = processCommand(command);
2103 endTime = PR_IntervalNow();
2104 elapsedTime = endTime - startTime;
2105 printf("Time -- %d msec \n",
2106 PR_IntervalToMilliseconds(elapsedTime));
2108 return ckrv;
2113 CK_RV
2114 process(FILE *inFile,int user)
2116 char buf[2048];
2117 CK_RV error;
2118 CK_RV ckrv = CKR_OK;
2120 if (user) { printf("pkcs11> "); fflush(stdout); }
2122 while (fgets(buf,2048,inFile) != NULL) {
2124 if (!user) printf("* %s",buf);
2125 error = processCommand(buf);
2126 if (error == CKR_QUIT) {
2127 break;
2128 } else if (error != CKR_OK) {
2129 ckrv = error;
2131 if (user) {
2132 printf("pkcs11> "); fflush(stdout);
2135 return ckrv;
2138 CK_RV
2139 run(const char *filename)
2141 FILE *infile;
2142 CK_RV ckrv;
2144 infile = fopen(filename,"r");
2146 if (infile == NULL) {
2147 perror(filename);
2148 return CKR_FUNCTION_FAILED;
2151 ckrv = process(infile, 0);
2153 fclose(infile);
2154 return ckrv;
2157 CK_RV
2158 loop(const char *filename, const char *var,
2159 CK_ULONG start, CK_ULONG end, CK_ULONG step)
2161 CK_ULONG i = 0;
2162 Value *value = 0;
2163 CK_RV ckrv;
2165 for (i=start; i < end; i += step)
2167 value = NewValue(ArgULong, 1);
2168 *(CK_ULONG *)value->data = i;
2169 DeleteVariable(var);
2170 AddVariable(var, &value);
2171 ckrv = run(filename);
2172 argFree(value);
2173 if (ckrv == CKR_QUIT) {
2174 break;
2177 return ckrv;
2181 main(int argc, char **argv)
2183 /* I suppose that some day we could parse some arguments */
2184 (void) process(stdin, 1);
2185 return 0;