mirror of
https://github.com/cryb-to/cryb-to.git
synced 2024-12-18 02:24:54 +00:00
Complete rewrite of oath_key_to_uri().
- The API has changed so that the function now writes its output into a caller-provided buffer, in a style similar to libcryb-enc. - All parameter values are now correctly percent-encoded. - The issuer parameter is now supported.
This commit is contained in:
parent
48fc358df7
commit
bd4b5c246e
2 changed files with 110 additions and 40 deletions
|
@ -57,7 +57,7 @@ int oath_key_create(oath_key *, oath_mode, oath_hash, unsigned int,
|
||||||
void oath_key_destroy(oath_key *);
|
void oath_key_destroy(oath_key *);
|
||||||
int oath_key_dummy(oath_key *, oath_mode, oath_hash, unsigned int);
|
int oath_key_dummy(oath_key *, oath_mode, oath_hash, unsigned int);
|
||||||
int oath_key_from_uri(oath_key *, const char *);
|
int oath_key_from_uri(oath_key *, const char *);
|
||||||
char *oath_key_to_uri(const oath_key *);
|
int oath_key_to_uri(const oath_key *, char *, size_t *);
|
||||||
|
|
||||||
const char *oath_mode_name(oath_mode);
|
const char *oath_mode_name(oath_mode);
|
||||||
oath_mode oath_mode_value(const char *);
|
oath_mode oath_mode_value(const char *);
|
||||||
|
|
|
@ -29,24 +29,96 @@
|
||||||
|
|
||||||
#include "cryb/impl.h"
|
#include "cryb/impl.h"
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <inttypes.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
|
#include <cryb/rfc3986.h>
|
||||||
#include <cryb/rfc4648.h>
|
#include <cryb/rfc4648.h>
|
||||||
#include <cryb/strlcmp.h>
|
|
||||||
#include <cryb/oath.h>
|
#include <cryb/oath.h>
|
||||||
|
|
||||||
char *
|
static inline void
|
||||||
oath_key_to_uri(const oath_key *key)
|
append_char(char *buf, size_t size, size_t *pos, int ch)
|
||||||
{
|
{
|
||||||
const char *hash;
|
|
||||||
char *tmp, *uri;
|
|
||||||
size_t kslen, urilen;
|
|
||||||
|
|
||||||
switch (key->hash) {
|
if (*pos + 1 < size)
|
||||||
|
buf[*pos] = ch;
|
||||||
|
(*pos)++;
|
||||||
|
if (*pos < size)
|
||||||
|
buf[*pos] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
append_str(char *buf, size_t size, size_t *pos, const char *str)
|
||||||
|
{
|
||||||
|
|
||||||
|
while (*str != '\0') {
|
||||||
|
if (*pos + 1 < size)
|
||||||
|
buf[*pos] = *str++;
|
||||||
|
(*pos)++;
|
||||||
|
}
|
||||||
|
if (*pos < size)
|
||||||
|
buf[*pos] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
append_num(char *buf, size_t size, size_t *pos, uintmax_t num)
|
||||||
|
{
|
||||||
|
char numbuf[32], *p;
|
||||||
|
|
||||||
|
p = numbuf + sizeof numbuf - 1;
|
||||||
|
*p-- = '\0';
|
||||||
|
do {
|
||||||
|
*p-- = '0' + num % 10;
|
||||||
|
num /= 10;
|
||||||
|
} while (num > 0);
|
||||||
|
append_str(buf, size, pos, p + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
append_percent(char *buf, size_t size, size_t *pos, const char *str)
|
||||||
|
{
|
||||||
|
size_t res;
|
||||||
|
|
||||||
|
res = *pos < size ? size - *pos : 0;
|
||||||
|
percent_encode(str, SIZE_MAX, buf + *pos, &res);
|
||||||
|
*pos += res - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
append_base32(char *buf, size_t size, size_t *pos,
|
||||||
|
const uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
|
size_t res;
|
||||||
|
|
||||||
|
res = *pos < size ? size - *pos : 0;
|
||||||
|
base32_encode(data, len, buf + *pos, &res);
|
||||||
|
*pos += res - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
oath_key_to_uri(const oath_key *ok, char *buf, size_t *size)
|
||||||
|
{
|
||||||
|
const char *mode, *hash;
|
||||||
|
size_t pos;
|
||||||
|
|
||||||
|
pos = 0;
|
||||||
|
append_str(buf, *size, &pos, "otpauth://");
|
||||||
|
switch (ok->mode) {
|
||||||
|
case om_hotp:
|
||||||
|
mode = "hotp";
|
||||||
|
break;
|
||||||
|
case om_totp:
|
||||||
|
mode = "totp";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
append_str(buf, *size, &pos, mode);
|
||||||
|
append_char(buf, *size, &pos, '/');
|
||||||
|
append_percent(buf, *size, &pos, ok->label);
|
||||||
|
append_str(buf, *size, &pos, "?algorithm=");
|
||||||
|
switch (ok->hash) {
|
||||||
case oh_sha1:
|
case oh_sha1:
|
||||||
hash = "SHA1";
|
hash = "SHA1";
|
||||||
break;
|
break;
|
||||||
|
@ -60,36 +132,34 @@ oath_key_to_uri(const oath_key *key)
|
||||||
hash = "MD5";
|
hash = "MD5";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return (NULL);
|
return (0);
|
||||||
}
|
}
|
||||||
|
append_str(buf, *size, &pos, hash);
|
||||||
/* XXX the label and secret should be URI-encoded */
|
append_str(buf, *size, &pos, "&digits=");
|
||||||
if (key->mode == om_hotp) {
|
append_num(buf, *size, &pos, ok->digits);
|
||||||
urilen = asprintf(&uri, "otpauth://%s/%s?"
|
if (ok->mode == om_hotp) {
|
||||||
"algorithm=%s&digits=%d&counter=%ju&secret=",
|
append_str(buf, *size, &pos, "&counter=");
|
||||||
"hotp", key->label, hash, key->digits,
|
append_num(buf, *size, &pos, (uintmax_t)ok->counter);
|
||||||
(uintmax_t)key->counter);
|
} else if (ok->mode == om_totp) {
|
||||||
} else if (key->mode == om_totp) {
|
append_str(buf, *size, &pos, "&period=");
|
||||||
urilen = asprintf(&uri, "otpauth://%s/%s?"
|
append_num(buf, *size, &pos, (uintmax_t)ok->timestep);
|
||||||
"algorithm=%s&digits=%d&period=%u&lastused=%ju&secret=",
|
append_str(buf, *size, &pos, "&lastused=");
|
||||||
"totp", key->label, hash, key->digits, key->timestep,
|
append_num(buf, *size, &pos, (uintmax_t)ok->lastused);
|
||||||
(uintmax_t)key->lastused);
|
|
||||||
} else {
|
} else {
|
||||||
/* unreachable */
|
return (0);
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
if (ok->issuerlen > 0) {
|
||||||
/* compute length of base32-encoded key and append it */
|
append_str(buf, *size, &pos, "&issuer=");
|
||||||
kslen = base32_enclen(key->keylen) + 1;
|
append_percent(buf, *size, &pos, ok->issuer);
|
||||||
if ((tmp = realloc(uri, urilen + kslen)) == NULL) {
|
|
||||||
free(uri);
|
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
uri = tmp;
|
append_str(buf, *size, &pos, "&secret=");
|
||||||
if (base32_encode(key->key, key->keylen, uri + urilen, &kslen) != 0) {
|
append_base32(buf, *size, &pos, ok->key, ok->keylen);
|
||||||
free(uri);
|
pos++; // terminating NUL
|
||||||
return (NULL);
|
if (pos > *size) {
|
||||||
|
*size = pos;
|
||||||
|
errno = ENOSPC;
|
||||||
|
return (-1);
|
||||||
}
|
}
|
||||||
|
*size = pos;
|
||||||
return (uri);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue