diff --git a/.gitignore b/.gitignore index 87345f5..60d4a49 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,11 @@ CPackSourceConfig.cmake # compiled files *.o *.so* +*~ usbk_cli/usbk +usbk_cli_pp/usbk++ +config.h +config.hpp # vim swap files *.swp diff --git a/CMakeLists.txt b/CMakeLists.txt index df76b98..da202ec 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,7 +24,11 @@ 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( usbk_cli_pp ) # temporary test cli + ADD_SUBDIRECTORY( man ) UNINSTALLLER_FEATURE() @@ -54,6 +58,8 @@ IF (UNIX) cmake.check_depends CMakeCache.txt */CMakeCache.txt + install_manifest.txt + cmake.check_cache *.cmake Makefile @@ -63,7 +69,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 ) @@ -75,6 +83,7 @@ IF (UNIX) ARGS -Rf CMakeTmp ${DISTCLEANED} TARGET distclean ) + ENDIF(UNIX) # Settings for debian package diff --git a/TODO b/TODO new file mode 100644 index 0000000..c88d727 --- /dev/null +++ b/TODO @@ -0,0 +1,12 @@ +TODO +==== + - Write unit tests for each function both for libusbk and libusbk++ + + - 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 + diff --git a/libusbk++/CMakeLists.txt b/libusbk++/CMakeLists.txt new file mode 100644 index 0000000..866e660 --- /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 usbk++ + ) + + # 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.hpp + 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..ba78c71 --- /dev/null +++ b/libusbk++/src/config.hpp.cmake @@ -0,0 +1,14 @@ +/** + * + * @project @PROJECT_NAME@ + * @file 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 // LIBUSBK_PP_CONFIG_HPP_ + diff --git a/libusbk++/src/libusbk.cpp b/libusbk++/src/libusbk.cpp new file mode 100644 index 0000000..e5713ff --- /dev/null +++ b/libusbk++/src/libusbk.cpp @@ -0,0 +1,333 @@ +/* + * @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 "config.hpp" +#include "libusbk.hpp" + +#include +#include +#include + +static bool _libusbk_plusplus_debug_enabled = false; +static const char* _libusbk_plusplus_version = USBK_CPP_LIB_VERSION_FULL; + +/* 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 *usbk_device = usbk_new(device_node.c_str()); + + if (!usbk_device) { + std::cerr << "no such device " << device_node; + + return; + } + + this->m_usbk_device = usbk_device; +} + +UsbkDevice::UsbkDevice(USBK *usbk_device) +{ + this->m_usbk_device = usbk_device; +} + +UsbkDevice::~UsbkDevice() +{ + usbk_release(m_usbk_device); +} + +usbk_lo_t UsbkDevice::lastOperation() const +{ + return usbk_get_lastopr_status(m_usbk_device); +} + +bool UsbkDevice::supported() const +{ + return usbk_check_support(m_usbk_device); +} + +std::string UsbkDevice::deviceNode() const +{ + return usbk_get_dev(m_usbk_device); +} + +std::string UsbkDevice::deviceNodePath() const +{ + return usbk_get_dev_path(m_usbk_device); +} + +std::string UsbkDevice::backdiskNode() const +{ + return usbk_get_backdisk(m_usbk_device); +} + +std::string UsbkDevice::backdiskNodePath() const +{ + return usbk_get_backdisk_path(m_usbk_device); +} + +std::string UsbkDevice::product() const +{ + return usbk_get_product(m_usbk_device); +} + +std::string UsbkDevice::model() const +{ + return usbk_get_model(m_usbk_device); +} + +std::string UsbkDevice::serial() const +{ + return usbk_get_serial(m_usbk_device); +} + +std::string UsbkDevice::usbSerial() const +{ + return usbk_get_usb_serial(m_usbk_device); +} + +std::string UsbkDevice::firmwareVersion() const +{ + return usbk_get_firmware_ver(m_usbk_device); +} + +std::string UsbkDevice::deviceLabel() const +{ + return usbk_get_dev_label(m_usbk_device); +} + +uint8_t UsbkDevice::multikeyCapability() const +{ + return usbk_get_multikeycap(m_usbk_device); +} + +usbk_ds_t UsbkDevice::deviceState() const +{ + return usbk_get_state(m_usbk_device); +} + +int UsbkDevice::retryNumber() const +{ + return usbk_get_retry_number(m_usbk_device); +} + +int UsbkDevice::currentKey() const +{ + return usbk_get_current_keyno(m_usbk_device); +} + +int UsbkDevice::autoactivateKeyNo() const +{ + return usbk_get_autoactivation_keyno(m_usbk_device); +} + +std::string UsbkDevice::keyName(int keyno) const +{ + if (keyno < 0 || keyno > multikeyCapability() - 1) + return std::string(); + + return usbk_get_keyname(m_usbk_device, keyno); +} + +bool UsbkDevice::activateKey(const std::string &password, int keyno) +{ + return usbk_key_activate(m_usbk_device, password.c_str(), keyno); +} + +bool UsbkDevice::deactivate() +{ + return usbk_key_deactivate(m_usbk_device); +} + +bool UsbkDevice::changePassword(const std::string &oldPassword, const std::string &newPassword) +{ + return usbk_change_password(m_usbk_device, oldPassword.c_str(), newPassword.c_str()); +} + +bool UsbkDevice::setDeviceLabel(const std::string &password, const std::string &label) +{ + 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) +{ + 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) +{ + 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) +{ + return usbk_set_autact(m_usbk_device, password.c_str(), keyno); +} + +/* FIXME: not implemented yet */ +std::string UsbkDevice::getRandomKey(usbk_keysize_t keysize) const +{ + return std::string(); +} + +/* FIXME: not implemented yet */ +bool UsbkDevice::refreshUsbkInfo() +{ + int ret = usbk_refresh_usbkinfo(m_usbk_device); + + return (ret < 0) ? false : true; +} + +std::ostream &operator<<(std::ostream &out, UsbkDevice &device) +{ + device.refreshUsbkInfo(); + + std::string title; + std::string retryNum; + std::string backdisk = "-"; + std::string status; + std::string autoactive; + + if (device.supported()) { + title = "USBK Information "; + + 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(); + } + + if (device.deviceState()==USBK_DS_ACTIVATE) + backdisk = "none"; + else + backdisk = "Plugged In"; + + break; + + case USBK_DS_DEACTIVATE: + status = "deactive"; + break; + + case USBK_DS_FABRIC_DEFAULT: + status = "USBK in fabric default. Please first set your password."; + break; + + case USBK_DS_MUST_REMOVE: + status = "Must remove. Please remove and re-plug the USBK."; + break; + + default: + status = "Unknown Status "; + status += device.deviceState(); + break; + } + + autoactive = (device.autoactivateKeyNo() == 0) + ? "Disable" + : "Enabled key with #" + device.autoactivateKeyNo(); + retryNum = device.retryNumber(); + } else { + title = "Unknown Device!!!"; + + status = "n/a"; + autoactive = "n/a"; + backdisk = "n/a"; + retryNum = "n/a"; + } + + 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; + + out << std::endl << "\tMax. Key Capacity : "; + out << (int)device.multikeyCapability() << std::endl; + + for (int i=0; ipush_back(new_usbk_device); + } + + usbk_list_release(_usbk_list); +} + +std::ostream &operator<<(std::ostream &out, UsbkDeviceList &deviceList) +{ + out << "All USBK devices:" << std::endl; + + 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; + } + + out << deviceList.capacity() << " USBK devices found." << std::endl; + + return out; +} diff --git a/libusbk++/src/libusbk.hpp b/libusbk++/src/libusbk.hpp new file mode 100644 index 0000000..43f1391 --- /dev/null +++ b/libusbk++/src/libusbk.hpp @@ -0,0 +1,94 @@ +/* + * @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 + +#include +#include +#include + +/* 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 + * */ + + 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 retryNumber() const; + int currentKey() const; + int autoactivateKeyNo() const; + std::string keyName(int keyno) const; + + /** + * Other control functions + * */ + bool activateKey(const std::string &password, int keyno); + bool deactivate(); + 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); + bool setAutoactivateKeyno(const std::string &password, int keyno); + std::string getRandomKey(usbk_keysize_t keysize) const; + bool refreshUsbkInfo(); + + friend std::ostream &operator<<(std::ostream &out, UsbkDevice &device); + +private: + USBK *m_usbk_device; +}; + +class UsbkDeviceList : public std::vector +{ +public: + UsbkDeviceList(); + + friend std::ostream &operator<<(std::ostream &out, UsbkDeviceList &device_list); +}; + +#endif /* LIBUSBK_H_ */ + diff --git a/libusbk/CMakeLists.txt b/libusbk/CMakeLists.txt index 2c1fb68..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} - LIBRARY DESTINATION lib + 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 include + 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,52 +106,53 @@ 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) 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/libusbk/src/config.h.cmake b/libusbk/src/config.h.cmake new file mode 100644 index 0000000..7797c7d --- /dev/null +++ b/libusbk/src/config.h.cmake @@ -0,0 +1,14 @@ +/** + * + * @project @PROJECT_NAME@ + * @file config.h + * + */ + +#ifndef LIBUSBK_CONFIG_H_ +#define LIBUSBK_CONFIG_H_ + +#define USBK_LIB_VERSION_FULL "@USBK_LIB_VERSION_FULL@" + +#endif // LIBUSBK_CONFIG_H_ + diff --git a/libusbk/src/libusbk.c b/libusbk/src/libusbk.c new file mode 100644 index 0000000..163a68c --- /dev/null +++ b/libusbk/src/libusbk.c @@ -0,0 +1,1224 @@ +/* + * @file libusbk.c + * + * 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 "config.h" + +#if defined(__linux__) +# include +# include +# include +#endif + +#include +#include +#include +#include + +#include "uip.h" + +#include "libusbk.h" +#include "usbk_scsi.h" + +/* terminal color definitions */ +#if defined(__linux__) +# 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" +#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; \ + } + +struct { + int retval; + const char *retval_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"} +}; + +struct LIBUSBK_SUPPORTED_MODELS{ + const char* model; + bool support; +} models_1_1[] = { + { "A101", true }, + { "A103", true }, + { NULL, false } +}; + +struct LIBUSBK_SUPPORTED_VERSIONS{ + int major_version; + int minor_version; + struct LIBUSBK_SUPPORTED_MODELS* models; +} versions_1[] = { + { 2, 5, models_1_1 }, + { 0, 0, NULL } +}; + +struct LIBUSBK_SUPPORTED_PRODUCTS { + const char* product; + struct LIBUSBK_SUPPORTED_VERSIONS* versions; +} products[] = { + { "USBK CryptoBridge 2.0", versions_1 }, + { "USBK CryptoBridge", versions_1 }, + { NULL, NULL } +}; + +#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); + +#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) +{ + int ret; + int i; + + USBK* usbk = (USBK*) calloc(1, sizeof(USBK)); + assert(usbk != 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; + } + + /** 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); + /* --- */ + + /* 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 < 0 || 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; + } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ + 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; + } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ + 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; + } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ + 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; + } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ + 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, usbk_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; + } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ + 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; +} + +#if 0 +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; +} +#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(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; + } + + /* FIXME: POTENTIONAL BUG: _command_status function does not return USBK_LO_* values */ + 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) + + 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; + + 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; + } + + /* 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); + } + +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 = usbk_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 != (usbk_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 < usbk_keysize_as_byte(keysize); n++) { + iss >> ikey[n]; + + if (iss.fail() || iss.bad()) 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++; + } + 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) +{ + int key_len = usbk_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 + + diff --git a/libusbk/src/libusbk.cpp b/libusbk/src/libusbk.cpp deleted file mode 100644 index 87f5118..0000000 --- a/libusbk/src/libusbk.cpp +++ /dev/null @@ -1,1075 +0,0 @@ -/* - * @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. - * - */ - -//PRIVATE HEADERS -#include - -#if defined(__linux__) -# include -#endif - -#include -#include -#include - -#include "uip.h" - -#include "libusbk.h" -#include "usbk_scsi.h" - -using namespace std; - -#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);}} -#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);}} -#endif - -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 __LIBUSBK_SUPPORTED_MODELS { - const char* model; - 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 } -}; - -LIBUSBK_SUPPORTED_VERSIONS versions_1[] = { - { 2, 5, models_1_1 }, - { 0, 0, NULL } -}; - -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" - -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; - -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); - - -USBK* usbk_new(const char* dev) { - int i; - int rtn = 0; - - USBK* usbk = (USBK*)calloc(1, sizeof(USBK)); - if (usbk == NULL) - { - usbk->lastopr = USBK_LO_MEM_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return NULL; - } - -#if defined(__linux__) - // get information about usbk by udev - rtn = getudevinfo(usbk, dev); - if (rtn < 0) { - free(usbk); - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return NULL; - } -#elif defined(WIN32) - usbk->dev = strdup(dev); - usbk->dev_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); - if (rtn < 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->supported = true; - - if (usbk->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_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->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); - free(usbk->dev_path); - free(usbk->backdisk); - free(usbk->backdisk_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_activatekey(USBK* usbk, const char* password, uint8_t key_no) { - int rtn; - t_UIP_ACTIVATE activate; - - if (usbk->supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - if (key_no > usbk->multikey_cap) { - usbk->lastopr = USBK_LO_INVALID_KEYNO; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - switch (usbk->dev_state) { - case USBK_DS_DEACTIVATE: - 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); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - break; - } - usbk->lastopr = commandstatus(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; - break; - } - - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} - -int usbk_deactivatekey(USBK* usbk) { - int rtn; - - if (usbk->supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - 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); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - break; - } - - 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; -} - -int usbk_changepassword(USBK* usbk, const char* old_pass, const char* new_pass) { - int rtn; - t_UIP_CHPASS chpass; - - if (usbk->supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - 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); - - - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &chpass, CHANGE_PASS, sizeof(chpass), WRITE_SCSI); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - break; - } - usbk->lastopr = commandstatus(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_setdevicelabel(USBK* usbk, const char* pass, const char* device_label) { - int rtn; - t_UIP_SETDEVICELABEL devlabel; - - if (usbk->supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - 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_path, (unsigned char*) &devlabel, SET_DEV_NAME, sizeof(devlabel), WRITE_SCSI); - - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - break; - } - usbk->lastopr = commandstatus(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; - break; - } - - DBG_LASTOPR_STRING(usbk->lastopr); - 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 rtn; - t_UIP_SETKEY setkey; - - if (usbk->supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - if (key_no > usbk->multikey_cap) { - usbk->lastopr = USBK_LO_INVALID_KEYNO; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - 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 { - strncpy(setkey.keyname.s, usbk->key_names[key_no - 1], sizeof(setkey.keyname.s)); - } - - if ((key_size != 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)); - } else { - setkey.options.me = SETKEY_NAMEONLY; - } - - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &setkey, SET_KEY, sizeof(setkey), WRITE_SCSI); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - break; - } - usbk->lastopr = commandstatus(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; - break; - } - - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} - -int usbk_setkey_decimal(USBK* usbk, const char *pass, uint8_t key_no, KEYSIZE 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); - } - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} - -int usbk_setkey_hex(USBK* usbk, const char *pass, uint8_t key_no, KEYSIZE 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 rtn = 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); - } - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} - -int usbk_setautact(USBK* usbk, const char *pass, int key_no) { - int rtn; - t_UIP_SETAUTOACTIVATE autoact; - - if (usbk->supported == false) { - usbk->lastopr = USBK_LO_UNSUPPORTED_USBK; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - if (key_no > usbk->multikey_cap) { - usbk->lastopr = USBK_LO_INVALID_KEYNO; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - 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; - rtn = send_scsi_command(usbk->dev_path, (unsigned char*) &autoact, SET_AUTO_ACTIVE, sizeof(autoact), WRITE_SCSI); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - break; - } - usbk->lastopr = commandstatus(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; - break; - } - - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} - -int usbk_getrandomkey(USBK* usbk, uint8_t* random_key, KEYSIZE keysize) { - int rtn; - t_UIP_GENERATEKEY genkey; - - if (usbk->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))) { - 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); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - usbk->lastopr = commandstatus(usbk); - if (usbk->lastopr == USBK_LO_PASS) { - memcpy(random_key, genkey.key.u8, keysize_inbyte); - } - - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} - -int usbk_refreshusbkinfo(USBK* usbk) { - int i; - int rtn = 0; - - if (usbk->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); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - 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_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; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} - -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); - if (rtn < 0) { - usbk->lastopr = USBK_LO_SCSI_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * 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, KEYSIZE 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? - - //int ikey[this->keysize_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_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_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 (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; -} - -static int convertkey_text2hex(uint8_t *key_hex, const char* key_text, KEYSIZE keysize) { - string key_string_text = key_text; - - if (key_string_text.size() > 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(KEYSIZE keysize) { - int size_byte = 0; - - switch (keysize) { - case KEYSIZE_128: - size_byte = 16; - break; - case KEYSIZE_192: - size_byte = 24; - break; - case KEYSIZE_256: - size_byte = 32; - break; - case KEYSIZE_NULL: - default: - size_byte = 0; - break; - } - 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; - 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; - const char *str; - int i; - struct udev_list_entry *list_entry; - - struct stat statbuf; - - udev = udev_new(); - if (udev == NULL) { - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - char devname[1024]; - 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; - } else { - char type; - if (S_ISBLK(statbuf.st_mode)) { - type = 'b'; - } else if (S_ISCHR(statbuf.st_mode)) { - type = 'c'; - } else { - udev_unref(udev); - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - - dev = udev_device_new_from_devnum(udev, type, statbuf.st_rdev); - - if (dev == NULL) { - usbk->lastopr = USBK_LO_UDEV_ERROR; - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; - } - } - - dev_usb = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device"); - - 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 = strdup(udev_device_get_sysname(dev)); - usbk->dev_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) { - 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); - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} -#endif - -#if defined(__linux__) - -static int getudevbackdisk(USBK* usbk) { - int rtn; - 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_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; - } - - // 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); - - usbk->lastopr = USBK_LO_UDEV_ERROR; - - 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 = strdup(udev_device_get_sysname(dev)); - usbk->backdisk_path = strdup(udev_device_get_devnode(dev)); - usbk->lastopr = USBK_LO_PASS; - } - } - } - } - } - } - udev_device_unref(dev); - } - udev_enumerate_unref(enumerate); - udev_unref(udev); - DBG_LASTOPR_STRING(usbk->lastopr); - return (-1) * usbk->lastopr; -} -#endif - -#if defined(__linux__) - -typedef struct __USBKS -{ - USBK* usbk; - int counter; - int lastopr; -}USBKS; - - -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 = NULL; - struct udev_device *dev_scsi = NULL; - - - USBKS* usbks = (USBKS*)calloc(1, sizeof(USBKS)); - - usbks->counter = 0; - - usbks->usbk = NULL; - USBK *new_usbk = NULL; - - // 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); - - 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; - } - - 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->counter++; - } - } - } - } - udev_device_unref(dev); - } - udev_enumerate_unref(enumerate); - udev_unref(udev); - return usbks; -} - - -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->counter--; - } - free(usbks); - return 0; -} - -USBK* usbk_list_get_entry(USBKS* usbks){ - return usbks->usbk; -} - -USBK* usbk_list_get_next(USBK* usbk){ - return usbk->next; -} - -USBK* usbk_list_get_previous(USBK* usbk){ - return usbk->previous; -} - -int usbk_list_refreshall(USBKS* usbks){ - - USBK* list_entry; - usbk_list_entry_foreach(list_entry, usbks->usbk){ - usbk_refreshusbkinfo(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->counter; -} - -int usbk_list_get_lastoprstatus(USBKS* usbks){ - return usbks->lastopr; -} - -#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; -} - - diff --git a/libusbk/src/libusbk.h b/libusbk/src/libusbk.h index 81494fd..55d74ed 100644 --- a/libusbk/src/libusbk.h +++ b/libusbk/src/libusbk.h @@ -23,151 +23,318 @@ extern "C" { #if defined(__AVR32__) || defined(__linux__) # include +# include #elif defined(WIN32) # include #else # error unsupported environment #endif -#define NB_AESKEY 3 - -#if defined(__linux__) -# define USBK_ATTRIBUTE_PACKED __attribute__((__packed__)) -#else -# define USBK_ATTRIBUTE_PACKED +#if defined(__GNUC__) +# define inline inline __attribute__((always_inline)) #endif -typedef enum USBK_ATTRIBUTE_PACKED __KEYSIZE +/* + * usbk keysize type values + * + * all keysize value is uint8_t (1 byte) long + * */ +enum E_USBK_KEYSIZE { - KEYSIZE_NULL = 0, - KEYSIZE_128BIT = 1, - KEYSIZE_192BIT = 2, - KEYSIZE_256BIT = 3, -} KEYSIZE; - -//-USBK DEVICE STATES -typedef enum __USBK_DS + 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 int8_t usbk_keysize_t; + +/* + * usbk device status type values + * + * all ds value is uint8_t (1 byte) long + * */ +enum E_USBK_DEVICE_STATUS { - 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_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 int8_t usbk_ds_t; + +/* + * usbk last operation type values + * + * all lo value is uint8_t (1 byte) long + * */ +enum E_USBK_LASTOPP { - 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; - -USBK* usbk_new(const char* dev); + 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 int8_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 +{ + /* 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; + + struct USBK *next; +}; +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) \ + 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) + +#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) \ + 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_set_autact(usbk, pass, key_no) +#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); -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); - -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); ;} - -#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) - -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); - - -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); + +/* Debugging informations */ +extern void libusbk_enable_debug(); +extern void libusbk_disable_debug(); + +/* libusbk version */ +extern const char* libusbk_version(); + +/* + * 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); +extern unsigned int usbk_keysize_as_byte(usbk_keysize_t keysize); + +#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); + +/* + * 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) + { 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); } + +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; +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 USBKS* usbk_list_new(void); -extern int usbk_list_release(USBKS* usbks); -extern USBK* usbk_list_get_entry(USBKS* usbks); -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 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(USBKS* usbks); +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/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..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 //! @{ @@ -35,7 +34,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 +48,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 +76,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..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__)) @@ -63,6 +69,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..be29dcc 100644 --- a/libusbk/src/usbk_scsi.c +++ b/libusbk/src/usbk_scsi.c @@ -22,18 +22,20 @@ #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] = { - {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 @@ -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!"); @@ -50,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); } @@ -58,21 +60,23 @@ 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 = 512; + packet.datalen = _PACK_BUFF_SIZE; if (usbk_sg_tansfer(&packet) < 0) { fprintf(stderr, "Error! SCSI Read error!"); return USBK_SCSI_TRANSFER_FAIL; } - if (rw == READ_SCSI) { + if (rw == USBK_READ_SCSI) { if (len != 0) { memcpy(buff, buffer, len); } } + usbk_sg_close(); + return USBK_SCSI_PASS; } 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/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) diff --git a/usbk_cli/src/main.cpp b/usbk_cli/src/main.cpp index 09b8afc..62154fb 100644 --- a/usbk_cli/src/main.cpp +++ b/usbk_cli/src/main.cpp @@ -23,7 +23,7 @@ #include #include -#include "libusbk.h" +#include using namespace std; @@ -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); @@ -170,7 +170,7 @@ int main(int argc, char *argv[]) { } if (Dflag) { - usbk_debug_enable(); + libusbk_enable_debug(); } if (question_flag) { @@ -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); } @@ -318,7 +320,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 +329,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); } @@ -344,15 +346,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; } } @@ -535,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_LIST* _usbk_list = 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_list) { + 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_count(_usbk_list); + printf("%d USBK(s) found", counter); - usbk_list_release(usbks); + usbk_list_release(_usbk_list); } void linuxcli_show_dev_info(USBK* myusbk) { @@ -568,7 +567,7 @@ void linuxcli_show_dev_info(USBK* myusbk) { char autoactive[64]; char model[32]; - usbk_refreshusbkinfo(myusbk); + usbk_refresh_usbkinfo(myusbk); sprintf(backdisk, "-"); diff --git a/usbk_cli_pp/CMakeLists.txt b/usbk_cli_pp/CMakeLists.txt new file mode 100644 index 0000000..6aa046b --- /dev/null +++ b/usbk_cli_pp/CMakeLists.txt @@ -0,0 +1,107 @@ +# +# 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}") + +CONFIGURE_FILE(src/config.hpp.cmake src/config.hpp) + +# 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/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 new file mode 100644 index 0000000..5b333da --- /dev/null +++ b/usbk_cli_pp/src/main.cpp @@ -0,0 +1,658 @@ + /* + * @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 "config.hpp" + +#include +#include +#include + +#include +#include +#include + +#include +#include + +//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(UsbkDevice* device); + +static void print_help(); +static void print_version(); + +//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)) { + std::cerr << "Parse Error!"; + exit(EXIT_FAILURE); + } + + if (question_flag) { + print_help(); + exit(EXIT_SUCCESS); + } + + if (vflag) { + print_version(); + exit(EXIT_SUCCESS); + } + + if (Dflag) { + libusbk_enable_debug(); + libusbk_plusplus_enable_debug(); + } + + if (main_operation > 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(EXIT_SUCCESS); + } + + if (geteuid() != 0) { + std::cerr << "Requested operation requires superuser privilege\n"; + exit(EXIT_FAILURE); + } + + if (sflag) { + linuxcli_show_devices(); + exit(EXIT_SUCCESS); + } + + if (uflag == 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) { + print_result(device); + delete device; + + exit(0); + } + + int ret; + + ///////////////////////////////////////////// + // ACTIVATE + // FIXME: ask password from command line + ///////////////////////////////////////////// + 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 { + std::cerr << "Missing parameter!\n"; + 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(UsbkDevice* device) +{ + std::string result; + + switch (device->lastOperation()) { + case USBK_LO_PASS: + result = "Operation Successful"; + break; + + case USBK_LO_GEN_FAIL: + result = "Operation Failed!"; + break; + + case USBK_LO_FAILED_PASS: + { + std::stringstream s; + + s << "Password Incorrect, Retry Number = " + << device->retryNumber(); + + result = s.str(); + } + + break; + + case USBK_LO_FABRIC_RESET: + result = + "****************************************\n" + " All keys and your password is erased. \n" + " Please, re-configure your device. \n" + "****************************************"; + break; + + case USBK_LO_USBK_UNPLUGING: + result = "USBK must be removed and replugged."; + break; + + case USBK_LO_INVALID_KEYNO: + result = "Invalid Key Number."; + break; + + case USBK_LO_INVALID_KEYSIZE: + result = "Invalid Key Size."; + break; + + case USBK_LO_INVALID_DEVICELABEL: + result = "Invalid Device Label"; + break; + + case USBK_LO_INVALID_NEWPASS: + result = "Invalid New Password."; + break; + + case USBK_LO_STATE_ERROR: + + switch (device->deviceState()) { + case USBK_DS_ACTIVATE: + result = "USBK in active. This operation is not able to done in this state."; + break; + + case USBK_DS_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: + result = "USBK in deactive. This operation is not able to done in this state."; + break; + + case USBK_DS_FABRIC_DEFAULT: + result = "USBK in fabric default. Please first set your password."; + break; + + case USBK_DS_MUST_REMOVE: + result = "USBK in must remove. This operation is not able to done in this state."; + break; + + default: + result = "The state of USBK is unknown. This operation is not able to done in this state."; + break; + } + + break; + case USBK_LO_SCSI_ERROR: + result = "SCSI Error! No Device Found!"; + break; + + case USBK_LO_UNSUPPORTED_USBK: + result = "Unsupported USBK."; + break; + + case USBK_LO_INVALID_KEY: + result = "Invalid Key."; + break; + + case USBK_LO_UDEV_ERROR: + result = "UDEV Error! No Device Found."; + break; + + case USBK_LO_MEM_ERROR: + result = "Mem Error! No Device Found."; + break; + + case USBK_LO_INVALID_PASS: + result = "Invalid Password."; + break; + + default: + result = "Unknown error!"; + break; + } + + std::cout << result << std::endl; +} + +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 void print_version(void) +{ + std::cout << USBK_CLI_PACKAGE << " " << USBK_CLI_VERSION << std::endl; +}