1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/gssapi/krb5/import_cred.c - krb5 import_cred implementation */
4 * Copyright (C) 2012 by the Massachusetts Institute of Technology.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "gssapiP_krb5.h"
37 /* Return the idx element of array if it is of type tid; otherwise return
38 * NULL. The caller is responsible for checking the array length. */
40 check_element(k5_json_array array, size_t idx, k5_json_tid tid)
44 v = k5_json_array_get(array, idx);
45 return (k5_json_get_tid(v) == tid) ? v : NULL;
48 /* All of the json_to_x functions return 0 on success, -1 on failure (either
49 * from running out of memory or from defective input). */
51 /* Convert a JSON value to a C string or to NULL. */
53 json_to_optional_string(k5_json_value v, char **string_out)
56 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
58 if (k5_json_get_tid(v) != K5_JSON_TID_STRING)
60 *string_out = strdup(k5_json_string_utf8(v));
61 return (*string_out == NULL) ? -1 : 0;
64 /* Convert a JSON value to a principal or to NULL. */
66 json_to_principal(krb5_context context, k5_json_value v,
67 krb5_principal *princ_out)
70 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
72 if (k5_json_get_tid(v) != K5_JSON_TID_STRING)
74 if (krb5_parse_name(context, k5_json_string_utf8(v), princ_out))
79 /* Convert a JSON value to a zero-terminated enctypes list or to NULL. */
81 json_to_etypes(k5_json_value v, krb5_enctype **etypes_out)
83 krb5_enctype *etypes = NULL;
89 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
91 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
94 len = k5_json_array_length(array);
95 etypes = calloc(len + 1, sizeof(*etypes));
96 for (i = 0; i < len; i++) {
97 n = check_element(array, i, K5_JSON_TID_NUMBER);
100 etypes[i] = k5_json_number_value(n);
102 *etypes_out = etypes;
110 /* Convert a JSON value to a krb5 GSS name or to NULL. */
112 json_to_kgname(krb5_context context, k5_json_value v,
113 krb5_gss_name_t *name_out)
116 krb5_gss_name_t name = NULL;
119 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
121 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
124 if (k5_json_array_length(array) != 3)
126 name = calloc(1, sizeof(*name));
129 if (k5_mutex_init(&name->lock)) {
134 if (json_to_principal(context, k5_json_array_get(array, 0), &name->princ))
136 if (json_to_optional_string(k5_json_array_get(array, 1), &name->service))
138 if (json_to_optional_string(k5_json_array_get(array, 2), &name->host))
145 kg_release_name(context, &name);
149 /* Convert a JSON value to a keytab handle or to NULL. */
151 json_to_keytab(krb5_context context, k5_json_value v, krb5_keytab *keytab_out)
154 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
156 if (k5_json_get_tid(v) != K5_JSON_TID_STRING)
158 if (krb5_kt_resolve(context, k5_json_string_utf8(v), keytab_out))
163 /* Convert a JSON value to an rcache handle or to NULL. */
165 json_to_rcache(krb5_context context, k5_json_value v, krb5_rcache *rcache_out)
170 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
172 if (k5_json_get_tid(v) != K5_JSON_TID_STRING)
174 if (k5_rc_resolve(context, (char *)k5_json_string_utf8(v), &rcache))
176 *rcache_out = rcache;
180 /* Convert a JSON value to a keyblock, filling in keyblock. */
182 json_to_keyblock(k5_json_value v, krb5_keyblock *keyblock)
189 memset(keyblock, 0, sizeof(*keyblock));
190 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
193 if (k5_json_array_length(array) != 2)
196 n = check_element(array, 0, K5_JSON_TID_NUMBER);
199 keyblock->enctype = k5_json_number_value(n);
201 s = check_element(array, 1, K5_JSON_TID_STRING);
204 if (k5_json_string_unbase64(s, &keyblock->contents, &len))
206 keyblock->length = len;
207 keyblock->magic = KV5M_KEYBLOCK;
211 /* Convert a JSON value to a krb5 address. */
213 json_to_address(k5_json_value v, krb5_address **addr_out)
216 krb5_address *addr = NULL;
222 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
225 if (k5_json_array_length(array) != 2)
228 n = check_element(array, 0, K5_JSON_TID_NUMBER);
231 s = check_element(array, 1, K5_JSON_TID_STRING);
235 addr = malloc(sizeof(*addr));
238 addr->addrtype = k5_json_number_value(n);
239 if (k5_json_string_unbase64(s, &addr->contents, &len)) {
244 addr->magic = KV5M_ADDRESS;
249 /* Convert a JSON value to a null-terminated list of krb5 addresses or to
252 json_to_addresses(krb5_context context, k5_json_value v,
253 krb5_address ***addresses_out)
256 krb5_address **addrs = NULL;
259 *addresses_out = NULL;
260 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
262 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
265 len = k5_json_array_length(array);
266 addrs = calloc(len + 1, sizeof(*addrs));
267 for (i = 0; i < len; i++) {
268 if (json_to_address(k5_json_array_get(array, i), &addrs[i]))
272 *addresses_out = addrs;
276 krb5_free_addresses(context, addrs);
280 /* Convert a JSON value to an authdata element. */
282 json_to_authdata_element(k5_json_value v, krb5_authdata **ad_out)
285 krb5_authdata *ad = NULL;
291 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
294 if (k5_json_array_length(array) != 2)
297 n = check_element(array, 0, K5_JSON_TID_NUMBER);
300 s = check_element(array, 1, K5_JSON_TID_STRING);
304 ad = malloc(sizeof(*ad));
307 ad->ad_type = k5_json_number_value(n);
308 if (k5_json_string_unbase64(s, &ad->contents, &len)) {
313 ad->magic = KV5M_AUTHDATA;
318 /* Convert a JSON value to a null-terminated authdata list or to NULL. */
320 json_to_authdata(krb5_context context, k5_json_value v,
321 krb5_authdata ***authdata_out)
324 krb5_authdata **authdata = NULL;
327 *authdata_out = NULL;
328 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
330 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
333 len = k5_json_array_length(array);
334 authdata = calloc(len + 1, sizeof(*authdata));
335 for (i = 0; i < len; i++) {
336 if (json_to_authdata_element(k5_json_array_get(array, i),
341 *authdata_out = authdata;
345 krb5_free_authdata(context, authdata);
349 /* Convert a JSON value to a krb5 credential structure, filling in creds. */
351 json_to_creds(krb5_context context, k5_json_value v, krb5_creds *creds)
360 memset(creds, 0, sizeof(*creds));
361 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
364 if (k5_json_array_length(array) != 13)
367 if (json_to_principal(context, k5_json_array_get(array, 0),
371 if (json_to_principal(context, k5_json_array_get(array, 1),
375 if (json_to_keyblock(k5_json_array_get(array, 2), &creds->keyblock))
378 n = check_element(array, 3, K5_JSON_TID_NUMBER);
381 creds->times.authtime = k5_json_number_value(n);
383 n = check_element(array, 4, K5_JSON_TID_NUMBER);
386 creds->times.starttime = k5_json_number_value(n);
388 n = check_element(array, 5, K5_JSON_TID_NUMBER);
391 creds->times.endtime = k5_json_number_value(n);
393 n = check_element(array, 6, K5_JSON_TID_NUMBER);
396 creds->times.renew_till = k5_json_number_value(n);
398 b = check_element(array, 7, K5_JSON_TID_BOOL);
401 creds->is_skey = k5_json_bool_value(b);
403 n = check_element(array, 8, K5_JSON_TID_NUMBER);
406 creds->ticket_flags = k5_json_number_value(n);
408 if (json_to_addresses(context, k5_json_array_get(array, 9),
412 s = check_element(array, 10, K5_JSON_TID_STRING);
415 if (k5_json_string_unbase64(s, &data, &len))
417 creds->ticket.data = (char *)data;
418 creds->ticket.length = len;
420 s = check_element(array, 11, K5_JSON_TID_STRING);
423 if (k5_json_string_unbase64(s, &data, &len))
425 creds->second_ticket.data = (char *)data;
426 creds->second_ticket.length = len;
428 if (json_to_authdata(context, k5_json_array_get(array, 12),
432 creds->magic = KV5M_CREDS;
436 krb5_free_cred_contents(context, creds);
437 memset(creds, 0, sizeof(*creds));
441 /* Convert a JSON value to a ccache handle or to NULL. Set *new_out to true if
442 * the ccache handle is a newly created memory ccache, false otherwise. */
444 json_to_ccache(krb5_context context, k5_json_value v, krb5_ccache *ccache_out,
445 krb5_boolean *new_out)
448 krb5_ccache ccache = NULL;
449 krb5_principal princ;
456 if (k5_json_get_tid(v) == K5_JSON_TID_NULL)
458 if (k5_json_get_tid(v) == K5_JSON_TID_STRING) {
459 /* We got a reference to an external ccache; just resolve it. */
460 return krb5_cc_resolve(context, k5_json_string_utf8(v), ccache_out) ?
464 /* We got the contents of a memory ccache. */
465 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
468 len = k5_json_array_length(array);
472 /* Initialize a new memory ccache using the principal in the first array
474 if (krb5_cc_new_unique(context, "MEMORY", NULL, &ccache))
476 if (json_to_principal(context, k5_json_array_get(array, 0), &princ))
478 ret = krb5_cc_initialize(context, ccache, princ);
479 krb5_free_principal(context, princ);
483 /* Add remaining array entries to the ccache as credentials. */
484 for (i = 1; i < len; i++) {
485 if (json_to_creds(context, k5_json_array_get(array, i), &creds))
487 ret = krb5_cc_store_cred(context, ccache, &creds);
488 krb5_free_cred_contents(context, &creds);
493 *ccache_out = ccache;
498 (void)krb5_cc_destroy(context, ccache);
502 /* Convert a JSON array value to a krb5 GSS credential. */
504 json_to_kgcred(krb5_context context, k5_json_array array,
505 krb5_gss_cred_id_t *cred_out)
507 krb5_gss_cred_id_t cred;
514 if (k5_json_array_length(array) != 14)
517 cred = calloc(1, sizeof(*cred));
520 if (k5_mutex_init(&cred->lock)) {
525 n = check_element(array, 0, K5_JSON_TID_NUMBER);
528 cred->usage = k5_json_number_value(n);
530 if (json_to_kgname(context, k5_json_array_get(array, 1), &cred->name))
533 if (json_to_principal(context, k5_json_array_get(array, 2),
534 &cred->impersonator))
537 b = check_element(array, 3, K5_JSON_TID_BOOL);
540 cred->default_identity = k5_json_bool_value(b);
542 b = check_element(array, 4, K5_JSON_TID_BOOL);
545 cred->iakerb_mech = k5_json_bool_value(b);
547 if (json_to_keytab(context, k5_json_array_get(array, 5), &cred->keytab))
550 if (json_to_rcache(context, k5_json_array_get(array, 6), &cred->rcache))
553 if (json_to_ccache(context, k5_json_array_get(array, 7), &cred->ccache,
556 cred->destroy_ccache = is_new;
558 if (json_to_keytab(context, k5_json_array_get(array, 8),
559 &cred->client_keytab))
562 b = check_element(array, 9, K5_JSON_TID_BOOL);
565 cred->have_tgt = k5_json_bool_value(b);
567 n = check_element(array, 10, K5_JSON_TID_NUMBER);
570 cred->expire = k5_json_number_value(n);
572 n = check_element(array, 11, K5_JSON_TID_NUMBER);
575 cred->refresh_time = k5_json_number_value(n);
577 if (json_to_etypes(k5_json_array_get(array, 12), &cred->req_enctypes))
580 if (json_to_optional_string(k5_json_array_get(array, 13), &cred->password))
587 (void)krb5_gss_release_cred(&tmp, (gss_cred_id_t *)&cred);
591 OM_uint32 KRB5_CALLCONV
592 krb5_gss_import_cred(OM_uint32 *minor_status, gss_buffer_t token,
593 gss_cred_id_t *cred_handle)
595 OM_uint32 status = GSS_S_COMPLETE;
596 krb5_context context;
598 krb5_gss_cred_id_t cred;
599 k5_json_value v = NULL;
604 ret = krb5_gss_init_context(&context);
607 return GSS_S_FAILURE;
611 copy = k5memdup0(token->value, token->length, &ret);
613 status = GSS_S_FAILURE;
617 if (k5_json_decode(copy, &v))
620 /* Decode the CRED_EXPORT_MAGIC array wrapper. */
621 if (k5_json_get_tid(v) != K5_JSON_TID_ARRAY)
624 if (k5_json_array_length(array) != 2)
626 str = check_element(array, 0, K5_JSON_TID_STRING);
628 strcmp(k5_json_string_utf8(str), CRED_EXPORT_MAGIC) != 0)
630 if (json_to_kgcred(context, k5_json_array_get(array, 1), &cred))
633 *cred_handle = (gss_cred_id_t)cred;
638 krb5_free_context(context);
642 status = GSS_S_DEFECTIVE_TOKEN;