2 * Copyright (c) 1997-2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
37 * return the realm of a krbtgt-ticket or NULL
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];
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
,
64 AuthorizationData child
;
68 if (ad
== NULL
|| ad
->len
== 0)
69 return KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
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
,
81 krb5_set_error_message(context
, ret
, "Failed to decode "
82 "IF_RELEVANT with %d", ret
);
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
;
97 ret
= der_copy_octet_string(&child
.val
[0].ad_data
, data
);
98 free_AuthorizationData(&child
);
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
,
115 krb5_crypto crypto
= NULL
;
118 if (server
&& principals
) {
119 ret
= add_Principals(principals
, server
);
125 KRB5SignedPathData spd
;
128 spd
.authtime
= tkt
->authtime
;
129 spd
.delegated
= principals
;
130 spd
.method_data
= NULL
;
132 ASN1_MALLOC_ENCODE(KRB5SignedPathData
, data
.data
, data
.length
,
136 if (data
.length
!= size
)
137 krb5_abortx(context
, "internal asn.1 encoder error");
142 ret
= hdb_enctype2key(context
, &krbtgt
->entry
, enctype
, &key
);
144 ret
= krb5_crypto_init(context
, &key
->key
, 0, &crypto
);
152 * Fill in KRB5SignedPath
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
);
166 ASN1_MALLOC_ENCODE(KRB5SignedPath
, data
.data
, data
.length
, &sp
, &size
, ret
);
167 free_Checksum(&sp
.cksum
);
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
);
186 static krb5_error_code
187 check_KRB5SignedPath(krb5_context context
,
188 krb5_kdc_configuration
*config
,
189 hdb_entry_ex
*krbtgt
,
192 krb5_principals
*delegated
,
197 krb5_crypto crypto
= NULL
;
202 ret
= find_KRB5SignedPath(context
, tkt
->authorization_data
, &data
);
204 KRB5SignedPathData spd
;
208 ret
= decode_KRB5SignedPath(data
.data
, data
.length
, &sp
, NULL
);
209 krb5_data_free(&data
);
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
,
221 free_KRB5SignedPath(&sp
);
224 if (data
.length
!= size
)
225 krb5_abortx(context
, "internal asn.1 encoder error");
229 ret
= hdb_enctype2key(context
, &krbtgt
->entry
, sp
.etype
, &key
);
231 ret
= krb5_crypto_init(context
, &key
->key
, 0, &crypto
);
234 free_KRB5SignedPath(&sp
);
238 ret
= krb5_verify_checksum(context
, crypto
, KRB5_KU_KRB5SIGNEDPATH
,
239 data
.data
, data
.length
,
241 krb5_crypto_destroy(context
, crypto
);
244 free_KRB5SignedPath(&sp
);
245 kdc_log(context
, config
, 5,
246 "KRB5SignedPath not signed correctly, not marking as signed");
250 if (delegated
&& sp
.delegated
) {
252 *delegated
= malloc(sizeof(*sp
.delegated
));
253 if (*delegated
== NULL
) {
254 free_KRB5SignedPath(&sp
);
258 ret
= copy_Principals(*delegated
, sp
.delegated
);
260 free_KRB5SignedPath(&sp
);
266 free_KRB5SignedPath(&sp
);
278 static krb5_error_code
279 check_PAC(krb5_context context
,
280 krb5_kdc_configuration
*config
,
281 const krb5_principal client_principal
,
282 hdb_entry_ex
*client
,
283 hdb_entry_ex
*server
,
284 hdb_entry_ex
*krbtgt
,
285 const EncryptionKey
*server_key
,
286 const EncryptionKey
*krbtgt_check_key
,
287 const EncryptionKey
*krbtgt_sign_key
,
292 AuthorizationData
*ad
= tkt
->authorization_data
;
296 if (ad
== NULL
|| ad
->len
== 0)
299 for (i
= 0; i
< ad
->len
; i
++) {
300 AuthorizationData child
;
302 if (ad
->val
[i
].ad_type
!= KRB5_AUTHDATA_IF_RELEVANT
)
305 ret
= decode_AuthorizationData(ad
->val
[i
].ad_data
.data
,
306 ad
->val
[i
].ad_data
.length
,
310 krb5_set_error_message(context
, ret
, "Failed to decode "
311 "IF_RELEVANT with %d", ret
);
314 for (j
= 0; j
< child
.len
; j
++) {
316 if (child
.val
[j
].ad_type
== KRB5_AUTHDATA_WIN2K_PAC
) {
321 ret
= krb5_pac_parse(context
,
322 child
.val
[j
].ad_data
.data
,
323 child
.val
[j
].ad_data
.length
,
325 free_AuthorizationData(&child
);
329 ret
= krb5_pac_verify(context
, pac
, tkt
->authtime
,
331 krbtgt_check_key
, NULL
);
333 krb5_pac_free(context
, pac
);
337 ret
= _kdc_pac_verify(context
, client_principal
,
338 client
, server
, krbtgt
, &pac
, &signed_pac
);
340 krb5_pac_free(context
, pac
);
345 * Only re-sign PAC if we could verify it with the PAC
346 * function. The no-verify case happens when we get in
347 * a PAC from cross realm from a Windows domain and
348 * that there is no PAC verification function.
352 ret
= _krb5_pac_sign(context
, pac
, tkt
->authtime
,
354 server_key
, krbtgt_sign_key
, rspac
);
356 krb5_pac_free(context
, pac
);
361 free_AuthorizationData(&child
);
370 static krb5_error_code
371 check_tgs_flags(krb5_context context
,
372 krb5_kdc_configuration
*config
,
373 KDC_REQ_BODY
*b
, const EncTicketPart
*tgt
, EncTicketPart
*et
)
375 KDCOptions f
= b
->kdc_options
;
378 if(!tgt
->flags
.invalid
|| tgt
->starttime
== NULL
){
379 kdc_log(context
, config
, 0,
380 "Bad request to validate ticket");
381 return KRB5KDC_ERR_BADOPTION
;
383 if(*tgt
->starttime
> kdc_time
){
384 kdc_log(context
, config
, 0,
385 "Early request to validate ticket");
386 return KRB5KRB_AP_ERR_TKT_NYV
;
389 et
->flags
.invalid
= 0;
390 }else if(tgt
->flags
.invalid
){
391 kdc_log(context
, config
, 0,
392 "Ticket-granting ticket has INVALID flag set");
393 return KRB5KRB_AP_ERR_TKT_INVALID
;
397 if(!tgt
->flags
.forwardable
){
398 kdc_log(context
, config
, 0,
399 "Bad request for forwardable ticket");
400 return KRB5KDC_ERR_BADOPTION
;
402 et
->flags
.forwardable
= 1;
405 if(!tgt
->flags
.forwardable
){
406 kdc_log(context
, config
, 0,
407 "Request to forward non-forwardable ticket");
408 return KRB5KDC_ERR_BADOPTION
;
410 et
->flags
.forwarded
= 1;
411 et
->caddr
= b
->addresses
;
413 if(tgt
->flags
.forwarded
)
414 et
->flags
.forwarded
= 1;
417 if(!tgt
->flags
.proxiable
){
418 kdc_log(context
, config
, 0,
419 "Bad request for proxiable ticket");
420 return KRB5KDC_ERR_BADOPTION
;
422 et
->flags
.proxiable
= 1;
425 if(!tgt
->flags
.proxiable
){
426 kdc_log(context
, config
, 0,
427 "Request to proxy non-proxiable ticket");
428 return KRB5KDC_ERR_BADOPTION
;
431 et
->caddr
= b
->addresses
;
436 if(f
.allow_postdate
){
437 if(!tgt
->flags
.may_postdate
){
438 kdc_log(context
, config
, 0,
439 "Bad request for post-datable ticket");
440 return KRB5KDC_ERR_BADOPTION
;
442 et
->flags
.may_postdate
= 1;
445 if(!tgt
->flags
.may_postdate
){
446 kdc_log(context
, config
, 0,
447 "Bad request for postdated ticket");
448 return KRB5KDC_ERR_BADOPTION
;
451 *et
->starttime
= *b
->from
;
452 et
->flags
.postdated
= 1;
453 et
->flags
.invalid
= 1;
454 }else if(b
->from
&& *b
->from
> kdc_time
+ context
->max_skew
){
455 kdc_log(context
, config
, 0, "Ticket cannot be postdated");
456 return KRB5KDC_ERR_CANNOT_POSTDATE
;
460 if(!tgt
->flags
.renewable
|| tgt
->renew_till
== NULL
){
461 kdc_log(context
, config
, 0,
462 "Bad request for renewable ticket");
463 return KRB5KDC_ERR_BADOPTION
;
465 et
->flags
.renewable
= 1;
466 ALLOC(et
->renew_till
);
467 _kdc_fix_time(&b
->rtime
);
468 *et
->renew_till
= *b
->rtime
;
472 if(!tgt
->flags
.renewable
|| tgt
->renew_till
== NULL
){
473 kdc_log(context
, config
, 0,
474 "Request to renew non-renewable ticket");
475 return KRB5KDC_ERR_BADOPTION
;
477 old_life
= tgt
->endtime
;
479 old_life
-= *tgt
->starttime
;
481 old_life
-= tgt
->authtime
;
482 et
->endtime
= *et
->starttime
+ old_life
;
483 if (et
->renew_till
!= NULL
)
484 et
->endtime
= min(*et
->renew_till
, et
->endtime
);
488 /* checks for excess flags */
489 if(f
.request_anonymous
&& !config
->allow_anonymous
){
490 kdc_log(context
, config
, 0,
491 "Request for anonymous ticket");
492 return KRB5KDC_ERR_BADOPTION
;
499 * Determine if constrained delegation is allowed from this client to this server
502 static krb5_error_code
503 check_constrained_delegation(krb5_context context
,
504 krb5_kdc_configuration
*config
,
506 hdb_entry_ex
*client
,
507 krb5_const_principal server
)
509 const HDB_Ext_Constrained_delegation_acl
*acl
;
513 /* if client delegates to itself, that ok */
514 if (krb5_principal_compare(context
, client
->entry
.principal
, server
) == TRUE
)
517 if (clientdb
->hdb_check_constrained_delegation
) {
518 ret
= clientdb
->hdb_check_constrained_delegation(context
, clientdb
, client
, server
);
522 ret
= hdb_entry_get_ConstrainedDelegACL(&client
->entry
, &acl
);
524 krb5_clear_error_message(context
);
529 for (i
= 0; i
< acl
->len
; i
++) {
530 if (krb5_principal_compare(context
, server
, &acl
->val
[i
]) == TRUE
)
534 ret
= KRB5KDC_ERR_BADOPTION
;
536 kdc_log(context
, config
, 0,
537 "Bad request for constrained delegation");
542 * Determine if s4u2self is allowed from this client to this server
544 * For example, regardless of the principal being impersonated, if the
545 * 'client' and 'server' are the same, then it's safe.
548 static krb5_error_code
549 check_s4u2self(krb5_context context
,
550 krb5_kdc_configuration
*config
,
552 hdb_entry_ex
*client
,
553 krb5_const_principal server
)
557 /* if client does a s4u2self to itself, that ok */
558 if (krb5_principal_compare(context
, client
->entry
.principal
, server
) == TRUE
)
561 if (clientdb
->hdb_check_s4u2self
) {
562 ret
= clientdb
->hdb_check_s4u2self(context
, clientdb
, client
, server
);
566 ret
= KRB5KDC_ERR_BADOPTION
;
575 static krb5_error_code
576 verify_flags (krb5_context context
,
577 krb5_kdc_configuration
*config
,
578 const EncTicketPart
*et
,
581 if(et
->endtime
< kdc_time
){
582 kdc_log(context
, config
, 0, "Ticket expired (%s)", pstr
);
583 return KRB5KRB_AP_ERR_TKT_EXPIRED
;
585 if(et
->flags
.invalid
){
586 kdc_log(context
, config
, 0, "Ticket not valid (%s)", pstr
);
587 return KRB5KRB_AP_ERR_TKT_NYV
;
596 static krb5_error_code
597 fix_transited_encoding(krb5_context context
,
598 krb5_kdc_configuration
*config
,
599 krb5_boolean check_policy
,
600 const TransitedEncoding
*tr
,
602 const char *client_realm
,
603 const char *server_realm
,
604 const char *tgt_realm
)
606 krb5_error_code ret
= 0;
607 char **realms
, **tmp
;
608 unsigned int num_realms
;
611 switch (tr
->tr_type
) {
612 case DOMAIN_X500_COMPRESS
:
616 * Allow empty content of type 0 because that is was Microsoft
617 * generates in their TGT.
619 if (tr
->contents
.length
== 0)
621 kdc_log(context
, config
, 0,
622 "Transited type 0 with non empty content");
623 return KRB5KDC_ERR_TRTYPE_NOSUPP
;
625 kdc_log(context
, config
, 0,
626 "Unknown transited type: %u", tr
->tr_type
);
627 return KRB5KDC_ERR_TRTYPE_NOSUPP
;
630 ret
= krb5_domain_x500_decode(context
,
637 krb5_warn(context
, ret
,
638 "Decoding transited encoding");
641 if(strcmp(client_realm
, tgt_realm
) && strcmp(server_realm
, tgt_realm
)) {
642 /* not us, so add the previous realm to transited set */
643 if (num_realms
+ 1 > UINT_MAX
/sizeof(*realms
)) {
647 tmp
= realloc(realms
, (num_realms
+ 1) * sizeof(*realms
));
653 realms
[num_realms
] = strdup(tgt_realm
);
654 if(realms
[num_realms
] == NULL
){
660 if(num_realms
== 0) {
661 if(strcmp(client_realm
, server_realm
))
662 kdc_log(context
, config
, 0,
663 "cross-realm %s -> %s", client_realm
, server_realm
);
667 for(i
= 0; i
< num_realms
; i
++)
668 l
+= strlen(realms
[i
]) + 2;
672 for(i
= 0; i
< num_realms
; i
++) {
674 strlcat(rs
, ", ", l
);
675 strlcat(rs
, realms
[i
], l
);
677 kdc_log(context
, config
, 0,
678 "cross-realm %s -> %s via [%s]",
679 client_realm
, server_realm
, rs
);
684 ret
= krb5_check_transited(context
, client_realm
,
686 realms
, num_realms
, NULL
);
688 krb5_warn(context
, ret
, "cross-realm %s -> %s",
689 client_realm
, server_realm
);
692 et
->flags
.transited_policy_checked
= 1;
694 et
->transited
.tr_type
= DOMAIN_X500_COMPRESS
;
695 ret
= krb5_domain_x500_encode(realms
, num_realms
, &et
->transited
.contents
);
697 krb5_warn(context
, ret
, "Encoding transited encoding");
699 for(i
= 0; i
< num_realms
; i
++)
706 static krb5_error_code
707 tgs_make_reply(krb5_context context
,
708 krb5_kdc_configuration
*config
,
710 krb5_const_principal tgt_name
,
711 const EncTicketPart
*tgt
,
712 const krb5_keyblock
*replykey
,
714 const EncryptionKey
*serverkey
,
715 const krb5_keyblock
*sessionkey
,
717 AuthorizationData
*auth_data
,
718 hdb_entry_ex
*server
,
719 krb5_principal server_principal
,
720 const char *server_name
,
721 hdb_entry_ex
*client
,
722 krb5_principal client_principal
,
723 hdb_entry_ex
*krbtgt
,
724 krb5_enctype krbtgt_etype
,
726 const krb5_data
*rspac
,
727 const METHOD_DATA
*enc_pa_data
,
734 KDCOptions f
= b
->kdc_options
;
738 memset(&rep
, 0, sizeof(rep
));
739 memset(&et
, 0, sizeof(et
));
740 memset(&ek
, 0, sizeof(ek
));
743 rep
.msg_type
= krb_tgs_rep
;
745 et
.authtime
= tgt
->authtime
;
746 _kdc_fix_time(&b
->till
);
747 et
.endtime
= min(tgt
->endtime
, *b
->till
);
749 *et
.starttime
= kdc_time
;
751 ret
= check_tgs_flags(context
, config
, b
, tgt
, &et
);
755 /* We should check the transited encoding if:
756 1) the request doesn't ask not to be checked
757 2) globally enforcing a check
758 3) principal requires checking
759 4) we allow non-check per-principal, but principal isn't marked as allowing this
760 5) we don't globally allow this
763 #define GLOBAL_FORCE_TRANSITED_CHECK \
764 (config->trpolicy == TRPOLICY_ALWAYS_CHECK)
765 #define GLOBAL_ALLOW_PER_PRINCIPAL \
766 (config->trpolicy == TRPOLICY_ALLOW_PER_PRINCIPAL)
767 #define GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK \
768 (config->trpolicy == TRPOLICY_ALWAYS_HONOUR_REQUEST)
770 /* these will consult the database in future release */
771 #define PRINCIPAL_FORCE_TRANSITED_CHECK(P) 0
772 #define PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(P) 0
774 ret
= fix_transited_encoding(context
, config
,
775 !f
.disable_transited_check
||
776 GLOBAL_FORCE_TRANSITED_CHECK
||
777 PRINCIPAL_FORCE_TRANSITED_CHECK(server
) ||
778 !((GLOBAL_ALLOW_PER_PRINCIPAL
&&
779 PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(server
)) ||
780 GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK
),
781 &tgt
->transited
, &et
,
782 krb5_principal_get_realm(context
, client_principal
),
783 krb5_principal_get_realm(context
, server
->entry
.principal
),
784 krb5_principal_get_realm(context
, krbtgt
->entry
.principal
));
788 copy_Realm(&server_principal
->realm
, &rep
.ticket
.realm
);
789 _krb5_principal2principalname(&rep
.ticket
.sname
, server_principal
);
790 copy_Realm(&tgt_name
->realm
, &rep
.crealm
);
792 if (f.request_anonymous)
793 _kdc_make_anonymous_principalname (&rep.cname);
796 copy_PrincipalName(&tgt_name
->name
, &rep
.cname
);
797 rep
.ticket
.tkt_vno
= 5;
801 et
.caddr
= tgt
->caddr
;
805 life
= et
.endtime
- *et
.starttime
;
806 if(client
&& client
->entry
.max_life
)
807 life
= min(life
, *client
->entry
.max_life
);
808 if(server
->entry
.max_life
)
809 life
= min(life
, *server
->entry
.max_life
);
810 et
.endtime
= *et
.starttime
+ life
;
812 if(f
.renewable_ok
&& tgt
->flags
.renewable
&&
813 et
.renew_till
== NULL
&& et
.endtime
< *b
->till
&&
814 tgt
->renew_till
!= NULL
)
816 et
.flags
.renewable
= 1;
817 ALLOC(et
.renew_till
);
818 *et
.renew_till
= *b
->till
;
822 renew
= *et
.renew_till
- et
.authtime
;
823 if(client
&& client
->entry
.max_renew
)
824 renew
= min(renew
, *client
->entry
.max_renew
);
825 if(server
->entry
.max_renew
)
826 renew
= min(renew
, *server
->entry
.max_renew
);
827 *et
.renew_till
= et
.authtime
+ renew
;
831 *et
.renew_till
= min(*et
.renew_till
, *tgt
->renew_till
);
832 *et
.starttime
= min(*et
.starttime
, *et
.renew_till
);
833 et
.endtime
= min(et
.endtime
, *et
.renew_till
);
836 *et
.starttime
= min(*et
.starttime
, et
.endtime
);
838 if(*et
.starttime
== et
.endtime
){
839 ret
= KRB5KDC_ERR_NEVER_VALID
;
842 if(et
.renew_till
&& et
.endtime
== *et
.renew_till
){
844 et
.renew_till
= NULL
;
845 et
.flags
.renewable
= 0;
848 et
.flags
.pre_authent
= tgt
->flags
.pre_authent
;
849 et
.flags
.hw_authent
= tgt
->flags
.hw_authent
;
850 et
.flags
.anonymous
= tgt
->flags
.anonymous
;
851 et
.flags
.ok_as_delegate
= server
->entry
.flags
.ok_as_delegate
;
855 * No not need to filter out the any PAC from the
856 * auth_data since it's signed by the KDC.
858 ret
= _kdc_tkt_add_if_relevant_ad(context
, &et
,
859 KRB5_AUTHDATA_WIN2K_PAC
, rspac
);
867 /* XXX check authdata */
869 if (et
.authorization_data
== NULL
) {
870 et
.authorization_data
= calloc(1, sizeof(*et
.authorization_data
));
871 if (et
.authorization_data
== NULL
) {
873 krb5_set_error_message(context
, ret
, "malloc: out of memory");
877 for(i
= 0; i
< auth_data
->len
; i
++) {
878 ret
= add_AuthorizationData(et
.authorization_data
, &auth_data
->val
[i
]);
880 krb5_set_error_message(context
, ret
, "malloc: out of memory");
885 /* Filter out type KRB5SignedPath */
886 ret
= find_KRB5SignedPath(context
, et
.authorization_data
, NULL
);
888 if (et
.authorization_data
->len
== 1) {
889 free_AuthorizationData(et
.authorization_data
);
890 free(et
.authorization_data
);
891 et
.authorization_data
= NULL
;
893 AuthorizationData
*ad
= et
.authorization_data
;
894 free_AuthorizationDataElement(&ad
->val
[ad
->len
- 1]);
900 ret
= krb5_copy_keyblock_contents(context
, sessionkey
, &et
.key
);
903 et
.crealm
= tgt
->crealm
;
904 et
.cname
= tgt_name
->name
;
907 /* MIT must have at least one last_req */
909 ek
.last_req
.val
= calloc(1, sizeof(*ek
.last_req
.val
));
910 if (ek
.last_req
.val
== NULL
) {
916 ek
.authtime
= et
.authtime
;
917 ek
.starttime
= et
.starttime
;
918 ek
.endtime
= et
.endtime
;
919 ek
.renew_till
= et
.renew_till
;
920 ek
.srealm
= rep
.ticket
.realm
;
921 ek
.sname
= rep
.ticket
.sname
;
923 _kdc_log_timestamp(context
, config
, "TGS-REQ", et
.authtime
, et
.starttime
,
924 et
.endtime
, et
.renew_till
);
926 /* Don't sign cross realm tickets, they can't be checked anyway */
928 char *r
= get_krbtgt_realm(&ek
.sname
);
930 if (r
== NULL
|| strcmp(r
, ek
.srealm
) == 0) {
931 ret
= _kdc_add_KRB5SignedPath(context
,
944 if (enc_pa_data
->len
) {
945 rep
.padata
= calloc(1, sizeof(*rep
.padata
));
946 if (rep
.padata
== NULL
) {
950 ret
= copy_METHOD_DATA(enc_pa_data
, rep
.padata
);
955 if (krb5_enctype_valid(context
, et
.key
.keytype
) != 0
956 && _kdc_is_weak_exception(server
->entry
.principal
, et
.key
.keytype
))
958 krb5_enctype_enable(context
, et
.key
.keytype
);
963 /* It is somewhat unclear where the etype in the following
964 encryption should come from. What we have is a session
965 key in the passed tgt, and a list of preferred etypes
966 *for the new ticket*. Should we pick the best possible
967 etype, given the keytype in the tgt, or should we look
968 at the etype list here as well? What if the tgt
969 session key is DES3 and we want a ticket with a (say)
970 CAST session key. Should the DES3 etype be added to the
971 etype list, even if we don't want a session key with
973 ret
= _kdc_encode_reply(context
, config
,
974 &rep
, &et
, &ek
, et
.key
.keytype
,
976 serverkey
, 0, replykey
, rk_is_subkey
,
979 krb5_enctype_disable(context
, et
.key
.keytype
);
983 free_TransitedEncoding(&et
.transited
);
988 if(et
.authorization_data
) {
989 free_AuthorizationData(et
.authorization_data
);
990 free(et
.authorization_data
);
992 free_LastReq(&ek
.last_req
);
993 memset(et
.key
.keyvalue
.data
, 0, et
.key
.keyvalue
.length
);
994 free_EncryptionKey(&et
.key
);
998 static krb5_error_code
999 tgs_check_authenticator(krb5_context context
,
1000 krb5_kdc_configuration
*config
,
1001 krb5_auth_context ac
,
1003 const char **e_text
,
1006 krb5_authenticator auth
;
1010 krb5_error_code ret
;
1013 krb5_auth_con_getauthenticator(context
, ac
, &auth
);
1014 if(auth
->cksum
== NULL
){
1015 kdc_log(context
, config
, 0, "No authenticator in request");
1016 ret
= KRB5KRB_AP_ERR_INAPP_CKSUM
;
1020 * according to RFC1510 it doesn't need to be keyed,
1021 * but according to the latest draft it needs to.
1025 !krb5_checksum_is_keyed(context
, auth
->cksum
->cksumtype
)
1028 !krb5_checksum_is_collision_proof(context
, auth
->cksum
->cksumtype
)) {
1029 kdc_log(context
, config
, 0, "Bad checksum type in authenticator: %d",
1030 auth
->cksum
->cksumtype
);
1031 ret
= KRB5KRB_AP_ERR_INAPP_CKSUM
;
1035 /* XXX should not re-encode this */
1036 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, b
, &len
, ret
);
1038 const char *msg
= krb5_get_error_message(context
, ret
);
1039 kdc_log(context
, config
, 0, "Failed to encode KDC-REQ-BODY: %s", msg
);
1040 krb5_free_error_message(context
, msg
);
1043 if(buf_size
!= len
) {
1045 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
1046 *e_text
= "KDC internal error";
1047 ret
= KRB5KRB_ERR_GENERIC
;
1050 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
1052 const char *msg
= krb5_get_error_message(context
, ret
);
1054 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1055 krb5_free_error_message(context
, msg
);
1058 ret
= krb5_verify_checksum(context
,
1060 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
1065 krb5_crypto_destroy(context
, crypto
);
1067 const char *msg
= krb5_get_error_message(context
, ret
);
1068 kdc_log(context
, config
, 0,
1069 "Failed to verify authenticator checksum: %s", msg
);
1070 krb5_free_error_message(context
, msg
);
1073 free_Authenticator(auth
);
1083 find_rpath(krb5_context context
, Realm crealm
, Realm srealm
)
1085 const char *new_realm
= krb5_config_get_string(context
,
1096 need_referral(krb5_context context
, krb5_kdc_configuration
*config
,
1097 const KDCOptions
* const options
, krb5_principal server
,
1098 krb5_realm
**realms
)
1102 if(!options
->canonicalize
&& server
->name
.name_type
!= KRB5_NT_SRV_INST
)
1105 if (server
->name
.name_string
.len
== 1)
1106 name
= server
->name
.name_string
.val
[0];
1107 else if (server
->name
.name_string
.len
> 1)
1108 name
= server
->name
.name_string
.val
[1];
1112 kdc_log(context
, config
, 0, "Searching referral for %s", name
);
1114 return _krb5_get_host_realm_int(context
, name
, FALSE
, realms
) == 0;
1117 static krb5_error_code
1118 tgs_parse_request(krb5_context context
,
1119 krb5_kdc_configuration
*config
,
1121 const PA_DATA
*tgs_req
,
1122 hdb_entry_ex
**krbtgt
,
1123 krb5_enctype
*krbtgt_etype
,
1124 krb5_ticket
**ticket
,
1125 const char **e_text
,
1127 const struct sockaddr
*from_addr
,
1130 AuthorizationData
**auth_data
,
1131 krb5_keyblock
**replykey
,
1134 static char failed
[] = "<unparse_name failed>";
1136 krb5_error_code ret
;
1137 krb5_principal princ
;
1138 krb5_auth_context ac
= NULL
;
1139 krb5_flags ap_req_options
;
1140 krb5_flags verify_ap_req_flags
;
1143 krb5_keyblock
*subkey
= NULL
;
1151 memset(&ap_req
, 0, sizeof(ap_req
));
1152 ret
= krb5_decode_ap_req(context
, &tgs_req
->padata_value
, &ap_req
);
1154 const char *msg
= krb5_get_error_message(context
, ret
);
1155 kdc_log(context
, config
, 0, "Failed to decode AP-REQ: %s", msg
);
1156 krb5_free_error_message(context
, msg
);
1160 if(!get_krbtgt_realm(&ap_req
.ticket
.sname
)){
1161 /* XXX check for ticket.sname == req.sname */
1162 kdc_log(context
, config
, 0, "PA-DATA is not a ticket-granting ticket");
1163 ret
= KRB5KDC_ERR_POLICY
; /* ? */
1167 _krb5_principalname2krb5_principal(context
,
1169 ap_req
.ticket
.sname
,
1170 ap_req
.ticket
.realm
);
1172 ret
= _kdc_db_fetch(context
, config
, princ
, HDB_F_GET_KRBTGT
, ap_req
.ticket
.enc_part
.kvno
, NULL
, krbtgt
);
1174 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1176 ret
= krb5_unparse_name(context
, princ
, &p
);
1179 krb5_free_principal(context
, princ
);
1180 kdc_log(context
, config
, 5, "Ticket-granting ticket account %s does not have secrets at this KDC, need to proxy", p
);
1183 ret
= HDB_ERR_NOT_FOUND_HERE
;
1186 const char *msg
= krb5_get_error_message(context
, ret
);
1188 ret
= krb5_unparse_name(context
, princ
, &p
);
1191 krb5_free_principal(context
, princ
);
1192 kdc_log(context
, config
, 0,
1193 "Ticket-granting ticket not found in database: %s", msg
);
1194 krb5_free_error_message(context
, msg
);
1197 ret
= KRB5KRB_AP_ERR_NOT_US
;
1201 if(ap_req
.ticket
.enc_part
.kvno
&&
1202 (size_t)*ap_req
.ticket
.enc_part
.kvno
!= (*krbtgt
)->entry
.kvno
){
1205 ret
= krb5_unparse_name (context
, princ
, &p
);
1206 krb5_free_principal(context
, princ
);
1209 kdc_log(context
, config
, 0,
1210 "Ticket kvno = %d, DB kvno = %d (%s)",
1211 *ap_req
.ticket
.enc_part
.kvno
,
1212 (*krbtgt
)->entry
.kvno
,
1216 ret
= KRB5KRB_AP_ERR_BADKEYVER
;
1220 *krbtgt_etype
= ap_req
.ticket
.enc_part
.etype
;
1222 ret
= hdb_enctype2key(context
, &(*krbtgt
)->entry
,
1223 ap_req
.ticket
.enc_part
.etype
, &tkey
);
1225 char *str
= NULL
, *p
= NULL
;
1227 krb5_enctype_to_string(context
, ap_req
.ticket
.enc_part
.etype
, &str
);
1228 krb5_unparse_name(context
, princ
, &p
);
1229 kdc_log(context
, config
, 0,
1230 "No server key with enctype %s found for %s",
1231 str
? str
: "<unknown enctype>",
1232 p
? p
: "<unparse_name failed>");
1235 ret
= KRB5KRB_AP_ERR_BADKEYVER
;
1239 if (b
->kdc_options
.validate
)
1240 verify_ap_req_flags
= KRB5_VERIFY_AP_REQ_IGNORE_INVALID
;
1242 verify_ap_req_flags
= 0;
1244 ret
= krb5_verify_ap_req2(context
,
1249 verify_ap_req_flags
,
1252 KRB5_KU_TGS_REQ_AUTH
);
1254 krb5_free_principal(context
, princ
);
1256 const char *msg
= krb5_get_error_message(context
, ret
);
1257 kdc_log(context
, config
, 0, "Failed to verify AP-REQ: %s", msg
);
1258 krb5_free_error_message(context
, msg
);
1263 krb5_authenticator auth
;
1265 ret
= krb5_auth_con_getauthenticator(context
, ac
, &auth
);
1267 *csec
= malloc(sizeof(**csec
));
1268 if (*csec
== NULL
) {
1269 krb5_free_authenticator(context
, &auth
);
1270 kdc_log(context
, config
, 0, "malloc failed");
1273 **csec
= auth
->ctime
;
1274 *cusec
= malloc(sizeof(**cusec
));
1275 if (*cusec
== NULL
) {
1276 krb5_free_authenticator(context
, &auth
);
1277 kdc_log(context
, config
, 0, "malloc failed");
1280 **cusec
= auth
->cusec
;
1281 krb5_free_authenticator(context
, &auth
);
1285 ret
= tgs_check_authenticator(context
, config
,
1286 ac
, b
, e_text
, &(*ticket
)->ticket
.key
);
1288 krb5_auth_con_free(context
, ac
);
1292 usage
= KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
;
1295 ret
= krb5_auth_con_getremotesubkey(context
, ac
, &subkey
);
1297 const char *msg
= krb5_get_error_message(context
, ret
);
1298 krb5_auth_con_free(context
, ac
);
1299 kdc_log(context
, config
, 0, "Failed to get remote subkey: %s", msg
);
1300 krb5_free_error_message(context
, msg
);
1304 usage
= KRB5_KU_TGS_REQ_AUTH_DAT_SESSION
;
1307 ret
= krb5_auth_con_getkey(context
, ac
, &subkey
);
1309 const char *msg
= krb5_get_error_message(context
, ret
);
1310 krb5_auth_con_free(context
, ac
);
1311 kdc_log(context
, config
, 0, "Failed to get session key: %s", msg
);
1312 krb5_free_error_message(context
, msg
);
1317 krb5_auth_con_free(context
, ac
);
1318 kdc_log(context
, config
, 0,
1319 "Failed to get key for enc-authorization-data");
1320 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1326 if (b
->enc_authorization_data
) {
1329 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
1331 const char *msg
= krb5_get_error_message(context
, ret
);
1332 krb5_auth_con_free(context
, ac
);
1333 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1334 krb5_free_error_message(context
, msg
);
1337 ret
= krb5_decrypt_EncryptedData (context
,
1340 b
->enc_authorization_data
,
1342 krb5_crypto_destroy(context
, crypto
);
1344 krb5_auth_con_free(context
, ac
);
1345 kdc_log(context
, config
, 0,
1346 "Failed to decrypt enc-authorization-data");
1347 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1351 if (*auth_data
== NULL
) {
1352 krb5_auth_con_free(context
, ac
);
1353 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1356 ret
= decode_AuthorizationData(ad
.data
, ad
.length
, *auth_data
, NULL
);
1358 krb5_auth_con_free(context
, ac
);
1361 kdc_log(context
, config
, 0, "Failed to decode authorization data");
1362 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1367 krb5_auth_con_free(context
, ac
);
1370 free_AP_REQ(&ap_req
);
1375 static krb5_error_code
1376 build_server_referral(krb5_context context
,
1377 krb5_kdc_configuration
*config
,
1378 krb5_crypto session
,
1379 krb5_const_realm referred_realm
,
1380 const PrincipalName
*true_principal_name
,
1381 const PrincipalName
*requested_principal
,
1384 PA_ServerReferralData ref
;
1385 krb5_error_code ret
;
1390 memset(&ref
, 0, sizeof(ref
));
1392 if (referred_realm
) {
1393 ALLOC(ref
.referred_realm
);
1394 if (ref
.referred_realm
== NULL
)
1396 *ref
.referred_realm
= strdup(referred_realm
);
1397 if (*ref
.referred_realm
== NULL
)
1400 if (true_principal_name
) {
1401 ALLOC(ref
.true_principal_name
);
1402 if (ref
.true_principal_name
== NULL
)
1404 ret
= copy_PrincipalName(true_principal_name
, ref
.true_principal_name
);
1408 if (requested_principal
) {
1409 ALLOC(ref
.requested_principal_name
);
1410 if (ref
.requested_principal_name
== NULL
)
1412 ret
= copy_PrincipalName(requested_principal
,
1413 ref
.requested_principal_name
);
1418 ASN1_MALLOC_ENCODE(PA_ServerReferralData
,
1419 data
.data
, data
.length
,
1421 free_PA_ServerReferralData(&ref
);
1424 if (data
.length
!= size
)
1425 krb5_abortx(context
, "internal asn.1 encoder error");
1427 ret
= krb5_encrypt_EncryptedData(context
, session
,
1428 KRB5_KU_PA_SERVER_REFERRAL
,
1429 data
.data
, data
.length
,
1435 ASN1_MALLOC_ENCODE(EncryptedData
,
1436 outdata
->data
, outdata
->length
,
1438 free_EncryptedData(&ed
);
1441 if (outdata
->length
!= size
)
1442 krb5_abortx(context
, "internal asn.1 encoder error");
1446 free_PA_ServerReferralData(&ref
);
1447 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1451 static krb5_error_code
1452 tgs_build_reply(krb5_context context
,
1453 krb5_kdc_configuration
*config
,
1456 hdb_entry_ex
*krbtgt
,
1457 krb5_enctype krbtgt_etype
,
1458 const krb5_keyblock
*replykey
,
1460 krb5_ticket
*ticket
,
1463 const char **e_text
,
1464 AuthorizationData
**auth_data
,
1465 const struct sockaddr
*from_addr
)
1467 krb5_error_code ret
;
1468 krb5_principal cp
= NULL
, sp
= NULL
;
1469 krb5_principal client_principal
= NULL
;
1470 krb5_principal krbtgt_principal
= NULL
;
1471 char *spn
= NULL
, *cpn
= NULL
;
1472 hdb_entry_ex
*server
= NULL
, *client
= NULL
, *s4u2self_impersonated_client
= NULL
;
1473 HDB
*clientdb
, *s4u2self_impersonated_clientdb
;
1474 krb5_realm ref_realm
= NULL
;
1475 EncTicketPart
*tgt
= &ticket
->ticket
;
1476 krb5_principals spp
= NULL
;
1477 const EncryptionKey
*ekey
;
1478 krb5_keyblock sessionkey
;
1482 hdb_entry_ex
*krbtgt_out
= NULL
;
1484 METHOD_DATA enc_pa_data
;
1489 EncTicketPart adtkt
;
1496 memset(&sessionkey
, 0, sizeof(sessionkey
));
1497 memset(&adtkt
, 0, sizeof(adtkt
));
1498 krb5_data_zero(&rspac
);
1499 memset(&enc_pa_data
, 0, sizeof(enc_pa_data
));
1504 if(b
->kdc_options
.enc_tkt_in_skey
){
1510 if(b
->additional_tickets
== NULL
||
1511 b
->additional_tickets
->len
== 0){
1512 ret
= KRB5KDC_ERR_BADOPTION
; /* ? */
1513 kdc_log(context
, config
, 0,
1514 "No second ticket present in request");
1517 t
= &b
->additional_tickets
->val
[0];
1518 if(!get_krbtgt_realm(&t
->sname
)){
1519 kdc_log(context
, config
, 0,
1520 "Additional ticket is not a ticket-granting ticket");
1521 ret
= KRB5KDC_ERR_POLICY
;
1524 _krb5_principalname2krb5_principal(context
, &p
, t
->sname
, t
->realm
);
1525 ret
= _kdc_db_fetch(context
, config
, p
,
1526 HDB_F_GET_KRBTGT
, t
->enc_part
.kvno
,
1528 krb5_free_principal(context
, p
);
1530 if (ret
== HDB_ERR_NOENTRY
)
1531 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1534 ret
= hdb_enctype2key(context
, &uu
->entry
,
1535 t
->enc_part
.etype
, &uukey
);
1537 _kdc_free_ent(context
, uu
);
1538 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
; /* XXX */
1541 ret
= krb5_decrypt_ticket(context
, t
, &uukey
->key
, &adtkt
, 0);
1542 _kdc_free_ent(context
, uu
);
1546 ret
= verify_flags(context
, config
, &adtkt
, spn
);
1554 _krb5_principalname2krb5_principal(context
, &sp
, *s
, r
);
1555 ret
= krb5_unparse_name(context
, sp
, &spn
);
1558 _krb5_principalname2krb5_principal(context
, &cp
, tgt
->cname
, tgt
->crealm
);
1559 ret
= krb5_unparse_name(context
, cp
, &cpn
);
1562 unparse_flags (KDCOptions2int(b
->kdc_options
),
1563 asn1_KDCOptions_units(),
1564 opt_str
, sizeof(opt_str
));
1566 kdc_log(context
, config
, 0,
1567 "TGS-REQ %s from %s for %s [%s]",
1568 cpn
, from
, spn
, opt_str
);
1570 kdc_log(context
, config
, 0,
1571 "TGS-REQ %s from %s for %s", cpn
, from
, spn
);
1578 ret
= _kdc_db_fetch(context
, config
, sp
, HDB_F_GET_SERVER
| HDB_F_CANON
,
1579 NULL
, NULL
, &server
);
1581 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1582 kdc_log(context
, config
, 5, "target %s does not have secrets at this KDC, need to proxy", sp
);
1585 const char *new_rlm
, *msg
;
1589 if ((req_rlm
= get_krbtgt_realm(&sp
->name
)) != NULL
) {
1591 new_rlm
= find_rpath(context
, tgt
->crealm
, req_rlm
);
1593 kdc_log(context
, config
, 5, "krbtgt for realm %s "
1594 "not found, trying %s",
1596 krb5_free_principal(context
, sp
);
1598 krb5_make_principal(context
, &sp
, r
,
1599 KRB5_TGS_NAME
, new_rlm
, NULL
);
1600 ret
= krb5_unparse_name(context
, sp
, &spn
);
1606 ref_realm
= strdup(new_rlm
);
1610 } else if(need_referral(context
, config
, &b
->kdc_options
, sp
, &realms
)) {
1611 if (strcmp(realms
[0], sp
->realm
) != 0) {
1612 kdc_log(context
, config
, 5,
1613 "Returning a referral to realm %s for "
1614 "server %s that was not found",
1616 krb5_free_principal(context
, sp
);
1618 krb5_make_principal(context
, &sp
, r
, KRB5_TGS_NAME
,
1620 ret
= krb5_unparse_name(context
, sp
, &spn
);
1626 ref_realm
= strdup(realms
[0]);
1628 krb5_free_host_realm(context
, realms
);
1631 krb5_free_host_realm(context
, realms
);
1633 msg
= krb5_get_error_message(context
, ret
);
1634 kdc_log(context
, config
, 0,
1635 "Server not found in database: %s: %s", spn
, msg
);
1636 krb5_free_error_message(context
, msg
);
1637 if (ret
== HDB_ERR_NOENTRY
)
1638 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1643 * Select enctype, return key and kvno.
1649 if(b
->kdc_options
.enc_tkt_in_skey
) {
1652 for(i
= 0; i
< b
->etype
.len
; i
++)
1653 if (b
->etype
.val
[i
] == adtkt
.key
.keytype
)
1655 if(i
== b
->etype
.len
) {
1656 kdc_log(context
, config
, 0,
1657 "Addition ticket have not matching etypes");
1658 krb5_clear_error_message(context
);
1659 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
1662 etype
= b
->etype
.val
[i
];
1667 ret
= _kdc_find_etype(context
, server
,
1668 b
->etype
.val
, b
->etype
.len
, &skey
);
1670 kdc_log(context
, config
, 0,
1671 "Server (%s) has no support for etypes", spn
);
1675 etype
= skey
->key
.keytype
;
1676 kvno
= server
->entry
.kvno
;
1679 ret
= krb5_generate_random_keyblock(context
, etype
, &sessionkey
);
1685 * Check that service is in the same realm as the krbtgt. If it's
1686 * not the same, it's someone that is using a uni-directional trust
1691 * Validate authoriation data
1694 ret
= hdb_enctype2key(context
, &krbtgt
->entry
,
1695 krbtgt_etype
, &tkey_check
);
1697 kdc_log(context
, config
, 0,
1698 "Failed to find key for krbtgt PAC check");
1702 /* Now refetch the primary krbtgt, and get the current kvno (the
1703 * sign check may have been on an old kvno, and the server may
1704 * have been an incoming trust) */
1705 ret
= krb5_make_principal(context
, &krbtgt_principal
,
1706 krb5_principal_get_comp_string(context
,
1707 krbtgt
->entry
.principal
,
1710 krb5_principal_get_comp_string(context
,
1711 krbtgt
->entry
.principal
,
1714 kdc_log(context
, config
, 0,
1715 "Failed to generate krbtgt principal");
1719 ret
= _kdc_db_fetch(context
, config
, krbtgt_principal
, HDB_F_GET_KRBTGT
, NULL
, NULL
, &krbtgt_out
);
1720 krb5_free_principal(context
, krbtgt_principal
);
1722 krb5_error_code ret2
;
1724 ret
= krb5_unparse_name(context
, krbtgt
->entry
.principal
, &tpn
);
1725 ret2
= krb5_unparse_name(context
, krbtgt
->entry
.principal
, &tpn2
);
1726 kdc_log(context
, config
, 0,
1727 "Request with wrong krbtgt: %s, %s not found in our database",
1728 (ret
== 0) ? tpn
: "<unknown>", (ret2
== 0) ? tpn2
: "<unknown>");
1733 ret
= KRB5KRB_AP_ERR_NOT_US
;
1737 /* The first realm is the realm of the service, the second is
1738 * krbtgt/<this>/@REALM component of the krbtgt DN the request was
1739 * encrypted to. The redirection via the krbtgt_out entry allows
1740 * the DB to possibly correct the case of the realm (Samba4 does
1741 * this) before the strcmp() */
1742 if (strcmp(krb5_principal_get_realm(context
, server
->entry
.principal
),
1743 krb5_principal_get_realm(context
, krbtgt_out
->entry
.principal
)) != 0) {
1745 ret
= krb5_unparse_name(context
, krbtgt_out
->entry
.principal
, &tpn
);
1746 kdc_log(context
, config
, 0,
1747 "Request with wrong krbtgt: %s",
1748 (ret
== 0) ? tpn
: "<unknown>");
1751 ret
= KRB5KRB_AP_ERR_NOT_US
;
1754 ret
= hdb_enctype2key(context
, &krbtgt_out
->entry
,
1755 krbtgt_etype
, &tkey_sign
);
1757 kdc_log(context
, config
, 0,
1758 "Failed to find key for krbtgt PAC signature");
1762 ret
= _kdc_db_fetch(context
, config
, cp
, HDB_F_GET_CLIENT
| HDB_F_CANON
,
1763 NULL
, &clientdb
, &client
);
1764 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1765 /* This is OK, we are just trying to find out if they have
1766 * been disabled or deleted in the meantime, missing secrets
1769 const char *krbtgt_realm
, *msg
;
1772 * If the client belongs to the same realm as our krbtgt, it
1773 * should exist in the local database.
1777 krbtgt_realm
= krb5_principal_get_realm(context
, krbtgt_out
->entry
.principal
);
1779 if(strcmp(krb5_principal_get_realm(context
, cp
), krbtgt_realm
) == 0) {
1780 if (ret
== HDB_ERR_NOENTRY
)
1781 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1782 kdc_log(context
, config
, 1, "Client no longer in database: %s",
1787 msg
= krb5_get_error_message(context
, ret
);
1788 kdc_log(context
, config
, 1, "Client not found in database: %s", msg
);
1789 krb5_free_error_message(context
, msg
);
1792 ret
= check_PAC(context
, config
, cp
,
1793 client
, server
, krbtgt
, ekey
, &tkey_check
->key
, &tkey_sign
->key
,
1794 tgt
, &rspac
, &signedpath
);
1796 const char *msg
= krb5_get_error_message(context
, ret
);
1797 kdc_log(context
, config
, 0,
1798 "Verify PAC failed for %s (%s) from %s with %s",
1799 spn
, cpn
, from
, msg
);
1800 krb5_free_error_message(context
, msg
);
1804 /* also check the krbtgt for signature */
1805 ret
= check_KRB5SignedPath(context
,
1813 const char *msg
= krb5_get_error_message(context
, ret
);
1814 kdc_log(context
, config
, 0,
1815 "KRB5SignedPath check failed for %s (%s) from %s with %s",
1816 spn
, cpn
, from
, msg
);
1817 krb5_free_error_message(context
, msg
);
1825 client_principal
= cp
;
1828 const PA_DATA
*sdata
;
1831 sdata
= _kdc_find_padata(req
, &i
, KRB5_PADATA_FOR_USER
);
1836 char *selfcpn
= NULL
;
1839 ret
= decode_PA_S4U2Self(sdata
->padata_value
.data
,
1840 sdata
->padata_value
.length
,
1843 kdc_log(context
, config
, 0, "Failed to decode PA-S4U2Self");
1847 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &datack
);
1851 ret
= krb5_crypto_init(context
, &tgt
->key
, 0, &crypto
);
1853 const char *msg
= krb5_get_error_message(context
, ret
);
1854 free_PA_S4U2Self(&self
);
1855 krb5_data_free(&datack
);
1856 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1857 krb5_free_error_message(context
, msg
);
1861 ret
= krb5_verify_checksum(context
,
1863 KRB5_KU_OTHER_CKSUM
,
1867 krb5_data_free(&datack
);
1868 krb5_crypto_destroy(context
, crypto
);
1870 const char *msg
= krb5_get_error_message(context
, ret
);
1871 free_PA_S4U2Self(&self
);
1872 kdc_log(context
, config
, 0,
1873 "krb5_verify_checksum failed for S4U2Self: %s", msg
);
1874 krb5_free_error_message(context
, msg
);
1878 ret
= _krb5_principalname2krb5_principal(context
,
1882 free_PA_S4U2Self(&self
);
1886 ret
= krb5_unparse_name(context
, client_principal
, &selfcpn
);
1890 /* If we were about to put a PAC into the ticket, we better fix it to be the right PAC */
1893 krb5_data_free(&rspac
);
1894 ret
= _kdc_db_fetch(context
, config
, client_principal
, HDB_F_GET_CLIENT
| HDB_F_CANON
,
1895 NULL
, &s4u2self_impersonated_clientdb
, &s4u2self_impersonated_client
);
1900 * If the client belongs to the same realm as our krbtgt, it
1901 * should exist in the local database.
1905 if (ret
== HDB_ERR_NOENTRY
)
1906 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1907 msg
= krb5_get_error_message(context
, ret
);
1908 kdc_log(context
, config
, 1, "S2U4Self principal to impersonate %s not found in database: %s", cpn
, msg
);
1909 krb5_free_error_message(context
, msg
);
1912 ret
= _kdc_pac_generate(context
, s4u2self_impersonated_client
, &p
);
1914 kdc_log(context
, config
, 0, "PAC generation failed for -- %s",
1919 ret
= _krb5_pac_sign(context
, p
, ticket
->ticket
.authtime
,
1920 s4u2self_impersonated_client
->entry
.principal
,
1921 ekey
, &tkey_sign
->key
,
1923 krb5_pac_free(context
, p
);
1925 kdc_log(context
, config
, 0, "PAC signing failed for -- %s",
1933 * Check that service doing the impersonating is
1934 * requesting a ticket to it-self.
1936 ret
= check_s4u2self(context
, config
, clientdb
, client
, sp
);
1938 kdc_log(context
, config
, 0, "S4U2Self: %s is not allowed "
1939 "to impersonate to service "
1940 "(tried for user %s to service %s)",
1947 * If the service isn't trusted for authentication to
1948 * delegation, remove the forward flag.
1951 if (client
->entry
.flags
.trusted_for_delegation
) {
1952 str
= "[forwardable]";
1954 b
->kdc_options
.forwardable
= 0;
1957 kdc_log(context
, config
, 0, "s4u2self %s impersonating %s to "
1958 "service %s %s", cpn
, selfcpn
, spn
, str
);
1964 * Constrained delegation
1968 && b
->additional_tickets
!= NULL
1969 && b
->additional_tickets
->len
!= 0
1970 && b
->kdc_options
.enc_tkt_in_skey
== 0)
1972 int ad_signedpath
= 0;
1978 * Require that the KDC have issued the service's krbtgt (not
1979 * self-issued ticket with kimpersonate(1).
1982 ret
= KRB5KDC_ERR_BADOPTION
;
1983 kdc_log(context
, config
, 0,
1984 "Constrained delegation done on service ticket %s/%s",
1989 t
= &b
->additional_tickets
->val
[0];
1991 ret
= hdb_enctype2key(context
, &client
->entry
,
1992 t
->enc_part
.etype
, &clientkey
);
1994 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
; /* XXX */
1998 ret
= krb5_decrypt_ticket(context
, t
, &clientkey
->key
, &adtkt
, 0);
2000 kdc_log(context
, config
, 0,
2001 "failed to decrypt ticket for "
2002 "constrained delegation from %s to %s ", cpn
, spn
);
2006 /* check that ticket is valid */
2007 if (adtkt
.flags
.forwardable
== 0) {
2008 kdc_log(context
, config
, 0,
2009 "Missing forwardable flag on ticket for "
2010 "constrained delegation from %s to %s ", cpn
, spn
);
2011 ret
= KRB5KDC_ERR_BADOPTION
;
2015 ret
= check_constrained_delegation(context
, config
, clientdb
,
2018 kdc_log(context
, config
, 0,
2019 "constrained delegation from %s to %s not allowed",
2024 ret
= _krb5_principalname2krb5_principal(context
,
2031 ret
= krb5_unparse_name(context
, client_principal
, &str
);
2035 ret
= verify_flags(context
, config
, &adtkt
, str
);
2042 * Check that the KDC issued the user's ticket.
2044 ret
= check_KRB5SignedPath(context
,
2051 if (ret
== 0 && !ad_signedpath
)
2052 ret
= KRB5KDC_ERR_BADOPTION
;
2054 const char *msg
= krb5_get_error_message(context
, ret
);
2055 kdc_log(context
, config
, 0,
2056 "KRB5SignedPath check from service %s failed "
2057 "for delegation to %s for client %s "
2058 "from %s failed with %s",
2059 spn
, str
, cpn
, from
, msg
);
2060 krb5_free_error_message(context
, msg
);
2065 kdc_log(context
, config
, 0, "constrained delegation for %s "
2066 "from %s to %s", str
, cpn
, spn
);
2074 ret
= kdc_check_flags(context
, config
,
2081 if((b
->kdc_options
.validate
|| b
->kdc_options
.renew
) &&
2082 !krb5_principal_compare(context
,
2083 krbtgt
->entry
.principal
,
2084 server
->entry
.principal
)){
2085 kdc_log(context
, config
, 0, "Inconsistent request.");
2086 ret
= KRB5KDC_ERR_SERVER_NOMATCH
;
2090 /* check for valid set of addresses */
2091 if(!_kdc_check_addresses(context
, config
, tgt
->caddr
, from_addr
)) {
2092 ret
= KRB5KRB_AP_ERR_BADADDR
;
2093 kdc_log(context
, config
, 0, "Request from wrong address");
2098 * If this is an referral, add server referral data to the
2105 kdc_log(context
, config
, 0,
2106 "Adding server referral to %s", ref_realm
);
2108 ret
= krb5_crypto_init(context
, &sessionkey
, 0, &crypto
);
2112 ret
= build_server_referral(context
, config
, crypto
, ref_realm
,
2113 NULL
, s
, &pa
.padata_value
);
2114 krb5_crypto_destroy(context
, crypto
);
2116 kdc_log(context
, config
, 0,
2117 "Failed building server referral");
2120 pa
.padata_type
= KRB5_PADATA_SERVER_REFERRAL
;
2122 ret
= add_METHOD_DATA(&enc_pa_data
, &pa
);
2123 krb5_data_free(&pa
.padata_value
);
2125 kdc_log(context
, config
, 0,
2126 "Add server referral METHOD-DATA failed");
2135 ret
= tgs_make_reply(context
,
2163 krb5_data_free(&rspac
);
2164 krb5_free_keyblock_contents(context
, &sessionkey
);
2166 _kdc_free_ent(context
, krbtgt_out
);
2168 _kdc_free_ent(context
, server
);
2170 _kdc_free_ent(context
, client
);
2171 if(s4u2self_impersonated_client
)
2172 _kdc_free_ent(context
, s4u2self_impersonated_client
);
2174 if (client_principal
&& client_principal
!= cp
)
2175 krb5_free_principal(context
, client_principal
);
2177 krb5_free_principal(context
, cp
);
2179 krb5_free_principal(context
, sp
);
2182 free_METHOD_DATA(&enc_pa_data
);
2184 free_EncTicketPart(&adtkt
);
2194 _kdc_tgs_rep(krb5_context context
,
2195 krb5_kdc_configuration
*config
,
2199 struct sockaddr
*from_addr
,
2202 AuthorizationData
*auth_data
= NULL
;
2203 krb5_error_code ret
;
2205 const PA_DATA
*tgs_req
;
2207 hdb_entry_ex
*krbtgt
= NULL
;
2208 krb5_ticket
*ticket
= NULL
;
2209 const char *e_text
= NULL
;
2210 krb5_enctype krbtgt_etype
= ETYPE_NULL
;
2212 krb5_keyblock
*replykey
= NULL
;
2213 int rk_is_subkey
= 0;
2214 time_t *csec
= NULL
;
2217 if(req
->padata
== NULL
){
2218 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
; /* XXX ??? */
2219 kdc_log(context
, config
, 0,
2220 "TGS-REQ from %s without PA-DATA", from
);
2224 tgs_req
= _kdc_find_padata(req
, &i
, KRB5_PADATA_TGS_REQ
);
2226 if(tgs_req
== NULL
){
2227 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
2229 kdc_log(context
, config
, 0,
2230 "TGS-REQ from %s without PA-TGS-REQ", from
);
2233 ret
= tgs_parse_request(context
, config
,
2234 &req
->req_body
, tgs_req
,
2244 if (ret
== HDB_ERR_NOT_FOUND_HERE
) {
2245 /* kdc_log() is called in tgs_parse_request() */
2249 kdc_log(context
, config
, 0,
2250 "Failed parsing TGS-REQ from %s", from
);
2254 ret
= tgs_build_reply(context
,
2269 kdc_log(context
, config
, 0,
2270 "Failed building TGS-REP to %s", from
);
2275 if (datagram_reply
&& data
->length
> config
->max_datagram_reply_length
) {
2276 krb5_data_free(data
);
2277 ret
= KRB5KRB_ERR_RESPONSE_TOO_BIG
;
2278 e_text
= "Reply packet too large";
2283 krb5_free_keyblock(context
, replykey
);
2284 if(ret
&& ret
!= HDB_ERR_NOT_FOUND_HERE
&& data
->data
== NULL
){
2285 krb5_mk_error(context
,
2299 krb5_free_ticket(context
, ticket
);
2301 _kdc_free_ent(context
, krbtgt
);
2304 free_AuthorizationData(auth_data
);