| 1 | #include <openssl/hmac.h> |
| 2 | #include <openssl/sha.h> |
| 3 | |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #if defined(__APPLE) |
| 7 | #include <libkern/OSByteOrder.h> |
| 8 | #define htobe64(x) OSSwapHostToBigInt64(x) |
| 9 | #endif |
| 10 | |
| 11 | #include "base32.h" |
| 12 | #include "common.h" |
| 13 | #include "otppass.h" |
| 14 | |
| 15 | unsigned char *extract_secret(const char *otpauth_url, size_t *decoded_len) { |
| 16 | char *lang = getlang(); |
| 17 | |
| 18 | const char *secret_start = strstr(otpauth_url, "secret="); |
| 19 | if (!secret_start) { |
| 20 | if (strncmp(lang, "en", 2) == 0) |
| 21 | perror("Failed to find secret in the OTPAuth URL"); |
| 22 | else perror("OTPAuth URLの中に、シークレットを見つけられませんでした"); |
| 23 | return NULL; |
| 24 | } |
| 25 | secret_start += 7; |
| 26 | |
| 27 | const char *secret_end = strchr(secret_start, '&'); |
| 28 | if (!secret_end) { |
| 29 | if (secret_start[0] != '\0') { |
| 30 | secret_end = secret_start + strlen(secret_start); |
| 31 | } else { |
| 32 | secret_end = secret_start; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if (secret_end < secret_start) { |
| 37 | if (strncmp(lang, "en", 2) == 0) |
| 38 | perror("Incorrect secret range"); |
| 39 | else perror("不正なシークレットの距離"); |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | size_t secret_len = secret_end - secret_start; |
| 44 | char *secret_encoded = (char *)malloc(secret_len + 1); |
| 45 | |
| 46 | if (secret_encoded == NULL) { |
| 47 | if (strncmp(lang, "en", 2) == 0) |
| 48 | perror("Failed to allocate memory"); |
| 49 | else perror("メモリの役割に失敗"); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | strncpy(secret_encoded, secret_start, secret_len); |
| 54 | secret_encoded[secret_len] = '\0'; |
| 55 | |
| 56 | unsigned char *secret_decoded = base32_decode(secret_encoded, decoded_len); |
| 57 | free(secret_encoded); |
| 58 | |
| 59 | if (!secret_decoded) { |
| 60 | if (strncmp(lang, "en", 2) == 0) |
| 61 | perror("Failed to decrypt BASE32"); |
| 62 | else perror("BASE32の復号化に失敗"); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | return secret_decoded; |
| 67 | } |
| 68 | |
| 69 | #if defined(__HAIKU__) |
| 70 | uint64_t htobe64(uint64_t counter) { |
| 71 | uint64_t res = 0; |
| 72 | uint8_t *dest = (uint8_t *)&res; |
| 73 | uint8_t *src = (uint8_t *)&counter; |
| 74 | |
| 75 | dest[0] = src[7]; |
| 76 | dest[1] = src[6]; |
| 77 | dest[2] = src[5]; |
| 78 | dest[3] = src[4]; |
| 79 | dest[4] = src[3]; |
| 80 | dest[5] = src[2]; |
| 81 | dest[6] = src[1]; |
| 82 | dest[7] = src[0]; |
| 83 | |
| 84 | return res; |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | uint32_t generate_totp(const char *secret, uint64_t counter) { |
| 89 | counter = htobe64(counter); |
| 90 | |
| 91 | unsigned char hash[SHA_DIGEST_LENGTH]; |
| 92 | HMAC( |
| 93 | EVP_sha1(), |
| 94 | secret, |
| 95 | strlen(secret), |
| 96 | (unsigned char *)&counter, |
| 97 | sizeof(counter), |
| 98 | hash, |
| 99 | NULL |
| 100 | ); |
| 101 | |
| 102 | int offset = hash[SHA_DIGEST_LENGTH - 1] & 0x0F; |
| 103 | uint32_t truncated_hash = |
| 104 | (hash[offset] & 0x7F) << 24 | |
| 105 | (hash[offset + 1] & 0xFF) << 16 | |
| 106 | (hash[offset + 2] & 0xFF) << 8 | |
| 107 | (hash[offset + 3] & 0xFF); |
| 108 | |
| 109 | return truncated_hash % 1000000; |
| 110 | } |
| 111 | |
| 112 | void otppass(char *file, int isCopy, int copyTimeout) { |
| 113 | if (isCopy == 1 && copyTimeout > 300) copyTimeout = 300; |
| 114 | char *lang = getlang(); |
| 115 | |
| 116 | int isGay = (getenv("WAYLAND_DISPLAY") != NULL); |
| 117 | |
| 118 | |
| 119 | if (isCopy == 1 && getenv("DISPLAY") == NULL && getenv("WAYLAND_DISPLAY") == NULL) { |
| 120 | if (strncmp(lang, "en", 2) == 0) |
| 121 | puts("There is no X or Wayland session, so running 'sp -o'."); |
| 122 | else puts("X又はWaylandセッションではありませんので、「sp -o」を実行します。"); |
| 123 | otppass(file, 0, 0); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | if (isCopy) { |
| 129 | signal(SIGINT, handle_sigint); |
| 130 | } |
| 131 | |
| 132 | gpgme_ctx_t ctx; |
| 133 | gpgme_error_t err; |
| 134 | gpgme_data_t in, out; |
| 135 | size_t secret_len; |
| 136 | |
| 137 | gpgme_check_version(NULL); |
| 138 | err = gpgme_new(&ctx); |
| 139 | if (err) { |
| 140 | if (strncmp(lang, "en", 2) == 0) |
| 141 | perror("Failed to generate the GPG"); |
| 142 | else perror("GPGを創作に失敗"); |
| 143 | exit(1); |
| 144 | } |
| 145 | |
| 146 | err = gpgme_data_new_from_file(&in, file, 1); |
| 147 | if (err) { |
| 148 | if (strncmp(lang, "en", 2) == 0) |
| 149 | perror("Failed to read the GPG file"); |
| 150 | else perror("GPGファイルを読込に失敗"); |
| 151 | gpgme_release(ctx); |
| 152 | exit(1); |
| 153 | } |
| 154 | |
| 155 | err = gpgme_data_new(&out); |
| 156 | if (err) { |
| 157 | if (strncmp(lang, "en", 2) == 0) |
| 158 | perror("Failed to read the GPG data"); |
| 159 | else perror("GPGデータを読込に失敗"); |
| 160 | gpgme_release(ctx); |
| 161 | gpgme_data_release(in); |
| 162 | exit(1); |
| 163 | } |
| 164 | |
| 165 | err = gpgme_op_decrypt(ctx, in, out); |
| 166 | if (err) { |
| 167 | if (strncmp(lang, "en", 2) == 0) |
| 168 | perror("Failed to decrypt the GPG"); |
| 169 | else perror("GPGを復号化に失敗"); |
| 170 | gpgme_release(ctx); |
| 171 | gpgme_data_release(in); |
| 172 | exit(1); |
| 173 | } |
| 174 | |
| 175 | char *secret = gpgme_data_release_and_get_mem(out, &secret_len); |
| 176 | if (!secret) { |
| 177 | if (strncmp(lang, "en", 2) == 0) |
| 178 | perror("Failed to get the GPG"); |
| 179 | else perror("GPGを受取に失敗"); |
| 180 | gpgme_data_release(in); |
| 181 | gpgme_release(ctx); |
| 182 | exit(1); |
| 183 | } |
| 184 | |
| 185 | secret[secret_len] = '\0'; |
| 186 | |
| 187 | size_t decoded_len; |
| 188 | unsigned char *secret_decoded = extract_secret(secret, &decoded_len); |
| 189 | if (!secret_decoded) { |
| 190 | if (strncmp(lang, "en", 2) == 0) |
| 191 | perror("Failed to decode or export secret"); |
| 192 | else perror("シークレットの抽出又はデコードに失敗しました"); |
| 193 | gpgme_data_release(in); |
| 194 | gpgme_release(ctx); |
| 195 | gpgme_free(secret); |
| 196 | exit(1); |
| 197 | } |
| 198 | |
| 199 | secret_decoded[decoded_len] = '\0'; |
| 200 | |
| 201 | time_t current_time = time(NULL); |
| 202 | uint64_t counter = current_time / 30; |
| 203 | uint32_t otp = generate_totp((const char *)secret_decoded, counter); |
| 204 | |
| 205 | gpgme_data_release(in); |
| 206 | gpgme_release(ctx); |
| 207 | gpgme_free(secret); |
| 208 | free(secret_decoded); |
| 209 | |
| 210 | if (isCopy) { |
| 211 | char cmd[64]; |
| 212 | if (isGay) snprintf(cmd, sizeof(cmd), "echo -n %06d | wl-copy", otp); |
| 213 | else snprintf(cmd, sizeof(cmd), "echo -n %06d | xclip -selection clipboard", otp); |
| 214 | |
| 215 | int ret = system(cmd); |
| 216 | if (ret != 0) { |
| 217 | char *ero = (strncmp(lang, "en", 2) == 0 ? |
| 218 | "Failed to copy OTP." : "ワンタイムパスワードをコピーに失敗。"); |
| 219 | fprintf(stderr, "%s\n", ero); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | if (strncmp(lang, "en", 2) == 0) |
| 224 | printf( |
| 225 | "%s\n%s%d%s\n", |
| 226 | "Added the one time password to the clipboard.", |
| 227 | "After ", |
| 228 | copyTimeout, |
| 229 | " seconds it'll be deleted from the clipboard." |
| 230 | ); |
| 231 | else |
| 232 | printf( |
| 233 | "%s\n%d%s\n", |
| 234 | "ワンタイムパスワードをクリップボードに追加しました。", |
| 235 | copyTimeout, |
| 236 | "秒後はクリップボードから取り消されます。" |
| 237 | ); |
| 238 | |
| 239 | sleep(copyTimeout); |
| 240 | |
| 241 | if (isGay) system("echo -n \"\" | wl-copy"); |
| 242 | else system("echo -n \"\" | xclip -selection clipboard"); |
| 243 | } else { |
| 244 | printf("%06d\n", otp); |
| 245 | } |
| 246 | } |