File: mk_req_ext.c

package info (click to toggle)
krb5 1.17-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 55,116 kB
  • sloc: ansic: 303,225; exp: 13,198; cpp: 9,731; python: 8,927; makefile: 7,178; sh: 6,279; perl: 2,289; asm: 1,460; yacc: 1,005; awk: 396; csh: 147; xml: 135; lisp: 104; sed: 41
file content (395 lines) | stat: -rw-r--r-- 14,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/krb5/krb/mk_req_ext.c */
/*
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

/*
 *
 * krb5_mk_req_extended()
 */


#include "k5-int.h"
#include "int-proto.h"
#include "auth_con.h"

/*
  Formats a KRB_AP_REQ message into outbuf, with more complete options than
  krb_mk_req.

  outbuf, ap_req_options, checksum, and ccache are used in the
  same fashion as for krb5_mk_req.

  creds is used to supply the credentials (ticket and session key) needed
  to form the request.

  if creds->ticket has no data (length == 0), then a ticket is obtained
  from either the cache or the TGS, passing creds to krb5_get_credentials().
  kdc_options specifies the options requested for the ticket to be used.
  If a ticket with appropriate flags is not found in the cache, then these
  options are passed on in a request to an appropriate KDC.

  ap_req_options specifies the KRB_AP_REQ options desired.

  if ap_req_options specifies AP_OPTS_USE_SESSION_KEY, then creds->ticket
  must contain the appropriate ENC-TKT-IN-SKEY ticket.

  checksum specifies the checksum to be used in the authenticator.

  The outbuf buffer storage is allocated, and should be freed by the
  caller when finished.

  On an error return, the credentials pointed to by creds might have been
  augmented with additional fields from the obtained credentials; the entire
  credentials should be released by calling krb5_free_creds().

  returns system errors
*/

static krb5_error_code
make_etype_list(krb5_context context,
                krb5_enctype *desired_etypes,
                krb5_enctype tkt_enctype,
                krb5_authdata ***authdata);

static krb5_error_code
generate_authenticator(krb5_context,
                       krb5_authenticator *, krb5_principal,
                       krb5_checksum *, krb5_key,
                       krb5_ui_4, krb5_authdata **,
                       krb5_authdata_context ad_context,
                       krb5_enctype *desired_etypes,
                       krb5_enctype tkt_enctype);

krb5_error_code KRB5_CALLCONV
krb5_mk_req_extended(krb5_context context, krb5_auth_context *auth_context,
                     krb5_flags ap_req_options, krb5_data *in_data,
                     krb5_creds *in_creds, krb5_data *outbuf)
{
    krb5_error_code       retval;
    krb5_checksum         checksum;
    krb5_checksum         *checksump = 0;
    krb5_auth_context     new_auth_context;
    krb5_enctype          *desired_etypes = NULL;

    krb5_ap_req request;
    krb5_data *scratch = 0;
    krb5_data *toutbuf;

    request.ap_options = ap_req_options & AP_OPTS_WIRE_MASK;
    request.authenticator.ciphertext.data = NULL;
    request.ticket = 0;

    if (!in_creds->ticket.length)
        return(KRB5_NO_TKT_SUPPLIED);

    if ((ap_req_options & AP_OPTS_ETYPE_NEGOTIATION) &&
        !(ap_req_options & AP_OPTS_MUTUAL_REQUIRED))
        return(EINVAL);

    /* we need a native ticket */
    if ((retval = decode_krb5_ticket(&(in_creds)->ticket, &request.ticket)))
        return(retval);

    /* verify that the ticket is not expired */
    if ((retval = krb5int_validate_times(context, &in_creds->times)) != 0)
        goto cleanup;

    /* generate auth_context if needed */
    if (*auth_context == NULL) {
        if ((retval = krb5_auth_con_init(context, &new_auth_context)))
            goto cleanup;
        *auth_context = new_auth_context;
    }

    if ((*auth_context)->key != NULL) {
        krb5_k_free_key(context, (*auth_context)->key);
        (*auth_context)->key = NULL;
    }

    /* set auth context keyblock */
    if ((retval = krb5_k_create_key(context, &in_creds->keyblock,
                                    &((*auth_context)->key))))
        goto cleanup;

    /* generate seq number if needed */
    if ((((*auth_context)->auth_context_flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE)
         || ((*auth_context)->auth_context_flags & KRB5_AUTH_CONTEXT_RET_SEQUENCE))
        && ((*auth_context)->local_seq_number == 0)) {
        if ((retval = krb5_generate_seq_number(context, &in_creds->keyblock,
                                               &(*auth_context)->local_seq_number)))
            goto cleanup;
    }

    /* generate subkey if needed */
    if ((ap_req_options & AP_OPTS_USE_SUBKEY)&&(!(*auth_context)->send_subkey)) {
        retval = k5_generate_and_save_subkey(context, *auth_context,
                                             &in_creds->keyblock,
                                             in_creds->keyblock.enctype);
        if (retval)
            goto cleanup;
    }


    if (!in_data && (*auth_context)->checksum_func) {
        retval = (*auth_context)->checksum_func( context,
                                                 *auth_context,
                                                 (*auth_context)->checksum_func_data,
                                                 &in_data);
        if (retval)
            goto cleanup;
    }

    if (in_data) {
        if ((*auth_context)->req_cksumtype == 0x8003) {
            /* XXX Special hack for GSSAPI */
            checksum.checksum_type = 0x8003;
            checksum.length = in_data->length;
            checksum.contents = (krb5_octet *) in_data->data;
        } else {
            retval = krb5_k_make_checksum(context, 0, (*auth_context)->key,
                                          KRB5_KEYUSAGE_AP_REQ_AUTH_CKSUM,
                                          in_data, &checksum);
            if (retval)
                goto cleanup_cksum;
        }
        checksump = &checksum;
    }

    /* Generate authenticator */
    if (((*auth_context)->authentp = (krb5_authenticator *)malloc(sizeof(
                                                                      krb5_authenticator))) == NULL) {
        retval = ENOMEM;
        goto cleanup_cksum;
    }

    if (ap_req_options & AP_OPTS_ETYPE_NEGOTIATION) {
        if ((*auth_context)->permitted_etypes == NULL) {
            retval = krb5_get_tgs_ktypes(context, in_creds->server, &desired_etypes);
            if (retval)
                goto cleanup_cksum;
        } else
            desired_etypes = (*auth_context)->permitted_etypes;
    }

    TRACE_MK_REQ(context, in_creds, (*auth_context)->local_seq_number,
                 (*auth_context)->send_subkey, &in_creds->keyblock);
    if ((retval = generate_authenticator(context,
                                         (*auth_context)->authentp,
                                         in_creds->client, checksump,
                                         (*auth_context)->send_subkey,
                                         (*auth_context)->local_seq_number,
                                         in_creds->authdata,
                                         (*auth_context)->ad_context,
                                         desired_etypes,
                                         in_creds->keyblock.enctype)))
        goto cleanup_cksum;

    /* encode the authenticator */
    if ((retval = encode_krb5_authenticator((*auth_context)->authentp,
                                            &scratch)))
        goto cleanup_cksum;

    /* call the encryption routine */
    if ((retval = krb5_encrypt_helper(context, &in_creds->keyblock,
                                      KRB5_KEYUSAGE_AP_REQ_AUTH,
                                      scratch, &request.authenticator)))
        goto cleanup_cksum;

    if ((retval = encode_krb5_ap_req(&request, &toutbuf)))
        goto cleanup_cksum;
    *outbuf = *toutbuf;

    free(toutbuf);

cleanup_cksum:
    /* Null out these fields, to prevent pointer sharing problems;
     * they were supplied by the caller
     */
    if ((*auth_context)->authentp != NULL) {
        (*auth_context)->authentp->client = NULL;
        (*auth_context)->authentp->checksum = NULL;
    }
    if (checksump && checksump->checksum_type != 0x8003)
        free(checksump->contents);

cleanup:
    if (desired_etypes &&
        desired_etypes != (*auth_context)->permitted_etypes)
        free(desired_etypes);
    if (request.ticket)
        krb5_free_ticket(context, request.ticket);
    if (request.authenticator.ciphertext.data) {
        (void) memset(request.authenticator.ciphertext.data, 0,
                      request.authenticator.ciphertext.length);
        free(request.authenticator.ciphertext.data);
    }
    if (scratch) {
        memset(scratch->data, 0, scratch->length);
        free(scratch->data);
        free(scratch);
    }
    return retval;
}

static krb5_error_code
generate_authenticator(krb5_context context, krb5_authenticator *authent,
                       krb5_principal client, krb5_checksum *cksum,
                       krb5_key key, krb5_ui_4 seq_number,
                       krb5_authdata **authorization,
                       krb5_authdata_context ad_context,
                       krb5_enctype *desired_etypes,
                       krb5_enctype tkt_enctype)
{
    krb5_error_code retval;
    krb5_authdata **ext_authdata = NULL;

    authent->client = client;
    authent->checksum = cksum;
    if (key) {
        retval = krb5_k_key_keyblock(context, key, &authent->subkey);
        if (retval)
            return retval;
    } else
        authent->subkey = 0;
    authent->seq_number = seq_number;
    authent->authorization_data = NULL;

    if (ad_context != NULL) {
        retval = krb5_authdata_export_authdata(context,
                                               ad_context,
                                               AD_USAGE_AP_REQ,
                                               &ext_authdata);
        if (retval)
            return retval;
    }

    if (authorization != NULL || ext_authdata != NULL) {
        retval = krb5_merge_authdata(context,
                                     authorization,
                                     ext_authdata,
                                     &authent->authorization_data);
        if (retval) {
            krb5_free_authdata(context, ext_authdata);
            return retval;
        }
        krb5_free_authdata(context, ext_authdata);
    }

    /* Only send EtypeList if we prefer another enctype to tkt_enctype */
    if (desired_etypes != NULL && desired_etypes[0] != tkt_enctype) {
        TRACE_MK_REQ_ETYPES(context, desired_etypes);
        retval = make_etype_list(context, desired_etypes, tkt_enctype,
                                 &authent->authorization_data);
        if (retval)
            return retval;
    }

    return(krb5_us_timeofday(context, &authent->ctime, &authent->cusec));
}

/* RFC 4537 */
static krb5_error_code
make_etype_list(krb5_context context,
                krb5_enctype *desired_etypes,
                krb5_enctype tkt_enctype,
                krb5_authdata ***authdata)
{
    krb5_error_code code;
    krb5_etype_list etypes;
    krb5_data *enc_etype_list;
    krb5_data *ad_if_relevant;
    krb5_authdata *etype_adata[2], etype_adatum, **adata;
    int i;

    etypes.etypes = desired_etypes;

    for (etypes.length = 0;
         etypes.etypes[etypes.length] != ENCTYPE_NULL;
         etypes.length++)
    {
        /*
         * RFC 4537:
         *
         *   If the enctype of the ticket session key is included in the enctype
         *   list sent by the client, it SHOULD be the last on the list;
         */
        if (etypes.length && etypes.etypes[etypes.length - 1] == tkt_enctype)
            break;
    }

    code = encode_krb5_etype_list(&etypes, &enc_etype_list);
    if (code) {
        return code;
    }

    etype_adatum.magic = KV5M_AUTHDATA;
    etype_adatum.ad_type = KRB5_AUTHDATA_ETYPE_NEGOTIATION;
    etype_adatum.length = enc_etype_list->length;
    etype_adatum.contents = (krb5_octet *)enc_etype_list->data;

    etype_adata[0] = &etype_adatum;
    etype_adata[1] = NULL;

    /* Wrap in AD-IF-RELEVANT container */
    code = encode_krb5_authdata(etype_adata, &ad_if_relevant);
    if (code) {
        krb5_free_data(context, enc_etype_list);
        return code;
    }

    krb5_free_data(context, enc_etype_list);

    adata = *authdata;
    if (adata == NULL) {
        adata = (krb5_authdata **)calloc(2, sizeof(krb5_authdata *));
        i = 0;
    } else {
        for (i = 0; adata[i] != NULL; i++)
            ;

        adata = (krb5_authdata **)realloc(*authdata,
                                          (i + 2) * sizeof(krb5_authdata *));
    }
    if (adata == NULL) {
        krb5_free_data(context, ad_if_relevant);
        return ENOMEM;
    }
    *authdata = adata;

    adata[i] = (krb5_authdata *)malloc(sizeof(krb5_authdata));
    if (adata[i] == NULL) {
        krb5_free_data(context, ad_if_relevant);
        return ENOMEM;
    }
    adata[i]->magic = KV5M_AUTHDATA;
    adata[i]->ad_type = KRB5_AUTHDATA_IF_RELEVANT;
    adata[i]->length = ad_if_relevant->length;
    adata[i]->contents = (krb5_octet *)ad_if_relevant->data;
    free(ad_if_relevant); /* contents owned by adata[i] */

    adata[i + 1] = NULL;

    return 0;
}