setup cfx context, found by Jaideep Padhye
[heimdal.git] / kdc / kerberos5.c
blob394f4dec67b86994237fce110302d688751fe5d5
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(*start < req->padata->len){
78 (*start)++;
79 if(req->padata->val[*start - 1].padata_type == 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, const hdb_entry_ex *princ,
127 krb5_enctype *etypes, unsigned len,
128 Key **ret_key)
130 int i;
131 krb5_error_code ret = KRB5KDC_ERR_ETYPE_NOSUPP;
132 krb5_salt def_salt;
134 krb5_get_pw_salt (context, princ->entry.principal, &def_salt);
136 for(i = 0; ret != 0 && i < len ; i++) {
137 Key *key = NULL;
139 if (krb5_enctype_valid(context, etypes[i]) != 0 &&
140 !_kdc_is_weak_exception(princ->entry.principal, etypes[i]))
141 continue;
143 while (hdb_next_enctype2key(context, &princ->entry, etypes[i], &key) == 0) {
144 if (key->key.keyvalue.length == 0) {
145 ret = KRB5KDC_ERR_NULL_KEY;
146 continue;
148 *ret_key = key;
149 ret = 0;
150 if (is_default_salt_p(&def_salt, key)) {
151 krb5_free_salt (context, def_salt);
152 return ret;
156 krb5_free_salt (context, def_salt);
157 return ret;
160 krb5_error_code
161 _kdc_make_anonymous_principalname (PrincipalName *pn)
163 pn->name_type = KRB5_NT_PRINCIPAL;
164 pn->name_string.len = 1;
165 pn->name_string.val = malloc(sizeof(*pn->name_string.val));
166 if (pn->name_string.val == NULL)
167 return ENOMEM;
168 pn->name_string.val[0] = strdup("anonymous");
169 if (pn->name_string.val[0] == NULL) {
170 free(pn->name_string.val);
171 pn->name_string.val = NULL;
172 return ENOMEM;
174 return 0;
177 void
178 _kdc_log_timestamp(krb5_context context,
179 krb5_kdc_configuration *config,
180 const char *type,
181 KerberosTime authtime, KerberosTime *starttime,
182 KerberosTime endtime, KerberosTime *renew_till)
184 char authtime_str[100], starttime_str[100],
185 endtime_str[100], renewtime_str[100];
187 krb5_format_time(context, authtime,
188 authtime_str, sizeof(authtime_str), TRUE);
189 if (starttime)
190 krb5_format_time(context, *starttime,
191 starttime_str, sizeof(starttime_str), TRUE);
192 else
193 strlcpy(starttime_str, "unset", sizeof(starttime_str));
194 krb5_format_time(context, endtime,
195 endtime_str, sizeof(endtime_str), TRUE);
196 if (renew_till)
197 krb5_format_time(context, *renew_till,
198 renewtime_str, sizeof(renewtime_str), TRUE);
199 else
200 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
202 kdc_log(context, config, 5,
203 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
204 type, authtime_str, starttime_str, endtime_str, renewtime_str);
207 static void
208 log_patypes(krb5_context context,
209 krb5_kdc_configuration *config,
210 METHOD_DATA *padata)
212 struct rk_strpool *p = NULL;
213 char *str;
214 int i;
216 for (i = 0; i < padata->len; i++) {
217 switch(padata->val[i].padata_type) {
218 case KRB5_PADATA_PK_AS_REQ:
219 p = rk_strpoolprintf(p, "PK-INIT(ietf)");
220 break;
221 case KRB5_PADATA_PK_AS_REQ_WIN:
222 p = rk_strpoolprintf(p, "PK-INIT(win2k)");
223 break;
224 case KRB5_PADATA_PA_PK_OCSP_RESPONSE:
225 p = rk_strpoolprintf(p, "OCSP");
226 break;
227 case KRB5_PADATA_ENC_TIMESTAMP:
228 p = rk_strpoolprintf(p, "encrypted-timestamp");
229 break;
230 default:
231 p = rk_strpoolprintf(p, "%d", padata->val[i].padata_type);
232 break;
234 if (p && i + 1 < padata->len)
235 p = rk_strpoolprintf(p, ", ");
236 if (p == NULL) {
237 kdc_log(context, config, 0, "out of memory");
238 return;
241 if (p == NULL)
242 p = rk_strpoolprintf(p, "none");
244 str = rk_strpoolcollect(p);
245 kdc_log(context, config, 0, "Client sent patypes: %s", str);
246 free(str);
254 krb5_error_code
255 _kdc_encode_reply(krb5_context context,
256 krb5_kdc_configuration *config,
257 KDC_REP *rep, const EncTicketPart *et, EncKDCRepPart *ek,
258 krb5_enctype etype,
259 int skvno, const EncryptionKey *skey,
260 int ckvno, const EncryptionKey *reply_key,
261 int rk_is_subkey,
262 const char **e_text,
263 krb5_data *reply)
265 unsigned char *buf;
266 size_t buf_size;
267 size_t len;
268 krb5_error_code ret;
269 krb5_crypto crypto;
271 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
272 if(ret) {
273 const char *msg = krb5_get_error_message(context, ret);
274 kdc_log(context, config, 0, "Failed to encode ticket: %s", msg);
275 krb5_free_error_message(context, msg);
276 return ret;
278 if(buf_size != len) {
279 free(buf);
280 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
281 *e_text = "KDC internal error";
282 return KRB5KRB_ERR_GENERIC;
285 ret = krb5_crypto_init(context, skey, etype, &crypto);
286 if (ret) {
287 const char *msg;
288 free(buf);
289 msg = krb5_get_error_message(context, ret);
290 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
291 krb5_free_error_message(context, msg);
292 return ret;
295 ret = krb5_encrypt_EncryptedData(context,
296 crypto,
297 KRB5_KU_TICKET,
298 buf,
299 len,
300 skvno,
301 &rep->ticket.enc_part);
302 free(buf);
303 krb5_crypto_destroy(context, crypto);
304 if(ret) {
305 const char *msg = krb5_get_error_message(context, ret);
306 kdc_log(context, config, 0, "Failed to encrypt data: %s", msg);
307 krb5_free_error_message(context, msg);
308 return ret;
311 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
312 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
313 else
314 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
315 if(ret) {
316 const char *msg = krb5_get_error_message(context, ret);
317 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
318 krb5_free_error_message(context, msg);
319 return ret;
321 if(buf_size != len) {
322 free(buf);
323 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
324 *e_text = "KDC internal error";
325 return KRB5KRB_ERR_GENERIC;
327 ret = krb5_crypto_init(context, reply_key, 0, &crypto);
328 if (ret) {
329 const char *msg = krb5_get_error_message(context, ret);
330 free(buf);
331 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
332 krb5_free_error_message(context, msg);
333 return ret;
335 if(rep->msg_type == krb_as_rep) {
336 krb5_encrypt_EncryptedData(context,
337 crypto,
338 KRB5_KU_AS_REP_ENC_PART,
339 buf,
340 len,
341 ckvno,
342 &rep->enc_part);
343 free(buf);
344 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
345 } else {
346 krb5_encrypt_EncryptedData(context,
347 crypto,
348 rk_is_subkey ? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY : KRB5_KU_TGS_REP_ENC_PART_SESSION,
349 buf,
350 len,
351 ckvno,
352 &rep->enc_part);
353 free(buf);
354 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
356 krb5_crypto_destroy(context, crypto);
357 if(ret) {
358 const char *msg = krb5_get_error_message(context, ret);
359 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
360 krb5_free_error_message(context, msg);
361 return ret;
363 if(buf_size != len) {
364 free(buf);
365 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
366 *e_text = "KDC internal error";
367 return KRB5KRB_ERR_GENERIC;
369 reply->data = buf;
370 reply->length = buf_size;
371 return 0;
375 * Return 1 if the client have only older enctypes, this is for
376 * determining if the server should send ETYPE_INFO2 or not.
379 static int
380 older_enctype(krb5_enctype enctype)
382 switch (enctype) {
383 case ETYPE_DES_CBC_CRC:
384 case ETYPE_DES_CBC_MD4:
385 case ETYPE_DES_CBC_MD5:
386 case ETYPE_DES3_CBC_SHA1:
387 case ETYPE_ARCFOUR_HMAC_MD5:
388 case ETYPE_ARCFOUR_HMAC_MD5_56:
390 * The following three is "old" windows enctypes and is needed for
391 * windows 2000 hosts.
393 case ETYPE_ARCFOUR_MD4:
394 case ETYPE_ARCFOUR_HMAC_OLD:
395 case ETYPE_ARCFOUR_HMAC_OLD_EXP:
396 return 1;
397 default:
398 return 0;
406 static krb5_error_code
407 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
409 ent->etype = key->key.keytype;
410 if(key->salt){
411 #if 0
412 ALLOC(ent->salttype);
414 if(key->salt->type == hdb_pw_salt)
415 *ent->salttype = 0; /* or 1? or NULL? */
416 else if(key->salt->type == hdb_afs3_salt)
417 *ent->salttype = 2;
418 else {
419 kdc_log(context, config, 0, "unknown salt-type: %d",
420 key->salt->type);
421 return KRB5KRB_ERR_GENERIC;
423 /* according to `the specs', we can't send a salt if
424 we have AFS3 salted key, but that requires that you
425 *know* what cell you are using (e.g by assuming
426 that the cell is the same as the realm in lower
427 case) */
428 #elif 0
429 ALLOC(ent->salttype);
430 *ent->salttype = key->salt->type;
431 #else
433 * We shouldn't sent salttype since it is incompatible with the
434 * specification and it breaks windows clients. The afs
435 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
436 * implemented in Heimdal 0.7 and later.
438 ent->salttype = NULL;
439 #endif
440 krb5_copy_data(context, &key->salt->salt,
441 &ent->salt);
442 } else {
443 /* we return no salt type at all, as that should indicate
444 * the default salt type and make everybody happy. some
445 * systems (like w2k) dislike being told the salt type
446 * here. */
448 ent->salttype = NULL;
449 ent->salt = NULL;
451 return 0;
454 static krb5_error_code
455 get_pa_etype_info(krb5_context context,
456 krb5_kdc_configuration *config,
457 METHOD_DATA *md, Key *ckey)
459 krb5_error_code ret = 0;
460 ETYPE_INFO pa;
461 unsigned char *buf;
462 size_t len;
465 pa.len = 1;
466 pa.val = calloc(1, sizeof(pa.val[0]));
467 if(pa.val == NULL)
468 return ENOMEM;
470 ret = make_etype_info_entry(context, &pa.val[0], ckey);
471 if (ret) {
472 free_ETYPE_INFO(&pa);
473 return ret;
476 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
477 free_ETYPE_INFO(&pa);
478 if(ret)
479 return ret;
480 ret = realloc_method_data(md);
481 if(ret) {
482 free(buf);
483 return ret;
485 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
486 md->val[md->len - 1].padata_value.length = len;
487 md->val[md->len - 1].padata_value.data = buf;
488 return 0;
495 extern int _krb5_AES_string_to_default_iterator;
497 static krb5_error_code
498 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
500 ent->etype = key->key.keytype;
501 if(key->salt) {
502 ALLOC(ent->salt);
503 if (ent->salt == NULL)
504 return ENOMEM;
505 *ent->salt = malloc(key->salt->salt.length + 1);
506 if (*ent->salt == NULL) {
507 free(ent->salt);
508 ent->salt = NULL;
509 return ENOMEM;
511 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
512 (*ent->salt)[key->salt->salt.length] = '\0';
513 } else
514 ent->salt = NULL;
516 ent->s2kparams = NULL;
518 switch (key->key.keytype) {
519 case ETYPE_AES128_CTS_HMAC_SHA1_96:
520 case ETYPE_AES256_CTS_HMAC_SHA1_96:
521 ALLOC(ent->s2kparams);
522 if (ent->s2kparams == NULL)
523 return ENOMEM;
524 ent->s2kparams->length = 4;
525 ent->s2kparams->data = malloc(ent->s2kparams->length);
526 if (ent->s2kparams->data == NULL) {
527 free(ent->s2kparams);
528 ent->s2kparams = NULL;
529 return ENOMEM;
531 _krb5_put_int(ent->s2kparams->data,
532 _krb5_AES_string_to_default_iterator,
533 ent->s2kparams->length);
534 break;
535 case ETYPE_DES_CBC_CRC:
536 case ETYPE_DES_CBC_MD4:
537 case ETYPE_DES_CBC_MD5:
538 /* Check if this was a AFS3 salted key */
539 if(key->salt && key->salt->type == hdb_afs3_salt){
540 ALLOC(ent->s2kparams);
541 if (ent->s2kparams == NULL)
542 return ENOMEM;
543 ent->s2kparams->length = 1;
544 ent->s2kparams->data = malloc(ent->s2kparams->length);
545 if (ent->s2kparams->data == NULL) {
546 free(ent->s2kparams);
547 ent->s2kparams = NULL;
548 return ENOMEM;
550 _krb5_put_int(ent->s2kparams->data,
552 ent->s2kparams->length);
554 break;
555 default:
556 break;
558 return 0;
562 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
563 * database (client supported enctypes first, then the unsupported
564 * enctypes).
567 static krb5_error_code
568 get_pa_etype_info2(krb5_context context,
569 krb5_kdc_configuration *config,
570 METHOD_DATA *md, Key *ckey)
572 krb5_error_code ret = 0;
573 ETYPE_INFO2 pa;
574 unsigned char *buf;
575 size_t len;
577 pa.len = 1;
578 pa.val = calloc(1, sizeof(pa.val[0]));
579 if(pa.val == NULL)
580 return ENOMEM;
582 ret = make_etype_info2_entry(&pa.val[0], ckey);
583 if (ret) {
584 free_ETYPE_INFO2(&pa);
585 return ret;
588 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
589 free_ETYPE_INFO2(&pa);
590 if(ret)
591 return ret;
592 ret = realloc_method_data(md);
593 if(ret) {
594 free(buf);
595 return ret;
597 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
598 md->val[md->len - 1].padata_value.length = len;
599 md->val[md->len - 1].padata_value.data = buf;
600 return 0;
607 static void
608 log_as_req(krb5_context context,
609 krb5_kdc_configuration *config,
610 krb5_enctype cetype,
611 krb5_enctype setype,
612 const KDC_REQ_BODY *b)
614 krb5_error_code ret;
615 struct rk_strpool *p;
616 char *str;
617 int i;
619 p = rk_strpoolprintf(NULL, "%s", "Client supported enctypes: ");
621 for (i = 0; i < b->etype.len; i++) {
622 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
623 if (ret == 0) {
624 p = rk_strpoolprintf(p, "%s", str);
625 free(str);
626 } else
627 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
628 if (p && i + 1 < b->etype.len)
629 p = rk_strpoolprintf(p, ", ");
630 if (p == NULL) {
631 kdc_log(context, config, 0, "out of memory");
632 return;
635 if (p == NULL)
636 p = rk_strpoolprintf(p, "no encryption types");
639 char *cet;
640 char *set;
642 ret = krb5_enctype_to_string(context, cetype, &cet);
643 if(ret == 0) {
644 ret = krb5_enctype_to_string(context, setype, &set);
645 if (ret == 0) {
646 p = rk_strpoolprintf(p, ", using %s/%s", cet, set);
647 free(set);
649 free(cet);
651 if (ret != 0)
652 p = rk_strpoolprintf(p, ", using enctypes %d/%d",
653 cetype, setype);
656 str = rk_strpoolcollect(p);
657 kdc_log(context, config, 0, "%s", str);
658 free(str);
661 char fixedstr[128];
662 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
663 fixedstr, sizeof(fixedstr));
664 if(*fixedstr)
665 kdc_log(context, config, 0, "Requested flags: %s", fixedstr);
670 * verify the flags on `client' and `server', returning 0
671 * if they are OK and generating an error messages and returning
672 * and error code otherwise.
675 krb5_error_code
676 kdc_check_flags(krb5_context context,
677 krb5_kdc_configuration *config,
678 hdb_entry_ex *client_ex, const char *client_name,
679 hdb_entry_ex *server_ex, const char *server_name,
680 krb5_boolean is_as_req)
682 if(client_ex != NULL) {
683 hdb_entry *client = &client_ex->entry;
685 /* check client */
686 if (client->flags.locked_out) {
687 kdc_log(context, config, 0,
688 "Client (%s) is locked out", client_name);
689 return KRB5KDC_ERR_POLICY;
692 if (client->flags.invalid) {
693 kdc_log(context, config, 0,
694 "Client (%s) has invalid bit set", client_name);
695 return KRB5KDC_ERR_POLICY;
698 if(!client->flags.client){
699 kdc_log(context, config, 0,
700 "Principal may not act as client -- %s", client_name);
701 return KRB5KDC_ERR_POLICY;
704 if (client->valid_start && *client->valid_start > kdc_time) {
705 char starttime_str[100];
706 krb5_format_time(context, *client->valid_start,
707 starttime_str, sizeof(starttime_str), TRUE);
708 kdc_log(context, config, 0,
709 "Client not yet valid until %s -- %s",
710 starttime_str, client_name);
711 return KRB5KDC_ERR_CLIENT_NOTYET;
714 if (client->valid_end && *client->valid_end < kdc_time) {
715 char endtime_str[100];
716 krb5_format_time(context, *client->valid_end,
717 endtime_str, sizeof(endtime_str), TRUE);
718 kdc_log(context, config, 0,
719 "Client expired at %s -- %s",
720 endtime_str, client_name);
721 return KRB5KDC_ERR_NAME_EXP;
724 if (client->pw_end && *client->pw_end < kdc_time
725 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
726 char pwend_str[100];
727 krb5_format_time(context, *client->pw_end,
728 pwend_str, sizeof(pwend_str), TRUE);
729 kdc_log(context, config, 0,
730 "Client's key has expired at %s -- %s",
731 pwend_str, client_name);
732 return KRB5KDC_ERR_KEY_EXPIRED;
736 /* check server */
738 if (server_ex != NULL) {
739 hdb_entry *server = &server_ex->entry;
741 if (server->flags.locked_out) {
742 kdc_log(context, config, 0,
743 "Client server locked out -- %s", server_name);
744 return KRB5KDC_ERR_POLICY;
746 if (server->flags.invalid) {
747 kdc_log(context, config, 0,
748 "Server has invalid flag set -- %s", server_name);
749 return KRB5KDC_ERR_POLICY;
752 if(!server->flags.server){
753 kdc_log(context, config, 0,
754 "Principal may not act as server -- %s", server_name);
755 return KRB5KDC_ERR_POLICY;
758 if(!is_as_req && server->flags.initial) {
759 kdc_log(context, config, 0,
760 "AS-REQ is required for server -- %s", server_name);
761 return KRB5KDC_ERR_POLICY;
764 if (server->valid_start && *server->valid_start > kdc_time) {
765 char starttime_str[100];
766 krb5_format_time(context, *server->valid_start,
767 starttime_str, sizeof(starttime_str), TRUE);
768 kdc_log(context, config, 0,
769 "Server not yet valid until %s -- %s",
770 starttime_str, server_name);
771 return KRB5KDC_ERR_SERVICE_NOTYET;
774 if (server->valid_end && *server->valid_end < kdc_time) {
775 char endtime_str[100];
776 krb5_format_time(context, *server->valid_end,
777 endtime_str, sizeof(endtime_str), TRUE);
778 kdc_log(context, config, 0,
779 "Server expired at %s -- %s",
780 endtime_str, server_name);
781 return KRB5KDC_ERR_SERVICE_EXP;
784 if (server->pw_end && *server->pw_end < kdc_time) {
785 char pwend_str[100];
786 krb5_format_time(context, *server->pw_end,
787 pwend_str, sizeof(pwend_str), TRUE);
788 kdc_log(context, config, 0,
789 "Server's key has expired at -- %s",
790 pwend_str, server_name);
791 return KRB5KDC_ERR_KEY_EXPIRED;
794 return 0;
798 * Return TRUE if `from' is part of `addresses' taking into consideration
799 * the configuration variables that tells us how strict we should be about
800 * these checks
803 krb5_boolean
804 _kdc_check_addresses(krb5_context context,
805 krb5_kdc_configuration *config,
806 HostAddresses *addresses, const struct sockaddr *from)
808 krb5_error_code ret;
809 krb5_address addr;
810 krb5_boolean result;
811 krb5_boolean only_netbios = TRUE;
812 int i;
814 if(config->check_ticket_addresses == 0)
815 return TRUE;
817 if(addresses == NULL)
818 return config->allow_null_ticket_addresses;
820 for (i = 0; i < addresses->len; ++i) {
821 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
822 only_netbios = FALSE;
826 /* Windows sends it's netbios name, which I can only assume is
827 * used for the 'allowed workstations' check. This is painful,
828 * but we still want to check IP addresses if they happen to be
829 * present.
832 if(only_netbios)
833 return config->allow_null_ticket_addresses;
835 ret = krb5_sockaddr2address (context, from, &addr);
836 if(ret)
837 return FALSE;
839 result = krb5_address_search(context, &addr, addresses);
840 krb5_free_address (context, &addr);
841 return result;
848 static krb5_boolean
849 send_pac_p(krb5_context context, KDC_REQ *req)
851 krb5_error_code ret;
852 PA_PAC_REQUEST pacreq;
853 const PA_DATA *pa;
854 int i = 0;
856 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
857 if (pa == NULL)
858 return TRUE;
860 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
861 pa->padata_value.length,
862 &pacreq,
863 NULL);
864 if (ret)
865 return TRUE;
866 i = pacreq.include_pac;
867 free_PA_PAC_REQUEST(&pacreq);
868 if (i == 0)
869 return FALSE;
870 return TRUE;
873 krb5_boolean
874 _kdc_is_anonymous(krb5_context context, krb5_principal principal)
876 if (principal->name.name_type != KRB5_NT_WELLKNOWN ||
877 principal->name.name_string.len != 2 ||
878 strcmp(principal->name.name_string.val[0], KRB5_WELLKNOWN_NAME) != 0 ||
879 strcmp(principal->name.name_string.val[1], KRB5_ANON_NAME) != 0)
880 return 0;
881 return 1;
888 krb5_error_code
889 _kdc_as_rep(krb5_context context,
890 krb5_kdc_configuration *config,
891 KDC_REQ *req,
892 const krb5_data *req_buffer,
893 krb5_data *reply,
894 const char *from,
895 struct sockaddr *from_addr,
896 int datagram_reply)
898 KDC_REQ_BODY *b = &req->req_body;
899 AS_REP rep;
900 KDCOptions f = b->kdc_options;
901 hdb_entry_ex *client = NULL, *server = NULL;
902 HDB *clientdb;
903 krb5_enctype setype, sessionetype;
904 krb5_data e_data;
905 EncTicketPart et;
906 EncKDCRepPart ek;
907 krb5_principal client_princ = NULL, server_princ = NULL;
908 char *client_name = NULL, *server_name = NULL;
909 krb5_error_code ret = 0;
910 const char *e_text = NULL;
911 krb5_crypto crypto;
912 Key *ckey, *skey;
913 EncryptionKey *reply_key = NULL, session_key;
914 int flags = 0;
915 #ifdef PKINIT
916 pk_client_params *pkp = NULL;
917 #endif
919 memset(&rep, 0, sizeof(rep));
920 memset(&session_key, 0, sizeof(session_key));
921 krb5_data_zero(&e_data);
923 ALLOC(rep.padata);
924 rep.padata->len = 0;
925 rep.padata->val = NULL;
927 if (f.canonicalize)
928 flags |= HDB_F_CANON;
930 if(b->sname == NULL){
931 ret = KRB5KRB_ERR_GENERIC;
932 e_text = "No server in request";
933 } else{
934 ret = _krb5_principalname2krb5_principal (context,
935 &server_princ,
936 *(b->sname),
937 b->realm);
938 if (ret == 0)
939 ret = krb5_unparse_name(context, server_princ, &server_name);
941 if (ret) {
942 kdc_log(context, config, 0,
943 "AS-REQ malformed server name from %s", from);
944 goto out;
946 if(b->cname == NULL){
947 ret = KRB5KRB_ERR_GENERIC;
948 e_text = "No client in request";
949 } else {
950 ret = _krb5_principalname2krb5_principal (context,
951 &client_princ,
952 *(b->cname),
953 b->realm);
954 if (ret)
955 goto out;
957 ret = krb5_unparse_name(context, client_princ, &client_name);
959 if (ret) {
960 kdc_log(context, config, 0,
961 "AS-REQ malformed client name from %s", from);
962 goto out;
965 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
966 client_name, from, server_name);
972 if (_kdc_is_anonymous(context, client_princ)) {
973 if (!b->kdc_options.request_anonymous) {
974 kdc_log(context, config, 0, "Anonymous ticket w/o anonymous flag");
975 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
976 goto out;
978 } else if (b->kdc_options.request_anonymous) {
979 kdc_log(context, config, 0,
980 "Request for a anonymous ticket with non "
981 "anonymous client name: %s", client_name);
982 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
983 goto out;
990 ret = _kdc_db_fetch(context, config, client_princ,
991 HDB_F_GET_CLIENT | flags, NULL,
992 &clientdb, &client);
993 if(ret == HDB_ERR_NOT_FOUND_HERE) {
994 kdc_log(context, config, 5, "client %s does not have secrets at this KDC, need to proxy", client_name);
995 goto out;
996 } else if(ret){
997 const char *msg = krb5_get_error_message(context, ret);
998 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", client_name, msg);
999 krb5_free_error_message(context, msg);
1000 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1001 goto out;
1004 ret = _kdc_db_fetch(context, config, server_princ,
1005 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
1006 NULL, NULL, &server);
1007 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1008 kdc_log(context, config, 5, "target %s does not have secrets at this KDC, need to proxy", server_name);
1009 goto out;
1010 } else if(ret){
1011 const char *msg = krb5_get_error_message(context, ret);
1012 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name, msg);
1013 krb5_free_error_message(context, msg);
1014 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1015 goto out;
1018 memset(&et, 0, sizeof(et));
1019 memset(&ek, 0, sizeof(ek));
1022 * Select a session enctype from the list of the crypto systems
1023 * supported enctype, is supported by the client and is one of the
1024 * enctype of the enctype of the krbtgt.
1026 * The later is used as a hint what enctype all KDC are supporting
1027 * to make sure a newer version of KDC wont generate a session
1028 * enctype that and older version of a KDC in the same realm can't
1029 * decrypt.
1031 * But if the KDC admin is paranoid and doesn't want to have "no
1032 * the best" enctypes on the krbtgt, lets save the best pick from
1033 * the client list and hope that that will work for any other
1034 * KDCs.
1037 const krb5_enctype *p;
1038 krb5_enctype clientbest = ETYPE_NULL;
1039 int i, j;
1041 p = krb5_kerberos_enctypes(context);
1043 sessionetype = ETYPE_NULL;
1045 for (i = 0; p[i] != ETYPE_NULL && sessionetype == ETYPE_NULL; i++) {
1046 if (krb5_enctype_valid(context, p[i]) != 0)
1047 continue;
1049 for (j = 0; j < b->etype.len && sessionetype == ETYPE_NULL; j++) {
1050 Key *dummy;
1051 /* check with client */
1052 if (p[i] != b->etype.val[j])
1053 continue;
1054 /* save best of union of { client, crypto system } */
1055 if (clientbest == ETYPE_NULL)
1056 clientbest = p[i];
1057 /* check with krbtgt */
1058 ret = hdb_enctype2key(context, &server->entry, p[i], &dummy);
1059 if (ret)
1060 continue;
1061 sessionetype = p[i];
1064 /* if krbtgt had no shared keys with client, pick clients best */
1065 if (clientbest != ETYPE_NULL && sessionetype == ETYPE_NULL) {
1066 sessionetype = clientbest;
1067 } else if (sessionetype == ETYPE_NULL) {
1068 kdc_log(context, config, 0,
1069 "Client (%s) from %s has no common enctypes with KDC"
1070 "to use for the session key",
1071 client_name, from);
1072 goto out;
1077 * Pre-auth processing
1080 if(req->padata){
1081 int i;
1082 const PA_DATA *pa;
1083 int found_pa = 0;
1085 log_patypes(context, config, req->padata);
1087 #ifdef PKINIT
1088 kdc_log(context, config, 5,
1089 "Looking for PKINIT pa-data -- %s", client_name);
1091 e_text = "No PKINIT PA found";
1093 i = 0;
1094 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ);
1095 if (pa == NULL) {
1096 i = 0;
1097 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN);
1099 if (pa) {
1100 char *client_cert = NULL;
1102 ret = _kdc_pk_rd_padata(context, config, req, pa, client, &pkp);
1103 if (ret) {
1104 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1105 kdc_log(context, config, 5,
1106 "Failed to decode PKINIT PA-DATA -- %s",
1107 client_name);
1108 goto ts_enc;
1110 if (ret == 0 && pkp == NULL)
1111 goto ts_enc;
1113 ret = _kdc_pk_check_client(context,
1114 config,
1115 clientdb,
1116 client,
1117 pkp,
1118 &client_cert);
1119 if (ret) {
1120 e_text = "PKINIT certificate not allowed to "
1121 "impersonate principal";
1122 _kdc_pk_free_client_param(context, pkp);
1124 kdc_log(context, config, 0, "%s", e_text);
1125 pkp = NULL;
1126 goto out;
1129 found_pa = 1;
1130 et.flags.pre_authent = 1;
1131 kdc_log(context, config, 0,
1132 "PKINIT pre-authentication succeeded -- %s using %s",
1133 client_name, client_cert);
1134 free(client_cert);
1135 if (pkp)
1136 goto preauth_done;
1138 ts_enc:
1139 #endif
1140 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1141 client_name);
1143 i = 0;
1144 e_text = "No ENC-TS found";
1145 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1146 krb5_data ts_data;
1147 PA_ENC_TS_ENC p;
1148 size_t len;
1149 EncryptedData enc_data;
1150 Key *pa_key;
1151 char *str;
1153 found_pa = 1;
1155 if (b->kdc_options.request_anonymous) {
1156 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1157 kdc_log(context, config, 0, "ENC-TS doesn't support anon");
1158 goto out;
1161 ret = decode_EncryptedData(pa->padata_value.data,
1162 pa->padata_value.length,
1163 &enc_data,
1164 &len);
1165 if (ret) {
1166 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1167 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1168 client_name);
1169 goto out;
1172 ret = hdb_enctype2key(context, &client->entry,
1173 enc_data.etype, &pa_key);
1174 if(ret){
1175 char *estr;
1176 e_text = "No key matches pa-data";
1177 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1178 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1179 estr = NULL;
1180 if(estr == NULL)
1181 kdc_log(context, config, 5,
1182 "No client key matching pa-data (%d) -- %s",
1183 enc_data.etype, client_name);
1184 else
1185 kdc_log(context, config, 5,
1186 "No client key matching pa-data (%s) -- %s",
1187 estr, client_name);
1188 free(estr);
1189 free_EncryptedData(&enc_data);
1191 continue;
1194 try_next_key:
1195 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1196 if (ret) {
1197 const char *msg = krb5_get_error_message(context, ret);
1198 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
1199 krb5_free_error_message(context, msg);
1200 free_EncryptedData(&enc_data);
1201 continue;
1204 ret = krb5_decrypt_EncryptedData (context,
1205 crypto,
1206 KRB5_KU_PA_ENC_TIMESTAMP,
1207 &enc_data,
1208 &ts_data);
1209 krb5_crypto_destroy(context, crypto);
1211 * Since the user might have several keys with the same
1212 * enctype but with diffrent salting, we need to try all
1213 * the keys with the same enctype.
1215 if(ret){
1216 krb5_error_code ret2;
1217 const char *msg = krb5_get_error_message(context, ret);
1219 ret2 = krb5_enctype_to_string(context,
1220 pa_key->key.keytype, &str);
1221 if (ret2)
1222 str = NULL;
1223 kdc_log(context, config, 5,
1224 "Failed to decrypt PA-DATA -- %s "
1225 "(enctype %s) error %s",
1226 client_name, str ? str : "unknown enctype", msg);
1227 krb5_free_error_message(context, msg);
1228 free(str);
1230 if(hdb_next_enctype2key(context, &client->entry,
1231 enc_data.etype, &pa_key) == 0)
1232 goto try_next_key;
1233 e_text = "Failed to decrypt PA-DATA";
1235 free_EncryptedData(&enc_data);
1237 if (clientdb->hdb_auth_status)
1238 (clientdb->hdb_auth_status)(context, clientdb, client, HDB_AUTH_WRONG_PASSWORD);
1240 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1241 continue;
1243 free_EncryptedData(&enc_data);
1244 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1245 ts_data.length,
1247 &len);
1248 krb5_data_free(&ts_data);
1249 if(ret){
1250 e_text = "Failed to decode PA-ENC-TS-ENC";
1251 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1252 kdc_log(context, config,
1253 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1254 client_name);
1255 continue;
1257 free_PA_ENC_TS_ENC(&p);
1258 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1259 char client_time[100];
1261 krb5_format_time(context, p.patimestamp,
1262 client_time, sizeof(client_time), TRUE);
1264 ret = KRB5KRB_AP_ERR_SKEW;
1265 kdc_log(context, config, 0,
1266 "Too large time skew, "
1267 "client time %s is out by %u > %u seconds -- %s",
1268 client_time,
1269 (unsigned)abs(kdc_time - p.patimestamp),
1270 context->max_skew,
1271 client_name);
1274 * The following is needed to make windows clients to
1275 * retry using the timestamp in the error message, if
1276 * there is a e_text, they become unhappy.
1278 e_text = NULL;
1279 goto out;
1281 et.flags.pre_authent = 1;
1283 set_salt_padata(rep.padata, pa_key->salt);
1285 reply_key = &pa_key->key;
1287 ret = krb5_enctype_to_string(context, pa_key->key.keytype, &str);
1288 if (ret)
1289 str = NULL;
1291 kdc_log(context, config, 2,
1292 "ENC-TS Pre-authentication succeeded -- %s using %s",
1293 client_name, str ? str : "unknown enctype");
1294 free(str);
1295 break;
1297 #ifdef PKINIT
1298 preauth_done:
1299 #endif
1300 if(found_pa == 0 && config->require_preauth)
1301 goto use_pa;
1302 /* We come here if we found a pa-enc-timestamp, but if there
1303 was some problem with it, other than too large skew */
1304 if(found_pa && et.flags.pre_authent == 0){
1305 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1306 e_text = NULL;
1307 goto out;
1309 }else if (config->require_preauth
1310 || b->kdc_options.request_anonymous /* hack to force anon */
1311 || client->entry.flags.require_preauth
1312 || server->entry.flags.require_preauth) {
1313 METHOD_DATA method_data;
1314 PA_DATA *pa;
1315 unsigned char *buf;
1316 size_t len;
1318 use_pa:
1319 method_data.len = 0;
1320 method_data.val = NULL;
1322 ret = realloc_method_data(&method_data);
1323 if (ret) {
1324 free_METHOD_DATA(&method_data);
1325 goto out;
1327 pa = &method_data.val[method_data.len-1];
1328 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1329 pa->padata_value.length = 0;
1330 pa->padata_value.data = NULL;
1332 #ifdef PKINIT
1333 ret = realloc_method_data(&method_data);
1334 if (ret) {
1335 free_METHOD_DATA(&method_data);
1336 goto out;
1338 pa = &method_data.val[method_data.len-1];
1339 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1340 pa->padata_value.length = 0;
1341 pa->padata_value.data = NULL;
1343 ret = realloc_method_data(&method_data);
1344 if (ret) {
1345 free_METHOD_DATA(&method_data);
1346 goto out;
1348 pa = &method_data.val[method_data.len-1];
1349 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1350 pa->padata_value.length = 0;
1351 pa->padata_value.data = NULL;
1352 #endif
1355 * If there is a client key, send ETYPE_INFO{,2}
1357 ret = _kdc_find_etype(context, client, b->etype.val, b->etype.len,
1358 &ckey);
1359 if (ret == 0) {
1362 * RFC4120 requires:
1363 * - If the client only knows about old enctypes, then send
1364 * both info replies (we send 'info' first in the list).
1365 * - If the client is 'modern', because it knows about 'new'
1366 * enctype types, then only send the 'info2' reply.
1368 * Before we send the full list of etype-info data, we pick
1369 * the client key we would have used anyway below, just pick
1370 * that instead.
1373 if (older_enctype(ckey->key.keytype)) {
1374 ret = get_pa_etype_info(context, config,
1375 &method_data, ckey);
1376 if (ret) {
1377 free_METHOD_DATA(&method_data);
1378 goto out;
1381 ret = get_pa_etype_info2(context, config,
1382 &method_data, ckey);
1383 if (ret) {
1384 free_METHOD_DATA(&method_data);
1385 goto out;
1389 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1390 free_METHOD_DATA(&method_data);
1392 e_data.data = buf;
1393 e_data.length = len;
1394 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1396 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1398 kdc_log(context, config, 0,
1399 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1400 client_name);
1401 goto out;
1404 if (clientdb->hdb_auth_status)
1405 (clientdb->hdb_auth_status)(context, clientdb, client,
1406 HDB_AUTH_SUCCESS);
1409 * Verify flags after the user been required to prove its identity
1410 * with in a preauth mech.
1413 ret = _kdc_check_access(context, config, client, client_name,
1414 server, server_name,
1415 req, &e_data);
1416 if(ret)
1417 goto out;
1420 * Selelct the best encryption type for the KDC with out regard to
1421 * the client since the client never needs to read that data.
1424 ret = _kdc_get_preferred_key(context, config,
1425 server, server_name,
1426 &setype, &skey);
1427 if(ret)
1428 goto out;
1430 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1431 || (f.request_anonymous && !config->allow_anonymous)) {
1432 ret = KRB5KDC_ERR_BADOPTION;
1433 e_text = "Bad KDC options";
1434 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1435 goto out;
1438 rep.pvno = 5;
1439 rep.msg_type = krb_as_rep;
1441 ret = copy_Realm(&client->entry.principal->realm, &rep.crealm);
1442 if (ret)
1443 goto out;
1444 ret = _krb5_principal2principalname(&rep.cname, client->entry.principal);
1445 if (ret)
1446 goto out;
1448 rep.ticket.tkt_vno = 5;
1449 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1450 _krb5_principal2principalname(&rep.ticket.sname,
1451 server->entry.principal);
1452 /* java 1.6 expects the name to be the same type, lets allow that
1453 * uncomplicated name-types. */
1454 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1455 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1456 rep.ticket.sname.name_type = b->sname->name_type;
1457 #undef CNT
1459 et.flags.initial = 1;
1460 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1461 et.flags.forwardable = f.forwardable;
1462 else if (f.forwardable) {
1463 e_text = "Ticket may not be forwardable";
1464 ret = KRB5KDC_ERR_POLICY;
1465 kdc_log(context, config, 0,
1466 "Ticket may not be forwardable -- %s", client_name);
1467 goto out;
1469 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1470 et.flags.proxiable = f.proxiable;
1471 else if (f.proxiable) {
1472 e_text = "Ticket may not be proxiable";
1473 ret = KRB5KDC_ERR_POLICY;
1474 kdc_log(context, config, 0,
1475 "Ticket may not be proxiable -- %s", client_name);
1476 goto out;
1478 if(client->entry.flags.postdate && server->entry.flags.postdate)
1479 et.flags.may_postdate = f.allow_postdate;
1480 else if (f.allow_postdate){
1481 e_text = "Ticket may not be postdate";
1482 ret = KRB5KDC_ERR_POLICY;
1483 kdc_log(context, config, 0,
1484 "Ticket may not be postdatable -- %s", client_name);
1485 goto out;
1488 /* check for valid set of addresses */
1489 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1490 e_text = "Bad address list in requested";
1491 ret = KRB5KRB_AP_ERR_BADADDR;
1492 kdc_log(context, config, 0,
1493 "Bad address list requested -- %s", client_name);
1494 goto out;
1497 ret = copy_PrincipalName(&rep.cname, &et.cname);
1498 if (ret)
1499 goto out;
1500 ret = copy_Realm(&rep.crealm, &et.crealm);
1501 if (ret)
1502 goto out;
1505 time_t start;
1506 time_t t;
1508 start = et.authtime = kdc_time;
1510 if(f.postdated && req->req_body.from){
1511 ALLOC(et.starttime);
1512 start = *et.starttime = *req->req_body.from;
1513 et.flags.invalid = 1;
1514 et.flags.postdated = 1; /* XXX ??? */
1516 _kdc_fix_time(&b->till);
1517 t = *b->till;
1519 /* be careful not overflowing */
1521 if(client->entry.max_life)
1522 t = start + min(t - start, *client->entry.max_life);
1523 if(server->entry.max_life)
1524 t = start + min(t - start, *server->entry.max_life);
1525 #if 0
1526 t = min(t, start + realm->max_life);
1527 #endif
1528 et.endtime = t;
1529 if(f.renewable_ok && et.endtime < *b->till){
1530 f.renewable = 1;
1531 if(b->rtime == NULL){
1532 ALLOC(b->rtime);
1533 *b->rtime = 0;
1535 if(*b->rtime < *b->till)
1536 *b->rtime = *b->till;
1538 if(f.renewable && b->rtime){
1539 t = *b->rtime;
1540 if(t == 0)
1541 t = MAX_TIME;
1542 if(client->entry.max_renew)
1543 t = start + min(t - start, *client->entry.max_renew);
1544 if(server->entry.max_renew)
1545 t = start + min(t - start, *server->entry.max_renew);
1546 #if 0
1547 t = min(t, start + realm->max_renew);
1548 #endif
1549 ALLOC(et.renew_till);
1550 *et.renew_till = t;
1551 et.flags.renewable = 1;
1555 if (f.request_anonymous)
1556 et.flags.anonymous = 1;
1558 if(b->addresses){
1559 ALLOC(et.caddr);
1560 copy_HostAddresses(b->addresses, et.caddr);
1563 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1564 krb5_data_zero(&et.transited.contents);
1566 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1567 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1568 * incapable of correctly decoding SEQUENCE OF's of zero length.
1570 * To fix this, always send at least one no-op last_req
1572 * If there's a pw_end or valid_end we will use that,
1573 * otherwise just a dummy lr.
1575 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1576 if (ek.last_req.val == NULL) {
1577 ret = ENOMEM;
1578 goto out;
1580 ek.last_req.len = 0;
1581 if (client->entry.pw_end
1582 && (config->kdc_warn_pwexpire == 0
1583 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1584 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1585 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1586 ++ek.last_req.len;
1588 if (client->entry.valid_end) {
1589 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1590 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1591 ++ek.last_req.len;
1593 if (ek.last_req.len == 0) {
1594 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1595 ek.last_req.val[ek.last_req.len].lr_value = 0;
1596 ++ek.last_req.len;
1598 ek.nonce = b->nonce;
1599 if (client->entry.valid_end || client->entry.pw_end) {
1600 ALLOC(ek.key_expiration);
1601 if (client->entry.valid_end) {
1602 if (client->entry.pw_end)
1603 *ek.key_expiration = min(*client->entry.valid_end,
1604 *client->entry.pw_end);
1605 else
1606 *ek.key_expiration = *client->entry.valid_end;
1607 } else
1608 *ek.key_expiration = *client->entry.pw_end;
1609 } else
1610 ek.key_expiration = NULL;
1611 ek.flags = et.flags;
1612 ek.authtime = et.authtime;
1613 if (et.starttime) {
1614 ALLOC(ek.starttime);
1615 *ek.starttime = *et.starttime;
1617 ek.endtime = et.endtime;
1618 if (et.renew_till) {
1619 ALLOC(ek.renew_till);
1620 *ek.renew_till = *et.renew_till;
1622 copy_Realm(&rep.ticket.realm, &ek.srealm);
1623 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1624 if(et.caddr){
1625 ALLOC(ek.caddr);
1626 copy_HostAddresses(et.caddr, ek.caddr);
1629 #if PKINIT
1630 if (pkp) {
1631 e_text = "Failed to build PK-INIT reply";
1632 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1633 sessionetype, req, req_buffer,
1634 &reply_key, &et.key, rep.padata);
1635 if (ret)
1636 goto out;
1637 ret = _kdc_add_inital_verified_cas(context,
1638 config,
1639 pkp,
1640 &et);
1641 if (ret)
1642 goto out;
1644 } else
1645 #endif
1647 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1648 if (ret)
1649 goto out;
1652 if (reply_key == NULL) {
1653 e_text = "Client have no reply key";
1654 ret = KRB5KDC_ERR_CLIENT_NOTYET;
1655 goto out;
1658 ret = copy_EncryptionKey(&et.key, &ek.key);
1659 if (ret)
1660 goto out;
1662 /* Add signing of alias referral */
1663 if (f.canonicalize) {
1664 PA_ClientCanonicalized canon;
1665 krb5_data data;
1666 PA_DATA pa;
1667 krb5_crypto crypto;
1668 size_t len;
1670 memset(&canon, 0, sizeof(canon));
1672 canon.names.requested_name = *b->cname;
1673 canon.names.mapped_name = client->entry.principal->name;
1675 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
1676 &canon.names, &len, ret);
1677 if (ret)
1678 goto out;
1679 if (data.length != len)
1680 krb5_abortx(context, "internal asn.1 error");
1682 /* sign using "returned session key" */
1683 ret = krb5_crypto_init(context, &et.key, 0, &crypto);
1684 if (ret) {
1685 free(data.data);
1686 goto out;
1689 ret = krb5_create_checksum(context, crypto,
1690 KRB5_KU_CANONICALIZED_NAMES, 0,
1691 data.data, data.length,
1692 &canon.canon_checksum);
1693 free(data.data);
1694 krb5_crypto_destroy(context, crypto);
1695 if (ret)
1696 goto out;
1698 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
1699 &canon, &len, ret);
1700 free_Checksum(&canon.canon_checksum);
1701 if (ret)
1702 goto out;
1703 if (data.length != len)
1704 krb5_abortx(context, "internal asn.1 error");
1706 pa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
1707 pa.padata_value = data;
1708 ret = add_METHOD_DATA(rep.padata, &pa);
1709 free(data.data);
1710 if (ret)
1711 goto out;
1714 if (rep.padata->len == 0) {
1715 free(rep.padata);
1716 rep.padata = NULL;
1719 /* Add the PAC */
1720 if (send_pac_p(context, req)) {
1721 krb5_pac p = NULL;
1722 krb5_data data;
1724 ret = _kdc_pac_generate(context, client, &p);
1725 if (ret) {
1726 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1727 client_name);
1728 goto out;
1730 if (p != NULL) {
1731 ret = _krb5_pac_sign(context, p, et.authtime,
1732 client->entry.principal,
1733 &skey->key, /* Server key */
1734 &skey->key, /* FIXME: should be krbtgt key */
1735 &data);
1736 krb5_pac_free(context, p);
1737 if (ret) {
1738 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1739 client_name);
1740 goto out;
1743 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
1744 KRB5_AUTHDATA_WIN2K_PAC,
1745 &data);
1746 krb5_data_free(&data);
1747 if (ret)
1748 goto out;
1752 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1753 et.endtime, et.renew_till);
1755 /* do this as the last thing since this signs the EncTicketPart */
1756 ret = _kdc_add_KRB5SignedPath(context,
1757 config,
1758 server,
1759 setype,
1760 client->entry.principal,
1761 NULL,
1762 NULL,
1763 &et);
1764 if (ret)
1765 goto out;
1767 log_as_req(context, config, reply_key->keytype, setype, b);
1769 ret = _kdc_encode_reply(context, config,
1770 &rep, &et, &ek, setype, server->entry.kvno,
1771 &skey->key, client->entry.kvno,
1772 reply_key, 0, &e_text, reply);
1773 free_EncTicketPart(&et);
1774 free_EncKDCRepPart(&ek);
1775 if (ret)
1776 goto out;
1778 /* */
1779 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1780 krb5_data_free(reply);
1781 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1782 e_text = "Reply packet too large";
1785 out:
1786 free_AS_REP(&rep);
1787 if(ret != 0 && ret != HDB_ERR_NOT_FOUND_HERE){
1788 krb5_mk_error(context,
1789 ret,
1790 e_text,
1791 (e_data.data ? &e_data : NULL),
1792 client_princ,
1793 server_princ,
1794 NULL,
1795 NULL,
1796 reply);
1797 ret = 0;
1799 #ifdef PKINIT
1800 if (pkp)
1801 _kdc_pk_free_client_param(context, pkp);
1802 #endif
1803 if (e_data.data)
1804 free(e_data.data);
1805 if (client_princ)
1806 krb5_free_principal(context, client_princ);
1807 free(client_name);
1808 if (server_princ)
1809 krb5_free_principal(context, server_princ);
1810 free(server_name);
1811 if(client)
1812 _kdc_free_ent(context, client);
1813 if(server)
1814 _kdc_free_ent(context, server);
1815 return ret;
1819 * Add the AuthorizationData `data´ of `type´ to the last element in
1820 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1823 krb5_error_code
1824 _kdc_tkt_add_if_relevant_ad(krb5_context context,
1825 EncTicketPart *tkt,
1826 int type,
1827 const krb5_data *data)
1829 krb5_error_code ret;
1830 size_t size;
1832 if (tkt->authorization_data == NULL) {
1833 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
1834 if (tkt->authorization_data == NULL) {
1835 krb5_set_error_message(context, ENOMEM, "out of memory");
1836 return ENOMEM;
1840 /* add the entry to the last element */
1842 AuthorizationData ad = { 0, NULL };
1843 AuthorizationDataElement ade;
1845 ade.ad_type = type;
1846 ade.ad_data = *data;
1848 ret = add_AuthorizationData(&ad, &ade);
1849 if (ret) {
1850 krb5_set_error_message(context, ret, "add AuthorizationData failed");
1851 return ret;
1854 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
1856 ASN1_MALLOC_ENCODE(AuthorizationData,
1857 ade.ad_data.data, ade.ad_data.length,
1858 &ad, &size, ret);
1859 free_AuthorizationData(&ad);
1860 if (ret) {
1861 krb5_set_error_message(context, ret, "ASN.1 encode of "
1862 "AuthorizationData failed");
1863 return ret;
1865 if (ade.ad_data.length != size)
1866 krb5_abortx(context, "internal asn.1 encoder error");
1868 ret = add_AuthorizationData(tkt->authorization_data, &ade);
1869 der_free_octet_string(&ade.ad_data);
1870 if (ret) {
1871 krb5_set_error_message(context, ret, "add AuthorizationData failed");
1872 return ret;
1876 return 0;