994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
|
CK_ATTRIBUTE template[] = {
{CKA_CLASS, NULL, 0},
{CKA_LABEL, NULL, 0},
{CKA_ID, NULL, 0},
{CKA_VALUE, NULL, 0}
}, *curr_attr;
CK_ULONG curr_attr_idx;
CK_OBJECT_CLASS *objectclass;
CK_RV chk_rv;
if (!cd) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid clientdata", -1));
return(TCL_ERROR);
}
if (objc != 3) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"pki::pkcs11::listcerts handle slot\"", -1));
return(TCL_ERROR);
}
tcl_handle = objv[1];
tcl_slotid = objv[2];
interpdata = (struct tclpkcs11_interpdata *) cd;
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
|
CK_ATTRIBUTE template[] = {
{CKA_CLASS, NULL, 0},
{CKA_LABEL, NULL, 0},
{CKA_ID, NULL, 0},
{CKA_VALUE, NULL, 0}
}, *curr_attr;
CK_ULONG curr_attr_idx;
CK_OBJECT_CLASS objectclass, *objectclass_p;
CK_RV chk_rv;
if (!cd) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid clientdata", -1));
return(TCL_ERROR);
}
if (objc != 3) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"pki::pkcs11::listcerts handle slot\"", -1));
return(TCL_ERROR);
}
tcl_handle = objv[1];
tcl_slotid = objv[2];
interpdata = (struct tclpkcs11_interpdata *) cd;
tcl_handle_entry = Tcl_FindHashEntry(&interpdata->handles, (const char *) tcl_handle);
if (!tcl_handle_entry) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid handle", -1));
return(TCL_ERROR);
}
handle = (struct tclpkcs11_handle *) Tcl_GetHashValue(tcl_handle_entry);
if (!handle) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid handle", -1));
return(TCL_ERROR);
}
tcl_rv = Tcl_GetLongFromObj(interp, tcl_slotid, &slotid_long);
if (tcl_rv != TCL_OK) {
return(tcl_rv);
}
slotid = slotid_long;
chk_rv = tclpkcs11_start_session(handle, slotid);
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
chk_rv = handle->pkcs11->C_FindObjectsInit(handle->session, NULL, 0);
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
ret_list = Tcl_NewObj();
while (1) {
chk_rv = handle->pkcs11->C_FindObjects(handle->session, &hObject, 1, &ulObjectCount);
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
handle->pkcs11->C_FindObjectsFinal(handle->session);
return(TCL_ERROR);
}
if (ulObjectCount == 0) {
break;
}
if (ulObjectCount != 1) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("FindObjects() returned a weird number of objects.", -1));
handle->pkcs11->C_FindObjectsFinal(handle->session);
return(TCL_ERROR);
}
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
curr_attr = &template[curr_attr_idx];
if (curr_attr->pValue) {
ckfree(curr_attr->pValue);
}
curr_attr->pValue = NULL;
curr_attr->ulValueLen = 0;
}
/* Determine size of values to allocate */
chk_rv = handle->pkcs11->C_GetAttributeValue(handle->session, hObject, template, sizeof(template) / sizeof(template[0]));
if (chk_rv == CKR_ATTRIBUTE_TYPE_INVALID || chk_rv == CKR_ATTRIBUTE_SENSITIVE || chk_rv == CKR_BUFFER_TOO_SMALL) {
chk_rv = CKR_OK;
}
if (chk_rv != CKR_OK) {
/* Skip this object if we are not able to process it */
continue;
}
/* Allocate values */
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
curr_attr = &template[curr_attr_idx];
if (((CK_LONG) curr_attr->ulValueLen) != ((CK_LONG) -1)) {
curr_attr->pValue = (void *) ckalloc(curr_attr->ulValueLen);
}
}
/* Populate template values */
chk_rv = handle->pkcs11->C_GetAttributeValue(handle->session, hObject, template, sizeof(template) / sizeof(template[0]));
if (chk_rv != CKR_OK && chk_rv != CKR_ATTRIBUTE_SENSITIVE && chk_rv != CKR_ATTRIBUTE_TYPE_INVALID && chk_rv != CKR_BUFFER_TOO_SMALL) {
/* Return an error if we are unable to process this entry due to unexpected errors */
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
curr_attr = &template[curr_attr_idx];
if (curr_attr->pValue) {
ckfree(curr_attr->pValue);
}
}
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
handle->pkcs11->C_FindObjectsFinal(handle->session);
return(TCL_ERROR);
}
/* Extract certificate data */
obj_label = NULL;
obj_id = NULL;
obj_cert = NULL;
objectclass_p = NULL;
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
curr_attr = &template[curr_attr_idx];
if (!curr_attr->pValue) {
continue;
}
if (curr_attr->type != CKA_CLASS && !objectclass_p) {
ckfree(curr_attr->pValue);
curr_attr->pValue = NULL;
continue;
}
switch (curr_attr->type) {
case CKA_CLASS:
objectclass_p = (CK_OBJECT_CLASS *) curr_attr->pValue;
objectclass = *objectclass_p;
break;
case CKA_LABEL:
obj_label = Tcl_NewStringObj(curr_attr->pValue, curr_attr->ulValueLen);
break;
case CKA_ID:
/* Convert the ID into a readable string */
obj_id = tclpkcs11_bytearray_to_string(curr_attr->pValue, curr_attr->ulValueLen);
break;
case CKA_VALUE:
obj_cert = Tcl_NewByteArrayObj(curr_attr->pValue, curr_attr->ulValueLen);
break;
}
ckfree(curr_attr->pValue);
curr_attr->pValue = NULL;
}
/* Add this certificate data to return list, if all found */
if (obj_label == NULL || obj_id == NULL || obj_cert == NULL) {
continue;
}
if (objectclass != CKO_CERTIFICATE) {
continue;
}
/* Create the current item list */
curr_item_list = Tcl_NewObj();
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_handle", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, tcl_handle);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_slotid", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, tcl_slotid);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_id", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, obj_id);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_label", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, obj_label);
/* Call "::pki::x509::parse_cert" to parse the cert */
parse_cert_cmd = Tcl_NewObj();
Tcl_ListObjAppendElement(interp, parse_cert_cmd, Tcl_NewStringObj("::pki::x509::parse_cert", -1));
Tcl_ListObjAppendElement(interp, parse_cert_cmd, obj_cert);
tcl_rv = Tcl_EvalObjEx(interp, parse_cert_cmd, 0);
if (tcl_rv != TCL_OK) {
continue;
}
/* Add results of [parse_cert] to our return value */
Tcl_ListObjAppendList(interp, curr_item_list, Tcl_GetObjResult(interp));
/*
* Override the "type" so that [array set] returns our new
* type, but we can still parse through the list and figure
* out the real subordinate type
*/
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("type", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11", -1));
/* Add the current item to the return value list */
Tcl_ListObjAppendElement(interp, ret_list, curr_item_list);
}
/* Terminate search */
handle->pkcs11->C_FindObjectsFinal(handle->session);
/* Return */
Tcl_SetObjResult(interp, ret_list);
return(TCL_OK);
}
/* XXX:TODO: Merge this with list_certs */
MODULE_SCOPE int tclpkcs11_list_keys(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
struct tclpkcs11_interpdata *interpdata;
struct tclpkcs11_handle *handle;
Tcl_HashEntry *tcl_handle_entry;
Tcl_Obj *tcl_handle, *tcl_slotid;
long slotid_long;
Tcl_Obj *obj_label, *obj_id, *obj_exponent, *obj_modulus;
Tcl_Obj *ret_list, *curr_item_list;
int tcl_rv;
CK_SLOT_ID slotid;
CK_OBJECT_HANDLE hObject;
CK_ULONG ulObjectCount;
CK_ATTRIBUTE template[] = {
{CKA_CLASS, NULL, 0},
{CKA_LABEL, NULL, 0},
{CKA_ID, NULL, 0},
{CKA_KEY_TYPE, NULL, 0},
{CKA_PUBLIC_EXPONENT, NULL, 0},
{CKA_MODULUS, NULL, 0}
};
CK_ATTRIBUTE *curr_attr;
CK_ULONG curr_attr_idx;
CK_OBJECT_CLASS objectclass, *objectclass_p;
CK_KEY_TYPE keytype;
CK_RV chk_rv;
if (!cd) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("invalid clientdata", -1));
return(TCL_ERROR);
}
if (objc != 3) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("wrong # args: should be \"pki::pkcs11::listkeys handle slot\"", -1));
return(TCL_ERROR);
}
tcl_handle = objv[1];
tcl_slotid = objv[2];
interpdata = (struct tclpkcs11_interpdata *) cd;
|
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
|
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
handle->pkcs11->C_FindObjectsFinal(handle->session);
return(TCL_ERROR);
}
/* Extract certificate data */
obj_label = NULL;
obj_id = NULL;
obj_cert = NULL;
objectclass = NULL;
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
curr_attr = &template[curr_attr_idx];
if (!curr_attr->pValue) {
continue;
}
switch (curr_attr->type) {
case CKA_CLASS:
objectclass = (CK_OBJECT_CLASS *) curr_attr->pValue;
if (*objectclass != CKO_CERTIFICATE) {
continue;
}
break;
case CKA_LABEL:
obj_label = Tcl_NewStringObj(curr_attr->pValue, curr_attr->ulValueLen);
break;
case CKA_ID:
/* Convert the ID into a readable string */
obj_id = tclpkcs11_bytearray_to_string(curr_attr->pValue, curr_attr->ulValueLen);
break;
case CKA_VALUE:
if (!objectclass) {
break;
}
obj_cert = Tcl_NewByteArrayObj(curr_attr->pValue, curr_attr->ulValueLen);
break;
}
ckfree(curr_attr->pValue);
curr_attr->pValue = NULL;
}
/* Add this certificate data to return list, if all found */
if (obj_label == NULL || obj_id == NULL || obj_cert == NULL) {
continue;
}
/* Create the current item list */
curr_item_list = Tcl_NewObj();
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_handle", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, tcl_handle);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_slotid", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, tcl_slotid);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_id", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, obj_id);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_label", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, obj_label);
/* Call "::pki::x509::parse_cert" to parse the cert */
parse_cert_cmd = Tcl_NewObj();
Tcl_ListObjAppendElement(interp, parse_cert_cmd, Tcl_NewStringObj("::pki::x509::parse_cert", -1));
Tcl_ListObjAppendElement(interp, parse_cert_cmd, obj_cert);
tcl_rv = Tcl_EvalObjEx(interp, parse_cert_cmd, 0);
if (tcl_rv != TCL_OK) {
continue;
}
/* Add results of [parse_cert] to our return value */
Tcl_ListObjAppendList(interp, curr_item_list, Tcl_GetObjResult(interp));
/*
* Override the "type" so that [array set] returns our new
* type, but we can still parse through the list and figure
* out the real subordinate type
*/
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("type", -1));
|
|
>
|
|
>
>
>
>
>
>
>
>
|
<
|
<
<
<
|
>
>
|
|
<
|
|
|
>
>
|
>
>
>
>
>
>
>
>
|
<
|
|
|
<
<
>
|
>
>
|
|
|
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
|
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
handle->pkcs11->C_FindObjectsFinal(handle->session);
return(TCL_ERROR);
}
/* Extract key data */
obj_label = NULL;
obj_id = NULL;
objectclass_p = NULL;
obj_exponent = NULL;
obj_modulus = NULL;
keytype = -1;
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
curr_attr = &template[curr_attr_idx];
if (!curr_attr->pValue) {
continue;
}
if (curr_attr->type != CKA_CLASS && !objectclass) {
ckfree(curr_attr->pValue);
curr_attr->pValue = NULL;
continue;
}
switch (curr_attr->type) {
case CKA_CLASS:
objectclass_p = (CK_OBJECT_CLASS *) curr_attr->pValue;
objectclass = *objectclass_p;
break;
case CKA_LABEL:
obj_label = Tcl_NewStringObj(curr_attr->pValue, curr_attr->ulValueLen);
break;
case CKA_ID:
/* Convert the ID into a readable string */
obj_id = tclpkcs11_bytearray_to_string(curr_attr->pValue, curr_attr->ulValueLen);
break;
case CKA_KEY_TYPE:
/* Convert the ID into a readable string */
keytype = *((CK_KEY_TYPE *) curr_attr->pValue);
break;
case CKA_MODULUS:
obj_modulus = Tcl_NewByteArrayObj(curr_attr->pValue, curr_attr->ulValueLen);
break;
case CKA_PUBLIC_EXPONENT:
obj_exponent = Tcl_NewByteArrayObj(curr_attr->pValue, curr_attr->ulValueLen);
break;
}
ckfree(curr_attr->pValue);
curr_attr->pValue = NULL;
}
/* Add this certificate data to return list, if all found */
if (obj_label == NULL || obj_id == NULL || obj_exponent == NULL || obj_modulus == NULL || keytype == -1) {
continue;
}
if (objectclass != CKO_PUBLIC_KEY) {
continue;
}
if (keytype != CKK_RSA) {
continue;
}
/* Create the current item list */
curr_item_list = Tcl_NewObj();
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_handle", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, tcl_handle);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_slotid", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, tcl_slotid);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_id", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, obj_id);
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("pkcs11_label", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, obj_label);
/* Add the RSA key to the list */
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("n", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_ObjPrintf("0x%s", Tcl_GetString(tclpkcs11_bytearray_to_string_from_obj(obj_modulus))));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("e", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_ObjPrintf("0x%s", Tcl_GetString(tclpkcs11_bytearray_to_string_from_obj(obj_exponent))));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("l", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_ObjPrintf("%i", Tcl_GetCharLength(obj_modulus) * 8));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("type", -1));
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("rsa", -1));
/*
* Override the "type" so that [array set] returns our new
* type, but we can still parse through the list and figure
* out the real subordinate type
*/
Tcl_ListObjAppendElement(interp, curr_item_list, Tcl_NewStringObj("type", -1));
|
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
|
/* CKA_CLASS */
objectclass_pk = CKO_PRIVATE_KEY;
template[1].pValue = &objectclass_pk;
template[1].ulValueLen = sizeof(objectclass_pk);
chk_rv = handle->pkcs11->C_FindObjectsInit(handle->session, template, sizeof(template) / sizeof(template[0]));
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
foundObjs = 0;
chk_rv = handle->pkcs11->C_FindObjects(handle->session, &hObject, 1, &foundObjs);
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
handle->pkcs11->C_FindObjectsFinal(handle->session);
return(TCL_ERROR);
}
/* Terminate Search */
handle->pkcs11->C_FindObjectsFinal(handle->session);
if (foundObjs < 1) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("PKCS11_ERROR MAYBE_LOGIN", -1));
return(TCL_ERROR);
}
/* Perform the PKI operation (encrypt/decrypt) */
input = Tcl_GetByteArrayFromObj(tcl_input, &input_len);
if (encrypt) {
sign = 0;
chk_rv = handle->pkcs11->C_EncryptInit(handle->session, &mechanism, hObject);
if (chk_rv != CKR_OK) {
sign = 1;
chk_rv = handle->pkcs11->C_SignInit(handle->session, &mechanism, hObject);
}
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
resultbuf_len = sizeof(resultbuf);
if (!sign) {
|
>
>
|
>
|
>
>
>
>
>
|
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
|
/* CKA_CLASS */
objectclass_pk = CKO_PRIVATE_KEY;
template[1].pValue = &objectclass_pk;
template[1].ulValueLen = sizeof(objectclass_pk);
chk_rv = handle->pkcs11->C_FindObjectsInit(handle->session, template, sizeof(template) / sizeof(template[0]));
if (chk_rv != CKR_OK) {
tclpkcs11_close_session(handle);
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
foundObjs = 0;
chk_rv = handle->pkcs11->C_FindObjects(handle->session, &hObject, 1, &foundObjs);
if (chk_rv != CKR_OK) {
handle->pkcs11->C_FindObjectsFinal(handle->session);
tclpkcs11_close_session(handle);
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
/* Terminate Search */
handle->pkcs11->C_FindObjectsFinal(handle->session);
if (foundObjs < 1) {
tclpkcs11_close_session(handle);
Tcl_SetObjResult(interp, Tcl_NewStringObj("PKCS11_ERROR MAYBE_LOGIN", -1));
return(TCL_ERROR);
}
/* Perform the PKI operation (encrypt/decrypt) */
input = Tcl_GetByteArrayFromObj(tcl_input, &input_len);
if (encrypt) {
sign = 0;
chk_rv = handle->pkcs11->C_EncryptInit(handle->session, &mechanism, hObject);
if (chk_rv != CKR_OK) {
sign = 1;
chk_rv = handle->pkcs11->C_SignInit(handle->session, &mechanism, hObject);
}
if (chk_rv != CKR_OK) {
tclpkcs11_close_session(handle);
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
resultbuf_len = sizeof(resultbuf);
if (!sign) {
|
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
|
handle->pkcs11->C_EncryptFinal(handle->session, dummybuf, &dummybuf_len);
} else {
handle->pkcs11->C_SignFinal(handle->session, dummybuf, &dummybuf_len);
}
}
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
} else {
chk_rv = handle->pkcs11->C_DecryptInit(handle->session, &mechanism, hObject);
if (chk_rv != CKR_OK) {
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
resultbuf_len = sizeof(resultbuf);
chk_rv = handle->pkcs11->C_Decrypt(handle->session, input, input_len, resultbuf, &resultbuf_len);
if (chk_rv != CKR_OK) {
if (chk_rv == CKR_BUFFER_TOO_SMALL) {
/* Terminate decryption operation */
handle->pkcs11->C_DecryptFinal(handle->session, NULL, 0);
}
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
}
tcl_result = Tcl_NewByteArrayObj(resultbuf, resultbuf_len);
Tcl_SetObjResult(interp, tcl_result);
return(TCL_OK);
}
|
>
>
>
>
>
>
>
>
|
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
|
handle->pkcs11->C_EncryptFinal(handle->session, dummybuf, &dummybuf_len);
} else {
handle->pkcs11->C_SignFinal(handle->session, dummybuf, &dummybuf_len);
}
}
if (chk_rv != CKR_OK) {
tclpkcs11_close_session(handle);
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
} else {
chk_rv = handle->pkcs11->C_DecryptInit(handle->session, &mechanism, hObject);
if (chk_rv != CKR_OK) {
tclpkcs11_close_session(handle);
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
resultbuf_len = sizeof(resultbuf);
chk_rv = handle->pkcs11->C_Decrypt(handle->session, input, input_len, resultbuf, &resultbuf_len);
if (chk_rv != CKR_OK) {
tclpkcs11_close_session(handle);
if (chk_rv == CKR_BUFFER_TOO_SMALL) {
/* Terminate decryption operation */
handle->pkcs11->C_DecryptFinal(handle->session, NULL, 0);
}
Tcl_SetObjResult(interp, tclpkcs11_pkcs11_error(chk_rv));
return(TCL_ERROR);
}
}
tclpkcs11_close_session(handle);
tcl_result = Tcl_NewByteArrayObj(resultbuf, resultbuf_len);
Tcl_SetObjResult(interp, tcl_result);
return(TCL_OK);
}
|
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
|
return(TCL_ERROR);
}
tclCreatComm_ret = Tcl_CreateObjCommand(interp, "pki::pkcs11::listcerts", tclpkcs11_list_certs, interpdata, NULL);
if (!tclCreatComm_ret) {
return(TCL_ERROR);
}
tclCreatComm_ret = Tcl_CreateObjCommand(interp, "pki::pkcs11::login", tclpkcs11_login, interpdata, NULL);
if (!tclCreatComm_ret) {
return(TCL_ERROR);
}
tclCreatComm_ret = Tcl_CreateObjCommand(interp, "pki::pkcs11::logout", tclpkcs11_logout, interpdata, NULL);
|
>
>
>
>
>
|
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
|
return(TCL_ERROR);
}
tclCreatComm_ret = Tcl_CreateObjCommand(interp, "pki::pkcs11::listcerts", tclpkcs11_list_certs, interpdata, NULL);
if (!tclCreatComm_ret) {
return(TCL_ERROR);
}
tclCreatComm_ret = Tcl_CreateObjCommand(interp, "pki::pkcs11::listkeys", tclpkcs11_list_keys, interpdata, NULL);
if (!tclCreatComm_ret) {
return(TCL_ERROR);
}
tclCreatComm_ret = Tcl_CreateObjCommand(interp, "pki::pkcs11::login", tclpkcs11_login, interpdata, NULL);
if (!tclCreatComm_ret) {
return(TCL_ERROR);
}
tclCreatComm_ret = Tcl_CreateObjCommand(interp, "pki::pkcs11::logout", tclpkcs11_logout, interpdata, NULL);
|