Skip to content

Commit

Permalink
Consolidate almost-identical set_[ht]otp_mode() functions
Browse files Browse the repository at this point in the history
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
  • Loading branch information
dwmw2 committed Dec 29, 2019
1 parent dc1a2aa commit 33a87b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 80 deletions.
9 changes: 4 additions & 5 deletions library.c
Expand Up @@ -874,15 +874,14 @@ int openconnect_set_token_mode(struct openconnect_info *vpninfo,
case OC_TOKEN_MODE_NONE:
return 0;

case OC_TOKEN_MODE_TOTP:
case OC_TOKEN_MODE_HOTP:
return set_oath_mode(vpninfo, token_str, token_mode);

#ifdef HAVE_LIBSTOKEN
case OC_TOKEN_MODE_STOKEN:
return set_libstoken_mode(vpninfo, token_str);
#endif
case OC_TOKEN_MODE_TOTP:
return set_totp_mode(vpninfo, token_str);

case OC_TOKEN_MODE_HOTP:
return set_hotp_mode(vpninfo, token_str);
#ifdef HAVE_LIBPCSCLITE
case OC_TOKEN_MODE_YUBIOATH:
return set_yubikey_mode(vpninfo, token_str);
Expand Down
97 changes: 24 additions & 73 deletions oath.c
Expand Up @@ -217,7 +217,8 @@ static int pskc_decode(struct openconnect_info *vpninfo, const char *token_str,
#endif /* HAVE_LIBPSKC */
}

int set_totp_mode(struct openconnect_info *vpninfo, const char *token_str)
int set_oath_mode(struct openconnect_info *vpninfo, const char *token_str,
oc_token_mode_t token_mode)
{
int ret, toklen;

Expand All @@ -230,62 +231,10 @@ int set_totp_mode(struct openconnect_info *vpninfo, const char *token_str)

if (strncmp(token_str, "<?xml", 5) == 0) {
vpninfo->hotp_secret_format = HOTP_SECRET_PSKC;
ret = pskc_decode(vpninfo, token_str, toklen, OC_TOKEN_MODE_TOTP);
ret = pskc_decode(vpninfo, token_str, toklen, token_mode);
if (ret)
return -EINVAL;
vpninfo->token_mode = OC_TOKEN_MODE_TOTP;
return 0;
}
if (!strncasecmp(token_str, "sha1:", 5)) {
token_str += 5;
toklen -= 5;
vpninfo->oath_hmac_alg = OATH_ALG_HMAC_SHA1;
} else if (!strncasecmp(token_str, "sha256:", 7)) {
token_str += 7;
toklen -= 7;
vpninfo->oath_hmac_alg = OATH_ALG_HMAC_SHA256;
} else if (!strncasecmp(token_str, "sha512:", 7)) {
token_str += 7;
toklen -= 7;
vpninfo->oath_hmac_alg = OATH_ALG_HMAC_SHA512;
} else
vpninfo->oath_hmac_alg = OATH_ALG_HMAC_SHA1;

if (strncasecmp(token_str, "base32:", strlen("base32:")) == 0) {
ret = decode_base32(vpninfo, token_str + strlen("base32:"),
toklen - strlen("base32:"));
if (ret)
return ret;
} else if (strncmp(token_str, "0x", 2) == 0) {
vpninfo->oath_secret_len = (toklen - 2) / 2;
vpninfo->oath_secret = parse_hex(token_str + 2, toklen - 2);
if (!vpninfo->oath_secret)
return -EINVAL;
} else {
vpninfo->oath_secret = strdup(token_str);
vpninfo->oath_secret_len = toklen;
}

vpninfo->token_mode = OC_TOKEN_MODE_TOTP;
return 0;
}

int set_hotp_mode(struct openconnect_info *vpninfo, const char *token_str)
{
int ret, toklen;
char *p;

if (!token_str)
return -EINVAL;

toklen = strlen(token_str);

if (strncmp(token_str, "<?xml", 5) == 0) {
vpninfo->hotp_secret_format = HOTP_SECRET_PSKC;
ret = pskc_decode(vpninfo, token_str, toklen, OC_TOKEN_MODE_HOTP);
if (ret)
return -EINVAL;
vpninfo->token_mode = OC_TOKEN_MODE_HOTP;
vpninfo->token_mode = token_mode;
return 0;
}

Expand All @@ -304,25 +253,27 @@ int set_hotp_mode(struct openconnect_info *vpninfo, const char *token_str)
} else
vpninfo->oath_hmac_alg = OATH_ALG_HMAC_SHA1;

p = strrchr(token_str, ',');
if (p) {
long counter;
toklen = p - token_str;
p++;
counter = strtol(p, &p, 0);
if (counter < 0)
return -EINVAL;
while (*p) {
if (isspace((int)(unsigned char)*p))
p++;
else
if (token_mode == OC_TOKEN_MODE_HOTP) {
char *p = strrchr(token_str, ',');
if (p) {
long counter;
toklen = p - token_str;
p++;
counter = strtol(p, &p, 0);
if (counter < 0)
return -EINVAL;
while (*p) {
if (isspace((int)(unsigned char)*p))
p++;
else
return -EINVAL;
}
vpninfo->token_time = counter;
} else {
while (toklen &&
isspace((int)(unsigned char)token_str[toklen-1]))
toklen--;
}
vpninfo->token_time = counter;
} else {
while (toklen &&
isspace((int)(unsigned char)token_str[toklen-1]))
toklen--;
}

if (strncasecmp(token_str, "base32:", strlen("base32:")) == 0) {
Expand All @@ -343,7 +294,7 @@ int set_hotp_mode(struct openconnect_info *vpninfo, const char *token_str)
vpninfo->oath_secret_len = toklen;
}

vpninfo->token_mode = OC_TOKEN_MODE_HOTP;
vpninfo->token_mode = token_mode;
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions openconnect-internal.h
Expand Up @@ -1030,8 +1030,8 @@ ssize_t read_file_into_string(struct openconnect_info *vpninfo, const char *fnam
int config_lookup_host(struct openconnect_info *vpninfo, const char *host);

/* oath.c */
int set_totp_mode(struct openconnect_info *vpninfo, const char *token_str);
int set_hotp_mode(struct openconnect_info *vpninfo, const char *token_str);
int set_oath_mode(struct openconnect_info *vpninfo, const char *token_str,
oc_token_mode_t token_mode);
int can_gen_totp_code(struct openconnect_info *vpninfo,
struct oc_auth_form *form,
struct oc_form_opt *opt);
Expand Down

0 comments on commit 33a87b1

Please sign in to comment.