diff options
| author | szszszsz <szszszsz@users.noreply.github.com> | 2016-09-08 16:49:52 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-09-08 16:49:52 +0200 | 
| commit | e164c5f3dc74fb2335b1fc573ce446cdd76a07dc (patch) | |
| tree | e11e0a7458d7aae9e484722550c60292b3a6d9ba | |
| parent | 7dbbe7fb4c50c62b0e1818c5356aafb97a00ac0f (diff) | |
| parent | bcbb54f49fad3f4bb3e8e6aca081d9d300e74d2b (diff) | |
| download | libnitrokey-e164c5f3dc74fb2335b1fc573ce446cdd76a07dc.tar.gz libnitrokey-e164c5f3dc74fb2335b1fc573ce446cdd76a07dc.tar.bz2 | |
Merge pull request #34 from Nitrokey/issue_33
Function for getting device's serial number in hex. Fixes #33
| -rw-r--r-- | NK_C_API.cc | 9 | ||||
| -rw-r--r-- | NK_C_API.h | 6 | ||||
| -rw-r--r-- | NitrokeyManager.cc | 7 | ||||
| -rw-r--r-- | include/NitrokeyManager.h | 1 | ||||
| -rw-r--r-- | include/misc.h | 2 | ||||
| -rw-r--r-- | include/stick10_commands.h | 9 | ||||
| -rw-r--r-- | misc.cc | 8 | ||||
| -rw-r--r-- | unittest/test_bindings.py | 7 | 
8 files changed, 42 insertions, 7 deletions
| diff --git a/NK_C_API.cc b/NK_C_API.cc index b755c70..6265215 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -175,6 +175,15 @@ extern const char * NK_status() {      });  } +extern const char * NK_device_serial_number(){ +    auto m = NitrokeyManager::instance(); +    return get_with_string_result([&](){ +        string && s = m->get_serial_number(); +        char * rs = strdup(s.c_str()); +        clear_string(s); +        return rs; +    }); +}  extern uint32_t NK_get_hotp_code(uint8_t slot_number) {      return NK_get_hotp_code_PIN(slot_number, ""); @@ -40,6 +40,12 @@ extern int NK_logout();  extern const char * NK_status();  /** + * Return the device's serial number string in hex. + * @return string device's serial number in hex + */ +extern const char * NK_device_serial_number(); + +/**   * Get last command processing status. Useful for commands which returns the results of their own and could not return   * an error code.   * @return previous command processing error code diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 161f7aa..5b648b5 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -88,6 +88,11 @@ namespace nitrokey{          }      } +    string NitrokeyManager::get_serial_number() { +        auto response = GetStatus::CommandTransaction::run(*device); +        return response.data().get_card_serial_hex(); +    } +      string NitrokeyManager::get_status() {          auto response = GetStatus::CommandTransaction::run(*device);          return response.data().dissect(); @@ -410,4 +415,4 @@ namespace nitrokey{          return true;      } -}
\ No newline at end of file +} diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index d2ea991..1e518f4 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -40,6 +40,7 @@ namespace nitrokey {          bool disconnect();          void set_debug(bool state);          string get_status(); +        string get_serial_number();          const char * get_totp_slot_name(uint8_t slot_number);          const char * get_hotp_slot_name(uint8_t slot_number); diff --git a/include/misc.h b/include/misc.h index a5d9e6d..cccd830 100644 --- a/include/misc.h +++ b/include/misc.h @@ -15,7 +15,7 @@ typename T::CommandPayload get_payload(){  } -    std::string hexdump(const char *p, size_t size); +    std::string hexdump(const char *p, size_t size, bool print_header=true);  uint32_t stm_crc32(const uint8_t *data, size_t size);  }  } diff --git a/include/stick10_commands.h b/include/stick10_commands.h index ef83747..a60be59 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -324,15 +324,20 @@ class GetStatus : Command<CommandID::GET_STATUS> {        };      bool isValid() const { return enable_user_password!=delete_user_password; } +    std::string get_card_serial_hex() const { +        return ::nitrokey::misc::hexdump((const char *)(card_serial), +                sizeof card_serial, false); +    } +      std::string dissect() const {        std::stringstream ss;        ss << "firmware_version:\t" << firmware_version << std::endl;        ss << "card_serial:\t"           << ::nitrokey::misc::hexdump((const char *)(card_serial), -                                      sizeof card_serial); +                                      sizeof card_serial, false);        ss << "general_config:\t"           << ::nitrokey::misc::hexdump((const char *)(general_config), -                                      sizeof general_config); +                                      sizeof general_config, false);          ss << "numlock:\t" << (int)numlock << std::endl;          ss << "capslock:\t" << (int)capslock << std::endl;          ss << "scrolllock:\t" << (int)scrolllock << std::endl; @@ -6,14 +6,16 @@  namespace nitrokey {  namespace misc { -std::string hexdump(const char *p, size_t size) { +std::string hexdump(const char *p, size_t size, bool print_header) {    std::stringstream out;    char formatbuf[128];    const char *pstart = p;    for (const char *pend = p + size; p < pend;) { -    snprintf(formatbuf, 128, "%04lx\t", p - pstart); -    out << formatbuf; +      if (print_header){ +          snprintf(formatbuf, 128, "%04lx\t", p - pstart); +          out << formatbuf; +      }      for (const char *le = p + 16; p < le && p < pend; p++) {        snprintf(formatbuf, 128, "%02x ", uint8_t(*p)); diff --git a/unittest/test_bindings.py b/unittest/test_bindings.py index 2ffa046..eeda247 100644 --- a/unittest/test_bindings.py +++ b/unittest/test_bindings.py @@ -496,3 +496,10 @@ def test_get_status(C):      status = C.NK_status()      s = gs(status)      assert len(s) > 0 + + +def test_get_serial_number(C): +    sn = C.NK_device_serial_number() +    sn = gs(sn) +    assert len(sn) > 0 +    print(('Serial number of the device: ', sn)) | 
