windows: Do not make test in kadmin/
[heimdal.git] / kdc / fast.c
blob85fab3310da925509e1df621a6a6103f1707f6f2
1 /*
2 * Copyright (c) 1997-2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "kdc_locl.h"
38 static krb5_error_code
39 salt_fastuser_crypto(astgs_request_t r,
40 krb5_const_principal salt_principal,
41 krb5_enctype enctype,
42 krb5_crypto fast_crypto,
43 krb5_crypto *salted_crypto)
45 krb5_error_code ret;
46 krb5_principal client_princ = NULL;
47 krb5_data salt;
48 krb5_keyblock dkey;
49 size_t size;
51 *salted_crypto = NULL;
53 krb5_data_zero(&salt);
54 krb5_keyblock_zero(&dkey);
56 if (salt_principal == NULL) {
57 if (r->req.req_body.cname == NULL) {
58 ret = KRB5KRB_ERR_GENERIC;
59 goto out;
62 ret = _krb5_principalname2krb5_principal(r->context, &client_princ,
63 *(r->req.req_body.cname),
64 r->req.req_body.realm);
65 if (ret)
66 goto out;
68 salt_principal = client_princ;
71 ret = krb5_unparse_name(r->context, salt_principal, (char **)&salt.data);
72 if (ret)
73 goto out;
75 salt.length = strlen(salt.data);
77 kdc_log(r->context, r->config, 10,
78 "salt_fastuser_crypto: salt principal is %s (%d)",
79 (char *)salt.data, enctype);
81 ret = krb5_enctype_keysize(r->context, enctype, &size);
82 if (ret)
83 goto out;
85 ret = krb5_crypto_prfplus(r->context, fast_crypto, &salt,
86 size, &dkey.keyvalue);
87 if (ret)
88 goto out;
90 dkey.keytype = enctype;
92 ret = krb5_crypto_init(r->context, &dkey, ENCTYPE_NULL, salted_crypto);
93 if (ret)
94 goto out;
96 out:
97 krb5_free_keyblock_contents(r->context, &dkey);
98 krb5_data_free(&salt);
99 krb5_free_principal(r->context, client_princ);
101 return ret;
104 static krb5_error_code
105 get_fastuser_crypto(astgs_request_t r,
106 krb5_const_principal ticket_client,
107 krb5_enctype enctype,
108 krb5_crypto *crypto)
110 krb5_principal fast_princ;
111 HDB *fast_db;
112 hdb_entry *fast_user = NULL;
113 Key *cookie_key = NULL;
114 krb5_crypto fast_crypto = NULL;
115 krb5_error_code ret;
117 *crypto = NULL;
119 ret = krb5_make_principal(r->context, &fast_princ,
120 KRB5_WELLKNOWN_ORG_H5L_REALM,
121 KRB5_WELLKNOWN_NAME, "org.h5l.fast-cookie", NULL);
122 if (ret)
123 goto out;
125 ret = _kdc_db_fetch(r->context, r->config, fast_princ,
126 HDB_F_GET_FAST_COOKIE, NULL, &fast_db, &fast_user);
127 if (ret)
128 goto out;
130 if (enctype == KRB5_ENCTYPE_NULL)
131 ret = _kdc_get_preferred_key(r->context, r->config, fast_user,
132 "fast-cookie", &enctype, &cookie_key);
133 else
134 ret = hdb_enctype2key(r->context, fast_user, NULL,
135 enctype, &cookie_key);
136 if (ret)
137 goto out;
139 ret = krb5_crypto_init(r->context, &cookie_key->key,
140 ENCTYPE_NULL, &fast_crypto);
141 if (ret)
142 goto out;
144 ret = salt_fastuser_crypto(r, ticket_client,
145 cookie_key->key.keytype,
146 fast_crypto, crypto);
147 if (ret)
148 goto out;
150 out:
151 if (fast_user)
152 _kdc_free_ent(r->context, fast_db, fast_user);
153 if (fast_crypto)
154 krb5_crypto_destroy(r->context, fast_crypto);
155 krb5_free_principal(r->context, fast_princ);
157 return ret;
161 static krb5_error_code
162 fast_parse_cookie(astgs_request_t r,
163 krb5_const_principal ticket_client,
164 const PA_DATA *pa)
166 krb5_crypto crypto = NULL;
167 krb5_error_code ret;
168 KDCFastCookie data;
169 krb5_data d1;
170 size_t len;
172 ret = decode_KDCFastCookie(pa->padata_value.data,
173 pa->padata_value.length,
174 &data, &len);
175 if (ret)
176 return ret;
178 if (len != pa->padata_value.length || strcmp("H5L1", data.version) != 0) {
179 free_KDCFastCookie(&data);
180 return KRB5KDC_ERR_POLICY;
183 ret = get_fastuser_crypto(r, ticket_client, data.cookie.etype, &crypto);
184 if (ret)
185 goto out;
187 ret = krb5_decrypt_EncryptedData(r->context, crypto,
188 KRB5_KU_H5L_COOKIE,
189 &data.cookie, &d1);
190 krb5_crypto_destroy(r->context, crypto);
191 if (ret)
192 goto out;
194 ret = decode_KDCFastState(d1.data, d1.length, &r->fast, &len);
195 krb5_data_free(&d1);
196 if (ret)
197 goto out;
199 if (r->fast.expiration < kdc_time) {
200 kdc_log(r->context, r->config, 2, "FAST cookie expired");
201 ret = KRB5KDC_ERR_POLICY;
202 goto out;
205 out:
206 free_KDCFastCookie(&data);
208 return ret;
211 static krb5_error_code
212 fast_add_cookie(astgs_request_t r,
213 krb5_const_principal ticket_client,
214 METHOD_DATA *method_data)
216 krb5_crypto crypto = NULL;
217 KDCFastCookie shell;
218 krb5_error_code ret;
219 krb5_data data;
220 size_t size;
222 memset(&shell, 0, sizeof(shell));
224 r->fast.expiration = kdc_time + FAST_EXPIRATION_TIME;
226 ASN1_MALLOC_ENCODE(KDCFastState, data.data, data.length,
227 &r->fast, &size, ret);
228 if (ret)
229 return ret;
230 heim_assert(size == data.length, "internal asn.1 encoder error");
232 ret = get_fastuser_crypto(r, ticket_client, KRB5_ENCTYPE_NULL, &crypto);
233 if (ret) {
234 kdc_log(r->context, r->config, 0,
235 "Failed to find FAST principal for cookie encryption: %d", ret);
236 goto out;
239 ret = krb5_encrypt_EncryptedData(r->context, crypto,
240 KRB5_KU_H5L_COOKIE,
241 data.data, data.length, 0,
242 &shell.cookie);
243 krb5_crypto_destroy(r->context, crypto);
244 if (ret)
245 goto out;
247 krb5_data_free(&data);
249 shell.version = "H5L1";
251 ASN1_MALLOC_ENCODE(KDCFastCookie, data.data, data.length,
252 &shell, &size, ret);
253 free_EncryptedData(&shell.cookie);
254 if (ret)
255 goto out;
256 heim_assert(size == data.length, "internal asn.1 encoder error");
258 ret = krb5_padata_add(r->context, method_data,
259 KRB5_PADATA_FX_COOKIE,
260 data.data, data.length);
261 if (ret == 0)
262 krb5_data_zero(&data);
264 out:
265 krb5_data_free(&data);
266 return ret;
269 krb5_error_code
270 _kdc_fast_mk_response(krb5_context context,
271 krb5_crypto armor_crypto,
272 METHOD_DATA *pa_data,
273 krb5_keyblock *strengthen_key,
274 KrbFastFinished *finished,
275 krb5uint32 nonce,
276 krb5_data *data)
278 PA_FX_FAST_REPLY fxfastrep;
279 KrbFastResponse fastrep;
280 krb5_error_code ret;
281 krb5_data buf;
282 size_t size;
284 memset(&fxfastrep, 0, sizeof(fxfastrep));
285 memset(&fastrep, 0, sizeof(fastrep));
286 krb5_data_zero(data);
288 if (pa_data) {
289 fastrep.padata.val = pa_data->val;
290 fastrep.padata.len = pa_data->len;
292 fastrep.strengthen_key = strengthen_key;
293 fastrep.finished = finished;
294 fastrep.nonce = nonce;
296 ASN1_MALLOC_ENCODE(KrbFastResponse, buf.data, buf.length,
297 &fastrep, &size, ret);
298 if (ret)
299 return ret;
300 heim_assert(size == buf.length, "internal asn.1 encoder error");
302 fxfastrep.element = choice_PA_FX_FAST_REPLY_armored_data;
304 ret = krb5_encrypt_EncryptedData(context,
305 armor_crypto,
306 KRB5_KU_FAST_REP,
307 buf.data,
308 buf.length,
310 &fxfastrep.u.armored_data.enc_fast_rep);
311 krb5_data_free(&buf);
312 if (ret)
313 return ret;
315 ASN1_MALLOC_ENCODE(PA_FX_FAST_REPLY, data->data, data->length,
316 &fxfastrep, &size, ret);
317 free_PA_FX_FAST_REPLY(&fxfastrep);
318 if (ret)
319 return ret;
320 heim_assert(size == data->length, "internal asn.1 encoder error");
322 return 0;
326 krb5_error_code
327 _kdc_fast_mk_error(astgs_request_t r,
328 METHOD_DATA *error_method,
329 krb5_crypto armor_crypto,
330 const KDC_REQ_BODY *req_body,
331 krb5_error_code outer_error,
332 krb5_principal error_client,
333 krb5_principal error_server,
334 time_t *csec, int *cusec,
335 krb5_data *error_msg)
337 krb5_error_code ret;
338 krb5_data e_data;
339 size_t size;
341 krb5_data_zero(&e_data);
343 heim_assert(r != NULL, "invalid request in _kdc_fast_mk_error");
346 * FX-COOKIE can be used outside of FAST, e.g. SRP or GSS.
348 if (armor_crypto || r->fast.fast_state.len) {
349 kdc_log(r->context, r->config, 5, "Adding FAST cookie for KRB-ERROR");
350 ret = fast_add_cookie(r, error_client, error_method);
351 if (ret) {
352 kdc_log(r->context, r->config, 1,
353 "Failed to add FAST cookie: %d", ret);
354 free_METHOD_DATA(error_method);
355 return ret;
359 if (armor_crypto) {
360 PA_FX_FAST_REPLY fxfastrep;
361 KrbFastResponse fastrep;
363 memset(&fxfastrep, 0, sizeof(fxfastrep));
364 memset(&fastrep, 0, sizeof(fastrep));
366 kdc_log(r->context, r->config, 5, "Making FAST inner KRB-ERROR");
368 /* first add the KRB-ERROR to the fast errors */
370 ret = krb5_mk_error(r->context,
371 outer_error,
372 r->e_text,
373 NULL,
374 error_client,
375 error_server,
376 NULL,
377 NULL,
378 &e_data);
379 if (ret) {
380 kdc_log(r->context, r->config, 1,
381 "Failed to make inner KRB-ERROR: %d", ret);
382 return ret;
385 ret = krb5_padata_add(r->context, error_method,
386 KRB5_PADATA_FX_ERROR,
387 e_data.data, e_data.length);
388 if (ret) {
389 kdc_log(r->context, r->config, 1,
390 "Failed to make add FAST PADATA to inner KRB-ERROR: %d", ret);
391 krb5_data_free(&e_data);
392 return ret;
395 outer_error = KRB5_KDC_ERR_MORE_PREAUTH_DATA_REQUIRED;
396 r->e_text = NULL;
397 if (r->fast.flags.requested_hidden_names) {
398 error_client = NULL;
399 error_server = NULL;
401 csec = 0;
402 cusec = 0;
404 ret = _kdc_fast_mk_response(r->context, armor_crypto,
405 error_method, NULL, NULL,
406 req_body->nonce, &e_data);
407 free_METHOD_DATA(error_method);
408 if (ret) {
409 kdc_log(r->context, r->config, 1,
410 "Failed to make outer KRB-ERROR: %d", ret);
411 return ret;
414 ret = krb5_padata_add(r->context, error_method,
415 KRB5_PADATA_FX_FAST,
416 e_data.data, e_data.length);
417 if (ret) {
418 kdc_log(r->context, r->config, 1,
419 "Failed to make add FAST PADATA to outer KRB-ERROR: %d", ret);
420 return ret;
422 } else
423 kdc_log(r->context, r->config, 5, "Making non-FAST KRB-ERROR");
425 if (error_method && error_method->len) {
426 ASN1_MALLOC_ENCODE(METHOD_DATA, e_data.data, e_data.length,
427 error_method, &size, ret);
428 if (ret) {
429 kdc_log(r->context, r->config, 1,
430 "Failed to make encode METHOD-DATA: %d", ret);
431 return ret;
433 heim_assert(size == e_data.length, "internal asn.1 encoder error");
436 ret = krb5_mk_error(r->context,
437 outer_error,
438 r->e_text,
439 (e_data.length ? &e_data : NULL),
440 error_client,
441 error_server,
442 csec,
443 cusec,
444 error_msg);
445 krb5_data_free(&e_data);
447 if (ret)
448 kdc_log(r->context, r->config, 1,
449 "Failed to make encode KRB-ERROR: %d", ret);
451 return ret;
454 static krb5_error_code
455 fast_unwrap_request(astgs_request_t r,
456 krb5_ticket *tgs_ticket,
457 krb5_auth_context tgs_ac)
459 krb5_principal armor_server_principal = NULL;
460 char *armor_client_principal_name = NULL;
461 char *armor_server_principal_name = NULL;
462 PA_FX_FAST_REQUEST fxreq;
463 krb5_auth_context ac = NULL;
464 krb5_ticket *ticket = NULL;
465 krb5_flags ap_req_options;
466 krb5_keyblock armorkey;
467 krb5_keyblock explicit_armorkey;
468 krb5_error_code ret;
469 krb5_ap_req ap_req;
470 KrbFastReq fastreq;
471 const PA_DATA *pa;
472 krb5_data data;
473 size_t len;
474 int i = 0;
476 memset(&fxreq, 0, sizeof(fxreq));
477 memset(&fastreq, 0, sizeof(fastreq));
479 pa = _kdc_find_padata(&r->req, &i, KRB5_PADATA_FX_FAST);
480 if (pa == NULL) {
481 if (tgs_ac && r->fast_asserted) {
482 kdc_log(r->context, r->config, 1,
483 "Client asserted FAST but did not include FX-FAST pa-data");
484 ret = KRB5KRB_AP_ERR_MODIFIED;
485 goto out;
488 kdc_log(r->context, r->config, 10, "Not a FAST request");
489 return 0;
492 ret = decode_PA_FX_FAST_REQUEST(pa->padata_value.data,
493 pa->padata_value.length,
494 &fxreq,
495 &len);
496 if (ret) {
497 kdc_log(r->context, r->config, 4,
498 "Failed to decode PA-FX-FAST-REQUEST: %d", ret);
499 goto out;
502 if (fxreq.element != choice_PA_FX_FAST_REQUEST_armored_data) {
503 kdc_log(r->context, r->config, 4,
504 "PA-FX-FAST-REQUEST contains unknown type: %d",
505 (int)fxreq.element);
506 ret = KRB5KDC_ERR_PREAUTH_FAILED;
507 goto out;
511 * If check for armor data or it's not a TGS-REQ with implicit
512 * armor.
514 if (fxreq.u.armored_data.armor == NULL && tgs_ac == NULL) {
515 kdc_log(r->context, r->config, 4,
516 "AS-REQ armor missing");
517 ret = KRB5KDC_ERR_PREAUTH_FAILED;
518 goto out;
521 r->explicit_armor_present = fxreq.u.armored_data.armor != NULL && tgs_ac != NULL;
526 if (fxreq.u.armored_data.armor != NULL) {
527 if (fxreq.u.armored_data.armor->armor_type != 1) {
528 kdc_log(r->context, r->config, 4,
529 "Incorrect AS-REQ armor type");
530 ret = KRB5KDC_ERR_PREAUTH_FAILED;
531 goto out;
534 ret = krb5_decode_ap_req(r->context,
535 &fxreq.u.armored_data.armor->armor_value,
536 &ap_req);
537 if(ret) {
538 kdc_log(r->context, r->config, 4, "Failed to decode AP-REQ");
539 goto out;
542 /* Save that principal that was in the request */
543 ret = _krb5_principalname2krb5_principal(r->context,
544 &armor_server_principal,
545 ap_req.ticket.sname,
546 ap_req.ticket.realm);
547 if (ret) {
548 free_AP_REQ(&ap_req);
549 goto out;
552 ret = _kdc_db_fetch(r->context, r->config, armor_server_principal,
553 HDB_F_GET_KRBTGT | HDB_F_DELAY_NEW_KEYS,
554 (krb5uint32 *)ap_req.ticket.enc_part.kvno,
555 &r->armor_serverdb, &r->armor_server);
556 if(ret == HDB_ERR_NOT_FOUND_HERE) {
557 free_AP_REQ(&ap_req);
558 kdc_log(r->context, r->config, 5,
559 "Armor key does not have secrets at this KDC, "
560 "need to proxy");
561 goto out;
562 } else if (ret) {
563 free_AP_REQ(&ap_req);
564 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
565 goto out;
568 ret = hdb_enctype2key(r->context, r->armor_server, NULL,
569 ap_req.ticket.enc_part.etype,
570 &r->armor_key);
571 if (ret) {
572 free_AP_REQ(&ap_req);
573 goto out;
576 ret = krb5_verify_ap_req2(r->context, &ac,
577 &ap_req,
578 armor_server_principal,
579 &r->armor_key->key,
581 &ap_req_options,
582 &r->armor_ticket,
583 KRB5_KU_AP_REQ_AUTH);
584 free_AP_REQ(&ap_req);
585 if (ret)
586 goto out;
588 ret = krb5_unparse_name(r->context, armor_server_principal,
589 &armor_server_principal_name);
590 if (ret)
591 goto out;
593 /* FIXME krb5_verify_ap_req2() also checks this */
594 ret = _kdc_verify_flags(r->context, r->config,
595 &r->armor_ticket->ticket,
596 armor_server_principal_name);
597 if (ret) {
598 kdc_audit_addreason((kdc_request_t)r,
599 "Armor TGT expired or invalid");
600 goto out;
602 ticket = r->armor_ticket;
603 } else {
604 heim_assert(tgs_ticket != NULL, "TGS authentication context without ticket");
605 ac = tgs_ac;
606 ticket = tgs_ticket;
609 krb5_unparse_name(r->context, ticket->client, &armor_client_principal_name);
610 kdc_audit_addkv((kdc_request_t)r, 0, "armor_client_name", "%s",
611 armor_client_principal_name ? armor_client_principal_name : "<unknown>");
613 if (ac->remote_subkey == NULL) {
614 krb5_auth_con_free(r->context, ac);
615 kdc_log(r->context, r->config, 2,
616 "FAST AP-REQ remote subkey missing");
617 ret = KRB5KDC_ERR_PREAUTH_FAILED;
618 goto out;
621 r->fast.flags.kdc_verified =
622 !_kdc_is_anonymous_pkinit(r->context, ticket->client);
624 ret = _krb5_fast_armor_key(r->context,
625 ac->remote_subkey,
626 &ticket->ticket.key,
627 &armorkey,
628 r->explicit_armor_present ? NULL : &r->armor_crypto);
629 if (ret)
630 goto out;
632 if (r->explicit_armor_present) {
633 ret = _krb5_fast_explicit_armor_key(r->context,
634 &armorkey,
635 tgs_ac->remote_subkey,
636 &explicit_armorkey,
637 &r->armor_crypto);
638 if (ret)
639 goto out;
641 krb5_free_keyblock_contents(r->context, &explicit_armorkey);
644 krb5_free_keyblock_contents(r->context, &armorkey);
646 ret = krb5_decrypt_EncryptedData(r->context, r->armor_crypto,
647 KRB5_KU_FAST_ENC,
648 &fxreq.u.armored_data.enc_fast_req,
649 &data);
650 if (ret) {
651 kdc_log(r->context, r->config, 2,
652 "Failed to decrypt FAST request");
653 goto out;
656 ret = decode_KrbFastReq(data.data, data.length, &fastreq, NULL);
657 krb5_data_free(&data);
658 if (ret)
659 goto out;
662 * verify req-checksum of the outer body
664 if (tgs_ac) {
666 * -- For TGS, contains the checksum performed over the type
667 * -- AP-REQ in the PA-TGS-REQ padata.
669 i = 0;
670 pa = _kdc_find_padata(&r->req, &i, KRB5_PADATA_TGS_REQ);
671 if (pa == NULL) {
672 kdc_log(r->context, r->config, 4,
673 "FAST TGS request missing TGS-REQ padata");
674 ret = KRB5KRB_ERR_GENERIC;
675 goto out;
678 ret = _kdc_verify_checksum(r->context, r->armor_crypto,
679 KRB5_KU_FAST_REQ_CHKSUM,
680 &pa->padata_value,
681 &fxreq.u.armored_data.req_checksum);
682 if (ret) {
683 kdc_log(r->context, r->config, 2,
684 "Bad checksum in FAST TGS request");
685 goto out;
687 } else {
689 * -- For AS, contains the checksum performed over the type
690 * -- KDC-REQ-BODY for the req-body field of the KDC-REQ
691 * -- structure;
693 ret = _kdc_verify_checksum(r->context, r->armor_crypto,
694 KRB5_KU_FAST_REQ_CHKSUM,
695 &r->req.req_body._save,
696 &fxreq.u.armored_data.req_checksum);
697 if (ret) {
698 kdc_log(r->context, r->config, 2,
699 "Bad checksum in FAST AS request");
700 goto out;
705 * check for unsupported mandatory options
707 if (FastOptions2int(fastreq.fast_options) & 0xfffc) {
708 kdc_log(r->context, r->config, 2,
709 "FAST unsupported mandatory option set");
710 ret = KRB5_KDC_ERR_UNKNOWN_CRITICAL_FAST_OPTIONS;
711 goto out;
714 r->fast.flags.requested_hidden_names = fastreq.fast_options.hide_client_names;
716 /* KDC MUST ignore outer pa data preauth-14 - 6.5.5 */
717 if (r->req.padata)
718 free_METHOD_DATA(r->req.padata);
719 else
720 ALLOC(r->req.padata);
722 ret = copy_METHOD_DATA(&fastreq.padata, r->req.padata);
723 if (ret)
724 goto out;
726 free_KDC_REQ_BODY(&r->req.req_body);
727 ret = copy_KDC_REQ_BODY(&fastreq.req_body, &r->req.req_body);
728 if (ret)
729 goto out;
731 kdc_log(r->context, r->config, 5, "Client selected FAST");
733 out:
734 if (ac && ac != tgs_ac)
735 krb5_auth_con_free(r->context, ac);
737 krb5_free_principal(r->context, armor_server_principal);
738 krb5_xfree(armor_client_principal_name);
739 krb5_xfree(armor_server_principal_name);
741 free_KrbFastReq(&fastreq);
742 free_PA_FX_FAST_REQUEST(&fxreq);
744 return ret;
750 krb5_error_code
751 _kdc_fast_unwrap_request(astgs_request_t r,
752 krb5_ticket *tgs_ticket,
753 krb5_auth_context tgs_ac)
755 krb5_error_code ret;
756 const PA_DATA *pa;
757 int i = 0;
759 if (!r->config->enable_fast)
760 return 0;
762 ret = fast_unwrap_request(r, tgs_ticket, tgs_ac);
763 if (ret)
764 return ret;
767 * FX-COOKIE can be used outside of FAST, e.g. SRP or GSS.
769 pa = _kdc_find_padata(&r->req, &i, KRB5_PADATA_FX_COOKIE);
770 if (pa) {
771 krb5_const_principal ticket_client = NULL;
773 if (tgs_ticket)
774 ticket_client = tgs_ticket->client;
776 ret = fast_parse_cookie(r, ticket_client, pa);
779 return ret;
783 * Strengthen reply key by mixing with a random key that is
784 * protected by FAST.
786 krb5_error_code
787 _kdc_fast_strengthen_reply_key(astgs_request_t r)
789 if (r->armor_crypto) {
790 krb5_keyblock new_reply_key;
791 krb5_error_code ret;
793 kdc_log(r->context, r->config, 5,
794 "FAST strengthen reply key with strengthen-key");
796 heim_assert(r->reply_key.keytype != KRB5_ENCTYPE_NULL, "NULL reply key enctype");
798 ret = krb5_generate_random_keyblock(r->context, r->reply_key.keytype,
799 &r->strengthen_key);
800 if (ret)
801 krb5_abortx(r->context, "random generator fail");
803 ret = _krb5_fast_cf2(r->context,
804 &r->strengthen_key, "strengthenkey",
805 &r->reply_key, "replykey",
806 &new_reply_key, NULL);
807 if (ret)
808 return ret;
810 krb5_free_keyblock_contents(r->context, &r->reply_key);
811 r->reply_key = new_reply_key;
814 return 0;
818 * Zero and free KDCFastState
820 void
821 _kdc_free_fast_state(KDCFastState *state)
823 size_t i;
825 for (i = 0; i < state->fast_state.len; i++) {
826 PA_DATA *pa = &state->fast_state.val[i];
828 if (pa->padata_value.data)
829 memset_s(pa->padata_value.data, 0,
830 pa->padata_value.length, pa->padata_value.length);
832 free_KDCFastState(state);
835 krb5_error_code
836 _kdc_fast_check_armor_pac(astgs_request_t r)
838 krb5_error_code ret;
839 int flags;
840 krb5_boolean ad_kdc_issued = FALSE;
841 krb5_pac mspac = NULL;
842 krb5_principal armor_client_principal = NULL;
843 HDB *armor_db;
844 hdb_entry *armor_client = NULL;
845 char *armor_client_principal_name = NULL;
847 flags = HDB_F_FOR_TGS_REQ;
848 if (_kdc_synthetic_princ_used_p(r->context, r->armor_ticket))
849 flags |= HDB_F_SYNTHETIC_OK;
850 if (r->req.req_body.kdc_options.canonicalize)
851 flags |= HDB_F_CANON;
853 ret = _krb5_principalname2krb5_principal(r->context,
854 &armor_client_principal,
855 r->armor_ticket->ticket.cname,
856 r->armor_ticket->ticket.crealm);
857 if (ret)
858 goto out;
860 ret = krb5_unparse_name(r->context, armor_client_principal,
861 &armor_client_principal_name);
862 if (ret)
863 goto out;
865 ret = _kdc_db_fetch_client(r->context, r->config, flags,
866 armor_client_principal, armor_client_principal_name,
867 r->req.req_body.realm, &armor_db, &armor_client);
868 if (ret)
869 goto out;
871 ret = kdc_check_flags(r, FALSE, armor_client, NULL);
872 if (ret)
873 goto out;
875 ret = _kdc_check_pac(r, armor_client_principal, NULL,
876 armor_client, r->armor_server,
877 r->armor_server, r->armor_server,
878 &r->armor_key->key, &r->armor_key->key,
879 &r->armor_ticket->ticket, &ad_kdc_issued, &mspac, NULL, NULL);
880 if (ret) {
881 const char *msg = krb5_get_error_message(r->context, ret);
883 kdc_log(r->context, r->config, 4,
884 "Verify armor PAC (%s) failed for %s (%s) from %s with %s (%s)",
885 armor_client_principal_name, r->cname, r->sname,
886 r->from, msg, mspac ? "Ticket unsigned" : "No PAC");
888 krb5_free_error_message(r->context, msg);
890 goto out;
893 if (r->explicit_armor_present) {
894 r->explicit_armor_clientdb = armor_db;
895 armor_db = NULL;
897 r->explicit_armor_client = armor_client;
898 armor_client = NULL;
900 r->explicit_armor_pac = mspac;
901 mspac = NULL;
904 out:
905 krb5_xfree(armor_client_principal_name);
906 if (armor_client)
907 _kdc_free_ent(r->context, armor_db, armor_client);
908 krb5_free_principal(r->context, armor_client_principal);
909 krb5_pac_free(r->context, mspac);
911 return ret;