summaryrefslogtreecommitdiffstats
path: root/src/lib/gssapi/krb5/export_cred.c
blob: 652b2604bda3177af0e2b4bf0b5f02f2ea444607 (plain)
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/gssapi/krb5/export_cred.c - krb5 export_cred implementation */
/*
 * Copyright (C) 2012 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "k5-int.h"
#include "k5-json.h"
#include "gssapiP_krb5.h"

/* Return a JSON null or array value representing princ. */
static krb5_error_code
json_principal(krb5_context context, krb5_principal princ,
               k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_string str = NULL;
    char *princname;

    *val_out = NULL;
    if (princ == NULL)
        return k5_json_null_create_val(val_out);
    ret = krb5_unparse_name(context, princ, &princname);
    if (ret)
        return ret;
    ret = k5_json_string_create(princname, &str);
    krb5_free_unparsed_name(context, princname);
    *val_out = str;
    return ret;
}

/* Return a json null or array value representing etypes. */
static krb5_error_code
json_etypes(krb5_enctype *etypes, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_number num;
    k5_json_array array;

    *val_out = NULL;
    if (etypes == NULL)
        return k5_json_null_create_val(val_out);
    ret = k5_json_array_create(&array);
    if (ret)
        return ret;
    for (; *etypes != 0; etypes++) {
        ret = k5_json_number_create(*etypes, &num);
        if (ret)
            goto err;
        ret = k5_json_array_add(array, num);
        k5_json_release(num);
        if (ret)
            goto err;
    }
    *val_out = array;
    return 0;
err:
    k5_json_release(array);
    return ret;
}

/* Return a JSON null or array value representing name. */
static krb5_error_code
json_kgname(krb5_context context, krb5_gss_name_t name, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array = NULL;
    k5_json_value princ;

    *val_out = NULL;
    if (name == NULL)
        return k5_json_null_create_val(val_out);
    ret = json_principal(context, name->princ, &princ);
    if (ret)
        return ret;
    ret = k5_json_array_fmt(&array, "vss", princ, name->service, name->host);
    k5_json_release(princ);
    *val_out = array;
    return ret;
}

/* Return a JSON null or string value representing keytab. */
static krb5_error_code
json_keytab(krb5_context context, krb5_keytab keytab, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_string str;
    char name[1024];

    *val_out = NULL;
    if (keytab == NULL)
        return k5_json_null_create_val(val_out);
    ret = krb5_kt_get_name(context, keytab, name, sizeof(name));
    if (ret)
        return ret;
    ret = k5_json_string_create(name, &str);
    *val_out = str;
    return ret;
}

/* Return a JSON null or string value representing rcache. */
static krb5_error_code
json_rcache(krb5_context context, krb5_rcache rcache, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_string str = NULL;
    char *name;

    if (rcache == NULL)
        return k5_json_null_create_val(val_out);
    if (asprintf(&name, "%s:%s", krb5_rc_get_type(context, rcache),
                 krb5_rc_get_name(context, rcache)) < 0)
        return ENOMEM;
    ret = k5_json_string_create(name, &str);
    free(name);
    *val_out = str;
    return ret;
}

/* Return a JSON array value representing keyblock. */
static krb5_error_code
json_keyblock(krb5_keyblock *kb, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array;

    *val_out = NULL;
    ret = k5_json_array_fmt(&array, "iB", kb->enctype, (void *)kb->contents,
                            (size_t)kb->length);
    if (ret)
        return ret;
    *val_out = array;
    return 0;
}

/* Return a JSON array value representing addr. */
static krb5_error_code
json_address(krb5_address *addr, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array;

    *val_out = NULL;
    ret = k5_json_array_fmt(&array, "iB", addr->addrtype,
                            (void *)addr->contents, (size_t)addr->length);
    if (ret)
        return ret;
    *val_out = array;
    return 0;
}

/* Return a JSON null or array value representing addrs. */
static krb5_error_code
json_addresses(krb5_address **addrs, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array;
    k5_json_value val;

    *val_out = NULL;
    if (addrs == NULL)
        return k5_json_null_create_val(val_out);
    ret = k5_json_array_create(&array);
    if (ret)
        return ret;
    for (; *addrs != NULL; addrs++) {
        ret = json_address(*addrs, &val);
        if (ret)
            goto err;
        ret = k5_json_array_add(array, val);
        k5_json_release(val);
        if (ret)
            goto err;
    }
    *val_out = array;
    return 0;
err:
    k5_json_release(array);
    return ret;
}

/* Return a JSON array value representing ad. */
static krb5_error_code
json_authdata_element(krb5_authdata *ad, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array;

    *val_out = NULL;
    ret = k5_json_array_fmt(&array, "iB", ad->ad_type, (void *)ad->contents,
                            (size_t)ad->length);
    if (ret)
        return ret;
    *val_out = array;
    return 0;
}

/* Return a JSON null or array value representing authdata. */
static krb5_error_code
json_authdata(krb5_authdata **authdata, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array;
    k5_json_value val;

    *val_out = NULL;
    if (authdata == NULL)
        return k5_json_null_create_val(val_out);
    ret = k5_json_array_create(&array);
    if (ret)
        return ret;
    for (; *authdata != NULL; authdata++) {
        ret = json_authdata_element(*authdata, &val);
        if (ret)
            goto err;
        ret = k5_json_array_add(array, val);
        k5_json_release(val);
        if (ret)
            goto err;
    }
    *val_out = array;
    return 0;
err:
    k5_json_release(array);
    return ret;
}

/* Return a JSON array value representing creds. */
static krb5_error_code
json_creds(krb5_context context, krb5_creds *creds, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array;
    k5_json_value client = NULL, server = NULL, keyblock = NULL, addrs = NULL;
    k5_json_value authdata = NULL;

    *val_out = NULL;
    ret = json_principal(context, creds->client, &client);
    if (ret)
        goto cleanup;
    ret = json_principal(context, creds->server, &server);
    if (ret)
        goto cleanup;
    ret = json_keyblock(&creds->keyblock, &keyblock);
    if (ret)
        goto cleanup;
    ret = json_addresses(creds->addresses, &addrs);
    if (ret)
        goto cleanup;
    ret = json_authdata(creds->authdata, &authdata);
    if (ret)
        goto cleanup;

    ret = k5_json_array_fmt(&array, "vvviiiibivBBv", client, server, keyblock,
                            creds->times.authtime, creds->times.starttime,
                            creds->times.endtime, creds->times.renew_till,
                            creds->is_skey, creds->ticket_flags, addrs,
                            (void *)creds->ticket.data,
                            (size_t)creds->ticket.length,
                            (void *)creds->second_ticket.data,
                            (size_t)creds->second_ticket.length, authdata);
    if (ret)
        goto cleanup;
    *val_out = array;

cleanup:
    k5_json_release(client);
    k5_json_release(server);
    k5_json_release(keyblock);
    k5_json_release(addrs);
    k5_json_release(authdata);
    return ret;
}

/* Return a JSON array value representing the contents of ccache. */
static krb5_error_code
json_ccache_contents(krb5_context context, krb5_ccache ccache,
                     k5_json_value *val_out)
{
    krb5_error_code ret;
    krb5_principal princ;
    krb5_cc_cursor cursor;
    krb5_creds creds;
    k5_json_array array;
    k5_json_value val;

    *val_out = NULL;
    ret = k5_json_array_create(&array);
    if (ret)
        return ret;

    /* Put the principal in the first array entry. */
    ret = krb5_cc_get_principal(context, ccache, &princ);
    if (ret)
        goto err;
    ret = json_principal(context, princ, &val);
    krb5_free_principal(context, princ);
    if (ret)
        goto err;
    ret = k5_json_array_add(array, val);
    k5_json_release(val);
    if (ret)
        goto err;

    /* Put credentials in the remaining array entries. */
    ret = krb5_cc_start_seq_get(context, ccache, &cursor);
    if (ret)
        goto err;
    while ((ret = krb5_cc_next_cred(context, ccache, &cursor, &creds)) == 0) {
        ret = json_creds(context, &creds, &val);
        krb5_free_cred_contents(context, &creds);
        if (ret)
            break;
        ret = k5_json_array_add(array, val);
        k5_json_release(val);
        if (ret)
            break;
    }
    krb5_cc_end_seq_get(context, ccache, &cursor);
    if (ret != KRB5_CC_END)
        goto err;
    *val_out = array;
    return 0;

err:
    k5_json_release(array);
    return ret;
}

/* Return a JSON null, string, or array value representing ccache. */
static krb5_error_code
json_ccache(krb5_context context, krb5_ccache ccache, k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_string str;
    char *name;

    *val_out = NULL;
    if (ccache == NULL)
        return k5_json_null_create_val(val_out);
    if (strcmp(krb5_cc_get_type(context, ccache), "MEMORY") == 0) {
        return json_ccache_contents(context, ccache, val_out);
    } else {
        ret = krb5_cc_get_full_name(context, ccache, &name);
        if (ret)
            return ret;
        ret = k5_json_string_create(name, &str);
        free(name);
        *val_out = str;
        return ret;
    }
}

/* Return a JSON array value representing cred. */
static krb5_error_code
json_kgcred(krb5_context context, krb5_gss_cred_id_t cred,
            k5_json_value *val_out)
{
    krb5_error_code ret;
    k5_json_array array;
    k5_json_value name = NULL, imp = NULL, keytab = NULL, rcache = NULL;
    k5_json_value ccache = NULL, ckeytab = NULL, etypes = NULL;

    *val_out = NULL;
    ret = json_kgname(context, cred->name, &name);
    if (ret)
        goto cleanup;
    ret = json_principal(context, cred->impersonator, &imp);
    if (ret)
        goto cleanup;
    ret = json_keytab(context, cred->keytab, &keytab);
    if (ret)
        goto cleanup;
    ret = json_rcache(context, cred->rcache, &rcache);
    if (ret)
        goto cleanup;
    ret = json_ccache(context, cred->ccache, &ccache);
    if (ret)
        goto cleanup;
    ret = json_keytab(context, cred->client_keytab, &ckeytab);
    if (ret)
        goto cleanup;
    ret = json_etypes(cred->req_enctypes, &etypes);
    if (ret)
        goto cleanup;

    ret = k5_json_array_fmt(&array, "ivvbbvvvvbiivs", cred->usage, name, imp,
                            cred->default_identity, cred->iakerb_mech, keytab,
                            rcache, ccache, ckeytab, cred->have_tgt,
                            cred->expire, cred->refresh_time, etypes,
                            cred->password);
    if (ret)
        goto cleanup;
    *val_out = array;

cleanup:
    k5_json_release(name);
    k5_json_release(imp);
    k5_json_release(keytab);
    k5_json_release(rcache);
    k5_json_release(ccache);
    k5_json_release(ckeytab);
    k5_json_release(etypes);
    return ret;
}

OM_uint32 KRB5_CALLCONV
krb5_gss_export_cred(OM_uint32 *minor_status, gss_cred_id_t cred_handle,
                     gss_buffer_t token)
{
    OM_uint32 status = GSS_S_COMPLETE;
    krb5_context context;
    krb5_error_code ret;
    krb5_gss_cred_id_t cred;
    k5_json_array array = NULL;
    k5_json_value jcred = NULL;
    char *str = NULL;
    krb5_data d;

    ret = krb5_gss_init_context(&context);
    if (ret) {
        *minor_status = ret;
        return GSS_S_FAILURE;
    }

    /* Validate and lock cred_handle. */
    status = krb5_gss_validate_cred_1(minor_status, cred_handle, context);
    if (status != GSS_S_COMPLETE)
        return status;
    cred = (krb5_gss_cred_id_t)cred_handle;

    if (json_kgcred(context, cred, &jcred))
        goto oom;
    if (k5_json_array_fmt(&array, "sv", CRED_EXPORT_MAGIC, jcred))
        goto oom;
    if (k5_json_encode(array, &str))
        goto oom;
    d = string2data(str);
    if (data_to_gss(&d, token))
        goto oom;
    str = NULL;

cleanup:
    free(str);
    k5_mutex_unlock(&cred->lock);
    k5_json_release(array);
    k5_json_release(jcred);
    krb5_free_context(context);
    return status;

oom:
    *minor_status = ENOMEM;
    status = GSS_S_FAILURE;
    goto cleanup;
}