1 | /*- |
---|
2 | * Copyright (c) 2013 Universitetet i Oslo |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 3. The name of the author may not be used to endorse or promote |
---|
14 | * products derived from this software without specific prior written |
---|
15 | * permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * $Id: oath_key.c 786 2014-03-10 15:37:55Z des $ |
---|
30 | */ |
---|
31 | |
---|
32 | #ifdef HAVE_CONFIG_H |
---|
33 | # include "config.h" |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <sys/types.h> |
---|
37 | |
---|
38 | #include <inttypes.h> |
---|
39 | #include <stdlib.h> |
---|
40 | |
---|
41 | #include <security/pam_appl.h> |
---|
42 | #include <security/openpam.h> |
---|
43 | |
---|
44 | #include "openpam_strlcmp.h" |
---|
45 | |
---|
46 | #include <security/oath.h> |
---|
47 | |
---|
48 | char * |
---|
49 | oath_key_to_uri(const struct oath_key *key) |
---|
50 | { |
---|
51 | const char *hash; |
---|
52 | char *tmp, *uri; |
---|
53 | size_t kslen, urilen; |
---|
54 | |
---|
55 | switch (key->hash) { |
---|
56 | case oh_sha1: |
---|
57 | hash = "SHA1"; |
---|
58 | break; |
---|
59 | case oh_sha256: |
---|
60 | hash = "SHA256"; |
---|
61 | break; |
---|
62 | case oh_sha512: |
---|
63 | hash = "SHA512"; |
---|
64 | break; |
---|
65 | case oh_md5: |
---|
66 | hash = "MD5"; |
---|
67 | break; |
---|
68 | default: |
---|
69 | return (NULL); |
---|
70 | } |
---|
71 | |
---|
72 | /* XXX the label should be URI-encoded */ |
---|
73 | if (key->mode == om_hotp) { |
---|
74 | urilen = asprintf(&uri, "otpauth://%s/%s?" |
---|
75 | "algorithm=%s&digits=%d&counter=%ju&secret=", |
---|
76 | "hotp", key->label, hash, key->digits, |
---|
77 | (uintmax_t)key->counter); |
---|
78 | } else if (key->mode == om_totp) { |
---|
79 | urilen = asprintf(&uri, "otpauth://%s/%s?" |
---|
80 | "algorithm=%s&digits=%d&period=%u&lastused=%ju&secret=", |
---|
81 | "totp", key->label, hash, key->digits, key->timestep, |
---|
82 | (uintmax_t)key->lastused); |
---|
83 | } else { |
---|
84 | /* unreachable */ |
---|
85 | return (NULL); |
---|
86 | } |
---|
87 | |
---|
88 | /* compute length of base32-encoded key and append it */ |
---|
89 | kslen = base32_enclen(key->keylen) + 1; |
---|
90 | if ((tmp = realloc(uri, urilen + kslen)) == NULL) { |
---|
91 | free(uri); |
---|
92 | return (NULL); |
---|
93 | } |
---|
94 | uri = tmp; |
---|
95 | if (base32_enc((char *)key->key, key->keylen, uri + urilen, &kslen) != 0) { |
---|
96 | free(uri); |
---|
97 | return (NULL); |
---|
98 | } |
---|
99 | |
---|
100 | return (uri); |
---|
101 | } |
---|