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
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.
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 ***** */
48 #include <sys/fcntl.h>
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
);
81 return (c
>= '0' && c
<= '9');
85 isConst(const char *c
)
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.
99 isSize(char *var
, int *isArray
)
105 if (PL_strncasecmp(var
,"sizeof(",/*)*/ 7) == 0) {
107 } else if (PL_strncasecmp(var
,"size(",/*)*/ 5) == 0) {
109 } else if (PL_strncasecmp(var
,"sizeofarray(",/*)*/ 12) == 0) {
112 } else if (PL_strncasecmp(var
,"sizea(",/*)*/ 6) == 0) {
118 end
= strchr(ptr
,/*(*/ ')') ;
122 if (isArray
) *isArray
= array
;
128 printConst(CK_ULONG value
, ConstType type
, int newLine
)
132 for (i
=0; i
< constCount
; i
++) {
133 if (consts
[i
].type
== type
&& consts
[i
].value
== value
) {
134 printf("%s",consts
[i
].name
);
137 if (type
== ConstNone
&& consts
[i
].value
== value
) {
138 printf("%s",consts
[i
].name
);
142 if (i
== constCount
) {
143 if ((type
== ConstAvailableSizes
) || (type
== ConstCurrentSize
)) {
146 printf("Unknown %s (%lu:0x%lx)",constTypeString
[type
],value
,value
);
155 getConstFromAttribute(CK_ATTRIBUTE_TYPE type
)
159 for (i
=0; i
< constCount
; i
++) {
160 if (consts
[i
].type
== ConstAttribute
&& consts
[i
].value
== type
) {
161 return consts
[i
].attrType
;
168 printChars(const char *name
, CK_ULONG size
)
171 for (i
=0; i
< size
; i
++) {
175 printf("%c",name
[i
]);
181 void printDump(const unsigned char *buf
, int size
)
185 for(i
=0; i
< size
; i
+= DUMP_LEN
) {
187 for (j
=0; j
< DUMP_LEN
; j
++) {
189 printf("%02x ",buf
[i
+j
]);
194 for (j
=0; j
< DUMP_LEN
; j
++) {
196 if (buf
[i
+j
] < ' ' || buf
[i
+j
] >= 0x7f) {
199 printf("%c",buf
[i
+j
]);
210 * free an argument structure
213 argFreeData(Value
*arg
)
215 if (arg
->data
&& ((arg
->type
& ArgStatic
) == 0)) {
216 if ((arg
->type
& ArgMask
) == ArgAttribute
) {
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
);
232 arg
->type
&= ~ArgStatic
;
239 if (arg
== NULL
) return;
242 if (arg
->reference
== 0) {
243 if (arg
->type
& ArgFile
) {
252 * free and argument list
255 parseFree(Value
**ap
)
258 for (i
=0 ; i
< MAX_ARGS
; i
++) {
264 * getEnd: how for to the end of this argmument list?
267 getEnd(const char *bp
)
272 if (*bp
== ' ' || *bp
== '\t' || *bp
== '\n') return count
;
281 * strip: return the first none white space character
284 strip(const char *bp
)
286 while (*bp
&& (*bp
== ' ' || *bp
== '\t' || *bp
== '\n')) bp
++;
291 * read in the next argument into dp ... don't overflow
294 readChars(const char *bp
, char *dp
, int max
)
298 if (*bp
== ' ' || *bp
== '\t' || *bp
== '\n' ) {
303 if (++count
== max
) break;
305 while (*bp
&& (*bp
!= ' ' && *bp
!= '\t' && *bp
!= '\n')) bp
++;
310 Value
* varLookup(const char *bp
, char *vname
, int max
, int *error
);
313 getValue(const char *v
, int *error
)
315 Value
* varVal
= NULL
;
322 varVal
= varLookup( v
, tvar
, sizeof(tvar
), error
);
325 if ((varVal
->type
& ArgMask
) == ArgULong
) {
326 retVal
= *(CK_ULONG
*)varVal
->data
;
328 fprintf(stderr
,"%s: is not a ulong\n", v
);
334 constLookup(v
, &retVal
, &type
);
339 NewValue(ArgType type
, CK_ULONG arraySize
)
343 value
= (Value
*)malloc(sizeof(Value
));
344 if (!value
) return NULL
;
345 value
->size
= ArgSize(type
)*arraySize
;
347 value
->filename
= NULL
;
348 value
->constType
= ConstNone
;
349 value
->data
= (void *)malloc(value
->size
);
354 value
->reference
= 1;
355 value
->arraySize
= (type
== ArgChar
) ? 1: arraySize
;
357 memset(value
->data
, 0, value
->size
);
361 #define INVALID_INDEX 0xffffffff
364 handleArray(char *vname
, int *error
)
367 CK_ULONG index
= INVALID_INDEX
;
369 if ((bracket
= strchr(vname
,'[')) != 0) {
370 char *tmpv
= bracket
+1;
372 bracket
= strchr(tmpv
,']');
375 fprintf(stderr
,"%s: missing closing brace\n", vname
);
376 return INVALID_INDEX
;
380 index
= getValue(tmpv
, error
);
382 return INVALID_INDEX
;
383 } else if (index
== INVALID_INDEX
) {
384 fprintf(stderr
, "%s: 0x%x is an invalid index\n",vname
,index
);
392 makeArrayTarget(const char *vname
, const Value
*value
, CK_ULONG index
)
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
);
403 target
= (char *)value
->data
;
404 elementSize
= value
->size
/value
->arraySize
;
405 target
+= index
* elementSize
;
410 * look up a variable from the variable chain
412 static Variable
*varHead
= NULL
;
414 varLookup(const char *bp
, char *vname
, int max
, int *error
)
417 CK_ULONG index
= INVALID_INDEX
;
423 readChars(bp
, vname
, max
);
426 /* don't make numbers into variables */
427 if (isNum(vname
[0])) {
431 if (isConst(vname
)) {
434 /* handle sizeof() */
435 if ((ptr
= isSize(vname
, &isArray
)) != NULL
) {
437 Value
*targetValue
= NULL
;
438 Value
*sourceValue
= varLookup(NULL
, ptr
, 0, error
);
441 /* just didn't find it */
443 fprintf(stderr
,"Couldn't find variable %s to take size of\n",
448 size
= isArray
? sourceValue
->arraySize
: sourceValue
->size
;
449 targetValue
= NewValue(ArgULong
,1);
450 memcpy(targetValue
->data
, &size
, sizeof(size
));
456 index
= handleArray(vname
, error
);
461 for (current
= varHead
; current
; current
= current
->next
) {
462 if (PL_strcasecmp(current
->vname
, vname
) == 0) {
464 if (index
== INVALID_INDEX
) {
465 (current
->value
->reference
)++;
466 return current
->value
;
468 target
= makeArrayTarget(vname
, current
->value
, index
);
470 Value
*element
= NewValue(current
->value
->type
, 1);
472 fprintf(stderr
, "MEMORY ERROR!\n");
475 argFreeData(element
);
476 element
->data
= target
;
477 element
->type
|= ArgStatic
;
493 printf(" %10s\t%16s\t%8s\tSize\tElements\n","Name","Type","Const");
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
);
508 printFlags(const char *s
, CK_ULONG flags
, ConstType type
)
514 for (i
=1; i
; i
=i
<< 1) {
516 printf("%s",needComma
?",":"");
517 printConst(i
, type
, 0);
529 * add a new variable to the chain
532 AddVariable(const char *bp
, Value
**ptr
)
536 int index
= INVALID_INDEX
;
540 bp
= readChars(bp
,vname
,sizeof(vname
));
542 /* don't make numbers into variables */
543 if (isNum(vname
[0])) {
547 if (isConst(vname
)) {
555 if (isSize(vname
, NULL
)) {
558 /* arrays values should be written back to the original */
559 index
= handleArray(vname
, &error
);
565 for (current
= varHead
; current
; current
= current
->next
) {
566 if (PL_strcasecmp(current
->vname
,vname
) == 0) {
568 /* found a complete object, return the found one */
569 if (index
== INVALID_INDEX
) {
571 *ptr
= current
->value
;
574 /* found an array, update the array element */
575 target
= makeArrayTarget(vname
, current
->value
, index
);
577 memcpy(target
, (*ptr
)->data
, (*ptr
)->size
);
579 (*ptr
)->data
= target
;
580 (*ptr
)->type
|= ArgStatic
;
586 /* we are looking for an array and didn't find one */
587 if (index
!= INVALID_INDEX
) {
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
;
599 current
->next
= varHead
;
605 FindTypeByName(const char *typeName
)
609 for (i
=0; i
< valueCount
; i
++) {
610 if (PL_strcasecmp(typeName
,valueString
[i
]) == 0) {
613 if (valueString
[i
][0] == 'C' && valueString
[i
][1] == 'K' &&
614 valueString
[i
][2] == '_' &&
615 (PL_strcasecmp(typeName
,&valueString
[i
][3]) == 0)) {
623 ArrayVariable(const char *bp
, const char *typeName
, CK_ULONG count
)
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
);
638 #define MAX_TEMPLATE 25
641 ArrayTemplate(const char *bp
, char *attributes
)
644 CK_ULONG attributeTypes
[MAX_TEMPLATE
];
645 CK_ATTRIBUTE
*template;
646 Value
*value
; /* new Value */
650 memcpy(aname
,attributes
,strlen(attributes
)+1);
652 for (ap
= aname
, count
= 0; ap
&& *ap
&& count
< MAX_TEMPLATE
; count
++) {
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
);
679 BuildTemplate(Value
*vp
)
681 CK_ATTRIBUTE
*template = (CK_ATTRIBUTE
*)vp
->data
;
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
);
694 SetTemplate(Value
*vp
, CK_ULONG index
, CK_ULONG value
)
696 CK_ATTRIBUTE
*template = (CK_ATTRIBUTE
*)vp
->data
;
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
;
722 *(CK_BBOOL
*)template[index
].pValue
= (CK_BBOOL
) value
;
724 *(CK_ULONG
*)template[index
].pValue
= (CK_ULONG
) value
;
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
);
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
;
754 if (strcmp(param
, "null") != 0) {
755 init
->LibraryParameters
= (CK_CHAR_PTR
*)strdup(param
);
757 (void) AddVariable(bp
, &value
);
762 * add a new variable to the chain
765 DeleteVariable(const char *bp
)
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
;
783 * convert an octal value to integer
791 if ((*o
>= '0') && (*o
<= '7')) {
792 value
= (value
<< 3) | (unsigned)(*o
- '0');
801 * convert a hex value to integer
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');
824 * look up or decode a constant value
827 constLookup(const char *bp
, CK_ULONG
*value
, ConstType
*type
)
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
;
844 if (vname
[0] == '0' && vname
[1] == 'X') {
845 *value
= htoi(&vname
[2]);
846 } else if (vname
[0] == '0') {
847 *value
= otoi(&vname
[1]);
849 *value
= atoi(vname
);
855 ArgSize(ArgType type
)
865 size
= sizeof(CK_ULONG
);
868 size
= 1; /* get's changed later */
875 size
= sizeof(CK_INFO
);
878 size
= sizeof(CK_SLOT_INFO
);
881 size
= sizeof(CK_TOKEN_INFO
);
884 size
= sizeof(CK_SESSION_INFO
);
887 size
= sizeof(CK_ATTRIBUTE
);
890 size
= sizeof(CK_MECHANISM
);
892 case ArgMechanismInfo
:
893 size
= sizeof(CK_MECHANISM_INFO
);
895 case ArgInitializeArgs
:
896 size
= sizeof(CK_C_INITIALIZE_ARGS
);
898 case ArgFunctionList
:
899 size
= sizeof(CK_FUNCTION_LIST
);
909 restore(const char *filename
,Value
*ptr
)
913 fd
= open(filename
,O_RDONLY
|O_BINARY
);
916 return CKR_FUNCTION_FAILED
;
919 size
= read(fd
,ptr
->data
,ptr
->size
);
920 if (systemFlags
& FLAG_VerifyFile
) {
921 printDump(ptr
->data
,ptr
->size
);
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
;
936 save(const char *filename
,Value
*ptr
)
940 fd
= open(filename
,O_WRONLY
|O_BINARY
|O_CREAT
,0666);
943 return CKR_FUNCTION_FAILED
;
946 size
= write(fd
,ptr
->data
,ptr
->size
);
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
;
960 increment(Value
*ptr
, CK_ULONG value
)
962 if ((ptr
->type
& ArgMask
) != ArgULong
) {
963 return CKR_ARGUMENTS_BAD
;
965 *(CK_ULONG
*)ptr
->data
+= value
;
970 decrement(Value
*ptr
, CK_ULONG value
)
972 if ((ptr
->type
& ArgMask
) != ArgULong
) {
973 return CKR_ARGUMENTS_BAD
;
975 *(CK_ULONG
*)ptr
->data
-= value
;
980 printArg(Value
*ptr
,int arg_number
)
982 ArgType type
= ptr
->type
& ArgMask
;
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
;
996 printf("Arg %d: \n",arg_number
);
998 if (ptr
->arraySize
> 1) {
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);
1018 if (ptr
->data
== NULL
) {
1019 printf(" NULL ptr to a %s\n", valueString
[type
]);
1027 printf(" %lu (0x%lx)\n", *((CK_ULONG
*)ptr
->data
),
1028 *((CK_ULONG
*)ptr
->data
));
1029 if (ptr
->constType
!= ConstNone
) {
1031 printConst(*(CK_ULONG
*)ptr
->data
,ptr
->constType
,1);
1035 printf(" %s\n",(char *)ptr
->data
);
1038 printf(" %s\n",(char *)ptr
->data
);
1041 printDump(ptr
->data
,ptr
->size
);
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
));
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
));
1069 tokenInfo
= (CK_TOKEN_INFO
*)ptr
->data
;
1071 printChars(tokenInfo
->label
,sizeof(tokenInfo
->label
));
1072 printf(" Manufacturer ID: ");
1073 printChars(tokenInfo
->manufacturerID
,sizeof(tokenInfo
->manufacturerID
));
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
));
1104 case ArgSessionInfo
:
1105 sessionInfo
= (CK_SESSION_INFO
*)ptr
->data
;
1106 printf(" SlotID: 0x%08lx\n", sessionInfo
->slotID
);
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
);
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
) {
1120 printf("Attribute Len: %lu\n",attribute
->ulValueLen
);
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);
1130 printDump(attribute
->pValue
, attribute
->ulValueLen
);
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
);
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
);
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
));
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
1165 ckrv
= CKR_ARGUMENTS_BAD
;
1174 * Feeling ambitious? turn this whole thing into lexx yacc parser
1175 * with full expressions.
1178 parseArgs(int index
, const char * bp
)
1180 const Commands
*cp
= &commands
[index
];
1181 int size
= strlen(cp
->fname
);
1185 Value
**argList
,*possible
;
1186 ConstType constType
;
1189 * skip pass the command
1191 if ((cp
->fname
[0] == 'C') && (cp
->fname
[1] == '_') && (bp
[1] != '_')) {
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
;
1212 /* if we hit ArgNone, we've nabbed all the arguments we need */
1213 if (type
== ArgNone
) {
1217 /* if we run out of space in the line, we weren't given enough
1220 /* we're into optional arguments, ok to quit now */
1221 if (cp
->args
[i
] & ArgOpt
) {
1224 fprintf(stderr
,"%s: only %d args found,\n",cp
->fname
,i
);
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
);
1239 * look up the argument in our variable list first... only
1240 * exception is the new argument type for set...
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
);
1257 * ... that is as long as they are big enough...
1259 if (ArgSize(type
) > possible
->size
) {
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
));
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
;
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",
1295 argFreeData(argList
[i
]);
1296 argList
[i
]->data
= NULL
;
1297 argList
[i
]->size
= 0;
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 /* ) */ ) {
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;
1313 argList
[i
]->filename
= (char *)malloc(size
+1);
1315 PL_strcpy(argList
[i
]->filename
,filename
);
1317 argList
[i
]->type
|= ArgOut
|ArgFile
;
1320 bp
= AddVariable(bp
,&argList
[i
]);
1321 argList
[i
]->type
|= ArgOut
;
1325 if (PL_strncasecmp(bp
, "file(", 5) == 0 /* ) */ ) {
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
) {
1343 bp
= constLookup(bp
, &value
, &constType
);
1344 *(int *)argList
[i
]->data
= value
;
1345 argList
[i
]->constType
= constType
;
1348 argFreeData(argList
[i
]);
1349 size
= getEnd(bp
)+1;
1350 argList
[i
]->data
= (void *)malloc(size
);
1351 argList
[i
]->size
= size
;
1356 case ArgSessionInfo
:
1359 case ArgMechanismInfo
:
1360 case ArgInitializeArgs
:
1363 bp
= readChars(bp
,(char *)argList
[i
]->data
,argList
[i
]->size
);
1373 /* lookup the command in the array */
1375 lookup(const char *buf
)
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) {
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)) {
1397 fprintf(stderr
,"Can't find command %s\n",buf
);
1402 putOutput(Value
**ptr
)
1406 for (i
=0; i
< MAX_ARGS
; i
++) {
1409 if (ptr
[i
] == NULL
) break;
1411 type
= ptr
[i
]->type
;
1413 ptr
[i
]->type
&= ~ArgOut
;
1414 if (type
== ArgNone
) {
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 */
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
;
1446 loadModule(Module
*module
, char *library
)
1448 PRLibrary
*newLibrary
;
1449 CK_C_GetFunctionList getFunctionList
;
1450 CK_FUNCTION_LIST
*functionList
;
1453 newLibrary
= PR_LoadLibrary(library
);
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
) {
1470 if (module
->library
) {
1471 PR_UnloadLibrary(module
->library
);
1474 module
->library
= newLibrary
;
1475 module
->functionList
= functionList
;
1481 printHelp(int index
, int full
)
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
) {
1490 printf(" %s", valueString
[type
]);
1493 printf(" %s\n",commands
[index
].helpString
);
1496 /* add Topical help here ! */
1498 printTopicHelp(char *topic
)
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) {
1515 if (i
== topicCount
) {
1516 fprintf(stderr
,"Can't find topic '%s'\n", topic
);
1517 return CKR_DATA_INVALID
;
1520 printf(" %s", topic
);
1522 printf(" %s\n",topics
[i
].helpString
);
1527 printGeneralHelp(void)
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
);
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
);
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
;
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.
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
);
1600 if (!func
) return CKR_CRYPTOKI_NOT_INITIALIZED
;
1601 return func
->C_Finalize((void *)a
[0]->data
);
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
;
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
);
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
);
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
);
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
,
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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 */
1975 case F_SetStringVar
:
1976 (void) DeleteVariable(a
[0]->data
);
1977 (void) AddVariable(a
[0]->data
,&a
[1]);
1981 return printArg(a
[0],0);
1983 return save(a
[0]->data
,a
[1]);
1985 return restore(a
[0]->data
,a
[1]);
1987 return DeleteVariable(a
[0]->data
);
1989 return increment(a
[0], *(CK_ULONG
*)a
[1]->data
);
1991 return decrement(a
[0], *(CK_ULONG
*)a
[1]->data
);
1995 return run(a
[0]->data
);
1997 return timeCommand(a
[0]->data
);
1999 return loadModule(&module
,a
[0]->data
);
2001 return unloadModule(&module
);
2003 (void) DeleteVariable(a
[0]->data
);
2004 return ArrayVariable(a
[0]->data
,a
[1]->data
,*(CK_ULONG
*)a
[2]->data
);
2006 (void) DeleteVariable(a
[0]->data
);
2007 return ArrayTemplate(a
[0]->data
,a
[1]->data
);
2008 case F_BuildTemplate
:
2009 return BuildTemplate(a
[0]);
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
);
2021 value
= *(int *)a
[0]->data
;
2022 if (value
& 0x80000000) {
2023 systemFlags
&= ~value
;
2025 systemFlags
|= value
;
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
);
2033 helpIndex
= lookup(a
[0]->data
);
2034 if (helpIndex
< 0) {
2035 return printTopicHelp(a
[0]->data
);
2037 printHelp(helpIndex
, 1);
2040 return printGeneralHelp();
2041 case F_QuitIfString
:
2042 return quitIfString(a
[0]->data
,a
[1]->data
,a
[2]->data
);
2044 return quitIf(*(CK_ULONG
*)a
[0]->data
,a
[1]->data
,*(CK_ULONG
*)a
[2]->data
);
2049 "Function %s not yet supported\n",commands
[index
].fname
);
2057 processCommand(const char * buf
)
2059 CK_RV error
= CKR_OK
;
2065 /* allow comments and blank lines in scripts */
2066 if ((*bp
== '#') || (*bp
== 0) || (*bp
== '\n')){
2076 arglist
= parseArgs(index
,bp
);
2077 if (arglist
== NULL
) {
2081 error
= do_func(index
,arglist
);
2082 if (error
== CKR_OK
) {
2084 } else if (error
!= CKR_QUIT
) {
2085 printf(">> Error : ");
2086 printConst(error
, ConstResult
, 1);
2094 timeCommand(const char *command
)
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
));
2114 process(FILE *inFile
,int user
)
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
) {
2128 } else if (error
!= CKR_OK
) {
2132 printf("pkcs11> "); fflush(stdout
);
2139 run(const char *filename
)
2144 infile
= fopen(filename
,"r");
2146 if (infile
== NULL
) {
2148 return CKR_FUNCTION_FAILED
;
2151 ckrv
= process(infile
, 0);
2158 loop(const char *filename
, const char *var
,
2159 CK_ULONG start
, CK_ULONG end
, CK_ULONG step
)
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
);
2173 if (ckrv
== CKR_QUIT
) {
2181 main(int argc
, char **argv
)
2183 /* I suppose that some day we could parse some arguments */
2184 (void) process(stdin
, 1);