/********************************************************************* * * _Echo1 * */ static int _Echo1(unsigned DeviceId) { int NumBytes2Send; int i; int r; char ac[100]; r = 0; if (USBBULK_OpenEx(DeviceId) == NULL) { _MessageBox("Unable to connect to USB BULK device"); return 1; } _ShowDriverInfo(); USBBULK_SetTimeoutEx(DeviceId, 3600 * 1000); printf("Enter the number of bytes to be send to the echo client: "); ac[0] = 96; _cgets(ac); NumBytes2Send = atoi(&ac[2]); for (i = 0; i < NumBytes2Send; i++) { char DataTx; DataTx = i % 255; if (_SendReceive1(DeviceId, DataTx)) { r = 1; break; } } if (r == 0) { printf ("\n%d bytes successfully transferred.", NumBytes2Send); } USBBULK_CloseEx(DeviceId); return r; }
/********************************************************************* * * main * */ int main(int argc, char* argv[]) { int r; U32 DeviceMask; char Restart; unsigned NumDevices; unsigned DeviceId; DeviceId = 0; // // Initialize first // USBHID_Init(USB_HID_DEFAULT_VENDOR_PAGE); // // Check first, how many devices are available. // Restart = 'N'; r = 1; // Set error so far do { char acRepeat[10]; NumDevices = USBHID_GetNumAvailableDevices(&DeviceMask); if (NumDevices == 1) { _ShowInfo(0); } else if (NumDevices) { printf("AvailableDevices = %d\n", NumDevices); for (r = 0; r < 32; r++) { if ((1 << r) & DeviceMask) { _ShowInfo(r); } } printf("To which device do you want to connect? "); scanf("%d", &DeviceId); } else { printf("No devices available\n"); break; } if (DeviceId < USB_MAX_DEVICES) { if ((1 << DeviceId) & DeviceMask) { printf("Starting Echo...\n"); r = _Echo1(DeviceId); if (r) { break; } printf("\nStart again? (y/n): "); acRepeat[0] = 6; _cgets(acRepeat); Restart = toupper(acRepeat[2]); if ((Restart != 'Y') && (Restart != 'N')) { Restart = 'Y'; } } } } while (Restart == 'Y'); if (r == 0) { printf("Communication with USB HID device succesful!"); } USBHID_Exit(); return r; }
static errno_t __cdecl _int_cgets_s (char *s, size_t l, size_t *r_len) { char *h, *p; if (s && l) s[0] = 0; if (!s || !l || !r_len) { _cgets (NULL); return EINVAL; } p = (char *) alloca (l + 2); p[0] = l; h = _cgets (s); if (!h) return EINVAL; *r_len = (size_t) p[1]; memcpy (s, &p[2], *r_len); return 0; }
/********************************************************************* * * _Echo1 * */ static int _Echo1(unsigned DeviceId) { int i; int r; int NumEchoes; char ac[100]; unsigned InputReportLength; unsigned OutputReportLength; U8 * pDataRx; U8 * pDataTx; r = 0; if (USBHID_Open(DeviceId) != 0) { _MessageBox("Unable to connect to USB HID device"); return 1; } // // Retrieve report sizes // InputReportLength = USBHID_GetInputReportSize(DeviceId); OutputReportLength = USBHID_GetOutputReportSize(DeviceId); // // Alloc memeory for in/out report pDataTx = (U8 *)calloc(OutputReportLength, sizeof(U8)); pDataRx = (U8 *)calloc(InputReportLength, sizeof(U8)); if ((pDataRx == NULL) || (pDataTx == NULL)) { printf("No memory available to create buffer for sending/receiving data\n"); r = 1; goto End; } printf("Enter the number of echoes to be sent to the echo client: "); ac[0] = 96; _cgets(ac); NumEchoes = atoi(&ac[2]); for (i = 0; i < NumEchoes; i++) { *pDataTx = i % 255; if (_SendReceive(DeviceId, pDataTx, OutputReportLength, pDataRx, InputReportLength)) { r = 1; break; } } if (r == 0) { printf ("\n%d echoes successfully transferred.", NumEchoes); } End: free(pDataTx); free(pDataRx); USBHID_Close(DeviceId); return r; }
/********************************************************************* * * main * */ int main(int argc, char* argv[]) { int r; U32 DeviceMask; char Restart; unsigned NumDevices; unsigned DeviceId; DeviceId = 0; NumDevices = USBBULK_GetNumAvailableDevices(&DeviceMask); if (NumDevices) { printf("Serial no of \n"); for (r = 0; r < 32; r++) { char ac[255]; if ((1 << r) & DeviceMask) { USBBULK_GetSN(r, ac, sizeof(ac)); printf("Device %d: %s\n", r, ac); } } } Restart = 'N'; do { char acRepeat[10]; printf("To which device do you want to connect?\nPlease type in device number (e.g. '0' for the first device):"); scanf("%d", &DeviceId); if (DeviceId < USB_MAX_DEVICES) { if ((1 << DeviceId) & DeviceMask) { printf("Starting Echo...\n"); r = _Echo1(DeviceId); if (r) { break; } printf("\nStart again? (y/n): "); acRepeat[0] = 6; _cgets(acRepeat); Restart = toupper(acRepeat[2]); if ((Restart != 'Y') && (Restart != 'N')) { Restart = 'Y'; } } } } while (Restart == 'Y'); if (r == 0) { printf("Communication with USB BULK device succesful!"); } return r; }
int main(int argc, char** argv, char** envp) { extern char *optarg; extern int optind; CK_RV rv = CKR_OK; CK_ULONG ulSlotCount; CK_SLOT_ID_PTR pSlotList; int i; rv = C_GetFunctionList(&pFunctionList); if(rv != CKR_OK) { printf("could not get function pointer list: %ld\n",rv); exit(1); } rv = (pFunctionList->C_Initialize)(NULL_PTR); if(rv != CKR_OK) { printf("could not initialize: %ld\n",rv); exit(1); } /* get the slot list with token. this is not a test of C_OpenSession, * thus only open sessions on token and not on slots */ rv = (pFunctionList->C_GetSlotList)(TRUE,NULL_PTR,&ulSlotCount); if(rv != CKR_OK) { printf("FAIL: could not get slot count: %ld\n",rv); exit(1); } pSlotList = malloc(sizeof(CK_SLOT_ID)*ulSlotCount); if(pSlotList == NULL) { printf("could not allocate slot list: %d\n",CKR_HOST_MEMORY); exit(1); } rv = (pFunctionList->C_GetSlotList)(TRUE,pSlotList,&ulSlotCount); if(rv != CKR_OK) { printf("FAIL: could not get slot List: %ld\n",rv); exit(1); } for(i=0;i<ulSlotCount;i++) { CK_SESSION_HANDLE sess_A, sess_B; CK_OBJECT_HANDLE obj_handle; printf("starting with token on slot %ld\n", pSlotList[i]); /* open session A */ rv = (pFunctionList->C_OpenSession)(pSlotList[i], CKF_SERIAL_SESSION, NULL,NULL,&sess_A); if(rv != CKR_OK) { printf("FAIL: C_OpenSession for Session A failed on slot %ld: %ld\n",pSlotList[i],rv); continue; } rv = (pFunctionList->C_OpenSession)(pSlotList[i], CKF_SERIAL_SESSION, NULL,NULL,&sess_B); if(rv != CKR_OK) { printf("FAIL: C_OpenSession for Session B failed on slot %ld for encryption: %ld\n", pSlotList[i],rv); continue; } /* open object in A */ /* destroy object in B */ /* close sessions */ rv = (pFunctionList->C_CloseSession)(sess_A); if(rv != CKR_OK) { printf("FAIL: C_CloseSession in session %ld (session A): %ld\n",sess_A,rv); continue; } rv = (pFunctionList->C_CloseSession)(sess_B); if(rv != CKR_OK) { printf("FAIL: C_CloseSession in session %ld (session B): %ld\n",sess_B,rv); continue; } printf("slot %ld done\n", pSlotList[i]); } /* for each slot */ free(pSlotList); (pFunctionList->C_Finalize)(NULL); #ifdef CK_Win32 { char buf[3]={1}; printf("weiter mit return"); _cgets(buf); } #endif return 0; }
int main(int argc, char** argv, char** envp) { extern char *optarg; extern int optind; CK_RV rv = CKR_OK; if(argc != 2) { printf("Wrong number of arguments\nusage: %s <user_pin>\n",argv[0]); exit(1); } rv = C_GetFunctionList(&pFunctionList); if(rv != CKR_OK) { printf("FAIL: could not get function pointer list: %ld\n",rv); exit(1); } rv = (pFunctionList->C_Initialize)(NULL_PTR); if(rv != CKR_OK) { printf("FAIL: could not initialize: %ld\n",rv); exit(1); } /* get the slot list with token. this is not a test of C_OpenSession, * thus only open sessions on token and not on slots */ { CK_ULONG ulSlotCount; CK_SLOT_ID_PTR pSlotList; int i; rv = (pFunctionList->C_GetSlotList)(TRUE,NULL_PTR,&ulSlotCount); if(rv != CKR_OK) { printf("FAIL: could not get slot count: %ld\n",rv); exit(1); } pSlotList = malloc(sizeof(CK_SLOT_ID)*ulSlotCount); if(pSlotList == NULL) { printf("could not allocate slot list: %d\n",CKR_HOST_MEMORY); exit(1); } rv = (pFunctionList->C_GetSlotList)(TRUE,pSlotList,&ulSlotCount); if(rv != CKR_OK) { printf("FAIL: could not get slot List: %ld\n",rv); exit(1); } /* for each slot with a token */ for(i=0;(unsigned long)i<ulSlotCount;i++) { CK_SESSION_HANDLE sign_sess=0, verify_sess=0; CK_ULONG mech_count; CK_MECHANISM_TYPE_PTR mech_list; CK_ULONG j,k, pub_handle_count,priv_handle_count; CK_BBOOL do_token; CK_OBJECT_HANDLE_PTR pub_handle_arr; CK_OBJECT_HANDLE_PTR priv_handle_arr; CK_SLOT_ID verify_slot; printf("starting with token on slot %ld\n", pSlotList[i]); /* check that the token supports RSA sign */ /* first only get the length of the list */ (pFunctionList->C_GetMechanismList)(pSlotList[i], NULL_PTR, &mech_count); mech_list = malloc(mech_count * sizeof(CK_MECHANISM_TYPE)); if(mech_list == NULL_PTR) { printf("FAIL: mem alloc\n"); exit(1); } (pFunctionList->C_GetMechanismList)(pSlotList[i],mech_list, &mech_count); /* look for the RSA operations */ for(j=0,do_token=FALSE;j<mech_count;j++) if(mech_list[j] == CKM_RSA_PKCS) do_token = TRUE; if(!do_token) { printf("FAIL: no RSA PKCS#11 on slot %lu\n",pSlotList[i]); continue; } /* clean up */ free(mech_list); /* open session */ rv = (pFunctionList->C_OpenSession)(pSlotList[i], CKF_SERIAL_SESSION, NULL,NULL,&sign_sess); if(rv != CKR_OK) { printf("FAIL: C_OpenSession failed on slot %ld: %ld\n",pSlotList[i],rv); continue; } // To be able to retrieve private objects from the token, the user must be authenticated */ rv = (pFunctionList->C_Login) (sign_sess, CKU_USER, argv[1], 8); if(rv != CKR_OK) { printf("Bad login, rv: %d",rv); exit(1); } /* find the public keys */ rv = pkcs11_find_object(sign_sess, CK_I_rsa_public_key_template, CK_I_rsa_public_key_count , &pub_handle_arr, &pub_handle_count); if(rv != CKR_OK) { printf("FAIL: unable to find public key: 0x%lx\n",rv); continue; } /* find a token that supports RSA_PKCS for verify */ rv = find_rsa_verify(pFunctionList,&verify_slot); if(rv != CKR_OK) { printf("FAIL: unable to find token for verify: 0x%lx. Cannot verify the signature\n",rv); verify_sess = 0; } else { if (verify_slot == pSlotList[i]) verify_sess = sign_sess; else { rv = (pFunctionList->C_OpenSession)(verify_slot, CKF_SERIAL_SESSION|CKF_RW_SESSION, NULL,NULL,&verify_sess); if(rv != CKR_OK) { printf("FAIL: C_OpenSession failed on slot %ld for verify: %ld\n", pSlotList[i],rv); continue; } } } for(k=0; k<pub_handle_count; k++) { CK_ATTRIBUTE theTemplate = {CKA_ID, NULL, 0}; CK_CHAR_PTR clear_text1 ="foobar1"; CK_CHAR_PTR clear_text2 ="This is a text that is too long even for a 1k key: The quick brown fox jumps over the lazy dog. 1234567890"; CK_OBJECT_HANDLE new_pub_handle=0; /* set the ID of the public key in the private key */ rv = (pFunctionList->C_GetAttributeValue)(sign_sess, pub_handle_arr[k], &theTemplate, 1); if(rv != CKR_OK) { printf("FAIL: unable read id length for public key %li: 0x%lx\n",pub_handle_arr[k],rv); continue; } theTemplate.pValue = malloc(theTemplate.ulValueLen); if(theTemplate.pValue == NULL_PTR) { printf("FAIL: unable allocate memory for private key id\n"); continue; } rv = (pFunctionList->C_GetAttributeValue)(sign_sess, pub_handle_arr[k], &theTemplate, 1); if(rv != CKR_OK) { printf("FAIL: unable to read id for public key %li: 0x%lx\n", pub_handle_arr[k],rv); continue; } rv = set_attribute(CK_I_rsa_private_key_template, CK_I_rsa_private_key_count, CKA_ID, theTemplate.pValue, theTemplate.ulValueLen); if(rv != CKR_OK) { printf("FAIL: unable to set id in templatee for private key: 0x%lx\n",rv); continue; } /* find the matching private key */ rv = pkcs11_find_object(sign_sess, CK_I_rsa_private_key_template, CK_I_rsa_private_key_count, &priv_handle_arr, &priv_handle_count); if(rv != CKR_OK) { printf("FAIL: unable to find public key: 0x%lx\n",rv); continue; } /* is there exactly one (1) private key to this public key? */ if(priv_handle_count != 1) { printf("FAIL: wrong number of private keys: %li\n",priv_handle_count); continue; } /* copy the public key if needed */ if(sign_sess != verify_sess) { if ( verify_sess != 0 ) rv = P11_loadPubRSAKey(pFunctionList, sign_sess, pFunctionList, verify_sess, pub_handle_arr[k], &new_pub_handle); else new_pub_handle = 0; } else new_pub_handle = pub_handle_arr[k]; /* sign test vector */ rv = sign_and_verify(sign_sess,verify_sess,clear_text1, priv_handle_arr[0],new_pub_handle, argv[1]); if(rv != CKR_OK) continue; if ( verify_sess != 0 ) printf("sign and verify succeded for short text session %ld: 0x%08lx\n", sign_sess,rv); else printf("sign succeded for short text session %ld: 0x%08lx\n", sign_sess,rv); rv = sign_and_verify(sign_sess,verify_sess,clear_text2, priv_handle_arr[0],new_pub_handle, argv[1]); if(rv == CKR_OK) { printf("FAIL: over long clear text did not fail\n"); continue; } if ( verify_sess != 0 ) printf("sign and verify succeded in session %ld: 0x%08lx\n", sign_sess,rv); else printf("sign succeded in session %ld: 0x%08lx\n", sign_sess,rv); } /* for each public key */ /* close session */ rv = (pFunctionList->C_CloseSession)(sign_sess); if(rv != CKR_OK) { printf("FAIL: C_CloseSession in session %ld: %ld\n",sign_sess,rv); continue; } printf("slot %ld done\n", pSlotList[i]); } free(pSlotList); } (pFunctionList->C_Finalize)(NULL); #ifdef CK_Win32 { char buf[3]={1}; printf("\nPress any key to continue..."); _cgets(buf); } #endif return 0; }