Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include <stdio.h>
7 #include <string.h>
9 #if defined(WIN32)
10 #undef __STDC__
11 #include "fcntl.h"
12 #include "io.h"
13 #include <fcntl.h>
14 #else
15 #include <unistd.h>
16 #include <fcntl.h>
17 #endif
19 #include "secutil.h"
22 #include "nspr.h"
23 #include "prtypes.h"
24 #include "prtime.h"
25 #include "prlong.h"
26 #include "prinrval.h"
27 #include "prenv.h"
29 #include "pkcs11.h"
31 #include "pk11table.h"
33 #ifndef O_BINARY
34 #define O_BINARY 0
35 #endif
37 CK_ULONG systemFlags;
38 #define FLAG_NEGATE 0x80000000
39 #define FLAG_Verify 0x00000001
40 #define FLAG_VerifyFile 0x00000002
41 #define CKR_QUIT 0x80000000
43 int ArgSize(ArgType type);
44 const char *constLookup(const char *bp, CK_ULONG *value, ConstType *type);
46 int
47 isNum(char c)
48 {
49 return (c >= '0' && c <= '9');
50 }
52 int
53 isConst(const char *c)
54 {
55 CK_ULONG value;
56 ConstType type;
58 constLookup(c, &value, &type);
59 return type != ConstNone;
60 }
62 /*
63 * see if the variable is really a 'size' function. This
64 * function may modify var if it is a size function.
65 */
66 char *
67 isSize(char *var, int *isArray)
68 {
69 char *ptr = NULL;
70 char *end;
71 int array = 0;
73 if (PL_strncasecmp(var,"sizeof(",/*)*/ 7) == 0) {
74 ptr = var + 7;
75 } else if (PL_strncasecmp(var,"size(",/*)*/ 5) == 0) {
76 ptr = var + 5;
77 } else if (PL_strncasecmp(var,"sizeofarray(",/*)*/ 12) == 0) {
78 ptr = var + 12;
79 array = 1;
80 } else if (PL_strncasecmp(var,"sizea(",/*)*/ 6) == 0) {
81 ptr = var + 6;
82 array = 1;
83 } else {
84 return NULL;
85 }
86 end = strchr(ptr,/*(*/ ')') ;
87 if (end == NULL) {
88 return NULL;
89 }
90 if (isArray) *isArray = array;
91 *end = 0;
92 return ptr;
93 }
95 void
96 printConst(CK_ULONG value, ConstType type, int newLine)
97 {
98 int i;
100 for (i=0; i < constCount; i++) {
101 if (consts[i].type == type && consts[i].value == value) {
102 printf("%s",consts[i].name);
103 break;
104 }
105 if (type == ConstNone && consts[i].value == value) {
106 printf("%s",consts[i].name);
107 break;
108 }
109 }
110 if (i == constCount) {
111 if ((type == ConstAvailableSizes) || (type == ConstCurrentSize)) {
112 printf("%lu",value);
113 } else {
114 printf("Unknown %s (%lu:0x%lx)",constTypeString[type],value,value);
115 }
116 }
117 if (newLine) {
118 printf("\n");
119 }
120 }
122 ConstType
123 getConstFromAttribute(CK_ATTRIBUTE_TYPE type)
124 {
125 int i;
127 for (i=0; i < constCount; i++) {
128 if (consts[i].type == ConstAttribute && consts[i].value == type) {
129 return consts[i].attrType;
130 }
131 }
132 return ConstNone;
133 }
135 void
136 printChars(const char *name, CK_ULONG size)
137 {
138 CK_ULONG i;
139 for (i=0; i < size; i++) {
140 if (name[i] == 0) {
141 break;
142 }
143 printf("%c",name[i]);
144 }
145 printf("\n");
146 }
148 #define DUMP_LEN 16
149 void printDump(const unsigned char *buf, int size)
150 {
151 int i,j;
153 for(i=0; i < size; i+= DUMP_LEN) {
154 printf(" ");
155 for (j=0; j< DUMP_LEN; j++) {
156 if (i+j < size) {
157 printf("%02x ",buf[i+j]);
158 } else {
159 printf(" ");
160 }
161 }
162 for (j=0; j< DUMP_LEN; j++) {
163 if (i+j < size) {
164 if (buf[i+j] < ' ' || buf[i+j] >= 0x7f) {
165 printf(".");
166 } else {
167 printf("%c",buf[i+j]);
168 }
169 } else {
170 printf(" ");
171 }
172 }
173 printf("\n");
174 }
175 }
177 /*
178 * free an argument structure
179 */
180 void
181 argFreeData(Value *arg)
182 {
183 if (arg->data && ((arg->type & ArgStatic) == 0)) {
184 if ((arg->type & ArgMask) == ArgAttribute) {
185 int i;
186 CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)arg->data;
188 for (i=0; i < arg->arraySize; i++) {
189 free(template[i].pValue);
190 }
191 }
192 if ((arg->type & ArgMask) == ArgInitializeArgs) {
193 CK_C_INITIALIZE_ARGS *init = (CK_C_INITIALIZE_ARGS *)arg->data;
194 if (init->LibraryParameters) {
195 free(init->LibraryParameters);
196 }
197 }
198 free(arg->data);
199 }
200 arg->type &= ~ArgStatic;
201 arg->data = NULL;
202 }
204 void
205 argFree(Value *arg)
206 {
207 if (arg == NULL) return;
209 arg->reference--;
210 if (arg->reference == 0) {
211 if (arg->type & ArgFile) {
212 free(arg->filename);
213 }
214 argFreeData(arg);
215 free (arg);
216 }
217 }
219 /*
220 * free and argument list
221 */
222 void
223 parseFree(Value **ap)
224 {
225 int i;
226 for (i=0 ; i < MAX_ARGS; i++) {
227 argFree(ap[i]);
228 }
229 }
231 /*
232 * getEnd: how for to the end of this argmument list?
233 */
234 int
235 getEnd(const char *bp)
236 {
237 int count = 0;
239 while (*bp) {
240 if (*bp == ' ' || *bp == '\t' || *bp == '\n') return count;
241 count++;
242 bp++;
243 }
244 return (count);
245 }
248 /*
249 * strip: return the first none white space character
250 */
251 const char *
252 strip(const char *bp)
253 {
254 while (*bp && (*bp == ' ' || *bp == '\t' || *bp == '\n')) bp++;
255 return bp;
256 }
258 /*
259 * read in the next argument into dp ... don't overflow
260 */
261 const char *
262 readChars(const char *bp, char *dp, int max )
263 {
264 int count = 1;
265 while (*bp) {
266 if (*bp == ' ' || *bp == '\t' || *bp == '\n' ) {
267 *dp = 0;
268 return bp;
269 }
270 *dp++ = *bp++;
271 if (++count == max) break;
272 }
273 while (*bp && (*bp != ' ' && *bp != '\t' && *bp != '\n')) bp++;
274 *dp = 0;
275 return (bp);
276 }
278 Value * varLookup(const char *bp, char *vname, int max, int *error);
280 CK_ULONG
281 getValue(const char *v, int *error)
282 {
283 Value * varVal = NULL;
284 CK_ULONG retVal = 0;
285 ConstType type;
286 char tvar[512];
288 *error = 0;
290 varVal = varLookup( v, tvar, sizeof(tvar), error);
292 if (varVal) {
293 if ((varVal->type & ArgMask) == ArgULong) {
294 retVal = *(CK_ULONG *)varVal->data;
295 } else {
296 fprintf(stderr,"%s: is not a ulong\n", v);
297 *error = 1;
298 }
299 argFree(varVal);
300 return retVal;
301 }
302 constLookup(v, &retVal, &type);
303 return retVal;
304 }
306 Value *
307 NewValue(ArgType type, CK_ULONG arraySize)
308 {
309 Value *value;
311 value = (Value *)malloc(sizeof(Value));
312 if (!value) return NULL;
313 value->size = ArgSize(type)*arraySize;
314 value->type = type;
315 value->filename = NULL;
316 value->constType = ConstNone;
317 value->data = (void *)malloc(value->size);
318 if (!value->data) {
319 free(value);
320 return NULL;
321 }
322 value->reference = 1;
323 value->arraySize = (type == ArgChar) ? 1: arraySize;
325 memset(value->data, 0, value->size);
326 return value;
327 }
329 #define INVALID_INDEX 0xffffffff
331 CK_ULONG
332 handleArray(char *vname, int *error)
333 {
334 char *bracket;
335 CK_ULONG index = INVALID_INDEX;
337 if ((bracket = strchr(vname,'[')) != 0) {
338 char *tmpv = bracket+1;
339 *bracket = 0;
340 bracket = strchr(tmpv,']');
342 if (bracket == 0) {
343 fprintf(stderr,"%s: missing closing brace\n", vname);
344 return INVALID_INDEX;
345 }
346 *bracket = 0;
348 index = getValue(tmpv, error);
349 if (*error == 1) {
350 return INVALID_INDEX;
351 } else if (index == INVALID_INDEX) {
352 fprintf(stderr, "%s: 0x%lx is an invalid index\n",vname,index);
353 *error = 1;
354 }
355 }
356 return index;
357 }
359 void *
360 makeArrayTarget(const char *vname, const Value *value, CK_ULONG index)
361 {
362 char * target;
363 CK_ULONG elementSize;
365 if (index >= (CK_ULONG)value->arraySize) {
366 fprintf(stderr, "%s[%lu]: index larger than array size (%d)\n",
367 vname, index, value->arraySize);
368 return NULL;
369 }
371 target = (char *)value->data;
372 elementSize = value->size/value->arraySize;
373 target += index * elementSize;
374 return target;
375 }
377 /*
378 * look up a variable from the variable chain
379 */
380 static Variable *varHead = NULL;
381 Value *
382 varLookup(const char *bp, char *vname, int max, int *error)
383 {
384 Variable *current;
385 CK_ULONG index = INVALID_INDEX;
386 int isArray = 0;
387 char *ptr;
388 *error = 0;
390 if (bp != NULL) {
391 readChars(bp, vname, max);
392 }
394 /* don't make numbers into variables */
395 if (isNum(vname[0])) {
396 return NULL;
397 }
398 /* nor consts */
399 if (isConst(vname)) {
400 return NULL;
401 }
402 /* handle sizeof() */
403 if ((ptr = isSize(vname, &isArray)) != NULL) {
404 CK_ULONG size;
405 Value *targetValue = NULL;
406 Value *sourceValue = varLookup(NULL, ptr, 0, error);
407 if (!sourceValue) {
408 if (*error == 0) {
409 /* just didn't find it */
410 *error = 1;
411 fprintf(stderr,"Couldn't find variable %s to take size of\n",
412 ptr);
413 return NULL;
414 }
415 }
416 size = isArray ? sourceValue->arraySize : sourceValue->size;
417 targetValue = NewValue(ArgULong,1);
418 memcpy(targetValue->data, &size, sizeof(size));
420 return targetValue;
421 }
423 /* modifies vname */
424 index = handleArray(vname, error);
425 if (*error == 1) {
426 return NULL;
427 }
429 for (current = varHead; current; current = current->next) {
430 if (PL_strcasecmp(current->vname, vname) == 0) {
431 char *target;
432 if (index == INVALID_INDEX) {
433 (current->value->reference)++;
434 return current->value;
435 }
436 target = makeArrayTarget(vname, current->value, index);
437 if (target) {
438 Value *element = NewValue(current->value->type, 1);
439 if (!element) {
440 fprintf(stderr, "MEMORY ERROR!\n");
441 *error = 1;
442 }
443 argFreeData(element);
444 element->data = target;
445 element->type |= ArgStatic;
446 return element;
447 }
448 *error = 1;
449 return NULL;
450 }
451 }
452 return NULL;
453 }
455 static CK_RV
456 list(void)
457 {
458 Variable *current;
460 if (varHead) {
461 printf(" %10s\t%16s\t%8s\tSize\tElements\n","Name","Type","Const");
462 } else {
463 printf(" no variables set\n");
464 }
466 for (current = varHead; current; current = current->next) {
467 printf(" %10s\t%16s\t%8s\t%d\t%d\n", current->vname,
468 valueString[current->value->type&ArgMask],
469 constTypeString[current->value->constType],
470 current->value->size, current->value->arraySize);
471 }
472 return CKR_OK;
473 }
475 CK_RV
476 printFlags(const char *s, CK_ULONG flags, ConstType type)
477 {
478 CK_ULONG i;
479 int needComma = 0;
481 printf("%s",s);
482 for (i=1; i ; i=i << 1) {
483 if (flags & i) {
484 printf("%s",needComma?",":"");
485 printConst(i, type, 0);
486 needComma=1;
487 }
488 }
489 if (!needComma) {
490 printf("Empty");
491 }
492 printf("\n");
493 return CKR_OK;
494 }
496 /*
497 * add a new variable to the chain
498 */
499 const char *
500 AddVariable(const char *bp, Value **ptr)
501 {
502 char vname[512];
503 Variable *current;
504 int index = INVALID_INDEX;
505 int size;
506 int error = 0;
508 bp = readChars(bp,vname,sizeof(vname));
510 /* don't make numbers into variables */
511 if (isNum(vname[0])) {
512 return bp;
513 }
514 /* or consts */
515 if (isConst(vname)) {
516 return bp;
517 }
518 /* or NULLs */
519 if (vname[0] == 0) {
520 return bp;
521 }
522 /* or sizeof */
523 if (isSize(vname, NULL)) {
524 return bp;
525 }
526 /* arrays values should be written back to the original */
527 index = handleArray(vname, &error);
528 if (error == 1) {
529 return bp;
530 }
533 for (current = varHead; current; current = current->next) {
534 if (PL_strcasecmp(current->vname,vname) == 0) {
535 char *target;
536 /* found a complete object, return the found one */
537 if (index == INVALID_INDEX) {
538 argFree(*ptr);
539 *ptr = current->value;
540 return bp;
541 }
542 /* found an array, update the array element */
543 target = makeArrayTarget(vname, current->value, index);
544 if (target) {
545 memcpy(target, (*ptr)->data, (*ptr)->size);
546 argFreeData(*ptr);
547 (*ptr)->data = target;
548 (*ptr)->type |= ArgStatic;
549 }
550 return bp;
551 }
552 }
554 /* we are looking for an array and didn't find one */
555 if (index != INVALID_INDEX) {
556 return bp;
557 }
560 current = (Variable *)malloc(sizeof(Variable));
561 size = strlen(vname);
562 current->vname = (char *)malloc(size+1);
563 strcpy(current->vname,vname);
564 current->value = *ptr;
565 (*ptr)->reference++;
567 current->next = varHead;
568 varHead = current;
569 return bp;
570 }
572 ArgType
573 FindTypeByName(const char *typeName)
574 {
575 int i;
577 for (i=0; i < valueCount; i++) {
578 if (PL_strcasecmp(typeName,valueString[i]) == 0) {
579 return (ArgType) i;
580 }
581 if (valueString[i][0] == 'C' && valueString[i][1] == 'K' &&
582 valueString[i][2] == '_' &&
583 (PL_strcasecmp(typeName,&valueString[i][3]) == 0)) {
584 return (ArgType) i;
585 }
586 }
587 return ArgNone;
588 }
590 CK_RV
591 ArrayVariable(const char *bp, const char *typeName, CK_ULONG count)
592 {
593 ArgType type;
594 Value *value; /* new Value */
596 type = FindTypeByName(typeName);
597 if (type == ArgNone) {
598 fprintf(stderr,"Invalid type (%s)\n", typeName);
599 return CKR_FUNCTION_FAILED;
600 }
601 value = NewValue(type, count);
602 (void) AddVariable(bp, &value);
603 return CKR_OK;
604 }
606 #define MAX_TEMPLATE 25
608 CK_RV
609 ArrayTemplate(const char *bp, char *attributes)
610 {
611 char aname[512];
612 CK_ULONG attributeTypes[MAX_TEMPLATE];
613 CK_ATTRIBUTE *template;
614 Value *value; /* new Value */
615 char *ap;
616 int i, count = 0;
618 memcpy(aname,attributes,strlen(attributes)+1);
620 for (ap = aname, count = 0; ap && *ap && count < MAX_TEMPLATE; count++) {
621 char *cur = ap;
622 ConstType type;
624 ap = strchr(ap,',');
625 if (ap) {
626 *ap++ = 0;
627 }
629 (void)constLookup(cur, &attributeTypes[count], &type);
630 if ((type != ConstAttribute) && (type != ConstNone)) {
631 fprintf(stderr, "Unknown Attribute %s\n", cur);
632 return CKR_FUNCTION_FAILED;
633 }
634 }
636 value = NewValue(ArgAttribute, count);
638 template = (CK_ATTRIBUTE *)value->data;
639 for (i=0; i < count ; i++) {
640 template[i].type = attributeTypes[i];
641 }
642 (void) AddVariable(bp, &value);
643 return CKR_OK;
644 }
646 CK_RV
647 BuildTemplate(Value *vp)
648 {
649 CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data;
650 int i;
652 for (i=0; i < vp->arraySize; i++) {
653 if (((signed long)template[i].ulValueLen) > 0) {
654 if (template[i].pValue) free(template[i].pValue);
655 template[i].pValue = malloc(template[i].ulValueLen);
656 }
657 }
658 return CKR_OK;
659 }
661 CK_RV
662 SetTemplate(Value *vp, CK_ULONG index, CK_ULONG value)
663 {
664 CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data;
665 int isbool = 0;
666 CK_ULONG len;
667 ConstType attrType;
669 if (index >= (CK_ULONG) vp->arraySize) {
670 fprintf(stderr,"index (%lu) greater than array (%d)\n",
671 index, vp->arraySize);
672 return CKR_ARGUMENTS_BAD;
673 }
674 attrType = getConstFromAttribute(template[index].type);
676 if (attrType == ConstNone) {
677 fprintf(stderr,"can't set index (%lu) because ", index);
678 printConst(template[index].type,ConstAttribute, 0);
679 fprintf(stderr, " is not a CK_BBOOL or CK_ULONG\n");
680 return CKR_ARGUMENTS_BAD;
681 }
682 isbool = (attrType == ConstBool);
683 len = isbool ? sizeof (CK_BBOOL) : sizeof(CK_ULONG);
684 if ((template[index].ulValueLen != len) || (template[index].pValue)) {
685 free(template[index].pValue);
686 template[index].pValue = malloc(len);
687 template[index].ulValueLen = len;
688 }
689 if (isbool) {
690 *(CK_BBOOL *)template[index].pValue = (CK_BBOOL) value;
691 } else {
692 *(CK_ULONG *)template[index].pValue = (CK_ULONG) value;
693 }
694 return CKR_OK;
696 }
698 CK_RV
699 NewMechanism(const char *bp, CK_ULONG mechType)
700 {
701 Value *value; /* new Value */
702 CK_MECHANISM *mechanism;
704 value = NewValue(ArgMechanism, 1);
705 mechanism = (CK_MECHANISM *)value->data;
706 mechanism->mechanism = mechType;
707 mechanism->pParameter = NULL;
708 mechanism->ulParameterLen = 0;
709 (void) AddVariable(bp, &value);
710 return CKR_OK;
711 }
713 CK_RV
714 NewInitializeArgs(const char *bp, CK_ULONG flags, const char *param)
715 {
716 Value *value; /* new Value */
717 CK_C_INITIALIZE_ARGS *init;
719 value = NewValue(ArgInitializeArgs, 1);
720 init = (CK_C_INITIALIZE_ARGS *)value->data;
721 init->flags = flags;
722 if (strcmp(param, "null") != 0) {
723 init->LibraryParameters = (CK_CHAR_PTR *)strdup(param);
724 }
725 (void) AddVariable(bp, &value);
726 return CKR_OK;
727 }
729 /*
730 * add a new variable to the chain
731 */
732 CK_RV
733 DeleteVariable(const char *bp)
734 {
735 char vname[512];
736 Variable **current;
738 bp = readChars(bp,vname,sizeof(vname));
740 for (current = &varHead; *current; current = &(*current)->next) {
741 if (PL_strcasecmp((*current)->vname,vname) == 0) {
742 argFree((*current)->value);
743 *current = (*current)->next;
744 break;
745 }
746 }
747 return CKR_OK;
748 }
750 /*
751 * convert an octal value to integer
752 */
753 CK_ULONG
754 otoi(const char *o)
755 {
756 CK_ULONG value = 0;
758 while (*o) {
759 if ((*o >= '0') && (*o <= '7')) {
760 value = (value << 3) | (unsigned)(*o - '0');
761 } else {
762 break;
763 }
764 }
765 return value;
766 }
768 /*
769 * convert a hex value to integer
770 */
771 CK_ULONG
772 htoi(const char *x)
773 {
774 CK_ULONG value = 0;
776 while (*x) {
777 if ((*x >= '0') && (*x <= '9')) {
778 value = (value << 4) | (unsigned)(*x - '0');
779 } else if ((*x >= 'a') && (*x <= 'f')) {
780 value = (value << 4) | (unsigned)(*x - 'a');
781 } else if ((*x >= 'A') && (*x <= 'F')) {
782 value = (value << 4) | (unsigned)(*x - 'A');
783 } else {
784 break;
785 }
786 }
787 return value;
788 }
791 /*
792 * look up or decode a constant value
793 */
794 const char *
795 constLookup(const char *bp, CK_ULONG *value, ConstType *type)
796 {
797 char vname[512];
798 int i;
800 bp = readChars(bp,vname,sizeof(vname));
802 for (i=0; i < constCount; i++) {
803 if ((PL_strcasecmp(consts[i].name,vname) == 0) ||
804 PL_strcasecmp(consts[i].name+5,vname) == 0) {
805 *value = consts[i].value;
806 *type = consts[i].type;
807 return bp;
808 }
809 }
811 *type = ConstNone;
812 if (vname[0] == '0' && vname[1] == 'X') {
813 *value = htoi(&vname[2]);
814 } else if (vname[0] == '0') {
815 *value = otoi(&vname[1]);
816 } else {
817 *value = atoi(vname);
818 }
819 return bp;
820 }
822 int
823 ArgSize(ArgType type)
824 {
825 int size=0;
826 type &= ArgMask;
828 switch (type) {
829 case ArgNone:
830 size = 0;
831 break;
832 case ArgULong:
833 size = sizeof(CK_ULONG);
834 break;
835 case ArgVar:
836 size = 1; /* get's changed later */
837 break;
838 case ArgChar:
839 case ArgUTF8:
840 size = 1;
841 break;
842 case ArgInfo:
843 size = sizeof(CK_INFO);
844 break;
845 case ArgSlotInfo:
846 size = sizeof(CK_SLOT_INFO);
847 break;
848 case ArgTokenInfo:
849 size = sizeof(CK_TOKEN_INFO);
850 break;
851 case ArgSessionInfo:
852 size = sizeof(CK_SESSION_INFO);
853 break;
854 case ArgAttribute:
855 size = sizeof(CK_ATTRIBUTE);
856 break;
857 case ArgMechanism:
858 size = sizeof(CK_MECHANISM);
859 break;
860 case ArgMechanismInfo:
861 size = sizeof(CK_MECHANISM_INFO);
862 break;
863 case ArgInitializeArgs:
864 size = sizeof(CK_C_INITIALIZE_ARGS);
865 break;
866 case ArgFunctionList:
867 size = sizeof(CK_FUNCTION_LIST);
868 break;
869 default:
870 break;
871 }
873 return (size);
874 }
876 CK_RV
877 restore(const char *filename,Value *ptr)
878 {
879 int fd,size;
881 fd = open(filename,O_RDONLY|O_BINARY);
882 if (fd < 0) {
883 perror(filename);
884 return CKR_FUNCTION_FAILED;
885 }
887 size = read(fd,ptr->data,ptr->size);
888 if (systemFlags & FLAG_VerifyFile) {
889 printDump(ptr->data,ptr->size);
890 }
891 if (size < 0) {
892 perror(filename);
893 return CKR_FUNCTION_FAILED;
894 } else if (size != ptr->size) {
895 fprintf(stderr,"%s: only read %d bytes, needed to read %d bytes\n",
896 filename,size,ptr->size);
897 return CKR_FUNCTION_FAILED;
898 }
899 close(fd);
900 return CKR_OK;
901 }
903 CK_RV
904 save(const char *filename,Value *ptr)
905 {
906 int fd,size;
908 fd = open(filename,O_WRONLY|O_BINARY|O_CREAT,0666);
909 if (fd < 0) {
910 perror(filename);
911 return CKR_FUNCTION_FAILED;
912 }
914 size = write(fd,ptr->data,ptr->size);
915 if (size < 0) {
916 perror(filename);
917 return CKR_FUNCTION_FAILED;
918 } else if (size != ptr->size) {
919 fprintf(stderr,"%s: only wrote %d bytes, need to write %d bytes\n",
920 filename,size,ptr->size);
921 return CKR_FUNCTION_FAILED;
922 }
923 close(fd);
924 return CKR_OK;
925 }
927 static CK_RV
928 increment(Value *ptr, CK_ULONG value)
929 {
930 if ((ptr->type & ArgMask) != ArgULong) {
931 return CKR_ARGUMENTS_BAD;
932 }
933 *(CK_ULONG *)ptr->data += value;
934 return CKR_OK;
935 }
937 static CK_RV
938 decrement(Value *ptr, CK_ULONG value)
939 {
940 if ((ptr->type & ArgMask) != ArgULong) {
941 return CKR_ARGUMENTS_BAD;
942 }
943 *(CK_ULONG *)ptr->data -= value;
944 return CKR_OK;
945 }
947 CK_RV
948 printArg(Value *ptr,int arg_number)
949 {
950 ArgType type = ptr->type & ArgMask;
951 CK_INFO *info;
952 CK_SLOT_INFO *slotInfo;
953 CK_TOKEN_INFO *tokenInfo;
954 CK_SESSION_INFO *sessionInfo;
955 CK_ATTRIBUTE *attribute;
956 CK_MECHANISM *mechanism;
957 CK_MECHANISM_INFO *mechanismInfo;
958 CK_C_INITIALIZE_ARGS *initArgs;
959 CK_FUNCTION_LIST *functionList;
960 CK_RV ckrv = CKR_OK;
961 ConstType constType;
963 if (arg_number) {
964 printf("Arg %d: \n",arg_number);
965 }
966 if (ptr->arraySize > 1) {
967 Value element;
968 int i;
969 int elementSize = ptr->size/ptr->arraySize;
970 char *dp = (char *)ptr->data;
972 /* build a temporary Value to hold a single element */
973 element.type = type;
974 element.constType = ptr->constType;
975 element.size = elementSize;
976 element.filename = ptr->filename;
977 element.reference = 1;
978 element.arraySize = 1;
979 for (i=0; i < ptr->arraySize; i++) {
980 printf(" -----[ %d ] -----\n", i);
981 element.data = (void *) &dp[i*elementSize];
982 (void) printArg(&element, 0);
983 }
984 return ckrv;
985 }
986 if (ptr->data == NULL) {
987 printf(" NULL ptr to a %s\n", valueString[type]);
988 return ckrv;
989 }
990 switch (type) {
991 case ArgNone:
992 printf(" None\n");
993 break;
994 case ArgULong:
995 printf(" %lu (0x%lx)\n", *((CK_ULONG *)ptr->data),
996 *((CK_ULONG *)ptr->data));
997 if (ptr->constType != ConstNone) {
998 printf(" ");
999 printConst(*(CK_ULONG *)ptr->data,ptr->constType,1);
1000 }
1001 break;
1002 case ArgVar:
1003 printf(" %s\n",(char *)ptr->data);
1004 break;
1005 case ArgUTF8:
1006 printf(" %s\n",(char *)ptr->data);
1007 break;
1008 case ArgChar:
1009 printDump(ptr->data,ptr->size);
1010 break;
1011 case ArgInfo:
1012 #define VERSION(x) (x).major, (x).minor
1013 info = (CK_INFO *)ptr->data;
1014 printf(" Cryptoki Version: %d.%02d\n",
1015 VERSION(info->cryptokiVersion));
1016 printf(" Manufacturer ID: ");
1017 printChars((char *)info->manufacturerID,
1018 sizeof(info->manufacturerID));
1019 printFlags(" Flags: ", info->flags, ConstInfoFlags);
1020 printf(" Library Description: ");
1021 printChars((char *)info->libraryDescription,
1022 sizeof(info->libraryDescription));
1023 printf(" Library Version: %d.%02d\n",
1024 VERSION(info->libraryVersion));
1025 break;
1026 case ArgSlotInfo:
1027 slotInfo = (CK_SLOT_INFO *)ptr->data;
1028 printf(" Slot Description: ");
1029 printChars((char *)slotInfo->slotDescription,
1030 sizeof(slotInfo->slotDescription));
1031 printf(" Manufacturer ID: ");
1032 printChars((char *)slotInfo->manufacturerID,
1033 sizeof(slotInfo->manufacturerID));
1034 printFlags(" Flags: ", slotInfo->flags, ConstSlotFlags);
1035 printf(" Hardware Version: %d.%02d\n",
1036 VERSION(slotInfo->hardwareVersion));
1037 printf(" Firmware Version: %d.%02d\n",
1038 VERSION(slotInfo->firmwareVersion));
1039 break;
1040 case ArgTokenInfo:
1041 tokenInfo = (CK_TOKEN_INFO *)ptr->data;
1042 printf(" Label: ");
1043 printChars((char *) tokenInfo->label,sizeof(tokenInfo->label));
1044 printf(" Manufacturer ID: ");
1045 printChars((char *)tokenInfo->manufacturerID,
1046 sizeof(tokenInfo->manufacturerID));
1047 printf(" Model: ");
1048 printChars((char *)tokenInfo->model,sizeof(tokenInfo->model));
1049 printf(" Serial Number: ");
1050 printChars((char *)tokenInfo->serialNumber,
1051 sizeof(tokenInfo->serialNumber));
1052 printFlags(" Flags: ", tokenInfo->flags, ConstTokenFlags);
1053 printf(" Max Session Count: ");
1054 printConst(tokenInfo->ulMaxSessionCount, ConstAvailableSizes, 1);
1055 printf(" Session Count: ");
1056 printConst(tokenInfo->ulSessionCount, ConstCurrentSize, 1);
1057 printf(" RW Session Count: ");
1058 printConst(tokenInfo->ulMaxRwSessionCount, ConstAvailableSizes, 1);
1059 printf(" Max Pin Length : ");
1060 printConst(tokenInfo->ulMaxPinLen, ConstCurrentSize, 1);
1061 printf(" Min Pin Length : ");
1062 printConst(tokenInfo->ulMinPinLen, ConstCurrentSize, 1);
1063 printf(" Total Public Memory: ");
1064 printConst(tokenInfo->ulTotalPublicMemory, ConstAvailableSizes, 1);
1065 printf(" Free Public Memory: ");
1066 printConst(tokenInfo->ulFreePublicMemory, ConstCurrentSize, 1);
1067 printf(" Total Private Memory: ");
1068 printConst(tokenInfo->ulTotalPrivateMemory, ConstAvailableSizes, 1);
1069 printf(" Free Private Memory: ");
1070 printConst(tokenInfo->ulFreePrivateMemory, ConstCurrentSize, 1);
1071 printf(" Hardware Version: %d.%02d\n",
1072 VERSION(tokenInfo->hardwareVersion));
1073 printf(" Firmware Version: %d.%02d\n",
1074 VERSION(tokenInfo->firmwareVersion));
1075 printf(" UTC Time: ");
1076 printChars((char *)tokenInfo->utcTime,sizeof(tokenInfo->utcTime));
1077 break;
1078 case ArgSessionInfo:
1079 sessionInfo = (CK_SESSION_INFO *)ptr->data;
1080 printf(" SlotID: 0x%08lx\n", sessionInfo->slotID);
1081 printf(" State: ");
1082 printConst(sessionInfo->state, ConstSessionState, 1);
1083 printFlags(" Flags: ", sessionInfo->flags, ConstSessionFlags);
1084 printf(" Device error: %lu 0x%08lx\n",sessionInfo->ulDeviceError,
1085 sessionInfo->ulDeviceError);
1086 break;
1087 case ArgAttribute:
1088 attribute = (CK_ATTRIBUTE *)ptr->data;
1089 printf(" Attribute Type: ");
1090 printConst(attribute->type, ConstAttribute, 1);
1091 printf(" Attribute Data: ");
1092 if (attribute->pValue == NULL) {
1093 printf("NULL\n");
1094 printf("Attribute Len: %lu\n",attribute->ulValueLen);
1095 } else {
1096 constType = getConstFromAttribute(attribute->type);
1097 if (constType != ConstNone) {
1098 CK_ULONG value = (constType == ConstBool) ?
1099 *(CK_BBOOL *)attribute->pValue :
1100 *(CK_ULONG *)attribute->pValue;
1101 printConst(value, constType, 1);
1102 } else {
1103 printf("\n");
1104 printDump(attribute->pValue, attribute->ulValueLen);
1105 }
1106 }
1107 break;
1108 case ArgMechanism:
1109 mechanism = (CK_MECHANISM *)ptr->data;
1110 printf(" Mechanism Type: ");
1111 printConst(mechanism->mechanism, ConstMechanism, 1);
1112 printf(" Mechanism Data:\n");
1113 printDump(mechanism->pParameter, mechanism->ulParameterLen);
1114 break;
1115 case ArgMechanismInfo:
1116 mechanismInfo = (CK_MECHANISM_INFO *)ptr->data;
1117 printf(" Minimum Key Size: %ld\n",mechanismInfo->ulMinKeySize);
1118 printf(" Maximum Key Size: %ld\n",mechanismInfo->ulMaxKeySize);
1119 printFlags(" Flags: ", mechanismInfo->flags, ConstMechanismFlags);
1120 break;
1121 case ArgInitializeArgs:
1122 initArgs = (CK_C_INITIALIZE_ARGS *)ptr->data;
1123 printFlags(" Flags: ", initArgs->flags, ConstInitializeFlags);
1124 if (initArgs->LibraryParameters) {
1125 printf("Params: %s\n",(char *)initArgs->LibraryParameters);
1126 }
1127 case ArgFunctionList:
1128 functionList = (CK_FUNCTION_LIST *)ptr->data;
1129 printf(" Version: %d.%02d\n", VERSION(functionList->version));
1130 #ifdef notdef
1131 #undef CK_NEED_ARG_LIST
1132 #define CK_PKCS11_FUNCTION_INFO(func) \
1133 printf(" %s: 0x%08lx\n", #func, (unsigned long) functionList->func );
1134 #include "pkcs11f.h"
1135 #undef CK_NEED_ARG_LIST
1136 #undef CK_PKCS11_FUNCTION_INFO
1137 #endif
1138 default:
1139 ckrv = CKR_ARGUMENTS_BAD;
1140 break;
1141 }
1143 return ckrv;
1144 }
1147 /*
1148 * Feeling ambitious? turn this whole thing into lexx yacc parser
1149 * with full expressions.
1150 */
1151 Value **
1152 parseArgs(int index, const char * bp)
1153 {
1154 const Commands *cp = &commands[index];
1155 int size = strlen(cp->fname);
1156 int i;
1157 CK_ULONG value;
1158 char vname[512];
1159 Value **argList,*possible;
1160 ConstType constType;
1162 /*
1163 * skip pass the command
1164 */
1165 if ((cp->fname[0] == 'C') && (cp->fname[1] == '_') && (bp[1] != '_')) {
1166 size -= 2;
1167 }
1168 bp += size;
1170 /*
1171 * Initialize our argument list
1172 */
1173 argList = (Value **)malloc(sizeof(Value*)*MAX_ARGS);
1174 for (i=0; i < MAX_ARGS; i++) { argList[i] = NULL; }
1176 /*
1177 * Walk the argument list parsing it...
1178 */
1179 for (i=0 ;i < MAX_ARGS; i++) {
1180 ArgType type = cp->args[i] & ArgMask;
1181 int error;
1183 /* strip blanks */
1184 bp = strip(bp);
1186 /* if we hit ArgNone, we've nabbed all the arguments we need */
1187 if (type == ArgNone) {
1188 break;
1189 }
1191 /* if we run out of space in the line, we weren't given enough
1192 * arguments... */
1193 if (*bp == '\0') {
1194 /* we're into optional arguments, ok to quit now */
1195 if (cp->args[i] & ArgOpt) {
1196 break;
1197 }
1198 fprintf(stderr,"%s: only %d args found,\n",cp->fname,i);
1199 parseFree(argList);
1200 return NULL;
1201 }
1203 /* collect all the rest of the command line and send
1204 * it as a single argument */
1205 if (cp->args[i] & ArgFull) {
1206 int size = strlen(bp)+1;
1207 argList[i] = NewValue(type, size);
1208 memcpy(argList[i]->data, bp, size);
1209 break;
1210 }
1212 /*
1213 * look up the argument in our variable list first... only
1214 * exception is the new argument type for set...
1215 */
1216 error = 0;
1217 if ((cp->args[i] != (ArgVar|ArgNew)) &&
1218 (possible = varLookup(bp,vname,sizeof(vname),&error))) {
1219 /* ints are only compatible with other ints... all other types
1220 * are interchangeable... */
1221 if (type != ArgVar) { /* ArgVar's match anyone */
1222 if ((type == ArgULong) ^
1223 ((possible->type & ArgMask) == ArgULong)) {
1224 fprintf(stderr,"%s: Arg %d incompatible type with <%s>\n",
1225 cp->fname,i+1,vname);
1226 argFree(possible);
1227 parseFree(argList);
1228 return NULL;
1229 }
1230 /*
1231 * ... that is as long as they are big enough...
1232 */
1233 if (ArgSize(type) > possible->size) {
1234 fprintf(stderr,
1235 "%s: Arg %d %s is too small (%d bytes needs to be %d bytes)\n",
1236 cp->fname,i+1,vname,possible->size,ArgSize(type));
1237 argFree(possible);
1238 parseFree(argList);
1239 return NULL;
1240 }
1241 }
1243 /* everything looks kosher here, use it */
1244 argList[i] = possible;
1246 bp = readChars(bp,vname,sizeof(vname));
1247 if (cp->args[i] & ArgOut) {
1248 possible->type |= ArgOut;
1249 }
1250 continue;
1251 }
1253 if (error == 1) {
1254 parseFree(argList);
1255 return NULL;
1256 }
1258 /* create space for our argument */
1259 argList[i] = NewValue(type, 1);
1261 if ((PL_strncasecmp(bp, "null", 4) == 0) && ((bp[4] == 0)
1262 || (bp[4] == ' ') || (bp[4] =='\t') || (bp[4] =='\n'))) {
1263 if (cp->args[i] == ArgULong) {
1264 fprintf(stderr, "%s: Arg %d CK_ULONG can't be NULL\n",
1265 cp->fname,i+1);
1266 parseFree(argList);
1267 return NULL;
1268 }
1269 argFreeData(argList[i]);
1270 argList[i]->data = NULL;
1271 argList[i]->size = 0;
1272 bp += 4;
1273 if (*bp) bp++;
1274 continue;
1275 }
1277 /* if we're an output variable, we need to add it */
1278 if (cp->args[i] & ArgOut) {
1279 if (PL_strncasecmp(bp,"file(",5) == 0 /* ) */ ) {
1280 char filename[512];
1281 bp = readChars(bp+5,filename,sizeof(filename));
1282 size = PL_strlen(filename);
1283 if ((size > 0) && (/* ( */filename[size-1] == ')')) {
1284 filename[size-1] = 0;
1285 }
1286 filename[size] = 0;
1287 argList[i]->filename = (char *)malloc(size+1);
1289 PL_strcpy(argList[i]->filename,filename);
1291 argList[i]->type |= ArgOut|ArgFile;
1292 break;
1293 }
1294 bp = AddVariable(bp,&argList[i]);
1295 argList[i]->type |= ArgOut;
1296 continue;
1297 }
1299 if (PL_strncasecmp(bp, "file(", 5) == 0 /* ) */ ) {
1300 char filename[512];
1302 bp = readChars(bp+5,filename,sizeof(filename));
1303 size = PL_strlen(filename);
1304 if ((size > 0) && ( /* ( */ filename[size-1] == ')')) {
1305 filename[size-1] = 0;
1306 }
1308 if (restore(filename,argList[i]) != CKR_OK) {
1309 parseFree(argList);
1310 return NULL;
1311 }
1312 continue;
1313 }
1315 switch (type) {
1316 case ArgULong:
1317 bp = constLookup(bp, &value, &constType);
1318 *(int *)argList[i]->data = value;
1319 argList[i]->constType = constType;
1320 break;
1321 case ArgVar:
1322 argFreeData(argList[i]);
1323 size = getEnd(bp)+1;
1324 argList[i]->data = (void *)malloc(size);
1325 argList[i]->size = size;
1326 /* fall through */
1327 case ArgInfo:
1328 case ArgSlotInfo:
1329 case ArgTokenInfo:
1330 case ArgSessionInfo:
1331 case ArgAttribute:
1332 case ArgMechanism:
1333 case ArgMechanismInfo:
1334 case ArgInitializeArgs:
1335 case ArgUTF8:
1336 case ArgChar:
1337 bp = readChars(bp,(char *)argList[i]->data,argList[i]->size);
1338 case ArgNone:
1339 default:
1340 break;
1341 }
1342 }
1344 return argList;
1345 }
1347 /* lookup the command in the array */
1348 int
1349 lookup(const char *buf)
1350 {
1351 int size,i;
1352 int buflen;
1354 buflen = PL_strlen(buf);
1356 for ( i = 0; i < commandCount; i++) {
1357 size = PL_strlen(commands[i].fname);
1359 if (size <= buflen) {
1360 if (PL_strncasecmp(buf,commands[i].fname,size) == 0) {
1361 return i;
1362 }
1363 }
1364 if (size-2 <= buflen) {
1365 if (commands[i].fname[0] == 'C' && commands[i].fname[1] == '_' &&
1366 (PL_strncasecmp(buf,&commands[i].fname[2],size-2) == 0)) {
1367 return i;
1368 }
1369 }
1370 }
1371 fprintf(stderr,"Can't find command %s\n",buf);
1372 return -1;
1373 }
1375 void
1376 putOutput(Value **ptr)
1377 {
1378 int i;
1380 for (i=0; i < MAX_ARGS; i++) {
1381 ArgType type;
1383 if (ptr[i] == NULL) break;
1385 type = ptr[i]->type;
1387 ptr[i]->type &= ~ArgOut;
1388 if (type == ArgNone) {
1389 break;
1390 }
1391 if (type & ArgOut) {
1392 (void) printArg(ptr[i],i+1);
1393 }
1394 if (type & ArgFile) {
1395 save(ptr[i]->filename,ptr[i]);
1396 free(ptr[i]->filename);
1397 ptr[i]->filename= NULL; /* paranoia */
1398 }
1399 }
1400 }
1402 CK_RV
1403 unloadModule(Module *module)
1404 {
1405 char *disableUnload = NULL;
1407 disableUnload = PR_GetEnv("NSS_DISABLE_UNLOAD");
1409 if (module->library && !disableUnload) {
1410 PR_UnloadLibrary(module->library);
1411 }
1413 module->library = NULL;
1414 module->functionList = NULL;
1416 return CKR_OK;
1417 }
1419 CK_RV
1420 loadModule(Module *module, char *library)
1421 {
1422 PRLibrary *newLibrary;
1423 CK_C_GetFunctionList getFunctionList;
1424 CK_FUNCTION_LIST *functionList;
1425 CK_RV ckrv;
1427 newLibrary = PR_LoadLibrary(library);
1428 if (!newLibrary) {
1429 fprintf(stderr,"Couldn't load library %s\n",library);
1430 return CKR_FUNCTION_FAILED;
1431 }
1432 getFunctionList = (CK_C_GetFunctionList)
1433 PR_FindSymbol(newLibrary,"C_GetFunctionList");
1434 if (!getFunctionList) {
1435 fprintf(stderr,"Couldn't find \"C_GetFunctionList\" in %s\n",library);
1436 return CKR_FUNCTION_FAILED;
1437 }
1439 ckrv = (*getFunctionList)(&functionList);
1440 if (ckrv != CKR_OK) {
1441 return ckrv;
1442 }
1444 if (module->library) {
1445 PR_UnloadLibrary(module->library);
1446 }
1448 module->library = newLibrary;
1449 module->functionList = functionList;
1451 return CKR_OK;
1452 }
1454 static void
1455 printHelp(int index, int full)
1456 {
1457 int j;
1458 printf(" %s", commands[index].fname);
1459 for (j=0; j < MAX_ARGS; j++) {
1460 ArgType type = commands[index].args[j] & ArgMask;
1461 if (type == ArgNone) {
1462 break;
1463 }
1464 printf(" %s", valueString[type]);
1465 }
1466 printf("\n");
1467 printf(" %s\n",commands[index].helpString);
1468 }
1470 /* add Topical help here ! */
1471 static CK_RV
1472 printTopicHelp(char *topic)
1473 {
1474 int size,i;
1475 int topicLen;
1477 topicLen = PL_strlen(topic);
1479 for ( i = 0; i < topicCount; i++) {
1480 size = PL_strlen(topics[i].name);
1482 if (size <= topicLen) {
1483 if (PL_strncasecmp(topic,topics[i].name,size) == 0) {
1484 break;
1485 }
1486 }
1487 }
1489 if (i == topicCount) {
1490 fprintf(stderr,"Can't find topic '%s'\n", topic);
1491 return CKR_DATA_INVALID;
1492 }
1494 printf(" %s", topic);
1495 printf("\n");
1496 printf(" %s\n",topics[i].helpString);
1497 return CKR_OK;
1498 }
1500 static CK_RV
1501 printGeneralHelp(void)
1502 {
1503 int i;
1504 printf(" To get help on commands, select from the list below:");
1505 for ( i = 0; i < commandCount; i++) {
1506 if (i % 5 == 0) printf("\n");
1507 printf("%s,", commands[i].fname);
1508 }
1509 printf("\n");
1510 /* print help topics */
1511 printf(" To get help on a topic, select from the list below:");
1512 for ( i = 0; i < topicCount; i++) {
1513 if (i % 5 == 0) printf("\n");
1514 printf("%s,", topics[i].name);
1515 }
1516 printf("\n");
1517 return CKR_OK;
1518 }
1520 static CK_RV
1521 quitIf(CK_ULONG a, const char *cmp, CK_ULONG b)
1522 {
1523 if (strcmp(cmp, "<") == 0) {
1524 return (a < b) ? CKR_QUIT : CKR_OK;
1525 } else if (strcmp(cmp, ">") == 0) {
1526 return (a > b) ? CKR_QUIT : CKR_OK;
1527 } else if (strcmp(cmp, "<=") == 0) {
1528 return (a <= b) ? CKR_QUIT : CKR_OK;
1529 } else if (strcmp(cmp, ">=") == 0) {
1530 return (a >= b) ? CKR_QUIT : CKR_OK;
1531 } else if (strcmp(cmp, "=") == 0) {
1532 return (a == b) ? CKR_QUIT : CKR_OK;
1533 } else if (strcmp(cmp, "!=") == 0) {
1534 return (a != b) ? CKR_QUIT : CKR_OK;
1535 }
1536 printf("Unkown integer comparator: '%s'\n", cmp);
1537 return CKR_ARGUMENTS_BAD;
1538 }
1540 static CK_RV
1541 quitIfString(const char *a, const char *cmp, const char *b)
1542 {
1544 if (strcmp(cmp, "=") == 0) {
1545 return (strcmp(a,b) == 0) ? CKR_QUIT : CKR_OK;
1546 } else if (strcmp(cmp, "!=") == 0) {
1547 return (strcmp(a,b) != 0) ? CKR_QUIT : CKR_OK;
1548 }
1549 printf("Unkown string comparator: '%s'\n", cmp);
1550 return CKR_ARGUMENTS_BAD;
1551 }
1553 CK_RV run(const char *);
1554 CK_RV timeCommand(const char *);
1555 CK_RV loop(const char *filename, const char *var,
1556 CK_ULONG start, CK_ULONG end, CK_ULONG step) ;
1558 /*
1559 * Actually dispatch the function... Bad things happen
1560 * if these don't match the commands array.
1561 */
1562 CK_RV
1563 do_func(int index, Value **a)
1564 {
1565 int value, helpIndex;
1566 static Module module = { NULL, NULL} ;
1567 CK_FUNCTION_LIST *func = module.functionList;
1569 switch (commands[index].fType) {
1570 case F_C_Initialize:
1571 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1572 return func->C_Initialize((void *)a[0]->data);
1573 case F_C_Finalize:
1574 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1575 return func->C_Finalize((void *)a[0]->data);
1576 case F_C_GetInfo:
1577 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1578 return func->C_GetInfo((CK_INFO *)a[0]->data);
1579 case F_C_GetFunctionList:
1580 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1581 return func->C_GetFunctionList((CK_FUNCTION_LIST **)a[0]->data);
1582 case F_C_GetSlotList:
1583 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1584 return func->C_GetSlotList((CK_BBOOL)*(CK_ULONG *)a[0]->data,
1585 (CK_SLOT_ID *)a[1]->data,
1586 (CK_ULONG *)a[2]->data);
1587 case F_C_GetSlotInfo:
1588 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1589 return func->C_GetSlotInfo(*(CK_ULONG *)a[0]->data,
1590 (CK_SLOT_INFO *)a[1]->data);
1591 case F_C_GetTokenInfo:
1592 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1593 return func->C_GetTokenInfo(*(CK_ULONG *)a[0]->data,
1594 (CK_TOKEN_INFO *)a[1]->data);
1595 case F_C_GetMechanismList:
1596 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1597 if (a[1]->data) {
1598 a[1]->constType = ConstMechanism;
1599 }
1600 return func->C_GetMechanismList(*(CK_ULONG *)a[0]->data,
1601 (CK_MECHANISM_TYPE*)a[1]->data,
1602 (CK_ULONG *)a[2]->data);
1603 case F_C_GetMechanismInfo:
1604 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1605 return func->C_GetMechanismInfo(*(CK_ULONG *)a[0]->data,
1606 *(CK_ULONG *)a[1]->data,
1607 (CK_MECHANISM_INFO *)a[2]->data);
1608 case F_C_InitToken:
1609 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1610 return func->C_InitToken(*(CK_ULONG *)a[0]->data,
1611 (CK_CHAR *)a[1]->data,
1612 *(CK_ULONG *)a[2]->data,
1613 (CK_CHAR *)a[3]->data);
1614 case F_C_InitPIN:
1615 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1616 return func->C_InitPIN(*(CK_ULONG *)a[0]->data,
1617 (CK_CHAR *)a[1]->data,
1618 *(CK_ULONG *)a[2]->data);
1619 case F_C_SetPIN:
1620 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1621 return func->C_SetPIN(*(CK_ULONG *)a[0]->data,
1622 (CK_CHAR *)a[1]->data,
1623 *(CK_ULONG *)a[2]->data,
1624 (CK_CHAR *)a[3]->data,
1625 *(CK_ULONG *)a[4]->data);
1626 case F_C_OpenSession:
1627 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1628 return func->C_OpenSession(*(CK_ULONG *)a[0]->data,
1629 *(CK_ULONG *)a[1]->data,
1630 (void *)NULL,
1631 (CK_NOTIFY) NULL,
1632 (CK_ULONG *)a[2]->data);
1633 case F_C_CloseSession:
1634 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1635 return func->C_CloseSession(*(CK_ULONG *)a[0]->data);
1636 case F_C_CloseAllSessions:
1637 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1638 return func->C_CloseAllSessions(*(CK_ULONG *)a[0]->data);
1639 case F_C_GetSessionInfo:
1640 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1641 return func->C_GetSessionInfo(*(CK_ULONG *)a[0]->data,
1642 (CK_SESSION_INFO *)a[1]->data);
1643 case F_C_GetOperationState:
1644 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1645 return func->C_GetOperationState(*(CK_ULONG *)a[0]->data,
1646 (CK_BYTE *)a[1]->data,
1647 (CK_ULONG *)a[2]->data);
1648 case F_C_SetOperationState:
1649 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1650 return func->C_SetOperationState(*(CK_ULONG *)a[0]->data,
1651 (CK_CHAR *)a[1]->data,
1652 *(CK_ULONG *)a[2]->data,
1653 *(CK_ULONG *)a[3]->data,
1654 *(CK_ULONG *)a[4]->data);
1655 case F_C_Login:
1656 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1657 return func->C_Login(*(CK_ULONG *)a[0]->data,
1658 *(CK_ULONG *)a[1]->data,
1659 (CK_CHAR *)a[2]->data,
1660 *(CK_ULONG *)a[3]->data);
1661 case F_C_Logout:
1662 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1663 return func->C_Logout(*(CK_ULONG *)a[0]->data);
1664 case F_C_CreateObject:
1665 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1666 return func->C_CreateObject(*(CK_ULONG *)a[0]->data,
1667 (CK_ATTRIBUTE *)a[1]->data,
1668 *(CK_ULONG *)a[2]->data,
1669 (CK_ULONG *)a[3]->data);
1670 case F_C_CopyObject:
1671 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1672 return func->C_CopyObject(*(CK_ULONG *)a[0]->data,
1673 *(CK_ULONG *)a[0]->data,
1674 (CK_ATTRIBUTE *)a[1]->data,
1675 *(CK_ULONG *)a[2]->data,
1676 (CK_ULONG *)a[3]->data);
1677 case F_C_DestroyObject:
1678 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1679 return func->C_DestroyObject(*(CK_ULONG *)a[0]->data,
1680 *(CK_ULONG *)a[1]->data);
1681 case F_C_GetObjectSize:
1682 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1683 return func->C_GetObjectSize(*(CK_ULONG *)a[0]->data,
1684 *(CK_ULONG *)a[1]->data,
1685 (CK_ULONG *)a[2]->data);
1686 case F_C_GetAttributeValue:
1687 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1688 return func->C_GetAttributeValue(*(CK_ULONG *)a[0]->data,
1689 *(CK_ULONG *)a[1]->data,
1690 (CK_ATTRIBUTE *)a[2]->data,
1691 *(CK_ULONG *)a[3]->data);
1692 case F_C_SetAttributeValue:
1693 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1694 return func->C_SetAttributeValue(*(CK_ULONG *)a[0]->data,
1695 *(CK_ULONG *)a[1]->data,
1696 (CK_ATTRIBUTE *)a[2]->data,
1697 *(CK_ULONG *)a[3]->data);
1698 case F_C_FindObjectsInit:
1699 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1700 return func->C_FindObjectsInit(*(CK_ULONG *)a[0]->data,
1701 (CK_ATTRIBUTE *)a[1]->data,
1702 *(CK_ULONG *)a[2]->data);
1703 case F_C_FindObjects:
1704 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1705 return func->C_FindObjects(*(CK_ULONG *)a[0]->data,
1706 (CK_ULONG *)a[1]->data,
1707 *(CK_ULONG *)a[2]->data,
1708 (CK_ULONG *)a[3]->data);
1709 case F_C_FindObjectsFinal:
1710 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1711 return func->C_FindObjectsFinal(*(CK_ULONG *)a[0]->data);
1712 case F_C_EncryptInit:
1713 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1714 return func->C_EncryptInit(*(CK_ULONG *)a[0]->data,
1715 (CK_MECHANISM *)a[1]->data,
1716 *(CK_ULONG *)a[2]->data);
1717 case F_C_Encrypt:
1718 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1719 return func->C_Encrypt(*(CK_ULONG *)a[0]->data,
1720 (CK_CHAR *)a[1]->data,
1721 *(CK_ULONG *)a[2]->data,
1722 (CK_CHAR *)a[3]->data,
1723 (CK_ULONG *)a[4]->data);
1724 case F_C_EncryptUpdate:
1725 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1726 return func->C_EncryptUpdate(*(CK_ULONG *)a[0]->data,
1727 (CK_CHAR *)a[1]->data,
1728 *(CK_ULONG *)a[2]->data,
1729 (CK_CHAR *)a[3]->data,
1730 (CK_ULONG *)a[4]->data);
1731 case F_C_EncryptFinal:
1732 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1733 return func->C_EncryptFinal(*(CK_ULONG *)a[0]->data,
1734 (CK_CHAR *)a[1]->data,
1735 (CK_ULONG *)a[2]->data);
1736 case F_C_DecryptInit:
1737 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1738 return func->C_DecryptInit(*(CK_ULONG *)a[0]->data,
1739 (CK_MECHANISM *)a[1]->data,
1740 *(CK_ULONG *)a[2]->data);
1741 case F_C_Decrypt:
1742 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1743 return func->C_Decrypt(*(CK_ULONG *)a[0]->data,
1744 (CK_CHAR *)a[1]->data,
1745 *(CK_ULONG *)a[2]->data,
1746 (CK_CHAR *)a[3]->data,
1747 (CK_ULONG *)a[4]->data);
1748 case F_C_DecryptUpdate:
1749 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1750 return func->C_DecryptUpdate(*(CK_ULONG *)a[0]->data,
1751 (CK_CHAR *)a[1]->data,
1752 *(CK_ULONG *)a[2]->data,
1753 (CK_CHAR *)a[3]->data,
1754 (CK_ULONG *)a[4]->data);
1755 case F_C_DecryptFinal:
1756 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1757 return func->C_DecryptFinal(*(CK_ULONG *)a[0]->data,
1758 (CK_CHAR *)a[1]->data,
1759 (CK_ULONG *)a[2]->data);
1760 case F_C_DigestInit:
1761 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1762 return func->C_DigestInit(*(CK_ULONG *)a[0]->data,
1763 (CK_MECHANISM *)a[1]->data);
1764 case F_C_Digest:
1765 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1766 return func->C_Digest(*(CK_ULONG *)a[0]->data,
1767 (CK_CHAR *)a[1]->data,
1768 *(CK_ULONG *)a[2]->data,
1769 (CK_CHAR *)a[3]->data,
1770 (CK_ULONG *)a[4]->data);
1771 case F_C_DigestUpdate:
1772 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1773 return func->C_DigestUpdate(*(CK_ULONG *)a[0]->data,
1774 (CK_CHAR *)a[1]->data,
1775 *(CK_ULONG *)a[2]->data);
1776 case F_C_DigestKey:
1777 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1778 return func->C_DigestKey(*(CK_ULONG *)a[0]->data,
1779 *(CK_ULONG *)a[1]->data);
1780 case F_C_DigestFinal:
1781 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1782 return func->C_DigestFinal(*(CK_ULONG *)a[0]->data,
1783 (CK_CHAR *)a[1]->data,
1784 (CK_ULONG *)a[2]->data);
1785 case F_C_SignInit:
1786 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1787 return func->C_SignInit(*(CK_ULONG *)a[0]->data,
1788 (CK_MECHANISM *)a[1]->data,
1789 *(CK_ULONG *)a[2]->data);
1790 case F_C_Sign:
1791 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1792 return func->C_Sign(*(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_SignUpdate:
1798 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1799 return func->C_SignUpdate(*(CK_ULONG *)a[0]->data,
1800 (CK_CHAR *)a[1]->data,
1801 *(CK_ULONG *)a[2]->data);
1802 case F_C_SignFinal:
1803 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1804 return func->C_SignFinal(*(CK_ULONG *)a[0]->data,
1805 (CK_CHAR *)a[1]->data,
1806 (CK_ULONG *)a[2]->data);
1808 case F_C_SignRecoverInit:
1809 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1810 return func->C_SignRecoverInit(*(CK_ULONG *)a[0]->data,
1811 (CK_MECHANISM *)a[1]->data,
1812 *(CK_ULONG *)a[2]->data);
1813 case F_C_SignRecover:
1814 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1815 return func->C_SignRecover(*(CK_ULONG *)a[0]->data,
1816 (CK_CHAR *)a[1]->data,
1817 *(CK_ULONG *)a[2]->data,
1818 (CK_CHAR *)a[3]->data,
1819 (CK_ULONG *)a[4]->data);
1820 case F_C_VerifyInit:
1821 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1822 return func->C_VerifyInit(*(CK_ULONG *)a[0]->data,
1823 (CK_MECHANISM *)a[1]->data,
1824 *(CK_ULONG *)a[2]->data);
1825 case F_C_Verify:
1826 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1827 return func->C_Verify(*(CK_ULONG *)a[0]->data,
1828 (CK_CHAR *)a[1]->data,
1829 *(CK_ULONG *)a[2]->data,
1830 (CK_CHAR *)a[3]->data,
1831 *(CK_ULONG *)a[4]->data);
1832 case F_C_VerifyUpdate:
1833 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1834 return func->C_VerifyUpdate(*(CK_ULONG *)a[0]->data,
1835 (CK_CHAR *)a[1]->data,
1836 *(CK_ULONG *)a[2]->data);
1837 case F_C_VerifyFinal:
1838 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1839 return func->C_VerifyFinal(*(CK_ULONG *)a[0]->data,
1840 (CK_CHAR *)a[1]->data,
1841 *(CK_ULONG *)a[2]->data);
1843 case F_C_VerifyRecoverInit:
1844 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1845 return func->C_VerifyRecoverInit(*(CK_ULONG *)a[0]->data,
1846 (CK_MECHANISM *)a[1]->data,
1847 *(CK_ULONG *)a[2]->data);
1848 case F_C_VerifyRecover:
1849 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1850 return func->C_VerifyRecover(*(CK_ULONG *)a[0]->data,
1851 (CK_CHAR *)a[1]->data,
1852 *(CK_ULONG *)a[2]->data,
1853 (CK_CHAR *)a[3]->data,
1854 (CK_ULONG *)a[4]->data);
1855 case F_C_DigestEncryptUpdate:
1856 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1857 return func->C_DigestEncryptUpdate(*(CK_ULONG *)a[0]->data,
1858 (CK_CHAR *)a[1]->data,
1859 *(CK_ULONG *)a[2]->data,
1860 (CK_CHAR *)a[3]->data,
1861 (CK_ULONG *)a[4]->data);
1862 case F_C_DecryptDigestUpdate:
1863 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1864 return func->C_DecryptDigestUpdate(*(CK_ULONG *)a[0]->data,
1865 (CK_CHAR *)a[1]->data,
1866 *(CK_ULONG *)a[2]->data,
1867 (CK_CHAR *)a[3]->data,
1868 (CK_ULONG *)a[4]->data);
1869 case F_C_SignEncryptUpdate:
1870 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1871 return func->C_SignEncryptUpdate(*(CK_ULONG *)a[0]->data,
1872 (CK_CHAR *)a[1]->data,
1873 *(CK_ULONG *)a[2]->data,
1874 (CK_CHAR *)a[3]->data,
1875 (CK_ULONG *)a[4]->data);
1876 case F_C_DecryptVerifyUpdate:
1877 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1878 return func->C_DecryptVerifyUpdate(*(CK_ULONG *)a[0]->data,
1879 (CK_CHAR *)a[1]->data,
1880 *(CK_ULONG *)a[2]->data,
1881 (CK_CHAR *)a[3]->data,
1882 (CK_ULONG *)a[4]->data);
1883 case F_C_GenerateKey:
1884 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1885 return func->C_GenerateKey(*(CK_ULONG *)a[0]->data,
1886 (CK_MECHANISM *)a[1]->data,
1887 (CK_ATTRIBUTE *)a[2]->data,
1888 *(CK_ULONG *)a[3]->data,
1889 (CK_ULONG *)a[4]->data);
1890 case F_C_GenerateKeyPair:
1891 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1892 return func->C_GenerateKeyPair(*(CK_ULONG *)a[0]->data,
1893 (CK_MECHANISM *)a[1]->data,
1894 (CK_ATTRIBUTE *)a[2]->data,
1895 *(CK_ULONG *)a[3]->data,
1896 (CK_ATTRIBUTE *)a[4]->data,
1897 *(CK_ULONG *)a[5]->data,
1898 (CK_ULONG *)a[6]->data,
1899 (CK_ULONG *)a[7]->data);
1900 case F_C_WrapKey:
1901 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1902 return func->C_WrapKey(*(CK_ULONG *)a[0]->data,
1903 (CK_MECHANISM *)a[1]->data,
1904 *(CK_ULONG *)a[2]->data,
1905 *(CK_ULONG *)a[3]->data,
1906 (CK_CHAR *)a[5]->data,
1907 (CK_ULONG *)a[6]->data);
1908 case F_C_UnwrapKey:
1909 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1910 return func->C_UnwrapKey(*(CK_ULONG *)a[0]->data,
1911 (CK_MECHANISM *)a[1]->data,
1912 *(CK_ULONG *)a[2]->data,
1913 (CK_CHAR *)a[3]->data,
1914 *(CK_ULONG *)a[4]->data,
1915 (CK_ATTRIBUTE *)a[5]->data,
1916 *(CK_ULONG *)a[6]->data,
1917 (CK_ULONG *)a[7]->data);
1918 case F_C_DeriveKey:
1919 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1920 return func->C_DeriveKey (*(CK_ULONG *)a[0]->data,
1921 (CK_MECHANISM *)a[1]->data,
1922 *(CK_ULONG *)a[2]->data,
1923 (CK_ATTRIBUTE *)a[3]->data,
1924 *(CK_ULONG *)a[4]->data,
1925 (CK_ULONG *)a[5]->data);
1926 case F_C_SeedRandom:
1927 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1928 return func->C_SeedRandom(*(CK_ULONG *)a[0]->data,
1929 (CK_CHAR *)a[1]->data,
1930 *(CK_ULONG *)a[2]->data);
1931 case F_C_GenerateRandom:
1932 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1933 return func->C_GenerateRandom(*(CK_ULONG *)a[0]->data,
1934 (CK_CHAR *)a[1]->data,
1935 *(CK_ULONG *)a[2]->data);
1936 case F_C_GetFunctionStatus:
1937 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1938 return func->C_GetFunctionStatus(*(CK_ULONG *)a[0]->data);
1939 case F_C_CancelFunction:
1940 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1941 return func->C_CancelFunction(*(CK_ULONG *)a[0]->data);
1942 case F_C_WaitForSlotEvent:
1943 if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1944 return func->C_WaitForSlotEvent(*(CK_ULONG *)a[0]->data,
1945 (CK_ULONG *)a[1]->data,
1946 (void *)a[2]->data);
1947 /* set a variable */
1948 case F_SetVar:
1949 case F_SetStringVar:
1950 (void) DeleteVariable(a[0]->data);
1951 (void) AddVariable(a[0]->data,&a[1]);
1952 return CKR_OK;
1953 /* print a value */
1954 case F_Print:
1955 return printArg(a[0],0);
1956 case F_SaveVar:
1957 return save(a[0]->data,a[1]);
1958 case F_RestoreVar:
1959 return restore(a[0]->data,a[1]);
1960 case F_Delete:
1961 return DeleteVariable(a[0]->data);
1962 case F_Increment:
1963 return increment(a[0], *(CK_ULONG *)a[1]->data);
1964 case F_Decrement:
1965 return decrement(a[0], *(CK_ULONG *)a[1]->data);
1966 case F_List:
1967 return list();
1968 case F_Run:
1969 return run(a[0]->data);
1970 case F_Time:
1971 return timeCommand(a[0]->data);
1972 case F_Load:
1973 return loadModule(&module,a[0]->data);
1974 case F_Unload:
1975 return unloadModule(&module);
1976 case F_NewArray:
1977 (void) DeleteVariable(a[0]->data);
1978 return ArrayVariable(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data);
1979 case F_NewTemplate:
1980 (void) DeleteVariable(a[0]->data);
1981 return ArrayTemplate(a[0]->data,a[1]->data);
1982 case F_BuildTemplate:
1983 return BuildTemplate(a[0]);
1984 case F_SetTemplate:
1985 return SetTemplate(a[0],
1986 *(CK_ULONG *)a[1]->data,
1987 *(CK_ULONG *)a[2]->data);
1988 case F_NewMechanism:
1989 (void) DeleteVariable(a[0]->data);
1990 return NewMechanism(a[0]->data,*(CK_ULONG *)a[1]->data);
1991 case F_NewInitializeArgs:
1992 (void) DeleteVariable(a[0]->data);
1993 return NewInitializeArgs(a[0]->data,*(CK_ULONG *)a[1]->data,a[2]->data);
1994 case F_System:
1995 value = *(int *)a[0]->data;
1996 if (value & 0x80000000) {
1997 systemFlags &= ~value;
1998 } else {
1999 systemFlags |= value;
2000 }
2001 return CKR_OK;
2002 case F_Loop:
2003 return loop(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data,
2004 *(CK_ULONG *)a[3]->data,*(CK_ULONG *)a[4]->data);
2005 case F_Help:
2006 if (a[0]) {
2007 helpIndex = lookup(a[0]->data);
2008 if (helpIndex < 0) {
2009 return printTopicHelp(a[0]->data);
2010 }
2011 printHelp(helpIndex, 1);
2012 return CKR_OK;
2013 }
2014 return printGeneralHelp();
2015 case F_QuitIfString:
2016 return quitIfString(a[0]->data,a[1]->data,a[2]->data);
2017 case F_QuitIf:
2018 return quitIf(*(CK_ULONG *)a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data);
2019 case F_Quit:
2020 return CKR_QUIT;
2021 default:
2022 fprintf(stderr,
2023 "Function %s not yet supported\n",commands[index].fname );
2024 return CKR_OK;
2025 }
2026 /* Not Reached */
2027 return CKR_OK;
2028 }
2030 CK_RV
2031 processCommand(const char * buf)
2032 {
2033 CK_RV error = CKR_OK;
2034 int index;
2035 const char *bp;
2036 Value **arglist;
2038 bp = strip(buf);
2039 /* allow comments and blank lines in scripts */
2040 if ((*bp == '#') || (*bp == 0) || (*bp == '\n')){
2041 return CKR_OK;
2042 }
2044 index = lookup(bp);
2046 if (index < 0) {
2047 return CKR_OK;
2048 }
2050 arglist = parseArgs(index,bp);
2051 if (arglist == NULL) {
2052 return CKR_OK;
2053 }
2055 error = do_func(index,arglist);
2056 if (error == CKR_OK) {
2057 putOutput(arglist);
2058 } else if (error != CKR_QUIT) {
2059 printf(">> Error : ");
2060 printConst(error, ConstResult, 1);
2061 }
2063 parseFree(arglist);
2064 return error;
2065 }
2067 CK_RV
2068 timeCommand(const char *command)
2069 {
2070 CK_RV ckrv;
2071 PRIntervalTime startTime = PR_IntervalNow();
2072 PRIntervalTime endTime;
2073 PRIntervalTime elapsedTime;
2075 ckrv = processCommand(command);
2077 endTime = PR_IntervalNow();
2078 elapsedTime = endTime - startTime;
2079 printf("Time -- %d msec \n",
2080 PR_IntervalToMilliseconds(elapsedTime));
2082 return ckrv;
2083 }
2087 CK_RV
2088 process(FILE *inFile,int user)
2089 {
2090 char buf[2048];
2091 CK_RV error;
2092 CK_RV ckrv = CKR_OK;
2094 if (user) { printf("pkcs11> "); fflush(stdout); }
2096 while (fgets(buf,2048,inFile) != NULL) {
2098 if (!user) printf("* %s",buf);
2099 error = processCommand(buf);
2100 if (error == CKR_QUIT) {
2101 break;
2102 } else if (error != CKR_OK) {
2103 ckrv = error;
2104 }
2105 if (user) {
2106 printf("pkcs11> "); fflush(stdout);
2107 }
2108 }
2109 return ckrv;
2110 }
2112 CK_RV
2113 run(const char *filename)
2114 {
2115 FILE *infile;
2116 CK_RV ckrv;
2118 infile = fopen(filename,"r");
2120 if (infile == NULL) {
2121 perror(filename);
2122 return CKR_FUNCTION_FAILED;
2123 }
2125 ckrv = process(infile, 0);
2127 fclose(infile);
2128 return ckrv;
2129 }
2131 CK_RV
2132 loop(const char *filename, const char *var,
2133 CK_ULONG start, CK_ULONG end, CK_ULONG step)
2134 {
2135 CK_ULONG i = 0;
2136 Value *value = 0;
2137 CK_RV ckrv;
2139 for (i=start; i < end; i += step)
2140 {
2141 value = NewValue(ArgULong, 1);
2142 *(CK_ULONG *)value->data = i;
2143 DeleteVariable(var);
2144 AddVariable(var, &value);
2145 ckrv = run(filename);
2146 argFree(value);
2147 if (ckrv == CKR_QUIT) {
2148 break;
2149 }
2150 }
2151 return ckrv;
2152 }
2154 int
2155 main(int argc, char **argv)
2156 {
2157 /* I suppose that some day we could parse some arguments */
2158 (void) process(stdin, 1);
2159 return 0;
2160 }