From 92d483a21a5beae5c546482f103f96c9837f976d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Thu, 28 Feb 2013 12:12:53 +0000 Subject: [PATCH] Implement URI encoding. git-svn-id: svn+ssh://svn.openpam.org/svn/openpam/trunk@628 185d5e19-27fe-0310-9dcf-9bff6b9f3609 --- modules/pam_oath/oath_key.c | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/modules/pam_oath/oath_key.c b/modules/pam_oath/oath_key.c index 11f89c6..eccab9d 100644 --- a/modules/pam_oath/oath_key.c +++ b/modules/pam_oath/oath_key.c @@ -46,6 +46,7 @@ #include #include +#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); +}