Add partial support for issuer parameter.

We can read the issuer from a URI (as a separate parameter, not as a prefix to the label) and store it, but not yet output it.  That will be implemented in a future rewrite of oath_key_to_uri().
This commit is contained in:
Dag-Erling Smørgrav 2018-04-25 23:42:48 +02:00
parent 4576565fd1
commit 54c67f337a
6 changed files with 33 additions and 7 deletions

View file

@ -54,8 +54,8 @@ const char *cryb_oath_version(void);
#define oath_mode_value cryb_oath_mode_value
struct oath_key *oath_key_alloc(void);
struct oath_key *oath_key_create(const char *, enum oath_mode,
enum oath_hash, const char *, size_t);
struct oath_key *oath_key_create(const char *, const char *,
enum oath_mode, enum oath_hash, const char *, size_t);
void oath_key_free(struct oath_key *);
struct oath_key *oath_key_from_uri(const char *);
struct oath_key *oath_key_from_file(const char *);

View file

@ -76,15 +76,21 @@ enum oath_hash {
*/
#define OATH_MAX_KEYLEN 64
/*
* Maximum issuer length in characters, including terminating NUL.
*/
#define OATH_MAX_ISSUERLEN 64
/*
* Maximum label length in characters, including terminating NUL.
*/
#define OATH_MAX_LABELLEN 64
/*
* Label to use for dummy keys
* Issuer and label to use for dummy keys.
*/
#define OATH_DUMMY_LABEL "oath-dummy@cryb.to"
#define OATH_DUMMY_ISSUER "cryb-oath"
#define OATH_DUMMY_LABEL "dummy@cryb.to"
CRYB_END

View file

@ -55,6 +55,10 @@ struct oath_key {
/* hash algorithm */
enum oath_hash hash;
/* issuer */
size_t issuerlen; /* bytes incl. NUL */
char issuer[OATH_MAX_ISSUERLEN];
/* label */
size_t labellen; /* bytes incl. NUL */
char label[OATH_MAX_LABELLEN];

View file

@ -44,13 +44,18 @@
*/
struct oath_key *
oath_key_create(const char *label,
oath_key_create(const char *issuer, const char *label,
enum oath_mode mode, enum oath_hash hash,
const char *keydata, size_t keylen)
{
char keybuf[OATH_MAX_KEYLEN];
struct oath_key *key;
int labellen;
int issuerlen, labellen;
/* check issuer */
if (issuer == NULL ||
(issuerlen = strlen(issuer)) >= OATH_MAX_ISSUERLEN)
return (NULL);
/* check label */
if (label == NULL ||
@ -98,6 +103,11 @@ oath_key_create(const char *label,
if ((key = oath_key_alloc()) == NULL)
return (NULL);
/* issuer */
memcpy(key->issuer, issuer, issuerlen);
key->issuer[issuerlen] = 0;
key->issuerlen = issuerlen;
/* label */
memcpy(key->label, label, labellen);
key->label[labellen] = 0;

View file

@ -53,6 +53,8 @@ oath_key_dummy(enum oath_mode mode, enum oath_hash hash, unsigned int digits)
key->counter = 0;
key->timestep = 30;
key->hash = hash;
memcpy(key->issuer, OATH_DUMMY_ISSUER, sizeof OATH_DUMMY_ISSUER);
key->issuerlen = sizeof OATH_DUMMY_ISSUER - 1;
memcpy(key->label, OATH_DUMMY_LABEL, sizeof OATH_DUMMY_LABEL);
key->labellen = sizeof OATH_DUMMY_LABEL - 1;
key->keylen = sizeof key->key;

View file

@ -36,6 +36,7 @@
#include <cryb/rfc3986.h>
#include <cryb/rfc4648.h>
#include <cryb/strlcmp.h>
#include <cryb/strlcpy.h>
#include <cryb/oath.h>
/*
@ -157,7 +158,10 @@ oath_key_from_uri(const char *uri)
goto invalid;
key->timestep = n;
} else if (strcmp("issuer", name) == 0) {
// noop for now
key->issuerlen = strlcpy(key->issuer, value,
sizeof key->issuer);
if (key->issuerlen > sizeof key->issuer)
goto invalid;
} else {
goto invalid;
}