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 * [MS-SFU] Kerberos Protocol Extensions:
38 * Service for User (S4U2Self) and Constrained Delegation Protocol (S4U2Proxy)
39 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-sfu/
43 * Determine if constrained delegation is allowed from this client to this server
46 static krb5_error_code
47 check_constrained_delegation(krb5_context context
,
48 krb5_kdc_configuration
*config
,
52 krb5_const_principal target
)
54 const HDB_Ext_Constrained_delegation_acl
*acl
;
59 * constrained delegation (S4U2Proxy) only works within
60 * the same realm. We use the already canonicalized version
61 * of the principals here, while "target" is the principal
62 * provided by the client.
64 if (!krb5_realm_compare(context
, client
->principal
, server
->principal
)) {
65 ret
= KRB5KDC_ERR_BADOPTION
;
66 kdc_log(context
, config
, 4,
67 "Bad request for constrained delegation");
71 if (clientdb
->hdb_check_constrained_delegation
) {
72 ret
= clientdb
->hdb_check_constrained_delegation(context
, clientdb
, client
, target
);
76 /* if client delegates to itself, that ok */
77 if (krb5_principal_compare(context
, client
->principal
, server
->principal
) == TRUE
)
80 ret
= hdb_entry_get_ConstrainedDelegACL(client
, &acl
);
82 krb5_clear_error_message(context
);
87 for (i
= 0; i
< acl
->len
; i
++) {
88 if (krb5_principal_compare(context
, target
, &acl
->val
[i
]) == TRUE
)
92 ret
= KRB5KDC_ERR_BADOPTION
;
94 kdc_log(context
, config
, 4,
95 "Bad request for constrained delegation");
100 * Determine if resource-based constrained delegation is allowed from this
101 * client to this server
104 static krb5_error_code
105 check_rbcd(krb5_context context
,
106 krb5_kdc_configuration
*config
,
108 krb5_const_principal s4u_principal
,
109 const hdb_entry
*client_krbtgt
,
110 const hdb_entry
*client
,
111 const hdb_entry
*device_krbtgt
,
112 const hdb_entry
*device
,
113 krb5_const_pac client_pac
,
114 krb5_const_pac device_pac
,
115 const hdb_entry
*target
)
117 krb5_error_code ret
= KRB5KDC_ERR_BADOPTION
;
119 if (clientdb
->hdb_check_rbcd
) {
120 ret
= clientdb
->hdb_check_rbcd(context
,
134 kdc_log(context
, config
, 4,
135 "Bad request for resource-based constrained delegation");
140 * Validate a protocol transition (S4U2Self) request. If successfully
141 * validated then the client in the request structure will be replaced
142 * with the impersonated client.
146 _kdc_validate_protocol_transition(astgs_request_t r
, const PA_DATA
*for_user
)
149 KDC_REQ_BODY
*b
= &r
->req
.req_body
;
150 EncTicketPart
*ticket
= &r
->ticket
->ticket
;
151 hdb_entry
*s4u_client
= NULL
;
153 int flags
= HDB_F_FOR_TGS_REQ
;
154 krb5_principal s4u_client_name
= NULL
, s4u_canon_client_name
= NULL
;
155 krb5_pac s4u_pac
= NULL
;
156 char *s4ucname
= NULL
;
162 heim_assert(r
->client
!= NULL
, "client must be non-NULL");
164 memset(&self
, 0, sizeof(self
));
166 if (b
->kdc_options
.canonicalize
)
167 flags
|= HDB_F_CANON
;
169 ret
= decode_PA_S4U2Self(for_user
->padata_value
.data
,
170 for_user
->padata_value
.length
,
173 kdc_audit_addreason((kdc_request_t
)r
,
174 "Failed to decode PA-S4U2Self");
175 kdc_log(r
->context
, r
->config
, 4, "Failed to decode PA-S4U2Self");
179 if (!krb5_checksum_is_keyed(r
->context
, self
.cksum
.cksumtype
)) {
180 kdc_audit_addreason((kdc_request_t
)r
,
181 "PA-S4U2Self with unkeyed checksum");
182 kdc_log(r
->context
, r
->config
, 4, "Reject PA-S4U2Self with unkeyed checksum");
183 ret
= KRB5KRB_AP_ERR_INAPP_CKSUM
;
187 ret
= _krb5_s4u2self_to_checksumdata(r
->context
, &self
, &datack
);
191 ret
= krb5_crypto_init(r
->context
, &ticket
->key
, 0, &crypto
);
193 const char *msg
= krb5_get_error_message(r
->context
, ret
);
194 krb5_data_free(&datack
);
195 kdc_log(r
->context
, r
->config
, 4, "krb5_crypto_init failed: %s", msg
);
196 krb5_free_error_message(r
->context
, msg
);
200 /* Allow HMAC_MD5 checksum with any key type */
201 if (self
.cksum
.cksumtype
== CKSUMTYPE_HMAC_MD5
) {
202 struct krb5_crypto_iov iov
;
203 unsigned char csdata
[16];
206 cs
.checksum
.length
= sizeof(csdata
);
207 cs
.checksum
.data
= &csdata
;
209 iov
.data
.data
= datack
.data
;
210 iov
.data
.length
= datack
.length
;
211 iov
.flags
= KRB5_CRYPTO_TYPE_DATA
;
213 ret
= _krb5_HMAC_MD5_checksum(r
->context
, NULL
, &crypto
->key
,
214 KRB5_KU_OTHER_CKSUM
, &iov
, 1,
217 krb5_data_ct_cmp(&cs
.checksum
, &self
.cksum
.checksum
) != 0)
218 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
220 ret
= _kdc_verify_checksum(r
->context
,
226 krb5_data_free(&datack
);
227 krb5_crypto_destroy(r
->context
, crypto
);
229 const char *msg
= krb5_get_error_message(r
->context
, ret
);
230 kdc_audit_addreason((kdc_request_t
)r
,
231 "S4U2Self checksum failed");
232 kdc_log(r
->context
, r
->config
, 4,
233 "krb5_verify_checksum failed for S4U2Self: %s", msg
);
234 krb5_free_error_message(r
->context
, msg
);
238 ret
= _krb5_principalname2krb5_principal(r
->context
,
245 ret
= krb5_unparse_name(r
->context
, s4u_client_name
, &s4ucname
);
250 * Note no HDB_F_SYNTHETIC_OK -- impersonating non-existent clients
251 * is probably not desirable!
253 ret
= _kdc_db_fetch(r
->context
, r
->config
, s4u_client_name
,
254 HDB_F_GET_CLIENT
| flags
, NULL
,
255 &s4u_clientdb
, &s4u_client
);
260 * If the client belongs to the same realm as our krbtgt, it
261 * should exist in the local database.
264 if (ret
== HDB_ERR_NOENTRY
)
265 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
266 msg
= krb5_get_error_message(r
->context
, ret
);
267 kdc_audit_addreason((kdc_request_t
)r
,
268 "S4U2Self principal to impersonate not found");
269 kdc_log(r
->context
, r
->config
, 2,
270 "S4U2Self principal to impersonate %s not found in database: %s",
272 krb5_free_error_message(r
->context
, msg
);
277 * Ignore require_pwchange and pw_end attributes (as Windows does),
278 * since S4U2Self is not password authentication.
280 s4u_client
->flags
.require_pwchange
= FALSE
;
281 free(s4u_client
->pw_end
);
282 s4u_client
->pw_end
= NULL
;
284 ret
= kdc_check_flags(r
, FALSE
, s4u_client
, r
->server
);
286 goto out
; /* kdc_check_flags() calls kdc_audit_addreason() */
288 ret
= _kdc_pac_generate(r
,
292 KRB5_PAC_WAS_GIVEN_IMPLICITLY
,
295 kdc_log(r
->context
, r
->config
, 4, "PAC generation failed for -- %s", s4ucname
);
300 * Check that service doing the impersonating is
301 * requesting a ticket to it-self.
303 ret
= _kdc_check_client_matches_target_service(r
->context
,
310 kdc_log(r
->context
, r
->config
, 4, "S4U2Self: %s is not allowed "
311 "to impersonate to service "
312 "(tried for user %s to service %s)",
313 r
->cname
, s4ucname
, r
->sname
);
317 ret
= krb5_copy_principal(r
->context
, s4u_client
->principal
,
318 &s4u_canon_client_name
);
323 * If the service isn't trusted for authentication to
324 * delegation or if the impersonate client is disallowed
325 * forwardable, remove the forwardable flag.
327 if (r
->client
->flags
.trusted_for_delegation
&&
328 s4u_client
->flags
.forwardable
) {
329 str
= " [forwardable]";
331 b
->kdc_options
.forwardable
= 0;
334 kdc_log(r
->context
, r
->config
, 4, "s4u2self %s impersonating %s to "
335 "service %s%s", r
->cname
, s4ucname
, r
->sname
, str
);
338 * Replace all client information in the request with the
339 * impersonated client. (The audit entry containing the original
340 * client name will have been created before this point.)
342 _kdc_request_set_cname_nocopy((kdc_request_t
)r
, &s4ucname
);
343 _kdc_request_set_client_princ_nocopy(r
, &s4u_client_name
);
345 _kdc_free_ent(r
->context
, r
->clientdb
, r
->client
);
346 r
->client
= s4u_client
;
348 r
->clientdb
= s4u_clientdb
;
351 _kdc_request_set_canon_client_princ_nocopy(r
, &s4u_canon_client_name
);
352 _kdc_request_set_pac_nocopy(r
, &s4u_pac
);
356 _kdc_free_ent(r
->context
, s4u_clientdb
, s4u_client
);
357 krb5_free_principal(r
->context
, s4u_client_name
);
358 krb5_xfree(s4ucname
);
359 krb5_free_principal(r
->context
, s4u_canon_client_name
);
360 krb5_pac_free(r
->context
, s4u_pac
);
362 free_PA_S4U2Self(&self
);
368 * Validate a constrained delegation (S4U2Proxy) request. If
369 * successfully validated then the client in the request structure will
370 * be replaced with the client from the evidence ticket.
374 _kdc_validate_constrained_delegation(astgs_request_t r
)
377 KDC_REQ_BODY
*b
= &r
->req
.req_body
;
378 int flags
= HDB_F_FOR_TGS_REQ
;
379 krb5_principal s4u_client_name
= NULL
, s4u_server_name
= NULL
;
380 krb5_principal s4u_canon_client_name
= NULL
;
381 krb5_pac s4u_pac
= NULL
;
382 uint64_t s4u_pac_attributes
;
383 char *s4ucname
= NULL
, *s4usname
= NULL
;
384 EncTicketPart evidence_tkt
;
386 hdb_entry
*s4u_client
= NULL
;
387 HDB
*s4u_serverdb
= NULL
;
388 hdb_entry
*s4u_server
= NULL
;
389 krb5_boolean ad_kdc_issued
= FALSE
;
392 krb5_const_realm local_realm
;
393 const PA_DATA
*pac_options_data
= NULL
;
394 int pac_options_data_idx
= 0;
395 krb5_boolean rbcd_support
= FALSE
;
397 memset(&evidence_tkt
, 0, sizeof(evidence_tkt
));
399 krb5_principal_get_comp_string(r
->context
, r
->krbtgt
->principal
, 1);
402 * We require that the service's TGT has a PAC; this will have been
403 * validated prior to this function being called.
405 if (r
->pac
== NULL
) {
406 ret
= KRB5KDC_ERR_BADOPTION
;
407 kdc_audit_addreason((kdc_request_t
)r
, "Missing PAC");
408 kdc_log(r
->context
, r
->config
, 4,
409 "Constrained delegation without PAC, %s/%s",
414 t
= &b
->additional_tickets
->val
[0];
416 ret
= _krb5_principalname2krb5_principal(r
->context
,
423 ret
= krb5_unparse_name(r
->context
, s4u_server_name
, &s4usname
);
428 * Look up the name given in the ticket in the database. We don’t ask for
429 * canonicalisation, so that we get back the same principal that was
430 * specified in the ticket.
432 ret
= _kdc_db_fetch(r
->context
, r
->config
, s4u_server_name
,
433 HDB_F_GET_SERVER
| HDB_F_DELAY_NEW_KEYS
| flags
,
434 NULL
, &s4u_serverdb
, &s4u_server
);
435 if (ret
== HDB_ERR_NOENTRY
)
436 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
438 kdc_audit_addreason((kdc_request_t
)r
,
439 "Constrained delegation service principal unknown");
444 * Check that the delegating server (r->client) is the same one as specified
445 * in the ticket. This is to make sure that the server hasn’t forged the
446 * sname, which is in the unencrypted part of the ticket.
448 ret
= _kdc_check_client_matches_target_service(r
->context
,
454 if (ret
== KRB5KRB_AP_ERR_BADMATCH
)
455 ret
= KRB5KDC_ERR_BADOPTION
;
459 ret
= hdb_enctype2key(r
->context
, r
->client
,
460 hdb_kvno2keys(r
->context
, r
->client
,
461 t
->enc_part
.kvno
? * t
->enc_part
.kvno
: 0),
462 t
->enc_part
.etype
, &clientkey
);
464 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
; /* XXX */
468 ret
= krb5_decrypt_ticket(r
->context
, t
, &clientkey
->key
, &evidence_tkt
, 0);
470 kdc_audit_addreason((kdc_request_t
)r
,
471 "Failed to decrypt constrained delegation ticket");
472 kdc_log(r
->context
, r
->config
, 4,
473 "failed to decrypt ticket for "
474 "constrained delegation from %s to %s", r
->cname
, r
->sname
);
478 ret
= _krb5_principalname2krb5_principal(r
->context
,
481 evidence_tkt
.crealm
);
485 ret
= krb5_unparse_name(r
->context
, s4u_client_name
, &s4ucname
);
489 kdc_audit_addkv((kdc_request_t
)r
, 0, "impersonatee", "%s", s4ucname
);
491 /* check that ticket is valid */
492 if (evidence_tkt
.flags
.forwardable
== 0) {
493 kdc_audit_addreason((kdc_request_t
)r
,
494 "Missing forwardable flag on ticket for constrained delegation");
495 kdc_log(r
->context
, r
->config
, 4,
496 "Missing forwardable flag on ticket for "
497 "constrained delegation from %s (%s) as %s to %s ",
498 r
->cname
, s4usname
, s4ucname
, r
->sname
);
499 ret
= KRB5KDC_ERR_BADOPTION
;
503 pac_options_data
= _kdc_find_padata(&r
->req
,
504 &pac_options_data_idx
,
505 KRB5_PADATA_PAC_OPTIONS
);
506 if (pac_options_data
!= NULL
) {
507 PA_PAC_OPTIONS pac_options
;
510 ret
= decode_PA_PAC_OPTIONS(pac_options_data
->padata_value
.data
,
511 pac_options_data
->padata_value
.length
,
518 if (size
!= pac_options_data
->padata_value
.length
) {
519 free_PA_PAC_OPTIONS(&pac_options
);
520 ret
= KRB5KDC_ERR_BADOPTION
;
524 rbcd_support
= pac_options
.flags
.resource_based_constrained_delegation
!= 0;
526 free_PA_PAC_OPTIONS(&pac_options
);
530 ret
= check_rbcd(r
->context
, r
->config
, r
->clientdb
,
532 r
->krbtgt
, r
->client
,
533 r
->armor_server
, r
->armor_client
,
534 r
->pac
, r
->armor_pac
,
537 ret
= KRB5KDC_ERR_BADOPTION
;
539 if (ret
== KRB5KDC_ERR_BADOPTION
) {
540 /* RBCD was denied or not supported; try constrained delegation. */
541 ret
= check_constrained_delegation(r
->context
, r
->config
, r
->clientdb
,
542 r
->client
, r
->server
, r
->server_princ
);
544 kdc_audit_addreason((kdc_request_t
)r
,
545 "Constrained delegation not allowed");
546 kdc_log(r
->context
, r
->config
, 4,
547 "constrained delegation from %s (%s) as %s to %s not allowed",
548 r
->cname
, s4usname
, s4ucname
, r
->sname
);
552 kdc_audit_addreason((kdc_request_t
)r
,
553 "Resource-based constrained delegation not allowed");
554 kdc_log(r
->context
, r
->config
, 4,
555 "resource-based constrained delegation from %s (%s) as %s to %s not allowed",
556 r
->cname
, s4usname
, s4ucname
, r
->sname
);
560 ret
= _kdc_verify_flags(r
->context
, r
->config
, &evidence_tkt
, s4ucname
);
562 kdc_audit_addreason((kdc_request_t
)r
,
563 "Constrained delegation ticket expired or invalid");
567 /* Try lookup the delegated client in DB */
568 ret
= _kdc_db_fetch_client(r
->context
, r
->config
, flags
,
569 s4u_client_name
, s4ucname
, local_realm
,
570 &s4u_clientdb
, &s4u_client
);
574 if (s4u_client
!= NULL
) {
575 ret
= kdc_check_flags(r
, FALSE
, s4u_client
, r
->server
);
581 * TODO: pass in t->sname and t->realm and build
582 * a S4U_DELEGATION_INFO blob to the PAC.
584 ret
= _kdc_check_pac(r
, s4u_client_name
, s4u_server
,
585 s4u_client
, r
->server
, r
->krbtgt
, r
->client
,
586 &clientkey
->key
, &r
->ticket_key
->key
, &evidence_tkt
,
587 &ad_kdc_issued
, &s4u_pac
,
588 &s4u_canon_client_name
, &s4u_pac_attributes
);
590 const char *msg
= krb5_get_error_message(r
->context
, ret
);
591 kdc_audit_addreason((kdc_request_t
)r
,
592 "Constrained delegation ticket PAC check failed");
593 kdc_log(r
->context
, r
->config
, 4,
594 "Verify delegated PAC failed to %s for client "
595 "%s (%s) as %s from %s with %s",
596 r
->sname
, r
->cname
, s4usname
, s4ucname
, r
->from
, msg
);
597 krb5_free_error_message(r
->context
, msg
);
601 if (s4u_pac
== NULL
|| !ad_kdc_issued
) {
602 ret
= KRB5KDC_ERR_BADOPTION
;
603 kdc_log(r
->context
, r
->config
, 4,
604 "Ticket not signed with PAC; service %s failed for "
605 "for delegation to %s for client %s (%s) from %s; (%s).",
606 r
->sname
, s4ucname
, s4usname
, r
->cname
, r
->from
,
607 s4u_pac
? "Ticket unsigned" : "No PAC");
608 kdc_audit_addreason((kdc_request_t
)r
,
609 "Constrained delegation ticket not signed");
613 heim_assert(s4u_pac
!= NULL
, "ad_kdc_issued implies the PAC is non-NULL");
615 ret
= _kdc_pac_update(r
, s4u_client_name
, s4u_server
, r
->pac
,
616 s4u_client
, r
->server
, r
->krbtgt
,
618 if (ret
== KRB5_PLUGIN_NO_HANDLE
) {
622 const char *msg
= krb5_get_error_message(r
->context
, ret
);
623 kdc_audit_addreason((kdc_request_t
)r
,
624 "Constrained delegation ticket PAC update failed");
625 kdc_log(r
->context
, r
->config
, 4,
626 "Update delegated PAC failed to %s for client "
627 "%s (%s) as %s from %s with %s",
628 r
->sname
, r
->cname
, s4usname
, s4ucname
, r
->from
, msg
);
629 krb5_free_error_message(r
->context
, msg
);
634 * If the evidence ticket PAC didn't include PAC_UPN_DNS_INFO with
635 * the canonical client name, but the user is local to our KDC, we
636 * can insert the canonical client name ourselves.
638 if (s4u_canon_client_name
== NULL
&& s4u_client
!= NULL
) {
639 ret
= krb5_copy_principal(r
->context
, s4u_client
->principal
,
640 &s4u_canon_client_name
);
645 if (b
->enc_authorization_data
&& r
->rk_is_subkey
== 0) {
646 krb5_free_keyblock_contents(r
->context
, &r
->enc_ad_key
);
647 ret
= krb5_copy_keyblock_contents(r
->context
,
654 kdc_log(r
->context
, r
->config
, 4, "constrained delegation for %s "
655 "from %s (%s) to %s", s4ucname
, r
->cname
, s4usname
, r
->sname
);
658 * Replace all client information in the request with the
659 * impersonated client. (The audit entry containing the original
660 * client name will have been created before this point.)
662 _kdc_request_set_cname_nocopy((kdc_request_t
)r
, &s4ucname
);
663 _kdc_request_set_client_princ_nocopy(r
, &s4u_client_name
);
665 _kdc_free_ent(r
->context
, r
->clientdb
, r
->client
);
666 r
->client
= s4u_client
;
668 r
->clientdb
= s4u_clientdb
;
671 _kdc_request_set_canon_client_princ_nocopy(r
, &s4u_canon_client_name
);
672 _kdc_request_set_pac_nocopy(r
, &s4u_pac
);
674 r
->pac_attributes
= s4u_pac_attributes
;
676 r
->et
.authtime
= evidence_tkt
.authtime
;
680 _kdc_free_ent(r
->context
, s4u_clientdb
, s4u_client
);
682 _kdc_free_ent(r
->context
, s4u_serverdb
, s4u_server
);
683 krb5_free_principal(r
->context
, s4u_client_name
);
684 krb5_xfree(s4ucname
);
685 krb5_free_principal(r
->context
, s4u_server_name
);
686 krb5_xfree(s4usname
);
687 krb5_free_principal(r
->context
, s4u_canon_client_name
);
688 krb5_pac_free(r
->context
, s4u_pac
);
690 free_EncTicketPart(&evidence_tkt
);