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");
658 if(strcmp(client_realm
, tgt_realm
) && strcmp(server_realm
, tgt_realm
)) {
659 /* not us, so add the previous realm to transited set */
660 if (num_realms
+ 1 > UINT_MAX
/sizeof(*realms
)) {
664 tmp
= realloc(realms
, (num_realms
+ 1) * sizeof(*realms
));
670 realms
[num_realms
] = strdup(tgt_realm
);
671 if(realms
[num_realms
] == NULL
){
677 if(num_realms
== 0) {
678 if(strcmp(client_realm
, server_realm
))
679 kdc_log(context
, config
, 0,
680 "cross-realm %s -> %s", client_realm
, server_realm
);
684 for(i
= 0; i
< num_realms
; i
++)
685 l
+= strlen(realms
[i
]) + 2;
689 for(i
= 0; i
< num_realms
; i
++) {
691 strlcat(rs
, ", ", l
);
692 strlcat(rs
, realms
[i
], l
);
694 kdc_log(context
, config
, 0,
695 "cross-realm %s -> %s via [%s]",
696 client_realm
, server_realm
, rs
);
701 ret
= krb5_check_transited(context
, client_realm
,
703 realms
, num_realms
, NULL
);
705 krb5_warn(context
, ret
, "cross-realm %s -> %s",
706 client_realm
, server_realm
);
709 et
->flags
.transited_policy_checked
= 1;
711 et
->transited
.tr_type
= DOMAIN_X500_COMPRESS
;
712 ret
= krb5_domain_x500_encode(realms
, num_realms
, &et
->transited
.contents
);
714 krb5_warn(context
, ret
, "Encoding transited encoding");
716 for(i
= 0; i
< num_realms
; i
++)
723 static krb5_error_code
724 tgs_make_reply(krb5_context context
,
725 krb5_kdc_configuration
*config
,
727 krb5_const_principal tgt_name
,
728 const EncTicketPart
*tgt
,
729 const krb5_keyblock
*replykey
,
731 const EncryptionKey
*serverkey
,
732 const krb5_keyblock
*sessionkey
,
734 AuthorizationData
*auth_data
,
735 hdb_entry_ex
*server
,
736 krb5_principal server_principal
,
737 const char *server_name
,
738 hdb_entry_ex
*client
,
739 krb5_principal client_principal
,
740 hdb_entry_ex
*krbtgt
,
741 krb5_enctype krbtgt_etype
,
743 const krb5_data
*rspac
,
744 const METHOD_DATA
*enc_pa_data
,
751 KDCOptions f
= b
->kdc_options
;
755 memset(&rep
, 0, sizeof(rep
));
756 memset(&et
, 0, sizeof(et
));
757 memset(&ek
, 0, sizeof(ek
));
760 rep
.msg_type
= krb_tgs_rep
;
762 et
.authtime
= tgt
->authtime
;
763 _kdc_fix_time(&b
->till
);
764 et
.endtime
= min(tgt
->endtime
, *b
->till
);
766 *et
.starttime
= kdc_time
;
768 ret
= check_tgs_flags(context
, config
, b
, tgt
, &et
);
772 /* We should check the transited encoding if:
773 1) the request doesn't ask not to be checked
774 2) globally enforcing a check
775 3) principal requires checking
776 4) we allow non-check per-principal, but principal isn't marked as allowing this
777 5) we don't globally allow this
780 #define GLOBAL_FORCE_TRANSITED_CHECK \
781 (config->trpolicy == TRPOLICY_ALWAYS_CHECK)
782 #define GLOBAL_ALLOW_PER_PRINCIPAL \
783 (config->trpolicy == TRPOLICY_ALLOW_PER_PRINCIPAL)
784 #define GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK \
785 (config->trpolicy == TRPOLICY_ALWAYS_HONOUR_REQUEST)
787 /* these will consult the database in future release */
788 #define PRINCIPAL_FORCE_TRANSITED_CHECK(P) 0
789 #define PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(P) 0
791 ret
= fix_transited_encoding(context
, config
,
792 !f
.disable_transited_check
||
793 GLOBAL_FORCE_TRANSITED_CHECK
||
794 PRINCIPAL_FORCE_TRANSITED_CHECK(server
) ||
795 !((GLOBAL_ALLOW_PER_PRINCIPAL
&&
796 PRINCIPAL_ALLOW_DISABLE_TRANSITED_CHECK(server
)) ||
797 GLOBAL_ALLOW_DISABLE_TRANSITED_CHECK
),
798 &tgt
->transited
, &et
,
799 krb5_principal_get_realm(context
, client_principal
),
800 krb5_principal_get_realm(context
, server
->entry
.principal
),
801 krb5_principal_get_realm(context
, krbtgt
->entry
.principal
));
805 copy_Realm(&server_principal
->realm
, &rep
.ticket
.realm
);
806 _krb5_principal2principalname(&rep
.ticket
.sname
, server_principal
);
807 copy_Realm(&tgt_name
->realm
, &rep
.crealm
);
809 if (f.request_anonymous)
810 _kdc_make_anonymous_principalname (&rep.cname);
813 copy_PrincipalName(&tgt_name
->name
, &rep
.cname
);
814 rep
.ticket
.tkt_vno
= 5;
818 et
.caddr
= tgt
->caddr
;
822 life
= et
.endtime
- *et
.starttime
;
823 if(client
&& client
->entry
.max_life
)
824 life
= min(life
, *client
->entry
.max_life
);
825 if(server
->entry
.max_life
)
826 life
= min(life
, *server
->entry
.max_life
);
827 et
.endtime
= *et
.starttime
+ life
;
829 if(f
.renewable_ok
&& tgt
->flags
.renewable
&&
830 et
.renew_till
== NULL
&& et
.endtime
< *b
->till
&&
831 tgt
->renew_till
!= NULL
)
833 et
.flags
.renewable
= 1;
834 ALLOC(et
.renew_till
);
835 *et
.renew_till
= *b
->till
;
839 renew
= *et
.renew_till
- *et
.starttime
;
840 if(client
&& client
->entry
.max_renew
)
841 renew
= min(renew
, *client
->entry
.max_renew
);
842 if(server
->entry
.max_renew
)
843 renew
= min(renew
, *server
->entry
.max_renew
);
844 *et
.renew_till
= *et
.starttime
+ renew
;
848 *et
.renew_till
= min(*et
.renew_till
, *tgt
->renew_till
);
849 *et
.starttime
= min(*et
.starttime
, *et
.renew_till
);
850 et
.endtime
= min(et
.endtime
, *et
.renew_till
);
853 *et
.starttime
= min(*et
.starttime
, et
.endtime
);
855 if(*et
.starttime
== et
.endtime
){
856 ret
= KRB5KDC_ERR_NEVER_VALID
;
859 if(et
.renew_till
&& et
.endtime
== *et
.renew_till
){
861 et
.renew_till
= NULL
;
862 et
.flags
.renewable
= 0;
865 et
.flags
.pre_authent
= tgt
->flags
.pre_authent
;
866 et
.flags
.hw_authent
= tgt
->flags
.hw_authent
;
867 et
.flags
.anonymous
= tgt
->flags
.anonymous
;
868 et
.flags
.ok_as_delegate
= server
->entry
.flags
.ok_as_delegate
;
872 * No not need to filter out the any PAC from the
873 * auth_data since it's signed by the KDC.
875 ret
= _kdc_tkt_add_if_relevant_ad(context
, &et
,
876 KRB5_AUTHDATA_WIN2K_PAC
, rspac
);
884 /* XXX check authdata */
886 if (et
.authorization_data
== NULL
) {
887 et
.authorization_data
= calloc(1, sizeof(*et
.authorization_data
));
888 if (et
.authorization_data
== NULL
) {
890 krb5_set_error_message(context
, ret
, "malloc: out of memory");
894 for(i
= 0; i
< auth_data
->len
; i
++) {
895 ret
= add_AuthorizationData(et
.authorization_data
, &auth_data
->val
[i
]);
897 krb5_set_error_message(context
, ret
, "malloc: out of memory");
902 /* Filter out type KRB5SignedPath */
903 ret
= find_KRB5SignedPath(context
, et
.authorization_data
, NULL
);
905 if (et
.authorization_data
->len
== 1) {
906 free_AuthorizationData(et
.authorization_data
);
907 free(et
.authorization_data
);
908 et
.authorization_data
= NULL
;
910 AuthorizationData
*ad
= et
.authorization_data
;
911 free_AuthorizationDataElement(&ad
->val
[ad
->len
- 1]);
917 ret
= krb5_copy_keyblock_contents(context
, sessionkey
, &et
.key
);
920 et
.crealm
= tgt_name
->realm
;
921 et
.cname
= tgt_name
->name
;
924 /* MIT must have at least one last_req */
926 ek
.last_req
.val
= calloc(1, sizeof(*ek
.last_req
.val
));
927 if (ek
.last_req
.val
== NULL
) {
933 ek
.authtime
= et
.authtime
;
934 ek
.starttime
= et
.starttime
;
935 ek
.endtime
= et
.endtime
;
936 ek
.renew_till
= et
.renew_till
;
937 ek
.srealm
= rep
.ticket
.realm
;
938 ek
.sname
= rep
.ticket
.sname
;
940 _kdc_log_timestamp(context
, config
, "TGS-REQ", et
.authtime
, et
.starttime
,
941 et
.endtime
, et
.renew_till
);
943 /* Don't sign cross realm tickets, they can't be checked anyway */
945 char *r
= get_krbtgt_realm(&ek
.sname
);
947 if (r
== NULL
|| strcmp(r
, ek
.srealm
) == 0) {
948 ret
= _kdc_add_KRB5SignedPath(context
,
961 if (enc_pa_data
->len
) {
962 rep
.padata
= calloc(1, sizeof(*rep
.padata
));
963 if (rep
.padata
== NULL
) {
967 ret
= copy_METHOD_DATA(enc_pa_data
, rep
.padata
);
972 if (krb5_enctype_valid(context
, serverkey
->keytype
) != 0
973 && _kdc_is_weak_exception(server
->entry
.principal
, serverkey
->keytype
))
975 krb5_enctype_enable(context
, serverkey
->keytype
);
980 /* It is somewhat unclear where the etype in the following
981 encryption should come from. What we have is a session
982 key in the passed tgt, and a list of preferred etypes
983 *for the new ticket*. Should we pick the best possible
984 etype, given the keytype in the tgt, or should we look
985 at the etype list here as well? What if the tgt
986 session key is DES3 and we want a ticket with a (say)
987 CAST session key. Should the DES3 etype be added to the
988 etype list, even if we don't want a session key with
990 ret
= _kdc_encode_reply(context
, config
, NULL
, 0,
991 &rep
, &et
, &ek
, serverkey
->keytype
,
993 serverkey
, 0, replykey
, rk_is_subkey
,
996 krb5_enctype_disable(context
, serverkey
->keytype
);
1000 free_TransitedEncoding(&et
.transited
);
1004 free(et
.renew_till
);
1005 if(et
.authorization_data
) {
1006 free_AuthorizationData(et
.authorization_data
);
1007 free(et
.authorization_data
);
1009 free_LastReq(&ek
.last_req
);
1010 memset(et
.key
.keyvalue
.data
, 0, et
.key
.keyvalue
.length
);
1011 free_EncryptionKey(&et
.key
);
1015 static krb5_error_code
1016 tgs_check_authenticator(krb5_context context
,
1017 krb5_kdc_configuration
*config
,
1018 krb5_auth_context ac
,
1020 const char **e_text
,
1023 krb5_authenticator auth
;
1027 krb5_error_code ret
;
1030 krb5_auth_con_getauthenticator(context
, ac
, &auth
);
1031 if(auth
->cksum
== NULL
){
1032 kdc_log(context
, config
, 0, "No authenticator in request");
1033 ret
= KRB5KRB_AP_ERR_INAPP_CKSUM
;
1037 * according to RFC1510 it doesn't need to be keyed,
1038 * but according to the latest draft it needs to.
1042 !krb5_checksum_is_keyed(context
, auth
->cksum
->cksumtype
)
1045 !krb5_checksum_is_collision_proof(context
, auth
->cksum
->cksumtype
)) {
1046 kdc_log(context
, config
, 0, "Bad checksum type in authenticator: %d",
1047 auth
->cksum
->cksumtype
);
1048 ret
= KRB5KRB_AP_ERR_INAPP_CKSUM
;
1052 /* XXX should not re-encode this */
1053 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, b
, &len
, ret
);
1055 const char *msg
= krb5_get_error_message(context
, ret
);
1056 kdc_log(context
, config
, 0, "Failed to encode KDC-REQ-BODY: %s", msg
);
1057 krb5_free_error_message(context
, msg
);
1060 if(buf_size
!= len
) {
1062 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
1063 *e_text
= "KDC internal error";
1064 ret
= KRB5KRB_ERR_GENERIC
;
1067 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
1069 const char *msg
= krb5_get_error_message(context
, ret
);
1071 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1072 krb5_free_error_message(context
, msg
);
1075 ret
= krb5_verify_checksum(context
,
1077 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
1082 krb5_crypto_destroy(context
, crypto
);
1084 const char *msg
= krb5_get_error_message(context
, ret
);
1085 kdc_log(context
, config
, 0,
1086 "Failed to verify authenticator checksum: %s", msg
);
1087 krb5_free_error_message(context
, msg
);
1090 free_Authenticator(auth
);
1100 find_rpath(krb5_context context
, Realm crealm
, Realm srealm
)
1102 const char *new_realm
= krb5_config_get_string(context
,
1113 need_referral(krb5_context context
, krb5_kdc_configuration
*config
,
1114 const KDCOptions
* const options
, krb5_principal server
,
1115 krb5_realm
**realms
)
1119 if(!options
->canonicalize
&& server
->name
.name_type
!= KRB5_NT_SRV_INST
)
1122 if (server
->name
.name_string
.len
== 1)
1123 name
= server
->name
.name_string
.val
[0];
1124 else if (server
->name
.name_string
.len
== 3) {
1126 This is used to give referrals for the
1127 E3514235-4B06-11D1-AB04-00C04FC2DCD2/NTDSGUID/DNSDOMAIN
1128 SPN form, which is used for inter-domain communication in AD
1130 name
= server
->name
.name_string
.val
[2];
1131 kdc_log(context
, config
, 0, "Giving 3 part referral for %s", name
);
1132 *realms
= malloc(sizeof(char *)*2);
1133 if (*realms
== NULL
) {
1134 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
1137 (*realms
)[0] = strdup(name
);
1138 (*realms
)[1] = NULL
;
1140 } else if (server
->name
.name_string
.len
> 1)
1141 name
= server
->name
.name_string
.val
[1];
1145 kdc_log(context
, config
, 0, "Searching referral for %s", name
);
1147 return _krb5_get_host_realm_int(context
, name
, FALSE
, realms
) == 0;
1150 static krb5_error_code
1151 tgs_parse_request(krb5_context context
,
1152 krb5_kdc_configuration
*config
,
1154 const PA_DATA
*tgs_req
,
1155 hdb_entry_ex
**krbtgt
,
1156 krb5_enctype
*krbtgt_etype
,
1157 krb5_ticket
**ticket
,
1158 const char **e_text
,
1160 const struct sockaddr
*from_addr
,
1163 AuthorizationData
**auth_data
,
1164 krb5_keyblock
**replykey
,
1167 static char failed
[] = "<unparse_name failed>";
1169 krb5_error_code ret
;
1170 krb5_principal princ
;
1171 krb5_auth_context ac
= NULL
;
1172 krb5_flags ap_req_options
;
1173 krb5_flags verify_ap_req_flags
;
1175 krb5uint32 krbtgt_kvno
; /* kvno used for the PA-TGS-REQ AP-REQ Ticket */
1176 krb5uint32 krbtgt_kvno_try
;
1177 int kvno_search_tries
= 4; /* number of kvnos to try when tkt_vno == 0 */
1178 const Keys
*krbtgt_keys
;/* keyset for TGT tkt_vno */
1180 krb5_keyblock
*subkey
= NULL
;
1188 memset(&ap_req
, 0, sizeof(ap_req
));
1189 ret
= krb5_decode_ap_req(context
, &tgs_req
->padata_value
, &ap_req
);
1191 const char *msg
= krb5_get_error_message(context
, ret
);
1192 kdc_log(context
, config
, 0, "Failed to decode AP-REQ: %s", msg
);
1193 krb5_free_error_message(context
, msg
);
1197 if(!get_krbtgt_realm(&ap_req
.ticket
.sname
)){
1198 /* XXX check for ticket.sname == req.sname */
1199 kdc_log(context
, config
, 0, "PA-DATA is not a ticket-granting ticket");
1200 ret
= KRB5KDC_ERR_POLICY
; /* ? */
1204 _krb5_principalname2krb5_principal(context
,
1206 ap_req
.ticket
.sname
,
1207 ap_req
.ticket
.realm
);
1209 krbtgt_kvno
= ap_req
.ticket
.enc_part
.kvno
? *ap_req
.ticket
.enc_part
.kvno
: 0;
1210 ret
= _kdc_db_fetch(context
, config
, princ
, HDB_F_GET_KRBTGT
,
1211 &krbtgt_kvno
, NULL
, krbtgt
);
1213 if (ret
== HDB_ERR_NOT_FOUND_HERE
) {
1214 /* XXX Factor out this unparsing of the same princ all over */
1216 ret
= krb5_unparse_name(context
, princ
, &p
);
1219 krb5_free_principal(context
, princ
);
1220 kdc_log(context
, config
, 5,
1221 "Ticket-granting ticket account %s does not have secrets at "
1222 "this KDC, need to proxy", p
);
1225 ret
= HDB_ERR_NOT_FOUND_HERE
;
1227 } else if (ret
== HDB_ERR_KVNO_NOT_FOUND
) {
1229 ret
= krb5_unparse_name(context
, princ
, &p
);
1232 krb5_free_principal(context
, princ
);
1233 kdc_log(context
, config
, 5,
1234 "Ticket-granting ticket account %s does not have keys for "
1235 "kvno %d at this KDC", p
, krbtgt_kvno
);
1238 ret
= HDB_ERR_KVNO_NOT_FOUND
;
1240 } else if (ret
== HDB_ERR_NO_MKEY
) {
1242 ret
= krb5_unparse_name(context
, princ
, &p
);
1245 krb5_free_principal(context
, princ
);
1246 kdc_log(context
, config
, 5,
1247 "Missing master key for decrypting keys for ticket-granting "
1248 "ticket account %s with kvno %d at this KDC", p
, krbtgt_kvno
);
1251 ret
= HDB_ERR_KVNO_NOT_FOUND
;
1254 const char *msg
= krb5_get_error_message(context
, ret
);
1256 ret
= krb5_unparse_name(context
, princ
, &p
);
1259 krb5_free_principal(context
, princ
);
1260 kdc_log(context
, config
, 0,
1261 "Ticket-granting ticket not found in database: %s", msg
);
1262 krb5_free_error_message(context
, msg
);
1265 ret
= KRB5KRB_AP_ERR_NOT_US
;
1269 krbtgt_kvno_try
= krbtgt_kvno
? krbtgt_kvno
: (*krbtgt
)->entry
.kvno
;
1270 *krbtgt_etype
= ap_req
.ticket
.enc_part
.etype
;
1273 krbtgt_keys
= hdb_kvno2keys(context
, &(*krbtgt
)->entry
, krbtgt_kvno_try
);
1274 ret
= hdb_enctype2key(context
, &(*krbtgt
)->entry
, krbtgt_keys
,
1275 ap_req
.ticket
.enc_part
.etype
, &tkey
);
1276 if (ret
&& krbtgt_kvno
== 0 && kvno_search_tries
> 0) {
1277 kvno_search_tries
--;
1281 char *str
= NULL
, *p
= NULL
;
1283 krb5_enctype_to_string(context
, ap_req
.ticket
.enc_part
.etype
, &str
);
1284 krb5_unparse_name(context
, princ
, &p
);
1285 kdc_log(context
, config
, 0,
1286 "No server key with enctype %s found for %s",
1287 str
? str
: "<unknown enctype>",
1288 p
? p
: "<unparse_name failed>");
1291 ret
= KRB5KRB_AP_ERR_BADKEYVER
;
1295 if (b
->kdc_options
.validate
)
1296 verify_ap_req_flags
= KRB5_VERIFY_AP_REQ_IGNORE_INVALID
;
1298 verify_ap_req_flags
= 0;
1300 ret
= krb5_verify_ap_req2(context
,
1305 verify_ap_req_flags
,
1308 KRB5_KU_TGS_REQ_AUTH
);
1309 if (ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
&& kvno_search_tries
> 0) {
1310 kvno_search_tries
--;
1315 krb5_free_principal(context
, princ
);
1317 const char *msg
= krb5_get_error_message(context
, ret
);
1318 kdc_log(context
, config
, 0, "Failed to verify AP-REQ: %s", msg
);
1319 krb5_free_error_message(context
, msg
);
1324 krb5_authenticator auth
;
1326 ret
= krb5_auth_con_getauthenticator(context
, ac
, &auth
);
1328 *csec
= malloc(sizeof(**csec
));
1329 if (*csec
== NULL
) {
1330 krb5_free_authenticator(context
, &auth
);
1331 kdc_log(context
, config
, 0, "malloc failed");
1334 **csec
= auth
->ctime
;
1335 *cusec
= malloc(sizeof(**cusec
));
1336 if (*cusec
== NULL
) {
1337 krb5_free_authenticator(context
, &auth
);
1338 kdc_log(context
, config
, 0, "malloc failed");
1341 **cusec
= auth
->cusec
;
1342 krb5_free_authenticator(context
, &auth
);
1346 ret
= tgs_check_authenticator(context
, config
,
1347 ac
, b
, e_text
, &(*ticket
)->ticket
.key
);
1349 krb5_auth_con_free(context
, ac
);
1353 usage
= KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
;
1356 ret
= krb5_auth_con_getremotesubkey(context
, ac
, &subkey
);
1358 const char *msg
= krb5_get_error_message(context
, ret
);
1359 krb5_auth_con_free(context
, ac
);
1360 kdc_log(context
, config
, 0, "Failed to get remote subkey: %s", msg
);
1361 krb5_free_error_message(context
, msg
);
1365 usage
= KRB5_KU_TGS_REQ_AUTH_DAT_SESSION
;
1368 ret
= krb5_auth_con_getkey(context
, ac
, &subkey
);
1370 const char *msg
= krb5_get_error_message(context
, ret
);
1371 krb5_auth_con_free(context
, ac
);
1372 kdc_log(context
, config
, 0, "Failed to get session key: %s", msg
);
1373 krb5_free_error_message(context
, msg
);
1378 krb5_auth_con_free(context
, ac
);
1379 kdc_log(context
, config
, 0,
1380 "Failed to get key for enc-authorization-data");
1381 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1387 if (b
->enc_authorization_data
) {
1390 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
1392 const char *msg
= krb5_get_error_message(context
, ret
);
1393 krb5_auth_con_free(context
, ac
);
1394 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1395 krb5_free_error_message(context
, msg
);
1398 ret
= krb5_decrypt_EncryptedData (context
,
1401 b
->enc_authorization_data
,
1403 krb5_crypto_destroy(context
, crypto
);
1405 krb5_auth_con_free(context
, ac
);
1406 kdc_log(context
, config
, 0,
1407 "Failed to decrypt enc-authorization-data");
1408 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1412 if (*auth_data
== NULL
) {
1413 krb5_auth_con_free(context
, ac
);
1414 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1417 ret
= decode_AuthorizationData(ad
.data
, ad
.length
, *auth_data
, NULL
);
1419 krb5_auth_con_free(context
, ac
);
1422 kdc_log(context
, config
, 0, "Failed to decode authorization data");
1423 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
; /* ? */
1428 krb5_auth_con_free(context
, ac
);
1431 free_AP_REQ(&ap_req
);
1436 static krb5_error_code
1437 build_server_referral(krb5_context context
,
1438 krb5_kdc_configuration
*config
,
1439 krb5_crypto session
,
1440 krb5_const_realm referred_realm
,
1441 const PrincipalName
*true_principal_name
,
1442 const PrincipalName
*requested_principal
,
1445 PA_ServerReferralData ref
;
1446 krb5_error_code ret
;
1451 memset(&ref
, 0, sizeof(ref
));
1453 if (referred_realm
) {
1454 ALLOC(ref
.referred_realm
);
1455 if (ref
.referred_realm
== NULL
)
1457 *ref
.referred_realm
= strdup(referred_realm
);
1458 if (*ref
.referred_realm
== NULL
)
1461 if (true_principal_name
) {
1462 ALLOC(ref
.true_principal_name
);
1463 if (ref
.true_principal_name
== NULL
)
1465 ret
= copy_PrincipalName(true_principal_name
, ref
.true_principal_name
);
1469 if (requested_principal
) {
1470 ALLOC(ref
.requested_principal_name
);
1471 if (ref
.requested_principal_name
== NULL
)
1473 ret
= copy_PrincipalName(requested_principal
,
1474 ref
.requested_principal_name
);
1479 ASN1_MALLOC_ENCODE(PA_ServerReferralData
,
1480 data
.data
, data
.length
,
1482 free_PA_ServerReferralData(&ref
);
1485 if (data
.length
!= size
)
1486 krb5_abortx(context
, "internal asn.1 encoder error");
1488 ret
= krb5_encrypt_EncryptedData(context
, session
,
1489 KRB5_KU_PA_SERVER_REFERRAL
,
1490 data
.data
, data
.length
,
1496 ASN1_MALLOC_ENCODE(EncryptedData
,
1497 outdata
->data
, outdata
->length
,
1499 free_EncryptedData(&ed
);
1502 if (outdata
->length
!= size
)
1503 krb5_abortx(context
, "internal asn.1 encoder error");
1507 free_PA_ServerReferralData(&ref
);
1508 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1512 static krb5_error_code
1513 tgs_build_reply(krb5_context context
,
1514 krb5_kdc_configuration
*config
,
1517 hdb_entry_ex
*krbtgt
,
1518 krb5_enctype krbtgt_etype
,
1519 const krb5_keyblock
*replykey
,
1521 krb5_ticket
*ticket
,
1524 const char **e_text
,
1525 AuthorizationData
**auth_data
,
1526 const struct sockaddr
*from_addr
)
1528 krb5_error_code ret
;
1529 krb5_principal cp
= NULL
, sp
= NULL
, rsp
= NULL
, tp
= NULL
, dp
= NULL
;
1530 krb5_principal krbtgt_out_principal
= NULL
;
1531 char *spn
= NULL
, *cpn
= NULL
, *tpn
= NULL
, *dpn
= NULL
, *krbtgt_out_n
= NULL
;
1532 hdb_entry_ex
*server
= NULL
, *client
= NULL
, *s4u2self_impersonated_client
= NULL
;
1533 HDB
*clientdb
, *s4u2self_impersonated_clientdb
;
1534 krb5_realm ref_realm
= NULL
;
1535 EncTicketPart
*tgt
= &ticket
->ticket
;
1536 krb5_principals spp
= NULL
;
1537 const EncryptionKey
*ekey
;
1538 krb5_keyblock sessionkey
;
1542 hdb_entry_ex
*krbtgt_out
= NULL
;
1544 METHOD_DATA enc_pa_data
;
1549 EncTicketPart adtkt
;
1555 int flags
= HDB_F_FOR_TGS_REQ
;
1557 memset(&sessionkey
, 0, sizeof(sessionkey
));
1558 memset(&adtkt
, 0, sizeof(adtkt
));
1559 krb5_data_zero(&rspac
);
1560 memset(&enc_pa_data
, 0, sizeof(enc_pa_data
));
1566 * Always to do CANON, see comment below about returned server principal (rsp).
1568 flags
|= HDB_F_CANON
;
1570 if(b
->kdc_options
.enc_tkt_in_skey
){
1576 if(b
->additional_tickets
== NULL
||
1577 b
->additional_tickets
->len
== 0){
1578 ret
= KRB5KDC_ERR_BADOPTION
; /* ? */
1579 kdc_log(context
, config
, 0,
1580 "No second ticket present in request");
1583 t
= &b
->additional_tickets
->val
[0];
1584 if(!get_krbtgt_realm(&t
->sname
)){
1585 kdc_log(context
, config
, 0,
1586 "Additional ticket is not a ticket-granting ticket");
1587 ret
= KRB5KDC_ERR_POLICY
;
1590 _krb5_principalname2krb5_principal(context
, &p
, t
->sname
, t
->realm
);
1591 ret
= _kdc_db_fetch(context
, config
, p
,
1592 HDB_F_GET_KRBTGT
, t
->enc_part
.kvno
,
1594 krb5_free_principal(context
, p
);
1596 if (ret
== HDB_ERR_NOENTRY
)
1597 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1600 ret
= hdb_enctype2key(context
, &uu
->entry
, NULL
,
1601 t
->enc_part
.etype
, &uukey
);
1603 _kdc_free_ent(context
, uu
);
1604 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
; /* XXX */
1607 ret
= krb5_decrypt_ticket(context
, t
, &uukey
->key
, &adtkt
, 0);
1608 _kdc_free_ent(context
, uu
);
1612 ret
= verify_flags(context
, config
, &adtkt
, spn
);
1620 _krb5_principalname2krb5_principal(context
, &sp
, *s
, r
);
1621 ret
= krb5_unparse_name(context
, sp
, &spn
);
1624 _krb5_principalname2krb5_principal(context
, &cp
, tgt
->cname
, tgt
->crealm
);
1625 ret
= krb5_unparse_name(context
, cp
, &cpn
);
1628 unparse_flags (KDCOptions2int(b
->kdc_options
),
1629 asn1_KDCOptions_units(),
1630 opt_str
, sizeof(opt_str
));
1632 kdc_log(context
, config
, 0,
1633 "TGS-REQ %s from %s for %s [%s]",
1634 cpn
, from
, spn
, opt_str
);
1636 kdc_log(context
, config
, 0,
1637 "TGS-REQ %s from %s for %s", cpn
, from
, spn
);
1644 ret
= _kdc_db_fetch(context
, config
, sp
, HDB_F_GET_SERVER
| flags
,
1645 NULL
, NULL
, &server
);
1647 if (ret
== HDB_ERR_NOT_FOUND_HERE
) {
1648 kdc_log(context
, config
, 5, "target %s does not have secrets at this KDC, need to proxy", sp
);
1650 } else if (ret
== HDB_ERR_WRONG_REALM
) {
1653 ref_realm
= strdup(server
->entry
.principal
->realm
);
1654 if (ref_realm
== NULL
) {
1659 kdc_log(context
, config
, 5,
1660 "Returning a referral to realm %s for "
1663 krb5_free_principal(context
, sp
);
1667 ret
= krb5_make_principal(context
, &sp
, r
, KRB5_TGS_NAME
,
1671 ret
= krb5_unparse_name(context
, sp
, &spn
);
1677 const char *new_rlm
, *msg
;
1681 if ((req_rlm
= get_krbtgt_realm(&sp
->name
)) != NULL
) {
1683 new_rlm
= find_rpath(context
, tgt
->crealm
, req_rlm
);
1685 kdc_log(context
, config
, 5, "krbtgt for realm %s "
1686 "not found, trying %s",
1688 krb5_free_principal(context
, sp
);
1690 krb5_make_principal(context
, &sp
, r
,
1691 KRB5_TGS_NAME
, new_rlm
, NULL
);
1692 ret
= krb5_unparse_name(context
, sp
, &spn
);
1698 ref_realm
= strdup(new_rlm
);
1702 } else if (need_referral(context
, config
, &b
->kdc_options
, sp
, &realms
)) {
1703 if (strcmp(realms
[0], sp
->realm
) != 0) {
1704 kdc_log(context
, config
, 5,
1705 "Returning a referral to realm %s for "
1706 "server %s that was not found",
1708 krb5_free_principal(context
, sp
);
1710 krb5_make_principal(context
, &sp
, r
, KRB5_TGS_NAME
,
1712 ret
= krb5_unparse_name(context
, sp
, &spn
);
1714 krb5_free_host_realm(context
, realms
);
1720 ref_realm
= strdup(realms
[0]);
1722 krb5_free_host_realm(context
, realms
);
1725 krb5_free_host_realm(context
, realms
);
1727 msg
= krb5_get_error_message(context
, ret
);
1728 kdc_log(context
, config
, 0,
1729 "Server not found in database: %s: %s", spn
, msg
);
1730 krb5_free_error_message(context
, msg
);
1731 if (ret
== HDB_ERR_NOENTRY
)
1732 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1736 /* the name returned to the client depend on what was asked for,
1737 * return canonical name if kdc_options.canonicalize was set, the
1738 * client wants the true name of the principal, if not it just
1739 * wants the name its asked for.
1742 if (b
->kdc_options
.canonicalize
)
1743 rsp
= server
->entry
.principal
;
1749 * Select enctype, return key and kvno.
1755 if(b
->kdc_options
.enc_tkt_in_skey
) {
1758 for(i
= 0; i
< b
->etype
.len
; i
++)
1759 if (b
->etype
.val
[i
] == adtkt
.key
.keytype
)
1761 if(i
== b
->etype
.len
) {
1762 kdc_log(context
, config
, 0,
1763 "Addition ticket have not matching etypes");
1764 krb5_clear_error_message(context
);
1765 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
1768 etype
= b
->etype
.val
[i
];
1773 ret
= _kdc_find_etype(context
,
1774 krb5_principal_is_krbtgt(context
, sp
) ?
1775 config
->tgt_use_strongest_session_key
:
1776 config
->svc_use_strongest_session_key
, FALSE
,
1777 server
, b
->etype
.val
, b
->etype
.len
, &etype
,
1780 kdc_log(context
, config
, 0,
1781 "Server (%s) has no support for etypes", spn
);
1784 ret
= _kdc_get_preferred_key(context
, config
, server
, spn
,
1787 kdc_log(context
, config
, 0,
1788 "Server (%s) has no supported etypes", spn
);
1792 kvno
= server
->entry
.kvno
;
1795 ret
= krb5_generate_random_keyblock(context
, etype
, &sessionkey
);
1801 * Check that service is in the same realm as the krbtgt. If it's
1802 * not the same, it's someone that is using a uni-directional trust
1807 * Validate authoriation data
1810 ret
= hdb_enctype2key(context
, &krbtgt
->entry
, NULL
, /* XXX use the right kvno! */
1811 krbtgt_etype
, &tkey_check
);
1813 kdc_log(context
, config
, 0,
1814 "Failed to find key for krbtgt PAC check");
1819 * Now refetch the primary krbtgt, and get the current kvno (the
1820 * sign check may have been on an old kvno, and the server may
1821 * have been an incoming trust)
1825 const char *remote_realm
=
1826 krb5_principal_get_comp_string(context
, krbtgt
->entry
.principal
, 1);
1828 ret
= krb5_make_principal(context
,
1829 &krbtgt_out_principal
,
1835 kdc_log(context
, config
, 0,
1836 "Failed to make krbtgt principal name object for "
1837 "authz-data signatures");
1840 ret
= krb5_unparse_name(context
, krbtgt_out_principal
, &krbtgt_out_n
);
1842 kdc_log(context
, config
, 0,
1843 "Failed to make krbtgt principal name object for "
1844 "authz-data signatures");
1849 ret
= _kdc_db_fetch(context
, config
, krbtgt_out_principal
,
1850 HDB_F_GET_KRBTGT
, NULL
, NULL
, &krbtgt_out
);
1853 ret
= krb5_unparse_name(context
, krbtgt
->entry
.principal
, &ktpn
);
1854 kdc_log(context
, config
, 0,
1855 "No such principal %s (needed for authz-data signature keys) "
1856 "while processing TGS-REQ for service %s with krbtg %s",
1857 krbtgt_out_n
, spn
, (ret
== 0) ? ktpn
: "<unknown>");
1859 ret
= KRB5KRB_AP_ERR_NOT_US
;
1864 * The first realm is the realm of the service, the second is
1865 * krbtgt/<this>/@REALM component of the krbtgt DN the request was
1866 * encrypted to. The redirection via the krbtgt_out entry allows
1867 * the DB to possibly correct the case of the realm (Samba4 does
1868 * this) before the strcmp()
1870 if (strcmp(krb5_principal_get_realm(context
, server
->entry
.principal
),
1871 krb5_principal_get_realm(context
, krbtgt_out
->entry
.principal
)) != 0) {
1873 ret
= krb5_unparse_name(context
, krbtgt_out
->entry
.principal
, &ktpn
);
1874 kdc_log(context
, config
, 0,
1875 "Request with wrong krbtgt: %s",
1876 (ret
== 0) ? ktpn
: "<unknown>");
1879 ret
= KRB5KRB_AP_ERR_NOT_US
;
1883 ret
= _kdc_get_preferred_key(context
, config
, krbtgt_out
, krbtgt_out_n
,
1886 kdc_log(context
, config
, 0,
1887 "Failed to find key for krbtgt PAC signature");
1890 ret
= hdb_enctype2key(context
, &krbtgt_out
->entry
, NULL
,
1891 tkey_sign
->key
.keytype
, &tkey_sign
);
1893 kdc_log(context
, config
, 0,
1894 "Failed to find key for krbtgt PAC signature");
1898 ret
= _kdc_db_fetch(context
, config
, cp
, HDB_F_GET_CLIENT
| flags
,
1899 NULL
, &clientdb
, &client
);
1900 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1901 /* This is OK, we are just trying to find out if they have
1902 * been disabled or deleted in the meantime, missing secrets
1905 const char *krbtgt_realm
, *msg
;
1908 * If the client belongs to the same realm as our krbtgt, it
1909 * should exist in the local database.
1913 krbtgt_realm
= krb5_principal_get_realm(context
, krbtgt_out
->entry
.principal
);
1915 if(strcmp(krb5_principal_get_realm(context
, cp
), krbtgt_realm
) == 0) {
1916 if (ret
== HDB_ERR_NOENTRY
)
1917 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1918 kdc_log(context
, config
, 1, "Client no longer in database: %s",
1923 msg
= krb5_get_error_message(context
, ret
);
1924 kdc_log(context
, config
, 1, "Client not found in database: %s", msg
);
1925 krb5_free_error_message(context
, msg
);
1928 ret
= check_PAC(context
, config
, cp
, NULL
,
1929 client
, server
, krbtgt
,
1931 ekey
, &tkey_sign
->key
,
1932 tgt
, &rspac
, &signedpath
);
1934 const char *msg
= krb5_get_error_message(context
, ret
);
1935 kdc_log(context
, config
, 0,
1936 "Verify PAC failed for %s (%s) from %s with %s",
1937 spn
, cpn
, from
, msg
);
1938 krb5_free_error_message(context
, msg
);
1942 /* also check the krbtgt for signature */
1943 ret
= check_KRB5SignedPath(context
,
1951 const char *msg
= krb5_get_error_message(context
, ret
);
1952 kdc_log(context
, config
, 0,
1953 "KRB5SignedPath check failed for %s (%s) from %s with %s",
1954 spn
, cpn
, from
, msg
);
1955 krb5_free_error_message(context
, msg
);
1963 /* by default the tgt principal matches the client principal */
1968 const PA_DATA
*sdata
;
1971 sdata
= _kdc_find_padata(req
, &i
, KRB5_PADATA_FOR_USER
);
1978 ret
= decode_PA_S4U2Self(sdata
->padata_value
.data
,
1979 sdata
->padata_value
.length
,
1982 kdc_log(context
, config
, 0, "Failed to decode PA-S4U2Self");
1986 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &datack
);
1990 ret
= krb5_crypto_init(context
, &tgt
->key
, 0, &crypto
);
1992 const char *msg
= krb5_get_error_message(context
, ret
);
1993 free_PA_S4U2Self(&self
);
1994 krb5_data_free(&datack
);
1995 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1996 krb5_free_error_message(context
, msg
);
2000 ret
= krb5_verify_checksum(context
,
2002 KRB5_KU_OTHER_CKSUM
,
2006 krb5_data_free(&datack
);
2007 krb5_crypto_destroy(context
, crypto
);
2009 const char *msg
= krb5_get_error_message(context
, ret
);
2010 free_PA_S4U2Self(&self
);
2011 kdc_log(context
, config
, 0,
2012 "krb5_verify_checksum failed for S4U2Self: %s", msg
);
2013 krb5_free_error_message(context
, msg
);
2017 ret
= _krb5_principalname2krb5_principal(context
,
2021 free_PA_S4U2Self(&self
);
2025 ret
= krb5_unparse_name(context
, tp
, &tpn
);
2029 /* If we were about to put a PAC into the ticket, we better fix it to be the right PAC */
2032 krb5_data_free(&rspac
);
2033 ret
= _kdc_db_fetch(context
, config
, tp
, HDB_F_GET_CLIENT
| flags
,
2034 NULL
, &s4u2self_impersonated_clientdb
, &s4u2self_impersonated_client
);
2039 * If the client belongs to the same realm as our krbtgt, it
2040 * should exist in the local database.
2044 if (ret
== HDB_ERR_NOENTRY
)
2045 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
2046 msg
= krb5_get_error_message(context
, ret
);
2047 kdc_log(context
, config
, 1,
2048 "S2U4Self principal to impersonate %s not found in database: %s",
2050 krb5_free_error_message(context
, msg
);
2053 ret
= _kdc_pac_generate(context
, s4u2self_impersonated_client
, &p
);
2055 kdc_log(context
, config
, 0, "PAC generation failed for -- %s",
2060 ret
= _krb5_pac_sign(context
, p
, ticket
->ticket
.authtime
,
2061 s4u2self_impersonated_client
->entry
.principal
,
2062 ekey
, &tkey_sign
->key
,
2064 krb5_pac_free(context
, p
);
2066 kdc_log(context
, config
, 0, "PAC signing failed for -- %s",
2074 * Check that service doing the impersonating is
2075 * requesting a ticket to it-self.
2077 ret
= check_s4u2self(context
, config
, clientdb
, client
, sp
);
2079 kdc_log(context
, config
, 0, "S4U2Self: %s is not allowed "
2080 "to impersonate to service "
2081 "(tried for user %s to service %s)",
2087 * If the service isn't trusted for authentication to
2088 * delegation, remove the forward flag.
2091 if (client
->entry
.flags
.trusted_for_delegation
) {
2092 str
= "[forwardable]";
2094 b
->kdc_options
.forwardable
= 0;
2097 kdc_log(context
, config
, 0, "s4u2self %s impersonating %s to "
2098 "service %s %s", cpn
, tpn
, spn
, str
);
2103 * Constrained delegation
2107 && b
->additional_tickets
!= NULL
2108 && b
->additional_tickets
->len
!= 0
2109 && b
->kdc_options
.enc_tkt_in_skey
== 0)
2111 int ad_signedpath
= 0;
2116 * Require that the KDC have issued the service's krbtgt (not
2117 * self-issued ticket with kimpersonate(1).
2120 ret
= KRB5KDC_ERR_BADOPTION
;
2121 kdc_log(context
, config
, 0,
2122 "Constrained delegation done on service ticket %s/%s",
2127 t
= &b
->additional_tickets
->val
[0];
2129 ret
= hdb_enctype2key(context
, &client
->entry
,
2130 hdb_kvno2keys(context
, &client
->entry
,
2131 t
->enc_part
.kvno
? * t
->enc_part
.kvno
: 0),
2132 t
->enc_part
.etype
, &clientkey
);
2134 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
; /* XXX */
2138 ret
= krb5_decrypt_ticket(context
, t
, &clientkey
->key
, &adtkt
, 0);
2140 kdc_log(context
, config
, 0,
2141 "failed to decrypt ticket for "
2142 "constrained delegation from %s to %s ", cpn
, spn
);
2146 ret
= _krb5_principalname2krb5_principal(context
,
2153 ret
= krb5_unparse_name(context
, tp
, &tpn
);
2157 ret
= _krb5_principalname2krb5_principal(context
,
2164 ret
= krb5_unparse_name(context
, dp
, &dpn
);
2168 /* check that ticket is valid */
2169 if (adtkt
.flags
.forwardable
== 0) {
2170 kdc_log(context
, config
, 0,
2171 "Missing forwardable flag on ticket for "
2172 "constrained delegation from %s (%s) as %s to %s ",
2173 cpn
, dpn
, tpn
, spn
);
2174 ret
= KRB5KDC_ERR_BADOPTION
;
2178 ret
= check_constrained_delegation(context
, config
, clientdb
,
2179 client
, server
, sp
);
2181 kdc_log(context
, config
, 0,
2182 "constrained delegation from %s (%s) as %s to %s not allowed",
2183 cpn
, dpn
, tpn
, spn
);
2187 ret
= verify_flags(context
, config
, &adtkt
, tpn
);
2192 krb5_data_free(&rspac
);
2195 * generate the PAC for the user.
2197 * TODO: pass in t->sname and t->realm and build
2198 * a S4U_DELEGATION_INFO blob to the PAC.
2200 ret
= check_PAC(context
, config
, tp
, dp
,
2201 client
, server
, krbtgt
,
2203 ekey
, &tkey_sign
->key
,
2204 &adtkt
, &rspac
, &ad_signedpath
);
2206 const char *msg
= krb5_get_error_message(context
, ret
);
2207 kdc_log(context
, config
, 0,
2208 "Verify delegated PAC failed to %s for client"
2209 "%s (%s) as %s from %s with %s",
2210 spn
, cpn
, dpn
, tpn
, from
, msg
);
2211 krb5_free_error_message(context
, msg
);
2216 * Check that the KDC issued the user's ticket.
2218 ret
= check_KRB5SignedPath(context
,
2226 const char *msg
= krb5_get_error_message(context
, ret
);
2227 kdc_log(context
, config
, 0,
2228 "KRB5SignedPath check from service %s failed "
2229 "for delegation to %s for client %s (%s)"
2230 "from %s failed with %s",
2231 spn
, tpn
, dpn
, cpn
, from
, msg
);
2232 krb5_free_error_message(context
, msg
);
2236 if (!ad_signedpath
) {
2237 ret
= KRB5KDC_ERR_BADOPTION
;
2238 kdc_log(context
, config
, 0,
2239 "Ticket not signed with PAC nor SignedPath service %s failed "
2240 "for delegation to %s for client %s (%s)"
2242 spn
, tpn
, dpn
, cpn
, from
);
2246 kdc_log(context
, config
, 0, "constrained delegation for %s "
2247 "from %s (%s) to %s", tpn
, cpn
, dpn
, spn
);
2254 ret
= kdc_check_flags(context
, config
,
2261 if((b
->kdc_options
.validate
|| b
->kdc_options
.renew
) &&
2262 !krb5_principal_compare(context
,
2263 krbtgt
->entry
.principal
,
2264 server
->entry
.principal
)){
2265 kdc_log(context
, config
, 0, "Inconsistent request.");
2266 ret
= KRB5KDC_ERR_SERVER_NOMATCH
;
2270 /* check for valid set of addresses */
2271 if(!_kdc_check_addresses(context
, config
, tgt
->caddr
, from_addr
)) {
2272 ret
= KRB5KRB_AP_ERR_BADADDR
;
2273 kdc_log(context
, config
, 0, "Request from wrong address");
2278 * If this is an referral, add server referral data to the
2285 kdc_log(context
, config
, 0,
2286 "Adding server referral to %s", ref_realm
);
2288 ret
= krb5_crypto_init(context
, &sessionkey
, 0, &crypto
);
2292 ret
= build_server_referral(context
, config
, crypto
, ref_realm
,
2293 NULL
, s
, &pa
.padata_value
);
2294 krb5_crypto_destroy(context
, crypto
);
2296 kdc_log(context
, config
, 0,
2297 "Failed building server referral");
2300 pa
.padata_type
= KRB5_PADATA_SERVER_REFERRAL
;
2302 ret
= add_METHOD_DATA(&enc_pa_data
, &pa
);
2303 krb5_data_free(&pa
.padata_value
);
2305 kdc_log(context
, config
, 0,
2306 "Add server referral METHOD-DATA failed");
2315 ret
= tgs_make_reply(context
,
2332 tkey_sign
->key
.keytype
,
2347 krb5_data_free(&rspac
);
2348 krb5_free_keyblock_contents(context
, &sessionkey
);
2350 _kdc_free_ent(context
, krbtgt_out
);
2352 _kdc_free_ent(context
, server
);
2354 _kdc_free_ent(context
, client
);
2355 if(s4u2self_impersonated_client
)
2356 _kdc_free_ent(context
, s4u2self_impersonated_client
);
2359 krb5_free_principal(context
, tp
);
2360 krb5_free_principal(context
, cp
);
2361 krb5_free_principal(context
, dp
);
2362 krb5_free_principal(context
, sp
);
2363 krb5_free_principal(context
, krbtgt_out_principal
);
2366 free_METHOD_DATA(&enc_pa_data
);
2368 free_EncTicketPart(&adtkt
);
2378 _kdc_tgs_rep(krb5_context context
,
2379 krb5_kdc_configuration
*config
,
2383 struct sockaddr
*from_addr
,
2386 AuthorizationData
*auth_data
= NULL
;
2387 krb5_error_code ret
;
2389 const PA_DATA
*tgs_req
;
2391 hdb_entry_ex
*krbtgt
= NULL
;
2392 krb5_ticket
*ticket
= NULL
;
2393 const char *e_text
= NULL
;
2394 krb5_enctype krbtgt_etype
= ETYPE_NULL
;
2396 krb5_keyblock
*replykey
= NULL
;
2397 int rk_is_subkey
= 0;
2398 time_t *csec
= NULL
;
2401 if(req
->padata
== NULL
){
2402 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
; /* XXX ??? */
2403 kdc_log(context
, config
, 0,
2404 "TGS-REQ from %s without PA-DATA", from
);
2408 tgs_req
= _kdc_find_padata(req
, &i
, KRB5_PADATA_TGS_REQ
);
2410 if(tgs_req
== NULL
){
2411 ret
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
2413 kdc_log(context
, config
, 0,
2414 "TGS-REQ from %s without PA-TGS-REQ", from
);
2417 ret
= tgs_parse_request(context
, config
,
2418 &req
->req_body
, tgs_req
,
2428 if (ret
== HDB_ERR_NOT_FOUND_HERE
) {
2429 /* kdc_log() is called in tgs_parse_request() */
2433 kdc_log(context
, config
, 0,
2434 "Failed parsing TGS-REQ from %s", from
);
2439 const PA_DATA
*pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_FX_FAST
);
2441 kdc_log(context
, config
, 10, "Got TGS FAST request");
2445 ret
= tgs_build_reply(context
,
2460 kdc_log(context
, config
, 0,
2461 "Failed building TGS-REP to %s", from
);
2466 if (datagram_reply
&& data
->length
> config
->max_datagram_reply_length
) {
2467 krb5_data_free(data
);
2468 ret
= KRB5KRB_ERR_RESPONSE_TOO_BIG
;
2469 e_text
= "Reply packet too large";
2474 krb5_free_keyblock(context
, replykey
);
2476 if(ret
&& ret
!= HDB_ERR_NOT_FOUND_HERE
&& data
->data
== NULL
){
2477 /* XXX add fast wrapping on the error */
2478 METHOD_DATA error_method
= { 0, NULL
};
2481 kdc_log(context
, config
, 10, "tgs-req: sending error: %d to client", ret
);
2482 ret
= _kdc_fast_mk_error(context
, NULL
,
2491 free_METHOD_DATA(&error_method
);
2496 krb5_free_ticket(context
, ticket
);
2498 _kdc_free_ent(context
, krbtgt
);
2501 free_AuthorizationData(auth_data
);