| 1 | #include "common.hh" |
| 2 | #include "base32.hh" |
| 3 | #include "otppass.hh" |
| 4 | |
| 5 | #include <openssl/hmac.h> |
| 6 | #include <openssl/sha.h> |
| 7 | |
| 8 | #include <FL/fl_ask.H> |
| 9 | |
| 10 | #if defined(__APPLE) |
| 11 | #include <libkern/OSByteOrder.h> |
| 12 | #define htobe64(x) OSSwapHostToBigInt64(x) |
| 13 | #endif |
| 14 | |
| 15 | std::vector<unsigned char> Otppass::extract_secret(const std::string &otpauth_url) { |
| 16 | std::string lang = Common::getlang(); |
| 17 | |
| 18 | auto secret_start = otpauth_url.find("secret="); |
| 19 | if (secret_start == std::string::npos) { |
| 20 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 21 | "Failed to find secret in the OTPAuth URL" : |
| 22 | "OTPAuth URLの中に、シークレットを見つけられませんでした"); |
| 23 | fl_alert("%s", err.c_str()); |
| 24 | return {}; |
| 25 | } |
| 26 | secret_start += 7; |
| 27 | |
| 28 | auto secret_end = otpauth_url.find('&', secret_start); |
| 29 | if (secret_end == std::string::npos) { |
| 30 | secret_end = otpauth_url.length(); |
| 31 | } |
| 32 | |
| 33 | if (secret_end < secret_start) { |
| 34 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 35 | "Incorrect secret range" : |
| 36 | "不正なシークレットの距離"); |
| 37 | fl_alert("%s", err.c_str()); |
| 38 | return {}; |
| 39 | } |
| 40 | |
| 41 | std::string secret_encoded = |
| 42 | otpauth_url.substr(secret_start, secret_end - secret_start); |
| 43 | |
| 44 | std::vector<unsigned char> secret_decoded; |
| 45 | secret_decoded = Base32::decode(secret_encoded.c_str()); |
| 46 | |
| 47 | if (secret_decoded.empty()) { |
| 48 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 49 | "Failed to decrypt BASE32" : |
| 50 | "BASE32の復号化に失敗"); |
| 51 | fl_alert("%s", err.c_str()); |
| 52 | return {}; |
| 53 | } |
| 54 | |
| 55 | return secret_decoded; |
| 56 | } |
| 57 | |
| 58 | #if defined(__HAIKU__) || defined(__linux) |
| 59 | uint64_t Otppass::htobe64(uint64_t counter) { |
| 60 | uint64_t res = 0; |
| 61 | uint8_t *dest = reinterpret_cast<uint8_t*>(&res); |
| 62 | const uint8_t *src = reinterpret_cast<const uint8_t*>(&counter); |
| 63 | |
| 64 | for (int i = 0; i < 8; ++i) { |
| 65 | dest[i] = src[7 - i]; |
| 66 | } |
| 67 | |
| 68 | return res; |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | uint32_t Otppass::generate_totp(const std::vector<unsigned char> &secret, |
| 73 | uint64_t counter) { |
| 74 | counter = htobe64(counter); |
| 75 | |
| 76 | unsigned char hash[SHA_DIGEST_LENGTH]; |
| 77 | HMAC( |
| 78 | EVP_sha1(), |
| 79 | secret.data(), |
| 80 | secret.size(), |
| 81 | reinterpret_cast<unsigned char *>(&counter), |
| 82 | sizeof(counter), |
| 83 | hash, |
| 84 | NULL |
| 85 | ); |
| 86 | |
| 87 | int offset = hash[SHA_DIGEST_LENGTH - 1] & 0x0F; |
| 88 | uint32_t truncated_hash = |
| 89 | (hash[offset] & 0x7F) << 24 | |
| 90 | (hash[offset + 1] & 0xFF) << 16 | |
| 91 | (hash[offset + 2] & 0xFF) << 8 | |
| 92 | (hash[offset + 3] & 0xFF); |
| 93 | |
| 94 | return truncated_hash % 1000000; |
| 95 | } |
| 96 | |
| 97 | std::string Otppass::exec(std::string &secret) { |
| 98 | std::string lang = Common::getlang(); |
| 99 | |
| 100 | try { |
| 101 | std::vector<unsigned char> secret_decoded = extract_secret(secret); |
| 102 | |
| 103 | time_t current_time = time(nullptr); |
| 104 | uint64_t counter = current_time / 30; |
| 105 | uint32_t otp = |
| 106 | generate_totp(secret_decoded, counter); |
| 107 | |
| 108 | char otpres[7]; |
| 109 | std::snprintf(otpres, sizeof(otpres), "%06u", otp); |
| 110 | |
| 111 | return std::string(otpres); |
| 112 | } catch (const std::exception &e) { |
| 113 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 114 | "Error" : |
| 115 | "エラー"); |
| 116 | fl_alert("%s: %s", err.c_str(), e.what()); |
| 117 | return ""; |
| 118 | } |
| 119 | } |