kdc: correctly generate PAC TGS signature
[Samba.git] / source4 / heimdal / kdc / kerberos5.c
bloba131f1af08e78f5edef9de87b61f3bb9128be6fd
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 void
39 _kdc_fix_time(time_t **t)
41 if(*t == NULL){
42 ALLOC(*t);
43 **t = MAX_TIME;
45 if(**t == 0) **t = MAX_TIME; /* fix for old clients */
48 static int
49 realloc_method_data(METHOD_DATA *md)
51 PA_DATA *pa;
52 pa = realloc(md->val, (md->len + 1) * sizeof(*md->val));
53 if(pa == NULL)
54 return ENOMEM;
55 md->val = pa;
56 md->len++;
57 return 0;
60 static void
61 set_salt_padata(METHOD_DATA *md, Salt *salt)
63 if (salt) {
64 realloc_method_data(md);
65 md->val[md->len - 1].padata_type = salt->type;
66 der_copy_octet_string(&salt->salt,
67 &md->val[md->len - 1].padata_value);
71 const PA_DATA*
72 _kdc_find_padata(const KDC_REQ *req, int *start, int type)
74 if (req->padata == NULL)
75 return NULL;
77 while((size_t)*start < req->padata->len){
78 (*start)++;
79 if(req->padata->val[*start - 1].padata_type == (unsigned)type)
80 return &req->padata->val[*start - 1];
82 return NULL;
86 * This is a hack to allow predefined weak services, like afs to
87 * still use weak types
90 krb5_boolean
91 _kdc_is_weak_exception(krb5_principal principal, krb5_enctype etype)
93 if (principal->name.name_string.len > 0 &&
94 strcmp(principal->name.name_string.val[0], "afs") == 0 &&
95 (etype == ETYPE_DES_CBC_CRC
96 || etype == ETYPE_DES_CBC_MD4
97 || etype == ETYPE_DES_CBC_MD5))
98 return TRUE;
99 return FALSE;
104 * Detect if `key' is the using the the precomputed `default_salt'.
107 static krb5_boolean
108 is_default_salt_p(const krb5_salt *default_salt, const Key *key)
110 if (key->salt == NULL)
111 return TRUE;
112 if (default_salt->salttype != key->salt->type)
113 return FALSE;
114 if (krb5_data_cmp(&default_salt->saltvalue, &key->salt->salt))
115 return FALSE;
116 return TRUE;
120 * return the first appropriate key of `princ' in `ret_key'. Look for
121 * all the etypes in (`etypes', `len'), stopping as soon as we find
122 * one, but preferring one that has default salt
125 krb5_error_code
126 _kdc_find_etype(krb5_context context, krb5_boolean use_strongest_session_key,
127 krb5_boolean is_preauth, hdb_entry_ex *princ,
128 krb5_enctype *etypes, unsigned len,
129 krb5_enctype *ret_enctype, Key **ret_key)
131 krb5_error_code ret;
132 krb5_salt def_salt;
133 krb5_enctype enctype = ETYPE_NULL;
134 Key *key;
135 int i;
137 /* We'll want to avoid keys with v4 salted keys in the pre-auth case... */
138 ret = krb5_get_pw_salt(context, princ->entry.principal, &def_salt);
139 if (ret)
140 return ret;
142 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
144 if (use_strongest_session_key) {
145 const krb5_enctype *p;
146 krb5_enctype clientbest = ETYPE_NULL;
147 int j;
150 * Pick the strongest key that the KDC, target service, and
151 * client all support, using the local cryptosystem enctype
152 * list in strongest-to-weakest order to drive the search.
154 * This is not what RFC4120 says to do, but it encourages
155 * adoption of stronger enctypes. This doesn't play well with
156 * clients that have multiple Kerberos client implementations
157 * available with different supported enctype lists.
160 /* drive the search with local supported enctypes list */
161 p = krb5_kerberos_enctypes(context);
162 for (i = 0; p[i] != ETYPE_NULL && enctype == ETYPE_NULL; i++) {
163 if (krb5_enctype_valid(context, p[i]) != 0)
164 continue;
166 /* check that the client supports it too */
167 for (j = 0; j < len && enctype == ETYPE_NULL; j++) {
168 if (p[i] != etypes[j])
169 continue;
170 /* save best of union of { client, crypto system } */
171 if (clientbest == ETYPE_NULL)
172 clientbest = p[i];
173 /* check target princ support */
174 ret = hdb_enctype2key(context, &princ->entry, p[i], &key);
175 if (ret)
176 continue;
177 if (is_preauth && !is_default_salt_p(&def_salt, key))
178 continue;
179 enctype = p[i];
182 if (clientbest != ETYPE_NULL && enctype == ETYPE_NULL)
183 enctype = clientbest;
184 else if (enctype == ETYPE_NULL)
185 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
186 if (ret == 0 && ret_enctype != NULL)
187 *ret_enctype = enctype;
188 if (ret == 0 && ret_key != NULL)
189 *ret_key = key;
190 } else {
192 * Pick the first key from the client's enctype list that is
193 * supported by the cryptosystem and by the given principal.
195 * RFC4120 says we SHOULD pick the first _strong_ key from the
196 * client's list... not the first key... If the admin disallows
197 * weak enctypes in krb5.conf and selects this key selection
198 * algorithm, then we get exactly what RFC4120 says.
200 for(key = NULL, i = 0; ret != 0 && i < len; i++, key = NULL) {
202 if (krb5_enctype_valid(context, etypes[i]) != 0 &&
203 !_kdc_is_weak_exception(princ->entry.principal, etypes[i]))
204 continue;
206 while (hdb_next_enctype2key(context, &princ->entry, etypes[i], &key) == 0) {
207 if (key->key.keyvalue.length == 0) {
208 ret = KRB5KDC_ERR_NULL_KEY;
209 continue;
211 if (ret_key != NULL)
212 *ret_key = key;
213 if (ret_enctype != NULL)
214 *ret_enctype = etypes[i];
215 ret = 0;
216 if (is_preauth && is_default_salt_p(&def_salt, key))
217 goto out;
222 out:
223 krb5_free_salt (context, def_salt);
224 return ret;
227 krb5_error_code
228 _kdc_make_anonymous_principalname (PrincipalName *pn)
230 pn->name_type = KRB5_NT_PRINCIPAL;
231 pn->name_string.len = 1;
232 pn->name_string.val = malloc(sizeof(*pn->name_string.val));
233 if (pn->name_string.val == NULL)
234 return ENOMEM;
235 pn->name_string.val[0] = strdup("anonymous");
236 if (pn->name_string.val[0] == NULL) {
237 free(pn->name_string.val);
238 pn->name_string.val = NULL;
239 return ENOMEM;
241 return 0;
244 void
245 _kdc_log_timestamp(krb5_context context,
246 krb5_kdc_configuration *config,
247 const char *type,
248 KerberosTime authtime, KerberosTime *starttime,
249 KerberosTime endtime, KerberosTime *renew_till)
251 char authtime_str[100], starttime_str[100],
252 endtime_str[100], renewtime_str[100];
254 krb5_format_time(context, authtime,
255 authtime_str, sizeof(authtime_str), TRUE);
256 if (starttime)
257 krb5_format_time(context, *starttime,
258 starttime_str, sizeof(starttime_str), TRUE);
259 else
260 strlcpy(starttime_str, "unset", sizeof(starttime_str));
261 krb5_format_time(context, endtime,
262 endtime_str, sizeof(endtime_str), TRUE);
263 if (renew_till)
264 krb5_format_time(context, *renew_till,
265 renewtime_str, sizeof(renewtime_str), TRUE);
266 else
267 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
269 kdc_log(context, config, 5,
270 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
271 type, authtime_str, starttime_str, endtime_str, renewtime_str);
274 static void
275 log_patypes(krb5_context context,
276 krb5_kdc_configuration *config,
277 METHOD_DATA *padata)
279 struct rk_strpool *p = NULL;
280 char *str;
281 size_t i;
283 for (i = 0; i < padata->len; i++) {
284 switch(padata->val[i].padata_type) {
285 case KRB5_PADATA_PK_AS_REQ:
286 p = rk_strpoolprintf(p, "PK-INIT(ietf)");
287 break;
288 case KRB5_PADATA_PK_AS_REQ_WIN:
289 p = rk_strpoolprintf(p, "PK-INIT(win2k)");
290 break;
291 case KRB5_PADATA_PA_PK_OCSP_RESPONSE:
292 p = rk_strpoolprintf(p, "OCSP");
293 break;
294 case KRB5_PADATA_ENC_TIMESTAMP:
295 p = rk_strpoolprintf(p, "encrypted-timestamp");
296 break;
297 default:
298 p = rk_strpoolprintf(p, "%d", padata->val[i].padata_type);
299 break;
301 if (p && i + 1 < padata->len)
302 p = rk_strpoolprintf(p, ", ");
303 if (p == NULL) {
304 kdc_log(context, config, 0, "out of memory");
305 return;
308 if (p == NULL)
309 p = rk_strpoolprintf(p, "none");
311 str = rk_strpoolcollect(p);
312 kdc_log(context, config, 0, "Client sent patypes: %s", str);
313 free(str);
321 krb5_error_code
322 _kdc_encode_reply(krb5_context context,
323 krb5_kdc_configuration *config,
324 KDC_REP *rep, const EncTicketPart *et, EncKDCRepPart *ek,
325 krb5_enctype etype,
326 int skvno, const EncryptionKey *skey,
327 int ckvno, const EncryptionKey *reply_key,
328 int rk_is_subkey,
329 const char **e_text,
330 krb5_data *reply)
332 unsigned char *buf;
333 size_t buf_size;
334 size_t len = 0;
335 krb5_error_code ret;
336 krb5_crypto crypto;
338 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
339 if(ret) {
340 const char *msg = krb5_get_error_message(context, ret);
341 kdc_log(context, config, 0, "Failed to encode ticket: %s", msg);
342 krb5_free_error_message(context, msg);
343 return ret;
345 if(buf_size != len) {
346 free(buf);
347 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
348 *e_text = "KDC internal error";
349 return KRB5KRB_ERR_GENERIC;
352 ret = krb5_crypto_init(context, skey, etype, &crypto);
353 if (ret) {
354 const char *msg;
355 free(buf);
356 msg = krb5_get_error_message(context, ret);
357 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
358 krb5_free_error_message(context, msg);
359 return ret;
362 ret = krb5_encrypt_EncryptedData(context,
363 crypto,
364 KRB5_KU_TICKET,
365 buf,
366 len,
367 skvno,
368 &rep->ticket.enc_part);
369 free(buf);
370 krb5_crypto_destroy(context, crypto);
371 if(ret) {
372 const char *msg = krb5_get_error_message(context, ret);
373 kdc_log(context, config, 0, "Failed to encrypt data: %s", msg);
374 krb5_free_error_message(context, msg);
375 return ret;
378 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
379 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
380 else
381 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
382 if(ret) {
383 const char *msg = krb5_get_error_message(context, ret);
384 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
385 krb5_free_error_message(context, msg);
386 return ret;
388 if(buf_size != len) {
389 free(buf);
390 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
391 *e_text = "KDC internal error";
392 return KRB5KRB_ERR_GENERIC;
394 ret = krb5_crypto_init(context, reply_key, 0, &crypto);
395 if (ret) {
396 const char *msg = krb5_get_error_message(context, ret);
397 free(buf);
398 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
399 krb5_free_error_message(context, msg);
400 return ret;
402 if(rep->msg_type == krb_as_rep) {
403 krb5_encrypt_EncryptedData(context,
404 crypto,
405 KRB5_KU_AS_REP_ENC_PART,
406 buf,
407 len,
408 ckvno,
409 &rep->enc_part);
410 free(buf);
411 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
412 } else {
413 krb5_encrypt_EncryptedData(context,
414 crypto,
415 rk_is_subkey ? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY : KRB5_KU_TGS_REP_ENC_PART_SESSION,
416 buf,
417 len,
418 ckvno,
419 &rep->enc_part);
420 free(buf);
421 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
423 krb5_crypto_destroy(context, crypto);
424 if(ret) {
425 const char *msg = krb5_get_error_message(context, ret);
426 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
427 krb5_free_error_message(context, msg);
428 return ret;
430 if(buf_size != len) {
431 free(buf);
432 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
433 *e_text = "KDC internal error";
434 return KRB5KRB_ERR_GENERIC;
436 reply->data = buf;
437 reply->length = buf_size;
438 return 0;
442 * Return 1 if the client have only older enctypes, this is for
443 * determining if the server should send ETYPE_INFO2 or not.
446 static int
447 older_enctype(krb5_enctype enctype)
449 switch (enctype) {
450 case ETYPE_DES_CBC_CRC:
451 case ETYPE_DES_CBC_MD4:
452 case ETYPE_DES_CBC_MD5:
453 case ETYPE_DES3_CBC_SHA1:
454 case ETYPE_ARCFOUR_HMAC_MD5:
455 case ETYPE_ARCFOUR_HMAC_MD5_56:
457 * The following three is "old" windows enctypes and is needed for
458 * windows 2000 hosts.
460 case ETYPE_ARCFOUR_MD4:
461 case ETYPE_ARCFOUR_HMAC_OLD:
462 case ETYPE_ARCFOUR_HMAC_OLD_EXP:
463 return 1;
464 default:
465 return 0;
473 static krb5_error_code
474 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
476 ent->etype = key->key.keytype;
477 if(key->salt){
478 #if 0
479 ALLOC(ent->salttype);
481 if(key->salt->type == hdb_pw_salt)
482 *ent->salttype = 0; /* or 1? or NULL? */
483 else if(key->salt->type == hdb_afs3_salt)
484 *ent->salttype = 2;
485 else {
486 kdc_log(context, config, 0, "unknown salt-type: %d",
487 key->salt->type);
488 return KRB5KRB_ERR_GENERIC;
490 /* according to `the specs', we can't send a salt if
491 we have AFS3 salted key, but that requires that you
492 *know* what cell you are using (e.g by assuming
493 that the cell is the same as the realm in lower
494 case) */
495 #elif 0
496 ALLOC(ent->salttype);
497 *ent->salttype = key->salt->type;
498 #else
500 * We shouldn't sent salttype since it is incompatible with the
501 * specification and it breaks windows clients. The afs
502 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
503 * implemented in Heimdal 0.7 and later.
505 ent->salttype = NULL;
506 #endif
507 krb5_copy_data(context, &key->salt->salt,
508 &ent->salt);
509 } else {
510 /* we return no salt type at all, as that should indicate
511 * the default salt type and make everybody happy. some
512 * systems (like w2k) dislike being told the salt type
513 * here. */
515 ent->salttype = NULL;
516 ent->salt = NULL;
518 return 0;
521 static krb5_error_code
522 get_pa_etype_info(krb5_context context,
523 krb5_kdc_configuration *config,
524 METHOD_DATA *md, Key *ckey)
526 krb5_error_code ret = 0;
527 ETYPE_INFO pa;
528 unsigned char *buf;
529 size_t len;
532 pa.len = 1;
533 pa.val = calloc(1, sizeof(pa.val[0]));
534 if(pa.val == NULL)
535 return ENOMEM;
537 ret = make_etype_info_entry(context, &pa.val[0], ckey);
538 if (ret) {
539 free_ETYPE_INFO(&pa);
540 return ret;
543 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
544 free_ETYPE_INFO(&pa);
545 if(ret)
546 return ret;
547 ret = realloc_method_data(md);
548 if(ret) {
549 free(buf);
550 return ret;
552 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
553 md->val[md->len - 1].padata_value.length = len;
554 md->val[md->len - 1].padata_value.data = buf;
555 return 0;
562 extern int _krb5_AES_string_to_default_iterator;
564 static krb5_error_code
565 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
567 ent->etype = key->key.keytype;
568 if(key->salt) {
569 ALLOC(ent->salt);
570 if (ent->salt == NULL)
571 return ENOMEM;
572 *ent->salt = malloc(key->salt->salt.length + 1);
573 if (*ent->salt == NULL) {
574 free(ent->salt);
575 ent->salt = NULL;
576 return ENOMEM;
578 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
579 (*ent->salt)[key->salt->salt.length] = '\0';
580 } else
581 ent->salt = NULL;
583 ent->s2kparams = NULL;
585 switch (key->key.keytype) {
586 case ETYPE_AES128_CTS_HMAC_SHA1_96:
587 case ETYPE_AES256_CTS_HMAC_SHA1_96:
588 ALLOC(ent->s2kparams);
589 if (ent->s2kparams == NULL)
590 return ENOMEM;
591 ent->s2kparams->length = 4;
592 ent->s2kparams->data = malloc(ent->s2kparams->length);
593 if (ent->s2kparams->data == NULL) {
594 free(ent->s2kparams);
595 ent->s2kparams = NULL;
596 return ENOMEM;
598 _krb5_put_int(ent->s2kparams->data,
599 _krb5_AES_string_to_default_iterator,
600 ent->s2kparams->length);
601 break;
602 case ETYPE_DES_CBC_CRC:
603 case ETYPE_DES_CBC_MD4:
604 case ETYPE_DES_CBC_MD5:
605 /* Check if this was a AFS3 salted key */
606 if(key->salt && key->salt->type == hdb_afs3_salt){
607 ALLOC(ent->s2kparams);
608 if (ent->s2kparams == NULL)
609 return ENOMEM;
610 ent->s2kparams->length = 1;
611 ent->s2kparams->data = malloc(ent->s2kparams->length);
612 if (ent->s2kparams->data == NULL) {
613 free(ent->s2kparams);
614 ent->s2kparams = NULL;
615 return ENOMEM;
617 _krb5_put_int(ent->s2kparams->data,
619 ent->s2kparams->length);
621 break;
622 default:
623 break;
625 return 0;
629 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
630 * database (client supported enctypes first, then the unsupported
631 * enctypes).
634 static krb5_error_code
635 get_pa_etype_info2(krb5_context context,
636 krb5_kdc_configuration *config,
637 METHOD_DATA *md, Key *ckey)
639 krb5_error_code ret = 0;
640 ETYPE_INFO2 pa;
641 unsigned char *buf;
642 size_t len;
644 pa.len = 1;
645 pa.val = calloc(1, sizeof(pa.val[0]));
646 if(pa.val == NULL)
647 return ENOMEM;
649 ret = make_etype_info2_entry(&pa.val[0], ckey);
650 if (ret) {
651 free_ETYPE_INFO2(&pa);
652 return ret;
655 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
656 free_ETYPE_INFO2(&pa);
657 if(ret)
658 return ret;
659 ret = realloc_method_data(md);
660 if(ret) {
661 free(buf);
662 return ret;
664 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
665 md->val[md->len - 1].padata_value.length = len;
666 md->val[md->len - 1].padata_value.data = buf;
667 return 0;
674 static void
675 log_as_req(krb5_context context,
676 krb5_kdc_configuration *config,
677 krb5_enctype cetype,
678 krb5_enctype setype,
679 const KDC_REQ_BODY *b)
681 krb5_error_code ret;
682 struct rk_strpool *p;
683 char *str;
684 size_t i;
686 p = rk_strpoolprintf(NULL, "%s", "Client supported enctypes: ");
688 for (i = 0; i < b->etype.len; i++) {
689 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
690 if (ret == 0) {
691 p = rk_strpoolprintf(p, "%s", str);
692 free(str);
693 } else
694 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
695 if (p && i + 1 < b->etype.len)
696 p = rk_strpoolprintf(p, ", ");
697 if (p == NULL) {
698 kdc_log(context, config, 0, "out of memory");
699 return;
702 if (p == NULL)
703 p = rk_strpoolprintf(p, "no encryption types");
706 char *cet;
707 char *set;
709 ret = krb5_enctype_to_string(context, cetype, &cet);
710 if(ret == 0) {
711 ret = krb5_enctype_to_string(context, setype, &set);
712 if (ret == 0) {
713 p = rk_strpoolprintf(p, ", using %s/%s", cet, set);
714 free(set);
716 free(cet);
718 if (ret != 0)
719 p = rk_strpoolprintf(p, ", using enctypes %d/%d",
720 cetype, setype);
723 str = rk_strpoolcollect(p);
724 kdc_log(context, config, 0, "%s", str);
725 free(str);
728 char fixedstr[128];
729 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
730 fixedstr, sizeof(fixedstr));
731 if(*fixedstr)
732 kdc_log(context, config, 0, "Requested flags: %s", fixedstr);
737 * verify the flags on `client' and `server', returning 0
738 * if they are OK and generating an error messages and returning
739 * and error code otherwise.
742 krb5_error_code
743 kdc_check_flags(krb5_context context,
744 krb5_kdc_configuration *config,
745 hdb_entry_ex *client_ex, const char *client_name,
746 hdb_entry_ex *server_ex, const char *server_name,
747 krb5_boolean is_as_req)
749 if(client_ex != NULL) {
750 hdb_entry *client = &client_ex->entry;
752 /* check client */
753 if (client->flags.locked_out) {
754 kdc_log(context, config, 0,
755 "Client (%s) is locked out", client_name);
756 return KRB5KDC_ERR_CLIENT_REVOKED;
759 if (client->flags.invalid) {
760 kdc_log(context, config, 0,
761 "Client (%s) has invalid bit set", client_name);
762 return KRB5KDC_ERR_POLICY;
765 if(!client->flags.client){
766 kdc_log(context, config, 0,
767 "Principal may not act as client -- %s", client_name);
768 return KRB5KDC_ERR_POLICY;
771 if (client->valid_start && *client->valid_start > kdc_time) {
772 char starttime_str[100];
773 krb5_format_time(context, *client->valid_start,
774 starttime_str, sizeof(starttime_str), TRUE);
775 kdc_log(context, config, 0,
776 "Client not yet valid until %s -- %s",
777 starttime_str, client_name);
778 return KRB5KDC_ERR_CLIENT_NOTYET;
781 if (client->valid_end && *client->valid_end < kdc_time) {
782 char endtime_str[100];
783 krb5_format_time(context, *client->valid_end,
784 endtime_str, sizeof(endtime_str), TRUE);
785 kdc_log(context, config, 0,
786 "Client expired at %s -- %s",
787 endtime_str, client_name);
788 return KRB5KDC_ERR_NAME_EXP;
791 if (client->pw_end && *client->pw_end < kdc_time
792 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
793 char pwend_str[100];
794 krb5_format_time(context, *client->pw_end,
795 pwend_str, sizeof(pwend_str), TRUE);
796 kdc_log(context, config, 0,
797 "Client's key has expired at %s -- %s",
798 pwend_str, client_name);
799 return KRB5KDC_ERR_KEY_EXPIRED;
803 /* check server */
805 if (server_ex != NULL) {
806 hdb_entry *server = &server_ex->entry;
808 if (server->flags.locked_out) {
809 kdc_log(context, config, 0,
810 "Client server locked out -- %s", server_name);
811 return KRB5KDC_ERR_POLICY;
813 if (server->flags.invalid) {
814 kdc_log(context, config, 0,
815 "Server has invalid flag set -- %s", server_name);
816 return KRB5KDC_ERR_POLICY;
819 if(!server->flags.server){
820 kdc_log(context, config, 0,
821 "Principal may not act as server -- %s", server_name);
822 return KRB5KDC_ERR_POLICY;
825 if(!is_as_req && server->flags.initial) {
826 kdc_log(context, config, 0,
827 "AS-REQ is required for server -- %s", server_name);
828 return KRB5KDC_ERR_POLICY;
831 if (server->valid_start && *server->valid_start > kdc_time) {
832 char starttime_str[100];
833 krb5_format_time(context, *server->valid_start,
834 starttime_str, sizeof(starttime_str), TRUE);
835 kdc_log(context, config, 0,
836 "Server not yet valid until %s -- %s",
837 starttime_str, server_name);
838 return KRB5KDC_ERR_SERVICE_NOTYET;
841 if (server->valid_end && *server->valid_end < kdc_time) {
842 char endtime_str[100];
843 krb5_format_time(context, *server->valid_end,
844 endtime_str, sizeof(endtime_str), TRUE);
845 kdc_log(context, config, 0,
846 "Server expired at %s -- %s",
847 endtime_str, server_name);
848 return KRB5KDC_ERR_SERVICE_EXP;
851 if (server->pw_end && *server->pw_end < kdc_time) {
852 char pwend_str[100];
853 krb5_format_time(context, *server->pw_end,
854 pwend_str, sizeof(pwend_str), TRUE);
855 kdc_log(context, config, 0,
856 "Server's key has expired at -- %s",
857 pwend_str, server_name);
858 return KRB5KDC_ERR_KEY_EXPIRED;
861 return 0;
865 * Return TRUE if `from' is part of `addresses' taking into consideration
866 * the configuration variables that tells us how strict we should be about
867 * these checks
870 krb5_boolean
871 _kdc_check_addresses(krb5_context context,
872 krb5_kdc_configuration *config,
873 HostAddresses *addresses, const struct sockaddr *from)
875 krb5_error_code ret;
876 krb5_address addr;
877 krb5_boolean result;
878 krb5_boolean only_netbios = TRUE;
879 size_t i;
881 if(config->check_ticket_addresses == 0)
882 return TRUE;
884 if(addresses == NULL)
885 return config->allow_null_ticket_addresses;
887 for (i = 0; i < addresses->len; ++i) {
888 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
889 only_netbios = FALSE;
893 /* Windows sends it's netbios name, which I can only assume is
894 * used for the 'allowed workstations' check. This is painful,
895 * but we still want to check IP addresses if they happen to be
896 * present.
899 if(only_netbios)
900 return config->allow_null_ticket_addresses;
902 ret = krb5_sockaddr2address (context, from, &addr);
903 if(ret)
904 return FALSE;
906 result = krb5_address_search(context, &addr, addresses);
907 krb5_free_address (context, &addr);
908 return result;
915 static krb5_boolean
916 send_pac_p(krb5_context context, KDC_REQ *req)
918 krb5_error_code ret;
919 PA_PAC_REQUEST pacreq;
920 const PA_DATA *pa;
921 int i = 0;
923 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
924 if (pa == NULL)
925 return TRUE;
927 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
928 pa->padata_value.length,
929 &pacreq,
930 NULL);
931 if (ret)
932 return TRUE;
933 i = pacreq.include_pac;
934 free_PA_PAC_REQUEST(&pacreq);
935 if (i == 0)
936 return FALSE;
937 return TRUE;
940 krb5_boolean
941 _kdc_is_anonymous(krb5_context context, krb5_principal principal)
943 if (principal->name.name_type != KRB5_NT_WELLKNOWN ||
944 principal->name.name_string.len != 2 ||
945 strcmp(principal->name.name_string.val[0], KRB5_WELLKNOWN_NAME) != 0 ||
946 strcmp(principal->name.name_string.val[1], KRB5_ANON_NAME) != 0)
947 return 0;
948 return 1;
951 static krb5_error_code
952 get_local_tgs(krb5_context context,
953 krb5_kdc_configuration *config,
954 krb5_const_realm realm,
955 hdb_entry_ex **krbtgt)
957 krb5_error_code ret;
958 krb5_principal tgs_name;
960 *krbtgt = NULL;
962 ret = krb5_make_principal(context,
963 &tgs_name,
964 realm,
965 KRB5_TGS_NAME,
966 realm,
967 NULL);
968 if (ret)
969 return ret;
971 ret = _kdc_db_fetch(context, config, tgs_name,
972 HDB_F_GET_KRBTGT, NULL, NULL, krbtgt);
973 krb5_free_principal(context, tgs_name);
975 return ret;
982 krb5_error_code
983 _kdc_as_rep(krb5_context context,
984 krb5_kdc_configuration *config,
985 KDC_REQ *req,
986 const krb5_data *req_buffer,
987 krb5_data *reply,
988 const char *from,
989 struct sockaddr *from_addr,
990 int datagram_reply)
992 KDC_REQ_BODY *b = &req->req_body;
993 AS_REP rep;
994 KDCOptions f = b->kdc_options;
995 hdb_entry_ex *client = NULL, *server = NULL;
996 HDB *clientdb = NULL;
997 krb5_enctype setype, sessionetype;
998 krb5_data e_data;
999 EncTicketPart et;
1000 EncKDCRepPart ek;
1001 krb5_principal client_princ = NULL, server_princ = NULL;
1002 char *client_name = NULL, *server_name = NULL;
1003 krb5_error_code ret = 0;
1004 const char *e_text = NULL;
1005 krb5_crypto crypto;
1006 Key *skey = NULL;
1007 EncryptionKey *reply_key = NULL, session_key;
1008 int flags = HDB_F_FOR_AS_REQ;
1009 #ifdef PKINIT
1010 pk_client_params *pkp = NULL;
1011 #endif
1012 const EncryptionKey *pk_reply_key = NULL;
1013 krb5_boolean is_tgs;
1014 hdb_entry_ex *krbtgt = NULL;
1015 Key *krbtgt_key = NULL;
1017 memset(&rep, 0, sizeof(rep));
1018 memset(&session_key, 0, sizeof(session_key));
1019 krb5_data_zero(&e_data);
1021 ALLOC(rep.padata);
1022 rep.padata->len = 0;
1023 rep.padata->val = NULL;
1025 if (f.canonicalize)
1026 flags |= HDB_F_CANON;
1028 if(b->sname == NULL){
1029 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1030 e_text = "No server in request";
1031 } else{
1032 ret = _krb5_principalname2krb5_principal (context,
1033 &server_princ,
1034 *(b->sname),
1035 b->realm);
1036 if (ret == 0)
1037 ret = krb5_unparse_name(context, server_princ, &server_name);
1039 if (ret) {
1040 kdc_log(context, config, 0,
1041 "AS-REQ malformed server name from %s", from);
1042 goto out;
1044 if(b->cname == NULL){
1045 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1046 e_text = "No client in request";
1047 } else {
1048 ret = _krb5_principalname2krb5_principal (context,
1049 &client_princ,
1050 *(b->cname),
1051 b->realm);
1052 if (ret)
1053 goto out;
1055 ret = krb5_unparse_name(context, client_princ, &client_name);
1057 if (ret) {
1058 kdc_log(context, config, 0,
1059 "AS-REQ malformed client name from %s", from);
1060 goto out;
1063 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
1064 client_name, from, server_name);
1066 is_tgs = krb5_principal_is_krbtgt(context, server_princ);
1072 if (_kdc_is_anonymous(context, client_princ)) {
1073 if (!b->kdc_options.request_anonymous) {
1074 kdc_log(context, config, 0, "Anonymous ticket w/o anonymous flag");
1075 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1076 goto out;
1078 } else if (b->kdc_options.request_anonymous) {
1079 kdc_log(context, config, 0,
1080 "Request for a anonymous ticket with non "
1081 "anonymous client name: %s", client_name);
1082 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1083 goto out;
1090 ret = _kdc_db_fetch(context, config, client_princ,
1091 HDB_F_GET_CLIENT | flags, NULL,
1092 &clientdb, &client);
1093 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1094 kdc_log(context, config, 5, "client %s does not have secrets at this KDC, need to proxy", client_name);
1095 goto out;
1096 } else if (ret == HDB_ERR_WRONG_REALM) {
1097 char *fixed_client_name = NULL;
1099 ret = krb5_unparse_name(context, client->entry.principal,
1100 &fixed_client_name);
1101 if (ret) {
1102 goto out;
1105 kdc_log(context, config, 0, "WRONG_REALM - %s -> %s",
1106 client_name, fixed_client_name);
1107 free(fixed_client_name);
1109 ret = krb5_mk_error_ext(context,
1110 KRB5_KDC_ERR_WRONG_REALM,
1111 NULL, /* e_text */
1112 NULL, /* e_data */
1113 server_princ,
1114 NULL, /* client_name */
1115 &client->entry.principal->realm,
1116 NULL, /* client_time */
1117 NULL, /* client_usec */
1118 reply);
1119 goto out;
1120 } else if(ret){
1121 const char *msg = krb5_get_error_message(context, ret);
1122 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", client_name, msg);
1123 krb5_free_error_message(context, msg);
1124 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1126 if (config->db[0] && config->db[0]->hdb_auth_status)
1127 (config->db[0]->hdb_auth_status)(context, config->db[0], NULL,
1128 from_addr,
1129 &_kdc_now,
1130 client_name,
1131 NULL,
1132 HDB_AUTH_CLIENT_UNKNOWN);
1133 goto out;
1135 ret = _kdc_db_fetch(context, config, server_princ,
1136 HDB_F_GET_SERVER | flags | (is_tgs ? HDB_F_GET_KRBTGT : 0),
1137 NULL, NULL, &server);
1138 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1139 kdc_log(context, config, 5, "target %s does not have secrets at this KDC, need to proxy", server_name);
1140 goto out;
1141 } else if(ret){
1142 const char *msg = krb5_get_error_message(context, ret);
1143 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name, msg);
1144 krb5_free_error_message(context, msg);
1145 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1146 goto out;
1149 memset(&et, 0, sizeof(et));
1150 memset(&ek, 0, sizeof(ek));
1153 * Select a session enctype from the list of the crypto system
1154 * supported enctypes that is supported by the client and is one of
1155 * the enctype of the enctype of the service (likely krbtgt).
1157 * The latter is used as a hint of what enctypes all KDC support,
1158 * to make sure a newer version of KDC won't generate a session
1159 * enctype that an older version of a KDC in the same realm can't
1160 * decrypt.
1162 ret = _kdc_find_etype(context, config->as_use_strongest_session_key, FALSE,
1163 client, b->etype.val, b->etype.len, &sessionetype,
1164 NULL);
1165 if (ret) {
1166 kdc_log(context, config, 0,
1167 "Client (%s) from %s has no common enctypes with KDC "
1168 "to use for the session key",
1169 client_name, from);
1170 goto out;
1173 * But if the KDC admin is paranoid and doesn't want to have "not
1174 * the best" enctypes on the krbtgt, lets save the best pick from
1175 * the client list and hope that that will work for any other
1176 * KDCs.
1180 * Pre-auth processing
1183 if(req->padata){
1184 int i;
1185 const PA_DATA *pa;
1186 int found_pa = 0;
1188 log_patypes(context, config, req->padata);
1190 #ifdef PKINIT
1191 kdc_log(context, config, 5,
1192 "Looking for PKINIT pa-data -- %s", client_name);
1194 e_text = "No PKINIT PA found";
1196 i = 0;
1197 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ);
1198 if (pa == NULL) {
1199 i = 0;
1200 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN);
1202 if (pa) {
1203 char *client_cert = NULL;
1205 ret = _kdc_pk_rd_padata(context, config, req, pa, client, &pkp);
1206 if (ret) {
1207 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1208 kdc_log(context, config, 5,
1209 "Failed to decode PKINIT PA-DATA -- %s",
1210 client_name);
1211 goto ts_enc;
1213 if (ret == 0 && pkp == NULL)
1214 goto ts_enc;
1216 ret = _kdc_pk_check_client(context,
1217 config,
1218 clientdb,
1219 client,
1220 pkp,
1221 &client_cert);
1222 if (ret) {
1223 e_text = "PKINIT certificate not allowed to "
1224 "impersonate principal";
1225 _kdc_pk_free_client_param(context, pkp);
1227 kdc_log(context, config, 0, "%s", e_text);
1228 pkp = NULL;
1229 goto out;
1232 found_pa = 1;
1233 et.flags.pre_authent = 1;
1234 kdc_log(context, config, 0,
1235 "PKINIT pre-authentication succeeded -- %s using %s",
1236 client_name, client_cert);
1237 if (clientdb->hdb_auth_status)
1238 (clientdb->hdb_auth_status)(context, clientdb, client,
1239 from_addr,
1240 &_kdc_now,
1241 client_name,
1242 "PKINIT",
1243 HDB_AUTH_PKINIT_SUCCESS);
1244 free(client_cert);
1245 if (pkp)
1246 goto preauth_done;
1248 ts_enc:
1249 #endif
1251 if (client->entry.flags.locked_out) {
1252 ret = KRB5KDC_ERR_CLIENT_REVOKED;
1253 kdc_log(context, config, 0,
1254 "Client (%s) is locked out", client_name);
1255 goto out;
1258 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1259 client_name);
1261 i = 0;
1262 e_text = "No ENC-TS found";
1263 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1264 krb5_data ts_data;
1265 PA_ENC_TS_ENC p;
1266 size_t len;
1267 EncryptedData enc_data;
1268 Key *pa_key;
1269 char *str;
1271 found_pa = 1;
1273 if (b->kdc_options.request_anonymous) {
1274 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1275 kdc_log(context, config, 0, "ENC-TS doesn't support anon");
1276 goto out;
1279 ret = decode_EncryptedData(pa->padata_value.data,
1280 pa->padata_value.length,
1281 &enc_data,
1282 &len);
1283 if (ret) {
1284 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1285 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1286 client_name);
1287 goto out;
1290 ret = hdb_enctype2key(context, &client->entry,
1291 enc_data.etype, &pa_key);
1292 if(ret){
1293 char *estr;
1294 e_text = "No key matches pa-data";
1295 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1296 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1297 estr = NULL;
1298 if(estr == NULL)
1299 kdc_log(context, config, 5,
1300 "No client key matching pa-data (%d) -- %s",
1301 enc_data.etype, client_name);
1302 else
1303 kdc_log(context, config, 5,
1304 "No client key matching pa-data (%s) -- %s",
1305 estr, client_name);
1306 free(estr);
1307 free_EncryptedData(&enc_data);
1309 continue;
1312 try_next_key:
1313 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1314 if (ret) {
1315 const char *msg = krb5_get_error_message(context, ret);
1316 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
1317 krb5_free_error_message(context, msg);
1318 free_EncryptedData(&enc_data);
1319 continue;
1322 ret = krb5_decrypt_EncryptedData (context,
1323 crypto,
1324 KRB5_KU_PA_ENC_TIMESTAMP,
1325 &enc_data,
1326 &ts_data);
1327 krb5_crypto_destroy(context, crypto);
1329 * Since the user might have several keys with the same
1330 * enctype but with diffrent salting, we need to try all
1331 * the keys with the same enctype.
1333 if(ret){
1334 krb5_error_code ret2;
1335 const char *msg = krb5_get_error_message(context, ret);
1337 ret2 = krb5_enctype_to_string(context,
1338 pa_key->key.keytype, &str);
1339 if (ret2)
1340 str = NULL;
1342 kdc_log(context, config, 5,
1343 "Failed to decrypt PA-DATA -- %s "
1344 "(enctype %s) error %s",
1345 client_name, str ? str : "unknown enctype", msg);
1346 krb5_free_error_message(context, msg);
1348 if(hdb_next_enctype2key(context, &client->entry,
1349 enc_data.etype, &pa_key) == 0) {
1350 free(str);
1351 goto try_next_key;
1353 e_text = "Failed to decrypt PA-DATA";
1355 free_EncryptedData(&enc_data);
1357 if (clientdb->hdb_auth_status)
1358 (clientdb->hdb_auth_status)(context, clientdb, client,
1359 from_addr,
1360 &_kdc_now,
1361 client_name,
1362 str ? str : "unknown enctype",
1363 HDB_AUTH_WRONG_PASSWORD);
1365 free(str);
1367 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1368 continue;
1370 free_EncryptedData(&enc_data);
1371 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1372 ts_data.length,
1374 &len);
1375 krb5_data_free(&ts_data);
1376 if(ret){
1377 e_text = "Failed to decode PA-ENC-TS-ENC";
1378 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1379 kdc_log(context, config,
1380 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1381 client_name);
1382 continue;
1384 free_PA_ENC_TS_ENC(&p);
1385 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1386 char client_time[100];
1388 krb5_format_time(context, p.patimestamp,
1389 client_time, sizeof(client_time), TRUE);
1391 ret = KRB5KRB_AP_ERR_SKEW;
1392 kdc_log(context, config, 0,
1393 "Too large time skew, "
1394 "client time %s is out by %u > %u seconds -- %s",
1395 client_time,
1396 (unsigned)abs(kdc_time - p.patimestamp),
1397 context->max_skew,
1398 client_name);
1401 * The following is needed to make windows clients to
1402 * retry using the timestamp in the error message, if
1403 * there is a e_text, they become unhappy.
1405 e_text = NULL;
1406 goto out;
1408 et.flags.pre_authent = 1;
1410 set_salt_padata(rep.padata, pa_key->salt);
1412 reply_key = &pa_key->key;
1414 ret = krb5_enctype_to_string(context, pa_key->key.keytype, &str);
1415 if (ret)
1416 str = NULL;
1418 kdc_log(context, config, 2,
1419 "ENC-TS Pre-authentication succeeded -- %s using %s",
1420 client_name, str ? str : "unknown enctype");
1421 if (clientdb->hdb_auth_status)
1422 (clientdb->hdb_auth_status)(context, clientdb, client,
1423 from_addr,
1424 &_kdc_now,
1425 client_name,
1426 str ? str : "unknown enctype",
1427 HDB_AUTH_CORRECT_PASSWORD);
1429 free(str);
1430 break;
1432 #ifdef PKINIT
1433 preauth_done:
1434 #endif
1435 if(found_pa == 0 && config->require_preauth)
1436 goto use_pa;
1437 /* We come here if we found a pa-enc-timestamp, but if there
1438 was some problem with it, other than too large skew */
1439 if(found_pa && et.flags.pre_authent == 0){
1440 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1441 if (!prepare_enc_data(context, config, &e_data, b, client)) {
1442 goto out;
1444 e_text = NULL;
1445 goto out;
1447 }else if (config->require_preauth
1448 || b->kdc_options.request_anonymous /* hack to force anon */
1449 || client->entry.flags.require_preauth
1450 || server->entry.flags.require_preauth) {
1451 use_pa:
1452 if (!prepare_enc_data(context, config, &e_data, b, client)) {
1453 goto out;
1456 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1458 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1460 kdc_log(context, config, 0,
1461 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1462 client_name);
1463 goto out;
1466 e_text = NULL;
1469 * Verify flags after the user been required to prove its identity
1470 * with in a preauth mech.
1473 ret = _kdc_check_access(context, config, client, client_name,
1474 server, server_name,
1475 req, &e_data);
1476 if(ret)
1477 goto out;
1479 if (clientdb->hdb_auth_status)
1480 (clientdb->hdb_auth_status)(context, clientdb, client,
1481 from_addr,
1482 &_kdc_now,
1483 client_name,
1484 NULL,
1485 HDB_AUTHZ_SUCCESS);
1488 * Selelct the best encryption type for the KDC with out regard to
1489 * the client since the client never needs to read that data.
1492 ret = _kdc_get_preferred_key(context, config,
1493 server, server_name,
1494 &setype, &skey);
1495 if(ret)
1496 goto out;
1498 /* If server is not krbtgt, fetch local krbtgt key for signing authdata */
1499 if (is_tgs) {
1500 krbtgt_key = skey;
1501 } else {
1502 ret = get_local_tgs(context, config, server_princ->realm,
1503 &krbtgt);
1504 if (ret)
1505 goto out;
1507 ret = _kdc_get_preferred_key(context, config, krbtgt,
1508 server_princ->realm,
1509 NULL, &krbtgt_key);
1510 if (ret)
1511 goto out;
1514 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1515 || (f.request_anonymous && !config->allow_anonymous)) {
1516 ret = KRB5KDC_ERR_BADOPTION;
1517 e_text = "Bad KDC options";
1518 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1519 goto out;
1522 rep.pvno = 5;
1523 rep.msg_type = krb_as_rep;
1525 ret = copy_Realm(&client->entry.principal->realm, &rep.crealm);
1526 if (ret)
1527 goto out;
1528 ret = _krb5_principal2principalname(&rep.cname, client->entry.principal);
1529 if (ret)
1530 goto out;
1532 rep.ticket.tkt_vno = 5;
1533 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1534 _krb5_principal2principalname(&rep.ticket.sname,
1535 server->entry.principal);
1536 /* java 1.6 expects the name to be the same type, lets allow that
1537 * uncomplicated name-types, when f.canonicalize is not set (to
1538 * match Windows Server 1709). */
1539 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1540 if (!f.canonicalize
1541 && (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))) {
1542 rep.ticket.sname.name_type = b->sname->name_type;
1544 #undef CNT
1546 et.flags.initial = 1;
1547 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1548 et.flags.forwardable = f.forwardable;
1549 else if (f.forwardable) {
1550 e_text = "Ticket may not be forwardable";
1551 ret = KRB5KDC_ERR_POLICY;
1552 kdc_log(context, config, 0,
1553 "Ticket may not be forwardable -- %s", client_name);
1554 goto out;
1556 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1557 et.flags.proxiable = f.proxiable;
1558 else if (f.proxiable) {
1559 e_text = "Ticket may not be proxiable";
1560 ret = KRB5KDC_ERR_POLICY;
1561 kdc_log(context, config, 0,
1562 "Ticket may not be proxiable -- %s", client_name);
1563 goto out;
1565 if(client->entry.flags.postdate && server->entry.flags.postdate)
1566 et.flags.may_postdate = f.allow_postdate;
1567 else if (f.allow_postdate){
1568 e_text = "Ticket may not be postdate";
1569 ret = KRB5KDC_ERR_POLICY;
1570 kdc_log(context, config, 0,
1571 "Ticket may not be postdatable -- %s", client_name);
1572 goto out;
1575 /* check for valid set of addresses */
1576 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1577 e_text = "Bad address list in requested";
1578 ret = KRB5KRB_AP_ERR_BADADDR;
1579 kdc_log(context, config, 0,
1580 "Bad address list requested -- %s", client_name);
1581 goto out;
1584 ret = copy_PrincipalName(&rep.cname, &et.cname);
1585 if (ret)
1586 goto out;
1587 ret = copy_Realm(&rep.crealm, &et.crealm);
1588 if (ret)
1589 goto out;
1592 time_t start;
1593 time_t t;
1595 start = et.authtime = kdc_time;
1597 if(f.postdated && req->req_body.from){
1598 ALLOC(et.starttime);
1599 start = *et.starttime = *req->req_body.from;
1600 et.flags.invalid = 1;
1601 et.flags.postdated = 1; /* XXX ??? */
1603 _kdc_fix_time(&b->till);
1604 t = *b->till;
1606 /* be careful not overflowing */
1608 if(client->entry.max_life)
1609 t = start + min(t - start, *client->entry.max_life);
1610 if(server->entry.max_life)
1611 t = start + min(t - start, *server->entry.max_life);
1612 #if 0
1613 t = min(t, start + realm->max_life);
1614 #endif
1615 et.endtime = t;
1616 if(f.renewable_ok && et.endtime < *b->till){
1617 f.renewable = 1;
1618 if(b->rtime == NULL){
1619 ALLOC(b->rtime);
1620 *b->rtime = 0;
1622 if(*b->rtime < *b->till)
1623 *b->rtime = *b->till;
1625 if(f.renewable && b->rtime){
1626 t = *b->rtime;
1627 if(t == 0)
1628 t = MAX_TIME;
1629 if(client->entry.max_renew)
1630 t = start + min(t - start, *client->entry.max_renew);
1631 if(server->entry.max_renew)
1632 t = start + min(t - start, *server->entry.max_renew);
1633 #if 0
1634 t = min(t, start + realm->max_renew);
1635 #endif
1636 ALLOC(et.renew_till);
1637 *et.renew_till = t;
1638 et.flags.renewable = 1;
1642 if (f.request_anonymous)
1643 et.flags.anonymous = 1;
1645 if(b->addresses){
1646 ALLOC(et.caddr);
1647 copy_HostAddresses(b->addresses, et.caddr);
1650 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1651 krb5_data_zero(&et.transited.contents);
1653 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1654 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1655 * incapable of correctly decoding SEQUENCE OF's of zero length.
1657 * To fix this, always send at least one no-op last_req
1659 * If there's a pw_end or valid_end we will use that,
1660 * otherwise just a dummy lr.
1662 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1663 if (ek.last_req.val == NULL) {
1664 ret = ENOMEM;
1665 goto out;
1667 ek.last_req.len = 0;
1668 if (client->entry.pw_end
1669 && (config->kdc_warn_pwexpire == 0
1670 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1671 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1672 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1673 ++ek.last_req.len;
1675 if (client->entry.valid_end) {
1676 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1677 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1678 ++ek.last_req.len;
1680 if (ek.last_req.len == 0) {
1681 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1682 ek.last_req.val[ek.last_req.len].lr_value = 0;
1683 ++ek.last_req.len;
1685 ek.nonce = b->nonce;
1686 if (client->entry.valid_end || client->entry.pw_end) {
1687 ALLOC(ek.key_expiration);
1688 if (client->entry.valid_end) {
1689 if (client->entry.pw_end)
1690 *ek.key_expiration = min(*client->entry.valid_end,
1691 *client->entry.pw_end);
1692 else
1693 *ek.key_expiration = *client->entry.valid_end;
1694 } else
1695 *ek.key_expiration = *client->entry.pw_end;
1696 } else
1697 ek.key_expiration = NULL;
1698 ek.flags = et.flags;
1699 ek.authtime = et.authtime;
1700 if (et.starttime) {
1701 ALLOC(ek.starttime);
1702 *ek.starttime = *et.starttime;
1704 ek.endtime = et.endtime;
1705 if (et.renew_till) {
1706 ALLOC(ek.renew_till);
1707 *ek.renew_till = *et.renew_till;
1709 copy_Realm(&rep.ticket.realm, &ek.srealm);
1710 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1711 if(et.caddr){
1712 ALLOC(ek.caddr);
1713 copy_HostAddresses(et.caddr, ek.caddr);
1716 #ifdef PKINIT
1717 if (pkp) {
1718 e_text = "Failed to build PK-INIT reply";
1719 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1720 sessionetype, req, req_buffer,
1721 &reply_key, &et.key, rep.padata);
1722 if (ret)
1723 goto out;
1724 ret = _kdc_add_inital_verified_cas(context,
1725 config,
1726 pkp,
1727 &et);
1728 if (ret)
1729 goto out;
1732 * Send reply key as constant value to pac generate which allows
1733 * parts of the buffer to be encrypted (i.e., PAC_CREDENTIAL_DATA).
1735 pk_reply_key = reply_key;
1736 } else
1737 #endif
1739 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1740 if (ret)
1741 goto out;
1744 if (reply_key == NULL) {
1745 e_text = "Client have no reply key";
1746 ret = KRB5KDC_ERR_CLIENT_NOTYET;
1747 goto out;
1750 ret = copy_EncryptionKey(&et.key, &ek.key);
1751 if (ret)
1752 goto out;
1754 if (rep.padata->len == 0) {
1755 free(rep.padata);
1756 rep.padata = NULL;
1759 /* Add the PAC */
1760 if (send_pac_p(context, req)) {
1761 krb5_pac p = NULL;
1762 krb5_data data;
1763 uint16_t rodc_id;
1764 krb5_principal client_pac;
1766 ret = _kdc_pac_generate(context, client, pk_reply_key, &p);
1767 if (ret) {
1768 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1769 client_name);
1770 goto out;
1772 if (p != NULL) {
1773 rodc_id = server->entry.kvno >> 16;
1775 /* libkrb5 expects ticket and PAC client names to match */
1776 ret = _krb5_principalname2krb5_principal(context, &client_pac,
1777 et.cname, et.crealm);
1778 if (ret) {
1779 krb5_pac_free(context, p);
1780 goto out;
1783 ret = _krb5_pac_sign(context, p, et.authtime,
1784 client_pac,
1785 &skey->key, /* Server key */
1786 &krbtgt_key->key, /* TGS key */
1787 rodc_id,
1788 &data);
1789 krb5_free_principal(context, client_pac);
1790 krb5_pac_free(context, p);
1791 if (ret) {
1792 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1793 client_name);
1794 goto out;
1797 ret = _kdc_tkt_insert_pac(context, &et, &data);
1798 krb5_data_free(&data);
1799 if (ret)
1800 goto out;
1804 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1805 et.endtime, et.renew_till);
1807 log_as_req(context, config, reply_key->keytype, setype, b);
1809 ret = _kdc_encode_reply(context, config,
1810 &rep, &et, &ek, setype, server->entry.kvno,
1811 &skey->key, client->entry.kvno,
1812 reply_key, 0, &e_text, reply);
1813 free_EncTicketPart(&et);
1814 free_EncKDCRepPart(&ek);
1815 if (ret)
1816 goto out;
1818 /* */
1819 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1820 krb5_data_free(reply);
1821 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1822 e_text = "Reply packet too large";
1825 out:
1826 free_AS_REP(&rep);
1827 if(ret != 0 && ret != HDB_ERR_NOT_FOUND_HERE && reply->length == 0) {
1828 krb5_mk_error(context,
1829 ret,
1830 e_text,
1831 (e_data.data ? &e_data : NULL),
1832 client_princ,
1833 server_princ,
1834 NULL,
1835 NULL,
1836 reply);
1837 ret = 0;
1839 #ifdef PKINIT
1840 if (pkp)
1841 _kdc_pk_free_client_param(context, pkp);
1842 #endif
1843 if (e_data.data)
1844 free(e_data.data);
1845 if (client_princ)
1846 krb5_free_principal(context, client_princ);
1847 free(client_name);
1848 if (server_princ)
1849 krb5_free_principal(context, server_princ);
1850 free(server_name);
1851 if(client)
1852 _kdc_free_ent(context, client);
1853 if(server)
1854 _kdc_free_ent(context, server);
1855 if (krbtgt)
1856 _kdc_free_ent(context, krbtgt);
1857 return ret;
1860 krb5_boolean
1861 prepare_enc_data(krb5_context context,
1862 krb5_kdc_configuration *config,
1863 krb5_data *e_data,
1864 KDC_REQ_BODY *b,
1865 hdb_entry_ex *client)
1867 METHOD_DATA method_data;
1868 PA_DATA *pa;
1869 unsigned char *buf;
1870 size_t len;
1871 Key *ckey;
1872 krb5_error_code ret;
1874 method_data.len = 0;
1875 method_data.val = NULL;
1877 ret = realloc_method_data(&method_data);
1878 if (ret) {
1879 free_METHOD_DATA(&method_data);
1880 return FALSE;
1882 pa = &method_data.val[method_data.len-1];
1883 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1884 pa->padata_value.length = 0;
1885 pa->padata_value.data = NULL;
1887 #ifdef PKINIT
1888 ret = realloc_method_data(&method_data);
1889 if (ret) {
1890 free_METHOD_DATA(&method_data);
1891 return FALSE;
1893 pa = &method_data.val[method_data.len-1];
1894 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1895 pa->padata_value.length = 0;
1896 pa->padata_value.data = NULL;
1898 ret = realloc_method_data(&method_data);
1899 if (ret) {
1900 free_METHOD_DATA(&method_data);
1901 return FALSE;
1903 pa = &method_data.val[method_data.len-1];
1904 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1905 pa->padata_value.length = 0;
1906 pa->padata_value.data = NULL;
1907 #endif
1910 * If there is a client key, send ETYPE_INFO{,2}
1912 ret = _kdc_find_etype(context,
1913 config->preauth_use_strongest_session_key, TRUE,
1914 client, b->etype.val, b->etype.len, NULL, &ckey);
1915 if (ret == 0) {
1918 * RFC4120 requires:
1919 * - If the client only knows about old enctypes, then send
1920 * both info replies (we send 'info' first in the list).
1921 * - If the client is 'modern', because it knows about 'new'
1922 * enctype types, then only send the 'info2' reply.
1924 * Before we send the full list of etype-info data, we pick
1925 * the client key we would have used anyway below, just pick
1926 * that instead.
1929 if (older_enctype(ckey->key.keytype)) {
1930 ret = get_pa_etype_info(context, config,
1931 &method_data, ckey);
1932 if (ret) {
1933 free_METHOD_DATA(&method_data);
1934 return FALSE;
1937 ret = get_pa_etype_info2(context, config,
1938 &method_data, ckey);
1939 if (ret) {
1940 free_METHOD_DATA(&method_data);
1941 return FALSE;
1945 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1946 free_METHOD_DATA(&method_data);
1948 e_data->data = buf;
1949 e_data->length = len;
1951 return TRUE;