2 * Copyright (c) 1997-2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2010 - 2011 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static krb5_error_code
39 get_fastuser_crypto(kdc_request_t r, krb5_enctype enctype, krb5_crypto *crypto)
41 krb5_principal fast_princ;
42 hdb_entry_ex *fast_user = NULL;
43 Key *cookie_key = NULL;
48 ret = krb5_make_principal(r->context, &fast_princ,
49 KRB5_WELLKNOWN_ORG_H5L_REALM,
50 KRB5_WELLKNOWN_NAME, "org.h5l.fast-cookie", NULL);
54 ret = _kdc_db_fetch(r->context, r->config, fast_princ,
55 HDB_F_GET_CLIENT, NULL, NULL, &fast_user);
56 krb5_free_principal(r->context, fast_princ);
60 if (enctype == KRB5_ENCTYPE_NULL)
61 ret = _kdc_get_preferred_key(r->context, r->config, fast_user,
62 "fast-cookie", &enctype, &cookie_key);
64 ret = hdb_enctype2key(r->context, &fast_user->entry, NULL,
65 enctype, &cookie_key);
69 ret = krb5_crypto_init(r->context, &cookie_key->key, 0, crypto);
75 _kdc_free_ent(r->context, fast_user);
81 static krb5_error_code
82 fast_parse_cookie(kdc_request_t r, const PA_DATA *pa)
84 krb5_crypto crypto = NULL;
90 ret = decode_KDCFastCookie(pa->padata_value.data,
91 pa->padata_value.length,
96 if (len != pa->padata_value.length || strcmp("H5L1", data.version) != 0) {
97 free_KDCFastCookie(&data);
98 return KRB5KDC_ERR_POLICY;
101 ret = get_fastuser_crypto(r, data.cookie.etype, &crypto);
105 ret = krb5_decrypt_EncryptedData(r->context, crypto,
108 krb5_crypto_destroy(r->context, crypto);
112 ret = decode_KDCFastState(d1.data, d1.length, &r->fast, &len);
117 if (r->fast.expiration < kdc_time) {
118 kdc_log(r->context, r->config, 0, "fast cookie expired");
119 ret = KRB5KDC_ERR_POLICY;
124 free_KDCFastCookie(&data);
129 static krb5_error_code
130 fast_add_cookie(kdc_request_t r, METHOD_DATA *method_data)
132 krb5_crypto crypto = NULL;
138 memset(&shell, 0, sizeof(shell));
140 r->fast.expiration = kdc_time + FAST_EXPIRATION_TIME;
142 ASN1_MALLOC_ENCODE(KDCFastState, data.data, data.length,
143 &r->fast, &size, ret);
146 heim_assert(size == data.length, "internal asn1 encoder error");
148 ret = get_fastuser_crypto(r, KRB5_ENCTYPE_NULL, &crypto);
152 ret = krb5_encrypt_EncryptedData(r->context, crypto,
154 data.data, data.length, 0,
156 krb5_crypto_destroy(r->context, crypto);
162 shell.version = "H5L1";
164 ASN1_MALLOC_ENCODE(KDCFastCookie, data.data, data.length,
166 free_EncryptedData(&shell.cookie);
169 heim_assert(size == data.length, "internal asn1 encoder error");
171 ret = krb5_padata_add(r->context, method_data,
172 KRB5_PADATA_FX_COOKIE,
173 data.data, data.length);
181 _kdc_fast_mk_response(krb5_context context,
182 krb5_crypto armor_crypto,
183 METHOD_DATA *pa_data,
184 krb5_keyblock *strengthen_key,
185 KrbFastFinished *finished,
189 PA_FX_FAST_REPLY fxfastrep;
190 KrbFastResponse fastrep;
195 memset(&fxfastrep, 0, sizeof(fxfastrep));
196 memset(&fastrep, 0, sizeof(fastrep));
197 krb5_data_zero(data);
200 fastrep.padata.val = pa_data->val;
201 fastrep.padata.len = pa_data->len;
203 fastrep.strengthen_key = strengthen_key;
204 fastrep.finished = finished;
205 fastrep.nonce = nonce;
207 ASN1_MALLOC_ENCODE(KrbFastResponse, buf.data, buf.length,
208 &fastrep, &size, ret);
211 if (buf.length != size)
212 krb5_abortx(context, "internal asn.1 error");
214 fxfastrep.element = choice_PA_FX_FAST_REPLY_armored_data;
216 ret = krb5_encrypt_EncryptedData(context,
222 &fxfastrep.u.armored_data.enc_fast_rep);
223 krb5_data_free(&buf);
227 ASN1_MALLOC_ENCODE(PA_FX_FAST_REPLY, data->data, data->length,
228 &fxfastrep, &size, ret);
229 free_PA_FX_FAST_REPLY(&fxfastrep);
232 if (data->length != size)
233 krb5_abortx(context, "internal asn.1 error");
240 _kdc_fast_mk_error(krb5_context context,
242 METHOD_DATA *error_method,
243 krb5_crypto armor_crypto,
244 const KDC_REQ_BODY *req_body,
245 krb5_error_code outer_error,
247 krb5_principal error_client,
248 krb5_principal error_server,
249 time_t *csec, int *cusec,
250 krb5_data *error_msg)
256 krb5_data_zero(&e_data);
259 PA_FX_FAST_REPLY fxfastrep;
260 KrbFastResponse fastrep;
262 memset(&fxfastrep, 0, sizeof(fxfastrep));
263 memset(&fastrep, 0, sizeof(fastrep));
265 /* first add the KRB-ERROR to the fast errors */
267 ret = krb5_mk_error(context,
279 ret = krb5_padata_add(context, error_method,
280 KRB5_PADATA_FX_ERROR,
281 e_data.data, e_data.length);
283 krb5_data_free(&e_data);
287 if (/* hide_principal */ 0) {
294 ret = fast_add_cookie(r, error_method);
296 ret = krb5_padata_add(context, error_method,
297 KRB5_PADATA_FX_COOKIE,
300 kdc_log(r->context, r->config, 0, "failed to add fast cookie with: %d", ret);
301 free_METHOD_DATA(error_method);
305 ret = _kdc_fast_mk_response(context, armor_crypto,
306 error_method, NULL, NULL,
307 req_body->nonce, &e_data);
308 free_METHOD_DATA(error_method);
312 ret = krb5_padata_add(context, error_method,
314 e_data.data, e_data.length);
319 if (error_method && error_method->len) {
320 ASN1_MALLOC_ENCODE(METHOD_DATA, e_data.data, e_data.length,
321 error_method, &size, ret);
324 if (e_data.length != size)
325 krb5_abortx(context, "internal asn.1 error");
328 ret = krb5_mk_error(context,
331 (e_data.length ? &e_data : NULL),
337 krb5_data_free(&e_data);
343 _kdc_fast_unwrap_request(kdc_request_t r)
345 krb5_principal armor_server = NULL;
346 hdb_entry_ex *armor_user = NULL;
347 PA_FX_FAST_REQUEST fxreq;
348 krb5_auth_context ac = NULL;
349 krb5_ticket *ticket = NULL;
350 krb5_flags ap_req_options;
351 Key *armor_key = NULL;
352 krb5_keyblock armorkey;
363 * First look for FX_COOKIE and and process it
365 pa = _kdc_find_padata(&r->req, &i, KRB5_PADATA_FX_COOKIE);
367 ret = fast_parse_cookie(r, pa);
373 pa = _kdc_find_padata(&r->req, &i, KRB5_PADATA_FX_FAST);
377 ret = decode_PA_FX_FAST_REQUEST(pa->padata_value.data,
378 pa->padata_value.length,
383 if (len != pa->padata_value.length) {
384 ret = KRB5KDC_ERR_PREAUTH_FAILED;
388 if (fxreq.element != choice_PA_FX_FAST_REQUEST_armored_data) {
389 kdc_log(r->context, r->config, 0,
390 "AS-REQ FAST contain unknown type: %d", (int)fxreq.element);
391 ret = KRB5KDC_ERR_PREAUTH_FAILED;
395 /* pull out armor key */
396 if (fxreq.u.armored_data.armor == NULL) {
397 kdc_log(r->context, r->config, 0,
398 "AS-REQ armor missing");
399 ret = KRB5KDC_ERR_PREAUTH_FAILED;
403 if (fxreq.u.armored_data.armor->armor_type != 1) {
404 kdc_log(r->context, r->config, 0,
405 "AS-REQ armor type not ap-req");
406 ret = KRB5KDC_ERR_PREAUTH_FAILED;
410 ret = krb5_decode_ap_req(r->context,
411 &fxreq.u.armored_data.armor->armor_value,
414 kdc_log(r->context, r->config, 0, "AP-REQ decode failed");
418 /* Save that principal that was in the request */
419 ret = _krb5_principalname2krb5_principal(r->context,
422 ap_req.ticket.realm);
424 free_AP_REQ(&ap_req);
428 ret = _kdc_db_fetch(r->context, r->config, armor_server,
429 HDB_F_GET_SERVER, NULL, NULL, &armor_user);
430 if(ret == HDB_ERR_NOT_FOUND_HERE) {
431 kdc_log(r->context, r->config, 5,
432 "armor key does not have secrets at this KDC, "
436 free_AP_REQ(&ap_req);
437 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
441 ret = hdb_enctype2key(r->context, &armor_user->entry, NULL,
442 ap_req.ticket.enc_part.etype,
445 free_AP_REQ(&ap_req);
449 ret = krb5_verify_ap_req2(r->context, &ac,
456 KRB5_KU_AP_REQ_AUTH);
457 free_AP_REQ(&ap_req);
461 if (ac->remote_subkey == NULL) {
462 krb5_auth_con_free(r->context, ac);
463 kdc_log(r->context, r->config, 0,
464 "FAST AP-REQ remote subkey missing");
465 ret = KRB5KDC_ERR_PREAUTH_FAILED;
469 ret = _krb5_fast_armor_key(r->context,
474 krb5_auth_con_free(r->context, ac);
475 krb5_free_ticket(r->context, ticket);
479 krb5_free_keyblock_contents(r->context, &armorkey);
481 /* verify req-checksum of the outer body */
483 ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, len, &r->req.req_body, &size, ret);
487 ret = KRB5KDC_ERR_PREAUTH_FAILED;
491 ret = krb5_verify_checksum(r->context, r->armor_crypto,
492 KRB5_KU_FAST_REQ_CHKSUM,
494 &fxreq.u.armored_data.req_checksum);
497 kdc_log(r->context, r->config, 0,
498 "FAST request have a bad checksum");
502 ret = krb5_decrypt_EncryptedData(r->context, r->armor_crypto,
504 &fxreq.u.armored_data.enc_fast_req,
507 kdc_log(r->context, r->config, 0,
508 "Failed to decrypt FAST request");
512 ret = decode_KrbFastReq(data.data, data.length, &fastreq, &size);
514 krb5_data_free(&data);
517 if (data.length != size) {
518 krb5_data_free(&data);
519 ret = KRB5KDC_ERR_PREAUTH_FAILED;
522 krb5_data_free(&data);
524 free_KDC_REQ_BODY(&r->req.req_body);
525 ret = copy_KDC_REQ_BODY(&fastreq.req_body, &r->req.req_body);
529 /* check for unsupported mandatory options */
530 if (FastOptions2int(fastreq.fast_options) & 0xfffc) {
531 kdc_log(r->context, r->config, 0,
532 "FAST unsupported mandatory option set");
533 ret = KRB5KDC_ERR_PREAUTH_FAILED;
537 /* KDC MUST ignore outer pa data preauth-14 - 6.5.5 */
539 free_METHOD_DATA(r->req.padata);
541 ALLOC(r->req.padata);
543 ret = copy_METHOD_DATA(&fastreq.padata, r->req.padata);
547 free_KrbFastReq(&fastreq);
548 free_PA_FX_FAST_REQUEST(&fxreq);
552 krb5_free_principal(r->context, armor_server);
554 _kdc_free_ent(r->context, armor_user);