gss_accept_sec_context disappeared from exports
[heimdal.git] / kdc / kerberos5.c
blob1d97a60ae4031b98ec4036b556225976fe95bc12
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, const hdb_entry_ex *princ,
127 krb5_enctype *etypes, unsigned len,
128 Key **ret_key)
130 size_t 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 size_t 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 size_t 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 size_t 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;
1003 ret = _kdc_db_fetch(context, config, server_princ,
1004 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT | flags,
1005 NULL, NULL, &server);
1006 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1007 kdc_log(context, config, 5, "target %s does not have secrets at this KDC, need to proxy", server_name);
1008 goto out;
1009 } else if(ret){
1010 const char *msg = krb5_get_error_message(context, ret);
1011 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name, msg);
1012 krb5_free_error_message(context, msg);
1013 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1014 goto out;
1017 memset(&et, 0, sizeof(et));
1018 memset(&ek, 0, sizeof(ek));
1021 * Select a session enctype from the list of the crypto systems
1022 * supported enctype, is supported by the client and is one of the
1023 * enctype of the enctype of the krbtgt.
1025 * The later is used as a hint what enctype all KDC are supporting
1026 * to make sure a newer version of KDC wont generate a session
1027 * enctype that and older version of a KDC in the same realm can't
1028 * decrypt.
1030 * But if the KDC admin is paranoid and doesn't want to have "no
1031 * the best" enctypes on the krbtgt, lets save the best pick from
1032 * the client list and hope that that will work for any other
1033 * KDCs.
1036 const krb5_enctype *p;
1037 krb5_enctype clientbest = ETYPE_NULL;
1038 size_t i, j;
1040 p = krb5_kerberos_enctypes(context);
1042 sessionetype = ETYPE_NULL;
1044 for (i = 0; p[i] != ETYPE_NULL && sessionetype == ETYPE_NULL; i++) {
1045 if (krb5_enctype_valid(context, p[i]) != 0)
1046 continue;
1048 for (j = 0; j < b->etype.len && sessionetype == ETYPE_NULL; j++) {
1049 Key *dummy;
1050 /* check with client */
1051 if (p[i] != b->etype.val[j])
1052 continue;
1053 /* save best of union of { client, crypto system } */
1054 if (clientbest == ETYPE_NULL)
1055 clientbest = p[i];
1056 /* check with krbtgt */
1057 ret = hdb_enctype2key(context, &server->entry, p[i], &dummy);
1058 if (ret)
1059 continue;
1060 sessionetype = p[i];
1063 /* if krbtgt had no shared keys with client, pick clients best */
1064 if (clientbest != ETYPE_NULL && sessionetype == ETYPE_NULL) {
1065 sessionetype = clientbest;
1066 } else if (sessionetype == ETYPE_NULL) {
1067 kdc_log(context, config, 0,
1068 "Client (%s) from %s has no common enctypes with KDC"
1069 "to use for the session key",
1070 client_name, from);
1071 goto out;
1076 * Pre-auth processing
1079 if(req->padata){
1080 int i;
1081 const PA_DATA *pa;
1082 int found_pa = 0;
1084 log_patypes(context, config, req->padata);
1086 #ifdef PKINIT
1087 kdc_log(context, config, 5,
1088 "Looking for PKINIT pa-data -- %s", client_name);
1090 e_text = "No PKINIT PA found";
1092 i = 0;
1093 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ);
1094 if (pa == NULL) {
1095 i = 0;
1096 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN);
1098 if (pa) {
1099 char *client_cert = NULL;
1101 ret = _kdc_pk_rd_padata(context, config, req, pa, client, &pkp);
1102 if (ret) {
1103 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1104 kdc_log(context, config, 5,
1105 "Failed to decode PKINIT PA-DATA -- %s",
1106 client_name);
1107 goto ts_enc;
1109 if (ret == 0 && pkp == NULL)
1110 goto ts_enc;
1112 ret = _kdc_pk_check_client(context,
1113 config,
1114 clientdb,
1115 client,
1116 pkp,
1117 &client_cert);
1118 if (ret) {
1119 e_text = "PKINIT certificate not allowed to "
1120 "impersonate principal";
1121 _kdc_pk_free_client_param(context, pkp);
1123 kdc_log(context, config, 0, "%s", e_text);
1124 pkp = NULL;
1125 goto out;
1128 found_pa = 1;
1129 et.flags.pre_authent = 1;
1130 kdc_log(context, config, 0,
1131 "PKINIT pre-authentication succeeded -- %s using %s",
1132 client_name, client_cert);
1133 free(client_cert);
1134 if (pkp)
1135 goto preauth_done;
1137 ts_enc:
1138 #endif
1139 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1140 client_name);
1142 i = 0;
1143 e_text = "No ENC-TS found";
1144 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1145 krb5_data ts_data;
1146 PA_ENC_TS_ENC p;
1147 size_t len;
1148 EncryptedData enc_data;
1149 Key *pa_key;
1150 char *str;
1152 found_pa = 1;
1154 if (b->kdc_options.request_anonymous) {
1155 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1156 kdc_log(context, config, 0, "ENC-TS doesn't support anon");
1157 goto out;
1160 ret = decode_EncryptedData(pa->padata_value.data,
1161 pa->padata_value.length,
1162 &enc_data,
1163 &len);
1164 if (ret) {
1165 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1166 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1167 client_name);
1168 goto out;
1171 ret = hdb_enctype2key(context, &client->entry,
1172 enc_data.etype, &pa_key);
1173 if(ret){
1174 char *estr;
1175 e_text = "No key matches pa-data";
1176 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1177 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1178 estr = NULL;
1179 if(estr == NULL)
1180 kdc_log(context, config, 5,
1181 "No client key matching pa-data (%d) -- %s",
1182 enc_data.etype, client_name);
1183 else
1184 kdc_log(context, config, 5,
1185 "No client key matching pa-data (%s) -- %s",
1186 estr, client_name);
1187 free(estr);
1188 free_EncryptedData(&enc_data);
1190 continue;
1193 try_next_key:
1194 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1195 if (ret) {
1196 const char *msg = krb5_get_error_message(context, ret);
1197 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
1198 krb5_free_error_message(context, msg);
1199 free_EncryptedData(&enc_data);
1200 continue;
1203 ret = krb5_decrypt_EncryptedData (context,
1204 crypto,
1205 KRB5_KU_PA_ENC_TIMESTAMP,
1206 &enc_data,
1207 &ts_data);
1208 krb5_crypto_destroy(context, crypto);
1210 * Since the user might have several keys with the same
1211 * enctype but with diffrent salting, we need to try all
1212 * the keys with the same enctype.
1214 if(ret){
1215 krb5_error_code ret2;
1216 const char *msg = krb5_get_error_message(context, ret);
1218 ret2 = krb5_enctype_to_string(context,
1219 pa_key->key.keytype, &str);
1220 if (ret2)
1221 str = NULL;
1222 kdc_log(context, config, 5,
1223 "Failed to decrypt PA-DATA -- %s "
1224 "(enctype %s) error %s",
1225 client_name, str ? str : "unknown enctype", msg);
1226 krb5_free_error_message(context, msg);
1227 free(str);
1229 if(hdb_next_enctype2key(context, &client->entry,
1230 enc_data.etype, &pa_key) == 0)
1231 goto try_next_key;
1232 e_text = "Failed to decrypt PA-DATA";
1234 free_EncryptedData(&enc_data);
1236 if (clientdb->hdb_auth_status)
1237 (clientdb->hdb_auth_status)(context, clientdb, client, HDB_AUTH_WRONG_PASSWORD);
1239 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1240 continue;
1242 free_EncryptedData(&enc_data);
1243 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1244 ts_data.length,
1246 &len);
1247 krb5_data_free(&ts_data);
1248 if(ret){
1249 e_text = "Failed to decode PA-ENC-TS-ENC";
1250 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1251 kdc_log(context, config,
1252 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1253 client_name);
1254 continue;
1256 free_PA_ENC_TS_ENC(&p);
1257 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1258 char client_time[100];
1260 krb5_format_time(context, p.patimestamp,
1261 client_time, sizeof(client_time), TRUE);
1263 ret = KRB5KRB_AP_ERR_SKEW;
1264 kdc_log(context, config, 0,
1265 "Too large time skew, "
1266 "client time %s is out by %u > %u seconds -- %s",
1267 client_time,
1268 (unsigned)abs(kdc_time - p.patimestamp),
1269 context->max_skew,
1270 client_name);
1273 * The following is needed to make windows clients to
1274 * retry using the timestamp in the error message, if
1275 * there is a e_text, they become unhappy.
1277 e_text = NULL;
1278 goto out;
1280 et.flags.pre_authent = 1;
1282 set_salt_padata(rep.padata, pa_key->salt);
1284 reply_key = &pa_key->key;
1286 ret = krb5_enctype_to_string(context, pa_key->key.keytype, &str);
1287 if (ret)
1288 str = NULL;
1290 kdc_log(context, config, 2,
1291 "ENC-TS Pre-authentication succeeded -- %s using %s",
1292 client_name, str ? str : "unknown enctype");
1293 free(str);
1294 break;
1296 #ifdef PKINIT
1297 preauth_done:
1298 #endif
1299 if(found_pa == 0 && config->require_preauth)
1300 goto use_pa;
1301 /* We come here if we found a pa-enc-timestamp, but if there
1302 was some problem with it, other than too large skew */
1303 if(found_pa && et.flags.pre_authent == 0){
1304 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1305 e_text = NULL;
1306 goto out;
1308 }else if (config->require_preauth
1309 || b->kdc_options.request_anonymous /* hack to force anon */
1310 || client->entry.flags.require_preauth
1311 || server->entry.flags.require_preauth) {
1312 METHOD_DATA method_data;
1313 PA_DATA *pa;
1314 unsigned char *buf;
1315 size_t len;
1317 use_pa:
1318 method_data.len = 0;
1319 method_data.val = NULL;
1321 ret = realloc_method_data(&method_data);
1322 if (ret) {
1323 free_METHOD_DATA(&method_data);
1324 goto out;
1326 pa = &method_data.val[method_data.len-1];
1327 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1328 pa->padata_value.length = 0;
1329 pa->padata_value.data = NULL;
1331 #ifdef PKINIT
1332 ret = realloc_method_data(&method_data);
1333 if (ret) {
1334 free_METHOD_DATA(&method_data);
1335 goto out;
1337 pa = &method_data.val[method_data.len-1];
1338 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1339 pa->padata_value.length = 0;
1340 pa->padata_value.data = NULL;
1342 ret = realloc_method_data(&method_data);
1343 if (ret) {
1344 free_METHOD_DATA(&method_data);
1345 goto out;
1347 pa = &method_data.val[method_data.len-1];
1348 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1349 pa->padata_value.length = 0;
1350 pa->padata_value.data = NULL;
1351 #endif
1354 * If there is a client key, send ETYPE_INFO{,2}
1356 ret = _kdc_find_etype(context, client, b->etype.val, b->etype.len,
1357 &ckey);
1358 if (ret == 0) {
1361 * RFC4120 requires:
1362 * - If the client only knows about old enctypes, then send
1363 * both info replies (we send 'info' first in the list).
1364 * - If the client is 'modern', because it knows about 'new'
1365 * enctype types, then only send the 'info2' reply.
1367 * Before we send the full list of etype-info data, we pick
1368 * the client key we would have used anyway below, just pick
1369 * that instead.
1372 if (older_enctype(ckey->key.keytype)) {
1373 ret = get_pa_etype_info(context, config,
1374 &method_data, ckey);
1375 if (ret) {
1376 free_METHOD_DATA(&method_data);
1377 goto out;
1380 ret = get_pa_etype_info2(context, config,
1381 &method_data, ckey);
1382 if (ret) {
1383 free_METHOD_DATA(&method_data);
1384 goto out;
1388 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1389 free_METHOD_DATA(&method_data);
1391 e_data.data = buf;
1392 e_data.length = len;
1393 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1395 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1397 kdc_log(context, config, 0,
1398 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1399 client_name);
1400 goto out;
1403 if (clientdb->hdb_auth_status)
1404 (clientdb->hdb_auth_status)(context, clientdb, client,
1405 HDB_AUTH_SUCCESS);
1408 * Verify flags after the user been required to prove its identity
1409 * with in a preauth mech.
1412 ret = _kdc_check_access(context, config, client, client_name,
1413 server, server_name,
1414 req, &e_data);
1415 if(ret)
1416 goto out;
1419 * Selelct the best encryption type for the KDC with out regard to
1420 * the client since the client never needs to read that data.
1423 ret = _kdc_get_preferred_key(context, config,
1424 server, server_name,
1425 &setype, &skey);
1426 if(ret)
1427 goto out;
1429 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1430 || (f.request_anonymous && !config->allow_anonymous)) {
1431 ret = KRB5KDC_ERR_BADOPTION;
1432 e_text = "Bad KDC options";
1433 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1434 goto out;
1437 rep.pvno = 5;
1438 rep.msg_type = krb_as_rep;
1440 ret = copy_Realm(&client->entry.principal->realm, &rep.crealm);
1441 if (ret)
1442 goto out;
1443 ret = _krb5_principal2principalname(&rep.cname, client->entry.principal);
1444 if (ret)
1445 goto out;
1447 rep.ticket.tkt_vno = 5;
1448 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1449 _krb5_principal2principalname(&rep.ticket.sname,
1450 server->entry.principal);
1451 /* java 1.6 expects the name to be the same type, lets allow that
1452 * uncomplicated name-types. */
1453 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1454 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1455 rep.ticket.sname.name_type = b->sname->name_type;
1456 #undef CNT
1458 et.flags.initial = 1;
1459 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1460 et.flags.forwardable = f.forwardable;
1461 else if (f.forwardable) {
1462 e_text = "Ticket may not be forwardable";
1463 ret = KRB5KDC_ERR_POLICY;
1464 kdc_log(context, config, 0,
1465 "Ticket may not be forwardable -- %s", client_name);
1466 goto out;
1468 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1469 et.flags.proxiable = f.proxiable;
1470 else if (f.proxiable) {
1471 e_text = "Ticket may not be proxiable";
1472 ret = KRB5KDC_ERR_POLICY;
1473 kdc_log(context, config, 0,
1474 "Ticket may not be proxiable -- %s", client_name);
1475 goto out;
1477 if(client->entry.flags.postdate && server->entry.flags.postdate)
1478 et.flags.may_postdate = f.allow_postdate;
1479 else if (f.allow_postdate){
1480 e_text = "Ticket may not be postdate";
1481 ret = KRB5KDC_ERR_POLICY;
1482 kdc_log(context, config, 0,
1483 "Ticket may not be postdatable -- %s", client_name);
1484 goto out;
1487 /* check for valid set of addresses */
1488 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1489 e_text = "Bad address list in requested";
1490 ret = KRB5KRB_AP_ERR_BADADDR;
1491 kdc_log(context, config, 0,
1492 "Bad address list requested -- %s", client_name);
1493 goto out;
1496 ret = copy_PrincipalName(&rep.cname, &et.cname);
1497 if (ret)
1498 goto out;
1499 ret = copy_Realm(&rep.crealm, &et.crealm);
1500 if (ret)
1501 goto out;
1504 time_t start;
1505 time_t t;
1507 start = et.authtime = kdc_time;
1509 if(f.postdated && req->req_body.from){
1510 ALLOC(et.starttime);
1511 start = *et.starttime = *req->req_body.from;
1512 et.flags.invalid = 1;
1513 et.flags.postdated = 1; /* XXX ??? */
1515 _kdc_fix_time(&b->till);
1516 t = *b->till;
1518 /* be careful not overflowing */
1520 if(client->entry.max_life)
1521 t = start + min(t - start, *client->entry.max_life);
1522 if(server->entry.max_life)
1523 t = start + min(t - start, *server->entry.max_life);
1524 #if 0
1525 t = min(t, start + realm->max_life);
1526 #endif
1527 et.endtime = t;
1528 if(f.renewable_ok && et.endtime < *b->till){
1529 f.renewable = 1;
1530 if(b->rtime == NULL){
1531 ALLOC(b->rtime);
1532 *b->rtime = 0;
1534 if(*b->rtime < *b->till)
1535 *b->rtime = *b->till;
1537 if(f.renewable && b->rtime){
1538 t = *b->rtime;
1539 if(t == 0)
1540 t = MAX_TIME;
1541 if(client->entry.max_renew)
1542 t = start + min(t - start, *client->entry.max_renew);
1543 if(server->entry.max_renew)
1544 t = start + min(t - start, *server->entry.max_renew);
1545 #if 0
1546 t = min(t, start + realm->max_renew);
1547 #endif
1548 ALLOC(et.renew_till);
1549 *et.renew_till = t;
1550 et.flags.renewable = 1;
1554 if (f.request_anonymous)
1555 et.flags.anonymous = 1;
1557 if(b->addresses){
1558 ALLOC(et.caddr);
1559 copy_HostAddresses(b->addresses, et.caddr);
1562 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1563 krb5_data_zero(&et.transited.contents);
1565 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1566 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1567 * incapable of correctly decoding SEQUENCE OF's of zero length.
1569 * To fix this, always send at least one no-op last_req
1571 * If there's a pw_end or valid_end we will use that,
1572 * otherwise just a dummy lr.
1574 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1575 if (ek.last_req.val == NULL) {
1576 ret = ENOMEM;
1577 goto out;
1579 ek.last_req.len = 0;
1580 if (client->entry.pw_end
1581 && (config->kdc_warn_pwexpire == 0
1582 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1583 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1584 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1585 ++ek.last_req.len;
1587 if (client->entry.valid_end) {
1588 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1589 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1590 ++ek.last_req.len;
1592 if (ek.last_req.len == 0) {
1593 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1594 ek.last_req.val[ek.last_req.len].lr_value = 0;
1595 ++ek.last_req.len;
1597 ek.nonce = b->nonce;
1598 if (client->entry.valid_end || client->entry.pw_end) {
1599 ALLOC(ek.key_expiration);
1600 if (client->entry.valid_end) {
1601 if (client->entry.pw_end)
1602 *ek.key_expiration = min(*client->entry.valid_end,
1603 *client->entry.pw_end);
1604 else
1605 *ek.key_expiration = *client->entry.valid_end;
1606 } else
1607 *ek.key_expiration = *client->entry.pw_end;
1608 } else
1609 ek.key_expiration = NULL;
1610 ek.flags = et.flags;
1611 ek.authtime = et.authtime;
1612 if (et.starttime) {
1613 ALLOC(ek.starttime);
1614 *ek.starttime = *et.starttime;
1616 ek.endtime = et.endtime;
1617 if (et.renew_till) {
1618 ALLOC(ek.renew_till);
1619 *ek.renew_till = *et.renew_till;
1621 copy_Realm(&rep.ticket.realm, &ek.srealm);
1622 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1623 if(et.caddr){
1624 ALLOC(ek.caddr);
1625 copy_HostAddresses(et.caddr, ek.caddr);
1628 #if PKINIT
1629 if (pkp) {
1630 e_text = "Failed to build PK-INIT reply";
1631 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1632 sessionetype, req, req_buffer,
1633 &reply_key, &et.key, rep.padata);
1634 if (ret)
1635 goto out;
1636 ret = _kdc_add_inital_verified_cas(context,
1637 config,
1638 pkp,
1639 &et);
1640 if (ret)
1641 goto out;
1643 } else
1644 #endif
1646 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1647 if (ret)
1648 goto out;
1651 if (reply_key == NULL) {
1652 e_text = "Client have no reply key";
1653 ret = KRB5KDC_ERR_CLIENT_NOTYET;
1654 goto out;
1657 ret = copy_EncryptionKey(&et.key, &ek.key);
1658 if (ret)
1659 goto out;
1661 /* Add signing of alias referral */
1662 if (f.canonicalize) {
1663 PA_ClientCanonicalized canon;
1664 krb5_data data;
1665 PA_DATA pa;
1666 krb5_crypto cryptox;
1667 size_t len;
1669 memset(&canon, 0, sizeof(canon));
1671 canon.names.requested_name = *b->cname;
1672 canon.names.mapped_name = client->entry.principal->name;
1674 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
1675 &canon.names, &len, ret);
1676 if (ret)
1677 goto out;
1678 if (data.length != len)
1679 krb5_abortx(context, "internal asn.1 error");
1681 /* sign using "returned session key" */
1682 ret = krb5_crypto_init(context, &et.key, 0, &cryptox);
1683 if (ret) {
1684 free(data.data);
1685 goto out;
1688 ret = krb5_create_checksum(context, cryptox,
1689 KRB5_KU_CANONICALIZED_NAMES, 0,
1690 data.data, data.length,
1691 &canon.canon_checksum);
1692 free(data.data);
1693 krb5_crypto_destroy(context, cryptox);
1694 if (ret)
1695 goto out;
1697 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
1698 &canon, &len, ret);
1699 free_Checksum(&canon.canon_checksum);
1700 if (ret)
1701 goto out;
1702 if (data.length != len)
1703 krb5_abortx(context, "internal asn.1 error");
1705 pa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
1706 pa.padata_value = data;
1707 ret = add_METHOD_DATA(rep.padata, &pa);
1708 free(data.data);
1709 if (ret)
1710 goto out;
1713 if (rep.padata->len == 0) {
1714 free(rep.padata);
1715 rep.padata = NULL;
1718 /* Add the PAC */
1719 if (send_pac_p(context, req)) {
1720 krb5_pac p = NULL;
1721 krb5_data data;
1723 ret = _kdc_pac_generate(context, client, &p);
1724 if (ret) {
1725 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1726 client_name);
1727 goto out;
1729 if (p != NULL) {
1730 ret = _krb5_pac_sign(context, p, et.authtime,
1731 client->entry.principal,
1732 &skey->key, /* Server key */
1733 &skey->key, /* FIXME: should be krbtgt key */
1734 &data);
1735 krb5_pac_free(context, p);
1736 if (ret) {
1737 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1738 client_name);
1739 goto out;
1742 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
1743 KRB5_AUTHDATA_WIN2K_PAC,
1744 &data);
1745 krb5_data_free(&data);
1746 if (ret)
1747 goto out;
1751 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1752 et.endtime, et.renew_till);
1754 /* do this as the last thing since this signs the EncTicketPart */
1755 ret = _kdc_add_KRB5SignedPath(context,
1756 config,
1757 server,
1758 setype,
1759 client->entry.principal,
1760 NULL,
1761 NULL,
1762 &et);
1763 if (ret)
1764 goto out;
1766 log_as_req(context, config, reply_key->keytype, setype, b);
1768 ret = _kdc_encode_reply(context, config,
1769 &rep, &et, &ek, setype, server->entry.kvno,
1770 &skey->key, client->entry.kvno,
1771 reply_key, 0, &e_text, reply);
1772 free_EncTicketPart(&et);
1773 free_EncKDCRepPart(&ek);
1774 if (ret)
1775 goto out;
1777 /* */
1778 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1779 krb5_data_free(reply);
1780 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1781 e_text = "Reply packet too large";
1784 out:
1785 free_AS_REP(&rep);
1786 if(ret != 0 && ret != HDB_ERR_NOT_FOUND_HERE){
1787 krb5_mk_error(context,
1788 ret,
1789 e_text,
1790 (e_data.data ? &e_data : NULL),
1791 client_princ,
1792 server_princ,
1793 NULL,
1794 NULL,
1795 reply);
1796 ret = 0;
1798 #ifdef PKINIT
1799 if (pkp)
1800 _kdc_pk_free_client_param(context, pkp);
1801 #endif
1802 if (e_data.data)
1803 free(e_data.data);
1804 if (client_princ)
1805 krb5_free_principal(context, client_princ);
1806 free(client_name);
1807 if (server_princ)
1808 krb5_free_principal(context, server_princ);
1809 free(server_name);
1810 if(client)
1811 _kdc_free_ent(context, client);
1812 if(server)
1813 _kdc_free_ent(context, server);
1814 return ret;
1818 * Add the AuthorizationData `data´ of `type´ to the last element in
1819 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1822 krb5_error_code
1823 _kdc_tkt_add_if_relevant_ad(krb5_context context,
1824 EncTicketPart *tkt,
1825 int type,
1826 const krb5_data *data)
1828 krb5_error_code ret;
1829 size_t size;
1831 if (tkt->authorization_data == NULL) {
1832 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
1833 if (tkt->authorization_data == NULL) {
1834 krb5_set_error_message(context, ENOMEM, "out of memory");
1835 return ENOMEM;
1839 /* add the entry to the last element */
1841 AuthorizationData ad = { 0, NULL };
1842 AuthorizationDataElement ade;
1844 ade.ad_type = type;
1845 ade.ad_data = *data;
1847 ret = add_AuthorizationData(&ad, &ade);
1848 if (ret) {
1849 krb5_set_error_message(context, ret, "add AuthorizationData failed");
1850 return ret;
1853 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
1855 ASN1_MALLOC_ENCODE(AuthorizationData,
1856 ade.ad_data.data, ade.ad_data.length,
1857 &ad, &size, ret);
1858 free_AuthorizationData(&ad);
1859 if (ret) {
1860 krb5_set_error_message(context, ret, "ASN.1 encode of "
1861 "AuthorizationData failed");
1862 return ret;
1864 if (ade.ad_data.length != size)
1865 krb5_abortx(context, "internal asn.1 encoder error");
1867 ret = add_AuthorizationData(tkt->authorization_data, &ade);
1868 der_free_octet_string(&ade.ad_data);
1869 if (ret) {
1870 krb5_set_error_message(context, ret, "add AuthorizationData failed");
1871 return ret;
1875 return 0;