/*
* This file is to be used for in-house testing purposes only, it has been
* modified from the test program source of the CACKey v0.7.4 component
* and adapted for use with this Solaris coolkey v1.1.0 userland component.
*/
#include "mypkcs11.h"
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
switch (attrib) {
case 0x00000000: return "CKA_CLASS";
case 0x00000001: return "CKA_TOKEN";
case 0x00000002: return "CKA_PRIVATE";
case 0x00000003: return "CKA_LABEL";
case 0x00000010: return "CKA_APPLICATION";
case 0x00000011: return "CKA_VALUE";
case 0x00000012: return "CKA_OBJECT_ID";
case 0x00000080: return "CKA_CERTIFICATE_TYPE";
case 0x00000081: return "CKA_ISSUER";
case 0x00000082: return "CKA_SERIAL_NUMBER";
case 0x00000083: return "CKA_AC_ISSUER";
case 0x00000084: return "CKA_OWNER";
case 0x00000085: return "CKA_ATTR_TYPES";
case 0x00000086: return "CKA_TRUSTED";
case 0x00000100: return "CKA_KEY_TYPE";
case 0x00000101: return "CKA_SUBJECT";
case 0x00000102: return "CKA_ID";
case 0x00000103: return "CKA_SENSITIVE";
case 0x00000104: return "CKA_ENCRYPT";
case 0x00000105: return "CKA_DECRYPT";
case 0x00000106: return "CKA_WRAP";
case 0x00000107: return "CKA_UNWRAP";
case 0x00000108: return "CKA_SIGN";
case 0x00000109: return "CKA_SIGN_RECOVER";
case 0x0000010A: return "CKA_VERIFY";
case 0x0000010B: return "CKA_VERIFY_RECOVER";
case 0x0000010C: return "CKA_DERIVE";
case 0x00000110: return "CKA_START_DATE";
case 0x00000111: return "CKA_END_DATE";
case 0x00000120: return "CKA_MODULUS";
case 0x00000121: return "CKA_MODULUS_BITS";
case 0x00000122: return "CKA_PUBLIC_EXPONENT";
case 0x00000123: return "CKA_PRIVATE_EXPONENT";
case 0x00000124: return "CKA_PRIME_1";
case 0x00000125: return "CKA_PRIME_2";
case 0x00000126: return "CKA_EXPONENT_1";
case 0x00000127: return "CKA_EXPONENT_2";
case 0x00000128: return "CKA_COEFFICIENT";
case 0x00000130: return "CKA_PRIME";
case 0x00000131: return "CKA_SUBPRIME";
case 0x00000132: return "CKA_BASE";
case 0x00000133: return "CKA_PRIME_BITS";
case 0x00000134: return "CKA_SUB_PRIME_BITS";
case 0x00000160: return "CKA_VALUE_BITS";
case 0x00000161: return "CKA_VALUE_LEN";
case 0x00000162: return "CKA_EXTRACTABLE";
case 0x00000163: return "CKA_LOCAL";
case 0x00000164: return "CKA_NEVER_EXTRACTABLE";
case 0x00000165: return "CKA_ALWAYS_SENSITIVE";
case 0x00000166: return "CKA_KEY_GEN_MECHANISM";
case 0x00000170: return "CKA_MODIFIABLE";
case 0x00000180: return "CKA_EC_PARAMS";
case 0x00000181: return "CKA_EC_POINT";
case 0x00000200: return "CKA_SECONDARY_AUTH";
case 0x00000201: return "CKA_AUTH_PIN_FLAGS";
case 0x00000300: return "CKA_HW_FEATURE_TYPE";
case 0x00000301: return "CKA_RESET_ON_INIT";
case 0x00000302: return "CKA_HAS_RESET";
case 0xce5363b4: return "CKA_CERT_SHA1_HASH";
case 0xce5363b5: return "CKA_CERT_MD5_HASH";
}
return(retbuf);
}
int main_pkcs11(void) {
CK_RV (*C_Decrypt)(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen) = NULL;
CK_RV (*C_DecryptInit)(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) = NULL;
CK_RV (*C_Encrypt)(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen) = NULL;
CK_RV (*C_EncryptInit)(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) = NULL;
CK_RV (*C_FindObjects)(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject, CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount) = NULL;
CK_RV (*C_FindObjectsInit)(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) = NULL;
CK_RV (*C_GetAttributeValue)(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) = NULL;
CK_RV (*C_GetSlotList)(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) = NULL;
CK_RV (*C_Login)(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen) = NULL;
CK_RV (*C_OpenSession)(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_SESSION_HANDLE_PTR phSession) = NULL;
CK_RV (*C_Sign)(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen) = NULL;
CK_RV (*C_SignInit)(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) = NULL;
{CKA_PRIVATE, NULL, 0},
{CKA_SERIAL_NUMBER, NULL, 0},
{CKA_SUBJECT, NULL, 0},
{CKA_ISSUER, NULL, 0},
{CKA_CERTIFICATE_TYPE, NULL, 0},
{CKA_KEY_TYPE, NULL, 0},
{CKA_TRUSTED, NULL, 0}
}, *curr_attr;
char *fgets_ret;
int i;
printf("C_GetFunctionList() failed.");
return(1);
}
for (i = 0; i < 1024; i++) {
}
printf("C_Initialize() failed.");
return(1);
}
}
return(1);
}
printf("PKCS#11 Client Version: %i.%i, Library Version %i.%i\n", clientinfo.cryptokiVersion.major, clientinfo.cryptokiVersion.minor, clientinfo.libraryVersion.major, clientinfo.libraryVersion.minor);
printf("PKCS#11 ManufID: %.*s, LibraryDesc: %.*s\n", 32, clientinfo.manufacturerID, 32, clientinfo.libraryDescription);
return(1);
}
return(1);
}
return(1);
}
printf(" Flags : ");
printf("CKF_TOKEN_PRESENT ");
}
printf("CKF_REMOVABLE_DEVICE ");
}
printf("CKF_HW_SLOT ");
}
printf("\n");
printf(" Token:\n");
return(1);
}
printf(" Flags : ");
printf("CKF_RNG ");
}
printf("CKF_WRITE_PROTECTED ");
}
printf("CKF_LOGIN_REQUIRED ");
}
printf("CKF_USER_PIN_INITIALIZED ");
}
printf("CKF_RESTORE_KEY_NOT_NEEDED ");
}
printf("CKF_CLOCK_ON_TOKEN ");
}
printf("CKF_PROTECTED_AUTHENTICATION_PATH ");
}
printf("CKF_DUAL_CRYPTO_OPERATIONS ");
}
printf("CKF_TOKEN_INITIALIZED ");
}
printf("CKF_SECONDARY_AUTHENTICATION ");
}
printf("CKF_USER_PIN_COUNT_LOW ");
}
printf("CKF_USER_PIN_FINAL_TRY ");
}
printf("CKF_USER_PIN_LOCKED ");
}
printf("CKF_USER_PIN_TO_BE_CHANGED ");
}
printf("CKF_SO_PIN_COUNT_LOW ");
}
printf("CKF_SO_PIN_FINAL_TRY ");
}
printf("CKF_SO_PIN_LOCKED ");
}
printf("CKF_SO_PIN_TO_BE_CHANGED ");
}
printf("\n");
}
}
return(1);
}
if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED && (tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
printf("** ENTER PIN: ");
}
}
}
} else {
}
printf("Login to device succeed.\n");
} else {
printf("Login to device failed.\n");
}
printf("Session Info:\n");
printf(" State : ");
printf("CKS_RO_PUBLIC_SESSION\n");
printf("CKS_RO_USER_FUNCTIONS\n");
printf("CKS_RW_PUBLIC_SESSION\n");
printf("CKS_RW_USER_FUNCTIONS\n");
printf("CKS_RW_SO_FUNCTIONS\n");
} else {
}
printf(" Flags : ");
printf("CKF_RW_SESSION ");
}
printf("CKF_SERIAL_SESSION ");
}
printf("\n");
} else {
printf("GetSessionInfo() failed.\n");
}
while (1) {
printf("FindObjects() failed.\n");
break;
}
if (ulObjectCount == 0) {
break;
}
if (ulObjectCount != 1) {
break;
}
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
}
}
chk_rv = C_GetAttributeValue(hSession, hObject, &template[0], sizeof(template) / sizeof(template[0]));
if (chk_rv == CKR_ATTRIBUTE_TYPE_INVALID || chk_rv == CKR_ATTRIBUTE_SENSITIVE || chk_rv == CKR_BUFFER_TOO_SMALL) {
}
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
}
}
chk_rv = C_GetAttributeValue(hSession, hObject, &template[0], 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) {
for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
case CKA_LABEL:
printf(" [%lu] %20s: %.*s\n", hObject, pkcs11_attribute_to_name(curr_attr->type), (int) curr_attr->ulValueLen, (char *) curr_attr->pValue);
break;
case CKA_CLASS:
if (objectClass == CKO_PRIVATE_KEY) {
}
case CKA_TOKEN:
case CKA_ID:
case CKA_SERIAL_NUMBER:
case CKA_PRIVATE:
case CKA_CERTIFICATE_TYPE:
case CKA_KEY_TYPE:
case CKA_SIGN:
case CKA_DECRYPT:
case CKA_TRUSTED:
}
break;
case CKA_SUBJECT:
case CKA_ISSUER:
}
break;
default:
printf(" [%lu] %20s: %p/%lu\n", hObject, pkcs11_attribute_to_name(curr_attr->type), curr_attr->pValue, curr_attr->ulValueLen);
break;
}
} else {
}
}
} else {
printf("GetAttributeValue()/2 failed.\n");
}
} else {
printf("GetAttributeValue(hObject=%lu)/1 failed (rv = %lu).\n", (unsigned long) hObject, (unsigned long) chk_rv);
}
}
printf("FindObjectsFinal() failed.\n");
}
} else {
printf("FindObjectsInit() failed.\n");
}
printf("--- Operations ---\n");
signature_len = sizeof(signature);
chk_rv = C_Sign(hSession, (CK_BYTE_PTR) "Test", strlen("Test"), (CK_BYTE_PTR) &signature, &signature_len);
printf("[%04lu/%02lx] Signature: ", (unsigned long) *currPrivKey, (unsigned long) mechanism.mechanism);
}
printf("\n");
} else {
printf("Sign() failed.\n");
}
} else {
printf("SignInit() failed.\n");
}
}
encrypted_buflen = sizeof(encrypted_buf);
chk_rv = C_Encrypt(hSession, (CK_BYTE_PTR) "Test", strlen("Test"), encrypted_buf, &encrypted_buflen);
printf("[%04lu/%02lx] Encrypted(Test): ", (unsigned long) *currPrivKey, (unsigned long) mechanism.mechanism);
}
printf("\n");
} else {
printf("Encrypt() failed.\n");
}
} else {
printf("EncryptInit() failed.\n");
}
}
decrypted_buflen = sizeof(decrypted_buf);
chk_rv = C_Decrypt(hSession, (CK_BYTE_PTR) "\x4c\x36\x0f\x86\x2d\xb7\xb2\x46\x92\x11\x7e\x5f\xd1\xeb\x2c\xb0\xdb\x34\x60\xb8\x0c\xf8\x27\xb5\xfb\xce\xd1\xf4\x58\xa3\x20\x52\x9d\x97\x08\xd8\x2b\x5e\xb2\x37\x46\x72\x45\x7c\x66\x23\x53\xb5\xa5\x16\x61\x96\xbc\x5c\x8d\x85\x18\x24\xcf\x74\x7f\xc2\x23\x15\xd6\x42\x72\xa5\x2b\x29\x29\x1d\xa6\xea\x2b\xcb\x57\x59\xb3\x5f\xe2\xf8\x30\x12\x2f\x1b\xfa\xbd\xa9\x19\xef\x5c\xbb\x48\xdc\x28\x42\xdd\x90\xbe\x63\xeb\x59\x0c\xaf\x59\xcb\xe4\x6a\xf2\x56\x24\x41\xc2\x77\x7b\xc9\xf8\x02\x0f\x67\x3d\x2a\x98\x91\x14\xa2\x57", 128, decrypted_buf, &decrypted_buflen);
printf("[%04lu/%02lx] Decrypted(It works!): ", (unsigned long) *currPrivKey, (unsigned long) mechanism.mechanism);
}
printf("\n");
} else {
printf("Decrypt() failed.\n");
}
} else {
printf("DecryptInit() failed.\n");
}
}
printf("CloseSession failed.\n");
}
} else {
printf("OpenSession failed.\n");
}
if (slots) {
}
if (privateKeyObjects_root) {
}
return(0);
}
int main(void) {
printf("Testing libcoolkey...\n");
ck_retval = main_pkcs11();
if (ck_retval != 0) {
}
return(retval);
}