1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* kdc/authind.c - Functions for manipulating authentication indicator lists */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert * are met:
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert * distribution.
18*7f2fe78bSCy Schubert *
19*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert #include "k5-int.h"
34*7f2fe78bSCy Schubert #include "kdc_util.h"
35*7f2fe78bSCy Schubert
36*7f2fe78bSCy Schubert /* Return true if ind matches an entry in indicators. */
37*7f2fe78bSCy Schubert krb5_boolean
authind_contains(krb5_data * const * indicators,const char * ind)38*7f2fe78bSCy Schubert authind_contains(krb5_data *const *indicators, const char *ind)
39*7f2fe78bSCy Schubert {
40*7f2fe78bSCy Schubert for (; indicators != NULL && *indicators != NULL; indicators++) {
41*7f2fe78bSCy Schubert if (data_eq_string(**indicators, ind))
42*7f2fe78bSCy Schubert return TRUE;
43*7f2fe78bSCy Schubert }
44*7f2fe78bSCy Schubert return FALSE;
45*7f2fe78bSCy Schubert }
46*7f2fe78bSCy Schubert
47*7f2fe78bSCy Schubert /* Add ind to *indicators, reallocating as necessary. */
48*7f2fe78bSCy Schubert krb5_error_code
authind_add(krb5_context context,const char * ind,krb5_data *** indicators)49*7f2fe78bSCy Schubert authind_add(krb5_context context, const char *ind, krb5_data ***indicators)
50*7f2fe78bSCy Schubert {
51*7f2fe78bSCy Schubert size_t count;
52*7f2fe78bSCy Schubert krb5_data **list = *indicators, *dptr, d;
53*7f2fe78bSCy Schubert
54*7f2fe78bSCy Schubert /* Count the number of existing indicators and check for duplicates. */
55*7f2fe78bSCy Schubert for (count = 0; list != NULL && list[count] != NULL; count++) {
56*7f2fe78bSCy Schubert if (data_eq_string(*list[count], ind))
57*7f2fe78bSCy Schubert return 0;
58*7f2fe78bSCy Schubert }
59*7f2fe78bSCy Schubert
60*7f2fe78bSCy Schubert /* Allocate space for a new entry. */
61*7f2fe78bSCy Schubert list = realloc(list, (count + 2) * sizeof(*list));
62*7f2fe78bSCy Schubert if (list == NULL)
63*7f2fe78bSCy Schubert return ENOMEM;
64*7f2fe78bSCy Schubert *indicators = list;
65*7f2fe78bSCy Schubert
66*7f2fe78bSCy Schubert /* Add a copy of ind (as a krb5_data object) to the list. */
67*7f2fe78bSCy Schubert d = string2data((char *)ind);
68*7f2fe78bSCy Schubert if (krb5_copy_data(context, &d, &dptr) != 0)
69*7f2fe78bSCy Schubert return ENOMEM;
70*7f2fe78bSCy Schubert list[count++] = dptr;
71*7f2fe78bSCy Schubert list[count] = NULL;
72*7f2fe78bSCy Schubert return 0;
73*7f2fe78bSCy Schubert }
74*7f2fe78bSCy Schubert
75*7f2fe78bSCy Schubert /* Add all auth indicators from authdata to *indicators, reallocating as
76*7f2fe78bSCy Schubert * necessary. (Currently does not compress duplicates.) */
77*7f2fe78bSCy Schubert krb5_error_code
authind_extract(krb5_context context,krb5_authdata ** authdata,krb5_data *** indicators)78*7f2fe78bSCy Schubert authind_extract(krb5_context context, krb5_authdata **authdata,
79*7f2fe78bSCy Schubert krb5_data ***indicators)
80*7f2fe78bSCy Schubert {
81*7f2fe78bSCy Schubert krb5_error_code ret;
82*7f2fe78bSCy Schubert size_t count, scount;
83*7f2fe78bSCy Schubert krb5_authdata **ind_authdata = NULL, **adp;
84*7f2fe78bSCy Schubert krb5_data der_indicators, **strings = NULL, **list = *indicators;
85*7f2fe78bSCy Schubert
86*7f2fe78bSCy Schubert for (count = 0; list != NULL && list[count] != NULL; count++);
87*7f2fe78bSCy Schubert
88*7f2fe78bSCy Schubert ret = krb5_find_authdata(context, authdata, NULL,
89*7f2fe78bSCy Schubert KRB5_AUTHDATA_AUTH_INDICATOR, &ind_authdata);
90*7f2fe78bSCy Schubert if (ret)
91*7f2fe78bSCy Schubert goto cleanup;
92*7f2fe78bSCy Schubert
93*7f2fe78bSCy Schubert for (adp = ind_authdata; adp != NULL && *adp != NULL; adp++) {
94*7f2fe78bSCy Schubert /* Decode this authdata element into an auth indicator list. */
95*7f2fe78bSCy Schubert der_indicators = make_data((*adp)->contents, (*adp)->length);
96*7f2fe78bSCy Schubert ret = decode_utf8_strings(&der_indicators, &strings);
97*7f2fe78bSCy Schubert if (ret == ENOMEM)
98*7f2fe78bSCy Schubert goto cleanup;
99*7f2fe78bSCy Schubert if (ret)
100*7f2fe78bSCy Schubert continue;
101*7f2fe78bSCy Schubert
102*7f2fe78bSCy Schubert /* Count the entries in strings and allocate space in list. */
103*7f2fe78bSCy Schubert for (scount = 0; strings != NULL && strings[scount] != NULL; scount++);
104*7f2fe78bSCy Schubert list = realloc(list, (count + scount + 1) * sizeof(*list));
105*7f2fe78bSCy Schubert if (list == NULL) {
106*7f2fe78bSCy Schubert ret = ENOMEM;
107*7f2fe78bSCy Schubert goto cleanup;
108*7f2fe78bSCy Schubert }
109*7f2fe78bSCy Schubert *indicators = list;
110*7f2fe78bSCy Schubert
111*7f2fe78bSCy Schubert /* Steal the krb5_data pointers from strings and free the array. */
112*7f2fe78bSCy Schubert memcpy(list + count, strings, scount * sizeof(*strings));
113*7f2fe78bSCy Schubert count += scount;
114*7f2fe78bSCy Schubert list[count] = NULL;
115*7f2fe78bSCy Schubert free(strings);
116*7f2fe78bSCy Schubert strings = NULL;
117*7f2fe78bSCy Schubert }
118*7f2fe78bSCy Schubert
119*7f2fe78bSCy Schubert cleanup:
120*7f2fe78bSCy Schubert krb5_free_authdata(context, ind_authdata);
121*7f2fe78bSCy Schubert k5_free_data_ptr_list(strings);
122*7f2fe78bSCy Schubert return ret;
123*7f2fe78bSCy Schubert }
124