lib/hx509: revoke_print free revoke_context
[heimdal.git] / kdc / kerberos5.c
blobe87477bc1667766b75fed235384186fd97af696d
1 /*
2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "kdc_locl.h"
36 #define MAX_TIME ((time_t)((1U << 31) - 1))
38 #undef __attribute__
39 #define __attribute__(X)
41 void
42 _kdc_fix_time(time_t **t)
44 if(*t == NULL){
45 ALLOC(*t);
46 **t = MAX_TIME;
48 if(**t == 0) **t = MAX_TIME; /* fix for old clients */
51 static int
52 realloc_method_data(METHOD_DATA *md)
54 PA_DATA *pa;
55 pa = realloc(md->val, (md->len + 1) * sizeof(*md->val));
56 if(pa == NULL)
57 return ENOMEM;
58 md->val = pa;
59 md->len++;
60 return 0;
63 static void
64 set_salt_padata(METHOD_DATA *md, Salt *salt)
66 if (salt) {
67 realloc_method_data(md);
68 md->val[md->len - 1].padata_type = salt->type;
69 der_copy_octet_string(&salt->salt,
70 &md->val[md->len - 1].padata_value);
74 const PA_DATA*
75 _kdc_find_padata(const KDC_REQ *req, int *start, int type)
77 if (req->padata == NULL)
78 return NULL;
80 while((size_t)*start < req->padata->len){
81 (*start)++;
82 if(req->padata->val[*start - 1].padata_type == (unsigned)type)
83 return &req->padata->val[*start - 1];
85 return NULL;
89 * This is a hack to allow predefined weak services, like afs to
90 * still use weak types
93 krb5_boolean
94 _kdc_is_weak_exception(krb5_principal principal, krb5_enctype etype)
96 if (principal->name.name_string.len > 0 &&
97 strcmp(principal->name.name_string.val[0], "afs") == 0 &&
98 (etype == ETYPE_DES_CBC_CRC
99 || etype == ETYPE_DES_CBC_MD4
100 || etype == ETYPE_DES_CBC_MD5))
101 return TRUE;
102 return FALSE;
107 * Detect if `key' is the using the the precomputed `default_salt'.
110 static krb5_boolean
111 is_default_salt_p(const krb5_salt *default_salt, const Key *key)
113 if (key->salt == NULL)
114 return TRUE;
115 if (default_salt->salttype != key->salt->type)
116 return FALSE;
117 if (krb5_data_cmp(&default_salt->saltvalue, &key->salt->salt))
118 return FALSE;
119 return TRUE;
123 krb5_boolean
124 _kdc_is_anon_request(const KDC_REQ *req)
126 const KDC_REQ_BODY *b = &req->req_body;
129 * Versions of Heimdal from 0.9rc1 through 1.50 use bit 14 instead
130 * of 16 for request_anonymous, as indicated in the anonymous draft
131 * prior to version 11. Bit 14 is assigned to S4U2Proxy, but S4U2Proxy
132 * requests are only sent to the TGS and, in any case, would have an
133 * additional ticket present.
135 return b->kdc_options.request_anonymous ||
136 (b->kdc_options.cname_in_addl_tkt && !b->additional_tickets);
140 * return the first appropriate key of `princ' in `ret_key'. Look for
141 * all the etypes in (`etypes', `len'), stopping as soon as we find
142 * one, but preferring one that has default salt.
144 * XXX This function does way way too much. Split it up!
146 * XXX `etypes' and `len' are always `b->etype.val' and `b->etype.len' -- the
147 * etype list from the KDC-REQ-BODY, which is available here as
148 * `r->req->req_body', so we could just stop having it passed in.
150 * XXX Picking an enctype(s) for PA-ETYPE-INFO* is rather different than
151 * picking an enctype for a ticket's session key. The former is what we do
152 * here when `(flags & KFE_IS_PREAUTH)', the latter otherwise.
155 krb5_error_code
156 _kdc_find_etype(astgs_request_t r, uint32_t flags,
157 krb5_enctype *etypes, unsigned len,
158 krb5_enctype *ret_enctype, Key **ret_key,
159 krb5_boolean *ret_default_salt)
161 krb5_boolean use_strongest_session_key;
162 krb5_boolean is_preauth = flags & KFE_IS_PREAUTH;
163 krb5_boolean is_tgs = flags & KFE_IS_TGS;
164 hdb_entry *princ;
165 krb5_principal request_princ;
166 krb5_error_code ret;
167 krb5_salt def_salt;
168 krb5_enctype enctype = ETYPE_NULL;
169 const krb5_enctype *p;
170 Key *key = NULL;
171 size_t i, k, m;
173 if (is_preauth && (flags & KFE_USE_CLIENT) &&
174 r->client->flags.synthetic)
175 return KRB5KDC_ERR_ETYPE_NOSUPP;
177 if ((flags & KFE_USE_CLIENT) && !r->client->flags.synthetic) {
178 princ = r->client;
179 request_princ = r->client_princ;
180 } else {
181 princ = r->server;
182 request_princ = r->server->principal;
185 use_strongest_session_key =
186 is_preauth ? r->config->preauth_use_strongest_session_key
187 : (is_tgs ? r->config->tgt_use_strongest_session_key :
188 r->config->svc_use_strongest_session_key);
190 /* We'll want to avoid keys with v4 salted keys in the pre-auth case... */
191 ret = krb5_get_pw_salt(r->context, request_princ, &def_salt);
192 if (ret)
193 return ret;
195 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
198 * Pick an enctype that is in the intersection of:
200 * - permitted_enctypes (local policy)
201 * - requested enctypes (KDC-REQ-BODY's etype list)
202 * - the client's long-term keys' enctypes
203 * OR
204 * the server's configured etype list
206 * There are two sub-cases:
208 * - use local enctype preference (local policy)
209 * - use the client's preference list
212 if (use_strongest_session_key) {
214 * Pick the strongest key that the KDC, target service, and
215 * client all support, using the local cryptosystem enctype
216 * list in strongest-to-weakest order to drive the search.
218 * This is not what RFC4120 says to do, but it encourages
219 * adoption of stronger enctypes. This doesn't play well with
220 * clients that have multiple Kerberos client implementations
221 * with different supported enctype lists sharing the same ccache.
224 /* drive the search with local supported enctypes list */
225 p = krb5_kerberos_enctypes(r->context);
226 for (i = 0;
227 p[i] != ETYPE_NULL && enctype == ETYPE_NULL;
228 i++) {
229 if (krb5_enctype_valid(r->context, p[i]) != 0 &&
230 !_kdc_is_weak_exception(princ->principal, p[i]))
231 continue;
233 /* check that the client supports it too */
234 for (k = 0; k < len && enctype == ETYPE_NULL; k++) {
236 if (p[i] != etypes[k])
237 continue;
239 if (!is_preauth && (flags & KFE_USE_CLIENT)) {
241 * It suffices that the client says it supports this
242 * enctype in its KDC-REQ-BODY's etype list, which is what
243 * `etypes' is here.
245 ret = 0;
246 break;
249 /* check target princ support */
250 key = NULL;
251 if (!(flags & KFE_USE_CLIENT) && princ->etypes) {
253 * Use the etypes list from the server's HDB entry instead
254 * of deriving it from its long-term keys. This allows an
255 * entry to have just one long-term key but record support
256 * for multiple enctypes.
258 for (m = 0; m < princ->etypes->len; m++) {
259 if (p[i] == princ->etypes->val[m]) {
260 ret = 0;
261 break;
264 } else {
266 * Use the entry's long-term keys as the source of its
267 * supported enctypes, either because we're making
268 * PA-ETYPE-INFO* or because we're selecting a session key
269 * enctype.
271 while (hdb_next_enctype2key(r->context, princ, NULL,
272 p[i], &key) == 0) {
273 if (key->key.keyvalue.length == 0) {
274 ret = KRB5KDC_ERR_NULL_KEY;
275 continue;
277 enctype = p[i];
278 ret = 0;
279 if (is_preauth && ret_key != NULL &&
280 !is_default_salt_p(&def_salt, key))
281 continue;
286 } else {
288 * Pick the first key from the client's enctype list that is
289 * supported by the cryptosystem and by the given principal.
291 * RFC4120 says we SHOULD pick the first _strong_ key from the
292 * client's list... not the first key... If the admin disallows
293 * weak enctypes in krb5.conf and selects this key selection
294 * algorithm, then we get exactly what RFC4120 says.
296 for(i = 0; ret != 0 && i < len; i++) {
298 if (krb5_enctype_valid(r->context, etypes[i]) != 0 &&
299 !_kdc_is_weak_exception(princ->principal, etypes[i]))
300 continue;
302 key = NULL;
303 while (ret != 0 &&
304 hdb_next_enctype2key(r->context, princ, NULL,
305 etypes[i], &key) == 0) {
306 if (key->key.keyvalue.length == 0) {
307 ret = KRB5KDC_ERR_NULL_KEY;
308 continue;
310 enctype = etypes[i];
311 ret = 0;
312 if (is_preauth && ret_key != NULL &&
313 !is_default_salt_p(&def_salt, key))
314 continue;
319 if (enctype == ETYPE_NULL) {
321 * if the service principal is one for which there is a known 1DES
322 * exception and no other enctype matches both the client request and
323 * the service key list, provide a DES-CBC-CRC key.
325 if (ret_key == NULL &&
326 _kdc_is_weak_exception(princ->principal, ETYPE_DES_CBC_CRC)) {
327 ret = 0;
328 enctype = ETYPE_DES_CBC_CRC;
329 } else {
330 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
334 if (ret == 0) {
335 if (ret_enctype != NULL)
336 *ret_enctype = enctype;
337 if (ret_key != NULL)
338 *ret_key = key;
339 if (ret_default_salt != NULL)
340 *ret_default_salt = is_default_salt_p(&def_salt, key);
343 krb5_free_salt (r->context, def_salt);
344 return ret;
347 krb5_error_code
348 _kdc_make_anonymous_principalname (PrincipalName *pn)
350 pn->name_type = KRB5_NT_WELLKNOWN;
351 pn->name_string.len = 2;
352 pn->name_string.val = calloc(2, sizeof(*pn->name_string.val));
353 if (pn->name_string.val == NULL)
354 goto failed;
356 pn->name_string.val[0] = strdup(KRB5_WELLKNOWN_NAME);
357 if (pn->name_string.val[0] == NULL)
358 goto failed;
360 pn->name_string.val[1] = strdup(KRB5_ANON_NAME);
361 if (pn->name_string.val[1] == NULL)
362 goto failed;
364 return 0;
366 failed:
367 free_PrincipalName(pn);
369 pn->name_type = KRB5_NT_UNKNOWN;
370 pn->name_string.len = 0;
371 pn->name_string.val = NULL;
373 return ENOMEM;
376 static void
377 _kdc_r_log(astgs_request_t r, int level, const char *fmt, ...)
378 __attribute__ ((__format__ (__printf__, 3, 4)))
380 va_list ap;
381 char *s;
382 va_start(ap, fmt);
383 s = kdc_log_msg_va(r->context, r->config, level, fmt, ap);
384 if(s) free(s);
385 va_end(ap);
388 void
389 _kdc_set_const_e_text(astgs_request_t r, const char *e_text)
391 /* We should never see this */
392 if (r->e_text) {
393 kdc_log(r->context, r->config, 1,
394 "trying to replace e-text \"%s\" with \"%s\"\n",
395 r->e_text, e_text);
396 return;
399 r->e_text = e_text;
400 kdc_log(r->context, r->config, 4, "%s", e_text);
403 void
404 _kdc_set_e_text(astgs_request_t r, const char *fmt, ...)
405 __attribute__ ((__format__ (__printf__, 2, 3)))
407 va_list ap;
408 char *e_text = NULL;
409 int vasprintf_ret;
411 va_start(ap, fmt);
412 vasprintf_ret = vasprintf(&e_text, fmt, ap);
413 va_end(ap);
415 if (vasprintf_ret < 0 || !e_text) {
416 /* not much else to do... */
417 kdc_log(r->context, r->config, 1,
418 "Could not set e_text: %s (out of memory)", fmt);
419 return;
422 /* We should never see this */
423 if (r->e_text) {
424 kdc_log(r->context, r->config, 1, "trying to replace e-text: %s\n",
425 e_text);
426 free(e_text);
427 return;
430 r->e_text = e_text;
431 r->e_text_buf = e_text;
432 kdc_log(r->context, r->config, 4, "%s", e_text);
435 void
436 _kdc_log_timestamp(astgs_request_t r, const char *type,
437 KerberosTime authtime, KerberosTime *starttime,
438 KerberosTime endtime, KerberosTime *renew_till)
440 krb5_kdc_configuration *config = r->config;
441 char authtime_str[100], starttime_str[100],
442 endtime_str[100], renewtime_str[100];
444 if (authtime)
445 _kdc_audit_setkv_number((kdc_request_t)r, "auth", authtime);
446 if (starttime && *starttime)
447 _kdc_audit_setkv_number((kdc_request_t)r, "start", *starttime);
448 if (endtime)
449 _kdc_audit_setkv_number((kdc_request_t)r, "end", endtime);
450 if (renew_till && *renew_till)
451 _kdc_audit_setkv_number((kdc_request_t)r, "renew", *renew_till);
453 krb5_format_time(r->context, authtime,
454 authtime_str, sizeof(authtime_str), TRUE);
455 if (starttime)
456 krb5_format_time(r->context, *starttime,
457 starttime_str, sizeof(starttime_str), TRUE);
458 else
459 strlcpy(starttime_str, "unset", sizeof(starttime_str));
460 krb5_format_time(r->context, endtime,
461 endtime_str, sizeof(endtime_str), TRUE);
462 if (renew_till)
463 krb5_format_time(r->context, *renew_till,
464 renewtime_str, sizeof(renewtime_str), TRUE);
465 else
466 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
468 kdc_log(r->context, config, 4,
469 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
470 type, authtime_str, starttime_str, endtime_str, renewtime_str);
477 #ifdef PKINIT
479 static krb5_error_code
480 pa_pkinit_validate(astgs_request_t r, const PA_DATA *pa)
482 pk_client_params *pkp = NULL;
483 char *client_cert = NULL;
484 krb5_error_code ret;
486 ret = _kdc_pk_rd_padata(r, pa, &pkp);
487 if (ret || pkp == NULL) {
488 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
489 _kdc_r_log(r, 4, "Failed to decode PKINIT PA-DATA -- %s",
490 r->cname);
491 goto out;
494 ret = _kdc_pk_check_client(r, pkp, &client_cert);
495 if (client_cert)
496 _kdc_audit_addkv((kdc_request_t)r, 0, KDC_REQUEST_KV_PKINIT_CLIENT_CERT,
497 "%s", client_cert);
498 if (ret) {
499 _kdc_set_e_text(r, "PKINIT certificate not allowed to "
500 "impersonate principal");
501 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
502 KDC_AUTH_EVENT_CLIENT_NAME_UNAUTHORIZED);
503 goto out;
506 r->pa_endtime = _kdc_pk_endtime(pkp);
507 if (!r->client->flags.synthetic)
508 r->pa_max_life = _kdc_pk_max_life(pkp);
510 _kdc_r_log(r, 4, "PKINIT pre-authentication succeeded -- %s using %s",
511 r->cname, client_cert);
513 ret = _kdc_pk_mk_pa_reply(r, pkp);
514 if (ret) {
515 _kdc_set_e_text(r, "Failed to build PK-INIT reply");
516 goto out;
518 ret = _kdc_add_initial_verified_cas(r->context, r->config,
519 pkp, &r->et);
521 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
522 KDC_AUTH_EVENT_PREAUTH_SUCCEEDED);
524 out:
525 if (pkp)
526 _kdc_pk_free_client_param(r->context, pkp);
527 free(client_cert);
529 return ret;
532 #endif /* PKINIT */
534 static krb5_error_code
535 pa_gss_validate(astgs_request_t r, const PA_DATA *pa)
537 gss_client_params *gcp = NULL;
538 char *client_name = NULL;
539 krb5_error_code ret;
540 int open = 0;
542 ret = _kdc_gss_rd_padata(r, pa, &gcp, &open);
543 if (ret && gcp == NULL)
544 return ret;
546 if (open) {
547 ret = _kdc_gss_check_client(r, gcp, &client_name);
548 if (client_name)
549 _kdc_audit_addkv((kdc_request_t)r, 0, KDC_REQUEST_KV_GSS_INITIATOR,
550 "%s", client_name);
551 if (ret) {
552 _kdc_set_e_text(r, "GSS-API client not allowed to "
553 "impersonate principal");
554 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
555 KDC_AUTH_EVENT_CLIENT_NAME_UNAUTHORIZED);
556 goto out;
559 r->pa_endtime = _kdc_gss_endtime(r, gcp);
561 _kdc_r_log(r, 4, "GSS pre-authentication succeeded -- %s using %s",
562 r->cname, client_name);
563 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
564 KDC_AUTH_EVENT_PREAUTH_SUCCEEDED);
566 ret = _kdc_gss_mk_composite_name_ad(r, gcp);
567 if (ret) {
568 _kdc_set_e_text(r, "Failed to build GSS authorization data");
569 goto out;
573 ret = _kdc_gss_mk_pa_reply(r, gcp);
574 if (ret) {
575 if (ret != KRB5_KDC_ERR_MORE_PREAUTH_DATA_REQUIRED)
576 _kdc_set_e_text(r, "Failed to build GSS pre-authentication reply");
577 goto out;
580 ret = krb5_kdc_request_set_attribute((kdc_request_t)r,
581 HSTR("org.h5l.pa-gss-client-params"), gcp);
582 if (ret)
583 goto out;
585 out:
586 heim_release(gcp);
587 free(client_name);
589 return ret;
592 static krb5_error_code
593 pa_gss_finalize_pac(astgs_request_t r)
595 gss_client_params *gcp;
597 gcp = krb5_kdc_request_get_attribute((kdc_request_t)r, HSTR("org.h5l.pa-gss-client-params"));
599 heim_assert(gcp != NULL, "invalid GSS-API client params");
601 return _kdc_gss_finalize_pac(r, gcp);
604 static krb5_error_code
605 pa_enc_chal_validate(astgs_request_t r, const PA_DATA *pa)
607 krb5_data pepper1, pepper2;
608 int invalidPassword = 0;
609 EncryptedData enc_data;
610 krb5_enctype aenctype;
611 krb5_error_code ret;
612 struct Key *k;
613 size_t size;
614 int i;
616 heim_assert(r->armor_crypto != NULL, "ENC-CHAL called for non FAST");
618 if (_kdc_is_anon_request(&r->req)) {
619 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
620 kdc_log(r->context, r->config, 4, "ENC-CHALL doesn't support anon");
621 return ret;
624 if (r->client->flags.locked_out) {
625 ret = KRB5KDC_ERR_CLIENT_REVOKED;
626 kdc_log(r->context, r->config, 0,
627 "Client (%s) is locked out", r->cname);
628 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
629 KDC_AUTH_EVENT_CLIENT_LOCKED_OUT);
630 return ret;
633 ret = decode_EncryptedData(pa->padata_value.data,
634 pa->padata_value.length,
635 &enc_data,
636 &size);
637 if (ret) {
638 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
639 _kdc_r_log(r, 4, "Failed to decode PA-DATA -- %s",
640 r->cname);
641 return ret;
644 pepper1.data = "clientchallengearmor";
645 pepper1.length = strlen(pepper1.data);
646 pepper2.data = "challengelongterm";
647 pepper2.length = strlen(pepper2.data);
649 krb5_crypto_getenctype(r->context, r->armor_crypto, &aenctype);
651 kdc_log(r->context, r->config, 5, "FAST armor enctype is: %d", (int)aenctype);
653 for (i = 0; i < r->client->keys.len; i++) {
654 krb5_crypto challengecrypto, longtermcrypto;
655 krb5_keyblock challengekey;
657 k = &r->client->keys.val[i];
659 ret = krb5_crypto_init(r->context, &k->key, 0, &longtermcrypto);
660 if (ret)
661 continue;
663 ret = krb5_crypto_fx_cf2(r->context, r->armor_crypto, longtermcrypto,
664 &pepper1, &pepper2, aenctype,
665 &challengekey);
666 if (ret) {
667 krb5_crypto_destroy(r->context, longtermcrypto);
668 continue;
671 ret = krb5_crypto_init(r->context, &challengekey, 0,
672 &challengecrypto);
673 krb5_free_keyblock_contents(r->context, &challengekey);
674 if (ret) {
675 krb5_crypto_destroy(r->context, longtermcrypto);
676 continue;
679 ret = _krb5_validate_pa_enc_challenge(r->context,
680 challengecrypto,
681 KRB5_KU_ENC_CHALLENGE_CLIENT,
682 &enc_data,
683 r->cname);
684 krb5_crypto_destroy(r->context, challengecrypto);
685 if (ret) {
686 const char *msg;
687 krb5_error_code ret2;
688 char *str = NULL;
690 krb5_crypto_destroy(r->context, longtermcrypto);
692 invalidPassword = (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY);
693 if (!invalidPassword) {
694 goto out;
697 ret2 = krb5_enctype_to_string(r->context, k->key.keytype, &str);
698 if (ret2)
699 str = NULL;
700 msg = krb5_get_error_message(r->context, ret);
701 _kdc_r_log(r, 2, "Failed to decrypt ENC-CHAL -- %s "
702 "(enctype %s) error %s",
703 r->cname, str ? str : "unknown enctype", msg);
704 krb5_free_error_message(r->context, msg);
705 free(str);
707 continue;
711 * Found a key that the client used, lets pick that as the reply key
714 krb5_free_keyblock_contents(r->context, &r->reply_key);
715 ret = krb5_copy_keyblock_contents(r->context, &k->key, &r->reply_key);
716 if (ret) {
717 krb5_crypto_destroy(r->context, longtermcrypto);
718 goto out;
721 krb5_free_keyblock_contents(r->context, &challengekey);
724 * Provide KDC authentication to the client, uses a different
725 * challenge key (different pepper).
728 pepper1.data = "kdcchallengearmor";
729 pepper1.length = strlen(pepper1.data);
731 ret = krb5_crypto_fx_cf2(r->context, r->armor_crypto, longtermcrypto,
732 &pepper1, &pepper2, aenctype,
733 &challengekey);
734 krb5_crypto_destroy(r->context, longtermcrypto);
735 if (ret)
736 goto out;
738 ret = krb5_crypto_init(r->context, &challengekey, 0, &challengecrypto);
739 krb5_free_keyblock_contents(r->context, &challengekey);
740 if (ret)
741 goto out;
743 ret = _krb5_make_pa_enc_challenge(r->context, challengecrypto,
744 KRB5_KU_ENC_CHALLENGE_KDC,
745 r->rep.padata);
746 krb5_crypto_destroy(r->context, challengecrypto);
747 if (ret)
748 goto out;
750 set_salt_padata(r->rep.padata, k->salt);
753 * Success
755 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
756 KDC_AUTH_EVENT_VALIDATED_LONG_TERM_KEY);
757 goto out;
760 if (invalidPassword) {
761 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
762 KDC_AUTH_EVENT_WRONG_LONG_TERM_KEY);
763 ret = KRB5KDC_ERR_PREAUTH_FAILED;
764 } else {
765 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
767 out:
768 free_EncryptedData(&enc_data);
770 return ret;
773 static krb5_error_code
774 pa_enc_ts_validate(astgs_request_t r, const PA_DATA *pa)
776 EncryptedData enc_data;
777 krb5_error_code ret;
778 krb5_crypto crypto;
779 krb5_data ts_data;
780 PA_ENC_TS_ENC p;
781 size_t len;
782 Key *pa_key;
783 char *str;
785 if (r->armor_crypto && !r->config->enable_armored_pa_enc_timestamp) {
786 ret = KRB5KDC_ERR_POLICY;
787 kdc_log(r->context, r->config, 0,
788 "Armored encrypted timestamp pre-authentication is disabled");
789 return ret;
790 } else if (!r->armor_crypto && !r->config->enable_unarmored_pa_enc_timestamp) {
791 ret = KRB5KDC_ERR_POLICY;
792 kdc_log(r->context, r->config, 0,
793 "Unarmored encrypted timestamp pre-authentication is disabled");
794 return ret;
797 if (r->client->flags.locked_out) {
798 ret = KRB5KDC_ERR_CLIENT_REVOKED;
799 kdc_log(r->context, r->config, 0,
800 "Client (%s) is locked out", r->cname);
801 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
802 KDC_AUTH_EVENT_CLIENT_LOCKED_OUT);
803 return ret;
806 ret = decode_EncryptedData(pa->padata_value.data,
807 pa->padata_value.length,
808 &enc_data,
809 &len);
810 if (ret) {
811 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
812 _kdc_r_log(r, 4, "Failed to decode PA-DATA -- %s",
813 r->cname);
814 goto out;
817 ret = hdb_enctype2key(r->context, r->client, NULL,
818 enc_data.etype, &pa_key);
819 if(ret){
820 char *estr;
821 _kdc_set_e_text(r, "No key matching entype");
822 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
823 if(krb5_enctype_to_string(r->context, enc_data.etype, &estr))
824 estr = NULL;
825 if(estr == NULL)
826 _kdc_r_log(r, 4,
827 "No client key matching pa-data (%d) -- %s",
828 enc_data.etype, r->cname);
829 else
830 _kdc_r_log(r, 4,
831 "No client key matching pa-data (%s) -- %s",
832 estr, r->cname);
833 free(estr);
834 free_EncryptedData(&enc_data);
835 goto out;
838 try_next_key:
839 ret = krb5_crypto_init(r->context, &pa_key->key, 0, &crypto);
840 if (ret) {
841 const char *msg = krb5_get_error_message(r->context, ret);
842 _kdc_r_log(r, 4, "krb5_crypto_init failed: %s", msg);
843 krb5_free_error_message(r->context, msg);
844 free_EncryptedData(&enc_data);
845 goto out;
848 ret = krb5_decrypt_EncryptedData (r->context,
849 crypto,
850 KRB5_KU_PA_ENC_TIMESTAMP,
851 &enc_data,
852 &ts_data);
853 krb5_crypto_destroy(r->context, crypto);
855 * Since the user might have several keys with the same
856 * enctype but with diffrent salting, we need to try all
857 * the keys with the same enctype.
859 if(ret){
860 krb5_error_code ret2;
861 const char *msg = krb5_get_error_message(r->context, ret);
863 ret2 = krb5_enctype_to_string(r->context,
864 pa_key->key.keytype, &str);
865 if (ret2)
866 str = NULL;
867 _kdc_r_log(r, 2, "Failed to decrypt PA-DATA -- %s "
868 "(enctype %s) error %s",
869 r->cname, str ? str : "unknown enctype", msg);
870 krb5_xfree(str);
871 krb5_free_error_message(r->context, msg);
872 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_PA_ETYPE,
873 pa_key->key.keytype);
874 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
875 KDC_AUTH_EVENT_WRONG_LONG_TERM_KEY);
876 if(hdb_next_enctype2key(r->context, r->client, NULL,
877 enc_data.etype, &pa_key) == 0)
878 goto try_next_key;
880 free_EncryptedData(&enc_data);
882 ret = KRB5KDC_ERR_PREAUTH_FAILED;
883 goto out;
885 free_EncryptedData(&enc_data);
886 ret = decode_PA_ENC_TS_ENC(ts_data.data,
887 ts_data.length,
889 &len);
890 krb5_data_free(&ts_data);
891 if(ret){
892 ret = KRB5KDC_ERR_PREAUTH_FAILED;
893 _kdc_r_log(r, 4, "Failed to decode PA-ENC-TS_ENC -- %s",
894 r->cname);
895 goto out;
897 if (labs(kdc_time - p.patimestamp) > r->context->max_skew) {
898 char client_time[100];
900 krb5_format_time(r->context, p.patimestamp,
901 client_time, sizeof(client_time), TRUE);
903 ret = KRB5KRB_AP_ERR_SKEW;
904 _kdc_r_log(r, 4, "Too large time skew, "
905 "client time %s is out by %u > %u seconds -- %s",
906 client_time,
907 (unsigned)labs(kdc_time - p.patimestamp),
908 r->context->max_skew,
909 r->cname);
910 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
911 KDC_AUTH_EVENT_CLIENT_TIME_SKEW);
914 * The following is needed to make windows clients to
915 * retry using the timestamp in the error message, if
916 * there is a e_text, they become unhappy.
918 r->e_text = NULL;
919 free_PA_ENC_TS_ENC(&p);
920 goto out;
922 free_PA_ENC_TS_ENC(&p);
924 set_salt_padata(r->rep.padata, pa_key->salt);
926 ret = krb5_copy_keyblock_contents(r->context, &pa_key->key, &r->reply_key);
927 if (ret)
928 return ret;
930 ret = krb5_enctype_to_string(r->context, pa_key->key.keytype, &str);
931 if (ret)
932 str = NULL;
933 _kdc_r_log(r, 4, "ENC-TS Pre-authentication succeeded -- %s using %s",
934 r->cname, str ? str : "unknown enctype");
935 krb5_xfree(str);
936 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_PA_ETYPE,
937 pa_key->key.keytype);
938 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
939 KDC_AUTH_EVENT_VALIDATED_LONG_TERM_KEY);
941 ret = 0;
943 out:
945 return ret;
948 struct kdc_patypes {
949 int type;
950 char *name;
951 unsigned int flags;
952 #define PA_ANNOUNCE 1
953 #define PA_REQ_FAST 2 /* only use inside fast */
954 #define PA_SYNTHETIC_OK 4
955 #define PA_REPLACE_REPLY_KEY 8 /* PA mech replaces reply key */
956 #define PA_USES_LONG_TERM_KEY 16 /* PA mech uses client's long-term key */
957 krb5_error_code (*validate)(astgs_request_t, const PA_DATA *pa);
958 krb5_error_code (*finalize_pac)(astgs_request_t r);
959 void (*cleanup)(astgs_request_t r);
962 static const struct kdc_patypes pat[] = {
963 #ifdef PKINIT
965 KRB5_PADATA_PK_AS_REQ, "PK-INIT(ietf)",
966 PA_ANNOUNCE | PA_SYNTHETIC_OK | PA_REPLACE_REPLY_KEY,
967 pa_pkinit_validate, NULL, NULL
970 KRB5_PADATA_PK_AS_REQ_WIN, "PK-INIT(win2k)", PA_ANNOUNCE | PA_REPLACE_REPLY_KEY,
971 pa_pkinit_validate, NULL, NULL
974 KRB5_PADATA_PKINIT_KX, "Anonymous PK-INIT", PA_ANNOUNCE,
975 NULL, NULL, NULL
977 #else
978 { KRB5_PADATA_PK_AS_REQ, "PK-INIT(ietf)", 0, NULL , NULL, NULL },
979 { KRB5_PADATA_PK_AS_REQ_WIN, "PK-INIT(win2k)", 0, NULL, NULL, NULL },
980 { KRB5_PADATA_PKINIT_KX, "Anonymous PK-INIT", 0, NULL, NULL, NULL },
981 #endif
982 { KRB5_PADATA_PA_PK_OCSP_RESPONSE , "OCSP", 0, NULL, NULL, NULL },
984 KRB5_PADATA_ENC_TIMESTAMP , "ENC-TS",
985 PA_ANNOUNCE | PA_USES_LONG_TERM_KEY,
986 pa_enc_ts_validate, NULL, NULL
989 KRB5_PADATA_ENCRYPTED_CHALLENGE , "ENC-CHAL",
990 PA_ANNOUNCE | PA_USES_LONG_TERM_KEY | PA_REQ_FAST,
991 pa_enc_chal_validate, NULL, NULL
993 { KRB5_PADATA_REQ_ENC_PA_REP , "REQ-ENC-PA-REP", 0, NULL, NULL, NULL },
994 { KRB5_PADATA_FX_FAST, "FX-FAST", PA_ANNOUNCE, NULL, NULL, NULL },
995 { KRB5_PADATA_FX_ERROR, "FX-ERROR", 0, NULL, NULL, NULL },
996 { KRB5_PADATA_FX_COOKIE, "FX-COOKIE", 0, NULL, NULL, NULL },
998 KRB5_PADATA_GSS , "GSS",
999 PA_ANNOUNCE | PA_SYNTHETIC_OK | PA_REPLACE_REPLY_KEY,
1000 pa_gss_validate, pa_gss_finalize_pac, NULL
1004 static void
1005 log_patypes(astgs_request_t r, METHOD_DATA *padata)
1007 krb5_kdc_configuration *config = r->config;
1008 struct rk_strpool *p = NULL;
1009 char *str;
1010 size_t n, m;
1012 for (n = 0; n < padata->len; n++) {
1013 for (m = 0; m < sizeof(pat) / sizeof(pat[0]); m++) {
1014 if (padata->val[n].padata_type == pat[m].type) {
1015 p = rk_strpoolprintf(p, "%s", pat[m].name);
1016 break;
1019 if (m == sizeof(pat) / sizeof(pat[0]))
1020 p = rk_strpoolprintf(p, "%d", padata->val[n].padata_type);
1021 if (p && n + 1 < padata->len)
1022 p = rk_strpoolprintf(p, ", ");
1023 if (p == NULL) {
1024 kdc_log(r->context, config, 1, "out of memory");
1025 return;
1028 if (p == NULL)
1029 p = rk_strpoolprintf(p, "none");
1031 str = rk_strpoolcollect(p);
1032 kdc_log(r->context, config, 4, "Client sent patypes: %s", str);
1033 _kdc_audit_addkv((kdc_request_t)r, KDC_AUDIT_EATWHITE,
1034 "client-pa", "%s", str);
1035 free(str);
1038 static krb5_boolean
1039 pa_used_flag_isset(astgs_request_t r, unsigned int flag)
1041 if (r->pa_used == NULL)
1042 return FALSE;
1044 return (r->pa_used->flags & flag) == flag;
1051 krb5_error_code
1052 _kdc_encode_reply(krb5_context context,
1053 krb5_kdc_configuration *config,
1054 astgs_request_t r, uint32_t nonce,
1055 krb5_enctype etype,
1056 int skvno, const EncryptionKey *skey,
1057 int ckvno,
1058 int rk_is_subkey,
1059 krb5_data *reply)
1061 unsigned char *buf;
1062 size_t buf_size;
1063 size_t len = 0;
1064 krb5_error_code ret;
1065 krb5_crypto crypto;
1066 KDC_REP *rep = &r->rep;
1067 EncTicketPart *et = &r->et;
1068 EncKDCRepPart *ek = &r->ek;
1070 heim_assert(rep->padata != NULL, "reply padata uninitialized");
1072 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
1073 if(ret) {
1074 const char *msg = krb5_get_error_message(context, ret);
1075 kdc_log(context, config, 4, "Failed to encode ticket: %s", msg);
1076 krb5_free_error_message(context, msg);
1077 return ret;
1079 if(buf_size != len)
1080 krb5_abortx(context, "Internal error in ASN.1 encoder");
1082 ret = krb5_crypto_init(context, skey, etype, &crypto);
1083 if (ret) {
1084 const char *msg = krb5_get_error_message(context, ret);
1085 kdc_log(context, config, 4, "krb5_crypto_init failed: %s", msg);
1086 krb5_free_error_message(context, msg);
1087 free(buf);
1088 return ret;
1091 ret = krb5_encrypt_EncryptedData(context,
1092 crypto,
1093 KRB5_KU_TICKET,
1094 buf,
1095 len,
1096 skvno,
1097 &rep->ticket.enc_part);
1098 free(buf);
1099 krb5_crypto_destroy(context, crypto);
1100 if(ret) {
1101 const char *msg = krb5_get_error_message(context, ret);
1102 kdc_log(context, config, 4, "Failed to encrypt data: %s", msg);
1103 krb5_free_error_message(context, msg);
1104 return ret;
1107 if (r && r->armor_crypto) {
1108 KrbFastFinished finished;
1109 krb5_data data;
1111 kdc_log(context, config, 4, "FAST armor protection");
1113 memset(&finished, 0, sizeof(finished));
1114 krb5_data_zero(&data);
1116 finished.timestamp = kdc_time;
1117 finished.usec = 0;
1118 finished.crealm = et->crealm;
1119 finished.cname = et->cname;
1121 ASN1_MALLOC_ENCODE(Ticket, data.data, data.length,
1122 &rep->ticket, &len, ret);
1123 if (ret)
1124 return ret;
1125 if (data.length != len)
1126 krb5_abortx(context, "internal asn.1 error");
1128 ret = krb5_create_checksum(context, r->armor_crypto,
1129 KRB5_KU_FAST_FINISHED, 0,
1130 data.data, data.length,
1131 &finished.ticket_checksum);
1132 krb5_data_free(&data);
1133 if (ret)
1134 return ret;
1136 ret = _kdc_fast_mk_response(context, r->armor_crypto,
1137 rep->padata, &r->strengthen_key, &finished,
1138 nonce, &data);
1139 free_Checksum(&finished.ticket_checksum);
1140 if (ret)
1141 return ret;
1143 free_METHOD_DATA(r->rep.padata);
1145 ret = krb5_padata_add(context, rep->padata,
1146 KRB5_PADATA_FX_FAST,
1147 data.data, data.length);
1148 if (ret)
1149 return ret;
1152 * Hide client name for privacy reasons
1154 if (r->fast.flags.requested_hidden_names) {
1155 Realm anon_realm = KRB5_ANON_REALM;
1157 free_Realm(&rep->crealm);
1158 ret = copy_Realm(&anon_realm, &rep->crealm);
1159 if (ret == 0) {
1160 free_PrincipalName(&rep->cname);
1161 ret = _kdc_make_anonymous_principalname(&rep->cname);
1163 if (ret)
1164 return ret;
1168 if (rep->padata->len == 0) {
1169 free_METHOD_DATA(rep->padata);
1170 free(rep->padata);
1171 rep->padata = NULL;
1174 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
1175 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
1176 else
1177 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
1178 if(ret) {
1179 const char *msg = krb5_get_error_message(context, ret);
1180 kdc_log(context, config, 4, "Failed to encode KDC-REP: %s", msg);
1181 krb5_free_error_message(context, msg);
1182 return ret;
1184 if(buf_size != len) {
1185 free(buf);
1186 kdc_log(context, config, 4, "Internal error in ASN.1 encoder");
1187 _kdc_set_e_text(r, "KDC internal error");
1188 return KRB5KRB_ERR_GENERIC;
1190 ret = krb5_crypto_init(context, &r->reply_key, 0, &crypto);
1191 if (ret) {
1192 const char *msg = krb5_get_error_message(context, ret);
1193 free(buf);
1194 kdc_log(context, config, 4, "krb5_crypto_init failed: %s", msg);
1195 krb5_free_error_message(context, msg);
1196 return ret;
1198 if(rep->msg_type == krb_as_rep) {
1199 krb5_encrypt_EncryptedData(context,
1200 crypto,
1201 KRB5_KU_AS_REP_ENC_PART,
1202 buf,
1203 len,
1204 ckvno,
1205 &rep->enc_part);
1206 free(buf);
1207 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
1208 } else {
1209 krb5_encrypt_EncryptedData(context,
1210 crypto,
1211 rk_is_subkey ? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY : KRB5_KU_TGS_REP_ENC_PART_SESSION,
1212 buf,
1213 len,
1214 ckvno,
1215 &rep->enc_part);
1216 free(buf);
1217 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
1219 krb5_crypto_destroy(context, crypto);
1220 if(ret) {
1221 const char *msg = krb5_get_error_message(context, ret);
1222 kdc_log(context, config, 4, "Failed to encode KDC-REP: %s", msg);
1223 krb5_free_error_message(context, msg);
1224 return ret;
1226 if(buf_size != len) {
1227 free(buf);
1228 kdc_log(context, config, 4, "Internal error in ASN.1 encoder");
1229 _kdc_set_e_text(r, "KDC internal error");
1230 return KRB5KRB_ERR_GENERIC;
1232 reply->data = buf;
1233 reply->length = buf_size;
1234 return 0;
1241 static krb5_error_code
1242 make_etype_info_entry(krb5_context context,
1243 ETYPE_INFO_ENTRY *ent,
1244 Key *key,
1245 krb5_boolean include_salt)
1247 ent->etype = key->key.keytype;
1248 if (key->salt && include_salt){
1249 #if 0
1250 ALLOC(ent->salttype);
1252 if(key->salt->type == hdb_pw_salt)
1253 *ent->salttype = 0; /* or 1? or NULL? */
1254 else if(key->salt->type == hdb_afs3_salt)
1255 *ent->salttype = 2;
1256 else {
1257 kdc_log(context, config, 4, "unknown salt-type: %d",
1258 key->salt->type);
1259 return KRB5KRB_ERR_GENERIC;
1261 /* according to `the specs', we can't send a salt if
1262 we have AFS3 salted key, but that requires that you
1263 *know* what cell you are using (e.g by assuming
1264 that the cell is the same as the realm in lower
1265 case) */
1266 #elif 0
1267 ALLOC(ent->salttype);
1268 *ent->salttype = key->salt->type;
1269 #else
1271 * We shouldn't sent salttype since it is incompatible with the
1272 * specification and it breaks windows clients. The afs
1273 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
1274 * implemented in Heimdal 0.7 and later.
1276 ent->salttype = NULL;
1277 #endif
1278 krb5_copy_data(context, &key->salt->salt,
1279 &ent->salt);
1280 } else {
1281 /* we return no salt type at all, as that should indicate
1282 * the default salt type and make everybody happy. some
1283 * systems (like w2k) dislike being told the salt type
1284 * here. */
1286 ent->salttype = NULL;
1287 ent->salt = NULL;
1289 return 0;
1292 static krb5_error_code
1293 get_pa_etype_info(krb5_context context,
1294 krb5_kdc_configuration *config,
1295 METHOD_DATA *md, Key *ckey,
1296 krb5_boolean include_salt)
1298 krb5_error_code ret = 0;
1299 ETYPE_INFO pa;
1300 unsigned char *buf;
1301 size_t len;
1304 pa.len = 1;
1305 pa.val = calloc(1, sizeof(pa.val[0]));
1306 if(pa.val == NULL)
1307 return ENOMEM;
1309 ret = make_etype_info_entry(context, &pa.val[0], ckey, include_salt);
1310 if (ret) {
1311 free_ETYPE_INFO(&pa);
1312 return ret;
1315 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
1316 free_ETYPE_INFO(&pa);
1317 if(ret)
1318 return ret;
1319 ret = realloc_method_data(md);
1320 if(ret) {
1321 free(buf);
1322 return ret;
1324 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
1325 md->val[md->len - 1].padata_value.length = len;
1326 md->val[md->len - 1].padata_value.data = buf;
1327 return 0;
1334 extern int _krb5_AES_SHA1_string_to_default_iterator;
1335 extern int _krb5_AES_SHA2_string_to_default_iterator;
1337 static krb5_error_code
1338 make_s2kparams(int value, size_t len, krb5_data **ps2kparams)
1340 krb5_data *s2kparams;
1341 krb5_error_code ret;
1343 ALLOC(s2kparams);
1344 if (s2kparams == NULL)
1345 return ENOMEM;
1346 ret = krb5_data_alloc(s2kparams, len);
1347 if (ret) {
1348 free(s2kparams);
1349 return ret;
1351 _krb5_put_int(s2kparams->data, value, len);
1352 *ps2kparams = s2kparams;
1353 return 0;
1356 static krb5_error_code
1357 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent,
1358 Key *key,
1359 krb5_boolean include_salt)
1361 krb5_error_code ret;
1363 ent->etype = key->key.keytype;
1364 if (key->salt && include_salt) {
1365 ALLOC(ent->salt);
1366 if (ent->salt == NULL)
1367 return ENOMEM;
1368 *ent->salt = malloc(key->salt->salt.length + 1);
1369 if (*ent->salt == NULL) {
1370 free(ent->salt);
1371 ent->salt = NULL;
1372 return ENOMEM;
1374 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
1375 (*ent->salt)[key->salt->salt.length] = '\0';
1376 } else
1377 ent->salt = NULL;
1379 ent->s2kparams = NULL;
1381 switch (key->key.keytype) {
1382 case ETYPE_AES128_CTS_HMAC_SHA1_96:
1383 case ETYPE_AES256_CTS_HMAC_SHA1_96:
1384 ret = make_s2kparams(_krb5_AES_SHA1_string_to_default_iterator,
1385 4, &ent->s2kparams);
1386 break;
1387 case KRB5_ENCTYPE_AES128_CTS_HMAC_SHA256_128:
1388 case KRB5_ENCTYPE_AES256_CTS_HMAC_SHA384_192:
1389 ret = make_s2kparams(_krb5_AES_SHA2_string_to_default_iterator,
1390 4, &ent->s2kparams);
1391 break;
1392 case ETYPE_DES_CBC_CRC:
1393 case ETYPE_DES_CBC_MD4:
1394 case ETYPE_DES_CBC_MD5:
1395 /* Check if this was a AFS3 salted key */
1396 if(key->salt && key->salt->type == hdb_afs3_salt)
1397 ret = make_s2kparams(1, 1, &ent->s2kparams);
1398 else
1399 ret = 0;
1400 break;
1401 default:
1402 ret = 0;
1403 break;
1405 return ret;
1409 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
1410 * database (client supported enctypes first, then the unsupported
1411 * enctypes).
1414 static krb5_error_code
1415 get_pa_etype_info2(krb5_context context,
1416 krb5_kdc_configuration *config,
1417 METHOD_DATA *md, Key *ckey,
1418 krb5_boolean include_salt)
1420 krb5_error_code ret = 0;
1421 ETYPE_INFO2 pa;
1422 unsigned char *buf;
1423 size_t len;
1425 pa.len = 1;
1426 pa.val = calloc(1, sizeof(pa.val[0]));
1427 if(pa.val == NULL)
1428 return ENOMEM;
1430 ret = make_etype_info2_entry(&pa.val[0], ckey, include_salt);
1431 if (ret) {
1432 free_ETYPE_INFO2(&pa);
1433 return ret;
1436 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
1437 free_ETYPE_INFO2(&pa);
1438 if(ret)
1439 return ret;
1440 ret = realloc_method_data(md);
1441 if(ret) {
1442 free(buf);
1443 return ret;
1445 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
1446 md->val[md->len - 1].padata_value.length = len;
1447 md->val[md->len - 1].padata_value.data = buf;
1448 return 0;
1452 * Return 0 if the client have only older enctypes, this is for
1453 * determining if the server should send ETYPE_INFO2 or not.
1456 static int
1457 newer_enctype_present(krb5_context context,
1458 struct KDC_REQ_BODY_etype *etype_list)
1460 size_t i;
1462 for (i = 0; i < etype_list->len; i++) {
1463 if (!krb5_is_enctype_old(context, etype_list->val[i]))
1464 return 1;
1466 return 0;
1469 static krb5_error_code
1470 get_pa_etype_info_both(krb5_context context,
1471 krb5_kdc_configuration *config,
1472 struct KDC_REQ_BODY_etype *etype_list,
1473 METHOD_DATA *md, Key *ckey,
1474 krb5_boolean include_salt)
1476 krb5_error_code ret;
1479 * Windows 2019 (and earlier versions) always sends the salt
1480 * and Samba has testsuites that check this behaviour, so a
1481 * Samba AD DC will set this flag to match the AS-REP packet
1482 * more closely.
1484 if (config->force_include_pa_etype_salt)
1485 include_salt = TRUE;
1488 * RFC4120 requires:
1489 * When the AS server is to include pre-authentication data in a
1490 * KRB-ERROR or in an AS-REP, it MUST use PA-ETYPE-INFO2, not
1491 * PA-ETYPE-INFO, if the etype field of the client's AS-REQ lists
1492 * at least one "newer" encryption type. Otherwise (when the etype
1493 * field of the client's AS-REQ does not list any "newer" encryption
1494 * types), it MUST send both PA-ETYPE-INFO2 and PA-ETYPE-INFO (both
1495 * with an entry for each enctype). A "newer" enctype is any enctype
1496 * first officially specified concurrently with or subsequent to the
1497 * issue of this RFC. The enctypes DES, 3DES, or RC4 and any defined
1498 * in [RFC1510] are not "newer" enctypes.
1500 * It goes on to state:
1501 * The preferred ordering of the "hint" pre-authentication data that
1502 * affect client key selection is: ETYPE-INFO2, followed by ETYPE-INFO,
1503 * followed by PW-SALT. As noted in Section 3.1.3, a KDC MUST NOT send
1504 * ETYPE-INFO or PW-SALT when the client's AS-REQ includes at least one
1505 * "newer" etype.
1508 ret = get_pa_etype_info2(context, config, md, ckey, include_salt);
1509 if (ret)
1510 return ret;
1512 if (!newer_enctype_present(context, etype_list))
1513 ret = get_pa_etype_info(context, config, md, ckey, include_salt);
1515 return ret;
1522 void
1523 _log_astgs_req(astgs_request_t r, krb5_enctype setype)
1525 const KDC_REQ_BODY *b = &r->req.req_body;
1526 krb5_enctype cetype = r->reply_key.keytype;
1527 krb5_error_code ret;
1528 struct rk_strpool *p;
1529 struct rk_strpool *s = NULL;
1530 char *str;
1531 char *cet;
1532 char *set;
1533 size_t i;
1536 * we are collecting ``p'' and ``s''. The former is a textual
1537 * representation of the enctypes as strings which will be used
1538 * for debugging. The latter is a terse comma separated list of
1539 * the %d's of the enctypes to emit into our audit trail to
1540 * conserve space in the logs.
1543 p = rk_strpoolprintf(NULL, "%s", "Client supported enctypes: ");
1545 for (i = 0; i < b->etype.len; i++) {
1546 ret = krb5_enctype_to_string(r->context, b->etype.val[i], &str);
1547 if (ret == 0) {
1548 p = rk_strpoolprintf(p, "%s", str);
1549 free(str);
1550 } else
1551 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
1552 if (p == NULL) {
1553 rk_strpoolfree(s);
1554 _kdc_r_log(r, 4, "out of memory");
1555 return;
1557 s = rk_strpoolprintf(s, "%d", b->etype.val[i]);
1558 if (i + 1 < b->etype.len) {
1559 p = rk_strpoolprintf(p, ", ");
1560 s = rk_strpoolprintf(s, ",");
1563 if (p == NULL)
1564 p = rk_strpoolprintf(p, "no encryption types");
1566 str = rk_strpoolcollect(s);
1567 if (str)
1568 _kdc_audit_addkv((kdc_request_t)r, KDC_AUDIT_EATWHITE, "etypes", "%s",
1569 str);
1570 free(str);
1572 ret = krb5_enctype_to_string(r->context, cetype, &cet);
1573 if(ret == 0) {
1574 ret = krb5_enctype_to_string(r->context, setype, &set);
1575 if (ret == 0) {
1576 p = rk_strpoolprintf(p, ", using %s/%s", cet, set);
1577 free(set);
1579 free(cet);
1581 if (ret != 0)
1582 p = rk_strpoolprintf(p, ", using enctypes %d/%d",
1583 cetype, setype);
1585 str = rk_strpoolcollect(p);
1586 if (str)
1587 _kdc_r_log(r, 4, "%s", str);
1588 free(str);
1590 _kdc_audit_addkv((kdc_request_t)r, 0, "etype", "%d/%d", cetype, setype);
1593 char fixedstr[128];
1594 int result;
1596 result = unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
1597 fixedstr, sizeof(fixedstr));
1598 if (result > 0) {
1599 _kdc_r_log(r, 4, "Requested flags: %s", fixedstr);
1600 _kdc_audit_addkv((kdc_request_t)r, KDC_AUDIT_EATWHITE,
1601 "flags", "%s", fixedstr);
1607 * verify the flags on `client' and `server', returning 0
1608 * if they are OK and generating an error messages and returning
1609 * and error code otherwise.
1612 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
1613 kdc_check_flags(astgs_request_t r,
1614 krb5_boolean is_as_req,
1615 hdb_entry *client,
1616 hdb_entry *server)
1618 if (client != NULL) {
1619 /* check client */
1620 if (client->flags.locked_out) {
1621 _kdc_audit_addreason((kdc_request_t)r, "Client is locked out");
1622 return KRB5KDC_ERR_CLIENT_REVOKED;
1625 if (client->flags.invalid) {
1626 _kdc_audit_addreason((kdc_request_t)r,
1627 "Client has invalid bit set");
1628 return KRB5KDC_ERR_POLICY;
1631 if (!client->flags.client) {
1632 _kdc_audit_addreason((kdc_request_t)r,
1633 "Principal may not act as client");
1634 return KRB5KDC_ERR_POLICY;
1637 if (client->valid_start && *client->valid_start > kdc_time) {
1638 char starttime_str[100];
1639 krb5_format_time(r->context, *client->valid_start,
1640 starttime_str, sizeof(starttime_str), TRUE);
1641 _kdc_audit_addreason((kdc_request_t)r, "Client not yet valid "
1642 "until %s", starttime_str);
1643 return KRB5KDC_ERR_CLIENT_NOTYET;
1646 if (client->valid_end && *client->valid_end < kdc_time) {
1647 char endtime_str[100];
1648 krb5_format_time(r->context, *client->valid_end,
1649 endtime_str, sizeof(endtime_str), TRUE);
1650 _kdc_audit_addreason((kdc_request_t)r, "Client expired at %s",
1651 endtime_str);
1652 return KRB5KDC_ERR_NAME_EXP;
1655 if (client->flags.require_pwchange &&
1656 (server == NULL || !server->flags.change_pw))
1657 return KRB5KDC_ERR_KEY_EXPIRED;
1659 if (client->pw_end && *client->pw_end < kdc_time
1660 && (server == NULL || !server->flags.change_pw)) {
1661 char pwend_str[100];
1662 krb5_format_time(r->context, *client->pw_end,
1663 pwend_str, sizeof(pwend_str), TRUE);
1664 _kdc_audit_addreason((kdc_request_t)r, "Client's key has expired "
1665 "at %s", pwend_str);
1666 return KRB5KDC_ERR_KEY_EXPIRED;
1670 /* check server */
1672 if (server != NULL) {
1673 if (server->flags.locked_out) {
1674 _kdc_audit_addreason((kdc_request_t)r, "Server locked out");
1675 return KRB5KDC_ERR_SERVICE_REVOKED;
1677 if (server->flags.invalid) {
1678 _kdc_audit_addreason((kdc_request_t)r,
1679 "Server has invalid flag set");
1680 return KRB5KDC_ERR_POLICY;
1682 if (!server->flags.server) {
1683 _kdc_audit_addreason((kdc_request_t)r,
1684 "Principal may not act as server");
1685 return KRB5KDC_ERR_POLICY;
1688 if (!is_as_req && server->flags.initial) {
1689 _kdc_audit_addreason((kdc_request_t)r,
1690 "AS-REQ is required for server");
1691 return KRB5KDC_ERR_POLICY;
1694 if (server->valid_start && *server->valid_start > kdc_time) {
1695 char starttime_str[100];
1696 krb5_format_time(r->context, *server->valid_start,
1697 starttime_str, sizeof(starttime_str), TRUE);
1698 _kdc_audit_addreason((kdc_request_t)r, "Server not yet valid "
1699 "until %s", starttime_str);
1700 return KRB5KDC_ERR_SERVICE_NOTYET;
1703 if (server->valid_end && *server->valid_end < kdc_time) {
1704 char endtime_str[100];
1705 krb5_format_time(r->context, *server->valid_end,
1706 endtime_str, sizeof(endtime_str), TRUE);
1707 _kdc_audit_addreason((kdc_request_t)r, "Server expired at %s",
1708 endtime_str);
1709 return KRB5KDC_ERR_SERVICE_EXP;
1712 if (server->pw_end && *server->pw_end < kdc_time) {
1713 char pwend_str[100];
1714 krb5_format_time(r->context, *server->pw_end,
1715 pwend_str, sizeof(pwend_str), TRUE);
1716 _kdc_audit_addreason((kdc_request_t)r, "Server's key has expired "
1717 "at %s", pwend_str);
1718 return KRB5KDC_ERR_KEY_EXPIRED;
1721 return 0;
1725 * Return TRUE if `from' is part of `addresses' taking into consideration
1726 * the configuration variables that tells us how strict we should be about
1727 * these checks
1730 krb5_boolean
1731 _kdc_check_addresses(astgs_request_t r, HostAddresses *addresses,
1732 const struct sockaddr *from)
1734 krb5_kdc_configuration *config = r->config;
1735 krb5_error_code ret;
1736 krb5_address addr;
1737 krb5_boolean result;
1738 krb5_boolean only_netbios = TRUE;
1739 size_t i;
1741 if (!config->check_ticket_addresses && !config->warn_ticket_addresses)
1742 return TRUE;
1745 * Fields of HostAddresses type are always OPTIONAL and should be non-
1746 * empty, but we check for empty just in case as our compiler doesn't
1747 * support size constraints on SEQUENCE OF.
1749 if (addresses == NULL || addresses->len == 0)
1750 return config->allow_null_ticket_addresses;
1752 for (i = 0; i < addresses->len; ++i) {
1753 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
1754 only_netbios = FALSE;
1758 /* Windows sends it's netbios name, which I can only assume is
1759 * used for the 'allowed workstations' check. This is painful,
1760 * but we still want to check IP addresses if they happen to be
1761 * present.
1764 if(only_netbios)
1765 return config->allow_null_ticket_addresses;
1767 ret = krb5_sockaddr2address (r->context, from, &addr);
1768 if(ret)
1769 return FALSE;
1771 result = krb5_address_search(r->context, &addr, addresses);
1772 krb5_free_address (r->context, &addr);
1773 return result;
1779 krb5_error_code
1780 _kdc_check_anon_policy(astgs_request_t r)
1782 if (!r->config->allow_anonymous) {
1783 _kdc_audit_addreason((kdc_request_t)r,
1784 "Anonymous tickets denied by local policy");
1785 return KRB5KDC_ERR_POLICY;
1788 return 0;
1792 * Determine whether the client requested a PAC be included
1793 * or excluded explictly, or whether it doesn't care.
1796 static uint64_t
1797 get_pac_attributes(krb5_context context, KDC_REQ *req)
1799 krb5_error_code ret;
1800 PA_PAC_REQUEST pacreq;
1801 const PA_DATA *pa;
1802 int i = 0;
1803 uint32_t pac_attributes;
1805 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
1806 if (pa == NULL)
1807 return KRB5_PAC_WAS_GIVEN_IMPLICITLY;
1809 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
1810 pa->padata_value.length,
1811 &pacreq,
1812 NULL);
1813 if (ret)
1814 return KRB5_PAC_WAS_GIVEN_IMPLICITLY;
1816 pac_attributes = pacreq.include_pac ? KRB5_PAC_WAS_REQUESTED : 0;
1817 free_PA_PAC_REQUEST(&pacreq);
1818 return pac_attributes;
1825 static krb5_error_code
1826 generate_pac(astgs_request_t r, const Key *skey, const Key *tkey,
1827 krb5_boolean is_tgs)
1829 krb5_error_code ret;
1830 krb5_data data;
1831 uint16_t rodc_id;
1832 krb5_principal client;
1833 krb5_const_principal canon_princ = NULL;
1835 r->pac_attributes = get_pac_attributes(r->context, &r->req);
1836 _kdc_audit_setkv_number((kdc_request_t)r, "pac_attributes",
1837 r->pac_attributes);
1839 if (!_kdc_include_pac_p(r))
1840 return 0;
1843 * When a PA mech does not use the client's long-term key, the PAC
1844 * may include the client's long-term key (encrypted in the reply key)
1845 * for use by other shared secret authentication protocols, e.g. NTLM.
1846 * Validate a PA mech was actually used before doing this.
1849 ret = _kdc_pac_generate(r->context,
1850 r->config,
1851 r->client,
1852 r->server,
1853 r->pa_used && !pa_used_flag_isset(r, PA_USES_LONG_TERM_KEY)
1854 ? &r->reply_key : NULL,
1855 r->pac_attributes,
1856 &r->pac);
1857 if (ret) {
1858 _kdc_r_log(r, 4, "PAC generation failed for -- %s",
1859 r->cname);
1860 return ret;
1862 if (r->pac == NULL)
1863 return 0;
1865 rodc_id = r->server->kvno >> 16;
1867 /* libkrb5 expects ticket and PAC client names to match */
1868 ret = _krb5_principalname2krb5_principal(r->context, &client,
1869 r->et.cname, r->et.crealm);
1870 if (ret)
1871 return ret;
1874 * Include the canonical name of the principal in the authorization
1875 * data, if the realms match (if they don't, then the KDC could
1876 * impersonate any realm. Windows always canonicalizes the realm,
1877 * but Heimdal permits aliases between realms.)
1879 if (krb5_realm_compare(r->context, client, r->canon_client_princ)) {
1880 char *cpn = NULL;
1882 canon_princ = r->canon_client_princ;
1884 krb5_unparse_name(r->context, canon_princ, &cpn);
1885 _kdc_audit_addkv((kdc_request_t)r, 0, "canon_client_name", "%s",
1886 cpn ? cpn : "<unknown>");
1887 krb5_xfree(cpn);
1890 if (r->pa_used && r->pa_used->finalize_pac) {
1891 ret = r->pa_used->finalize_pac(r);
1892 if (ret)
1893 return ret;
1896 ret = _krb5_pac_sign(r->context,
1897 r->pac,
1898 r->et.authtime,
1899 client,
1900 &skey->key, /* Server key */
1901 &tkey->key, /* TGS key */
1902 rodc_id,
1903 NULL, /* UPN */
1904 canon_princ,
1905 is_tgs ? &r->pac_attributes : NULL,
1906 &data);
1907 krb5_free_principal(r->context, client);
1908 krb5_pac_free(r->context, r->pac);
1909 r->pac = NULL;
1910 if (ret) {
1911 _kdc_r_log(r, 4, "PAC signing failed for -- %s",
1912 r->cname);
1913 return ret;
1916 ret = _kdc_tkt_insert_pac(r->context, &r->et, &data);
1917 krb5_data_free(&data);
1919 return ret;
1926 krb5_boolean
1927 _kdc_is_anonymous(krb5_context context, krb5_const_principal principal)
1929 return krb5_principal_is_anonymous(context, principal, KRB5_ANON_MATCH_ANY);
1933 * Returns TRUE if principal is the unauthenticated anonymous identity,
1934 * i.e. WELLKNOWN/ANONYMOUS@WELLKNOWN:ANONYMOUS. Unfortunately due to
1935 * backwards compatibility logic in krb5_principal_is_anonymous() we
1936 * have to use our own implementation.
1939 krb5_boolean
1940 _kdc_is_anonymous_pkinit(krb5_context context, krb5_const_principal principal)
1942 return _kdc_is_anonymous(context, principal) &&
1943 strcmp(principal->realm, KRB5_ANON_REALM) == 0;
1946 static int
1947 require_preauth_p(astgs_request_t r)
1949 return r->config->require_preauth
1950 || r->client->flags.require_preauth
1951 || r->server->flags.require_preauth;
1959 static krb5_error_code
1960 add_enc_pa_rep(astgs_request_t r)
1962 krb5_error_code ret;
1963 krb5_crypto crypto;
1964 Checksum checksum;
1965 krb5_data cdata;
1966 size_t len;
1968 ret = krb5_crypto_init(r->context, &r->reply_key, 0, &crypto);
1969 if (ret)
1970 return ret;
1972 ret = krb5_create_checksum(r->context, crypto,
1973 KRB5_KU_AS_REQ, 0,
1974 r->request.data, r->request.length,
1975 &checksum);
1976 krb5_crypto_destroy(r->context, crypto);
1977 if (ret)
1978 return ret;
1980 ASN1_MALLOC_ENCODE(Checksum, cdata.data, cdata.length,
1981 &checksum, &len, ret);
1982 free_Checksum(&checksum);
1983 if (ret)
1984 return ret;
1985 heim_assert(cdata.length == len, "ASN.1 internal error");
1987 if (r->ek.encrypted_pa_data == NULL) {
1988 ALLOC(r->ek.encrypted_pa_data);
1989 if (r->ek.encrypted_pa_data == NULL)
1990 return ENOMEM;
1992 ret = krb5_padata_add(r->context, r->ek.encrypted_pa_data,
1993 KRB5_PADATA_REQ_ENC_PA_REP, cdata.data, cdata.length);
1994 if (ret)
1995 return ret;
1997 return krb5_padata_add(r->context, r->ek.encrypted_pa_data,
1998 KRB5_PADATA_FX_FAST, NULL, 0);
2002 * Add an authorization data element indicating that a synthetic
2003 * principal was used, so that the TGS does not accidentally
2004 * synthesize a non-synthetic principal that has since been deleted.
2006 static krb5_error_code
2007 add_synthetic_princ_ad(astgs_request_t r)
2009 krb5_data data;
2011 krb5_data_zero(&data);
2013 return _kdc_tkt_add_if_relevant_ad(r->context, &r->et,
2014 KRB5_AUTHDATA_SYNTHETIC_PRINC_USED,
2015 &data);
2018 static krb5_error_code
2019 get_local_tgs(krb5_context context,
2020 krb5_kdc_configuration *config,
2021 krb5_const_realm realm,
2022 HDB **krbtgtdb,
2023 hdb_entry **krbtgt)
2025 krb5_error_code ret;
2026 krb5_principal tgs_name;
2028 *krbtgtdb = NULL;
2029 *krbtgt = NULL;
2031 ret = krb5_make_principal(context,
2032 &tgs_name,
2033 realm,
2034 KRB5_TGS_NAME,
2035 realm,
2036 NULL);
2037 if (ret)
2038 return ret;
2040 ret = _kdc_db_fetch(context, config, tgs_name,
2041 HDB_F_GET_KRBTGT, NULL, krbtgtdb, krbtgt);
2042 krb5_free_principal(context, tgs_name);
2044 return ret;
2051 krb5_error_code
2052 _kdc_as_rep(astgs_request_t r)
2054 krb5_kdc_configuration *config = r->config;
2055 KDC_REQ *req = &r->req;
2056 const char *from = r->from;
2057 KDC_REQ_BODY *b = NULL;
2058 KDC_REP *rep = &r->rep;
2059 KDCOptions f;
2060 krb5_enctype setype;
2061 krb5_error_code ret = 0;
2062 Key *skey;
2063 int found_pa = 0;
2064 int i, flags = HDB_F_FOR_AS_REQ;
2065 const PA_DATA *pa;
2066 krb5_boolean is_tgs;
2067 const char *msg;
2068 Key *krbtgt_key;
2070 memset(rep, 0, sizeof(*rep));
2072 ALLOC(rep->padata);
2073 if (rep->padata == NULL) {
2074 ret = ENOMEM;
2075 krb5_set_error_message(r->context, ret, N_("malloc: out of memory", ""));
2076 goto out;
2080 * Look for FAST armor and unwrap
2082 ret = _kdc_fast_unwrap_request(r, NULL, NULL);
2083 if (ret) {
2084 _kdc_r_log(r, 1, "FAST unwrap request from %s failed: %d", from, ret);
2085 goto out;
2088 b = &req->req_body;
2089 f = b->kdc_options;
2091 if (f.canonicalize)
2092 flags |= HDB_F_CANON;
2094 if (b->sname == NULL) {
2095 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
2096 _kdc_set_e_text(r, "No server in request");
2097 goto out;
2100 ret = _krb5_principalname2krb5_principal(r->context, &r->server_princ,
2101 *(b->sname), b->realm);
2102 if (!ret)
2103 ret = krb5_unparse_name(r->context, r->server_princ, &r->sname);
2104 if (ret) {
2105 kdc_log(r->context, config, 2,
2106 "AS_REQ malformed server name from %s", from);
2107 goto out;
2110 if (b->cname == NULL) {
2111 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
2112 _kdc_set_e_text(r, "No client in request");
2113 goto out;
2116 ret = _krb5_principalname2krb5_principal(r->context, &r->client_princ,
2117 *(b->cname), b->realm);
2118 if (!ret)
2119 ret = krb5_unparse_name(r->context, r->client_princ, &r->cname);
2120 if (ret) {
2121 kdc_log(r->context, config, 2,
2122 "AS-REQ malformed client name from %s", from);
2123 goto out;
2126 kdc_log(r->context, config, 4, "AS-REQ %s from %s for %s",
2127 r->cname, r->from, r->sname);
2129 is_tgs = krb5_principal_is_krbtgt(r->context, r->server_princ);
2131 if (_kdc_is_anonymous(r->context, r->client_princ) &&
2132 !_kdc_is_anon_request(req)) {
2133 kdc_log(r->context, config, 2, "Anonymous client w/o anonymous flag");
2134 ret = KRB5KDC_ERR_BADOPTION;
2135 goto out;
2138 ret = _kdc_db_fetch(r->context, config, r->client_princ,
2139 HDB_F_GET_CLIENT | HDB_F_SYNTHETIC_OK | flags, NULL,
2140 &r->clientdb, &r->client);
2141 switch (ret) {
2142 case 0: /* Success */
2143 break;
2144 case HDB_ERR_NOT_FOUND_HERE:
2145 kdc_log(r->context, config, 5, "client %s does not have secrets at this KDC, need to proxy",
2146 r->cname);
2147 goto out;
2148 case HDB_ERR_WRONG_REALM: {
2149 char *fixed_client_name = NULL;
2151 ret = krb5_unparse_name(r->context, r->client->principal,
2152 &fixed_client_name);
2153 if (ret) {
2154 goto out;
2157 kdc_log(r->context, config, 4, "WRONG_REALM - %s -> %s",
2158 r->cname, fixed_client_name);
2159 free(fixed_client_name);
2161 r->e_text = NULL;
2162 ret = _kdc_fast_mk_error(r, r->rep.padata, r->armor_crypto,
2163 &req->req_body,
2164 r->ret = KRB5_KDC_ERR_WRONG_REALM,
2165 r->client->principal, r->server_princ,
2166 NULL, NULL, r->reply);
2167 goto out;
2169 default:
2171 msg = krb5_get_error_message(r->context, ret);
2172 kdc_log(r->context, config, 4, "UNKNOWN -- %s: %s", r->cname, msg);
2173 krb5_free_error_message(r->context, msg);
2174 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
2175 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
2176 KDC_AUTH_EVENT_CLIENT_UNKNOWN);
2177 goto out;
2180 ret = _kdc_db_fetch(r->context, config, r->server_princ,
2181 HDB_F_GET_SERVER | HDB_F_DELAY_NEW_KEYS |
2182 flags | (is_tgs ? HDB_F_GET_KRBTGT : 0),
2183 NULL, &r->serverdb, &r->server);
2184 switch (ret) {
2185 case 0: /* Success */
2186 break;
2187 case HDB_ERR_NOT_FOUND_HERE:
2188 kdc_log(r->context, config, 5, "target %s does not have secrets at this KDC, need to proxy",
2189 r->sname);
2190 goto out;
2191 default:
2192 msg = krb5_get_error_message(r->context, ret);
2193 kdc_log(r->context, config, 4, "UNKNOWN -- %s: %s", r->sname, msg);
2194 krb5_free_error_message(r->context, msg);
2195 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
2196 goto out;
2200 * Select an enctype for the to-be-issued ticket's session key using the
2201 * intersection of the client's requested enctypes and the server's (like a
2202 * root krbtgt, but not necessarily) etypes from its HDB entry.
2204 ret = _kdc_find_etype(r, (is_tgs ? KFE_IS_TGS:0) | KFE_USE_CLIENT,
2205 b->etype.val, b->etype.len,
2206 &r->sessionetype, NULL, NULL);
2207 if (ret) {
2208 kdc_log(r->context, config, 4,
2209 "Client (%s) from %s has no common enctypes with KDC "
2210 "to use for the session key",
2211 r->cname, from);
2212 goto out;
2216 * Pre-auth processing
2219 if(req->padata){
2220 unsigned int n;
2222 log_patypes(r, req->padata);
2224 /* Check if preauth matching */
2226 for (n = 0; !found_pa && n < sizeof(pat) / sizeof(pat[0]); n++) {
2227 if (pat[n].validate == NULL)
2228 continue;
2229 if (r->armor_crypto == NULL && (pat[n].flags & PA_REQ_FAST))
2230 continue;
2232 kdc_log(r->context, config, 5,
2233 "Looking for %s pa-data -- %s", pat[n].name, r->cname);
2234 i = 0;
2235 pa = _kdc_find_padata(req, &i, pat[n].type);
2236 if (pa) {
2237 if (r->client->flags.synthetic &&
2238 !(pat[n].flags & PA_SYNTHETIC_OK)) {
2239 kdc_log(r->context, config, 4, "UNKNOWN -- %s", r->cname);
2240 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
2241 goto out;
2243 _kdc_audit_addkv((kdc_request_t)r, KDC_AUDIT_VIS, "pa", "%s",
2244 pat[n].name);
2245 ret = pat[n].validate(r, pa);
2246 if (ret != 0) {
2247 krb5_error_code ret2;
2248 Key *ckey = NULL;
2249 krb5_boolean default_salt;
2251 if (ret != KRB5_KDC_ERR_MORE_PREAUTH_DATA_REQUIRED &&
2252 !_kdc_audit_getkv((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT))
2253 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
2254 KDC_AUTH_EVENT_PREAUTH_FAILED);
2257 * If there is a client key, send ETYPE_INFO{,2}
2259 ret2 = _kdc_find_etype(r, KFE_IS_PREAUTH|KFE_USE_CLIENT,
2260 b->etype.val, b->etype.len,
2261 NULL, &ckey, &default_salt);
2262 if (ret2 == 0) {
2263 ret2 = get_pa_etype_info_both(r->context, config, &b->etype,
2264 r->rep.padata, ckey, !default_salt);
2265 if (ret2 != 0)
2266 ret = ret2;
2268 goto out;
2270 if (!_kdc_audit_getkv((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT))
2271 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
2272 KDC_AUTH_EVENT_PREAUTH_SUCCEEDED);
2273 kdc_log(r->context, config, 4,
2274 "%s pre-authentication succeeded -- %s",
2275 pat[n].name, r->cname);
2276 found_pa = 1;
2277 r->pa_used = &pat[n];
2278 r->et.flags.pre_authent = 1;
2283 if (found_pa == 0) {
2284 Key *ckey = NULL;
2285 size_t n;
2286 krb5_boolean default_salt;
2288 if (r->client->flags.synthetic) {
2289 kdc_log(r->context, config, 4, "UNKNOWN -- %s", r->cname);
2290 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
2291 goto out;
2294 for (n = 0; n < sizeof(pat) / sizeof(pat[0]); n++) {
2295 if ((pat[n].flags & PA_ANNOUNCE) == 0)
2296 continue;
2298 if (!r->armor_crypto && (pat[n].flags & PA_REQ_FAST))
2299 continue;
2300 if (pat[n].type == KRB5_PADATA_ENC_TIMESTAMP) {
2301 if (r->armor_crypto && !r->config->enable_armored_pa_enc_timestamp)
2302 continue;
2303 if (!r->armor_crypto && !r->config->enable_unarmored_pa_enc_timestamp)
2304 continue;
2307 ret = krb5_padata_add(r->context, r->rep.padata,
2308 pat[n].type, NULL, 0);
2309 if (ret)
2310 goto out;
2314 * If there is a client key, send ETYPE_INFO{,2}
2316 ret = _kdc_find_etype(r, KFE_IS_PREAUTH|KFE_USE_CLIENT,
2317 b->etype.val, b->etype.len,
2318 NULL, &ckey, &default_salt);
2319 if (ret == 0) {
2320 ret = get_pa_etype_info_both(r->context, config, &b->etype,
2321 r->rep.padata, ckey, !default_salt);
2322 if (ret)
2323 goto out;
2327 * send requre preauth is its required or anon is requested,
2328 * anon is today only allowed via preauth mechanisms.
2330 if (require_preauth_p(r) || _kdc_is_anon_request(&r->req)) {
2331 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
2332 _kdc_set_e_text(r, "Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ");
2333 goto out;
2336 if (ckey == NULL) {
2337 ret = KRB5KDC_ERR_CLIENT_NOTYET;
2338 _kdc_set_e_text(r, "Doesn't have a client key available");
2339 goto out;
2341 krb5_free_keyblock_contents(r->context, &r->reply_key);
2342 ret = krb5_copy_keyblock_contents(r->context, &ckey->key, &r->reply_key);
2343 if (ret)
2344 goto out;
2347 r->canon_client_princ = r->client->principal;
2350 * Verify flags after the user been required to prove its identity
2351 * with in a preauth mech.
2354 ret = _kdc_check_access(r);
2355 if(ret)
2356 goto out;
2358 if (_kdc_is_anon_request(&r->req)) {
2359 ret = _kdc_check_anon_policy(r);
2360 if (ret) {
2361 _kdc_set_e_text(r, "Anonymous ticket requests are disabled");
2362 goto out;
2365 r->et.flags.anonymous = 1;
2368 _kdc_audit_setkv_number((kdc_request_t)r, KDC_REQUEST_KV_AUTH_EVENT,
2369 KDC_AUTH_EVENT_CLIENT_AUTHORIZED);
2372 * Select the best encryption type for the KDC with out regard to
2373 * the client since the client never needs to read that data.
2376 ret = _kdc_get_preferred_key(r->context, config,
2377 r->server, r->sname,
2378 &setype, &skey);
2379 if(ret)
2380 goto out;
2382 /* If server is not krbtgt, fetch local krbtgt key for signing authdata */
2383 if (is_tgs) {
2384 krbtgt_key = skey;
2385 } else {
2386 ret = get_local_tgs(r->context, config, r->server_princ->realm,
2387 &r->krbtgtdb, &r->krbtgt);
2388 if (ret)
2389 goto out;
2391 ret = _kdc_get_preferred_key(r->context, config, r->krbtgt,
2392 r->server_princ->realm,
2393 NULL, &krbtgt_key);
2394 if (ret)
2395 goto out;
2398 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey) {
2399 ret = KRB5KDC_ERR_BADOPTION;
2400 _kdc_set_e_text(r, "Bad KDC options");
2401 goto out;
2405 * Build reply
2407 rep->pvno = 5;
2408 rep->msg_type = krb_as_rep;
2410 if (!config->historical_anon_realm &&
2411 _kdc_is_anonymous(r->context, r->client_princ)) {
2412 Realm anon_realm = KRB5_ANON_REALM;
2413 ret = copy_Realm(&anon_realm, &rep->crealm);
2414 } else if (f.canonicalize || r->client->flags.force_canonicalize)
2415 ret = copy_Realm(&r->canon_client_princ->realm, &rep->crealm);
2416 else
2417 ret = copy_Realm(&r->client_princ->realm, &rep->crealm);
2418 if (ret)
2419 goto out;
2420 if (r->et.flags.anonymous)
2421 ret = _kdc_make_anonymous_principalname(&rep->cname);
2422 else if (f.canonicalize || r->client->flags.force_canonicalize)
2423 ret = _krb5_principal2principalname(&rep->cname, r->canon_client_princ);
2424 else
2425 ret = _krb5_principal2principalname(&rep->cname, r->client_princ);
2426 if (ret)
2427 goto out;
2429 rep->ticket.tkt_vno = 5;
2430 if (f.canonicalize || r->server->flags.force_canonicalize)
2431 ret = copy_Realm(&r->server->principal->realm, &rep->ticket.realm);
2432 else
2433 ret = copy_Realm(&r->server_princ->realm, &rep->ticket.realm);
2434 if (ret)
2435 goto out;
2436 if (f.canonicalize || r->server->flags.force_canonicalize)
2437 _krb5_principal2principalname(&rep->ticket.sname,
2438 r->server->principal);
2439 else
2440 _krb5_principal2principalname(&rep->ticket.sname,
2441 r->server_princ);
2442 /* java 1.6 expects the name to be the same type, lets allow that
2443 * uncomplicated name-types. */
2444 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
2445 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
2446 rep->ticket.sname.name_type = b->sname->name_type;
2447 #undef CNT
2449 r->et.flags.initial = 1;
2450 if(r->client->flags.forwardable && r->server->flags.forwardable)
2451 r->et.flags.forwardable = f.forwardable;
2452 if(r->client->flags.proxiable && r->server->flags.proxiable)
2453 r->et.flags.proxiable = f.proxiable;
2454 else if (f.proxiable) {
2455 _kdc_set_e_text(r, "Ticket may not be proxiable");
2456 ret = KRB5KDC_ERR_POLICY;
2457 goto out;
2459 if(r->client->flags.postdate && r->server->flags.postdate)
2460 r->et.flags.may_postdate = f.allow_postdate;
2461 else if (f.allow_postdate){
2462 _kdc_set_e_text(r, "Ticket may not be postdate");
2463 ret = KRB5KDC_ERR_POLICY;
2464 goto out;
2467 if (b->addresses)
2468 _kdc_audit_addaddrs((kdc_request_t)r, b->addresses, "reqaddrs");
2470 /* check for valid set of addresses */
2471 if (!_kdc_check_addresses(r, b->addresses, r->addr)) {
2472 if (r->config->warn_ticket_addresses) {
2473 _kdc_audit_setkv_bool((kdc_request_t)r, "wrongaddr", TRUE);
2474 } else {
2475 _kdc_set_e_text(r, "Request from wrong address");
2476 ret = KRB5KRB_AP_ERR_BADADDR;
2477 goto out;
2481 ret = copy_PrincipalName(&rep->cname, &r->et.cname);
2482 if (ret)
2483 goto out;
2484 ret = copy_Realm(&rep->crealm, &r->et.crealm);
2485 if (ret)
2486 goto out;
2489 time_t start;
2490 time_t t;
2492 start = r->et.authtime = kdc_time;
2494 if(f.postdated && req->req_body.from){
2495 ALLOC(r->et.starttime);
2496 start = *r->et.starttime = *req->req_body.from;
2497 r->et.flags.invalid = 1;
2498 r->et.flags.postdated = 1; /* XXX ??? */
2500 _kdc_fix_time(&b->till);
2501 t = *b->till;
2503 /* be careful not overflowing */
2506 * Pre-auth can override r->client->max_life if configured.
2508 * See pre-auth methods, specifically PKINIT, which can get or derive
2509 * this from the client's certificate.
2511 if (r->pa_max_life > 0)
2512 t = start + min(t - start, r->pa_max_life);
2513 else if (r->client->max_life)
2514 t = start + min(t - start, *r->client->max_life);
2516 if (r->server->max_life)
2517 t = start + min(t - start, *r->server->max_life);
2519 /* Pre-auth can bound endtime as well */
2520 if (r->pa_endtime > 0)
2521 t = start + min(t - start, r->pa_endtime);
2522 #if 0
2523 t = min(t, start + realm->max_life);
2524 #endif
2525 r->et.endtime = t;
2526 if(f.renewable_ok && r->et.endtime < *b->till){
2527 f.renewable = 1;
2528 if(b->rtime == NULL){
2529 ALLOC(b->rtime);
2530 *b->rtime = 0;
2532 if(*b->rtime < *b->till)
2533 *b->rtime = *b->till;
2535 if(f.renewable && b->rtime){
2536 t = *b->rtime;
2537 if(t == 0)
2538 t = MAX_TIME;
2539 if(r->client->max_renew)
2540 t = start + min(t - start, *r->client->max_renew);
2541 if(r->server->max_renew)
2542 t = start + min(t - start, *r->server->max_renew);
2543 #if 0
2544 t = min(t, start + realm->max_renew);
2545 #endif
2546 ALLOC(r->et.renew_till);
2547 *r->et.renew_till = t;
2548 r->et.flags.renewable = 1;
2552 if(b->addresses){
2553 ALLOC(r->et.caddr);
2554 copy_HostAddresses(b->addresses, r->et.caddr);
2557 r->et.transited.tr_type = domain_X500_Compress;
2558 krb5_data_zero(&r->et.transited.contents);
2560 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
2561 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
2562 * incapable of correctly decoding SEQUENCE OF's of zero length.
2564 * To fix this, always send at least one no-op last_req
2566 * If there's a pw_end or valid_end we will use that,
2567 * otherwise just a dummy lr.
2569 r->ek.last_req.val = malloc(2 * sizeof(*r->ek.last_req.val));
2570 if (r->ek.last_req.val == NULL) {
2571 ret = ENOMEM;
2572 goto out;
2574 r->ek.last_req.len = 0;
2575 if (r->client->pw_end
2576 && (config->kdc_warn_pwexpire == 0
2577 || kdc_time + config->kdc_warn_pwexpire >= *r->client->pw_end)) {
2578 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_PW_EXPTIME;
2579 r->ek.last_req.val[r->ek.last_req.len].lr_value = *r->client->pw_end;
2580 ++r->ek.last_req.len;
2582 if (r->client->valid_end) {
2583 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
2584 r->ek.last_req.val[r->ek.last_req.len].lr_value = *r->client->valid_end;
2585 ++r->ek.last_req.len;
2587 if (r->ek.last_req.len == 0) {
2588 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_NONE;
2589 r->ek.last_req.val[r->ek.last_req.len].lr_value = 0;
2590 ++r->ek.last_req.len;
2592 r->ek.nonce = b->nonce;
2593 if (r->client->valid_end || r->client->pw_end) {
2594 ALLOC(r->ek.key_expiration);
2595 if (r->client->valid_end) {
2596 if (r->client->pw_end)
2597 *r->ek.key_expiration = min(*r->client->valid_end,
2598 *r->client->pw_end);
2599 else
2600 *r->ek.key_expiration = *r->client->valid_end;
2601 } else
2602 *r->ek.key_expiration = *r->client->pw_end;
2603 } else
2604 r->ek.key_expiration = NULL;
2605 r->ek.flags = r->et.flags;
2606 r->ek.authtime = r->et.authtime;
2607 if (r->et.starttime) {
2608 ALLOC(r->ek.starttime);
2609 *r->ek.starttime = *r->et.starttime;
2611 r->ek.endtime = r->et.endtime;
2612 if (r->et.renew_till) {
2613 ALLOC(r->ek.renew_till);
2614 *r->ek.renew_till = *r->et.renew_till;
2616 ret = copy_Realm(&rep->ticket.realm, &r->ek.srealm);
2617 if (ret)
2618 goto out;
2619 ret = copy_PrincipalName(&rep->ticket.sname, &r->ek.sname);
2620 if (ret)
2621 goto out;
2622 if(r->et.caddr){
2623 ALLOC(r->ek.caddr);
2624 copy_HostAddresses(r->et.caddr, r->ek.caddr);
2628 * Check and session and reply keys
2631 if (r->session_key.keytype == ETYPE_NULL) {
2632 ret = krb5_generate_random_keyblock(r->context, r->sessionetype, &r->session_key);
2633 if (ret)
2634 goto out;
2637 if (r->reply_key.keytype == ETYPE_NULL) {
2638 _kdc_set_e_text(r, "Client have no reply key");
2639 ret = KRB5KDC_ERR_CLIENT_NOTYET;
2640 goto out;
2643 ret = copy_EncryptionKey(&r->session_key, &r->et.key);
2644 if (ret)
2645 goto out;
2647 ret = copy_EncryptionKey(&r->session_key, &r->ek.key);
2648 if (ret)
2649 goto out;
2651 /* Add the PAC */
2652 if (!r->et.flags.anonymous) {
2653 generate_pac(r, skey, krbtgt_key, is_tgs);
2656 if (r->client->flags.synthetic) {
2657 ret = add_synthetic_princ_ad(r);
2658 if (ret)
2659 goto out;
2662 _kdc_log_timestamp(r, "AS-REQ", r->et.authtime,
2663 r->et.starttime, r->et.endtime,
2664 r->et.renew_till);
2666 _log_astgs_req(r, setype);
2669 * We always say we support FAST/enc-pa-rep
2672 r->et.flags.enc_pa_rep = r->ek.flags.enc_pa_rep = 1;
2675 * update reply-key with strengthen-key
2678 ret = _kdc_fast_strengthen_reply_key(r);
2679 if (ret)
2680 goto out;
2683 * Add REQ_ENC_PA_REP if client supports it
2686 i = 0;
2687 pa = _kdc_find_padata(req, &i, KRB5_PADATA_REQ_ENC_PA_REP);
2688 if (pa) {
2690 ret = add_enc_pa_rep(r);
2691 if (ret) {
2692 msg = krb5_get_error_message(r->context, ret);
2693 _kdc_r_log(r, 4, "add_enc_pa_rep failed: %s: %d", msg, ret);
2694 krb5_free_error_message(r->context, msg);
2695 goto out;
2700 * Last chance for plugins to update reply
2702 ret = _kdc_finalize_reply(r);
2703 if (ret)
2704 goto out;
2707 * Don't send kvno from client entry if the pre-authentication
2708 * mechanism replaced the reply key.
2711 ret = _kdc_encode_reply(r->context, config,
2712 r, req->req_body.nonce, setype,
2713 r->server->kvno, &skey->key,
2714 pa_used_flag_isset(r, PA_REPLACE_REPLY_KEY) ? 0 : r->client->kvno,
2715 0, r->reply);
2716 if (ret)
2717 goto out;
2720 * Check if message too large
2722 if (r->datagram_reply && r->reply->length > config->max_datagram_reply_length) {
2723 krb5_data_free(r->reply);
2724 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
2725 _kdc_set_e_text(r, "Reply packet too large");
2728 out:
2729 r->ret = ret;
2730 _kdc_audit_request(r);
2733 * In case of a non proxy error, build an error message.
2735 if (ret != 0 && ret != HDB_ERR_NOT_FOUND_HERE && r->reply->length == 0)
2736 ret = _kdc_fast_mk_error(r,
2737 r->rep.padata,
2738 r->armor_crypto,
2739 &req->req_body,
2740 r->ret,
2741 r->client_princ,
2742 r->server_princ,
2743 NULL, NULL,
2744 r->reply);
2746 if (r->pa_used && r->pa_used->cleanup)
2747 r->pa_used->cleanup(r);
2749 free_AS_REP(&r->rep);
2750 free_EncTicketPart(&r->et);
2751 free_EncKDCRepPart(&r->ek);
2752 _kdc_free_fast_state(&r->fast);
2754 if (r->client_princ) {
2755 krb5_free_principal(r->context, r->client_princ);
2756 r->client_princ = NULL;
2758 if (r->server_princ){
2759 krb5_free_principal(r->context, r->server_princ);
2760 r->server_princ = NULL;
2762 if (r->client)
2763 _kdc_free_ent(r->context, r->clientdb, r->client);
2764 if (r->server)
2765 _kdc_free_ent(r->context, r->serverdb, r->server);
2766 if (r->krbtgt)
2767 _kdc_free_ent(r->context, r->krbtgtdb, r->krbtgt);
2768 if (r->armor_crypto) {
2769 krb5_crypto_destroy(r->context, r->armor_crypto);
2770 r->armor_crypto = NULL;
2772 if (r->armor_ticket)
2773 krb5_free_ticket(r->context, r->armor_ticket);
2774 if (r->armor_server)
2775 _kdc_free_ent(r->context, r->armor_serverdb, r->armor_server);
2776 krb5_free_keyblock_contents(r->context, &r->reply_key);
2777 krb5_free_keyblock_contents(r->context, &r->session_key);
2778 krb5_free_keyblock_contents(r->context, &r->strengthen_key);
2779 krb5_pac_free(r->context, r->pac);
2781 return ret;