Make aes-test.c more useful
[heimdal.git] / kdc / krb5tgs.c
blob334ceb95dee2027522ce9b134bef2caa521af9a4
1 /*
2 * Copyright (c) 1997-2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kdc_locl.h"
37 * return the realm of a krbtgt-ticket or NULL
40 static Realm
41 get_krbtgt_realm(const PrincipalName *p)
43 if(p->name_string.len == 2
44 && strcmp(p->name_string.val[0], KRB5_TGS_NAME) == 0)
45 return p->name_string.val[1];
46 else
47 return NULL;
51 * The KDC might add a signed path to the ticket authorization data
52 * field. This is to avoid server impersonating clients and the
53 * request constrained delegation.
55 * This is done by storing a KRB5_AUTHDATA_IF_RELEVANT with a single
56 * entry of type KRB5SignedPath.
59 static krb5_error_code
60 find_KRB5SignedPath(krb5_context context,
61 const AuthorizationData *ad,
62 krb5_data *data)
64 AuthorizationData child;
65 krb5_error_code ret;
66 int pos;
68 if (ad == NULL || ad->len == 0)
69 return KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
71 pos = ad->len - 1;
73 if (ad->val[pos].ad_type != KRB5_AUTHDATA_IF_RELEVANT)
74 return KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
76 ret = decode_AuthorizationData(ad->val[pos].ad_data.data,
77 ad->val[pos].ad_data.length,
78 &child,
79 NULL);
80 if (ret) {
81 krb5_set_error_message(context, ret, "Failed to decode "
82 "IF_RELEVANT with %d", ret);
83 return ret;
86 if (child.len != 1) {
87 free_AuthorizationData(&child);
88 return KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
91 if (child.val[0].ad_type != KRB5_AUTHDATA_SIGNTICKET) {
92 free_AuthorizationData(&child);
93 return KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
96 if (data)
97 ret = der_copy_octet_string(&child.val[0].ad_data, data);
98 free_AuthorizationData(&child);
99 return ret;
102 krb5_error_code
103 _kdc_add_KRB5SignedPath(krb5_context context,
104 krb5_kdc_configuration *config,
105 hdb_entry_ex *krbtgt,
106 krb5_enctype enctype,
107 krb5_principal client,
108 krb5_const_principal server,
109 krb5_principals principals,
110 EncTicketPart *tkt)
112 krb5_error_code ret;
113 KRB5SignedPath sp;
114 krb5_data data;
115 krb5_crypto crypto = NULL;
116 size_t size = 0;
118 if (server && principals) {
119 ret = add_Principals(principals, server);
120 if (ret)
121 return ret;
125 KRB5SignedPathData spd;
127 spd.client = client;
128 spd.authtime = tkt->authtime;
129 spd.delegated = principals;
130 spd.method_data = NULL;
132 ASN1_MALLOC_ENCODE(KRB5SignedPathData, data.data, data.length,
133 &spd, &size, ret);
134 if (ret)
135 return ret;
136 if (data.length != size)
137 krb5_abortx(context, "internal asn.1 encoder error");
141 Key *key;
142 ret = hdb_enctype2key(context, &krbtgt->entry, NULL, enctype, &key);
143 if (ret == 0)
144 ret = krb5_crypto_init(context, &key->key, 0, &crypto);
145 if (ret) {
146 free(data.data);
147 return ret;
152 * Fill in KRB5SignedPath
155 sp.etype = enctype;
156 sp.delegated = principals;
157 sp.method_data = NULL;
159 ret = krb5_create_checksum(context, crypto, KRB5_KU_KRB5SIGNEDPATH, 0,
160 data.data, data.length, &sp.cksum);
161 krb5_crypto_destroy(context, crypto);
162 free(data.data);
163 if (ret)
164 return ret;
166 ASN1_MALLOC_ENCODE(KRB5SignedPath, data.data, data.length, &sp, &size, ret);
167 free_Checksum(&sp.cksum);
168 if (ret)
169 return ret;
170 if (data.length != size)
171 krb5_abortx(context, "internal asn.1 encoder error");
175 * Add IF-RELEVANT(KRB5SignedPath) to the last slot in
176 * authorization data field.
179 ret = _kdc_tkt_add_if_relevant_ad(context, tkt,
180 KRB5_AUTHDATA_SIGNTICKET, &data);
181 krb5_data_free(&data);
183 return ret;
186 static krb5_error_code
187 check_KRB5SignedPath(krb5_context context,
188 krb5_kdc_configuration *config,
189 hdb_entry_ex *krbtgt,
190 krb5_principal cp,
191 EncTicketPart *tkt,
192 krb5_principals *delegated,
193 int *signedpath)
195 krb5_error_code ret;
196 krb5_data data;
197 krb5_crypto crypto = NULL;
199 if (delegated)
200 *delegated = NULL;
202 ret = find_KRB5SignedPath(context, tkt->authorization_data, &data);
203 if (ret == 0) {
204 KRB5SignedPathData spd;
205 KRB5SignedPath sp;
206 size_t size = 0;
208 ret = decode_KRB5SignedPath(data.data, data.length, &sp, NULL);
209 krb5_data_free(&data);
210 if (ret)
211 return ret;
213 spd.client = cp;
214 spd.authtime = tkt->authtime;
215 spd.delegated = sp.delegated;
216 spd.method_data = sp.method_data;
218 ASN1_MALLOC_ENCODE(KRB5SignedPathData, data.data, data.length,
219 &spd, &size, ret);
220 if (ret) {
221 free_KRB5SignedPath(&sp);
222 return ret;
224 if (data.length != size)
225 krb5_abortx(context, "internal asn.1 encoder error");
228 Key *key;
229 ret = hdb_enctype2key(context, &krbtgt->entry, NULL, /* XXX use correct kvno! */
230 sp.etype, &key);
231 if (ret == 0)
232 ret = krb5_crypto_init(context, &key->key, 0, &crypto);
233 if (ret) {
234 free(data.data);
235 free_KRB5SignedPath(&sp);
236 return ret;
239 ret = krb5_verify_checksum(context, crypto, KRB5_KU_KRB5SIGNEDPATH,
240 data.data, data.length,
241 &sp.cksum);
242 krb5_crypto_destroy(context, crypto);
243 free(data.data);
244 if (ret) {
245 free_KRB5SignedPath(&sp);
246 kdc_log(context, config, 5,
247 "KRB5SignedPath not signed correctly, not marking as signed");
248 return 0;
251 if (delegated && sp.delegated) {
253 *delegated = malloc(sizeof(*sp.delegated));
254 if (*delegated == NULL) {
255 free_KRB5SignedPath(&sp);
256 return ENOMEM;
259 ret = copy_Principals(*delegated, sp.delegated);
260 if (ret) {
261 free_KRB5SignedPath(&sp);
262 free(*delegated);
263 *delegated = NULL;
264 return ret;
267 free_KRB5SignedPath(&sp);
269 *signedpath = 1;
272 return 0;
279 static krb5_error_code
280 check_PAC(krb5_context context,
281 krb5_kdc_configuration *config,
282 const krb5_principal client_principal,
283 const krb5_principal delegated_proxy_principal,
284 hdb_entry_ex *client,
285 hdb_entry_ex *server,
286 hdb_entry_ex *krbtgt,
287 const EncryptionKey *server_check_key,
288 const EncryptionKey *server_sign_key,
289 const EncryptionKey *krbtgt_sign_key,
290 EncTicketPart *tkt,
291 krb5_data *rspac,
292 int *signedpath)
294 AuthorizationData *ad = tkt->authorization_data;
295 unsigned i, j;
296 krb5_error_code ret;
298 if (ad == NULL || ad->len == 0)
299 return 0;
301 for (i = 0; i < ad->len; i++) {
302 AuthorizationData child;
304 if (ad->val[i].ad_type != KRB5_AUTHDATA_IF_RELEVANT)
305 continue;
307 ret = decode_AuthorizationData(ad->val[i].ad_data.data,
308 ad->val[i].ad_data.length,
309 &child,
310 NULL);
311 if (ret) {
312 krb5_set_error_message(context, ret, "Failed to decode "
313 "IF_RELEVANT with %d", ret);
314 return ret;
316 for (j = 0; j < child.len; j++) {
318 if (child.val[j].ad_type == KRB5_AUTHDATA_WIN2K_PAC) {
319 int signed_pac = 0;
320 krb5_pac pac;
322 /* Found PAC */
323 ret = krb5_pac_parse(context,
324 child.val[j].ad_data.data,
325 child.val[j].ad_data.length,
326 &pac);
327 free_AuthorizationData(&child);
328 if (ret)
329 return ret;
331 ret = krb5_pac_verify(context, pac, tkt->authtime,
332 client_principal,
333 server_check_key, NULL);
334 if (ret) {
335 krb5_pac_free(context, pac);
336 return ret;
339 ret = _kdc_pac_verify(context, client_principal,
340 delegated_proxy_principal,
341 client, server, krbtgt, &pac, &signed_pac);
342 if (ret) {
343 krb5_pac_free(context, pac);
344 return ret;
348 * Only re-sign PAC if we could verify it with the PAC
349 * function. The no-verify case happens when we get in
350 * a PAC from cross realm from a Windows domain and
351 * that there is no PAC verification function.
353 if (signed_pac) {
354 *signedpath = 1;
355 ret = _krb5_pac_sign(context, pac, tkt->authtime,
356 client_principal,
357 server_sign_key, krbtgt_sign_key, rspac);
359 krb5_pac_free(context, pac);
361 return ret;
364 free_AuthorizationData(&child);
366 return 0;
373 static krb5_error_code
374 check_tgs_flags(krb5_context context,
375 krb5_kdc_configuration *config,
376 KDC_REQ_BODY *b, const EncTicketPart *tgt, EncTicketPart *et)
378 KDCOptions f = b->kdc_options;
380 if(f.validate){
381 if(!tgt->flags.invalid || tgt->starttime == NULL){
382 kdc_log(context, config, 0,
383 "Bad request to validate ticket");
384 return KRB5KDC_ERR_BADOPTION;
386 if(*tgt->starttime > kdc_time){
387 kdc_log(context, config, 0,
388 "Early request to validate ticket");
389 return KRB5KRB_AP_ERR_TKT_NYV;
391 /* XXX tkt = tgt */
392 et->flags.invalid = 0;
393 }else if(tgt->flags.invalid){
394 kdc_log(context, config, 0,
395 "Ticket-granting ticket has INVALID flag set");
396 return KRB5KRB_AP_ERR_TKT_INVALID;
399 if(f.forwardable){
400 if(!tgt->flags.forwardable){
401 kdc_log(context, config, 0,
402 "Bad request for forwardable ticket");
403 return KRB5KDC_ERR_BADOPTION;
405 et->flags.forwardable = 1;
407 if(f.forwarded){
408 if(!tgt->flags.forwardable){
409 kdc_log(context, config, 0,
410 "Request to forward non-forwardable ticket");
411 return KRB5KDC_ERR_BADOPTION;
413 et->flags.forwarded = 1;
414 et->caddr = b->addresses;
416 if(tgt->flags.forwarded)
417 et->flags.forwarded = 1;
419 if(f.proxiable){
420 if(!tgt->flags.proxiable){
421 kdc_log(context, config, 0,
422 "Bad request for proxiable ticket");
423 return KRB5KDC_ERR_BADOPTION;
425 et->flags.proxiable = 1;
427 if(f.proxy){
428 if(!tgt->flags.proxiable){
429 kdc_log(context, config, 0,
430 "Request to proxy non-proxiable ticket");
431 return KRB5KDC_ERR_BADOPTION;
433 et->flags.proxy = 1;
434 et->caddr = b->addresses;
436 if(tgt->flags.proxy)
437 et->flags.proxy = 1;
439 if(f.allow_postdate){
440 if(!tgt->flags.may_postdate){
441 kdc_log(context, config, 0,
442 "Bad request for post-datable ticket");
443 return KRB5KDC_ERR_BADOPTION;
445 et->flags.may_postdate = 1;
447 if(f.postdated){
448 if(!tgt->flags.may_postdate){
449 kdc_log(context, config, 0,
450 "Bad request for postdated ticket");
451 return KRB5KDC_ERR_BADOPTION;
453 if(b->from)
454 *et->starttime = *b->from;
455 et->flags.postdated = 1;
456 et->flags.invalid = 1;
457 }else if(b->from && *b->from > kdc_time + context->max_skew){
458 kdc_log(context, config, 0, "Ticket cannot be postdated");
459 return KRB5KDC_ERR_CANNOT_POSTDATE;
462 if(f.renewable){
463 if(!tgt->flags.renewable || tgt->renew_till == NULL){
464 kdc_log(context, config, 0,
465 "Bad request for renewable ticket");
466 return KRB5KDC_ERR_BADOPTION;
468 et->flags.renewable = 1;
469 ALLOC(et->renew_till);
470 _kdc_fix_time(&b->rtime);
471 *et->renew_till = *b->rtime;
473 if(f.renew){
474 time_t old_life;
475 if(!tgt->flags.renewable || tgt->renew_till == NULL){
476 kdc_log(context, config, 0,
477 "Request to renew non-renewable ticket");
478 return KRB5KDC_ERR_BADOPTION;
480 old_life = tgt->endtime;
481 if(tgt->starttime)
482 old_life -= *tgt->starttime;
483 else
484 old_life -= tgt->authtime;
485 et->endtime = *et->starttime + old_life;
486 if (et->renew_till != NULL)
487 et->endtime = min(*et->renew_till, et->endtime);
490 #if 0
491 /* checks for excess flags */
492 if(f.request_anonymous && !config->allow_anonymous){
493 kdc_log(context, config, 0,
494 "Request for anonymous ticket");
495 return KRB5KDC_ERR_BADOPTION;
497 #endif
498 return 0;
502 * Determine if constrained delegation is allowed from this client to this server
505 static krb5_error_code
506 check_constrained_delegation(krb5_context context,
507 krb5_kdc_configuration *config,
508 HDB *clientdb,
509 hdb_entry_ex *client,
510 hdb_entry_ex *server,
511 krb5_const_principal target)
513 const HDB_Ext_Constrained_delegation_acl *acl;
514 krb5_error_code ret;
515 size_t i;
518 * constrained_delegation (S4U2Proxy) only works within
519 * the same realm. We use the already canonicalized version
520 * of the principals here, while "target" is the principal
521 * provided by the client.
523 if(!krb5_realm_compare(context, client->entry.principal, server->entry.principal)) {
524 ret = KRB5KDC_ERR_BADOPTION;
525 kdc_log(context, config, 0,
526 "Bad request for constrained delegation");
527 return ret;
530 if (clientdb->hdb_check_constrained_delegation) {
531 ret = clientdb->hdb_check_constrained_delegation(context, clientdb, client, target);
532 if (ret == 0)
533 return 0;
534 } else {
535 /* if client delegates to itself, that ok */
536 if (krb5_principal_compare(context, client->entry.principal, server->entry.principal) == TRUE)
537 return 0;
539 ret = hdb_entry_get_ConstrainedDelegACL(&client->entry, &acl);
540 if (ret) {
541 krb5_clear_error_message(context);
542 return ret;
545 if (acl) {
546 for (i = 0; i < acl->len; i++) {
547 if (krb5_principal_compare(context, target, &acl->val[i]) == TRUE)
548 return 0;
551 ret = KRB5KDC_ERR_BADOPTION;
553 kdc_log(context, config, 0,
554 "Bad request for constrained delegation");
555 return ret;
559 * Determine if s4u2self is allowed from this client to this server
561 * For example, regardless of the principal being impersonated, if the
562 * 'client' and 'server' are the same, then it's safe.
565 static krb5_error_code
566 check_s4u2self(krb5_context context,
567 krb5_kdc_configuration *config,
568 HDB *clientdb,
569 hdb_entry_ex *client,
570 krb5_const_principal server)
572 krb5_error_code ret;
574 /* if client does a s4u2self to itself, that ok */
575 if (krb5_principal_compare(context, client->entry.principal, server) == TRUE)
576 return 0;
578 if (clientdb->hdb_check_s4u2self) {
579 ret = clientdb->hdb_check_s4u2self(context, clientdb, client, server);
580 if (ret == 0)
581 return 0;
582 } else {
583 ret = KRB5KDC_ERR_BADOPTION;
585 return ret;
592 static krb5_error_code
593 verify_flags (krb5_context context,
594 krb5_kdc_configuration *config,
595 const EncTicketPart *et,
596 const char *pstr)
598 if(et->endtime < kdc_time){
599 kdc_log(context, config, 0, "Ticket expired (%s)", pstr);
600 return KRB5KRB_AP_ERR_TKT_EXPIRED;
602 if(et->flags.invalid){
603 kdc_log(context, config, 0, "Ticket not valid (%s)", pstr);
604 return KRB5KRB_AP_ERR_TKT_NYV;
606 return 0;
613 static krb5_error_code
614 fix_transited_encoding(krb5_context context,
615 krb5_kdc_configuration *config,
616 krb5_boolean check_policy,
617 const TransitedEncoding *tr,
618 EncTicketPart *et,
619 const char *client_realm,
620 const char *server_realm,
621 const char *tgt_realm)
623 krb5_error_code ret = 0;
624 char **realms, **tmp;
625 unsigned int num_realms;
626 size_t i;
628 switch (tr->tr_type) {
629 case DOMAIN_X500_COMPRESS:
630 break;
631 case 0:
633 * Allow empty content of type 0 because that is was Microsoft
634 * generates in their TGT.
636 if (tr->contents.length == 0)
637 break;
638 kdc_log(context, config, 0,
639 "Transited type 0 with non empty content");
640 return KRB5KDC_ERR_TRTYPE_NOSUPP;
641 default:
642 kdc_log(context, config, 0,
643 "Unknown transited type: %u", tr->tr_type);
644 return KRB5KDC_ERR_TRTYPE_NOSUPP;
647 ret = krb5_domain_x500_decode(context,
648 tr->contents,
649 &realms,
650 &num_realms,
651 client_realm,
652 server_realm);
653 if(ret){
654 krb5_warn(context, ret,
655 "Decoding transited encoding");
656 return ret;
658 if(strcmp(client_realm, tgt_realm) && strcmp(server_realm, tgt_realm)) {
659 /* not us, so add the previous realm to transited set */
660 if (num_realms + 1 > UINT_MAX/sizeof(*realms)) {
661 ret = ERANGE;
662 goto free_realms;
664 tmp = realloc(realms, (num_realms + 1) * sizeof(*realms));
665 if(tmp == NULL){
666 ret = ENOMEM;
667 goto free_realms;
669 realms = tmp;
670 realms[num_realms] = strdup(tgt_realm);
671 if(realms[num_realms] == NULL){
672 ret = ENOMEM;
673 goto free_realms;
675 num_realms++;
677 if(num_realms == 0) {
678 if(strcmp(client_realm, server_realm))
679 kdc_log(context, config, 0,
680 "cross-realm %s -> %s", client_realm, server_realm);
681 } else {
682 size_t l = 0;
683 char *rs;
684 for(i = 0; i < num_realms; i++)
685 l += strlen(realms[i]) + 2;
686 rs = malloc(l);
687 if(rs != NULL) {
688 *rs = '\0';
689 for(i = 0; i < num_realms; i++) {
690 if(i > 0)
691 strlcat(rs, ", ", l);
692 strlcat(rs, realms[i], l);
694 kdc_log(context, config, 0,
695 "cross-realm %s -> %s via [%s]",
696 client_realm, server_realm, rs);
697 free(rs);
700 if(check_policy) {
701 ret = krb5_check_transited(context, client_realm,
702 server_realm,
703 realms, num_realms, NULL);
704 if(ret) {
705 krb5_warn(context, ret, "cross-realm %s -> %s",
706 client_realm, server_realm);
707 goto free_realms;
709 et->flags.transited_policy_checked = 1;
711 et->transited.tr_type = DOMAIN_X500_COMPRESS;
712 ret = krb5_domain_x500_encode(realms, num_realms, &et->transited.contents);
713 if(ret)
714 krb5_warn(context, ret, "Encoding transited encoding");
715 free_realms:
716 for(i = 0; i < num_realms; i++)
717 free(realms[i]);
718 free(realms);
719 return ret;
723 static krb5_error_code
724 tgs_make_reply(krb5_context context,
725 krb5_kdc_configuration *config,
726 KDC_REQ_BODY *b,
727 krb5_const_principal tgt_name,
728 const EncTicketPart *tgt,
729 const krb5_keyblock *replykey,
730 int rk_is_subkey,
731 const EncryptionKey *serverkey,
732 const krb5_keyblock *sessionkey,
733 krb5_kvno kvno,
734 AuthorizationData *auth_data,
735 hdb_entry_ex *server,
736 krb5_principal server_principal,
737 const char *server_name,
738 hdb_entry_ex *client,
739 krb5_principal client_principal,
740 hdb_entry_ex *krbtgt,
741 krb5_enctype krbtgt_etype,
742 krb5_principals spp,
743 const krb5_data *rspac,
744 const METHOD_DATA *enc_pa_data,
745 const char **e_text,
746 krb5_data *reply)
748 KDC_REP rep;
749 EncKDCRepPart ek;
750 EncTicketPart et;
751 KDCOptions f = b->kdc_options;
752 krb5_error_code ret;
753 int is_weak = 0;
755 memset(&rep, 0, sizeof(rep));
756 memset(&et, 0, sizeof(et));
757 memset(&ek, 0, sizeof(ek));
759 rep.pvno = 5;
760 rep.msg_type = krb_tgs_rep;
762 et.authtime = tgt->authtime;
763 _kdc_fix_time(&b->till);
764 et.endtime = min(tgt->endtime, *b->till);
765 ALLOC(et.starttime);
766 *et.starttime = kdc_time;
768 ret = check_tgs_flags(context, config, b, tgt, &et);
769 if(ret)
770 goto out;
772 /* We should check the transited encoding if:
773 1) the request doesn't ask not to be checked
774 2) globally enforcing a check
775 3) principal requires checking
776 4) we allow non-check per-principal, but principal isn't marked as allowing this
777 5) we don't globally allow this
780 #define GLOBAL_FORCE_TRANSITED_CHECK \
781 (config->trpolicy == TRPOLICY_ALWAYS_CHECK)
782 #define GLOBAL_ALLOW_PER_PRINCIPAL \
783 (config->trpolicy == TRPOLICY_ALLOW_PER_PRINCIPAL)
784 #define GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK \
785 (config->trpolicy == TRPOLICY_ALWAYS_HONOUR_REQUEST)
787 /* these will consult the database in future release */
788 #define PRINCIPAL_FORCE_TRANSITED_CHECK(P) 0
789 #define PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(P) 0
791 ret = fix_transited_encoding(context, config,
792 !f.disable_transited_check ||
793 GLOBAL_FORCE_TRANSITED_CHECK ||
794 PRINCIPAL_FORCE_TRANSITED_CHECK(server) ||
795 !((GLOBAL_ALLOW_PER_PRINCIPAL &&
796 PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(server)) ||
797 GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK),
798 &tgt->transited, &et,
799 krb5_principal_get_realm(context, client_principal),
800 krb5_principal_get_realm(context, server->entry.principal),
801 krb5_principal_get_realm(context, krbtgt->entry.principal));
802 if(ret)
803 goto out;
805 copy_Realm(&server_principal->realm, &rep.ticket.realm);
806 _krb5_principal2principalname(&rep.ticket.sname, server_principal);
807 copy_Realm(&tgt_name->realm, &rep.crealm);
809 if (f.request_anonymous)
810 _kdc_make_anonymous_principalname (&rep.cname);
811 else */
813 copy_PrincipalName(&tgt_name->name, &rep.cname);
814 rep.ticket.tkt_vno = 5;
816 ek.caddr = et.caddr;
817 if(et.caddr == NULL)
818 et.caddr = tgt->caddr;
821 time_t life;
822 life = et.endtime - *et.starttime;
823 if(client && client->entry.max_life)
824 life = min(life, *client->entry.max_life);
825 if(server->entry.max_life)
826 life = min(life, *server->entry.max_life);
827 et.endtime = *et.starttime + life;
829 if(f.renewable_ok && tgt->flags.renewable &&
830 et.renew_till == NULL && et.endtime < *b->till &&
831 tgt->renew_till != NULL)
833 et.flags.renewable = 1;
834 ALLOC(et.renew_till);
835 *et.renew_till = *b->till;
837 if(et.renew_till){
838 time_t renew;
839 renew = *et.renew_till - *et.starttime;
840 if(client && client->entry.max_renew)
841 renew = min(renew, *client->entry.max_renew);
842 if(server->entry.max_renew)
843 renew = min(renew, *server->entry.max_renew);
844 *et.renew_till = *et.starttime + renew;
847 if(et.renew_till){
848 *et.renew_till = min(*et.renew_till, *tgt->renew_till);
849 *et.starttime = min(*et.starttime, *et.renew_till);
850 et.endtime = min(et.endtime, *et.renew_till);
853 *et.starttime = min(*et.starttime, et.endtime);
855 if(*et.starttime == et.endtime){
856 ret = KRB5KDC_ERR_NEVER_VALID;
857 goto out;
859 if(et.renew_till && et.endtime == *et.renew_till){
860 free(et.renew_till);
861 et.renew_till = NULL;
862 et.flags.renewable = 0;
865 et.flags.pre_authent = tgt->flags.pre_authent;
866 et.flags.hw_authent = tgt->flags.hw_authent;
867 et.flags.anonymous = tgt->flags.anonymous;
868 et.flags.ok_as_delegate = server->entry.flags.ok_as_delegate;
870 if(rspac->length) {
872 * No not need to filter out the any PAC from the
873 * auth_data since it's signed by the KDC.
875 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
876 KRB5_AUTHDATA_WIN2K_PAC, rspac);
877 if (ret)
878 goto out;
881 if (auth_data) {
882 unsigned int i = 0;
884 /* XXX check authdata */
886 if (et.authorization_data == NULL) {
887 et.authorization_data = calloc(1, sizeof(*et.authorization_data));
888 if (et.authorization_data == NULL) {
889 ret = ENOMEM;
890 krb5_set_error_message(context, ret, "malloc: out of memory");
891 goto out;
894 for(i = 0; i < auth_data->len ; i++) {
895 ret = add_AuthorizationData(et.authorization_data, &auth_data->val[i]);
896 if (ret) {
897 krb5_set_error_message(context, ret, "malloc: out of memory");
898 goto out;
902 /* Filter out type KRB5SignedPath */
903 ret = find_KRB5SignedPath(context, et.authorization_data, NULL);
904 if (ret == 0) {
905 if (et.authorization_data->len == 1) {
906 free_AuthorizationData(et.authorization_data);
907 free(et.authorization_data);
908 et.authorization_data = NULL;
909 } else {
910 AuthorizationData *ad = et.authorization_data;
911 free_AuthorizationDataElement(&ad->val[ad->len - 1]);
912 ad->len--;
917 ret = krb5_copy_keyblock_contents(context, sessionkey, &et.key);
918 if (ret)
919 goto out;
920 et.crealm = tgt_name->realm;
921 et.cname = tgt_name->name;
923 ek.key = et.key;
924 /* MIT must have at least one last_req */
925 ek.last_req.len = 1;
926 ek.last_req.val = calloc(1, sizeof(*ek.last_req.val));
927 if (ek.last_req.val == NULL) {
928 ret = ENOMEM;
929 goto out;
931 ek.nonce = b->nonce;
932 ek.flags = et.flags;
933 ek.authtime = et.authtime;
934 ek.starttime = et.starttime;
935 ek.endtime = et.endtime;
936 ek.renew_till = et.renew_till;
937 ek.srealm = rep.ticket.realm;
938 ek.sname = rep.ticket.sname;
940 _kdc_log_timestamp(context, config, "TGS-REQ", et.authtime, et.starttime,
941 et.endtime, et.renew_till);
943 /* Don't sign cross realm tickets, they can't be checked anyway */
945 char *r = get_krbtgt_realm(&ek.sname);
947 if (r == NULL || strcmp(r, ek.srealm) == 0) {
948 ret = _kdc_add_KRB5SignedPath(context,
949 config,
950 krbtgt,
951 krbtgt_etype,
952 client_principal,
953 NULL,
954 spp,
955 &et);
956 if (ret)
957 goto out;
961 if (enc_pa_data->len) {
962 rep.padata = calloc(1, sizeof(*rep.padata));
963 if (rep.padata == NULL) {
964 ret = ENOMEM;
965 goto out;
967 ret = copy_METHOD_DATA(enc_pa_data, rep.padata);
968 if (ret)
969 goto out;
972 if (krb5_enctype_valid(context, serverkey->keytype) != 0
973 && _kdc_is_weak_exception(server->entry.principal, serverkey->keytype))
975 krb5_enctype_enable(context, serverkey->keytype);
976 is_weak = 1;
980 /* It is somewhat unclear where the etype in the following
981 encryption should come from. What we have is a session
982 key in the passed tgt, and a list of preferred etypes
983 *for the new ticket*. Should we pick the best possible
984 etype, given the keytype in the tgt, or should we look
985 at the etype list here as well? What if the tgt
986 session key is DES3 and we want a ticket with a (say)
987 CAST session key. Should the DES3 etype be added to the
988 etype list, even if we don't want a session key with
989 DES3? */
990 ret = _kdc_encode_reply(context, config, NULL, 0,
991 &rep, &et, &ek, serverkey->keytype,
992 kvno,
993 serverkey, 0, replykey, rk_is_subkey,
994 e_text, reply);
995 if (is_weak)
996 krb5_enctype_disable(context, serverkey->keytype);
998 out:
999 free_TGS_REP(&rep);
1000 free_TransitedEncoding(&et.transited);
1001 if(et.starttime)
1002 free(et.starttime);
1003 if(et.renew_till)
1004 free(et.renew_till);
1005 if(et.authorization_data) {
1006 free_AuthorizationData(et.authorization_data);
1007 free(et.authorization_data);
1009 free_LastReq(&ek.last_req);
1010 memset(et.key.keyvalue.data, 0, et.key.keyvalue.length);
1011 free_EncryptionKey(&et.key);
1012 return ret;
1015 static krb5_error_code
1016 tgs_check_authenticator(krb5_context context,
1017 krb5_kdc_configuration *config,
1018 krb5_auth_context ac,
1019 KDC_REQ_BODY *b,
1020 const char **e_text,
1021 krb5_keyblock *key)
1023 krb5_authenticator auth;
1024 size_t len = 0;
1025 unsigned char *buf;
1026 size_t buf_size;
1027 krb5_error_code ret;
1028 krb5_crypto crypto;
1030 krb5_auth_con_getauthenticator(context, ac, &auth);
1031 if(auth->cksum == NULL){
1032 kdc_log(context, config, 0, "No authenticator in request");
1033 ret = KRB5KRB_AP_ERR_INAPP_CKSUM;
1034 goto out;
1037 * according to RFC1510 it doesn't need to be keyed,
1038 * but according to the latest draft it needs to.
1040 if (
1041 #if 0
1042 !krb5_checksum_is_keyed(context, auth->cksum->cksumtype)
1044 #endif
1045 !krb5_checksum_is_collision_proof(context, auth->cksum->cksumtype)) {
1046 kdc_log(context, config, 0, "Bad checksum type in authenticator: %d",
1047 auth->cksum->cksumtype);
1048 ret = KRB5KRB_AP_ERR_INAPP_CKSUM;
1049 goto out;
1052 /* XXX should not re-encode this */
1053 ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, buf_size, b, &len, ret);
1054 if(ret){
1055 const char *msg = krb5_get_error_message(context, ret);
1056 kdc_log(context, config, 0, "Failed to encode KDC-REQ-BODY: %s", msg);
1057 krb5_free_error_message(context, msg);
1058 goto out;
1060 if(buf_size != len) {
1061 free(buf);
1062 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
1063 *e_text = "KDC internal error";
1064 ret = KRB5KRB_ERR_GENERIC;
1065 goto out;
1067 ret = krb5_crypto_init(context, key, 0, &crypto);
1068 if (ret) {
1069 const char *msg = krb5_get_error_message(context, ret);
1070 free(buf);
1071 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
1072 krb5_free_error_message(context, msg);
1073 goto out;
1075 ret = krb5_verify_checksum(context,
1076 crypto,
1077 KRB5_KU_TGS_REQ_AUTH_CKSUM,
1078 buf,
1079 len,
1080 auth->cksum);
1081 free(buf);
1082 krb5_crypto_destroy(context, crypto);
1083 if(ret){
1084 const char *msg = krb5_get_error_message(context, ret);
1085 kdc_log(context, config, 0,
1086 "Failed to verify authenticator checksum: %s", msg);
1087 krb5_free_error_message(context, msg);
1089 out:
1090 free_Authenticator(auth);
1091 free(auth);
1092 return ret;
1099 static const char *
1100 find_rpath(krb5_context context, Realm crealm, Realm srealm)
1102 const char *new_realm = krb5_config_get_string(context,
1103 NULL,
1104 "capaths",
1105 crealm,
1106 srealm,
1107 NULL);
1108 return new_realm;
1112 static krb5_boolean
1113 need_referral(krb5_context context, krb5_kdc_configuration *config,
1114 const KDCOptions * const options, krb5_principal server,
1115 krb5_realm **realms)
1117 const char *name;
1119 if(!options->canonicalize && server->name.name_type != KRB5_NT_SRV_INST)
1120 return FALSE;
1122 if (server->name.name_string.len == 1)
1123 name = server->name.name_string.val[0];
1124 else if (server->name.name_string.len == 3) {
1126 This is used to give referrals for the
1127 E3514235-4B06-11D1-AB04-00C04FC2DCD2/NTDSGUID/DNSDOMAIN
1128 SPN form, which is used for inter-domain communication in AD
1130 name = server->name.name_string.val[2];
1131 kdc_log(context, config, 0, "Giving 3 part referral for %s", name);
1132 *realms = malloc(sizeof(char *)*2);
1133 if (*realms == NULL) {
1134 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1135 return FALSE;
1137 (*realms)[0] = strdup(name);
1138 (*realms)[1] = NULL;
1139 return TRUE;
1140 } else if (server->name.name_string.len > 1)
1141 name = server->name.name_string.val[1];
1142 else
1143 return FALSE;
1145 kdc_log(context, config, 0, "Searching referral for %s", name);
1147 return _krb5_get_host_realm_int(context, name, FALSE, realms) == 0;
1150 static krb5_error_code
1151 tgs_parse_request(krb5_context context,
1152 krb5_kdc_configuration *config,
1153 KDC_REQ_BODY *b,
1154 const PA_DATA *tgs_req,
1155 hdb_entry_ex **krbtgt,
1156 krb5_enctype *krbtgt_etype,
1157 krb5_ticket **ticket,
1158 const char **e_text,
1159 const char *from,
1160 const struct sockaddr *from_addr,
1161 time_t **csec,
1162 int **cusec,
1163 AuthorizationData **auth_data,
1164 krb5_keyblock **replykey,
1165 int *rk_is_subkey)
1167 static char failed[] = "<unparse_name failed>";
1168 krb5_ap_req ap_req;
1169 krb5_error_code ret;
1170 krb5_principal princ;
1171 krb5_auth_context ac = NULL;
1172 krb5_flags ap_req_options;
1173 krb5_flags verify_ap_req_flags;
1174 krb5_crypto crypto;
1175 krb5uint32 krbtgt_kvno; /* kvno used for the PA-TGS-REQ AP-REQ Ticket */
1176 krb5uint32 krbtgt_kvno_try;
1177 int kvno_search_tries = 4; /* number of kvnos to try when tkt_vno == 0 */
1178 const Keys *krbtgt_keys;/* keyset for TGT tkt_vno */
1179 Key *tkey;
1180 krb5_keyblock *subkey = NULL;
1181 unsigned usage;
1183 *auth_data = NULL;
1184 *csec = NULL;
1185 *cusec = NULL;
1186 *replykey = NULL;
1188 memset(&ap_req, 0, sizeof(ap_req));
1189 ret = krb5_decode_ap_req(context, &tgs_req->padata_value, &ap_req);
1190 if(ret){
1191 const char *msg = krb5_get_error_message(context, ret);
1192 kdc_log(context, config, 0, "Failed to decode AP-REQ: %s", msg);
1193 krb5_free_error_message(context, msg);
1194 goto out;
1197 if(!get_krbtgt_realm(&ap_req.ticket.sname)){
1198 /* XXX check for ticket.sname == req.sname */
1199 kdc_log(context, config, 0, "PA-DATA is not a ticket-granting ticket");
1200 ret = KRB5KDC_ERR_POLICY; /* ? */
1201 goto out;
1204 _krb5_principalname2krb5_principal(context,
1205 &princ,
1206 ap_req.ticket.sname,
1207 ap_req.ticket.realm);
1209 krbtgt_kvno = ap_req.ticket.enc_part.kvno ? *ap_req.ticket.enc_part.kvno : 0;
1210 ret = _kdc_db_fetch(context, config, princ, HDB_F_GET_KRBTGT,
1211 &krbtgt_kvno, NULL, krbtgt);
1213 if (ret == HDB_ERR_NOT_FOUND_HERE) {
1214 /* XXX Factor out this unparsing of the same princ all over */
1215 char *p;
1216 ret = krb5_unparse_name(context, princ, &p);
1217 if (ret != 0)
1218 p = failed;
1219 krb5_free_principal(context, princ);
1220 kdc_log(context, config, 5,
1221 "Ticket-granting ticket account %s does not have secrets at "
1222 "this KDC, need to proxy", p);
1223 if (ret == 0)
1224 free(p);
1225 ret = HDB_ERR_NOT_FOUND_HERE;
1226 goto out;
1227 } else if (ret == HDB_ERR_KVNO_NOT_FOUND) {
1228 char *p;
1229 ret = krb5_unparse_name(context, princ, &p);
1230 if (ret != 0)
1231 p = failed;
1232 krb5_free_principal(context, princ);
1233 kdc_log(context, config, 5,
1234 "Ticket-granting ticket account %s does not have keys for "
1235 "kvno %d at this KDC", p, krbtgt_kvno);
1236 if (ret == 0)
1237 free(p);
1238 ret = HDB_ERR_KVNO_NOT_FOUND;
1239 goto out;
1240 } else if (ret == HDB_ERR_NO_MKEY) {
1241 char *p;
1242 ret = krb5_unparse_name(context, princ, &p);
1243 if (ret != 0)
1244 p = failed;
1245 krb5_free_principal(context, princ);
1246 kdc_log(context, config, 5,
1247 "Missing master key for decrypting keys for ticket-granting "
1248 "ticket account %s with kvno %d at this KDC", p, krbtgt_kvno);
1249 if (ret == 0)
1250 free(p);
1251 ret = HDB_ERR_KVNO_NOT_FOUND;
1252 goto out;
1253 } else if (ret) {
1254 const char *msg = krb5_get_error_message(context, ret);
1255 char *p;
1256 ret = krb5_unparse_name(context, princ, &p);
1257 if (ret != 0)
1258 p = failed;
1259 krb5_free_principal(context, princ);
1260 kdc_log(context, config, 0,
1261 "Ticket-granting ticket not found in database: %s", msg);
1262 krb5_free_error_message(context, msg);
1263 if (ret == 0)
1264 free(p);
1265 ret = KRB5KRB_AP_ERR_NOT_US;
1266 goto out;
1269 krbtgt_kvno_try = krbtgt_kvno ? krbtgt_kvno : (*krbtgt)->entry.kvno;
1270 *krbtgt_etype = ap_req.ticket.enc_part.etype;
1272 next_kvno:
1273 krbtgt_keys = hdb_kvno2keys(context, &(*krbtgt)->entry, krbtgt_kvno_try);
1274 ret = hdb_enctype2key(context, &(*krbtgt)->entry, krbtgt_keys,
1275 ap_req.ticket.enc_part.etype, &tkey);
1276 if (ret && krbtgt_kvno == 0 && kvno_search_tries > 0) {
1277 kvno_search_tries--;
1278 krbtgt_kvno_try--;
1279 goto next_kvno;
1280 } else if (ret) {
1281 char *str = NULL, *p = NULL;
1283 krb5_enctype_to_string(context, ap_req.ticket.enc_part.etype, &str);
1284 krb5_unparse_name(context, princ, &p);
1285 kdc_log(context, config, 0,
1286 "No server key with enctype %s found for %s",
1287 str ? str : "<unknown enctype>",
1288 p ? p : "<unparse_name failed>");
1289 free(str);
1290 free(p);
1291 ret = KRB5KRB_AP_ERR_BADKEYVER;
1292 goto out;
1295 if (b->kdc_options.validate)
1296 verify_ap_req_flags = KRB5_VERIFY_AP_REQ_IGNORE_INVALID;
1297 else
1298 verify_ap_req_flags = 0;
1300 ret = krb5_verify_ap_req2(context,
1301 &ac,
1302 &ap_req,
1303 princ,
1304 &tkey->key,
1305 verify_ap_req_flags,
1306 &ap_req_options,
1307 ticket,
1308 KRB5_KU_TGS_REQ_AUTH);
1309 if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY && kvno_search_tries > 0) {
1310 kvno_search_tries--;
1311 krbtgt_kvno_try--;
1312 goto next_kvno;
1315 krb5_free_principal(context, princ);
1316 if(ret) {
1317 const char *msg = krb5_get_error_message(context, ret);
1318 kdc_log(context, config, 0, "Failed to verify AP-REQ: %s", msg);
1319 krb5_free_error_message(context, msg);
1320 goto out;
1324 krb5_authenticator auth;
1326 ret = krb5_auth_con_getauthenticator(context, ac, &auth);
1327 if (ret == 0) {
1328 *csec = malloc(sizeof(**csec));
1329 if (*csec == NULL) {
1330 krb5_free_authenticator(context, &auth);
1331 kdc_log(context, config, 0, "malloc failed");
1332 goto out;
1334 **csec = auth->ctime;
1335 *cusec = malloc(sizeof(**cusec));
1336 if (*cusec == NULL) {
1337 krb5_free_authenticator(context, &auth);
1338 kdc_log(context, config, 0, "malloc failed");
1339 goto out;
1341 **cusec = auth->cusec;
1342 krb5_free_authenticator(context, &auth);
1346 ret = tgs_check_authenticator(context, config,
1347 ac, b, e_text, &(*ticket)->ticket.key);
1348 if (ret) {
1349 krb5_auth_con_free(context, ac);
1350 goto out;
1353 usage = KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY;
1354 *rk_is_subkey = 1;
1356 ret = krb5_auth_con_getremotesubkey(context, ac, &subkey);
1357 if(ret){
1358 const char *msg = krb5_get_error_message(context, ret);
1359 krb5_auth_con_free(context, ac);
1360 kdc_log(context, config, 0, "Failed to get remote subkey: %s", msg);
1361 krb5_free_error_message(context, msg);
1362 goto out;
1364 if(subkey == NULL){
1365 usage = KRB5_KU_TGS_REQ_AUTH_DAT_SESSION;
1366 *rk_is_subkey = 0;
1368 ret = krb5_auth_con_getkey(context, ac, &subkey);
1369 if(ret) {
1370 const char *msg = krb5_get_error_message(context, ret);
1371 krb5_auth_con_free(context, ac);
1372 kdc_log(context, config, 0, "Failed to get session key: %s", msg);
1373 krb5_free_error_message(context, msg);
1374 goto out;
1377 if(subkey == NULL){
1378 krb5_auth_con_free(context, ac);
1379 kdc_log(context, config, 0,
1380 "Failed to get key for enc-authorization-data");
1381 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY; /* ? */
1382 goto out;
1385 *replykey = subkey;
1387 if (b->enc_authorization_data) {
1388 krb5_data ad;
1390 ret = krb5_crypto_init(context, subkey, 0, &crypto);
1391 if (ret) {
1392 const char *msg = krb5_get_error_message(context, ret);
1393 krb5_auth_con_free(context, ac);
1394 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
1395 krb5_free_error_message(context, msg);
1396 goto out;
1398 ret = krb5_decrypt_EncryptedData (context,
1399 crypto,
1400 usage,
1401 b->enc_authorization_data,
1402 &ad);
1403 krb5_crypto_destroy(context, crypto);
1404 if(ret){
1405 krb5_auth_con_free(context, ac);
1406 kdc_log(context, config, 0,
1407 "Failed to decrypt enc-authorization-data");
1408 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY; /* ? */
1409 goto out;
1411 ALLOC(*auth_data);
1412 if (*auth_data == NULL) {
1413 krb5_auth_con_free(context, ac);
1414 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY; /* ? */
1415 goto out;
1417 ret = decode_AuthorizationData(ad.data, ad.length, *auth_data, NULL);
1418 if(ret){
1419 krb5_auth_con_free(context, ac);
1420 free(*auth_data);
1421 *auth_data = NULL;
1422 kdc_log(context, config, 0, "Failed to decode authorization data");
1423 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY; /* ? */
1424 goto out;
1428 krb5_auth_con_free(context, ac);
1430 out:
1431 free_AP_REQ(&ap_req);
1433 return ret;
1436 static krb5_error_code
1437 build_server_referral(krb5_context context,
1438 krb5_kdc_configuration *config,
1439 krb5_crypto session,
1440 krb5_const_realm referred_realm,
1441 const PrincipalName *true_principal_name,
1442 const PrincipalName *requested_principal,
1443 krb5_data *outdata)
1445 PA_ServerReferralData ref;
1446 krb5_error_code ret;
1447 EncryptedData ed;
1448 krb5_data data;
1449 size_t size = 0;
1451 memset(&ref, 0, sizeof(ref));
1453 if (referred_realm) {
1454 ALLOC(ref.referred_realm);
1455 if (ref.referred_realm == NULL)
1456 goto eout;
1457 *ref.referred_realm = strdup(referred_realm);
1458 if (*ref.referred_realm == NULL)
1459 goto eout;
1461 if (true_principal_name) {
1462 ALLOC(ref.true_principal_name);
1463 if (ref.true_principal_name == NULL)
1464 goto eout;
1465 ret = copy_PrincipalName(true_principal_name, ref.true_principal_name);
1466 if (ret)
1467 goto eout;
1469 if (requested_principal) {
1470 ALLOC(ref.requested_principal_name);
1471 if (ref.requested_principal_name == NULL)
1472 goto eout;
1473 ret = copy_PrincipalName(requested_principal,
1474 ref.requested_principal_name);
1475 if (ret)
1476 goto eout;
1479 ASN1_MALLOC_ENCODE(PA_ServerReferralData,
1480 data.data, data.length,
1481 &ref, &size, ret);
1482 free_PA_ServerReferralData(&ref);
1483 if (ret)
1484 return ret;
1485 if (data.length != size)
1486 krb5_abortx(context, "internal asn.1 encoder error");
1488 ret = krb5_encrypt_EncryptedData(context, session,
1489 KRB5_KU_PA_SERVER_REFERRAL,
1490 data.data, data.length,
1491 0 /* kvno */, &ed);
1492 free(data.data);
1493 if (ret)
1494 return ret;
1496 ASN1_MALLOC_ENCODE(EncryptedData,
1497 outdata->data, outdata->length,
1498 &ed, &size, ret);
1499 free_EncryptedData(&ed);
1500 if (ret)
1501 return ret;
1502 if (outdata->length != size)
1503 krb5_abortx(context, "internal asn.1 encoder error");
1505 return 0;
1506 eout:
1507 free_PA_ServerReferralData(&ref);
1508 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
1509 return ENOMEM;
1512 static krb5_error_code
1513 tgs_build_reply(krb5_context context,
1514 krb5_kdc_configuration *config,
1515 KDC_REQ *req,
1516 KDC_REQ_BODY *b,
1517 hdb_entry_ex *krbtgt,
1518 krb5_enctype krbtgt_etype,
1519 const krb5_keyblock *replykey,
1520 int rk_is_subkey,
1521 krb5_ticket *ticket,
1522 krb5_data *reply,
1523 const char *from,
1524 const char **e_text,
1525 AuthorizationData **auth_data,
1526 const struct sockaddr *from_addr)
1528 krb5_error_code ret;
1529 krb5_principal cp = NULL, sp = NULL, rsp = NULL, tp = NULL, dp = NULL;
1530 krb5_principal krbtgt_out_principal = NULL;
1531 char *spn = NULL, *cpn = NULL, *tpn = NULL, *dpn = NULL, *krbtgt_out_n = NULL;
1532 hdb_entry_ex *server = NULL, *client = NULL, *s4u2self_impersonated_client = NULL;
1533 HDB *clientdb, *s4u2self_impersonated_clientdb;
1534 krb5_realm ref_realm = NULL;
1535 EncTicketPart *tgt = &ticket->ticket;
1536 krb5_principals spp = NULL;
1537 const EncryptionKey *ekey;
1538 krb5_keyblock sessionkey;
1539 krb5_kvno kvno;
1540 krb5_data rspac;
1542 hdb_entry_ex *krbtgt_out = NULL;
1544 METHOD_DATA enc_pa_data;
1546 PrincipalName *s;
1547 Realm r;
1548 int nloop = 0;
1549 EncTicketPart adtkt;
1550 char opt_str[128];
1551 int signedpath = 0;
1553 Key *tkey_check;
1554 Key *tkey_sign;
1555 int flags = HDB_F_FOR_TGS_REQ;
1557 memset(&sessionkey, 0, sizeof(sessionkey));
1558 memset(&adtkt, 0, sizeof(adtkt));
1559 krb5_data_zero(&rspac);
1560 memset(&enc_pa_data, 0, sizeof(enc_pa_data));
1562 s = b->sname;
1563 r = b->realm;
1566 * Always to do CANON, see comment below about returned server principal (rsp).
1568 flags |= HDB_F_CANON;
1570 if(b->kdc_options.enc_tkt_in_skey){
1571 Ticket *t;
1572 hdb_entry_ex *uu;
1573 krb5_principal p;
1574 Key *uukey;
1576 if(b->additional_tickets == NULL ||
1577 b->additional_tickets->len == 0){
1578 ret = KRB5KDC_ERR_BADOPTION; /* ? */
1579 kdc_log(context, config, 0,
1580 "No second ticket present in request");
1581 goto out;
1583 t = &b->additional_tickets->val[0];
1584 if(!get_krbtgt_realm(&t->sname)){
1585 kdc_log(context, config, 0,
1586 "Additional ticket is not a ticket-granting ticket");
1587 ret = KRB5KDC_ERR_POLICY;
1588 goto out;
1590 _krb5_principalname2krb5_principal(context, &p, t->sname, t->realm);
1591 ret = _kdc_db_fetch(context, config, p,
1592 HDB_F_GET_KRBTGT, t->enc_part.kvno,
1593 NULL, &uu);
1594 krb5_free_principal(context, p);
1595 if(ret){
1596 if (ret == HDB_ERR_NOENTRY)
1597 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1598 goto out;
1600 ret = hdb_enctype2key(context, &uu->entry, NULL,
1601 t->enc_part.etype, &uukey);
1602 if(ret){
1603 _kdc_free_ent(context, uu);
1604 ret = KRB5KDC_ERR_ETYPE_NOSUPP; /* XXX */
1605 goto out;
1607 ret = krb5_decrypt_ticket(context, t, &uukey->key, &adtkt, 0);
1608 _kdc_free_ent(context, uu);
1609 if(ret)
1610 goto out;
1612 ret = verify_flags(context, config, &adtkt, spn);
1613 if (ret)
1614 goto out;
1616 s = &adtkt.cname;
1617 r = adtkt.crealm;
1620 _krb5_principalname2krb5_principal(context, &sp, *s, r);
1621 ret = krb5_unparse_name(context, sp, &spn);
1622 if (ret)
1623 goto out;
1624 _krb5_principalname2krb5_principal(context, &cp, tgt->cname, tgt->crealm);
1625 ret = krb5_unparse_name(context, cp, &cpn);
1626 if (ret)
1627 goto out;
1628 unparse_flags (KDCOptions2int(b->kdc_options),
1629 asn1_KDCOptions_units(),
1630 opt_str, sizeof(opt_str));
1631 if(*opt_str)
1632 kdc_log(context, config, 0,
1633 "TGS-REQ %s from %s for %s [%s]",
1634 cpn, from, spn, opt_str);
1635 else
1636 kdc_log(context, config, 0,
1637 "TGS-REQ %s from %s for %s", cpn, from, spn);
1640 * Fetch server
1643 server_lookup:
1644 ret = _kdc_db_fetch(context, config, sp, HDB_F_GET_SERVER | flags,
1645 NULL, NULL, &server);
1647 if (ret == HDB_ERR_NOT_FOUND_HERE) {
1648 kdc_log(context, config, 5, "target %s does not have secrets at this KDC, need to proxy", sp);
1649 goto out;
1650 } else if (ret == HDB_ERR_WRONG_REALM) {
1651 if (ref_realm)
1652 free(ref_realm);
1653 ref_realm = strdup(server->entry.principal->realm);
1654 if (ref_realm == NULL) {
1655 ret = ENOMEM;
1656 goto out;
1659 kdc_log(context, config, 5,
1660 "Returning a referral to realm %s for "
1661 "server %s.",
1662 ref_realm, spn);
1663 krb5_free_principal(context, sp);
1664 sp = NULL;
1665 free(spn);
1666 spn = NULL;
1667 ret = krb5_make_principal(context, &sp, r, KRB5_TGS_NAME,
1668 ref_realm, NULL);
1669 if (ret)
1670 goto out;
1671 ret = krb5_unparse_name(context, sp, &spn);
1672 if (ret)
1673 goto out;
1675 goto server_lookup;
1676 } else if (ret) {
1677 const char *new_rlm, *msg;
1678 Realm req_rlm;
1679 krb5_realm *realms;
1681 if ((req_rlm = get_krbtgt_realm(&sp->name)) != NULL) {
1682 if (nloop++ < 2) {
1683 new_rlm = find_rpath(context, tgt->crealm, req_rlm);
1684 if(new_rlm) {
1685 kdc_log(context, config, 5, "krbtgt for realm %s "
1686 "not found, trying %s",
1687 req_rlm, new_rlm);
1688 krb5_free_principal(context, sp);
1689 free(spn);
1690 krb5_make_principal(context, &sp, r,
1691 KRB5_TGS_NAME, new_rlm, NULL);
1692 ret = krb5_unparse_name(context, sp, &spn);
1693 if (ret)
1694 goto out;
1696 if (ref_realm)
1697 free(ref_realm);
1698 ref_realm = strdup(new_rlm);
1699 goto server_lookup;
1702 } else if (need_referral(context, config, &b->kdc_options, sp, &realms)) {
1703 if (strcmp(realms[0], sp->realm) != 0) {
1704 kdc_log(context, config, 5,
1705 "Returning a referral to realm %s for "
1706 "server %s that was not found",
1707 realms[0], spn);
1708 krb5_free_principal(context, sp);
1709 free(spn);
1710 krb5_make_principal(context, &sp, r, KRB5_TGS_NAME,
1711 realms[0], NULL);
1712 ret = krb5_unparse_name(context, sp, &spn);
1713 if (ret) {
1714 krb5_free_host_realm(context, realms);
1715 goto out;
1718 if (ref_realm)
1719 free(ref_realm);
1720 ref_realm = strdup(realms[0]);
1722 krb5_free_host_realm(context, realms);
1723 goto server_lookup;
1725 krb5_free_host_realm(context, realms);
1727 msg = krb5_get_error_message(context, ret);
1728 kdc_log(context, config, 0,
1729 "Server not found in database: %s: %s", spn, msg);
1730 krb5_free_error_message(context, msg);
1731 if (ret == HDB_ERR_NOENTRY)
1732 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1733 goto out;
1736 /* the name returned to the client depend on what was asked for,
1737 * return canonical name if kdc_options.canonicalize was set, the
1738 * client wants the true name of the principal, if not it just
1739 * wants the name its asked for.
1742 if (b->kdc_options.canonicalize)
1743 rsp = server->entry.principal;
1744 else
1745 rsp = sp;
1749 * Select enctype, return key and kvno.
1753 krb5_enctype etype;
1755 if(b->kdc_options.enc_tkt_in_skey) {
1756 size_t i;
1757 ekey = &adtkt.key;
1758 for(i = 0; i < b->etype.len; i++)
1759 if (b->etype.val[i] == adtkt.key.keytype)
1760 break;
1761 if(i == b->etype.len) {
1762 kdc_log(context, config, 0,
1763 "Addition ticket have not matching etypes");
1764 krb5_clear_error_message(context);
1765 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1766 goto out;
1768 etype = b->etype.val[i];
1769 kvno = 0;
1770 } else {
1771 Key *skey;
1773 ret = _kdc_find_etype(context,
1774 krb5_principal_is_krbtgt(context, sp) ?
1775 config->tgt_use_strongest_session_key :
1776 config->svc_use_strongest_session_key, FALSE,
1777 server, b->etype.val, b->etype.len, &etype,
1778 NULL);
1779 if(ret) {
1780 kdc_log(context, config, 0,
1781 "Server (%s) has no support for etypes", spn);
1782 goto out;
1784 ret = _kdc_get_preferred_key(context, config, server, spn,
1785 NULL, &skey);
1786 if(ret) {
1787 kdc_log(context, config, 0,
1788 "Server (%s) has no supported etypes", spn);
1789 goto out;
1791 ekey = &skey->key;
1792 kvno = server->entry.kvno;
1795 ret = krb5_generate_random_keyblock(context, etype, &sessionkey);
1796 if (ret)
1797 goto out;
1801 * Check that service is in the same realm as the krbtgt. If it's
1802 * not the same, it's someone that is using a uni-directional trust
1803 * backward.
1807 * Validate authoriation data
1810 ret = hdb_enctype2key(context, &krbtgt->entry, NULL, /* XXX use the right kvno! */
1811 krbtgt_etype, &tkey_check);
1812 if(ret) {
1813 kdc_log(context, config, 0,
1814 "Failed to find key for krbtgt PAC check");
1815 goto out;
1819 * Now refetch the primary krbtgt, and get the current kvno (the
1820 * sign check may have been on an old kvno, and the server may
1821 * have been an incoming trust)
1825 const char *remote_realm =
1826 krb5_principal_get_comp_string(context, krbtgt->entry.principal, 1);
1828 ret = krb5_make_principal(context,
1829 &krbtgt_out_principal,
1830 remote_realm,
1831 KRB5_TGS_NAME,
1832 remote_realm,
1833 NULL);
1834 if(ret) {
1835 kdc_log(context, config, 0,
1836 "Failed to make krbtgt principal name object for "
1837 "authz-data signatures");
1838 goto out;
1840 ret = krb5_unparse_name(context, krbtgt_out_principal, &krbtgt_out_n);
1841 if (ret) {
1842 kdc_log(context, config, 0,
1843 "Failed to make krbtgt principal name object for "
1844 "authz-data signatures");
1845 goto out;
1849 ret = _kdc_db_fetch(context, config, krbtgt_out_principal,
1850 HDB_F_GET_KRBTGT, NULL, NULL, &krbtgt_out);
1851 if (ret) {
1852 char *ktpn = NULL;
1853 ret = krb5_unparse_name(context, krbtgt->entry.principal, &ktpn);
1854 kdc_log(context, config, 0,
1855 "No such principal %s (needed for authz-data signature keys) "
1856 "while processing TGS-REQ for service %s with krbtg %s",
1857 krbtgt_out_n, spn, (ret == 0) ? ktpn : "<unknown>");
1858 free(ktpn);
1859 ret = KRB5KRB_AP_ERR_NOT_US;
1860 goto out;
1864 * The first realm is the realm of the service, the second is
1865 * krbtgt/<this>/@REALM component of the krbtgt DN the request was
1866 * encrypted to. The redirection via the krbtgt_out entry allows
1867 * the DB to possibly correct the case of the realm (Samba4 does
1868 * this) before the strcmp()
1870 if (strcmp(krb5_principal_get_realm(context, server->entry.principal),
1871 krb5_principal_get_realm(context, krbtgt_out->entry.principal)) != 0) {
1872 char *ktpn;
1873 ret = krb5_unparse_name(context, krbtgt_out->entry.principal, &ktpn);
1874 kdc_log(context, config, 0,
1875 "Request with wrong krbtgt: %s",
1876 (ret == 0) ? ktpn : "<unknown>");
1877 if(ret == 0)
1878 free(ktpn);
1879 ret = KRB5KRB_AP_ERR_NOT_US;
1880 goto out;
1883 ret = _kdc_get_preferred_key(context, config, krbtgt_out, krbtgt_out_n,
1884 NULL, &tkey_sign);
1885 if (ret) {
1886 kdc_log(context, config, 0,
1887 "Failed to find key for krbtgt PAC signature");
1888 goto out;
1890 ret = hdb_enctype2key(context, &krbtgt_out->entry, NULL,
1891 tkey_sign->key.keytype, &tkey_sign);
1892 if(ret) {
1893 kdc_log(context, config, 0,
1894 "Failed to find key for krbtgt PAC signature");
1895 goto out;
1898 ret = _kdc_db_fetch(context, config, cp, HDB_F_GET_CLIENT | flags,
1899 NULL, &clientdb, &client);
1900 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1901 /* This is OK, we are just trying to find out if they have
1902 * been disabled or deleted in the meantime, missing secrets
1903 * is OK */
1904 } else if(ret){
1905 const char *krbtgt_realm, *msg;
1908 * If the client belongs to the same realm as our krbtgt, it
1909 * should exist in the local database.
1913 krbtgt_realm = krb5_principal_get_realm(context, krbtgt_out->entry.principal);
1915 if(strcmp(krb5_principal_get_realm(context, cp), krbtgt_realm) == 0) {
1916 if (ret == HDB_ERR_NOENTRY)
1917 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1918 kdc_log(context, config, 1, "Client no longer in database: %s",
1919 cpn);
1920 goto out;
1923 msg = krb5_get_error_message(context, ret);
1924 kdc_log(context, config, 1, "Client not found in database: %s", msg);
1925 krb5_free_error_message(context, msg);
1928 ret = check_PAC(context, config, cp, NULL,
1929 client, server, krbtgt,
1930 &tkey_check->key,
1931 ekey, &tkey_sign->key,
1932 tgt, &rspac, &signedpath);
1933 if (ret) {
1934 const char *msg = krb5_get_error_message(context, ret);
1935 kdc_log(context, config, 0,
1936 "Verify PAC failed for %s (%s) from %s with %s",
1937 spn, cpn, from, msg);
1938 krb5_free_error_message(context, msg);
1939 goto out;
1942 /* also check the krbtgt for signature */
1943 ret = check_KRB5SignedPath(context,
1944 config,
1945 krbtgt,
1947 tgt,
1948 &spp,
1949 &signedpath);
1950 if (ret) {
1951 const char *msg = krb5_get_error_message(context, ret);
1952 kdc_log(context, config, 0,
1953 "KRB5SignedPath check failed for %s (%s) from %s with %s",
1954 spn, cpn, from, msg);
1955 krb5_free_error_message(context, msg);
1956 goto out;
1960 * Process request
1963 /* by default the tgt principal matches the client principal */
1964 tp = cp;
1965 tpn = cpn;
1967 if (client) {
1968 const PA_DATA *sdata;
1969 int i = 0;
1971 sdata = _kdc_find_padata(req, &i, KRB5_PADATA_FOR_USER);
1972 if (sdata) {
1973 krb5_crypto crypto;
1974 krb5_data datack;
1975 PA_S4U2Self self;
1976 const char *str;
1978 ret = decode_PA_S4U2Self(sdata->padata_value.data,
1979 sdata->padata_value.length,
1980 &self, NULL);
1981 if (ret) {
1982 kdc_log(context, config, 0, "Failed to decode PA-S4U2Self");
1983 goto out;
1986 ret = _krb5_s4u2self_to_checksumdata(context, &self, &datack);
1987 if (ret)
1988 goto out;
1990 ret = krb5_crypto_init(context, &tgt->key, 0, &crypto);
1991 if (ret) {
1992 const char *msg = krb5_get_error_message(context, ret);
1993 free_PA_S4U2Self(&self);
1994 krb5_data_free(&datack);
1995 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
1996 krb5_free_error_message(context, msg);
1997 goto out;
2000 ret = krb5_verify_checksum(context,
2001 crypto,
2002 KRB5_KU_OTHER_CKSUM,
2003 datack.data,
2004 datack.length,
2005 &self.cksum);
2006 krb5_data_free(&datack);
2007 krb5_crypto_destroy(context, crypto);
2008 if (ret) {
2009 const char *msg = krb5_get_error_message(context, ret);
2010 free_PA_S4U2Self(&self);
2011 kdc_log(context, config, 0,
2012 "krb5_verify_checksum failed for S4U2Self: %s", msg);
2013 krb5_free_error_message(context, msg);
2014 goto out;
2017 ret = _krb5_principalname2krb5_principal(context,
2018 &tp,
2019 self.name,
2020 self.realm);
2021 free_PA_S4U2Self(&self);
2022 if (ret)
2023 goto out;
2025 ret = krb5_unparse_name(context, tp, &tpn);
2026 if (ret)
2027 goto out;
2029 /* If we were about to put a PAC into the ticket, we better fix it to be the right PAC */
2030 if(rspac.data) {
2031 krb5_pac p = NULL;
2032 krb5_data_free(&rspac);
2033 ret = _kdc_db_fetch(context, config, tp, HDB_F_GET_CLIENT | flags,
2034 NULL, &s4u2self_impersonated_clientdb, &s4u2self_impersonated_client);
2035 if (ret) {
2036 const char *msg;
2039 * If the client belongs to the same realm as our krbtgt, it
2040 * should exist in the local database.
2044 if (ret == HDB_ERR_NOENTRY)
2045 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
2046 msg = krb5_get_error_message(context, ret);
2047 kdc_log(context, config, 1,
2048 "S2U4Self principal to impersonate %s not found in database: %s",
2049 tpn, msg);
2050 krb5_free_error_message(context, msg);
2051 goto out;
2053 ret = _kdc_pac_generate(context, s4u2self_impersonated_client, &p);
2054 if (ret) {
2055 kdc_log(context, config, 0, "PAC generation failed for -- %s",
2056 tpn);
2057 goto out;
2059 if (p != NULL) {
2060 ret = _krb5_pac_sign(context, p, ticket->ticket.authtime,
2061 s4u2self_impersonated_client->entry.principal,
2062 ekey, &tkey_sign->key,
2063 &rspac);
2064 krb5_pac_free(context, p);
2065 if (ret) {
2066 kdc_log(context, config, 0, "PAC signing failed for -- %s",
2067 tpn);
2068 goto out;
2074 * Check that service doing the impersonating is
2075 * requesting a ticket to it-self.
2077 ret = check_s4u2self(context, config, clientdb, client, sp);
2078 if (ret) {
2079 kdc_log(context, config, 0, "S4U2Self: %s is not allowed "
2080 "to impersonate to service "
2081 "(tried for user %s to service %s)",
2082 cpn, tpn, spn);
2083 goto out;
2087 * If the service isn't trusted for authentication to
2088 * delegation, remove the forward flag.
2091 if (client->entry.flags.trusted_for_delegation) {
2092 str = "[forwardable]";
2093 } else {
2094 b->kdc_options.forwardable = 0;
2095 str = "";
2097 kdc_log(context, config, 0, "s4u2self %s impersonating %s to "
2098 "service %s %s", cpn, tpn, spn, str);
2103 * Constrained delegation
2106 if (client != NULL
2107 && b->additional_tickets != NULL
2108 && b->additional_tickets->len != 0
2109 && b->kdc_options.enc_tkt_in_skey == 0)
2111 int ad_signedpath = 0;
2112 Key *clientkey;
2113 Ticket *t;
2116 * Require that the KDC have issued the service's krbtgt (not
2117 * self-issued ticket with kimpersonate(1).
2119 if (!signedpath) {
2120 ret = KRB5KDC_ERR_BADOPTION;
2121 kdc_log(context, config, 0,
2122 "Constrained delegation done on service ticket %s/%s",
2123 cpn, spn);
2124 goto out;
2127 t = &b->additional_tickets->val[0];
2129 ret = hdb_enctype2key(context, &client->entry,
2130 hdb_kvno2keys(context, &client->entry,
2131 t->enc_part.kvno ? * t->enc_part.kvno : 0),
2132 t->enc_part.etype, &clientkey);
2133 if(ret){
2134 ret = KRB5KDC_ERR_ETYPE_NOSUPP; /* XXX */
2135 goto out;
2138 ret = krb5_decrypt_ticket(context, t, &clientkey->key, &adtkt, 0);
2139 if (ret) {
2140 kdc_log(context, config, 0,
2141 "failed to decrypt ticket for "
2142 "constrained delegation from %s to %s ", cpn, spn);
2143 goto out;
2146 ret = _krb5_principalname2krb5_principal(context,
2147 &tp,
2148 adtkt.cname,
2149 adtkt.crealm);
2150 if (ret)
2151 goto out;
2153 ret = krb5_unparse_name(context, tp, &tpn);
2154 if (ret)
2155 goto out;
2157 ret = _krb5_principalname2krb5_principal(context,
2158 &dp,
2159 t->sname,
2160 t->realm);
2161 if (ret)
2162 goto out;
2164 ret = krb5_unparse_name(context, dp, &dpn);
2165 if (ret)
2166 goto out;
2168 /* check that ticket is valid */
2169 if (adtkt.flags.forwardable == 0) {
2170 kdc_log(context, config, 0,
2171 "Missing forwardable flag on ticket for "
2172 "constrained delegation from %s (%s) as %s to %s ",
2173 cpn, dpn, tpn, spn);
2174 ret = KRB5KDC_ERR_BADOPTION;
2175 goto out;
2178 ret = check_constrained_delegation(context, config, clientdb,
2179 client, server, sp);
2180 if (ret) {
2181 kdc_log(context, config, 0,
2182 "constrained delegation from %s (%s) as %s to %s not allowed",
2183 cpn, dpn, tpn, spn);
2184 goto out;
2187 ret = verify_flags(context, config, &adtkt, tpn);
2188 if (ret) {
2189 goto out;
2192 krb5_data_free(&rspac);
2195 * generate the PAC for the user.
2197 * TODO: pass in t->sname and t->realm and build
2198 * a S4U_DELEGATION_INFO blob to the PAC.
2200 ret = check_PAC(context, config, tp, dp,
2201 client, server, krbtgt,
2202 &clientkey->key,
2203 ekey, &tkey_sign->key,
2204 &adtkt, &rspac, &ad_signedpath);
2205 if (ret) {
2206 const char *msg = krb5_get_error_message(context, ret);
2207 kdc_log(context, config, 0,
2208 "Verify delegated PAC failed to %s for client"
2209 "%s (%s) as %s from %s with %s",
2210 spn, cpn, dpn, tpn, from, msg);
2211 krb5_free_error_message(context, msg);
2212 goto out;
2216 * Check that the KDC issued the user's ticket.
2218 ret = check_KRB5SignedPath(context,
2219 config,
2220 krbtgt,
2222 &adtkt,
2223 NULL,
2224 &ad_signedpath);
2225 if (ret) {
2226 const char *msg = krb5_get_error_message(context, ret);
2227 kdc_log(context, config, 0,
2228 "KRB5SignedPath check from service %s failed "
2229 "for delegation to %s for client %s (%s)"
2230 "from %s failed with %s",
2231 spn, tpn, dpn, cpn, from, msg);
2232 krb5_free_error_message(context, msg);
2233 goto out;
2236 if (!ad_signedpath) {
2237 ret = KRB5KDC_ERR_BADOPTION;
2238 kdc_log(context, config, 0,
2239 "Ticket not signed with PAC nor SignedPath service %s failed "
2240 "for delegation to %s for client %s (%s)"
2241 "from %s",
2242 spn, tpn, dpn, cpn, from);
2243 goto out;
2246 kdc_log(context, config, 0, "constrained delegation for %s "
2247 "from %s (%s) to %s", tpn, cpn, dpn, spn);
2251 * Check flags
2254 ret = kdc_check_flags(context, config,
2255 client, cpn,
2256 server, spn,
2257 FALSE);
2258 if(ret)
2259 goto out;
2261 if((b->kdc_options.validate || b->kdc_options.renew) &&
2262 !krb5_principal_compare(context,
2263 krbtgt->entry.principal,
2264 server->entry.principal)){
2265 kdc_log(context, config, 0, "Inconsistent request.");
2266 ret = KRB5KDC_ERR_SERVER_NOMATCH;
2267 goto out;
2270 /* check for valid set of addresses */
2271 if(!_kdc_check_addresses(context, config, tgt->caddr, from_addr)) {
2272 ret = KRB5KRB_AP_ERR_BADADDR;
2273 kdc_log(context, config, 0, "Request from wrong address");
2274 goto out;
2278 * If this is an referral, add server referral data to the
2279 * auth_data reply .
2281 if (ref_realm) {
2282 PA_DATA pa;
2283 krb5_crypto crypto;
2285 kdc_log(context, config, 0,
2286 "Adding server referral to %s", ref_realm);
2288 ret = krb5_crypto_init(context, &sessionkey, 0, &crypto);
2289 if (ret)
2290 goto out;
2292 ret = build_server_referral(context, config, crypto, ref_realm,
2293 NULL, s, &pa.padata_value);
2294 krb5_crypto_destroy(context, crypto);
2295 if (ret) {
2296 kdc_log(context, config, 0,
2297 "Failed building server referral");
2298 goto out;
2300 pa.padata_type = KRB5_PADATA_SERVER_REFERRAL;
2302 ret = add_METHOD_DATA(&enc_pa_data, &pa);
2303 krb5_data_free(&pa.padata_value);
2304 if (ret) {
2305 kdc_log(context, config, 0,
2306 "Add server referral METHOD-DATA failed");
2307 goto out;
2315 ret = tgs_make_reply(context,
2316 config,
2319 tgt,
2320 replykey,
2321 rk_is_subkey,
2322 ekey,
2323 &sessionkey,
2324 kvno,
2325 *auth_data,
2326 server,
2327 rsp,
2328 spn,
2329 client,
2331 krbtgt_out,
2332 tkey_sign->key.keytype,
2333 spp,
2334 &rspac,
2335 &enc_pa_data,
2336 e_text,
2337 reply);
2339 out:
2340 if (tpn != cpn)
2341 free(tpn);
2342 free(spn);
2343 free(cpn);
2344 free(dpn);
2345 free(krbtgt_out_n);
2347 krb5_data_free(&rspac);
2348 krb5_free_keyblock_contents(context, &sessionkey);
2349 if(krbtgt_out)
2350 _kdc_free_ent(context, krbtgt_out);
2351 if(server)
2352 _kdc_free_ent(context, server);
2353 if(client)
2354 _kdc_free_ent(context, client);
2355 if(s4u2self_impersonated_client)
2356 _kdc_free_ent(context, s4u2self_impersonated_client);
2358 if (tp && tp != cp)
2359 krb5_free_principal(context, tp);
2360 krb5_free_principal(context, cp);
2361 krb5_free_principal(context, dp);
2362 krb5_free_principal(context, sp);
2363 krb5_free_principal(context, krbtgt_out_principal);
2364 if (ref_realm)
2365 free(ref_realm);
2366 free_METHOD_DATA(&enc_pa_data);
2368 free_EncTicketPart(&adtkt);
2370 return ret;
2377 krb5_error_code
2378 _kdc_tgs_rep(krb5_context context,
2379 krb5_kdc_configuration *config,
2380 KDC_REQ *req,
2381 krb5_data *data,
2382 const char *from,
2383 struct sockaddr *from_addr,
2384 int datagram_reply)
2386 AuthorizationData *auth_data = NULL;
2387 krb5_error_code ret;
2388 int i = 0;
2389 const PA_DATA *tgs_req;
2391 hdb_entry_ex *krbtgt = NULL;
2392 krb5_ticket *ticket = NULL;
2393 const char *e_text = NULL;
2394 krb5_enctype krbtgt_etype = ETYPE_NULL;
2396 krb5_keyblock *replykey = NULL;
2397 int rk_is_subkey = 0;
2398 time_t *csec = NULL;
2399 int *cusec = NULL;
2401 if(req->padata == NULL){
2402 ret = KRB5KDC_ERR_PREAUTH_REQUIRED; /* XXX ??? */
2403 kdc_log(context, config, 0,
2404 "TGS-REQ from %s without PA-DATA", from);
2405 goto out;
2408 tgs_req = _kdc_find_padata(req, &i, KRB5_PADATA_TGS_REQ);
2410 if(tgs_req == NULL){
2411 ret = KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
2413 kdc_log(context, config, 0,
2414 "TGS-REQ from %s without PA-TGS-REQ", from);
2415 goto out;
2417 ret = tgs_parse_request(context, config,
2418 &req->req_body, tgs_req,
2419 &krbtgt,
2420 &krbtgt_etype,
2421 &ticket,
2422 &e_text,
2423 from, from_addr,
2424 &csec, &cusec,
2425 &auth_data,
2426 &replykey,
2427 &rk_is_subkey);
2428 if (ret == HDB_ERR_NOT_FOUND_HERE) {
2429 /* kdc_log() is called in tgs_parse_request() */
2430 goto out;
2432 if (ret) {
2433 kdc_log(context, config, 0,
2434 "Failed parsing TGS-REQ from %s", from);
2435 goto out;
2439 const PA_DATA *pa = _kdc_find_padata(req, &i, KRB5_PADATA_FX_FAST);
2440 if (pa)
2441 kdc_log(context, config, 10, "Got TGS FAST request");
2445 ret = tgs_build_reply(context,
2446 config,
2447 req,
2448 &req->req_body,
2449 krbtgt,
2450 krbtgt_etype,
2451 replykey,
2452 rk_is_subkey,
2453 ticket,
2454 data,
2455 from,
2456 &e_text,
2457 &auth_data,
2458 from_addr);
2459 if (ret) {
2460 kdc_log(context, config, 0,
2461 "Failed building TGS-REP to %s", from);
2462 goto out;
2465 /* */
2466 if (datagram_reply && data->length > config->max_datagram_reply_length) {
2467 krb5_data_free(data);
2468 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
2469 e_text = "Reply packet too large";
2472 out:
2473 if (replykey)
2474 krb5_free_keyblock(context, replykey);
2476 if(ret && ret != HDB_ERR_NOT_FOUND_HERE && data->data == NULL){
2477 /* XXX add fast wrapping on the error */
2478 METHOD_DATA error_method = { 0, NULL };
2481 kdc_log(context, config, 10, "tgs-req: sending error: %d to client", ret);
2482 ret = _kdc_fast_mk_error(context, NULL,
2483 &error_method,
2484 NULL,
2485 NULL,
2486 ret, NULL,
2487 NULL,
2488 NULL, NULL,
2489 csec, cusec,
2490 data);
2491 free_METHOD_DATA(&error_method);
2493 free(csec);
2494 free(cusec);
2495 if (ticket)
2496 krb5_free_ticket(context, ticket);
2497 if(krbtgt)
2498 _kdc_free_ent(context, krbtgt);
2500 if (auth_data) {
2501 free_AuthorizationData(auth_data);
2502 free(auth_data);
2505 return ret;