Blob


1 /*
2 * Copyright (c) 2022, 2023 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <ctype.h>
18 #include <err.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <unistd.h>
26 #include <openssl/evp.h>
27 #include <openssl/hmac.h>
29 #ifndef __OpenBSD__
30 # define pledge(a, b) (0)
31 #endif
33 #ifndef __dead
34 # define __dead __attribute__((noreturn))
35 #endif
37 #if defined(__FreeBSD__)
38 # include <sys/endian.h>
39 #elif defined(__APPLE__)
40 # include <machine/endian.h>
41 # include <libkern/OSByteOrder.h>
42 # define htobe64(x) OSSwapHostToBigInt64(x)
43 # define be32toh(x) OSSwapBigToHostInt32(x)
44 #else
45 # include <endian.h>
46 #endif
48 static __dead void
49 usage(void)
50 {
51 fprintf(stderr, "usage: %s\n", getprogname());
52 exit(1);
53 }
55 static int
56 b32c(unsigned char c)
57 {
58 if (c >= 'A' && c <= 'Z')
59 return (c - 'A');
60 if (c >= '2' && c <= '7')
61 return (c - '2' + 26);
62 errno = EINVAL;
63 return (-1);
64 }
66 static size_t
67 b32decode(const char *s, char *q, size_t qlen)
68 {
69 int i, val[8];
70 char *t = q;
72 while (*s != '\0') {
73 memset(val, 0, sizeof(val));
74 for (i = 0; i < 8; ++i) {
75 if (*s == '\0')
76 break;
77 if ((val[i] = b32c(*s)) == -1)
78 return (0);
79 s++;
80 }
82 if (qlen < 5) {
83 errno = ENOSPC;
84 return (0);
85 }
86 qlen -= 5;
88 *q++ = (val[0] << 3) | (val[1] >> 2);
89 *q++ = ((val[1] & 0x03) << 6) | (val[2] << 1) | (val[3] >> 4);
90 *q++ = ((val[3] & 0x0F) << 4) | (val[4] >> 1);
91 *q++ = ((val[4] & 0x01) << 7) | (val[5] << 2) | (val[6] >> 3);
92 *q++ = ((val[6] & 0x07) << 5) | val[7];
93 }
95 return (q - t);
96 }
98 static int
99 uri2secret(char *s)
101 char *q, *t;
103 if ((q = strchr(s, '?')) == NULL)
104 return (-1);
105 if ((t = strstr(q, "?secret=")) == NULL &&
106 (t = strstr(q, "&secret=")) == NULL)
107 return (-1);
108 t += 8;
109 while (*t != '\0' && *t != '&' && *t != '#')
110 *s++ = *t++;
111 *s = '\0';
112 return (0);
115 int
116 main(int argc, char **argv)
118 char buf[1024];
119 size_t buflen;
120 unsigned char md[EVP_MAX_MD_SIZE];
121 unsigned int mdlen;
122 char *s, *q, *line = NULL;
123 size_t linesize = 0;
124 ssize_t linelen;
125 uint64_t ct;
126 uint32_t hash;
127 uint8_t off;
128 int ch;
130 if (pledge("stdio", NULL) == -1)
131 err(1, "pledge");
133 while ((ch = getopt(argc, argv, "")) != -1) {
134 switch (ch) {
135 default:
136 usage();
139 argc -= optind;
140 argv += optind;
142 if (argc != 0)
143 usage();
145 linelen = getline(&line, &linesize, stdin);
146 if (linelen == -1) {
147 if (ferror(stdin))
148 err(1, "getline");
149 errx(1, "no secret provided");
151 for (s = q = line; *s != '\0'; ++s) {
152 if (isspace((unsigned char)*s)) {
153 linelen--;
154 continue;
156 *q++ = *s;
158 *q = '\0';
159 if (linelen < 1)
160 errx(1, "no secret provided");
162 if (!strncmp(line, "otpauth://", 10) && uri2secret(line) == -1)
163 errx(1, "failed to decode otpauth URI");
165 if ((buflen = b32decode(line, buf, sizeof(buf))) == 0)
166 err(1, "can't base32 decode the secret");
168 ct = htobe64(time(NULL) / 30);
170 HMAC(EVP_sha1(), buf, buflen, (unsigned char *)&ct, sizeof(ct),
171 md, &mdlen);
173 off = md[mdlen - 1] & 0x0F;
175 memcpy(&hash, md + off, sizeof(hash));
176 hash = be32toh(hash);
177 printf("%06d\n", (hash & 0x7FFFFFFF) % 1000000);
179 free(line);
180 return (0);