Implement URI encoding.

git-svn-id: svn+ssh://svn.openpam.org/svn/openpam/trunk@628 185d5e19-27fe-0310-9dcf-9bff6b9f3609
This commit is contained in:
Dag-Erling Smørgrav 2013-02-28 12:12:53 +00:00
parent 16e805fc4c
commit 92d483a21a
1 changed files with 54 additions and 0 deletions

View File

@ -46,6 +46,7 @@
#include <security/pam_appl.h>
#include <security/openpam.h>
#include "openpam_asprintf.h"
#include "openpam_strlcmp.h"
#include "oath.h"
@ -254,3 +255,56 @@ oath_key_from_file(const char *filename)
fclose(f);
return (key);
}
char *
oath_key_to_uri(const struct oath_key *key)
{
const char *hash;
char *tmp, *uri;
size_t kslen, urilen;
switch (key->hash) {
case oh_sha1:
hash = "SHA1";
break;
case oh_sha256:
hash = "SHA256";
break;
case oh_sha512:
hash = "SHA512";
break;
case oh_md5:
hash = "MD5";
break;
default:
return (NULL);
}
if (key->mode == om_hotp) {
urilen = asprintf(&uri, "otpauth://"
"%s/%s?algorithm=%s&digits=%d&counter=%ju&secret=",
"hotp", key->label, hash, key->digits,
(uintmax_t)key->counter);
} else if (key->mode == om_totp) {
urilen = asprintf(&uri, "otpauth://"
"%s/%s?algorithm=%s&digits=%d&period=%u&secret=",
"totp", key->label, hash, key->digits, key->timestep);
} else {
/* unreachable */
return (NULL);
}
/* compute length of base32-encoded key and append it */
kslen = base32_enclen(key->keylen);
if ((tmp = realloc(uri, urilen + kslen + 1)) == NULL) {
free(uri);
return (NULL);
}
uri = tmp;
if (base32_enc(key->key, key->keylen, uri + urilen, &kslen) != 0) {
free(uri);
return (NULL);
}
return (uri);
}