cli.c: rename key{,len} -> secret{,len} and free after use

This commit is contained in:
Lucas 2021-02-16 17:01:25 +00:00
parent dd84a23e7e
commit 87e8c2862c
1 changed files with 10 additions and 9 deletions

19
cli.c
View File

@ -42,7 +42,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
const char *errstr; const char *errstr;
unsigned char *key; unsigned char *secret;
char *in, *line; char *in, *line;
struct otpcfg otpcfg; struct otpcfg otpcfg;
size_t inlen, linesz; size_t inlen, linesz;
@ -50,7 +50,7 @@ main(int argc, char *argv[])
uint64_t counter; uint64_t counter;
unsigned int period; unsigned int period;
int32_t r; int32_t r;
int ch, digits, do_hotp, do_totp, keylen; int ch, digits, do_hotp, do_totp, secretlen;
enum otp_hmac hmac; enum otp_hmac hmac;
counter = (uint64_t)time(NULL); counter = (uint64_t)time(NULL);
@ -124,19 +124,19 @@ main(int argc, char *argv[])
inlen = linelen; inlen = linelen;
} }
keylen = b32_decoded_len(in, inlen); secretlen = b32_decoded_len(in, inlen);
if (keylen == -1) if (secretlen == -1)
errx(1, "invalid base32 string: %s", in); errx(1, "invalid base32 string: %s", in);
key = malloc(keylen); secret = malloc(secretlen);
if (key == NULL) if (secret == NULL)
err(1, "malloc"); err(1, "malloc");
if (!b32_decode(key, keylen, in, inlen)) if (!b32_decode(secret, secretlen, in, inlen))
errx(1, "error decoding base32 string"); errx(1, "error decoding base32 string");
otpcfg.algorithm = hmac; otpcfg.algorithm = hmac;
otpcfg.digits = digits; otpcfg.digits = digits;
otpcfg.secret = key; otpcfg.secret = secret;
otpcfg.secretlen = keylen; otpcfg.secretlen = secretlen;
if (do_hotp) { if (do_hotp) {
otpcfg.type = OTP_HOTP; otpcfg.type = OTP_HOTP;
otpcfg.u.hotp.counter = counter; otpcfg.u.hotp.counter = counter;
@ -147,6 +147,7 @@ main(int argc, char *argv[])
} }
r = otp(&otpcfg); r = otp(&otpcfg);
free(secret);
if (r == -1) if (r == -1)
errx(1, "couldn't calculate %cOTP", do_hotp ? 'H' : 'T'); errx(1, "couldn't calculate %cOTP", do_hotp ? 'H' : 'T');
printf("%0*" PRId32 "\n", digits, r); printf("%0*" PRId32 "\n", digits, r);