Blame


1 ecf69ddf 2022-10-20 op /*
2 f31cd5a4 2023-01-16 op * Copyright (c) 2022, 2023 Omar Polo <op@openbsd.org>
3 50705614 2023-05-16 op * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
4 ecf69ddf 2022-10-20 op *
5 ecf69ddf 2022-10-20 op * Permission to use, copy, modify, and distribute this software for any
6 ecf69ddf 2022-10-20 op * purpose with or without fee is hereby granted, provided that the above
7 ecf69ddf 2022-10-20 op * copyright notice and this permission notice appear in all copies.
8 ecf69ddf 2022-10-20 op *
9 ecf69ddf 2022-10-20 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 ecf69ddf 2022-10-20 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 ecf69ddf 2022-10-20 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ecf69ddf 2022-10-20 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 ecf69ddf 2022-10-20 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ecf69ddf 2022-10-20 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 ecf69ddf 2022-10-20 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 ecf69ddf 2022-10-20 op */
17 ecf69ddf 2022-10-20 op
18 ecf69ddf 2022-10-20 op #include <ctype.h>
19 ecf69ddf 2022-10-20 op #include <err.h>
20 ecf69ddf 2022-10-20 op #include <errno.h>
21 ecf69ddf 2022-10-20 op #include <stdio.h>
22 ecf69ddf 2022-10-20 op #include <stdlib.h>
23 ecf69ddf 2022-10-20 op #include <string.h>
24 ecf69ddf 2022-10-20 op #include <time.h>
25 ecf69ddf 2022-10-20 op #include <unistd.h>
26 ecf69ddf 2022-10-20 op
27 ecf69ddf 2022-10-20 op #include <openssl/evp.h>
28 ecf69ddf 2022-10-20 op #include <openssl/hmac.h>
29 ecf69ddf 2022-10-20 op
30 ecf69ddf 2022-10-20 op #ifndef __OpenBSD__
31 ecf69ddf 2022-10-20 op # define pledge(a, b) (0)
32 ecf69ddf 2022-10-20 op #endif
33 ecf69ddf 2022-10-20 op
34 ecf69ddf 2022-10-20 op #ifndef __dead
35 ecf69ddf 2022-10-20 op # define __dead __attribute__((noreturn))
36 ecf69ddf 2022-10-20 op #endif
37 ecf69ddf 2022-10-20 op
38 9bf09c65 2023-05-16 op #if defined(__FreeBSD__) || defined(__NetBSD__)
39 ecf69ddf 2022-10-20 op # include <sys/endian.h>
40 ecf69ddf 2022-10-20 op #elif defined(__APPLE__)
41 ecf69ddf 2022-10-20 op # include <machine/endian.h>
42 ecf69ddf 2022-10-20 op # include <libkern/OSByteOrder.h>
43 ecf69ddf 2022-10-20 op # define htobe64(x) OSSwapHostToBigInt64(x)
44 ecf69ddf 2022-10-20 op # define be32toh(x) OSSwapBigToHostInt32(x)
45 ecf69ddf 2022-10-20 op #else
46 ecf69ddf 2022-10-20 op # include <endian.h>
47 ecf69ddf 2022-10-20 op #endif
48 ecf69ddf 2022-10-20 op
49 ecf69ddf 2022-10-20 op static __dead void
50 ecf69ddf 2022-10-20 op usage(void)
51 ecf69ddf 2022-10-20 op {
52 ecf69ddf 2022-10-20 op fprintf(stderr, "usage: %s\n", getprogname());
53 ecf69ddf 2022-10-20 op exit(1);
54 ecf69ddf 2022-10-20 op }
55 ecf69ddf 2022-10-20 op
56 ecf69ddf 2022-10-20 op static int
57 ecf69ddf 2022-10-20 op b32c(unsigned char c)
58 ecf69ddf 2022-10-20 op {
59 ecf69ddf 2022-10-20 op if (c >= 'A' && c <= 'Z')
60 ecf69ddf 2022-10-20 op return (c - 'A');
61 ecf69ddf 2022-10-20 op if (c >= '2' && c <= '7')
62 ecf69ddf 2022-10-20 op return (c - '2' + 26);
63 ecf69ddf 2022-10-20 op errno = EINVAL;
64 ecf69ddf 2022-10-20 op return (-1);
65 ecf69ddf 2022-10-20 op }
66 ecf69ddf 2022-10-20 op
67 ecf69ddf 2022-10-20 op static size_t
68 cc5f172a 2022-10-25 op b32decode(const char *s, char *q, size_t qlen)
69 ecf69ddf 2022-10-20 op {
70 ecf69ddf 2022-10-20 op int i, val[8];
71 ecf69ddf 2022-10-20 op char *t = q;
72 ecf69ddf 2022-10-20 op
73 cc5f172a 2022-10-25 op while (*s != '\0') {
74 ecf69ddf 2022-10-20 op memset(val, 0, sizeof(val));
75 ecf69ddf 2022-10-20 op for (i = 0; i < 8; ++i) {
76 cc5f172a 2022-10-25 op if (*s == '\0')
77 ecf69ddf 2022-10-20 op break;
78 ecf69ddf 2022-10-20 op if ((val[i] = b32c(*s)) == -1)
79 ecf69ddf 2022-10-20 op return (0);
80 cc5f172a 2022-10-25 op s++;
81 ecf69ddf 2022-10-20 op }
82 ecf69ddf 2022-10-20 op
83 ecf69ddf 2022-10-20 op if (qlen < 5) {
84 ecf69ddf 2022-10-20 op errno = ENOSPC;
85 ecf69ddf 2022-10-20 op return (0);
86 ecf69ddf 2022-10-20 op }
87 ecf69ddf 2022-10-20 op qlen -= 5;
88 ecf69ddf 2022-10-20 op
89 ecf69ddf 2022-10-20 op *q++ = (val[0] << 3) | (val[1] >> 2);
90 ecf69ddf 2022-10-20 op *q++ = ((val[1] & 0x03) << 6) | (val[2] << 1) | (val[3] >> 4);
91 ecf69ddf 2022-10-20 op *q++ = ((val[3] & 0x0F) << 4) | (val[4] >> 1);
92 ecf69ddf 2022-10-20 op *q++ = ((val[4] & 0x01) << 7) | (val[5] << 2) | (val[6] >> 3);
93 ecf69ddf 2022-10-20 op *q++ = ((val[6] & 0x07) << 5) | val[7];
94 ecf69ddf 2022-10-20 op }
95 ecf69ddf 2022-10-20 op
96 ecf69ddf 2022-10-20 op return (q - t);
97 ecf69ddf 2022-10-20 op }
98 ecf69ddf 2022-10-20 op
99 50705614 2023-05-16 op /* adapted from httpd(8) */
100 50705614 2023-05-16 op static char *
101 50705614 2023-05-16 op url_decode(char *url, char *dst)
102 50705614 2023-05-16 op {
103 50705614 2023-05-16 op char *p, *q;
104 50705614 2023-05-16 op char hex[3];
105 50705614 2023-05-16 op unsigned long x;
106 50705614 2023-05-16 op
107 50705614 2023-05-16 op hex[2] = '\0';
108 50705614 2023-05-16 op p = url;
109 50705614 2023-05-16 op q = dst;
110 50705614 2023-05-16 op
111 50705614 2023-05-16 op while (*p != '\0') {
112 50705614 2023-05-16 op if (*p != '%') {
113 50705614 2023-05-16 op *q++ = *p++;
114 50705614 2023-05-16 op continue;
115 50705614 2023-05-16 op }
116 50705614 2023-05-16 op
117 50705614 2023-05-16 op if (!isxdigit((unsigned char)p[1]) ||
118 50705614 2023-05-16 op !isxdigit((unsigned char)p[2]))
119 50705614 2023-05-16 op return (NULL);
120 50705614 2023-05-16 op
121 50705614 2023-05-16 op hex[0] = p[1];
122 50705614 2023-05-16 op hex[1] = p[2];
123 50705614 2023-05-16 op
124 50705614 2023-05-16 op /*
125 50705614 2023-05-16 op * We don't have to validate "hex" because it is
126 50705614 2023-05-16 op * guaranteed to include two hex chars followed by nul.
127 50705614 2023-05-16 op */
128 50705614 2023-05-16 op x = strtoul(hex, NULL, 16);
129 50705614 2023-05-16 op *q++ = (char)x;
130 50705614 2023-05-16 op p += 3;
131 50705614 2023-05-16 op }
132 50705614 2023-05-16 op *q = '\0';
133 50705614 2023-05-16 op return (url);
134 50705614 2023-05-16 op }
135 50705614 2023-05-16 op
136 f31cd5a4 2023-01-16 op static int
137 a5b7d2ee 2023-05-21 op uri2secret(char *s, int *digits, const EVP_MD **alg, int *period)
138 f31cd5a4 2023-01-16 op {
139 44703d29 2023-05-16 op char *q, *t, *f, *secret = NULL;
140 a5b7d2ee 2023-05-21 op const char *errstr;
141 f31cd5a4 2023-01-16 op
142 f31cd5a4 2023-01-16 op if ((q = strchr(s, '?')) == NULL)
143 f31cd5a4 2023-01-16 op return (-1);
144 44703d29 2023-05-16 op
145 44703d29 2023-05-16 op t = q + 1;
146 44703d29 2023-05-16 op while ((f = strsep(&t, "&")) != NULL) {
147 44703d29 2023-05-16 op if (!strncmp(f, "secret=", 7))
148 44703d29 2023-05-16 op secret = f + 7;
149 efd5f30c 2023-05-21 op else if (!strncmp(f, "digits=", 7)) {
150 efd5f30c 2023-05-21 op f += 7;
151 efd5f30c 2023-05-21 op if (!strcmp(f, "6"))
152 efd5f30c 2023-05-21 op *digits = 6;
153 efd5f30c 2023-05-21 op else if (!strcmp(f, "7"))
154 efd5f30c 2023-05-21 op *digits = 7;
155 efd5f30c 2023-05-21 op else if (!strcmp(f, "8"))
156 efd5f30c 2023-05-21 op *digits = 8;
157 efd5f30c 2023-05-21 op else
158 efd5f30c 2023-05-21 op warnx("invalid number of digits; using 6");
159 d2c2e549 2023-05-21 op } else if (!strncmp(f, "algorithm=", 10)) {
160 d2c2e549 2023-05-21 op f += 10;
161 d2c2e549 2023-05-21 op if (!strcmp(f, "SHA1"))
162 d2c2e549 2023-05-21 op *alg = EVP_sha1();
163 d2c2e549 2023-05-21 op else if (!strcmp(f, "SHA256"))
164 d2c2e549 2023-05-21 op *alg = EVP_sha256();
165 d2c2e549 2023-05-21 op else if (!strcmp(f, "SHA512"))
166 d2c2e549 2023-05-21 op *alg = EVP_sha512();
167 d2c2e549 2023-05-21 op else
168 d2c2e549 2023-05-21 op warnx("unknown algorithm; using SHA1");
169 a5b7d2ee 2023-05-21 op } else if (!strncmp(f, "period=", 7)) {
170 a5b7d2ee 2023-05-21 op f += 7;
171 a5b7d2ee 2023-05-21 op *period = strtonum(f, 1, 120, &errstr);
172 a5b7d2ee 2023-05-21 op if (errstr) {
173 a5b7d2ee 2023-05-21 op warnx("period is %s: %s; using 30", errstr, f);
174 a5b7d2ee 2023-05-21 op *period = 30;
175 a5b7d2ee 2023-05-21 op }
176 efd5f30c 2023-05-21 op }
177 44703d29 2023-05-16 op }
178 44703d29 2023-05-16 op
179 44703d29 2023-05-16 op if (secret == NULL)
180 f31cd5a4 2023-01-16 op return (-1);
181 50705614 2023-05-16 op if (url_decode(secret, s) == NULL)
182 50705614 2023-05-16 op errx(1, "failed to percent-decode the secret");
183 f31cd5a4 2023-01-16 op return (0);
184 f31cd5a4 2023-01-16 op }
185 f31cd5a4 2023-01-16 op
186 ecf69ddf 2022-10-20 op int
187 ecf69ddf 2022-10-20 op main(int argc, char **argv)
188 ecf69ddf 2022-10-20 op {
189 ecf69ddf 2022-10-20 op char buf[1024];
190 ecf69ddf 2022-10-20 op size_t buflen;
191 d2c2e549 2023-05-21 op const EVP_MD *alg;
192 ecf69ddf 2022-10-20 op unsigned char md[EVP_MAX_MD_SIZE];
193 ecf69ddf 2022-10-20 op unsigned int mdlen;
194 ecf69ddf 2022-10-20 op char *s, *q, *line = NULL;
195 ecf69ddf 2022-10-20 op size_t linesize = 0;
196 ecf69ddf 2022-10-20 op ssize_t linelen;
197 ecf69ddf 2022-10-20 op uint64_t ct;
198 ecf69ddf 2022-10-20 op uint32_t hash;
199 ecf69ddf 2022-10-20 op uint8_t off;
200 a5b7d2ee 2023-05-21 op int ch, digits = 6, period = 30;
201 ecf69ddf 2022-10-20 op
202 ecf69ddf 2022-10-20 op if (pledge("stdio", NULL) == -1)
203 ecf69ddf 2022-10-20 op err(1, "pledge");
204 ecf69ddf 2022-10-20 op
205 ecf69ddf 2022-10-20 op while ((ch = getopt(argc, argv, "")) != -1) {
206 ecf69ddf 2022-10-20 op switch (ch) {
207 ecf69ddf 2022-10-20 op default:
208 ecf69ddf 2022-10-20 op usage();
209 ecf69ddf 2022-10-20 op }
210 ecf69ddf 2022-10-20 op }
211 ecf69ddf 2022-10-20 op argc -= optind;
212 ecf69ddf 2022-10-20 op argv += optind;
213 ecf69ddf 2022-10-20 op
214 ecf69ddf 2022-10-20 op if (argc != 0)
215 ecf69ddf 2022-10-20 op usage();
216 ecf69ddf 2022-10-20 op
217 d2c2e549 2023-05-21 op alg = EVP_sha1();
218 d2c2e549 2023-05-21 op
219 ecf69ddf 2022-10-20 op linelen = getline(&line, &linesize, stdin);
220 ecf69ddf 2022-10-20 op if (linelen == -1) {
221 ecf69ddf 2022-10-20 op if (ferror(stdin))
222 ecf69ddf 2022-10-20 op err(1, "getline");
223 ecf69ddf 2022-10-20 op errx(1, "no secret provided");
224 ecf69ddf 2022-10-20 op }
225 ecf69ddf 2022-10-20 op for (s = q = line; *s != '\0'; ++s) {
226 ecf69ddf 2022-10-20 op if (isspace((unsigned char)*s)) {
227 ecf69ddf 2022-10-20 op linelen--;
228 ecf69ddf 2022-10-20 op continue;
229 ecf69ddf 2022-10-20 op }
230 ecf69ddf 2022-10-20 op *q++ = *s;
231 ecf69ddf 2022-10-20 op }
232 ecf69ddf 2022-10-20 op *q = '\0';
233 ecf69ddf 2022-10-20 op if (linelen < 1)
234 ecf69ddf 2022-10-20 op errx(1, "no secret provided");
235 ecf69ddf 2022-10-20 op
236 efd5f30c 2023-05-21 op if (!strncmp(line, "otpauth://", 10) &&
237 a5b7d2ee 2023-05-21 op uri2secret(line, &digits, &alg, &period) == -1)
238 f31cd5a4 2023-01-16 op errx(1, "failed to decode otpauth URI");
239 f31cd5a4 2023-01-16 op
240 cc5f172a 2022-10-25 op if ((buflen = b32decode(line, buf, sizeof(buf))) == 0)
241 ecf69ddf 2022-10-20 op err(1, "can't base32 decode the secret");
242 ecf69ddf 2022-10-20 op
243 a5b7d2ee 2023-05-21 op ct = htobe64(time(NULL) / period);
244 ecf69ddf 2022-10-20 op
245 d2c2e549 2023-05-21 op HMAC(alg, buf, buflen, (unsigned char *)&ct, sizeof(ct), md, &mdlen);
246 ecf69ddf 2022-10-20 op
247 ecf69ddf 2022-10-20 op off = md[mdlen - 1] & 0x0F;
248 ecf69ddf 2022-10-20 op
249 ecf69ddf 2022-10-20 op memcpy(&hash, md + off, sizeof(hash));
250 ecf69ddf 2022-10-20 op hash = be32toh(hash);
251 ecf69ddf 2022-10-20 op
252 efd5f30c 2023-05-21 op switch (digits) {
253 efd5f30c 2023-05-21 op case 6:
254 efd5f30c 2023-05-21 op printf("%06d\n", (hash & 0x7FFFFFFF) % 1000000);
255 efd5f30c 2023-05-21 op break;
256 efd5f30c 2023-05-21 op case 7:
257 efd5f30c 2023-05-21 op printf("%07d\n", (hash & 0x7FFFFFFF) % 10000000);
258 efd5f30c 2023-05-21 op break;
259 efd5f30c 2023-05-21 op case 8:
260 efd5f30c 2023-05-21 op printf("%08d\n", (hash & 0x7FFFFFFF) % 100000000);
261 efd5f30c 2023-05-21 op break;
262 efd5f30c 2023-05-21 op }
263 efd5f30c 2023-05-21 op
264 ecf69ddf 2022-10-20 op free(line);
265 ecf69ddf 2022-10-20 op return (0);
266 ecf69ddf 2022-10-20 op }