hasp_status_t openAoC(hasp_handle_t *handle) { hasp_handle_t h; hasp_status_t status; const char *scope = "<haspscope><hasp type='HASP-HL'><license_manager hostname='localhost'/></hasp></haspscope>"; /* login to HASP key and get session handle, status should be 0 on success */ status = hasp_login_scope(0, scope, vendor_code, &h); if (status != HASP_STATUS_OK) return status; /* initlialize VM engine */ status = hasp_vm_init(h, REDAppResourceId); if (status != HASP_STATUS_OK) { hasp_logout(h); return status; } /* pass back handle */ *handle = h; return HASP_STATUS_OK; } /* openAoC */
HASPKEY_API BOOL DC(void) { DWORD count; BOOL ret_val = FALSE; hasp_handle_t handle = HASP_INVALID_HANDLE_VALUE; EnterCriticalSection(&dec_critic); do { if (hasp_login(HASP_DEFAULT_FID, vendor_code, &handle) != HASP_STATUS_OK) break; if (hasp_read(handle, HASP_FILEID_RW, 0, 4, &count) != HASP_STATUS_OK) break; if (count == 0) break; count--; if (hasp_write(handle, HASP_FILEID_RW, 0, 4, &count) != HASP_STATUS_OK) break; ret_val = TRUE; } while(0); if (handle != HASP_INVALID_HANDLE_VALUE) hasp_logout(handle); LeaveCriticalSection(&dec_critic); return ret_val; }
void closeAoC(hasp_handle_t handle) { /* hasp_vm_close must be called to free vm engine for other processes or threads usage */ hasp_vm_close(handle); /* hasp_logout closes established session and releases allocated memory */ hasp_logout(handle); } /* closeAoC */
hasp_status_t openAoC(hasp_handle_t *handle) { hasp_handle_t h; hasp_status_t status; /* const char *scope = "<haspscope><hasp type='HASP-HL'><license_manager hostname='localhost'/></hasp></haspscope>"; */ /* only use Sentinel HL keys starting with firmware 4.50 */ const char *scope = "<haspscope><hasp type='HASP-HL'><hasp cmp='gt' version='4.50'><license_manager hostname='localhost'/></hasp></hasp></haspscope>"; /* use runtime-less API only; current LMS is not capable of new static VM function */ status = hasp_login(-4, 0, NULL); if (status != HASP_STATUS_OK) { #ifdef DEBUG printf("openAoC hasp_login(-4) failed with status: %d\n", status); #endif return status; } /* login to HASP key and get session handle, status should be 0 on success */ status = hasp_login_scope(0, scope, vendor_code, &h); if (status != HASP_STATUS_OK) { #ifdef DEBUG printf("openAoC hasp_login_scope() failed with status: %d\n", status); #endif return status; } /* initlialize VM engine */ status = hasp_vm_init(h, REDAppResourceId); if (status != HASP_STATUS_OK) { #ifdef DEBUG printf("openAoC hasp_vm_init() failed with status: %d\n", status); #endif hasp_logout(h); return status; } /* pass back handle */ *handle = h; return HASP_STATUS_OK; } /* openAoC */
int main(void) { hasp_status_t status; hasp_handle_t handle; hasp_u32_t vm_returncode = 0; hasp_size_t iolength = 0; unsigned char iobuffer[256] = { 0 }; char md5ResultBuffer[64] ={ 0 }; VM_IO_BUFFER_T iobuffer_cxt = { 0 }; char * szmd5str = "this is md5 test"; const char* scope = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" "<haspscope>" " <hasp type=\"HASP-HL\" >" " <license_manager hostname=\"localhost\" />" " </hasp>" "</haspscope>"; printf("A simple demo program for the Sentinel LDK licensing functions\n"); printf("Copyright (C) SafeNet, Inc. All rights reserved.\n\n"); /* Access Direct Mode (-4 mode) [TEST-ONLY] This mode force the API to always access directly DriverLess keys even if the local LM is recent enough to provide such support. To enable this special mode, you must use the undocumented call hasp_login(-4, 0, 0) before any other hasp functions. */ hasp_login(-4, 0, 0); /************************************************************************ * hasp_login_scope * establishes a context for HASP services * allows specification of several restrictions */ printf("restricting the license to 'local':\n"); status = hasp_login_scope(HASP_DEFAULT_FID, scope, (hasp_vendor_code_t *)vendor_code, &handle); switch (status) { case HASP_STATUS_OK: printf("OK\n"); break; case HASP_FEATURE_NOT_FOUND: printf("login to default feature failed\n"); break; case HASP_CONTAINER_NOT_FOUND: printf("no sentinel key with vendor code DEMOMA found\n"); break; case HASP_OLD_DRIVER: printf("outdated driver version installed\n"); break; case HASP_NO_DRIVER: printf("sentinel driver not installed\n"); break; case HASP_INV_VCODE: printf("invalid vendor code\n"); break; case HASP_INV_SCOPE: printf("invalid XML scope\n"); break; default: printf("login to default feature failed with status %d\n", status); } if (status) { exit(-1); } /************************************************************************ * Initlialize vm engine */ //md5 test status = hasp_vm_init(handle, VM_RESOURCE_ID); if (status) { hasp_logout(handle); printf("hasp_vm_init failed: %d!\n", status); exit(-1); } else { printf("hasp_vm_init succeeded!\n"); } /************************************************************************ /* Prepare input buffer and to invoke the method of vm resource */ iobuffer_cxt.cmdBuff = iobuffer; iobuffer_cxt.rspBuff = iobuffer; put_byte_array(&iobuffer_cxt, szmd5str, strlen(szmd5str)); put_byte_array(&iobuffer_cxt, md5ResultBuffer, 16); iolength = iobuffer_cxt.cmdSize; printf("public static void MD5Hash( String p1, byte[] p2 )\n"); status = hasp_vm_invoke(handle, VM_METHOD_ID,//method id iobuffer, &iolength, &vm_returncode); if (status) { hasp_logout(handle); printf("hasp_vm_invoke failed: %d!\n", status); exit(-1); } else { printf("hasp_vm_invoke succeeded!\n"); } get_byte_array(&iobuffer_cxt, md5ResultBuffer, 16); dump(md5ResultBuffer, 16, " "); /************************************************************************ /* hasp_vm_close must be called to free vm engine for other processes or threads usage*/ status = hasp_vm_close(handle); if (status) { hasp_logout(handle); printf("hasp_vm_close failed: %d!\n", status); exit(-1); } else { printf("hasp_vm_close succeeded!\n"); } /************************************************************************ * hasp_logout * closes established session and releases allocated memory */ printf("\nlogout : "); status = hasp_logout(handle); switch (status) { case HASP_STATUS_OK: printf("OK\n"); break; case HASP_INV_HND: printf("failed: handle not active\n"); break; default: printf("failed\n"); } if (status) { exit(-1); } /***********************************************************************/ wait4key("to close the sample"); return 0; } /* main */
HASPKEY_API encrypted_info_t RI(BYTE *crypt_iv) { char *info = NULL, *start = NULL, *end = NULL; hasp_handle_t handle = HASP_INVALID_HANDLE_VALUE; hasp_time_t htime; struct_info_t struct_info; encrypted_info_t encrypted_info; aes_context crypt_ctx; DWORD tot_len, pad_len; BYTE iv[16]; memcpy(iv, crypt_iv, 16); ZeroMemory(&struct_info, sizeof(struct_info)); struct_info.version = VERSION; ZeroMemory(&encrypted_info, sizeof(encrypted_info)); do { if(hasp_get_info("<haspscope />", "<haspformat root=\"rcs\"><hasp><attribute name=\"id\" /></hasp></haspformat>", vendor_code, &info) != HASP_STATUS_OK) { struct_info.error_code = ERROR_INFO; sprintf_s(struct_info.error_msg, sizeof(struct_info.error_msg), "cannot get info"); break; } if(!(start = strstr(info, "<hasp id=\""))) { struct_info.error_code = ERROR_PARSING; sprintf_s(struct_info.error_msg, sizeof(struct_info.error_msg), "bad parsing start"); break; } start += strlen("<hasp id=\""); if(!(end = strchr(start, '"'))) { struct_info.error_code = ERROR_PARSING; sprintf_s(struct_info.error_msg, sizeof(struct_info.error_msg), "bad parsing end"); break; } *end = '\0'; // Copio il seriale _snprintf_s(struct_info.serial, sizeof(struct_info.serial), _TRUNCATE, "%s", start); if (hasp_login(HASP_DEFAULT_FID, vendor_code, &handle) != HASP_STATUS_OK){ sprintf_s(struct_info.error_msg, sizeof(struct_info.error_msg), "cannot login with FID 0"); if (hasp_login(HASP_RCS_FID, vendor_code, &handle) != HASP_STATUS_OK){ struct_info.error_code = ERROR_LOGIN; sprintf_s(struct_info.error_msg, sizeof(struct_info.error_msg), "cannot login with FID 1"); break; } } if(hasp_get_rtc(handle, &htime) != HASP_STATUS_OK) { struct_info.error_code = ERROR_RTC; sprintf_s(struct_info.error_msg, sizeof(struct_info.error_msg), "cannot get rtc time"); break; } // Copio il timestamp struct_info.time = htime; // Copio il numero di agent rimasti per il pay-per-use if (hasp_read(handle, HASP_FILEID_RW, 0, 4, &struct_info.license_left) != HASP_STATUS_OK){ struct_info.error_code = ERROR_STORAGE; sprintf_s(struct_info.error_msg, sizeof(struct_info.error_msg), "cannot get storage value"); break; } } while(0); // Cifro tutta la struttura dentro encrypted_info.encrypted // inserendo il padding tot_len = sizeof(struct_info); tot_len/=16; tot_len++; tot_len*=16; pad_len = tot_len - sizeof(struct_info); memset(encrypted_info.encrypted, pad_len, tot_len); memcpy(encrypted_info.encrypted, &struct_info, sizeof(struct_info)); aes_set_key( &crypt_ctx, crypt_key, 128); aes_cbc_encrypt(&crypt_ctx, iv, encrypted_info.encrypted, encrypted_info.encrypted, tot_len); if(info) hasp_free(info); if(handle != HASP_INVALID_HANDLE_VALUE) hasp_logout(handle); return encrypted_info; }