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>
69
#define FLAG_NEGATE 0x80000000
70
#define FLAG_Verify 0x00000001
71
#define FLAG_VerifyFile 0x00000002
72
#define CKR_QUIT 0x80000000
74
int ArgSize(ArgType type);
75
const char *constLookup(const char *bp, CK_ULONG *value, ConstType *type);
80
return (c >= '0' && c <= '9');
84
isConst(const char *c)
89
constLookup(c, &value, &type);
90
return type != ConstNone;
94
* see if the variable is really a 'size' function. This
95
* function may modify var if it is a size function.
98
isSize(char *var, int *isArray)
104
if (PL_strncasecmp(var,"sizeof(",/*)*/ 7) == 0) {
106
} else if (PL_strncasecmp(var,"size(",/*)*/ 5) == 0) {
108
} else if (PL_strncasecmp(var,"sizeofarray(",/*)*/ 12) == 0) {
111
} else if (PL_strncasecmp(var,"sizea(",/*)*/ 6) == 0) {
117
end = strchr(ptr,/*(*/ ')') ;
121
if (isArray) *isArray = array;
127
printConst(CK_ULONG value, ConstType type, int newLine)
131
for (i=0; i < constCount; i++) {
132
if (consts[i].type == type && consts[i].value == value) {
133
printf("%s",consts[i].name);
136
if (type == ConstNone && consts[i].value == value) {
137
printf("%s",consts[i].name);
141
if (i == constCount) {
142
if ((type == ConstAvailableSizes) || (type == ConstCurrentSize)) {
145
printf("Unknown %s (%lu:0x%lx)",constTypeString[type],value,value);
154
getConstFromAttribute(CK_ATTRIBUTE_TYPE type)
158
for (i=0; i < constCount; i++) {
159
if (consts[i].type == ConstAttribute && consts[i].value == type) {
160
return consts[i].attrType;
167
printChars(const char *name, CK_ULONG size)
170
for (i=0; i < size; i++) {
174
printf("%c",name[i]);
180
void printDump(const unsigned char *buf, int size)
184
for(i=0; i < size; i+= DUMP_LEN) {
186
for (j=0; j< DUMP_LEN; j++) {
188
printf("%02x ",buf[i+j]);
193
for (j=0; j< DUMP_LEN; j++) {
195
if (buf[i+j] < ' ' || buf[i+j] >= 0x7f) {
198
printf("%c",buf[i+j]);
209
* free an argument structure
212
argFreeData(Value *arg)
214
if (arg->data && ((arg->type & ArgStatic) == 0)) {
215
if ((arg->type & ArgMask) == ArgAttribute) {
217
CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)arg->data;
219
for (i=0; i < arg->arraySize; i++) {
220
free(template[i].pValue);
223
if ((arg->type & ArgMask) == ArgInitializeArgs) {
224
CK_C_INITIALIZE_ARGS *init = (CK_C_INITIALIZE_ARGS *)arg->data;
225
if (init->LibraryParameters) {
226
free(init->LibraryParameters);
231
arg->type &= ~ArgStatic;
238
if (arg == NULL) return;
241
if (arg->reference == 0) {
242
if (arg->type & ArgFile) {
251
* free and argument list
254
parseFree(Value **ap)
257
for (i=0 ; i < MAX_ARGS; i++) {
263
* getEnd: how for to the end of this argmument list?
266
getEnd(const char *bp)
271
if (*bp == ' ' || *bp == '\t' || *bp == '\n') return count;
280
* strip: return the first none white space character
283
strip(const char *bp)
285
while (*bp && (*bp == ' ' || *bp == '\t' || *bp == '\n')) bp++;
290
* read in the next argument into dp ... don't overflow
293
readChars(const char *bp, char *dp, int max )
297
if (*bp == ' ' || *bp == '\t' || *bp == '\n' ) {
302
if (++count == max) break;
304
while (*bp && (*bp != ' ' && *bp != '\t' && *bp != '\n')) bp++;
309
Value * varLookup(const char *bp, char *vname, int max, int *error);
312
getValue(const char *v, int *error)
314
Value * varVal = NULL;
321
varVal = varLookup( v, tvar, sizeof(tvar), error);
324
if ((varVal->type & ArgMask) == ArgULong) {
325
retVal = *(CK_ULONG *)varVal->data;
327
fprintf(stderr,"%s: is not a ulong\n", v);
333
constLookup(v, &retVal, &type);
338
NewValue(ArgType type, CK_ULONG arraySize)
342
value = (Value *)malloc(sizeof(Value));
343
if (!value) return NULL;
344
value->size = ArgSize(type)*arraySize;
346
value->filename = NULL;
347
value->constType = ConstNone;
348
value->data = (void *)malloc(value->size);
353
value->reference = 1;
354
value->arraySize = (type == ArgChar) ? 1: arraySize;
356
memset(value->data, 0, value->size);
360
#define INVALID_INDEX 0xffffffff
363
handleArray(char *vname, int *error)
366
CK_ULONG index = INVALID_INDEX;
368
if ((bracket = strchr(vname,'[')) != 0) {
369
char *tmpv = bracket+1;
371
bracket = strchr(tmpv,']');
374
fprintf(stderr,"%s: missing closing brace\n", vname);
375
return INVALID_INDEX;
379
index = getValue(tmpv, error);
381
return INVALID_INDEX;
382
} else if (index == INVALID_INDEX) {
383
fprintf(stderr, "%s: 0x%x is an invalid index\n",vname,index);
391
makeArrayTarget(const char *vname, const Value *value, CK_ULONG index)
394
CK_ULONG elementSize;
396
if (index >= (CK_ULONG)value->arraySize) {
397
fprintf(stderr, "%s[%d]: index larger than array size (%d)\n",
398
vname, index, value->arraySize);
402
target = (char *)value->data;
403
elementSize = value->size/value->arraySize;
404
target += index * elementSize;
409
* look up a variable from the variable chain
411
static Variable *varHead = NULL;
413
varLookup(const char *bp, char *vname, int max, int *error)
416
CK_ULONG index = INVALID_INDEX;
422
readChars(bp, vname, max);
425
/* don't make numbers into variables */
426
if (isNum(vname[0])) {
430
if (isConst(vname)) {
433
/* handle sizeof() */
434
if ((ptr = isSize(vname, &isArray)) != NULL) {
436
Value *targetValue = NULL;
437
Value *sourceValue = varLookup(NULL, ptr, 0, error);
440
/* just didn't find it */
442
fprintf(stderr,"Couldn't find variable %s to take size of\n",
447
size = isArray ? sourceValue->arraySize : sourceValue->size;
448
targetValue = NewValue(ArgULong,1);
449
memcpy(targetValue->data, &size, sizeof(size));
455
index = handleArray(vname, error);
460
for (current = varHead; current; current = current->next) {
461
if (PL_strcasecmp(current->vname, vname) == 0) {
463
if (index == INVALID_INDEX) {
464
(current->value->reference)++;
465
return current->value;
467
target = makeArrayTarget(vname, current->value, index);
469
Value *element = NewValue(current->value->type, 1);
471
fprintf(stderr, "MEMORY ERROR!\n");
474
argFreeData(element);
475
element->data = target;
476
element->type |= ArgStatic;
492
printf(" %10s\t%16s\t%8s\tSize\tElements\n","Name","Type","Const");
494
printf(" no variables set\n");
497
for (current = varHead; current; current = current->next) {
498
printf(" %10s\t%16s\t%8s\t%d\t%d\n", current->vname,
499
valueString[current->value->type&ArgMask],
500
constTypeString[current->value->constType],
501
current->value->size, current->value->arraySize);
507
printFlags(const char *s, CK_ULONG flags, ConstType type)
513
for (i=1; i ; i=i << 1) {
515
printf("%s",needComma?",":"");
516
printConst(i, type, 0);
528
* add a new variable to the chain
531
AddVariable(const char *bp, Value **ptr)
535
int index = INVALID_INDEX;
539
bp = readChars(bp,vname,sizeof(vname));
541
/* don't make numbers into variables */
542
if (isNum(vname[0])) {
546
if (isConst(vname)) {
554
if (isSize(vname, NULL)) {
557
/* arrays values should be written back to the original */
558
index = handleArray(vname, &error);
564
for (current = varHead; current; current = current->next) {
565
if (PL_strcasecmp(current->vname,vname) == 0) {
567
/* found a complete object, return the found one */
568
if (index == INVALID_INDEX) {
570
*ptr = current->value;
573
/* found an array, update the array element */
574
target = makeArrayTarget(vname, current->value, index);
576
memcpy(target, (*ptr)->data, (*ptr)->size);
578
(*ptr)->data = target;
579
(*ptr)->type |= ArgStatic;
585
/* we are looking for an array and didn't find one */
586
if (index != INVALID_INDEX) {
591
current = (Variable *)malloc(sizeof(Variable));
592
size = strlen(vname);
593
current->vname = (char *)malloc(size+1);
594
strcpy(current->vname,vname);
595
current->value = *ptr;
598
current->next = varHead;
604
FindTypeByName(const char *typeName)
608
for (i=0; i < valueCount; i++) {
609
if (PL_strcasecmp(typeName,valueString[i]) == 0) {
612
if (valueString[i][0] == 'C' && valueString[i][1] == 'K' &&
613
valueString[i][2] == '_' &&
614
(PL_strcasecmp(typeName,&valueString[i][3]) == 0)) {
622
ArrayVariable(const char *bp, const char *typeName, CK_ULONG count)
625
Value *value; /* new Value */
627
type = FindTypeByName(typeName);
628
if (type == ArgNone) {
629
fprintf(stderr,"Invalid type (%s)\n", typeName);
630
return CKR_FUNCTION_FAILED;
632
value = NewValue(type, count);
633
(void) AddVariable(bp, &value);
637
#define MAX_TEMPLATE 25
640
ArrayTemplate(const char *bp, char *attributes)
643
CK_ULONG attributeTypes[MAX_TEMPLATE];
644
CK_ATTRIBUTE *template;
645
Value *value; /* new Value */
649
memcpy(aname,attributes,strlen(attributes)+1);
651
for (ap = aname, count = 0; ap && *ap && count < MAX_TEMPLATE; count++) {
660
(void)constLookup(cur, &attributeTypes[count], &type);
661
if ((type != ConstAttribute) && (type != ConstNone)) {
662
fprintf(stderr, "Unknown Attribute %s\n", cur);
663
return CKR_FUNCTION_FAILED;
667
value = NewValue(ArgAttribute, count);
669
template = (CK_ATTRIBUTE *)value->data;
670
for (i=0; i < count ; i++) {
671
template[i].type = attributeTypes[i];
673
(void) AddVariable(bp, &value);
678
BuildTemplate(Value *vp)
680
CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data;
683
for (i=0; i < vp->arraySize; i++) {
684
if (((signed long)template[i].ulValueLen) > 0) {
685
if (template[i].pValue) free(template[i].pValue);
686
template[i].pValue = malloc(template[i].ulValueLen);
693
SetTemplate(Value *vp, CK_ULONG index, CK_ULONG value)
695
CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data;
700
if (index >= (CK_ULONG) vp->arraySize) {
701
fprintf(stderr,"index (%lu) greater than array (%d)\n",
702
index, vp->arraySize);
703
return CKR_ARGUMENTS_BAD;
705
attrType = getConstFromAttribute(template[index].type);
707
if (attrType == ConstNone) {
708
fprintf(stderr,"can't set index (%lu) because ", index);
709
printConst(template[index].type,ConstAttribute, 0);
710
fprintf(stderr, " is not a CK_BBOOL or CK_ULONG\n");
711
return CKR_ARGUMENTS_BAD;
713
isbool = (attrType == ConstBool);
714
len = isbool ? sizeof (CK_BBOOL) : sizeof(CK_ULONG);
715
if ((template[index].ulValueLen != len) || (template[index].pValue)) {
716
free(template[index].pValue);
717
template[index].pValue = malloc(len);
718
template[index].ulValueLen = len;
721
*(CK_BBOOL *)template[index].pValue = (CK_BBOOL) value;
723
*(CK_ULONG *)template[index].pValue = (CK_ULONG) value;
730
NewMechanism(const char *bp, CK_ULONG mechType)
732
Value *value; /* new Value */
733
CK_MECHANISM *mechanism;
735
value = NewValue(ArgMechanism, 1);
736
mechanism = (CK_MECHANISM *)value->data;
737
mechanism->mechanism = mechType;
738
mechanism->pParameter = NULL;
739
mechanism->ulParameterLen = 0;
740
(void) AddVariable(bp, &value);
745
NewInitializeArgs(const char *bp, CK_ULONG flags, const char *param)
747
Value *value; /* new Value */
748
CK_C_INITIALIZE_ARGS *init;
750
value = NewValue(ArgInitializeArgs, 1);
751
init = (CK_C_INITIALIZE_ARGS *)value->data;
753
if (strcmp(param, "null") != 0) {
754
init->LibraryParameters = (CK_CHAR_PTR *)strdup(param);
756
(void) AddVariable(bp, &value);
761
* add a new variable to the chain
764
DeleteVariable(const char *bp)
769
bp = readChars(bp,vname,sizeof(vname));
771
for (current = &varHead; *current; current = &(*current)->next) {
772
if (PL_strcasecmp((*current)->vname,vname) == 0) {
773
argFree((*current)->value);
774
*current = (*current)->next;
782
* convert an octal value to integer
790
if ((*o >= '0') && (*o <= '7')) {
791
value = (value << 3) | (unsigned)(*o - '0');
800
* convert a hex value to integer
808
if ((*x >= '0') && (*x <= '9')) {
809
value = (value << 4) | (unsigned)(*x - '0');
810
} else if ((*x >= 'a') && (*x <= 'f')) {
811
value = (value << 4) | (unsigned)(*x - 'a');
812
} else if ((*x >= 'A') && (*x <= 'F')) {
813
value = (value << 4) | (unsigned)(*x - 'A');
823
* look up or decode a constant value
826
constLookup(const char *bp, CK_ULONG *value, ConstType *type)
831
bp = readChars(bp,vname,sizeof(vname));
833
for (i=0; i < constCount; i++) {
834
if ((PL_strcasecmp(consts[i].name,vname) == 0) ||
835
PL_strcasecmp(consts[i].name+5,vname) == 0) {
836
*value = consts[i].value;
837
*type = consts[i].type;
843
if (vname[0] == '0' && vname[1] == 'X') {
844
*value = htoi(&vname[2]);
845
} else if (vname[0] == '0') {
846
*value = otoi(&vname[1]);
848
*value = atoi(vname);
854
ArgSize(ArgType type)
864
size = sizeof(CK_ULONG);
867
size = 1; /* get's changed later */
874
size = sizeof(CK_INFO);
877
size = sizeof(CK_SLOT_INFO);
880
size = sizeof(CK_TOKEN_INFO);
883
size = sizeof(CK_SESSION_INFO);
886
size = sizeof(CK_ATTRIBUTE);
889
size = sizeof(CK_MECHANISM);
891
case ArgMechanismInfo:
892
size = sizeof(CK_MECHANISM_INFO);
894
case ArgInitializeArgs:
895
size = sizeof(CK_C_INITIALIZE_ARGS);
897
case ArgFunctionList:
898
size = sizeof(CK_FUNCTION_LIST);
908
restore(const char *filename,Value *ptr)
912
fd = open(filename,O_RDONLY|O_BINARY);
915
return CKR_FUNCTION_FAILED;
918
size = read(fd,ptr->data,ptr->size);
919
if (systemFlags & FLAG_VerifyFile) {
920
printDump(ptr->data,ptr->size);
924
return CKR_FUNCTION_FAILED;
925
} else if (size != ptr->size) {
926
fprintf(stderr,"%s: only read %d bytes, needed to read %d bytes\n",
927
filename,size,ptr->size);
928
return CKR_FUNCTION_FAILED;
935
save(const char *filename,Value *ptr)
939
fd = open(filename,O_WRONLY|O_BINARY|O_CREAT,0666);
942
return CKR_FUNCTION_FAILED;
945
size = write(fd,ptr->data,ptr->size);
948
return CKR_FUNCTION_FAILED;
949
} else if (size != ptr->size) {
950
fprintf(stderr,"%s: only wrote %d bytes, need to write %d bytes\n",
951
filename,size,ptr->size);
952
return CKR_FUNCTION_FAILED;
959
increment(Value *ptr, CK_ULONG value)
961
if ((ptr->type & ArgMask) != ArgULong) {
962
return CKR_ARGUMENTS_BAD;
964
*(CK_ULONG *)ptr->data += value;
969
decrement(Value *ptr, CK_ULONG value)
971
if ((ptr->type & ArgMask) != ArgULong) {
972
return CKR_ARGUMENTS_BAD;
974
*(CK_ULONG *)ptr->data -= value;
979
printArg(Value *ptr,int arg_number)
981
ArgType type = ptr->type & ArgMask;
983
CK_SLOT_INFO *slotInfo;
984
CK_TOKEN_INFO *tokenInfo;
985
CK_SESSION_INFO *sessionInfo;
986
CK_ATTRIBUTE *attribute;
987
CK_MECHANISM *mechanism;
988
CK_MECHANISM_INFO *mechanismInfo;
989
CK_C_INITIALIZE_ARGS *initArgs;
990
CK_FUNCTION_LIST *functionList;
995
printf("Arg %d: \n",arg_number);
997
if (ptr->arraySize > 1) {
1000
int elementSize = ptr->size/ptr->arraySize;
1001
char *dp = (char *)ptr->data;
1003
/* build a temporary Value to hold a single element */
1004
element.type = type;
1005
element.constType = ptr->constType;
1006
element.size = elementSize;
1007
element.filename = ptr->filename;
1008
element.reference = 1;
1009
element.arraySize = 1;
1010
for (i=0; i < ptr->arraySize; i++) {
1011
printf(" -----[ %d ] -----\n", i);
1012
element.data = (void *) &dp[i*elementSize];
1013
(void) printArg(&element, 0);
1017
if (ptr->data == NULL) {
1018
printf(" NULL ptr to a %s\n", valueString[type]);
1026
printf(" %lu (0x%lx)\n", *((CK_ULONG *)ptr->data),
1027
*((CK_ULONG *)ptr->data));
1028
if (ptr->constType != ConstNone) {
1030
printConst(*(CK_ULONG *)ptr->data,ptr->constType,1);
1034
printf(" %s\n",(char *)ptr->data);
1037
printf(" %s\n",(char *)ptr->data);
1040
printDump(ptr->data,ptr->size);
1043
#define VERSION(x) (x).major, (x).minor
1044
info = (CK_INFO *)ptr->data;
1045
printf(" Cryptoki Version: %d.%02d\n",
1046
VERSION(info->cryptokiVersion));
1047
printf(" Manufacturer ID: ");
1048
printChars(info->manufacturerID,sizeof(info->manufacturerID));
1049
printFlags(" Flags: ", info->flags, ConstInfoFlags);
1050
printf(" Library Description: ");
1051
printChars(info->libraryDescription,sizeof(info->libraryDescription));
1052
printf(" Library Version: %d.%02d\n",
1053
VERSION(info->libraryVersion));
1056
slotInfo = (CK_SLOT_INFO *)ptr->data;
1057
printf(" Slot Description: ");
1058
printChars(slotInfo->slotDescription,sizeof(slotInfo->slotDescription));
1059
printf(" Manufacturer ID: ");
1060
printChars(slotInfo->manufacturerID,sizeof(slotInfo->manufacturerID));
1061
printFlags(" Flags: ", slotInfo->flags, ConstSlotFlags);
1062
printf(" Hardware Version: %d.%02d\n",
1063
VERSION(slotInfo->hardwareVersion));
1064
printf(" Firmware Version: %d.%02d\n",
1065
VERSION(slotInfo->firmwareVersion));
1068
tokenInfo = (CK_TOKEN_INFO *)ptr->data;
1070
printChars(tokenInfo->label,sizeof(tokenInfo->label));
1071
printf(" Manufacturer ID: ");
1072
printChars(tokenInfo->manufacturerID,sizeof(tokenInfo->manufacturerID));
1074
printChars(tokenInfo->model,sizeof(tokenInfo->model));
1075
printf(" Serial Number: ");
1076
printChars(tokenInfo->serialNumber,sizeof(tokenInfo->serialNumber));
1077
printFlags(" Flags: ", tokenInfo->flags, ConstTokenFlags);
1078
printf(" Max Session Count: ");
1079
printConst(tokenInfo->ulMaxSessionCount, ConstAvailableSizes, 1);
1080
printf(" Session Count: ");
1081
printConst(tokenInfo->ulSessionCount, ConstCurrentSize, 1);
1082
printf(" RW Session Count: ");
1083
printConst(tokenInfo->ulMaxRwSessionCount, ConstAvailableSizes, 1);
1084
printf(" Max Pin Length : ");
1085
printConst(tokenInfo->ulMaxPinLen, ConstCurrentSize, 1);
1086
printf(" Min Pin Length : ");
1087
printConst(tokenInfo->ulMinPinLen, ConstCurrentSize, 1);
1088
printf(" Total Public Memory: ");
1089
printConst(tokenInfo->ulTotalPublicMemory, ConstAvailableSizes, 1);
1090
printf(" Free Public Memory: ");
1091
printConst(tokenInfo->ulFreePublicMemory, ConstCurrentSize, 1);
1092
printf(" Total Private Memory: ");
1093
printConst(tokenInfo->ulTotalPrivateMemory, ConstAvailableSizes, 1);
1094
printf(" Free Private Memory: ");
1095
printConst(tokenInfo->ulFreePrivateMemory, ConstCurrentSize, 1);
1096
printf(" Hardware Version: %d.%02d\n",
1097
VERSION(tokenInfo->hardwareVersion));
1098
printf(" Firmware Version: %d.%02d\n",
1099
VERSION(tokenInfo->firmwareVersion));
1100
printf(" UTC Time: ");
1101
printChars(tokenInfo->utcTime,sizeof(tokenInfo->utcTime));
1103
case ArgSessionInfo:
1104
sessionInfo = (CK_SESSION_INFO *)ptr->data;
1105
printf(" SlotID: 0x%08lx\n", sessionInfo->slotID);
1107
printConst(sessionInfo->state, ConstSessionState, 1);
1108
printFlags(" Flags: ", sessionInfo->flags, ConstSessionFlags);
1109
printf(" Device error: %lu 0x%08lx\n",sessionInfo->ulDeviceError,
1110
sessionInfo->ulDeviceError);
1113
attribute = (CK_ATTRIBUTE *)ptr->data;
1114
printf(" Attribute Type: ");
1115
printConst(attribute->type, ConstAttribute, 1);
1116
printf(" Attribute Data: ");
1117
if (attribute->pValue == NULL) {
1119
printf("Attribute Len: %lu\n",attribute->ulValueLen);
1121
constType = getConstFromAttribute(attribute->type);
1122
if (constType != ConstNone) {
1123
CK_ULONG value = (constType == ConstBool) ?
1124
*(CK_BBOOL *)attribute->pValue :
1125
*(CK_ULONG *)attribute->pValue;
1126
printConst(value, constType, 1);
1129
printDump(attribute->pValue, attribute->ulValueLen);
1134
mechanism = (CK_MECHANISM *)ptr->data;
1135
printf(" Mechanism Type: ");
1136
printConst(mechanism->mechanism, ConstMechanism, 1);
1137
printf(" Mechanism Data:\n");
1138
printDump(mechanism->pParameter, mechanism->ulParameterLen);
1140
case ArgMechanismInfo:
1141
mechanismInfo = (CK_MECHANISM_INFO *)ptr->data;
1142
printf(" Minimum Key Size: %ld\n",mechanismInfo->ulMinKeySize);
1143
printf(" Maximum Key Size: %ld\n",mechanismInfo->ulMaxKeySize);
1144
printFlags(" Flags: ", mechanismInfo->flags, ConstMechanismFlags);
1146
case ArgInitializeArgs:
1147
initArgs = (CK_C_INITIALIZE_ARGS *)ptr->data;
1148
printFlags(" Flags: ", initArgs->flags, ConstInitializeFlags);
1149
if (initArgs->LibraryParameters) {
1150
printf("Params: %s\n",initArgs->LibraryParameters);
1152
case ArgFunctionList:
1153
functionList = (CK_FUNCTION_LIST *)ptr->data;
1154
printf(" Version: %d.%02d\n", VERSION(functionList->version));
1156
#undef CK_NEED_ARG_LIST
1157
#define CK_PKCS11_FUNCTION_INFO(func) \
1158
printf(" %s: 0x%08lx\n", #func, (unsigned long) functionList->func );
1159
#include "pkcs11f.h"
1160
#undef CK_NEED_ARG_LIST
1161
#undef CK_PKCS11_FUNCTION_INFO
1164
ckrv = CKR_ARGUMENTS_BAD;
1173
* Feeling ambitious? turn this whole thing into lexx yacc parser
1174
* with full expressions.
1177
parseArgs(int index, const char * bp)
1179
const Commands *cp = &commands[index];
1180
int size = strlen(cp->fname);
1184
Value **argList,*possible;
1185
ConstType constType;
1188
* skip pass the command
1190
if ((cp->fname[0] == 'C') && (cp->fname[1] == '_') && (bp[1] != '_')) {
1196
* Initialize our argument list
1198
argList = (Value **)malloc(sizeof(Value*)*MAX_ARGS);
1199
for (i=0; i < MAX_ARGS; i++) { argList[i] = NULL; }
1202
* Walk the argument list parsing it...
1204
for (i=0 ;i < MAX_ARGS; i++) {
1205
ArgType type = cp->args[i] & ArgMask;
1211
/* if we hit ArgNone, we've nabbed all the arguments we need */
1212
if (type == ArgNone) {
1216
/* if we run out of space in the line, we weren't given enough
1219
/* we're into optional arguments, ok to quit now */
1220
if (cp->args[i] & ArgOpt) {
1223
fprintf(stderr,"%s: only %d args found,\n",cp->fname,i);
1228
/* collect all the rest of the command line and send
1229
* it as a single argument */
1230
if (cp->args[i] & ArgFull) {
1231
int size = strlen(bp)+1;
1232
argList[i] = NewValue(type, size);
1233
memcpy(argList[i]->data, bp, size);
1238
* look up the argument in our variable list first... only
1239
* exception is the new argument type for set...
1242
if ((cp->args[i] != (ArgVar|ArgNew)) &&
1243
(possible = varLookup(bp,vname,sizeof(vname),&error))) {
1244
/* ints are only compatible with other ints... all other types
1245
* are interchangeable... */
1246
if (type != ArgVar) { /* ArgVar's match anyone */
1247
if ((type == ArgULong) ^
1248
((possible->type & ArgMask) == ArgULong)) {
1249
fprintf(stderr,"%s: Arg %d incompatible type with <%s>\n",
1250
cp->fname,i+1,vname);
1256
* ... that is as long as they are big enough...
1258
if (ArgSize(type) > possible->size) {
1260
"%s: Arg %d %s is too small (%d bytes needs to be %d bytes)\n",
1261
cp->fname,i+1,vname,possible->size,ArgSize(type));
1268
/* everything looks kosher here, use it */
1269
argList[i] = possible;
1271
bp = readChars(bp,vname,sizeof(vname));
1272
if (cp->args[i] & ArgOut) {
1273
possible->type |= ArgOut;
1283
/* create space for our argument */
1284
argList[i] = NewValue(type, 1);
1286
if ((PL_strncasecmp(bp, "null", 4) == 0) && ((bp[4] == 0)
1287
|| (bp[4] == ' ') || (bp[4] =='\t') || (bp[4] =='\n'))) {
1288
if (cp->args[i] == ArgULong) {
1289
fprintf(stderr, "%s: Arg %d CK_ULONG can't be NULL\n",
1294
argFreeData(argList[i]);
1295
argList[i]->data = NULL;
1296
argList[i]->size = 0;
1302
/* if we're an output variable, we need to add it */
1303
if (cp->args[i] & ArgOut) {
1304
if (PL_strncasecmp(bp,"file(",5) == 0 /* ) */ ) {
1306
bp = readChars(bp+5,filename,sizeof(filename));
1307
size = PL_strlen(filename);
1308
if ((size > 0) && (/* ( */filename[size-1] == ')')) {
1309
filename[size-1] = 0;
1312
argList[i]->filename = (char *)malloc(size+1);
1314
PL_strcpy(argList[i]->filename,filename);
1316
argList[i]->type |= ArgOut|ArgFile;
1319
bp = AddVariable(bp,&argList[i]);
1320
argList[i]->type |= ArgOut;
1324
if (PL_strncasecmp(bp, "file(", 5) == 0 /* ) */ ) {
1327
bp = readChars(bp+5,filename,sizeof(filename));
1328
size = PL_strlen(filename);
1329
if ((size > 0) && ( /* ( */ filename[size-1] == ')')) {
1330
filename[size-1] = 0;
1333
if (restore(filename,argList[i]) != CKR_OK) {
1342
bp = constLookup(bp, &value, &constType);
1343
*(int *)argList[i]->data = value;
1344
argList[i]->constType = constType;
1347
argFreeData(argList[i]);
1348
size = getEnd(bp)+1;
1349
argList[i]->data = (void *)malloc(size);
1350
argList[i]->size = size;
1355
case ArgSessionInfo:
1358
case ArgMechanismInfo:
1359
case ArgInitializeArgs:
1362
bp = readChars(bp,(char *)argList[i]->data,argList[i]->size);
1372
/* lookup the command in the array */
1374
lookup(const char *buf)
1379
buflen = PL_strlen(buf);
1381
for ( i = 0; i < commandCount; i++) {
1382
size = PL_strlen(commands[i].fname);
1384
if (size <= buflen) {
1385
if (PL_strncasecmp(buf,commands[i].fname,size) == 0) {
1389
if (size-2 <= buflen) {
1390
if (commands[i].fname[0] == 'C' && commands[i].fname[1] == '_' &&
1391
(PL_strncasecmp(buf,&commands[i].fname[2],size-2) == 0)) {
1396
fprintf(stderr,"Can't find command %s\n",buf);
1401
putOutput(Value **ptr)
1405
for (i=0; i < MAX_ARGS; i++) {
1408
if (ptr[i] == NULL) break;
1410
type = ptr[i]->type;
1412
ptr[i]->type &= ~ArgOut;
1413
if (type == ArgNone) {
1416
if (type & ArgOut) {
1417
(void) printArg(ptr[i],i+1);
1419
if (type & ArgFile) {
1420
save(ptr[i]->filename,ptr[i]);
1421
free(ptr[i]->filename);
1422
ptr[i]->filename= NULL; /* paranoia */
1428
unloadModule(Module *module)
1431
if (module->library) {
1432
PR_UnloadLibrary(module->library);
1435
module->library = NULL;
1436
module->functionList = NULL;
1442
loadModule(Module *module, char *library)
1444
PRLibrary *newLibrary;
1445
CK_C_GetFunctionList getFunctionList;
1446
CK_FUNCTION_LIST *functionList;
1449
newLibrary = PR_LoadLibrary(library);
1451
fprintf(stderr,"Couldn't load library %s\n",library);
1452
return CKR_FUNCTION_FAILED;
1454
getFunctionList = (CK_C_GetFunctionList)
1455
PR_FindSymbol(newLibrary,"C_GetFunctionList");
1456
if (!getFunctionList) {
1457
fprintf(stderr,"Couldn't find \"C_GetFunctionList\" in %s\n",library);
1458
return CKR_FUNCTION_FAILED;
1461
ckrv = (*getFunctionList)(&functionList);
1462
if (ckrv != CKR_OK) {
1466
if (module->library) {
1467
PR_UnloadLibrary(module->library);
1470
module->library = newLibrary;
1471
module->functionList = functionList;
1477
printHelp(int index, int full)
1480
printf(" %s", commands[index].fname);
1481
for (j=0; j < MAX_ARGS; j++) {
1482
ArgType type = commands[index].args[j] & ArgMask;
1483
if (type == ArgNone) {
1486
printf(" %s", valueString[type]);
1489
printf(" %s\n",commands[index].helpString);
1492
/* add Topical help here ! */
1494
printTopicHelp(char *topic)
1499
topicLen = PL_strlen(topic);
1501
for ( i = 0; i < topicCount; i++) {
1502
size = PL_strlen(topics[i].name);
1504
if (size <= topicLen) {
1505
if (PL_strncasecmp(topic,topics[i].name,size) == 0) {
1511
if (i == topicCount) {
1512
fprintf(stderr,"Can't find topic '%s'\n", topic);
1513
return CKR_DATA_INVALID;
1516
printf(" %s", topic);
1518
printf(" %s\n",topics[i].helpString);
1523
printGeneralHelp(void)
1526
printf(" To get help on commands, select from the list below:");
1527
for ( i = 0; i < commandCount; i++) {
1528
if (i % 5 == 0) printf("\n");
1529
printf("%s,", commands[i].fname);
1532
/* print help topics */
1533
printf(" To get help on a topic, select from the list below:");
1534
for ( i = 0; i < topicCount; i++) {
1535
if (i % 5 == 0) printf("\n");
1536
printf("%s,", topics[i].name);
1543
quitIf(CK_ULONG a, const char *cmp, CK_ULONG b)
1545
if (strcmp(cmp, "<") == 0) {
1546
return (a < b) ? CKR_QUIT : CKR_OK;
1547
} else if (strcmp(cmp, ">") == 0) {
1548
return (a > b) ? CKR_QUIT : CKR_OK;
1549
} else 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;
1558
printf("Unkown integer comparator: '%s'\n", cmp);
1559
return CKR_ARGUMENTS_BAD;
1563
quitIfString(const char *a, const char *cmp, const char *b)
1566
if (strcmp(cmp, "=") == 0) {
1567
return (strcmp(a,b) == 0) ? CKR_QUIT : CKR_OK;
1568
} else if (strcmp(cmp, "!=") == 0) {
1569
return (strcmp(a,b) != 0) ? CKR_QUIT : CKR_OK;
1571
printf("Unkown string comparator: '%s'\n", cmp);
1572
return CKR_ARGUMENTS_BAD;
1575
CK_RV run(const char *);
1576
CK_RV timeCommand(const char *);
1577
CK_RV loop(const char *filename, const char *var,
1578
CK_ULONG start, CK_ULONG end, CK_ULONG step) ;
1581
* Actually dispatch the function... Bad things happen
1582
* if these don't match the commands array.
1585
do_func(int index, Value **a)
1587
int value, helpIndex;
1588
static Module module = { NULL, NULL} ;
1589
CK_FUNCTION_LIST *func = module.functionList;
1591
switch (commands[index].fType) {
1592
case F_C_Initialize:
1593
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1594
return func->C_Initialize((void *)a[0]->data);
1596
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1597
return func->C_Finalize((void *)a[0]->data);
1599
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1600
return func->C_GetInfo((CK_INFO *)a[0]->data);
1601
case F_C_GetFunctionList:
1602
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1603
return func->C_GetFunctionList((CK_FUNCTION_LIST **)a[0]->data);
1604
case F_C_GetSlotList:
1605
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1606
return func->C_GetSlotList((CK_BBOOL)*(CK_ULONG *)a[0]->data,
1607
(CK_SLOT_ID *)a[1]->data,
1608
(CK_LONG *)a[2]->data);
1609
case F_C_GetSlotInfo:
1610
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1611
return func->C_GetSlotInfo(*(CK_ULONG *)a[0]->data,
1612
(CK_SLOT_INFO *)a[1]->data);
1613
case F_C_GetTokenInfo:
1614
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1615
return func->C_GetTokenInfo(*(CK_ULONG *)a[0]->data,
1616
(CK_TOKEN_INFO *)a[1]->data);
1617
case F_C_GetMechanismList:
1618
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1620
a[1]->constType = ConstMechanism;
1622
return func->C_GetMechanismList(*(CK_ULONG *)a[0]->data,
1623
(CK_MECHANISM_TYPE*)a[1]->data,
1624
(CK_ULONG *)a[2]->data);
1625
case F_C_GetMechanismInfo:
1626
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1627
return func->C_GetMechanismInfo(*(CK_ULONG *)a[0]->data,
1628
*(CK_ULONG *)a[1]->data,
1629
(CK_MECHANISM_INFO *)a[2]->data);
1631
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1632
return func->C_InitToken(*(CK_ULONG *)a[0]->data,
1633
(CK_CHAR *)a[1]->data,
1634
*(CK_ULONG *)a[2]->data,
1635
(CK_CHAR *)a[3]->data);
1637
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1638
return func->C_InitPIN(*(CK_ULONG *)a[0]->data,
1639
(CK_CHAR *)a[1]->data,
1640
*(CK_ULONG *)a[2]->data);
1642
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1643
return func->C_SetPIN(*(CK_ULONG *)a[0]->data,
1644
(CK_CHAR *)a[1]->data,
1645
*(CK_ULONG *)a[2]->data,
1646
(CK_CHAR *)a[3]->data,
1647
*(CK_ULONG *)a[4]->data);
1648
case F_C_OpenSession:
1649
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1650
return func->C_OpenSession(*(CK_ULONG *)a[0]->data,
1651
*(CK_ULONG *)a[1]->data,
1654
(CK_ULONG *)a[2]->data);
1655
case F_C_CloseSession:
1656
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1657
return func->C_CloseSession(*(CK_ULONG *)a[0]->data);
1658
case F_C_CloseAllSessions:
1659
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1660
return func->C_CloseAllSessions(*(CK_ULONG *)a[0]->data);
1661
case F_C_GetSessionInfo:
1662
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1663
return func->C_GetSessionInfo(*(CK_ULONG *)a[0]->data,
1664
(CK_SESSION_INFO *)a[1]->data);
1665
case F_C_GetOperationState:
1666
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1667
return func->C_GetOperationState(*(CK_ULONG *)a[0]->data,
1668
(CK_BYTE *)a[1]->data,
1669
(CK_ULONG *)a[2]->data);
1670
case F_C_SetOperationState:
1671
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1672
return func->C_SetOperationState(*(CK_ULONG *)a[0]->data,
1673
(CK_CHAR *)a[1]->data,
1674
*(CK_ULONG *)a[2]->data,
1675
*(CK_ULONG *)a[3]->data,
1676
*(CK_ULONG *)a[4]->data);
1678
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1679
return func->C_Login(*(CK_ULONG *)a[0]->data,
1680
*(CK_ULONG *)a[1]->data,
1681
(CK_CHAR *)a[2]->data,
1682
*(CK_ULONG *)a[3]->data);
1684
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1685
return func->C_Logout(*(CK_ULONG *)a[0]->data);
1686
case F_C_CreateObject:
1687
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1688
return func->C_CreateObject(*(CK_ULONG *)a[0]->data,
1689
(CK_ATTRIBUTE *)a[1]->data,
1690
*(CK_ULONG *)a[2]->data,
1691
(CK_ULONG *)a[3]->data);
1692
case F_C_CopyObject:
1693
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1694
return func->C_CopyObject(*(CK_ULONG *)a[0]->data,
1695
*(CK_ULONG *)a[0]->data,
1696
(CK_ATTRIBUTE *)a[1]->data,
1697
*(CK_ULONG *)a[2]->data,
1698
(CK_ULONG *)a[3]->data);
1699
case F_C_DestroyObject:
1700
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1701
return func->C_DestroyObject(*(CK_ULONG *)a[0]->data,
1702
*(CK_ULONG *)a[1]->data);
1703
case F_C_GetObjectSize:
1704
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1705
return func->C_GetObjectSize(*(CK_ULONG *)a[0]->data,
1706
*(CK_ULONG *)a[1]->data,
1707
(CK_ULONG *)a[2]->data);
1708
case F_C_GetAttributeValue:
1709
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1710
return func->C_GetAttributeValue(*(CK_ULONG *)a[0]->data,
1711
*(CK_ULONG *)a[1]->data,
1712
(CK_ATTRIBUTE *)a[2]->data,
1713
*(CK_ULONG *)a[3]->data);
1714
case F_C_SetAttributeValue:
1715
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1716
return func->C_SetAttributeValue(*(CK_ULONG *)a[0]->data,
1717
*(CK_ULONG *)a[1]->data,
1718
(CK_ATTRIBUTE *)a[2]->data,
1719
*(CK_ULONG *)a[3]->data);
1720
case F_C_FindObjectsInit:
1721
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1722
return func->C_FindObjectsInit(*(CK_ULONG *)a[0]->data,
1723
(CK_ATTRIBUTE *)a[1]->data,
1724
*(CK_ULONG *)a[2]->data);
1725
case F_C_FindObjects:
1726
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1727
return func->C_FindObjects(*(CK_ULONG *)a[0]->data,
1728
(CK_ULONG *)a[1]->data,
1729
*(CK_ULONG *)a[2]->data,
1730
(CK_ULONG *)a[3]->data);
1731
case F_C_FindObjectsFinal:
1732
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1733
return func->C_FindObjectsFinal(*(CK_ULONG *)a[0]->data);
1734
case F_C_EncryptInit:
1735
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1736
return func->C_EncryptInit(*(CK_ULONG *)a[0]->data,
1737
(CK_MECHANISM *)a[1]->data,
1738
*(CK_ULONG *)a[2]->data);
1740
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1741
return func->C_Encrypt(*(CK_ULONG *)a[0]->data,
1742
(CK_CHAR *)a[1]->data,
1743
*(CK_ULONG *)a[2]->data,
1744
(CK_CHAR *)a[3]->data,
1745
(CK_ULONG *)a[4]->data);
1746
case F_C_EncryptUpdate:
1747
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1748
return func->C_EncryptUpdate(*(CK_ULONG *)a[0]->data,
1749
(CK_CHAR *)a[1]->data,
1750
*(CK_ULONG *)a[2]->data,
1751
(CK_CHAR *)a[3]->data,
1752
(CK_ULONG *)a[4]->data);
1753
case F_C_EncryptFinal:
1754
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1755
return func->C_EncryptFinal(*(CK_ULONG *)a[0]->data,
1756
(CK_CHAR *)a[1]->data,
1757
(CK_ULONG *)a[2]->data);
1758
case F_C_DecryptInit:
1759
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1760
return func->C_DecryptInit(*(CK_ULONG *)a[0]->data,
1761
(CK_MECHANISM *)a[1]->data,
1762
*(CK_ULONG *)a[2]->data);
1764
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1765
return func->C_Decrypt(*(CK_ULONG *)a[0]->data,
1766
(CK_CHAR *)a[1]->data,
1767
*(CK_ULONG *)a[2]->data,
1768
(CK_CHAR *)a[3]->data,
1769
(CK_ULONG *)a[4]->data);
1770
case F_C_DecryptUpdate:
1771
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1772
return func->C_DecryptUpdate(*(CK_ULONG *)a[0]->data,
1773
(CK_CHAR *)a[1]->data,
1774
*(CK_ULONG *)a[2]->data,
1775
(CK_CHAR *)a[3]->data,
1776
(CK_ULONG *)a[4]->data);
1777
case F_C_DecryptFinal:
1778
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1779
return func->C_DecryptFinal(*(CK_ULONG *)a[0]->data,
1780
(CK_CHAR *)a[1]->data,
1781
(CK_ULONG *)a[2]->data);
1782
case F_C_DigestInit:
1783
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1784
return func->C_DigestInit(*(CK_ULONG *)a[0]->data,
1785
(CK_MECHANISM *)a[1]->data);
1787
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1788
return func->C_Digest(*(CK_ULONG *)a[0]->data,
1789
(CK_CHAR *)a[1]->data,
1790
*(CK_ULONG *)a[2]->data,
1791
(CK_CHAR *)a[3]->data,
1792
(CK_ULONG *)a[4]->data);
1793
case F_C_DigestUpdate:
1794
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1795
return func->C_DigestUpdate(*(CK_ULONG *)a[0]->data,
1796
(CK_CHAR *)a[1]->data,
1797
*(CK_ULONG *)a[2]->data);
1799
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1800
return func->C_DigestKey(*(CK_ULONG *)a[0]->data,
1801
*(CK_ULONG *)a[1]->data);
1802
case F_C_DigestFinal:
1803
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1804
return func->C_DigestFinal(*(CK_ULONG *)a[0]->data,
1805
(CK_CHAR *)a[1]->data,
1806
(CK_ULONG *)a[2]->data);
1808
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1809
return func->C_SignInit(*(CK_ULONG *)a[0]->data,
1810
(CK_MECHANISM *)a[1]->data,
1811
*(CK_ULONG *)a[2]->data);
1813
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1814
return func->C_Sign(*(CK_ULONG *)a[0]->data,
1815
(CK_CHAR *)a[1]->data,
1816
*(CK_ULONG *)a[2]->data,
1817
(CK_CHAR *)a[3]->data,
1818
(CK_ULONG *)a[4]->data);
1819
case F_C_SignUpdate:
1820
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1821
return func->C_SignUpdate(*(CK_ULONG *)a[0]->data,
1822
(CK_CHAR *)a[1]->data,
1823
*(CK_ULONG *)a[2]->data);
1825
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1826
return func->C_SignFinal(*(CK_ULONG *)a[0]->data,
1827
(CK_CHAR *)a[1]->data,
1828
(CK_ULONG *)a[2]->data);
1830
case F_C_SignRecoverInit:
1831
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1832
return func->C_SignRecoverInit(*(CK_ULONG *)a[0]->data,
1833
(CK_MECHANISM *)a[1]->data,
1834
*(CK_ULONG *)a[2]->data);
1835
case F_C_SignRecover:
1836
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1837
return func->C_SignRecover(*(CK_ULONG *)a[0]->data,
1838
(CK_CHAR *)a[1]->data,
1839
*(CK_ULONG *)a[2]->data,
1840
(CK_CHAR *)a[3]->data,
1841
(CK_ULONG *)a[4]->data);
1842
case F_C_VerifyInit:
1843
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1844
return func->C_VerifyInit(*(CK_ULONG *)a[0]->data,
1845
(CK_MECHANISM *)a[1]->data,
1846
*(CK_ULONG *)a[2]->data);
1848
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1849
return func->C_Verify(*(CK_ULONG *)a[0]->data,
1850
(CK_CHAR *)a[1]->data,
1851
*(CK_ULONG *)a[2]->data,
1852
(CK_CHAR *)a[3]->data,
1853
*(CK_ULONG *)a[4]->data);
1854
case F_C_VerifyUpdate:
1855
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1856
return func->C_VerifyUpdate(*(CK_ULONG *)a[0]->data,
1857
(CK_CHAR *)a[1]->data,
1858
*(CK_ULONG *)a[2]->data);
1859
case F_C_VerifyFinal:
1860
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1861
return func->C_VerifyFinal(*(CK_ULONG *)a[0]->data,
1862
(CK_CHAR *)a[1]->data,
1863
*(CK_ULONG *)a[2]->data);
1865
case F_C_VerifyRecoverInit:
1866
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1867
return func->C_VerifyRecoverInit(*(CK_ULONG *)a[0]->data,
1868
(CK_MECHANISM *)a[1]->data,
1869
*(CK_ULONG *)a[2]->data);
1870
case F_C_VerifyRecover:
1871
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1872
return func->C_VerifyRecover(*(CK_ULONG *)a[0]->data,
1873
(CK_CHAR *)a[1]->data,
1874
*(CK_ULONG *)a[2]->data,
1875
(CK_CHAR *)a[3]->data,
1876
(CK_ULONG *)a[4]->data);
1877
case F_C_DigestEncryptUpdate:
1878
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1879
return func->C_DigestEncryptUpdate(*(CK_ULONG *)a[0]->data,
1880
(CK_CHAR *)a[1]->data,
1881
*(CK_ULONG *)a[2]->data,
1882
(CK_CHAR *)a[3]->data,
1883
(CK_ULONG *)a[4]->data);
1884
case F_C_DecryptDigestUpdate:
1885
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1886
return func->C_DecryptDigestUpdate(*(CK_ULONG *)a[0]->data,
1887
(CK_CHAR *)a[1]->data,
1888
*(CK_ULONG *)a[2]->data,
1889
(CK_CHAR *)a[3]->data,
1890
(CK_ULONG *)a[4]->data);
1891
case F_C_SignEncryptUpdate:
1892
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1893
return func->C_SignEncryptUpdate(*(CK_ULONG *)a[0]->data,
1894
(CK_CHAR *)a[1]->data,
1895
*(CK_ULONG *)a[2]->data,
1896
(CK_CHAR *)a[3]->data,
1897
(CK_ULONG *)a[4]->data);
1898
case F_C_DecryptVerifyUpdate:
1899
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1900
return func->C_DecryptVerifyUpdate(*(CK_ULONG *)a[0]->data,
1901
(CK_CHAR *)a[1]->data,
1902
*(CK_ULONG *)a[2]->data,
1903
(CK_CHAR *)a[3]->data,
1904
(CK_ULONG *)a[4]->data);
1905
case F_C_GenerateKey:
1906
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1907
return func->C_GenerateKey(*(CK_ULONG *)a[0]->data,
1908
(CK_MECHANISM *)a[1]->data,
1909
(CK_ATTRIBUTE *)a[2]->data,
1910
*(CK_ULONG *)a[3]->data,
1911
(CK_ULONG *)a[4]->data);
1912
case F_C_GenerateKeyPair:
1913
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1914
return func->C_GenerateKeyPair(*(CK_ULONG *)a[0]->data,
1915
(CK_MECHANISM *)a[1]->data,
1916
(CK_ATTRIBUTE *)a[2]->data,
1917
*(CK_ULONG *)a[3]->data,
1918
(CK_ATTRIBUTE *)a[4]->data,
1919
*(CK_ULONG *)a[5]->data,
1920
(CK_ULONG *)a[6]->data,
1921
(CK_ULONG *)a[7]->data);
1923
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1924
return func->C_WrapKey(*(CK_ULONG *)a[0]->data,
1925
(CK_MECHANISM *)a[1]->data,
1926
*(CK_ULONG *)a[2]->data,
1927
*(CK_ULONG *)a[3]->data,
1928
(CK_CHAR *)a[5]->data,
1929
(CK_ULONG *)a[6]->data);
1931
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1932
return func->C_UnwrapKey(*(CK_ULONG *)a[0]->data,
1933
(CK_MECHANISM *)a[1]->data,
1934
*(CK_ULONG *)a[2]->data,
1935
(CK_CHAR *)a[3]->data,
1936
*(CK_ULONG *)a[4]->data,
1937
(CK_ATTRIBUTE *)a[5]->data,
1938
*(CK_ULONG *)a[6]->data,
1939
(CK_ULONG *)a[7]->data);
1941
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1942
return func->C_DeriveKey (*(CK_ULONG *)a[0]->data,
1943
(CK_MECHANISM *)a[1]->data,
1944
*(CK_ULONG *)a[2]->data,
1945
(CK_ATTRIBUTE *)a[3]->data,
1946
*(CK_ULONG *)a[4]->data,
1947
(CK_ULONG *)a[5]->data);
1948
case F_C_SeedRandom:
1949
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1950
return func->C_SeedRandom(*(CK_ULONG *)a[0]->data,
1951
(CK_CHAR *)a[1]->data,
1952
*(CK_ULONG *)a[2]->data);
1953
case F_C_GenerateRandom:
1954
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1955
return func->C_GenerateRandom(*(CK_ULONG *)a[0]->data,
1956
(CK_CHAR *)a[1]->data,
1957
*(CK_ULONG *)a[2]->data);
1958
case F_C_GetFunctionStatus:
1959
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1960
return func->C_GetFunctionStatus(*(CK_ULONG *)a[0]->data);
1961
case F_C_CancelFunction:
1962
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1963
return func->C_CancelFunction(*(CK_ULONG *)a[0]->data);
1964
case F_C_WaitForSlotEvent:
1965
if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED;
1966
return func->C_WaitForSlotEvent(*(CK_ULONG *)a[0]->data,
1967
(CK_ULONG *)a[1]->data,
1968
(void *)a[2]->data);
1969
/* set a variable */
1971
case F_SetStringVar:
1972
(void) DeleteVariable(a[0]->data);
1973
(void) AddVariable(a[0]->data,&a[1]);
1977
return printArg(a[0],0);
1979
return save(a[0]->data,a[1]);
1981
return restore(a[0]->data,a[1]);
1983
return DeleteVariable(a[0]->data);
1985
return increment(a[0], *(CK_ULONG *)a[1]->data);
1987
return decrement(a[0], *(CK_ULONG *)a[1]->data);
1991
return run(a[0]->data);
1993
return timeCommand(a[0]->data);
1995
return loadModule(&module,a[0]->data);
1997
return unloadModule(&module);
1999
(void) DeleteVariable(a[0]->data);
2000
return ArrayVariable(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data);
2002
(void) DeleteVariable(a[0]->data);
2003
return ArrayTemplate(a[0]->data,a[1]->data);
2004
case F_BuildTemplate:
2005
return BuildTemplate(a[0]);
2007
return SetTemplate(a[0],
2008
*(CK_ULONG *)a[1]->data,
2009
*(CK_ULONG *)a[2]->data);
2010
case F_NewMechanism:
2011
(void) DeleteVariable(a[0]->data);
2012
return NewMechanism(a[0]->data,*(CK_ULONG *)a[1]->data);
2013
case F_NewInitializeArgs:
2014
(void) DeleteVariable(a[0]->data);
2015
return NewInitializeArgs(a[0]->data,*(CK_ULONG *)a[1]->data,a[2]->data);
2017
value = *(int *)a[0]->data;
2018
if (value & 0x80000000) {
2019
systemFlags &= ~value;
2021
systemFlags |= value;
2025
return loop(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data,
2026
*(CK_ULONG *)a[3]->data,*(CK_ULONG *)a[4]->data);
2029
helpIndex = lookup(a[0]->data);
2030
if (helpIndex < 0) {
2031
return printTopicHelp(a[0]->data);
2033
printHelp(helpIndex, 1);
2036
return printGeneralHelp();
2037
case F_QuitIfString:
2038
return quitIfString(a[0]->data,a[1]->data,a[2]->data);
2040
return quitIf(*(CK_ULONG *)a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data);
2045
"Function %s not yet supported\n",commands[index].fname );
2053
processCommand(const char * buf)
2055
CK_RV error = CKR_OK;
2061
/* allow comments and blank lines in scripts */
2062
if ((*bp == '#') || (*bp == 0) || (*bp == '\n')){
2072
arglist = parseArgs(index,bp);
2073
if (arglist == NULL) {
2077
error = do_func(index,arglist);
2078
if (error == CKR_OK) {
2080
} else if (error != CKR_QUIT) {
2081
printf(">> Error : ");
2082
printConst(error, ConstResult, 1);
2090
timeCommand(const char *command)
2093
PRIntervalTime startTime = PR_IntervalNow();
2094
PRIntervalTime endTime;
2095
PRIntervalTime elapsedTime;
2097
ckrv = processCommand(command);
2099
endTime = PR_IntervalNow();
2100
elapsedTime = endTime - startTime;
2101
printf("Time -- %d msec \n",
2102
PR_IntervalToMilliseconds(elapsedTime));
2110
process(FILE *inFile,int user)
2114
CK_RV ckrv = CKR_OK;
2116
if (user) { printf("pkcs11> "); fflush(stdout); }
2118
while (fgets(buf,2048,inFile) != NULL) {
2120
if (!user) printf("* %s",buf);
2121
error = processCommand(buf);
2122
if (error == CKR_QUIT) {
2124
} else if (error != CKR_OK) {
2128
printf("pkcs11> "); fflush(stdout);
2135
run(const char *filename)
2140
infile = fopen(filename,"r");
2142
if (infile == NULL) {
2144
return CKR_FUNCTION_FAILED;
2147
ckrv = process(infile, 0);
2154
loop(const char *filename, const char *var,
2155
CK_ULONG start, CK_ULONG end, CK_ULONG step)
2161
for (i=start; i < end; i += step)
2163
value = NewValue(ArgULong, 1);
2164
*(CK_ULONG *)value->data = i;
2165
DeleteVariable(var);
2166
AddVariable(var, &value);
2167
ckrv = run(filename);
2169
if (ckrv == CKR_QUIT) {
2177
main(int argc, char **argv)
2179
/* I suppose that some day we could parse some arguments */
2180
(void) process(stdin, 1);