Changeset 786 in openpam
- Timestamp:
- Mar 10, 2014, 3:37:55 PM (7 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/man/Makefile.am
r781 r786 67 67 oath_key_dummy.3 \ 68 68 oath_key_free.3 \ 69 oath_key_from_file.3 \ 69 70 oath_key_from_uri.3 \ 70 71 oath_uri_decode.3 \ -
trunk/lib/liboath/Makefile.am
r782 r786 15 15 oath_key_create.c \ 16 16 oath_key_dummy.c \ 17 oath_key_from_file.c \ 17 18 oath_key_from_uri.c \ 18 19 oath_key_free.c \ -
trunk/lib/liboath/oath_key.c
r772 r786 37 37 38 38 #include <inttypes.h> 39 #include <stdio.h>40 39 #include <stdlib.h> 41 40 … … 46 45 47 46 #include <security/oath.h> 48 49 struct oath_key *50 oath_key_from_file(const char *filename)51 {52 struct oath_key *key;53 FILE *f;54 char *line;55 size_t len;56 57 if ((f = fopen(filename, "r")) == NULL)58 return (NULL);59 /* get first non-empty non-comment line */60 line = openpam_readline(f, NULL, &len);61 if (strlcmp("otpauth://", line, len) == 0) {62 key = oath_key_from_uri(line);63 } else {64 openpam_log(PAM_LOG_ERROR,65 "unrecognized key file format: %s", filename);66 key = NULL;67 }68 fclose(f);69 return (key);70 }71 47 72 48 char * -
trunk/lib/liboath/oath_key_from_file.c
r783 r786 47 47 #include <security/oath.h> 48 48 49 /* 50 * OATH 51 * 52 * Loads an OATH key from a file 53 */ 54 49 55 struct oath_key * 50 56 oath_key_from_file(const char *filename) … … 70 76 } 71 77 72 char * 73 oath_key_to_uri(const struct oath_key *key) 74 { 75 const char *hash; 76 char *tmp, *uri; 77 size_t kslen, urilen; 78 79 switch (key->hash) { 80 case oh_sha1: 81 hash = "SHA1"; 82 break; 83 case oh_sha256: 84 hash = "SHA256"; 85 break; 86 case oh_sha512: 87 hash = "SHA512"; 88 break; 89 case oh_md5: 90 hash = "MD5"; 91 break; 92 default: 93 return (NULL); 94 } 95 96 /* XXX the label should be URI-encoded */ 97 if (key->mode == om_hotp) { 98 urilen = asprintf(&uri, "otpauth://%s/%s?" 99 "algorithm=%s&digits=%d&counter=%ju&secret=", 100 "hotp", key->label, hash, key->digits, 101 (uintmax_t)key->counter); 102 } else if (key->mode == om_totp) { 103 urilen = asprintf(&uri, "otpauth://%s/%s?" 104 "algorithm=%s&digits=%d&period=%u&lastused=%ju&secret=", 105 "totp", key->label, hash, key->digits, key->timestep, 106 (uintmax_t)key->lastused); 107 } else { 108 /* unreachable */ 109 return (NULL); 110 } 111 112 /* compute length of base32-encoded key and append it */ 113 kslen = base32_enclen(key->keylen) + 1; 114 if ((tmp = realloc(uri, urilen + kslen)) == NULL) { 115 free(uri); 116 return (NULL); 117 } 118 uri = tmp; 119 if (base32_enc((char *)key->key, key->keylen, uri + urilen, &kslen) != 0) { 120 free(uri); 121 return (NULL); 122 } 123 124 return (uri); 125 } 78 /** 79 * The =oath_key_from_file function loads a key from the specified file. 80 * The file format is automatically detected. 81 * 82 * The following key file formats are supported: 83 * 84 * - otpauth URI 85 * 86 * Keys created with =oath_key_from_file must be freed using 87 * =oath_key_free. 88 * 89 * >oath_key_alloc 90 * >oath_key_free 91 * >oath_key_from_uri 92 * 93 * AUTHOR UIO 94 */
Note: See TracChangeset
for help on using the changeset viewer.