diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-28 11:53:58 +0100 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-28 11:53:58 +0100 |
commit | 146f94df39372490756ca7014e9c02a99cb08ac9 (patch) | |
tree | a09065b937c7b97bd1ed734c83f4fc4bdd89d7bb | |
parent | b0d1d6a1a44210a25b3d0e6950ec08f551714ccf (diff) | |
download | libnitrokey-146f94df39372490756ca7014e9c02a99cb08ac9.tar.gz libnitrokey-146f94df39372490756ca7014e9c02a99cb08ac9.tar.bz2 |
Add NK_get_status function to get status struct
Currently, the C API provides access to the device status using the
NK_get_{major,minor}_firmware_version, NK_device_serial_number and
NK_read_config functions. Each function sends a command to the device,
although the data is returned in a single response. This patch adds a
NK_status struct and a NK_get_status function that provide access to
this data with a single command.
-rw-r--r-- | NK_C_API.cc | 24 | ||||
-rw-r--r-- | NK_C_API.h | 52 |
2 files changed, 76 insertions, 0 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc index eae35d5..6905ef4 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -261,6 +261,30 @@ extern "C" { }); } + NK_C_API int NK_get_status(struct NK_status* out) { + if (out == nullptr) { + return -1; + } + auto m = NitrokeyManager::instance(); + auto result = get_with_status([&]() { + return m->get_status(); + }, proto::stick10::GetStatus::ResponsePayload()); + auto error_code = std::get<0>(result); + if (error_code != 0) { + return error_code; + } + + auto status = std::get<1>(result); + out->firmware_version_major = status.firmware_version_st.major; + out->firmware_version_minor = status.firmware_version_st.minor; + out->serial_number_smart_card = status.card_serial_u32; + out->config_numlock = status.numlock; + out->config_capslock = status.capslock; + out->config_scrolllock = status.scrolllock; + out->otp_user_password = status.enable_user_password != 0; + return 0; + } + NK_C_API char * NK_device_serial_number() { auto m = NitrokeyManager::instance(); return get_with_string_result([&]() { @@ -138,6 +138,47 @@ extern "C" { }; /** + * Stores the common device status for all Nitrokey devices. + */ + struct NK_status { + /** + * The major firmware version, e. g. 0 in v0.40. + */ + uint8_t firmware_version_major; + /** + * The minor firmware version, e. g. 40 in v0.40. + */ + uint8_t firmware_version_minor; + /** + * The serial number of the smart card. + */ + uint32_t serial_number_smart_card; + /** + * The HOTP slot to generate a password from if the numlock + * key is pressed twice (slot 0-1, or any other value to + * disable the function). + */ + uint8_t config_numlock; + /** + * The HOTP slot to generate a password from if the capslock + * key is pressed twice (slot 0-1, or any other value to + * disable the function). + */ + uint8_t config_capslock; + /** + * The HOTP slot to generate a password from if the scrolllock + * key is pressed twice (slot 0-1, or any other value to + * disable the function). + */ + uint8_t config_scrolllock; + /** + * Indicates whether the user password is required to generate + * an OTP value. + */ + bool otp_user_password; + }; + + /** * Stores the status of a Storage device. */ struct NK_storage_status { @@ -318,6 +359,17 @@ extern "C" { NK_C_API char * NK_status(); /** + * Get the stick status common to all Nitrokey devices and return the + * command processing error code. If the code is zero, i. e. the + * command was successful, the storage status is written to the output + * pointer's target. The output pointer must not be null. + * + * @param out the output pointer for the status + * @return command processing error code + */ + NK_C_API int NK_get_status(struct NK_status* out); + + /** * Return the device's serial number string in hex. * @return string device's serial number in hex */ |