aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: bc0ac4a726c6594164383b369cdc582c02b677f4 (plain) (blame)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
/* References: https://datatracker.ietf.org/doc/html/rfc4226#section-5 */

#include <err.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>

#include <openssl/hmac.h>
#include <uriparser/Uri.h>
#include <uriparser/UriBase.h>

#include "b32.h"

#define STREQ(x, y) (strcmp(x, y) == 0)
#define PRINT_CODE(w, x) printf("%0*d\n", (int)w, x)
#define WARNX_AND_RET(...) \
	do { \
		rv = EXIT_FAILURE; \
		warnx(__VA_ARGS__); \
		return false; \
	} while (false)

#define TOTP_DEFAULT (struct totp_config){ .len = 6, .p = 30 }

typedef unsigned char uchar;

struct totp_config {
	const char *enc_sec;
	long len, p;
};

extern char *__progname;

static int rv;

static const char *bad_scheme = "Invalid scheme ‘%.*s’; expected ‘otpauth’";
static const char *bad_param = "Invalid ‘%s’ parameter provided";
static const char *empty_param = "Empty ‘%s’ parameter provided";
static const char *usage_s =
	"Usage: %s [-d digits] [-p period] [secret ...]\n"
	"       %s [-u] [uri ...]\n";

static void      usage(void);
static bool      strtol_safe(long *, const char *);
static bool      totp(struct totp_config, uint32_t *);
static uint32_t  pow32(uint32_t, uint32_t);
static bool      uri_parse(struct totp_config *, const char *);

void
usage(void)
{
	fprintf(stderr, usage_s, __progname, __progname);
	exit(EXIT_FAILURE);
}

int
main(int argc, char *argv[])
{
	int opt;
	bool uflag = false;
	long n;
	char *buf;
	size_t bufsiz;
	ssize_t nr;
	uint32_t code;
	struct totp_config conf = TOTP_DEFAULT;
	struct option longopts[] = {
		{"digits", required_argument, 0, 'd'},
		{"period", required_argument, 0, 'p'},
		{"uri",    no_argument,       0, 'u'},
		{ NULL,    0,                 0,  0 },
	};

	while ((opt = getopt_long(argc, argv, "d:p:u", longopts, NULL)) != -1) {
		switch (opt) {
		case 'd':
		case 'p':
			if (!strtol_safe(&n, optarg))
				errx(EXIT_FAILURE, bad_param,
				     opt == 'd' ? "digits" : "period");
			if (opt == 'd')
				conf.len = n;
			else
				conf.p = n;
			break;
		case 'u':
			uflag = true;
			break;
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;

	for (int i = 0; i < argc; i++) {
		conf.enc_sec = argv[i];
		if (uflag) {
			conf = TOTP_DEFAULT;
			if (!uri_parse(&conf, argv[i])) {
				rv = EXIT_FAILURE;
				continue;
			}
		} else
			conf.enc_sec = argv[i];
		if (totp(conf, &code))
			PRINT_CODE(conf.len, code);
	}

	if (argc == 0) {
		buf = NULL;
		bufsiz = 0;

		while ((nr = getline(&buf, &bufsiz, stdin)) > 0) {
			if (buf[--nr] == '\n')
				buf[nr] = '\0';
			if (uflag) {
				conf = TOTP_DEFAULT;
				if (!uri_parse(&conf, buf)) {
					rv = EXIT_FAILURE;
					continue;
				}
			} else
				conf.enc_sec = buf;
			if (totp(conf, &code))
				PRINT_CODE(conf.len, code);
		}
	}

	return rv;
}

bool
uri_parse(struct totp_config *conf, const char *uri_raw)
{
	int n;
	bool reject;
	size_t len;
	UriUriA uri;
	UriQueryListA *qs;
	const char *epos;

	if ((n = uriParseSingleUriA(&uri, uri_raw, &epos)) != URI_SUCCESS) {
		len = epos - uri_raw + 24 + strlen(__progname);
		WARNX_AND_RET("Failed to parse URI ‘%s’\n"
		              "%*c Error detected here",
		              uri_raw, (int)len, '^');
	}

	len = uri.scheme.afterLast - uri.scheme.first;
	reject = len != strlen("otpauth");
	reject = reject || strncasecmp(uri.scheme.first, "otpauth", len) != 0;

	if (reject)
		WARNX_AND_RET(bad_scheme, (int)len, uri.scheme.first);
	if (uriDissectQueryMallocA(&qs, NULL, uri.query.first,
	                           uri.query.afterLast) != URI_SUCCESS)
		WARNX_AND_RET("Failed to parse query string");

	for (UriQueryListA *p = qs; p != NULL; p = p->next) {
		if (STREQ(p->key, "secret")) {
			if (p->value == NULL)
				WARNX_AND_RET("Secret key has no value");
			conf->enc_sec = p->value;
		} else if (STREQ(p->key, "digits")) {
			if (p->value == NULL)
				WARNX_AND_RET(empty_param, "digits");
			if (!strtol_safe(&conf->len, p->value))
				WARNX_AND_RET(bad_param, "digits");
		} else if (STREQ(p->key, "period")) {
			if (p->value == NULL)
				WARNX_AND_RET(empty_param, "period");
			if (!strtol_safe(&conf->p, p->value))
				WARNX_AND_RET(bad_param, "period");
		}
	}

	uriFreeQueryListA(qs);
	uriFreeUriMembersA(&uri);

	return true;
}

bool
totp(struct totp_config conf, uint32_t *code)
{
	int off;
	char *key;
	uchar *mac;
	time_t epoch;
	size_t keylen;
	uint8_t buf[sizeof(time_t)];  /* Enough for a 64bit num */
	uint32_t binc;
	
	/* TODO: conf.enc_sec needs to be ‘=’ padded to a multiple of 8 */

	/* When decoding base32, you need ceil(conf.enc_sec / 1.6) bytes */
	keylen = strlen(conf.enc_sec) / 1.6 + 1;
	key = calloc(keylen, sizeof(char));
	b32toa(key, conf.enc_sec, strlen(conf.enc_sec));

	if (time(&epoch) == (time_t)-1) {
		warn("time");
		return false;
	}

	epoch /= conf.p;

	buf[0] = (epoch >> 56) & 0xFF;
	buf[1] = (epoch >> 48) & 0xFF;
	buf[2] = (epoch >> 40) & 0xFF;
	buf[3] = (epoch >> 32) & 0xFF;
	buf[4] = (epoch >> 24) & 0xFF;
	buf[5] = (epoch >> 16) & 0xFF;
	buf[6] = (epoch >>  8) & 0xFF;
	buf[7] = (epoch >>  0) & 0xFF;
	
	mac = HMAC(EVP_sha1(), key, keylen, buf, sizeof(buf), NULL, NULL);
	if (mac == NULL)
		WARNX_AND_RET("Failed to compute HMAC SHA-1 hash");

        /* SHA1 hashes are 20 bytes long */
	off = mac[19] & 0x0F;
	binc = (mac[off + 0] & 0x7F) << 24
             | (mac[off + 1] & 0xFF) << 16
             | (mac[off + 2] & 0xFF) <<  8
             | (mac[off + 3] & 0xFF) <<  0;

	*code = binc % pow32(10, conf.len);
	return true;
}

bool
strtol_safe(long *n, const char *s)
{
	char *e;
	*n = strtol(s, &e, 10);
	return *n > 0 && *s != '\0' && *e == '\0';
}

/* This could overflow if you did some autistic shit */
uint32_t
pow32(uint32_t x, uint32_t y)
{
	int n = x;
	if (y == 0)
		return 1;
	while (--y != 0)
		x *= n;
	return x;
}