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
, NULL
, 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
, NULL
, /* XXX use correct kvno! */
232 ret
= krb5_crypto_init(context
, &key
->key
, 0, &crypto
);
235 free_KRB5SignedPath(&sp
);
239 ret
= krb5_verify_checksum(context
, crypto
, KRB5_KU_KRB5SIGNEDPATH
,
240 data
.data
, data
.length
,
242 krb5_crypto_destroy(context
, crypto
);
245 free_KRB5SignedPath(&sp
);
246 kdc_log(context
, config
, 5,
247 "KRB5SignedPath not signed correctly, not marking as signed");
251 if (delegated
&& sp
.delegated
) {
253 *delegated
= malloc(sizeof(*sp
.delegated
));
254 if (*delegated
== NULL
) {
255 free_KRB5SignedPath(&sp
);
259 ret
= copy_Principals(*delegated
, sp
.delegated
);
261 free_KRB5SignedPath(&sp
);
267 free_KRB5SignedPath(&sp
);
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
,
294 AuthorizationData
*ad
= tkt
->authorization_data
;
298 if (ad
== NULL
|| ad
->len
== 0)
301 for (i
= 0; i
< ad
->len
; i
++) {
302 AuthorizationData child
;
304 if (ad
->val
[i
].ad_type
!= KRB5_AUTHDATA_IF_RELEVANT
)
307 ret
= decode_AuthorizationData(ad
->val
[i
].ad_data
.data
,
308 ad
->val
[i
].ad_data
.length
,
312 krb5_set_error_message(context
, ret
, "Failed to decode "
313 "IF_RELEVANT with %d", ret
);
316 for (j
= 0; j
< child
.len
; j
++) {
318 if (child
.val
[j
].ad_type
== KRB5_AUTHDATA_WIN2K_PAC
) {
323 ret
= krb5_pac_parse(context
,
324 child
.val
[j
].ad_data
.data
,
325 child
.val
[j
].ad_data
.length
,
327 free_AuthorizationData(&child
);
331 ret
= krb5_pac_verify(context
, pac
, tkt
->authtime
,
333 server_check_key
, NULL
);
335 krb5_pac_free(context
, pac
);
339 ret
= _kdc_pac_verify(context
, client_principal
,
340 delegated_proxy_principal
,
341 client
, server
, krbtgt
, &pac
, &signed_pac
);
343 krb5_pac_free(context
, pac
);
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.
355 ret
= _krb5_pac_sign(context
, pac
, tkt
->authtime
,
357 server_sign_key
, krbtgt_sign_key
, rspac
);
359 krb5_pac_free(context
, pac
);
364 free_AuthorizationData(&child
);
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
;
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
;
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
;
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;
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;
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;
428 if(!tgt
->flags
.proxiable
){
429 kdc_log(context
, config
, 0,
430 "Request to proxy non-proxiable ticket");
431 return KRB5KDC_ERR_BADOPTION
;
434 et
->caddr
= b
->addresses
;
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;
448 if(!tgt
->flags
.may_postdate
){
449 kdc_log(context
, config
, 0,
450 "Bad request for postdated ticket");
451 return KRB5KDC_ERR_BADOPTION
;
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
;
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
;
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
;
482 old_life
-= *tgt
->starttime
;
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
);
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
;
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
,
509 hdb_entry_ex
*client
,
510 hdb_entry_ex
*server
,
511 krb5_const_principal target
)
513 const HDB_Ext_Constrained_delegation_acl
*acl
;
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");
530 if (clientdb
->hdb_check_constrained_delegation
) {
531 ret
= clientdb
->hdb_check_constrained_delegation(context
, clientdb
, client
, target
);
535 /* if client delegates to itself, that ok */
536 if (krb5_principal_compare(context
, client
->entry
.principal
, server
->entry
.principal
) == TRUE
)
539 ret
= hdb_entry_get_ConstrainedDelegACL(&client
->entry
, &acl
);
541 krb5_clear_error_message(context
);
546 for (i
= 0; i
< acl
->len
; i
++) {
547 if (krb5_principal_compare(context
, target
, &acl
->val
[i
]) == TRUE
)
551 ret
= KRB5KDC_ERR_BADOPTION
;
553 kdc_log(context
, config
, 0,
554 "Bad request for constrained delegation");
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
,
569 hdb_entry_ex
*client
,
570 krb5_const_principal server
)
574 /* if client does a s4u2self to itself, that ok */
575 if (krb5_principal_compare(context
, client
->entry
.principal
, server
) == TRUE
)
578 if (clientdb
->hdb_check_s4u2self
) {
579 ret
= clientdb
->hdb_check_s4u2self(context
, clientdb
, client
, server
);
583 ret
= KRB5KDC_ERR_BADOPTION
;
592 static krb5_error_code
593 verify_flags (krb5_context context
,
594 krb5_kdc_configuration
*config
,
595 const EncTicketPart
*et
,
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
;
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
,
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
;
628 switch (tr
->tr_type
) {
629 case DOMAIN_X500_COMPRESS
:
633 * Allow empty content of type 0 because that is was Microsoft
634 * generates in their TGT.
636 if (tr
->contents
.length
== 0)
638 kdc_log(context
, config
, 0,
639 "Transited type 0 with non empty content");
640 return KRB5KDC_ERR_TRTYPE_NOSUPP
;
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
,
654 krb5_warn(context
, ret
,
655 "Decoding transited encoding");
660 * If the realm of the presented tgt is neither the client nor the server
661 * realm, it is a transit realm and must be added to transited set.
663 if(strcmp(client_realm
, tgt_realm
) && strcmp(server_realm
, tgt_realm
)) {
664 if (num_realms
+ 1 > UINT_MAX
/sizeof(*realms
)) {
668 tmp
= realloc(realms
, (num_realms
+ 1) * sizeof(*realms
));
674 realms
[num_realms
] = strdup(tgt_realm
);
675 if(realms
[num_realms
] == NULL
){
681 if(num_realms
== 0) {
682 if(strcmp(client_realm
, server_realm
))
683 kdc_log(context
, config
, 0,
684 "cross-realm %s -> %s", client_realm
, server_realm
);
688 for(i
= 0; i
< num_realms
; i
++)
689 l
+= strlen(realms
[i
]) + 2;
693 for(i
= 0; i
< num_realms
; i
++) {
695 strlcat(rs
, ", ", l
);
696 strlcat(rs
, realms
[i
], l
);
698 kdc_log(context
, config
, 0,
699 "cross-realm %s -> %s via [%s]",
700 client_realm
, server_realm
, rs
);
705 ret
= krb5_check_transited(context
, client_realm
,
707 realms
, num_realms
, NULL
);
709 krb5_warn(context
, ret
, "cross-realm %s -> %s",
710 client_realm
, server_realm
);
713 et
->flags
.transited_policy_checked
= 1;
715 et
->transited
.tr_type
= DOMAIN_X500_COMPRESS
;
716 ret
= krb5_domain_x500_encode(realms
, num_realms
, &et
->transited
.contents
);
718 krb5_warn(context
, ret
, "Encoding transited encoding");
720 for(i
= 0; i
< num_realms
; i
++)
727 static krb5_error_code
728 tgs_make_reply(krb5_context context
,
729 krb5_kdc_configuration
*config
,
731 krb5_const_principal tgt_name
,
732 const EncTicketPart
*tgt
,
733 const krb5_keyblock
*replykey
,
735 const EncryptionKey
*serverkey
,
736 const krb5_keyblock
*sessionkey
,
738 AuthorizationData
*auth_data
,
739 hdb_entry_ex
*server
,
740 krb5_principal server_principal
,
741 const char *server_name
,
742 hdb_entry_ex
*client
,
743 krb5_principal client_principal
,
744 const char *tgt_realm
,
745 hdb_entry_ex
*krbtgt
,
746 krb5_enctype krbtgt_etype
,
748 const krb5_data
*rspac
,
749 const METHOD_DATA
*enc_pa_data
,
756 KDCOptions f
= b
->kdc_options
;
760 memset(&rep
, 0, sizeof(rep
));
761 memset(&et
, 0, sizeof(et
));
762 memset(&ek
, 0, sizeof(ek
));
765 rep
.msg_type
= krb_tgs_rep
;
767 et
.authtime
= tgt
->authtime
;
768 _kdc_fix_time(&b
->till
);
769 et
.endtime
= min(tgt
->endtime
, *b
->till
);
771 *et
.starttime
= kdc_time
;
773 ret
= check_tgs_flags(context
, config
, b
, tgt
, &et
);
777 /* We should check the transited encoding if:
778 1) the request doesn't ask not to be checked
779 2) globally enforcing a check
780 3) principal requires checking
781 4) we allow non-check per-principal, but principal isn't marked as allowing this
782 5) we don't globally allow this
785 #define GLOBAL_FORCE_TRANSITED_CHECK \
786 (config->trpolicy == TRPOLICY_ALWAYS_CHECK)
787 #define GLOBAL_ALLOW_PER_PRINCIPAL \
788 (config->trpolicy == TRPOLICY_ALLOW_PER_PRINCIPAL)
789 #define GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK \
790 (config->trpolicy == TRPOLICY_ALWAYS_HONOUR_REQUEST)
792 /* these will consult the database in future release */
793 #define PRINCIPAL_FORCE_TRANSITED_CHECK(P) 0
794 #define PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(P) 0
796 ret
= fix_transited_encoding(context
, config
,
797 !f
.disable_transited_check
||
798 GLOBAL_FORCE_TRANSITED_CHECK
||
799 PRINCIPAL_FORCE_TRANSITED_CHECK(server
) ||
800 !((GLOBAL_ALLOW_PER_PRINCIPAL
&&
801 PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(server
)) ||
802 GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK
),
803 &tgt
->transited
, &et
,
804 krb5_principal_get_realm(context
, client_principal
),
805 krb5_principal_get_realm(context
, server
->entry
.principal
),
810 ret
= copy_Realm(&server
->entry
.principal
->realm
, &rep
.ticket
.realm
);
813 _krb5_principal2principalname(&rep
.ticket
.sname
, server_principal
);
814 ret
= copy_Realm(&tgt_name
->realm
, &rep
.crealm
);
818 if (f.request_anonymous)
819 _kdc_make_anonymous_principalname (&rep.cname);
822 ret
= copy_PrincipalName(&tgt_name
->name
, &rep
.cname
);
825 rep
.ticket
.tkt_vno
= 5;
831 life
= et
.endtime
- *et
.starttime
;
832 if(client
&& client
->entry
.max_life
)
833 life
= min(life
, *client
->entry
.max_life
);
834 if(server
->entry
.max_life
)
835 life
= min(life
, *server
->entry
.max_life
);
836 et
.endtime
= *et
.starttime
+ life
;
838 if(f
.renewable_ok
&& tgt
->flags
.renewable
&&
839 et
.renew_till
== NULL
&& et
.endtime
< *b
->till
&&
840 tgt
->renew_till
!= NULL
)
842 et
.flags
.renewable
= 1;
843 ALLOC(et
.renew_till
);
844 *et
.renew_till
= *b
->till
;
848 renew
= *et
.renew_till
- *et
.starttime
;
849 if(client
&& client
->entry
.max_renew
)
850 renew
= min(renew
, *client
->entry
.max_renew
);
851 if(server
->entry
.max_renew
)
852 renew
= min(renew
, *server
->entry
.max_renew
);
853 *et
.renew_till
= *et
.starttime
+ renew
;
857 *et
.renew_till
= min(*et
.renew_till
, *tgt
->renew_till
);
858 *et
.starttime
= min(*et
.starttime
, *et
.renew_till
);
859 et
.endtime
= min(et
.endtime
, *et
.renew_till
);
862 *et
.starttime
= min(*et
.starttime
, et
.endtime
);
864 if(*et
.starttime
== et
.endtime
){
865 ret
= KRB5KDC_ERR_NEVER_VALID
;
868 if(et
.renew_till
&& et
.endtime
== *et
.renew_till
){
870 et
.renew_till
= NULL
;
871 et
.flags
.renewable
= 0;
874 et
.flags
.pre_authent
= tgt
->flags
.pre_authent
;
875 et
.flags
.hw_authent
= tgt
->flags
.hw_authent
;
876 et
.flags
.anonymous
= tgt
->flags
.anonymous
;
877 et
.flags
.ok_as_delegate
= server
->entry
.flags
.ok_as_delegate
;
881 * No not need to filter out the any PAC from the
882 * auth_data since it's signed by the KDC.
884 ret
= _kdc_tkt_add_if_relevant_ad(context
, &et
,
885 KRB5_AUTHDATA_WIN2K_PAC
, rspac
);
893 /* XXX check authdata */
895 if (et
.authorization_data
== NULL
) {
896 et
.authorization_data
= calloc(1, sizeof(*et
.authorization_data
));
897 if (et
.authorization_data
== NULL
) {
899 krb5_set_error_message(context
, ret
, "malloc: out of memory");
903 for(i
= 0; i
< auth_data
->len
; i
++) {
904 ret
= add_AuthorizationData(et
.authorization_data
, &auth_data
->val
[i
]);
906 krb5_set_error_message(context
, ret
, "malloc: out of memory");
911 /* Filter out type KRB5SignedPath */
912 ret
= find_KRB5SignedPath(context
, et
.authorization_data
, NULL
);
914 if (et
.authorization_data
->len
== 1) {
915 free_AuthorizationData(et
.authorization_data
);
916 free(et
.authorization_data
);
917 et
.authorization_data
= NULL
;
919 AuthorizationData
*ad
= et
.authorization_data
;
920 free_AuthorizationDataElement(&ad
->val
[ad
->len
- 1]);
926 ret
= krb5_copy_keyblock_contents(context
, sessionkey
, &et
.key
);
929 et
.crealm
= tgt_name
->realm
;
930 et
.cname
= tgt_name
->name
;
933 /* MIT must have at least one last_req */
934 ek
.last_req
.val
= calloc(1, sizeof(*ek
.last_req
.val
));
935 if (ek
.last_req
.val
== NULL
) {
939 ek
.last_req
.len
= 1; /* set after alloc to avoid null deref on cleanup */
942 ek
.authtime
= et
.authtime
;
943 ek
.starttime
= et
.starttime
;
944 ek
.endtime
= et
.endtime
;
945 ek
.renew_till
= et
.renew_till
;
946 ek
.srealm
= rep
.ticket
.realm
;
947 ek
.sname
= rep
.ticket
.sname
;
949 _kdc_log_timestamp(context
, config
, "TGS-REQ", et
.authtime
, et
.starttime
,
950 et
.endtime
, et
.renew_till
);
952 /* Don't sign cross realm tickets, they can't be checked anyway */
954 char *r
= get_krbtgt_realm(&ek
.sname
);
956 if (r
== NULL
|| strcmp(r
, ek
.srealm
) == 0) {
957 ret
= _kdc_add_KRB5SignedPath(context
,
970 if (enc_pa_data
->len
) {
971 rep
.padata
= calloc(1, sizeof(*rep
.padata
));
972 if (rep
.padata
== NULL
) {
976 ret
= copy_METHOD_DATA(enc_pa_data
, rep
.padata
);
981 if (krb5_enctype_valid(context
, serverkey
->keytype
) != 0
982 && _kdc_is_weak_exception(server
->entry
.principal
, serverkey
->keytype
))
984 krb5_enctype_enable(context
, serverkey
->keytype
);
989 /* It is somewhat unclear where the etype in the following
990 encryption should come from. What we have is a session
991 key in the passed tgt, and a list of preferred etypes
992 *for the new ticket*. Should we pick the best possible
993 etype, given the keytype in the tgt, or should we look
994 at the etype list here as well? What if the tgt
995 session key is DES3 and we want a ticket with a (say)
996 CAST session key. Should the DES3 etype be added to the
997 etype list, even if we don't want a session key with
999 ret
= _kdc_encode_reply(context
, config
, NULL
, 0,
1000 &rep
, &et
, &ek
, serverkey
->keytype
,
1002 serverkey
, 0, replykey
, rk_is_subkey
,
1005 krb5_enctype_disable(context
, serverkey
->keytype
);
1009 free_TransitedEncoding(&et
.transited
);
1013 free(et
.renew_till
);
1014 if(et
.authorization_data
) {
1015 free_AuthorizationData(et
.authorization_data
);
1016 free(et
.authorization_data
);
1018 free_LastReq(&ek
.last_req
);
1019 memset(et
.key
.keyvalue
.data
, 0, et
.key
.keyvalue
.length
);
1020 free_EncryptionKey(&et
.key
);
1024 static krb5_error_code
1025 tgs_check_authenticator(krb5_context context
,
1026 krb5_kdc_configuration
*config
,
1027 krb5_auth_context ac
,
1029 const char **e_text
,
1032 krb5_authenticator auth
;
1036 krb5_error_code ret
;
1039 krb5_auth_con_getauthenticator(context
, ac
, &auth
);
1040 if(auth
->cksum
== NULL
){
1041 kdc_log(context
, config
, 0, "No authenticator in request");
1042 ret
= KRB5KRB_AP_ERR_INAPP_CKSUM
;
1046 * according to RFC1510 it doesn't need to be keyed,
1047 * but according to the latest draft it needs to.
1051 !krb5_checksum_is_keyed(context
, auth
->cksum
->cksumtype
)
1054 !krb5_checksum_is_collision_proof(context
, auth
->cksum
->cksumtype
)) {
1055 kdc_log(context
, config
, 0, "Bad checksum type in authenticator: %d",
1056 auth
->cksum
->cksumtype
);
1057 ret
= KRB5KRB_AP_ERR_INAPP_CKSUM
;
1061 /* XXX should not re-encode this */
1062 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, b
, &len
, ret
);
1064 const char *msg
= krb5_get_error_message(context
, ret
);
1065 kdc_log(context
, config
, 0, "Failed to encode KDC-REQ-BODY: %s", msg
);
1066 krb5_free_error_message(context
, msg
);
1069 if(buf_size
!= len
) {
1071 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
1072 *e_text
= "KDC internal error";
1073 ret
= KRB5KRB_ERR_GENERIC
;
1076 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
1078 const char *msg
= krb5_get_error_message(context
, ret
);
1080 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1081 krb5_free_error_message(context
, msg
);
1084 ret
= krb5_verify_checksum(context
,
1086 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
1091 krb5_crypto_destroy(context
, crypto
);
1093 const char *msg
= krb5_get_error_message(context
, ret
);
1094 kdc_log(context
, config
, 0,
1095 "Failed to verify authenticator checksum: %s", msg
);
1096 krb5_free_error_message(context
, msg
);
1099 free_Authenticator(auth
);
1105 need_referral(krb5_context context
, krb5_kdc_configuration
*config
,
1106 const KDCOptions
* const options
, krb5_principal server
,
1107 krb5_realm
**realms
)
1111 if(!options
->canonicalize
&& server
->name
.name_type
!= KRB5_NT_SRV_INST
)
1114 if (server
->name
.name_string
.len
== 1)
1115 name
= server
->name
.name_string
.val
[0];
1116 else if (server
->name
.name_string
.len
== 3) {
1118 This is used to give referrals for the
1119 E3514235-4B06-11D1-AB04-00C04FC2DCD2/NTDSGUID/DNSDOMAIN
1120 SPN form, which is used for inter-domain communication in AD
1122 name
= server
->name
.name_string
.val
[2];
1123 kdc_log(context
, config
, 0, "Giving 3 part referral for %s", name
);
1124 *realms
= malloc(sizeof(char *)*2);
1125 if (*realms
== NULL
) {
1126 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
1129 (*realms
)[0] = strdup(name
);
1130 (*realms
)[1] = NULL
;
1132 } else if (server
->name
.name_string
.len
> 1)
1133 name
= server
->name
.name_string
.val
[1];
1137 kdc_log(context
, config
, 0, "Searching referral for %s", name
);
1139 return _krb5_get_host_realm_int(context
, name
, FALSE
, realms
) == 0;
1142 static krb5_error_code
1143 tgs_parse_request(krb5_context context
,
1144 krb5_kdc_configuration
*config
,
1146 const PA_DATA
*tgs_req
,
1147 hdb_entry_ex
**krbtgt
,
1148 krb5_enctype
*krbtgt_etype
,
1149 krb5_ticket
**ticket
,
1150 const char **e_text
,
1152 const struct sockaddr
*from_addr
,
1155 AuthorizationData
**auth_data
,
1156 krb5_keyblock
**replykey
,
1159 static char failed
[] = "<unparse_name failed>";
1161 krb5_error_code ret
;
1162 krb5_principal princ
;
1163 krb5_auth_context ac
= NULL
;
1164 krb5_flags ap_req_options
;
1165 krb5_flags verify_ap_req_flags
;
1167 krb5uint32 krbtgt_kvno
; /* kvno used for the PA-TGS-REQ AP-REQ Ticket */
1168 krb5uint32 krbtgt_kvno_try
;
1169 int kvno_search_tries
= 4; /* number of kvnos to try when tkt_vno == 0 */
1170 const Keys
*krbtgt_keys
;/* keyset for TGT tkt_vno */
1172 krb5_keyblock
*subkey
= NULL
;
1180 memset(&ap_req
, 0, sizeof(ap_req
));
1181 ret
= krb5_decode_ap_req(context
, &tgs_req
->padata_value
, &ap_req
);
1183 const char *msg
= krb5_get_error_message(context
, ret
);
1184 kdc_log(context
, config
, 0, "Failed to decode AP-REQ: %s", msg
);
1185 krb5_free_error_message(context
, msg
);
1189 if(!get_krbtgt_realm(&ap_req
.ticket
.sname
)){
1190 /* XXX check for ticket.sname == req.sname */
1191 kdc_log(context
, config
, 0, "PA-DATA is not a ticket-granting ticket");
1192 ret
= KRB5KDC_ERR_POLICY
; /* ? */
1196 _krb5_principalname2krb5_principal(context
,
1198 ap_req
.ticket
.sname
,
1199 ap_req
.ticket
.realm
);
1201 krbtgt_kvno
= ap_req
.ticket
.enc_part
.kvno
? *ap_req
.ticket
.enc_part
.kvno
: 0;
1202 ret
= _kdc_db_fetch(context
, config
, princ
, HDB_F_GET_KRBTGT
,
1203 &krbtgt_kvno
, NULL
, krbtgt
);
1205 if (ret
== HDB_ERR_NOT_FOUND_HERE
) {
1206 /* XXX Factor out this unparsing of the same princ all over */
1208 ret
= krb5_unparse_name(context
, princ
, &p
);
1211 krb5_free_principal(context
, princ
);
1212 kdc_log(context
, config
, 5,
1213 "Ticket-granting ticket account %s does not have secrets at "
1214 "this KDC, need to proxy", p
);
1217 ret
= HDB_ERR_NOT_FOUND_HERE
;
1219 } else if (ret
== HDB_ERR_KVNO_NOT_FOUND
) {
1221 ret
= krb5_unparse_name(context
, princ
, &p
);
1224 krb5_free_principal(context
, princ
);
1225 kdc_log(context
, config
, 5,
1226 "Ticket-granting ticket account %s does not have keys for "
1227 "kvno %d at this KDC", p
, krbtgt_kvno
);
1230 ret
= HDB_ERR_KVNO_NOT_FOUND
;
1232 } else if (ret
== HDB_ERR_NO_MKEY
) {
1234 ret
= krb5_unparse_name(context
, princ
, &p
);
1237 krb5_free_principal(context
, princ
);
1238 kdc_log(context
, config
, 5,
1239 "Missing master key for decrypting keys for ticket-granting "
1240 "ticket account %s with kvno %d at this KDC", p
, krbtgt_kvno
);
1243 ret
= HDB_ERR_KVNO_NOT_FOUND
;
1246 const char *msg
= krb5_get_error_message(context
, ret
);
1248 ret
= krb5_unparse_name(context
, princ
, &p
);
1251 kdc_log(context
, config
, 0,
1252 "Ticket-granting ticket %s not found in database: %s", p
, msg
);
1253 krb5_free_principal(context
, princ
);
1254 krb5_free_error_message(context
, msg
);
1257 ret
= KRB5KRB_AP_ERR_NOT_US
;
1261 krbtgt_kvno_try
= krbtgt_kvno
? krbtgt_kvno
: (*krbtgt
)->entry
.kvno
;
1262 *krbtgt_etype
= ap_req
.ticket
.enc_part
.etype
;
1265 krbtgt_keys
= hdb_kvno2keys(context
, &(*krbtgt
)->entry
, krbtgt_kvno_try
);
1266 ret
= hdb_enctype2key(context
, &(*krbtgt
)->entry
, krbtgt_keys
,
1267 ap_req
.ticket
.enc_part
.etype
, &tkey
);
1268 if (ret
&& krbtgt_kvno
== 0 && kvno_search_tries
> 0) {
1269 kvno_search_tries
--;
1273 char *str
= NULL
, *p
= NULL
;
1275 krb5_enctype_to_string(context
, ap_req
.ticket
.enc_part
.etype
, &str
);
1276 krb5_unparse_name(context
, princ
, &p
);
1277 kdc_log(context
, config
, 0,
1278 "No server key with enctype %s found for %s",
1279 str
? str
: "<unknown enctype>",
1280 p
? p
: "<unparse_name failed>");
1283 ret
= KRB5KRB_AP_ERR_BADKEYVER
;
1287 if (b
->kdc_options
.validate
)
1288 verify_ap_req_flags
= KRB5_VERIFY_AP_REQ_IGNORE_INVALID
;
1290 verify_ap_req_flags
= 0;
1292 ret
= krb5_verify_ap_req2(context
,
1297 verify_ap_req_flags
,
1300 KRB5_KU_TGS_REQ_AUTH
);
1301 if (ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
&& kvno_search_tries
> 0) {
1302 kvno_search_tries
--;
1307 krb5_free_principal(context
, princ
);
1309 const char *msg
= krb5_get_error_message(context
, ret
);
1310 kdc_log(context
, config
, 0, "Failed to verify AP-REQ: %s", msg
);
1311 krb5_free_error_message(context
, msg
);
1316 krb5_authenticator auth
;
1318 ret
= krb5_auth_con_getauthenticator(context
, ac
, &auth
);
1320 *csec
= malloc(sizeof(**csec
));
1321 if (*csec
== NULL
) {
1322 krb5_free_authenticator(context
, &auth
);
1323 kdc_log(context
, config
, 0, "malloc failed");
1326 **csec
= auth
->ctime
;
1327 *cusec
= malloc(sizeof(**cusec
));
1328 if (*cusec
== NULL
) {
1329 krb5_free_authenticator(context
, &auth
);
1330 kdc_log(context
, config
, 0, "malloc failed");
1333 **cusec
= auth
->cusec
;
1334 krb5_free_authenticator(context
, &auth
);
1338 ret
= tgs_check_authenticator(context
, config
,
1339 ac
, b
, e_text
, &(*ticket
)->ticket
.key
);
1341 krb5_auth_con_free(context
, ac
);
1345 usage
= KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
;
1348 ret
= krb5_auth_con_getremotesubkey(context
, ac
, &subkey
);
1350 const char *msg
= krb5_get_error_message(context
, ret
);
1351 krb5_auth_con_free(context
, ac
);
1352 kdc_log(context
, config
, 0, "Failed to get remote subkey: %s", msg
);
1353 krb5_free_error_message(context
, msg
);
1357 usage
= KRB5_KU_TGS_REQ_AUTH_DAT_SESSION
;
1360 ret
= krb5_auth_con_getkey(context
, ac
, &subkey
);
1362 const char *msg
= krb5_get_error_message(context
, ret
);
1363 krb5_auth_con_free(context
, ac
);
1364 kdc_log(context
, config
, 0, "Failed to get session key: %s", msg
);
1365 krb5_free_error_message(context
, msg
);
1370 krb5_auth_con_free(context
, ac
);
1371 kdc_log(context
, config
, 0,
1372 "Failed to get key for enc-authorization-data");
1373 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1379 if (b
->enc_authorization_data
) {
1382 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
1384 const char *msg
= krb5_get_error_message(context
, ret
);
1385 krb5_auth_con_free(context
, ac
);
1386 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1387 krb5_free_error_message(context
, msg
);
1390 ret
= krb5_decrypt_EncryptedData (context
,
1393 b
->enc_authorization_data
,
1395 krb5_crypto_destroy(context
, crypto
);
1397 krb5_auth_con_free(context
, ac
);
1398 kdc_log(context
, config
, 0,
1399 "Failed to decrypt enc-authorization-data");
1400 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1404 if (*auth_data
== NULL
) {
1405 krb5_auth_con_free(context
, ac
);
1406 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1409 ret
= decode_AuthorizationData(ad
.data
, ad
.length
, *auth_data
, NULL
);
1411 krb5_auth_con_free(context
, ac
);
1414 kdc_log(context
, config
, 0, "Failed to decode authorization data");
1415 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1420 krb5_auth_con_free(context
, ac
);
1423 free_AP_REQ(&ap_req
);
1428 static krb5_error_code
1429 build_server_referral(krb5_context context
,
1430 krb5_kdc_configuration
*config
,
1431 krb5_crypto session
,
1432 krb5_const_realm referred_realm
,
1433 const PrincipalName
*true_principal_name
,
1434 const PrincipalName
*requested_principal
,
1437 PA_ServerReferralData ref
;
1438 krb5_error_code ret
;
1443 memset(&ref
, 0, sizeof(ref
));
1445 if (referred_realm
) {
1446 ALLOC(ref
.referred_realm
);
1447 if (ref
.referred_realm
== NULL
)
1449 *ref
.referred_realm
= strdup(referred_realm
);
1450 if (*ref
.referred_realm
== NULL
)
1453 if (true_principal_name
) {
1454 ALLOC(ref
.true_principal_name
);
1455 if (ref
.true_principal_name
== NULL
)
1457 ret
= copy_PrincipalName(true_principal_name
, ref
.true_principal_name
);
1461 if (requested_principal
) {
1462 ALLOC(ref
.requested_principal_name
);
1463 if (ref
.requested_principal_name
== NULL
)
1465 ret
= copy_PrincipalName(requested_principal
,
1466 ref
.requested_principal_name
);
1471 ASN1_MALLOC_ENCODE(PA_ServerReferralData
,
1472 data
.data
, data
.length
,
1474 free_PA_ServerReferralData(&ref
);
1477 if (data
.length
!= size
)
1478 krb5_abortx(context
, "internal asn.1 encoder error");
1480 ret
= krb5_encrypt_EncryptedData(context
, session
,
1481 KRB5_KU_PA_SERVER_REFERRAL
,
1482 data
.data
, data
.length
,
1488 ASN1_MALLOC_ENCODE(EncryptedData
,
1489 outdata
->data
, outdata
->length
,
1491 free_EncryptedData(&ed
);
1494 if (outdata
->length
!= size
)
1495 krb5_abortx(context
, "internal asn.1 encoder error");
1499 free_PA_ServerReferralData(&ref
);
1500 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1504 static krb5_error_code
1505 tgs_build_reply(krb5_context context
,
1506 krb5_kdc_configuration
*config
,
1509 hdb_entry_ex
*krbtgt
,
1510 krb5_enctype krbtgt_etype
,
1511 const krb5_keyblock
*replykey
,
1513 krb5_ticket
*ticket
,
1516 const char **e_text
,
1517 AuthorizationData
**auth_data
,
1518 const struct sockaddr
*from_addr
)
1520 krb5_error_code ret
, ret2
;
1521 krb5_principal cp
= NULL
, sp
= NULL
, rsp
= NULL
, tp
= NULL
, dp
= NULL
;
1522 krb5_principal krbtgt_out_principal
= NULL
;
1523 char *spn
= NULL
, *cpn
= NULL
, *tpn
= NULL
, *dpn
= NULL
, *krbtgt_out_n
= NULL
;
1524 hdb_entry_ex
*server
= NULL
, *client
= NULL
, *s4u2self_impersonated_client
= NULL
;
1525 HDB
*clientdb
, *s4u2self_impersonated_clientdb
;
1526 krb5_realm ref_realm
= NULL
;
1527 EncTicketPart
*tgt
= &ticket
->ticket
;
1528 krb5_principals spp
= NULL
;
1529 const EncryptionKey
*ekey
;
1530 krb5_keyblock sessionkey
;
1533 const char *tgt_realm
= /* Realm of TGT issuer */
1534 krb5_principal_get_realm(context
, krbtgt
->entry
.principal
);
1535 const char *our_realm
= /* Realm of this KDC */
1536 krb5_principal_get_comp_string(context
, krbtgt
->entry
.principal
, 1);
1537 char **capath
= NULL
;
1538 size_t num_capath
= 0;
1540 hdb_entry_ex
*krbtgt_out
= NULL
;
1542 METHOD_DATA enc_pa_data
;
1546 EncTicketPart adtkt
;
1552 int flags
= HDB_F_FOR_TGS_REQ
;
1554 memset(&sessionkey
, 0, sizeof(sessionkey
));
1555 memset(&adtkt
, 0, sizeof(adtkt
));
1556 krb5_data_zero(&rspac
);
1557 memset(&enc_pa_data
, 0, sizeof(enc_pa_data
));
1563 * Always to do CANON, see comment below about returned server principal (rsp).
1565 flags
|= HDB_F_CANON
;
1567 if(b
->kdc_options
.enc_tkt_in_skey
){
1572 krb5uint32 second_kvno
= 0;
1573 krb5uint32
*kvno_ptr
= NULL
;
1575 if(b
->additional_tickets
== NULL
||
1576 b
->additional_tickets
->len
== 0){
1577 ret
= KRB5KDC_ERR_BADOPTION
; /* ? */
1578 kdc_log(context
, config
, 0,
1579 "No second ticket present in request");
1582 t
= &b
->additional_tickets
->val
[0];
1583 if(!get_krbtgt_realm(&t
->sname
)){
1584 kdc_log(context
, config
, 0,
1585 "Additional ticket is not a ticket-granting ticket");
1586 ret
= KRB5KDC_ERR_POLICY
;
1589 _krb5_principalname2krb5_principal(context
, &p
, t
->sname
, t
->realm
);
1590 if(t
->enc_part
.kvno
){
1591 second_kvno
= *t
->enc_part
.kvno
;
1592 kvno_ptr
= &second_kvno
;
1594 ret
= _kdc_db_fetch(context
, config
, p
,
1595 HDB_F_GET_KRBTGT
, kvno_ptr
,
1597 krb5_free_principal(context
, p
);
1599 if (ret
== HDB_ERR_NOENTRY
)
1600 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1603 ret
= hdb_enctype2key(context
, &uu
->entry
, NULL
,
1604 t
->enc_part
.etype
, &uukey
);
1606 _kdc_free_ent(context
, uu
);
1607 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
; /* XXX */
1610 ret
= krb5_decrypt_ticket(context
, t
, &uukey
->key
, &adtkt
, 0);
1611 _kdc_free_ent(context
, uu
);
1615 ret
= verify_flags(context
, config
, &adtkt
, spn
);
1623 _krb5_principalname2krb5_principal(context
, &sp
, *s
, r
);
1624 ret
= krb5_unparse_name(context
, sp
, &spn
);
1627 _krb5_principalname2krb5_principal(context
, &cp
, tgt
->cname
, tgt
->crealm
);
1628 ret
= krb5_unparse_name(context
, cp
, &cpn
);
1631 unparse_flags (KDCOptions2int(b
->kdc_options
),
1632 asn1_KDCOptions_units(),
1633 opt_str
, sizeof(opt_str
));
1635 kdc_log(context
, config
, 0,
1636 "TGS-REQ %s from %s for %s [%s]",
1637 cpn
, from
, spn
, opt_str
);
1639 kdc_log(context
, config
, 0,
1640 "TGS-REQ %s from %s for %s", cpn
, from
, spn
);
1647 ret
= _kdc_db_fetch(context
, config
, sp
, HDB_F_GET_SERVER
| flags
,
1648 NULL
, NULL
, &server
);
1650 if (ret
== HDB_ERR_NOT_FOUND_HERE
) {
1651 kdc_log(context
, config
, 5, "target %s does not have secrets at this KDC, need to proxy", spn
);
1653 } else if (ret
== HDB_ERR_WRONG_REALM
) {
1655 ref_realm
= strdup(server
->entry
.principal
->realm
);
1656 if (ref_realm
== NULL
) {
1657 ret
= krb5_enomem(context
);
1661 kdc_log(context
, config
, 5,
1662 "Returning a referral to realm %s for "
1665 krb5_free_principal(context
, sp
);
1667 ret
= krb5_make_principal(context
, &sp
, r
, KRB5_TGS_NAME
,
1673 ret
= krb5_unparse_name(context
, sp
, &spn
);
1679 const char *new_rlm
, *msg
;
1683 if ((req_rlm
= get_krbtgt_realm(&sp
->name
)) != NULL
) {
1684 if (capath
== NULL
) {
1685 /* With referalls, hierarchical capaths are always enabled */
1686 ret2
= _krb5_find_capath(context
, tgt
->crealm
, our_realm
,
1687 req_rlm
, TRUE
, &capath
, &num_capath
);
1693 new_rlm
= num_capath
> 0 ? capath
[--num_capath
] : NULL
;
1695 kdc_log(context
, config
, 5, "krbtgt from %s via %s for "
1696 "realm %s not found, trying %s", tgt
->crealm
,
1697 our_realm
, req_rlm
, new_rlm
);
1700 ref_realm
= strdup(new_rlm
);
1701 if (ref_realm
== NULL
) {
1702 ret
= krb5_enomem(context
);
1706 krb5_free_principal(context
, sp
);
1708 krb5_make_principal(context
, &sp
, r
,
1709 KRB5_TGS_NAME
, ref_realm
, NULL
);
1712 ret
= krb5_unparse_name(context
, sp
, &spn
);
1717 } else if (need_referral(context
, config
, &b
->kdc_options
, sp
, &realms
)) {
1718 if (strcmp(realms
[0], sp
->realm
) != 0) {
1719 kdc_log(context
, config
, 5,
1720 "Returning a referral to realm %s for "
1721 "server %s that was not found",
1723 krb5_free_principal(context
, sp
);
1725 krb5_make_principal(context
, &sp
, r
, KRB5_TGS_NAME
,
1729 ret
= krb5_unparse_name(context
, sp
, &spn
);
1731 krb5_free_host_realm(context
, realms
);
1736 ref_realm
= strdup(realms
[0]);
1738 krb5_free_host_realm(context
, realms
);
1741 krb5_free_host_realm(context
, realms
);
1743 msg
= krb5_get_error_message(context
, ret
);
1744 kdc_log(context
, config
, 0,
1745 "Server not found in database: %s: %s", spn
, msg
);
1746 krb5_free_error_message(context
, msg
);
1747 if (ret
== HDB_ERR_NOENTRY
)
1748 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1752 /* the name returned to the client depend on what was asked for,
1753 * return canonical name if kdc_options.canonicalize was set, the
1754 * client wants the true name of the principal, if not it just
1755 * wants the name its asked for.
1758 if (b
->kdc_options
.canonicalize
)
1759 rsp
= server
->entry
.principal
;
1765 * Select enctype, return key and kvno.
1771 if(b
->kdc_options
.enc_tkt_in_skey
) {
1774 for(i
= 0; i
< b
->etype
.len
; i
++)
1775 if (b
->etype
.val
[i
] == adtkt
.key
.keytype
)
1777 if(i
== b
->etype
.len
) {
1778 kdc_log(context
, config
, 0,
1779 "Addition ticket have not matching etypes");
1780 krb5_clear_error_message(context
);
1781 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
1784 etype
= b
->etype
.val
[i
];
1789 ret
= _kdc_find_etype(context
,
1790 krb5_principal_is_krbtgt(context
, sp
) ?
1791 config
->tgt_use_strongest_session_key
:
1792 config
->svc_use_strongest_session_key
, FALSE
,
1793 server
, b
->etype
.val
, b
->etype
.len
, &etype
,
1796 kdc_log(context
, config
, 0,
1797 "Server (%s) has no support for etypes", spn
);
1800 ret
= _kdc_get_preferred_key(context
, config
, server
, spn
,
1803 kdc_log(context
, config
, 0,
1804 "Server (%s) has no supported etypes", spn
);
1808 kvno
= server
->entry
.kvno
;
1811 ret
= krb5_generate_random_keyblock(context
, etype
, &sessionkey
);
1817 * Check that service is in the same realm as the krbtgt. If it's
1818 * not the same, it's someone that is using a uni-directional trust
1823 * Validate authoriation data
1826 ret
= hdb_enctype2key(context
, &krbtgt
->entry
, NULL
, /* XXX use the right kvno! */
1827 krbtgt_etype
, &tkey_check
);
1829 kdc_log(context
, config
, 0,
1830 "Failed to find key for krbtgt PAC check");
1835 * Now refetch the primary krbtgt, and get the current kvno (the
1836 * sign check may have been on an old kvno, and the server may
1837 * have been an incoming trust)
1840 ret
= krb5_make_principal(context
,
1841 &krbtgt_out_principal
,
1847 kdc_log(context
, config
, 0,
1848 "Failed to make krbtgt principal name object for "
1849 "authz-data signatures");
1852 ret
= krb5_unparse_name(context
, krbtgt_out_principal
, &krbtgt_out_n
);
1854 kdc_log(context
, config
, 0,
1855 "Failed to make krbtgt principal name object for "
1856 "authz-data signatures");
1860 ret
= _kdc_db_fetch(context
, config
, krbtgt_out_principal
,
1861 HDB_F_GET_KRBTGT
, NULL
, NULL
, &krbtgt_out
);
1864 ret
= krb5_unparse_name(context
, krbtgt
->entry
.principal
, &ktpn
);
1865 kdc_log(context
, config
, 0,
1866 "No such principal %s (needed for authz-data signature keys) "
1867 "while processing TGS-REQ for service %s with krbtg %s",
1868 krbtgt_out_n
, spn
, (ret
== 0) ? ktpn
: "<unknown>");
1870 ret
= KRB5KRB_AP_ERR_NOT_US
;
1875 * The first realm is the realm of the service, the second is
1876 * krbtgt/<this>/@REALM component of the krbtgt DN the request was
1877 * encrypted to. The redirection via the krbtgt_out entry allows
1878 * the DB to possibly correct the case of the realm (Samba4 does
1879 * this) before the strcmp()
1881 if (strcmp(krb5_principal_get_realm(context
, server
->entry
.principal
),
1882 krb5_principal_get_realm(context
, krbtgt_out
->entry
.principal
)) != 0) {
1884 ret
= krb5_unparse_name(context
, krbtgt_out
->entry
.principal
, &ktpn
);
1885 kdc_log(context
, config
, 0,
1886 "Request with wrong krbtgt: %s",
1887 (ret
== 0) ? ktpn
: "<unknown>");
1890 ret
= KRB5KRB_AP_ERR_NOT_US
;
1894 ret
= _kdc_get_preferred_key(context
, config
, krbtgt_out
, krbtgt_out_n
,
1897 kdc_log(context
, config
, 0,
1898 "Failed to find key for krbtgt PAC signature");
1901 ret
= hdb_enctype2key(context
, &krbtgt_out
->entry
, NULL
,
1902 tkey_sign
->key
.keytype
, &tkey_sign
);
1904 kdc_log(context
, config
, 0,
1905 "Failed to find key for krbtgt PAC signature");
1909 ret
= _kdc_db_fetch(context
, config
, cp
, HDB_F_GET_CLIENT
| flags
,
1910 NULL
, &clientdb
, &client
);
1911 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1912 /* This is OK, we are just trying to find out if they have
1913 * been disabled or deleted in the meantime, missing secrets
1916 const char *krbtgt_realm
, *msg
;
1919 * If the client belongs to the same realm as our krbtgt, it
1920 * should exist in the local database.
1924 krbtgt_realm
= krb5_principal_get_realm(context
, krbtgt_out
->entry
.principal
);
1926 if(strcmp(krb5_principal_get_realm(context
, cp
), krbtgt_realm
) == 0) {
1927 if (ret
== HDB_ERR_NOENTRY
)
1928 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1929 kdc_log(context
, config
, 1, "Client no longer in database: %s",
1934 msg
= krb5_get_error_message(context
, ret
);
1935 kdc_log(context
, config
, 1, "Client not found in database: %s", msg
);
1936 krb5_free_error_message(context
, msg
);
1939 ret
= check_PAC(context
, config
, cp
, NULL
,
1940 client
, server
, krbtgt
,
1942 ekey
, &tkey_sign
->key
,
1943 tgt
, &rspac
, &signedpath
);
1945 const char *msg
= krb5_get_error_message(context
, ret
);
1946 kdc_log(context
, config
, 0,
1947 "Verify PAC failed for %s (%s) from %s with %s",
1948 spn
, cpn
, from
, msg
);
1949 krb5_free_error_message(context
, msg
);
1953 /* also check the krbtgt for signature */
1954 ret
= check_KRB5SignedPath(context
,
1962 const char *msg
= krb5_get_error_message(context
, ret
);
1963 kdc_log(context
, config
, 0,
1964 "KRB5SignedPath check failed for %s (%s) from %s with %s",
1965 spn
, cpn
, from
, msg
);
1966 krb5_free_error_message(context
, msg
);
1974 /* by default the tgt principal matches the client principal */
1979 const PA_DATA
*sdata
;
1982 sdata
= _kdc_find_padata(req
, &i
, KRB5_PADATA_FOR_USER
);
1989 ret
= decode_PA_S4U2Self(sdata
->padata_value
.data
,
1990 sdata
->padata_value
.length
,
1993 kdc_log(context
, config
, 0, "Failed to decode PA-S4U2Self");
1997 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &datack
);
2001 ret
= krb5_crypto_init(context
, &tgt
->key
, 0, &crypto
);
2003 const char *msg
= krb5_get_error_message(context
, ret
);
2004 free_PA_S4U2Self(&self
);
2005 krb5_data_free(&datack
);
2006 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
2007 krb5_free_error_message(context
, msg
);
2011 ret
= krb5_verify_checksum(context
,
2013 KRB5_KU_OTHER_CKSUM
,
2017 krb5_data_free(&datack
);
2018 krb5_crypto_destroy(context
, crypto
);
2020 const char *msg
= krb5_get_error_message(context
, ret
);
2021 free_PA_S4U2Self(&self
);
2022 kdc_log(context
, config
, 0,
2023 "krb5_verify_checksum failed for S4U2Self: %s", msg
);
2024 krb5_free_error_message(context
, msg
);
2028 ret
= _krb5_principalname2krb5_principal(context
,
2032 free_PA_S4U2Self(&self
);
2036 ret
= krb5_unparse_name(context
, tp
, &tpn
);
2040 /* If we were about to put a PAC into the ticket, we better fix it to be the right PAC */
2043 krb5_data_free(&rspac
);
2044 ret
= _kdc_db_fetch(context
, config
, tp
, HDB_F_GET_CLIENT
| flags
,
2045 NULL
, &s4u2self_impersonated_clientdb
, &s4u2self_impersonated_client
);
2050 * If the client belongs to the same realm as our krbtgt, it
2051 * should exist in the local database.
2055 if (ret
== HDB_ERR_NOENTRY
)
2056 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
2057 msg
= krb5_get_error_message(context
, ret
);
2058 kdc_log(context
, config
, 1,
2059 "S4U2Self principal to impersonate %s not found in database: %s",
2061 krb5_free_error_message(context
, msg
);
2064 ret
= _kdc_pac_generate(context
, s4u2self_impersonated_client
, &p
);
2066 kdc_log(context
, config
, 0, "PAC generation failed for -- %s",
2071 ret
= _krb5_pac_sign(context
, p
, ticket
->ticket
.authtime
,
2072 s4u2self_impersonated_client
->entry
.principal
,
2073 ekey
, &tkey_sign
->key
,
2075 krb5_pac_free(context
, p
);
2077 kdc_log(context
, config
, 0, "PAC signing failed for -- %s",
2085 * Check that service doing the impersonating is
2086 * requesting a ticket to it-self.
2088 ret
= check_s4u2self(context
, config
, clientdb
, client
, sp
);
2090 kdc_log(context
, config
, 0, "S4U2Self: %s is not allowed "
2091 "to impersonate to service "
2092 "(tried for user %s to service %s)",
2098 * If the service isn't trusted for authentication to
2099 * delegation, remove the forward flag.
2102 if (client
->entry
.flags
.trusted_for_delegation
) {
2103 str
= "[forwardable]";
2105 b
->kdc_options
.forwardable
= 0;
2108 kdc_log(context
, config
, 0, "s4u2self %s impersonating %s to "
2109 "service %s %s", cpn
, tpn
, spn
, str
);
2114 * Constrained delegation
2118 && b
->additional_tickets
!= NULL
2119 && b
->additional_tickets
->len
!= 0
2120 && b
->kdc_options
.enc_tkt_in_skey
== 0)
2122 int ad_signedpath
= 0;
2127 * Require that the KDC have issued the service's krbtgt (not
2128 * self-issued ticket with kimpersonate(1).
2131 ret
= KRB5KDC_ERR_BADOPTION
;
2132 kdc_log(context
, config
, 0,
2133 "Constrained delegation done on service ticket %s/%s",
2138 t
= &b
->additional_tickets
->val
[0];
2140 ret
= hdb_enctype2key(context
, &client
->entry
,
2141 hdb_kvno2keys(context
, &client
->entry
,
2142 t
->enc_part
.kvno
? * t
->enc_part
.kvno
: 0),
2143 t
->enc_part
.etype
, &clientkey
);
2145 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
; /* XXX */
2149 ret
= krb5_decrypt_ticket(context
, t
, &clientkey
->key
, &adtkt
, 0);
2151 kdc_log(context
, config
, 0,
2152 "failed to decrypt ticket for "
2153 "constrained delegation from %s to %s ", cpn
, spn
);
2157 ret
= _krb5_principalname2krb5_principal(context
,
2164 ret
= krb5_unparse_name(context
, tp
, &tpn
);
2168 ret
= _krb5_principalname2krb5_principal(context
,
2175 ret
= krb5_unparse_name(context
, dp
, &dpn
);
2179 /* check that ticket is valid */
2180 if (adtkt
.flags
.forwardable
== 0) {
2181 kdc_log(context
, config
, 0,
2182 "Missing forwardable flag on ticket for "
2183 "constrained delegation from %s (%s) as %s to %s ",
2184 cpn
, dpn
, tpn
, spn
);
2185 ret
= KRB5KDC_ERR_BADOPTION
;
2189 ret
= check_constrained_delegation(context
, config
, clientdb
,
2190 client
, server
, sp
);
2192 kdc_log(context
, config
, 0,
2193 "constrained delegation from %s (%s) as %s to %s not allowed",
2194 cpn
, dpn
, tpn
, spn
);
2198 ret
= verify_flags(context
, config
, &adtkt
, tpn
);
2203 krb5_data_free(&rspac
);
2206 * generate the PAC for the user.
2208 * TODO: pass in t->sname and t->realm and build
2209 * a S4U_DELEGATION_INFO blob to the PAC.
2211 ret
= check_PAC(context
, config
, tp
, dp
,
2212 client
, server
, krbtgt
,
2214 ekey
, &tkey_sign
->key
,
2215 &adtkt
, &rspac
, &ad_signedpath
);
2217 const char *msg
= krb5_get_error_message(context
, ret
);
2218 kdc_log(context
, config
, 0,
2219 "Verify delegated PAC failed to %s for client"
2220 "%s (%s) as %s from %s with %s",
2221 spn
, cpn
, dpn
, tpn
, from
, msg
);
2222 krb5_free_error_message(context
, msg
);
2227 * Check that the KDC issued the user's ticket.
2229 ret
= check_KRB5SignedPath(context
,
2237 const char *msg
= krb5_get_error_message(context
, ret
);
2238 kdc_log(context
, config
, 0,
2239 "KRB5SignedPath check from service %s failed "
2240 "for delegation to %s for client %s (%s)"
2241 "from %s failed with %s",
2242 spn
, tpn
, dpn
, cpn
, from
, msg
);
2243 krb5_free_error_message(context
, msg
);
2247 if (!ad_signedpath
) {
2248 ret
= KRB5KDC_ERR_BADOPTION
;
2249 kdc_log(context
, config
, 0,
2250 "Ticket not signed with PAC nor SignedPath service %s failed "
2251 "for delegation to %s for client %s (%s)"
2253 spn
, tpn
, dpn
, cpn
, from
);
2257 kdc_log(context
, config
, 0, "constrained delegation for %s "
2258 "from %s (%s) to %s", tpn
, cpn
, dpn
, spn
);
2265 ret
= kdc_check_flags(context
, config
,
2272 if((b
->kdc_options
.validate
|| b
->kdc_options
.renew
) &&
2273 !krb5_principal_compare(context
,
2274 krbtgt
->entry
.principal
,
2275 server
->entry
.principal
)){
2276 kdc_log(context
, config
, 0, "Inconsistent request.");
2277 ret
= KRB5KDC_ERR_SERVER_NOMATCH
;
2281 /* check for valid set of addresses */
2282 if(!_kdc_check_addresses(context
, config
, tgt
->caddr
, from_addr
)) {
2283 ret
= KRB5KRB_AP_ERR_BADADDR
;
2284 kdc_log(context
, config
, 0, "Request from wrong address");
2289 * If this is an referral, add server referral data to the
2296 kdc_log(context
, config
, 0,
2297 "Adding server referral to %s", ref_realm
);
2299 ret
= krb5_crypto_init(context
, &sessionkey
, 0, &crypto
);
2303 ret
= build_server_referral(context
, config
, crypto
, ref_realm
,
2304 NULL
, s
, &pa
.padata_value
);
2305 krb5_crypto_destroy(context
, crypto
);
2307 kdc_log(context
, config
, 0,
2308 "Failed building server referral");
2311 pa
.padata_type
= KRB5_PADATA_SERVER_REFERRAL
;
2313 ret
= add_METHOD_DATA(&enc_pa_data
, &pa
);
2314 krb5_data_free(&pa
.padata_value
);
2316 kdc_log(context
, config
, 0,
2317 "Add server referral METHOD-DATA failed");
2326 ret
= tgs_make_reply(context
,
2344 tkey_sign
->key
.keytype
,
2358 _krb5_free_capath(context
, capath
);
2360 krb5_data_free(&rspac
);
2361 krb5_free_keyblock_contents(context
, &sessionkey
);
2363 _kdc_free_ent(context
, krbtgt_out
);
2365 _kdc_free_ent(context
, server
);
2367 _kdc_free_ent(context
, client
);
2368 if(s4u2self_impersonated_client
)
2369 _kdc_free_ent(context
, s4u2self_impersonated_client
);
2372 krb5_free_principal(context
, tp
);
2373 krb5_free_principal(context
, cp
);
2374 krb5_free_principal(context
, dp
);
2375 krb5_free_principal(context
, sp
);
2376 krb5_free_principal(context
, krbtgt_out_principal
);
2378 free_METHOD_DATA(&enc_pa_data
);
2380 free_EncTicketPart(&adtkt
);
2390 _kdc_tgs_rep(krb5_context context
,
2391 krb5_kdc_configuration
*config
,
2395 struct sockaddr
*from_addr
,
2398 AuthorizationData
*auth_data
= NULL
;
2399 krb5_error_code ret
;
2401 const PA_DATA
*tgs_req
;
2403 hdb_entry_ex
*krbtgt
= NULL
;
2404 krb5_ticket
*ticket
= NULL
;
2405 const char *e_text
= NULL
;
2406 krb5_enctype krbtgt_etype
= ETYPE_NULL
;
2408 krb5_keyblock
*replykey
= NULL
;
2409 int rk_is_subkey
= 0;
2410 time_t *csec
= NULL
;
2413 if(req
->padata
== NULL
){
2414 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
; /* XXX ??? */
2415 kdc_log(context
, config
, 0,
2416 "TGS-REQ from %s without PA-DATA", from
);
2420 tgs_req
= _kdc_find_padata(req
, &i
, KRB5_PADATA_TGS_REQ
);
2422 if(tgs_req
== NULL
){
2423 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
2425 kdc_log(context
, config
, 0,
2426 "TGS-REQ from %s without PA-TGS-REQ", from
);
2429 ret
= tgs_parse_request(context
, config
,
2430 &req
->req_body
, tgs_req
,
2440 if (ret
== HDB_ERR_NOT_FOUND_HERE
) {
2441 /* kdc_log() is called in tgs_parse_request() */
2445 kdc_log(context
, config
, 0,
2446 "Failed parsing TGS-REQ from %s", from
);
2451 const PA_DATA
*pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_FX_FAST
);
2453 kdc_log(context
, config
, 10, "Got TGS FAST request");
2457 ret
= tgs_build_reply(context
,
2472 kdc_log(context
, config
, 0,
2473 "Failed building TGS-REP to %s", from
);
2478 if (datagram_reply
&& data
->length
> config
->max_datagram_reply_length
) {
2479 krb5_data_free(data
);
2480 ret
= KRB5KRB_ERR_RESPONSE_TOO_BIG
;
2481 e_text
= "Reply packet too large";
2486 krb5_free_keyblock(context
, replykey
);
2488 if(ret
&& ret
!= HDB_ERR_NOT_FOUND_HERE
&& data
->data
== NULL
){
2489 /* XXX add fast wrapping on the error */
2490 METHOD_DATA error_method
= { 0, NULL
};
2493 kdc_log(context
, config
, 10, "tgs-req: sending error: %d to client", ret
);
2494 ret
= _kdc_fast_mk_error(context
, NULL
,
2503 free_METHOD_DATA(&error_method
);
2508 krb5_free_ticket(context
, ticket
);
2510 _kdc_free_ent(context
, krbtgt
);
2513 free_AuthorizationData(auth_data
);