Changeset 678 in openpam


Ignore:
Timestamp:
Mar 18, 2013, 9:34:29 PM (8 years ago)
Author:
Dag-Erling Smørgrav
Message:

Set a reasonable, hard limit on label length. This removes the need for
a variable-length key structure (to accommodate a variable-length label)
and vastly simplifies key parsing.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/security/oath.h

    r677 r678  
    3737#include <security/oath_rfc4648.h>
    3838
    39 struct oath_key *oath_key_alloc(size_t);
     39struct oath_key *oath_key_alloc(void);
    4040void oath_key_free(struct oath_key *);
    4141struct oath_key *oath_key_from_uri(const char *);
  • trunk/include/security/oath_constants.h

    r655 r678  
    7272#define OATH_MAX_KEYLEN         64
    7373
     74/*
     75 * Maximum label length in characters, including terminating NUL.
     76 */
     77#define OATH_MAX_LABELLEN       64
     78
    7479#endif
  • trunk/include/security/oath_types.h

    r655 r678  
    4848        /* label */
    4949        size_t           labellen; /* bytes incl. NUL */
    50         char            *label;
     50        char             label[OATH_MAX_LABELLEN];
    5151
    5252        /* key */
    5353        size_t           keylen; /* bytes */
    54         uint8_t         *key;
    55 
    56         /* buffer for label + NUL + key */
    57         size_t           datalen; /* bytes */
    58         uint8_t          data[];
     54        uint8_t          key[OATH_MAX_KEYLEN];
    5955};
    6056
  • trunk/lib/liboath/oath_key.c

    r646 r678  
    5757 */
    5858struct oath_key *
    59 oath_key_alloc(size_t extra)
     59oath_key_alloc(void)
    6060{
    6161        struct oath_key *key;
    6262
    63         if ((key = calloc(1, sizeof *key + extra)) == NULL) {
     63        if ((key = calloc(1, sizeof *key)) == NULL) {
    6464                openpam_log(PAM_LOG_ERROR, "malloc(): %s", strerror(errno));
    6565                return (NULL);
    6666        }
    67         key->datalen = extra;
    6867        /* XXX should try to wire */
    6968        return (key);
     
    7877
    7978        if (key != NULL) {
    80                 memset(key, 0, sizeof *key + key->datalen);
     79                memset(key, 0, sizeof *key);
    8180                free(key);
    8281        }
     
    9594        char *e;
    9695
    97         /*
    98          * The URI string contains the label, the base32-encoded key and
    99          * some fluff, so the combined length of the label and key can
    100          * never exceed the length of the URI string.
    101          */
    102         if ((key = oath_key_alloc(strlen(uri))) == NULL)
     96        if ((key = oath_key_alloc()) == NULL)
    10397                return (NULL);
    10498
     
    124118        if ((q = strchr(p, '?')) == NULL)
    125119                goto invalid;
    126         key->label = (char *)key->data;
    127         key->labellen = (q - p) + 1;
     120        if ((key->labellen = q - p + 1) > sizeof key->label)
     121                goto invalid;
    128122        memcpy(key->label, p, q - p);
    129123        key->label[q - p] = '\0';
     
    146140                                /* dupe */
    147141                                goto invalid;
    148                         /* base32-encoded key - multiple of 40 bits */
    149                         if ((r - q) % 8 != 0 ||
    150                             base32_declen(r - q) > OATH_MAX_KEYLEN)
    151                                 goto invalid;
    152                         key->key = key->data + key->labellen;
    153                         key->keylen = key->datalen - key->labellen;
     142                        key->keylen = sizeof key->key;
    154143                        if (base32_dec(q, r - q, key->key, &key->keylen) != 0)
    155144                                goto invalid;
     
    315304        struct oath_key *key;
    316305
    317         if ((key = oath_key_alloc(DUMMY_LABELLEN + DUMMY_KEYLEN)) == NULL)
     306        if ((key = oath_key_alloc()) == NULL)
    318307                return (NULL);
    319308        key->mode = mode;
     
    322311        key->timestep = 30;
    323312        key->hash = hash;
    324         key->label = (char *)key->data;
    325313        memcpy(key->label, DUMMY_LABEL, DUMMY_LABELLEN);
    326         key->key = key->data + DUMMY_LABELLEN;
     314        key->labellen = DUMMY_LABELLEN;
    327315        key->keylen = DUMMY_KEYLEN;
    328316        return (key);
Note: See TracChangeset for help on using the changeset viewer.