/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* kdc/authind.c - Functions for manipulating authentication indicator lists */2/*3* Copyright (C) 2015 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#include "k5-int.h"33#include "kdc_util.h"3435/* Return true if ind matches an entry in indicators. */36krb5_boolean37authind_contains(krb5_data *const *indicators, const char *ind)38{39for (; indicators != NULL && *indicators != NULL; indicators++) {40if (data_eq_string(**indicators, ind))41return TRUE;42}43return FALSE;44}4546/* Add ind to *indicators, reallocating as necessary. */47krb5_error_code48authind_add(krb5_context context, const char *ind, krb5_data ***indicators)49{50size_t count;51krb5_data **list = *indicators, *dptr, d;5253/* Count the number of existing indicators and check for duplicates. */54for (count = 0; list != NULL && list[count] != NULL; count++) {55if (data_eq_string(*list[count], ind))56return 0;57}5859/* Allocate space for a new entry. */60list = realloc(list, (count + 2) * sizeof(*list));61if (list == NULL)62return ENOMEM;63*indicators = list;6465/* Add a copy of ind (as a krb5_data object) to the list. */66d = string2data((char *)ind);67if (krb5_copy_data(context, &d, &dptr) != 0)68return ENOMEM;69list[count++] = dptr;70list[count] = NULL;71return 0;72}7374/* Add all auth indicators from authdata to *indicators, reallocating as75* necessary. (Currently does not compress duplicates.) */76krb5_error_code77authind_extract(krb5_context context, krb5_authdata **authdata,78krb5_data ***indicators)79{80krb5_error_code ret;81size_t count, scount;82krb5_authdata **ind_authdata = NULL, **adp;83krb5_data der_indicators, **strings = NULL, **list = *indicators;8485for (count = 0; list != NULL && list[count] != NULL; count++);8687ret = krb5_find_authdata(context, authdata, NULL,88KRB5_AUTHDATA_AUTH_INDICATOR, &ind_authdata);89if (ret)90goto cleanup;9192for (adp = ind_authdata; adp != NULL && *adp != NULL; adp++) {93/* Decode this authdata element into an auth indicator list. */94der_indicators = make_data((*adp)->contents, (*adp)->length);95ret = decode_utf8_strings(&der_indicators, &strings);96if (ret == ENOMEM)97goto cleanup;98if (ret)99continue;100101/* Count the entries in strings and allocate space in list. */102for (scount = 0; strings != NULL && strings[scount] != NULL; scount++);103list = realloc(list, (count + scount + 1) * sizeof(*list));104if (list == NULL) {105ret = ENOMEM;106goto cleanup;107}108*indicators = list;109110/* Steal the krb5_data pointers from strings and free the array. */111memcpy(list + count, strings, scount * sizeof(*strings));112count += scount;113list[count] = NULL;114free(strings);115strings = NULL;116}117118cleanup:119krb5_free_authdata(context, ind_authdata);120k5_free_data_ptr_list(strings);121return ret;122}123124125