-
Notifications
You must be signed in to change notification settings - Fork 17
/
otptool.c
244 lines (225 loc) · 8.12 KB
/
otptool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* otptool - HOTP/OATH one-time password utility
*
* Copyright 2009 Archie L. Cobbs <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "otpdefs.h"
/* Program name */
#define PROG_NAME "otptool"
/* Definitions */
#define OTP_BUF_SIZE 16
/* Internal functions */
static void usage(void);
int
main(int argc, char **argv)
{
const char *otp = NULL;
const char *key = NULL;
const char *motp_pin = NULL;
unsigned char keybuf[128];
unsigned char filebuf[520];
char otpbuf10[OTP_BUF_SIZE];
char otpbuf16[OTP_BUF_SIZE];
size_t keylen = -1;
int time_interval = DEFAULT_TIME_INTERVAL;
int ndigits = -1;
int counter_start;
int counter_stop;
int read_from_file = 0;
int parse_ascii_key = 1;
int counter = -1;
int use_time = 0;
int window = 0;
int ch;
int i;
/* Parse command line */
while ((ch = getopt(argc, argv, "c:d:fFhi:m:tvw:")) != -1) {
switch (ch) {
case 'c':
if (use_time)
errx(EXIT_USAGE_ERROR, "only one of `-c' or `-t' should be specified");
counter = atoi(optarg);
if (counter < 0)
errx(EXIT_USAGE_ERROR, "invalid counter value `%s'", optarg);
break;
case 'd':
ndigits = atoi(optarg);
if (ndigits < 1)
errx(EXIT_USAGE_ERROR, "invalid digit count `%s'", optarg);
break;
case 'F':
read_from_file = 1;
parse_ascii_key = 1;
break;
case 'f':
read_from_file = 1;
parse_ascii_key = 0;
break;
case 'h':
usage();
return 0;
case 'i':
time_interval = atoi(optarg);
break;
case 'm':
ndigits = 6;
time_interval = 10;
motp_pin = optarg;
*otpbuf10 = '\0';
break;
case 't':
if (counter != -1)
errx(EXIT_USAGE_ERROR, "only one of `-c' or `-t' should be specified");
use_time = 1;
break;
case 'w':
window = atoi(optarg);
if (window < 0)
errx(EXIT_USAGE_ERROR, "invalid counter window `%s'", optarg);
break;
default:
usage();
return EXIT_USAGE_ERROR;
}
}
/* Parse command line arguments */
switch (argc - optind) {
case 2:
otp = argv[optind + 1];
if (ndigits == -1)
ndigits = strlen(otp);
if (strlen(otp) != ndigits)
errx(EXIT_NOT_MATCHED, "the given OTP `%s' has the wrong length %d != %d", otp, (int)strlen(otp), ndigits);
// FALLTHROUGH
case 1:
key = argv[optind];
break;
default:
warnx("wrong number of command line arguments");
usage();
return EXIT_USAGE_ERROR;
}
/* Set default #digits */
if (ndigits == -1)
ndigits = DEFAULT_NUM_DIGITS;
/* Read from file if needed */
if (read_from_file) {
FILE *fp;
size_t len;
/* Read data */
if ((fp = fopen(key, "rb")) == NULL)
err(EXIT_SYSTEM_ERROR, "error reading `%s'", key);
len = fread(filebuf, 1, sizeof(filebuf) - 1, fp);
if (ferror(fp))
err(EXIT_SYSTEM_ERROR, "error reading `%s'", key);
if (!feof(fp))
errx(EXIT_SYSTEM_ERROR, "error reading `%s': %s", key, "key is too long");
fclose(fp);
/* Optionally parse file content as ASCII */
if (parse_ascii_key) {
filebuf[len] = 0;
while (len > 0 && isspace(filebuf[len - 1])) /* trim whitespace */
filebuf[--len] = 0;
key = (char *)filebuf;
} else {
if (len > sizeof(keybuf))
errx(EXIT_SYSTEM_ERROR, "error reading `%s': %s", key, "key is too long");
memcpy(keybuf, filebuf, len);
keylen = len;
}
}
/* Parse key */
if (parse_ascii_key) {
for (keylen = 0; keylen < sizeof(keybuf) && key[keylen * 2] != '\0'; keylen++) {
const char *s = &key[keylen * 2];
int nibs[2];
for (i = 0; i < 2; i++) {
if (isdigit(s[i]))
nibs[i] = s[i] - '0';
else if (isxdigit(s[i]))
nibs[i] = tolower(s[i]) - 'a' + 10;
else
errx(EXIT_USAGE_ERROR, "invalid key `%s'", key);
}
keybuf[keylen] = (nibs[0] << 4) | nibs[1];
}
}
/* Determine target counter */
if (use_time)
counter = time(NULL) / time_interval;
else if (counter < 0)
counter = 0;
/* Search or generate */
if (otp == NULL) {
if (use_time) {
counter_start = counter - window;
counter_stop = counter + window;
} else {
counter_start = counter;
counter_stop = counter + window;
}
for (counter = counter_start; counter <= counter_stop; counter++) {
if (motp_pin != NULL)
motp(keybuf, keylen, motp_pin, counter, ndigits, otpbuf16, OTP_BUF_SIZE);
else
hotp(keybuf, keylen, counter, ndigits, otpbuf10, otpbuf16, OTP_BUF_SIZE);
printf("%d: %s%s%s\n", counter, otpbuf10, *otpbuf10 != '\0' && *otpbuf16 != '\0' ? " " : "", otpbuf16);
}
return 0;
} else {
for (i = 0; i <= window; i++) {
int try;
try = counter + i;
if (motp_pin != NULL)
motp(keybuf, keylen, motp_pin, try, ndigits, otpbuf16, OTP_BUF_SIZE);
else
hotp(keybuf, keylen, try, ndigits, otpbuf10, otpbuf16, OTP_BUF_SIZE);
if (strcasecmp(otp, otpbuf10) == 0 || strcasecmp(otp, otpbuf16) == 0)
goto match;
if (use_time && i != 0) {
try = counter - i;
if (motp_pin != NULL)
motp(keybuf, keylen, motp_pin, try, ndigits, otpbuf16, OTP_BUF_SIZE);
else
hotp(keybuf, keylen, try, ndigits, otpbuf10, otpbuf16, OTP_BUF_SIZE);
if (strcasecmp(otp, otpbuf10) == 0 || strcasecmp(otp, otpbuf16) == 0)
goto match;
}
continue;
match:
printf("%d\n", try);
return 0;
}
}
/* Not found */
fprintf(stderr, "one-time password \"%s\" was not found within the counter range %d ... %d\n", otp,
use_time ? counter - window : counter, counter + window);
return EXIT_NOT_MATCHED;
}
static void
usage()
{
fprintf(stderr, "Usage: %s [-Ffht] [-c counter] [-d digits] [-i interval] [-m PIN] [-w window] key [otp]\n", PROG_NAME);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -c\tSpecify the initial counter value (conflicts with `-t')\n");
fprintf(stderr, " -F\t`key' refers to the file containing the key in hexadecimal format\n");
fprintf(stderr, " -f\t`key' refers to the file containing the key in binary format\n");
fprintf(stderr, " -h\tDisplay this usage message\n");
fprintf(stderr, " -i\tSpecify time interval in seconds (default %d)\n", DEFAULT_TIME_INTERVAL);
fprintf(stderr, " -m\tUse mOTP algorithm with given PIN; also implies `-d 6' and `-i 10'\n");
fprintf(stderr, " -t\tDerive initial counter value from the current time (conflicts with `-c')\n");
fprintf(stderr, " -d\tSpecify number of digits in the generated OTP(s) (default %d)\n", DEFAULT_NUM_DIGITS);
fprintf(stderr, " -w\tSpecify size of window for additional counter values (default %d)\n", DEFAULT_WINDOW);
}