diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2017-03-31 18:15:37 +0200 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2017-04-03 13:20:36 +0200 |
commit | 9782076fd0c80385f48e2a3c4c61c9dda06841b3 (patch) | |
tree | ad94f5c728d20f265dd8d5b24476f23a0b97cb0b /NitrokeyManager.cc | |
parent | 71fd63402dcbbc37d6c8b3818de35f7142b850b0 (diff) | |
download | libnitrokey-9782076fd0c80385f48e2a3c4c61c9dda06841b3.tar.gz libnitrokey-9782076fd0c80385f48e2a3c4c61c9dda06841b3.tar.bz2 |
Return OTP codes as strings to make sure they are zero-filled properly
Adjust Python tests for new OTP codes return value
Also remove manual 0-filling
Fixes #57
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
Diffstat (limited to 'NitrokeyManager.cc')
-rw-r--r-- | NitrokeyManager.cc | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 159d647..a4ce3a5 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -216,7 +216,13 @@ namespace nitrokey{ return response.data().dissect(); } - uint32_t NitrokeyManager::get_HOTP_code(uint8_t slot_number, const char *user_temporary_password) { + string getFilledOTPCode(uint32_t code, bool use_8_digits){ + stringstream s; + s << std::right << std::setw(use_8_digits ? 8 : 6) << std::setfill('0') << code; + return s.str(); + } + + string NitrokeyManager::get_HOTP_code(uint8_t slot_number, const char *user_temporary_password) { if (!is_valid_hotp_slot_number(slot_number)) throw InvalidSlotException(slot_number); if (is_authorization_command_supported()){ @@ -226,7 +232,7 @@ namespace nitrokey{ authorize_packet<GetHOTP, UserAuthorize>(gh, user_temporary_password, device); } auto resp = GetHOTP::CommandTransaction::run(device, gh); - return resp.data().code; + return getFilledOTPCode(resp.data().code, resp.data().use_8_digits); } else { auto gh = get_payload<stick10_08::GetHOTP>(); gh.slot_number = get_internal_slot_number_for_hotp(slot_number); @@ -234,19 +240,21 @@ namespace nitrokey{ strcpyT(gh.temporary_user_password, user_temporary_password); } auto resp = stick10_08::GetHOTP::CommandTransaction::run(device, gh); - return resp.data().code; + return getFilledOTPCode(resp.data().code, resp.data().use_8_digits); } + return ""; } - bool NitrokeyManager::is_valid_hotp_slot_number(uint8_t slot_number) const { return slot_number < 3; } bool NitrokeyManager::is_valid_totp_slot_number(uint8_t slot_number) const { return slot_number < 0x10-1; } //15 uint8_t NitrokeyManager::get_internal_slot_number_for_totp(uint8_t slot_number) const { return (uint8_t) (0x20 + slot_number); } uint8_t NitrokeyManager::get_internal_slot_number_for_hotp(uint8_t slot_number) const { return (uint8_t) (0x10 + slot_number); } - uint32_t NitrokeyManager::get_TOTP_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time, - uint8_t last_interval, - const char *user_temporary_password) { + + + string NitrokeyManager::get_TOTP_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time, + uint8_t last_interval, + const char *user_temporary_password) { if(!is_valid_totp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_totp(slot_number); @@ -261,15 +269,15 @@ namespace nitrokey{ authorize_packet<GetTOTP, UserAuthorize>(gt, user_temporary_password, device); } auto resp = GetTOTP::CommandTransaction::run(device, gt); - return resp.data().code; + return getFilledOTPCode(resp.data().code, resp.data().use_8_digits); } else { auto gt = get_payload<stick10_08::GetTOTP>(); strcpyT(gt.temporary_user_password, user_temporary_password); gt.slot_number = slot_number; auto resp = stick10_08::GetTOTP::CommandTransaction::run(device, gt); - return resp.data().code; + return getFilledOTPCode(resp.data().code, resp.data().use_8_digits); } - + return ""; } bool NitrokeyManager::erase_slot(uint8_t slot_number, const char *temporary_password) { @@ -830,7 +838,7 @@ namespace nitrokey{ } } - uint32_t NitrokeyManager::get_TOTP_code(uint8_t slot_number, const char *user_temporary_password) { + string NitrokeyManager::get_TOTP_code(uint8_t slot_number, const char *user_temporary_password) { return get_TOTP_code(slot_number, 0, 0, 0, user_temporary_password); } |