From 741900e724a879e9ffa0c1a35036949df64a95be Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Mon, 26 Sep 2011 00:53:35 +0300 Subject: [PATCH 01/16] various differences - fix 64bit problem - install files to /usr, not /usr/local --- CMakeLists.txt | 1 + libusbk/CMakeLists.txt | 4 ++-- man/CMakeLists.txt | 4 ++-- usbk_cli/CMakeLists.txt | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32e9ae2..3ca8f00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ IF (UNIX) ARGS -Rf CMakeTmp ${DISTCLEANED} TARGET distclean ) + ENDIF(UNIX) # Settings for debian package diff --git a/libusbk/CMakeLists.txt b/libusbk/CMakeLists.txt index 2c1fb68..6db3458 100644 --- a/libusbk/CMakeLists.txt +++ b/libusbk/CMakeLists.txt @@ -86,7 +86,7 @@ IF(UNIX AND NOT APPLE) # Install this library INSTALL(TARGETS ${PROJECT_NAME} - LIBRARY DESTINATION lib + LIBRARY DESTINATION /usr/lib PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) INSTALL(FILES src/libusbk.h @@ -157,4 +157,4 @@ ELSEIF(APPLE) ELSE(UNIX AND NOT APPLE) message(FATAL_ERROR "Undefined Operating System") -ENDIF(UNIX AND NOT APPLE) \ No newline at end of file +ENDIF(UNIX AND NOT APPLE) diff --git a/man/CMakeLists.txt b/man/CMakeLists.txt index 5e2f756..7d9bf27 100644 --- a/man/CMakeLists.txt +++ b/man/CMakeLists.txt @@ -27,5 +27,5 @@ ADD_CUSTOM_TARGET(man ALL # Installation of the manuals INSTALL(FILES ${PROJECT_BINARY_DIR}/${usbk_man_gz} - DESTINATION "share/man/man1" - PERMISSIONS OWNER_READ GROUP_READ WORLD_READ) \ No newline at end of file + DESTINATION "/usr/share/man/man1" + PERMISSIONS OWNER_READ GROUP_READ WORLD_READ) diff --git a/usbk_cli/CMakeLists.txt b/usbk_cli/CMakeLists.txt index 1a182a0..1adbfb8 100644 --- a/usbk_cli/CMakeLists.txt +++ b/usbk_cli/CMakeLists.txt @@ -82,7 +82,7 @@ SET_TARGET_PROPERTIES(${PROJECT_NAME} # Install project INSTALL(TARGETS ${PROJECT_NAME} RUNTIME - DESTINATION bin + DESTINATION /usr/bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) From a5f90db269300eb2bd5491c0c3e1b717a4f2fcfe Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Mon, 26 Sep 2011 00:58:46 +0300 Subject: [PATCH 02/16] add TODO --- TODO | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 0000000..08c4619 --- /dev/null +++ b/TODO @@ -0,0 +1,7 @@ + - unify linux/windows usbk_sg_ctl.[h|c] sources and make differences with MACROS + - make libusbk fully c source since no need to use c++ + - make variables fixed sizes (eg. use int32_t or int64_t instead of int (except file descriptors) ) + + - c++ bindings can be written, but first qusbk project must be started! + + From 7c3f6a6529d0716527d993937f3414579547233c Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Mon, 26 Sep 2011 01:00:28 +0300 Subject: [PATCH 03/16] update CMakeList.txt to remove install_manifeset.txt file at "distclean" operation --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ca8f00..31f206a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,8 @@ IF (UNIX) cmake.check_depends CMakeCache.txt */CMakeCache.txt + install_manifest.txt + cmake.check_cache *.cmake Makefile From 642b92eba22f47bfd4bf9fd99aef4d3a175f1fdf Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Mon, 26 Sep 2011 22:27:34 +0300 Subject: [PATCH 04/16] fix libusbk.h Writing functions inside libusbk.h header causes multiple definitions if two or more module includes libusbk. When a module includes libusbk.h, functions includes in the module too, and if another module includes libusbk.h then other all functions will be placed that module again and linker causes multiple definition functions error. headers should contain just prototypes of the functions. --- libusbk/CMakeLists.txt | 2 +- libusbk/src/libusbk.cpp | 14 ++++++++++++++ libusbk/src/libusbk.h | 26 +++++++++++++------------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/libusbk/CMakeLists.txt b/libusbk/CMakeLists.txt index 6db3458..fde2820 100644 --- a/libusbk/CMakeLists.txt +++ b/libusbk/CMakeLists.txt @@ -90,7 +90,7 @@ IF(UNIX AND NOT APPLE) PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) INSTALL(FILES src/libusbk.h - DESTINATION include + DESTINATION /usr/include PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) IF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) diff --git a/libusbk/src/libusbk.cpp b/libusbk/src/libusbk.cpp index 87f5118..d0d408f 100644 --- a/libusbk/src/libusbk.cpp +++ b/libusbk/src/libusbk.cpp @@ -1073,3 +1073,17 @@ const char* usbk_libversion(void){ } +int usbk_assignpassword(USBK* usbk, const char* new_pass) {return usbk_changepassword(usbk, NULL, new_pass) ;} +int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) {return usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL) ;} +int usbk_setkey_128bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} +int usbk_setkey_192bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} +int usbk_setkey_256bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} +int usbk_setkey_128bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} +int usbk_setkey_192bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} +int usbk_setkey_256bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} +int usbk_setkey_128bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} +int usbk_setkey_192bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} +int usbk_setkey_256bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} +int usbk_enableautact(USBK* usbk, const char* pass, uint8_t key_no) {return usbk_setautact(usbk, pass, key_no) ;} +int usbk_disableautact(USBK* usbk, const char* pass) {return usbk_setautact(usbk, pass, 0); ;} + diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index 81494fd..e319395 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -95,19 +95,19 @@ extern int usbk_setautact(USBK* usbk, const char* pass, int key_no); extern int usbk_getrandomkey(USBK* usbk, uint8_t*random_key, KEYSIZE keysize); extern int usbk_refreshusbkinfo(USBK* usbk); -int usbk_assignpassword(USBK* usbk, const char* new_pass) {return usbk_changepassword(usbk, NULL, new_pass) ;} -int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) {return usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL) ;} -int usbk_setkey_128bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} -int usbk_setkey_192bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} -int usbk_setkey_256bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} -int usbk_setkey_128bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} -int usbk_setkey_192bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} -int usbk_setkey_256bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} -int usbk_setkey_128bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} -int usbk_setkey_192bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} -int usbk_setkey_256bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} -int usbk_enableautact(USBK* usbk, const char* pass, uint8_t key_no) {return usbk_setautact(usbk, pass, key_no) ;} -int usbk_disableautact(USBK* usbk, const char* pass) {return usbk_setautact(usbk, pass, 0); ;} +extern int usbk_assignpassword(USBK* usbk, const char* new_pass); +extern int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); +extern int usbk_setkey_128bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key); +extern int usbk_setkey_192bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key); +extern int usbk_setkey_256bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key); +extern int usbk_setkey_128bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key); +extern int usbk_setkey_192bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key); +extern int usbk_setkey_256bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key); +extern int usbk_setkey_128bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key); +extern int usbk_setkey_192bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key); +extern int usbk_setkey_256bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key); +extern int usbk_enableautact(USBK* usbk, const char* pass, uint8_t key_no); +extern int usbk_disableautact(USBK* usbk, const char* pass); #define USBK_ASSIGN_PASSWORD(usbk, new_pass) usbk_changepassword(usbk, NULL, new_pass) #define USBK_SETKEY_NAME(usbk, pass, key_no, key_name) usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL) From 22795af8ac3afe1d1578e94e1b37da83b2a5c72a Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Tue, 27 Sep 2011 23:58:50 +0300 Subject: [PATCH 05/16] various changes - some functions should be inline - remove unneccessary library functions --- libusbk/src/libusbk.cpp | 164 +++++++++----------------------- libusbk/src/libusbk.h | 102 ++++++++++---------- libusbk/src/linux/usbk_sg_ctl.c | 4 +- libusbk/src/linux/usbk_sg_ctl.h | 8 +- libusbk/src/uip.h | 2 + libusbk/src/usbk_scsi.c | 8 +- usbk_cli/src/main.cpp | 4 +- 7 files changed, 110 insertions(+), 182 deletions(-) diff --git a/libusbk/src/libusbk.cpp b/libusbk/src/libusbk.cpp index d0d408f..2c4e174 100644 --- a/libusbk/src/libusbk.cpp +++ b/libusbk/src/libusbk.cpp @@ -33,6 +33,35 @@ #include "libusbk.h" #include "usbk_scsi.h" +typedef struct __USBK +{ + int32_t lastopr; + + char *dev; + char *dev_path; + char *backdisk; + char *backdisk_path; + + bool supported; + + char *product; + char *model; + char *serial; + char *usb_serial; + char *firmware_ver; + int32_t multikey_cap; + char *dev_label; + USBK_DS dev_state; + int current_key; + int autoact_keyno; + int retry_num; + char **key_names; + + struct __USBK *next; + struct __USBK *previous; + +} USBK; + using namespace std; #if defined(__linux__) @@ -115,35 +144,6 @@ LIBUSBK_SUPPORTED_PRODUCTS products[] = { #define USBK_SCSI_VENDOR "USBK" #define USBK_SCSI_BACKDISK_VENDOR "BackDisk" -typedef struct __USBK -{ - int32_t lastopr; - - char *dev; - char *dev_path; - char *backdisk; - char *backdisk_path; - - bool supported; - - char *product; - char *model; - char *serial; - char *usb_serial; - char *firmware_ver; - int32_t multikey_cap; - char *dev_label; - USBK_DS dev_state; - int current_key; - int autoact_keyno; - int retry_num; - char **key_names; - - struct __USBK *next; - struct __USBK *previous; - -}USBK; - static bool debug_enable = false; @@ -362,6 +362,10 @@ int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass) return (-1) * usbk->lastopr; } +int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) { + return usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL); +} + int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label) { int rtn; t_UIP_SETDEVICELABEL devlabel; @@ -702,78 +706,6 @@ static unsigned int keysize_byte(KEYSIZE keysize) { return size_byte; } -const char* usbk_get_dev(USBK* usbk){ - return usbk->dev; -} - -const char* usbk_get_dev_path(USBK* usbk){ - return usbk->dev_path; -} - -const char* usbk_get_backdisk(USBK* usbk){ - return usbk->backdisk; -} - -const char* usbk_get_backdisk_path(USBK* usbk){ - return usbk->backdisk_path; -} - -const char* usbk_get_product(USBK* usbk){ - return usbk->product; -} - -const char* usbk_get_model(USBK* usbk){ - return usbk->model; -} - -const char* usbk_get_serial(USBK* usbk){ - return usbk->serial; -} - -const char* usbk_get_usb_serial(USBK* usbk){ - return usbk->usb_serial; -} - -const char* usbk_get_firmware_ver(USBK* usbk){ - return usbk->firmware_ver; -} - -const char* usbk_get_dev_label(USBK* usbk){ - return usbk->dev_label; -} - -const char* usbk_get_keyname(USBK* usbk, int key_no){ - return usbk->key_names[key_no]; -} - -int usbk_get_lastopr_status(USBK* usbk){ - return usbk->lastopr; -} - -bool usbk_check_support(USBK* usbk){ - return usbk->supported; -} - -int usbk_get_multikeycap(USBK* usbk){ - return usbk->multikey_cap; -} - -int usbk_get_current_keyno(USBK* usbk){ - return usbk->current_key; -} - -int usbk_get_autoactivation_keyno(USBK* usbk){ - return usbk->autoact_keyno; -} - -int usbk_get_retry_number(USBK* usbk){ - return usbk->retry_num; -} - -USBK_DS usbk_get_state(USBK* usbk){ - return usbk->dev_state; -} - #if defined(__linux__) static int getudevinfo(USBK* usbk, const char *device) { int rtn; @@ -1054,36 +986,28 @@ int usbk_list_get_lastoprstatus(USBKS* usbks){ #endif -void usbk_debug_enable(void){ +void usbk_debug_enable(void) +{ debug_enable = true; } -void usbk_debug_disable(void){ + +void usbk_debug_disable(void) +{ debug_enable = false; } -void usbk_debug(bool enable){ + +void usbk_debug(bool enable) +{ debug_enable = enable; } + bool usbk_debug_check(void) { return debug_enable; } -const char* usbk_libversion(void){ +const char* usbk_libversion(void) +{ return VERSION; } - -int usbk_assignpassword(USBK* usbk, const char* new_pass) {return usbk_changepassword(usbk, NULL, new_pass) ;} -int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) {return usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL) ;} -int usbk_setkey_128bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} -int usbk_setkey_192bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} -int usbk_setkey_256bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key) {return usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} -int usbk_setkey_128bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} -int usbk_setkey_192bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} -int usbk_setkey_256bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} -int usbk_setkey_128bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_128BIT, key) ;} -int usbk_setkey_192bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_192BIT, key) ;} -int usbk_setkey_256bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key) {return usbk_setkey_text(usbk, pass, key_no, KEYSIZE_256BIT, key) ;} -int usbk_enableautact(USBK* usbk, const char* pass, uint8_t key_no) {return usbk_setautact(usbk, pass, key_no) ;} -int usbk_disableautact(USBK* usbk, const char* pass) {return usbk_setautact(usbk, pass, 0); ;} - diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index e319395..e975898 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -29,8 +29,6 @@ extern "C" { # error unsupported environment #endif -#define NB_AESKEY 3 - #if defined(__linux__) # define USBK_ATTRIBUTE_PACKED __attribute__((__packed__)) #else @@ -79,36 +77,6 @@ typedef enum __USBK_LASTOPR typedef struct __USBK USBK; -USBK* usbk_new(const char* dev); - -extern int usbk_release(USBK* usbk); -extern int usbk_activatekey(USBK* usbk, const char* pass, uint8_t key_no); -extern int usbk_deactivatekey(USBK* usbk); -extern int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass); -extern int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label); -extern int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); -extern int usbk_setkey_keyname(USBK* usbk, const char* pass, int key_no, const char* key_name, KEYSIZE key_size, const uint8_t* key); -extern int usbk_setkey_hex(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const uint8_t* key); -extern int usbk_setkey_decimal(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const char* key); -extern int usbk_setkey_text(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const char* key); -extern int usbk_setautact(USBK* usbk, const char* pass, int key_no); -extern int usbk_getrandomkey(USBK* usbk, uint8_t*random_key, KEYSIZE keysize); -extern int usbk_refreshusbkinfo(USBK* usbk); - -extern int usbk_assignpassword(USBK* usbk, const char* new_pass); -extern int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); -extern int usbk_setkey_128bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key); -extern int usbk_setkey_192bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key); -extern int usbk_setkey_256bit_hex(USBK* usbk, const char* pass, uint8_t key_no, uint8_t* key); -extern int usbk_setkey_128bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key); -extern int usbk_setkey_192bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key); -extern int usbk_setkey_256bit_decimal(USBK* usbk, const char* pass, uint8_t key_no, const char* key); -extern int usbk_setkey_128bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key); -extern int usbk_setkey_192bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key); -extern int usbk_setkey_256bit_text(USBK* usbk, const char* pass, uint8_t key_no, const char* key); -extern int usbk_enableautact(USBK* usbk, const char* pass, uint8_t key_no); -extern int usbk_disableautact(USBK* usbk, const char* pass); - #define USBK_ASSIGN_PASSWORD(usbk, new_pass) usbk_changepassword(usbk, NULL, new_pass) #define USBK_SETKEY_NAME(usbk, pass, key_no, key_name) usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL) #define USBK_SETKEY_128BIT_HEX(usbk, pass, key_no, key) usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_128BIT, key) @@ -123,28 +91,38 @@ extern int usbk_disableautact(USBK* usbk, const char* pass); #define USBK_ENABLE_AUTACT(usbk, pass, key_no) usbk_setautact(usbk, pass, key_no) #define USBK_DISABLE_AUTACT(usbk, pass) usbk_setautact(usbk, pass, 0) -extern const char* usbk_get_dev(USBK* usbk); -extern const char* usbk_get_dev_path(USBK* usbk); -extern const char* usbk_get_backdisk(USBK* usbk); -extern const char* usbk_get_backdisk_path(USBK* usbk); -extern const char* usbk_get_product(USBK* usbk); -extern const char* usbk_get_model(USBK* usbk); -extern const char* usbk_get_serial(USBK* usbk); -extern const char* usbk_get_usb_serial(USBK* usbk); -extern const char* usbk_get_firmware_ver(USBK* usbk); -extern const char* usbk_get_dev_label(USBK* usbk); -extern const char* usbk_get_keyname(USBK* usbk, int key_no); - -extern int usbk_get_lastopr_status(USBK* usbk); - -extern bool usbk_check_support(USBK* usbk); -extern int usbk_get_multikeycap(USBK* usbk); -extern int usbk_get_current_keyno(USBK* usbk); -extern int usbk_get_autoactivation_keyno(USBK* usbk); -extern int usbk_get_retry_number(USBK* usbk); -extern USBK_DS usbk_get_state(USBK* usbk); - +USBK* usbk_new(const char* dev); +extern int usbk_release(USBK* usbk); +/* + * All the getter functions + * */ + +/* This is the helper macro for reaching the USBK structure */ +#define USBK_GET_ELEMENT(usbk, element) (usbk->element) + +inline const char* usbk_get_dev(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev); } +inline const char* usbk_get_dev_path(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_path); } +inline const char* usbk_get_backdisk(USBK* usbk) { return USBK_GET_ELEMENT(usbk, backdisk); } +inline const char* usbk_get_backdisk_path(USBK* usbk) { return USBK_GET_ELEMENT(usbk, backdisk_path); } +inline const char* usbk_get_product(USBK* usbk) { return USBK_GET_ELEMENT(usbk, product); } +inline const char* usbk_get_model(USBK* usbk) { return USBK_GET_ELEMENT(usbk, model); } +inline const char* usbk_get_serial(USBK* usbk) { return USBK_GET_ELEMENT(usbk, serial); } +inline const char* usbk_get_usb_serial(USBK* usbk) { return USBK_GET_ELEMENT(usbk, usb_serial); } +inline const char* usbk_get_firmware_ver(USBK* usbk) { return USBK_GET_ELEMENT(usbk, firmware_ver); } +inline const char* usbk_get_dev_label(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_label); } +inline const char* usbk_get_keyname(USBK* usbk, int key_no) { return USBK_GET_ELEMENT(usbk, key_names[key_no]); } + +inline int usbk_get_lastopr_status(USBK* usbk) { return USBK_GET_ELEMENT(usbk, lastopr); } + +inline bool usbk_check_support(USBK* usbk) { return USBK_GET_ELEMENT(usbk, supported); } +inline int usbk_get_multikeycap(USBK* usbk) { return USBK_GET_ELEMENT(usbk, multikey_cap); } +inline int usbk_get_current_keyno(USBK* usbk) { return USBK_GET_ELEMENT(usbk, current_key); } +inline int usbk_get_autoactivation_keyno(USBK* usbk) { return USBK_GET_ELEMENT(usbk, autoact_keyno); } +inline int usbk_get_retry_number(USBK* usbk) { return USBK_GET_ELEMENT(usbk, retry_num); } +inline USBK_DS usbk_get_state(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_state); } + +/* Debugging informations */ extern void usbk_debug_enable(void); extern void usbk_debug_disable(void); extern void usbk_debug(bool enable); @@ -152,6 +130,24 @@ extern bool usbk_debug_check(void); extern const char* usbk_libversion(void); +/* + * And all the setter and other functions which interacts + * directly with USBK + * + * */ +extern int usbk_activatekey(USBK* usbk, const char* pass, uint8_t key_no); +extern int usbk_deactivatekey(USBK* usbk); +extern int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass); +extern int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label); +extern int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); +extern int usbk_setkey_keyname(USBK* usbk, const char* pass, int key_no, const char* key_name, KEYSIZE key_size, const uint8_t* key); +extern int usbk_setkey_hex(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const uint8_t* key); +extern int usbk_setkey_decimal(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const char* key); +extern int usbk_setkey_text(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const char* key); +extern int usbk_setautact(USBK* usbk, const char* pass, int key_no); +extern int usbk_getrandomkey(USBK* usbk, uint8_t*random_key, KEYSIZE keysize); +extern int usbk_refreshusbkinfo(USBK* usbk); + #if defined(__linux__) typedef struct __USBKS USBKS; diff --git a/libusbk/src/linux/usbk_sg_ctl.c b/libusbk/src/linux/usbk_sg_ctl.c index 9e8b4b2..e41e609 100644 --- a/libusbk/src/linux/usbk_sg_ctl.c +++ b/libusbk/src/linux/usbk_sg_ctl.c @@ -30,9 +30,9 @@ static int sg_fd; //PUBLIC FUNCTIONS -int usbk_sg_open(const char* DevicePath) +int usbk_sg_open(const char* device_path) { - sg_fd = open(DevicePath , O_RDWR); + sg_fd = open(device_path, O_RDWR); if (sg_fd < 0) { fprintf(stderr, "Error! Device not found!"); diff --git a/libusbk/src/linux/usbk_sg_ctl.h b/libusbk/src/linux/usbk_sg_ctl.h index 8152d0d..5a12447 100644 --- a/libusbk/src/linux/usbk_sg_ctl.h +++ b/libusbk/src/linux/usbk_sg_ctl.h @@ -35,7 +35,8 @@ typedef enum __USBK_SG_RET //! @} //PUBLIC STRUCTURES -typedef struct __ST_CMD { +typedef struct __ST_CMD +{ char opcode; char lun; char v_opcode; @@ -48,7 +49,8 @@ typedef struct __ST_CMD { char cntrl; } ST_CMD_T; -typedef struct __ST_PACKET { +typedef struct __ST_PACKET +{ int sg_fd; char cmdtype; int cmddir; @@ -75,7 +77,7 @@ typedef struct __ST_PACKET { * \ingroup ScsiGeneric * */ -int usbk_sg_open(const char* DevicePath); +int usbk_sg_open(const char* device_path); /** diff --git a/libusbk/src/uip.h b/libusbk/src/uip.h index 1fb742c..f71c0a3 100644 --- a/libusbk/src/uip.h +++ b/libusbk/src/uip.h @@ -63,6 +63,8 @@ # error unsupported environment #endif +#define NB_AESKEY 3 + #define PROTOCOL_HEADER "USBK" typedef uint32_t t_UIP_TAGNUMBER; diff --git a/libusbk/src/usbk_scsi.c b/libusbk/src/usbk_scsi.c index c964abe..49008d0 100644 --- a/libusbk/src/usbk_scsi.c +++ b/libusbk/src/usbk_scsi.c @@ -22,6 +22,8 @@ #include "usbk_scsi.h" #include "usbk_sg_ctl.h" +#define _PACK_BUFF_SIZE 512 + //PRIVATE VARIABLES //-VENDOR SPECIFIC CMD COMMAND const ST_CMD_T scsi_cmd[9] = { @@ -41,7 +43,7 @@ int send_scsi_command(const char *usbk_path, unsigned char *buff, int cmd_index, { short int cmdlen = sizeof(ST_CMD_T); ST_PACKET_T packet; - unsigned char buffer[512]; + unsigned char buffer[_PACK_BUFF_SIZE]; if (usbk_sg_open(usbk_path) < 0) { fprintf(stderr, "Error! Wrong device name or you don't have root permission!"); @@ -60,7 +62,7 @@ int send_scsi_command(const char *usbk_path, unsigned char *buff, int cmd_index, packet.cmdlen = cmdlen; packet.cmddir = (rw == WRITE_SCSI) ? OUTDIR : INDIR; packet.data = buffer; - packet.datalen = 512; + packet.datalen = _PACK_BUFF_SIZE; if (usbk_sg_tansfer(&packet) < 0) { fprintf(stderr, "Error! SCSI Read error!"); @@ -72,7 +74,9 @@ int send_scsi_command(const char *usbk_path, unsigned char *buff, int cmd_index, memcpy(buff, buffer, len); } } + usbk_sg_close(); + return USBK_SCSI_PASS; } diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index 09b8afc..a3b0a85 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -318,7 +318,7 @@ int main(int argc, char *argv[]) { // ENABLE AUTO ACTIVATE ///////////////////////////////////////////// if (pflag & tflag & kflag) { - res = usbk_enableautact(myusbk, opt_parola, opt_key); + res = USBK_ENABLE_AUTACT(myusbk, opt_parola, opt_key); printf("%d\n", res); print_result(myusbk); } @@ -327,7 +327,7 @@ int main(int argc, char *argv[]) { // DISABLE AUTO ACTIVATE ///////////////////////////////////////////// if (pflag & Tflag) { - res = usbk_disableautact(myusbk, opt_parola); + res = USBK_DISABLE_AUTACT(myusbk, opt_parola); printf("%d\n", res); print_result(myusbk); } From e11b3bbf232267877827351efadf85606242237b Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Sat, 1 Oct 2011 15:05:34 +0300 Subject: [PATCH 06/16] code enhancement for libusbk c++ bindings - enumerated types reorganazed both their typedefs and types. - elements of the usbk struct renamed - elements of the usbk structure documented. --- TODO | 7 +- libusbk/src/libusbk.cpp | 131 +++++++---------- libusbk/src/libusbk.h | 308 +++++++++++++++++++++++++++++----------- libusbk/src/uip.h | 8 +- usbk_cli/src/main.cpp | 12 +- 5 files changed, 296 insertions(+), 170 deletions(-) diff --git a/TODO b/TODO index 08c4619..c0b681e 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,10 @@ - - unify linux/windows usbk_sg_ctl.[h|c] sources and make differences with MACROS +urgent +====== + - some api enhancement required for making usbk c++ api. this api will + be used in qusbk. - make libusbk fully c source since no need to use c++ +====== + - make variables fixed sizes (eg. use int32_t or int64_t instead of int (except file descriptors) ) - c++ bindings can be written, but first qusbk project must be started! diff --git a/libusbk/src/libusbk.cpp b/libusbk/src/libusbk.cpp index 2c4e174..7822980 100644 --- a/libusbk/src/libusbk.cpp +++ b/libusbk/src/libusbk.cpp @@ -33,35 +33,6 @@ #include "libusbk.h" #include "usbk_scsi.h" -typedef struct __USBK -{ - int32_t lastopr; - - char *dev; - char *dev_path; - char *backdisk; - char *backdisk_path; - - bool supported; - - char *product; - char *model; - char *serial; - char *usb_serial; - char *firmware_ver; - int32_t multikey_cap; - char *dev_label; - USBK_DS dev_state; - int current_key; - int autoact_keyno; - int retry_num; - char **key_names; - - struct __USBK *next; - struct __USBK *previous; - -} USBK; - using namespace std; #if defined(__linux__) @@ -150,9 +121,9 @@ static bool debug_enable = false; static int getudevinfo(USBK* usbk, const char *device); static int getudevbackdisk(USBK* usbk); static int commandstatus(USBK* usbk); -static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, KEYSIZE keysize); -static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, KEYSIZE keysize); -static unsigned int keysize_byte(KEYSIZE keysize); +static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize); +static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize); +static unsigned int keysize_byte(usbk_keysize_t keysize); USBK* usbk_new(const char* dev) { @@ -177,12 +148,12 @@ USBK* usbk_new(const char* dev) { return NULL; } #elif defined(WIN32) - usbk->dev = strdup(dev); - usbk->dev_path = strdup(dev); + usbk->dev_node = strdup(dev); + usbk->dev_node_path = strdup(dev); #endif t_UIP_DEVINFO usbk_info; - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &usbk_info, GET_DEV_INFO, sizeof(usbk_info), READ_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &usbk_info, GET_DEV_INFO, sizeof(usbk_info), READ_SCSI); if (rtn < 0) { free(usbk); usbk->lastopr = USBK_LO_SCSI_ERROR; @@ -195,14 +166,14 @@ USBK* usbk_new(const char* dev) { usbk->firmware_ver = strdup(usbk_info.firmware_ver.s); //checksupported(); - usbk->supported = true; + usbk->is_supported = true; - if (usbk->supported == true) { + if (usbk->is_supported == true) { usbk->multikey_cap = usbk_info.multikeycap; usbk->current_key = usbk_info.current_keyno; usbk->autoact_keyno = usbk_info.autoactivate_keyno; usbk->retry_num = usbk_info.retry_num; - usbk->dev_state = (USBK_DS) usbk_info.devstate.me; + usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; usbk->dev_label = strdup(usbk_info.devlabel.s); usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); for (i = 0; i < 15; i++) { @@ -224,10 +195,10 @@ int usbk_release(USBK* usbk) { int i; - free(usbk->dev); - free(usbk->dev_path); - free(usbk->backdisk); - free(usbk->backdisk_path); + free(usbk->dev_node); + free(usbk->dev_node_path); + free(usbk->backdisk_node); + free(usbk->backdisk_node_path); free(usbk->product); free(usbk->model); free(usbk->serial); @@ -248,7 +219,7 @@ int usbk_activatekey(USBK* usbk, const char* password, uint8_t key_no) { int rtn; t_UIP_ACTIVATE activate; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; @@ -265,7 +236,7 @@ int usbk_activatekey(USBK* usbk, const char* password, uint8_t key_no) { memset(&activate, 0, sizeof(activate)); strcpy(activate.password.s, password); activate.keyno = key_no; - rtn = send_scsi_command(usbk->dev_path, (unsigned char *) &activate, ACTIVATE_KEY, sizeof(activate), WRITE_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char *) &activate, ACTIVATE_KEY, sizeof(activate), WRITE_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; @@ -289,7 +260,7 @@ int usbk_activatekey(USBK* usbk, const char* password, uint8_t key_no) { int usbk_deactivatekey(USBK* usbk) { int rtn; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; @@ -298,7 +269,7 @@ int usbk_deactivatekey(USBK* usbk) { switch (usbk->dev_state) { case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) NULL, DEACTIVATE_KEY, 0, WRITE_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) NULL, DEACTIVATE_KEY, 0, WRITE_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; @@ -322,7 +293,7 @@ int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass) int rtn; t_UIP_CHPASS chpass; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; @@ -343,7 +314,7 @@ int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass) DBG_INFO("new : %s",chpass.new_password.s); - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &chpass, CHANGE_PASS, sizeof(chpass), WRITE_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &chpass, CHANGE_PASS, sizeof(chpass), WRITE_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; @@ -363,14 +334,14 @@ int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass) } int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) { - return usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL); + return usbk_setkey_keyname(usbk, pass, key_no, key_name, USBK_KEYSIZE_NULL, NULL); } int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label) { int rtn; t_UIP_SETDEVICELABEL devlabel; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; @@ -381,7 +352,7 @@ int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label) memset(&devlabel, 0, sizeof(devlabel)); strncpy(devlabel.password.s, pass, sizeof(devlabel.password.s)); strncpy(devlabel.devlabel.s, device_label, sizeof(devlabel.devlabel.s)); - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &devlabel, SET_DEV_NAME, sizeof(devlabel), WRITE_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &devlabel, SET_DEV_NAME, sizeof(devlabel), WRITE_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; @@ -402,11 +373,11 @@ int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label) return (-1) * usbk->lastopr; } -int usbk_setkey_keyname(USBK* usbk, const char *pass, int key_no, const char* key_name, KEYSIZE key_size, const uint8_t* key) { +int usbk_setkey_keyname(USBK* usbk, const char *pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key) { int rtn; t_UIP_SETKEY setkey; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; @@ -430,15 +401,15 @@ int usbk_setkey_keyname(USBK* usbk, const char *pass, int key_no, const char* ke strncpy(setkey.keyname.s, usbk->key_names[key_no - 1], sizeof(setkey.keyname.s)); } - if ((key_size != KEYSIZE_NULL) && (key != NULL)) { + if ((key_size != USBK_KEYSIZE_NULL) && (key != NULL)) { setkey.options.me = SETKEY_NAME_AND_KEY; - setkey.keysize.me = (e_UIP_KEYSIZE) key_size; + setkey.keysize.me = (e_UIP_KEYSIZE) key_size; memcpy(setkey.key.u8, key, keysize_byte(key_size)); } else { setkey.options.me = SETKEY_NAMEONLY; } - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &setkey, SET_KEY, sizeof(setkey), WRITE_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &setkey, SET_KEY, sizeof(setkey), WRITE_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; @@ -458,7 +429,7 @@ int usbk_setkey_keyname(USBK* usbk, const char *pass, int key_no, const char* ke return (-1) * usbk->lastopr; } -int usbk_setkey_decimal(USBK* usbk, const char *pass, uint8_t key_no, KEYSIZE key_size, const char* key) { +int usbk_setkey_decimal(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) { int rtn = 0; uint8_t key_hex[32]; usbk->lastopr = convertkey_decimal2hex(key_hex, key, key_size); @@ -469,11 +440,11 @@ int usbk_setkey_decimal(USBK* usbk, const char *pass, uint8_t key_no, KEYSIZE ke return (-1) * usbk->lastopr; } -int usbk_setkey_hex(USBK* usbk, const char *pass, uint8_t key_no, KEYSIZE key_size, const uint8_t* key) { +int usbk_setkey_hex(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key) { return usbk_setkey_keyname(usbk, pass, key_no, NULL, key_size, key); } -int usbk_setkey_text(USBK* usbk, const char *pass, uint8_t key_no, KEYSIZE key_size, const char* key) { +int usbk_setkey_text(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) { int rtn = 0; uint8_t key_hex[32]; @@ -489,7 +460,7 @@ int usbk_setautact(USBK* usbk, const char *pass, int key_no) { int rtn; t_UIP_SETAUTOACTIVATE autoact; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; @@ -506,7 +477,7 @@ int usbk_setautact(USBK* usbk, const char *pass, int key_no) { memset(&autoact, 0, sizeof(autoact)); strncpy(autoact.password.s, pass, sizeof(autoact.password.s)); autoact.keyno = key_no; - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &autoact, SET_AUTO_ACTIVE, sizeof(autoact), WRITE_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &autoact, SET_AUTO_ACTIVE, sizeof(autoact), WRITE_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; @@ -526,25 +497,25 @@ int usbk_setautact(USBK* usbk, const char *pass, int key_no) { return (-1) * usbk->lastopr; } -int usbk_getrandomkey(USBK* usbk, uint8_t* random_key, KEYSIZE keysize) { +int usbk_getrandomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) { int rtn; t_UIP_GENERATEKEY genkey; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; } unsigned int keysize_inbyte = keysize_byte(keysize); - if ((keysize_inbyte == 0) || (keysize_inbyte > keysize_byte(KEYSIZE_256BIT))) { + if ((keysize_inbyte == 0) || (keysize_inbyte > keysize_byte(USBK_KEYSIZE_256BIT))) { usbk->lastopr = USBK_LO_INVALID_KEYSIZE; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; } - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &genkey, GENERATE_KEY, sizeof(genkey), READ_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &genkey, GENERATE_KEY, sizeof(genkey), READ_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); @@ -564,14 +535,14 @@ int usbk_refreshusbkinfo(USBK* usbk) { int i; int rtn = 0; - if (usbk->supported == false) { + if (usbk->is_supported == false) { usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; } t_UIP_DEVINFO usbk_info; - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &usbk_info, GET_DEV_INFO, sizeof(usbk_info), READ_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &usbk_info, GET_DEV_INFO, sizeof(usbk_info), READ_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); @@ -581,7 +552,7 @@ int usbk_refreshusbkinfo(USBK* usbk) { usbk->current_key = usbk_info.current_keyno; usbk->autoact_keyno = usbk_info.autoactivate_keyno; usbk->retry_num = usbk_info.retry_num; - usbk->dev_state = (USBK_DS) usbk_info.devstate.me; + usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; usbk->dev_label = strdup(usbk_info.devlabel.s); usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); @@ -596,7 +567,7 @@ int usbk_refreshusbkinfo(USBK* usbk) { static int commandstatus(USBK* usbk) { t_UIP_GETSTATUS status; int rtn = USBK_LO_PASS; - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &status, GET_STATUS, sizeof(t_UIP_GETSTATUS), READ_SCSI); + rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &status, GET_STATUS, sizeof(t_UIP_GETSTATUS), READ_SCSI); if (rtn < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); @@ -607,7 +578,7 @@ static int commandstatus(USBK* usbk) { return (status.lastop.me - 1); //todo versiyon checking'ine göre düzleltilecek. } -static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, KEYSIZE keysize) { +static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize) { // 1) string icinde bosluk karakteri var mi? // 2) string icerisindeki '.' karakterinin sayisi 15 mi? // 3) '.' karakterlerini ' ' karakterine donustur @@ -675,7 +646,7 @@ static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, KEY return USBK_LO_PASS; } -static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, KEYSIZE keysize) { +static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize) { string key_string_text = key_text; if (key_string_text.size() > keysize_byte(keysize)) { @@ -685,20 +656,20 @@ static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, KEYSIZE k return USBK_LO_PASS; } -static unsigned int keysize_byte(KEYSIZE keysize) { +static unsigned int keysize_byte(usbk_keysize_t keysize) { int size_byte = 0; switch (keysize) { - case KEYSIZE_128: + case USBK_KEYSIZE_128BIT: size_byte = 16; break; - case KEYSIZE_192: + case USBK_KEYSIZE_192BIT: size_byte = 24; break; - case KEYSIZE_256: + case USBK_KEYSIZE_256BIT: size_byte = 32; break; - case KEYSIZE_NULL: + case USBK_KEYSIZE_NULL: default: size_byte = 0; break; @@ -768,8 +739,8 @@ static int getudevinfo(USBK* usbk, const char *device) { dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device"); if (dev_scsi != NULL) { if (strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(dev_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)) == 0) { - usbk->dev = strdup(udev_device_get_sysname(dev)); - usbk->dev_path = strdup(udev_device_get_devnode(dev)); + usbk->dev_node = strdup(udev_device_get_sysname(dev)); + usbk->dev_node_path = strdup(udev_device_get_devnode(dev)); usbk->usb_serial = strdup(udev_device_get_sysattr_value(dev_usb, "serial")); rtn = getudevbackdisk(usbk); @@ -834,8 +805,8 @@ static int getudevbackdisk(USBK* usbk) { dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device"); if (dev_scsi != NULL) { if (strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(dev_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)) == 0) { - usbk->backdisk = strdup(udev_device_get_sysname(dev)); - usbk->backdisk_path = strdup(udev_device_get_devnode(dev)); + usbk->backdisk_node= strdup(udev_device_get_sysname(dev)); + usbk->backdisk_node_path = strdup(udev_device_get_devnode(dev)); usbk->lastopr = USBK_LO_PASS; } } diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index e975898..cc059d7 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -29,67 +29,193 @@ extern "C" { # error unsupported environment #endif -#if defined(__linux__) -# define USBK_ATTRIBUTE_PACKED __attribute__((__packed__)) -#else -# define USBK_ATTRIBUTE_PACKED -#endif +/* + * usbk keysize type values + * + * all keysize value is uint8_t (1 byte) long + * */ +enum E_USBK_KEYSIZE +{ + USBK_KEYSIZE_NULL = 0x00, + USBK_KEYSIZE_128BIT = 0x01, + USBK_KEYSIZE_192BIT = 0x02, + USBK_KEYSIZE_256BIT = 0x03, +}; +typedef enum E_USBK_KEYSIZE E_USBK_KEYSIZE; +typedef uint8_t usbk_keysize_t; -typedef enum USBK_ATTRIBUTE_PACKED __KEYSIZE +/* + * usbk device status type values + * + * all ds value is uint8_t (1 byte) long + * */ +enum E_USBK_DEVICE_STATUS { - KEYSIZE_NULL = 0, - KEYSIZE_128BIT = 1, - KEYSIZE_192BIT = 2, - KEYSIZE_256BIT = 3, -} KEYSIZE; - -//-USBK DEVICE STATES -typedef enum __USBK_DS + USBK_DS_ACTIVATE = 0x01, + USBK_DS_ACTIVATE_WITH_BACKDISK = 0x02, + USBK_DS_DEACTIVATE = 0x03, + USBK_DS_FABRIC_DEFAULT = 0x04, + USBK_DS_MUST_REMOVE = 0x05, +}; +typedef enum E_USBK_DEVICE_STATUS E_USBK_DEVICE_STATUS; +typedef uint8_t usbk_ds_t; + +/* + * usbk last operation type values + * + * all lo value is uint8_t (1 byte) long + * */ +enum E_USBK_LASTOPP { - USBK_DS_ACTIVATE = 1, - USBK_DS_ACTIVATE_WITH_BACKDISK = 2, - USBK_DS_DEACTIVATE = 3, - USBK_DS_FABRIC_DEFAULT = 4, - USBK_DS_MUST_REMOVE = 5, -} USBK_DS; - -typedef enum __USBK_LASTOPR + USBK_LO_PASS = 0x00, + USBK_LO_GEN_FAIL = 0x01, + USBK_LO_FAILED_PASS = 0x02, + USBK_LO_FABRIC_RESET = 0x03, + USBK_LO_USBK_UNPLUGING = 0x04, + USBK_LO_INVALID_KEYNO = 0x05, + USBK_LO_INVALID_KEYSIZE = 0x06, + USBK_LO_INVALID_DEVICELABEL = 0x07, + USBK_LO_INVALID_PASS = 0x08, + USBK_LO_INVALID_NEWPASS = 0x09, + + USBK_LO_STATE_ERROR = 0x0a, + USBK_LO_SCSI_ERROR = 0x0b, + USBK_LO_UNSUPPORTED_USBK = 0x0c, + USBK_LO_INVALID_KEY = 0x0d, + + USBK_LO_UDEV_ERROR = 0x0e, + USBK_LO_MEM_ERROR = 0x0f, +}; +typedef enum E_USBK_LASTOPP E_USBK_LASTOPP; +typedef uint8_t usbk_lo_t; + +#ifndef __cplusplus +/* + * bool definition for c + * */ +enum { false = 0, true = !false }; + +typedef int8_t bool; +#endif + +/* + * usbk structure, contains all the details about usbk device. + * */ +struct USBK { - USBK_LO_PASS = 0, - USBK_LO_GEN_FAIL = 1, - USBK_LO_FAILED_PASS = 2, - USBK_LO_FABRIC_RESET = 3, - USBK_LO_USBK_UNPLUGING = 4, - USBK_LO_INVALID_KEYNO = 5, - USBK_LO_INVALID_KEYSIZE = 6, - USBK_LO_INVALID_DEVICELABEL = 7, - USBK_LO_INVALID_PASS = 8, - USBK_LO_INVALID_NEWPASS = 9, - - USBK_LO_STATE_ERROR = 10, - USBK_LO_SCSI_ERROR = 11, - USBK_LO_UNSUPPORTED_USBK = 12, - USBK_LO_INVALID_KEY = 13, - - USBK_LO_UDEV_ERROR = 14, - USBK_LO_MEM_ERROR = 15, -} LIBUSBK_LASTOPR; - -typedef struct __USBK USBK; - -#define USBK_ASSIGN_PASSWORD(usbk, new_pass) usbk_changepassword(usbk, NULL, new_pass) -#define USBK_SETKEY_NAME(usbk, pass, key_no, key_name) usbk_setkey_keyname(usbk, pass, key_no, key_name, KEYSIZE_NULL, NULL) -#define USBK_SETKEY_128BIT_HEX(usbk, pass, key_no, key) usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_128BIT, key) -#define USBK_SETKEY_192BIT_HEX(usbk, pass, key_no, key) usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_192BIT, key) -#define USBK_SETKEY_256BIT_HEX(usbk, pass, key_no, key) usbk_setkey_hex(usbk, pass, key_no, KEYSIZE_256BIT, key) -#define USBK_SETKEY_128BIT_DECIMAL(usbk, pass, key_no, key) usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_128BIT, key) -#define USBK_SETKEY_192BIT_DECIMAL(usbk, pass, key_no, key) usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_192BIT, key) -#define USBK_SETKEY_256BIT_DECIMAL(usbk, pass, key_no, key) usbk_setkey_decimal(usbk, pass, key_no, KEYSIZE_256BIT, key) -#define USBK_SETKEY_128BIT_TEXT(usbk, pass, key_no, key) usbk_setkey_text(usbk, pass, key_no, KEYSIZE_128BIT, key) -#define USBK_SETKEY_192BIT_TEXT(usbk, pass, key_no, key) usbk_setkey_text(usbk, pass, key_no, KEYSIZE_192BIT, key) -#define USBK_SETKEY_256BIT_TEXT(usbk, pass, key_no, key) usbk_setkey_text(usbk, pass, key_no, KEYSIZE_256BIT, key) -#define USBK_ENABLE_AUTACT(usbk, pass, key_no) usbk_setautact(usbk, pass, key_no) -#define USBK_DISABLE_AUTACT(usbk, pass) usbk_setautact(usbk, pass, 0) + /* refers the last operation of usbk device */ + usbk_lo_t lastopr; + + /* refers node of usbk device in /dev. + * e.g. usbk device node is /dev/sdc, then value is sdc */ + char *dev_node; + + /* refers full path of node of usbk device. + * e.g. usbk device node is /dev/sdc, then value is /dev/sdc */ + char *dev_node_path; + + /* refers node of backdisk device which plugged to usbk device. + * e.g. device node is /dev/sdb, then valuse is sdb */ + char *backdisk_node; + + /* refers full path of node of backdisk device. + * e.g. backdisk device node is /dev/sdb, then value is /dev/sdb */ + char *backdisk_node_path; + + /* refers if usbk device supported or not */ + bool is_supported; + + /* refers usbk product information + * e.g. USBK CryptoBridge 2.0 */ + char *product; + + /* refers usbk product model + * e.g. A103, A101 */ + char *model; + + /* refers serial number of the usbk device + * e.g. 842CBDAA3952313832312020FF0913 */ + char *serial; + + /* FIXME: ??? + * */ + char *usb_serial; + + /* refers firmware version of the usbk device + * e.g. 2.5.4 */ + char *firmware_ver; + + /* refers number of keys in the usbk device + * e.g. 3 */ + uint8_t multikey_cap; + + /* refers device label of the usbk device + * This label is just a block device label. + * + * e.g. "USBK CryptoBridge 2.0" */ + char *dev_label; + + /* refers status of usbk device + * e.g. USBK_DS_ACTIVATE, USBK_DS_ACTIVATE_WITH_BACKDISK */ + usbk_ds_t dev_state; + + /* refers the key working on. usbk device may contains more than one + * key (multikey_cap contains this number) + * + * e.g. 1 */ + int current_key; + + /* refers the autoactivated key of the usbk. there might a key which + * can be used without a password. + * */ + int autoact_keyno; + + /* refers the remaining retry number. User must enter true password, + * if he/she couldn't enter true password, then retry_num value + * decreses 1. When retry_num value reaches 0, then ALL THE KEYS + * AND PASSWORDS removes from the device and the backdisk becomes + * a mass raw data. + * + * retry_num is 3 on true entry. */ + int retry_num; + + /* refers key labels for a better user interaction. User sets this. + * + * default values are "" */ + char **key_names; + + /* FIXME: remove this double-linked list structure */ + struct USBK *next; + struct USBK *previous; +}; +typedef struct USBK USBK; + +#define USBK_ASSIGN_PASSWORD(usbk, new_pass) \ + usbk_changepassword(usbk, NULL, new_pass) +#define USBK_SETKEY_NAME(usbk, pass, key_no, key_name) \ + usbk_setkey_keyname(usbk, pass, key_no, key_name, USBK_KEYSIZE_NULL, NULL) +#define USBK_SETKEY_128BIT_HEX(usbk, pass, key_no, key) \ + usbk_setkey_hex(usbk, pass, key_no, USBK_KEYSIZE_128BIT, key) +#define USBK_SETKEY_192BIT_HEX(usbk, pass, key_no, key) \ + usbk_setkey_hex(usbk, pass, key_no, USBK_KEYSIZE_192BIT, key) +#define USBK_SETKEY_256BIT_HEX(usbk, pass, key_no, key) \ + usbk_setkey_hex(usbk, pass, key_no, USBK_KEYSIZE_256BIT, key) +#define USBK_SETKEY_128BIT_DECIMAL(usbk, pass, key_no, key) \ + usbk_setkey_decimal(usbk, pass, key_no, USBK_KEYSIZE_128BIT, key) +#define USBK_SETKEY_192BIT_DECIMAL(usbk, pass, key_no, key) \ + usbk_setkey_decimal(usbk, pass, key_no, USBK_KEYSIZE_192BIT, key) +#define USBK_SETKEY_256BIT_DECIMAL(usbk, pass, key_no, key) \ + usbk_setkey_decimal(usbk, pass, key_no, USBK_KEYSIZE_256BIT, key) +#define USBK_SETKEY_128BIT_TEXT(usbk, pass, key_no, key) \ + usbk_setkey_text(usbk, pass, key_no, USBK_KEYSIZE_128BIT, key) +#define USBK_SETKEY_192BIT_TEXT(usbk, pass, key_no, key) \ + usbk_setkey_text(usbk, pass, key_no, USBK_KEYSIZE_192BIT, key) +#define USBK_SETKEY_256BIT_TEXT(usbk, pass, key_no, key) \ + usbk_setkey_text(usbk, pass, key_no, USBK_KEYSIZE_256BIT, key) +#define USBK_ENABLE_AUTACT(usbk, pass, key_no) \ + usbk_setautact(usbk, pass, key_no) +#define USBK_DISABLE_AUTACT(usbk, pass) \ + usbk_setautact(usbk, pass, 0) USBK* usbk_new(const char* dev); extern int usbk_release(USBK* usbk); @@ -101,26 +227,44 @@ extern int usbk_release(USBK* usbk); /* This is the helper macro for reaching the USBK structure */ #define USBK_GET_ELEMENT(usbk, element) (usbk->element) -inline const char* usbk_get_dev(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev); } -inline const char* usbk_get_dev_path(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_path); } -inline const char* usbk_get_backdisk(USBK* usbk) { return USBK_GET_ELEMENT(usbk, backdisk); } -inline const char* usbk_get_backdisk_path(USBK* usbk) { return USBK_GET_ELEMENT(usbk, backdisk_path); } -inline const char* usbk_get_product(USBK* usbk) { return USBK_GET_ELEMENT(usbk, product); } -inline const char* usbk_get_model(USBK* usbk) { return USBK_GET_ELEMENT(usbk, model); } -inline const char* usbk_get_serial(USBK* usbk) { return USBK_GET_ELEMENT(usbk, serial); } -inline const char* usbk_get_usb_serial(USBK* usbk) { return USBK_GET_ELEMENT(usbk, usb_serial); } -inline const char* usbk_get_firmware_ver(USBK* usbk) { return USBK_GET_ELEMENT(usbk, firmware_ver); } -inline const char* usbk_get_dev_label(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_label); } -inline const char* usbk_get_keyname(USBK* usbk, int key_no) { return USBK_GET_ELEMENT(usbk, key_names[key_no]); } - -inline int usbk_get_lastopr_status(USBK* usbk) { return USBK_GET_ELEMENT(usbk, lastopr); } - -inline bool usbk_check_support(USBK* usbk) { return USBK_GET_ELEMENT(usbk, supported); } -inline int usbk_get_multikeycap(USBK* usbk) { return USBK_GET_ELEMENT(usbk, multikey_cap); } -inline int usbk_get_current_keyno(USBK* usbk) { return USBK_GET_ELEMENT(usbk, current_key); } -inline int usbk_get_autoactivation_keyno(USBK* usbk) { return USBK_GET_ELEMENT(usbk, autoact_keyno); } -inline int usbk_get_retry_number(USBK* usbk) { return USBK_GET_ELEMENT(usbk, retry_num); } -inline USBK_DS usbk_get_state(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_state); } +inline const char* usbk_get_dev(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, dev_node); } +inline const char* usbk_get_dev_path(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, dev_node_path); } +inline const char* usbk_get_backdisk(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, backdisk_node); } +inline const char* usbk_get_backdisk_path(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, backdisk_node_path); } +inline const char* usbk_get_product(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, product); } +inline const char* usbk_get_model(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, model); } +inline const char* usbk_get_serial(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, serial); } +inline const char* usbk_get_usb_serial(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, usb_serial); } +inline const char* usbk_get_firmware_ver(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, firmware_ver); } +inline const char* usbk_get_dev_label(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, dev_label); } +inline const char* usbk_get_keyname(USBK* usbk, int key_no) + { return USBK_GET_ELEMENT(usbk, key_names[key_no]); } + +inline int usbk_get_lastopr_status(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, lastopr); } + +inline bool usbk_check_support(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, is_supported); } +inline int usbk_get_multikeycap(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, multikey_cap); } +inline int usbk_get_current_keyno(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, current_key); } +inline int usbk_get_autoactivation_keyno(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, autoact_keyno); } +inline int usbk_get_retry_number(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, retry_num); } +inline usbk_ds_t usbk_get_state(USBK* usbk) + { return USBK_GET_ELEMENT(usbk, dev_state); } /* Debugging informations */ extern void usbk_debug_enable(void); @@ -140,12 +284,12 @@ extern int usbk_deactivatekey(USBK* usbk); extern int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass); extern int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label); extern int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); -extern int usbk_setkey_keyname(USBK* usbk, const char* pass, int key_no, const char* key_name, KEYSIZE key_size, const uint8_t* key); -extern int usbk_setkey_hex(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const uint8_t* key); -extern int usbk_setkey_decimal(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const char* key); -extern int usbk_setkey_text(USBK* usbk, const char* pass, uint8_t key_no, KEYSIZE key_size, const char* key); +extern int usbk_setkey_keyname(USBK* usbk, const char* pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key); +extern int usbk_setkey_hex(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key); +extern int usbk_setkey_decimal(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const char* key); +extern int usbk_setkey_text(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const char* key); extern int usbk_setautact(USBK* usbk, const char* pass, int key_no); -extern int usbk_getrandomkey(USBK* usbk, uint8_t*random_key, KEYSIZE keysize); +extern int usbk_getrandomkey(USBK* usbk, uint8_t*random_key, usbk_keysize_t keysize); extern int usbk_refreshusbkinfo(USBK* usbk); #if defined(__linux__) diff --git a/libusbk/src/uip.h b/libusbk/src/uip.h index f71c0a3..71207c1 100644 --- a/libusbk/src/uip.h +++ b/libusbk/src/uip.h @@ -42,7 +42,13 @@ // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#include "libusbk.h" +#if defined(__AVR32__) || defined(__linux__) +# include +#elif defined(WIN32) +# include +#else +# error unsupported environment +#endif #if defined(__AVR32__) # define ATTR_PACKED_BEGIN __attribute__((__packed__)) diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index a3b0a85..f0c596d 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -69,7 +69,7 @@ using namespace std; //PRIVATE FUNCTION DECLARATIONS static int _parse_options(int *argc, char** argv[]); -KEYSIZE parse_keysize(char *s); +usbk_keysize_t parse_keysize(char *s); int parse_keysize_inbyte(char *s); static void linuxcli_show_devices(void); @@ -344,15 +344,15 @@ int main(int argc, char *argv[]) { exit(0); } -KEYSIZE parse_keysize(char *s){ +usbk_keysize_t parse_keysize(char *s){ if (!strcmp(s, "128")) { - return KEYSIZE_128BIT; + return USBK_KEYSIZE_128BIT; } else if (!strcmp(s, "192")) { - return KEYSIZE_192BIT; + return USBK_KEYSIZE_192BIT; } else if (!strcmp(s, "256")) { - return KEYSIZE_256BIT; + return USBK_KEYSIZE_256BIT; } else { - return KEYSIZE_NULL; + return USBK_KEYSIZE_NULL; } } From d64877f97dae7925d8ac24b543fbe747839fade9 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Sun, 2 Oct 2011 13:28:38 +0300 Subject: [PATCH 07/16] libusbk c++ to c and lots of cosmetic changes --- libusbk/src/{libusbk.cpp => libusbk.c} | 561 +++++++++++++++---------- libusbk/src/libusbk.h | 81 ++-- libusbk/src/linux/usbk_sg_ctl.h | 1 - libusbk/src/usbk_scsi.c | 26 +- libusbk/src/usbk_scsi.h | 28 +- usbk_cli/src/main.cpp | 26 +- 6 files changed, 419 insertions(+), 304 deletions(-) rename libusbk/src/{libusbk.cpp => libusbk.c} (70%) diff --git a/libusbk/src/libusbk.cpp b/libusbk/src/libusbk.c similarity index 70% rename from libusbk/src/libusbk.cpp rename to libusbk/src/libusbk.c index 7822980..4607df8 100644 --- a/libusbk/src/libusbk.cpp +++ b/libusbk/src/libusbk.c @@ -17,44 +17,56 @@ * */ -//PRIVATE HEADERS -#include - #if defined(__linux__) # include +# include +# include #endif #include #include #include +#include #include "uip.h" #include "libusbk.h" #include "usbk_scsi.h" -using namespace std; - -#if defined(__linux__) +#if defined(__linux__) #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "\033[0;32;40m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "\033[1;3;31m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} - #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last opration status: %s", lastopr_string[x].rtrn_string);} \ - else {DBG_INFO("last opration status: %s", lastopr_string[x].rtrn_string);}} + #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ + else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} #elif defined(WIN32) #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} - #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last opration status: %s", lastopr_string[x].rtrn_string);} \ - else {DBG_INFO("last opration status: %s", lastopr_string[x].rtrn_string);}} + #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ + else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} #endif +//-DEFINES FOR FINDING USBK AND BACKDISK +#define USBK_USB_IDVENDOR "2384" +#define USBK_USB_IDPRODUCT_A103 "a103" +#define USBK_USB_IDPRODUCT_A101 "a101" +#define USBK_SCSI_VENDOR "USBK" +#define USBK_SCSI_BACKDISK_VENDOR "BackDisk" + +#define USBK_CHECK_SUPPORTED \ + if (!usbk->is_supported) { \ + usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; \ + DBG_LASTOPR_STRING(usbk->lastopr); \ + return - usbk->lastopr; \ + } + typedef struct __RTRN_STRING { int rtrn; const char *rtrn_string; } RTRN_STRING; -RTRN_STRING lastopr_string[] ={ +RTRN_STRING lastopr_string[] = { {USBK_LO_PASS, "Ok" }, {USBK_LO_GEN_FAIL, "fail" }, {USBK_LO_FAILED_PASS, "wrong password" }, @@ -80,114 +92,117 @@ typedef struct __LIBUSBK_SUPPORTED_MODELS { bool support; } LIBUSBK_SUPPORTED_MODELS; -typedef struct __LIBUSBK_SUPPORTED_VERSIONS { - int major_version; - int minor_version; - LIBUSBK_SUPPORTED_MODELS* models; -} LIBUSBK_SUPPORTED_VERSIONS; - -typedef struct __LIBUSBK_SUPPORTED_PRODUCTS { - const char* product; - LIBUSBK_SUPPORTED_VERSIONS* versions; -} LIBUSBK_SUPPORTED_PRODUCTS; - LIBUSBK_SUPPORTED_MODELS models_1_1[] = { { "A101", true }, { "A103", true }, {NULL, false } }; +typedef struct __LIBUSBK_SUPPORTED_VERSIONS { + int major_version; + int minor_version; + LIBUSBK_SUPPORTED_MODELS* models; +} LIBUSBK_SUPPORTED_VERSIONS; + LIBUSBK_SUPPORTED_VERSIONS versions_1[] = { { 2, 5, models_1_1 }, { 0, 0, NULL } }; +typedef struct __LIBUSBK_SUPPORTED_PRODUCTS { + const char* product; + LIBUSBK_SUPPORTED_VERSIONS* versions; +} LIBUSBK_SUPPORTED_PRODUCTS; + LIBUSBK_SUPPORTED_PRODUCTS products[] = { { "USBK CryptoBridge 2.0", versions_1 }, { "USBK CryptoBridge", versions_1 }, { NULL, NULL } }; -//-DEFINES FOR FINDING USBK AND BACKDISK -#define USBK_USB_IDVENDOR "2384" -#define USBK_USB_IDPRODUCT_A103 "a103" -#define USBK_USB_IDPRODUCT_A101 "a101" -#define USBK_SCSI_VENDOR "USBK" -#define USBK_SCSI_BACKDISK_VENDOR "BackDisk" - - static bool debug_enable = false; -static int getudevinfo(USBK* usbk, const char *device); -static int getudevbackdisk(USBK* usbk); -static int commandstatus(USBK* usbk); -static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize); -static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize); -static unsigned int keysize_byte(usbk_keysize_t keysize); +static int _get_udev_info(USBK* usbk, const char *device); +static int _get_udev_backdisk(USBK* usbk); +static int _command_status(USBK* usbk); + +#if 0 +static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize); +#endif +static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize); +static unsigned int _keysize_as_byte(usbk_keysize_t keysize); -USBK* usbk_new(const char* dev) { +USBK* usbk_new(const char* dev) +{ + int ret; int i; - int rtn = 0; USBK* usbk = (USBK*)calloc(1, sizeof(USBK)); - if (usbk == NULL) - { + if (usbk == NULL) { usbk->lastopr = USBK_LO_MEM_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); + return NULL; } -#if defined(__linux__) +#if defined(__linux__) // get information about usbk by udev - rtn = getudevinfo(usbk, dev); - if (rtn < 0) { + ret = _get_udev_info(usbk, dev); + if (ret < 0) { free(usbk); usbk->lastopr = USBK_LO_UDEV_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); + return NULL; } -#elif defined(WIN32) + +#elif defined(WIN32) usbk->dev_node = strdup(dev); usbk->dev_node_path = strdup(dev); #endif t_UIP_DEVINFO usbk_info; - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &usbk_info, GET_DEV_INFO, sizeof(usbk_info), READ_SCSI); - if (rtn < 0) { + ret = send_scsi_command( + usbk->dev_node_path, + (unsigned char*) &usbk_info, + USBK_GET_DEV_INFO, + sizeof(usbk_info), + USBK_READ_SCSI); + if (ret < 0) { free(usbk); usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); + return NULL; } usbk->product = strdup(usbk_info.product.s); usbk->model = strdup(usbk_info.model.s); usbk->firmware_ver = strdup(usbk_info.firmware_ver.s); - - //checksupported(); usbk->is_supported = true; - if (usbk->is_supported == true) { - usbk->multikey_cap = usbk_info.multikeycap; - usbk->current_key = usbk_info.current_keyno; - usbk->autoact_keyno = usbk_info.autoactivate_keyno; - usbk->retry_num = usbk_info.retry_num; - usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; - usbk->dev_label = strdup(usbk_info.devlabel.s); - usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); - for (i = 0; i < 15; i++) { - sprintf((usbk->serial + i * 2), "%2.2X", usbk_info.serial.u8[i]); - } + usbk->multikey_cap = usbk_info.multikeycap; + usbk->current_key = usbk_info.current_keyno; + usbk->autoact_keyno = usbk_info.autoactivate_keyno; + usbk->retry_num = usbk_info.retry_num; + usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; + usbk->dev_label = strdup(usbk_info.devlabel.s); - usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); - for (i = 0; i < usbk->multikey_cap; i++) { - usbk->key_names[i] = strdup(usbk_info.keyname[i].s); - } - } + /* FIXME why we need this much space */ + usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); + for (i = 0; i < UIP_SERIAL_SIZE; i++) { + sprintf((usbk->serial + i * 2), "%2.2X", usbk_info.serial.u8[i]); + } + + usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); + for (i = 0; i < usbk->multikey_cap; i++) { + usbk->key_names[i] = strdup(usbk_info.keyname[i].s); + } usbk->lastopr = USBK_LO_PASS; DBG_LASTOPR_STRING(usbk->lastopr); + return usbk; } @@ -209,26 +224,25 @@ int usbk_release(USBK* usbk) for (i = 0; i < usbk->multikey_cap; i++) { free(usbk->key_names[i]); } + free(usbk->key_names); free(usbk); return 0; } -int usbk_activatekey(USBK* usbk, const char* password, uint8_t key_no) { - int rtn; +int usbk_key_activate(USBK* usbk, const char* password, uint8_t key_no) +{ + int ret; t_UIP_ACTIVATE activate; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED if (key_no > usbk->multikey_cap) { usbk->lastopr = USBK_LO_INVALID_KEYNO; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } switch (usbk->dev_state) { @@ -236,12 +250,13 @@ int usbk_activatekey(USBK* usbk, const char* password, uint8_t key_no) { memset(&activate, 0, sizeof(activate)); strcpy(activate.password.s, password); activate.keyno = key_no; - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char *) &activate, ACTIVATE_KEY, sizeof(activate), WRITE_SCSI); - if (rtn < 0) { + ret = send_scsi_command(usbk->dev_node_path, (unsigned char *) &activate, USBK_ACTIVATE_KEY, sizeof(activate), USBK_WRITE_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; } - usbk->lastopr = commandstatus(usbk); + usbk->lastopr = _command_status(usbk); + break; case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: @@ -250,54 +265,52 @@ int usbk_activatekey(USBK* usbk, const char* password, uint8_t key_no) { default: printf("state error"); usbk->lastopr = USBK_LO_STATE_ERROR; + break; } DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -int usbk_deactivatekey(USBK* usbk) { - int rtn; +int usbk_key_deactivate(USBK* usbk) +{ + int ret; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED switch (usbk->dev_state) { case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) NULL, DEACTIVATE_KEY, 0, WRITE_SCSI); - if (rtn < 0) { + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) NULL, USBK_DEACTIVATE_KEY, 0, USBK_WRITE_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; } + usbk->lastopr = _command_status(usbk); - usbk->lastopr = commandstatus(usbk); break; case USBK_DS_DEACTIVATE: case USBK_DS_FABRIC_DEFAULT: case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + break; } DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass) { - int rtn; +int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass) +{ + int ret; t_UIP_CHPASS chpass; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED switch (usbk->dev_state) { case USBK_DS_DEACTIVATE: @@ -313,52 +326,49 @@ int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass) DBG_INFO("old : %s",chpass.old_password.s); DBG_INFO("new : %s",chpass.new_password.s); - - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &chpass, CHANGE_PASS, sizeof(chpass), WRITE_SCSI); - if (rtn < 0) { + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &chpass, USBK_CHANGE_PASS, sizeof(chpass), USBK_WRITE_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; + break; } - usbk->lastopr = commandstatus(usbk); + usbk->lastopr = _command_status(usbk); + break; case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + break; } DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} -int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) { - return usbk_setkey_keyname(usbk, pass, key_no, key_name, USBK_KEYSIZE_NULL, NULL); + return usbk->lastopr; } -int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label) { - int rtn; +int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) +{ + int ret; t_UIP_SETDEVICELABEL devlabel; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED switch (usbk->dev_state) { case USBK_DS_DEACTIVATE: memset(&devlabel, 0, sizeof(devlabel)); strncpy(devlabel.password.s, pass, sizeof(devlabel.password.s)); strncpy(devlabel.devlabel.s, device_label, sizeof(devlabel.devlabel.s)); - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &devlabel, SET_DEV_NAME, sizeof(devlabel), WRITE_SCSI); + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &devlabel, USBK_SET_DEV_NAME, sizeof(devlabel), USBK_WRITE_SCSI); - if (rtn < 0) { + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; break; } - usbk->lastopr = commandstatus(usbk); + usbk->lastopr = _command_status(usbk); + break; case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: @@ -366,27 +376,27 @@ int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label) case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + break; } DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -int usbk_setkey_keyname(USBK* usbk, const char *pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key) { - int rtn; +int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key) +{ + int ret; t_UIP_SETKEY setkey; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED if (key_no > usbk->multikey_cap) { usbk->lastopr = USBK_LO_INVALID_KEYNO; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } switch (usbk->dev_state) { @@ -398,23 +408,26 @@ int usbk_setkey_keyname(USBK* usbk, const char *pass, int key_no, const char* ke if (key_name != NULL) { strncpy(setkey.keyname.s, key_name, sizeof(setkey.keyname.s)); } else { + /* preserve old keyname if keyname is not changed */ strncpy(setkey.keyname.s, usbk->key_names[key_no - 1], sizeof(setkey.keyname.s)); } if ((key_size != USBK_KEYSIZE_NULL) && (key != NULL)) { setkey.options.me = SETKEY_NAME_AND_KEY; setkey.keysize.me = (e_UIP_KEYSIZE) key_size; - memcpy(setkey.key.u8, key, keysize_byte(key_size)); + memcpy(setkey.key.u8, key, _keysize_as_byte(key_size)); } else { setkey.options.me = SETKEY_NAMEONLY; } - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &setkey, SET_KEY, sizeof(setkey), WRITE_SCSI); - if (rtn < 0) { + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &setkey, USBK_SET_KEY, sizeof(setkey), USBK_WRITE_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; + break; } - usbk->lastopr = commandstatus(usbk); + usbk->lastopr = _command_status(usbk); + break; case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: @@ -422,54 +435,70 @@ int usbk_setkey_keyname(USBK* usbk, const char *pass, int key_no, const char* ke case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + break; } DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -int usbk_setkey_decimal(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) { - int rtn = 0; - uint8_t key_hex[32]; - usbk->lastopr = convertkey_decimal2hex(key_hex, key, key_size); - if (rtn == USBK_LO_PASS){ - usbk->lastopr = usbk_setkey_keyname(usbk, pass, key_no, NULL, key_size, key_hex); +#if 0 +int usbk_set_key_decimal(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) +{ + int ret = 0; + uint8_t key_hex[UIP_KEYSIZE_TOTALSIZE]; + + USBK_CHECK_SUPPORTED + + usbk->lastopr = _convert_key_decimal2hex(key_hex, key, key_size); + if (ret == USBK_LO_PASS){ + usbk->lastopr = usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key_hex); } + DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } +#endif + +int usbk_set_key_hex(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key) +{ + USBK_CHECK_SUPPORTED -int usbk_setkey_hex(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key) { - return usbk_setkey_keyname(usbk, pass, key_no, NULL, key_size, key); + return usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key); } -int usbk_setkey_text(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) { - int rtn = 0; +int usbk_set_key_text(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) +{ + int ret = 0; uint8_t key_hex[32]; - usbk->lastopr = convertkey_text2hex(key_hex, key, key_size); - if (rtn == USBK_LO_PASS){ - usbk->lastopr = usbk_setkey_keyname(usbk, pass, key_no, NULL, key_size, key_hex); + USBK_CHECK_SUPPORTED + + usbk->lastopr = _convert_key_text2hex(key_hex, key, key_size); + if (ret == USBK_LO_PASS){ + usbk->lastopr = usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key_hex); } + DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -int usbk_setautact(USBK* usbk, const char *pass, int key_no) { - int rtn; +int usbk_set_autact(USBK* usbk, const char *pass, int key_no) +{ + int ret; t_UIP_SETAUTOACTIVATE autoact; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED if (key_no > usbk->multikey_cap) { usbk->lastopr = USBK_LO_INVALID_KEYNO; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } switch (usbk->dev_state) { @@ -477,12 +506,14 @@ int usbk_setautact(USBK* usbk, const char *pass, int key_no) { memset(&autoact, 0, sizeof(autoact)); strncpy(autoact.password.s, pass, sizeof(autoact.password.s)); autoact.keyno = key_no; - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &autoact, SET_AUTO_ACTIVE, sizeof(autoact), WRITE_SCSI); - if (rtn < 0) { + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &autoact, USBK_SET_AUTO_ACTIVE, sizeof(autoact), USBK_WRITE_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; + break; } - usbk->lastopr = commandstatus(usbk); + usbk->lastopr = _command_status(usbk); + break; case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: @@ -490,63 +521,62 @@ int usbk_setautact(USBK* usbk, const char *pass, int key_no) { case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + break; } DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -int usbk_getrandomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) { - int rtn; +int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) +{ + int ret; t_UIP_GENERATEKEY genkey; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED - unsigned int keysize_inbyte = keysize_byte(keysize); - if ((keysize_inbyte == 0) || (keysize_inbyte > keysize_byte(USBK_KEYSIZE_256BIT))) { + unsigned int keysize_inbyte = _keysize_as_byte(keysize); + if ((keysize_inbyte == 0) || (keysize_inbyte > _keysize_as_byte(USBK_KEYSIZE_256BIT))) { usbk->lastopr = USBK_LO_INVALID_KEYSIZE; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + return - usbk->lastopr; } - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &genkey, GENERATE_KEY, sizeof(genkey), READ_SCSI); - if (rtn < 0) { + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &genkey, USBK_GENERATE_KEY, sizeof(genkey), USBK_READ_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } - usbk->lastopr = commandstatus(usbk); + usbk->lastopr = _command_status(usbk); if (usbk->lastopr == USBK_LO_PASS) { memcpy(random_key, genkey.key.u8, keysize_inbyte); } DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -int usbk_refreshusbkinfo(USBK* usbk) { +int usbk_refresh_usbkinfo(USBK* usbk) +{ int i; - int rtn = 0; + int ret; - if (usbk->is_supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } + USBK_CHECK_SUPPORTED t_UIP_DEVINFO usbk_info; - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &usbk_info, GET_DEV_INFO, sizeof(usbk_info), READ_SCSI); - if (rtn < 0) { + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &usbk_info, USBK_GET_DEV_INFO, sizeof(usbk_info), USBK_READ_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } usbk->current_key = usbk_info.current_keyno; @@ -560,25 +590,32 @@ int usbk_refreshusbkinfo(USBK* usbk) { usbk->key_names[i] = strdup(usbk_info.keyname[i].s); } usbk->lastopr = USBK_LO_PASS; + DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } -static int commandstatus(USBK* usbk) { +static int _command_status(USBK* usbk) +{ t_UIP_GETSTATUS status; - int rtn = USBK_LO_PASS; - rtn = send_scsi_command(usbk->dev_node_path, (unsigned char*) &status, GET_STATUS, sizeof(t_UIP_GETSTATUS), READ_SCSI); - if (rtn < 0) { + int ret; + + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &status, USBK_GET_STATUS, sizeof(t_UIP_GETSTATUS), USBK_READ_SCSI); + if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + return - usbk->lastopr; } usbk->retry_num = status.retry_num; + return (status.lastop.me - 1); //todo versiyon checking'ine göre düzleltilecek. } -static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize) { +#if 0 +static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize) +{ // 1) string icinde bosluk karakteri var mi? // 2) string icerisindeki '.' karakterinin sayisi 15 mi? // 3) '.' karakterlerini ' ' karakterine donustur @@ -587,7 +624,30 @@ static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usb // 6) 16 adet sayı cikartabildin mi? // 7) sayılar 0 ile 255 arasinda mi? - //int ikey[this->keysize_byte(keysize)]; + unsigned int key_len = _keysize_as_byte(keysize); + int i; + + /* check key_text if it is null or not */ + if (key_decimal == NULL || key_hex == NULL) + return USBK_LO_INVALID_KEY; + + /* check key length if it is proper or not */ + if(strlen(key_hex) > key_len) { + /* FIXME USBK_LO_INVALID_KEYSIZE? */ + return USBK_LO_INVALID_KEY; + } + + for(i=0; i_keysize_as_byte(keysize)]; int ikey[50]; size_t found; string::iterator it; @@ -606,7 +666,7 @@ static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usb key_decimal_str[found] = ' '; found = key_decimal_str.find_first_of(".", found + 1); } - if (i != (keysize_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; + if (i != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; //cout << str << endl; // string icerisinde bosluklar haric decimal olmayan karakter var mi? @@ -626,12 +686,12 @@ static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usb // sayılar 0 ile 255 arasinda mi? istringstream iss(key_decimal_str, istringstream::in); i = 0; - for (unsigned int n = 0; n < keysize_byte(keysize); n++) { + for (unsigned int n = 0; n < _keysize_as_byte(keysize); n++) { iss >> ikey[n]; if (iss.fail() || iss.bad()) return USBK_LO_INVALID_KEY; - if (iss.eof() && n != (keysize_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; + if (iss.eof() && n != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; if (ikey[n] > 255 || ikey[n] < 0) { i++; @@ -645,51 +705,87 @@ static int convertkey_decimal2hex(uint8_t *key_hex, const char* key_decimal, usb return USBK_LO_PASS; } +#endif -static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize) { - string key_string_text = key_text; +static inline int16_t _xtod(const char c) +{ + if (c>='0' && c<='9') return c-'0'; + if (c>='A' && c<='F') return c-'A'+10; + if (c>='a' && c<='f') return c-'a'+10; + + return -1; +} + +static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize) +{ + unsigned int key_len = _keysize_as_byte(keysize); + int i; + + /* check key_text if it is null or not */ + if (key_text == NULL || key_hex == NULL) + return USBK_LO_INVALID_KEY; + + /* check key length if it is proper or not */ + if(strlen(key_hex) > key_len) { + /* FIXME USBK_LO_INVALID_KEYSIZE? */ + return USBK_LO_INVALID_KEY; + } + + for(i=0; i keysize_byte(keysize)) { - return USBK_LO_INVALID_KEY; - } - strncpy((char*) key_hex, key_string_text.c_str(), keysize_byte(keysize)); return USBK_LO_PASS; } -static unsigned int keysize_byte(usbk_keysize_t keysize) { - int size_byte = 0; +static unsigned int _keysize_as_byte(usbk_keysize_t keysize) +{ + unsigned int size = 0; switch (keysize) { case USBK_KEYSIZE_128BIT: - size_byte = 16; + size = 16; break; case USBK_KEYSIZE_192BIT: - size_byte = 24; + size = 24; break; case USBK_KEYSIZE_256BIT: - size_byte = 32; + size = 32; break; case USBK_KEYSIZE_NULL: default: - size_byte = 0; + size = 0; break; } - return size_byte; + + return size; } -#if defined(__linux__) -static int getudevinfo(USBK* usbk, const char *device) { - int rtn; +#if defined(__linux__) + +static int _get_udev_info(USBK* usbk, const char *device) +{ + int ret; + struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; - struct udev_device *dev = NULL; - struct udev_device *dev_usb = NULL; - struct udev_device *dev_scsi = NULL; - size_t len; + struct udev_device *dev; + struct udev_device *dev_usb; + struct udev_device *dev_scsi; + struct udev_list_entry *list_entry; + const char *str; + size_t len; int i; - struct udev_list_entry *list_entry; + const char *udev_dev_path; struct stat statbuf; @@ -697,16 +793,21 @@ static int getudevinfo(USBK* usbk, const char *device) { if (udev == NULL) { usbk->lastopr = USBK_LO_UDEV_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } - char devname[1024]; + udev_dev_path = udev_get_dev_path(udev); + + char *devname = alloca(strlen(udev_dev_path) + strlen(device) + 1); + assert(devname != NULL); sprintf(devname, "%s/%s", udev_get_dev_path(udev), device); if (stat(devname, &statbuf) < 0) { usbk->lastopr = USBK_LO_UDEV_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } else { char type; if (S_ISBLK(statbuf.st_mode)) { @@ -743,8 +844,8 @@ static int getudevinfo(USBK* usbk, const char *device) { usbk->dev_node_path = strdup(udev_device_get_devnode(dev)); usbk->usb_serial = strdup(udev_device_get_sysattr_value(dev_usb, "serial")); - rtn = getudevbackdisk(usbk); - if (rtn < 0) { + ret = _get_udev_backdisk(usbk); + if (ret < 0) { udev_device_unref(dev); udev_unref(udev); usbk->lastopr = USBK_LO_UDEV_ERROR; @@ -766,7 +867,7 @@ static int getudevinfo(USBK* usbk, const char *device) { #if defined(__linux__) -static int getudevbackdisk(USBK* usbk) { +static int _get_udev_backdisk(USBK* usbk) { int rtn; struct udev *udev; struct udev_enumerate *enumerate; @@ -821,17 +922,13 @@ static int getudevbackdisk(USBK* usbk) { DBG_LASTOPR_STRING(usbk->lastopr); return (-1) * usbk->lastopr; } -#endif -#if defined(__linux__) - -typedef struct __USBKS +struct USBKS { USBK* usbk; - int counter; + int usbk_disk_count; int lastopr; -}USBKS; - +}; USBKS* usbk_list_new(void) { int i; @@ -839,13 +936,12 @@ USBKS* usbk_list_new(void) { struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; struct udev_device *dev; - struct udev_device *dev_usb = NULL; - struct udev_device *dev_scsi = NULL; - + struct udev_device *dev_usb; + struct udev_device *dev_scsi; USBKS* usbks = (USBKS*)calloc(1, sizeof(USBKS)); - usbks->counter = 0; + usbks->usbk_disk_count = 0; usbks->usbk = NULL; USBK *new_usbk = NULL; @@ -897,7 +993,7 @@ USBKS* usbk_list_new(void) { new_usbk->next->previous = new_usbk; } usbks->usbk = new_usbk; - usbks->counter++; + usbks->usbk_disk_count++; } } } @@ -917,7 +1013,7 @@ int usbk_list_release(USBKS* usbks) { usbk_release(usbks->usbk); usbks->usbk->next = NULL; usbks->usbk = d_usbks; - usbks->counter--; + usbks->usbk_disk_count--; } free(usbks); return 0; @@ -939,7 +1035,7 @@ int usbk_list_refreshall(USBKS* usbks){ USBK* list_entry; usbk_list_entry_foreach(list_entry, usbks->usbk){ - usbk_refreshusbkinfo(list_entry); + usbk_refresh_usbkinfo(list_entry); } usbks->lastopr = USBK_LO_PASS; @@ -948,7 +1044,7 @@ int usbk_list_refreshall(USBKS* usbks){ } int usbk_list_get_counter(USBKS* usbks){ - return usbks->counter; + return usbks->usbk_disk_count; } int usbk_list_get_lastoprstatus(USBKS* usbks){ @@ -977,8 +1073,9 @@ bool usbk_debug_check(void) return debug_enable; } +#if 0 const char* usbk_libversion(void) { return VERSION; } - +#endif diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index cc059d7..ddaf0c3 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -42,7 +42,7 @@ enum E_USBK_KEYSIZE USBK_KEYSIZE_256BIT = 0x03, }; typedef enum E_USBK_KEYSIZE E_USBK_KEYSIZE; -typedef uint8_t usbk_keysize_t; +typedef int8_t usbk_keysize_t; /* * usbk device status type values @@ -58,7 +58,7 @@ enum E_USBK_DEVICE_STATUS USBK_DS_MUST_REMOVE = 0x05, }; typedef enum E_USBK_DEVICE_STATUS E_USBK_DEVICE_STATUS; -typedef uint8_t usbk_ds_t; +typedef int8_t usbk_ds_t; /* * usbk last operation type values @@ -87,7 +87,7 @@ enum E_USBK_LASTOPP USBK_LO_MEM_ERROR = 0x0f, }; typedef enum E_USBK_LASTOPP E_USBK_LASTOPP; -typedef uint8_t usbk_lo_t; +typedef int8_t usbk_lo_t; #ifndef __cplusplus /* @@ -200,12 +200,17 @@ typedef struct USBK USBK; usbk_setkey_hex(usbk, pass, key_no, USBK_KEYSIZE_192BIT, key) #define USBK_SETKEY_256BIT_HEX(usbk, pass, key_no, key) \ usbk_setkey_hex(usbk, pass, key_no, USBK_KEYSIZE_256BIT, key) + +#if 0 +/* FIXME macros of the depreciated function usbk_setkey_decimal */ #define USBK_SETKEY_128BIT_DECIMAL(usbk, pass, key_no, key) \ usbk_setkey_decimal(usbk, pass, key_no, USBK_KEYSIZE_128BIT, key) #define USBK_SETKEY_192BIT_DECIMAL(usbk, pass, key_no, key) \ usbk_setkey_decimal(usbk, pass, key_no, USBK_KEYSIZE_192BIT, key) #define USBK_SETKEY_256BIT_DECIMAL(usbk, pass, key_no, key) \ usbk_setkey_decimal(usbk, pass, key_no, USBK_KEYSIZE_256BIT, key) +#endif + #define USBK_SETKEY_128BIT_TEXT(usbk, pass, key_no, key) \ usbk_setkey_text(usbk, pass, key_no, USBK_KEYSIZE_128BIT, key) #define USBK_SETKEY_192BIT_TEXT(usbk, pass, key_no, key) \ @@ -213,11 +218,11 @@ typedef struct USBK USBK; #define USBK_SETKEY_256BIT_TEXT(usbk, pass, key_no, key) \ usbk_setkey_text(usbk, pass, key_no, USBK_KEYSIZE_256BIT, key) #define USBK_ENABLE_AUTACT(usbk, pass, key_no) \ - usbk_setautact(usbk, pass, key_no) + usbk_set_autact(usbk, pass, key_no) #define USBK_DISABLE_AUTACT(usbk, pass) \ - usbk_setautact(usbk, pass, 0) + usbk_set_autact(usbk, pass, 0) -USBK* usbk_new(const char* dev); +extern USBK* usbk_new(const char* dev); extern int usbk_release(USBK* usbk); /* @@ -227,6 +232,41 @@ extern int usbk_release(USBK* usbk); /* This is the helper macro for reaching the USBK structure */ #define USBK_GET_ELEMENT(usbk, element) (usbk->element) +#if defined(__GNUC__) +# define inline inline __attribute__((always_inline)) +#endif + +/* Debugging informations */ +extern void usbk_debug_enable(void); +extern void usbk_debug_disable(void); +extern void usbk_debug(bool enable); +extern bool usbk_debug_check(void); + +extern const char* usbk_libversion(void); + +/* + * And all the setter and other functions which interacts + * directly with USBK + * + * */ +extern int usbk_key_activate(USBK* usbk, const char* pass, uint8_t key_no); +extern int usbk_key_deactivate(USBK* usbk); +extern int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass); +extern int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label); +extern int usbk_set_keyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); +extern int usbk_set_key_hex(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key); +extern int usbk_set_key_text(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const char* key); + +#if 0 +/* FIXME depreciated function */ +extern int usbk_set_key_decimal(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const char* key); +#endif + +extern int usbk_set_key_and_keyname(USBK* usbk, const char* pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key); +extern int usbk_set_autact(USBK* usbk, const char* pass, int key_no); +extern int usbk_get_randomkey(USBK* usbk, uint8_t*random_key, usbk_keysize_t keysize); +extern int usbk_refresh_usbkinfo(USBK* usbk); + inline const char* usbk_get_dev(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_node); } inline const char* usbk_get_dev_path(USBK* usbk) @@ -266,35 +306,12 @@ inline int usbk_get_retry_number(USBK* usbk) inline usbk_ds_t usbk_get_state(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_state); } -/* Debugging informations */ -extern void usbk_debug_enable(void); -extern void usbk_debug_disable(void); -extern void usbk_debug(bool enable); -extern bool usbk_debug_check(void); - -extern const char* usbk_libversion(void); - -/* - * And all the setter and other functions which interacts - * directly with USBK - * - * */ -extern int usbk_activatekey(USBK* usbk, const char* pass, uint8_t key_no); -extern int usbk_deactivatekey(USBK* usbk); -extern int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass); -extern int usbk_setdevicelabel(USBK* usbk, const char* pass, const char* device_label); -extern int usbk_setkeyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); -extern int usbk_setkey_keyname(USBK* usbk, const char* pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key); -extern int usbk_setkey_hex(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key); -extern int usbk_setkey_decimal(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const char* key); -extern int usbk_setkey_text(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const char* key); -extern int usbk_setautact(USBK* usbk, const char* pass, int key_no); -extern int usbk_getrandomkey(USBK* usbk, uint8_t*random_key, usbk_keysize_t keysize); -extern int usbk_refreshusbkinfo(USBK* usbk); +inline int usbk_set_keyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) + { return usbk_set_key_and_keyname(usbk, pass, key_no, key_name, USBK_KEYSIZE_NULL, NULL); } #if defined(__linux__) -typedef struct __USBKS USBKS; +typedef struct USBKS USBKS; extern USBKS* usbk_list_new(void); extern int usbk_list_release(USBKS* usbks); diff --git a/libusbk/src/linux/usbk_sg_ctl.h b/libusbk/src/linux/usbk_sg_ctl.h index 5a12447..7d6ef80 100644 --- a/libusbk/src/linux/usbk_sg_ctl.h +++ b/libusbk/src/linux/usbk_sg_ctl.h @@ -20,7 +20,6 @@ #ifdef __cplusplus extern "C" { #endif - //PUBLIC DEFINES //*! \name -RETURN VALUES OF SCSI GENERIC LAYER //! @{ diff --git a/libusbk/src/usbk_scsi.c b/libusbk/src/usbk_scsi.c index 49008d0..be29dcc 100644 --- a/libusbk/src/usbk_scsi.c +++ b/libusbk/src/usbk_scsi.c @@ -26,16 +26,16 @@ //PRIVATE VARIABLES //-VENDOR SPECIFIC CMD COMMAND -const ST_CMD_T scsi_cmd[9] = { - {0xFE, 0x00, GET_STATUS, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, GET_DEV_INFO, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, ACTIVATE_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, DEACTIVATE_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, CHANGE_PASS, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, SET_DEV_NAME, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, SET_AUTO_ACTIVE, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, SET_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, - {0xFE, 0x00, GENERATE_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00} +const ST_CMD_T scsi_cmd[] = { + {0xFE, 0x00, USBK_GET_STATUS, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_GET_DEV_INFO, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_ACTIVATE_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_DEACTIVATE_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_CHANGE_PASS, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_SET_DEV_NAME, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_SET_AUTO_ACTIVE, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_SET_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00}, + {0xFE, 0x00, USBK_GENERATE_KEY, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00} }; //PUBLIC FUNCTIONS @@ -52,7 +52,7 @@ int send_scsi_command(const char *usbk_path, unsigned char *buff, int cmd_index, memset(buffer, 0, sizeof(buffer)); - if (rw == WRITE_SCSI) { + if (rw == USBK_WRITE_SCSI) { if (len != 0) { memcpy(buffer, buff, len); } @@ -60,7 +60,7 @@ int send_scsi_command(const char *usbk_path, unsigned char *buff, int cmd_index, packet.cmd = (ST_CMD_T*)&scsi_cmd[cmd_index - 1]; packet.cmdlen = cmdlen; - packet.cmddir = (rw == WRITE_SCSI) ? OUTDIR : INDIR; + packet.cmddir = (rw == USBK_WRITE_SCSI) ? USBK_OUTDIR : USBK_INDIR; packet.data = buffer; packet.datalen = _PACK_BUFF_SIZE; @@ -69,7 +69,7 @@ int send_scsi_command(const char *usbk_path, unsigned char *buff, int cmd_index, return USBK_SCSI_TRANSFER_FAIL; } - if (rw == READ_SCSI) { + if (rw == USBK_READ_SCSI) { if (len != 0) { memcpy(buff, buffer, len); } diff --git a/libusbk/src/usbk_scsi.h b/libusbk/src/usbk_scsi.h index 9bf75b4..c36f383 100644 --- a/libusbk/src/usbk_scsi.h +++ b/libusbk/src/usbk_scsi.h @@ -23,30 +23,30 @@ extern "C" { //PUBLIC DEFINES //-DIRECTIONS -#define INDIR 0X01 -#define OUTDIR 0X00 +#define USBK_INDIR 0X01 +#define USBK_OUTDIR 0X00 //-SCSI CMD DIRECTION -#define WRITE_SCSI OUTDIR -#define READ_SCSI INDIR +#define USBK_WRITE_SCSI USBK_OUTDIR +#define USBK_READ_SCSI USBK_INDIR //!\name VENDOR SPECIFIC SCSI COMMAND FOR USBK //! @{ -#define GET_STATUS 0x01 //!< Get Status of Current Command -#define GET_DEV_INFO 0x02 //!< Get Detail Information about USBK -#define ACTIVATE_KEY 0x03 //!< Activate USBK with Selected Key -#define DEACTIVATE_KEY 0x04 //!< Deactivate USBK -#define CHANGE_PASS 0x05 //!< Change Password -#define SET_DEV_NAME 0x06 //!< Set Device Label -#define SET_AUTO_ACTIVE 0x07 //!< Change AutoActivation Setting (Enable with Selected Key / Disable) -#define SET_KEY 0x08 //!< Set Key -#define GENERATE_KEY 0x09 //!< Generate 256-Bit Random Key +#define USBK_GET_STATUS 0x01 //!< Get Status of Current Command +#define USBK_GET_DEV_INFO 0x02 //!< Get Detail Information about USBK +#define USBK_ACTIVATE_KEY 0x03 //!< Activate USBK with Selected Key +#define USBK_DEACTIVATE_KEY 0x04 //!< Deactivate USBK +#define USBK_CHANGE_PASS 0x05 //!< Change Password +#define USBK_SET_DEV_NAME 0x06 //!< Set Device Label +#define USBK_SET_AUTO_ACTIVE 0x07 //!< Change AutoActivation Setting (Enable with Selected Key / Disable) +#define USBK_SET_KEY 0x08 //!< Set Key +#define USBK_GENERATE_KEY 0x09 //!< Generate 256-Bit Random Key //! @} //!\name -RETURN VALUES OF SCSI LAYER //! @{ -enum usbk_scsi_ret { +enum E_USBK_SCSI_RET { USBK_SCSI_PASS = 0, //!< Pass USBK_SCSI_OPEN_FAIL = -1, //!< Device Open Error USBK_SCSI_TRANSFER_FAIL = -2, //!< Transfer Error diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index f0c596d..6acbd59 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -23,7 +23,7 @@ #include #include -#include "libusbk.h" +#include "../../libusbk/src/libusbk.h" using namespace std; @@ -229,7 +229,7 @@ int main(int argc, char *argv[]) { // ACTIVATE ///////////////////////////////////////////// if (pflag & aflag & kflag) { - res = usbk_activatekey(myusbk, opt_parola, (int) opt_key); + res = usbk_key_activate(myusbk, opt_parola, (int) opt_key); printf("%d\n", res); print_result(myusbk); } @@ -238,7 +238,7 @@ int main(int argc, char *argv[]) { // DEACTIVATE ///////////////////////////////////////////// if (dflag) { - res = usbk_deactivatekey(myusbk); + res = usbk_key_deactivate(myusbk); printf("%d\n", res); print_result(myusbk); } @@ -247,7 +247,7 @@ int main(int argc, char *argv[]) { // CHANGE PASSWORD ///////////////////////////////////////////// if (cflag) { - res = usbk_changepassword(myusbk, opt_parola, opt_new_password); + res = usbk_change_password(myusbk, opt_parola, opt_new_password); printf("%d\n", res); print_result(myusbk); } @@ -256,7 +256,7 @@ int main(int argc, char *argv[]) { // SET DEVICE NAME ///////////////////////////////////////////// if (pflag & nflag) { - res = usbk_setdevicelabel(myusbk, opt_parola, opt_dev_label); + res = usbk_set_devicelabel(myusbk, opt_parola, opt_dev_label); printf("%d\n", res); print_result(myusbk); } @@ -267,10 +267,13 @@ int main(int argc, char *argv[]) { if (pflag & xflag & kflag & Fflag) { switch (opt_key_format) { case 'd': - res = usbk_setkey_decimal(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), opt_string_key); + printf("ERROR: Depreciated function!\n"); + res=-1; + + //res = usbk_setkey_decimal(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), opt_string_key); break; case 't': - res = usbk_setkey_text(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), opt_string_key); + res = usbk_set_key_text(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), opt_string_key); break; default: break; @@ -285,11 +288,10 @@ int main(int argc, char *argv[]) { if (pflag & Xflag & kflag & Fflag) { int i; uint8_t randomkey[32]; - res = usbk_getrandomkey(myusbk, randomkey, parse_keysize(opt_key_size_str)); + res = usbk_get_randomkey(myusbk, randomkey, parse_keysize(opt_key_size_str)); if (res == USBK_LO_PASS) { - res = - usbk_setkey_hex(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), randomkey); + res = usbk_set_key_hex(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), randomkey); if (res == USBK_LO_PASS) { for (i = 0; i < parse_keysize_inbyte(opt_key_size_str); i++) { fprintf(stdout, "%d", randomkey[i]); @@ -308,7 +310,7 @@ int main(int argc, char *argv[]) { ///////////////////////////////////////////// if (mflag & !xflag & !Xflag) { if (pflag & kflag) { - res = usbk_setkeyname(myusbk, opt_parola, opt_key, opt_aes_name); + res = usbk_set_keyname(myusbk, opt_parola, opt_key, opt_aes_name); printf("%d\n", res); print_result(myusbk); } @@ -568,7 +570,7 @@ void linuxcli_show_dev_info(USBK* myusbk) { char autoactive[64]; char model[32]; - usbk_refreshusbkinfo(myusbk); + usbk_refresh_usbkinfo(myusbk); sprintf(backdisk, "-"); From accccaee6d8f404461ac67a9c175aacecff21d30 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Sun, 2 Oct 2011 23:31:08 +0300 Subject: [PATCH 08/16] usbk_list_new refactoring --- libusbk/src/libusbk.c | 243 +++++++++++++++++++++++------------------- libusbk/src/libusbk.h | 14 +-- usbk_cli/src/main.cpp | 21 ++-- 3 files changed, 145 insertions(+), 133 deletions(-) diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c index 4607df8..256bff0 100644 --- a/libusbk/src/libusbk.c +++ b/libusbk/src/libusbk.c @@ -121,6 +121,8 @@ LIBUSBK_SUPPORTED_PRODUCTS products[] = { }; static bool debug_enable = false; +static int usbk_disk_count = 0; +static USBK *usbk_head; static int _get_udev_info(USBK* usbk, const char *device); static int _get_udev_backdisk(USBK* usbk); @@ -777,10 +779,11 @@ static int _get_udev_info(USBK* usbk, const char *device) struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; + struct udev_list_entry *list_entry; + struct udev_device *dev; struct udev_device *dev_usb; struct udev_device *dev_scsi; - struct udev_list_entry *list_entry; const char *str; size_t len; @@ -799,6 +802,7 @@ static int _get_udev_info(USBK* usbk, const char *device) udev_dev_path = udev_get_dev_path(udev); + /* allocate ram space from stack with alloca */ char *devname = alloca(strlen(udev_dev_path) + strlen(device) + 1); assert(devname != NULL); sprintf(devname, "%s/%s", udev_get_dev_path(udev), device); @@ -809,24 +813,30 @@ static int _get_udev_info(USBK* usbk, const char *device) return - usbk->lastopr; } else { - char type; + char dev_type; + if (S_ISBLK(statbuf.st_mode)) { - type = 'b'; - } else if (S_ISCHR(statbuf.st_mode)) { - type = 'c'; + dev_type = 'b'; + } + /* FIXME char devtype is legal? */ + else if (S_ISCHR(statbuf.st_mode)) { + dev_type = 'c'; } else { udev_unref(udev); usbk->lastopr = USBK_LO_UDEV_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } - dev = udev_device_new_from_devnum(udev, type, statbuf.st_rdev); + dev = udev_device_new_from_devnum(udev, dev_type, statbuf.st_rdev); if (dev == NULL) { + udev_unref(udev); usbk->lastopr = USBK_LO_UDEV_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } } @@ -860,28 +870,34 @@ static int _get_udev_info(USBK* usbk, const char *device) } udev_device_unref(dev); udev_unref(udev); + DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } #endif #if defined(__linux__) -static int _get_udev_backdisk(USBK* usbk) { - int rtn; +static int _get_udev_backdisk(USBK* usbk) +{ + int ret; struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; + struct udev_device *dev; - struct udev_device *dev_usb = NULL; + struct udev_device *dev_usb; struct udev_device *dev_scsi; // Create the udev object udev = udev_new(); + if (udev == NULL) { usbk->lastopr = USBK_LO_UDEV_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return - usbk->lastopr; } // Create a list of the devices in the 'abcde' subsystem. @@ -923,132 +939,135 @@ static int _get_udev_backdisk(USBK* usbk) { return (-1) * usbk->lastopr; } -struct USBKS + +int get_usbk_count() { - USBK* usbk; - int usbk_disk_count; - int lastopr; -}; + return usbk_disk_count; +} -USBKS* usbk_list_new(void) { - int i; - struct udev *udev; - struct udev_enumerate *enumerate; - struct udev_list_entry *devices, *dev_list_entry; - struct udev_device *dev; - struct udev_device *dev_usb; - struct udev_device *dev_scsi; +USBK* usbk_list_new(void) +{ + struct udev *_udev = NULL; + struct udev_enumerate *_udev_enumerate = NULL; + struct udev_list_entry *_udev_enumerated_devices = NULL; + struct udev_list_entry *_udev_enumerated_device = NULL; - USBKS* usbks = (USBKS*)calloc(1, sizeof(USBKS)); + struct udev_device *_udev_device = NULL; + struct udev_device *_udev_device_scsi = NULL; + struct udev_device *_udev_device_usb = NULL; - usbks->usbk_disk_count = 0; + const char *_udev_enumerated_device_path; + const char *_udev_device_idvendor; - usbks->usbk = NULL; - USBK *new_usbk = NULL; + int ret; + + USBK *usbk_last_entry = NULL; + USBK *usbk_new_entry = NULL; + + /* initialize udev */ + _udev = udev_new(); + if (_udev == NULL) { + fprintf(stderr, "Error! Udev cannot be initialized!\n"); - // Create the udev object - udev = udev_new(); - if (udev == NULL) { - usbks->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbks->lastopr); return NULL; } - enumerate = udev_enumerate_new(udev); - udev_enumerate_add_match_subsystem(enumerate, "block"); - udev_enumerate_scan_devices(enumerate); - devices = udev_enumerate_get_list_entry(enumerate); + /* get udev enumerated devices */ + _udev_enumerate = udev_enumerate_new(_udev); - udev_list_entry_foreach(dev_list_entry, devices) { - const char *path; - path = udev_list_entry_get_name(dev_list_entry); - dev = udev_device_new_from_syspath(udev, path); - // usb device directory - dev_usb = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device"); - if (dev_usb != NULL) { - if (strncmp(USBK_USB_IDVENDOR, udev_device_get_sysattr_value(dev_usb, "idVendor"), strlen(USBK_USB_IDVENDOR)) == 0) { -//if((strncmp(USBK_USB_IDPRODUCT_A101, udev_device_get_sysattr_value(dev_usb,"idProduct"), strlen(USBK_USB_IDPRODUCT_A101)) == 0) || -//(strncmp(USBK_USB_IDPRODUCT_A103, udev_device_get_sysattr_value(dev_usb,"idProduct"), strlen(USBK_USB_IDPRODUCT_A103)) == 0)){ - dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device"); - if (dev_scsi != NULL) { - if (strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(dev_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)) == 0) { - new_usbk = usbk_new(udev_device_get_sysname(dev)); - if (new_usbk ==NULL) - { - // todo: release all mem location - /* - LibUSBK__list_devices_release(usbk_list); - udev_device_unref(dev); - udev_enumerate_unref(enumerate); - udev_unref(udev); - */ - usbks->lastopr = USBK_LO_MEM_ERROR; - DBG_LASTOPR_STRING(usbks->lastopr); - return NULL; - } + /* add block devices */ + ret = udev_enumerate_add_match_subsystem(_udev_enumerate, "block"); + if (ret) goto done; - new_usbk->previous = NULL; - new_usbk->next = usbks->usbk; - if (new_usbk->previous != NULL){ - new_usbk->next->previous = new_usbk; - } - usbks->usbk = new_usbk; - usbks->usbk_disk_count++; - } - } - } - } - udev_device_unref(dev); - } - udev_enumerate_unref(enumerate); - udev_unref(udev); - return usbks; -} + /* add scan for devices */ + ret = udev_enumerate_scan_devices(_udev_enumerate); + if (ret) goto done; + + /* get device list */ + _udev_enumerated_devices = udev_enumerate_get_list_entry(_udev_enumerate); + + udev_list_entry_foreach (_udev_enumerated_device, _udev_enumerated_devices) { + _udev_enumerated_device_path = udev_list_entry_get_name(_udev_enumerated_device); + _udev_device = udev_device_new_from_syspath(_udev, _udev_enumerated_device_path); + + /* get usb devices */ + _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); + if (!_udev_device_usb) continue; + + _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); + + ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); + if (ret) continue; + + _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); + if (!_udev_device_scsi) continue; + ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); + if (ret) continue; -int usbk_list_release(USBKS* usbks) { - USBK *d_usbks; - while (usbks->usbk != NULL) { - d_usbks = usbks->usbk->next; - usbk_release(usbks->usbk); - usbks->usbk->next = NULL; - usbks->usbk = d_usbks; - usbks->usbk_disk_count--; + usbk_new_entry = usbk_new(udev_device_get_sysname(_udev_device)); + if (!usbk_new_entry) { + usbk_new_entry->lastopr = USBK_LO_MEM_ERROR; + DBG_LASTOPR_STRING(usbk_new_entry->lastopr); + + goto done; + } + + if (!usbk_head) { + usbk_head = usbk_last_entry = usbk_new_entry; + } else { + usbk_last_entry->next = usbk_new_entry; + usbk_last_entry = usbk_new_entry; + } + + usbk_disk_count++; } - free(usbks); - return 0; -} -USBK* usbk_list_get_entry(USBKS* usbks){ - return usbks->usbk; -} +done: + udev_device_unref(_udev_device_usb); + udev_device_unref(_udev_device_scsi); + udev_device_unref(_udev_device); + udev_enumerate_unref(_udev_enumerate); + udev_unref(_udev); -USBK* usbk_list_get_next(USBK* usbk){ - return usbk->next; + return usbk_head; } -USBK* usbk_list_get_previous(USBK* usbk){ - return usbk->previous; +int usbk_list_release() +{ + USBK *_usbk=usbk_head; + + while (_usbk->next) { + usbk_release(_usbk); + _usbk = _usbk->next; + usbk_disk_count--; + } + + usbk_head = NULL; + + return 0; } -int usbk_list_refreshall(USBKS* usbks){ +USBK* usbk_list_get_next(USBK* usbk) +{ + return usbk->next; +} +int usbk_list_refreshall() +{ USBK* list_entry; - usbk_list_entry_foreach(list_entry, usbks->usbk){ + + usbk_list_entry_foreach(list_entry, usbk_head){ usbk_refresh_usbkinfo(list_entry); } - usbks->lastopr = USBK_LO_PASS; - DBG_LASTOPR_STRING(usbks->lastopr); - return (-1) * usbks->lastopr; -} - -int usbk_list_get_counter(USBKS* usbks){ - return usbks->usbk_disk_count; + DBG_LASTOPR_STRING(USBK_LO_PASS); + return USBK_LO_PASS; } -int usbk_list_get_lastoprstatus(USBKS* usbks){ - return usbks->lastopr; +int usbk_list_get_counter() +{ + return usbk_disk_count; } #endif diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index ddaf0c3..3558300 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -186,7 +186,6 @@ struct USBK /* FIXME: remove this double-linked list structure */ struct USBK *next; - struct USBK *previous; }; typedef struct USBK USBK; @@ -311,17 +310,14 @@ inline int usbk_set_keyname(USBK* usbk, const char* pass, uint8_t key_no, const #if defined(__linux__) -typedef struct USBKS USBKS; +extern int get_usbk_count(); -extern USBKS* usbk_list_new(void); -extern int usbk_list_release(USBKS* usbks); -extern USBK* usbk_list_get_entry(USBKS* usbks); +extern USBK* usbk_list_new(); +extern int usbk_list_release(); extern USBK* usbk_list_get_next(USBK* usbk); -extern USBK* usbk_list_get_previous(USBK* usbk); -extern int usbk_list_get_counter(USBKS* usbks); -extern int usbk_list_get_lastoprstatus(USBKS* usbks); +extern int usbk_list_get_counter(); -extern int usbk_list_refreshall(USBKS* usbks); +extern int usbk_list_refreshall(); #define usbk_list_entry_foreach(list_entry, first_entry) \ for (list_entry = first_entry; \ diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index 6acbd59..ad33aaf 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -537,24 +537,21 @@ void print_device(USBK* myusbk) } } -void linuxcli_show_devices(void) { - int i; - int rtn; +void linuxcli_show_devices(void) +{ int counter; - USBKS* usbks = usbk_list_new(); - USBK* list_entry; + USBK* _usbk_head = usbk_list_new(); + USBK* _usbk_entry; - usbk_list_entry_foreach(list_entry, usbk_list_get_entry(usbks)) - { - print_device(list_entry); + usbk_list_entry_foreach (_usbk_entry, _usbk_head) { + print_device(_usbk_entry); } - counter = usbk_list_get_counter(usbks); - (counter > 0) ? printf ("%d",counter):printf ("None"); - printf (" USBK Found.\n\n"); + counter = usbk_list_get_counter(); + printf("%d USBK(s) found", counter); - usbk_list_release(usbks); + usbk_list_release(); } void linuxcli_show_dev_info(USBK* myusbk) { From 2d87edd917aa13e218606864026c6ca87d97e661 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Sun, 2 Oct 2011 23:57:55 +0300 Subject: [PATCH 09/16] whitespace correction --- libusbk/src/libusbk.c | 208 +++++++++++++++++++++--------------------- libusbk/src/libusbk.h | 2 +- 2 files changed, 105 insertions(+), 105 deletions(-) diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c index 256bff0..57dac82 100644 --- a/libusbk/src/libusbk.c +++ b/libusbk/src/libusbk.c @@ -55,11 +55,11 @@ #define USBK_SCSI_BACKDISK_VENDOR "BackDisk" #define USBK_CHECK_SUPPORTED \ - if (!usbk->is_supported) { \ - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; \ - DBG_LASTOPR_STRING(usbk->lastopr); \ - return - usbk->lastopr; \ - } + if (!usbk->is_supported) { \ + usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; \ + DBG_LASTOPR_STRING(usbk->lastopr); \ + return - usbk->lastopr; \ + } typedef struct __RTRN_STRING { int rtrn; @@ -160,17 +160,17 @@ USBK* usbk_new(const char* dev) } #elif defined(WIN32) - usbk->dev_node = strdup(dev); - usbk->dev_node_path = strdup(dev); + usbk->dev_node = strdup(dev); + usbk->dev_node_path = strdup(dev); #endif t_UIP_DEVINFO usbk_info; ret = send_scsi_command( - usbk->dev_node_path, - (unsigned char*) &usbk_info, - USBK_GET_DEV_INFO, - sizeof(usbk_info), - USBK_READ_SCSI); + usbk->dev_node_path, + (unsigned char*) &usbk_info, + USBK_GET_DEV_INFO, + sizeof(usbk_info), + USBK_READ_SCSI); if (ret < 0) { free(usbk); usbk->lastopr = USBK_LO_SCSI_ERROR; @@ -184,23 +184,23 @@ USBK* usbk_new(const char* dev) usbk->firmware_ver = strdup(usbk_info.firmware_ver.s); usbk->is_supported = true; - usbk->multikey_cap = usbk_info.multikeycap; - usbk->current_key = usbk_info.current_keyno; - usbk->autoact_keyno = usbk_info.autoactivate_keyno; - usbk->retry_num = usbk_info.retry_num; - usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; - usbk->dev_label = strdup(usbk_info.devlabel.s); + usbk->multikey_cap = usbk_info.multikeycap; + usbk->current_key = usbk_info.current_keyno; + usbk->autoact_keyno = usbk_info.autoactivate_keyno; + usbk->retry_num = usbk_info.retry_num; + usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; + usbk->dev_label = strdup(usbk_info.devlabel.s); - /* FIXME why we need this much space */ - usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); - for (i = 0; i < UIP_SERIAL_SIZE; i++) { - sprintf((usbk->serial + i * 2), "%2.2X", usbk_info.serial.u8[i]); - } + /* FIXME why we need this much space */ + usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); + for (i = 0; i < UIP_SERIAL_SIZE; i++) { + sprintf((usbk->serial + i * 2), "%2.2X", usbk_info.serial.u8[i]); + } - usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); - for (i = 0; i < usbk->multikey_cap; i++) { - usbk->key_names[i] = strdup(usbk_info.keyname[i].s); - } + usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); + for (i = 0; i < usbk->multikey_cap; i++) { + usbk->key_names[i] = strdup(usbk_info.keyname[i].s); + } usbk->lastopr = USBK_LO_PASS; DBG_LASTOPR_STRING(usbk->lastopr); @@ -410,7 +410,7 @@ int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const cha if (key_name != NULL) { strncpy(setkey.keyname.s, key_name, sizeof(setkey.keyname.s)); } else { - /* preserve old keyname if keyname is not changed */ + /* preserve old keyname if keyname is not changed */ strncpy(setkey.keyname.s, usbk->key_names[key_no - 1], sizeof(setkey.keyname.s)); } @@ -467,7 +467,7 @@ int usbk_set_key_decimal(USBK* usbk, const char *pass, uint8_t key_no, usbk_keys int usbk_set_key_hex(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key) { - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED return usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key); } @@ -626,27 +626,27 @@ static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, u // 6) 16 adet sayı cikartabildin mi? // 7) sayılar 0 ile 255 arasinda mi? - unsigned int key_len = _keysize_as_byte(keysize); - int i; + unsigned int key_len = _keysize_as_byte(keysize); + int i; - /* check key_text if it is null or not */ - if (key_decimal == NULL || key_hex == NULL) - return USBK_LO_INVALID_KEY; + /* check key_text if it is null or not */ + if (key_decimal == NULL || key_hex == NULL) + return USBK_LO_INVALID_KEY; - /* check key length if it is proper or not */ - if(strlen(key_hex) > key_len) { - /* FIXME USBK_LO_INVALID_KEYSIZE? */ - return USBK_LO_INVALID_KEY; - } + /* check key length if it is proper or not */ + if(strlen(key_hex) > key_len) { + /* FIXME USBK_LO_INVALID_KEYSIZE? */ + return USBK_LO_INVALID_KEY; + } - for(i=0; i_keysize_as_byte(keysize)]; @@ -711,38 +711,38 @@ static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, u static inline int16_t _xtod(const char c) { - if (c>='0' && c<='9') return c-'0'; - if (c>='A' && c<='F') return c-'A'+10; - if (c>='a' && c<='f') return c-'a'+10; + if (c>='0' && c<='9') return c-'0'; + if (c>='A' && c<='F') return c-'A'+10; + if (c>='a' && c<='f') return c-'a'+10; - return -1; + return -1; } static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize) { - unsigned int key_len = _keysize_as_byte(keysize); - int i; + unsigned int key_len = _keysize_as_byte(keysize); + int i; - /* check key_text if it is null or not */ - if (key_text == NULL || key_hex == NULL) - return USBK_LO_INVALID_KEY; + /* check key_text if it is null or not */ + if (key_text == NULL || key_hex == NULL) + return USBK_LO_INVALID_KEY; - /* check key length if it is proper or not */ - if(strlen(key_hex) > key_len) { - /* FIXME USBK_LO_INVALID_KEYSIZE? */ - return USBK_LO_INVALID_KEY; - } + /* check key length if it is proper or not */ + if(strlen(key_hex) > key_len) { + /* FIXME USBK_LO_INVALID_KEYSIZE? */ + return USBK_LO_INVALID_KEY; + } - for(i=0; ilastopr = USBK_LO_UDEV_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); @@ -942,7 +942,7 @@ static int _get_udev_backdisk(USBK* usbk) int get_usbk_count() { - return usbk_disk_count; + return usbk_disk_count; } USBK* usbk_list_new(void) @@ -953,7 +953,7 @@ USBK* usbk_list_new(void) struct udev_list_entry *_udev_enumerated_device = NULL; struct udev_device *_udev_device = NULL; - struct udev_device *_udev_device_scsi = NULL; + struct udev_device *_udev_device_scsi = NULL; struct udev_device *_udev_device_usb = NULL; const char *_udev_enumerated_device_path; @@ -967,7 +967,7 @@ USBK* usbk_list_new(void) /* initialize udev */ _udev = udev_new(); if (_udev == NULL) { - fprintf(stderr, "Error! Udev cannot be initialized!\n"); + fprintf(stderr, "Error! Udev cannot be initialized!\n"); return NULL; } @@ -994,56 +994,56 @@ USBK* usbk_list_new(void) _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); if (!_udev_device_usb) continue; - _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); + _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); - ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); - if (ret) continue; + ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); + if (ret) continue; - _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); - if (!_udev_device_scsi) continue; + _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); + if (!_udev_device_scsi) continue; - ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); - if (ret) continue; + ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); + if (ret) continue; - usbk_new_entry = usbk_new(udev_device_get_sysname(_udev_device)); - if (!usbk_new_entry) { - usbk_new_entry->lastopr = USBK_LO_MEM_ERROR; - DBG_LASTOPR_STRING(usbk_new_entry->lastopr); + usbk_new_entry = usbk_new(udev_device_get_sysname(_udev_device)); + if (!usbk_new_entry) { + usbk_new_entry->lastopr = USBK_LO_MEM_ERROR; + DBG_LASTOPR_STRING(usbk_new_entry->lastopr); - goto done; - } + goto done; + } - if (!usbk_head) { - usbk_head = usbk_last_entry = usbk_new_entry; - } else { - usbk_last_entry->next = usbk_new_entry; - usbk_last_entry = usbk_new_entry; - } + if (!usbk_head) { + usbk_head = usbk_last_entry = usbk_new_entry; + } else { + usbk_last_entry->next = usbk_new_entry; + usbk_last_entry = usbk_new_entry; + } - usbk_disk_count++; + usbk_disk_count++; } done: - udev_device_unref(_udev_device_usb); - udev_device_unref(_udev_device_scsi); - udev_device_unref(_udev_device); - udev_enumerate_unref(_udev_enumerate); - udev_unref(_udev); + udev_device_unref(_udev_device_usb); + udev_device_unref(_udev_device_scsi); + udev_device_unref(_udev_device); + udev_enumerate_unref(_udev_enumerate); + udev_unref(_udev); return usbk_head; } int usbk_list_release() { - USBK *_usbk=usbk_head; + USBK *_usbk=usbk_head; - while (_usbk->next) { - usbk_release(_usbk); - _usbk = _usbk->next; - usbk_disk_count--; - } + while (_usbk->next) { + usbk_release(_usbk); + _usbk = _usbk->next; + usbk_disk_count--; + } - usbk_head = NULL; + usbk_head = NULL; return 0; } diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index 3558300..6e86982 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -306,7 +306,7 @@ inline usbk_ds_t usbk_get_state(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_state); } inline int usbk_set_keyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name) - { return usbk_set_key_and_keyname(usbk, pass, key_no, key_name, USBK_KEYSIZE_NULL, NULL); } + { return usbk_set_key_and_keyname(usbk, pass, key_no, key_name, USBK_KEYSIZE_NULL, NULL); } #if defined(__linux__) From 83df632a45211838b5becf831c0c1de73cac9771 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Tue, 11 Oct 2011 22:26:07 +0300 Subject: [PATCH 10/16] clear segfaults - libudef clears all contexts as default, so manually unrefs causes segfaults. --- libusbk/src/libusbk.c | 469 ++++++++++++++++++++++++------------------ 1 file changed, 272 insertions(+), 197 deletions(-) diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c index 57dac82..dea65e9 100644 --- a/libusbk/src/libusbk.c +++ b/libusbk/src/libusbk.c @@ -54,7 +54,7 @@ #define USBK_SCSI_VENDOR "USBK" #define USBK_SCSI_BACKDISK_VENDOR "BackDisk" -#define USBK_CHECK_SUPPORTED \ +#define USBK_CHECK_SUPPORTED(usbk) \ if (!usbk->is_supported) { \ usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; \ DBG_LASTOPR_STRING(usbk->lastopr); \ @@ -87,7 +87,7 @@ RTRN_STRING lastopr_string[] = { {USBK_LO_MEM_ERROR, "memory allocation error"} }; -typedef struct __LIBUSBK_SUPPORTED_MODELS { +typedef struct { const char* model; bool support; } LIBUSBK_SUPPORTED_MODELS; @@ -95,10 +95,10 @@ typedef struct __LIBUSBK_SUPPORTED_MODELS { LIBUSBK_SUPPORTED_MODELS models_1_1[] = { { "A101", true }, { "A103", true }, - {NULL, false } + { NULL, false } }; -typedef struct __LIBUSBK_SUPPORTED_VERSIONS { +typedef struct { int major_version; int minor_version; LIBUSBK_SUPPORTED_MODELS* models; @@ -109,7 +109,7 @@ LIBUSBK_SUPPORTED_VERSIONS versions_1[] = { { 0, 0, NULL } }; -typedef struct __LIBUSBK_SUPPORTED_PRODUCTS { +typedef struct { const char* product; LIBUSBK_SUPPORTED_VERSIONS* versions; } LIBUSBK_SUPPORTED_PRODUCTS; @@ -140,10 +140,10 @@ USBK* usbk_new(const char* dev) int ret; int i; - USBK* usbk = (USBK*)calloc(1, sizeof(USBK)); + USBK* usbk = (USBK*) calloc(1, sizeof(USBK)); if (usbk == NULL) { - usbk->lastopr = USBK_LO_MEM_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + free(usbk); + DBG_LASTOPR_STRING(USBK_LO_UDEV_ERROR); return NULL; } @@ -153,19 +153,17 @@ USBK* usbk_new(const char* dev) ret = _get_udev_info(usbk, dev); if (ret < 0) { free(usbk); - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + DBG_LASTOPR_STRING(USBK_LO_UDEV_ERROR); return NULL; } - #elif defined(WIN32) usbk->dev_node = strdup(dev); usbk->dev_node_path = strdup(dev); #endif t_UIP_DEVINFO usbk_info; - ret = send_scsi_command( + ret = send_scsi_command ( usbk->dev_node_path, (unsigned char*) &usbk_info, USBK_GET_DEV_INFO, @@ -173,8 +171,7 @@ USBK* usbk_new(const char* dev) USBK_READ_SCSI); if (ret < 0) { free(usbk); - usbk->lastopr = USBK_LO_SCSI_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + DBG_LASTOPR_STRING(USBK_LO_SCSI_ERROR); return NULL; } @@ -238,73 +235,94 @@ int usbk_key_activate(USBK* usbk, const char* password, uint8_t key_no) int ret; t_UIP_ACTIVATE activate; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) if (key_no > usbk->multikey_cap) { usbk->lastopr = USBK_LO_INVALID_KEYNO; - DBG_LASTOPR_STRING(usbk->lastopr); + ret = - usbk->lastopr; - return - usbk->lastopr; + goto done; } switch (usbk->dev_state) { case USBK_DS_DEACTIVATE: memset(&activate, 0, sizeof(activate)); + + /* FIXME: check password size */ strcpy(activate.password.s, password); activate.keyno = key_no; - ret = send_scsi_command(usbk->dev_node_path, (unsigned char *) &activate, USBK_ACTIVATE_KEY, sizeof(activate), USBK_WRITE_SCSI); + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char *) &activate, + USBK_ACTIVATE_KEY, + sizeof(activate), + USBK_WRITE_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; - break; + ret = - usbk->lastopr; + + goto done; } usbk->lastopr = _command_status(usbk); break; + case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: case USBK_DS_FABRIC_DEFAULT: case USBK_DS_MUST_REMOVE: default: - printf("state error"); usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; - break; + goto done; } +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } int usbk_key_deactivate(USBK* usbk) { int ret; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) switch (usbk->dev_state) { case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) NULL, USBK_DEACTIVATE_KEY, 0, USBK_WRITE_SCSI); + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) NULL, + USBK_DEACTIVATE_KEY, + 0, + USBK_WRITE_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; - break; + ret = - usbk->lastopr; + + goto done; } usbk->lastopr = _command_status(usbk); break; + case USBK_DS_DEACTIVATE: case USBK_DS_FABRIC_DEFAULT: case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; - break; + goto done; } +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass) @@ -312,7 +330,7 @@ int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass) int ret; t_UIP_CHPASS chpass; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) switch (usbk->dev_state) { case USBK_DS_DEACTIVATE: @@ -331,24 +349,28 @@ int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass) ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &chpass, USBK_CHANGE_PASS, sizeof(chpass), USBK_WRITE_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; - break; + goto done; } usbk->lastopr = _command_status(usbk); break; + case USBK_DS_ACTIVATE: case USBK_DS_ACTIVATE_WITH_BACKDISK: case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; - break; + goto done; } +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) @@ -356,7 +378,7 @@ int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) int ret; t_UIP_SETDEVICELABEL devlabel; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) switch (usbk->dev_state) { case USBK_DS_DEACTIVATE: @@ -367,7 +389,9 @@ int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; - break; + ret = - usbk->lastopr; + + goto done; } usbk->lastopr = _command_status(usbk); @@ -378,13 +402,15 @@ int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; - break; + goto done; } +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key) @@ -392,13 +418,13 @@ int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const cha int ret; t_UIP_SETKEY setkey; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) if (key_no > usbk->multikey_cap) { usbk->lastopr = USBK_LO_INVALID_KEYNO; - DBG_LASTOPR_STRING(usbk->lastopr); + ret = - usbk->lastopr; - return - usbk->lastopr; + goto done; } switch (usbk->dev_state) { @@ -422,11 +448,17 @@ int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const cha setkey.options.me = SETKEY_NAMEONLY; } - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &setkey, USBK_SET_KEY, sizeof(setkey), USBK_WRITE_SCSI); + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &setkey, + USBK_SET_KEY, + sizeof(setkey), + USBK_WRITE_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; - break; + goto done; } usbk->lastopr = _command_status(usbk); @@ -437,13 +469,15 @@ int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const cha case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; - break; + goto done; } +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } #if 0 @@ -452,7 +486,7 @@ int usbk_set_key_decimal(USBK* usbk, const char *pass, uint8_t key_no, usbk_keys int ret = 0; uint8_t key_hex[UIP_KEYSIZE_TOTALSIZE]; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) usbk->lastopr = _convert_key_decimal2hex(key_hex, key, key_size); if (ret == USBK_LO_PASS){ @@ -467,26 +501,28 @@ int usbk_set_key_decimal(USBK* usbk, const char *pass, uint8_t key_no, usbk_keys int usbk_set_key_hex(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key) { - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) return usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key); } int usbk_set_key_text(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) { - int ret = 0; - uint8_t key_hex[32]; + int ret; + uint8_t key_hex[32]; /* use macro definition instead of 32 */ - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) - usbk->lastopr = _convert_key_text2hex(key_hex, key, key_size); + ret = _convert_key_text2hex(key_hex, key, key_size); if (ret == USBK_LO_PASS){ - usbk->lastopr = usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key_hex); + ret = usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key_hex); } + usbk->lastopr = ret; + DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } int usbk_set_autact(USBK* usbk, const char *pass, int key_no) @@ -494,13 +530,13 @@ int usbk_set_autact(USBK* usbk, const char *pass, int key_no) int ret; t_UIP_SETAUTOACTIVATE autoact; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) if (key_no > usbk->multikey_cap) { usbk->lastopr = USBK_LO_INVALID_KEYNO; - DBG_LASTOPR_STRING(usbk->lastopr); + ret = - usbk->lastopr; - return - usbk->lastopr; + goto done; } switch (usbk->dev_state) { @@ -508,11 +544,17 @@ int usbk_set_autact(USBK* usbk, const char *pass, int key_no) memset(&autoact, 0, sizeof(autoact)); strncpy(autoact.password.s, pass, sizeof(autoact.password.s)); autoact.keyno = key_no; - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &autoact, USBK_SET_AUTO_ACTIVE, sizeof(autoact), USBK_WRITE_SCSI); + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &autoact, + USBK_SET_AUTO_ACTIVE, + sizeof(autoact), + USBK_WRITE_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; - break; + goto done; } usbk->lastopr = _command_status(usbk); @@ -523,13 +565,15 @@ int usbk_set_autact(USBK* usbk, const char *pass, int key_no) case USBK_DS_MUST_REMOVE: default: usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; - break; + goto done; } +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) @@ -537,22 +581,27 @@ int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) int ret; t_UIP_GENERATEKEY genkey; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) unsigned int keysize_inbyte = _keysize_as_byte(keysize); if ((keysize_inbyte == 0) || (keysize_inbyte > _keysize_as_byte(USBK_KEYSIZE_256BIT))) { usbk->lastopr = USBK_LO_INVALID_KEYSIZE; - DBG_LASTOPR_STRING(usbk->lastopr); + ret = - usbk->lastopr; - return - usbk->lastopr; + goto done; } - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &genkey, USBK_GENERATE_KEY, sizeof(genkey), USBK_READ_SCSI); + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &genkey, + USBK_GENERATE_KEY, + sizeof(genkey), + USBK_READ_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + ret = - usbk->lastopr; - return - usbk->lastopr; + goto done; } usbk->lastopr = _command_status(usbk); @@ -560,9 +609,10 @@ int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) memcpy(random_key, genkey.key.u8, keysize_inbyte); } +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } int usbk_refresh_usbkinfo(USBK* usbk) @@ -570,15 +620,20 @@ int usbk_refresh_usbkinfo(USBK* usbk) int i; int ret; - USBK_CHECK_SUPPORTED + USBK_CHECK_SUPPORTED(usbk) t_UIP_DEVINFO usbk_info; - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &usbk_info, USBK_GET_DEV_INFO, sizeof(usbk_info), USBK_READ_SCSI); + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &usbk_info, + USBK_GET_DEV_INFO, + sizeof(usbk_info), + USBK_READ_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + ret = - usbk->lastopr; - return - usbk->lastopr; + goto done; } usbk->current_key = usbk_info.current_keyno; @@ -593,25 +648,33 @@ int usbk_refresh_usbkinfo(USBK* usbk) } usbk->lastopr = USBK_LO_PASS; +done: DBG_LASTOPR_STRING(usbk->lastopr); - return usbk->lastopr; + return ret; } static int _command_status(USBK* usbk) { - t_UIP_GETSTATUS status; int ret; + t_UIP_GETSTATUS status; - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &status, USBK_GET_STATUS, sizeof(t_UIP_GETSTATUS), USBK_READ_SCSI); + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &status, + USBK_GET_STATUS, + sizeof(t_UIP_GETSTATUS), + USBK_READ_SCSI); if (ret < 0) { usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); + return - usbk->lastopr; } usbk->retry_num = status.retry_num; + /* FIXME a new review required!! */ return (status.lastop.me - 1); //todo versiyon checking'ine göre düzleltilecek. } @@ -776,100 +839,89 @@ static int _get_udev_info(USBK* usbk, const char *device) { int ret; - struct udev *udev; - struct udev_enumerate *enumerate; - struct udev_list_entry *devices, *dev_list_entry; - struct udev_list_entry *list_entry; + struct udev *_udev = NULL; + struct udev_enumerate *_udev_enumerate = NULL; - struct udev_device *dev; - struct udev_device *dev_usb; - struct udev_device *dev_scsi; + struct udev_list_entry *_udev_enumerated_devices; + struct udev_list_entry *_udev_enumerated_device = NULL; + struct udev_list_entry *_udev_list_entry; - const char *str; - size_t len; - int i; - const char *udev_dev_path; + struct udev_device *_udev_device = NULL; + struct udev_device *_udev_device_scsi = NULL; + struct udev_device *_udev_device_usb = NULL; + + const char *_udev_enumerated_device_path; + const char *_udev_device_idvendor; + const char *_udev_device_idproduct; + + const char *_udev_dev_path; + char *_udev_devname; + char dev_type; struct stat statbuf; - udev = udev_new(); - if (udev == NULL) { - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + usbk->lastopr = USBK_LO_UDEV_ERROR; - return - usbk->lastopr; - } + _udev = udev_new(); + if (!_udev) goto done; - udev_dev_path = udev_get_dev_path(udev); + _udev_dev_path = udev_get_dev_path(_udev); /* allocate ram space from stack with alloca */ - char *devname = alloca(strlen(udev_dev_path) + strlen(device) + 1); - assert(devname != NULL); - sprintf(devname, "%s/%s", udev_get_dev_path(udev), device); + _udev_devname = alloca(strlen(_udev_dev_path) + strlen(device) + 1); + assert(_udev_devname != NULL); + sprintf(_udev_devname, "%s/%s", udev_get_dev_path(_udev), device); - if (stat(devname, &statbuf) < 0) { - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + if (stat(_udev_devname, &statbuf) < 0) goto done; - return - usbk->lastopr; - } else { - char dev_type; + if (S_ISBLK(statbuf.st_mode)) { + dev_type = 'b'; + } + /* FIXME char devtype is legal? */ + else if (S_ISCHR(statbuf.st_mode)) { + dev_type = 'c'; + } else { + goto done; + } - if (S_ISBLK(statbuf.st_mode)) { - dev_type = 'b'; - } - /* FIXME char devtype is legal? */ - else if (S_ISCHR(statbuf.st_mode)) { - dev_type = 'c'; - } else { - udev_unref(udev); - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + _udev_device = udev_device_new_from_devnum(_udev, dev_type, statbuf.st_rdev); - return - usbk->lastopr; - } + if (!_udev_device) goto done; - dev = udev_device_new_from_devnum(udev, dev_type, statbuf.st_rdev); + _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); + if (!_udev_device_usb) goto done; - if (dev == NULL) { - udev_unref(udev); - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); - return - usbk->lastopr; - } - } + ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); + if (ret) goto done; - dev_usb = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device"); + _udev_device_idproduct = udev_device_get_sysattr_value(_udev_device_usb, "idProduct"); - usbk->lastopr = USBK_LO_UDEV_ERROR; - if (dev_usb != NULL) { - if (strncmp(USBK_USB_IDVENDOR, udev_device_get_sysattr_value(dev_usb, "idVendor"), strlen(USBK_USB_IDVENDOR)) == 0) { - if ((strncmp(USBK_USB_IDPRODUCT_A101, udev_device_get_sysattr_value(dev_usb, "idProduct"), strlen(USBK_USB_IDPRODUCT_A101)) == 0) - || (strncmp(USBK_USB_IDPRODUCT_A103, udev_device_get_sysattr_value(dev_usb, "idProduct"), strlen(USBK_USB_IDPRODUCT_A103)) == 0)) { - dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device"); - if (dev_scsi != NULL) { - if (strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(dev_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)) == 0) { - usbk->dev_node = strdup(udev_device_get_sysname(dev)); - usbk->dev_node_path = strdup(udev_device_get_devnode(dev)); - usbk->usb_serial = strdup(udev_device_get_sysattr_value(dev_usb, "serial")); - - ret = _get_udev_backdisk(usbk); - if (ret < 0) { - udev_device_unref(dev); - udev_unref(udev); - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - usbk->lastopr = USBK_LO_PASS; - } - } - } - } - } - udev_device_unref(dev); - udev_unref(udev); + if (!strncmp(USBK_USB_IDPRODUCT_A101, _udev_device_idproduct, strlen(_udev_device_idproduct)) && + !strncmp(USBK_USB_IDPRODUCT_A103, _udev_device_idproduct, strlen(_udev_device_idproduct))) + goto done; + + _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); + if (!_udev_device_scsi) goto done; + + ret = strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)); + if (!ret) goto done; + + usbk->dev_node = strdup(udev_device_get_sysname(_udev_device)); + usbk->dev_node_path = strdup(udev_device_get_devnode(_udev_device)); + usbk->usb_serial = strdup(udev_device_get_sysattr_value(_udev_device_usb, "serial")); + + /* another usbk_lo_** required for backdisk error */ + ret = _get_udev_backdisk(usbk); + if (ret < 0) goto done; + + usbk->lastopr = USBK_LO_PASS; + +done: + udev_device_unref(_udev_device); + udev_enumerate_unref(_udev_enumerate); + udev_unref(_udev); DBG_LASTOPR_STRING(usbk->lastopr); @@ -882,61 +934,87 @@ static int _get_udev_info(USBK* usbk, const char *device) static int _get_udev_backdisk(USBK* usbk) { int ret; - struct udev *udev; - struct udev_enumerate *enumerate; - struct udev_list_entry *devices, *dev_list_entry; - struct udev_device *dev; - struct udev_device *dev_usb; - struct udev_device *dev_scsi; + struct udev *_udev = NULL; + struct udev_enumerate *_udev_enumerate = NULL; + struct udev_list_entry *_udev_enumerated_devices = NULL; + struct udev_list_entry *_udev_enumerated_device = NULL; + + struct udev_device *_udev_device = NULL; + struct udev_device *_udev_device_scsi = NULL; + struct udev_device *_udev_device_usb = NULL; - // Create the udev object - udev = udev_new(); + const char *_udev_enumerated_device_path; + const char *_udev_device_idvendor; + const char *_udev_device_idproduct; + const char *_udev_device_usb_serial; - if (udev == NULL) { - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); + usbk->lastopr = USBK_LO_UDEV_ERROR; - return - usbk->lastopr; - } + // Create the _udev object + _udev = udev_new(); - // Create a list of the devices in the 'abcde' subsystem. - enumerate = udev_enumerate_new(udev); - udev_enumerate_add_match_subsystem(enumerate, "block"); - udev_enumerate_scan_devices(enumerate); - devices = udev_enumerate_get_list_entry(enumerate); + if (!_udev) goto done; - usbk->lastopr = USBK_LO_UDEV_ERROR; + /* get udev enumerated devices */ + _udev_enumerate = udev_enumerate_new(_udev); - udev_list_entry_foreach(dev_list_entry, devices) { - const char *path; - path = udev_list_entry_get_name(dev_list_entry); - dev = udev_device_new_from_syspath(udev, path); - // usb device directory - dev_usb = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device"); - if (dev_usb != NULL) { - if (strncmp(USBK_USB_IDVENDOR, udev_device_get_sysattr_value(dev_usb, "idVendor"), strlen(USBK_USB_IDVENDOR)) == 0) { - if ((strncmp(USBK_USB_IDPRODUCT_A101, udev_device_get_sysattr_value(dev_usb, "idProduct"), strlen(USBK_USB_IDPRODUCT_A101)) == 0) - || (strncmp(USBK_USB_IDPRODUCT_A103, udev_device_get_sysattr_value(dev_usb, "idProduct"), strlen(USBK_USB_IDPRODUCT_A103)) == 0)) { - if (strcmp(usbk->usb_serial, udev_device_get_sysattr_value(dev_usb, "serial")) == 0) { - dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device"); - if (dev_scsi != NULL) { - if (strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(dev_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)) == 0) { - usbk->backdisk_node= strdup(udev_device_get_sysname(dev)); - usbk->backdisk_node_path = strdup(udev_device_get_devnode(dev)); - usbk->lastopr = USBK_LO_PASS; - } - } - } - } - } - } - udev_device_unref(dev); + /* add block devices */ + ret = udev_enumerate_add_match_subsystem(_udev_enumerate, "block"); + if (ret) goto done; + + /* add scan for devices */ + ret = udev_enumerate_scan_devices(_udev_enumerate); + if (ret) goto done; + + /* get device list */ + _udev_enumerated_devices = udev_enumerate_get_list_entry(_udev_enumerate); + + udev_list_entry_foreach (_udev_enumerated_device, _udev_enumerated_devices) { + _udev_enumerated_device_path = udev_list_entry_get_name(_udev_enumerated_device); + _udev_device = udev_device_new_from_syspath(_udev, _udev_enumerated_device_path); + + /* get usb devices */ + _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); + if (!_udev_device_usb) continue; + + _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); + + ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); + if (ret) continue; + + _udev_device_idproduct = udev_device_get_sysattr_value(_udev_device_usb, "idProduct"); + + if (!strncmp(USBK_USB_IDPRODUCT_A101, _udev_device_idproduct, strlen(_udev_device_idproduct)) && + !strncmp(USBK_USB_IDPRODUCT_A103, _udev_device_idproduct, strlen(_udev_device_idproduct))) + continue; + + _udev_device_usb_serial = udev_device_get_sysattr_value(_udev_device_usb, "serial"); + + ret = strcmp(usbk->usb_serial, _udev_device_usb_serial); + if (ret) continue; + + _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); + if (!_udev_device_scsi) continue; + + ret = strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)); + if (!ret) continue; + + usbk->backdisk_node= strdup(udev_device_get_sysname(_udev_device)); + usbk->backdisk_node_path = strdup(udev_device_get_devnode(_udev_device)); + + usbk->lastopr = USBK_LO_PASS; + break; } - udev_enumerate_unref(enumerate); - udev_unref(udev); + +done: + udev_device_unref(_udev_device); + udev_enumerate_unref(_udev_enumerate); + udev_unref(_udev); + DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; + + return usbk->lastopr; } @@ -1007,8 +1085,7 @@ USBK* usbk_list_new(void) usbk_new_entry = usbk_new(udev_device_get_sysname(_udev_device)); if (!usbk_new_entry) { - usbk_new_entry->lastopr = USBK_LO_MEM_ERROR; - DBG_LASTOPR_STRING(usbk_new_entry->lastopr); + DBG_LASTOPR_STRING(USBK_LO_MEM_ERROR); goto done; } @@ -1024,8 +1101,6 @@ USBK* usbk_list_new(void) } done: - udev_device_unref(_udev_device_usb); - udev_device_unref(_udev_device_scsi); udev_device_unref(_udev_device); udev_enumerate_unref(_udev_enumerate); udev_unref(_udev); From 255ef7c856d1ee425c9c208eb179e39070b5ddb0 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Tue, 18 Oct 2011 23:39:50 +0300 Subject: [PATCH 11/16] ne diyim bilemiyorum ki.. --- libusbk/src/libusbk.c | 77 ++++++++++++++++++++++--------------------- libusbk/src/libusbk.h | 24 +++++++++----- usbk_cli/src/main.cpp | 8 ++--- 3 files changed, 60 insertions(+), 49 deletions(-) diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c index dea65e9..4da8427 100644 --- a/libusbk/src/libusbk.c +++ b/libusbk/src/libusbk.c @@ -121,8 +121,6 @@ LIBUSBK_SUPPORTED_PRODUCTS products[] = { }; static bool debug_enable = false; -static int usbk_disk_count = 0; -static USBK *usbk_head; static int _get_udev_info(USBK* usbk, const char *device); static int _get_udev_backdisk(USBK* usbk); @@ -158,8 +156,8 @@ USBK* usbk_new(const char* dev) return NULL; } #elif defined(WIN32) - usbk->dev_node = strdup(dev); - usbk->dev_node_path = strdup(dev); + usbk_head->dev_node = strdup(dev); + usbk_head->dev_node_path = strdup(dev); #endif t_UIP_DEVINFO usbk_info; @@ -481,21 +479,21 @@ int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const cha } #if 0 -int usbk_set_key_decimal(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) +int usbk_set_key_decimal(USBK* usbk_head, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) { int ret = 0; uint8_t key_hex[UIP_KEYSIZE_TOTALSIZE]; - USBK_CHECK_SUPPORTED(usbk) + USBK_CHECK_SUPPORTED(usbk_head) - usbk->lastopr = _convert_key_decimal2hex(key_hex, key, key_size); + usbk_head->lastopr = _convert_key_decimal2hex(key_hex, key, key_size); if (ret == USBK_LO_PASS){ - usbk->lastopr = usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key_hex); + usbk_head->lastopr = usbk_set_key_and_keyname(usbk_head, pass, key_no, NULL, key_size, key_hex); } - DBG_LASTOPR_STRING(usbk->lastopr); + DBG_LASTOPR_STRING(usbk_head->lastopr); - return usbk->lastopr; + return usbk_head->lastopr; } #endif @@ -877,8 +875,8 @@ static int _get_udev_info(USBK* usbk, const char *device) if (S_ISBLK(statbuf.st_mode)) { dev_type = 'b'; } - /* FIXME char devtype is legal? */ else if (S_ISCHR(statbuf.st_mode)) { + /* FIXME char devtype is legal? */ dev_type = 'c'; } else { goto done; @@ -905,8 +903,8 @@ static int _get_udev_info(USBK* usbk, const char *device) _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); if (!_udev_device_scsi) goto done; - ret = strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)); - if (!ret) goto done; + ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); + if (ret) goto done; usbk->dev_node = strdup(udev_device_get_sysname(_udev_device)); usbk->dev_node_path = strdup(udev_device_get_devnode(_udev_device)); @@ -927,9 +925,6 @@ static int _get_udev_info(USBK* usbk, const char *device) return usbk->lastopr; } -#endif - -#if defined(__linux__) static int _get_udev_backdisk(USBK* usbk) { @@ -985,6 +980,7 @@ static int _get_udev_backdisk(USBK* usbk) _udev_device_idproduct = udev_device_get_sysattr_value(_udev_device_usb, "idProduct"); + /* FIXME: code enhancement required */ if (!strncmp(USBK_USB_IDPRODUCT_A101, _udev_device_idproduct, strlen(_udev_device_idproduct)) && !strncmp(USBK_USB_IDPRODUCT_A103, _udev_device_idproduct, strlen(_udev_device_idproduct))) continue; @@ -998,12 +994,14 @@ static int _get_udev_backdisk(USBK* usbk) if (!_udev_device_scsi) continue; ret = strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)); - if (!ret) continue; + if (ret) continue; - usbk->backdisk_node= strdup(udev_device_get_sysname(_udev_device)); + usbk->backdisk_node = strdup(udev_device_get_sysname(_udev_device)); usbk->backdisk_node_path = strdup(udev_device_get_devnode(_udev_device)); usbk->lastopr = USBK_LO_PASS; + + /* no need to proceed loop */ break; } @@ -1017,13 +1015,7 @@ static int _get_udev_backdisk(USBK* usbk) return usbk->lastopr; } - -int get_usbk_count() -{ - return usbk_disk_count; -} - -USBK* usbk_list_new(void) +USBK_LIST* usbk_list_new(void) { struct udev *_udev = NULL; struct udev_enumerate *_udev_enumerate = NULL; @@ -1038,6 +1030,7 @@ USBK* usbk_list_new(void) const char *_udev_device_idvendor; int ret; + USBK_LIST *usbk_list; USBK *usbk_last_entry = NULL; USBK *usbk_new_entry = NULL; @@ -1064,6 +1057,14 @@ USBK* usbk_list_new(void) /* get device list */ _udev_enumerated_devices = udev_enumerate_get_list_entry(_udev_enumerate); + usbk_list = (USBK_LIST *) calloc(1, sizeof(USBK_LIST)); + + if (!usbk_list) { + DBG_LASTOPR_STRING(USBK_LO_MEM_ERROR); + + goto done; + } + udev_list_entry_foreach (_udev_enumerated_device, _udev_enumerated_devices) { _udev_enumerated_device_path = udev_list_entry_get_name(_udev_enumerated_device); _udev_device = udev_device_new_from_syspath(_udev, _udev_enumerated_device_path); @@ -1088,16 +1089,17 @@ USBK* usbk_list_new(void) DBG_LASTOPR_STRING(USBK_LO_MEM_ERROR); goto done; + } - if (!usbk_head) { - usbk_head = usbk_last_entry = usbk_new_entry; + if (!usbk_list->usbk_head) { + usbk_list->usbk_head = usbk_last_entry = usbk_new_entry; } else { usbk_last_entry->next = usbk_new_entry; usbk_last_entry = usbk_new_entry; } - usbk_disk_count++; + usbk_list->count++; } done: @@ -1105,20 +1107,20 @@ USBK* usbk_list_new(void) udev_enumerate_unref(_udev_enumerate); udev_unref(_udev); - return usbk_head; + return usbk_list; } -int usbk_list_release() +int usbk_list_release(USBK_LIST *usbk_list) { - USBK *_usbk=usbk_head; + USBK *_usbk=usbk_list->usbk_head; while (_usbk->next) { usbk_release(_usbk); _usbk = _usbk->next; - usbk_disk_count--; + usbk_list->count--; } - usbk_head = NULL; + free(usbk_list); return 0; } @@ -1128,21 +1130,22 @@ USBK* usbk_list_get_next(USBK* usbk) return usbk->next; } -int usbk_list_refreshall() +int usbk_list_refreshall(USBK_LIST *usbk_list) { USBK* list_entry; - usbk_list_entry_foreach(list_entry, usbk_head){ + usbk_list_entry_foreach (list_entry, usbk_list) { usbk_refresh_usbkinfo(list_entry); } DBG_LASTOPR_STRING(USBK_LO_PASS); + return USBK_LO_PASS; } -int usbk_list_get_counter() +int usbk_list_get_count(USBK_LIST *usbk_list) { - return usbk_disk_count; + return usbk_list->count; } #endif diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index 6e86982..6bd919e 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -184,7 +184,6 @@ struct USBK * default values are "" */ char **key_names; - /* FIXME: remove this double-linked list structure */ struct USBK *next; }; typedef struct USBK USBK; @@ -310,17 +309,26 @@ inline int usbk_set_keyname(USBK* usbk, const char* pass, uint8_t key_no, const #if defined(__linux__) -extern int get_usbk_count(); +struct USBK_LIST +{ + USBK* usbk_head; + + int count; + int lastopr; +}; +typedef struct USBK_LIST USBK_LIST; + +extern int usbk_list_get_count(USBK_LIST *usbk_list); -extern USBK* usbk_list_new(); -extern int usbk_list_release(); -extern USBK* usbk_list_get_next(USBK* usbk); -extern int usbk_list_get_counter(); +extern USBK_LIST* usbk_list_new(void); +extern int usbk_list_release(USBK_LIST *usbk_list); +extern USBK* usbk_list_get_next(USBK *usbk); +extern int usbk_list_get_count(USBK_LIST *usbk_list); extern int usbk_list_refreshall(); -#define usbk_list_entry_foreach(list_entry, first_entry) \ - for (list_entry = first_entry; \ +#define usbk_list_entry_foreach(list_entry, usbk_list) \ + for (list_entry = usbk_list->usbk_head; \ list_entry != NULL; \ list_entry = usbk_list_get_next(list_entry)) diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index ad33aaf..489585f 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -541,17 +541,17 @@ void linuxcli_show_devices(void) { int counter; - USBK* _usbk_head = usbk_list_new(); + USBK_LIST* _usbk_list = usbk_list_new(); USBK* _usbk_entry; - usbk_list_entry_foreach (_usbk_entry, _usbk_head) { + usbk_list_entry_foreach (_usbk_entry, _usbk_list) { print_device(_usbk_entry); } - counter = usbk_list_get_counter(); + counter = usbk_list_get_count(_usbk_list); printf("%d USBK(s) found", counter); - usbk_list_release(); + usbk_list_release(_usbk_list); } void linuxcli_show_dev_info(USBK* myusbk) { From 6603e6e3a272001393ca930aa35905ef2ca9c642 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Sat, 22 Oct 2011 18:11:42 +0300 Subject: [PATCH 12/16] libusbk++ binding skeleton - add libusbk++ skeleton - add config.h to libusbk - some refactors to libusbk for c++ bindings --- .gitignore | 2 + CMakeLists.txt | 7 +- libusbk++/CMakeLists.txt | 167 ++++ libusbk++/src/config.hpp.cmake | 14 + libusbk++/src/libusbk.cpp | 1396 ++++++++++++++++++++++++++++++++ libusbk++/src/libusbk.hpp | 77 ++ libusbk/CMakeLists.txt | 74 +- libusbk/src/config.h.cmake | 14 + libusbk/src/libusbk.c | 7 +- libusbk/src/libusbk.h | 3 + 10 files changed, 1717 insertions(+), 44 deletions(-) create mode 100644 libusbk++/CMakeLists.txt create mode 100644 libusbk++/src/config.hpp.cmake create mode 100644 libusbk++/src/libusbk.cpp create mode 100644 libusbk++/src/libusbk.hpp create mode 100644 libusbk/src/config.h.cmake diff --git a/.gitignore b/.gitignore index 87345f5..e97a43b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,8 @@ CPackSourceConfig.cmake *.o *.so* usbk_cli/usbk +config.h +config.hpp # vim swap files *.swp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8883ea7..c998778 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ PROJECT (usbk) # CMAKE Settings -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(CMAKE_COLOR_MAKEFILE ON) SET(CMAKE_VERBOSE_MAKEFILE OFF) @@ -24,6 +24,7 @@ SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules;${CMAKE_MODULE_P INCLUDE (Uninstaller) ADD_SUBDIRECTORY( libusbk ) +ADD_SUBDIRECTORY( libusbk++ ) ADD_SUBDIRECTORY( usbk_cli ) ADD_SUBDIRECTORY( man ) @@ -65,7 +66,9 @@ IF (UNIX) */Makefile */CMakeFiles */cmake_install.cmake - libusbk/*.so* + libusbk*/*.so* + libusbk*/src/config.h + libusbk*/src/config.hpp usbk_cli/usbk man/*.gz ) diff --git a/libusbk++/CMakeLists.txt b/libusbk++/CMakeLists.txt new file mode 100644 index 0000000..a5cacdd --- /dev/null +++ b/libusbk++/CMakeLists.txt @@ -0,0 +1,167 @@ +# +# Build file of USBK CryptoBridge +# +# Tamara Electronics (C) 2010 +# +# GNU General Public License +# + +IF(UNIX) + message(STATUS "Cmake for Unix") +ENDIF(UNIX) + +IF(WIN32) + message(STATUS "Cmake for Windows") +ENDIF(WIN32) + +IF(APPLE) + message(FATAL_ERROR "APPLE NOT SUPPORTED") +ENDIF(APPLE) + +# Project name +PROJECT (USBK_LIB_CPP_BINDING) + +# CMAKE Settings +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +SET(CMAKE_COLOR_MAKEFILE ON) +SET(CMAKE_VERBOSE_MAKEFILE OFF) + +# The libUSBK version number. +SET(${PROJECT_NAME}_VERSION_MAJOR 0) +SET(${PROJECT_NAME}_VERSION_MINOR 0) +SET(${PROJECT_NAME}_VERSION_PATCH 1) + +SET(${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}") +SET(${PROJECT_NAME}_VERSION_FULL "${${PROJECT_NAME}_VERSION}.${${PROJECT_NAME}_VERSION_PATCH}") +SET(${PROJECT_NAME}_SOVERSION "${${PROJECT_NAME}_VERSION_FULL}") + +CONFIGURE_FILE(src/config.hpp.cmake src/config.hpp) + +IF(UNIX AND NOT APPLE) + # Header files are located in these directories + SET (${PROJECT_NAME}_INCLUDE_DIRS + ${${PROJECT_NAME}_SOURCE_DIR}/../libusbk/src + ${${PROJECT_NAME}_SOURCE_DIR}/src/ + ${CMAKE_SYSTEM_INCLUDE_PATH} + ) + INCLUDE_DIRECTORIES (${${PROJECT_NAME}_INCLUDE_DIRS}) + + # Libraries are located in these directories + SET (${PROJECT_NAME}_LIBRARY_DIRS + ${CMAKE_CURRENT_BINARY_DIR}/../libusbk/ + ${CMAKE_SYSTEM_LIBRARY_PATH} + ) + LINK_DIRECTORIES ( ${${PROJECT_NAME}_LIBRARY_DIRS}) + + # Depend Includes + SET (${PROJECT_NAME}_INCLUDES + libusbk.h + libusbk.hpp + ) + + # Libraries are located in these directories + SET (${PROJECT_NAME}_LIBRARIES_PATH + ${CMAKE_SYSTEM_LIBRARY_PATH} + ) + LINK_DIRECTORIES (${${PROJECT_NAME}_LIBRARIES_PATH}) + + # Source Files + #AUX_SOURCE_DIRECTORY (src ${PROJECT_NAME}_SOURCES) + + FILE(GLOB SRC_FILES "src/*" ) + + # Create Library File + ADD_LIBRARY( ${PROJECT_NAME} SHARED ${SRC_FILES}) + + # These are the symbols for the linker + TARGET_LINK_LIBRARIES(${PROJECT_NAME} udev) + + # change lib_target properties + SET_TARGET_PROPERTIES( ${PROJECT_NAME} PROPERTIES + # create *nix style library versions + symbolic links + VERSION ${${PROJECT_NAME}_VERSION_FULL} + SOVERSION ${${PROJECT_NAME}_SOVERSION} + + # allow creating static and shared libs without conflicts + CLEAN_DIRECT_OUTPUT 1 + + # avoid conflicts between library and binary target names + OUTPUT_NAME libusbk++ + ) + + # Install this library + INSTALL(TARGETS ${PROJECT_NAME} + LIBRARY DESTINATION /usr/lib + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + + INSTALL(FILES src/libusbk.h + DESTINATION /usr/include + PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) + + IF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) + + # Settings for debian package + SET(CPACK_GENERATOR "DEB") + SET(CPACK_DEBIAN_PACKAGE_NAME ${PROJECT_NAME}) + SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Tamara Elektronik ") + SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "USBK CryptoBridge Library") + SET(CPACK_DEBIAN_PACKAGE_DESCRIPTION " USBK CryptoBridge Library\n USBK CryptoBridge encrypts data independent by the operation systems and all filesystems.\n USBK works in SCSI level and encrypts/decrypts data on the fly. USBK has two model as A101\n and A103. A103 model uses 3 keys and A101 model uses 1 key. A password for this operation.\n Whenever user inserts USB-K into USB Hub of the PC, USB-K must be initialized (there is an\n exceptional situation!).") + SET(CPACK_PACKAGE_VERSION "${${PROJECT_NAME}_VERSION}") + SET(CPACK_DEBIAN_PACKAGE_VERSION "${${PROJECT_NAME}_VERSION}") + #SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "i386") + SET(CPACK_DEBIAN_PACKAGE_DEPENDS "udev (>= 151-12.3)") + SET(CPACK_DEBIAN_PACKAGE_SECTION "devel") + SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") + SET(CPACK_DEBIAN_PACKAGE_RECOMMENDS "recommends") + SET(CPACK_DEBIAN_PACKAGE_SUGGESTS "suggests") + INCLUDE(CPack) + + ENDIF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) + +ELSEIF(WIN32) + + # Header files are located in these directories + SET (${PROJECT_NAME}_INCLUDE_DIRS + ${${PROJECT_NAME}_SOURCE_DIR}/src/ + ${${PROJECT_NAME}_SOURCE_DIR}/src/windows/ + ${CMAKE_SYSTEM_INCLUDE_PATH} + ) + INCLUDE_DIRECTORIES (${${PROJECT_NAME}_INCLUDE_DIRS}) + + # Libraries are located in these directories + SET (${PROJECT_NAME}_LIBRARIES_PATH + ${CMAKE_SYSTEM_LIBRARY_PATH} + ) + LINK_DIRECTORIES (${${PROJECT_NAME}_LIBRARIES_PATH}) + + FILE(GLOB SRC_FILES "src/*" "src/windows/*") + + ADD_LIBRARY( ${PROJECT_NAME} SHARED ${SRC_FILES}) + + SET_TARGET_PROPERTIES( ${PROJECT_NAME} PROPERTIES + VERSION ${${PROJECT_NAME}_VERSION} + SOVERSION ${${PROJECT_NAME}_SOVERSION} + + # allow creating static and shared libs without conflicts + CLEAN_DIRECT_OUTPUT 1 + + # avoid conflicts between library and binary target names + OUTPUT_NAME libusbk++ + ) + + INSTALL(TARGETS + ${PROJECT_NAME} + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE + ) + + INCLUDE(CPack) + +ELSEIF(APPLE) + +ELSE(UNIX AND NOT APPLE) + message(FATAL_ERROR "Undefined Operating System") +ENDIF(UNIX AND NOT APPLE) + diff --git a/libusbk++/src/config.hpp.cmake b/libusbk++/src/config.hpp.cmake new file mode 100644 index 0000000..551a826 --- /dev/null +++ b/libusbk++/src/config.hpp.cmake @@ -0,0 +1,14 @@ +/** + * + * @project @PROJECT_NAME@ + * @file config.h + * + */ + +#ifndef CONFIG_HPP_ +#define CONFIG_HPP_ + +#define USBK_CPP_LIB_VERSION_FULL "@USBK_LIB_CPP_BINDING_VERSION_FULL@" + +#endif // CONFIG_HPP_ + diff --git a/libusbk++/src/libusbk.cpp b/libusbk++/src/libusbk.cpp new file mode 100644 index 0000000..ac53984 --- /dev/null +++ b/libusbk++/src/libusbk.cpp @@ -0,0 +1,1396 @@ +/* + * @file libusbk.cpp + * + * Copyright (C) 2010 USB-K Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * See http://www.gnu.org/licenses/ for more information + * + **************************************************************************** + * This is a utility program for listing SCSI devices and hosts (HBAs) + * in the Linux operating system. It is applicable to kernel versions + * 2.6.1 and greater. + * + */ + +#include "libusbk.hpp" + +#if 0 +#if defined(__linux__) +# include +# include +# include +#endif + +#include +#include +#include +#include + +#include "uip.h" + +#include "libusbk.h" +#include "usbk_scsi.h" + +#if defined(__linux__) + #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "\033[0;32;40m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} + #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "\033[1;3;31m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} + #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} + #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ + else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} +#elif defined(WIN32) + #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} + #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} + #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} + #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ + else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} +#endif + +//-DEFINES FOR FINDING USBK AND BACKDISK +#define USBK_USB_IDVENDOR "2384" +#define USBK_USB_IDPRODUCT_A103 "a103" +#define USBK_USB_IDPRODUCT_A101 "a101" +#define USBK_SCSI_VENDOR "USBK" +#define USBK_SCSI_BACKDISK_VENDOR "BackDisk" + +#define USBK_CHECK_SUPPORTED(usbk) \ + if (!usbk->is_supported) { \ + usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; \ + DBG_LASTOPR_STRING(usbk->lastopr); \ + return - usbk->lastopr; \ + } + +typedef struct __RTRN_STRING { + int rtrn; + const char *rtrn_string; +} RTRN_STRING; + +RTRN_STRING lastopr_string[] = { + {USBK_LO_PASS, "Ok" }, + {USBK_LO_GEN_FAIL, "fail" }, + {USBK_LO_FAILED_PASS, "wrong password" }, + {USBK_LO_FABRIC_RESET, "Error, FABRIC_RESET" }, + {USBK_LO_USBK_UNPLUGING, "n/a" }, + {USBK_LO_INVALID_KEYNO, "invalid key number" }, + {USBK_LO_INVALID_KEYSIZE, "invalid key size" }, + {USBK_LO_INVALID_DEVICELABEL, "invalid device label" }, + {USBK_LO_INVALID_PASS, "n/a" }, + {USBK_LO_INVALID_NEWPASS, "invalid new password" }, + {USBK_LO_STATE_ERROR, "state error" }, + {USBK_LO_SCSI_ERROR, "scsi error" }, + {USBK_LO_UNSUPPORTED_USBK, "unsupported usbk" }, + {USBK_LO_INVALID_KEY, "invalid key" }, +#if defined(__linux__) + {USBK_LO_UDEV_ERROR, "udev error" }, +#endif + {USBK_LO_MEM_ERROR, "memory allocation error"} +}; + +typedef struct { + const char* model; + bool support; +} LIBUSBK_SUPPORTED_MODELS; + +LIBUSBK_SUPPORTED_MODELS models_1_1[] = { + { "A101", true }, + { "A103", true }, + { NULL, false } +}; + +typedef struct { + int major_version; + int minor_version; + LIBUSBK_SUPPORTED_MODELS* models; +} LIBUSBK_SUPPORTED_VERSIONS; + +LIBUSBK_SUPPORTED_VERSIONS versions_1[] = { + { 2, 5, models_1_1 }, + { 0, 0, NULL } +}; + +typedef struct { + const char* product; + LIBUSBK_SUPPORTED_VERSIONS* versions; +} LIBUSBK_SUPPORTED_PRODUCTS; + +LIBUSBK_SUPPORTED_PRODUCTS products[] = { + { "USBK CryptoBridge 2.0", versions_1 }, + { "USBK CryptoBridge", versions_1 }, + { NULL, NULL } +}; + +static bool debug_enable = false; + +static int _get_udev_info(USBK* usbk, const char *device); +static int _get_udev_backdisk(USBK* usbk); +static int _command_status(USBK* usbk); + +static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize); + +static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize); +static unsigned int _keysize_as_byte(usbk_keysize_t keysize); +#endif + +usbk_lo_t UsbkDevice::lastOperation() const +{ + usbk_lo_t ret = 0; + + return ret; +} + +bool UsbkDevice::supported() const +{ + bool ret = true; + + return ret; +} + +std::string UsbkDevice::deviceNode() const +{ + std::string ret; + + return ret; +} + +std::string UsbkDevice::deviceNodePath() const +{ + std::string ret; + + return ret; +} + +std::string UsbkDevice::backdiskNode() const +{ + std::string ret; + + return ret; + +} + +std::string UsbkDevice::backdiskNodePath() const +{ + std::string ret; + + return ret; + +} + +std::string UsbkDevice::product() const +{ + + std::string ret; + + return ret; +} + +std::string UsbkDevice::model() const +{ + + std::string ret; + + return ret; +} + +std::string UsbkDevice::serial() const +{ + + std::string ret; + + return ret; +} + +std::string UsbkDevice::usbSerial() const +{ + + std::string ret; + + return ret; +} + +std::string UsbkDevice::firmwareVersion() const +{ + std::string ret; + + return ret; + +} + +std::string UsbkDevice::deviceLabel() const +{ + std::string ret; + + return ret; + +} + +uint8_t UsbkDevice::multikeyCapability() const +{ + uint8_t ret; + + return ret; +} + +usbk_ds_t UsbkDevice::deviceState() const +{ + usbk_ds_t ret = USBK_DS_ACTIVATE_WITH_BACKDISK; + + return ret; +} + +int UsbkDevice::currentKey() const +{ + int ret; + + return ret; +} + +int UsbkDevice::autoactivateKeyNo() const +{ + int ret; + + return ret; +} + +std::vector UsbkDevice::keyNames() const +{ + std::vector ret; + + return ret; +} + +bool UsbkDevice::activateKey(int /*keyno*/) +{ + bool ret; + + return ret; +} + +bool UsbkDevice::deactivate() +{ + bool ret; + + return ret; + +} + +bool UsbkDevice::changePassword(const std::string &old_password, const std::string &new_password) +{ + bool ret; + + return ret; + + +} + +bool UsbkDevice::setDeviceLabel(const std::string &password, const std::string &label) +{ + bool ret; + + return ret; + +} + +bool UsbkDevice::setKeyname(const std::string &password, int keyno, const std::string &keyname) +{ + + bool ret; + + return ret; +} + +bool UsbkDevice::setKey(const std::string &password, int keyno, usbk_keysize_t keysize, const std::string &key) +{ + bool ret; + + return ret; + +} + +bool UsbkDevice::setAutoactivateKeyno(const std::string &password, int keyno) +{ + + bool ret; + + return ret; +} + +std::string UsbkDevice::getRandomKey(usbk_keysize_t keysize) const +{ + std::string ret; + + return ret; +} + +bool UsbkDevice::refreshUsbkInfo() +{ + + bool ret; + + return ret; +} + +bool UsbkDevice::isDebugEnabled() +{ + + bool ret; + + return ret; +} + +void UsbkDevice::setDebug(bool enabled) +{ + +} + +std::string UsbkDevice::libusbkVersion() +{ + std::string ret; + + return ret; +} + +#if 0 +USBK* usbk_new(const char* dev) +{ + int ret; + int i; + + USBK* usbk = (USBK*) calloc(1, sizeof(USBK)); + if (usbk == NULL) { + free(usbk); + DBG_LASTOPR_STRING(USBK_LO_UDEV_ERROR); + + return NULL; + } + +#if defined(__linux__) + // get information about usbk by udev + ret = _get_udev_info(usbk, dev); + if (ret < 0) { + free(usbk); + DBG_LASTOPR_STRING(USBK_LO_UDEV_ERROR); + + return NULL; + } +#elif defined(WIN32) + usbk_head->dev_node = strdup(dev); + usbk_head->dev_node_path = strdup(dev); +#endif + + t_UIP_DEVINFO usbk_info; + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &usbk_info, + USBK_GET_DEV_INFO, + sizeof(usbk_info), + USBK_READ_SCSI); + if (ret < 0) { + free(usbk); + DBG_LASTOPR_STRING(USBK_LO_SCSI_ERROR); + + return NULL; + } + + usbk->product = strdup(usbk_info.product.s); + usbk->model = strdup(usbk_info.model.s); + usbk->firmware_ver = strdup(usbk_info.firmware_ver.s); + usbk->is_supported = true; + + usbk->multikey_cap = usbk_info.multikeycap; + usbk->current_key = usbk_info.current_keyno; + usbk->autoact_keyno = usbk_info.autoactivate_keyno; + usbk->retry_num = usbk_info.retry_num; + usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; + usbk->dev_label = strdup(usbk_info.devlabel.s); + + /* FIXME why we need this much space */ + usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); + for (i = 0; i < UIP_SERIAL_SIZE; i++) { + sprintf((usbk->serial + i * 2), "%2.2X", usbk_info.serial.u8[i]); + } + + usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); + for (i = 0; i < usbk->multikey_cap; i++) { + usbk->key_names[i] = strdup(usbk_info.keyname[i].s); + } + + usbk->lastopr = USBK_LO_PASS; + DBG_LASTOPR_STRING(usbk->lastopr); + + return usbk; +} + +int usbk_release(USBK* usbk) +{ + int i; + + free(usbk->dev_node); + free(usbk->dev_node_path); + free(usbk->backdisk_node); + free(usbk->backdisk_node_path); + free(usbk->product); + free(usbk->model); + free(usbk->serial); + free(usbk->usb_serial); + free(usbk->firmware_ver); + free(usbk->dev_label); + + for (i = 0; i < usbk->multikey_cap; i++) { + free(usbk->key_names[i]); + } + + free(usbk->key_names); + free(usbk); + + return 0; +} + +int usbk_key_activate(USBK* usbk, const char* password, uint8_t key_no) +{ + int ret; + t_UIP_ACTIVATE activate; + + USBK_CHECK_SUPPORTED(usbk) + + if (key_no > usbk->multikey_cap) { + usbk->lastopr = USBK_LO_INVALID_KEYNO; + ret = - usbk->lastopr; + + goto done; + } + + switch (usbk->dev_state) { + case USBK_DS_DEACTIVATE: + memset(&activate, 0, sizeof(activate)); + + /* FIXME: check password size */ + strcpy(activate.password.s, password); + activate.keyno = key_no; + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char *) &activate, + USBK_ACTIVATE_KEY, + sizeof(activate), + USBK_WRITE_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + usbk->lastopr = _command_status(usbk); + + break; + + case USBK_DS_ACTIVATE: + case USBK_DS_ACTIVATE_WITH_BACKDISK: + case USBK_DS_FABRIC_DEFAULT: + case USBK_DS_MUST_REMOVE: + default: + usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; + + goto done; + } + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_key_deactivate(USBK* usbk) +{ + int ret; + + USBK_CHECK_SUPPORTED(usbk) + + switch (usbk->dev_state) { + case USBK_DS_ACTIVATE: + case USBK_DS_ACTIVATE_WITH_BACKDISK: + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) NULL, + USBK_DEACTIVATE_KEY, + 0, + USBK_WRITE_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + usbk->lastopr = _command_status(usbk); + + break; + + case USBK_DS_DEACTIVATE: + case USBK_DS_FABRIC_DEFAULT: + case USBK_DS_MUST_REMOVE: + default: + usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; + + goto done; + } + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass) +{ + int ret; + t_UIP_CHPASS chpass; + + USBK_CHECK_SUPPORTED(usbk) + + switch (usbk->dev_state) { + case USBK_DS_DEACTIVATE: + case USBK_DS_FABRIC_DEFAULT: + memset(&chpass, 0, sizeof(chpass)); + if (new_pass != NULL) { + strncpy(chpass.new_password.s, new_pass, sizeof(chpass.new_password.s)); + } + if (old_pass != NULL) { + strncpy(chpass.old_password.s, old_pass, sizeof(chpass.old_password.s)); + } + + DBG_INFO("old : %s",chpass.old_password.s); + DBG_INFO("new : %s",chpass.new_password.s); + + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &chpass, USBK_CHANGE_PASS, sizeof(chpass), USBK_WRITE_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + usbk->lastopr = _command_status(usbk); + + break; + + case USBK_DS_ACTIVATE: + case USBK_DS_ACTIVATE_WITH_BACKDISK: + case USBK_DS_MUST_REMOVE: + default: + usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; + + goto done; + } + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) +{ + int ret; + t_UIP_SETDEVICELABEL devlabel; + + USBK_CHECK_SUPPORTED(usbk) + + switch (usbk->dev_state) { + case USBK_DS_DEACTIVATE: + memset(&devlabel, 0, sizeof(devlabel)); + strncpy(devlabel.password.s, pass, sizeof(devlabel.password.s)); + strncpy(devlabel.devlabel.s, device_label, sizeof(devlabel.devlabel.s)); + ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &devlabel, USBK_SET_DEV_NAME, sizeof(devlabel), USBK_WRITE_SCSI); + + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + usbk->lastopr = _command_status(usbk); + + break; + case USBK_DS_ACTIVATE: + case USBK_DS_ACTIVATE_WITH_BACKDISK: + case USBK_DS_FABRIC_DEFAULT: + case USBK_DS_MUST_REMOVE: + default: + usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; + + goto done; + } + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key) +{ + int ret; + t_UIP_SETKEY setkey; + + USBK_CHECK_SUPPORTED(usbk) + + if (key_no > usbk->multikey_cap) { + usbk->lastopr = USBK_LO_INVALID_KEYNO; + ret = - usbk->lastopr; + + goto done; + } + + switch (usbk->dev_state) { + case USBK_DS_DEACTIVATE: + memset(&setkey, 0, sizeof(setkey)); + strncpy(setkey.password.s, pass, sizeof(setkey.password.s)); + setkey.keyno = key_no; + + if (key_name != NULL) { + strncpy(setkey.keyname.s, key_name, sizeof(setkey.keyname.s)); + } else { + /* preserve old keyname if keyname is not changed */ + strncpy(setkey.keyname.s, usbk->key_names[key_no - 1], sizeof(setkey.keyname.s)); + } + + if ((key_size != USBK_KEYSIZE_NULL) && (key != NULL)) { + setkey.options.me = SETKEY_NAME_AND_KEY; + setkey.keysize.me = (e_UIP_KEYSIZE) key_size; + memcpy(setkey.key.u8, key, _keysize_as_byte(key_size)); + } else { + setkey.options.me = SETKEY_NAMEONLY; + } + + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &setkey, + USBK_SET_KEY, + sizeof(setkey), + USBK_WRITE_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + usbk->lastopr = _command_status(usbk); + + break; + case USBK_DS_ACTIVATE: + case USBK_DS_ACTIVATE_WITH_BACKDISK: + case USBK_DS_FABRIC_DEFAULT: + case USBK_DS_MUST_REMOVE: + default: + usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; + + goto done; + } + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_set_key_decimal(USBK* usbk_head, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) +{ + int ret = 0; + uint8_t key_hex[UIP_KEYSIZE_TOTALSIZE]; + + USBK_CHECK_SUPPORTED(usbk_head) + + usbk_head->lastopr = _convert_key_decimal2hex(key_hex, key, key_size); + if (ret == USBK_LO_PASS){ + usbk_head->lastopr = usbk_set_key_and_keyname(usbk_head, pass, key_no, NULL, key_size, key_hex); + } + + DBG_LASTOPR_STRING(usbk_head->lastopr); + + return usbk_head->lastopr; +} + +int usbk_set_key_hex(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key) +{ + USBK_CHECK_SUPPORTED(usbk) + + return usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key); +} + +int usbk_set_key_text(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) +{ + int ret; + uint8_t key_hex[32]; /* use macro definition instead of 32 */ + + USBK_CHECK_SUPPORTED(usbk) + + ret = _convert_key_text2hex(key_hex, key, key_size); + if (ret == USBK_LO_PASS){ + ret = usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key_hex); + } + + usbk->lastopr = ret; + + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_set_autact(USBK* usbk, const char *pass, int key_no) +{ + int ret; + t_UIP_SETAUTOACTIVATE autoact; + + USBK_CHECK_SUPPORTED(usbk) + + if (key_no > usbk->multikey_cap) { + usbk->lastopr = USBK_LO_INVALID_KEYNO; + ret = - usbk->lastopr; + + goto done; + } + + switch (usbk->dev_state) { + case USBK_DS_DEACTIVATE: + memset(&autoact, 0, sizeof(autoact)); + strncpy(autoact.password.s, pass, sizeof(autoact.password.s)); + autoact.keyno = key_no; + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &autoact, + USBK_SET_AUTO_ACTIVE, + sizeof(autoact), + USBK_WRITE_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + usbk->lastopr = _command_status(usbk); + + break; + case USBK_DS_ACTIVATE: + case USBK_DS_ACTIVATE_WITH_BACKDISK: + case USBK_DS_FABRIC_DEFAULT: + case USBK_DS_MUST_REMOVE: + default: + usbk->lastopr = USBK_LO_STATE_ERROR; + ret = - usbk->lastopr; + + goto done; + } + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) +{ + int ret; + t_UIP_GENERATEKEY genkey; + + USBK_CHECK_SUPPORTED(usbk) + + unsigned int keysize_inbyte = _keysize_as_byte(keysize); + if ((keysize_inbyte == 0) || (keysize_inbyte > _keysize_as_byte(USBK_KEYSIZE_256BIT))) { + usbk->lastopr = USBK_LO_INVALID_KEYSIZE; + ret = - usbk->lastopr; + + goto done; + } + + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &genkey, + USBK_GENERATE_KEY, + sizeof(genkey), + USBK_READ_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + + usbk->lastopr = _command_status(usbk); + if (usbk->lastopr == USBK_LO_PASS) { + memcpy(random_key, genkey.key.u8, keysize_inbyte); + } + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +int usbk_refresh_usbkinfo(USBK* usbk) +{ + int i; + int ret; + + USBK_CHECK_SUPPORTED(usbk) + + t_UIP_DEVINFO usbk_info; + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &usbk_info, + USBK_GET_DEV_INFO, + sizeof(usbk_info), + USBK_READ_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + ret = - usbk->lastopr; + + goto done; + } + + usbk->current_key = usbk_info.current_keyno; + usbk->autoact_keyno = usbk_info.autoactivate_keyno; + usbk->retry_num = usbk_info.retry_num; + usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; + usbk->dev_label = strdup(usbk_info.devlabel.s); + + usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); + for (i = 0; i < usbk->multikey_cap; i++) { + usbk->key_names[i] = strdup(usbk_info.keyname[i].s); + } + usbk->lastopr = USBK_LO_PASS; + +done: + DBG_LASTOPR_STRING(usbk->lastopr); + + return ret; +} + +static int _command_status(USBK* usbk) +{ + int ret; + t_UIP_GETSTATUS status; + + ret = send_scsi_command ( + usbk->dev_node_path, + (unsigned char*) &status, + USBK_GET_STATUS, + sizeof(t_UIP_GETSTATUS), + USBK_READ_SCSI); + if (ret < 0) { + usbk->lastopr = USBK_LO_SCSI_ERROR; + DBG_LASTOPR_STRING(usbk->lastopr); + + return - usbk->lastopr; + } + + usbk->retry_num = status.retry_num; + + /* FIXME a new review required!! */ + return (status.lastop.me - 1); //todo versiyon checking'ine göre düzleltilecek. +} + +#if 0 +static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize) +{ + // 1) string icinde bosluk karakteri var mi? + // 2) string icerisindeki '.' karakterinin sayisi 15 mi? + // 3) '.' karakterlerini ' ' karakterine donustur + // 4) string icerisinde bosluklar haric decimal olmayan karakter var mi? + // 5) string to integer islemini yap + // 6) 16 adet sayı cikartabildin mi? + // 7) sayılar 0 ile 255 arasinda mi? + + unsigned int key_len = _keysize_as_byte(keysize); + int i; + + /* check key_text if it is null or not */ + if (key_decimal == NULL || key_hex == NULL) + return USBK_LO_INVALID_KEY; + + /* check key length if it is proper or not */ + if(strlen(key_hex) > key_len) { + /* FIXME USBK_LO_INVALID_KEYSIZE? */ + return USBK_LO_INVALID_KEY; + } + + for(i=0; i_keysize_as_byte(keysize)]; + int ikey[50]; + size_t found; + string::iterator it; + unsigned int i; + string key_decimal_str = key_decimal; + + // string icinde bosluk karakteri var mi? + if (key_decimal_str.find(" ") != string::npos) return USBK_LO_INVALID_KEY; + + // string icerisindeki '.' karakterinin sayisi 15 mi? + // '.' karakterlerini ' ' karakterine donustur + i = 0; + found = key_decimal_str.find_first_of("."); + while (found != string::npos) { + i++; + key_decimal_str[found] = ' '; + found = key_decimal_str.find_first_of(".", found + 1); + } + if (i != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; + //cout << str << endl; + + // string icerisinde bosluklar haric decimal olmayan karakter var mi? + i = 0; + for (it = key_decimal_str.begin(); it < key_decimal_str.end(); it++) { + if (*it != ' ') { + if (isdigit(*it) == false) { + i++; + } + } + } + if (i != 0) { + return USBK_LO_INVALID_KEY; + } + + // string to integer islemini yap + // sayılar 0 ile 255 arasinda mi? + istringstream iss(key_decimal_str, istringstream::in); + i = 0; + for (unsigned int n = 0; n < _keysize_as_byte(keysize); n++) { + iss >> ikey[n]; + + if (iss.fail() || iss.bad()) return USBK_LO_INVALID_KEY; + + if (iss.eof() && n != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; + + if (ikey[n] > 255 || ikey[n] < 0) { + i++; + } + key_hex[n] = ikey[n]; + //cout << n+1 << "." << ikey[n] << endl; + } + if (i != 0) { + return USBK_LO_INVALID_KEY; + } + + return USBK_LO_PASS; +} +#endif + +static inline int16_t _xtod(const char c) +{ + if (c>='0' && c<='9') return c-'0'; + if (c>='A' && c<='F') return c-'A'+10; + if (c>='a' && c<='f') return c-'a'+10; + + return -1; +} + +static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize) +{ + unsigned int key_len = _keysize_as_byte(keysize); + int i; + + /* check key_text if it is null or not */ + if (key_text == NULL || key_hex == NULL) + return USBK_LO_INVALID_KEY; + + /* check key length if it is proper or not */ + if(strlen(key_hex) > key_len) { + /* FIXME USBK_LO_INVALID_KEYSIZE? */ + return USBK_LO_INVALID_KEY; + } + + for(i=0; ilastopr = USBK_LO_UDEV_ERROR; + + _udev = udev_new(); + if (!_udev) goto done; + + _udev_dev_path = udev_get_dev_path(_udev); + + /* allocate ram space from stack with alloca */ + _udev_devname = alloca(strlen(_udev_dev_path) + strlen(device) + 1); + assert(_udev_devname != NULL); + sprintf(_udev_devname, "%s/%s", udev_get_dev_path(_udev), device); + + if (stat(_udev_devname, &statbuf) < 0) goto done; + + if (S_ISBLK(statbuf.st_mode)) { + dev_type = 'b'; + } + else if (S_ISCHR(statbuf.st_mode)) { + /* FIXME char devtype is legal? */ + dev_type = 'c'; + } else { + goto done; + } + + _udev_device = udev_device_new_from_devnum(_udev, dev_type, statbuf.st_rdev); + + if (!_udev_device) goto done; + + _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); + if (!_udev_device_usb) goto done; + + _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); + + ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); + if (ret) goto done; + + _udev_device_idproduct = udev_device_get_sysattr_value(_udev_device_usb, "idProduct"); + + if (!strncmp(USBK_USB_IDPRODUCT_A101, _udev_device_idproduct, strlen(_udev_device_idproduct)) && + !strncmp(USBK_USB_IDPRODUCT_A103, _udev_device_idproduct, strlen(_udev_device_idproduct))) + goto done; + + _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); + if (!_udev_device_scsi) goto done; + + ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); + if (ret) goto done; + + usbk->dev_node = strdup(udev_device_get_sysname(_udev_device)); + usbk->dev_node_path = strdup(udev_device_get_devnode(_udev_device)); + usbk->usb_serial = strdup(udev_device_get_sysattr_value(_udev_device_usb, "serial")); + + /* another usbk_lo_** required for backdisk error */ + ret = _get_udev_backdisk(usbk); + if (ret < 0) goto done; + + usbk->lastopr = USBK_LO_PASS; + +done: + udev_device_unref(_udev_device); + udev_enumerate_unref(_udev_enumerate); + udev_unref(_udev); + + DBG_LASTOPR_STRING(usbk->lastopr); + + return usbk->lastopr; +} + +static int _get_udev_backdisk(USBK* usbk) +{ + int ret; + + struct udev *_udev = NULL; + struct udev_enumerate *_udev_enumerate = NULL; + struct udev_list_entry *_udev_enumerated_devices = NULL; + struct udev_list_entry *_udev_enumerated_device = NULL; + + struct udev_device *_udev_device = NULL; + struct udev_device *_udev_device_scsi = NULL; + struct udev_device *_udev_device_usb = NULL; + + const char *_udev_enumerated_device_path; + const char *_udev_device_idvendor; + const char *_udev_device_idproduct; + const char *_udev_device_usb_serial; + + usbk->lastopr = USBK_LO_UDEV_ERROR; + + // Create the _udev object + _udev = udev_new(); + + if (!_udev) goto done; + + /* get udev enumerated devices */ + _udev_enumerate = udev_enumerate_new(_udev); + + /* add block devices */ + ret = udev_enumerate_add_match_subsystem(_udev_enumerate, "block"); + if (ret) goto done; + + /* add scan for devices */ + ret = udev_enumerate_scan_devices(_udev_enumerate); + if (ret) goto done; + + /* get device list */ + _udev_enumerated_devices = udev_enumerate_get_list_entry(_udev_enumerate); + + udev_list_entry_foreach (_udev_enumerated_device, _udev_enumerated_devices) { + _udev_enumerated_device_path = udev_list_entry_get_name(_udev_enumerated_device); + _udev_device = udev_device_new_from_syspath(_udev, _udev_enumerated_device_path); + + /* get usb devices */ + _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); + if (!_udev_device_usb) continue; + + _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); + + ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); + if (ret) continue; + + _udev_device_idproduct = udev_device_get_sysattr_value(_udev_device_usb, "idProduct"); + + /* FIXME: code enhancement required */ + if (!strncmp(USBK_USB_IDPRODUCT_A101, _udev_device_idproduct, strlen(_udev_device_idproduct)) && + !strncmp(USBK_USB_IDPRODUCT_A103, _udev_device_idproduct, strlen(_udev_device_idproduct))) + continue; + + _udev_device_usb_serial = udev_device_get_sysattr_value(_udev_device_usb, "serial"); + + ret = strcmp(usbk->usb_serial, _udev_device_usb_serial); + if (ret) continue; + + _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); + if (!_udev_device_scsi) continue; + + ret = strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)); + if (ret) continue; + + usbk->backdisk_node = strdup(udev_device_get_sysname(_udev_device)); + usbk->backdisk_node_path = strdup(udev_device_get_devnode(_udev_device)); + + usbk->lastopr = USBK_LO_PASS; + + /* no need to proceed loop */ + break; + } + +done: + udev_device_unref(_udev_device); + udev_enumerate_unref(_udev_enumerate); + udev_unref(_udev); + + DBG_LASTOPR_STRING(usbk->lastopr); + + return usbk->lastopr; +} + +USBK_LIST* usbk_list_new(void) +{ + struct udev *_udev = NULL; + struct udev_enumerate *_udev_enumerate = NULL; + struct udev_list_entry *_udev_enumerated_devices = NULL; + struct udev_list_entry *_udev_enumerated_device = NULL; + + struct udev_device *_udev_device = NULL; + struct udev_device *_udev_device_scsi = NULL; + struct udev_device *_udev_device_usb = NULL; + + const char *_udev_enumerated_device_path; + const char *_udev_device_idvendor; + + int ret; + USBK_LIST *usbk_list; + + USBK *usbk_last_entry = NULL; + USBK *usbk_new_entry = NULL; + + /* initialize udev */ + _udev = udev_new(); + if (_udev == NULL) { + fprintf(stderr, "Error! Udev cannot be initialized!\n"); + + return NULL; + } + + /* get udev enumerated devices */ + _udev_enumerate = udev_enumerate_new(_udev); + + /* add block devices */ + ret = udev_enumerate_add_match_subsystem(_udev_enumerate, "block"); + if (ret) goto done; + + /* add scan for devices */ + ret = udev_enumerate_scan_devices(_udev_enumerate); + if (ret) goto done; + + /* get device list */ + _udev_enumerated_devices = udev_enumerate_get_list_entry(_udev_enumerate); + + usbk_list = (USBK_LIST *) calloc(1, sizeof(USBK_LIST)); + + if (!usbk_list) { + DBG_LASTOPR_STRING(USBK_LO_MEM_ERROR); + + goto done; + } + + udev_list_entry_foreach (_udev_enumerated_device, _udev_enumerated_devices) { + _udev_enumerated_device_path = udev_list_entry_get_name(_udev_enumerated_device); + _udev_device = udev_device_new_from_syspath(_udev, _udev_enumerated_device_path); + + /* get usb devices */ + _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); + if (!_udev_device_usb) continue; + + _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); + + ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); + if (ret) continue; + + _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); + if (!_udev_device_scsi) continue; + + ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); + if (ret) continue; + + usbk_new_entry = usbk_new(udev_device_get_sysname(_udev_device)); + if (!usbk_new_entry) { + DBG_LASTOPR_STRING(USBK_LO_MEM_ERROR); + + goto done; + + } + + if (!usbk_list->usbk_head) { + usbk_list->usbk_head = usbk_last_entry = usbk_new_entry; + } else { + usbk_last_entry->next = usbk_new_entry; + usbk_last_entry = usbk_new_entry; + } + + usbk_list->count++; + } + +done: + udev_device_unref(_udev_device); + udev_enumerate_unref(_udev_enumerate); + udev_unref(_udev); + + return usbk_list; +} + +int usbk_list_release(USBK_LIST *usbk_list) +{ + USBK *_usbk=usbk_list->usbk_head; + + while (_usbk->next) { + usbk_release(_usbk); + _usbk = _usbk->next; + usbk_list->count--; + } + + free(usbk_list); + + return 0; +} + +USBK* usbk_list_get_next(USBK* usbk) +{ + return usbk->next; +} + +int usbk_list_refreshall(USBK_LIST *usbk_list) +{ + USBK* list_entry; + + usbk_list_entry_foreach (list_entry, usbk_list) { + usbk_refresh_usbkinfo(list_entry); + } + + DBG_LASTOPR_STRING(USBK_LO_PASS); + + return USBK_LO_PASS; +} + +int usbk_list_get_count(USBK_LIST *usbk_list) +{ + return usbk_list->count; +} + +#endif + +void usbk_debug_enable(void) +{ + debug_enable = true; +} + +void usbk_debug_disable(void) +{ + debug_enable = false; +} + +void usbk_debug(bool enable) +{ + debug_enable = enable; +} + +bool usbk_debug_check(void) +{ + return debug_enable; +} + +const char* usbk_libversion(void) +{ + return VERSION; +} +#endif diff --git a/libusbk++/src/libusbk.hpp b/libusbk++/src/libusbk.hpp new file mode 100644 index 0000000..87d1556 --- /dev/null +++ b/libusbk++/src/libusbk.hpp @@ -0,0 +1,77 @@ +/* + * @file libusbk.hpp + * + * Copyright (C) 2010 USB-K Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * See http://www.gnu.org/licenses/ for more information + * + **************************************************************************** + * + * USBK c++ binding header + */ + +#ifndef LIBUSBK_HPP_ +#define LIBUSBK_HPP_ + +#include "config.h" +#include "../../libusbk/src/libusbk.h" + +#include +#include + +class UsbkDevice : public USBK +{ +public: + + /** + * All getter functions + * */ + + usbk_lo_t lastOperation() const; + bool supported() const; + std::string deviceNode() const; + std::string deviceNodePath() const; + std::string backdiskNode() const; + std::string backdiskNodePath() const; + std::string product() const; + std::string model() const; + std::string serial() const; + std::string usbSerial() const; + std::string firmwareVersion() const; + std::string deviceLabel() const; + uint8_t multikeyCapability() const; + usbk_ds_t deviceState() const; + int currentKey() const; + int autoactivateKeyNo() const; + std::vector keyNames() const; + + bool activateKey(int keyno); + bool deactivate(); + bool changePassword(const std::string &old_password, const std::string &new_password); + bool setDeviceLabel(const std::string &password, const std::string &label); + bool setKeyname(const std::string &password, int keyno, const std::string &keyname); + bool setKey(const std::string &password, int keyno, usbk_keysize_t keysize, const std::string &key); + bool setAutoactivateKeyno(const std::string &password, int keyno); + std::string getRandomKey(usbk_keysize_t keysize) const; + bool refreshUsbkInfo(); + + static bool isDebugEnabled(); + static void setDebug(bool enabled); + static std::string libusbkVersion(); + +private: + bool m_debugEnabled; +}; + +/*class UsbkDeviceList : public std::vector +{ + +};*/ + +#endif /* LIBUSBK_H_ */ + diff --git a/libusbk/CMakeLists.txt b/libusbk/CMakeLists.txt index fde2820..571fe86 100644 --- a/libusbk/CMakeLists.txt +++ b/libusbk/CMakeLists.txt @@ -19,7 +19,7 @@ IF(APPLE) ENDIF(APPLE) # Project name -PROJECT (usbk_lib) +PROJECT (USBK_LIB) # CMAKE Settings CMAKE_MINIMUM_REQUIRED(VERSION 2.8) @@ -30,11 +30,12 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF) SET(${PROJECT_NAME}_VERSION_MAJOR 0) SET(${PROJECT_NAME}_VERSION_MINOR 0) SET(${PROJECT_NAME}_VERSION_PATCH 1) + SET(${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}") SET(${PROJECT_NAME}_VERSION_FULL "${${PROJECT_NAME}_VERSION}.${${PROJECT_NAME}_VERSION_PATCH}") -SET(${PROJECT_NAME}_SOVERSION "${${PROJECT_NAME}_VERSION}.${${PROJECT_NAME}_VERSION_PATCH}") +SET(${PROJECT_NAME}_SOVERSION "${${PROJECT_NAME}_VERSION_FULL}") -ADD_DEFINITIONS ( -DVERSION="${${PROJECT_NAME}_VERSION_FULL}" ) +CONFIGURE_FILE(src/config.h.cmake src/config.h) IF(UNIX AND NOT APPLE) # Search for udev package @@ -42,8 +43,7 @@ IF(UNIX AND NOT APPLE) IF(NOT UDEV) message(FATAL_ERROR "Unable to find 'udev' library!") ENDIF(NOT UDEV) - - + # Header files are located in these directories SET (${PROJECT_NAME}_INCLUDE_DIRS ${${PROJECT_NAME}_SOURCE_DIR}/src/ @@ -51,50 +51,47 @@ IF(UNIX AND NOT APPLE) ${CMAKE_SYSTEM_INCLUDE_PATH} ) INCLUDE_DIRECTORIES (${${PROJECT_NAME}_INCLUDE_DIRS}) - - + # Libraries are located in these directories SET (${PROJECT_NAME}_LIBRARIES_PATH ${CMAKE_SYSTEM_LIBRARY_PATH} ) LINK_DIRECTORIES (${${PROJECT_NAME}_LIBRARIES_PATH}) - + # Source Files #AUX_SOURCE_DIRECTORY (src ${PROJECT_NAME}_SOURCES) - + FILE(GLOB SRC_FILES "src/*" "src/linux/*") - + # Create Library File ADD_LIBRARY( ${PROJECT_NAME} SHARED ${SRC_FILES}) - + # These are the symbols for the linker - TARGET_LINK_LIBRARIES(${PROJECT_NAME} udev) - + TARGET_LINK_LIBRARIES(${PROJECT_NAME} udev) + # change lib_target properties SET_TARGET_PROPERTIES( ${PROJECT_NAME} PROPERTIES # create *nix style library versions + symbolic links - VERSION ${${PROJECT_NAME}_VERSION} + VERSION ${${PROJECT_NAME}_VERSION_FULL} SOVERSION ${${PROJECT_NAME}_SOVERSION} - + # allow creating static and shared libs without conflicts CLEAN_DIRECT_OUTPUT 1 - + # avoid conflicts between library and binary target names - OUTPUT_NAME usbk -# LINK_FLAGS -m64 + OUTPUT_NAME usbk ) # Install this library - INSTALL(TARGETS ${PROJECT_NAME} + INSTALL(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /usr/lib PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) - + INSTALL(FILES src/libusbk.h DESTINATION /usr/include PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) IF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) - # Settings for debian package SET(CPACK_GENERATOR "DEB") SET(CPACK_DEBIAN_PACKAGE_NAME ${PROJECT_NAME}) @@ -109,48 +106,48 @@ IF(UNIX AND NOT APPLE) SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") SET(CPACK_DEBIAN_PACKAGE_RECOMMENDS "recommends") SET(CPACK_DEBIAN_PACKAGE_SUGGESTS "suggests") + INCLUDE(CPack) - + ENDIF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) - ELSEIF(WIN32) - - # Header files are located in these directories + # Header files are located in these directories SET (${PROJECT_NAME}_INCLUDE_DIRS ${${PROJECT_NAME}_SOURCE_DIR}/src/ ${${PROJECT_NAME}_SOURCE_DIR}/src/windows/ ${CMAKE_SYSTEM_INCLUDE_PATH} ) INCLUDE_DIRECTORIES (${${PROJECT_NAME}_INCLUDE_DIRS}) - + # Libraries are located in these directories SET (${PROJECT_NAME}_LIBRARIES_PATH ${CMAKE_SYSTEM_LIBRARY_PATH} ) LINK_DIRECTORIES (${${PROJECT_NAME}_LIBRARIES_PATH}) - + FILE(GLOB SRC_FILES "src/*" "src/windows/*") - + ADD_LIBRARY( ${PROJECT_NAME} SHARED ${SRC_FILES}) - + SET_TARGET_PROPERTIES( ${PROJECT_NAME} PROPERTIES VERSION ${${PROJECT_NAME}_VERSION} SOVERSION ${${PROJECT_NAME}_SOVERSION} - + # allow creating static and shared libs without conflicts CLEAN_DIRECT_OUTPUT 1 - + # avoid conflicts between library and binary target names - OUTPUT_NAME libusbk + OUTPUT_NAME libusbk ) - INSTALL(TARGETS ${PROJECT_NAME} - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - RUNTIME DESTINATION bin + INSTALL(TARGETS + ${PROJECT_NAME} + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE - ) - + ) + INCLUDE(CPack) ELSEIF(APPLE) @@ -158,3 +155,4 @@ ELSEIF(APPLE) ELSE(UNIX AND NOT APPLE) message(FATAL_ERROR "Undefined Operating System") ENDIF(UNIX AND NOT APPLE) + diff --git a/libusbk/src/config.h.cmake b/libusbk/src/config.h.cmake new file mode 100644 index 0000000..3a0ca86 --- /dev/null +++ b/libusbk/src/config.h.cmake @@ -0,0 +1,14 @@ +/** + * + * @project @PROJECT_NAME@ + * @file config.h + * + */ + +#ifndef CONFIG_H_ +#define CONFIG_H_ + +#define USBK_LIB_VERSION_FULL "@USBK_LIB_VERSION_FULL@" + +#endif // CONFIG_H_ + diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c index 4da8427..0498ba2 100644 --- a/libusbk/src/libusbk.c +++ b/libusbk/src/libusbk.c @@ -1,5 +1,5 @@ /* - * @file libusbk.cpp + * @file libusbk.c * * Copyright (C) 2010 USB-K Team * @@ -1170,9 +1170,8 @@ bool usbk_debug_check(void) return debug_enable; } -#if 0 const char* usbk_libversion(void) { - return VERSION; + return USBK_LIB_VERSION_FULL; } -#endif + diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index 6bd919e..e3c4000 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -21,8 +21,11 @@ extern "C" { #endif +#include "config.h" + #if defined(__AVR32__) || defined(__linux__) # include +# include #elif defined(WIN32) # include #else From 359ebd1d8840d151db2a4ddad3633b0a672e3b19 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Mon, 24 Oct 2011 23:01:08 +0300 Subject: [PATCH 13/16] libusbk++ initial works --- .gitignore | 1 + CMakeLists.txt | 3 + libusbk++/CMakeLists.txt | 4 +- libusbk++/src/libusbk.cpp | 1403 +++++------------------------------- libusbk++/src/libusbk.hpp | 40 +- libusbk/src/libusbk.c | 227 +++--- libusbk/src/libusbk.h | 37 +- usbk_cli/src/main.cpp | 2 +- usbk_cli_pp/CMakeLists.txt | 108 +++ usbk_cli_pp/src/main.cpp | 753 +++++++++++++++++++ 10 files changed, 1224 insertions(+), 1354 deletions(-) create mode 100644 usbk_cli_pp/CMakeLists.txt create mode 100644 usbk_cli_pp/src/main.cpp diff --git a/.gitignore b/.gitignore index e97a43b..69dee56 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ CPackSourceConfig.cmake *.o *.so* usbk_cli/usbk +usbk_cli_pp/usbk++ config.h config.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c998778..da202ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,9 @@ INCLUDE (Uninstaller) ADD_SUBDIRECTORY( libusbk ) ADD_SUBDIRECTORY( libusbk++ ) ADD_SUBDIRECTORY( usbk_cli ) + +ADD_SUBDIRECTORY( usbk_cli_pp ) # temporary test cli + ADD_SUBDIRECTORY( man ) UNINSTALLLER_FEATURE() diff --git a/libusbk++/CMakeLists.txt b/libusbk++/CMakeLists.txt index a5cacdd..866e660 100644 --- a/libusbk++/CMakeLists.txt +++ b/libusbk++/CMakeLists.txt @@ -86,7 +86,7 @@ IF(UNIX AND NOT APPLE) CLEAN_DIRECT_OUTPUT 1 # avoid conflicts between library and binary target names - OUTPUT_NAME libusbk++ + OUTPUT_NAME usbk++ ) # Install this library @@ -94,7 +94,7 @@ IF(UNIX AND NOT APPLE) LIBRARY DESTINATION /usr/lib PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) - INSTALL(FILES src/libusbk.h + INSTALL(FILES src/libusbk.hpp DESTINATION /usr/include PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) diff --git a/libusbk++/src/libusbk.cpp b/libusbk++/src/libusbk.cpp index ac53984..73c8477 100644 --- a/libusbk++/src/libusbk.cpp +++ b/libusbk++/src/libusbk.cpp @@ -19,1378 +19,315 @@ #include "libusbk.hpp" -#if 0 -#if defined(__linux__) -# include -# include -# include -#endif - -#include +#include +#include #include -#include -#include - -#include "uip.h" - -#include "libusbk.h" -#include "usbk_scsi.h" - -#if defined(__linux__) - #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "\033[0;32;40m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "\033[1;3;31m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} - #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ - else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} -#elif defined(WIN32) - #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} - #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ - else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} -#endif - -//-DEFINES FOR FINDING USBK AND BACKDISK -#define USBK_USB_IDVENDOR "2384" -#define USBK_USB_IDPRODUCT_A103 "a103" -#define USBK_USB_IDPRODUCT_A101 "a101" -#define USBK_SCSI_VENDOR "USBK" -#define USBK_SCSI_BACKDISK_VENDOR "BackDisk" - -#define USBK_CHECK_SUPPORTED(usbk) \ - if (!usbk->is_supported) { \ - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; \ - DBG_LASTOPR_STRING(usbk->lastopr); \ - return - usbk->lastopr; \ - } -typedef struct __RTRN_STRING { - int rtrn; - const char *rtrn_string; -} RTRN_STRING; - -RTRN_STRING lastopr_string[] = { - {USBK_LO_PASS, "Ok" }, - {USBK_LO_GEN_FAIL, "fail" }, - {USBK_LO_FAILED_PASS, "wrong password" }, - {USBK_LO_FABRIC_RESET, "Error, FABRIC_RESET" }, - {USBK_LO_USBK_UNPLUGING, "n/a" }, - {USBK_LO_INVALID_KEYNO, "invalid key number" }, - {USBK_LO_INVALID_KEYSIZE, "invalid key size" }, - {USBK_LO_INVALID_DEVICELABEL, "invalid device label" }, - {USBK_LO_INVALID_PASS, "n/a" }, - {USBK_LO_INVALID_NEWPASS, "invalid new password" }, - {USBK_LO_STATE_ERROR, "state error" }, - {USBK_LO_SCSI_ERROR, "scsi error" }, - {USBK_LO_UNSUPPORTED_USBK, "unsupported usbk" }, - {USBK_LO_INVALID_KEY, "invalid key" }, -#if defined(__linux__) - {USBK_LO_UDEV_ERROR, "udev error" }, -#endif - {USBK_LO_MEM_ERROR, "memory allocation error"} -}; - -typedef struct { - const char* model; - bool support; -} LIBUSBK_SUPPORTED_MODELS; - -LIBUSBK_SUPPORTED_MODELS models_1_1[] = { - { "A101", true }, - { "A103", true }, - { NULL, false } -}; - -typedef struct { - int major_version; - int minor_version; - LIBUSBK_SUPPORTED_MODELS* models; -} LIBUSBK_SUPPORTED_VERSIONS; - -LIBUSBK_SUPPORTED_VERSIONS versions_1[] = { - { 2, 5, models_1_1 }, - { 0, 0, NULL } -}; - -typedef struct { - const char* product; - LIBUSBK_SUPPORTED_VERSIONS* versions; -} LIBUSBK_SUPPORTED_PRODUCTS; - -LIBUSBK_SUPPORTED_PRODUCTS products[] = { - { "USBK CryptoBridge 2.0", versions_1 }, - { "USBK CryptoBridge", versions_1 }, - { NULL, NULL } -}; - -static bool debug_enable = false; - -static int _get_udev_info(USBK* usbk, const char *device); -static int _get_udev_backdisk(USBK* usbk); -static int _command_status(USBK* usbk); - -static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize); - -static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize); -static unsigned int _keysize_as_byte(usbk_keysize_t keysize); -#endif +static bool _libusbk_plusplus_debug_enabled = false; +static const char* _libusbk_plusplus_version = USBK_CPP_LIB_VERSION_FULL; -usbk_lo_t UsbkDevice::lastOperation() const +/* Debugging informations */ +void libusbk_plusplus_enable_debug() { _libusbk_plusplus_debug_enabled = true; } +void libusbk_plusplus_disable_debug() { _libusbk_plusplus_debug_enabled = false; } + +/* libusbk version */ +const char* libusbk_plusplus_plusplus_version() { return _libusbk_plusplus_version; } + + +UsbkDevice::UsbkDevice(const std::string &device_node) { - usbk_lo_t ret = 0; + USBK *usbk_device = usbk_new(device_node.c_str()); + + if (!usbk_device) { + std::cerr << "no such device " << device_node; - return ret; + return; + } + + this->m_usbk_device = usbk_device; } -bool UsbkDevice::supported() const +UsbkDevice::UsbkDevice(USBK *usbk_device) { - bool ret = true; + this->m_usbk_device = usbk_device; +} - return ret; +UsbkDevice::~UsbkDevice() +{ + usbk_release(m_usbk_device); } -std::string UsbkDevice::deviceNode() const +usbk_lo_t UsbkDevice::lastOperation() const +{ + return usbk_get_lastopr_status(m_usbk_device); +} + +bool UsbkDevice::supported() const { - std::string ret; + return usbk_check_support(m_usbk_device); +} - return ret; +std::string UsbkDevice::deviceNode() const +{ + return usbk_get_dev(m_usbk_device); } std::string UsbkDevice::deviceNodePath() const { - std::string ret; - - return ret; + return usbk_get_dev_path(m_usbk_device); } std::string UsbkDevice::backdiskNode() const { - std::string ret; - - return ret; - + return usbk_get_backdisk(m_usbk_device); } std::string UsbkDevice::backdiskNodePath() const { - std::string ret; - - return ret; - + return usbk_get_backdisk_path(m_usbk_device); } std::string UsbkDevice::product() const { - - std::string ret; - - return ret; + return usbk_get_product(m_usbk_device); } std::string UsbkDevice::model() const { - - std::string ret; - - return ret; + return usbk_get_model(m_usbk_device); } std::string UsbkDevice::serial() const { - - std::string ret; - - return ret; + return usbk_get_serial(m_usbk_device); } std::string UsbkDevice::usbSerial() const { - - std::string ret; - - return ret; + return usbk_get_usb_serial(m_usbk_device); } std::string UsbkDevice::firmwareVersion() const { - std::string ret; - - return ret; - + return usbk_get_firmware_ver(m_usbk_device); } std::string UsbkDevice::deviceLabel() const { - std::string ret; - - return ret; - + return usbk_get_dev_label(m_usbk_device); } uint8_t UsbkDevice::multikeyCapability() const { - uint8_t ret; - - return ret; + return usbk_get_multikeycap(m_usbk_device); } usbk_ds_t UsbkDevice::deviceState() const { - usbk_ds_t ret = USBK_DS_ACTIVATE_WITH_BACKDISK; + return usbk_get_state(m_usbk_device); +} - return ret; +int UsbkDevice::retryNumber() const +{ + return usbk_get_retry_number(m_usbk_device); } int UsbkDevice::currentKey() const { - int ret; - - return ret; + return usbk_get_current_keyno(m_usbk_device); } int UsbkDevice::autoactivateKeyNo() const { - int ret; - - return ret; + return usbk_get_autoactivation_keyno(m_usbk_device); } -std::vector UsbkDevice::keyNames() const +std::string UsbkDevice::keyName(int keyno) const { - std::vector ret; + if (keyno < 0 || keyno > multikeyCapability() - 1) + return std::string(); - return ret; + return usbk_get_keyname(m_usbk_device, keyno); } -bool UsbkDevice::activateKey(int /*keyno*/) +bool UsbkDevice::activateKey(const std::string &password, int keyno) { - bool ret; - - return ret; + return usbk_key_activate(m_usbk_device, password.c_str(), keyno); } bool UsbkDevice::deactivate() { - bool ret; - - return ret; - + return usbk_key_deactivate(m_usbk_device); } -bool UsbkDevice::changePassword(const std::string &old_password, const std::string &new_password) +bool UsbkDevice::changePassword(const std::string &oldPassword, const std::string &newPassword) { - bool ret; - - return ret; - - + return usbk_change_password(m_usbk_device, oldPassword.c_str(), newPassword.c_str()); } bool UsbkDevice::setDeviceLabel(const std::string &password, const std::string &label) { - bool ret; - - return ret; - + return usbk_set_devicelabel(m_usbk_device, password.c_str(), label.c_str()); } bool UsbkDevice::setKeyname(const std::string &password, int keyno, const std::string &keyname) { - - bool ret; - - return ret; + return usbk_set_keyname(m_usbk_device, password.c_str(), keyno, keyname.c_str()); } bool UsbkDevice::setKey(const std::string &password, int keyno, usbk_keysize_t keysize, const std::string &key) { - bool ret; - - return ret; - + return usbk_set_key_text(m_usbk_device, password.c_str(), keyno, keysize, key.c_str()); } bool UsbkDevice::setAutoactivateKeyno(const std::string &password, int keyno) { - - bool ret; - - return ret; + return usbk_set_autact(m_usbk_device, password.c_str(), keyno); } +/* FIXME: not implemented yet */ std::string UsbkDevice::getRandomKey(usbk_keysize_t keysize) const { - std::string ret; - - return ret; + return std::string(); } +/* FIXME: not implemented yet */ bool UsbkDevice::refreshUsbkInfo() { + int ret = usbk_refresh_usbkinfo(m_usbk_device); - bool ret; - - return ret; + return (ret < 0) ? false : true; } -bool UsbkDevice::isDebugEnabled() +std::ostream &operator<<(std::ostream &out, UsbkDevice &device) { + device.refreshUsbkInfo(); - bool ret; + std::string title; + std::string retryNum; + std::string backdisk = "-"; + std::string status; + std::string autoactive; - return ret; -} + if (device.supported()) { + title = "USBK Information "; -void UsbkDevice::setDebug(bool enabled) -{ + switch (device.deviceState()) { + case USBK_DS_ACTIVATE: + case USBK_DS_ACTIVATE_WITH_BACKDISK: + { + std::stringstream s; -} + s << "active [#" + << (int)device.currentKey() + << ": " << device.keyName((int)device.currentKey()-1) + << "]"; + status = s.str(); + } -std::string UsbkDevice::libusbkVersion() -{ - std::string ret; + if (device.deviceState()==USBK_DS_ACTIVATE) + backdisk = "none"; + else + backdisk = "Plugged In"; - return ret; -} + break; -#if 0 -USBK* usbk_new(const char* dev) -{ - int ret; - int i; - - USBK* usbk = (USBK*) calloc(1, sizeof(USBK)); - if (usbk == NULL) { - free(usbk); - DBG_LASTOPR_STRING(USBK_LO_UDEV_ERROR); - - return NULL; - } - -#if defined(__linux__) - // get information about usbk by udev - ret = _get_udev_info(usbk, dev); - if (ret < 0) { - free(usbk); - DBG_LASTOPR_STRING(USBK_LO_UDEV_ERROR); - - return NULL; - } -#elif defined(WIN32) - usbk_head->dev_node = strdup(dev); - usbk_head->dev_node_path = strdup(dev); -#endif - - t_UIP_DEVINFO usbk_info; - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char*) &usbk_info, - USBK_GET_DEV_INFO, - sizeof(usbk_info), - USBK_READ_SCSI); - if (ret < 0) { - free(usbk); - DBG_LASTOPR_STRING(USBK_LO_SCSI_ERROR); - - return NULL; - } - - usbk->product = strdup(usbk_info.product.s); - usbk->model = strdup(usbk_info.model.s); - usbk->firmware_ver = strdup(usbk_info.firmware_ver.s); - usbk->is_supported = true; - - usbk->multikey_cap = usbk_info.multikeycap; - usbk->current_key = usbk_info.current_keyno; - usbk->autoact_keyno = usbk_info.autoactivate_keyno; - usbk->retry_num = usbk_info.retry_num; - usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; - usbk->dev_label = strdup(usbk_info.devlabel.s); - - /* FIXME why we need this much space */ - usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); - for (i = 0; i < UIP_SERIAL_SIZE; i++) { - sprintf((usbk->serial + i * 2), "%2.2X", usbk_info.serial.u8[i]); - } + case USBK_DS_DEACTIVATE: + status = "deactive"; + break; - usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); - for (i = 0; i < usbk->multikey_cap; i++) { - usbk->key_names[i] = strdup(usbk_info.keyname[i].s); - } - - usbk->lastopr = USBK_LO_PASS; - DBG_LASTOPR_STRING(usbk->lastopr); - - return usbk; -} - -int usbk_release(USBK* usbk) -{ - int i; - - free(usbk->dev_node); - free(usbk->dev_node_path); - free(usbk->backdisk_node); - free(usbk->backdisk_node_path); - free(usbk->product); - free(usbk->model); - free(usbk->serial); - free(usbk->usb_serial); - free(usbk->firmware_ver); - free(usbk->dev_label); - - for (i = 0; i < usbk->multikey_cap; i++) { - free(usbk->key_names[i]); - } + case USBK_DS_FABRIC_DEFAULT: + status = "USBK in fabric default. Please first set your password."; + break; - free(usbk->key_names); - free(usbk); + case USBK_DS_MUST_REMOVE: + status = "Must remove. Please remove and re-plug the USBK."; + break; - return 0; -} - -int usbk_key_activate(USBK* usbk, const char* password, uint8_t key_no) -{ - int ret; - t_UIP_ACTIVATE activate; + default: + status = "Unknown Status "; + status += device.deviceState(); + break; + } - USBK_CHECK_SUPPORTED(usbk) - - if (key_no > usbk->multikey_cap) { - usbk->lastopr = USBK_LO_INVALID_KEYNO; - ret = - usbk->lastopr; - - goto done; - } - - switch (usbk->dev_state) { - case USBK_DS_DEACTIVATE: - memset(&activate, 0, sizeof(activate)); - - /* FIXME: check password size */ - strcpy(activate.password.s, password); - activate.keyno = key_no; - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char *) &activate, - USBK_ACTIVATE_KEY, - sizeof(activate), - USBK_WRITE_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - usbk->lastopr = _command_status(usbk); - - break; - - case USBK_DS_ACTIVATE: - case USBK_DS_ACTIVATE_WITH_BACKDISK: - case USBK_DS_FABRIC_DEFAULT: - case USBK_DS_MUST_REMOVE: - default: - usbk->lastopr = USBK_LO_STATE_ERROR; - ret = - usbk->lastopr; - - goto done; - } - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_key_deactivate(USBK* usbk) -{ - int ret; - - USBK_CHECK_SUPPORTED(usbk) - - switch (usbk->dev_state) { - case USBK_DS_ACTIVATE: - case USBK_DS_ACTIVATE_WITH_BACKDISK: - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char*) NULL, - USBK_DEACTIVATE_KEY, - 0, - USBK_WRITE_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - usbk->lastopr = _command_status(usbk); - - break; - - case USBK_DS_DEACTIVATE: - case USBK_DS_FABRIC_DEFAULT: - case USBK_DS_MUST_REMOVE: - default: - usbk->lastopr = USBK_LO_STATE_ERROR; - ret = - usbk->lastopr; - - goto done; - } - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass) -{ - int ret; - t_UIP_CHPASS chpass; - - USBK_CHECK_SUPPORTED(usbk) - - switch (usbk->dev_state) { - case USBK_DS_DEACTIVATE: - case USBK_DS_FABRIC_DEFAULT: - memset(&chpass, 0, sizeof(chpass)); - if (new_pass != NULL) { - strncpy(chpass.new_password.s, new_pass, sizeof(chpass.new_password.s)); - } - if (old_pass != NULL) { - strncpy(chpass.old_password.s, old_pass, sizeof(chpass.old_password.s)); - } - - DBG_INFO("old : %s",chpass.old_password.s); - DBG_INFO("new : %s",chpass.new_password.s); - - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &chpass, USBK_CHANGE_PASS, sizeof(chpass), USBK_WRITE_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - usbk->lastopr = _command_status(usbk); - - break; - - case USBK_DS_ACTIVATE: - case USBK_DS_ACTIVATE_WITH_BACKDISK: - case USBK_DS_MUST_REMOVE: - default: - usbk->lastopr = USBK_LO_STATE_ERROR; - ret = - usbk->lastopr; - - goto done; - } - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) -{ - int ret; - t_UIP_SETDEVICELABEL devlabel; - - USBK_CHECK_SUPPORTED(usbk) - - switch (usbk->dev_state) { - case USBK_DS_DEACTIVATE: - memset(&devlabel, 0, sizeof(devlabel)); - strncpy(devlabel.password.s, pass, sizeof(devlabel.password.s)); - strncpy(devlabel.devlabel.s, device_label, sizeof(devlabel.devlabel.s)); - ret = send_scsi_command(usbk->dev_node_path, (unsigned char*) &devlabel, USBK_SET_DEV_NAME, sizeof(devlabel), USBK_WRITE_SCSI); - - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - usbk->lastopr = _command_status(usbk); - - break; - case USBK_DS_ACTIVATE: - case USBK_DS_ACTIVATE_WITH_BACKDISK: - case USBK_DS_FABRIC_DEFAULT: - case USBK_DS_MUST_REMOVE: - default: - usbk->lastopr = USBK_LO_STATE_ERROR; - ret = - usbk->lastopr; - - goto done; - } - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const char* key_name, usbk_keysize_t key_size, const uint8_t* key) -{ - int ret; - t_UIP_SETKEY setkey; - - USBK_CHECK_SUPPORTED(usbk) - - if (key_no > usbk->multikey_cap) { - usbk->lastopr = USBK_LO_INVALID_KEYNO; - ret = - usbk->lastopr; - - goto done; - } - - switch (usbk->dev_state) { - case USBK_DS_DEACTIVATE: - memset(&setkey, 0, sizeof(setkey)); - strncpy(setkey.password.s, pass, sizeof(setkey.password.s)); - setkey.keyno = key_no; - - if (key_name != NULL) { - strncpy(setkey.keyname.s, key_name, sizeof(setkey.keyname.s)); - } else { - /* preserve old keyname if keyname is not changed */ - strncpy(setkey.keyname.s, usbk->key_names[key_no - 1], sizeof(setkey.keyname.s)); - } - - if ((key_size != USBK_KEYSIZE_NULL) && (key != NULL)) { - setkey.options.me = SETKEY_NAME_AND_KEY; - setkey.keysize.me = (e_UIP_KEYSIZE) key_size; - memcpy(setkey.key.u8, key, _keysize_as_byte(key_size)); - } else { - setkey.options.me = SETKEY_NAMEONLY; - } - - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char*) &setkey, - USBK_SET_KEY, - sizeof(setkey), - USBK_WRITE_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - usbk->lastopr = _command_status(usbk); - - break; - case USBK_DS_ACTIVATE: - case USBK_DS_ACTIVATE_WITH_BACKDISK: - case USBK_DS_FABRIC_DEFAULT: - case USBK_DS_MUST_REMOVE: - default: - usbk->lastopr = USBK_LO_STATE_ERROR; - ret = - usbk->lastopr; - - goto done; - } - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_set_key_decimal(USBK* usbk_head, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) -{ - int ret = 0; - uint8_t key_hex[UIP_KEYSIZE_TOTALSIZE]; - - USBK_CHECK_SUPPORTED(usbk_head) - - usbk_head->lastopr = _convert_key_decimal2hex(key_hex, key, key_size); - if (ret == USBK_LO_PASS){ - usbk_head->lastopr = usbk_set_key_and_keyname(usbk_head, pass, key_no, NULL, key_size, key_hex); - } - - DBG_LASTOPR_STRING(usbk_head->lastopr); - - return usbk_head->lastopr; -} - -int usbk_set_key_hex(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key) -{ - USBK_CHECK_SUPPORTED(usbk) - - return usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key); -} - -int usbk_set_key_text(USBK* usbk, const char *pass, uint8_t key_no, usbk_keysize_t key_size, const char* key) -{ - int ret; - uint8_t key_hex[32]; /* use macro definition instead of 32 */ - - USBK_CHECK_SUPPORTED(usbk) - - ret = _convert_key_text2hex(key_hex, key, key_size); - if (ret == USBK_LO_PASS){ - ret = usbk_set_key_and_keyname(usbk, pass, key_no, NULL, key_size, key_hex); - } - - usbk->lastopr = ret; - - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_set_autact(USBK* usbk, const char *pass, int key_no) -{ - int ret; - t_UIP_SETAUTOACTIVATE autoact; - - USBK_CHECK_SUPPORTED(usbk) - - if (key_no > usbk->multikey_cap) { - usbk->lastopr = USBK_LO_INVALID_KEYNO; - ret = - usbk->lastopr; - - goto done; - } - - switch (usbk->dev_state) { - case USBK_DS_DEACTIVATE: - memset(&autoact, 0, sizeof(autoact)); - strncpy(autoact.password.s, pass, sizeof(autoact.password.s)); - autoact.keyno = key_no; - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char*) &autoact, - USBK_SET_AUTO_ACTIVE, - sizeof(autoact), - USBK_WRITE_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - usbk->lastopr = _command_status(usbk); - - break; - case USBK_DS_ACTIVATE: - case USBK_DS_ACTIVATE_WITH_BACKDISK: - case USBK_DS_FABRIC_DEFAULT: - case USBK_DS_MUST_REMOVE: - default: - usbk->lastopr = USBK_LO_STATE_ERROR; - ret = - usbk->lastopr; - - goto done; - } - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) -{ - int ret; - t_UIP_GENERATEKEY genkey; - - USBK_CHECK_SUPPORTED(usbk) - - unsigned int keysize_inbyte = _keysize_as_byte(keysize); - if ((keysize_inbyte == 0) || (keysize_inbyte > _keysize_as_byte(USBK_KEYSIZE_256BIT))) { - usbk->lastopr = USBK_LO_INVALID_KEYSIZE; - ret = - usbk->lastopr; - - goto done; - } - - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char*) &genkey, - USBK_GENERATE_KEY, - sizeof(genkey), - USBK_READ_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - - usbk->lastopr = _command_status(usbk); - if (usbk->lastopr == USBK_LO_PASS) { - memcpy(random_key, genkey.key.u8, keysize_inbyte); - } - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -int usbk_refresh_usbkinfo(USBK* usbk) -{ - int i; - int ret; - - USBK_CHECK_SUPPORTED(usbk) - - t_UIP_DEVINFO usbk_info; - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char*) &usbk_info, - USBK_GET_DEV_INFO, - sizeof(usbk_info), - USBK_READ_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - ret = - usbk->lastopr; - - goto done; - } - - usbk->current_key = usbk_info.current_keyno; - usbk->autoact_keyno = usbk_info.autoactivate_keyno; - usbk->retry_num = usbk_info.retry_num; - usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; - usbk->dev_label = strdup(usbk_info.devlabel.s); - - usbk->key_names = (char**) calloc(usbk->multikey_cap, sizeof(char*)); - for (i = 0; i < usbk->multikey_cap; i++) { - usbk->key_names[i] = strdup(usbk_info.keyname[i].s); - } - usbk->lastopr = USBK_LO_PASS; - -done: - DBG_LASTOPR_STRING(usbk->lastopr); - - return ret; -} - -static int _command_status(USBK* usbk) -{ - int ret; - t_UIP_GETSTATUS status; - - ret = send_scsi_command ( - usbk->dev_node_path, - (unsigned char*) &status, - USBK_GET_STATUS, - sizeof(t_UIP_GETSTATUS), - USBK_READ_SCSI); - if (ret < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - - return - usbk->lastopr; - } - - usbk->retry_num = status.retry_num; - - /* FIXME a new review required!! */ - return (status.lastop.me - 1); //todo versiyon checking'ine göre düzleltilecek. -} - -#if 0 -static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize) -{ - // 1) string icinde bosluk karakteri var mi? - // 2) string icerisindeki '.' karakterinin sayisi 15 mi? - // 3) '.' karakterlerini ' ' karakterine donustur - // 4) string icerisinde bosluklar haric decimal olmayan karakter var mi? - // 5) string to integer islemini yap - // 6) 16 adet sayı cikartabildin mi? - // 7) sayılar 0 ile 255 arasinda mi? - - unsigned int key_len = _keysize_as_byte(keysize); - int i; - - /* check key_text if it is null or not */ - if (key_decimal == NULL || key_hex == NULL) - return USBK_LO_INVALID_KEY; - - /* check key length if it is proper or not */ - if(strlen(key_hex) > key_len) { - /* FIXME USBK_LO_INVALID_KEYSIZE? */ - return USBK_LO_INVALID_KEY; - } - - for(i=0; i_keysize_as_byte(keysize)]; - int ikey[50]; - size_t found; - string::iterator it; - unsigned int i; - string key_decimal_str = key_decimal; - - // string icinde bosluk karakteri var mi? - if (key_decimal_str.find(" ") != string::npos) return USBK_LO_INVALID_KEY; - - // string icerisindeki '.' karakterinin sayisi 15 mi? - // '.' karakterlerini ' ' karakterine donustur - i = 0; - found = key_decimal_str.find_first_of("."); - while (found != string::npos) { - i++; - key_decimal_str[found] = ' '; - found = key_decimal_str.find_first_of(".", found + 1); - } - if (i != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; - //cout << str << endl; - - // string icerisinde bosluklar haric decimal olmayan karakter var mi? - i = 0; - for (it = key_decimal_str.begin(); it < key_decimal_str.end(); it++) { - if (*it != ' ') { - if (isdigit(*it) == false) { - i++; - } - } - } - if (i != 0) { - return USBK_LO_INVALID_KEY; - } - - // string to integer islemini yap - // sayılar 0 ile 255 arasinda mi? - istringstream iss(key_decimal_str, istringstream::in); - i = 0; - for (unsigned int n = 0; n < _keysize_as_byte(keysize); n++) { - iss >> ikey[n]; - - if (iss.fail() || iss.bad()) return USBK_LO_INVALID_KEY; - - if (iss.eof() && n != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; - - if (ikey[n] > 255 || ikey[n] < 0) { - i++; - } - key_hex[n] = ikey[n]; - //cout << n+1 << "." << ikey[n] << endl; - } - if (i != 0) { - return USBK_LO_INVALID_KEY; - } - - return USBK_LO_PASS; -} -#endif - -static inline int16_t _xtod(const char c) -{ - if (c>='0' && c<='9') return c-'0'; - if (c>='A' && c<='F') return c-'A'+10; - if (c>='a' && c<='f') return c-'a'+10; - - return -1; -} - -static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize) -{ - unsigned int key_len = _keysize_as_byte(keysize); - int i; - - /* check key_text if it is null or not */ - if (key_text == NULL || key_hex == NULL) - return USBK_LO_INVALID_KEY; - - /* check key length if it is proper or not */ - if(strlen(key_hex) > key_len) { - /* FIXME USBK_LO_INVALID_KEYSIZE? */ - return USBK_LO_INVALID_KEY; - } - - for(i=0; ilastopr = USBK_LO_UDEV_ERROR; - - _udev = udev_new(); - if (!_udev) goto done; - - _udev_dev_path = udev_get_dev_path(_udev); - - /* allocate ram space from stack with alloca */ - _udev_devname = alloca(strlen(_udev_dev_path) + strlen(device) + 1); - assert(_udev_devname != NULL); - sprintf(_udev_devname, "%s/%s", udev_get_dev_path(_udev), device); - - if (stat(_udev_devname, &statbuf) < 0) goto done; - - if (S_ISBLK(statbuf.st_mode)) { - dev_type = 'b'; - } - else if (S_ISCHR(statbuf.st_mode)) { - /* FIXME char devtype is legal? */ - dev_type = 'c'; + autoactive = (device.autoactivateKeyNo() == 0) + ? "Disable" + : "Enabled key with #" + device.autoactivateKeyNo(); + retryNum = device.retryNumber(); } else { - goto done; - } - - _udev_device = udev_device_new_from_devnum(_udev, dev_type, statbuf.st_rdev); - - if (!_udev_device) goto done; - - _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); - if (!_udev_device_usb) goto done; - - _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); - - ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); - if (ret) goto done; - - _udev_device_idproduct = udev_device_get_sysattr_value(_udev_device_usb, "idProduct"); - - if (!strncmp(USBK_USB_IDPRODUCT_A101, _udev_device_idproduct, strlen(_udev_device_idproduct)) && - !strncmp(USBK_USB_IDPRODUCT_A103, _udev_device_idproduct, strlen(_udev_device_idproduct))) - goto done; - - _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); - if (!_udev_device_scsi) goto done; - - ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); - if (ret) goto done; - - usbk->dev_node = strdup(udev_device_get_sysname(_udev_device)); - usbk->dev_node_path = strdup(udev_device_get_devnode(_udev_device)); - usbk->usb_serial = strdup(udev_device_get_sysattr_value(_udev_device_usb, "serial")); - - /* another usbk_lo_** required for backdisk error */ - ret = _get_udev_backdisk(usbk); - if (ret < 0) goto done; + title = "Unknown Device!!!"; - usbk->lastopr = USBK_LO_PASS; - -done: - udev_device_unref(_udev_device); - udev_enumerate_unref(_udev_enumerate); - udev_unref(_udev); - - DBG_LASTOPR_STRING(usbk->lastopr); - - return usbk->lastopr; -} - -static int _get_udev_backdisk(USBK* usbk) -{ - int ret; - - struct udev *_udev = NULL; - struct udev_enumerate *_udev_enumerate = NULL; - struct udev_list_entry *_udev_enumerated_devices = NULL; - struct udev_list_entry *_udev_enumerated_device = NULL; - - struct udev_device *_udev_device = NULL; - struct udev_device *_udev_device_scsi = NULL; - struct udev_device *_udev_device_usb = NULL; - - const char *_udev_enumerated_device_path; - const char *_udev_device_idvendor; - const char *_udev_device_idproduct; - const char *_udev_device_usb_serial; - - usbk->lastopr = USBK_LO_UDEV_ERROR; - - // Create the _udev object - _udev = udev_new(); - - if (!_udev) goto done; - - /* get udev enumerated devices */ - _udev_enumerate = udev_enumerate_new(_udev); - - /* add block devices */ - ret = udev_enumerate_add_match_subsystem(_udev_enumerate, "block"); - if (ret) goto done; - - /* add scan for devices */ - ret = udev_enumerate_scan_devices(_udev_enumerate); - if (ret) goto done; - - /* get device list */ - _udev_enumerated_devices = udev_enumerate_get_list_entry(_udev_enumerate); - - udev_list_entry_foreach (_udev_enumerated_device, _udev_enumerated_devices) { - _udev_enumerated_device_path = udev_list_entry_get_name(_udev_enumerated_device); - _udev_device = udev_device_new_from_syspath(_udev, _udev_enumerated_device_path); - - /* get usb devices */ - _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); - if (!_udev_device_usb) continue; - - _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); - - ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); - if (ret) continue; - - _udev_device_idproduct = udev_device_get_sysattr_value(_udev_device_usb, "idProduct"); - - /* FIXME: code enhancement required */ - if (!strncmp(USBK_USB_IDPRODUCT_A101, _udev_device_idproduct, strlen(_udev_device_idproduct)) && - !strncmp(USBK_USB_IDPRODUCT_A103, _udev_device_idproduct, strlen(_udev_device_idproduct))) - continue; - - _udev_device_usb_serial = udev_device_get_sysattr_value(_udev_device_usb, "serial"); - - ret = strcmp(usbk->usb_serial, _udev_device_usb_serial); - if (ret) continue; - - _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); - if (!_udev_device_scsi) continue; - - ret = strncmp(USBK_SCSI_BACKDISK_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_BACKDISK_VENDOR)); - if (ret) continue; - - usbk->backdisk_node = strdup(udev_device_get_sysname(_udev_device)); - usbk->backdisk_node_path = strdup(udev_device_get_devnode(_udev_device)); - - usbk->lastopr = USBK_LO_PASS; - - /* no need to proceed loop */ - break; - } - -done: - udev_device_unref(_udev_device); - udev_enumerate_unref(_udev_enumerate); - udev_unref(_udev); - - DBG_LASTOPR_STRING(usbk->lastopr); - - return usbk->lastopr; -} - -USBK_LIST* usbk_list_new(void) -{ - struct udev *_udev = NULL; - struct udev_enumerate *_udev_enumerate = NULL; - struct udev_list_entry *_udev_enumerated_devices = NULL; - struct udev_list_entry *_udev_enumerated_device = NULL; - - struct udev_device *_udev_device = NULL; - struct udev_device *_udev_device_scsi = NULL; - struct udev_device *_udev_device_usb = NULL; - - const char *_udev_enumerated_device_path; - const char *_udev_device_idvendor; - - int ret; - USBK_LIST *usbk_list; - - USBK *usbk_last_entry = NULL; - USBK *usbk_new_entry = NULL; - - /* initialize udev */ - _udev = udev_new(); - if (_udev == NULL) { - fprintf(stderr, "Error! Udev cannot be initialized!\n"); - - return NULL; - } - - /* get udev enumerated devices */ - _udev_enumerate = udev_enumerate_new(_udev); - - /* add block devices */ - ret = udev_enumerate_add_match_subsystem(_udev_enumerate, "block"); - if (ret) goto done; - - /* add scan for devices */ - ret = udev_enumerate_scan_devices(_udev_enumerate); - if (ret) goto done; - - /* get device list */ - _udev_enumerated_devices = udev_enumerate_get_list_entry(_udev_enumerate); - - usbk_list = (USBK_LIST *) calloc(1, sizeof(USBK_LIST)); - - if (!usbk_list) { - DBG_LASTOPR_STRING(USBK_LO_MEM_ERROR); - - goto done; - } - - udev_list_entry_foreach (_udev_enumerated_device, _udev_enumerated_devices) { - _udev_enumerated_device_path = udev_list_entry_get_name(_udev_enumerated_device); - _udev_device = udev_device_new_from_syspath(_udev, _udev_enumerated_device_path); - - /* get usb devices */ - _udev_device_usb = udev_device_get_parent_with_subsystem_devtype(_udev_device, "usb", "usb_device"); - if (!_udev_device_usb) continue; - - _udev_device_idvendor = udev_device_get_sysattr_value(_udev_device_usb, "idVendor"); - - ret = strncmp(USBK_USB_IDVENDOR, _udev_device_idvendor, strlen(USBK_USB_IDVENDOR)); - if (ret) continue; - - _udev_device_scsi = udev_device_get_parent_with_subsystem_devtype(_udev_device, "scsi", "scsi_device"); - if (!_udev_device_scsi) continue; - - ret = strncmp(USBK_SCSI_VENDOR, udev_device_get_sysattr_value(_udev_device_scsi, "vendor"), strlen(USBK_SCSI_VENDOR)); - if (ret) continue; - - usbk_new_entry = usbk_new(udev_device_get_sysname(_udev_device)); - if (!usbk_new_entry) { - DBG_LASTOPR_STRING(USBK_LO_MEM_ERROR); - - goto done; - - } - - if (!usbk_list->usbk_head) { - usbk_list->usbk_head = usbk_last_entry = usbk_new_entry; - } else { - usbk_last_entry->next = usbk_new_entry; - usbk_last_entry = usbk_new_entry; - } - - usbk_list->count++; - } + status = "n/a"; + autoactive = "n/a"; + backdisk = "n/a"; + retryNum = "n/a"; + } -done: - udev_device_unref(_udev_device); - udev_enumerate_unref(_udev_enumerate); - udev_unref(_udev); + out << title + << std::endl << "\tProduct : " << device.product() + << std::endl << "\tModel : " << device.model() + << std::endl << "\tSerial Number : " << device.serial() + << std::endl << "\tFirmware Version : " << device.firmwareVersion() + << std::endl + << std::endl << "\tDeviceLabel : " << device.deviceLabel() + << std::endl << "\tDevice Node : " << device.deviceNodePath() + << std::endl << "\tBackDisk Node : " << device.backdiskNodePath() + << std::endl << "\tStatus : " << status + << std::endl << "\tRetry Number : " << retryNum + << std::endl << "\tBackDisk : " << backdisk + << std::endl << "\tAuto Activation : " << autoactive + << std::endl; - return usbk_list; -} + out << std::endl << "\tMax. Key Capacity : "; + out << (int)device.multikeyCapability() << std::endl; -int usbk_list_release(USBK_LIST *usbk_list) -{ - USBK *_usbk=usbk_list->usbk_head; + for (int i=0; inext) { - usbk_release(_usbk); - _usbk = _usbk->next; - usbk_list->count--; + out << std::endl; } - free(usbk_list); - - return 0; + return out; } -USBK* usbk_list_get_next(USBK* usbk) -{ - return usbk->next; -} -int usbk_list_refreshall(USBK_LIST *usbk_list) +UsbkDeviceList::UsbkDeviceList() { - USBK* list_entry; + USBK_LIST *_usbk_list = usbk_list_new(); + USBK* _usbk_entry; - usbk_list_entry_foreach (list_entry, usbk_list) { - usbk_refresh_usbkinfo(list_entry); + usbk_list_entry_foreach (_usbk_entry, _usbk_list) { + UsbkDevice *new_usbk_device = new UsbkDevice(_usbk_entry); + this->push_back(new_usbk_device); } - DBG_LASTOPR_STRING(USBK_LO_PASS); - - return USBK_LO_PASS; -} - -int usbk_list_get_count(USBK_LIST *usbk_list) -{ - return usbk_list->count; + usbk_list_release(_usbk_list); } -#endif - -void usbk_debug_enable(void) +std::ostream &operator<<(std::ostream &out, UsbkDeviceList &deviceList) { - debug_enable = true; -} + out << "All USBK devices:" << std::endl; -void usbk_debug_disable(void) -{ - debug_enable = false; -} + for (int i=0; ideviceLabel() + << std::endl << "\t Device :" << device->deviceNode() + << std::endl << "\t BackDisk :" << device->backdiskNode() + << std::endl << "\t Product :" << device->product() + << std::endl << "\t Model :" << device->model() + << std::endl << "\t Serial :" << device->serial() + << std::endl << "\t Firmware :" << device->firmwareVersion() + << std::endl << std::endl; + } -bool usbk_debug_check(void) -{ - return debug_enable; -} + out << deviceList.capacity() << " USBK devices found." << std::endl; -const char* usbk_libversion(void) -{ - return VERSION; + return out; } -#endif diff --git a/libusbk++/src/libusbk.hpp b/libusbk++/src/libusbk.hpp index 87d1556..ede792b 100644 --- a/libusbk++/src/libusbk.hpp +++ b/libusbk++/src/libusbk.hpp @@ -18,15 +18,28 @@ #ifndef LIBUSBK_HPP_ #define LIBUSBK_HPP_ -#include "config.h" +#include "config.hpp" #include "../../libusbk/src/libusbk.h" #include #include +#include -class UsbkDevice : public USBK +/* Debugging informations */ +extern void libusbk_plusplus_enable_debug(); +extern void libusbk_plusplus_disable_debug(); + +/* libusbk version */ +extern const char* libusbk_plusplus_plusplus_version(); + +class UsbkDevice { public: + UsbkDevice(); + UsbkDevice(const std::string &device_node); + UsbkDevice(USBK *usbk_device); + + ~UsbkDevice(); /** * All getter functions @@ -46,13 +59,17 @@ class UsbkDevice : public USBK std::string deviceLabel() const; uint8_t multikeyCapability() const; usbk_ds_t deviceState() const; + int retryNumber() const; int currentKey() const; int autoactivateKeyNo() const; - std::vector keyNames() const; + std::string keyName(int keyno) const; - bool activateKey(int keyno); + /** + * Other control functions + * */ + bool activateKey(const std::string &password, int keyno); bool deactivate(); - bool changePassword(const std::string &old_password, const std::string &new_password); + bool changePassword(const std::string &oldPassword, const std::string &newPassword); bool setDeviceLabel(const std::string &password, const std::string &label); bool setKeyname(const std::string &password, int keyno, const std::string &keyname); bool setKey(const std::string &password, int keyno, usbk_keysize_t keysize, const std::string &key); @@ -60,18 +77,19 @@ class UsbkDevice : public USBK std::string getRandomKey(usbk_keysize_t keysize) const; bool refreshUsbkInfo(); - static bool isDebugEnabled(); - static void setDebug(bool enabled); - static std::string libusbkVersion(); + friend std::ostream &operator<<(std::ostream &out, UsbkDevice &device); private: - bool m_debugEnabled; + USBK *m_usbk_device; }; -/*class UsbkDeviceList : public std::vector +class UsbkDeviceList : public std::vector { +public: + UsbkDeviceList(); -};*/ + friend std::ostream &operator<<(std::ostream &out, UsbkDeviceList &device_list); +}; #endif /* LIBUSBK_H_ */ diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c index 0498ba2..a479a15 100644 --- a/libusbk/src/libusbk.c +++ b/libusbk/src/libusbk.c @@ -33,19 +33,78 @@ #include "libusbk.h" #include "usbk_scsi.h" +/* terminal color definitions */ #if defined(__linux__) - #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "\033[0;32;40m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "\033[1;3;31m%s(%d):%s:\033[0;37;40m" x "\033[0;37;40m\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} - #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ - else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} -#elif defined(WIN32) - #define DBG_INFO(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_ERROR(x, ...) {if(debug_enable) {fprintf(stderr, "%s(%d):%s:" x "\n", "libusbk",__LINE__, __FUNCTION__, ## __VA_ARGS__);}} - #define DBG_RETURN_STRING(x) {if (x!=0) DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]);} - #define DBG_LASTOPR_STRING(x) {if (x!=0) {DBG_ERROR("last operation status: %s", lastopr_string[x].rtrn_string);} \ - else {DBG_INFO("last operation status: %s", lastopr_string[x].rtrn_string);}} -#endif +# define _COLOR_NO_COLOR "\e[0m" +# define _COLOR_WHITE "\e[1;37m" +# define _COLOR_YELLOW "\e[1;33m" +# define _COLOR_CYAN "\e[0;36m" +# define _COLOR_BLACK "\e[0;30m" +# define _COLOR_BLUE "\e[0;34m" +# define _COLOR_GREEN "\e[0;32m" +# define _COLOR_RED "\e[0;31m" +# define _COLOR_PURPLE "\e[0;35m" +# define _COLOR_BROWN "\e[0;33m" +# define _COLOR_LIGHTGRAY "\e[0;37m" +# define _COLOR_LIGHTPURPLE "\e[1;35m" +# define _COLOR_LIGHTRED "\e[1;31m" +# define _COLOR_LIGHTCYAN "\e[1;36m" +# define _COLOR_LIGHTGREEN "\e[1;32m" +# define _COLOR_LIGHTBLUE "\e[1;34m" +# define _COLOR_DARKGRAY "\e[1;30m" +#else +# define _COLOR_NO_COLOR +# define _COLOR_WHITE +# define _COLOR_YELLOW +# define _COLOR_CYAN +# define _COLOR_BLACK +# define _COLOR_BLUE +# define _COLOR_GREEN +# define _COLOR_RED +# define _COLOR_PURPLE +# define _COLOR_BROWN +# define _COLOR_LIGHTGRAY +# define _COLOR_LIGHTPURPLE +# define _COLOR_LIGHTRED +# define _COLOR_LIGHTCYAN +# define _COLOR_LIGHTGREEN +# define _COLOR_LIGHTBLUE +# define _COLOR_DARKGRAY +#endif /* defined(__linux__) */ + +#define DBG_INFO(message, ...) \ +{ \ + if (_libusbk_debug_enabled) \ + fprintf(stderr, _COLOR_LIGHTGRAY "%s::%s:%04d: " \ + _COLOR_GREEN message \ + _COLOR_NO_COLOR "\n", \ + "libusbk", __FUNCTION__, __LINE__, \ + ## __VA_ARGS__);\ +} + +#define DBG_ERROR(message, ...) \ +{ \ + if (_libusbk_debug_enabled) \ + fprintf(stderr, _COLOR_LIGHTGRAY "%s::%s:%04d: " \ + _COLOR_RED message \ + _COLOR_NO_COLOR "\n", \ + "libusbk", __FUNCTION__, __LINE__, \ + ## __VA_ARGS__); \ +} + +#define DBG_RETURN_STRING(x) \ +{ \ + if (x!=0) { DBG_ERROR("%s", USBK_RTN_ERRORS_STRING[x]); } \ +} + +#define DBG_LASTOPR_STRING(x) \ +{ \ + if (x!=0) { \ + DBG_ERROR("last operation status: %s", lastopr_string[x].retval_string); \ + } else { \ + DBG_INFO("last operation status: %s", lastopr_string[x].retval_string); \ + } \ +} //-DEFINES FOR FINDING USBK AND BACKDISK #define USBK_USB_IDVENDOR "2384" @@ -61,12 +120,10 @@ return - usbk->lastopr; \ } -typedef struct __RTRN_STRING { - int rtrn; - const char *rtrn_string; -} RTRN_STRING; - -RTRN_STRING lastopr_string[] = { +struct { + int retval; + const char *retval_string; +} lastopr_string[] = { {USBK_LO_PASS, "Ok" }, {USBK_LO_GEN_FAIL, "fail" }, {USBK_LO_FAILED_PASS, "wrong password" }, @@ -81,57 +138,58 @@ RTRN_STRING lastopr_string[] = { {USBK_LO_SCSI_ERROR, "scsi error" }, {USBK_LO_UNSUPPORTED_USBK, "unsupported usbk" }, {USBK_LO_INVALID_KEY, "invalid key" }, -#if defined(__linux__) +#if defined(__linux__) {USBK_LO_UDEV_ERROR, "udev error" }, #endif {USBK_LO_MEM_ERROR, "memory allocation error"} }; -typedef struct { +struct LIBUSBK_SUPPORTED_MODELS{ const char* model; bool support; -} LIBUSBK_SUPPORTED_MODELS; - -LIBUSBK_SUPPORTED_MODELS models_1_1[] = { +} models_1_1[] = { { "A101", true }, { "A103", true }, { NULL, false } }; -typedef struct { +struct LIBUSBK_SUPPORTED_VERSIONS{ int major_version; int minor_version; - LIBUSBK_SUPPORTED_MODELS* models; -} LIBUSBK_SUPPORTED_VERSIONS; - -LIBUSBK_SUPPORTED_VERSIONS versions_1[] = { + struct LIBUSBK_SUPPORTED_MODELS* models; +} versions_1[] = { { 2, 5, models_1_1 }, { 0, 0, NULL } }; -typedef struct { +struct LIBUSBK_SUPPORTED_PRODUCTS { const char* product; - LIBUSBK_SUPPORTED_VERSIONS* versions; -} LIBUSBK_SUPPORTED_PRODUCTS; - -LIBUSBK_SUPPORTED_PRODUCTS products[] = { + struct LIBUSBK_SUPPORTED_VERSIONS* versions; +} products[] = { { "USBK CryptoBridge 2.0", versions_1 }, { "USBK CryptoBridge", versions_1 }, { NULL, NULL } }; -static bool debug_enable = false; - -static int _get_udev_info(USBK* usbk, const char *device); -static int _get_udev_backdisk(USBK* usbk); -static int _command_status(USBK* usbk); - #if 0 static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, usbk_keysize_t keysize); #endif +static int _command_status(USBK* usbk); static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize); -static unsigned int _keysize_as_byte(usbk_keysize_t keysize); + +#if defined(__linux__) +static int _get_udev_info(USBK* usbk, const char *device); +static int _get_udev_backdisk(USBK* usbk); +#endif + +static bool _libusbk_debug_enabled = false; +static const char* _libusbk_version = USBK_LIB_VERSION_FULL; + +/* Debugging informations */ +void libusbk_enable_debug() { _libusbk_debug_enabled = true; } +void libusbk_disable_debug() { _libusbk_debug_enabled = false; } +const char* libusbk_version() { return _libusbk_version; } USBK* usbk_new(const char* dev) { @@ -139,12 +197,7 @@ USBK* usbk_new(const char* dev) int i; USBK* usbk = (USBK*) calloc(1, sizeof(USBK)); - if (usbk == NULL) { - free(usbk); - DBG_LASTOPR_STRING(USBK_LO_UDEV_ERROR); - - return NULL; - } + assert(usbk != NULL); #if defined(__linux__) // get information about usbk by udev @@ -174,17 +227,19 @@ USBK* usbk_new(const char* dev) return NULL; } - usbk->product = strdup(usbk_info.product.s); - usbk->model = strdup(usbk_info.model.s); - usbk->firmware_ver = strdup(usbk_info.firmware_ver.s); - usbk->is_supported = true; + /** fill usbk struct elements */ + usbk->product = strdup(usbk_info.product.s); + usbk->model = strdup(usbk_info.model.s); + usbk->firmware_ver = strdup(usbk_info.firmware_ver.s); + usbk->is_supported = true; - usbk->multikey_cap = usbk_info.multikeycap; - usbk->current_key = usbk_info.current_keyno; - usbk->autoact_keyno = usbk_info.autoactivate_keyno; - usbk->retry_num = usbk_info.retry_num; - usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; - usbk->dev_label = strdup(usbk_info.devlabel.s); + usbk->multikey_cap = usbk_info.multikeycap; + usbk->current_key = usbk_info.current_keyno; + usbk->autoact_keyno = usbk_info.autoactivate_keyno; + usbk->retry_num = usbk_info.retry_num; + usbk->dev_state = (usbk_ds_t) usbk_info.devstate.me; + usbk->dev_label = strdup(usbk_info.devlabel.s); + /* --- */ /* FIXME why we need this much space */ usbk->serial = (char*) calloc((sizeof(usbk_info.serial) * 2) + 2, sizeof(char)); @@ -235,7 +290,7 @@ int usbk_key_activate(USBK* usbk, const char* password, uint8_t key_no) USBK_CHECK_SUPPORTED(usbk) - if (key_no > usbk->multikey_cap) { + if (key_no < 0 || key_no > usbk->multikey_cap) { usbk->lastopr = USBK_LO_INVALID_KEYNO; ret = - usbk->lastopr; @@ -261,6 +316,8 @@ int usbk_key_activate(USBK* usbk, const char* password, uint8_t key_no) goto done; } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ usbk->lastopr = _command_status(usbk); break; @@ -303,6 +360,8 @@ int usbk_key_deactivate(USBK* usbk) goto done; } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ usbk->lastopr = _command_status(usbk); break; @@ -351,6 +410,8 @@ int usbk_change_password(USBK* usbk, const char* old_pass, const char* new_pass) goto done; } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ usbk->lastopr = _command_status(usbk); break; @@ -391,6 +452,8 @@ int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device_label) goto done; } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ usbk->lastopr = _command_status(usbk); break; @@ -441,7 +504,7 @@ int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const cha if ((key_size != USBK_KEYSIZE_NULL) && (key != NULL)) { setkey.options.me = SETKEY_NAME_AND_KEY; setkey.keysize.me = (e_UIP_KEYSIZE) key_size; - memcpy(setkey.key.u8, key, _keysize_as_byte(key_size)); + memcpy(setkey.key.u8, key, usbk_keysize_as_byte(key_size)); } else { setkey.options.me = SETKEY_NAMEONLY; } @@ -458,6 +521,8 @@ int usbk_set_key_and_keyname(USBK* usbk, const char *pass, int key_no, const cha goto done; } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ usbk->lastopr = _command_status(usbk); break; @@ -554,6 +619,8 @@ int usbk_set_autact(USBK* usbk, const char *pass, int key_no) goto done; } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ usbk->lastopr = _command_status(usbk); break; @@ -581,8 +648,8 @@ int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) USBK_CHECK_SUPPORTED(usbk) - unsigned int keysize_inbyte = _keysize_as_byte(keysize); - if ((keysize_inbyte == 0) || (keysize_inbyte > _keysize_as_byte(USBK_KEYSIZE_256BIT))) { + int keysize_inbyte = usbk_keysize_as_byte(keysize); + if ((keysize_inbyte == 0) || (keysize_inbyte > usbk_keysize_as_byte(USBK_KEYSIZE_256BIT))) { usbk->lastopr = USBK_LO_INVALID_KEYSIZE; ret = - usbk->lastopr; @@ -602,7 +669,9 @@ int usbk_get_randomkey(USBK* usbk, uint8_t* random_key, usbk_keysize_t keysize) goto done; } + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ usbk->lastopr = _command_status(usbk); + if (usbk->lastopr == USBK_LO_PASS) { memcpy(random_key, genkey.key.u8, keysize_inbyte); } @@ -667,7 +736,7 @@ static int _command_status(USBK* usbk) usbk->lastopr = USBK_LO_SCSI_ERROR; DBG_LASTOPR_STRING(usbk->lastopr); - return - usbk->lastopr; + return - usbk->lastopr; } usbk->retry_num = status.retry_num; @@ -687,7 +756,7 @@ static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, u // 6) 16 adet sayı cikartabildin mi? // 7) sayılar 0 ile 255 arasinda mi? - unsigned int key_len = _keysize_as_byte(keysize); + unsigned int key_len = usbk_keysize_as_byte(keysize); int i; /* check key_text if it is null or not */ @@ -729,7 +798,7 @@ static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, u key_decimal_str[found] = ' '; found = key_decimal_str.find_first_of(".", found + 1); } - if (i != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; + if (i != (usbk_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; //cout << str << endl; // string icerisinde bosluklar haric decimal olmayan karakter var mi? @@ -749,12 +818,12 @@ static int _convert_key_decimal2hex(uint8_t *key_hex, const char* key_decimal, u // sayılar 0 ile 255 arasinda mi? istringstream iss(key_decimal_str, istringstream::in); i = 0; - for (unsigned int n = 0; n < _keysize_as_byte(keysize); n++) { + for (unsigned int n = 0; n < usbk_keysize_as_byte(keysize); n++) { iss >> ikey[n]; if (iss.fail() || iss.bad()) return USBK_LO_INVALID_KEY; - if (iss.eof() && n != (_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; + if (iss.eof() && n != (usbk_keysize_as_byte(keysize) - 1)) return USBK_LO_INVALID_KEY; if (ikey[n] > 255 || ikey[n] < 0) { i++; @@ -781,7 +850,7 @@ static inline int16_t _xtod(const char c) static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_keysize_t keysize) { - unsigned int key_len = _keysize_as_byte(keysize); + int key_len = usbk_keysize_as_byte(keysize); int i; /* check key_text if it is null or not */ @@ -808,7 +877,7 @@ static int _convert_key_text2hex(uint8_t *key_hex, const char* key_text, usbk_ke return USBK_LO_PASS; } -static unsigned int _keysize_as_byte(usbk_keysize_t keysize) +unsigned int usbk_keysize_as_byte(usbk_keysize_t keysize) { unsigned int size = 0; @@ -1150,28 +1219,4 @@ int usbk_list_get_count(USBK_LIST *usbk_list) #endif -void usbk_debug_enable(void) -{ - debug_enable = true; -} - -void usbk_debug_disable(void) -{ - debug_enable = false; -} - -void usbk_debug(bool enable) -{ - debug_enable = enable; -} - -bool usbk_debug_check(void) -{ - return debug_enable; -} - -const char* usbk_libversion(void) -{ - return USBK_LIB_VERSION_FULL; -} diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index e3c4000..2303531 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -32,6 +32,10 @@ extern "C" { # error unsupported environment #endif +#if defined(__GNUC__) +# define inline inline __attribute__((always_inline)) +#endif + /* * usbk keysize type values * @@ -191,6 +195,9 @@ struct USBK }; typedef struct USBK USBK; +/* + * FIXME: do we really need these macros? + * */ #define USBK_ASSIGN_PASSWORD(usbk, new_pass) \ usbk_changepassword(usbk, NULL, new_pass) #define USBK_SETKEY_NAME(usbk, pass, key_no, key_name) \ @@ -223,27 +230,17 @@ typedef struct USBK USBK; #define USBK_DISABLE_AUTACT(usbk, pass) \ usbk_set_autact(usbk, pass, 0) +/* ----------------------------------------------- */ + extern USBK* usbk_new(const char* dev); extern int usbk_release(USBK* usbk); -/* - * All the getter functions - * */ - -/* This is the helper macro for reaching the USBK structure */ -#define USBK_GET_ELEMENT(usbk, element) (usbk->element) - -#if defined(__GNUC__) -# define inline inline __attribute__((always_inline)) -#endif - /* Debugging informations */ -extern void usbk_debug_enable(void); -extern void usbk_debug_disable(void); -extern void usbk_debug(bool enable); -extern bool usbk_debug_check(void); +extern void libusbk_enable_debug(); +extern void libusbk_disable_debug(); -extern const char* usbk_libversion(void); +/* libusbk version */ +extern const char* libusbk_version(); /* * And all the setter and other functions which interacts @@ -257,6 +254,7 @@ extern int usbk_set_devicelabel(USBK* usbk, const char* pass, const char* device extern int usbk_set_keyname(USBK* usbk, const char* pass, uint8_t key_no, const char* key_name); extern int usbk_set_key_hex(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const uint8_t* key); extern int usbk_set_key_text(USBK* usbk, const char* pass, uint8_t key_no, usbk_keysize_t key_size, const char* key); +extern unsigned int usbk_keysize_as_byte(usbk_keysize_t keysize); #if 0 /* FIXME depreciated function */ @@ -268,6 +266,13 @@ extern int usbk_set_autact(USBK* usbk, const char* pass, int key_no); extern int usbk_get_randomkey(USBK* usbk, uint8_t*random_key, usbk_keysize_t keysize); extern int usbk_refresh_usbkinfo(USBK* usbk); +/* + * All the getter functions + * */ + +/* This is the helper macro for reaching the USBK structure */ +#define USBK_GET_ELEMENT(usbk, element) (usbk->element) + inline const char* usbk_get_dev(USBK* usbk) { return USBK_GET_ELEMENT(usbk, dev_node); } inline const char* usbk_get_dev_path(USBK* usbk) diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index 489585f..230650e 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -170,7 +170,7 @@ int main(int argc, char *argv[]) { } if (Dflag) { - usbk_debug_enable(); + libusbk_enable_debug(); } if (question_flag) { diff --git a/usbk_cli_pp/CMakeLists.txt b/usbk_cli_pp/CMakeLists.txt new file mode 100644 index 0000000..2f156fc --- /dev/null +++ b/usbk_cli_pp/CMakeLists.txt @@ -0,0 +1,108 @@ +# +# Build file of USBK CryptoBridge +# +# Tamara Electronics (C) 2010 +# +# GNU General Public License +# + +# Project name +PROJECT (usbk_cli_pp C CXX) + +# CMAKE Settings +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(CMAKE_COLOR_MAKEFILE ON) +SET(CMAKE_VERBOSE_MAKEFILE OFF) + +# The USBK_CLI version number. +IF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) +SET(${PROJECT_NAME}_VERSION_MAJOR 1) +SET(${PROJECT_NAME}_VERSION_MINOR 2) +SET(${PROJECT_NAME}_VERSION_PATCH 2) +ELSE (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) +SET(${PROJECT_NAME}_VERSION_MAJOR ${${CMAKE_PROJECT_NAME}_VERSION_MAJOR}) +SET(${PROJECT_NAME}_VERSION_MINOR ${${CMAKE_PROJECT_NAME}_VERSION_MINOR}) +SET(${PROJECT_NAME}_VERSION_PATCH ${${CMAKE_PROJECT_NAME}_VERSION_PATCH}) +ENDIF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) + +SET(${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}") +SET(${PROJECT_NAME}_VERSION_FULL "${${PROJECT_NAME}_VERSION}.${${PROJECT_NAME}_VERSION_PATCH}") +ADD_DEFINITIONS ( -DVERSION="${${PROJECT_NAME}_VERSION_FULL}" ) +ADD_DEFINITIONS ( -DPACKAGE="${CMAKE_PROJECT_NAME}") + + +# Source Files +AUX_SOURCE_DIRECTORY (${${PROJECT_NAME}_SOURCE_DIR}/src ${PROJECT_NAME}_SOURCES) + +# Header files are located in these directories +SET (${PROJECT_NAME}_INCLUDE_DIRS + ${${PROJECT_NAME}_SOURCE_DIR}/../libusbk/src + ${${PROJECT_NAME}_SOURCE_DIR}/../libusbk++/src + ${CMAKE_SYSTEM_INCLUDE_PATH} +) +INCLUDE_DIRECTORIES (${${PROJECT_NAME}_INCLUDE_DIRS}) + + +# Libraries are located in these directories +SET (${PROJECT_NAME}_LIBRARY_DIRS + ${CMAKE_CURRENT_BINARY_DIR}/../libusbk/ + ${CMAKE_CURRENT_BINARY_DIR}/../libusbk++/ + ${CMAKE_SYSTEM_LIBRARY_PATH} +) +LINK_DIRECTORIES ( ${${PROJECT_NAME}_LIBRARY_DIRS}) + +# Depend Includes +SET (${PROJECT_NAME}_INCLUDES + libusbk.h + getopt.h +) + +# Depend Internal Libraries +SET (${PROJECT_NAME}_LIBRARIES_INTERNAL + usbk + usbk++ +) + +# Depend External Libraries +SET (${PROJECT_NAME}_LIBRARIES_EXTERNAL +) + +# Depend All Libraries +SET (${PROJECT_NAME}_LIBRARIES + ${${PROJECT_NAME}_LIBRARIES_INTERNAL} + ${${PROJECT_NAME}_LIBRARIES_EXTERNAL} +) + +# Create project +ADD_EXECUTABLE (${PROJECT_NAME} ${${PROJECT_NAME}_SOURCES}) + +# These are the symbols for the linker +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${${PROJECT_NAME}_LIBRARIES}) + +SET_TARGET_PROPERTIES(${PROJECT_NAME} + PROPERTIES OUTPUT_NAME usbk++) + +# Install project +INSTALL(TARGETS ${PROJECT_NAME} + RUNTIME + DESTINATION /usr/bin + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + + +IF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) + # Settings for Debian Package + SET(CPACK_GENERATOR "DEB") + SET(CPACK_DEBIAN_PACKAGE_NAME ${PROJECT_NAME}) + SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Tamara Elektronik") + SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "USBK CryptoBridge CLI") + SET(CPACK_DEBIAN_PACKAGE_DESCRIPTION " USBK CryptoBridge CLI\n USBK CryptoBridge encrypts data independent by the operation systems and all filesystems.\n USBK works in SCSI level and encrypts/decrypts data on the fly. USBK has two model as A101\n and A103. A103 model uses 3 keys and A101 model uses 1 key. A password for this operation.\n Whenever user inserts USB-K into USB Hub of the PC, USB-K must be initialized (there is an\n exceptional situation!).") + SET(CPACK_PACKAGE_VERSION "${${PROJECT_NAME}_VERSION}") + SET(CPACK_DEBIAN_PACKAGE_VERSION "${${PROJECT_NAME}_VERSION}") + SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "i386") + SET(CPACK_DEBIAN_PACKAGE_DEPENDS "usbk (>= 0.0)") + SET(CPACK_DEBIAN_PACKAGE_SECTION "devel") + SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") + SET(CPACK_DEBIAN_PACKAGE_RECOMMENDS "recommends") + SET(CPACK_DEBIAN_PACKAGE_SUGGESTS "suggests") + INCLUDE(CPack) +ENDIF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) diff --git a/usbk_cli_pp/src/main.cpp b/usbk_cli_pp/src/main.cpp new file mode 100644 index 0000000..a7829db --- /dev/null +++ b/usbk_cli_pp/src/main.cpp @@ -0,0 +1,753 @@ + /* + * @file main.cpp + * + * Copyright (C) 2010 USB-K Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * See http://www.gnu.org/licenses/ for more information + * + **************************************************************************** + * Main function of USB-K crypro device + * + */ + +//TODO: BackDisk mount edilmiş ise deactive işlemi sırasında kullanıcıya uyarı ver ve emin misin diye sor. FORCE EKLENEBİLİR + +#include +#include +#include + +#include +#include + +#include "../../libusbk/src/libusbk.h" +#include "../../libusbk++/src/libusbk.hpp" + +using namespace std; + +#define MSG_PASS "Pass.\n" +#define MSG_GEN_FAIL "Fail.\n" +#define MSG_FAILED_PASS "Failed Password, Retry Number = %d.\n", myusbk->info.retry_num +#define MSG_FABRIC_RESET "\n"\ + "***********************************************\n"\ + " All keys and your password is erased. \n"\ + " Please, re-configure your device. \n"\ + "***********************************************\n"\ + "\n" +#define MSG_USBK_UNPLUGING "Must remove. Please remove and re-plug the USBK.\n" +#define MSG_INVALID_KEYNO "Invalid Key Number.\n" +#define MSG_INVALID_KEYSIZE "Invalid Key Size.\n" +#define MSG_INVALID_DEVICELABEL "Invalid Device Label\n" +#define MSG_INVALID_NEWPASS "Invalid New Password.\n" + +#define MSG_DSE_ACTIVATE "USBK in active. This operation is not able to done in this state." +#define MSG_DSE_ACTIVATE_WITH_BACKDISK "The backdisk is plugged to USBK. This operation is not able to done in this state." +#define MSG_DSE_DEACTIVATE "USBK in deactive. This operation is not able to done in this state." +#define MSG_DSE_FABRIC_DEFAULT "USBK in fabric default. Please first set your password." +#define MSG_DSE_MUST_REMOVE "USBK in must remove. This operation is not able to done in this state." +#define MSG_DSE_UNKNOWN "The state of USBK is unknown. This operation is not able to done in this state." + +#define MSG_SCSI_ERROR "No Device Found (scsi error).\n" +#define MSG_UNSUPPORTED_USBK "Unsupported USBK.\n" +#define MSG_INVALID_KEY "Invalid Key.\n" +#define MSG_UDEV_ERROR "No Device Found (udev error).\n" +#define MSG_MEM_ERROR "No Device Found (mem error).\n" +#define MSG_UNKOWN_ERROR "Unkown Error.\n" + + +//MESSAGES OF LINUX CLI +#define ARG_PARSE_ERROR "Parse error\n" +#define MISSING_PARAMETER "Missing parameter(s)\n" +#define MAIN_OPERTAION_MORE "You may not specify more than one `-adcnmxXtTls' option\n" +#define TRY_HELP "Try `usbk --help' for more information.\n" +#define MUST_SUPERUSER "requested operation requires superuser privilege\n" +#define MISSING_PARAM_DEV "Dev paramter missing\n" + + +//PRIVATE FUNCTION DECLARATIONS +static int _parse_options(int *argc, char** argv[]); +usbk_keysize_t parse_keysize(char *s); +int parse_keysize_inbyte(char *s); + +static void linuxcli_show_devices(void); +static void linuxcli_show_dev_info(UsbkDevice* device); + +static void print_result(USBK* myusbk); +static void print_result(UsbkDevice* device); + +static void print_help(int exval); +static void print_version(void); +static bool check_superuser(void); + +//PRIVATE VARIABLES +//-FLAGS +int verbose_flag; +int uflag = 0; +int aflag = 0; +int dflag = 0; +int cflag = 0; +int Dflag = 0; +int nflag = 0; +int mflag = 0; +int xflag = 0; +int Xflag = 0; +int tflag = 0; +int Tflag = 0; +int lflag = 0; +int sflag = 0; +int kflag = 0; +int pflag = 0; +int fflag = 0; +int Fflag = 0; +int iflag = 0; +int vflag = 0; +int question_flag = 0; + +int main_operation = 0; +//-OTHERS +int opt_key; +int opt_key_size_byte; +char *opt_parola = NULL; +char *opt_dev_label; +char *opt_aes_name; +char *opt_new_password; +char *opt_key_size_str; +char opt_key_format; +char usbk_dev[1024]; +char opt_string_key[128]; +char opt_aes_key[1024]; +char set_key[1024]; +//USBK_INFO *usbk_info; + +static struct option long_options[] = + { + /* These options set a flag. */ + { "dev", required_argument, 0, 'u' }, + /* These options don't set a flag. We distinguish them by their indices. */ + { "activate", no_argument, 0, 'a' }, + { "deactivate", no_argument, 0, 'd' }, + { "debug", no_argument, 0, 'D' }, + { "newpass", required_argument, 0, 'c' }, + { "label", required_argument, 0, 'n' }, + { "keyname", required_argument, 0, 'm' }, + { "change-key", required_argument, 0, 'x' }, + { "change-key-with-random", no_argument, 0, 'X' }, + { "enable-auto", no_argument, 0, 't' }, + { "disable-auto", no_argument, 0, 'T' }, + { "gen-key", no_argument, 0, 'l' }, + { "show-devices", no_argument, 0, 's' }, + { "key-no", required_argument, 0, 'k' }, + { "passwd", required_argument, 0, 'p' }, + { "key-format", required_argument, 0, 'f' }, + { "key-size", required_argument, 0, 'F' }, + { "show-info", no_argument, 0, 'i' }, + { "version", no_argument, 0, 'v' }, + { "help", no_argument, 0, '?' }, + { NULL, 0, NULL, 0 } // Last element must be NULL +}; + + +//ALL FUNCTIONS + +//!-\brief Main Function +//! \param argc command line argument count +//! \param argv argument list +//! \return 0 normal exit +//! \return 1 abnormal exit +int main(int argc, char *argv[]) { + + int c; + int status; + + kflag = 1; opt_key = 1; // default key no 1 + fflag = 1; opt_key_format = 'd'; // default key format decimal + Fflag = 1; opt_key_size_str = strdup("256"); + + if (!_parse_options(&argc, &argv)) { + fprintf(stderr, ARG_PARSE_ERROR); + exit(0); + } + + if (Dflag) { + libusbk_enable_debug(); + libusbk_plusplus_enable_debug(); + } + + if (question_flag) { + print_help(0); + exit(0); + } + + if (vflag) { + print_version(); + exit(0); + } + + if (main_operation > 1) { + fprintf(stderr, MAIN_OPERTAION_MORE); + fprintf(stderr, TRY_HELP); + exit(1); + } + + if (!iflag && main_operation == 0) { + exit(0); + } + + if (check_superuser() == false) { + fprintf(stderr, MUST_SUPERUSER); + exit(0); + } + + if (sflag) { + linuxcli_show_devices(); + exit(0); + } + + if (uflag == 0) { + fprintf(stderr, MISSING_PARAM_DEV); + fprintf(stderr, TRY_HELP); + exit(0); + } + + /* FIXME: write exception handlers */ + UsbkDevice *device = new UsbkDevice(usbk_dev); + + if (device->lastOperation() != USBK_LO_PASS) { + std::cerr << __LINE__ << ": device " << device->deviceNode() << " sucked!"; + //print_result(myusbk); + //usbk_release(myusbk); + + exit(0); + } + + int ret; + + ///////////////////////////////////////////// + // ACTIVATE + ///////////////////////////////////////////// + if (pflag & aflag & kflag) { + ret = device->activateKey(opt_parola, opt_key); + printf("%d\n", ret); + print_result(device); + } + + ///////////////////////////////////////////// + // DEACTIVATE + ///////////////////////////////////////////// + if (dflag) { + ret = device->deactivate(); + printf("%d\n", ret); + print_result(device); + } + + ///////////////////////////////////////////// + // CHANGE PASSWORD + ///////////////////////////////////////////// + if (cflag) { + ret = device->changePassword(opt_parola, opt_new_password); + printf("%d\n", ret); + print_result(device); + } + + ///////////////////////////////////////////// + // SET DEVICE NAME + ///////////////////////////////////////////// + if (pflag & nflag) { + ret = device->setDeviceLabel(opt_parola, opt_dev_label); + printf("%d\n", ret); + print_result(device); + } + +#if 0 + ///////////////////////////////////////////// + // SET KEY + ///////////////////////////////////////////// + + /* FIXME: setting keys will be rehandled! */ + + if (pflag & xflag & kflag & Fflag) { + switch (opt_key_format) { + case 'd': + printf("ERROR: Depreciated function!\n"); + ret=-1; + + //res = usbk_setkey_decimal(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), opt_string_key); + break; + case 't': + ret = usbk_set_key_text(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), opt_string_key); + break; + default: + break; + } + printf("%d\n", ret); + print_result(myusbk); + } + + ///////////////////////////////////////////// + // SET KEY RANDOMLY + ///////////////////////////////////////////// + /*if (pflag & Xflag & kflag & Fflag) { + int i; + uint8_t randomkey[32]; + ret = usbk_get_randomkey(myusbk, randomkey, parse_keysize(opt_key_size_str)); + + if (ret == USBK_LO_PASS) { + ret = usbk_set_key_hex(myusbk, opt_parola, opt_key, parse_keysize(opt_key_size_str), randomkey); + if (ret == USBK_LO_PASS) { + for (i = 0; i < parse_keysize_inbyte(opt_key_size_str); i++) { + fprintf(stdout, "%d", randomkey[i]); + if (i != (parse_keysize_inbyte(opt_key_size_str) - 1)) + fprintf(stdout, "."); + } + printf("\n"); + } + } + printf("%d\n", ret); + print_result(myusbk); + }*/ + + ///////////////////////////////////////////// + // SET KEY NAME ONLY + ///////////////////////////////////////////// + if (mflag & !xflag & !Xflag) { + if (pflag & kflag) { + ret = usbk_set_keyname(myusbk, opt_parola, opt_key, opt_aes_name); + printf("%d\n", ret); + print_result(myusbk); + } + } + + ///////////////////////////////////////////// + // ENABLE AUTO ACTIVATE + ///////////////////////////////////////////// + if (pflag & tflag & kflag) { + ret = USBK_ENABLE_AUTACT(myusbk, opt_parola, opt_key); + printf("%d\n", ret); + print_result(myusbk); + } + + ///////////////////////////////////////////// + // DISABLE AUTO ACTIVATE + ///////////////////////////////////////////// + if (pflag & Tflag) { + ret = USBK_DISABLE_AUTACT(myusbk, opt_parola); + printf("%d\n", ret); + print_result(myusbk); + } +#endif + + ///////////////////////////////////////////// + // SHOW USBK INFORMATION + ///////////////////////////////////////////// + if (iflag) { + linuxcli_show_dev_info(device); + } + + delete device; + + exit(0); +} + +usbk_keysize_t parse_keysize(char *s){ + if (!strcmp(s, "128")) { + return USBK_KEYSIZE_128BIT; + } else if (!strcmp(s, "192")) { + return USBK_KEYSIZE_192BIT; + } else if (!strcmp(s, "256")) { + return USBK_KEYSIZE_256BIT; + } else { + return USBK_KEYSIZE_NULL; + } +} + +int parse_keysize_inbyte(char *s){ + if (!strcmp(s, "128")) { + return 16; + } else if (!strcmp(s, "192")) { + return 24; + } else if (!strcmp(s, "256")) { + return 32; + } else { + return 0; + } +} + +static int _parse_options(int *argc, char** argv[]) { + /* getopt_long stores the option index here. */ + int option_index = 0; + int opt; + + if (*argc == 1) + { + question_flag = 1; + } + else + { + while (( opt = getopt_long(*argc, *argv, "u:adc:n:m:x:XtTlsk:p:F:f:ivD?", long_options, &option_index)) != -1) { + switch (opt) { + case 0: + /* If this option set a flag, do nothing else now. */ + if (long_options[option_index].flag != 0) + break; + printf("option %s", long_options[option_index].name); + if (optarg) printf(" with arg %s", optarg); + printf("\n"); + break; + + case 'u': + uflag = 1; + strncpy (usbk_dev ,optarg, sizeof (usbk_dev)); + break; + case 'a': + aflag = 1; + main_operation++; + break; + + case 'd': + dflag = 1; + main_operation++; + break; + + case 'c': + cflag = 1; + main_operation++; + opt_new_password = strdup(optarg); + break; + + case 'n': + nflag = 1; + main_operation++; + opt_dev_label = strdup(optarg); + break; + + case 'm': + mflag = 1; + if (!Xflag && !xflag) main_operation++; + opt_aes_name = strdup(optarg); + break; + + case 'x': + xflag = 1; + if (!mflag) main_operation++; + sprintf(opt_string_key, "%s", optarg); + break; + + case 'X': + Xflag = 1; + if (!mflag) main_operation++; + break; + + case 't': + tflag = 1; + main_operation++; + break; + + case 'T': + Tflag = 1; + main_operation++; + break; + + case 'l': + lflag = 1; + main_operation++; + break; + + case 's': + sflag = 1; + main_operation++; + break; + + case 'k': + kflag = 1; + opt_key = atoi(optarg); + break; + + case 'p': + pflag = 1; + opt_parola = strdup(optarg); + break; + + case 'f': + fflag = 1; + if (!strcmp(optarg, "d")) { + opt_key_format = 'd'; + } else if (!strcmp(optarg, "t")) { + opt_key_format = 't'; + } else { + fprintf(stderr, MISSING_PARAMETER); + exit(1); + } + break; + + case 'F': + Fflag = 1; + opt_key_size_str = strdup(optarg); + break; + case 'i': + iflag = 1; + break; + + case 'D': + Dflag = 1; + break; + + case 'v': + vflag = 1; + break; + + case '?': + question_flag = 1; + break; + default: + break; + } + } + + /* Instead of reporting ‘--verbose’ + and ‘--brief’ as they are encountered, + we report the final status resulting from them. */ + if (verbose_flag) + fprintf(stdout, "verbose flag is set"); + + } + + return 1; +} + +void linuxcli_show_devices(void) +{ + UsbkDeviceList deviceList; + + std::cout << deviceList; +} + +void linuxcli_show_dev_info(UsbkDevice* myusbk) +{ + std::cout << *myusbk; +} + +static void print_result(USBK* myusbk) +{ + switch (usbk_get_lastopr_status(myusbk)) { + case USBK_LO_PASS: + fprintf(stdout, "Pass.\n"); + break; + case USBK_LO_GEN_FAIL: + fprintf(stderr, "Fail.\n"); + break; + case USBK_LO_FAILED_PASS: + fprintf(stderr, "Failed Password, Retry Number = %d.\n", usbk_get_retry_number(myusbk)); + break; + case USBK_LO_FABRIC_RESET: + printf("\n"); + printf("***********************************************\n"); + printf(" All keys and your password is erased. \n"); + printf(" Please, re-configure your device. \n"); + printf("***********************************************\n"); + printf("\n"); + break; + case USBK_LO_USBK_UNPLUGING: + fprintf(stderr, "Must remove. Please remove and re-plug the USBK.\n"); + break; + case USBK_LO_INVALID_KEYNO: + fprintf(stderr, "Invalid Key Number.\n"); + break; + case USBK_LO_INVALID_KEYSIZE: + fprintf(stderr, "Invalid Key Size.\n"); + break; + case USBK_LO_INVALID_DEVICELABEL: + fprintf(stderr, "Invalid Device Label\n"); + break; + case USBK_LO_INVALID_NEWPASS: + fprintf(stderr, "Invalid New Password.\n"); + break; + case USBK_LO_STATE_ERROR: + + switch (usbk_get_state(myusbk)) { + case USBK_DS_ACTIVATE: + fprintf(stderr, MSG_DSE_ACTIVATE); + break; + case USBK_DS_ACTIVATE_WITH_BACKDISK: + fprintf(stderr, MSG_DSE_ACTIVATE_WITH_BACKDISK); + break; + case USBK_DS_DEACTIVATE: + fprintf(stderr, MSG_DSE_DEACTIVATE); + break; + case USBK_DS_FABRIC_DEFAULT: + fprintf(stderr, MSG_DSE_FABRIC_DEFAULT); + break; + case USBK_DS_MUST_REMOVE: + fprintf(stderr, MSG_DSE_MUST_REMOVE); + break; + default: + fprintf(stderr, MSG_DSE_UNKNOWN); + break; + } + + break; + case USBK_LO_SCSI_ERROR: + fprintf(stderr, MSG_SCSI_ERROR); + break; + case USBK_LO_UNSUPPORTED_USBK: + fprintf(stderr, MSG_UNSUPPORTED_USBK); + break; + case USBK_LO_INVALID_KEY: + fprintf(stderr, MSG_INVALID_KEY); + break; + case USBK_LO_UDEV_ERROR: + fprintf(stderr, MSG_UDEV_ERROR); + break; + case USBK_LO_MEM_ERROR: + fprintf(stderr, MSG_MEM_ERROR); + break; + case USBK_LO_INVALID_PASS: + default: + fprintf(stderr, MSG_MEM_ERROR); + break; + } +} + +static void print_result(UsbkDevice* device) +{ + switch (device->lastOperation()) { + case USBK_LO_PASS: + fprintf(stdout, "Pass.\n"); + break; + case USBK_LO_GEN_FAIL: + fprintf(stderr, "Fail.\n"); + break; + case USBK_LO_FAILED_PASS: + fprintf(stderr, "Failed Password, Retry Number = %d.\n", device->retryNumber()); + break; + case USBK_LO_FABRIC_RESET: + printf("\n"); + printf("***********************************************\n"); + printf(" All keys and your password is erased. \n"); + printf(" Please, re-configure your device. \n"); + printf("***********************************************\n"); + printf("\n"); + break; + case USBK_LO_USBK_UNPLUGING: + fprintf(stderr, "Must remove. Please remove and re-plug the USBK.\n"); + break; + case USBK_LO_INVALID_KEYNO: + fprintf(stderr, "Invalid Key Number.\n"); + break; + case USBK_LO_INVALID_KEYSIZE: + fprintf(stderr, "Invalid Key Size.\n"); + break; + case USBK_LO_INVALID_DEVICELABEL: + fprintf(stderr, "Invalid Device Label\n"); + break; + case USBK_LO_INVALID_NEWPASS: + fprintf(stderr, "Invalid New Password.\n"); + break; + case USBK_LO_STATE_ERROR: + + switch (device->deviceState()) { + case USBK_DS_ACTIVATE: + fprintf(stderr, MSG_DSE_ACTIVATE); + break; + case USBK_DS_ACTIVATE_WITH_BACKDISK: + fprintf(stderr, MSG_DSE_ACTIVATE_WITH_BACKDISK); + break; + case USBK_DS_DEACTIVATE: + fprintf(stderr, MSG_DSE_DEACTIVATE); + break; + case USBK_DS_FABRIC_DEFAULT: + fprintf(stderr, MSG_DSE_FABRIC_DEFAULT); + break; + case USBK_DS_MUST_REMOVE: + fprintf(stderr, MSG_DSE_MUST_REMOVE); + break; + default: + fprintf(stderr, MSG_DSE_UNKNOWN); + break; + } + + break; + case USBK_LO_SCSI_ERROR: + fprintf(stderr, MSG_SCSI_ERROR); + break; + case USBK_LO_UNSUPPORTED_USBK: + fprintf(stderr, MSG_UNSUPPORTED_USBK); + break; + case USBK_LO_INVALID_KEY: + fprintf(stderr, MSG_INVALID_KEY); + break; + case USBK_LO_UDEV_ERROR: + fprintf(stderr, MSG_UDEV_ERROR); + break; + case USBK_LO_MEM_ERROR: + fprintf(stderr, MSG_MEM_ERROR); + break; + case USBK_LO_INVALID_PASS: + default: + fprintf(stderr, MSG_MEM_ERROR); + break; + } +} + +static void print_help(int exval) { + print_version(); + printf("" + "Usage: usbk [OPTION...]\n" + "\n" + "Examples:\n" + " usbk -s # Show device list\n" + " usbk sdc -a -k 1 -p foo # activate device with key 1\n" + " usbk sdc -d # deactivate device\n" + "\n" + " Main operation mode:\n" + "\n" + " -u, --dev device\n" + " -a, --activate activate device\n" + " -d, --deactivate deactivate device\n" + " -c, --newpasswd=NEWPASS change the password to NEWPASS\n" + " -n, --label=LABEL change the label to LABEL\n" + " -m, --keyname=KEYNAME change then key nameto NAME\n" + " -x, --change-key=NEWKEY change the key to NEWKEY\n" + " -X, --change-key-with-random change the key to random key\n" + " -t, --enable-auto enable auto activate\n" + " -T, --disable-auto disable auto activate\n" + " -l, --gen-key generate and set random key\n" + " -s, --show-devices show device list\n" + "\n" + " Setting options:\n" + "\n" + " -k, --key-no=KEYNO use KEYNO as key number\n" + " -p, --passwd=PASSWD checks password with PASSWD\n" + " -F, --key-size=KEY_SIZE KEYSIZE is 128, 192 or 256\n" + " -f, --key-format=FORMAT FORMAT=t for text or FORMAT=d for\n" + " decimal input. default is decimal\n" + "\n" + " Other options:\n" + "\n" + " -i, --show-info show device info\n" + " -D, --debug print debug output\n" + " -v, --version print program version\n" + " -?, --help give this help list\n" + "\n" + "defaults for options:\n" + "--key-size=256\n" + "--key-format=d\n" + "--key-no=1\n" + "\n"); + exit(exval); +} + +static void print_version(void) { + fprintf(stdout, "iyi iyi\n"); +} + +static bool check_superuser(void){ + if(geteuid() == 0){ + return true; + }else{ + return false; + } +} From 6ea2bbc13ffa22f9c17bbcfabaee0e2647b64598 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Tue, 25 Oct 2011 00:54:09 +0300 Subject: [PATCH 14/16] usbkcli++ reimplementation and libusbk++ enhancements. --- .gitignore | 1 + libusbk++/src/config.hpp.cmake | 8 +- libusbk++/src/libusbk.cpp | 4 +- libusbk++/src/libusbk.hpp | 1 - libusbk/src/config.h.cmake | 6 +- libusbk/src/libusbk.c | 2 + libusbk/src/libusbk.h | 2 - usbk_cli_pp/CMakeLists.txt | 5 +- usbk_cli_pp/src/config.hpp.cmake | 15 ++ usbk_cli_pp/src/main.cpp | 377 ++++++++++++------------------- 10 files changed, 170 insertions(+), 251 deletions(-) create mode 100644 usbk_cli_pp/src/config.hpp.cmake diff --git a/.gitignore b/.gitignore index 69dee56..60d4a49 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ CPackSourceConfig.cmake # compiled files *.o *.so* +*~ usbk_cli/usbk usbk_cli_pp/usbk++ config.h diff --git a/libusbk++/src/config.hpp.cmake b/libusbk++/src/config.hpp.cmake index 551a826..ba78c71 100644 --- a/libusbk++/src/config.hpp.cmake +++ b/libusbk++/src/config.hpp.cmake @@ -1,14 +1,14 @@ /** * * @project @PROJECT_NAME@ - * @file config.h + * @file config.hpp * */ -#ifndef CONFIG_HPP_ -#define CONFIG_HPP_ +#ifndef LIBUSBK_PP_CONFIG_HPP_ +#define LIBUSBK_PP_CONFIG_HPP_ #define USBK_CPP_LIB_VERSION_FULL "@USBK_LIB_CPP_BINDING_VERSION_FULL@" -#endif // CONFIG_HPP_ +#endif // LIBUSBK_PP_CONFIG_HPP_ diff --git a/libusbk++/src/libusbk.cpp b/libusbk++/src/libusbk.cpp index 73c8477..e5713ff 100644 --- a/libusbk++/src/libusbk.cpp +++ b/libusbk++/src/libusbk.cpp @@ -17,6 +17,7 @@ * */ +#include "config.hpp" #include "libusbk.hpp" #include @@ -286,7 +287,7 @@ std::ostream &operator<<(std::ostream &out, UsbkDevice &device) (i == device.currentKey() - 1 && (device.deviceState() == USBK_DS_ACTIVATE || device.deviceState() == USBK_DS_ACTIVATE_WITH_BACKDISK) - ? "[*]" : "[ ]"); + ? "[*]" : " "); out << " Key #" << i+1 << " , Name " << device.keyName(i); @@ -296,7 +297,6 @@ std::ostream &operator<<(std::ostream &out, UsbkDevice &device) return out; } - UsbkDeviceList::UsbkDeviceList() { USBK_LIST *_usbk_list = usbk_list_new(); diff --git a/libusbk++/src/libusbk.hpp b/libusbk++/src/libusbk.hpp index ede792b..d954bdb 100644 --- a/libusbk++/src/libusbk.hpp +++ b/libusbk++/src/libusbk.hpp @@ -18,7 +18,6 @@ #ifndef LIBUSBK_HPP_ #define LIBUSBK_HPP_ -#include "config.hpp" #include "../../libusbk/src/libusbk.h" #include diff --git a/libusbk/src/config.h.cmake b/libusbk/src/config.h.cmake index 3a0ca86..7797c7d 100644 --- a/libusbk/src/config.h.cmake +++ b/libusbk/src/config.h.cmake @@ -5,10 +5,10 @@ * */ -#ifndef CONFIG_H_ -#define CONFIG_H_ +#ifndef LIBUSBK_CONFIG_H_ +#define LIBUSBK_CONFIG_H_ #define USBK_LIB_VERSION_FULL "@USBK_LIB_VERSION_FULL@" -#endif // CONFIG_H_ +#endif // LIBUSBK_CONFIG_H_ diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c index a479a15..163a68c 100644 --- a/libusbk/src/libusbk.c +++ b/libusbk/src/libusbk.c @@ -17,6 +17,8 @@ * */ +#include "config.h" + #if defined(__linux__) # include # include diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index 2303531..55d74ed 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -21,8 +21,6 @@ extern "C" { #endif -#include "config.h" - #if defined(__AVR32__) || defined(__linux__) # include # include diff --git a/usbk_cli_pp/CMakeLists.txt b/usbk_cli_pp/CMakeLists.txt index 2f156fc..6aa046b 100644 --- a/usbk_cli_pp/CMakeLists.txt +++ b/usbk_cli_pp/CMakeLists.txt @@ -7,7 +7,7 @@ # # Project name -PROJECT (usbk_cli_pp C CXX) +PROJECT (USBK_CLI_PP C CXX) # CMAKE Settings CMAKE_MINIMUM_REQUIRED(VERSION 2.6) @@ -27,9 +27,8 @@ ENDIF (${CMAKE_PROJECT_NAME} MATCHES ${PROJECT_NAME}) SET(${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}") SET(${PROJECT_NAME}_VERSION_FULL "${${PROJECT_NAME}_VERSION}.${${PROJECT_NAME}_VERSION_PATCH}") -ADD_DEFINITIONS ( -DVERSION="${${PROJECT_NAME}_VERSION_FULL}" ) -ADD_DEFINITIONS ( -DPACKAGE="${CMAKE_PROJECT_NAME}") +CONFIGURE_FILE(src/config.hpp.cmake src/config.hpp) # Source Files AUX_SOURCE_DIRECTORY (${${PROJECT_NAME}_SOURCE_DIR}/src ${PROJECT_NAME}_SOURCES) diff --git a/usbk_cli_pp/src/config.hpp.cmake b/usbk_cli_pp/src/config.hpp.cmake new file mode 100644 index 0000000..dff9699 --- /dev/null +++ b/usbk_cli_pp/src/config.hpp.cmake @@ -0,0 +1,15 @@ +/** + * + * @project @PROJECT_NAME@ + * @file config.hpp + * + */ + +#ifndef USBK_CLI_CONFIG_HPP_ +#define USBK_CLI_CONFIG_HPP_ + +#define USBK_CLI_VERSION "@USBK_CLI_PP_VERSION_FULL@" +#define USBK_CLI_PACKAGE "@CMAKE_PROJECT_NAME@" + +#endif // USBK_CLI_CONFIG_HPP_ + diff --git a/usbk_cli_pp/src/main.cpp b/usbk_cli_pp/src/main.cpp index a7829db..55aba34 100644 --- a/usbk_cli_pp/src/main.cpp +++ b/usbk_cli_pp/src/main.cpp @@ -17,71 +17,33 @@ //TODO: BackDisk mount edilmiş ise deactive işlemi sırasında kullanıcıya uyarı ver ve emin misin diye sor. FORCE EKLENEBİLİR +#include "config.hpp" + #include #include #include #include +#include #include #include "../../libusbk/src/libusbk.h" #include "../../libusbk++/src/libusbk.hpp" -using namespace std; - -#define MSG_PASS "Pass.\n" -#define MSG_GEN_FAIL "Fail.\n" -#define MSG_FAILED_PASS "Failed Password, Retry Number = %d.\n", myusbk->info.retry_num -#define MSG_FABRIC_RESET "\n"\ - "***********************************************\n"\ - " All keys and your password is erased. \n"\ - " Please, re-configure your device. \n"\ - "***********************************************\n"\ - "\n" -#define MSG_USBK_UNPLUGING "Must remove. Please remove and re-plug the USBK.\n" -#define MSG_INVALID_KEYNO "Invalid Key Number.\n" -#define MSG_INVALID_KEYSIZE "Invalid Key Size.\n" -#define MSG_INVALID_DEVICELABEL "Invalid Device Label\n" -#define MSG_INVALID_NEWPASS "Invalid New Password.\n" - -#define MSG_DSE_ACTIVATE "USBK in active. This operation is not able to done in this state." -#define MSG_DSE_ACTIVATE_WITH_BACKDISK "The backdisk is plugged to USBK. This operation is not able to done in this state." -#define MSG_DSE_DEACTIVATE "USBK in deactive. This operation is not able to done in this state." -#define MSG_DSE_FABRIC_DEFAULT "USBK in fabric default. Please first set your password." -#define MSG_DSE_MUST_REMOVE "USBK in must remove. This operation is not able to done in this state." -#define MSG_DSE_UNKNOWN "The state of USBK is unknown. This operation is not able to done in this state." - -#define MSG_SCSI_ERROR "No Device Found (scsi error).\n" -#define MSG_UNSUPPORTED_USBK "Unsupported USBK.\n" -#define MSG_INVALID_KEY "Invalid Key.\n" -#define MSG_UDEV_ERROR "No Device Found (udev error).\n" -#define MSG_MEM_ERROR "No Device Found (mem error).\n" -#define MSG_UNKOWN_ERROR "Unkown Error.\n" - - -//MESSAGES OF LINUX CLI -#define ARG_PARSE_ERROR "Parse error\n" -#define MISSING_PARAMETER "Missing parameter(s)\n" -#define MAIN_OPERTAION_MORE "You may not specify more than one `-adcnmxXtTls' option\n" -#define TRY_HELP "Try `usbk --help' for more information.\n" -#define MUST_SUPERUSER "requested operation requires superuser privilege\n" -#define MISSING_PARAM_DEV "Dev paramter missing\n" - - //PRIVATE FUNCTION DECLARATIONS static int _parse_options(int *argc, char** argv[]); + +// FIXME: remove these and find another way to deal with it. usbk_keysize_t parse_keysize(char *s); int parse_keysize_inbyte(char *s); static void linuxcli_show_devices(void); static void linuxcli_show_dev_info(UsbkDevice* device); -static void print_result(USBK* myusbk); static void print_result(UsbkDevice* device); -static void print_help(int exval); -static void print_version(void); -static bool check_superuser(void); +static void print_help(); +static void print_version(); //PRIVATE VARIABLES //-FLAGS @@ -168,58 +130,59 @@ int main(int argc, char *argv[]) { Fflag = 1; opt_key_size_str = strdup("256"); if (!_parse_options(&argc, &argv)) { - fprintf(stderr, ARG_PARSE_ERROR); - exit(0); - } - - if (Dflag) { - libusbk_enable_debug(); - libusbk_plusplus_enable_debug(); + std::cerr << "Parse Error!"; + exit(EXIT_FAILURE); } if (question_flag) { - print_help(0); - exit(0); + print_help(); + exit(EXIT_SUCCESS); } if (vflag) { print_version(); - exit(0); + exit(EXIT_SUCCESS); + } + + if (Dflag) { + libusbk_enable_debug(); + libusbk_plusplus_enable_debug(); } if (main_operation > 1) { - fprintf(stderr, MAIN_OPERTAION_MORE); - fprintf(stderr, TRY_HELP); - exit(1); + std::cerr + << "You may not specify more than one `-adcnmxXtTls' option\n" + << "Try `usbk --help' for more information.\n"; + exit(EXIT_FAILURE); } if (!iflag && main_operation == 0) { - exit(0); + exit(EXIT_SUCCESS); } - if (check_superuser() == false) { - fprintf(stderr, MUST_SUPERUSER); - exit(0); + if (geteuid() != 0) { + std::cerr << "Requested operation requires superuser privilege\n"; + exit(EXIT_FAILURE); } if (sflag) { linuxcli_show_devices(); - exit(0); + exit(EXIT_SUCCESS); } if (uflag == 0) { - fprintf(stderr, MISSING_PARAM_DEV); - fprintf(stderr, TRY_HELP); - exit(0); + std::cerr + << "Dev paramter missing\n" + << "Try `usbk --help' for more information.\n"; + exit(EXIT_SUCCESS); } /* FIXME: write exception handlers */ UsbkDevice *device = new UsbkDevice(usbk_dev); if (device->lastOperation() != USBK_LO_PASS) { - std::cerr << __LINE__ << ": device " << device->deviceNode() << " sucked!"; - //print_result(myusbk); - //usbk_release(myusbk); + print_result(device); + delete device; exit(0); } @@ -228,6 +191,7 @@ int main(int argc, char *argv[]) { ///////////////////////////////////////////// // ACTIVATE + // FIXME: ask password from command line ///////////////////////////////////////////// if (pflag & aflag & kflag) { ret = device->activateKey(opt_parola, opt_key); @@ -381,12 +345,9 @@ static int _parse_options(int *argc, char** argv[]) { int option_index = 0; int opt; - if (*argc == 1) - { + if (*argc == 1) { question_flag = 1; - } - else - { + } else { while (( opt = getopt_long(*argc, *argv, "u:adc:n:m:x:XtTlsk:p:F:f:ivD?", long_options, &option_index)) != -1) { switch (opt) { case 0: @@ -478,7 +439,7 @@ static int _parse_options(int *argc, char** argv[]) { } else if (!strcmp(optarg, "t")) { opt_key_format = 't'; } else { - fprintf(stderr, MISSING_PARAMETER); + std::cerr << "Missing parameter!\n"; exit(1); } break; @@ -530,224 +491,168 @@ void linuxcli_show_dev_info(UsbkDevice* myusbk) std::cout << *myusbk; } -static void print_result(USBK* myusbk) +static void print_result(UsbkDevice* device) { - switch (usbk_get_lastopr_status(myusbk)) { + std::string result; + + switch (device->lastOperation()) { case USBK_LO_PASS: - fprintf(stdout, "Pass.\n"); + result = "Operation Successful"; break; + case USBK_LO_GEN_FAIL: - fprintf(stderr, "Fail.\n"); + result = "Operation Failed!"; break; + case USBK_LO_FAILED_PASS: - fprintf(stderr, "Failed Password, Retry Number = %d.\n", usbk_get_retry_number(myusbk)); - break; - case USBK_LO_FABRIC_RESET: - printf("\n"); - printf("***********************************************\n"); - printf(" All keys and your password is erased. \n"); - printf(" Please, re-configure your device. \n"); - printf("***********************************************\n"); - printf("\n"); - break; - case USBK_LO_USBK_UNPLUGING: - fprintf(stderr, "Must remove. Please remove and re-plug the USBK.\n"); - break; - case USBK_LO_INVALID_KEYNO: - fprintf(stderr, "Invalid Key Number.\n"); - break; - case USBK_LO_INVALID_KEYSIZE: - fprintf(stderr, "Invalid Key Size.\n"); - break; - case USBK_LO_INVALID_DEVICELABEL: - fprintf(stderr, "Invalid Device Label\n"); - break; - case USBK_LO_INVALID_NEWPASS: - fprintf(stderr, "Invalid New Password.\n"); - break; - case USBK_LO_STATE_ERROR: + { + std::stringstream s; - switch (usbk_get_state(myusbk)) { - case USBK_DS_ACTIVATE: - fprintf(stderr, MSG_DSE_ACTIVATE); - break; - case USBK_DS_ACTIVATE_WITH_BACKDISK: - fprintf(stderr, MSG_DSE_ACTIVATE_WITH_BACKDISK); - break; - case USBK_DS_DEACTIVATE: - fprintf(stderr, MSG_DSE_DEACTIVATE); - break; - case USBK_DS_FABRIC_DEFAULT: - fprintf(stderr, MSG_DSE_FABRIC_DEFAULT); - break; - case USBK_DS_MUST_REMOVE: - fprintf(stderr, MSG_DSE_MUST_REMOVE); - break; - default: - fprintf(stderr, MSG_DSE_UNKNOWN); - break; - } + s << "Password Incorrect, Retry Number = " + << device->retryNumber(); - break; - case USBK_LO_SCSI_ERROR: - fprintf(stderr, MSG_SCSI_ERROR); - break; - case USBK_LO_UNSUPPORTED_USBK: - fprintf(stderr, MSG_UNSUPPORTED_USBK); - break; - case USBK_LO_INVALID_KEY: - fprintf(stderr, MSG_INVALID_KEY); - break; - case USBK_LO_UDEV_ERROR: - fprintf(stderr, MSG_UDEV_ERROR); - break; - case USBK_LO_MEM_ERROR: - fprintf(stderr, MSG_MEM_ERROR); - break; - case USBK_LO_INVALID_PASS: - default: - fprintf(stderr, MSG_MEM_ERROR); - break; - } -} + result = s.str(); + } -static void print_result(UsbkDevice* device) -{ - switch (device->lastOperation()) { - case USBK_LO_PASS: - fprintf(stdout, "Pass.\n"); - break; - case USBK_LO_GEN_FAIL: - fprintf(stderr, "Fail.\n"); - break; - case USBK_LO_FAILED_PASS: - fprintf(stderr, "Failed Password, Retry Number = %d.\n", device->retryNumber()); break; + case USBK_LO_FABRIC_RESET: - printf("\n"); - printf("***********************************************\n"); - printf(" All keys and your password is erased. \n"); - printf(" Please, re-configure your device. \n"); - printf("***********************************************\n"); - printf("\n"); + result = + "****************************************\n" + " All keys and your password is erased. \n" + " Please, re-configure your device. \n" + "****************************************"; break; + case USBK_LO_USBK_UNPLUGING: - fprintf(stderr, "Must remove. Please remove and re-plug the USBK.\n"); + result = "USBK must be removed and replugged."; break; + case USBK_LO_INVALID_KEYNO: - fprintf(stderr, "Invalid Key Number.\n"); + result = "Invalid Key Number."; break; + case USBK_LO_INVALID_KEYSIZE: - fprintf(stderr, "Invalid Key Size.\n"); + result = "Invalid Key Size."; break; + case USBK_LO_INVALID_DEVICELABEL: - fprintf(stderr, "Invalid Device Label\n"); + result = "Invalid Device Label"; break; + case USBK_LO_INVALID_NEWPASS: - fprintf(stderr, "Invalid New Password.\n"); + result = "Invalid New Password."; break; + case USBK_LO_STATE_ERROR: switch (device->deviceState()) { case USBK_DS_ACTIVATE: - fprintf(stderr, MSG_DSE_ACTIVATE); + result = "USBK in active. This operation is not able to done in this state."; break; + case USBK_DS_ACTIVATE_WITH_BACKDISK: - fprintf(stderr, MSG_DSE_ACTIVATE_WITH_BACKDISK); + result = "The backdisk is plugged to USBK. This operation is not able to done in this state."; break; + case USBK_DS_DEACTIVATE: - fprintf(stderr, MSG_DSE_DEACTIVATE); + result = "USBK in deactive. This operation is not able to done in this state."; break; + case USBK_DS_FABRIC_DEFAULT: - fprintf(stderr, MSG_DSE_FABRIC_DEFAULT); + result = "USBK in fabric default. Please first set your password."; break; + case USBK_DS_MUST_REMOVE: - fprintf(stderr, MSG_DSE_MUST_REMOVE); + result = "USBK in must remove. This operation is not able to done in this state."; break; + default: - fprintf(stderr, MSG_DSE_UNKNOWN); + result = "The state of USBK is unknown. This operation is not able to done in this state."; break; } break; case USBK_LO_SCSI_ERROR: - fprintf(stderr, MSG_SCSI_ERROR); + result = "SCSI Error! No Device Found!"; break; + case USBK_LO_UNSUPPORTED_USBK: - fprintf(stderr, MSG_UNSUPPORTED_USBK); + result = "Unsupported USBK."; break; + case USBK_LO_INVALID_KEY: - fprintf(stderr, MSG_INVALID_KEY); + result = "Invalid Key."; break; + case USBK_LO_UDEV_ERROR: - fprintf(stderr, MSG_UDEV_ERROR); + result = "UDEV Error! No Device Found."; break; + case USBK_LO_MEM_ERROR: - fprintf(stderr, MSG_MEM_ERROR); + result = "Mem Error! No Device Found."; break; + case USBK_LO_INVALID_PASS: + result = "Invalid Password."; + break; + default: - fprintf(stderr, MSG_MEM_ERROR); + result = "Unknown error!"; break; } -} -static void print_help(int exval) { - print_version(); - printf("" - "Usage: usbk [OPTION...]\n" - "\n" - "Examples:\n" - " usbk -s # Show device list\n" - " usbk sdc -a -k 1 -p foo # activate device with key 1\n" - " usbk sdc -d # deactivate device\n" - "\n" - " Main operation mode:\n" - "\n" - " -u, --dev device\n" - " -a, --activate activate device\n" - " -d, --deactivate deactivate device\n" - " -c, --newpasswd=NEWPASS change the password to NEWPASS\n" - " -n, --label=LABEL change the label to LABEL\n" - " -m, --keyname=KEYNAME change then key nameto NAME\n" - " -x, --change-key=NEWKEY change the key to NEWKEY\n" - " -X, --change-key-with-random change the key to random key\n" - " -t, --enable-auto enable auto activate\n" - " -T, --disable-auto disable auto activate\n" - " -l, --gen-key generate and set random key\n" - " -s, --show-devices show device list\n" - "\n" - " Setting options:\n" - "\n" - " -k, --key-no=KEYNO use KEYNO as key number\n" - " -p, --passwd=PASSWD checks password with PASSWD\n" - " -F, --key-size=KEY_SIZE KEYSIZE is 128, 192 or 256\n" - " -f, --key-format=FORMAT FORMAT=t for text or FORMAT=d for\n" - " decimal input. default is decimal\n" - "\n" - " Other options:\n" - "\n" - " -i, --show-info show device info\n" - " -D, --debug print debug output\n" - " -v, --version print program version\n" - " -?, --help give this help list\n" - "\n" - "defaults for options:\n" - "--key-size=256\n" - "--key-format=d\n" - "--key-no=1\n" - "\n"); - exit(exval); + std::cout << result << std::endl; } -static void print_version(void) { - fprintf(stdout, "iyi iyi\n"); +static void print_help() +{ + print_version(); + std::cout + << std::endl << "USBK CryptoBridge Configurator" + << std::endl << "\tUsage: usbk [options]" + << std::endl + << std::endl << "Main operations:" + << std::endl << " -u, --dev device" + << std::endl << " -a, --activate activate device" + << std::endl << " -d, --deactivate deactivate device" + << std::endl << " -c, --newpasswd=NEWPASS change the password to NEWPASS" + << std::endl << " -n, --label=LABEL change the label to LABEL" + << std::endl << " -m, --keyname=KEYNAME change then key nameto NAME" + << std::endl << " -x, --change-key=NEWKEY change the key to NEWKEY" + << std::endl << " -X, --change-key-with-random change the key to random key" + << std::endl << " -t, --enable-auto enable auto activate" + << std::endl << " -T, --disable-auto disable auto activate" + << std::endl << " -l, --gen-key generate and set random key" + << std::endl << " -s, --show-devices show device list" + << std::endl + << std::endl << " Setting options:" + << std::endl + << std::endl << " -k, --key-no=KEYNO use KEYNO as key number" + << std::endl << " -p, --passwd=PASSWD checks password with PASSWD" + << std::endl << " -F, --key-size=KEY_SIZE KEYSIZE is 128, 192 or 256" + << std::endl << " -f, --key-format=FORMAT FORMAT=t for text or FORMAT=d for" + << std::endl + << std::endl << " Other options:\n" + << std::endl + << std::endl << " -i, --show-info show device info" + << std::endl << " -D, --debug print debug output" + << std::endl << " -v, --version print program version" + << std::endl << " -?, --help give this help list" + << std::endl + << std::endl << "defaults for options:" + << std::endl << "--key-size=256" + << std::endl << "--key-format=d" + << std::endl << "--key-no=1" + << std::endl + << std::endl << "Examples:" + << std::endl << " usbk -s # Show device list" + << std::endl << " usbk sdc -a -k 1 -p foo # activate device with key 1" + << std::endl << " usbk sdc -d # deactivate device" + << std::endl << std::endl; } -static bool check_superuser(void){ - if(geteuid() == 0){ - return true; - }else{ - return false; - } +static void print_version(void) +{ + std::cout << USBK_CLI_PACKAGE << " " << USBK_CLI_VERSION << std::endl; } From 8de0d31974a3efd4e36abf7fb4fc98c6cc88ddbb Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Thu, 27 Oct 2011 23:51:41 +0300 Subject: [PATCH 15/16] add todo --- TODO | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index c0b681e..c88d727 100644 --- a/TODO +++ b/TODO @@ -1,12 +1,12 @@ -urgent -====== - - some api enhancement required for making usbk c++ api. this api will - be used in qusbk. - - make libusbk fully c source since no need to use c++ -====== +TODO +==== + - Write unit tests for each function both for libusbk and libusbk++ - - make variables fixed sizes (eg. use int32_t or int64_t instead of int (except file descriptors) ) - - - c++ bindings can be written, but first qusbk project must be started! + - libusbk enhancement + . make struct USBK fully abstracted (as before) + . prevent segfaults caused by root access + . write more controls for NULL pointer addressing (still we have some problems) + - libusbk++ enhancements + . same as libusbk From 956243617e661b60ad8bd8922bd4f1499c87b177 Mon Sep 17 00:00:00 2001 From: Mehmet Emre Atasever Date: Thu, 27 Oct 2011 23:53:34 +0300 Subject: [PATCH 16/16] fix including headers for development By the time developing libusbk++ and usbk_cli, if there is a header installed in the system, sometimes that header might be included instead of most current version of libusbk.h{,pp}. This will be fixed soon, but we wrote relative path as temporary. --- libusbk++/src/libusbk.hpp | 2 +- usbk_cli/src/main.cpp | 2 +- usbk_cli_pp/src/main.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libusbk++/src/libusbk.hpp b/libusbk++/src/libusbk.hpp index d954bdb..43f1391 100644 --- a/libusbk++/src/libusbk.hpp +++ b/libusbk++/src/libusbk.hpp @@ -18,7 +18,7 @@ #ifndef LIBUSBK_HPP_ #define LIBUSBK_HPP_ -#include "../../libusbk/src/libusbk.h" +#include #include #include diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index 230650e..62154fb 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -23,7 +23,7 @@ #include #include -#include "../../libusbk/src/libusbk.h" +#include using namespace std; diff --git a/usbk_cli_pp/src/main.cpp b/usbk_cli_pp/src/main.cpp index 55aba34..5b333da 100644 --- a/usbk_cli_pp/src/main.cpp +++ b/usbk_cli_pp/src/main.cpp @@ -27,8 +27,8 @@ #include #include -#include "../../libusbk/src/libusbk.h" -#include "../../libusbk++/src/libusbk.hpp" +#include +#include //PRIVATE FUNCTION DECLARATIONS static int _parse_options(int *argc, char** argv[]);