4fe373eb526a4d5d339ba9326e1578fedc26011f
[lorikeet-heimdal.git] / kdc / mssfu.c
1 /*
2  * Copyright (c) 1997-2008 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "kdc_locl.h"
35
36 /*
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/
40  */
41
42 /*
43  * Determine if constrained delegation is allowed from this client to this server
44  */
45
46 static krb5_error_code
47 check_constrained_delegation(krb5_context context,
48                              krb5_kdc_configuration *config,
49                              HDB *clientdb,
50                              hdb_entry *client,
51                              hdb_entry *server,
52                              krb5_const_principal target)
53 {
54     const HDB_Ext_Constrained_delegation_acl *acl;
55     krb5_error_code ret;
56     size_t i;
57
58     /*
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.
63      */
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");
68         return ret;
69     }
70
71     if (clientdb->hdb_check_constrained_delegation) {
72         ret = clientdb->hdb_check_constrained_delegation(context, clientdb, client, target);
73         if (ret == 0)
74             return 0;
75     } else {
76         /* if client delegates to itself, that ok */
77         if (krb5_principal_compare(context, client->principal, server->principal) == TRUE)
78             return 0;
79
80         ret = hdb_entry_get_ConstrainedDelegACL(client, &acl);
81         if (ret) {
82             krb5_clear_error_message(context);
83             return ret;
84         }
85
86         if (acl) {
87             for (i = 0; i < acl->len; i++) {
88                 if (krb5_principal_compare(context, target, &acl->val[i]) == TRUE)
89                     return 0;
90             }
91         }
92         ret = KRB5KDC_ERR_BADOPTION;
93     }
94     kdc_log(context, config, 4,
95             "Bad request for constrained delegation");
96     return ret;
97 }
98
99 /*
100  * Validate a protocol transition (S4U2Self) request. If successfully
101  * validated then the client in the request structure will be replaced
102  * with the impersonated client.
103  */
104
105 krb5_error_code
106 _kdc_validate_protocol_transition(astgs_request_t r, const PA_DATA *for_user)
107 {
108     krb5_error_code ret;
109     KDC_REQ_BODY *b = &r->req.req_body;
110     EncTicketPart *ticket = &r->ticket->ticket;
111     hdb_entry *s4u_client = NULL;
112     HDB *s4u_clientdb;
113     int flags = HDB_F_FOR_TGS_REQ;
114     krb5_principal s4u_client_name = NULL, s4u_canon_client_name = NULL;
115     krb5_pac s4u_pac = NULL;
116     char *s4ucname = NULL;
117     krb5_crypto crypto;
118     krb5_data datack;
119     PA_S4U2Self self;
120     const char *str;
121
122     heim_assert(r->client != NULL, "client must be non-NULL");
123
124     memset(&self, 0, sizeof(self));
125
126     if (b->kdc_options.canonicalize)
127         flags |= HDB_F_CANON;
128
129     ret = decode_PA_S4U2Self(for_user->padata_value.data,
130                              for_user->padata_value.length,
131                              &self, NULL);
132     if (ret) {
133         kdc_audit_addreason((kdc_request_t)r,
134                             "Failed to decode PA-S4U2Self");
135         kdc_log(r->context, r->config, 4, "Failed to decode PA-S4U2Self");
136         goto out;
137     }
138
139     if (!krb5_checksum_is_keyed(r->context, self.cksum.cksumtype)) {
140         kdc_audit_addreason((kdc_request_t)r,
141                             "PA-S4U2Self with unkeyed checksum");
142         kdc_log(r->context, r->config, 4, "Reject PA-S4U2Self with unkeyed checksum");
143         ret = KRB5KRB_AP_ERR_INAPP_CKSUM;
144         goto out;
145     }
146
147     ret = _krb5_s4u2self_to_checksumdata(r->context, &self, &datack);
148     if (ret)
149         goto out;
150
151     ret = krb5_crypto_init(r->context, &ticket->key, 0, &crypto);
152     if (ret) {
153         const char *msg = krb5_get_error_message(r->context, ret);
154         krb5_data_free(&datack);
155         kdc_log(r->context, r->config, 4, "krb5_crypto_init failed: %s", msg);
156         krb5_free_error_message(r->context, msg);
157         goto out;
158     }
159
160     /* Allow HMAC_MD5 checksum with any key type */
161     if (self.cksum.cksumtype == CKSUMTYPE_HMAC_MD5) {
162         struct krb5_crypto_iov iov;
163         unsigned char csdata[16];
164         Checksum cs;
165
166         cs.checksum.length = sizeof(csdata);
167         cs.checksum.data = &csdata;
168
169         iov.data.data = datack.data;
170         iov.data.length = datack.length;
171         iov.flags = KRB5_CRYPTO_TYPE_DATA;
172
173         ret = _krb5_HMAC_MD5_checksum(r->context, NULL, &crypto->key,
174                                       KRB5_KU_OTHER_CKSUM, &iov, 1,
175                                       &cs);
176         if (ret == 0 &&
177             krb5_data_ct_cmp(&cs.checksum, &self.cksum.checksum) != 0)
178             ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
179     } else {
180         ret = _kdc_verify_checksum(r->context,
181                                    crypto,
182                                    KRB5_KU_OTHER_CKSUM,
183                                    &datack,
184                                    &self.cksum);
185     }
186     krb5_data_free(&datack);
187     krb5_crypto_destroy(r->context, crypto);
188     if (ret) {
189         const char *msg = krb5_get_error_message(r->context, ret);
190         kdc_audit_addreason((kdc_request_t)r,
191                             "S4U2Self checksum failed");
192         kdc_log(r->context, r->config, 4,
193                 "krb5_verify_checksum failed for S4U2Self: %s", msg);
194         krb5_free_error_message(r->context, msg);
195         goto out;
196     }
197
198     ret = _krb5_principalname2krb5_principal(r->context,
199                                              &s4u_client_name,
200                                              self.name,
201                                              self.realm);
202     if (ret)
203         goto out;
204
205     ret = krb5_unparse_name(r->context, s4u_client_name, &s4ucname);
206     if (ret)
207         goto out;
208
209     /*
210      * Note no HDB_F_SYNTHETIC_OK -- impersonating non-existent clients
211      * is probably not desirable!
212      */
213     ret = _kdc_db_fetch(r->context, r->config, s4u_client_name,
214                         HDB_F_GET_CLIENT | flags, NULL,
215                         &s4u_clientdb, &s4u_client);
216     if (ret) {
217         const char *msg;
218
219         /*
220          * If the client belongs to the same realm as our krbtgt, it
221          * should exist in the local database.
222          *
223          */
224         if (ret == HDB_ERR_NOENTRY)
225             ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
226         msg = krb5_get_error_message(r->context, ret);
227         kdc_audit_addreason((kdc_request_t)r,
228                             "S4U2Self principal to impersonate not found");
229         kdc_log(r->context, r->config, 2,
230                 "S4U2Self principal to impersonate %s not found in database: %s",
231                 s4ucname, msg);
232         krb5_free_error_message(r->context, msg);
233         goto out;
234     }
235
236     /*
237      * Ignore require_pwchange and pw_end attributes (as Windows does),
238      * since S4U2Self is not password authentication.
239      */
240     s4u_client->flags.require_pwchange = FALSE;
241     free(s4u_client->pw_end);
242     s4u_client->pw_end = NULL;
243
244     ret = kdc_check_flags(r, FALSE, s4u_client, r->server);
245     if (ret)
246         goto out; /* kdc_check_flags() calls kdc_audit_addreason() */
247
248     ret = _kdc_pac_generate(r,
249                             s4u_client,
250                             r->server,
251                             NULL,
252                             KRB5_PAC_WAS_GIVEN_IMPLICITLY,
253                             &s4u_pac);
254     if (ret) {
255         kdc_log(r->context, r->config, 4, "PAC generation failed for -- %s", s4ucname);
256         goto out;
257     }
258
259     /*
260      * Check that service doing the impersonating is
261      * requesting a ticket to it-self.
262      */
263     ret = _kdc_check_client_matches_target_service(r->context,
264                                                    r->config,
265                                                    r->clientdb,
266                                                    r->client,
267                                                    r->server,
268                                                    r->server_princ);
269     if (ret) {
270         kdc_log(r->context, r->config, 4, "S4U2Self: %s is not allowed "
271                 "to impersonate to service "
272                  "(tried for user %s to service %s)",
273                  r->cname, s4ucname, r->sname);
274         goto out;
275     }
276
277     ret = krb5_copy_principal(r->context, s4u_client->principal,
278                               &s4u_canon_client_name);
279     if (ret)
280         goto out;
281
282     /*
283      * If the service isn't trusted for authentication to
284      * delegation or if the impersonate client is disallowed
285      * forwardable, remove the forwardable flag.
286      */
287     if (r->client->flags.trusted_for_delegation &&
288         s4u_client->flags.forwardable) {
289         str = " [forwardable]";
290     } else {
291         b->kdc_options.forwardable = 0;
292         str = "";
293     }
294     kdc_log(r->context, r->config, 4, "s4u2self %s impersonating %s to "
295             "service %s%s", r->cname, s4ucname, r->sname, str);
296
297     /*
298      * Replace all client information in the request with the
299      * impersonated client. (The audit entry containing the original
300      * client name will have been created before this point.)
301      */
302     _kdc_request_set_cname_nocopy((kdc_request_t)r, &s4ucname);
303     _kdc_request_set_client_princ_nocopy(r, &s4u_client_name);
304
305     _kdc_free_ent(r->context, r->clientdb, r->client);
306     r->client = s4u_client;
307     s4u_client = NULL;
308     r->clientdb = s4u_clientdb;
309     s4u_clientdb = NULL;
310
311     _kdc_request_set_canon_client_princ_nocopy(r, &s4u_canon_client_name);
312     _kdc_request_set_pac_nocopy(r, &s4u_pac);
313
314 out:
315     if (s4u_client)
316         _kdc_free_ent(r->context, s4u_clientdb, s4u_client);
317     krb5_free_principal(r->context, s4u_client_name);
318     krb5_xfree(s4ucname);
319     krb5_free_principal(r->context, s4u_canon_client_name);
320     krb5_pac_free(r->context, s4u_pac);
321
322     free_PA_S4U2Self(&self);
323
324     return ret;
325 }
326
327 /*
328  * Validate a constrained delegation (S4U2Proxy) request. If
329  * successfully validated then the client in the request structure will
330  * be replaced with the client from the evidence ticket.
331  */
332
333 krb5_error_code
334 _kdc_validate_constrained_delegation(astgs_request_t r)
335 {
336     krb5_error_code ret;
337     KDC_REQ_BODY *b = &r->req.req_body;
338     int flags = HDB_F_FOR_TGS_REQ;
339     krb5_principal s4u_client_name = NULL, s4u_server_name = NULL;
340     krb5_principal s4u_canon_client_name = NULL;
341     krb5_pac s4u_pac = NULL;
342     uint64_t s4u_pac_attributes;
343     char *s4ucname = NULL, *s4usname = NULL;
344     EncTicketPart evidence_tkt;
345     HDB *s4u_clientdb;
346     hdb_entry *s4u_client = NULL;
347     HDB *s4u_serverdb = NULL;
348     hdb_entry *s4u_server = NULL;
349     krb5_boolean ad_kdc_issued = FALSE;
350     Key *clientkey;
351     Ticket *t;
352     krb5_const_realm local_realm;
353
354     memset(&evidence_tkt, 0, sizeof(evidence_tkt));
355     local_realm =
356             krb5_principal_get_comp_string(r->context, r->krbtgt->principal, 1);
357
358     /*
359      * We require that the service's TGT has a PAC; this will have been
360      * validated prior to this function being called.
361      */
362     if (r->pac == NULL) {
363         ret = KRB5KDC_ERR_BADOPTION;
364         kdc_audit_addreason((kdc_request_t)r, "Missing PAC");
365         kdc_log(r->context, r->config, 4,
366                 "Constrained delegation without PAC, %s/%s",
367                 r->cname, r->sname);
368         goto out;
369     }
370
371     t = &b->additional_tickets->val[0];
372
373     ret = _krb5_principalname2krb5_principal(r->context,
374                                              &s4u_server_name,
375                                              t->sname,
376                                              t->realm);
377     if (ret)
378         goto out;
379
380     ret = krb5_unparse_name(r->context, s4u_server_name, &s4usname);
381     if (ret)
382         goto out;
383
384     /*
385      * Look up the name given in the ticket in the database. We don’t ask for
386      * canonicalisation, so that we get back the same principal that was
387      * specified in the ticket.
388      */
389     ret = _kdc_db_fetch(r->context, r->config, s4u_server_name,
390                         HDB_F_GET_SERVER | HDB_F_DELAY_NEW_KEYS | flags,
391                         NULL, &s4u_serverdb, &s4u_server);
392     if (ret == HDB_ERR_NOENTRY)
393         ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
394     if (ret) {
395         kdc_audit_addreason((kdc_request_t)r,
396                             "Constrained delegation service principal unknown");
397         goto out;
398     }
399
400     /*
401      * Check that the delegating server (r->client) is the same one as specified
402      * in the ticket. This is to make sure that the server hasn’t forged the
403      * sname, which is in the unencrypted part of the ticket.
404      */
405     ret = _kdc_check_client_matches_target_service(r->context,
406                                                    r->config,
407                                                    s4u_serverdb,
408                                                    s4u_server,
409                                                    r->client,
410                                                    r->client_princ);
411     if (ret == KRB5KRB_AP_ERR_BADMATCH)
412         ret = KRB5KDC_ERR_BADOPTION;
413     if (ret)
414         goto out;
415
416     ret = hdb_enctype2key(r->context, r->client,
417                           hdb_kvno2keys(r->context, r->client,
418                                         t->enc_part.kvno ? * t->enc_part.kvno : 0),
419                           t->enc_part.etype, &clientkey);
420     if (ret) {
421         ret = KRB5KDC_ERR_ETYPE_NOSUPP; /* XXX */
422         goto out;
423     }
424
425     ret = krb5_decrypt_ticket(r->context, t, &clientkey->key, &evidence_tkt, 0);
426     if (ret) {
427         kdc_audit_addreason((kdc_request_t)r,
428                             "Failed to decrypt constrained delegation ticket");
429         kdc_log(r->context, r->config, 4,
430                 "failed to decrypt ticket for "
431                 "constrained delegation from %s to %s", r->cname, r->sname);
432         goto out;
433     }
434
435     ret = _krb5_principalname2krb5_principal(r->context,
436                                              &s4u_client_name,
437                                              evidence_tkt.cname,
438                                              evidence_tkt.crealm);
439     if (ret)
440         goto out;
441
442     ret = krb5_unparse_name(r->context, s4u_client_name, &s4ucname);
443     if (ret)
444         goto out;
445
446     kdc_audit_addkv((kdc_request_t)r, 0, "impersonatee", "%s", s4ucname);
447
448         /* check that ticket is valid */
449     if (evidence_tkt.flags.forwardable == 0) {
450         kdc_audit_addreason((kdc_request_t)r,
451                             "Missing forwardable flag on ticket for constrained delegation");
452         kdc_log(r->context, r->config, 4,
453                 "Missing forwardable flag on ticket for "
454                 "constrained delegation from %s (%s) as %s to %s ",
455                 r->cname, s4usname, s4ucname, r->sname);
456         ret = KRB5KDC_ERR_BADOPTION;
457         goto out;
458     }
459
460     ret = check_constrained_delegation(r->context, r->config, r->clientdb,
461                                        r->client, r->server, r->server_princ);
462     if (ret) {
463         kdc_audit_addreason((kdc_request_t)r,
464                             "Constrained delegation not allowed");
465         kdc_log(r->context, r->config, 4,
466                 "constrained delegation from %s (%s) as %s to %s not allowed",
467                 r->cname, s4usname, s4ucname, r->sname);
468         goto out;
469     }
470
471     ret = _kdc_verify_flags(r->context, r->config, &evidence_tkt, s4ucname);
472     if (ret) {
473         kdc_audit_addreason((kdc_request_t)r,
474                             "Constrained delegation ticket expired or invalid");
475         goto out;
476     }
477
478     /* Try lookup the delegated client in DB */
479     ret = _kdc_db_fetch_client(r->context, r->config, flags,
480                                s4u_client_name, s4ucname, local_realm,
481                                &s4u_clientdb, &s4u_client);
482     if (ret)
483         goto out;
484
485     if (s4u_client != NULL) {
486         ret = kdc_check_flags(r, FALSE, s4u_client, r->server);
487         if (ret)
488             goto out;
489     }
490
491     /*
492      * TODO: pass in t->sname and t->realm and build
493      * a S4U_DELEGATION_INFO blob to the PAC.
494      */
495     ret = _kdc_check_pac(r, s4u_client_name, s4u_server_name,
496                          s4u_client, r->server, r->krbtgt, r->client,
497                          &clientkey->key, &r->ticket_key->key, &evidence_tkt,
498                          &ad_kdc_issued, &s4u_pac,
499                          &s4u_canon_client_name, &s4u_pac_attributes);
500     if (ret) {
501         const char *msg = krb5_get_error_message(r->context, ret);
502         kdc_audit_addreason((kdc_request_t)r,
503                             "Constrained delegation ticket PAC check failed");
504         kdc_log(r->context, r->config, 4,
505                 "Verify delegated PAC failed to %s for client "
506                 "%s (%s) as %s from %s with %s",
507                 r->sname, r->cname, s4usname, s4ucname, r->from, msg);
508         krb5_free_error_message(r->context, msg);
509         goto out;
510     }
511
512     if (s4u_pac == NULL || !ad_kdc_issued) {
513         ret = KRB5KDC_ERR_BADOPTION;
514         kdc_log(r->context, r->config, 4,
515                 "Ticket not signed with PAC; service %s failed for "
516                 "for delegation to %s for client %s (%s) from %s; (%s).",
517                 r->sname, s4ucname, s4usname, r->cname, r->from,
518                 s4u_pac ? "Ticket unsigned" : "No PAC");
519         kdc_audit_addreason((kdc_request_t)r,
520                             "Constrained delegation ticket not signed");
521         goto out;
522     }
523
524     heim_assert(s4u_pac != NULL, "ad_kdc_issued implies the PAC is non-NULL");
525
526     ret = _kdc_pac_update(r, s4u_client_name, s4u_server_name,
527                           s4u_client, r->server, r->krbtgt,
528                           &s4u_pac);
529     if (ret == KRB5_PLUGIN_NO_HANDLE) {
530         ret = 0;
531     }
532     if (ret) {
533         const char *msg = krb5_get_error_message(r->context, ret);
534         kdc_audit_addreason((kdc_request_t)r,
535                             "Constrained delegation ticket PAC update failed");
536         kdc_log(r->context, r->config, 4,
537                 "Update delegated PAC failed to %s for client "
538                 "%s (%s) as %s from %s with %s",
539                 r->sname, r->cname, s4usname, s4ucname, r->from, msg);
540         krb5_free_error_message(r->context, msg);
541         goto out;
542     }
543
544     /*
545      * If the evidence ticket PAC didn't include PAC_UPN_DNS_INFO with
546      * the canonical client name, but the user is local to our KDC, we
547      * can insert the canonical client name ourselves.
548      */
549     if (s4u_canon_client_name == NULL && s4u_client != NULL) {
550         ret = krb5_copy_principal(r->context, s4u_client->principal,
551                                   &s4u_canon_client_name);
552         if (ret)
553             goto out;
554     }
555
556     if (b->enc_authorization_data && r->rk_is_subkey == 0) {
557         krb5_free_keyblock_contents(r->context, &r->enc_ad_key);
558         ret = krb5_copy_keyblock_contents(r->context,
559                                           &evidence_tkt.key,
560                                           &r->enc_ad_key);
561         if (ret)
562             goto out;
563     }
564
565     kdc_log(r->context, r->config, 4, "constrained delegation for %s "
566             "from %s (%s) to %s", s4ucname, r->cname, s4usname, r->sname);
567
568     /*
569      * Replace all client information in the request with the
570      * impersonated client. (The audit entry containing the original
571      * client name will have been created before this point.)
572      */
573     _kdc_request_set_cname_nocopy((kdc_request_t)r, &s4ucname);
574     _kdc_request_set_client_princ_nocopy(r, &s4u_client_name);
575
576     _kdc_free_ent(r->context, r->clientdb, r->client);
577     r->client = s4u_client;
578     s4u_client = NULL;
579     r->clientdb = s4u_clientdb;
580     s4u_clientdb = NULL;
581
582     _kdc_request_set_canon_client_princ_nocopy(r, &s4u_canon_client_name);
583     _kdc_request_set_pac_nocopy(r, &s4u_pac);
584
585     r->pac_attributes = s4u_pac_attributes;
586
587     r->et.authtime = evidence_tkt.authtime;
588
589 out:
590     if (s4u_client)
591         _kdc_free_ent(r->context, s4u_clientdb, s4u_client);
592     if (s4u_server)
593         _kdc_free_ent(r->context, s4u_serverdb, s4u_server);
594     krb5_free_principal(r->context, s4u_client_name);
595     krb5_xfree(s4ucname);
596     krb5_free_principal(r->context, s4u_server_name);
597     krb5_xfree(s4usname);
598     krb5_free_principal(r->context, s4u_canon_client_name);
599     krb5_pac_free(r->context, s4u_pac);
600
601     free_EncTicketPart(&evidence_tkt);
602
603     return ret;
604 }